Path of a packet in the Linux kernel Vincent Guffens April 22, 2003

Contents 1 Introduction

1

2 From the physical medium to the network queue - Layer1 2.1 The sk buff network buffer by Alan Cox . . . . . . . . . . . . 2.2 Congestion collapse and NAPI . . . . . . . . . . . . . . . . .

2 3 5

3 From the network queue to the protocol handler - Layer2

6

4 Forwarding the packet - Layer3

7

5 Queueing the packet to the outgoing interface - Layer2

10

6 Appendix

10

A The OSI model

10

B The path of a packet figure

10

1

Introduction

This section is intended to give an overview of how an ip packet will traverse the Linux kernel. The Linux networking code is complex and giving an exhaustive description of the different sub-paths an exceptions that can occur is outside the scope of this text. The different layers refer to the well known Open Systems Interconnection (OSI) reference model that can be found in annex A. A schema representing a graphical representation of this document can be found in annex B. This document is based on Linux 2.4.17.

1

2

From the physical medium to the network queue - Layer1

Linux supports a wide number of interface types. Beside the typical Ethernet adapter, 10/100baseT or gigabit Ethernet, one can find some atm (in prealpha stage at the time of writing), isdn, hssi, fddi, wireless lan adaptors and others. Off course, each interface has its on way of receiving a frame. To fix the idea, let’s consider what happens in the case of an Ethernet adapter : The on-board memory is typically split into two regions used for receiving and sending frames. The 3c509, for instance, has a 4kB packet buffer split into 2kB Rx, 2kB Tx. A newer model, the 3c509B as 8kB on-board that can be split into 4/4 5/3 or 6/2 for Rx/Tx.[5]. The frame is stored into that memory region in a FIFO structure called rx-ring. Upon reception of a frame, the adapter will then issue an interrupt (irq) to inform the cpu of the event. An interrupt handler, registered during the open method of the device will then be run (See Chap. 9 and 14 of [7]). A new Linux network buffer (sk buff )1 is then allocated and the frame is copied into the new buffer. The frame can be transfered using programmed I/O (NE200, 3com509 ), shared memory (WD90x03, 3c503) or Direct Memory Access (DMA) and bus mastering (Lance, DEC21040). If using DMA, the sk buff can be pre-allocated an mapped to the DMA region. This optimization will reduce the cpu usage but will however not populate the cpu cache. An interface in Linux is represented by a structure called net device 2 . This structure holds a lot of information ranging from physical data to some layer 3 related pointers. Among other, you can find the name of the interface, the I/O memory, irq, information on the tx queue and some pointers to functions used to manipulate the device such as the initialization function, functions to manipulate the hardware header, to transmit a frame... The time spend to process the interrupt is critical and must be kept at a minimum. Until the irq is acknowledged by the cpu, the interruption mechanism is disable. Packets can accumulate in the rx ring ( which sometimes, can contain only 2 packets) and get dropped. User land processes won’t have access to the cpu anymore and the system will finally seems to hang. Once the frame has been transfered into the sk buff, the only action taken will be to queue the packet in the network buffer queue and return from the interrupt. This queue is refer to as sofnet an is unique for all the interfaces for a single cpu machine. More precisely, sofnet data[NR CPUS] 3 is an array of NR CPUS sofnet data structure, that is, one per cpu which 1

A Linux network buffer is a structure called sk buff declared in include/linux/skbuff.h. This structure is at the heart of the Linux networking code, see later in the text. 2 include/linux/netdevice.h 3 /include/linux/netdevice.h

2

permit to avoid serialization and locking issues ( At the expense of out of order packets4 ). The packets enter an leave this queue in a FIFO manner. Once the packet is queue, we can safely return from the interrupt after having warned the kernel that he will have to dequeue this packet sometimes later. The mechanism used for that purpose is called “software interrupt” or bottom half.[3] Although we really want to return as fast as possible from the interrupt, we also want to try to control the queue in case of congestion. As of 2.4.17, the congestion control is quite simple. We queue the buffers until the queue length reach netdev max backlog ( set to 300 ) at which point we throttle the queue until the queue length comes back to zero. We, off course, drop all the incoming packets during that time. A number of more elaborate control congestion methods are already used by some drivers and some enhancements have been suggested in [9]. We will discuss these methods in detail later in the text.

2.1

The sk buff network buffer by Alan Cox

