net_device & net_device_ops

struct net_device_ops 详解 【ndo_init】 注册网络设备时调用,驱动程序一般不使用,虚拟网络设备可能使用 【ndo_uninit】 网络设备卸载时调用,驱动程序一般使用 【ndo_open】 1、禁止所有中断、关闭载波(netif_carrier_off); 2、分配并初始化传输和接收描述符环内存(使用dma_alloc_coherent分配确保EMAC和CPU都可以访问); 3、分配 阅读详情

1. net_device在内核中代表一个网络设备。

网络设备驱动程序只需要填充net_device的具体成员,并注册net_device 即可实现硬件操作函数与内核的挂接。

net_device 是一个巨型结构体,包含了网络设备的属性描述和接口操作:

/*
 *	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.
 *
 *	FIXME: cleanup struct net_device such that network protocol info
 *	moves out.
 */

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
	 * of the interface.
	 */
	char			name[IFNAMSIZ];    //网络设备的具体名字

	struct pm_qos_request_list pm_qos_req;

	/* device name hash chain */
	struct hlist_node	name_hlist;
	/* snmp alias */
	char 			*ifalias;

	/*
	 *	I/O specific fields
	 *	FIXME: Merge these and struct ifmap into one
	 */
	unsigned long		mem_end;	/* shared mem end	*/ //设备说是用的共享内存的起始和结束地址
	unsigned long		mem_start;	/* shared mem start	*/
	unsigned long		base_addr;	/* device I/O address	*/ //网络设备I/O基地址
	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		*/ //分配给该设备的 DMA 通道

	unsigned long		state;

	struct list_head	dev_list;
	struct list_head	napi_list;
	struct list_head	unreg_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*/
#define NETIF_F_NTUPLE		(1 << 27) /* N-tuple filters supported */
#define NETIF_F_RXHASH		(1 << 28) /* Receive hashing offload */

	/* 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 | NETIF_F_UFO)


#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;
	atomic_long_t		rx_dropped; /* dropped packets by core network
					     * Do not use this in drivers.
					     */

#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 int            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 int		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;

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

	spinlock_t		addr_list_lock;
	struct netdev_hw_addr_list	uc;	/* Unicast mac addresses */
	struct netdev_hw_addr_list	mc;	/* Multicast mac addresses */
	int			uc_promisc;
	unsigned int		promiscuity;
	unsigned int		allmulti;


	/* Protocol specific pointers */

#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
	struct vlan_group __rcu	*vlgrp;		/* VLAN group */
#endif
#ifdef CONFIG_NET_DSA
	void			*dsa_ptr;	/* dsa specific data */
#endif
	void 			*atalk_ptr;	/* AppleTalk link 	*/
	struct in_device __rcu	*ip_ptr;	/* IPv4 specific data	*/
	void                    *dn_ptr;        /* DECnet specific data */
	struct inet6_dev __rcu	*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 lines mostly used on receive path (including eth_type_trans())
 */
	unsigned long		last_rx;	/* Time of last Rx
						 * This should not be set in
						 * drivers, unless really needed,
						 * because network stack (bonding)
						 * use it if/when necessary, to
						 * avoid dirtying this cache line.
						 */

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

	/* Interface address info used in eth_type_trans() */
	unsigned char		*dev_addr;	/* hw address, (before bcast  //设备的硬件地址,即MAC地址
						   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	*/

#ifdef CONFIG_RPS
	struct kset		*queues_kset;

	struct netdev_rx_queue	*_rx;

	/* Number of RX queues allocated at register_netdev() time */
	unsigned int		num_rx_queues;

	/* Number of RX queues currently active in device */
	unsigned int		real_num_rx_queues;
#endif

	rx_handler_func_t	*rx_handler;
	void			*rx_handler_data;

	struct netdev_queue __rcu *ingress_queue;

