Linux kernel network functionality spans from sockets interface through protocols to network cards.
⚲ Shell interfaces:
- man 8 netstat prints network connections, routing tables, interface statistics and other details
- man 8 ip shows and configures routing, network devices, interfaces and tunnels
- man 8 ss - socket statistics utility
Sockets
⚲ sys/socket.h — main user mode sockets header
⚲ Basic common and client side interface:
- man 2 socket ↪ __sys_socket id,
- struct sockaddr id - abstract socket address
- man 2 connect ↪ __sys_connect id;
- man 2 shutdown shuts down part of a full-duplex connection
- man 2 send ↪ __sys_sendto id sends a message on a socket
- man 2 recv ↪ __sys_recvfrom id, __sys_recvmsg id receives a message from a socket
⚲ Additional server side interface:
- man 2 bind ↪ __sys_bind id - binds a sockaddr to a socket
- man 2 listen ↪ __sys_listen id - listens for connections on a socket
- man 2 accept ↪ __sys_accept4 id - accepts a connection on a socket
⚙️ Internals
- struct socket id @ linux/net.h inc
- struct proto_ops id - abstract protocols interface
- struct sock id - network layer representation of sockets net/sock.h inc
- net/socket.c src
📚 References
Network storage
🔧 TODO man 2 sendfile ↪ do_sendfile id. See also Zero-copy between file descriptors
Names
⚲ API: man 2 uname, man 2 sethostname, man 2 gethostname, man 2 setdomainname man 2 getdomainname
⚙️ Details
- utsname id returns writeable pointer to new_utsname id from uts_namespace id from nsproxy id from current id task_struct id.
- CLONE_NEWUTS id, setns id
- kernel/utsname.c src
📚 References
Address families
⚲ Address Family aka AF, domain defines address format and address length socklen_t. See man 3 inet_ntop, function man 3 inet_pton derives socklen_t from AF, man 2 getsockname man 2 getpeername.
PF - Protocol Family index (PF_MAX id) actually is the same as Address Family index (AF).
- man 7 address_families. Common AF of more than defined 40 (AF_MAX id):
- man 7 unix ↪ unix_family_ops id - sockets for local IPC
- man 7 ip ↪ inet_family_ops id - IPv4
- man 7 netlink ↪ netlink_family_ops id - communication between kernel and user space
- man 7 vsock ↪ vsock_family_ops id - communication between VM and hypervisor
- man 7 packet ↪ packet_family_ops id - device level interface
- bt_sock_family_ops id - Bluetooth
⚙️ Internals
- sock_register id - registers net_proto_family id. See references to this identifiers to find more than 30 protocol families.
- __sock_create id
Protocols
Each Protocol Family (PF, same index as Address Family AF) consists of several protocol implementations.
Directory /proc/net contains various files and subdirectories containing information about the networking layer. File /proc/net/protocols lists available and used protocols.
In each PF protocols are classified to different types sock_type id, for example stream, datagram and raw socket. TCP is type of stream, UDP is type of datagram, raw and ping are type of raw.
- proto_register id - registers struct proto id - protocol implementations:
- In inet_init id initcall and inetsw_array id:
- In af_unix_init id initcall:
References
- Transport layer and TCP
Network device interfaces
- devm_register_netdev id registers net_device id, net_device_ops
- sk_buff id socket buffer (skb)
- dev_queue_xmit id queues socket buffers into transmit queue
👁 Example: drivers/net/loopback.c src - the most famous and simple interface lo
⚙️ function loopback_xmit id receives skb and passes it back with netif_rx id
📚 References
Network drivers
- netif_rx id - before NAPI
- NAPI
- NAPI Driver design
- ⚲ API:
- netif_napi_add id adds napi_struct id
- napi_schedule id - called by an IRQ handler to schedule a poll
- netif_receive_skb id - instead netif_rx, finally calls ip_rcv id
- napi_complete_done id - called from custom napi->poll()
- ⚙️ Internals:
- net_dev_init id
- net_rx_action id
- napi_poll id calls custom napi->poll()
- net_rx_action id
- net_dev_init id
- 👁 example
- e1000_intr id calls __napi_schedule id
- custom napi->poll() e1000e_poll id calls napi_complete_done id
- ⚲ API:
- ether_setup id setups Ethernet network device
- 👁 An example of Ethernet driver: e1000_probe id
⚙️ Internals
📚 References
- Networking doc
- https://lwn.net/Kernel/Index/#Networking
- Data link layer: Ethernet
- network_overview
- GRO - Generic Receive Offload
💾 Historical