Department of Computer Sciences THE UNIVERSITY OF TEXAS AT AUSTIN CS 378 (Spring 2003) Linux Kernel Programming Yongguang Zhang (ygz@cs.utexas.edu) Copyright 2003, Yongguang Zhang
This Lecture Linux Networking Setting up networks with UML Overview of Linux networking code Major data structures Questions? How much do you know about IP networking? Spring 2003 2003 Yongguang Zhang 2
Building a Test-bed Network To build a test-bed network with UML instances To test your network implementation Here, you need to know a little bit about Linux networking and network administration How to configure an IP address: ifconfig How to configure the route table: route How to test the network: ping How to debug the network: tcpdump Spring 2003 2003 Yongguang Zhang 3
Virtual Networks with UML UML supports a virtual Ethernet driver Ethernet packets are sent via UDP through UML host Emulate an Ethernet segment with an IP multicast group (UML host joins this group) UML UML UML host Virtual Ethernet Virtual Ethernet UTCS UDP Spring 2003 2003 Yongguang Zhang 4
A Very Simple IP Network IP network basics Each network interface must have a unique IP address Network interfaces connected by the same network must belong to the same subnet Host 1 Host 2 Network interface Network interface IP address: 192.168.1.1 IP address: 192.168.1.2 Ethernet segment (subnet: 192.168.1.0/24) Spring 2003 2003 Yongguang Zhang 5
Build this Network with UML Host 1 Host 2 Network interface Network interface IP address: 192.168.1.1 IP address: 192.168.1.2 UML host UDP 224.1.0.0 UML host Spring 2003 2003 Yongguang Zhang 6
Before We Start You will need to run multiple instances of UML In the same UTCS host, or In different hosts Use COW (copy-on-write) to use disk space Make root fs file read-only: chmod a-r root_fs_utcs First UML:./linux ubd0=cow1,root_fs_utcs Second UML:./linux ubd0=cow2,root_fs_utcs These cow* files take little space Spring 2003 2003 Yongguang Zhang 7
Step 1: Add an Ethernet to UML Adding a network device to UML Specify an ethn= argument in UML command line Pick an Ethernet segment for this device (i.e. pick a multicast address) UML command line syntax eth0=mcast,ethernetaddr,multicastaddr,port,ttl Example: To add a eth0 device on Ethernet segment 224.1.0.0./linux ubd0=cow1,root_fs_utcs eth0=mcast,,224.1.0.0,, Spring 2003 2003 Yongguang Zhang 8
Step 2: Start Up this Interface Configure the network interface within UML Once you run UML with the eth0= argument, you get an eth0 device Check this with: ifconfig eth0 Assign an IP address to this interface ifconfig eth0 192.168.1.1 up To check this configuration: ifconfig eth0 route n Spring 2003 2003 Yongguang Zhang 9
Step 3: Start up Another Host Start another UML host with the same virtual network In the same Linux host or another host in the same physical network (e.g, both in UTCS net)./linux ubd0=cow2,root_fs_utcs eth0=mcast,,224.1.0.0,, Make sure you use the same multicast address & port! Within this UML, configure the network with a different IP address, but in the same subnet ifconfig eth0 192.168.1.2 up Spring 2003 2003 Yongguang Zhang 10
Step 4: Test the Network Now, you have two UML hosts connected by a virtual network In first host: try ping 192.168.1.2 In second host: try ping 192.168.1.1 In first host: try ssh 192.168.1.2 Try tcpdump while you are doing these Spring 2003 2003 Yongguang Zhang 11
UML Networking Cautions Pick your multicast address to avoid conflict with other people I suggest: 224.1.ab.cd, where abcd is the last 4 digits of your account uid Spring 2003 2003 Yongguang Zhang 12
A More Complex Network Build your own network (with multiple subnets) Remember to use different multicast address (one per subnet) Set up your own routing Host 1 Host 2 Network interface NIC NIC 192.168.1.1 192.168.1.2 192.168.2.1 Host 3 Network interface 192.168.2.2 Subnet (192.168.1.0/24) Subnet (192.168.2.0/24) Spring 2003 2003 Yongguang Zhang 13
Building it with UML Host 2 (router): To start UML with 2 virtual Ethernet interfaces:./linux ubd0=cow2,root_fs_utcs eth0=mcast,,224.1.0.0,, eth1=mcast,,224.2.0.0,, To configure within UML: ifconfig eth0 192.168.1.2 up ifconfig eth1 192.168.2.1 up Host 3: To start UML:./linux ubd0=cow3,root_fs_utcs eth0=mcast,,224.2.0.0,, To configure within UML: ifconfig eth0 192.168.2.2 up Spring 2003 2003 Yongguang Zhang 14
Enable Routing Enable routing Enable IP forwarding at router (Host 2) echo 1 > /proc/sys/net/ipv4/ip_forward Need to add a static route at host 1 route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.2 Need to add a static route at host 2 route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.2.1 To test At host 1: ping 192.168.2.2 Spring 2003 2003 Yongguang Zhang 15
Linux Networking Architecture Network subsystem in Linux kernel (NET4.0) Supports a wide range of network architecture (family): IP, IPv6, IPX, Supports a wide range of network hardware: Ethernet, token ring, wireless, serial line, Supports network roles: host, router, bridge/switch A complete and robust internetworking system This class Study IP(v4) only Study network implementation in kernel only Spring 2003 2003 Yongguang Zhang 16
Linux Kernel Responsibility Protocol implementation and API Transport level and below: TCP, UDP, IP, ARP, Ethernet Higher-level protocols are implemented in user-space: HTTP, DNS, SMTP, SSH, Network configuration/management API for setting IP address, route table, ARP table Dynamic configuration/network management are implemented in user-space: DHCP, dynamic routing Networked OS services Network file system (NFS, SMB,...) Spring 2003 2003 Yongguang Zhang 17
Networking Stack Application Presentation Session Transport Network Data-link Physical User Space socket Linux Kernel hardware Java, HTML Socket programming Protocol implementation Network administration Networking Stack Spring 2003 2003 Yongguang Zhang 18
Networking Stack Components Socket API Common API for all network architectures/devices Network and transport layers Core protocols, multiplexing/de-multiplexing Grouped under different network families Network device drivers Manage hardware (network interface cards) Deal with data-link functions (such as MAC) Can be virtual (e.g., ppp0) Spring 2003 2003 Yongguang Zhang 19
Source Code Layout Under linux kernel source tree net/*.c, net/core/ : socket API, kernel abstractions and common utilities net/ethernet/, /net/atm/ : common device layer net/sched/ : packet schedulers (QoS) net/bridge/ : Ethernet bridge code net/*/ : protocol family (one per dir, e.g, net/ipv4/) drivers/net/*/ : network device driver (one per dir) Spring 2003 2003 Yongguang Zhang 20
Layering in the Networking Stack BSD Socket INET Socket TCP/UDP IP Common network layer Socket API IPv4-specific socket interface multiplexing Transport protocols IP input/output chain forwarding/routing IP-layer protocols Socket buffer, netfilter queue disciplines interface to device driver Spring 2003 2003 Yongguang Zhang 21
Socket Layer BSD socket Defining API and servicing related system calls Providing a common file interface for sockets (through a special file system called sockfs) Major data structure: struct socket (definition: include/linux/net.h, code: net/socket.c, net/core/) INET socket IPv4-specific socket interface Protocols multiplexing Major data structure: struct sock (definition: include/net/sock.h, code: net/ipv4/af_inet.c) Spring 2003 2003 Yongguang Zhang 22
Protocol Layer Protocol multiplexing and demultiplexing Setting up socket types, protocol switches, and handlers Major data structure: list of socket types, protocol hash Code: net/ipv4/protocol.c, net/ipv4/af_inet.c Raw socket: net/ipv4/raw.c Protocol-specific processing TCP, UDP: net/ipv4/tcp*.c, net/ipv4/udp.c ICMP, IGMP: net/ipv4/icmp.c, net/ipv4/igmp.c Tunneling, multicast: net/ipv4/ipip.c, net/ipv4/ipmr.c Spring 2003 2003 Yongguang Zhang 23
The IP Layer IP packet processing Code: net/ipv4/ip_*.c Address resolution Major data structure: neighor cache (ARP cache) Code: net/ipv4/arp.c Forwarding/routing Major data structure: FIB, route cache Code: net/ipv4/fib_*.c, net/ipv4/route.c Spring 2003 2003 Yongguang Zhang 24
Common Network Layer Basic data structure: socket buffer Used in all network stack: carry the packet payload for going through the input/output chains Minimize copying: only twice (between user mode and kernel mode, and from/to the network hardware) Other functions Netfilter hooks (for packet mangling, like firewall) Scheduling and queueing disciplines (for QoS) Common net device functions (for interfacing with device driver) Spring 2003 2003 Yongguang Zhang 25
Input Chain and Output Chain application socket TCP output IP output TCP input IP input Network device driver Spring 2003 2003 Yongguang Zhang 26
Summary Linux Networking LKP: 8 ULK: 18 Next lecture: Linux Networking Spring 2003 2003 Yongguang Zhang 27