: netdevice rx mode synchronization fixes + macvlan driver
Subsystems (2)
cover : netdevice rx mode synchronization fixes + macvlan driver
These patches introduce a new net_device hook for synchronizing promiscous and allmulti state to an underlying device (as done by 8021q, bonding, maxvlan, ..) to fix some races that occur when doing this from dev->set_multicast_list. They also introduce two helpers for multicast list synchonization and change VLAN to make use of all of this (mainly to show that its not only for macvlan, but it also fixes some real races). The last patch adds the macvlan driver.
Please apply, thanks.
MAINTAINERS | 6 + drivers/net/Kconfig | 10 + drivers/net/Makefile | 1 + drivers/net/macvlan.c | 496 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/if_macvlan.h | 9 + include/linux/if_vlan.h | 7 - include/linux/netdevice.h | 8 + net/8021q/vlan.c | 1 + net/8021q/vlan.h | 1 + net/8021q/vlan_dev.c | 167 ++------------- net/core/dev.c | 43 ++++- net/core/dev_mcast.c | 75 +++++++ 12 files changed, 670 insertions(+), 154 deletions(-) create mode 100644 drivers/net/macvlan.c create mode 100644 include/linux/if_macvlan.h
Patrick McHardy (5):
[NET]: Add net_device change_rx_mode callback [NET]: dev_mcast: add multicast list synchronization helpers [VLAN]: Fix promiscous/allmulti synchronization races [VLAN]: Use multicast list synchronization helpers [NET]: Add macvlan driver
PATCH 1/5 : Add net_device change_rx_mode callback
[NET]: Add net_device change_rx_mode callback
Currently the set_multicast_list (and set_rx_mode) callbacks are responsible for configuring the device according to the IFF_PROMISC, IFF_MULTICAST and IFF_ALLMULTI flags and the mc_list (and uc_list in case of set_rx_mode).
These callbacks can be invoked from BH context without the rtnl_mutex by dev_mc_add/dev_mc_delete, which makes reading the device flags and promiscous/allmulti count racy. For real hardware drivers that just commit all changes to the hardware this is not a real problem since the stack guarantees to call them for every change, so at least the final call will not race and commit the correct configuration to the hardware.
For software devices that want to synchronize promiscous and multicast state to an underlying device however this can cause corruption of the underlying device's flags or promisc/allmulti counts.
When the software device is concurrently put in promiscous or allmulti mode while set_multicast_list is invoked from bottem half context, the device might synchronize the change to the underlying device without holding the rtnl_mutex, which races with concurrent changes to the underlying device.
Add a dev->change_rx_flags hook that is invoked when any of the flags that affect rx filtering change (under the rtnl_mutex), which allows drivers to perform synchronization immediately and only synchronize the address lists in set_multicast_list/set_rx_mode.
---
commit b977db07766ffb261d2c7ebe050cbc3b1d7d281a
tree 1497ca885659afdde8ea81ea7d1534de28a773d3
parent 15028aad00ddf241581fbe74a02ec89cbb28d35d
author Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:53:55 +0200 committer Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:53:55 +0200
include/linux/netdevice.h | 3 +++ net/core/dev.c | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletions(-)
include/linux/netdevice.h +3 -0
@@ -516,6 +516,9 @@ struct net_devicevoid *saddr, unsigned len);int (*rebuild_header)(struct sk_buff *skb); +#define HAVE_CHANGE_RX_FLAGS +void (*change_rx_flags)(struct net_device *dev, +int flags);#define HAVE_SET_RX_MODE void (*set_rx_mode)(struct net_device *dev);#define HAVE_MULTICAST
net/core/dev.c +16 -1
@@ -2507,6 +2507,8 @@ static void __dev_set_promiscuity(struct net_device *dev, int inc) {unsigned short old_flags = dev->flags; +ASSERT_RTNL (); +if ((dev->promiscuity += inc) ==0 ) dev->flags &= ~IFF_PROMISC ;else
@@ -2521,6 +2523,9 @@ static void __dev_set_promiscuity(struct net_device *dev, int inc) dev->name, (dev->flags &IFF_PROMISC ), (old_flags &IFF_PROMISC ), audit_get_loginuid(current->audit_context)); + +if (dev->change_rx_flags) + dev->change_rx_flags (dev,IFF_PROMISC ); } }
@@ -2559,11 +2564,16 @@ void dev_set_allmulti(struct net_device *dev, int inc) {unsigned short old_flags = dev->flags; +ASSERT_RTNL (); + dev->flags |=IFF_ALLMULTI ;if ((dev->allmulti += inc) ==0 ) dev->flags &= ~IFF_ALLMULTI ; -if (dev->flags ^ old_flags) +if (dev->flags ^ old_flags) { +if (dev->change_rx_flags) + dev->change_rx_flags (dev,IFF_ALLMULTI );dev_set_rx_mode (dev); + } }/*
@@ -2764,6 +2774,8 @@ int dev_change_flags(struct net_device *dev, unsigned flags)int ret, changes;int old_flags = dev->flags; +ASSERT_RTNL (); +/* * Set the flags on our device. */
@@ -2778,6 +2790,9 @@ int dev_change_flags(struct net_device *dev, unsigned flags)* Load in the correct multicast list now the flags have changed. */ +if (dev->change_rx_flags && (dev->flags ^ flags) &IFF_MULTICAST ) + dev->change_rx_flags (dev,IFF_MULTICAST ); +dev_set_rx_mode (dev);/*
Re: [NET 01/05]: Add net_device change_rx_mode callback
From: Patrick McHardy <[email protected]>
Date: Thu, 12 Jul 2007 20:13:40 +0200 (MEST)
Quoted 25 lines
[NET]: Add
net_devicechange_rx_modecallbackCurrently the
set_multicast_list(andset_rx_mode) callbacks are responsible for configuring the device according to theIFF_PROMISC,IFF_MULTICASTandIFF_ALLMULTIflags and themc_list(anduc_listin case ofset_rx_mode).These callbacks can be invoked from BH context without the
rtnl_mutexby dev_mc_add/dev_mc_delete, which makes reading the device flags and promiscous/allmulti count racy. For real hardware drivers that just commit all changes to the hardware this is not a real problem since the stack guarantees to call them for every change, so at least the final call will not race and commit the correct configuration to the hardware.For software devices that want to synchronize promiscous and multicast state to an underlying device however this can cause corruption of the underlying device's flags or promisc/allmulti counts.
When the software device is concurrently put in promiscous or allmulti mode while
set_multicast_listis invoked from bottem half context, the device might synchronize the change to the underlying device without holding thertnl_mutex, which races with concurrent changes to the underlying device.
Quoted 6 lines
Add a
dev->change_rx_flagshook that is invoked when any of the flags that affect rx filtering change (under thertnl_mutex), which allows drivers to perform synchronization immediately and only synchronize the address lists in set_multicast_list/set_rx_mode.Signed-off-by Patrick McHardy <[email protected]>
Applied, thanks Patrick.
PATCH 2/5 : dev_mcast: add multicast list synchronization helpers
In reply to Patrick McHardy[NET]: dev_mcast: add multicast list synchronization helpers
The method drivers currently use to synchronize multicast lists is not very pretty:
- walk the multicast list
- search each entry on a copy of the previous list
- if new add to lower device
- walk the copy of the previous list
- search each entry on the current list
- if removed delete from lower device
- copy entire list
This patch adds a new field to struct dev_addr_list to store the synchronization state and adds two helper functions for synchronization and cleanup.
---
commit 934ee4749ce12cb9b868a98fd3822cf5cdfa8208
tree ddb91cc1428e52f82712101518fccce65ed4597d
parent b977db07766ffb261d2c7ebe050cbc3b1d7d281a
author Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:53:58 +0200 committer Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:53:58 +0200
include/linux/netdevice.h | 3 ++ net/core/dev_mcast.c | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 0 deletions(-)
include/linux/netdevice.h +3 -0
@@ -190,6 +190,7 @@ struct dev_addr_liststruct dev_addr_list *next;u8 da_addr[MAX_ADDR_LEN ];u8 da_addrlen; +u8 da_synced;int da_users;int da_gusers; };
@@ -1103,6 +1104,8 @@ extern int dev_unicast_delete(struct net_device *dev, void *addr, int alen);extern int dev_unicast_add (struct net_device *dev,void *addr,int alen);extern int dev_mc_delete (struct net_device *dev,void *addr,int alen,int all);extern int dev_mc_add (struct net_device *dev,void *addr,int alen,int newonly); +extern int dev_mc_sync (struct net_device *to,struct net_device *from); +extern void dev_mc_unsync (struct net_device *to,struct net_device *from);extern void dev_mc_discard (struct net_device *dev);extern int __dev_addr_delete (struct dev_addr_list **list,int *count,void *addr,int alen,int all);extern int __dev_addr_add (struct dev_addr_list **list,int *count,void *addr,int alen,int newonly);
net/core/dev_mcast.c +75 -0
81 lines not on this page · raw message
Re: [NET 02/05]: dev_mcast: add multicast list synchronization helpers
From: Patrick McHardy <[email protected]>
Date: Thu, 12 Jul 2007 20:13:42 +0200 (MEST)
Quoted 13 lines
[NET]:
dev_mcast: add multicast list synchronization helpersThe method drivers currently use to synchronize multicast lists is not very pretty:
- walk the multicast list
- search each entry on a copy of the previous list
- if new add to lower device
- walk the copy of the previous list
- search each entry on the current list
- if removed delete from lower device
- copy entire list
Quoted 5 lines
This patch adds a new field to struct
dev_addr_listto store the synchronization state and adds two helper functions for synchronization and cleanup.Signed-off-by Patrick McHardy <[email protected]>
Applied.
PATCH 3/5 : Fix promiscous/allmulti synchronization races
In reply to Patrick McHardy[VLAN]: Fix promiscous/allmulti synchronization races
The set_multicast_list function may be called without holding the rtnl mutex, resulting in races when changing the underlying device's promiscous and allmulti state. Use the change_rx_mode hook, which is always invoked under the rtnl.
---
commit 86de1244f4e8736a7bdfc28b899cc2ce5e36cf5c
tree 1379e7ae0b054d4f1390b0d1960aecee9ece37e6
parent 934ee4749ce12cb9b868a98fd3822cf5cdfa8208
author Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:57:34 +0200 committer Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:57:34 +0200
include/linux/if_vlan.h | 2 -- net/8021q/vlan.c | 1 + net/8021q/vlan.h | 1 + net/8021q/vlan_dev.c | 38 ++++++++++++++++++++------------------ 4 files changed, 22 insertions(+), 20 deletions(-)
include/linux/if_vlan.h +0 -2
@@ -132,8 +132,6 @@ struct vlan_dev_info {* made, in order to feed the right changes down * to the real hardware... */ -int old_allmulti;/* similar to above. */ -int old_promiscuity;/* similar to above. */ struct net_device *real_dev;/* the underlying device/interface */ unsigned char real_dev_addr[ETH_ALEN ];struct proc_dir_entry *dent;/* Holds the proc data */
net/8021q/vlan.c +1 -0
@@ -373,6 +373,7 @@ void vlan_setup(struct net_device *new_dev) new_dev->open = vlan_dev_open; new_dev->stop = vlan_dev_stop; new_dev->set_multicast_list = vlan_dev_set_multicast_list; + new_dev->change_rx_flags = vlan_change_rx_flags; new_dev->destructor = free_netdev; new_dev->do_ioctl = vlan_dev_ioctl;
net/8021q/vlan.h +1 -0
@@ -69,6 +69,7 @@ int vlan_dev_set_vlan_flag(const struct net_device *dev, u32 flag, short flag_val);void vlan_dev_get_realdev_name (const struct net_device *dev,char *result);void vlan_dev_get_vid (const struct net_device *dev,unsigned short *result); +void vlan_change_rx_flags (struct net_device *dev,int change);void vlan_dev_set_multicast_list (struct net_device *vlan_dev);int vlan_check_real_dev (struct net_device *real_dev,unsigned short vlan_id);
net/8021q/vlan_dev.c +20 -18
@@ -712,6 +712,11 @@ int vlan_dev_open(struct net_device *dev) }memcpy (vlan->real_dev_addr, real_dev->dev_addr,ETH_ALEN ); +if (dev->flags &IFF_ALLMULTI ) +dev_set_allmulti (real_dev,1 ); +if (dev->flags &IFF_PROMISC ) +dev_set_promiscuity (real_dev,1 ); +return 0 ; }
@@ -721,6 +726,11 @@ int vlan_dev_stop(struct net_device *dev)vlan_flush_mc_list (dev); +if (dev->flags &IFF_ALLMULTI ) +dev_set_allmulti (real_dev,-1 ); +if (dev->flags &IFF_PROMISC ) +dev_set_promiscuity (real_dev,-1 ); +if (compare_ether_addr (dev->dev_addr, real_dev->dev_addr))dev_unicast_delete (real_dev, dev->dev_addr, dev->addr_len);
@@ -754,34 +764,26 @@ int vlan_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)return err; } +void vlan_change_rx_flags (struct net_device *dev,int change) +{ +struct net_device *real_dev =VLAN_DEV_INFO (dev)->real_dev; + +if (change &IFF_ALLMULTI ) +dev_set_allmulti (real_dev, dev->flags &IFF_ALLMULTI ?1 :-1 ); +if (change &IFF_PROMISC ) +dev_set_promiscuity (real_dev, dev->flags &IFF_PROMISC ?1 :-1 ); +} +/** Taken from Gleb + Lennert's VLAN code, and modified... */ void vlan_dev_set_multicast_list (struct net_device *vlan_dev) {struct dev_mc_list *dmi;struct net_device *real_dev; -int inc;if (vlan_dev && (vlan_dev->priv_flags &IFF_802_1Q_VLAN )) {/* Then it's a real vlan device, as far as we can tell.. */ real_dev =VLAN_DEV_INFO (vlan_dev)->real_dev; -/* compare the current promiscuity to the last promisc we had.. */ - inc = vlan_dev->promiscuity -VLAN_DEV_INFO (vlan_dev)->old_promiscuity; -if (inc) { -printk (KERN_INFO "%s: dev_set_promiscuity(master, %d)\n" , - vlan_dev->name, inc); -dev_set_promiscuity (real_dev, inc);/* found in dev.c */ -VLAN_DEV_INFO (vlan_dev)->old_promiscuity = vlan_dev->promiscuity; - } - - inc = vlan_dev->allmulti -VLAN_DEV_INFO (vlan_dev)->old_allmulti; -if (inc) { -printk (KERN_INFO "%s: dev_set_allmulti(master, %d)\n" , - vlan_dev->name, inc); -dev_set_allmulti (real_dev, inc);/* dev.c */ -VLAN_DEV_INFO (vlan_dev)->old_allmulti = vlan_dev->allmulti; - } -/* looking for addresses to add to master's list */ for (dmi = vlan_dev->mc_list; dmi !=NULL ; dmi = dmi->next) {if (vlan_should_add_mc (dmi,VLAN_DEV_INFO (vlan_dev)->old_mc_list)) {
Re: [VLAN 03/05]: Fix promiscous/allmulti synchronization races
From: Patrick McHardy <[email protected]>
Date: Thu, 12 Jul 2007 20:13:44 +0200 (MEST)
Quoted 8 lines
[VLAN]: Fix promiscous/allmulti synchronization races
The
set_multicast_listfunction may be called without holding the rtnl mutex, resulting in races when changing the underlying device's promiscous and allmulti state. Use thechange_rx_modehook, which is always invoked under the rtnl.Signed-off-by Patrick McHardy <[email protected]>
Applied.
PATCH 4/5 : Use multicast list synchronization helpers
In reply to Patrick McHardy[VLAN]: Use multicast list synchronization helpers
---
commit 30fed2136dc8c9baea5aada97fc6ce60e99fdbf9
tree 8919e11b22f621c4ab2a3cbb45e8d4431385a38f
parent 86de1244f4e8736a7bdfc28b899cc2ce5e36cf5c
author Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:57:37 +0200 committer Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:57:37 +0200
include/linux/if_vlan.h | 5 -- net/8021q/vlan_dev.c | 131 +---------------------------------------------- 2 files changed, 2 insertions(+), 134 deletions(-)
include/linux/if_vlan.h +0 -5
@@ -127,11 +127,6 @@ struct vlan_dev_info {* like DHCP that use packet-filtering and don't understand * 802.1Q */ -struct dev_mc_list *old_mc_list;/* old multi-cast list for the VLAN interface.. -* we save this so we can tell what changes were -* made, in order to feed the right changes down -* to the real hardware... -*/ struct net_device *real_dev;/* the underlying device/interface */ unsigned char real_dev_addr[ETH_ALEN ];struct proc_dir_entry *dent;/* Holds the proc data */
net/8021q/vlan_dev.c +2 -129
147 lines not on this page · raw message
Re: [VLAN 04/05]: Use multicast list synchronization helpers
From: Patrick McHardy <[email protected]>
Date: Thu, 12 Jul 2007 20:13:46 +0200 (MEST)
Quoted 3 lines
[VLAN]: Use multicast list synchronization helpers
Signed-off-by Patrick McHardy <[email protected]>
Applied.
PATCH 5/5 : Add macvlan driver
In reply to Patrick McHardy[NET]: Add macvlan driver
Add macvlan driver, which allows to create virtual ethernet devices based on MAC address.
---
commit 553029f4dd24c41322c3e6cb31458e7e094d21ee
tree 71791bde3cea46103a44389f7f07cf6eea86eba9
parent 30fed2136dc8c9baea5aada97fc6ce60e99fdbf9
author Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:57:45 +0200 committer Patrick McHardy <[email protected]> Thu, 12 Jul 2007 19:57:45 +0200
MAINTAINERS | 6 + drivers/net/Kconfig | 10 + drivers/net/Makefile | 1 drivers/net/macvlan.c | 496 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/if_macvlan.h | 9 + include/linux/netdevice.h | 2 net/core/dev.c | 26 ++ 7 files changed, 550 insertions(+), 0 deletions(-)
MAINTAINERS +6 -0
@@ -2330,6 +2330,12 @@ W: http://linuxwireless.org/ T: git kernel.org:/pub/scm/linux/kernel/git/jbenc/mac80211.git S: Maintained +MACVLAN DRIVER +P: Patrick McHardy +M: [email protected] +L: [email protected] +S: Maintained + MARVELL YUKON / SYSKONNECT DRIVER P: Mirko Lindner M: [email protected]
drivers/net/Kconfig +10 -0
@@ -82,6 +82,16 @@ config BONDING To compile this driver as a module, choose M here: the module will be called bonding. +config MACVLAN + tristate "MAC-VLAN support (EXPERIMENTAL)" + depends on EXPERIMENTAL + ---help--- + This allows one to create virtual interfaces that map packets to + or from specific MAC addresses to a particular interface. + + To compile this driver as a module, choose M here: the module + will be called macvlan. + config EQUALIZER tristate "EQL (serial line load balancing) support" ---help---
drivers/net/Makefile +1 -0
@@ -128,6 +128,7 @@ obj-$(CONFIG_SLHC) += slhc.o obj-$(CONFIG_DUMMY) += dummy.o obj-$(CONFIG_IFB) += ifb.o +obj-$(CONFIG_MACVLAN) += macvlan.o obj-$(CONFIG_DE600) += de600.o obj-$(CONFIG_DE620) += de620.o obj-$(CONFIG_LANCE) += lance.o
drivers/net/macvlan.c new file +496 -0
496 lines not on this page · raw message
include/linux/if_macvlan.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef _LINUX_IF_MACVLAN_H +#define _LINUX_IF_MACVLAN_H + +#ifdef __KERNEL__ + +extern struct sk_buff *(*macvlan_handle_frame_hook)(struct sk_buff *); + +#endif /* __KERNEL__ */ +#endif /* _LINUX_IF_MACVLAN_H */
include/linux/netdevice.h +2 -0
@@ -564,6 +564,8 @@ struct net_device/* bridge stuff */ struct net_bridge_port *br_port; +/* macvlan */ +struct macvlan_port *macvlan_port;/* class/net/name entry */ struct device dev;
net/core/dev.c +26 -0
@@ -98,6 +98,7 @@#include <linux/seq_file.h> #include <linux/stat.h> #include <linux/if_bridge.h> +#include <linux/if_macvlan.h> #include <net/dst.h> #include <net/pkt_sched.h> #include <net/checksum.h>
@@ -1800,6 +1801,28 @@ static inline struct sk_buff *handle_bridge(struct sk_buff *skb,#define handle_bridge (skb, pt_prev, ret, orig_dev) (skb)#endif +#if defined(CONFIG_MACVLAN ) || defined(CONFIG_MACVLAN_MODULE ) +struct sk_buff *(*macvlan_handle_frame_hook)(struct sk_buff *skb)__read_mostly ; +EXPORT_SYMBOL_GPL (macvlan_handle_frame_hook); + +static inline struct sk_buff *handle_macvlan (struct sk_buff *skb, +struct packet_type **pt_prev, +int *ret, +struct net_device *orig_dev) +{ +if (skb->dev->macvlan_port ==NULL ) +return skb; + +if (*pt_prev) { + *ret =deliver_skb (skb, *pt_prev, orig_dev); + *pt_prev =NULL ; + } +return macvlan_handle_frame_hook (skb); +} +#else +#define handle_macvlan (skb, pt_prev, ret, orig_dev) (skb) +#endif +#ifdef CONFIG_NET_CLS_ACT /* TODO: Maybe we should just force sch_ingress to be compiled in * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
@@ -1907,6 +1930,9 @@ ncls: skb =handle_bridge (skb, &pt_prev, &ret, orig_dev);if (!skb) goto out; + skb =handle_macvlan (skb, &pt_prev, &ret, orig_dev); +if (!skb) + goto out; type = skb->protocol;list_for_each_entry_rcu (ptype, &ptype_base[ntohs (type)&15 ], list) {
David Miller
maintainerRe: [NET 05/05]: Add macvlan driver
From: Patrick McHardy <[email protected]>
Date: Thu, 12 Jul 2007 20:13:48 +0200 (MEST)
Quoted 6 lines
[NET]: Add macvlan driver
Add macvlan driver, which allows to create virtual ethernet devices based on MAC address.
Signed-off-by Patrick McHardy <[email protected]>
Looks good, applied.