Skip to content

Commit d314774

Browse files
Stephen Hemmingerdavem330
authored andcommitted
netdev: network device operations infrastructure
This patch changes the network device internal API to move adminstrative operations out of the network device structure and into a separate structure. This patch involves some hackery to maintain compatablity between the new and old model, so all 300+ drivers don't have to be changed at once. For drivers that aren't converted yet, the netdevice_ops virt function list still resides in the net_device structure. For old protocols, the new net_device_ops are copied out to the old net_device pointers. After the transistion is completed the nag message can be changed to an WARN_ON, and the compatiablity code can be made configurable. Some function pointers aren't moved: * destructor can't be in net_device_ops because it may need to be referenced after the module is unloaded. * neighbor setup is manipulated in a couple of places that need special consideration * hard_start_xmit is in the fast path for transmit. Signed-off-by: Stephen Hemminger <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6b41e7d commit d314774

File tree

6 files changed

+259
-105
lines changed

6 files changed

+259
-105
lines changed

include/linux/netdevice.h

Lines changed: 168 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,131 @@ struct netdev_queue {
451451
struct Qdisc *qdisc_sleeping;
452452
} ____cacheline_aligned_in_smp;
453453

454+
455+
/*
456+
* This structure defines the management hooks for network devices.
457+
* The following hooks can bed defined and are optonal (can be null)
458+
* unless otherwise noted.
459+
*
460+
* int (*ndo_init)(struct net_device *dev);
461+
* This function is called once when network device is registered.
462+
* The network device can use this to any late stage initializaton
463+
* or semantic validattion. It can fail with an error code which will
464+
* be propogated back to register_netdev
465+
*
466+
* void (*ndo_uninit)(struct net_device *dev);
467+
* This function is called when device is unregistered or when registration
468+
* fails. It is not called if init fails.
469+
*
470+
* int (*ndo_open)(struct net_device *dev);
471+
* This function is called when network device transistions to the up
472+
* state.
473+
*
474+
* int (*ndo_stop)(struct net_device *dev);
475+
* This function is called when network device transistions to the down
476+
* state.
477+
*
478+
* void (*ndo_change_rx_flags)(struct net_device *dev, int flags);
479+
* This function is called to allow device receiver to make
480+
* changes to configuration when multicast or promiscious is enabled.
481+
*
482+
* void (*ndo_set_rx_mode)(struct net_device *dev);
483+
* This function is called device changes address list filtering.
484+
*
485+
* void (*ndo_set_multicast_list)(struct net_device *dev);
486+
* This function is called when the multicast address list changes.
487+
*
488+
* int (*ndo_set_mac_address)(struct net_device *dev, void *addr);
489+
* This function is called when the Media Access Control address
490+
* needs to be changed. If not this interface is not defined, the
491+
* mac address can not be changed.
492+
*
493+
* int (*ndo_validate_addr)(struct net_device *dev);
494+
* Test if Media Access Control address is valid for the device.
495+
*
496+
* int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
497+
* Called when a user request an ioctl which can't be handled by
498+
* the generic interface code. If not defined ioctl's return
499+
* not supported error code.
500+
*
501+
* int (*ndo_set_config)(struct net_device *dev, struct ifmap *map);
502+
* Used to set network devices bus interface parameters. This interface
503+
* is retained for legacy reason, new devices should use the bus
504+
* interface (PCI) for low level management.
505+
*
506+
* int (*ndo_change_mtu)(struct net_device *dev, int new_mtu);
507+
* Called when a user wants to change the Maximum Transfer Unit
508+
* of a device. If not defined, any request to change MTU will
509+
* will return an error.
510+
*
511+
* void (*ndo_tx_timeout) (struct net_device *dev);
512+
* Callback uses when the transmitter has not made any progress
513+
* for dev->watchdog ticks.
514+
*
515+
* struct net_device_stats* (*get_stats)(struct net_device *dev);
516+
* Called when a user wants to get the network device usage
517+
* statistics. If not defined, the counters in dev->stats will
518+
* be used.
519+
*
520+
* void (*ndo_vlan_rx_register)(struct net_device *dev, struct vlan_group *grp);
521+
* If device support VLAN receive accleration
522+
* (ie. dev->features & NETIF_F_HW_VLAN_RX), then this function is called
523+
* when vlan groups for the device changes. Note: grp is NULL
524+
* if no vlan's groups are being used.
525+
*
526+
* void (*ndo_vlan_rx_add_vid)(struct net_device *dev, unsigned short vid);
527+
* If device support VLAN filtering (dev->features & NETIF_F_HW_VLAN_FILTER)
528+
* this function is called when a VLAN id is registered.
529+
*
530+
* void (*ndo_vlan_rx_kill_vid)(struct net_device *dev, unsigned short vid);
531+
* If device support VLAN filtering (dev->features & NETIF_F_HW_VLAN_FILTER)
532+
* this function is called when a VLAN id is unregistered.
533+
*
534+
* void (*ndo_poll_controller)(struct net_device *dev);
535+
*/
536+
struct net_device_ops {
537+
int (*ndo_init)(struct net_device *dev);
538+
void (*ndo_uninit)(struct net_device *dev);
539+
int (*ndo_open)(struct net_device *dev);
540+
int (*ndo_stop)(struct net_device *dev);
541+
#define HAVE_CHANGE_RX_FLAGS
542+
void (*ndo_change_rx_flags)(struct net_device *dev,
543+
int flags);
544+
#define HAVE_SET_RX_MODE
545+
void (*ndo_set_rx_mode)(struct net_device *dev);
546+
#define HAVE_MULTICAST
547+
void (*ndo_set_multicast_list)(struct net_device *dev);
548+
#define HAVE_SET_MAC_ADDR
549+
int (*ndo_set_mac_address)(struct net_device *dev,
550+
void *addr);
551+
#define HAVE_VALIDATE_ADDR
552+
int (*ndo_validate_addr)(struct net_device *dev);
553+
#define HAVE_PRIVATE_IOCTL
554+
int (*ndo_do_ioctl)(struct net_device *dev,
555+
struct ifreq *ifr, int cmd);
556+
#define HAVE_SET_CONFIG
557+
int (*ndo_set_config)(struct net_device *dev,
558+
struct ifmap *map);
559+
#define HAVE_CHANGE_MTU
560+
int (*ndo_change_mtu)(struct net_device *dev, int new_mtu);
561+
562+
#define HAVE_TX_TIMEOUT
563+
void (*ndo_tx_timeout) (struct net_device *dev);
564+
565+
struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);
566+
567+
void (*ndo_vlan_rx_register)(struct net_device *dev,
568+
struct vlan_group *grp);
569+
void (*ndo_vlan_rx_add_vid)(struct net_device *dev,
570+
unsigned short vid);
571+
void (*ndo_vlan_rx_kill_vid)(struct net_device *dev,
572+
unsigned short vid);
573+
#ifdef CONFIG_NET_POLL_CONTROLLER
574+
#define HAVE_NETDEV_POLL
575+
void (*ndo_poll_controller)(struct net_device *dev);
576+
#endif
577+
};
578+
454579
/*
455580
* The DEVICE structure.
456581
* Actually, this whole structure is a big mistake. It mixes I/O
@@ -498,11 +623,6 @@ struct net_device
498623
#ifdef CONFIG_NETPOLL
499624
struct list_head napi_list;
500625
#endif
501-
502-
/* The device initialization function. Called only once. */
503-
int (*init)(struct net_device *dev);
504-
505-
/* ------- Fields preinitialized in Space.c finish here ------- */
506626

