Skip to contentSkip to conversation

bridge: export port_no and port_id via IFA_INFO_DATA

RFC PATCH In review Cong Wang 2 participants 7 messages Nov 27, 2012 – Nov 28, 2012

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

RFC PATCH 1/2 bridge: export port_no and port_id via IFA_INFO_DATA

Based on net-next.

This patch exports port->port_no port->port_id in the end of IFA_INFO_DATA.

Cc Herbert Xu <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Thomas Graf <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
---
 include/uapi/linux/if_link.h |    2 ++
 net/bridge/br_netlink.c      |    8 +++++++-
 2 files changed, 9 insertions(+), 1 deletions(-)
include/uapi/linux/if_link.h +2 -0
index bb58aeb..9cd91a9 100644
@@ -218,6 +218,8 @@ enum {
 	IFLA_BRPORT_MODE,	/* mode (hairpin)          */
 	IFLA_BRPORT_GUARD,	/* bpdu guard              */
 	IFLA_BRPORT_PROTECT,	/* root port protection    */
+	IFLA_BRPORT_NO,		/* port no                 */
+	IFLA_BRPORT_ID,		/* port id                 */
 	__IFLA_BRPORT_MAX
 };
 #define IFLA_BRPORT_MAX (__IFLA_BRPORT_MAX - 1)
net/bridge/br_netlink.c +7 -1
index 65429b9..7b7414e 100644
@@ -28,6 +28,8 @@ static inline size_t br_port_info_size(void)
 		+ nla_total_size(1)	/* IFLA_BRPORT_MODE */
 		+ nla_total_size(1)	/* IFLA_BRPORT_GUARD */
 		+ nla_total_size(1)	/* IFLA_BRPORT_PROTECT */
+		+ nla_total_size(2)	/* IFLA_BRPORT_NO */
+		+ nla_total_size(2)	/* IFLA_BRPORT_ID */
 		+ 0;
 }
 
@@ -53,7 +55,9 @@ static int br_port_fill_attrs(struct sk_buff *skb,
 	    nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
 	    nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
 	    nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
-	    nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)))
+	    nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
+	    nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
+	    nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id))
 		return -EMSGSIZE;
 
 	return 0;
@@ -168,6 +172,8 @@ static const struct nla_policy ifla_brport_policy[IFLA_BRPORT_MAX + 1] = {
 	[IFLA_BRPORT_MODE]	= { .type = NLA_U8 },
 	[IFLA_BRPORT_GUARD]	= { .type = NLA_U8 },
 	[IFLA_BRPORT_PROTECT]	= { .type = NLA_U8 },
+	[IFLA_BRPORT_NO]	= { .type = NLA_U16 },
+	[IFLA_BRPORT_ID]	= { .type = NLA_U16 },
 };
 
 /* Change the state of the port and notify spanning tree */
Signature · 1 line
1.7.7.6
author
#1 lore raw

RFC PATCH 2/2 bridge: export multicast database via netlink

Based on net-next.

Warning: this patch is still a draft! :)

This patch exports bridge multicast database via netlink message type RTM_GETMDB. Similar to fdb, but currently bridge-specific. We may need to support modify multicast database too (RTM_{ADD,DEL}MDB).

So, the questions are:

1) Is this design okay?
2) Do we need to make it generic like fdb?
3) Do we need to support RTM_{ADD,DEL}MDB?

Cc Herbert Xu <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Thomas Graf <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
---
 include/uapi/linux/if_bridge.h |   37 +++++++++
 include/uapi/linux/rtnetlink.h |    3 +
 net/bridge/Makefile            |    2 +-
 net/bridge/br_mdb.c            |  174 ++++++++++++++++++++++++++++++++++++++++
 net/bridge/br_multicast.c      |    1 +
 net/bridge/br_private.h        |    1 +
 6 files changed, 217 insertions(+), 1 deletions(-)
 create mode 100644 net/bridge/br_mdb.c
include/uapi/linux/if_bridge.h +37 -0
index b388579..f5655ea 100644
@@ -116,4 +116,41 @@ enum {
 	__IFLA_BRIDGE_MAX,
 };
 #define IFLA_BRIDGE_MAX (__IFLA_BRIDGE_MAX - 1)
