SlideShare a Scribd company logo
1 of 14
ETHERNET DEVICE DRIVER 
1
Application 
Socket interface 
TCP/UDP protocol 
IP protocol 
2 
網路Driver架構 
Ethernet Hardware Device Driver
它收送都是有封包關念 
它是一種broadcast的網路封包 
MAC位址必需向世界組織申請,在同一個 
local LAN需是唯一的,並且它只存在local 
LAN,在internet中會被去除。 
3 
認識ethernet封包 
Dest MAC Source MAC Type Payload 
6 bytes 6 bytes 2 bytes Max 1566 bytes 
Max 1580 bytes
4 
開發流程 
宣告struct 
net_device 結構變數 
或 
用alloc_etherdev() 
設定必要的callback 
fuction在上述的結構 
中 
呼叫 
register_netdev()註 
冊該driver 
系統呼叫init callback 
function初始化該元件 
啓動該介面時系統會 
呼叫open callback 
function 
送資料時系統呼叫 
hard_start_xmit 
callback function 
當收到資料時 
可呼叫 
netif_rx()往 
上層送資料
struct net_device 
struct net_device 
{ 
char name[IFNAMSIZ]; 
………………………………….. 
unsigned long base_addr; /* device I/O address */ 
unsigned int irq; /* device IRQ number */ 
struct net_device_stats* (*get_stats)(struct net_device *dev); 
const struct ethtool_ops *ethtool_ops; 
int (*open)(struct net_device *dev); 
int (*stop)(struct net_device *dev); 
void (*set_multicast_list)(struct net_device *dev); 
int (*do_ioctl)(struct net_device *dev, 
struct ifreq *ifr, int cmd); 
void (*tx_timeout) (struct net_device *dev); 
unsigned char dev_addr[MAX_ADDR_LEN]; /* hw address, 
(before bcast 
because most packets are unicast) */ 
……………………………………. 
}
Module init example (dm9000.c) 
static struct platform_driver dm9000_driver = { 
.driver = { 
.name = "dm9000", 
.owner = THIS_MODULE, 
}, 
.probe = dm9000_probe, 
.remove = __devexit_p(dm9000_drv_remove), 
.suspend = dm9000_drv_suspend, 
.resume = dm9000_drv_resume, 
}; 
static int __init dm9000_init(void) 
{ 
printk(KERN_INFO "%s Ethernet Driver, V%sn", CARDNAME, DRV_VERSION); 
return platform_driver_register(&dm9000_driver); 
} 
static void __exitdm9000_cleanup(void) 
{ 
platform_driver_unregister(&dm9000_driver); 
} 
module_init(dm9000_init); 
module_exit(dm9000_cleanup);
Probe example (dm9000.c) 
static int __devinit dm9000_probe(struct platform_device *pdev) 
{ 
………………………. 
ndev = alloc_etherdev(sizeof(struct board_info)); 
……………………… 
db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 
……………………. 
db->addr_req = request_mem_region(db->addr_res->start, iosize, 
pdev->name); 
db->io_addr = ioremap(db->addr_res->start, iosize); 
/* fill in parameters for net-dev structure */ 
ndev->base_addr = (unsigned long)db->io_addr; 
ndev->irq = db->irq_res->start; 
/* driver system function */ 
ether_setup(ndev); 
ndev->open = &dm9000_open; 
ndev->hard_start_xmit = &dm9000_start_xmit; 
ndev->tx_timeout = &dm9000_timeout; 
ndev->watchdog_timeo = msecs_to_jiffies(watchdog); 
ndev->stop = &dm9000_stop; 
ndev->set_multicast_list = &dm9000_hash_table; 
ndev->ethtool_ops = &dm9000_ethtool_ops; 
ndev->do_ioctl = &dm9000_ioctl; 
………………. 
ret = register_netdev(ndev); 
return 0; 
}
上傳資料 
1) 可用interrupt service routine or polling將底層的資 
料收進來 
2) 呼叫netif_rx()上傳資料 
下傳資料 
1) 上層將會呼叫所註冊的hard_start_xmit() callback 
function 
Allocate buffer kernel API 
1) struct sk_buff *alloc_skb(unsigned int size,gfp_t 
priority) 
8 
如何上下傳資料
Start xmit example 
/* Move data to DM9000 TX RAM */ 
writeb(DM9000_MWCMD, db->io_addr); 
(db->outblk)(db->io_data, skb->data, skb->len); 
dev->stats.tx_bytes += skb->len; 
db->tx_pkt_cnt++; 
/* TX control: First packet immediately send, second packet queue */ 
if (db->tx_pkt_cnt == 1) { 
/* Set TX length to DM9000 */ 
iow(db, DM9000_TXPLL, skb->len); 
iow(db, DM9000_TXPLH, skb->len >> 8); 
/* Issue TX polling command */ 
iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */ 
dev->trans_start = jiffies; /* save the time stamp */ 
} else { 
/* Second packet */ 
db->queue_pkt_len = skb->len; 
netif_stop_queue(dev); 
} 
/* free this SKB */ 
dev_kfree_skb(skb); 
return 0;
Receive packet interrupt example 
if (GoodPacket 
&& ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) { 
skb_reserve(skb, 2); 
rdptr = (u8 *) skb_put(skb, RxLen - 4); 
/* Read received packet from RX SRAM */ 
(db->inblk)(db->io_data, rdptr, RxLen); 
dev->stats.rx_bytes += RxLen; 
/* Pass to upper layer */ 
skb->protocol = eth_type_trans(skb, dev); 
netif_rx(skb); 
dev->stats.rx_packets++; 
} else { 
/* need to dump the packet's data */ 
(db->dumpblk)(db->io_data, RxLen); 
}
struct sk_buff { 
struct sk_buff *next; 
struct sk_buff *prev; 
………………………….. 
unsigned char *data; 
unsigned int len; 
…………. 
} 
11 
skb buffer stream 
struct sk_buff { 
struct sk_buff *next; 
struct sk_buff *prev; 
………………………….. 
unsigned char *data; 
unsigned int len; 
…………. 
} 
Data 
buffer 
Data 
buffer
Tx/Rx Buffer Management 
12 
Buffer Descriptor 
Start Address 
Register 
+ 
BRXBDCNT+0 
… … 
BDMATXDPTR 
buffer pointer #1 
status length 
buffer pointer #2 
status length 
buffer pointer #N 
status length 
Buffer Descriptor Rx Buffer 
… … … 
buffer #1 
not used 
buffer #2 
not used 
buffer #N 
not used 
Memory for frame 
BRXBDCNT+1 
BRXBDCNT+(N-1) 
BRxBS of 
BDMARXLEN 
BRxBS of 
BDMARXLEN 
BRxBS of 
BDMARXLEN 
BDMA buffer 
descriptor 
counter of 
current pointer 
… 
Memory for Rx buffer descriptor
skb buffer由下而上時是由ethernet device 
driver allocate,而由上層的TCP free 
skb buffer由上而下時是由TCP allocate,而 
由底層的ethernet free 
其中在open的call back function需要 
netif_start_queue() 
在close時需要netif_stop_queue() 
在timeout時需要netif_wake_queue() 
13 
注意事項
若網路元件有支援DMA功能,則盡量使用 
以提供網路傳輸效能。 
若網路元件和CPU之間有共用的記憶體,記 
得必需要設定為non-cache。 
在中斷程式中最好不要做搬動資料的動作, 
最好使用schedule_work()來完成,可是這會 
有一個問題就是較不real-time。 
研讀程式drivers/net/my_mac.c 
14 
注意事項

