SlideShare uma empresa Scribd logo
1 de 22
Baixar para ler offline
Hands-on ethernet driver
David Chang
Software Engineer
dchang@suse.com
2
Agenda
• Introduction
• Data structure of network stack
• A brief look at linux ethernet driver
• Tips for linux ethernet driver
3
Basic layers of the network stack
4
IEEE 802.3 frame vs. Ethernet II
https://www.safaribooksonline.com/library/view/junos-enterprise-switching/9780596804244
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
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
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
8
Reception / Transmission
Eth0
skb → protocol
netif_rx/netif_rx_schedule
do_softirq / NET_RX_ACTION
netif_receive_skb
ip_rcv
hard_start_xmit
dev_queue_xmit
Traffic Control
RX TX
IRQ
L3
Driver
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 */
10
An sample ethernet driver layout
#include <linux/module.h>
#include <linux/init.h>
#include <linux/netdevice.h>
static struct net_device *dev;
static struct net_device_stats *stats;
…
labnet_open()
labnet_close()
labnet_setup()
labnet_start_xmit()
labnet_get_stats()
labnet_init()
labnet_exit()
11
labnet_init()
static int __init labnet_init(void)
{
dev = alloc_netdev(0, "labnet%d", NET_NAME_UNKNOWN,
labnet_setup);
if (register_netdev(dev)) {
pr_info("Register netdev failed!n");
free_netdev(dev);
return -1;
}
return 0;
}
12
labnet_exit()
static void __exit labnet_exit(void)
{
unregister_netdev(dev);
free_netdev(dev);
}
13
labnet_setup()
static void labnet_setup(struct net_device *dev)
{
int i;
/* Assign the MAC address */
for (i = 0; i < ETH_ALEN; ++i) {
dev->dev_addr[i] = (char)i;
}
ether_setup(dev);
dev->netdev_ops = &ndo;
dev->flags |= IFF_NOARP;
stats = &dev->stats;
}
14
net_device_ops
static struct net_device_ops ndo = {
.ndo_open = labnet_open,
.ndo_stop = labnet_close,
.ndo_start_xmit = labnet_start_xmit,
.ndo_do_ioctl = labnet_do_ioctl,
.ndo_get_stats = labnet_get_stats,
};
15
labnet_open()
static int labnet_open(struct net_device *dev)
{
netif_start_queue(dev);
return 0;
}
16
labnet_close()
static int labnet_close(struct net_device *dev)
{
netif_stop_queue(dev);
return 0;
}
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;
}
18
labnet_rx()
static void labnet_rx(struct sk_buff *skb, struct net_device *dev)
{
stats->rx_packets += 1;
stats->rx_bytes += skb->len;
skb->protocol = eth_type_trans( skb, dev );
netif_rx(skb);
}
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)
20
Statistic
• ip -s -s link show eth0
• netstat -s (statistics for each protocol)
• ethtool -S eth0
• /sys/class/net/eth0/statistics/
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether f0:de:f1:19:20:57 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
12094943228 10939498 0 265160 0 27052
RX errors: length crc frame fifo missed
0 0 0 0 10535
TX: bytes packets errors dropped carrier collsns
651092971 6667618 0 0 0 0
TX errors: aborted fifo window heartbeat transns
0 0 0 0 164
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.
22
Thank you!

Mais conteúdo relacionado

Mais procurados

The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
hugo lu
 

Mais procurados (20)

Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_maps
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network Interfaces
 
Qemu device prototyping
Qemu device prototypingQemu device prototyping
Qemu device prototyping
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
Anatomy of the loadable kernel module (lkm)
Anatomy of the loadable kernel module (lkm)Anatomy of the loadable kernel module (lkm)
Anatomy of the loadable kernel module (lkm)
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
spinlock.pdf
spinlock.pdfspinlock.pdf
spinlock.pdf
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
COSCUP 2020 RISC-V 32 bit linux highmem porting
COSCUP 2020 RISC-V 32 bit linux highmem portingCOSCUP 2020 RISC-V 32 bit linux highmem porting
COSCUP 2020 RISC-V 32 bit linux highmem porting
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Linux DMA Engine
 

Destaque

Develop and Maintain a Distro with Open Build Service
Develop and Maintain a Distro with Open Build ServiceDevelop and Maintain a Distro with Open Build Service
Develop and Maintain a Distro with Open Build Service
SUSE Labs Taipei
 

Destaque (19)

Use build service API in your program
Use build service API in your programUse build service API in your program
Use build service API in your program
 
Use bonding driver with ethernet
Use bonding driver with ethernetUse bonding driver with ethernet
Use bonding driver with ethernet
 
eBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to UserspaceeBPF Trace from Kernel to Userspace
eBPF Trace from Kernel to Userspace
 
Ethernet and TCP optimizations
Ethernet and TCP optimizationsEthernet and TCP optimizations
Ethernet and TCP optimizations
 
