Comprehensive quick reference guide for Linux networking data structures, kernel functions, system calls, and debugging commands. Essential resource for network developers and system administrators.
Welcome to the comprehensive Linux networking reference guide! This post serves as your quick lookup resource for essential data structures, kernel functions, system calls, and debugging commands that form the foundation of Linux networking. Whether you’re debugging network issues, developing network applications, or diving deep into kernel networking code, this reference will be your indispensable companion.
This guide is designed for quick lookups and serves as a practical reference for the concepts covered throughout our networking series. Keep this bookmarked for easy access during development and troubleshooting sessions.
How to Use This Guide
This reference is organized into logical sections:
Data Structures: Core kernel networking structures and their key fields
Function Reference: Essential kernel functions for network programming
System Calls: User-space networking APIs and their implementations
Debugging Commands: Practical commands for network analysis and troubleshooting
Each section includes code snippets, parameter descriptions, and usage examples to help you quickly find the information you need.
Core Data Structures Quick Reference
Socket Buffer (sk_buff) - The Heart of Packet Processing
The sk_buff structure is the fundamental unit of packet processing in the Linux kernel:
/* Buffer management */ unsignedint len; // Total data length unsignedint data_len; // Length of paged data unsignedchar *head, *data; // Buffer start and data start sk_buff_data_t tail, end; // Tail and end pointers
/* Protocol headers */ __u16 transport_header; // Transport layer header offset __u16 network_header; // Network layer header offset __u16 mac_header; // MAC layer header offset __be16 protocol; // Packet protocol type __u8 pkt_type; // Packet type classification
refcount_t users; // Reference count };
Essential sk_buff Macros:
1 2 3 4 5
skb_put(skb, len) // Add data to end of buffer skb_push(skb, len) // Add space at beginning skb_pull(skb, len) // Remove data from beginning skb_reserve(skb, len) // Reserve space at head skb_trim(skb, len) // Trim buffer to length
structtcp_sock { structinet_connection_sockinet_conn;// Inherit from inet socket
/* Sequence numbers */ u32 rcv_nxt; // Next expected sequence number u32 snd_nxt; // Next sequence number to send u32 snd_una; // First unacknowledged sequence u32 snd_up; // Urgent pointer
/* Congestion control */ u32 snd_cwnd; // Congestion window u32 snd_ssthresh; // Slow start threshold u32 prior_cwnd; // Prior congestion window u32 prr_delivered; // Number of packets delivered in Recovery
/* Round trip time */ u32 srtt_us; // Smoothed round trip time u32 mdev_us; // Medium deviation u32 rttvar_us; // Round trip time variance u32 rtt_seq; // RTT measurement sequence number
/* Packet tracking */ u32 packets_out; // Packets which are "in flight" u32 retrans_out; // Retransmitted packets out u32 max_packets_out; // max packets_out in last window u32 max_packets_seq; // Right edge of max_packets_out flight
/* Statistics */ u32 total_retrans; // Total retransmissions for entire connection };
TCP States:
1 2 3 4 5 6 7 8 9 10 11
TCP_ESTABLISHED // Normal data transfer state TCP_SYN_SENT // Client waiting for matching connection request TCP_SYN_RECV // Server waiting for confirming connection request TCP_FIN_WAIT1 // Waiting for remote TCP termination request TCP_FIN_WAIT2 // Waiting for remote TCP termination request TCP_TIME_WAIT // Waiting to ensure remote received acknowledgment TCP_CLOSE // No connection state TCP_CLOSE_WAIT // Waiting for local user termination request TCP_LAST_ACK // Waiting for remote termination acknowledgment TCP_LISTEN // Waiting for incoming connection requests TCP_CLOSING // Waiting for remote termination acknowledgment
Protocol Headers Reference
Ethernet Header
1 2 3 4 5
structethhdr { unsignedchar h_dest[ETH_ALEN]; // Destination MAC (6 bytes) unsignedchar h_source[ETH_ALEN]; // Source MAC (6 bytes) __be16 h_proto; // Protocol type (2 bytes) } __attribute__((packed));
/* Send data */ ssize_tsend(int sockfd, constvoid *buf, size_t len, int flags); ssize_tsendto(int sockfd, constvoid *buf, size_t len, int flags, conststruct sockaddr *dest_addr, socklen_t addrlen);
/* Receive data */ ssize_trecv(int sockfd, void *buf, size_t len, int flags); ssize_trecvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
/* Advanced I/O */ ssize_tsendmsg(int sockfd, conststruct msghdr *msg, int flags); ssize_trecvmsg(int sockfd, struct msghdr *msg, int flags);
Common Flags:
1 2 3 4 5 6 7
/* Send/Receive Flags */ MSG_DONTWAIT // Non-blocking operation MSG_PEEK // Peek at incoming data without removing MSG_TRUNC // Return real packet length MSG_WAITALL // Wait for full request or error MSG_OOB // Process out-of-band data MSG_NOSIGNAL // Don't send SIGPIPE on errors
Socket Configuration:
1 2 3 4 5 6 7
/* Get/set socket options */ intgetsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); intsetsockopt(int sockfd, int level, int optname, constvoid *optval, socklen_t optlen);
/* SOL_SOCKET level options */ SO_REUSEADDR // Allow address reuse SO_BROADCAST // Enable broadcast SO_KEEPALIVE // Keep connections alive SO_LINGER // Linger on close if data present SO_RCVBUF // Receive buffer size SO_SNDBUF // Send buffer size SO_RCVTIMEO // Receive timeout SO_SNDTIMEO // Send timeout SO_BINDTODEVICE // Bind socket to specific device
/* TCP level options (IPPROTO_TCP) */ TCP_NODELAY // Disable Nagle algorithm TCP_MAXSEG // Maximum segment size TCP_KEEPIDLE // Idle time before keepalive probes TCP_KEEPINTVL // Interval between keepalive probes TCP_KEEPCNT // Number of keepalive probes TCP_USER_TIMEOUT // Total time for unacknowledged data
Essential Debugging Commands
Network Interface Analysis
Interface Statistics:
1 2 3 4 5 6 7 8 9 10 11 12 13
# Basic interface information ip link show # Show all interfaces ip -s link show eth0 # Show interface with statistics cat /proc/net/dev # Kernel interface statistics netstat -i # Interface statistics (legacy)
# Detailed hardware information ethtool eth0 # Basic interface info ethtool -k eth0 # Show offload features ethtool -S eth0 # Show detailed NIC statistics ethtool -g eth0 # Show ring buffer parameters ethtool -l eth0 # Show channel/queue information ethtool -c eth0 # Show interrupt coalescing settings
Queue and CPU Analysis:
1 2 3 4 5 6 7 8
# Multi-queue information cat /sys/class/net/eth0/queues/rx-*/rps_cpus cat /sys/class/net/eth0/queues/tx-*/xps_cpus
# Modern socket statistics ss -tuln # TCP/UDP listening sockets ss -tulpn # Include process information ss -i # Show internal TCP information ss -m # Show socket memory usage ss -o state established # Show established connections with timers ss --info sport = :80 # Detailed info for specific port
# Connection states ss -o state established # Established connections ss -o state syn-sent # Outgoing connection attempts ss -o state syn-recv # Incoming connection attempts ss -o state time-wait # TIME_WAIT connections
# Process and socket mapping lsof -i :80 # Processes using port 80 fuser -n tcp 80 # Find process using TCP port 80
# Routing information ip route show # Show routing table ip route get <IP_ADDRESS> # Show route to destination cat /proc/net/route # Kernel routing table (IPv4) cat /proc/net/ipv6_route # Kernel routing table (IPv6)
# ARP/Neighbor information ip neigh show # ARP/neighbor table cat /proc/net/arp # ARP table arp -a # ARP table (legacy)
# Queue management net.core.netdev_max_backlog = 5000 # Max packets in queue net.core.netdev_budget = 600 # NAPI budget per softirq net.core.dev_weight = 64 # NAPI weight
This comprehensive reference guide provides the essential information needed for Linux networking development, debugging, and optimization. Keep this guide handy for quick lookups during your networking adventures!
This reference guide is part of the comprehensive Linux Networking Deep Dive series, providing essential information for network developers and system administrators.