Title: CS 498 Lecture 6 Network Devices
1CS 498 Lecture 6Network Devices
- Jennifer Hou
- Department of Computer Science
- University of Illinois at Urbana-Champaign
- Reading Chapters 5, The Linux Networking
Architecture Design and Implementation of
Network Protocols in the Linux Kernel
2APIs Needed in Project 3
- Chapter 6 Flow of Time, Linux Device Driver
3timer_list structure
- (Defined in ltlinux/timer.hgt)
- struct timer_list
- struct list_head list
- unsigned long expires
- unsigned long data
- void (function)(unsigned long)
-
- jiffies the number of ticks since the last boot
defined in ltlinux/sched.hgt - HZ the number of ticks per second defined in
ltlinux/param.hgt machine dependent. - timer?function will run when jiffies is equal to
or greater than timer?expires.
4Functions Used for Timers
- void init_timer(struct timer_list timer)
- Initializes the timer structure.
- void add_timer(struct timer_list timer)
- Inserts a timer into the global list of active
timers. - mod_timer(struct timer_list timer, unsigned long
expires) - Changes the time at which a timer expires.
- int del_timer(struct timer_list timer)
- Removes a timer from the list before it expires.
- int del_timer_sync(struct timer_list timer)
- Same as del_timer, and guarantees that when the
function returns, the timer function is not
running on any CPU.
5Sleeping and Awakening
- A wake queue is a queue of processes that are
waiting for an event - wait_queue_head_t my_queue
- init_waitqueue_head(my_queue)
- Once the wait queue is declared and initialized,
a process can be put into sleep on this queue.
6Sleeping and Awakening
- sleep_on(wait_queue_head_t queue)
- Puts the process to sleep on this queue not
interruptible. - interruptible_sleep_on(wait_queue_head_t queue)
- sleep_on_timeout(wait_queue_head_t queue, long
timeout) - Puts the process into sleep on this queue, but
the sleep will not be longer than timeout. - interruptible_sleep_on_timeout(wait_queue_head_t
queue, long timeout)
7Sleeping and Awakening
- wake_up(wait_queue_head_t queue)
- Wakes up all the processes that are waiting on
this event queue - wake_up_interruptible(wait_queue_head_t queue)
- Wakes up on the processes that are in
interruptible sleeps. - wake_up_sync(wait_queue_head_t queue)
- wake_up_interruptible_sync(wait_queue_head_t
queue)
8Network Devices
- An interface between software-based protocols and
network adapters. - Two major functions
- Abstract from the technical properties of
network adapters (that implement different
layer-1 and layer-2 protocols and are
manufactured by different vendors). - Provide a uniform interface for access by
protocol instances.
9Network Device Interface
Higher Protocol Instances
Network devices (adapter-independent)
dev.c
netif_rx
dev_queue_xmit
dev_open
dev_close
Network devices interface
net_device
dev-gthard_start_xmit
dev-gtopen
dev-gtstop
Abstraction from Adapter specifics
driver.c
net_start_xmit
net_tx
net_rx
Network driver (adapter-specific)
net_open
net_stop
skb
skb
net_interrupt
skb
10net_device structure
11net_device structure
- Represents a general interface between higher
protocol instances and the hardware used. - Contains a number of function pointers that
points to hardware-specific methods of the
driver. - Are grouped into three categories
- General fields
- Hardware-specific fields
- Network layer fields
- Device driver methods
12General Fields in net_device
- name name of the network device, e.g.,
eth0-eth4, lo (loopback device) - next points to the next net_device in the list
of network devices (that starts with dev_base). - owner a pointer to the module structure of the
module that creates net_device. - ifindex a second identifier to a network device.
- When a new network device is created,
dev_get_index() assigns a new unused index to the
device.
13General Fields in net_device
- state state of the device
- LINK_STATE_START the network adapter has been
opened with dev?open() - netif_running(dev) can test this flag.
- LINK_STATE_XOFF the transmit buffers of the
network adapter are all busy. - netif_queue_stopped(dev) can test this flag.
- netif_stop_queue(dev) sets the LINK_STATE_XOFF
flag, and no packet can be passed onto the
adapter. - netif_start_queue(dev) deletes the
LINK_STATE_XOFF flag, and resumes accepting
packets from the kernel. - Netif_wake_queue(dev) resumes passing packets,
and trigger the NET_TX software interrupt.
14General Fields in net_device
- trans_start the time (in jiffies) when the
transmission of a packet started. - last_rx the time (in jiffies) when the last
packet arrived. - priv a pointer to the private data of a network
device. - qdisk a structure of they type Qdisk, and
governs the serving discipline of the network
device. - refcnt the number of references to the network
device. - xmit_lock, xmit_lock_owner are used for mutual
exclusion on the transmit queue.
15Hardware-Specific Fields
- rmem_end, rmem_start, mem_end, mem_start specify
the common memory space that the network adapter
and the kernel share. - (mem_start-mem_end) designates the buffers for
packets to be sent (rmem_start-rmem_end)
designates the location for received packets. - base_addr I/O basic address set in the drivers
probing routine during the search for a device. - Irq the of the interrupt of a network adapter
set in the drivers probing routine. - dma the number of the DMA channel.
16Hardware-Specific Fields
- hard_header_length specifies the length of the
layer-2 packet header. - mtu maximum transfer unit in a layer-2 frame.
- tx_queue_len specifies the maximum length of the
output queue of the network device. - type specifies the hardware type of the network
adapter, e.g., ARPHRD_ETHER, ARPHRD_IEEE802,
ARPHRD_LOOPBACK
17Hardware-Specific Fields
- addr_len, dev_addrMAX_ADDR_LEN,
broadcastMAX_ADDR_LEN - dev_mc_list points to a linear list that
contains multicast layer-2 addresses. - set_multicast_list() is used to pass the list to
the network adapter. - mc_count the number of addresses in dev_mc_list.
18Hardware-Specific Fields
- watchdog_timer is initialized when a network
device starts. - The handling routine dev_watchdog() is called
after watchdog_timeo time units, and checks
whether or not watchdog_timeo time units have
passed since the last transmission of a packet. - Yes, tx_timeout() of the driver is called.
- No, the watchdog timer is restarted.
-
19Network Layer Fields
- ip_ptr, ip6_ptr point to the information of
layer 3 protocols that use the network device,
e.g., ip_ptr points to a structure of type
in_device (that keeps track of a list of IP
addresses of the network device, a list of IP
mluticast groups, and the parameters for the ARP
protocol). - family designates the address family of the
network device, e.g., AF_INET - pa_alen the length of the addresses, e.g., 4
bytes. - pa_addr, pa_braddr, pa_mask the address, the
broadcast address, and the network mask. - pa_dstaddr specifies the address of the other
partner in a point-to-point connection.
20Network Layer Fields
- flags properties of the network device
- IFF_UP the device is on.
- IFF_ARP the device supports ARP
- IFF_BROADCAST the device is broadcast-enabled.
- IFF_LOOKBACK the device is a loopback network
device. - IFF_POINTTOPOINT this is a point-to-point
connection. - IFF_PROMISC this flag switches the promiscuous
mode on. - IFF_MULTICAST activates the receipt of multicast
packets.
21Device-Driver Methods
- init() is used to initialize network devices.
- uninit() is called when the network device is
unregistered (unregister_netdevice()). - destructor() is called when dev?refcnt0.
- open() opens a named network device. ifconfig
calls dev_open() which in turn calls dev?open() - stop() terminates the activity of a network
adapter. - hard_start_xmit() transmit a socket buffer over
the network device.
22Device Driver Methods
- get_stats() gets statistics and information
about the network device. - get_wireless_stats() returns additional
information for wireless network adapters. - set_multicast_list() passes the list of
multicast MAC addresses to the network adapter. - tx_timeout() is called when no acknowledgement
for the packet is received after watchdog_timeo
time units. - set_config() is used to change the configuration
of a network adapter. - do_ioctl() is used by higher protocols to pass
adapter-specific ioctl() commands to the network
adapter.
23Device Driver Methods
- hard_header() creates a layer-2 packet header.
- rebuild_header() is called only when the hard
header cache contains wrong information. - hard_header_cache() fills a layer-2 packet
header in the hard header cache with passed data.
Subsequent transmission processes can access a
layer-2 packet header. - header_cache_update() changes the layer-2
destination address in a stored layer-2 packet
header in the hard header cache.
24Device Driver Methods
- hard_header_parse() reads the layer-2 sender
address from the packet data space of a socket
buffer and copies it to the passed address,
haddr. - set_mac_address() Is used to change the layer-2
address of a network adapter. - change_mtu() changes the MTU of a network
adapter.
25Managing Network Device
26dev_base List
- All the network devices in the Linux kernel are
connected in a linear list. - /proc/net/dev or ifconfig a gives the list of
currently registered devices. - When the system is booted or when the driver
module is loaded, an appropriate adapter is
found. Then a net_device structure is created,
initialized and added to the list with
register_netdevice().
27List of Network Devices
name eth1
dev_base
state
net_device
next
priv
net_device
...
name eth0
Hardware
state
local2private driver structure
next
MAC layer
priv
...
Network layer
Hardware
open
MAC layer
adapter2_open()
stop
adapter2_stop()
hard_start_xmit
Network layer
...
adapter_start_xmit()
open
adapter_open()
stop
adapter_stop()
hard_start_xmit
adapter_start_xmit()
...
local1private driver structure
28Functions for Managing Network Devices
- init_netdev(dev,sizeof_priv,mask,setup)
- If dev is null, call net_alloc_dev() to create a
new net_device structure. - If dev?name is empty, call dev_alloc_name() to
allocate a name. - mask can be used to specify a prefix, e.g.,
testd produces test0, test1, etc. - The list dev_boot_setup in the method
netdev_boot_setup_check() is searched for an
entry with a matched name. If there is such an
entry, then the hardware specific fields (e.g.,
mem_start, mem_end, base_addr) are filled out.
29Functions for Managing Network Devices
- init_netdev(dev,sizeof_priv,mask,setup)
- The function which setup points to is used to
initialize layer-2 fields. For example, setup may
points to ether_setup(dev). - The net_device is added to dev_base by
register_netdevice(). - init_netdev() returns with a pointer to the
net_device structure.
30init_etherdev()
- struct net_device init_etherdev(struct
net_device dev, int sizeof_priv) -
- return init_netdev(dev, sizeof_priv, "ethd",
ether_setup) -
31ether_setup
- void ether_setup(struct net_device dev)
-
- / Fill in the fields of the device structure
with ethernet-generic values. / - dev-gtchange_mtu eth_change_mtu
- dev-gthard_header eth_header
- dev-gtrebuild_header eth_rebuild_header
- dev-gtset_mac_address eth_mac_addr
- dev-gthard_header_cache eth_header_cache
- dev-gtheader_cache_update eth_header_cache_upd
ate - dev-gthard_header_parse eth_header_parse
- dev-gttype ARPHRD_ETHER
- dev-gthard_header_len ETH_HLEN
- dev-gtmtu 1500 / eth_mtu /
- dev-gtaddr_len ETH_ALEN
- dev-gttx_queue_len 1000 / Ethernet wants
good queues / - memset(dev-gtbroadcast,0xFF, ETH_ALEN)
- dev-gtflags IFF_BROADCASTIFF_MULTICAST
-
32register_netdevice()
- If dev?init() exists, the network adapter is
first searched and initialized with the function
init(). - If a net_device with the same name is already in
the list dev_base, return an error message
otherwise, add the dev_device, dev, to the list. - Sets dev?state to LINK_STATE_PRESENT
33register_netdevice()
- dev_init_scheduler() (i) sets the scheduling
discipline to FIFO (ii) initializes the timer
dev?watchdog_timer with dev_watchdog_init(). The
timer is not started until dev_open() is invoked. - The network device is assigned a new index
(dev?ifindex). - notifier_call_chain(netdev_chain,
NETDEV_REGISTER, dev) calls all the registered
methods in netdev_chain
34unregister_netdevice(dev)
- Remove dev from the dev_base list.
- If dev?flagIFF_UP, dev?close() is called to
close the device. - The queueing discipline is released with
dev_shutdown(). - All registered functions in the notification
chain netdev_chain are informed about the
NETDEV_UNREGISTER event.
35dev_open()
- If (dev?flags IFF_UP) (!netif_device_present(
dev)), returns with an error message. - Invokes the open() function of the network
driver. - After the initialization,
- dev? flags IFF_UP
- dev? state LINK_STATE_START
- The multicast state is activated with
dev_mc_upload() - The queueing and scheduling disciplines are
activated with dev_activate(dev). - dev(watchdog_up(dev) starts the timer. The timer
calls dev_watchdog() every dev?watchdog_timeo
ticks to check for transmission problems. - The notification chain netdev_chain is informed
of the event NETDEV_UP.
36dev_close(dev)
- dev_deactivate(dev) removes the packet scheduler
dev?qdisc. - The bit LINK_STATE_START in dev?state is deleted.
- dev_watchdog_down() stops the timer.
- dev?stop() deactivates the network adapter.
- notifier_call_chain(netdev_chain, NETDEV_DOWN,
dev). - dev?refcnt is decremented by one.
37dev_get_()
- dev_get_by_name(name) and dev_get(name) search
for the network device specified by name. - dev_get_by_index(ifindex)
- dev_getbyhwaddr(type,ha) searches for the network
device by the MAC address ha and the type.
38Notification Chain
- notifier_call_chain(netdev_chain, EVENT, dev)
- notifier_chain_register(netdev_chain,nb)
- notifier_chain_unregister(nb)