507627
/* Net device features */
508628
unsigned long features;
@@ -546,15 +666,13 @@ struct net_device
546666
* for all in netdev_increment_features.
547667
*/
548668
#define NETIF_F_ONE_FOR_ALL (NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ROBUST | \
549-
NETIF_F_SG | NETIF_F_HIGHDMA | \
669+
NETIF_F_SG | NETIF_F_HIGHDMA | \
550670
NETIF_F_FRAGLIST)
551671

552672
/* Interface index. Unique device identifier */
553673
int ifindex;
554674
int iflink;
555675

556-
557-
struct net_device_stats* (*get_stats)(struct net_device *dev);
558676
struct net_device_stats stats;
559677

560678
#ifdef CONFIG_WIRELESS_EXT
@@ -564,18 +682,13 @@ struct net_device
564682
/* Instance data managed by the core of Wireless Extensions. */
565683
struct iw_public_data * wireless_data;
566684
#endif
685+
/* Management operations */
686+
const struct net_device_ops *netdev_ops;
567687
const struct ethtool_ops *ethtool_ops;
568688

569689
/* Hardware header description */
570690
const struct header_ops *header_ops;
571691

572-
/*
573-
* This marks the end of the "visible" part of the structure. All
574-
* fields hereafter are internal to the system, and may change at
575-
* will (read: may be cleaned up at will).
576-
*/
577-
578-
579692
unsigned int flags; /* interface flags (a la BSD) */
580693
unsigned short gflags;
581694
unsigned short priv_flags; /* Like 'flags' but invisible to userspace. */
@@ -634,7 +747,7 @@ struct net_device
634747
unsigned long last_rx; /* Time of last Rx */
635748
/* Interface address info used in eth_type_trans() */
636749
unsigned char dev_addr[MAX_ADDR_LEN]; /* hw address, (before bcast
637-
because most packets are unicast) */
750+
because most packets are unicast) */
638751

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

@@ -648,6 +761,10 @@ struct net_device
648761
/* Number of TX queues currently active in device */
649762
unsigned int real_num_tx_queues;
650763

764+
/* Map buffer to appropriate transmit queue */
765+
u16 (*select_queue)(struct net_device *dev,
766+
struct sk_buff *skb);
767+
651768
unsigned long tx_queue_len; /* Max frames per queue allowed */
652769
spinlock_t tx_global_lock;
653770
/*
@@ -662,9 +779,6 @@ struct net_device
662779
int watchdog_timeo; /* used by dev_watchdog() */
663780
struct timer_list watchdog_timer;
664781

