网络驱动有关结构体的含义

本文介绍了网络驱动中的两个重要结构体:struct sk_buff和struct net_device。struct sk_buff主要用于描述网络数据包,包含如数据长度、头部信息和缓冲区等关键字段;而struct net_device则包含了设备的I/O特定信息,如设备名、中断号、硬件特性等。这两个结构体在Linux内核网络层中起着核心作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先看一下网络驱动必用结构体struct sk_buff

不怎么使用的就没有出现解释:

struct sk_buff {
 /* These two members must be first. */
 struct sk_buff  *next;
 struct sk_buff  *prev;

 struct sock   *sk;  //哪个socket套机字拥有此buffer
 ktime_t    tstamp;  //到达所使用的时间
 struct net_device *dev;

 unsigned long  _skb_dst; //目的入口
#ifdef CONFIG_XFRM
 struct sec_path *sp;
#endif
 /*
  * This is the control buffer. It is free to use for every
  * layer. Please put your private variables there. If you
  * want to keep them across layers you have to do a skb_clone()
  * first. This is owned by whoever has the skb queued ATM.
  */
 char    cb[48];  //可控的缓冲,可以对于每一层自由使用

 unsigned int  len,  //真实的数据长度
      data_len; //数据长度
 __u16    mac_len, //链路层头部长度
      hdr_len; //skb_clone的skb可改写头部的长度
 
 //校验
 union {
  __wsum  csum;
  struct {
   __u16 csum_start;
   __u16 csum_offset;
  };
 };
 __u32   priority;  //包的优先级
 kmemcheck_bitfield_begin(flags1);
 __u8   local_df:1,   //是否允许分片
     cloned:1,   //头部被复制到子分片
     ip_summed:2,  //ip校验和
     nohdr:1,   //有效载荷的参考
     nfctinfo:3;
 __u8   pkt_type:3,   //包的类型
     fclone:2,   //skbuff clone状态
     ipvs_property:1, //ipvs拥有
     peeked:1,   //此包已发送过
     nf_trace:1;   //网络过滤包的追踪标志
 __be16   protocol:16;  //协议版本
 kmemcheck_bitfield_end(flags1);

 void   (*destructor)(struct sk_buff *skb);   //自动释放
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
 struct nf_conntrack *nfct;
 struct sk_buff  *nfct_reasm;
#endif
#ifdef CONFIG_BRIDGE_NETFILTER
 struct nf_bridge_info *nf_bridge;
#endif

 int   iif;
#ifdef CONFIG_NET_SCHED
 __u16   tc_index; /* traffic control index */
#ifdef CONFIG_NET_CLS_ACT
 __u16   tc_verd; /* traffic control verdict */
#endif
#endif

 kmemcheck_bitfield_begin(flags2);
 __u16   queue_mapping:16;
#ifdef CONFIG_IPV6_NDISC_NODETYPE
 __u8   ndisc_nodetype:2;
#endif
 kmemcheck_bitfield_end(flags2);

 /* 0/14 bit hole */

#ifdef CONFIG_NET_DMA
 dma_cookie_t  dma_cookie;
#endif
#ifdef CONFIG_NETWORK_SECMARK
 __u32   secmark;
#endif

 __u32   mark;

 __u16   vlan_tci;

 sk_buff_data_t  transport_header; //如tcp/udp头部
 sk_buff_data_t  network_header;  //ip头部
 sk_buff_data_t  mac_header;   //数据链路层头部 mac头部
 /* These elements must be at the end, see alloc_skb() for details.  */
 sk_buff_data_t  tail;    //有效数据指针尾部
 sk_buff_data_t  end;    //缓冲区结束处
 unsigned char  *head,    //缓冲区头部
    *data;      //缓冲真正的数据头部指针
 unsigned int  truesize;   //缓冲的真正数据的长度
 atomic_t  users;     //使用的人数
};

