4. 4
IEEE 802.3 frame vs. Ethernet II
https://www.safaribooksonline.com/library/view/junos-enterprise-switching/9780596804244
5. 5
net_device structure
• Defined in the <include/linux/netdevice.h>
• This structure represents a network interface
• Doubly-linked list all net_device
• Allocation with alloc_netdev()
• alloc_etherdev() is a specialization of alloc_netdev()
for ethernet interfaces
6. 6
sk_buff structure
• Defined in the <include/linux/skbuff.h>
• The socket buffer, is the most fundamental data
structure in the Linux networking code
• A doubly linked list
• Every packet sent or received is handled using this
data structure
• Allocation with dev_alloc_skb()
7. 7
Interact between NIC device and
Kernel
• Polling
‒ Kernel side check the device constantly if new data is
available
• Interrupt
‒ NIC informs the kernel when frame is received
‒ Transmission complete, transmission error
• NAPI (New API)
‒ A mix of polling and interrupts mode
9. 9
Linux kernel module example
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init init_hello(void)
{
printk(KERN_INFO "Hello, world !n");
return 0;
}
static void __exit cleanup_hello(void)
{
printk(KERN_INFO "Goodbye, world !n");
}
module_init(init_hello);
module_exit(cleanup_hello);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Chang"); /* Who wrote this module? */
MODULE_DESCRIPTION("Simple Hello World");/* What does this module do */
17. 17
labnet_start_xmit()
static int labnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
int len;
char *data;
len = skb->len;
data = skb->data;
dev->trans_start = jiffies; /* save the timestamp */
stats->tx_packets += 1;
stats->tx_bytes += skb->len;
/* actual deliver of data is device-specific */
hw_tx(data, len, dev);
/* loopback it to receive */
labnet_rx(skb, dev);
return 0;
}
19. 19
Virtual / Real network device
• Each network device is represented by a instance of
net_device structure
• Virtual Devices:
‒ Build on top of a real (or virtual) device
‒ Bonding, VLAN (802.1Q), Bridging, Aliasing interface, IP
over IP (IPIP), GRE
‒ Similar handling like real devices (register device et cetera)
21. 21
Another way
• /proc/net: various networking parameters and
statistics
• /proc/sys/net: concerning various networking topics
• /sys/class/net: The class directory contains
representations of every device class that is
registered with the kernel.