The full version of this section can be found online at : http://www.linux.org.uk/Documents/buffers.html An sk buff is a control structure with a block of memory attached. There are two primary sets of functions provided in the sk buff library. Firstly routines to manipulate doubly linked lists of sk buffs, secondly functions for controlling the attached memory. The buffers are held on linked lists optimized for the common network operations of append to end and remove from start. As so much of the networking functionality occurs during interrupts these routines are written to be atomic. The small extra overhead this causes is well worth the pain it saves in bug hunting. We use the list operations to manage groups of packets as they arrive from the network, and as we send them to the physical interfaces. We use the memory manipulation routines for handling the contents of packets in a standardized and efficient manner. At its most basic a list of buffers is managed using functions like this: void append_frame(char *buf, int len) { struct sk_buff *skb=alloc_skb(len, GFP_ATOMIC); if(skb==NULL) my_dropped++; else { 4

The problem of out of order packets was however solved using irq affinity, see [9]

3

skb_put(skb,len); memcpy(skb->data,data,len); skb_append(&my_list, skb); } } void process_queue(void) { struct sk_buff *skb; while((skb=skb_dequeue(&my_list))!=NULL) { process_data(skb); kfree_skb(skb, FREE_READ); } }

These two fairly simplistic pieces of code actually capture quite closely the receive packet mechanism. If you consider append frame to be called from an interrupt by a device driver receiving a packet, and process frame as the code called to feed data into the protocols you can go and look in dev.c at netif rx() and net bh(). They are far more complex as they have to feed packets to the right protocol and manage flow control, but the basic operations are the same. This is just as true if you look at buffers going from the protocol to a user application. The example also shows the use of one of the data control functions skb put(). Here it is used to reserve space in the buffer for the data we wish to pass down. After alloc skb() obtains a buffer of len bytes, it consists of 0 bytes of room at the head of the buffer, 0 bytes of data, and len bytes of room at the end of the data. The skb put() function grows the data area upwards in memory through the free space at the buffer end and thus reserves space for the memcpy(). Many network operations used in sending add to the start of the frame each time in order to add headers to packets, thus skb push() is provided to allow you to move the start of the frame down through memory, providing enough space has been reserved to leave room for this. After a buffer has been allocated all the room is at the end. A further call skb reserve() called before data is added allows you to specify that some of the room should be at the beginning. Thus many sending routines start with something like skb=alloc_skb(len+headspace, GFP_KERNEL); skb_reserve(skb, headspace); skb_put(skb,len); 4

Figure 1: After alloc skb()

Figure 2: After skb reserve()

Figure 3: An sk buff containing data

Figure 4: After skb put()

Figure 5: After skb push()

memcpy_fromfs(skb->data,data,len); pass_to_m_protocol(skb); In systems such as BSD Unix you don’t need to know in advance how much space you will need as it uses chains of small buffers (mbufs) for its network buffers. Linux chooses to use linear buffers and know in advance (often wasting a few bytes to allow for the worst case) because linear buffers make many other things much faster. One useful trick you will see in the Ethernet drivers is to reserve a couple of bytes before reading a buffer from the card to main memory. This lands the IP header on a four byte boundary for faster access. Better still it lands it on a sixteen byte boundary which leaves it cache aligned.

2.2

Congestion collapse and NAPI

The interrupt mechanism used for frame reception leads to a phenomena called “congestion collapse”. In [9], the reasons for this collapse are analyzed and a solution, referred to as NAPI (New API) is proposed. Congestion collapse occurs when, although a very high number of packets per second enter a Linux router, not a single packet is going out. Measurements carried in [9] have shown that this point was situated at around 60 Kpps for a Pentium II based pc with Linux 2.3.99. This rather undesirable behavior is due to interrupt live lock : for each packet entering the system, an interruption is issued and a fraction of time is lost. For a very high number of interruptions, the processor does not have any time left for producing any useful 5

New packet arrival (rx_sched)

CLOSED − no timers − no packets on rx/tx

INT_ON −rx and rxnobuff on −waiting for packets − device not on poll list no more packets (rx_stat)

timer expired but still OOM

!OOM OOM_SUSPEND − device not in poll_list − int off − timer on

POLL_MODE − rx and rxnobuff off − device in poll list

Device has packet but 1) quota exceeded or 2) stack returned error

device scheduled to receive

RECEIVING − int off − device not in poll_list − receiving packets and sending them up the stack

OOM

Figure 6: NAPI finite state machine (source : linux-net mailing list, Jamal)