More Related Content

What's hot

What's hot (20)

Spi drivers
Spi driversSpi drivers
Spi drivers
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
 
Bootloaders
BootloadersBootloaders
Bootloaders
 
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
 
Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
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
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDP
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDPDockerCon 2017 - Cilium - Network and Application Security with BPF and XDP
DockerCon 2017 - Cilium - Network and Application Security with BPF and XDP
 
Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking Mechanisms
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
Programming guide for linux usb device drivers
Programming guide for linux usb device driversProgramming guide for linux usb device drivers
Programming guide for linux usb device drivers
 
Grub2 Booting Process
Grub2 Booting ProcessGrub2 Booting Process
Grub2 Booting Process
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
Shell and its types in LINUX
Shell and its types in LINUXShell and its types in LINUX
Shell and its types in LINUX
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot)
 
Ixgbe internals
Ixgbe internalsIxgbe internals
Ixgbe internals
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 

Viewers also liked

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

Viewers also liked (20)

Ethernet and TCP optimizations
Ethernet and TCP optimizationsEthernet and TCP optimizations
Ethernet and TCP optimizations
 
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
 
運動腰帶
運動腰帶運動腰帶
運動腰帶
 
Three Reasons your Qualifying Income may Cause Confusion on an FHA Loan
Three Reasons your Qualifying Income may Cause Confusion on an FHA LoanThree Reasons your Qualifying Income may Cause Confusion on an FHA Loan
Three Reasons your Qualifying Income may Cause Confusion on an FHA Loan
 
