4. interfaces ethernet 4.1 intro how simple ethernet driver works plus ioctls that it uses for config not look at hw, but os side functionality note that BUS ARCHITECTURE always make driver itself more complex ... 1. isa bus 2. pci bus 3. pccard now newcard ... (16 bit vs 32 bit) meaning you can dynamically take hw out or stick it in ... BSD kernels have per hw directory for "hiding" some hw details vax/ alpha/ i386 but device-oriented directories have crept in too ... often for dealing with bus types that cross cpu architectures. He shows "ne2000" as i386/isa/if_ne.c ... in FreeBSD now /sys/dev/ed Interface functions: ifnet driver description if_init leinit hw init if_output ether_output queue frame for xmit if_start le_start begin xmit if_ioct leioctl handle ioctl functions if_reset lereset reset device to known state if_watchdog .... nope watch for failures or do stats note: also will need interrupt side routine for dealing with xmit/recv interrupts. recv interrupts ... copy data into mbuf cluster and q for ip input, i.e., *read* xmit - done, try to start again stats - sometimes one gathers stats because of interrupts bus service - you may have to bow to the bus to make it happy 4.2 code intro netinet/if_ether.h - ethernet structures net/if.h - ioctl commands and if stuff net/if_ethersubr.c - generic common ethernet functions net/if.c - if routines, and ioctl processing global variables arpintq - arp input q ipintrq - ip input queue le_softc - softc structure for le0 etherbroadcastaddr - handle place to get 0xffffffffffff softc may be array unit 0, unit 1, NLE (number of LEs) stats in ifnet structure, see Figure 4.5 4.3 ethernet interface driver writing rules: 1. start with as similar as possible a driver ... same bus 2. find hw example if at all possible; e.g., same driver from another open source OS 3. get hw documentation Ethernet.2 (DIX) encapsulation: Figure 4.8 802.3 is alternative (LLC takes 8 more bytes out for no apparent reason). See RFC 894/1042. arp does translation. ethernet addresses come in 2.5 forms, unicast/multicast/broadcast Figure 4.9 -- ethernet device driver structures/routines overview ether_output: q and try to start ... leread function, Figure 4.11 note: data frame ptr passed into this ... other drivers might work differently note: ethernet frame itself is at front of buffer, other drivers might work differently note: log message about RUNT is extremely unlikely in this day and age note: not all multicast are IP multicast; e.g., Cisco CDP 802.1d spanning tree note: not a good idea to send promiscuous mode packets up the "in" spigot if they are not for us. Yes, give them to bpf, but don't give them to IP layer. note: m_devget may perhaps be used in older drivers for klunky hw ether_input function: Figure 4.13 called by read routine. note how eh is passed on the side. note that in general you are NOT getting the ethernet header from a "normal" socket or a raw socket for that matter. if we are not UP drop packet increment input bytes ... (common code, not done by all drivers ...) switch on type if IP ... "schednetisr" (turn on ip side sw interrupt) set q ptr. ISO case dealt with at this point ... but we won't look at it splimp drop/q pkt for ip splx ip/arp input queues at 50 pkts max ipintr/arpintr called ... ether_output function: Figure 4.15 ip determines routing ... produces ptr to "interface" (if) ip calls if_output function, uses indirect pointer to call ether_output ... encapsulates with ethernet header places pkt on send q 4 phases: routing arp args: ifp m0 - pkt chain dst - destination addr in sockaddr rt0 - routing info ether_output may be called by bpf ... (no route) last ditch attempt to find route gateway lookup too why: we need a MAC destination in all cases ... nasty detail: arp table is same as routing table, just marked with LL flags clone route: ... inserted by ifconfig. arp -a provides a *view* of the routing table rt_flags & RTF_REJECT arp code gave up ... if arp lookup: arpresolve ... arpresolve buffers 1 packet. calls ether_output again when we have MAC address with that packet. AF_UNSPEC is in case you want to output a pkt, but don't want ARP; i.e., you have the dst MAC figure 4.17 M_PREPEND for ethernet mbuf header eh = mtod ... to get pointer to area for ethernet info copy type/dst/src in figure 4.18 try to q packet call start function if not already started (OACTIVE) count obytes ... note terrible goto ... lestart function: figure 4.19 take frame from output queue and send it into cold cruel wire called top-down or by interrupt handler iff there are more packets on q note how process can pile up packets ... and how async process drives them out will tcp block process? will udp block process? if not running, stop take pkt off of q call "leput" if bpf tap feed to bpf set active flag leput cpies frame to hw buffer ... will try to free frame 4.4 ioctl system call 2 needs here: 1. support a set of generic commands needed by most drivers ... e.g., binding of multicast/unicast ip addresses 2. support a possible wide or non-existant set of device specific calls. E.g., wicontrol is used to do 802.11 specific/bizarre things to the wi0 wireless driver. network interface access structures: ifioctl function: figure 4.22 (# ifconfig -a) SIOCGIFCONF - calls ifconf to return a table of ifreq structures (if list) note how we may search by name (# ifconfig ed0 ...) if interface ioctl not recognize ifioctl forweards to to user-request function of the protocol associated with the socket. ifconf function struct ifreq: figure 4.23 Figure 4.25 kernel/user data structures note: this is mostly for dealing with the set of addresses that can be associated with an interface See Figure 4.27 (kernel) and Figure 4.28 (data returned by SIOCGIFCONF) generic interface ioctl commands SIOCGIFFLAGS/SIOSIFFLAGS G for GET, S for SET wi0: flags=8842 mtu 1500 *********************************** ether 00:60:b3:68:b8:3f media: IEEE 802.11 Wireless Ethernet autoselect (DS/2Mbps) status: no carrier ssid "" 1:"" stationname "FreeBSD WaveLAN/IEEE node" channel 6 authmode OPEN powersavemode OFF powersavesleep 100 wepmode OFF weptxkey 1 if_flags here SIOCGIFMETRIC/SIOCSIFMETRIC ifr_metru (MTU) note up/down: #ifconfig wi0 up (or down) else in Figure 4.29 passed to device ioctl if_down/if_up functions if_down clears queue, pfctlinput PRC_IFDOWN issued on per (IP) address basis. TCP/UDP try to use routing table to find alternate paths. Routing message about loss of interface is sent to any routing daemon using route(4) socket. ethernet/slip/loopback leioctl has something ... others do not ... leioctl SIOCSIFFLAGS use running flag to keep track of whether or not we are actually up. must sync with UP set by upstairs layer ... if not UP but RUNNING hw stops ... set flag to not running if UP now and not running leinit to get running if promiscuous flag changes ... change internal state reset start ... SIOCGIFCONF - get interface config info SIOCGIFFLAGS - get flags SIOCGIFMETRIC SIOCSIFFLAGS SIOCSIFMETRIC