SlideShare a Scribd company logo
The TCP/IP stack in the FreeBSD
kernel: an overview of the
implementation
--------------------
svg version by killasmurf86
killasmurf86@gmail.com
http://killasmurf86.lv
Kevin Lo
msi
The FreeBSD project
Examples of operating systems use
FreeBSD-based network stack
DragonflyBSD,a fork of FreeBSD
OS X from Apple
Osv from Cloudius Systems
RTEMS
Peeking at the Linux kernel changelogs
Increase the initial cwnd to 10 (RFC 6928)
: 2.6.39
Proportional Rate Reduction for TCP
(RFC 6937) : 3.2
Early Retransmit for TCP (RFC 5827)
: 3.5
TCP Fast Open : 3.6, 3.7
Memory buffers (mbufs)
struct mbuf: the most important data structure in
the FreeBSD networking subsystem,
which is defined in <sys/mbuf.h>
Every packet sent/received is handled using the
mbuf structure
Mbufs are fixed size data buffers (256 bytes each)
mbuf structure
struct mbuf {
struct m_hdr m_hdr;
union {
struct {
struct pkthdr MH_pkthdr; /* M_PKTHDR set */
union {
struct m_ext MH_ext; /* M_EXT set */
char MH_databuf[MHLEN];
} MH_dat;
} MH;
char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
} M_dat;
};
m_hdr structure
/*
* Header present at the beginning of every mbuf.
* Size ILP32: 24
* LP64: 32
*/
struct m_hdr {
struct mbuf *mh_next; /* next buffer in chain */
struct mbuf *mh_nextpkt; /* next chain in queue/record */
caddr_t mh_data; /* location of data */
int32_t mh_len; /* amount of data in this mbuf */
uint32_t mh_type:8, /* type of data in this mbuf */
mh_flags:24; /* flags; see below */
#if !defined(__LP64__)
uint32_t mh_pad; /* pad for 64bit alignment */
#endif
};
On 64-bit platforms, this results into 3 * 8 bytes + 2 * 4 bytes = 32 bytes
struct mbuf {
struct m_hdr m_hdr;
union {
struct {
struct pkthdr MH_pkthdr; /* M_PKTHDR set */
union {
struct m_ext MH_ext; /* M_EXT set */
char MH_databuf[MHLEN];
} MH_dat;
} MH;
char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
} M_dat;
};
Simple mbuf
Simple mbuf
m_next
m_nextpkt
m_data
m_len
m_type
m_flags 0
m_dat
Pointer to the next mbuf
Pointer to the next mbuf chain
Pointer to data attached to this
mbuf
Length of the data in this mbuf
Type of the data in this mbuf
Type of mbuf
MLEN (224 bytes)
m_hdr
struct mbuf {
struct m_hdr m_hdr;
union {
struct {
struct pkthdr MH_pkthdr; /* M_PKTHDR set */
union {
struct m_ext MH_ext; /* M_EXT set */
char MH_databuf[MHLEN];
} MH_dat;
} MH;
char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
} M_dat;
};
Packet header mbuf
Packet header mbuf
m_next
m_nextpkt
m_data
m_len
m_type
m_flags M_PKTHDR
m_pkthdr.rcvif
m_pkthdr.len
m_pkthdr.csum_flags
m_pkthdr.csum_data
...
m_pktdat
Pointer to the received interface
Total length of mbuf chain
Used for hardware checksum offloading
Checksum of the data portion of the
packet
MHLEN (168 bytes)
pkthdr
A typical UDP packet
m_next
m_nextpkt
m_data
m_len
m_type
m_flags
m_pkthdr.rcvif
m_pkthdr.len
m_pkthdr.csum_flags
m_pkthdr.csum_data
...
28 bytes for IPv4 + UDP
header
m_next
m_nextpkt
m_data
m_len
m_type
m_flags
150 bytes of data
NULL
NULL
150
MT_DATA
0M_PKTHDR
NULL
MT_DATA
28
178
struct mbuf {
struct m_hdr m_hdr;
union {
struct {
struct pkthdr MH_pkthdr; /* M_PKTHDR set */
union {
struct m_ext MH_ext; /* M_EXT set */
char MH_databuf[MHLEN];
} MH_dat;
} MH;
char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
} M_dat;
};
Mbuf page cluster
Mbuf page cluster
m_next
m_nextpkt
m_data
m_len
m_type
m_flags M_EXT
m_ext.ref_cnt
m_ext.ext_buf
m_ext.ext_size
m_ext.ext_type
m_ext.ext_free
...
not used
Pointer to the reference counter
Pointer to the external buffer
Size of the buffer
Type of external storage
Pointer to the function is used to
release the buffer
m_ext
MCLBYTES
(2048 bytes)
struct mbuf {
struct m_hdr m_hdr;
union {
struct {
struct pkthdr MH_pkthdr; /* M_PKTHDR set */
union {
struct m_ext MH_ext; /* M_EXT set */
char MH_databuf[MHLEN];
} MH_dat;
} MH;
char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
} M_dat;
};
Package header + page cluster
Packet header + page cluster
m_next
m_nextpkt
m_data
m_len
m_type
m_flags M_PKTHDR | M_EXT
m_pkthdr.rcvif
m_pkthdr.len
m_pkthdr.csum_flags
m_pkthdr.csum_data
...
m_ext.ref_cnt
m_ext.ext_buf
m_ext.ext_size
m_ext.ext_type
m_ext.ext_free
...
not used
pkthdr
m_ext
MCLBYTES
(2048 bytes)
Mbuf utility routines
MGET() / m_get(): allocate an mbuf
MGETHDR() / m_gethdr(): allocate an mbuf with a packet
header
MCLGET() / m_clget(): add an external cluster to an mbuf
m_free(): free a single mbuf
m_freem(): free a chain of mbufs
man mbuf
Protocol data structures
The protocol layer uses three main types
of structures:
● domain structure
● protocol switch structure (protosw &
ip6protosw)
● protocol control block (PCB)
Communication domains
Group of related protocols
Each has address family constant
domain structure
Defined in <sys/domain.h>
struct domain {
int dom_family; /* AF_xxx */
char *dom_name;
...
struct protosw *dom_protosw,*dom_protoswNPROTOSW;
struct domain *dom_next;
...
void *(*dom_ifattach)(struct ifnet *);
void (*dom_ifdetach)(struct ifnet *, void *);
/* af-dependent data on ifnet */
};
Supported address families
AF_LOCAL / AF_UNIX Local communication
AF_INET Internet version 4
AF_INET6 Internet version 6
AF_ROUTE Link layer interface
PF_KEY Internal key-management
AF_NATM Asynchronous transfer
mode
AF_NETGRAPH Netgraph sockets
AF_BLUETOOTH Bluetooth protocols
AF_INET_SDP OFED socket direct
protocol
RFC 2367 section 1.3
The PF_KEY protocol family (PF_KEY) symbol is defined in
<sys/socket.h> in the same manner that other protocol
families are defined. PF_KEY does not use any socket
addresses.
Applications using PF_KEY MUST NOT depend on the
availability of a symbol named AF_KEY, but kernel
implementations are encouraged to define that symbol for
completeness.
int s;
s = socket(AF_KEY, SOCK_RAW,PF_KEY_V2);
inetdomainstruct domain inetdomain = {
.dom_family = AF_INET,
.dom_name = "internet",
.dom_protosw = inetsw,
.dom_protoswNPROTOSW = &inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
#ifdef RADIX_MPATH
.dom_rtattach = rn4_mpath_inithead,
#else
.dom_rtattach = in_inithead,
#endif
#ifdef VIMAGE
.dom_rtdetach = in_detachhead,
#endif
.dom_rtoffset = 32,
.dom_maxrtkey = sizeof(struct sockaddr_in),
.dom_ifattach = in_domifattach,
.dom_ifdetach = in_domifdetach
};
VNET_DOMAIN_SET(inet);
Domains list
domain{} domain{} domain{}
domain{} domain{} domain{}
domain{}
domains:
localdomain: natmdomain: inet6domain:
sdpdomain: inetdomain: ngdomain:
domain{} domain{}
routedomain: ng_btsocket_domain: keydomain:
protosw structure
Defined in <sys/protosw.h>
/* USE THESE FOR YOUR PROTOTYPES ! */
typedef void pr_input_t (struct mbuf *, int);
typedef int pr_input6_t (struct mbuf **, int*, int); /* XXX FIX THIS */
typedef int pr_output_t (struct mbuf *, struct socket *);
typedef void pr_ctlinput_t (int, struct sockaddr *, void *);
typedef int pr_ctloutput_t (struct socket *, struct sockopt *);
typedef void pr_init_t (void);
typedef void pr_destroy_t (void);
typedef void pr_fasttimo_t (void);
typedef void pr_slowtimo_t (void);
typedef void pr_drain_t (void);
protosw structure (cont.)
pr_type
pr_domain
pr_protocol
pr_flags
...
pr_input
pr_output
pr_ctlinput
pr_ctloutput
...
pr_usrreqs
...
protocol identifiers
protocol – protocol
interface
socket – protocol
interface
Protocol type
Pointer to the associated domain{}
Protocol number
Protocol flags
Input to protocol
Output to protocol
Control input
Control output
User – protocol hook
inetsw[] switch table
struct protosw inetsw[] = {
{
.pr_type = SOCK_DGRAM,
.pr_domain = &inetdomain,
.pr_protocol = IPPROTO_UDP,
.pr_flags = PR_ATOMIC|PR_ADDR,
.pr_input = udp_input,
.pr_ctlinput = udp_ctlinput,
.pr_ctloutput = udp_ctloutput,
.pr_init = udp_init,
#ifdef VIMAGE
.pr_destroy = udp_destroy,
#endif
.pr_usrreqs = &udp_usrreqs
},
socket{}
so_count
so_type
so_options
so_linger
so_state
so_qstate
so_pcb
so_proto
...
so_rcv
so_snd
...
protosw{}
pr_type
pr_domain
pr_protocol
pr_flags
...
pr_input
pr_output
pr_ctlinput
pr_ctloutput
...
pr_usrreqs
...
pr_usrreqs{}
pru_aboart
pru_accept
pru_attach
pru_bind
pru_connect
...
pru_detach
pru_disconnect
pru_listen
pru_rcvd
pru_send
...
protocol
layer info
socket
buffers
Protocol control block (pcb)
Hold protocol information
Stored as a doubly linked list
Internet protocol control block (inpcb)
Foreign and local IP addresses
Foreign and local port numbers
Back pointer to socket
Per-protocol pcb
TCP control block (tcpcb)
Protocol state information
socket{}
so_count
so_type
so_options
so_linger
so_state
so_qstate
so_pcb
so_proto
...
so_rcv
so_snd
...
inpcb{}
inp_ppcb
inp_socket
...
inp_fport
inp_lport
inp_faddr
inp_laddr
inp_ip_tos
inp_optons
...
inp_lock
tcpcb{}
t_inpcb
t_state
t_flags
...
rcv_wnd
snd_wnd
snd_cwnd
...
t_maxseg
...
Bibliography

More Related Content

PDF
VLANs in the Linux Kernel
Kernel TLV
 
PPTX
GCC for ARMv8 Aarch64
Yi-Hsiu Hsu
 
PDF
USB Drivers
Anil Kumar Pugalia
 
PPT
Ipmi Server Management
sjtu1234567
 
PDF
BPF / XDP 8월 세미나 KossLab
Taeung Song
 
PDF
DPDK in Containers Hands-on Lab
Michelle Holley
 
PPTX
RISC-V Boot Process: One Step at a Time
Atish Patra
 
PDF
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 
VLANs in the Linux Kernel
Kernel TLV
 
GCC for ARMv8 Aarch64
Yi-Hsiu Hsu
 
USB Drivers
Anil Kumar Pugalia
 
Ipmi Server Management
sjtu1234567
 
BPF / XDP 8월 세미나 KossLab
Taeung Song
 
DPDK in Containers Hands-on Lab
Michelle Holley
 
RISC-V Boot Process: One Step at a Time
Atish Patra
 
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 

What's hot (20)

PPTX
U-Boot presentation 2013
Wave Digitech
 
PDF
Introduction of eBPF - 時下最夯的Linux Technology
Jace Liang
 
PPTX
Linux Network Stack
Adrien Mahieux
 
PDF
Arm device tree and linux device drivers
Houcheng Lin
 
PDF
Time Sensitive Networking in the Linux Kernel
henrikau
 
PPTX
USB Universal Serial Bus
Varun Kambrath
 
PDF
Embedded_Linux_Booting
Rashila Rr
 
PPTX
ARM Architecture in Details
GlobalLogic Ukraine
 
PPTX
BSP.pptx
taruian
 
PDF
BUD17-209: Reliability, Availability, and Serviceability (RAS) on ARM64
Linaro
 
PDF
Embedded Operating System - Linux
Emertxe Information Technologies Pvt Ltd
 
PDF
DPDK: Multi Architecture High Performance Packet Processing
Michelle Holley
 
PDF
Jagan Teki - U-boot from scratch
linuxlab_conf
 
PPTX
OVN 設定サンプル | OVN config example 2015/12/27
Kentaro Ebisawa
 
PDF
DPDK QoS
Masaru Oki
 
PDF
Troubleshooting BGP
APNIC
 
PDF
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Brendan Gregg
 
PPTX
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
Adrian Huang
 
PPTX
Linux PCI device driver
艾鍗科技
 
PDF
Page reclaim
siburu
 
U-Boot presentation 2013
Wave Digitech
 
Introduction of eBPF - 時下最夯的Linux Technology
Jace Liang
 
Linux Network Stack
Adrien Mahieux
 
Arm device tree and linux device drivers
Houcheng Lin
 
Time Sensitive Networking in the Linux Kernel
henrikau
 
USB Universal Serial Bus
Varun Kambrath
 
Embedded_Linux_Booting
Rashila Rr
 
ARM Architecture in Details
GlobalLogic Ukraine
 
BSP.pptx
taruian
 
BUD17-209: Reliability, Availability, and Serviceability (RAS) on ARM64
Linaro
 
Embedded Operating System - Linux
Emertxe Information Technologies Pvt Ltd
 
DPDK: Multi Architecture High Performance Packet Processing
Michelle Holley
 
Jagan Teki - U-boot from scratch
linuxlab_conf
 
OVN 設定サンプル | OVN config example 2015/12/27
Kentaro Ebisawa
 
DPDK QoS
Masaru Oki
 
Troubleshooting BGP
APNIC
 
Linux 4.x Tracing: Performance Analysis with bcc/BPF
Brendan Gregg
 
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
Adrian Huang
 
Linux PCI device driver
艾鍗科技
 
Page reclaim
siburu
 
Ad

Viewers also liked (20)

PDF
MOD server & FreeBSD (FreeBSD Day Taiwan)
Kevin Lo
 
PDF
New sendfile
Gleb Smirnoff
 
PDF
Linux performance
Will Sterling
 
PPTX
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
Andrew Case
 
PPTX
Cybermania Mains
Divye Kapoor
 
PDF
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
Anne Nicolas
 
PPTX
Cybermania Prelims
Divye Kapoor
 
PDF
Rootkit 102 - Kernel-Based Rootkit
Chia-Hao Tsai
 
PPTX
A particle filter based scheme for indoor tracking on an Android Smartphone
Divye Kapoor
 
ODP
Linux Internals - Kernel/Core
Shay Cohen
 
PDF
LAS16-403 - GDB Linux Kernel Awareness
Peter Griffin
 
PDF
The Linux Kernel Implementation of Pipes and FIFOs
Divye Kapoor
 
PDF
FreeBSD and Drivers
Kernel TLV
 
PDF
Rootkit on Linux X86 v2.6
fisher.w.y
 
PDF
Linux Kernel Exploitation
Scio Security
 
PDF
Part 04 Creating a System Call in Linux
Tushar B Kute
 
PPTX
了解网络
Feng Yu
 
PPT
Debugging Applications with GNU Debugger
Priyank Kapadia
 
PPTX
了解Cpu
Feng Yu
 
PDF
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
MOD server & FreeBSD (FreeBSD Day Taiwan)
Kevin Lo
 
New sendfile
Gleb Smirnoff
 
Linux performance
Will Sterling
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
Andrew Case
 
Cybermania Mains
Divye Kapoor
 
Kernel Recipes 2015: The stable Linux Kernel Tree - 10 years of insanity
Anne Nicolas
 
Cybermania Prelims
Divye Kapoor
 
Rootkit 102 - Kernel-Based Rootkit
Chia-Hao Tsai
 
A particle filter based scheme for indoor tracking on an Android Smartphone
Divye Kapoor
 
Linux Internals - Kernel/Core
Shay Cohen
 
LAS16-403 - GDB Linux Kernel Awareness
Peter Griffin
 
The Linux Kernel Implementation of Pipes and FIFOs
Divye Kapoor
 
FreeBSD and Drivers
Kernel TLV
 
Rootkit on Linux X86 v2.6
fisher.w.y
 
Linux Kernel Exploitation
Scio Security
 
Part 04 Creating a System Call in Linux
Tushar B Kute
 
了解网络
Feng Yu
 
Debugging Applications with GNU Debugger
Priyank Kapadia
 
了解Cpu
Feng Yu
 
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
Ad

Similar to The TCP/IP stack in the FreeBSD kernel COSCUP 2014 (20)

PPTX
The TCP/IP Stack in the Linux Kernel
Divye Kapoor
 
ODP
Introduction to Kernel Programming
Ahmed Mekkawy
 
PPT
5.IPC.ppt JSS science and technology university
tempemailtemp19
 
ODP
Sysprog17
Ahmed Mekkawy
 
PDF
Mobile Email Security
Rahul Sihag
 
PPTX
ch 7 POSIX.pptx
sibokac
 
PPTX
Socket Programming Intro.pptx
ssuserc4a497
 
PDF
Multithreaded sockets c++11
Russell Childs
 
PDF
Степан Кольцов — Rust — лучше, чем C++
Yandex
 
PDF
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
ANJALIENTERPRISES1
 
PPT
Basic socket programming
Kristian Arjianto
 
ODP
Sockets and Socket-Buffer
Sourav Punoriyar
 
PDF
SimpleArray between Python and C++
Yung-Yu Chen
 
PDF
sockets
AbhinavRapartiwar
 
PDF
Embedded C - Lecture 4
Mohamed Abdallah
 
PDF
OpenSSL Basic Function Call Flow
William Lee
 
PPTX
Chapter 3
lopjuan
 
PDF
Farewell to Disks: Efficient Processing of Obstinate Data
Distinguished Lecturer Series - Leon The Mathematician
 
DOCX
#include stdio.h#include systypes.h#include syssocket.h
SilvaGraf83
 
PDF
Unit_ 5.3 Interprocess communication.pdf
AnilkumarBrahmane2
 
The TCP/IP Stack in the Linux Kernel
Divye Kapoor
 
Introduction to Kernel Programming
Ahmed Mekkawy
 
5.IPC.ppt JSS science and technology university
tempemailtemp19
 
Sysprog17
Ahmed Mekkawy
 
Mobile Email Security
Rahul Sihag
 
ch 7 POSIX.pptx
sibokac
 
Socket Programming Intro.pptx
ssuserc4a497
 
Multithreaded sockets c++11
Russell Childs
 
Степан Кольцов — Rust — лучше, чем C++
Yandex
 
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
ANJALIENTERPRISES1
 
Basic socket programming
Kristian Arjianto
 
Sockets and Socket-Buffer
Sourav Punoriyar
 
SimpleArray between Python and C++
Yung-Yu Chen
 
Embedded C - Lecture 4
Mohamed Abdallah
 
OpenSSL Basic Function Call Flow
William Lee
 
Chapter 3
lopjuan
 
Farewell to Disks: Efficient Processing of Obstinate Data
Distinguished Lecturer Series - Leon The Mathematician
 
#include stdio.h#include systypes.h#include syssocket.h
SilvaGraf83
 
Unit_ 5.3 Interprocess communication.pdf
AnilkumarBrahmane2
 

Recently uploaded (20)

PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
oapresentation.pptx
mehatdhavalrajubhai
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Presentation about variables and constant.pptx
safalsingh810
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 

The TCP/IP stack in the FreeBSD kernel COSCUP 2014