物流系統解決方案
物流系統解決方案物流系統解決方案
物流系統解決方案
 
艾鍗學院-單晶片韌體-CC2500通訊實驗
艾鍗學院-單晶片韌體-CC2500通訊實驗艾鍗學院-單晶片韌體-CC2500通訊實驗
艾鍗學院-單晶片韌體-CC2500通訊實驗
 
Linux SD/MMC device driver
Linux SD/MMC device driverLinux SD/MMC device driver
Linux SD/MMC device driver
 
艾鍗學院-單晶片韌體開發- LCM模組實驗
艾鍗學院-單晶片韌體開發- LCM模組實驗艾鍗學院-單晶片韌體開發- LCM模組實驗
艾鍗學院-單晶片韌體開發- LCM模組實驗
 
Verilog 語法教學
Verilog 語法教學 Verilog 語法教學
Verilog 語法教學
 
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
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
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
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
LinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking WalkthroughLinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking Walkthrough
 

Similar to Linux Ethernet device driver

Owasp Indy Q2 2012 Advanced SQLi
Owasp Indy Q2 2012 Advanced SQLiOwasp Indy Q2 2012 Advanced SQLi
Owasp Indy Q2 2012 Advanced SQLi
owaspindy
 
Sedna XML Database: Executor Internals
Sedna XML Database: Executor InternalsSedna XML Database: Executor Internals
Sedna XML Database: Executor Internals
Ivan Shcheklein
 

Similar to Linux Ethernet device driver (20)

Xdp and ebpf_maps
Xdp and ebpf_mapsXdp and ebpf_maps
Xdp and ebpf_maps
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
Analyzing the Performance Effects of Meltdown + Spectre on Apache Spark Workl...
 
Genode Compositions
Genode CompositionsGenode Compositions
Genode Compositions
 
Sockets and Socket-Buffer
Sockets and Socket-BufferSockets and Socket-Buffer
Sockets and Socket-Buffer
 
VLANs in the Linux Kernel
VLANs in the Linux KernelVLANs in the Linux Kernel
VLANs in the Linux Kernel
 
Intel DPDK Step by Step instructions
Intel DPDK Step by Step instructionsIntel DPDK Step by Step instructions
Intel DPDK Step by Step instructions
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
 
Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545
 
Introduction to Apache Mesos
Introduction to Apache MesosIntroduction to Apache Mesos
Introduction to Apache Mesos
 
Owasp Indy Q2 2012 Advanced SQLi
Owasp Indy Q2 2012 Advanced SQLiOwasp Indy Q2 2012 Advanced SQLi
Owasp Indy Q2 2012 Advanced SQLi
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
Apache Spark Workshop
Apache Spark WorkshopApache Spark Workshop
Apache Spark Workshop
 
Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernel
 
netLec2.pdf
netLec2.pdfnetLec2.pdf
netLec2.pdf
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
Scaling IO-bound microservices
Scaling IO-bound microservicesScaling IO-bound microservices
Scaling IO-bound microservices
 
Using Netconf/Yang with OpenDalight
Using Netconf/Yang with OpenDalightUsing Netconf/Yang with OpenDalight
Using Netconf/Yang with OpenDalight
 
Logical volume manager xfs
Logical volume manager xfsLogical volume manager xfs
Logical volume manager xfs
 
Sedna XML Database: Executor Internals
Sedna XML Database: Executor InternalsSedna XML Database: Executor Internals
Sedna XML Database: Executor Internals
 

More from 艾鍗科技

More from 艾鍗科技 (20)

TinyML - 4 speech recognition
TinyML - 4 speech recognition TinyML - 4 speech recognition
TinyML - 4 speech recognition
 
Appendix 1 Goolge colab
Appendix 1 Goolge colabAppendix 1 Goolge colab
Appendix 1 Goolge colab
 
Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用Project-IOT於餐館系統的應用
Project-IOT於餐館系統的應用
 