S4 sig-check-lpc-20130918
S4 sig-check-lpc-20130918S4 sig-check-lpc-20130918
S4 sig-check-lpc-20130918
 
Develop and Maintain a Distro with Open Build Service
Develop and Maintain a Distro with Open Build ServiceDevelop and Maintain a Distro with Open Build Service
Develop and Maintain a Distro with Open Build Service
 
Coscup 2012-urfkill
Coscup 2012-urfkillCoscup 2012-urfkill
Coscup 2012-urfkill
 
NVDIMM block drivers with NFIT
NVDIMM block drivers with NFITNVDIMM block drivers with NFIT
NVDIMM block drivers with NFIT
 
Networking in linux
Networking in linuxNetworking in linux
Networking in linux
 
An Internet Based Interactive Data Acquisition System
An Internet Based Interactive Data Acquisition System An Internet Based Interactive Data Acquisition System
An Internet Based Interactive Data Acquisition System
 
Linux Network commands
Linux Network commandsLinux Network commands
Linux Network commands
 
Chapter09 -- networking with unix and linux
Chapter09  -- networking with unix and linuxChapter09  -- networking with unix and linux
Chapter09 -- networking with unix and linux
 
introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Interrupts
InterruptsInterrupts
Interrupts
 
LinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking WalkthroughLinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking Walkthrough
 
Library Operating System for Linux #netdev01
Library Operating System for Linux #netdev01Library Operating System for Linux #netdev01
Library Operating System for Linux #netdev01
 

Semelhante a Hands-on ethernet driver