work. The congestion collapse point is reached. To alleviate this problem, a mechanism that prevent the network card from interrupting the processor must be found. This mechanism is shown on Fig. 6 which describes the NAPI strategy. Instead of having the card interrupt the processor for each packet it receives, the interface is registered on the poll list and its interruption is disabled. The interface is then scheduled to send a certain quota. If the interface has still some work to do after the quota is reached, it is put back in the poll list, otherwise, it return to its initial state with interruption on. With this mechanism, congestion collapse is never reached. Instead, the number of outgoing packets reach a saturation point but does not decrease to zero anymore.

3

From the network queue to the protocol handler - Layer2

Once the software interrupt is scheduled for execution, the frame is dequeued and the protocol type can be used to send the frame to the right protocol handler. In fact the protocol type has already been identified by the drivers which knows what encapsulation type is used and therefore knows how to get the protocol information from the layer 2 header. For instance, the 3c509 driver use the function eth type trans 5 to set the value of sk buff − > protocol. As the layer 2 has been further divided by the IEEE into a logical link control (LLC) and a Medium Access Control (MAC) layer, this func5

net/ethernet/eth.c

6

ptype_base

Struct packet_type unsigned short type; struct net_device *dev; int (*func); void *data struct packet_type *next

/*This is really htons(ether_type). /* NULL is wildcarded here */ /*Private to the packet type

*/

*/

NULL

Figure 7: Protocols must be registered in a global structure

tion is a little bit more complex then one might expect. In effect the 802.2 LLC hold a “length” field after the destination and source address where the Ethernet II header has the “type” field. An 802.2 header is then used to carried protocol information6 . ( See for instance chap. 7 of [4] for more information) . Further complication arise from some old Novell implementation of the protocol which encapsulates IPX packets directly into an 802.3 header without 802.2 header. The net result is that the layer 3 protocol that is carried into that layer 2 header can be unequivocally determined. Once this information is known, the right protocol handler must be determined. This is done by looking into the ptype base array (see Fig. 7)that holds all the protocols that can be processed along with the corresponding entry function such as ipr cv or arpr cv. New protocols can be registered by calling the function void dev add pack(struct packet type *pt) 7 that will link the packet type structure to the global array ptype base[16].

4

Forwarding the packet - Layer3

In this section, we suppose that we are dealing with an IP packet that must be forwarded through the linux box. Therefore, the packet enter the layer 3 processing with the function ip rcv. At this point, we find the first Netfilter hook. Netfilter is the packet filtering / packet mangling / NAT framework of the Linux 2.4 kernel series. Netfilter is a generalized framework of hooks in the network stack. Any kernel module can plug into one or more of these hooks an will receive each packet traversing this hook. The netfilter hooks are currently implemented for IPv4, IPv6 and DECnet. As explained in [6], there are five hooks in the Linux kernel, represented in Fig. 8. Three of 6

In that case, the DSAP and SSAP field are set to 0xAA to indicate the presence of a SNAP header made of 3 bytes of organization code ( assigned by IEEE ) and finally the 2 bytes used for the protocol type 7 net/core/dev.c

7

input interface

1

route

1. NF_IP_PRE_ROUTING 2. NF_IP_LOCAL_IN 3. NF_IP_FORWARD 4. NF_IP_POST_ROUTING 5. NF_IP_LOCAL_OUT

3

2

4

output interface

route 5

localhost

Figure 8: Netfilter hook along the packet path (source [6])

these hooks are also visible in Fig. 12. Basically, these hooks can decide to drop the packet or to let it continue, sometimes after modification (mangling). Supposing that our packet survived the first hook, it is now time for it to be routed. Routing involve a look up in a complex structure called Forwarding Information Base Table (FIB table). The goal of this lookup is to find a route entry corresponding to the destination ip address of the packet. A next hop is associated with each of these routes. The next hop is the router we should forward the packet to. Because looking up in this complex structure is quite expensive, a route cache is also used to store the routes that are being used. Look up in the cache is done with a hash function that uses a combination of source and destination address, TOS and incoming device. Therefore, two packets having these fields in common will be routed toward the same next hop, rendering multipath routing impossible without modification to the caching system. Traditionally, the FIB has been populated by way of IOCTL call. Today, netlink offer a reacher set of possibilities and is used by programs such has ip and tc. A library libnetlink is available to use the functions offered by netlink. Now that a next hop has been found, the packet is ready to be forwarded. During the routing phase, the skb− >dst field was set. The next step is to call the input method associated with this destination. At this stage, the TTL field in the ip header is decremented and the mtu of the outgoing interface is check. If this mtu is smaller than the packet size, the ip packet has to be fragmented, otherwise, it can directly be transmitted to the outgoing interface. ICMP messages may also be generated at this stage (see [2] for details on requirement for IP routers) If the information needed to successfully send the packet to the next router is not known, an arp packet might be sent to obtain the hardware address of the next interface. When this information is known, the mac rewrite can take place and the packet is ready to be sent toward the next hop.