02 IoT implementation
02 IoT implementation02 IoT implementation
02 IoT implementation
 
Tiny ML for spark Fun Edge
Tiny ML for spark Fun EdgeTiny ML for spark Fun Edge
Tiny ML for spark Fun Edge
 
Openvino ncs2
Openvino ncs2Openvino ncs2
Openvino ncs2
 
Step motor
Step motorStep motor
Step motor
 
2. 機器學習簡介
2. 機器學習簡介2. 機器學習簡介
2. 機器學習簡介
 
5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron) 5.MLP(Multi-Layer Perceptron)
5.MLP(Multi-Layer Perceptron)
 
3. data features
3. data features3. data features
3. data features
 
心率血氧檢測與運動促進
心率血氧檢測與運動促進心率血氧檢測與運動促進
心率血氧檢測與運動促進
 
利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆利用音樂&情境燈幫助放鬆
利用音樂&情境燈幫助放鬆
 
IoT感測器驅動程式 在樹莓派上實作
IoT感測器驅動程式在樹莓派上實作IoT感測器驅動程式在樹莓派上實作
IoT感測器驅動程式 在樹莓派上實作
 
無線聲控遙控車
無線聲控遙控車無線聲控遙控車
無線聲控遙控車
 
最佳光源的研究和實作
最佳光源的研究和實作最佳光源的研究和實作
最佳光源的研究和實作
 
無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車無線監控網路攝影機與控制自走車
無線監控網路攝影機與控制自走車
 
Reinforcement Learning
Reinforcement LearningReinforcement Learning
Reinforcement Learning
 
Linux Device Tree
Linux Device TreeLinux Device Tree
Linux Device Tree
 
人臉辨識考勤系統
人臉辨識考勤系統人臉辨識考勤系統
人臉辨識考勤系統
 
智慧家庭Smart Home
智慧家庭Smart Home智慧家庭Smart Home
智慧家庭Smart Home
 

Recently uploaded

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
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
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
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
 
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
 
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...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
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
 
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
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
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
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