/*
 * Cache lines mostly used on transmit path
 */
	struct netdev_queue	*_tx ____cacheline_aligned_in_smp;

	/* 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;

	/* 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;

	/* 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 */
	int __percpu		*pcpu_refcnt;

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

	struct list_head	link_watch_list;

	/* 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:16;

	enum {
		RTNL_LINK_INITIALIZED,
		RTNL_LINK_INITIALIZING,
	} rtnl_link_state:16;

	/* 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 */
	union {
		void				*ml_priv;
		struct pcpu_lstats __percpu	*lstats; /* loopback stats */
		struct pcpu_tstats __percpu	*tstats; /* tunnel stats */
		struct pcpu_dstats __percpu	*dstats; /* dummy stats */
	};
	/* GARP */
	struct garp_port __rcu	*garp_port;

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

	/* 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 */
	const 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
	/* n-tuple filter list attached to this device */
	struct ethtool_rx_ntuple_list ethtool_ntuple_list;

	/* phy device may attach itself for hardware timestamping */
	struct phy_device *phydev;
};

--- flags : 指网络接口标志,以IFF_ 开头 (interface flags), 部分标志由内核管理,其他的标志在接口初始化时被设置,以说明接口的能力和特性。

各类接口标志定义如下:

/* Standard interface flags (netdevice->flags). */
#define	IFF_UP		0x1		/* interface is up		*/
#define	IFF_BROADCAST	0x2		/* broadcast address valid	*/  //允许广播
#define	IFF_DEBUG	0x4		/* turn on debugging		*/
#define	IFF_LOOPBACK	0x8		/* is a loopback net		*/  //回环
#define	IFF_POINTOPOINT	0x10		/* interface is has p-p link	*/  //接口连接点到点链路
#define	IFF_NOTRAILERS	0x20		/* avoid use of trailers	*/
#define	IFF_RUNNING	0x40		/* interface RFC2863 OPER_UP	*/
#define	IFF_NOARP	0x80		/* no ARP protocol		*/   //不能执行ARP
#define	IFF_PROMISC	0x100		/* receive all packets		*/
#define	IFF_ALLMULTI	0x200		/* receive all multicast packets*/

#define IFF_MASTER	0x400		/* master of a load balancer 	*/
#define IFF_SLAVE	0x800		/* slave of a load balancer	*/

#define IFF_MULTICAST	0x1000		/* Supports multicast		*/   //允许组播

#define IFF_PORTSEL	0x2000          /* can set media type		*/
#define IFF_AUTOMEDIA	0x4000		/* auto media select active	*/
#define IFF_DYNAMIC	0x8000		/* dialup device with changing addresses*/

#define IFF_LOWER_UP	0x10000		/* driver signals L1 up		*/
#define IFF_DORMANT	0x20000		/* driver signals dormant	*/

#define IFF_ECHO	0x40000		/* echo sent packets		*/

#define IFF_VOLATILE	(IFF_LOOPBACK|IFF_POINTOPOINT|IFF_BROADCAST|IFF_ECHO|\
		IFF_MASTER|IFF_SLAVE|IFF_RUNNING|IFF_LOWER_UP|IFF_DORMANT)

/* Private (from user) interface flags (netdevice->priv_flags). */
#define IFF_802_1Q_VLAN 0x1             /* 802.1Q VLAN device.          */
#define IFF_EBRIDGE	0x2		/* Ethernet bridging device.	*/
#define IFF_SLAVE_INACTIVE	0x4	/* bonding slave not the curr. active */
#define IFF_MASTER_8023AD	0x8	/* bonding master, 802.3ad. 	*/
#define IFF_MASTER_ALB	0x10		/* bonding master, balance-alb.	*/
#define IFF_BONDING	0x20		/* bonding master or slave	*/
#define IFF_SLAVE_NEEDARP 0x40		/* need ARPs for validation	*/
#define IFF_ISATAP	0x80		/* ISATAP interface (RFC4214)	*/
#define IFF_MASTER_ARPMON 0x100		/* bonding master, ARP mon in use */
#define IFF_WAN_HDLC	0x200		/* WAN HDLC device		*/
#define IFF_XMIT_DST_RELEASE 0x400	/* dev_hard_start_xmit() is allowed to
					 * release skb->dst
					 */