[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN
[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN
[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN
OpenStack Korea Community
 
20151222_Interoperability with ML2: LinuxBridge, OVS and SDN
20151222_Interoperability with ML2: LinuxBridge, OVS and SDN20151222_Interoperability with ML2: LinuxBridge, OVS and SDN
20151222_Interoperability with ML2: LinuxBridge, OVS and SDN
Sungman Jang
 
Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!
Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!
Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!
Priyanka Aash
 

Semelhante a Hands-on ethernet driver (20)

Using Netconf/Yang with OpenDalight
Using Netconf/Yang with OpenDalightUsing Netconf/Yang with OpenDalight
Using Netconf/Yang with OpenDalight
 
Intermediate Intel® Distribution of OpenVINO™ toolkit for Computer Vision Ap...
 Intermediate Intel® Distribution of OpenVINO™ toolkit for Computer Vision Ap... Intermediate Intel® Distribution of OpenVINO™ toolkit for Computer Vision Ap...
Intermediate Intel® Distribution of OpenVINO™ toolkit for Computer Vision Ap...
 
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
 
Java
JavaJava
Java
 
ENSA_Module_12.pptx
ENSA_Module_12.pptxENSA_Module_12.pptx
ENSA_Module_12.pptx
 
[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN
[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN
[OpenStack 하반기 스터디] Interoperability with ML2: LinuxBridge, OVS and SDN
 
20151222_Interoperability with ML2: LinuxBridge, OVS and SDN
20151222_Interoperability with ML2: LinuxBridge, OVS and SDN20151222_Interoperability with ML2: LinuxBridge, OVS and SDN
20151222_Interoperability with ML2: LinuxBridge, OVS and SDN
 
Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...
Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...
Lessons Learned Running InfluxDB Cloud and Other Cloud Services at Scale by T...
 
Network automation with Ansible and Python
Network automation with Ansible and PythonNetwork automation with Ansible and Python
Network automation with Ansible and Python
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 
Linux networking
Linux networkingLinux networking
Linux networking
 
IoT Best practices
 IoT Best practices IoT Best practices
IoT Best practices
 
Mod04 debuggers
Mod04 debuggersMod04 debuggers
Mod04 debuggers
 
Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!
Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!
Breaking Parser Logic: Take Your Path Normalization Off and Pop 0days Out!
 
Advanced .NET Data Access with Dapper
Advanced .NET Data Access with Dapper Advanced .NET Data Access with Dapper
Advanced .NET Data Access with Dapper
 
/etc/rc.d配下とかのリーディング勉強会
/etc/rc.d配下とかのリーディング勉強会/etc/rc.d配下とかのリーディング勉強会
/etc/rc.d配下とかのリーディング勉強会
 
Java Future S Ritter
Java Future S RitterJava Future S Ritter
Java Future S Ritter
 
Network Automation Tools
Network Automation ToolsNetwork Automation Tools
Network Automation Tools
 
The n00bs guide to ovs dpdk
The n00bs guide to ovs dpdkThe n00bs guide to ovs dpdk
The n00bs guide to ovs dpdk
 

Mais de SUSE Labs Taipei

Mais de SUSE Labs Taipei (15)

Locked down openSUSE Tumbleweed kernel
Locked down openSUSE Tumbleweed kernelLocked down openSUSE Tumbleweed kernel
Locked down openSUSE Tumbleweed kernel
 
SUSE shim and things related to it
SUSE shim and things related to itSUSE shim and things related to it
SUSE shim and things related to it
 
Multi-signed Kernel Module
Multi-signed Kernel ModuleMulti-signed Kernel Module
Multi-signed Kernel Module
 
ACPI Debugging from Linux Kernel
ACPI Debugging from Linux KernelACPI Debugging from Linux Kernel
ACPI Debugging from Linux Kernel
 
Profiling the ACPICA Namespace and Event Handing
Profiling the ACPICA Namespace and Event HandingProfiling the ACPICA Namespace and Event Handing
Profiling the ACPICA Namespace and Event Handing
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSE
 
The bright future of SUSE and openSUSE
The bright future of SUSE and openSUSEThe bright future of SUSE and openSUSE
The bright future of SUSE and openSUSE
 
EFI Secure Key
EFI Secure KeyEFI Secure Key
EFI Secure Key
 
eBPF maps 101
eBPF maps 101eBPF maps 101
eBPF maps 101
 
Convert your package to multibuild on Open Build Service
Convert your package to multibuild on Open Build ServiceConvert your package to multibuild on Open Build Service
Convert your package to multibuild on Open Build Service
 
Ixgbe internals
Ixgbe internalsIxgbe internals
Ixgbe internals
 
Linux Linux Traffic Control
Linux Linux Traffic ControlLinux Linux Traffic Control
Linux Linux Traffic Control
 
Looking into trusted and encrypted keys
Looking into trusted and encrypted keysLooking into trusted and encrypted keys
Looking into trusted and encrypted keys
 
openSUSE12.2 Review
openSUSE12.2 ReviewopenSUSE12.2 Review
openSUSE12.2 Review
 
oS KDE Repos & MM
oS KDE Repos & MMoS KDE Repos & MM
oS KDE Repos & MM
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Último (20)

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 

Hands-on ethernet driver

  • 1. Hands-on ethernet driver David Chang Software Engineer dchang@suse.com
  • 2. 2 Agenda • Introduction • Data structure of network stack • A brief look at linux ethernet driver • Tips for linux ethernet driver
  • 3. 3 Basic layers of the network stack
  • 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
  • 8. 8 Reception / Transmission Eth0 skb → protocol netif_rx/netif_rx_schedule do_softirq / NET_RX_ACTION netif_receive_skb ip_rcv hard_start_xmit dev_queue_xmit Traffic Control RX TX IRQ L3 Driver
  • 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 */
  • 10. 10 An sample ethernet driver layout #include <linux/module.h> #include <linux/init.h> #include <linux/netdevice.h> static struct net_device *dev; static struct net_device_stats *stats; … labnet_open() labnet_close() labnet_setup() labnet_start_xmit() labnet_get_stats() labnet_init() labnet_exit()
  • 11. 11 labnet_init() static int __init labnet_init(void) { dev = alloc_netdev(0, "labnet%d", NET_NAME_UNKNOWN, labnet_setup); if (register_netdev(dev)) { pr_info("Register netdev failed!n"); free_netdev(dev); return -1; } return 0; }
  • 12. 12 labnet_exit() static void __exit labnet_exit(void) { unregister_netdev(dev); free_netdev(dev); }
  • 13. 13 labnet_setup() static void labnet_setup(struct net_device *dev) { int i; /* Assign the MAC address */ for (i = 0; i < ETH_ALEN; ++i) { dev->dev_addr[i] = (char)i; } ether_setup(dev); dev->netdev_ops = &ndo; dev->flags |= IFF_NOARP; stats = &dev->stats; }
  • 14. 14 net_device_ops static struct net_device_ops ndo = { .ndo_open = labnet_open, .ndo_stop = labnet_close, .ndo_start_xmit = labnet_start_xmit, .ndo_do_ioctl = labnet_do_ioctl, .ndo_get_stats = labnet_get_stats, };
  • 15. 15 labnet_open() static int labnet_open(struct net_device *dev) { netif_start_queue(dev); return 0; }
  • 16. 16 labnet_close() static int labnet_close(struct net_device *dev) { netif_stop_queue(dev); return 0; }
  • 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; }
  • 18. 18 labnet_rx() static void labnet_rx(struct sk_buff *skb, struct net_device *dev) { stats->rx_packets += 1; stats->rx_bytes += skb->len; skb->protocol = eth_type_trans( skb, dev ); netif_rx(skb); }
  • 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)
  • 20. 20 Statistic • ip -s -s link show eth0 • netstat -s (statistics for each protocol) • ethtool -S eth0 • /sys/class/net/eth0/statistics/ 3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether f0:de:f1:19:20:57 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 12094943228 10939498 0 265160 0 27052 RX errors: length crc frame fifo missed 0 0 0 0 10535 TX: bytes packets errors dropped carrier collsns 651092971 6667618 0 0 0 0 TX errors: aborted fifo window heartbeat transns 0 0 0 0 164
  • 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.