Linux Ethernet device driver

  • 2. Application Socket interface TCP/UDP protocol IP protocol 2 網路Driver架構 Ethernet Hardware Device Driver
  • 3. 它收送都是有封包關念 它是一種broadcast的網路封包 MAC位址必需向世界組織申請,在同一個 local LAN需是唯一的,並且它只存在local LAN,在internet中會被去除。 3 認識ethernet封包 Dest MAC Source MAC Type Payload 6 bytes 6 bytes 2 bytes Max 1566 bytes Max 1580 bytes
  • 4. 4 開發流程 宣告struct net_device 結構變數 或 用alloc_etherdev() 設定必要的callback fuction在上述的結構 中 呼叫 register_netdev()註 冊該driver 系統呼叫init callback function初始化該元件 啓動該介面時系統會 呼叫open callback function 送資料時系統呼叫 hard_start_xmit callback function 當收到資料時 可呼叫 netif_rx()往 上層送資料
  • 5. struct net_device struct net_device { char name[IFNAMSIZ]; ………………………………….. unsigned long base_addr; /* device I/O address */ unsigned int irq; /* device IRQ number */ struct net_device_stats* (*get_stats)(struct net_device *dev); const struct ethtool_ops *ethtool_ops; int (*open)(struct net_device *dev); int (*stop)(struct net_device *dev); void (*set_multicast_list)(struct net_device *dev); int (*do_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd); void (*tx_timeout) (struct net_device *dev); unsigned char dev_addr[MAX_ADDR_LEN]; /* hw address, (before bcast because most packets are unicast) */ ……………………………………. }
  • 6. Module init example (dm9000.c) static struct platform_driver dm9000_driver = { .driver = { .name = "dm9000", .owner = THIS_MODULE, }, .probe = dm9000_probe, .remove = __devexit_p(dm9000_drv_remove), .suspend = dm9000_drv_suspend, .resume = dm9000_drv_resume, }; static int __init dm9000_init(void) { printk(KERN_INFO "%s Ethernet Driver, V%sn", CARDNAME, DRV_VERSION); return platform_driver_register(&dm9000_driver); } static void __exitdm9000_cleanup(void) { platform_driver_unregister(&dm9000_driver); } module_init(dm9000_init); module_exit(dm9000_cleanup);
  • 7. Probe example (dm9000.c) static int __devinit dm9000_probe(struct platform_device *pdev) { ………………………. ndev = alloc_etherdev(sizeof(struct board_info)); ……………………… db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); ……………………. db->addr_req = request_mem_region(db->addr_res->start, iosize, pdev->name); db->io_addr = ioremap(db->addr_res->start, iosize); /* fill in parameters for net-dev structure */ ndev->base_addr = (unsigned long)db->io_addr; ndev->irq = db->irq_res->start; /* driver system function */ ether_setup(ndev); ndev->open = &dm9000_open; ndev->hard_start_xmit = &dm9000_start_xmit; ndev->tx_timeout = &dm9000_timeout; ndev->watchdog_timeo = msecs_to_jiffies(watchdog); ndev->stop = &dm9000_stop; ndev->set_multicast_list = &dm9000_hash_table; ndev->ethtool_ops = &dm9000_ethtool_ops; ndev->do_ioctl = &dm9000_ioctl; ………………. ret = register_netdev(ndev); return 0; }
  • 8. 上傳資料 1) 可用interrupt service routine or polling將底層的資 料收進來 2) 呼叫netif_rx()上傳資料 下傳資料 1) 上層將會呼叫所註冊的hard_start_xmit() callback function Allocate buffer kernel API 1) struct sk_buff *alloc_skb(unsigned int size,gfp_t priority) 8 如何上下傳資料
  • 9. Start xmit example /* Move data to DM9000 TX RAM */ writeb(DM9000_MWCMD, db->io_addr); (db->outblk)(db->io_data, skb->data, skb->len); dev->stats.tx_bytes += skb->len; db->tx_pkt_cnt++; /* TX control: First packet immediately send, second packet queue */ if (db->tx_pkt_cnt == 1) { /* Set TX length to DM9000 */ iow(db, DM9000_TXPLL, skb->len); iow(db, DM9000_TXPLH, skb->len >> 8); /* Issue TX polling command */ iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */ dev->trans_start = jiffies; /* save the time stamp */ } else { /* Second packet */ db->queue_pkt_len = skb->len; netif_stop_queue(dev); } /* free this SKB */ dev_kfree_skb(skb); return 0;
  • 10. Receive packet interrupt example if (GoodPacket && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) { skb_reserve(skb, 2); rdptr = (u8 *) skb_put(skb, RxLen - 4); /* Read received packet from RX SRAM */ (db->inblk)(db->io_data, rdptr, RxLen); dev->stats.rx_bytes += RxLen; /* Pass to upper layer */ skb->protocol = eth_type_trans(skb, dev); netif_rx(skb); dev->stats.rx_packets++; } else { /* need to dump the packet's data */ (db->dumpblk)(db->io_data, RxLen); }
  • 11. struct sk_buff { struct sk_buff *next; struct sk_buff *prev; ………………………….. unsigned char *data; unsigned int len; …………. } 11 skb buffer stream struct sk_buff { struct sk_buff *next; struct sk_buff *prev; ………………………….. unsigned char *data; unsigned int len; …………. } Data buffer Data buffer
  • 12. Tx/Rx Buffer Management 12 Buffer Descriptor Start Address Register + BRXBDCNT+0 … … BDMATXDPTR buffer pointer #1 status length buffer pointer #2 status length buffer pointer #N status length Buffer Descriptor Rx Buffer … … … buffer #1 not used buffer #2 not used buffer #N not used Memory for frame BRXBDCNT+1 BRXBDCNT+(N-1) BRxBS of BDMARXLEN BRxBS of BDMARXLEN BRxBS of BDMARXLEN BDMA buffer descriptor counter of current pointer … Memory for Rx buffer descriptor
  • 13. skb buffer由下而上時是由ethernet device driver allocate,而由上層的TCP free skb buffer由上而下時是由TCP allocate,而 由底層的ethernet free 其中在open的call back function需要 netif_start_queue() 在close時需要netif_stop_queue() 在timeout時需要netif_wake_queue() 13 注意事項
  • 14. 若網路元件有支援DMA功能,則盡量使用 以提供網路傳輸效能。 若網路元件和CPU之間有共用的記憶體,記 得必需要設定為non-cache。 在中斷程式中最好不要做搬動資料的動作, 最好使用schedule_work()來完成,可是這會 有一個問題就是較不real-time。 研讀程式drivers/net/my_mac.c 14 注意事項