+
+/* Bridge multicast database attributes
+ * [MDBA_MDB] = {
+ *    [MDBA_MCADDR]
+ *    [MDBA_BRPORT] = {
+ *        [MDBA_BRPORT_NO]
+ *    }
+ * }
+ * [MDBA_ROUTER] = {
+ *    [MDBA_BRPORT] = {
+ *        [MDBA_BRPORT_NO]
+ *    }
+ * }
+ */
+enum {
+	MDBA_UNSPEC,
+	MDBA_MDB,
+	MDBA_ROUTER,
+	__MDBA_MAX,
+};
+#define MDBA_MAX (__MDBA_MAX - 1)
+
+enum {
+	MDBA_MDB_UNSPEC,
+	MDBA_MDB_MCADDR,
+	MDBA_MDB_BRPORT,
+	__MDBA_MDB_MAX,
+};
+#define MDBA_MDB_MAX (__MDBA_MDB_MAX - 1)
+
+enum {
+	MDBA_BRPORT_UNSPEC,
+	MDBA_BRPORT_NO,
+	__MDBA_BRPORT_MAX,
+};
+#define MDBA_BRPORT_MAX (__MDBA_BRPORT_MAX - 1)
+
 #endif /* _UAPI_LINUX_IF_BRIDGE_H */
include/uapi/linux/rtnetlink.h +3 -0
index 3dee071..0df623f 100644
@@ -125,6 +125,9 @@ enum {
 	RTM_GETNETCONF = 82,
 #define RTM_GETNETCONF RTM_GETNETCONF
 
+	RTM_GETMDB = 86,
+#define RTM_GETMDB RTM_GETMDB
+
 	__RTM_MAX,
 #define RTM_MAX		(((__RTM_MAX + 3) & ~3) - 1)
 };
net/bridge/Makefile +1 -1
index d0359ea..e859098 100644
@@ -12,6 +12,6 @@ bridge-$(CONFIG_SYSFS) += br_sysfs_if.o br_sysfs_br.o
 
 bridge-$(CONFIG_BRIDGE_NETFILTER) += br_netfilter.o
 
-bridge-$(CONFIG_BRIDGE_IGMP_SNOOPING) += br_multicast.o
+bridge-$(CONFIG_BRIDGE_IGMP_SNOOPING) += br_multicast.o br_mdb.o
 
 obj-$(CONFIG_BRIDGE_NF_EBTABLES) += netfilter/
net/bridge/br_mdb.c new file +174 -0

174 lines not on this page · raw message

net/bridge/br_multicast.c +1 -0
index 2417434..be69cd4 100644
@@ -1584,6 +1584,7 @@ void br_multicast_init(struct net_bridge *br)
 		    br_multicast_querier_expired, (unsigned long)br);
 	setup_timer(&br->multicast_query_timer, br_multicast_query_expired,
 		    (unsigned long)br);
+	br_mdb_init();
 }
 
 void br_multicast_open(struct net_bridge *br)
