网络设备之net_device结构与操作

net_device结构是一个很大的结构,其中包含了硬件信息,接口信息,其他辅助信息,以及设备操作函数等;

目前仍在读代码中,后续字段注释会逐渐补充;

  1 /**
  2  *    struct net_device - The DEVICE structure.
  3  *        Actually, this whole structure is a big mistake.  It mixes I/O
  4  *        data with strictly "high-level" data, and it has to know about
  5  *        almost every data structure used in the INET module.
  6  *
  7  *    @name:    This is the first field of the "visible" part of this structure
  8  *        (i.e. as seen by users in the "Space.c" file).  It is the name
  9  *         of the interface.
 10  *
 11  *    @name_hlist:     Device name hash chain, please keep it close to name[]
 12  *    @ifalias:    SNMP alias
 13  *    @mem_end:    Shared memory end
 14  *    @mem_start:    Shared memory start
 15  *    @base_addr:    Device I/O address
 16  *    @irq:        Device IRQ number
 17  *
 18  *    @carrier_changes:    Stats to monitor carrier on<->off transitions
 19  *
 20  *    @state:        Generic network queuing layer state, see netdev_state_t
 21  *    @dev_list:    The global list of network devices
 22  *    @napi_list:    List entry used for polling NAPI devices
 23  *    @unreg_list:    List entry  when we are unregistering the
 24  *            device; see the function unregister_netdev
 25  *    @close_list:    List entry used when we are closing the device
 26  *    @ptype_all:     Device-specific packet handlers for all protocols
 27  *    @ptype_specific: Device-specific, protocol-specific packet handlers
 28  *
 29  *    @adj_list:    Directly linked devices, like slaves for bonding
 30  *    @features:    Currently active device features
 31  *    @hw_features:    User-changeable features
 32  *
 33  *    @wanted_features:    User-requested features
 34  *    @vlan_features:        Mask of features inheritable by VLAN devices
 35  *
 36  *    @hw_enc_features:    Mask of features inherited by encapsulating devices
 37  *                This field indicates what encapsulation
 38  *                offloads the hardware is capable of doing,
 39  *                and drivers will need to set them appropriately.
 40  *
 41  *    @mpls_features:    Mask of features inheritable by MPLS
 42  *
 43  *    @ifindex:    interface index
 44  *    @group:        The group the device belongs to
 45  *
 46  *    @stats:        Statistics struct, which was left as a legacy, use
 47  *            rtnl_link_stats64 instead
 48  *
 49  *    @rx_dropped:    Dropped packets by core network,
 50  *            do not use this in drivers
 51  *    @tx_dropped:    Dropped packets by core network,
 52  *            do not use this in drivers
 53  *    @rx_nohandler:    nohandler dropped packets by core network on
 54  *            inactive devices, do not use this in drivers
 55  *
 56  *    @wireless_handlers:    List of functions to handle Wireless Extensions,
 57  *                instead of ioctl,
 58  *                see <net/iw_handler.h> for details.
 59  *    @wireless_data:    Instance data managed by the core of wireless extensions
 60  *
 61  *    @netdev_ops:    Includes several pointers to callbacks,
 62  *            if one wants to override the ndo_*() functions
 63  *    @ethtool_ops:    Management operations
 64  *    @ndisc_ops:    Includes callbacks for different IPv6 neighbour
 65  *            discovery handling. Necessary for e.g. 6LoWPAN.
 66  *    @header_ops:    Includes callbacks for creating,parsing,caching,etc
 67  *            of Layer 2 headers.
 68  *
 69  *    @flags:        Interface flags (a la BSD)
 70  *    @priv_flags:    Like 'flags' but invisible to userspace,
 71  *            see if.h for the definitions
 72  *    @gflags:    Global flags ( kept as legacy )
 73  *    @padded:    How much padding added by alloc_netdev()
 74  *    @operstate:    RFC2863 operstate
 75  *    @link_mode:    Mapping policy to operstate
 76  *    @if_port:    Selectable AUI, TP, ...
 77  *    @dma:        DMA channel
 78  *    @mtu:        Interface MTU value
 79  *    @min_mtu:    Interface Minimum MTU value
 80  *    @max_mtu:    Interface Maximum MTU value
 81  *    @type:        Interface hardware type
 82  *    @hard_header_len: Maximum hardware header length.
 83  *    @min_header_len:  Minimum hardware header length
 84  *
 85  *    @needed_headroom: Extra headroom the hardware may need, but not in all
 86  *              cases can this be guaranteed
 87  *    @needed_tailroom: Extra tailroom the hardware may need, but not in all
 88  *              cases can this be guaranteed. Some cases also use
 89  *              LL_MAX_HEADER instead to allocate the skb
 90  *
 91  *    interface address info:
 92  *
 93  *     @perm_addr:        Permanent hw address
 94  *     @addr_assign_type:    Hw address assignment type
 95  *     @addr_len:        Hardware address length
 96  *    @neigh_priv_len:    Used in neigh_alloc()
 97  *     @dev_id:        Used to differentiate devices that share
 98  *                 the same link layer address
 99  *     @dev_port:        Used to differentiate devices that share
100  *                 the same function
101  *    @addr_list_lock:    XXX: need comments on this one
102  *    @uc_promisc:        Counter that indicates promiscuous mode
103  *                has been enabled due to the need to listen to
104  *                additional unicast addresses in a device that
105  *                does not implement ndo_set_rx_mode()
106  *    @uc:            unicast mac addresses
107  *    @mc:            multicast mac addresses
108  *    @dev_addrs:        list of device hw addresses
109  *    @queues_kset:        Group of all Kobjects in the Tx and RX queues
110  *    @promiscuity:        Number of times the NIC is told to work in
111  *                promiscuous mode; if it becomes 0 the NIC will
112  *                exit promiscuous mode
113  *    @allmulti:        Counter, enables or disables allmulticast mode
114  *
115  *    @vlan_info:    VLAN info
116  *    @dsa_ptr:    dsa specific data
117  *    @tipc_ptr:    TIPC specific data
118  *    @atalk_ptr:    AppleTalk link
119  *    @ip_ptr:    IPv4 specific data
120  *    @dn_ptr:    DECnet specific data
121  *    @ip6_ptr:    IPv6 specific data
122  *    @ax25_ptr:    AX.25 specific data
123  *    @ieee80211_ptr:    IEEE 802.11 specific data, assign before registering
124  *
125  *    @dev_addr:    Hw address (before bcast,
126  *            because most packets are unicast)
127  *
128  *    @_rx:            Array of RX queues
129  *    @num_rx_queues:        Number of RX queues
130  *                allocated at register_netdev() time
131  *    @real_num_rx_queues:     Number of RX queues currently active in device
132  *
133  *    @rx_handler:        handler for received packets
134  *    @rx_handler_data:     XXX: need comments on this one
135  *    @ingress_queue:        XXX: need comments on this one
136  *    @broadcast:        hw bcast address
137  *
138  *    @rx_cpu_rmap:    CPU reverse-mapping for RX completion interrupts,
139  *            indexed by RX queue number. Assigned by driver.
140  *            This must only be set if the ndo_rx_flow_steer
141  *            operation is defined
142  *    @index_hlist:        Device index hash chain
143  *
144  *    @_tx:            Array of TX queues
145  *    @num_tx_queues:        Number of TX queues allocated at alloc_netdev_mq() time
146  *    @real_num_tx_queues:     Number of TX queues currently active in device
147  *    @qdisc:            Root qdisc from userspace point of view
148  *    @tx_queue_len:        Max frames per queue allowed
149  *    @tx_global_lock:     XXX: need comments on this one
150  *
151  *    @xps_maps:    XXX: need comments on this one
152  *
153  *    @watchdog_timeo:    Represents the timeout that is used by
154  *                the watchdog (see dev_watchdog())
155  *    @watchdog_timer:    List of timers
156  *
157  *    @pcpu_refcnt:        Number of references to this device
158  *    @todo_list:        Delayed register/unregister
159  *    @link_watch_list:    XXX: need comments on this one
160  *
161  *    @reg_state:        Register/unregister state machine
162  *    @dismantle:        Device is going to be freed
163  *    @rtnl_link_state:    This enum represents the phases of creating
164  *                a new link
165  *
166  *    @needs_free_netdev:    Should unregister perform free_netdev?
167  *    @priv_destructor:    Called from unregister
168  *    @npinfo:        XXX: need comments on this one
169  *     @nd_net:        Network namespace this network device is inside
170  *
171  *     @ml_priv:    Mid-layer private
172  *     @lstats:    Loopback statistics
173  *     @tstats:    Tunnel statistics
174  *     @dstats:    Dummy statistics
175  *     @vstats:    Virtual ethernet statistics
176  *
177  *    @garp_port:    GARP
178  *    @mrp_port:    MRP
179  *
180  *    @dev:        Class/net/name entry
181  *    @sysfs_groups:    Space for optional device, statistics and wireless
182  *            sysfs groups
183  *
184  *    @sysfs_rx_queue_group:    Space for optional per-rx queue attributes
185  *    @rtnl_link_ops:    Rtnl_link_ops
186  *
187  *    @gso_max_size:    Maximum size of generic segmentation offload
188  *    @gso_max_segs:    Maximum number of segments that can be passed to the
189  *            NIC for GSO
190  *
191  *    @dcbnl_ops:    Data Center Bridging netlink ops
192  *    @num_tc:    Number of traffic classes in the net device
193  *    @tc_to_txq:    XXX: need comments on this one
194  *    @prio_tc_map:    XXX: need comments on this one
195  *
196  *    @fcoe_ddp_xid:    Max exchange id for FCoE LRO by ddp
197  *
198  *    @priomap:    XXX: need comments on this one
199  *    @phydev:    Physical device may attach itself
200  *            for hardware timestamping
201  *
202  *    @qdisc_tx_busylock: lockdep class annotating Qdisc->busylock spinlock
203  *    @qdisc_running_key: lockdep class annotating Qdisc->running seqcount
204  *
205  *    @proto_down:    protocol port state information can be sent to the
206  *            switch driver and used to set the phys state of the
207  *            switch port.
208  *
209  *    FIXME: cleanup struct net_device such that network protocol info
210  *    moves out.
211  */
212 
213 struct net_device {
214     /* 设备名称,如eth0 */
215     char            name[IFNAMSIZ];
216     /* 名称hash */
217     struct hlist_node    name_hlist;
218     char             *ifalias;
219     /*
220      *    I/O specific fields
221      *    FIXME: Merge these and struct ifmap into one
222      */
223     /* 
224         描述设备所用的共享内存,用于设备与内核沟通 
225         其初始化和访问只会在设备驱动程序内进行
226     */
227     unsigned long        mem_end;
228     unsigned long        mem_start;
229 
230     /* 设备自有内存映射到I/O内存的起始地址 */
231     unsigned long        base_addr;
232 
233     /*
234         设备与内核对话的中断编号,此值可由多个设备共享
235         驱动程序使用request_irq函数分配此变量,使用free_irq予以释放
236     */
237     int            irq;
238 
239     atomic_t        carrier_changes;
240 
241     /*
242      *    Some hardware also needs these fields (state,dev_list,
243      *    napi_list,unreg_list,close_list) but they are not
244      *    part of the usual set specified in Space.c.
245      */
246 
247     /* 
248         网络队列子系统使用的一组标识
249         由__LINK_STATE_xxx标识
250     */
251     unsigned long        state;
252 
253     struct list_head    dev_list;
254     struct list_head    napi_list;
255     struct list_head    unreg_list;
256     struct list_head    close_list;
257     struct list_head    ptype_all;
258     struct list_head    ptype_specific;
259 
260     struct {
261         struct list_head upper;
262         struct list_head lower;
263     } adj_list;
264 
265     /* 
266         用于存在其他一些设备功能
267         可报告适配卡的功能,以便与CPU通信
268         使用NETIF_F_XXX标识功能特性
269     */
270     netdev_features_t    features;
271     netdev_features_t    hw_features;
272     netdev_features_t    wanted_features;
273     netdev_features_t    vlan_features;
274     netdev_features_t    hw_enc_features;
275     netdev_features_t    mpls_features;
276     netdev_features_t    gso_partial_features;
277 
278     /* 网络设备索引号 */
279     int            ifindex;
280     int            group;
281 
282     struct net_device_stats    stats;
283 
284     atomic_long_t        rx_dropped;
285     atomic_long_t        tx_dropped;
286     atomic_long_t        rx_nohandler;
287 
288 #ifdef CONFIG_WIRELESS_EXT
289     const struct iw_handler_def *wireless_handlers;
290     struct iw_public_data    *wireless_data;
291 #endif
292     /* 设备操作接口 */
293     const struct net_device_ops *netdev_ops;
294     /* ethtool操作接口 */
295     const struct ethtool_ops *ethtool_ops;
296 #ifdef CONFIG_NET_SWITCHDEV
297     const struct switchdev_ops *switchdev_ops;
298 #endif
299 #ifdef CONFIG_NET_L3_MASTER_DEV
300     const struct l3mdev_ops    *l3mdev_ops;
301 #endif
302 #if IS_ENABLED(CONFIG_IPV6)
303     const struct ndisc_ops *ndisc_ops;
304 #endif
305 
306 #ifdef CONFIG_XFRM
307     const struct xfrmdev_ops *xfrmdev_ops;
308 #endif
309 
310     /* 头部一些操作,如链路层缓存,校验等 */
311     const struct header_ops *header_ops;
312 
313     /* 标识接口特性,IFF_XXX,如IFF_UP */
314     unsigned int        flags;
315 
316     /* 
317         用于存储用户空间不可见的标识
318         由VLAN和Bridge虚拟设备使用
319     */
320     unsigned int        priv_flags;
321 
322     /* 几乎不使用,为了兼容保留 */
323     unsigned short        gflags;
324 
325     /* 结构对齐填充 */
326     unsigned short        padded;
327 
328     /* 与interface group mib中的IfOperStatus相关 */
329     unsigned char        operstate;
330     unsigned char        link_mode;
331 
332     /* 
333         接口使用的端口类型
334     */
335     unsigned char        if_port;
336 
337     /*
338         设备使用的DMA通道
339         并非所有设备都可以用DMA,有些总线不支持DMA
340     */
341     unsigned char        dma;
342 
343     /*
344         最大传输单元,标识设备能处理帧的最大尺寸 
345         Ethernet-1500
346     */
347     unsigned int        mtu;
348     unsigned int        min_mtu;
349     unsigned int        max_mtu;
350 
351     /*     设备所属类型
352         ARP模块中,用type判断接口的硬件地址类型
353         以太网接口为ARPHRD_ETHER 
354     */
355     unsigned short        type;
356     /* 
357         设备头部长度
358         Ethernet报头是ETH_HLEN=14字节
359     */
360     unsigned short        hard_header_len;
361     unsigned char        min_header_len;
362 
363     unsigned short        needed_headroom;
364     unsigned short        needed_tailroom;
365 
366     /* Interface address info. */
367     /* 硬件地址,通常在初始化过程中从硬件读取 */
368     unsigned char        perm_addr[MAX_ADDR_LEN];
369     unsigned char        addr_assign_type;
370     /* 硬件地址长度 */
371     unsigned char        addr_len;
372     unsigned short        neigh_priv_len;
373     unsigned short          dev_id;
374     unsigned short          dev_port;
375     spinlock_t        addr_list_lock;
376     unsigned char        name_assign_type;
377     bool            uc_promisc;
378     struct netdev_hw_addr_list    uc;
379     struct netdev_hw_addr_list    mc;
380     struct netdev_hw_addr_list    dev_addrs;
381 
382 #ifdef CONFIG_SYSFS
383     struct kset        *queues_kset;
384 #endif
385     /* 混杂模式 */
386     unsigned int        promiscuity;
387 
388     /* 非零值时,设备监听所有多播地址 */
389     unsigned int        allmulti;
390 
391 
392     /* Protocol-specific pointers */
393 /* 特定协议的指针 */
394 #if IS_ENABLED(CONFIG_VLAN_8021Q)
395     struct vlan_info __rcu    *vlan_info;
396 #endif
397 #if IS_ENABLED(CONFIG_NET_DSA)
398     struct dsa_switch_tree    *dsa_ptr;
399 #endif
400 #if IS_ENABLED(CONFIG_TIPC)
401     struct tipc_bearer __rcu *tipc_ptr;
402 #endif
403     void             *atalk_ptr;
404     /* ip指向in_device结构 */    
405     struct in_device __rcu    *ip_ptr;
406     struct dn_dev __rcu     *dn_ptr;
407     struct inet6_dev __rcu    *ip6_ptr;
408     void            *ax25_ptr;
409     struct wireless_dev    *ieee80211_ptr;
410     struct wpan_dev        *ieee802154_ptr;
411 #if IS_ENABLED(CONFIG_MPLS_ROUTING)
412     struct mpls_dev __rcu    *mpls_ptr;
413 #endif
414 
415 /*
416  * Cache lines mostly used on receive path (including eth_type_trans())
417  */
418     /* Interface address info used in eth_type_trans() */
419     unsigned char        *dev_addr;
420 
421 #ifdef CONFIG_SYSFS
422     struct netdev_rx_queue    *_rx;
423 
424     unsigned int        num_rx_queues;
425     unsigned int        real_num_rx_queues;
426 #endif
427 
428     struct bpf_prog __rcu    *xdp_prog;
429     unsigned long        gro_flush_timeout;
430     rx_handler_func_t __rcu    *rx_handler;
431     void __rcu        *rx_handler_data;
432 
433 #ifdef CONFIG_NET_CLS_ACT
434     struct tcf_proto __rcu  *ingress_cl_list;
435 #endif
436     struct netdev_queue __rcu *ingress_queue;
437 #ifdef CONFIG_NETFILTER_INGRESS
438     struct nf_hook_entry __rcu *nf_hooks_ingress;
439 #endif
440 
441     /* 链路层广播地址 */
442     unsigned char        broadcast[MAX_ADDR_LEN];
443 #ifdef CONFIG_RFS_ACCEL
444     struct cpu_rmap        *rx_cpu_rmap;
445 #endif
446     /* 接口索引hash */
447     struct hlist_node    index_hlist;
448 
449 /*
450  * Cache lines mostly used on transmit path
451  */
452     struct netdev_queue    *_tx ____cacheline_aligned_in_smp;
453     unsigned int        num_tx_queues;
454     unsigned int        real_num_tx_queues;
455     /* 排队规则 */
456     struct Qdisc        *qdisc;
457 #ifdef CONFIG_NET_SCHED
458     DECLARE_HASHTABLE    (qdisc_hash, 4);
459 #endif
460     /* 
461         可在设备发送队列中排队的最大数据包数
462     */
463     unsigned long        tx_queue_len;
464     spinlock_t        tx_global_lock;
465 
466     /*     网络层确定传输超时,
467         调用驱动程序tx_timeout接口的最短时间 
468     */
469     int            watchdog_timeo;
470 
471 #ifdef CONFIG_XPS
472     struct xps_dev_maps __rcu *xps_maps;
473 #endif
474 #ifdef CONFIG_NET_CLS_ACT
475     struct tcf_proto __rcu  *egress_cl_list;
476 #endif
477 
478     /* These may be needed for future network-power-down code. */
479     /* watchdog定时器 */
480     struct timer_list    watchdog_timer;
481 
482     /* 引用计数 */
483     int __percpu        *pcpu_refcnt;
484 
485     /*     网络设备的注册和除名以两步进行,
486         该字段用于处理第二步
487     */
488     struct list_head    todo_list;
489 
490     struct list_head    link_watch_list;
491 
492     /* 设备的注册状态 */
493     enum { NETREG_UNINITIALIZED=0,
494            NETREG_REGISTERED,    /* completed register_netdevice */
495            NETREG_UNREGISTERING,    /* called unregister_netdevice */
496            NETREG_UNREGISTERED,    /* completed unregister todo */
497            NETREG_RELEASED,        /* called free_netdev */
498            NETREG_DUMMY,        /* dummy device for NAPI poll */
499     } reg_state:8;
500 
501     bool dismantle;
502 
503     enum {
504         RTNL_LINK_INITIALIZED,
505         RTNL_LINK_INITIALIZING,
506     } rtnl_link_state:16;
507 
508     bool needs_free_netdev;
509     void (*priv_destructor)(struct net_device *dev);
510 
511 #ifdef CONFIG_NETPOLL
512     struct netpoll_info __rcu    *npinfo;
513 #endif
514 
515     possible_net_t            nd_net;
516 
517     /* mid-layer private */
518     union {
519         void                    *ml_priv;
520         struct pcpu_lstats __percpu        *lstats;
521         struct pcpu_sw_netstats __percpu    *tstats;
522         struct pcpu_dstats __percpu        *dstats;
523         struct pcpu_vstats __percpu        *vstats;
524     };
525 
526 #if IS_ENABLED(CONFIG_GARP)
527     struct garp_port __rcu    *garp_port;
528 #endif
529 #if IS_ENABLED(CONFIG_MRP)
530     struct mrp_port __rcu    *mrp_port;
531 #endif
532 
533     struct device        dev;
534     const struct attribute_group *sysfs_groups[4];
535     const struct attribute_group *sysfs_rx_queue_group;
536 
537     const struct rtnl_link_ops *rtnl_link_ops;
538 
539     /* for setting kernel sock attribute on TCP connection setup */
540 #define GSO_MAX_SIZE        65536
541     unsigned int        gso_max_size;
542 #define GSO_MAX_SEGS        65535
543     u16            gso_max_segs;
544 
545 #ifdef CONFIG_DCB
546     const struct dcbnl_rtnl_ops *dcbnl_ops;
547 #endif
548     u8            num_tc;
549     struct netdev_tc_txq    tc_to_txq[TC_MAX_QUEUE];
550     u8            prio_tc_map[TC_BITMASK + 1];
551 
552 #if IS_ENABLED(CONFIG_FCOE)
553     unsigned int        fcoe_ddp_xid;
554 #endif
555 #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
556     struct netprio_map __rcu *priomap;
557 #endif
558     struct phy_device    *phydev;
559     struct lock_class_key    *qdisc_tx_busylock;
560     struct lock_class_key    *qdisc_running_key;
561     bool            proto_down;
562 };

 