#define IFF_DONT_BRIDGE 0x800		/* disallow bridging this ether dev */
#define IFF_IN_NETPOLL	0x1000		/* whether we are processing netpoll */
#define IFF_DISABLE_NETPOLL	0x2000	/* disable netpoll at run-time */
#define IFF_MACVLAN_PORT	0x4000	/* device used as macvlan port */
#define IFF_BRIDGE_PORT	0x8000		/* device used as bridge port */
#define IFF_OVS_DATAPATH	0x10000	/* device used as Open vSwitch
					 * datapath port */

2. net_device_ops 是操作 net_device的各种操作:

之前linux版本的net_device操作函数,直接就在net_device结构里定义,但是新的linux版本,把这些函数归纳到了 net_device_ops 中了。

/*
 * This structure defines the management hooks for network devices.
 * The following hooks can be defined; unless noted otherwise, they are
 * optional and can be filled with a null pointer.
 *
 * int (*ndo_init)(struct net_device *dev);
 *     This function is called once when network device is registered.
 *     The network device can use this to any late stage initializaton
 *     or semantic validattion. It can fail with an error code which will
 *     be propogated back to register_netdev
 *
 * void (*ndo_uninit)(struct net_device *dev);
 *     This function is called when device is unregistered or when registration
 *     fails. It is not called if init fails.
 *
 * int (*ndo_open)(struct net_device *dev);
 *     This function is called when network device transistions to the up
 *     state.
 *
 * int (*ndo_stop)(struct net_device *dev);
 *     This function is called when network device transistions to the down
 *     state.
 *
 * netdev_tx_t (*ndo_start_xmit)(struct sk_buff *skb,
 *                               struct net_device *dev);
 *	Called when a packet needs to be transmitted.
 *	Must return NETDEV_TX_OK , NETDEV_TX_BUSY.
 *        (can also return NETDEV_TX_LOCKED iff NETIF_F_LLTX)
 *	Required can not be NULL.
 *
 * u16 (*ndo_select_queue)(struct net_device *dev, struct sk_buff *skb);
 *	Called to decide which queue to when device supports multiple
 *	transmit queues.
 *
 * void (*ndo_change_rx_flags)(struct net_device *dev, int flags);
 *	This function is called to allow device receiver to make
 *	changes to configuration when multicast or promiscious is enabled.
 *
 * void (*ndo_set_rx_mode)(struct net_device *dev);
 *	This function is called device changes address list filtering.
 *
 * void (*ndo_set_multicast_list)(struct net_device *dev);
 *	This function is called when the multicast address list changes.
 *
 * int (*ndo_set_mac_address)(struct net_device *dev, void *addr);
 *	This function  is called when the Media Access Control address
 *	needs to be changed. If this interface is not defined, the
 *	mac address can not be changed.
 *
 * int (*ndo_validate_addr)(struct net_device *dev);
 *	Test if Media Access Control address is valid for the device.
 *
 * int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
 *	Called when a user request an ioctl which can't be handled by
 *	the generic interface code. If not defined ioctl's return
 *	not supported error code.
 *
 * int (*ndo_set_config)(struct net_device *dev, struct ifmap *map);
 *	Used to set network devices bus interface parameters. This interface
 *	is retained for legacy reason, new devices should use the bus
 *	interface (PCI) for low level management.
 *
 * int (*ndo_change_mtu)(struct net_device *dev, int new_mtu);
 *	Called when a user wants to change the Maximum Transfer Unit
 *	of a device. If not defined, any request to change MTU will
 *	will return an error.
 *
 * void (*ndo_tx_timeout)(struct net_device *dev);
 *	Callback uses when the transmitter has not made any progress
 *	for dev->watchdog ticks.
 *
 * struct rtnl_link_stats64* (*ndo_get_stats64)(struct net_device *dev,
 *                      struct rtnl_link_stats64 *storage);
 * struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);
 *	Called when a user wants to get the network device usage
 *	statistics. Drivers must do one of the following:
 *	1. Define @ndo_get_stats64 to fill in a zero-initialised
 *	   rtnl_link_stats64 structure passed by the caller.
 *	2. Define @ndo_get_stats to update a net_device_stats structure
 *	   (which should normally be dev->stats) and return a pointer to
 *	   it. The structure may be changed asynchronously only if each
 *	   field is written atomically.
 *	3. Update dev->stats asynchronously and atomically, and define
 *	   neither operation.
 *
 * void (*ndo_vlan_rx_register)(struct net_device *dev, struct vlan_group *grp);
 *	If device support VLAN receive accleration
 *	(ie. dev->features & NETIF_F_HW_VLAN_RX), then this function is called
 *	when vlan groups for the device changes.  Note: grp is NULL
 *	if no vlan's groups are being used.
 *
 * void (*ndo_vlan_rx_add_vid)(struct net_device *dev, unsigned short vid);
 *	If device support VLAN filtering (dev->features & NETIF_F_HW_VLAN_FILTER)
 *	this function is called when a VLAN id is registered.
 *
 * void (*ndo_vlan_rx_kill_vid)(struct net_device *dev, unsigned short vid);
 *	If device support VLAN filtering (dev->features & NETIF_F_HW_VLAN_FILTER)
 *	this function is called when a VLAN id is unregistered.
 *
 * void (*ndo_poll_controller)(struct net_device *dev);
 *
 *	SR-IOV management functions.
 * int (*ndo_set_vf_mac)(struct net_device *dev, int vf, u8* mac);
 * int (*ndo_set_vf_vlan)(struct net_device *dev, int vf, u16 vlan, u8 qos);
 * int (*ndo_set_vf_tx_rate)(struct net_device *dev, int vf, int rate);
 * int (*ndo_get_vf_config)(struct net_device *dev,
 *			    int vf, struct ifla_vf_info *ivf);
 * int (*ndo_set_vf_port)(struct net_device *dev, int vf,
 *			  struct nlattr *port[]);
 * int (*ndo_get_vf_port)(struct net_device *dev, int vf, struct sk_buff *skb);
 */
