[PATCH v2] veth: grab a reference for rtnl_link_veth_get_peer()

Cong Wang xiyou.wangcong at gmail.com
Wed Apr 23 17:28:28 PDT 2014


Capabilit trick is from Thomas Haller.

Signed-off-by: Cong Wang <xiyou.wangcong at gmail.com>
---
 include/netlink/utils.h  | 7 +++++++
 lib/route/link/veth.c    | 1 +
 lib/utils.c              | 2 +-
 tests/test-create-veth.c | 1 +
 4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/netlink/utils.h b/include/netlink/utils.h
index 2094bb4..6c4cb52 100644
--- a/include/netlink/utils.h
+++ b/include/netlink/utils.h
@@ -90,6 +90,13 @@ enum {
 	NL_CAPABILITY_ROUTE_BUILD_MSG_SET_SCOPE         = 1,
 #define NL_CAPABILITY_ROUTE_BUILD_MSG_SET_SCOPE NL_CAPABILITY_ROUTE_BUILD_MSG_SET_SCOPE
 
+	NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE = 2,
+	/**
+	 * rtnl_link_veth_get_peer() now returns a reference that is owned by the
+	 * caller and must be released by the caller with rtnl_link_put().
+	 */
+#define NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE
+
 	__NL_CAPABILITY_MAX
 #define NL_CAPABILITY_MAX                               (__NL_CAPABILITY_MAX - 1)
 };
diff --git a/lib/route/link/veth.c b/lib/route/link/veth.c
index 19c84f8..3b6a89c 100644
--- a/lib/route/link/veth.c
+++ b/lib/route/link/veth.c
@@ -232,6 +232,7 @@ struct rtnl_link *rtnl_link_veth_alloc(void)
 struct rtnl_link *rtnl_link_veth_get_peer(struct rtnl_link *link)
 {
 	IS_VETH_LINK_ASSERT(link);
+	nl_object_get(OBJ_CAST(link->l_info));
 	return link->l_info;
 }
 
diff --git a/lib/utils.c b/lib/utils.c
index 49ff120..ac36493 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1145,7 +1145,7 @@ int nl_has_capability (int capability)
 			_NL_SETV((i), 3, (v3)) | _NL_SETV((i), 7, (v7)) )
 		_NL_SET(0,
 			NL_CAPABILITY_ROUTE_BUILD_MSG_SET_SCOPE,
-			0,
+			NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE,
 			0,
 			0,
 			0,
diff --git a/tests/test-create-veth.c b/tests/test-create-veth.c
index c0d9145..db5ab8b 100644
--- a/tests/test-create-veth.c
+++ b/tests/test-create-veth.c
@@ -33,6 +33,7 @@ int main(int argc, char *argv[])
 		return err;
 	}
 	printf("peer is %s\n", rtnl_link_get_name(peer));
+	rtnl_link_put(peer);
 	rtnl_link_put(link);
 #endif
 	nl_close(sk);
-- 
1.8.3.1




More information about the libnl mailing list