当前位置:网站首页>Analysis of USB network card sending and receiving data
Analysis of USB network card sending and receiving data
2022-07-07 02:55:00 【Li-Yongjun】
Preface
The network subsystem is Linux The top priority of the kernel . today , Let's start with the network card driver , Start the exploration of network subsystem .
Hardware environment
Raspberry pie 3B+, Equipped with Gigabit network card LAN7515, But it uses USB2.0 passageway ( The maximum theoretical speed is 480Mbps), The official gave 300Mbps Ethernet transmission rate .
drive
LAN7515 The driver is lan78xx, The source code is relatively simple , Just two documents ./linux/drivers/net/usb/lan78xx.c/h
, Compile to produce lan78xx.ko Driver file .
Drive analysis
1. USB Driver registration
static struct usb_driver lan78xx_driver = {
.name = DRIVER_NAME,
.id_table = products,
.probe = lan78xx_probe,
.disconnect = lan78xx_disconnect,
.suspend = lan78xx_suspend,
.resume = lan78xx_resume,
.reset_resume = lan78xx_reset_resume,
.supports_autosuspend = 1,
.disable_hub_initiated_lpm = 1,
};
module_usb_driver(lan78xx_driver);
LAN7515 yes USB network card , and CPU The interface of interaction is USB, So it's a USB equipment , To register it in the kernel USB drive ——lan78xx.ko.
insmod lan78xx.ko That is to say USB Register on the bus LAN7515 Network card driver , here , Can traverse USB Devices registered on the bus , With bus match Method to determine whether there is a matching device , If there is , Then call the driven probe function .
Let's see in detail :
/sys/bus/usb/ There are devices and drivers Catalog , These two directories are respectively in the current system USB On the bus Registered devices and Registered driver .
You can see , stay drivers There is no lan78xx drive , And we don't know devices Which in the directory USB The equipment is LAN7515 equipment .
Let's install lan78xx drive
See the execution lan78xx_probe() function , This also confirms our above statement ( Registration drive , Will traverse the device , Looking for a match , The match is successful , Trigger probe).
Let's look at... Again /sys/bus/usb/drivers Directory under
You can see , Has produced lan78xx Catalog , Description: the driver registration is successful .
2. Driver and device binding
stay lan78xx_probe() Called in the function lan78xx_bind() Method , Officially will lan78xx Drive and 1-1.1.1:1.1
The device is bound . stay lan78xx This can also be reflected in the directory .
We can also bind and unbind manually
# ifconfig -a
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:42 errors:0 dropped:0 overruns:0 frame:0
TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4478 (4.3 KiB) TX bytes:4478 (4.3 KiB)
# ls
bind module new_id remove_id uevent unbind
# echo "1-1.1.1:1.0" > bind
[37493.831496] lan78xx_probe()
[37494.126379] lan78xx 1-1.1.1:1.0 (unnamed net_device) (uninitialized): No External EEPROM. Setting MAC Speed
[37494.144756] libphy: lan78xx-mdiobus: probed
[37494.264385] lan78xx 1-1.1.1:1.0 (unnamed net_device) (uninitialized): int urb period 64
[37494.276093] lan78xx_phy_init()
# ls
1-1.1.1:1.0 module remove_id unbind
bind new_id uevent
# ifconfig -a
eth0 Link encap:Ethernet HWaddr B8:27:EB:8A:BC:F4
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:42 errors:0 dropped:0 overruns:0 frame:0
TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4478 (4.3 KiB) TX bytes:4478 (4.3 KiB)
#
#
# echo "1-1.1.1:1.0" > unbind
# ls
bind module new_id remove_id uevent unbind
# ifconfig -a
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:42 errors:0 dropped:0 overruns:0 frame:0
TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4478 (4.3 KiB) TX bytes:4478 (4.3 KiB)
#
3. Data transmission
With PC1 The data link layer sends a packet to PC2 Take the data link layer as an example
rx
tasklet_schedule(&dev->bh); // USB When data is received , The interrupt , Trigger the task
lan78xx_bh()
lan78xx_rx_bh()
rx_submit()
usb_fill_bulk_urb() // from USB Reading data
tasklet_schedule(&dev->bh);
lan78xx_bh()
rx_process()
lan78xx_rx()
lan78xx_skb_return()
----- Above is the drive ------- The following is the data link layer -----------------------
netif_rx() // Send it to the data link layer
tx
struct net_device_ops {
.ndo_start_xmit = lan78xx_start_xmit,
}
----- Above is the data link layer ------- The following is the drive -----------------------
lan78xx_start_xmit()
skb_queue_tail() // Insert queue
tasklet_schedule(&dev->bh); // Trigger task scheduling lan78xx_bh
lan78xx_bh()
lan78xx_tx_bh()
skb_dequeue() // Take out the data
------ The above is the drive network -------- The following is the drive USB part -----------
usb_fill_bulk_urb() // Fill data to USB
usb_submit_urb() // USB send data
RX call tree: At present, it's a little confused , The general process is USB Hardware interrupts , Drive to retrieve data , after rx_process() After processing , The final call netif_rx(skb), Send data to the kernel network subsystem , Further treatment skb.
TX call tree: The kernel network subsystem finally calls ndo_start_xmit() send data , The interface is finally implemented by the device driver , This example is lan78xx_start_xmit(), Finally, the data will be passed USB Send to LAN7515 network card , The network card is processed by its own hardware , Finally, the data is sent to the network cable .
Print frame header
static netdev_tx_t
lan78xx_start_xmit(struct sk_buff *skb, struct net_device *net)
{
printk("%s()\n", __FUNCTION__);
struct ethhdr *eth = eth_hdr(skb);
printk(KERN_INFO "DEST:" MAC_FMT "\n", MAC_ARG(eth->h_dest));
printk(KERN_INFO "SOURCE:" MAC_FMT "\n", MAC_ARG(eth->h_source));
struct iphdr *ip_header = ip_hdr(skb);
printk("daddr:%x is %d.%d.%d.%d\n", ip_header->daddr, inet_ntoa(ip_header->daddr));
printk("saddr:%x is %d.%d.%d.%d\n\n", ip_header->saddr, inet_ntoa(ip_header->saddr));
...
[44851.676380] lan78xx_start_xmit()
[44851.680906] DEST:08:00:27:38:fc:d0
[44851.685656] SOURCE:b8:27:eb:8a:bc:f4
[44851.690571] daddr:6401a8c0 is 192.168.1.100
[44851.696164] saddr:101a8c0 is 192.168.1.1
Print TX Direction , Drive the purpose in the data frame sent to the network card MAC、 Source MAC、 Purpose IP、 Source IP, The diagram above ① Data frame header at .
summary
network card 、 Network card driver 、 Kernel network subsystem , The core of what they do is to deliver data packets , among sk_buff In the whole process, it plays the role of threading , In the process of analyzing the network card drive sending and receiving , I also tried to print sk_buff Part of the information , As a beginning of exploration . Actually sk_buff It really plays a very important role in the network subsystem ,sk_buff yes socket buffer It means , This further reflects socket The importance of , Lianqi buffer All that NB,socket No more NB Do you ? So the later research route is :driver --> sk_buff --> socket.
边栏推荐
- The 8 element positioning methods of selenium that you have to know are simple and practical
- Planning and design of double click hot standby layer 2 network based on ENSP firewall
- 慧通编程入门课程 - 2A闯关
- Babbitt | metauniverse daily must read: is IP authorization the way to break the circle of NFT? What are the difficulties? How should holder choose the cooperation platform
- PSINS中19维组合导航模块sinsgps详解(滤波部分)
- MySQL - common functions - string functions
- Redis入門完整教程:問題定比特與優化
- 进程管理基础
- Digital scrolling increases effect
- 安全交付工程师
猜你喜欢
随机推荐
MATLB|具有储能的经济调度及机会约束和鲁棒优化
Redis introduction complete tutorial: client case analysis
Common fitting models and application methods of PCL
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
导数、偏导数、方向导数
MySQL
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
6-6漏洞利用-SSH安全防御
[leetcode]Search for a Range
Mmdetection3d loads millimeter wave radar data
AWS learning notes (I)
记一次JAP查询导致OOM的问题分析
MySQL提升大量数据查询效率的优化神器
MySQL - common functions - string functions
从零安装Redis
Code line breaking problem of untiy text box
Redis入门完整教程:RDB持久化
NuScenes数据集关于Radar数据的统计
Derivative, partial derivative, directional derivative
KYSL 海康摄像头 8247 h9 isapi测试