net/bridge/br_private.h +1 -0
index eb9cd42..bf0f6d5 100644
@@ -432,6 +432,7 @@ extern int br_multicast_set_port_router(struct net_bridge_port *p,
 extern int br_multicast_toggle(struct net_bridge *br, unsigned long val);
 extern int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
 extern int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
+extern void br_mdb_init(void);
 
 static inline bool br_multicast_is_router(struct net_bridge *br)
 {
Signature · 1 line
1.7.7.6
author
#5 lore raw

RFC PATCH 1/1 Add mdb command to bridge

In reply to Cong Wang

Warning: it is still a draft! :)

The output is ugly, I just want to prove it works.

Cc Herbert Xu <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Thomas Graf <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
---
 bridge/Makefile           |    2 +-
 bridge/br_common.h        |    1 +
 bridge/bridge.c           |    1 +
 bridge/mdb.c              |  200 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/rtnetlink.h |    3 +
 5 files changed, 206 insertions(+), 1 deletions(-)
 create mode 100644 bridge/mdb.c
bridge/Makefile +1 -1
index 9a6743e..67aceb4 100644
@@ -1,4 +1,4 @@
-BROBJ = bridge.o fdb.o monitor.o link.o
+BROBJ = bridge.o fdb.o monitor.o link.o mdb.o
 
 include ../Config
 
bridge/br_common.h +1 -0
index 718ecb9..67fd75c 100644
@@ -5,6 +5,7 @@ extern int print_fdb(const struct sockaddr_nl *who,
 		     struct nlmsghdr *n, void *arg);
 
 extern int do_fdb(int argc, char **argv);
+extern int do_mdb(int argc, char **argv);
 extern int do_monitor(int argc, char **argv);
 
 extern int preferred_family;
bridge/bridge.c +1 -0
index e2c33b0..1fcd365 100644
@@ -43,6 +43,7 @@ static const struct cmd {
 	int (*func)(int argc, char **argv);
 } cmds[] = {
 	{ "fdb", 	do_fdb },
+	{ "mdb", 	do_mdb },
 	{ "monitor",	do_monitor },
 	{ "help",	do_help },
 	{ 0 }
bridge/mdb.c new file +200 -0

200 lines not on this page · raw message

include/linux/rtnetlink.h +3 -0
index 0e3e0c1..f400b5e 100644
@@ -120,6 +120,9 @@ enum {
 	RTM_SETDCB,
 #define RTM_SETDCB RTM_SETDCB
 
+	RTM_GETMDB = 86,
+#define RTM_GETMDB RTM_GETMDB
+
 	__RTM_MAX,
 #define RTM_MAX		(((__RTM_MAX + 3) & ~3) - 1)
 };
Signature · 1 line
1.7.7.6
#2 lore raw

Re: [RFC PATCH 2/2] bridge: export multicast database via netlink

In reply to Cong Wang
Quoted 19 lines · Cong Wang
net/bridge/br_mdb.c new file +19 -0
+static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
+			       u32 seq, struct net_device *dev)
+{
+	struct net_bridge *br = netdev_priv(dev);
+	struct net_bridge_port *p;
+	struct hlist_node *n;
+	struct nlattr *nest, *nest2;
+
+	if (!br->multicast_router || hlist_empty(&br->router_list)) {
+		printk(KERN_INFO "no router on bridge\n");
+		return 0;
+	}
+
+	nest = nla_nest_start(skb, MDBA_ROUTER);
+	if (nest == NULL)
+		return -EMSGSIZE;
+	nest2 = nla_nest_start(skb, MDBA_MDB_BRPORT);
+	if (nest2 == NULL)
+		goto fail;

net/bridge/br_mdb.c line 51

Quoted 10 lines
net/bridge/br_mdb.c new file +10 -0
+
+	hlist_for_each_entry_rcu(p, n, &br->router_list, rlist) {
+		if (p && nla_put_u16(skb, MDBA_BRPORT_NO, p->port_no)) {
+			nla_nest_cancel(skb, nest2);
+			goto fail;
+		}
+	}
+
+	nla_nest_end(skb, nest2);
+	nla_nest_end(skb, nest);

I would simplify the MDBA_ROUTER attribute to a u16[len(br->router_list)]. If we ever need something more complex we can retire the MDBA_ROUTER attribute and replace it with something newer.

Quoted 7 lines
net/bridge/br_mdb.c new file +7 -0
+	nest = nla_nest_start(skb, MDBA_MDB);
+	if (nest == NULL)
+		return -EMSGSIZE;
+
+	for (i = 0; i < mdb->max; i++) {
+		struct hlist_node *h;
+		struct net_bridge_mdb_entry *mp;

net/bridge/br_mdb.c line 93

Quoted 10 lines
net/bridge/br_mdb.c new file +10 -0
+		struct net_bridge_port_group *p, **pp;
+		struct net_bridge_port *port;
+
+		hlist_for_each_entry_rcu(mp, h, &mdb->mhash[i], hlist[mdb->ver]) {
+			if (nla_put_be32(skb, MDBA_MDB_MCADDR, mp->addr.u.ip4))
+				goto fail;
+
+			nest2 = nla_nest_start(skb, MDBA_MDB_BRPORT);
+			if (nest2 == NULL)
+				goto fail;

What if you can't fit all theh hash entries into a single netlink message? You need to allow splitting theh hash across multiple messages. Therefore I suggest that you add a container attribute for each mdb_entry like this:

MDBA_MDB = {

  1 = {
    MDBA_MDB_MCADDR = { ... },
    MDBA_MDB_BRPORT = { ... },
  },
  2 = {
    MDBA_MDB_MCADDR = { ... },
    MDBA_MDB_BR_PORT = { ... },

},
[...]
}

net/bridge/br_mdb.c line 129

Quoted 12 lines
net/bridge/br_mdb.c new file +12 -0
+static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+	struct net_device *dev;
+	struct net *net = sock_net(skb->sk);
+	struct nlmsghdr *nlh;
+	u32 seq = cb->nlh->nlmsg_seq;
+	int idx = 0, s_idx;
+
+	s_idx = cb->args[0];
+
+	rcu_read_lock();
+	cb->seq = net->dev_base_seq;

Using RCU read lock is OK but that means you must be prepared to handle additions/removals to the table between dump iterations and thus you must introduce a seq counter bumped on each table change and add it to the dev_base_seq above.

Quoted 8 lines
net/bridge/br_mdb.c new file +8 -0
+	for_each_netdev_rcu(net, dev) {
+		if (dev->priv_flags & IFF_EBRIDGE) {
+			struct br_port_msg *bpm;
+
+			if (idx < s_idx)
+				goto cont;
+
+			nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,

net/bridge/br_mdb.c line 148

Quoted 10 lines
net/bridge/br_mdb.c new file +10 -0
+					seq, RTM_GETMDB,
+					sizeof(*bpm), NLM_F_MULTI);
+			if (nlh == NULL)
+				break;
+
+			bpm = nlmsg_data(nlh);
+			bpm->ifindex = dev->ifindex;
+			if (br_mdb_fill_info(skb, cb, seq, dev) < 0) {
+				printk(KERN_INFO "br_mdb_fill_info failed\n");
+				goto fail;

As stated above I believe that you should allow for hashtable to be split across multiple messages so you need to store the hash table offset as well and properly finalize and send the message on error here.

author
#3 lore raw

Re: [RFC PATCH 2/2] bridge: export multicast database via netlink

In reply to Thomas Graf
Quoted 26 lines · Thomas Graf
Quoted 25 lines · Cong Wang
net/bridge/br_mdb.c new file +25 -0
+static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
+			       u32 seq, struct net_device *dev)
+{
+	struct net_bridge *br = netdev_priv(dev);
+	struct net_bridge_port *p;
+	struct hlist_node *n;
+	struct nlattr *nest, *nest2;
+
+	if (!br->multicast_router || hlist_empty(&br->router_list)) {
+		printk(KERN_INFO "no router on bridge\n");
+		return 0;
+	}
+
+	nest = nla_nest_start(skb, MDBA_ROUTER);
+	if (nest == NULL)
+		return -EMSGSIZE;
+	nest2 = nla_nest_start(skb, MDBA_MDB_BRPORT);
+	if (nest2 == NULL)
+		goto fail;
+
+	hlist_for_each_entry_rcu(p, n, &br->router_list, rlist) {
+		if (p && nla_put_u16(skb, MDBA_BRPORT_NO, p->port_no)) {
+			nla_nest_cancel(skb, nest2);
+			goto fail;
+		}
Quoted 8 lines

net/bridge/br_mdb.c line 51

Quoted 4 lines
net/bridge/br_mdb.c new file +4 -0
+	}
+
+	nla_nest_end(skb, nest2);
+	nla_nest_end(skb, nest);

I would simplify the MDBA_ROUTER attribute to a u16[len(br->router_list)]. If we ever need something more complex we can retire the MDBA_ROUTER attribute and replace it with something newer.

Makes sense, will do.

I wanted to reserve some for adding new attributes to MDBA_ROUTER, but so far it is not necessary.

Quoted 32 lines
Quoted 8 lines
net/bridge/br_mdb.c new file +7 -0
+	nest = nla_nest_start(skb, MDBA_MDB);
+	if (nest == NULL)
+		return -EMSGSIZE;
+
+	for (i = 0; i < mdb->max; i++) {
+		struct hlist_node *h;
+		struct net_bridge_mdb_entry *mp;

net/bridge/br_mdb.c line 93

Quoted 10 lines
net/bridge/br_mdb.c new file +10 -0
+		struct net_bridge_port_group *p, **pp;
+		struct net_bridge_port *port;
+
+		hlist_for_each_entry_rcu(mp, h, &mdb->mhash[i], hlist[mdb->ver]) {
+			if (nla_put_be32(skb, MDBA_MDB_MCADDR, mp->addr.u.ip4))
+				goto fail;
+
+			nest2 = nla_nest_start(skb, MDBA_MDB_BRPORT);
+			if (nest2 == NULL)
+				goto fail;

What if you can't fit all theh hash entries into a single netlink message? You need to allow splitting theh hash across multiple messages. Therefore I suggest that you add a container attribute for each mdb_entry like this:

MDBA_MDB = {

  1 = {
    MDBA_MDB_MCADDR = { ... },
    MDBA_MDB_BRPORT = { ... },
  },
  2 = {
    MDBA_MDB_MCADDR = { ... },
    MDBA_MDB_BR_PORT = { ... },
Quoted 3 lines

},
[...]
}

I thought the user-space will reassemble multiple-part messages, but I probably misunderstand this...

Actually I was trying to reduce the size of the netlink message. :)

Quoted 10 lines
Quoted 10 lines
net/bridge/br_mdb.c new file +9 -0
+static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+	struct net_device *dev;
+	struct net *net = sock_net(skb->sk);
+	struct nlmsghdr *nlh;
+	u32 seq = cb->nlh->nlmsg_seq;
+	int idx = 0, s_idx;
+
+	s_idx = cb->args[0];
Quoted 8 lines

net/bridge/br_mdb.c line 129

Quoted 3 lines
net/bridge/br_mdb.c new file +3 -0
+
+	rcu_read_lock();
+	cb->seq = net->dev_base_seq;

Using RCU read lock is OK but that means you must be prepared to handle additions/removals to the table between dump iterations and thus you must introduce a seq counter bumped on each table change and add it to the dev_base_seq above.

Yeah, as you told me before. I will make another patch for this. Thanks for reminding!

Quoted 16 lines
Quoted 16 lines
net/bridge/br_mdb.c new file +15 -0
+	for_each_netdev_rcu(net, dev) {
+		if (dev->priv_flags & IFF_EBRIDGE) {
+			struct br_port_msg *bpm;
+
+			if (idx < s_idx)
+				goto cont;
+
+			nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
+					seq, RTM_GETMDB,
+					sizeof(*bpm), NLM_F_MULTI);
+			if (nlh == NULL)
+				break;
+
+			bpm = nlmsg_data(nlh);
+			bpm->ifindex = dev->ifindex;
Quoted 8 lines

net/bridge/br_mdb.c line 148

Quoted 3 lines
net/bridge/br_mdb.c new file +3 -0
+			if (br_mdb_fill_info(skb, cb, seq, dev) < 0) {
+				printk(KERN_INFO "br_mdb_fill_info failed\n");
+				goto fail;

As stated above I believe that you should allow for hashtable to be split across multiple messages so you need to store the hash table offset as well and properly finalize and send the message on error here.

You mean saving the offset into cb->args[1]?

Thanks!

author
#4 lore raw

Re: [RFC PATCH 2/2] bridge: export multicast database via netlink

In reply to Thomas Graf
Quoted 5 lines · Thomas Graf

Using RCU read lock is OK but that means you must be prepared to handle additions/removals to the table between dump iterations and thus you must introduce a seq counter bumped on each table change and add it to the dev_base_seq above.

Something like this?

net/bridge/br_mdb.c +0 -0
index dc73091..4c3b097 100644
@@ -74,6 +74,8 @@ static int br_mdb_fill_info(struct sk_buff *skb,

struct netlink_callback *cb,
return 0;
}

+ cb->seq = mdb->seq;
+
nest = nla_nest_start(skb, MDBA_MDB);
if (nest == NULL)
return -EMSGSIZE;
@@ -126,7 +128,6 @@ static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
s_idx = cb->args[0];

rcu_read_lock();
- cb->seq = net->dev_base_seq;

for_each_netdev_rcu(net, dev) {
if (dev->priv_flags & IFF_EBRIDGE) {

net/bridge/br_multicast.c +0 -0
index 1e6ce50..ccf5cfb 100644
@@ -322,6 +322,7 @@ static int br_mdb_rehash(struct

net_bridge_mdb_htable __rcu **mdbp, int max,

mdb->size = old ? old->size : 0;
mdb->ver = old ? old->ver ^ 1 : 0;
+ mdb->seq = old ? (old->seq + 1): 0;

if (!old || elasticity)
get_random_bytes(&mdb->secret, sizeof(mdb->secret));

net/bridge/br_private.h +1 -0
index a02921e..2f5f5b8 100644
@@ -105,6 +105,7 @@ struct net_bridge_mdb_htable
 	u32				max;
 	u32				secret;
 	u32				ver;
+	u32				seq;
 };
 
 struct net_bridge_port