下一个就是struct net_device,这个里面的确较为麻烦,连linux内核都这么注释

 * The DEVICE structure.
 * Actually, this whole structure is a big mistake.  It mixes I/O
 * data with strictly "high-level" data, and it has to know about
 * almost every data structure used in the INET module.

在使用中用到后,再注释其他的

struct net_device
{

 /*
  * This is the first field of the "visible" part of this structure
  * (i.e. as seen by users in the "Space.c" file).  It is the name
  * the interface.
  */
 char   name[IFNAMSIZ];    //设备名,一般会组织成eth%d
 /* device name hash chain */
 struct hlist_node name_hlist;   //设备的hash链表
 /* snmp alias */
 char    *ifalias;     

 /*
  * I/O specific fields
  * FIXME: Merge these and struct ifmap into one
  */
 unsigned long  mem_end; /* shared mem end */   //映射的io端口的结束地址
 unsigned long  mem_start; /* shared mem start */   //映射的io端口的开始地址
 unsigned long  base_addr; /* device I/O address */ //IO基地址
 unsigned int  irq;  /* device IRQ number */ //中断号

 /*
  * Some hardware also needs these fields, but they are not
  * part of the usual set specified in Space.c.
  */

 unsigned char  if_port; /* Selectable AUI, TP,..*/
 unsigned char  dma;  /* DMA channel  */

 unsigned long  state;      //设备的状态

 struct list_head dev_list;     //设备列表
 struct list_head napi_list;

 /* Net device features */
 unsigned long  features;
#define NETIF_F_SG    1  /* Scatter/gather IO. */
#define NETIF_F_IP_CSUM   2  /* Can checksum TCP/UDP over IPv4. */
#define NETIF_F_NO_CSUM   4  /* Does not require checksum. F.e. loopack. */
#define NETIF_F_HW_CSUM   8  /* Can checksum all the packets. */
#define NETIF_F_IPV6_CSUM  16  /* Can checksum TCP/UDP over IPV6 */
#define NETIF_F_HIGHDMA   32  /* Can DMA to high memory. */
#define NETIF_F_FRAGLIST  64  /* Scatter/gather IO. */
#define NETIF_F_HW_VLAN_TX  128  /* Transmit VLAN hw acceleration */
#define NETIF_F_HW_VLAN_RX  256  /* Receive VLAN hw acceleration */
#define NETIF_F_HW_VLAN_FILTER 512  /* Receive filtering on VLAN */
#define NETIF_F_VLAN_CHALLENGED 1024 /* Device cannot handle VLAN packets */
#define NETIF_F_GSO    2048 /* Enable software GSO. */
#define NETIF_F_LLTX   4096 /* LockLess TX - deprecated. Please */
     /* do not use LLTX in new drivers */
#define NETIF_F_NETNS_LOCAL  8192 /* Does not change network namespaces */
#define NETIF_F_GRO    16384 /* Generic receive offload */
#define NETIF_F_LRO    32768 /* large receive offload */

/* the GSO_MASK reserves bits 16 through 23 */
#define NETIF_F_FCOE_CRC (1 << 24) /* FCoE CRC32 */
#define NETIF_F_SCTP_CSUM (1 << 25) /* SCTP checksum offload */
#define NETIF_F_FCOE_MTU (1 << 26) /* Supports max FCoE MTU, 2158 bytes*/

 /* Segmentation offload features */
#define NETIF_F_GSO_SHIFT   16
#define NETIF_F_GSO_MASK   0x00ff0000
#define NETIF_F_TSO     (SKB_GSO_TCPV4 << NETIF_F_GSO_SHIFT)
#define NETIF_F_UFO     (SKB_GSO_UDP << NETIF_F_GSO_SHIFT)
#define NETIF_F_GSO_ROBUST   (SKB_GSO_DODGY << NETIF_F_GSO_SHIFT)
#define NETIF_F_TSO_ECN    (SKB_GSO_TCP_ECN << NETIF_F_GSO_SHIFT)
#define NETIF_F_TSO6    (SKB_GSO_TCPV6 << NETIF_F_GSO_SHIFT)
#define NETIF_F_FSO     (SKB_GSO_FCOE << NETIF_F_GSO_SHIFT)

