Title: CS 498 Lecture 6 Packet Management
1CS 498 Lecture 6Packet Management
- Jennifer Hou
- Department of Computer Science
- University of Illinois at Urbana-Champaign
- Reading Chapters 3-4, The Linux Networking
Architecture Design and Implementation of
Network Protocols in the Linux Kernel
2Outline
- Socket buffer sk_buff structure
- APIs for creating, releasing, and duplicating
socket buffers. - APIs for manipulating parameters within the
sk_buff structure - APIs for managing the socket buffer queue.
3Structure of sk_buff
sk_buff
sk_buff_head
next
sk_buff
prev
list
stamp
net_device
dev
h
nh
mac
Paketdatenbereich
dst
len
MAC-Header
...
IP-Header
head
UDP-Header
data
UDP-Data
tail
end
datarefp 1
4Socket Buffer
- The structure used to manage a packet during its
lifetime. - Created when an application passes data to a
socket or when a packet arrives at the network
adaptor (dev_alloc_skb() is invoked). - Packet headers of each layer are inserted in
front of the payload. - The packet is copied only twice once from the
user address space to the kernel address space,
and once when the packet is passed onto the
network adaptor.
5Change of sk_buff as a Packet Traverses Across
the Stack
sk_buff
sk_buff
sk_buff
next
next
next
prev
prev
prev
...
...
...
head
head
head
data
data
data
tail
tail
tail
end
end
end
Paketdatenbereich
Paketdatenbereich
Paketdatenbereich
IP-Header
UDP-Header
UDP-Header
UDP-Daten
UDP-Daten
UDP-Daten
datarefp 1
datarefp 1
datarefp 1
6Parameters of sk_buff Structure
- list points to the queue where the socket buffer
is currently located. - sk points to the socket that created the packet.
- stamp specifies the time when the packet arrived
in the Linux (in jiffies) - dev states the current network device on which
the socket buffer operates. Once the routing
decision is made, dev points to the network
adapter on which the packet leaves.
7Parameters of sk_buff Structure
- input_dev always points to the network device
that received the packet. - h, nh, mac are pointers to packet headers in the
transport layer (h), the network layer (nh), and
the MAC layer (mac). - dst a reference to the adapter on which the
packet leaves the computer, or a reference to a
MAC header stored in the hard header cache. - cloned indicates if a packet was cloned.
8Parameters of sk_buff Structure
- pkt_type specifies the type of a packet
- PACKET_HOST a packet sent to the local host
- PACKET_BROADCAST a broadcast packet
- PACKET_MULTICAST a multicast packet
- PACKET_OTHERHOSTa packet not destined for the
local host, but received in the promiscuous mode. - PACKET_OTGOING a packet leaving the host
- PACKET_LOOKBACK a packet sent by the local host
to itself.
9Parameters of sk_buff Structure
- len specifies the length of a packet.
- data, tail point to currently valid packet data.
- head, end point to the total location that can
be used for packet data. - The space between head and data is called
headroom, and the space between tail and end is
called tailroom - datarefp a referece counter that indicates how
many times the packet has been referenced. Not
part of the sk_buff structure
10Operations on Socket Buffers
- Create, release, and duplicate socket buffers.
- Manipulate parameters within the sk_buffer
structure - Manage socket buffer queues.
111. Creating and Releasing Socket Buffers
- alloc_skb(size,gfp_mask)
- No immediate attempt is made to allocate the
memory with kmalloc() for the sk_buff structure. - Tries to reuse a sk_buff in the skb_head_cache
queue if not successful, tries to obtain a
packet from the central socket-buffer cache
(skbuff_head_cache) with kmem_cache_alloc(). - If neither is successful, the invoke kmalloc() to
reserve memory. - The packet data space is reserved by use of
kmalloc().
121. Creating and Releasing Socket Buffers
- skb_copy(skb,gfp_mask) creates a copy of the
socket buffer skb, copying both the sk_buff
structure and the packet data. - skb_copy_expand(skb,newheadroom, newtailroom,
gfp_mask) creates a new copy of the socket
buffer and packet data, and in addition, reserves
a larger space before and after the packet data.
13Copying Socket Buffers
skb_copy
sk_buff
sk_buff
sk_buff
next
next
next
prev
prev
prev
...
...
...
head
head
head
data
data
data
tail
tail
tail
end
end
end
Paket dat storage
Paket data storage
Paket data storage
IP-Header
IP-Header
IP-Header
UDP-Header
UDP-Header
UDP-Header
UDP-Daten
UDP-Daten
UDP-Daten
datarefp 1
datarefp 1
datarefp 1
141. Creating and Releasing Socket Buffers
- skb_clone() creates a new socket buffer sk_buff,
but not the packet data. Pointers in both
sk_buffs point to the same packet data space. - Needed in multicast.
15Cloning Socket Buffers
skb_clone
sk_buff
sk_buff
sk_buff
next
next
next
prev
prev
prev
...
...
...
head
head
head
data
data
data
tail
tail
tail
end
end
end
Paketdatenbereich
Paketdatenbereich
IP-Header
IP-Header
UDP-Header
UDP-Header
UDP-Daten
UDP-Daten
datarefp 1
datarefp 2
161. Creating and Releasing Socket Buffers
- kfree_skb() calls kfree_skbmem() to release the
packet memory, and does all the sanity check
(e.g., calls the destructor() for the socket
buffer) - kfree_skbmem() if skb_cloned() is tested for
null, and datarefp is tested for one - invokes kfree() to release the packet data
memory. - Invokes skb_head_to_pool() to insert the
sk_buffer structure into the socket_buffer cache.
172. Manipulating Packet Data Space
- skb_unshare(skb) uses skb_cloned to check if the
socket buffer is available for exclusive use. If
not, then creates a copy of skb and returns the
pointer. - skb_put(skb,len) appends data to the end of the
packet increments the pointer tail and skb?len
by len need to ensure the tailroom is
sufficient. - skb_push(skb,len) inserts data in front of the
packet data space decrements the pointer data by
len, and increment skb?len by len need to check
the headroom size.
182. Manipulating Packet Data Space
- skb_pull(skb,len) truncates len bytes at the
beginning of a packet. - skb_tailroom(skb) returns the size of the
tailroom (in bytes). - skb_headroom(skb) returns the size of the
headroom (data-head) - skb_realloc_headroom(skb,newheadroom) creates a
new socket buffer with a headroom of size
newheadroom. - skb_reserve(skb,len) shifts the entire packet
data backward by len bytes.
193. Managing the Socket Buffer Queue
- The socket buffers are arranged in a
dual-concatenated ring structure. - struct sk_buff_head
- struct sk_buff next
- struct sk_buff prev
- __u32 qlen
- spinlock_t lock
-
Used for atomic execution of operations on the
queue under critical conditions.
20Packet Queue in the Linux Kernel
sk_buff_head
next
prev
qlen 3
sk_buff
sk_buff
sk_buff
next
next
next
prev
prev
prev
...
...
...
head
head
head
data
data
data
tail
tail
tail
end
end
end
Paketdaten
Paketdaten
Paketdaten
213. Managing the Socket Buffer Queue
- skb_queue_head_init(list) initializes an
skb_queue_head structure - skb_queue_empty(list) checks whether the queue
list is empty list?qlen is returned. - skb_queue_len(list) returns the actual length of
the queue. - skb_queue_head(list,skb) inserts the socket
buffer skb at the head of the queue and increment
list?qlen by one. - skb_queue_tail(list,skb) appends the socket
buffer skb to the end of the queue and increment
list?qlen by one.
223. Managing the Socket Buffer Queue
- skb_dequeue(list) removes the top packet from
the queue and returns the pointer to the packet. - skb_dequene_tail(list) removes the last packet
from the queue and returns the pointer to the
packet. - skb_queue_purge() empties the queue list all
packets are removed via kfree_skb(). - skb_insert(oldskb,newskb) inserts newskb in
front of oldskb in the queue of oldskb. - skb_append(oldskb,newskb) inserts newskb behind
oldskb in the queue of oldskb.
233. Managing the Socket Buffer Queue
- skb_unlink(skb) removes the socket buffer skb
from its queue and decrement the queue length. - skb_peek(list) returns a pointer to the first
element of a list, if this list is not empty
otherwise, returns NULL. - skb_peek_tail(list) returns a pointer to the
last element of a queue if the list is empty,
returns NULL.