665-
/*
666-
* refcnt is a very hot point, so align it on SMP
667-
*/
668782
/* Number of references to this device */
669783
atomic_t refcnt ____cacheline_aligned_in_smp;
670784

@@ -683,56 +797,14 @@ struct net_device
683797
NETREG_RELEASED, /* called free_netdev */
684798
} reg_state;
685799

686-
/* Called after device is detached from network. */
687-
void (*uninit)(struct net_device *dev);
688-
/* Called after last user reference disappears. */
689-
void (*destructor)(struct net_device *dev);
800+
/* Called from unregister, can be used to call free_netdev */
801+
void (*destructor)(struct net_device *dev);
690802

691-
/* Pointers to interface service routines. */
692-
int (*open)(struct net_device *dev);
693-
int (*stop)(struct net_device *dev);
694-
#define HAVE_NETDEV_POLL
695-
#define HAVE_CHANGE_RX_FLAGS
696-
void (*change_rx_flags)(struct net_device *dev,
697-
int flags);
698-
#define HAVE_SET_RX_MODE
699-
void (*set_rx_mode)(struct net_device *dev);
700-
#define HAVE_MULTICAST
701-
void (*set_multicast_list)(struct net_device *dev);
702-
#define HAVE_SET_MAC_ADDR
703-
int (*set_mac_address)(struct net_device *dev,
704-
void *addr);
705-
#define HAVE_VALIDATE_ADDR
706-
int (*validate_addr)(struct net_device *dev);
707-
#define HAVE_PRIVATE_IOCTL
708-
int (*do_ioctl)(struct net_device *dev,
709-
struct ifreq *ifr, int cmd);
710-
#define HAVE_SET_CONFIG
711-
int (*set_config)(struct net_device *dev,
712-
struct ifmap *map);
713-
#define HAVE_CHANGE_MTU
714-
int (*change_mtu)(struct net_device *dev, int new_mtu);
803+
int (*neigh_setup)(struct net_device *dev, struct neigh_parms *);
715804

716-
#define HAVE_TX_TIMEOUT
717-
void (*tx_timeout) (struct net_device *dev);
718-
719-
void (*vlan_rx_register)(struct net_device *dev,
720-
struct vlan_group *grp);
721-
void (*vlan_rx_add_vid)(struct net_device *dev,
722-
unsigned short vid);
723-
void (*vlan_rx_kill_vid)(struct net_device *dev,
724-
unsigned short vid);
725-
726-
int (*neigh_setup)(struct net_device *dev, struct neigh_parms *);
727805
#ifdef CONFIG_NETPOLL
728806
struct netpoll_info *npinfo;
729807
#endif
730-
#ifdef CONFIG_NET_POLL_CONTROLLER
731-
void (*poll_controller)(struct net_device *dev);
732-
#endif
733-
734-
u16 (*select_queue)(struct net_device *dev,
735-
struct sk_buff *skb);
736808

737809
#ifdef CONFIG_NET_NS
738810
/* Network namespace this network device is inside */
@@ -763,6 +835,38 @@ struct net_device
763835
/* for setting kernel sock attribute on TCP connection setup */
764836
#define GSO_MAX_SIZE 65536
765837
unsigned int gso_max_size;
838+
839+
#ifdef CONFIG_COMPAT_NET_DEV_OPS
840+
struct {
841+
int (*init)(struct net_device *dev);
842+
void (*uninit)(struct net_device *dev);
843+
int (*open)(struct net_device *dev);
844+
int (*stop)(struct net_device *dev);
845+
void (*change_rx_flags)(struct net_device *dev,
846+
int flags);
847+
void (*set_rx_mode)(struct net_device *dev);
848+
void (*set_multicast_list)(struct net_device *dev);
849+
int (*set_mac_address)(struct net_device *dev,
850+
void *addr);
851+
int (*validate_addr)(struct net_device *dev);
852+
int (*do_ioctl)(struct net_device *dev,
853+
struct ifreq *ifr, int cmd);
854+
int (*set_config)(struct net_device *dev,
855+
struct ifmap *map);
856+
int (*change_mtu)(struct net_device *dev, int new_mtu);
857+
void (*tx_timeout) (struct net_device *dev);
858+
struct net_device_stats* (*get_stats)(struct net_device *dev);
859+
void (*vlan_rx_register)(struct net_device *dev,
860+
struct vlan_group *grp);
861+
void (*vlan_rx_add_vid)(struct net_device *dev,
862+
unsigned short vid);
863+
void (*vlan_rx_kill_vid)(struct net_device *dev,
864+
unsigned short vid);
865+
#ifdef CONFIG_NET_POLL_CONTROLLER
866+
void (*poll_controller)(struct net_device *dev);
867+
#endif
868+
#endif
869+
};
766870
};
767871
#define to_net_dev(d) container_of(d, struct net_device, dev)
768872

net/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ config NET_NS
3232
Allow user space to create what appear to be multiple instances
3333
of the network stack.
3434

35+
config COMPAT_NET_DEV_OPS
36+
def_bool y
37+
3538
source "net/packet/Kconfig"
3639
source "net/unix/Kconfig"
3740
source "net/xfrm/Kconfig"

0 commit comments

Comments
 (0)