 /* List of features with software fallbacks. */
#define NETIF_F_GSO_SOFTWARE  (NETIF_F_TSO | NETIF_F_TSO_ECN | NETIF_F_TSO6)


#define NETIF_F_GEN_CSUM   (NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)
#define NETIF_F_V4_CSUM    (NETIF_F_GEN_CSUM | NETIF_F_IP_CSUM)
#define NETIF_F_V6_CSUM    (NETIF_F_GEN_CSUM | NETIF_F_IPV6_CSUM)
#define NETIF_F_ALL_CSUM   (NETIF_F_V4_CSUM | NETIF_F_V6_CSUM)

 /*
  * If one device supports one of these features, then enable them
  * for all in netdev_increment_features.
  */
#define NETIF_F_ONE_FOR_ALL (NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ROBUST | \
     NETIF_F_SG | NETIF_F_HIGHDMA |  \
     NETIF_F_FRAGLIST)

 /* Interface index. Unique device identifier */
 int   ifindex;
 int   iflink;

 struct net_device_stats stats;

#ifdef CONFIG_WIRELESS_EXT
 /* List of functions to handle Wireless Extensions (instead of ioctl).
  * See <net/iw_handler.h> for details. Jean II */
 const struct iw_handler_def *  wireless_handlers;
 /* Instance data managed by the core of Wireless Extensions. */
 struct iw_public_data *    wireless_data;
#endif

 /* Management operations */
 const struct net_device_ops*  netdev_ops;   //网络设备的操作集
 const struct ethtool_ops*   ethtool_ops; //以太网工具的操作集

 /* Hardware header description */
 const struct header_ops *   header_ops;   //头部操作函数集

 unsigned int    flags; /* interface flags (a la BSD) */
 unsigned short    gflags;
    unsigned short    priv_flags; /* Like 'flags' but invisible to userspace. */
 unsigned short    padded; /* How much padding added by alloc_netdev() */

 unsigned char    operstate; /* RFC2863 operstate */
 unsigned char    link_mode; /* mapping policy to operstate */    //连接状态

 unsigned     mtu; /* interface MTU value  */         //接口最大传输单元
 unsigned short    type; /* interface hardware type */         //硬件接口类型
 unsigned short    hard_header_len; /* hardware hdr length */ //硬件头部长度

 /* extra head- and tailroom the hardware may need, but not in all cases
  * can this be guaranteed, especially tailroom. Some cases also use
  * LL_MAX_HEADER instead to allocate the skb.
  */
 unsigned short  needed_headroom;
 unsigned short  needed_tailroom;

 struct net_device *master; /* Pointer to master device of a group,
       * which this device is member of.
       */

 /* Interface address info. */
 unsigned char   perm_addr[MAX_ADDR_LEN]; /* permanent hw address */
 unsigned char   addr_len;     /* hardware address length */
 unsigned short          dev_id;      /* for shared network cards */

 struct netdev_hw_addr_list uc;    /* Secondary unicast mac addresses */
 int      uc_promisc;
 spinlock_t    addr_list_lock;
 struct dev_addr_list *mc_list;   /* Multicast mac addresses */   //多个网卡地址列表
 int      mc_count;   /* Number of installed mcasts */
 unsigned int   promiscuity;
 unsigned int   allmulti;


 /* Protocol specific pointers */
 
#ifdef CONFIG_NET_DSA
 void   *dsa_ptr; /* dsa specific data */