#define HAVE_NET_DEVICE_OPS
struct net_device_ops {
	int			(*ndo_init)(struct net_device *dev);
	void			(*ndo_uninit)(struct net_device *dev);
	int			(*ndo_open)(struct net_device *dev);  //打开网络接口设备,获得设备需要的I/O地址,IRQ,DMA通道等。
	int			(*ndo_stop)(struct net_device *dev);  //关闭网络设备
	netdev_tx_t		(*ndo_start_xmit) (struct sk_buff *skb,   //驱动数据包发送,参数是sk_buff,使得驱动程序获取从上层传递下来的数据包
						   struct net_device *dev);
	u16			(*ndo_select_queue)(struct net_device *dev,
						    struct sk_buff *skb);
	void			(*ndo_change_rx_flags)(struct net_device *dev,
						       int flags);
	void			(*ndo_set_rx_mode)(struct net_device *dev);
	void			(*ndo_set_multicast_list)(struct net_device *dev);
	int			(*ndo_set_mac_address)(struct net_device *dev,
						       void *addr);
	int			(*ndo_validate_addr)(struct net_device *dev);
	int			(*ndo_do_ioctl)(struct net_device *dev,
					        struct ifreq *ifr, int cmd);
	int			(*ndo_set_config)(struct net_device *dev,
					          struct ifmap *map);
	int			(*ndo_change_mtu)(struct net_device *dev,
						  int new_mtu);
	int			(*ndo_neigh_setup)(struct net_device *dev,
						   struct neigh_parms *);
	void			(*ndo_tx_timeout) (struct net_device *dev);    //当数据包发送超时时,该函数会被调用,该函数需重新启动数据包发送过程,或重新启动硬件等回复网络到正常状态。

	struct rtnl_link_stats64* (*ndo_get_stats64)(struct net_device *dev,
						     struct rtnl_link_stats64 *storage);
	struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);

	void			(*ndo_vlan_rx_register)(struct net_device *dev,
						        struct vlan_group *grp);
	void			(*ndo_vlan_rx_add_vid)(struct net_device *dev,
						       unsigned short vid);
	void			(*ndo_vlan_rx_kill_vid)(struct net_device *dev,
						        unsigned short vid);
