[PATCH 3/3] route/link: add support for IFLA_LINK_NETNSID

Thomas Haller thaller at redhat.com
Fri Aug 14 08:32:38 PDT 2015


---
 include/netlink-private/types.h |  3 ++-
 include/netlink/route/link.h    |  3 +++
 lib/route/link.c                | 33 +++++++++++++++++++++++++++++++--
 libnl-route-3.sym               |  2 ++
 4 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/include/netlink-private/types.h b/include/netlink-private/types.h
index 013816f..4179c66 100644
--- a/include/netlink-private/types.h
+++ b/include/netlink-private/types.h
@@ -164,8 +164,9 @@ struct rtnl_link
 	uint32_t			l_index;
 	uint32_t			l_flags;
 	uint32_t			l_change;
-	uint32_t 			l_mtu;
+	uint32_t			l_mtu;
 	uint32_t			l_link;
+	int32_t				l_link_netnsid;
 	uint32_t			l_txqlen;
 	uint32_t			l_weight;
 	uint32_t			l_master;
diff --git a/include/netlink/route/link.h b/include/netlink/route/link.h
index 8f74f42..7ec1c15 100644
--- a/include/netlink/route/link.h
+++ b/include/netlink/route/link.h
@@ -202,6 +202,9 @@ extern uint8_t	rtnl_link_get_operstate(struct rtnl_link *);
 extern void	rtnl_link_set_linkmode(struct rtnl_link *, uint8_t);
 extern uint8_t	rtnl_link_get_linkmode(struct rtnl_link *);
 
+void rtnl_link_set_link_netnsid(struct rtnl_link *link, int32_t link_netnsid);
+int rtnl_link_get_link_netnsid(const struct rtnl_link *link, int32_t *out_link_netnsid);
+
 extern const char *	rtnl_link_get_ifalias(struct rtnl_link *);
 extern void		rtnl_link_set_ifalias(struct rtnl_link *, const char *);
 
diff --git a/lib/route/link.c b/lib/route/link.c
index bee1048..0405b0d 100644
--- a/lib/route/link.c
+++ b/lib/route/link.c
@@ -61,6 +61,7 @@
 #define LINK_ATTR_PHYS_PORT_ID	(1 << 28)
 #define LINK_ATTR_NS_FD		(1 << 29)
 #define LINK_ATTR_NS_PID	(1 << 30)
+#define LINK_ATTR_LINK_NETNSID	(1 << 31)
 
 static struct nl_cache_ops rtnl_link_ops;
 static struct nl_object_ops link_obj_ops;
@@ -416,6 +417,11 @@ int rtnl_link_info_parse(struct rtnl_link *link, struct nlattr **tb)
 		link->ce_mask |= LINK_ATTR_LINK;
 	}
 
+	if (tb[IFLA_LINK_NETNSID]) {
+		link->l_link_netnsid = nla_get_s32(tb[IFLA_LINK_NETNSID]);
+		link->ce_mask |= LINK_ATTR_LINK_NETNSID;
+	}
+
 	if (tb[IFLA_WEIGHT]) {
 		link->l_weight = nla_get_u32(tb[IFLA_WEIGHT]);
 		link->ce_mask |= LINK_ATTR_WEIGHT;
@@ -677,13 +683,17 @@ static void link_dump_line(struct nl_object *obj, struct nl_dump_params *p)
 		nl_dump(p, "<%s> ", buf);
 
 	if (link->ce_mask & LINK_ATTR_LINK) {
-		if (cache) {
+		if (   cache
+		    && !(link->ce_mask & LINK_ATTR_LINK_NETNSID)) {
 			struct rtnl_link *ll = rtnl_link_get(cache, link->l_link);
 			nl_dump(p, "slave-of %s ", ll ? ll->l_name : "NONE");
 			if (ll)
 				rtnl_link_put(ll);
-		} else
+		} else {
 			nl_dump(p, "slave-of %d ", link->l_link);
+			if (link->ce_mask & LINK_ATTR_LINK_NETNSID)
+				nl_dump(p, "(netns %d) ", link->l_link_netnsid);
+		}
 	}
 
 	if (link->ce_mask & LINK_ATTR_GROUP)
@@ -908,6 +918,7 @@ static int link_compare(struct nl_object *_a, struct nl_object *_b,
 	diff |= LINK_DIFF(IFINDEX,	a->l_index != b->l_index);
 	diff |= LINK_DIFF(MTU,		a->l_mtu != b->l_mtu);
 	diff |= LINK_DIFF(LINK,		a->l_link != b->l_link);
+	diff |= LINK_DIFF(LINK_NETNSID,	a->l_link_netnsid != b->l_link_netnsid);
 	diff |= LINK_DIFF(TXQLEN,	a->l_txqlen != b->l_txqlen);
 	diff |= LINK_DIFF(WEIGHT,	a->l_weight != b->l_weight);
 	diff |= LINK_DIFF(MASTER,	a->l_master != b->l_master);
@@ -1294,6 +1305,9 @@ int rtnl_link_fill_info(struct nl_msg *msg, struct rtnl_link *link)
 	if (link->ce_mask & LINK_ATTR_LINK)
 		NLA_PUT_U32(msg, IFLA_LINK, link->l_link);
 
+	if (link->ce_mask & LINK_ATTR_LINK_NETNSID)
+		NLA_PUT_S32(msg, IFLA_LINK_NETNSID, link->l_link_netnsid);
+
 	if (link->ce_mask & LINK_ATTR_MASTER)
 		NLA_PUT_U32(msg, IFLA_MASTER, link->l_master);
 
@@ -1986,6 +2000,21 @@ int rtnl_link_get_link(struct rtnl_link *link)
 	return link->l_link;
 }
 
+void rtnl_link_set_link_netnsid(struct rtnl_link *link, int32_t link_netnsid)
+{
+	link->l_link_netnsid = link_netnsid;
+	link->ce_mask |= LINK_ATTR_LINK_NETNSID;
+}
+
+int rtnl_link_get_link_netnsid(const struct rtnl_link *link, int32_t *out_link_netnsid)
+{
+	if (!(link->ce_mask & LINK_ATTR_LINK_NETNSID))
+		return -NLE_INVAL;
+
+	*out_link_netnsid = link->l_link_netnsid;
+	return 0;
+}
+
 /**
  * Set master link of link object
  * @arg link		Link object
diff --git a/libnl-route-3.sym b/libnl-route-3.sym
index f55a162..c6a7832 100644
--- a/libnl-route-3.sym
+++ b/libnl-route-3.sym
@@ -875,10 +875,12 @@ global:
 
 libnl_3_2_27 {
 global:
+	rtnl_link_get_link_netnsid;
 	rtnl_link_ipvlan_alloc;
 	rtnl_link_is_ipvlan;
 	rtnl_link_ipvlan_mode2str;
 	rtnl_link_ipvlan_str2mode;
 	rtnl_link_ipvlan_set_mode;
 	rtnl_link_ipvlan_get_mode;
+	rtnl_link_set_link_netnsid;
 } libnl_3_2_26;
-- 
2.4.3




More information about the libnl mailing list