#endif
 void    *atalk_ptr; /* AppleTalk link  */
 void   *ip_ptr; /* IPv4 specific data */
 void            *dn_ptr;        /* DECnet specific data */
 void            *ip6_ptr;       /* IPv6 specific data */
 void   *ec_ptr; /* Econet specific data */
 void   *ax25_ptr; /* AX.25 specific data */
 struct wireless_dev *ieee80211_ptr; /* IEEE 802.11 specific data,
         assign before registering */

/*
 * Cache line mostly used on receive path (including eth_type_trans())
 */
 unsigned long  last_rx; /* Time of last Rx */
 /* Interface address info used in eth_type_trans() */
 unsigned char  *dev_addr; /* hw address, (before bcast
         because most packets are
         unicast) */

 struct netdev_hw_addr_list dev_addrs; /* list of device
            hw addresses */

 unsigned char  broadcast[MAX_ADDR_LEN]; /* hw bcast add */

 struct netdev_queue rx_queue;

 struct netdev_queue *_tx ____cacheline_aligned_in_smp;

 /* Number of TX queues allocated at alloc_netdev_mq() time  */
 unsigned int  num_tx_queues;               //mq队列的数量,在初始化分配空间会建立这个队列

 /* Number of TX queues currently active in device  */
 unsigned int  real_num_tx_queues;          //真正使用的有几个队列

 /* root qdisc from userspace point of view */
 struct Qdisc  *qdisc;

 unsigned long  tx_queue_len; /* Max frames per queue allowed */
 spinlock_t   tx_global_lock;
/*
 * One part is mostly used on xmit path (device)
 */
 /* These may be needed for future network-power-down code. */

 /*
  * trans_start here is expensive for high speed devices on SMP,
  * please use netdev_queue->trans_start instead.
  */
 unsigned long  trans_start; /* Time (in jiffies) of last Tx */

 int     watchdog_timeo; /* used by dev_watchdog() */
 struct timer_list watchdog_timer;

 /* Number of references to this device */
 atomic_t  refcnt ____cacheline_aligned_in_smp;

 /* delayed register/unregister */
 struct list_head todo_list;
 /* device index hash chain */
 struct hlist_node index_hlist;

 struct net_device *link_watch_next;

 /* register/unregister state machine */
 enum { NETREG_UNINITIALIZED=0,
        NETREG_REGISTERED, /* completed register_netdevice */
        NETREG_UNREGISTERING, /* called unregister_netdevice */
        NETREG_UNREGISTERED, /* completed unregister todo */
        NETREG_RELEASED,  /* called free_netdev */
        NETREG_DUMMY,  /* dummy device for NAPI poll */
 } reg_state;

 /* Called from unregister, can be used to call free_netdev */
 void (*destructor)(struct net_device *dev);

#ifdef CONFIG_NETPOLL
 struct netpoll_info *npinfo;
#endif

#ifdef CONFIG_NET_NS
 /* Network namespace this network device is inside */
 struct net  *nd_net;
#endif

 /* mid-layer private */
 void   *ml_priv;

 /* bridge stuff */
 struct net_bridge_port *br_port;
 /* macvlan */
 struct macvlan_port *macvlan_port;
 /* GARP */
 struct garp_port *garp_port;

 /* class/net/name entry */
 struct device  dev;
 /* space for optional statistics and wireless sysfs groups */
 const struct attribute_group *sysfs_groups[3];

 /* rtnetlink link ops */
 const struct rtnl_link_ops *rtnl_link_ops;

 /* VLAN feature mask */
 unsigned long vlan_features;

 /* for setting kernel sock attribute on TCP connection setup */
#define GSO_MAX_SIZE  65536
 unsigned int  gso_max_size;

#ifdef CONFIG_DCB
 /* Data Center Bridging netlink ops */
 struct dcbnl_rtnl_ops *dcbnl_ops;
#endif

#if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
 /* max exchange id for FCoE LRO by ddp */
 unsigned int  fcoe_ddp_xid;
#endif
};

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值