#ifdef CONFIG_NET_POLL_CONTROLLER
	void                    (*ndo_poll_controller)(struct net_device *dev);
	int			(*ndo_netpoll_setup)(struct net_device *dev,
						     struct netpoll_info *info);
	void			(*ndo_netpoll_cleanup)(struct net_device *dev);
#endif
	int			(*ndo_set_vf_mac)(struct net_device *dev,
						  int queue, u8 *mac);
	int			(*ndo_set_vf_vlan)(struct net_device *dev,
						   int queue, u16 vlan, u8 qos);
	int			(*ndo_set_vf_tx_rate)(struct net_device *dev,
						      int vf, int rate);
	int			(*ndo_get_vf_config)(struct net_device *dev,
						     int vf,
						     struct ifla_vf_info *ivf);
	int			(*ndo_set_vf_port)(struct net_device *dev,
						   int vf,
						   struct nlattr *port[]);
	int			(*ndo_get_vf_port)(struct net_device *dev,
						   int vf, struct sk_buff *skb);
#if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
	int			(*ndo_fcoe_enable)(struct net_device *dev);
	int			(*ndo_fcoe_disable)(struct net_device *dev);
	int			(*ndo_fcoe_ddp_setup)(struct net_device *dev,
						      u16 xid,
						      struct scatterlist *sgl,
						      unsigned int sgc);
	int			(*ndo_fcoe_ddp_done)(struct net_device *dev,
						     u16 xid);
#define NETDEV_FCOE_WWNN 0
#define NETDEV_FCOE_WWPN 1
	int			(*ndo_fcoe_get_wwn)(struct net_device *dev,
						    u64 *wwn, int type);
#endif
};



Linux网络驱动核心:net_device结构体与操作集详解 net_device是Linux内核网络子系统的核心抽象,作为硬件与协议栈之间的统一接口,承载设备状态、配置、统计及操作能力。其设计遵循分层解耦原理,通过netdev_ops定义标准函数钩子(如ndo_open、ndo_start_xmit),实现驱动与内核的契约式交互;ethtool_ops则支撑运行时调试与硬件特性控制。该结构体不仅服务于以太网(ARPHRD_ETHER),也兼容Wi-Fi(ARPHRD_IEEE80211)、虚拟设备(tun/veth/lo)等多元网络类型,是嵌入式开发、网卡驱动移植及 阅读详情

相关推荐

单片机中的网络设备操作:net_device_ops结构体

在Linux内核中,网络设备驱动程序需要实现net_device_ops结构体中定义的操作函数来完成设备初始化、数据收发等功能。上述结构体中包含了8个函数指针,分别代表了设备初始化、设备关闭、设备打开、设备停止、数据发送、队列选择等操作。上述代码中,我们定义了一个名为my_netdev_ops的net_device_ops结构体,并为其中的每个函数指针分别指定了对应的实现函数。在开发单片机网络设备驱动程序时,我们可以通过实现这些函数来完成设备初始化、数据收发等操作,从而实现网络通信功能。

UoEmacs_Lisp的博客 569

Linux 网络驱动-内核网络驱动框架(二)

Linux 网络驱动-内核网络驱动框架(二)

wanglei_11的博客 6246

Linux内核自定义协议族开发指南:理解net_device_ops、proto_ops与net_proto_family

注册协议族,提供套接字创建入口。proto_ops实现协议对套接字操作的支持,处理用户空间数据。管理物理/虚拟设备的数据收发。通过分层次实现并确保各层间的正确交互,可以构建高效可靠的自定义网络协议栈。在 Linux 内核网络协议栈中,数据发送是一个涉及多个层次的复杂过程。用户空间的send调用最终需要通过内核中的多个关键函数协作,才能将数据传递到物理网络设备。其中,sendmsg和是两个核心函数,分别负责套接字层和网络设备层的数据处理。本文将深入解析这两个函数的作用、协作流程以及它们在协议栈中的重要性。

数字人生 937

linux内核网络驱动框架(linux驱动开发篇)

