Transferred from: http://blog.csdn.net/viewsky11/article/details/53046787
A struct Net_device structure is used in Linux to describe each network device. At the same time, the struct net_device structure used to characterize the network device contains so many fields that the kernel developers feel that the struct net_device is a big mistake in the current Linux kernel.
In this article, only some of the fields in struct Net_device are introduced, and the other fields are used later.
#define IFNAMSIZ 32struct Net_device {The name of the device used to store the network device;Char Name[ifnamsiz];The alias of the network device;Char *ifalias;Network Device interface index value, unique network device identifier;int ifindex;This field is used to construct a hash list of network device names, while theThe name_hlist points to each hash list's linked list header;struct Hlist_node name_hlist;The hash list of interface index values used to build the network device, in struct netIndex_hlist the list header used to refer to the hash table of interface index values;struct Hlist_node index_hlist;For adding each network device to a network device in a network namespace double-linked liststruct List_head dev_list;The identifier of the network device interface, whose state type is defined in <linux/if.h>;Unsignedint flags;Identifier of the network device interface, but not visible to user space;UnsignedShort priv_flags;Interface hardware type, which defines each interface hardware type in <if_arp.h>;UnsignedShort type;The maximum transmission unit of the network device interface;Unsigned MTU;hardware connection oral length;UnsignedShort Hard_header_len;The MAC address of the network device interface;UnsignedChar *dev_addr;Unicast mode for network device interfacesint Uc_promisc;Promiscuous mode of network device interface;Unsignedint promiscuity;Full multicast mode for network device interface; unsigendint allmulti;Secondary unicast MAC Addressstruct Netdev_hw_addr_list UC;List of device HW address;struct Netdev_hw_addr_list Dev_addrs;HW broadcast address;UnsignedChar Broadcast[max_addr_len];multicast MAC address;struct Dev_addr_list *mac_list;Packet receiving queue for network device interface;struct Netdev_queue rx_queue;Packet sending queue for network device interface;struct Netdev_queue *tx; //number of TX queues allocated at ALLOC_NETDEV_MQ () time unsigned int num_tx_queues; //number of TX queues currently active in device; unsigned int real_num_tx_queues; //max frame per queue allowned; unsigned long tx_queue_len; //The status of the network device interface; unsigned long state; //Network Device interface statistical situation; struct net_device_state states; //Used to execute the namespace where the network device resides; struct net *nd_net;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
The following diagram shows how the Linux kernel uses struct net and struct net_device to form a network namespace:
1. Assign a network device function, that is, assign a struct net_device struct:alloc_netdev(sizeof_priv, name, setup);
This alloc_netdev () function is essentially a macro definition:
#define alloc_netdev(sizeof_priv, name, setup) alloc_netdev_mq(sizeof_priv, name, setup, 1)struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name, void(*setup)(struct net_device*), unsigned int queue_count) EXPORT_SYMBOL(alloc_netdev_mq)
Parameter description:
-Sizeof_priv: The size of the private space allocated to the network device;
-Name: Names of network devices;
-Setup: A callback function that initializes the assigned network device;
-Queue_count: Number of sub-queues allocated to network devices;
struct Net_device *alloc_netdev_mq (int Sizeof_priv,ConstChar *name,void (*setup) (struct Net_device *),Unsignedint Queue_count) {struct Net_device *dev;struct Netdev_queue *tx;int alloc_size; Alloc_size =sizeofstruct net_device); dev = Kzalloc (alloc_size, Gfp_kernel); tx = Kcalloc (Queue_count, sizeof (struct netdev_queue), GFP_ KERNEL); Dev_addr_init (Dev); //the Net_device member in the struct Dev_addrs; Dev_unicast_init (Dev); //initializes the UC members in the struct net_device; Dev_net_set (Dev, &init_net); //the namespace of the network device is initialized, the default is init_net; dev->_tx = TX; //set the send queue of the network device; dev->num_tx_queues = queue_count; dev->real_num_tx_queues = Queue_ Count; Netdev_init_queues (Dev); //initializes the Net_device members in the struct rx_queue; setup (dev); //the struct net_device structure to initialize; strcpy (dev->name, name); Span class= "Hljs-comment" >//set the device name of the network device;}export_symbol (ALLOC_NETDEV_MQ);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
2. Release a network device:
void free_netdev(struct net_device *dev);EXPORT_SYMBOL(free_netdev);
3. Register a network device, only after registering a network device, this network device will function in the kernel:
int register_netdev(struct net_device *dev)EXPORT_SYMBOL(register_netdev);
return value:
-0: Registration is successful;
-A negative errno code: Indicates that the registration failed;
int Register_netdev (struct net_device*dev) {struct hlist_head*head; struct Hlist_node*p; int ret; struct NET*net= Dev_net (dev);Gets the namespace in which the network device resides; Rtnl_lock ();Obtain the RTNL signal volume;If!dev_valid_name (Dev->name))Determine if the device name of the network device is valid; {} dev->ifindex= Dev_new_index (NET);Find a globally unique network from the namespace in which the network device residesinterface index value; dev->iflink = dev->ifindex; //is used to determine whether there is a network device with the same name in the network namespace; head = dev_name_hash (NET, Dev->name); Hlist_for_each (p,head) {struct net_device *d = hlist_entry (P, struct Net_device, name_hlist); if (! STRNCMP (D->name, Dev->name, 32 ) {ret = -eexist;}} set_bit (__link_state_present, &dev->state); //set the status of the network device; List_netdevice (dev); //to call the network device into the appropriate namespace;} Export_symbol (Register_netdev);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
3. Unregister a network device structure:
void unregister_netdev(struct net_device *dev);EXPORT_SYMBOL(unregister_netdev);
4. Set the MAC address of a network device:
int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa);EXPORT_SYMBOL(dev_set_mac_address);
5. Set the maximum transmission unit for a network device:
int dev_set_mtu(struct net_device *dev, int new_mtu);EXPORT_SYMBOL(dev_set_mtu);
6. Change the flag identifier of a network device:
int dev_change_flags(struct net_device *dev, unsigend flags);EXPORT_SYMBOL(dev_change_flags);
7. Obtain a flag identifier for a network device:
unsigned dev_get_flags(struct net_device *dev);EXPORT_SYMBOL(dev_get_flags);
8. Add a unicast MAC address to the network device when the network device uses a unicast MAC address when sending the unicast
int dev_unicast_add(struct net_device *dev, void *addr);EXPORT_SYMBOL(dev_unicast_add);
9. Remove the unicast MAC address from the network device:
int dev_unicast_delete(struct net_device *dev, void *addr);EXPORT_SYMBOL(dev_unicast_delete);
10. Add a device address to the network device:
int dev_addr_add(struct net_device *dev, unsigned char *addr, unsigned char addr_type)EXPORT_SYMBOL(dev_addr_add);addr_type : address type;
11. Remove a device address from the network device:
int dev_addr_del(struct net_device *dev, unsigend char *addr, unsigned char addr_type);EXPORT_SYMBOL(dev_addr_del);
12. Set the interface for the network device to promiscuous mode:
int dev_set_promiscuity(struct net_device *dev, int inc); EXPORT_SYMBOL(dev_set_promiscuity);
When the INC > 0 sets the network device to promiscuous mode;
When inc = 0 sets the network device to normal mode;
When the INC < 0 will remove the promiscuous mode of the network device;
13. Set the interface for the network device to Allmulticast mode:
int dev_set_allmulti(struct net_device *dev, int inc); EXPORT_SYMBOL(dev_set_allmulti); inc 含义同上;
14. When assigning a device name to a network device, first check if the name is valid:
int dev_valid_name(const char *name); EXPORT_SYMBOL(dev_valid_name);
Return:
1: the expression is valid;
0: the expression is invalid;
15. Obtain the device structure of the network device via the device's MAC address and device type:
struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, char *hwaddr); EXPORT_SYMBOL(dev_getbyhwaddr);
NET: Network namespace;
Type:media type of device;
Hwaddr:hardware address;
Through MEMCMP (dev->dev_addr, HWADDR, Dev->addr_len) to achieve;
16. Obtain the network device structure through the interface index value of the network device:
struct net_device *dev_get_by_index(struct net *net, int ifindex); EXPORT_SYMBOL(dev_get_by_index);
17. Obtain the network device structure through the device name of the network device:
struct net_device *dev_get_by_name(struct net *net, const char *name); EXPORT_SYMBOL(dev_get_by_name);
All the functions described above are located in/NET/CORE/DEV.C;
This article is reproduced from http://weiguozhihui.blog.51cto.com/3060615/1584894
struct Net_device network equipment structure body detailed