  • 1. The TCP/IP stack in the FreeBSD kernel: an overview of the implementation -------------------- svg version by killasmurf86 [email protected] http://killasmurf86.lv Kevin Lo msi The FreeBSD project
  • 2. Examples of operating systems use FreeBSD-based network stack DragonflyBSD,a fork of FreeBSD OS X from Apple Osv from Cloudius Systems RTEMS
  • 3. Peeking at the Linux kernel changelogs Increase the initial cwnd to 10 (RFC 6928) : 2.6.39 Proportional Rate Reduction for TCP (RFC 6937) : 3.2 Early Retransmit for TCP (RFC 5827) : 3.5 TCP Fast Open : 3.6, 3.7
  • 4. Memory buffers (mbufs) struct mbuf: the most important data structure in the FreeBSD networking subsystem, which is defined in <sys/mbuf.h> Every packet sent/received is handled using the mbuf structure Mbufs are fixed size data buffers (256 bytes each)
  • 5. mbuf structure struct mbuf { struct m_hdr m_hdr; union { struct { struct pkthdr MH_pkthdr; /* M_PKTHDR set */ union { struct m_ext MH_ext; /* M_EXT set */ char MH_databuf[MHLEN]; } MH_dat; } MH; char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ } M_dat; };
  • 6. m_hdr structure /* * Header present at the beginning of every mbuf. * Size ILP32: 24 * LP64: 32 */ struct m_hdr { struct mbuf *mh_next; /* next buffer in chain */ struct mbuf *mh_nextpkt; /* next chain in queue/record */ caddr_t mh_data; /* location of data */ int32_t mh_len; /* amount of data in this mbuf */ uint32_t mh_type:8, /* type of data in this mbuf */ mh_flags:24; /* flags; see below */ #if !defined(__LP64__) uint32_t mh_pad; /* pad for 64bit alignment */ #endif }; On 64-bit platforms, this results into 3 * 8 bytes + 2 * 4 bytes = 32 bytes
  • 7. struct mbuf { struct m_hdr m_hdr; union { struct { struct pkthdr MH_pkthdr; /* M_PKTHDR set */ union { struct m_ext MH_ext; /* M_EXT set */ char MH_databuf[MHLEN]; } MH_dat; } MH; char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ } M_dat; }; Simple mbuf
  • 8. Simple mbuf m_next m_nextpkt m_data m_len m_type m_flags 0 m_dat Pointer to the next mbuf Pointer to the next mbuf chain Pointer to data attached to this mbuf Length of the data in this mbuf Type of the data in this mbuf Type of mbuf MLEN (224 bytes) m_hdr
  • 9. struct mbuf { struct m_hdr m_hdr; union { struct { struct pkthdr MH_pkthdr; /* M_PKTHDR set */ union { struct m_ext MH_ext; /* M_EXT set */ char MH_databuf[MHLEN]; } MH_dat; } MH; char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ } M_dat; }; Packet header mbuf
  • 10. Packet header mbuf m_next m_nextpkt m_data m_len m_type m_flags M_PKTHDR m_pkthdr.rcvif m_pkthdr.len m_pkthdr.csum_flags m_pkthdr.csum_data ... m_pktdat Pointer to the received interface Total length of mbuf chain Used for hardware checksum offloading Checksum of the data portion of the packet MHLEN (168 bytes) pkthdr
  • 11. A typical UDP packet m_next m_nextpkt m_data m_len m_type m_flags m_pkthdr.rcvif m_pkthdr.len m_pkthdr.csum_flags m_pkthdr.csum_data ... 28 bytes for IPv4 + UDP header m_next m_nextpkt m_data m_len m_type m_flags 150 bytes of data NULL NULL 150 MT_DATA 0M_PKTHDR NULL MT_DATA 28 178
  • 12. struct mbuf { struct m_hdr m_hdr; union { struct { struct pkthdr MH_pkthdr; /* M_PKTHDR set */ union { struct m_ext MH_ext; /* M_EXT set */ char MH_databuf[MHLEN]; } MH_dat; } MH; char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ } M_dat; }; Mbuf page cluster
  • 13. Mbuf page cluster m_next m_nextpkt m_data m_len m_type m_flags M_EXT m_ext.ref_cnt m_ext.ext_buf m_ext.ext_size m_ext.ext_type m_ext.ext_free ... not used Pointer to the reference counter Pointer to the external buffer Size of the buffer Type of external storage Pointer to the function is used to release the buffer m_ext MCLBYTES (2048 bytes)
  • 14. struct mbuf { struct m_hdr m_hdr; union { struct { struct pkthdr MH_pkthdr; /* M_PKTHDR set */ union { struct m_ext MH_ext; /* M_EXT set */ char MH_databuf[MHLEN]; } MH_dat; } MH; char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ } M_dat; }; Package header + page cluster
  • 15. Packet header + page cluster m_next m_nextpkt m_data m_len m_type m_flags M_PKTHDR | M_EXT m_pkthdr.rcvif m_pkthdr.len m_pkthdr.csum_flags m_pkthdr.csum_data ... m_ext.ref_cnt m_ext.ext_buf m_ext.ext_size m_ext.ext_type m_ext.ext_free ... not used pkthdr m_ext MCLBYTES (2048 bytes)
  • 16. Mbuf utility routines MGET() / m_get(): allocate an mbuf MGETHDR() / m_gethdr(): allocate an mbuf with a packet header MCLGET() / m_clget(): add an external cluster to an mbuf m_free(): free a single mbuf m_freem(): free a chain of mbufs man mbuf
  • 17. Protocol data structures The protocol layer uses three main types of structures: ● domain structure ● protocol switch structure (protosw & ip6protosw) ● protocol control block (PCB)
  • 18. Communication domains Group of related protocols Each has address family constant
  • 19. domain structure Defined in <sys/domain.h> struct domain { int dom_family; /* AF_xxx */ char *dom_name; ... struct protosw *dom_protosw,*dom_protoswNPROTOSW; struct domain *dom_next; ... void *(*dom_ifattach)(struct ifnet *); void (*dom_ifdetach)(struct ifnet *, void *); /* af-dependent data on ifnet */ };
  • 20. Supported address families AF_LOCAL / AF_UNIX Local communication AF_INET Internet version 4 AF_INET6 Internet version 6 AF_ROUTE Link layer interface PF_KEY Internal key-management AF_NATM Asynchronous transfer mode AF_NETGRAPH Netgraph sockets AF_BLUETOOTH Bluetooth protocols AF_INET_SDP OFED socket direct protocol
  • 21. RFC 2367 section 1.3 The PF_KEY protocol family (PF_KEY) symbol is defined in <sys/socket.h> in the same manner that other protocol families are defined. PF_KEY does not use any socket addresses. Applications using PF_KEY MUST NOT depend on the availability of a symbol named AF_KEY, but kernel implementations are encouraged to define that symbol for completeness. int s; s = socket(AF_KEY, SOCK_RAW,PF_KEY_V2);
  • 22. inetdomainstruct domain inetdomain = { .dom_family = AF_INET, .dom_name = "internet", .dom_protosw = inetsw, .dom_protoswNPROTOSW = &inetsw[sizeof(inetsw)/sizeof(inetsw[0])], #ifdef RADIX_MPATH .dom_rtattach = rn4_mpath_inithead, #else .dom_rtattach = in_inithead, #endif #ifdef VIMAGE .dom_rtdetach = in_detachhead, #endif .dom_rtoffset = 32, .dom_maxrtkey = sizeof(struct sockaddr_in), .dom_ifattach = in_domifattach, .dom_ifdetach = in_domifdetach }; VNET_DOMAIN_SET(inet);
  • 23. Domains list domain{} domain{} domain{} domain{} domain{} domain{} domain{} domains: localdomain: natmdomain: inet6domain: sdpdomain: inetdomain: ngdomain: domain{} domain{} routedomain: ng_btsocket_domain: keydomain:
  • 24. protosw structure Defined in <sys/protosw.h> /* USE THESE FOR YOUR PROTOTYPES ! */ typedef void pr_input_t (struct mbuf *, int); typedef int pr_input6_t (struct mbuf **, int*, int); /* XXX FIX THIS */ typedef int pr_output_t (struct mbuf *, struct socket *); typedef void pr_ctlinput_t (int, struct sockaddr *, void *); typedef int pr_ctloutput_t (struct socket *, struct sockopt *); typedef void pr_init_t (void); typedef void pr_destroy_t (void); typedef void pr_fasttimo_t (void); typedef void pr_slowtimo_t (void); typedef void pr_drain_t (void);
  • 25. protosw structure (cont.) pr_type pr_domain pr_protocol pr_flags ... pr_input pr_output pr_ctlinput pr_ctloutput ... pr_usrreqs ... protocol identifiers protocol – protocol interface socket – protocol interface Protocol type Pointer to the associated domain{} Protocol number Protocol flags Input to protocol Output to protocol Control input Control output User – protocol hook
  • 26. inetsw[] switch table struct protosw inetsw[] = { { .pr_type = SOCK_DGRAM, .pr_domain = &inetdomain, .pr_protocol = IPPROTO_UDP, .pr_flags = PR_ATOMIC|PR_ADDR, .pr_input = udp_input, .pr_ctlinput = udp_ctlinput, .pr_ctloutput = udp_ctloutput, .pr_init = udp_init, #ifdef VIMAGE .pr_destroy = udp_destroy, #endif .pr_usrreqs = &udp_usrreqs },
  • 28. Protocol control block (pcb) Hold protocol information Stored as a doubly linked list Internet protocol control block (inpcb) Foreign and local IP addresses Foreign and local port numbers Back pointer to socket Per-protocol pcb TCP control block (tcpcb) Protocol state information