网络驱动的核心: 1、就是初始化 net_device 结构体中的各个成员变量, 2、然后将初始化完成以后的 net_device 注册到 Linux 内核中 1、网络设备(用net_device结构体) /* @ net_device 结构体 */ struct net_device { char name[IFNAMSIZ]; /*网络设备名字*/ struct hlist_node name_hlist; ch.

栋哥修炼日记 5927

RK3399平台开发系列讲解(以太网)5.8、网卡操作描述结构:struct net_device_ops

今天介绍以太网网卡操作描述结构:struct net_device_ops

内核笔记 4001

ITIL 4 Foundation 自学通关笔记1

ITIL 4 Foundation 自学通关笔记

m0_58817196的博客 953

Linux网络驱动学习

正点原子LInux驱动学习之网络驱动

qq_42049394的博客 2680

通达信股本变迁文件(gbbq)解密方法

近年来A股市场跌宕起伏真特么精彩,本人愚钝,被整爆仓,损失惨重 痛定思痛,认为交易这种对纪律要求极高,且必须完全排除情绪影响的勾当,还是应该交给程序来干 数据哪里来呢?当然是拿来主义。。。问券商的交易软件要呗 到处查资料,得知通达信的权息文件数据齐全,不仅含有除权除息数据,还含有限售解禁、增发、可转债上市等股本变化数据 ——这对于某些对成交量变化敏感的交易模型是非常重要的

不甘碌碌老,结庐铸宝刀 2万+

net_device&nbsp;&amp;&nbsp;net_device_ops&nbsp;.

1. net_device在内核中代表一个网络设备。 网络设备驱动程序只需要填充net_device的具体成员,并注册net_device 即可实现硬件操作函数与内核的挂接。 net_device 是一个巨型结构体,包含了网络设备的属性描述和接口操作: [cpp] view plaincopyprint?       struct net_device {               

shangyaowei的专栏 3704

STM32F103C8T6+HX711实战:从零打造智能体重秤(附OLED显示与语音播报)

本文详细介绍了基于STM32F103C8T6微控制器和HX711称重模块,从零构建智能体重秤的完整实战教程。内容涵盖硬件选型、系统架构、驱动编写、数据滤波校准算法,并实现了OLED显示与语音播报功能。该项目是嵌入式学习的综合性实践,尤其适合作为毕业设计课题,帮助开发者掌握传感器应用、数据处理及人机交互等核心技能。

weixin_28285943的博客 517

Landsat9 地表温度反演方法

landsat9地表温度反演及缺失值处理方法

xiangliu12345的博客 3917

net_device的打开与关闭

前面的笔记分析了net_device的分配和注册流程,在可以进行数据收发之前,还需要打开net_device。对应的net_device不再工作时需要关闭net_device,这篇笔记来分析下这两个流程是如何实现的。

九天小哥的专栏 2503

F280049C_HRPWM_死区时间高精度控制与MEP技术实战解析

本文深入解析了F280049C的HRPWM模块及其基于MEP技术的死区时间高精度控制。通过实测数据与TI官方代码对比,详细阐述了DBREDHR和DBFEDHR寄存器的配置技巧、精度优化方法及常见问题调试,为电源管理和电机控制应用提供了实用的实战指南。

yolo5detector的博客 217

net_device的注册与注销

net_device对象只有通过register_netdev()注册到系统后才能被外部感知到。类似的,在释放之前需要通过unregister_netdev()将net_device对象先从系统中注销。这篇笔记分析了这两个过程的实现。

九天小哥的专栏 5377

浪涌抗扰度部分解读

8.3 测试仪器: 8.3.1浪涌发生器(对于连接到对称通信线的接口采用10/700us的组合波发生器;对于其他情况采用1.2/50us(8/20us)组合波发生器,输出极性正负可调、移相范围0-360°可调,最大重复率至少每分钟1次,开路输出电压0.5-4kV,电源线的话有效效输出阻抗为2Ω,通信线的话有效输出阻抗为40Ω) 8.3.2 耦合/去耦网络 8.3.3 参考接地平板 8...

喵叫兽的博客 2503

滤波算法实现大全——基于匀速直线运动模型的kalman滤波算法

滤波算法实现大全——基于匀速直线运动模型的kalman滤波算法

jane0819的博客 522

DM9000网卡驱动源码分析系列05 - net_device_ops(ioctl ...)

static const struct net_device_ops dm9000_netdev_ops = { .ndo_open = dm9000_open, .ndo_stop = dm9000_stop, .ndo_start_xmit = dm9000_start_xmit, .ndo_tx_timeout = dm9000_timeout, .ndo_set_rx_m

shuimuyq的专栏 798

QGis软件 —— 2、QGis加载在线地图两种方式(谷歌地图、天地图)

QGis软件 —— 2、QGis加载在线地图两种方式(谷歌地图)

1万+

net_device版本转换

由于内核版本的变更,net_device的结构体变化很大,struct net_device 的结构在新版本内核中发生了变化,删去了quota成员;将(open,stop,set_config,hard_start_xmit等)封装进了struct net_device_ops;将(poll,weight等)封装进了 struct napi_struct;将(hard_header,hard_he

cocodada的专栏 879

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

struct net_device

struct net_device {  char   name[IFNAMSIZ];  struct hlist_node name_hlist;  char    *ifalias;  /*   * I/O specific fields   * FIXME: Merge these and struct ifmap into one   */  unsigned long

本博客仅供免费阅读,不提供咨询服务。请勿私信。 983

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

分析struct net_device转

分析struct net_device相关文件:linux/include/linux/netdevice.hlinux/drivers/net/Space.clinux/include/net/iw_handler.hchar name[IFNAMSIZ];在space.c文件中初始化。unsigned long mem_end;unsigned long mem_start;设备相关的共享内存

weixiuc的专栏 1509

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

网络设备之net_device结构与操作

net_device结构是一个很大的结构,其中包含了硬件信息,接口信息,其他辅助信息,以及设备操作函数等; 目前仍在读代码中,后续字段注释会逐渐补充; 1 /** 2 * struct net_device - The DEVICE structure. 3 * Actually, this whole structure is a big mist...

weixin_30929295的博客 1102

net_device 结构分析

在linux内核中采用一个net_struct的实例来表示一个网络设备,这其中包括了虚拟网络设备和实际网络设备。该数据结构比较复杂,主要任务分为两部分:第一对上层协议屏蔽底层设备的区别,提供统一的操作接口。第二对下层设备,提供实际驱动方法。 strcut net_device{ //通用区域     char name[]// 名字     struct hlist_node name_h

chn89的专栏 1730

Linux网络驱动核心:net_device_ops与sk_buff深度解析

在Linux内核网络栈中,网络设备驱动需通过标准化接口与协议栈协同工作。net_device_ops作为设备操作的函数指针契约,定义了open、stop、start_xmit等关键生命周期与数据通路行为;sk_buff则作为贯穿协议栈的数据载体,通过指针偏移机制实现零拷贝协议处理。二者共同构成驱动开发的技术基座,支撑DMA映射、中断管理、NAPI轮询等工程实践。理解其原理对嵌入式Linux网络驱动开发、性能调优及SoC平台适配具有基础性价值,尤其适用于STM32MP1、ARM64等典型嵌入式场景。

weixin_33239721的博客 78

Linux Mii management/mdio子系统分析之五 PHY状态机分析及其与net_device的关联

前面几章基本上完成了mdio模块驱动模型的分析,本篇文章主要讲述phy device的状态机以及phy device与net_device的关联。Phy device主要是对phy的抽象,而net_device主要是对mac的抽象,而mdio总线以及mii_bus主要用于对phy设备的控制(包括设置phy设备的工作模式、速率模式、是否为自适应、phy的link状态等),而net_...

lickylin的专栏 3699

【linux网络】net_device及注册与销毁

1、net_device结构体 net_device (include/linux/netdevice.h ) /** * struct net_device - 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 eve

风雨无阻 2472
上一篇: PPI_DPI 对比
下一篇: BSS段_数据段_代码段_堆_栈
lamdoc
博客等级 码龄18年 155粉丝 655原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值