当前位置:网站首页>i. Mx6ull driver development | 32 - manually write a virtual network card device
i. Mx6ull driver development | 32 - manually write a virtual network card device
2022-07-29 09:37:00 【Mculover666】
One 、Linux Kernel network driver processing flow
1. net_device Application and release of structures
net_device Structural instances can be dynamically applied :
#define alloc_netdev(sizeof_priv, name, name_assign_type, setup) \ alloc_netdev_mqs(sizeof_priv, name, name_assign_type, setup, 1, 1)
#define alloc_netdev_mq(sizeof_priv, name, name_assign_type, setup, count) \ alloc_netdev_mqs(sizeof_priv, name, name_assign_type, setup, count, \ count)
The functions called at the bottom of these two macros are alloc_netdev_mqs, The prototype is as follows :
struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
unsigned char name_assign_type,
void (*setup)(struct net_device *),
unsigned int txqs, unsigned int rxqs);
The meaning of the parameter is as follows :
- sizeof_priv: The size of the private member of the device
- name: Device name
- name_assign_type: Name alignment type
- setup: A function pointer
- txqs: Number of send queues to allocate
- rxqs: Number of receive queues to allocate
Release net_device Structure of the API as follows :
void free_netdev(struct net_device *dev);
2. Registration and cancellation of network device driver
Network driver registration and logout use the following two API, Defined in the file include/linux/netdevice.h in :
int register_netdev(struct net_device *dev);
void unregister_netdev(struct net_device *dev);
General , When the driver module is initialized, register the network card device to the kernel , When the driver module is unloaded , Log off the network card device from the kernel .
3. Initialization of network devices
Network device initialization mainly completes the following work :
- Prepare the hardware , Check whether the network device exists , If there is , Then the hardware resources used by the detection device ;
- Prepare the software , Distribute net_device Structure and assign values to its data and function pointer members ;
- Get the private information pointer of the device and initialize the value of each member ;
4. Opening and releasing of network devices
The opening function of the network device needs to complete the following work :
- Hardware resources used by the enabling device , apply I/O Area 、 interrupt 、DMA Channels, etc
- call Linux kernel-provided API, Activate the device send queue
The release function of network equipment needs to complete the following work :
- call Linux kernel-provided API, Stop the device from transferring packets
- Release the used by the device I/O Area 、 interrupt 、DMA Channels, etc
Linux The kernel provides two API as follows :
/** * netif_start_queue - allow transmit * @dev: network device * * Allow upper layers to call the device hard_start_xmit routine. */
static inline void netif_start_queue(struct net_device *dev)
{
netif_tx_start_queue(netdev_get_tx_queue(dev, 0));
}
/** * netif_stop_queue - stop transmitted packets * @dev: network device * * Stop upper layers calling the device hard_start_xmit routine. * Used for flow control when transmit resources are unavailable. */
static inline void netif_stop_queue(struct net_device *dev)
{
netif_tx_stop_queue(netdev_get_tx_queue(dev, 0));
}
5. Data transmission process
Linux When the network subsystem sends data packets , Will call the driver provided ndo_start_xmit function , Used to start the sending of data packets , So the bottom layer only needs to implement the function pointer .
The sending process is as follows :
- Prevent the upper layer from continuing to transmit data packets to the network device driver
- Hardware sends packets
- Record sending bytes and sending timestamp
- Wake up the upper layer and continue to deliver packets
Linux The kernel provides the ability to wake up the upper layer to transmit packets API as follows :
/** * netif_wake_queue - restart transmit * @dev: network device * * Allow upper layers to call the device hard_start_xmit routine. * Used for flow control when transmit resources are available. */
static inline void netif_wake_queue(struct net_device *dev)
{
netif_tx_wake_queue(netdev_get_tx_queue(dev, 0));
}
6. Data receiving process
The main method for network devices to receive data is the interrupt processing function of the device caused by the interrupt , Determine the interrupt type in the interrupt handling function , If it is a receive interrupt , be Read received data , Distribute sk_buffer Data structures and data buffers , Copy the received data to the data buffer , And call netif_rx() Function will sk_buffer Pass to upper layer protocol .
Two 、 example —— Write a virtual network card device
Reference article :26.Linux- Introduction of network card driver and making virtual network card driver ( Detailed explanation )
This blog post is very good , Bloggers on this basis , Updated drive adaptation to 4.14 kernel .
1. Purpose
Write a virtual network card device , Can be added to the kernel , And write a virtual contracting and receiving equipment , So that the network card can ping Through any ip.
2. Code implementation
https://github.com/Mculover666/linux_driver_study/
3. effect
3.1. Load virtual network card driver module
Before loading the module , Check the network card existing in the current system :
Load virtual network card driver module :
Check the system network card after loading , You can see more virt_eth0:
Reuse ifconfig see :
3.2.ping test
Set the nic ip:
View routing table :
ping Any in the network segment ip test :
You can see that the number of contracts and packages received are counted :
边栏推荐
- Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
- 不用Swagger,那我用啥?
- 一知半解 ~题目杂记 ~ 一个多态问题
- Pyqt5 rapid development and practice 6.4 qboxlayout (box layout)
- 网络安全(5)
- 《UnityShader入门精要》总结(2):初级篇
- Pyqt5 rapid development and practice 6.5 qgridlayout (grid layout)
- 如何介绍自己的项目经验?
- First order traversal / second order traversal determines the approximate shape of the tree
- Flowable UI production flow chart
猜你喜欢

How does alternates achieve high-performance publish and subscribe?

用户身份标识与账号体系实践

AxureRP原型设计 快速开始

一知半解 ~题目杂记 ~ 一个多态问题

Parameter initialization

Use cpolar to publish raspberry pie web pages (improvement of cpolar function)

Pyqt5 rapid development and practice 6.5 qgridlayout (grid layout)

Pyqt5 rapid development and practice 6.1 three dimensions of good software & 6.2 layout management in pyqt5 & 6.3 absolute location layout of pyqt5

36. JS animation

《LOL》从代码上来说最难的是哪个英雄?
随机推荐
redis命令[逐渐完善]
Implementation of DFA string recognition based on C language simulation
Use cpolar to publish raspberry pie web pages (improvement of cpolar tunnel)
Data type of MySQL
常用的DOS命令[逐渐完善]
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
(Video + graphics) introduction to machine learning series - Chapter 1 Introduction
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
机器学习之分类模型评估指标及sklearn代码实现
Pytest+allure generate test report
[Yunzhu co creation] [hcsd live broadcast] teach the interview tips of big companies in person
Is the marginal old technology in its 40s weak in the future or rising from the ground?
NFA determination and DFA minimization based on C language
Unity3d hodgepodge
Commonly used DOS commands [gradually improved]
No swagger, what do I use?
36. JS animation
Nutnews developed based on arkui ETS
使用cpolar发布树莓派网页(cpolar功能的完善)
Trie树(字典树)讲解