8

fib_zones fib_node fib_next fib_dst fib_use fib_info fib_metric fib_tos

fib_zone fz_next fz_hash_table fz_list fz_nent fz_logmask fz_mask

fib_info fib_next fib_prev fib_gateway fib_dev fib_refcnt fib_window fib_flags fib_mtu fib_irtt

fib_node fib_next fib_dst fib_use fib_info fib_metric fib_tos

Figure 9: The forwarding information base (source [8]).

9

fib_info fib_next fib_prev fib_gateway fib_dev fib_refcnt fib_window fib_flags fib_mtu fib_irtt

dev_queue_transmit q_disc_enqueue

TIMER

q_disc_wakeup q_disc_restart q_disc_dequeue

q_disc_run_queues

hard_start_transmit net_bh Figure 10: Functions called when queueing and sending packets (source [1])

5

Queueing the packet to the outgoing interface Layer2

At this stage, the packet enters the outgoing queue where traffic control, bandwidth shaping and complex queueing can take place. The way the packet enter this queue is illustrated in Fig. 10. The queueing policy in use may not allow for directly sending a packet. Therefore, the packet has to be left in the queue and a timer has to be configure to schedule the departure of this packet. Because of the very low timing resolution of Linux (1 tick = 10 [ms]) on intel based architecture, this timer may lead to restriction on configurable parameters. Let’s say that a packet is left in a TBF queue. This packet may wait 1/HZ (in Linux 1 tick = 1/HZ seconds and is the resolution of the kernel, HZ=100 for intel architecture, 1000 for alpha ) seconds. Because a maximum of B bytes can be send by this buckets in any time interval, the maximum achievable bandwidth is therefore B ∗ HZ. Of course, the queue can be woken up asynchronously by others packets entering the system which has a positive effect on the effective resolution.

6

Appendix

A

The OSI model

B

The path of a packet figure

See fig. 12. 10

Layer 7

Application

Application

6

Presentation

Presentation

5

Session

Session

4

Transport

Transport

3

Network

Network

Network

Network

2

Data link

Data link

Data link

Data link

1

Physical

Physical

Physical

Physical

Figure 11: The Open System Interconnection reference model

References [1] Werner Almesberger. Linux traffic control - implementation overview, November 1998. [2] F. Baker. RFC1812 - Requirements for IP Version 4 Routers, June 1995. [3] D. P. Bovet and M. Cesati. Linux Kernel. O’Reilly, 2001. [4] K. Downes, M. Ford, H. K. Lew, S. Spanier, and T. Stevenson. Internetworking technologies handbook. Macmillan Technical Publishing, 1998. [5] P. Gortmaker. Linux ethernet-howto, 2000. [6] LaForge. Irc talk on netfilter - http://www.gnumonks.org/papers/netfilterlk2000, 2000. [7] A. Rubini and J. Corbet. Linux Device Drivers. O’Reilly, 2001. [8] David A. Rusling. The Linux Kernel. 1996. [9] J. H. Salim, R. Olsson, and A. Kuznetsov. Beyond sofnet, proceedings of the 5th annual linux showcase & conference, oakland, california, November 2001.

11

net_rx_action

rx_ring

next

. .

softnet_data[cpu]

....

ptype_base[16]

./include/linux/if_ether.h ETH_P_IP 0x0800 ETH_P_ARP 0x0806 ETH_P_8021Q 0x8100

layer 3

rx_ring

hardware (or logical ) device

alloc skb

skb−>mac.raw = raw packet

