Professional Documents
Culture Documents
Linux Kernel Networking: Raoul Rivas
Linux Kernel Networking: Raoul Rivas
Raoul Rivas
Kernel vs Application Programming
No memory protection
Sometimes no preemption
an hog the P!
oncurrency is di""icult
No li#raries
Print", "open
No security descriptors
&emory Protection
Segmentation 'ault
Preemption
Signals )ontrol*+
Li#raries
Security %escriptors
Locking
%e"erring Work
Packet reation
syscall)num#er,
arguments+
copy/to/user)+,
copy/"rom/user)+
Spinlocks
&utex
Semaphores
Reader*Writer Spinlocks
$nside Locking Primitives
Spinlock
//spinlock_lock:
disable_interrupts();
while(locked==true);
//critical region
//spinlock_unlock:
enable_interrupts();
locked=false;
&utex
//mutex_lock:
If (locked==true)
{
n!ueue(this);
"ield();
#
locked=true;
//critical region
//mutex_unlock:
If $ismpt%(wait!ueue)
{
wakeup(&e!ueue());
#
lse locked=false;
We can't sleep while the
spinlock is locked!
We can't use a mutex in
an interrupt because
interrupts can't sleep!
THE MUTEX SLEES THE S!"L#$% S!"S&&&
When to use what;
Mutex Spinlock
Short Lock .ime
Long Lock .ime
$nterrupt ontext
Sleeping
2xtensi#ility
Separate ompilation
Runtime*Linkage
LK& <-ello*World=
'define ()&*+
'define +I,*-
'define __./,+__
'include 0linux/module1h2
'include 0linux/kernel1h2
'include 0linux/init1h2
static int __init m%init(3oid)
{
printk(./,_4+/5 67ello8
world9n6);
/eturn :;
#
static 3oid __exit m%exit(3oid)
{
printk(./,_4+/5 6;oodb%e8
world9n6);
#
module_init(m%init);
module_exit(m%exit);
()&*+_+I<,=(6;>+6);
.he Kernel Loop
sys/socket)+Ksock/create
sys/socket
socket
$nteger
handler
Socket
create
-andler
ta#le
Protocol 'amilies
alls net/proto/"amiliy K
create "or "amiliy speci"ic
initili0ation
9p"
inet/create
net/proto/"amily
A'/L,AL
A'/!N$I
Socket Splice
Link*layer header
Received .imestamp
%evice we received it
'ragmentation is per"ormed
inside ip/"ragment
Route ache
'orwarding $n"ormation
Fase
Slow Routing
ip/"ragment
'$F
Slow routing
ip/route/output/"low
Route cache
"orward
dev/Lueue/xmit
)Lueue packet+
N :
N
N
N
:
:
:
ip/"orward
)packet "orwarding+
%ata Link Layer
.he dev/Lueue/xmit is
responsi#le o" enLueing
packets "or transmission in
the Ldisc o" the device
.he dev/hard/start/xmit is
responsi#le "or sending to
the device
%ev/Lueue/xmit)sk/#u"+
%ev Ldisc enLueue
dev/hard/start/xmit)+
%ev Ldisc deLueue
ase Study8 iN2.
We implemented it as a
Linux Kernel &odule
We implement a packet
scheduler at the Ldisc levelA
&inimi0e copying
Page remapping
Franch optimi0ation
%ata encapsulation
$nterrupt &oderationC&itigation
Packet &itigation
!se spinlock "or interrupt context and mutexes i" you plan to
sleep holding the lock