其中net_device->netdev_ops对应设备操作函数结构,用来初始化,销毁,开启,关闭,修改某些变量值等相关操作;

  1 /*
  2  * This structure defines the management hooks for network devices.
  3  * The following hooks can be defined; unless noted otherwise, they are
  4  * optional and can be filled with a null pointer.
  5  *
  6  * int (*ndo_init)(struct net_device *dev);
  7  *     This function is called once when a network device is registered.
  8  *     The network device can use this for any late stage initialization
  9  *     or semantic validation. It can fail with an error code which will
 10  *     be propagated back to register_netdev.
 11  *
 12  * void (*ndo_uninit)(struct net_device *dev);
 13  *     This function is called when device is unregistered or when registration
 14  *     fails. It is not called if init fails.
 15  *
 16  * int (*ndo_open)(struct net_device *dev);
 17  *     This function is called when a network device transitions to the up
 18  *     state.
 19  *
 20  * int (*ndo_stop)(struct net_device *dev);
 21  *     This function is called when a network device transitions to the down
 22  *     state.
 23  *
 24  * netdev_tx_t (*ndo_start_xmit)(struct sk_buff *skb,
 25  *                               struct net_device *dev);
 26  *    Called when a packet needs to be transmitted.
 27  *    Returns NETDEV_TX_OK.  Can return NETDEV_TX_BUSY, but you should stop
 28  *    the queue before that can happen; it's for obsolete devices and weird
 29  *    corner cases, but the stack really does a non-trivial amount
 30  *    of useless work if you return NETDEV_TX_BUSY.
 31  *    Required; cannot be NULL.
 32  *
 33  * netdev_features_t (*ndo_features_check)(struct sk_buff *skb,
 34  *                       struct net_device *dev
 35  *                       netdev_features_t features);
 36  *    Called by core transmit path to determine if device is capable of
 37  *    performing offload operations on a given packet. This is to give
 38  *    the device an opportunity to implement any restrictions that cannot
 39  *    be otherwise expressed by feature flags. The check is called with
 40  *    the set of features that the stack has calculated and it returns
 41  *    those the driver believes to be appropriate.
 42  *
 43  * u16 (*ndo_select_queue)(struct net_device *dev, struct sk_buff *skb,
 44  *                         void *accel_priv, select_queue_fallback_t fallback);
 45  *    Called to decide which queue to use when device supports multiple
 46  *    transmit queues.
 47  *
 48  * void (*ndo_change_rx_flags)(struct net_device *dev, int flags);
 49  *    This function is called to allow device receiver to make
 50  *    changes to configuration when multicast or promiscuous is enabled.
 51  *
 52  * void (*ndo_set_rx_mode)(struct net_device *dev);
 53  *    This function is called device changes address list filtering.
 54  *    If driver handles unicast address filtering, it should set
 55  *    IFF_UNICAST_FLT in its priv_flags.
 56  *
 57  * int (*ndo_set_mac_address)(struct net_device *dev, void *addr);
 58  *    This function  is called when the Media Access Control address
 59  *    needs to be changed. If this interface is not defined, the
 60  *    MAC address can not be changed.
 61  *
 62  * int (*ndo_validate_addr)(struct net_device *dev);
 63  *    Test if Media Access Control address is valid for the device.
 64  *
 65  * int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
 66  *    Called when a user requests an ioctl which can't be handled by
 67  *    the generic interface code. If not defined ioctls return
 68  *    not supported error code.
 69  *
 70  * int (*ndo_set_config)(struct net_device *dev, struct ifmap *map);
 71  *    Used to set network devices bus interface parameters. This interface
 72  *    is retained for legacy reasons; new devices should use the bus
 73  *    interface (PCI) for low level management.
 74  *
 75  * int (*ndo_change_mtu)(struct net_device *dev, int new_mtu);
 76  *    Called when a user wants to change the Maximum Transfer Unit
 77  *    of a device.
 78  *
 79  * void (*ndo_tx_timeout)(struct net_device *dev);
 80  *    Callback used when the transmitter has not made any progress
 81  *    for dev->watchdog ticks.
 82  *
 83  * void (*ndo_get_stats64)(struct net_device *dev,
 84  *                         struct rtnl_link_stats64 *storage);
 85  * struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);
 86  *    Called when a user wants to get the network device usage
 87  *    statistics. Drivers must do one of the following:
 88  *    1. Define @ndo_get_stats64 to fill in a zero-initialised
 89  *       rtnl_link_stats64 structure passed by the caller.
 90  *    2. Define @ndo_get_stats to update a net_device_stats structure
 91  *       (which should normally be dev->stats) and return a pointer to
 92  *       it. The structure may be changed asynchronously only if each
 93  *       field is written atomically.
 94  *    3. Update dev->stats asynchronously and atomically, and define
 95  *       neither operation.
 96  *
 97  * bool (*ndo_has_offload_stats)(const struct net_device *dev, int attr_id)
 98  *    Return true if this device supports offload stats of this attr_id.
 99  *
100  * int (*ndo_get_offload_stats)(int attr_id, const struct net_device *dev,
101  *    void *attr_data)
102  *    Get statistics for offload operations by attr_id. Write it into the
103  *    attr_data pointer.
104  *
105  * int (*ndo_vlan_rx_add_vid)(struct net_device *dev, __be16 proto, u16 vid);
106  *    If device supports VLAN filtering this function is called when a
107  *    VLAN id is registered.
108  *
109  * int (*ndo_vlan_rx_kill_vid)(struct net_device *dev, __be16 proto, u16 vid);
110  *    If device supports VLAN filtering this function is called when a
111  *    VLAN id is unregistered.
112  *
113  * void (*ndo_poll_controller)(struct net_device *dev);
114  *
115  *    SR-IOV management functions.
116  * int (*ndo_set_vf_mac)(struct net_device *dev, int vf, u8* mac);
117  * int (*ndo_set_vf_vlan)(struct net_device *dev, int vf, u16 vlan,
118  *              u8 qos, __be16 proto);
119  * int (*ndo_set_vf_rate)(struct net_device *dev, int vf, int min_tx_rate,
120  *              int max_tx_rate);
121  * int (*ndo_set_vf_spoofchk)(struct net_device *dev, int vf, bool setting);
122  * int (*ndo_set_vf_trust)(struct net_device *dev, int vf, bool setting);
123  * int (*ndo_get_vf_config)(struct net_device *dev,
124  *                int vf, struct ifla_vf_info *ivf);
125  * int (*ndo_set_vf_link_state)(struct net_device *dev, int vf, int link_state);
126  * int (*ndo_set_vf_port)(struct net_device *dev, int vf,
127  *              struct nlattr *port[]);
128  *
129  *      Enable or disable the VF ability to query its RSS Redirection Table and
130  *      Hash Key. This is needed since on some devices VF share this information
131  *      with PF and querying it may introduce a theoretical security risk.
132  * int (*ndo_set_vf_rss_query_en)(struct net_device *dev, int vf, bool setting);
133  * int (*ndo_get_vf_port)(struct net_device *dev, int vf, struct sk_buff *skb);
134  * int (*ndo_setup_tc)(struct net_device *dev, u32 handle,
135  *               __be16 protocol, struct tc_to_netdev *tc);
136  *    Called to setup any 'tc' scheduler, classifier or action on @dev.
137  *    This is always called from the stack with the rtnl lock held and netif
138  *    tx queues stopped. This allows the netdevice to perform queue
139  *    management safely.
140  *
141  *    Fiber Channel over Ethernet (FCoE) offload functions.
142  * int (*ndo_fcoe_enable)(struct net_device *dev);
143  *    Called when the FCoE protocol stack wants to start using LLD for FCoE
144  *    so the underlying device can perform whatever needed configuration or
145  *    initialization to support acceleration of FCoE traffic.
146  *
147  * int (*ndo_fcoe_disable)(struct net_device *dev);
148  *    Called when the FCoE protocol stack wants to stop using LLD for FCoE
149  *    so the underlying device can perform whatever needed clean-ups to
150  *    stop supporting acceleration of FCoE traffic.
151  *
152  * int (*ndo_fcoe_ddp_setup)(struct net_device *dev, u16 xid,
153  *                 struct scatterlist *sgl, unsigned int sgc);
154  *    Called when the FCoE Initiator wants to initialize an I/O that
155  *    is a possible candidate for Direct Data Placement (DDP). The LLD can
156  *    perform necessary setup and returns 1 to indicate the device is set up
157  *    successfully to perform DDP on this I/O, otherwise this returns 0.
158  *
159  * int (*ndo_fcoe_ddp_done)(struct net_device *dev,  u16 xid);
160  *    Called when the FCoE Initiator/Target is done with the DDPed I/O as
161  *    indicated by the FC exchange id 'xid', so the underlying device can
162  *    clean up and reuse resources for later DDP requests.
163  *
164  * int (*ndo_fcoe_ddp_target)(struct net_device *dev, u16 xid,
165  *                  struct scatterlist *sgl, unsigned int sgc);
166  *    Called when the FCoE Target wants to initialize an I/O that
167  *    is a possible candidate for Direct Data Placement (DDP). The LLD can
168  *    perform necessary setup and returns 1 to indicate the device is set up
169  *    successfully to perform DDP on this I/O, otherwise this returns 0.
170  *
171  * int (*ndo_fcoe_get_hbainfo)(struct net_device *dev,
172  *                   struct netdev_fcoe_hbainfo *hbainfo);
173  *    Called when the FCoE Protocol stack wants information on the underlying
174  *    device. This information is utilized by the FCoE protocol stack to
175  *    register attributes with Fiber Channel management service as per the
176  *    FC-GS Fabric Device Management Information(FDMI) specification.
177  *
178  * int (*ndo_fcoe_get_wwn)(struct net_device *dev, u64 *wwn, int type);
179  *    Called when the underlying device wants to override default World Wide
180  *    Name (WWN) generation mechanism in FCoE protocol stack to pass its own
181  *    World Wide Port Name (WWPN) or World Wide Node Name (WWNN) to the FCoE
182  *    protocol stack to use.
183  *
184  *    RFS acceleration.
185  * int (*ndo_rx_flow_steer)(struct net_device *dev, const struct sk_buff *skb,
186  *                u16 rxq_index, u32 flow_id);
187  *    Set hardware filter for RFS.  rxq_index is the target queue index;
188  *    flow_id is a flow ID to be passed to rps_may_expire_flow() later.
189  *    Return the filter ID on success, or a negative error code.
190  *
191  *    Slave management functions (for bridge, bonding, etc).
192  * int (*ndo_add_slave)(struct net_device *dev, struct net_device *slave_dev);
193  *    Called to make another netdev an underling.
194  *
195  * int (*ndo_del_slave)(struct net_device *dev, struct net_device *slave_dev);
196  *    Called to release previously enslaved netdev.
197  *
198  *      Feature/offload setting functions.
199  * netdev_features_t (*ndo_fix_features)(struct net_device *dev,
200  *        netdev_features_t features);
201  *    Adjusts the requested feature flags according to device-specific
202  *    constraints, and returns the resulting flags. Must not modify
203  *    the device state.
204  *
205  * int (*ndo_set_features)(struct net_device *dev, netdev_features_t features);
206  *    Called to update device configuration to new features. Passed
207  *    feature set might be less than what was returned by ndo_fix_features()).
208  *    Must return >0 or -errno if it changed dev->features itself.
209  *
210  * int (*ndo_fdb_add)(struct ndmsg *ndm, struct nlattr *tb[],
211  *              struct net_device *dev,
212  *              const unsigned char *addr, u16 vid, u16 flags)
213  *    Adds an FDB entry to dev for addr.
214  * int (*ndo_fdb_del)(struct ndmsg *ndm, struct nlattr *tb[],
215  *              struct net_device *dev,
216  *              const unsigned char *addr, u16 vid)
217  *    Deletes the FDB entry from dev coresponding to addr.
218  * int (*ndo_fdb_dump)(struct sk_buff *skb, struct netlink_callback *cb,
219  *               struct net_device *dev, struct net_device *filter_dev,
220  *               int *idx)
221  *    Used to add FDB entries to dump requests. Implementers should add
222  *    entries to skb and update idx with the number of entries.
223  *
224  * int (*ndo_bridge_setlink)(struct net_device *dev, struct nlmsghdr *nlh,
225  *                 u16 flags)
226  * int (*ndo_bridge_getlink)(struct sk_buff *skb, u32 pid, u32 seq,
227  *                 struct net_device *dev, u32 filter_mask,
228  *                 int nlflags)
229  * int (*ndo_bridge_dellink)(struct net_device *dev, struct nlmsghdr *nlh,
230  *                 u16 flags);
231  *
232  * int (*ndo_change_carrier)(struct net_device *dev, bool new_carrier);
233  *    Called to change device carrier. Soft-devices (like dummy, team, etc)
234  *    which do not represent real hardware may define this to allow their
235  *    userspace components to manage their virtual carrier state. Devices
236  *    that determine carrier state from physical hardware properties (eg
237  *    network cables) or protocol-dependent mechanisms (eg
238  *    USB_CDC_NOTIFY_NETWORK_CONNECTION) should NOT implement this function.
239  *
240  * int (*ndo_get_phys_port_id)(struct net_device *dev,
241  *                   struct netdev_phys_item_id *ppid);
242  *    Called to get ID of physical port of this device. If driver does
243  *    not implement this, it is assumed that the hw is not able to have
244  *    multiple net devices on single physical port.
245  *
246  * void (*ndo_udp_tunnel_add)(struct net_device *dev,
247  *                  struct udp_tunnel_info *ti);
248  *    Called by UDP tunnel to notify a driver about the UDP port and socket
249  *    address family that a UDP tunnel is listnening to. It is called only
250  *    when a new port starts listening. The operation is protected by the
251  *    RTNL.
252  *
253  * void (*ndo_udp_tunnel_del)(struct net_device *dev,
254  *                  struct udp_tunnel_info *ti);
255  *    Called by UDP tunnel to notify the driver about a UDP port and socket
256  *    address family that the UDP tunnel is not listening to anymore. The
257  *    operation is protected by the RTNL.
258  *
259  * void* (*ndo_dfwd_add_station)(struct net_device *pdev,
260  *                 struct net_device *dev)
261  *    Called by upper layer devices to accelerate switching or other
262  *    station functionality into hardware. 'pdev is the lowerdev
263  *    to use for the offload and 'dev' is the net device that will
264  *    back the offload. Returns a pointer to the private structure
265  *    the upper layer will maintain.
266  * void (*ndo_dfwd_del_station)(struct net_device *pdev, void *priv)
267  *    Called by upper layer device to delete the station created
268  *    by 'ndo_dfwd_add_station'. 'pdev' is the net device backing
269  *    the station and priv is the structure returned by the add
270  *    operation.
271  * netdev_tx_t (*ndo_dfwd_start_xmit)(struct sk_buff *skb,
272  *                      struct net_device *dev,
273  *                      void *priv);
274  *    Callback to use for xmit over the accelerated station. This
275  *    is used in place of ndo_start_xmit on accelerated net
276  *    devices.
277  * int (*ndo_set_tx_maxrate)(struct net_device *dev,
278  *                 int queue_index, u32 maxrate);
279  *    Called when a user wants to set a max-rate limitation of specific
280  *    TX queue.
281  * int (*ndo_get_iflink)(const struct net_device *dev);
282  *    Called to get the iflink value of this device.
283  * void (*ndo_change_proto_down)(struct net_device *dev,
284  *                 bool proto_down);
285  *    This function is used to pass protocol port error state information
286  *    to the switch driver. The switch driver can react to the proto_down
287  *      by doing a phys down on the associated switch port.
288  * int (*ndo_fill_metadata_dst)(struct net_device *dev, struct sk_buff *skb);
289  *    This function is used to get egress tunnel information for given skb.
290  *    This is useful for retrieving outer tunnel header parameters while
291  *    sampling packet.
292  * void (*ndo_set_rx_headroom)(struct net_device *dev, int needed_headroom);
293  *    This function is used to specify the headroom that the skb must
294  *    consider when allocation skb during packet reception. Setting
295  *    appropriate rx headroom value allows avoiding skb head copy on
296  *    forward. Setting a negative value resets the rx headroom to the
297  *    default value.
298  * int (*ndo_xdp)(struct net_device *dev, struct netdev_xdp *xdp);
299  *    This function is used to set or query state related to XDP on the
300  *    netdevice. See definition of enum xdp_netdev_command for details.
301  *
302  */
303 struct net_device_ops {
304     int            (*ndo_init)(struct net_device *dev);
305     void            (*ndo_uninit)(struct net_device *dev);
306     int            (*ndo_open)(struct net_device *dev);
307     int            (*ndo_stop)(struct net_device *dev);
308     netdev_tx_t        (*ndo_start_xmit)(struct sk_buff *skb,
309                           struct net_device *dev);
310     netdev_features_t    (*ndo_features_check)(struct sk_buff *skb,
311                               struct net_device *dev,
312                               netdev_features_t features);
313     u16            (*ndo_select_queue)(struct net_device *dev,
314                             struct sk_buff *skb,
315                             void *accel_priv,
316                             select_queue_fallback_t fallback);
317     void            (*ndo_change_rx_flags)(struct net_device *dev,
318                                int flags);
319     void            (*ndo_set_rx_mode)(struct net_device *dev);
320     int            (*ndo_set_mac_address)(struct net_device *dev,
321                                void *addr);
322     int            (*ndo_validate_addr)(struct net_device *dev);
323     int            (*ndo_do_ioctl)(struct net_device *dev,
324                             struct ifreq *ifr, int cmd);
325     int            (*ndo_set_config)(struct net_device *dev,
326                               struct ifmap *map);
327     int            (*ndo_change_mtu)(struct net_device *dev,
328                           int new_mtu);
329     int            (*ndo_neigh_setup)(struct net_device *dev,
330                            struct neigh_parms *);
331     void            (*ndo_tx_timeout) (struct net_device *dev);
332 
333     void            (*ndo_get_stats64)(struct net_device *dev,
334                            struct rtnl_link_stats64 *storage);
335     bool            (*ndo_has_offload_stats)(const struct net_device *dev, int attr_id);
336     int            (*ndo_get_offload_stats)(int attr_id,
337                              const struct net_device *dev,
338                              void *attr_data);
339     struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);
340 
341     int            (*ndo_vlan_rx_add_vid)(struct net_device *dev,
342                                __be16 proto, u16 vid);
343     int            (*ndo_vlan_rx_kill_vid)(struct net_device *dev,
344                                 __be16 proto, u16 vid);
345 #ifdef CONFIG_NET_POLL_CONTROLLER
346     void                    (*ndo_poll_controller)(struct net_device *dev);
347     int            (*ndo_netpoll_setup)(struct net_device *dev,
348                              struct netpoll_info *info);
349     void            (*ndo_netpoll_cleanup)(struct net_device *dev);
350 #endif
351     int            (*ndo_set_vf_mac)(struct net_device *dev,
352                           int queue, u8 *mac);
353     int            (*ndo_set_vf_vlan)(struct net_device *dev,
354                            int queue, u16 vlan,
355                            u8 qos, __be16 proto);
356     int            (*ndo_set_vf_rate)(struct net_device *dev,
357                            int vf, int min_tx_rate,
358                            int max_tx_rate);
359     int            (*ndo_set_vf_spoofchk)(struct net_device *dev,
360                                int vf, bool setting);
361     int            (*ndo_set_vf_trust)(struct net_device *dev,
362                             int vf, bool setting);
363     int            (*ndo_get_vf_config)(struct net_device *dev,
364                              int vf,
365                              struct ifla_vf_info *ivf);
366     int            (*ndo_set_vf_link_state)(struct net_device *dev,
367                              int vf, int link_state);
368     int            (*ndo_get_vf_stats)(struct net_device *dev,
369                             int vf,
370                             struct ifla_vf_stats
371                             *vf_stats);
372     int            (*ndo_set_vf_port)(struct net_device *dev,
373                            int vf,
374                            struct nlattr *port[]);
375     int            (*ndo_get_vf_port)(struct net_device *dev,
376                            int vf, struct sk_buff *skb);
377     int            (*ndo_set_vf_guid)(struct net_device *dev,
378                            int vf, u64 guid,
379                            int guid_type);
380     int            (*ndo_set_vf_rss_query_en)(
381                            struct net_device *dev,
382                            int vf, bool setting);
383     int            (*ndo_setup_tc)(struct net_device *dev,
384                         u32 handle,
385                         __be16 protocol,
386                         struct tc_to_netdev *tc);
387 #if IS_ENABLED(CONFIG_FCOE)
388     int            (*ndo_fcoe_enable)(struct net_device *dev);
389     int            (*ndo_fcoe_disable)(struct net_device *dev);
390     int            (*ndo_fcoe_ddp_setup)(struct net_device *dev,
391                               u16 xid,
392                               struct scatterlist *sgl,
393                               unsigned int sgc);
394     int            (*ndo_fcoe_ddp_done)(struct net_device *dev,
395                              u16 xid);
396     int            (*ndo_fcoe_ddp_target)(struct net_device *dev,
397                                u16 xid,
398                                struct scatterlist *sgl,
399                                unsigned int sgc);
400     int            (*ndo_fcoe_get_hbainfo)(struct net_device *dev,
401                             struct netdev_fcoe_hbainfo *hbainfo);
402 #endif
403 
404 #if IS_ENABLED(CONFIG_LIBFCOE)
405 #define NETDEV_FCOE_WWNN 0
406 #define NETDEV_FCOE_WWPN 1
407     int            (*ndo_fcoe_get_wwn)(struct net_device *dev,
408                             u64 *wwn, int type);
409 #endif
410 
411 #ifdef CONFIG_RFS_ACCEL
412     int            (*ndo_rx_flow_steer)(struct net_device *dev,
413                              const struct sk_buff *skb,
414                              u16 rxq_index,
415                              u32 flow_id);
416 #endif
417     int            (*ndo_add_slave)(struct net_device *dev,
418                          struct net_device *slave_dev);
419     int            (*ndo_del_slave)(struct net_device *dev,
420                          struct net_device *slave_dev);
421     netdev_features_t    (*ndo_fix_features)(struct net_device *dev,
422                             netdev_features_t features);
423     int            (*ndo_set_features)(struct net_device *dev,
424                             netdev_features_t features);
425     int            (*ndo_neigh_construct)(struct net_device *dev,
426                                struct neighbour *n);
427     void            (*ndo_neigh_destroy)(struct net_device *dev,
428                              struct neighbour *n);
429 
430     int            (*ndo_fdb_add)(struct ndmsg *ndm,
431                            struct nlattr *tb[],
432                            struct net_device *dev,
433                            const unsigned char *addr,
434                            u16 vid,
435                            u16 flags);
436     int            (*ndo_fdb_del)(struct ndmsg *ndm,
437                            struct nlattr *tb[],
438                            struct net_device *dev,
439                            const unsigned char *addr,
440                            u16 vid);
441     int            (*ndo_fdb_dump)(struct sk_buff *skb,
442                         struct netlink_callback *cb,
443                         struct net_device *dev,
444                         struct net_device *filter_dev,
445                         int *idx);
446 
447     int            (*ndo_bridge_setlink)(struct net_device *dev,
448                               struct nlmsghdr *nlh,
449                               u16 flags);
450     int            (*ndo_bridge_getlink)(struct sk_buff *skb,
451                               u32 pid, u32 seq,
452                               struct net_device *dev,
453                               u32 filter_mask,
454                               int nlflags);
455     int            (*ndo_bridge_dellink)(struct net_device *dev,
456                               struct nlmsghdr *nlh,
457                               u16 flags);
458     int            (*ndo_change_carrier)(struct net_device *dev,
459                               bool new_carrier);
460     int            (*ndo_get_phys_port_id)(struct net_device *dev,
461                             struct netdev_phys_item_id *ppid);
462     int            (*ndo_get_phys_port_name)(struct net_device *dev,
463                               char *name, size_t len);
464     void            (*ndo_udp_tunnel_add)(struct net_device *dev,
465                               struct udp_tunnel_info *ti);
466     void            (*ndo_udp_tunnel_del)(struct net_device *dev,
467                               struct udp_tunnel_info *ti);
468     void*            (*ndo_dfwd_add_station)(struct net_device *pdev,
469                             struct net_device *dev);
470     void            (*ndo_dfwd_del_station)(struct net_device *pdev,
471                             void *priv);
472 
473     netdev_tx_t        (*ndo_dfwd_start_xmit) (struct sk_buff *skb,
474                             struct net_device *dev,
475                             void *priv);
476     int            (*ndo_get_lock_subclass)(struct net_device *dev);
477     int            (*ndo_set_tx_maxrate)(struct net_device *dev,
478                               int queue_index,
479                               u32 maxrate);
480     int            (*ndo_get_iflink)(const struct net_device *dev);
481     int            (*ndo_change_proto_down)(struct net_device *dev,
482                              bool proto_down);
483     int            (*ndo_fill_metadata_dst)(struct net_device *dev,
484                                struct sk_buff *skb);
485     void            (*ndo_set_rx_headroom)(struct net_device *dev,
486                                int needed_headroom);
487     int            (*ndo_xdp)(struct net_device *dev,
488                        struct netdev_xdp *xdp);
489 };

 

net_device->header_ops用来进行链路头部操作,包括创建,获取,缓存,校验等;

 1 struct header_ops {
 2     int    (*create) (struct sk_buff *skb, struct net_device *dev,
 3                unsigned short type, const void *daddr,
 4                const void *saddr, unsigned int len);
 5     int    (*parse)(const struct sk_buff *skb, unsigned char *haddr);
 6     int    (*cache)(const struct neighbour *neigh, struct hh_cache *hh, __be16 type);
 7     void    (*cache_update)(struct hh_cache *hh,
 8                 const struct net_device *dev,
 9                 const unsigned char *haddr);
10     bool    (*validate)(const char *ll_header, unsigned int len);
11 };

 

转载于:https://www.cnblogs.com/wanpengcoder/p/7526116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值