{

layer 4

?

IRQ processing

layer 2

next

driver =daemon ( UML )

uml_net_interrupt

uml_net_rx

daemon_read

net_rcv_from

dev_base

IRQ

netif_rx(skb)

__skb_queue_tail

DEVICE DRIVER

IP PACKET

scheduler

cpu_raise_softirq(NET_RX_SOFTIRQ)

do_softirq

SOFTIRQ

__sbk_dequeue

packet_type−>func()

net_device

LAYER 2

net_device

Figure 12: The path of a packet

net_device

12

blue text : global structure

NF_HOOK

ip_finish_output2

NF_HOOK

( DFLT )

pfifo_fast_ops

tbf_qdisc_ops

sfq_qdisc_ops

red_qdisc_ops

prio_qdisc_ops

./net/sched/

dev−>qdisk−>enqueue

dev_queue_xmit

rtable

rt_hash_table

set skb−>dst

main_table

local_table

( NF_IP_POST_ROUTING )

skb−>dev=dev

( NF_IP_FORWARD )

icmp redirect ?

ttl−−

skb−>dst−>input(skb)

rtable rtable rtable hash(daddr,saddr,dev,tos)

yes

pfifo_fast_enqueue

tbf_enqueue

sfq_enqueue

red_enqueue

prio_enqueue

dev has no queue (soft device)

dst−>neighbour−>output

if skb−>len > skb−>dst−>pmtu

ip_finish_output

ip_fragment

ip_send

hh−>hh_output

dev_open dev_activate qdisc=qdisc_create_dflt

ip_rcv_finish

ip_forward_finish

NF_HOOK

ip_forward

( NF_IP_PRE_ROUTING )

FORWARD

func=vlan_skb_recv

func=arp_rcv

func= ip_rcv

ip_route_input

in cache

no

ip_route_input_slow

fib_lookup

fn_hash_lookup

rt_intern_hash

ROUTING ENGINE

q_disc_restart

__skb_queue_tail

RTN_LOCAL RTN_BROADCAST RTN_UNICAST

include/linux/rtnetlink.h

dev−>hard_start_xmit(skb,dev)

dev−>Qdisc

udp_rcv

tcp_rcv

( uml )

( 3c509 )

( 8390 )

uml_net_start_xmit

el3_start_xmit

ei_start_xmit

spin_lock_irqsave

tx_ring

outw

tx_ring

IP PACKET

Path of a packet in the Linux kernel

Apr 22, 2003 - At its most basic a list of buffers is managed using functions like this: ... and a solution, referred to as NAPI (New API) is proposed. Congestion ...

171KB Sizes 3 Downloads 249 Views

Recommend Documents

Linux Kernel - The Series
fs include init ipc kernel lib mm net samples scripts security sound tools usr virt ..... then the system can get severely damaged, files can be deleted or corrupted, ...

Linux Kernel Development - GitHub
Page 10 .... Android's “life of a patch” flowchart. Gerrit is only one tiny part in the middle. Replace that one part with email, and everything still works, and goes ...

On the Complexity of Maintaining the Linux Kernel ...
Apr 6, 2009 - mm: Contains all of the memory management code for the kernel. Architecture specific ... fs: Contains all of the file system code. It is further .... C, Python, Java or any procedural or object oriented code. As seen in section 2.2, ...

Understanding the Linux Kernel
Linux is also small when compared to some popular applications; Netscape .... O'Reilly & Associates, Inc. 101 Morris St. Sebastopol, CA 95472 (800) ..... numbers are used to identify the version; the third number identifies the release ... appear mor

Conceptual Architecture of the Linux Kernel Contents
2.5 System Data Structures. 3. Subsystem Architectures. 3.1 Process Scheduler Architecture. 3.2 Memory Manager Architecture. 3.3 Virtual File System ...

Switch to Linux - The Lawful Path
already knows his or her way around the ... not know it, but Linux forms the basis of Android, the ... What we call “Linux” today is the work of ... 3 It's reliable.

Message Path Delays in Packet-Switching ...
buffer systems equipped with (infinite space) memory ... a customer (packet) from the server (channel) i is equal ..... Cmin = min (C1,Cz,.*.,CN). We note that dm,, (given by (39) for the exponential. ' case) is minimized at = 0. Thus, as expected, t

Linux 2.6 Kernel Exploits
Linux 2.6 Kernel Exploits. Stéphane DUVERGER. EADS. Suresnes .... default WorkQueue managed via [events/cpu] succession of function calls in process ...

OReilly.Understanding.The Linux Kernel 1st Edition.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. OReilly.

OReilly.Understanding.The Linux Kernel 1st Edition.pdf
OReilly.Understanding.The Linux Kernel 1st Edition.pdf. OReilly.Understanding.The Linux Kernel 1st Edition.pdf. Open. Extract. Open with. Sign In. Main menu.

The Linux kernel hidden inside windows 10 - The Swiss Bay
Currently only one Pico provider can be registered, as the callbacks are simply a global function array ... A Windows launcher service which communicates with the LxssManager and ..... WSL file access and network I/O is kernel-sourced.

The-Linux-Kernel-Module-Programming-Guide.pdf
... Kernel Module Programming Guide. ii. Page 3 of 82. The-Linux-Kernel-Module-Programming-Guide.pdf. The-Linux-Kernel-Module-Programming-Guide.pdf.