700 likes | 1.09k Views
Linux Networking. High Speed Network Lab canaan@totoro.cs.nthu.edu.tw 939609 高迦南 946321 賴昇鴻 944361 陳世興. Outline. Introduction BSD Socket Interface Packet Sending Packet Receiving Packet Forwarding NAT Implementation References. Outline. Introduction BSD Socket Interface
E N D
Linux Networking High Speed Network Lab canaan@totoro.cs.nthu.edu.tw 939609 高迦南 946321 賴昇鴻 944361 陳世興
Outline • Introduction • BSD Socket Interface • Packet Sending • Packet Receiving • Packet Forwarding • NAT Implementation • References Linux Networking
Outline • Introduction • BSD Socket Interface • Packet Sending • Packet Receiving • Packet Forwarding • NAT Implementation • References Linux Networking
Introduction 1Source Lines of Linux 2.6.5 Linux Networking
Introduction 2 Source Lines of Linux 2.6.5 Drivers Top 7 Linux Networking
Introduction 3 • 6% (244,421 lines) of all kernel code is devoted to networking(L3L4,Tcp/Ip Stack). • 10.1% (409,618 lines) of all kernel code is devoted to networking driver (L2). • The data are generated using David A. Wheeler's 'SLOCCount'. Linux Networking
Process 2: Data Link Interface Layer (Ethernet, etc.) NIC driver (MAC Layer) Introduction 4Linux Networking Layers TCP / IP vs. OSI model 7: Application 6: Presentation 5: Session User Space BSD socket API 4: Transport 3: Network TCP/IP Stack Kernel Space 1: Physical Layer Linux Networking
Outline • Introduction • BSD Socket Interface • Packet Sending • Packet Receiving • Packet Forwarding • NAT Implementation • References Linux Networking
BSD Socket Interfacecygwin 1.5.9 /usr/include/sys/socket.h Linux Networking
BSD Socket InterfaceLinux System Call Linux Networking
BSD Socket Interface syscall_handler_t *sys_call_table[] Linux Networking
BSD Socket Interface syscall_handler_t *sys_call_table[] Linux Networking
BSD Socket Interface Linux Socket System Calllinux-2.6.2\net\socket.c Linux Networking
BSD Socket Interface Socket Layer Detail Relation Linux Networking
linux-2.6.2\net\socket.c Linux Networking
BSD Socket Interface Sockfd_lookup() Linux Networking
BSD Socket Interface linux-2.6.2\include\linux\net.h Linux Networking
BSD Socket Interface BSD socket,proto_opslinux-2.6.2\include\linux\net.h Linux Networking
BSD Socket Interface Linux Networking
What is the sk->sk_prot->connect()? Linux Networking
Process 2: Data Link Interface Layer (Ethernet, etc.) NIC driver (MAC Layer) Linux Networking LayersCurrent Tracing Pointer TCP / IP vs. OSI model 7: Application 6: Presentation 5: Session User Space BSD socket API 4: Transport 3: Network TCP/IP Stack Kernel Space 1: Physical Layer Linux Networking
Outline • Introduction • BSD Socket Interface • Packet Sending (TCP) • Packet Receiving • Packet Forwarding • NAT Implementation • References Linux Networking
Packet Sending (TCP)Tcp Ops Linux Networking
Packet Sending (TCP)L4 Tcp Header Linux Networking
linux-2.6.2\net\ipv4\tcp_ipv4.c Linux Networking
linux-2.6.2\net\ipv4\tcp_ipv4.ctcp_v4_connect() • checks for errors • calls ip_route_connect() to find route to destination • creates connection packet • calls tcp_connect() to send packet Linux Networking
linux-2.6.2\net\ipv4\tcp_output.c Linux Networking
linux-2.6.2\net\ipv4\tcp_output.c tcp_connect() • completes connection packet with appropriate bits and window sizes set • puts packet on socket output queue • calls tcp_transmit_skb() to send packet, initiating TCP connection Linux Networking
// include/linux/skbuff.h struct sk_buff { struct sk_buff *next; struct sk_buff *prev; unsigned char *head; unsigned char *data; unsigned char *tail; unsigned char *end; struct sk_buff_head *list; struct sock *sk; struct timeval stamp; struct net_device *dev; /* Transport layer header */ union { struct tcphdr *th; struct udphdr *uh; struct icmphdr *icmph; struct igmphdr *igmph; struct iphdr *ipiph; struct spxhdr *spxh; unsigned char *raw; } h; sk_buff Sock buffer? Packet buffer? Linux Networking
/* Network layer header */ union { struct iphdr *iph; struct ipv6hdr *ipv6h; struct arphdr *arph; struct ipxhdr *ipxh; unsigned char *raw; } nh; /* Link layer header */ union { struct ethhdr *ethernet; unsigned char *raw; } mac; struct dst_entry *dst; unsigned int len; unsigned int data_len; unsigned int csum; }; sk_buff Sock buffer? Packet buffer? Linux Networking
linux-2.6.2\net\ipv4\tcp_output.c Linux Networking
linux-2.6.2\net\ipv4\tcp_output.ctcp_transmit_skb() • builds TCP header and adds checksum • calls tcp_build_and_update_options() • checks ACKs,SYN • calls tp->af_specific->queue_xmit() Linux Networking
Packet Sending (IP)L3 IP Header Linux Networking
linux-2.6.2/net/ipv4/ip_output.cip_queue_xmit() • looks up route • builds IP header • fragments if required • adds IP checksum • Enter NetFilter LocalOut Hook • calls dst_output() “calls skb->dst->output() ” Linux Networking
Process 2: Data Link Interface Layer (Ethernet, etc.) NIC driver (MAC Layer) Linux Networking LayersCurrent Tracing Pointer TCP / IP vs. OSI model 7: Application 6: Presentation 5: Session User Space BSD socket API 4: Transport 3: Network TCP/IP Stack Kernel Space 1: Physical Layer Linux Networking
After ip_queue_xmit() Linux Networking
Outline • Introduction • BSD Socket Interface • Packet Sending (TCP) • Packet Receiving • Packet Forwarding • NAT Implementation • References Linux Networking
L3 NetFilter Architecture Linux Networking
L3 NetFilter Function View Linux Networking
Process 2: Data Link Interface Layer (Ethernet, etc.) NIC driver (MAC Layer) Linux Networking LayersCurrent Tracing Pointer TCP / IP vs. OSI model 7: Application 6: Presentation 5: Session User Space BSD socket API 4: Transport 3: Network TCP/IP Stack Kernel Space 1: Physical Layer Linux Networking
eth_type_trans() … Linux Networking
After eth_type_trans() IP_Header Linux Networking
netif_rx() (1) enqueue (2) Send NET_RX_SOFTIRQ to interrupt handler (排隊) NET_RX_SOFTIRQ netif_rx() Kernel Interrupt handler sk_buff driver packet net_rx_action() … Cpu queue Linux Networking
net_rx_action • Extracts the first packet from the queueuntil the queue is empty • Detemines the network layer protocol number • Invokes a suitable function of the network layer protocol (ip_rcv) Linux Networking
ip_rcv() 1. check length and checksum of the packet and disgards it if it is corrupted 2. Invoke ip_route_input => determines whether the packet (1) must be forwarded to another host (2) pass to a protocal of transport layer Linux Networking
ip_rcv_finish() ip_route_input() ip_route_input_slow() IN_DEV_FORWARD Ip_forward() Ip_local_deliver Linux Networking
Outline • Introduction • BSD Socket Interface • Packet Sending (TCP) • Packet Receiving • Packet Forwarding • NAT Implementation • References Linux Networking