Skip to content

Commit

Permalink
veth: grab a reference for rtnl_link_veth_get_peer()
Browse files Browse the repository at this point in the history
So that users could keep a refcount for the peer.
The capability trick is from Thomas Haller.

Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Thomas Haller <thaller@redhat.com>
  • Loading branch information
congwang authored and thom311 committed Apr 30, 2014
1 parent f55ef93 commit 0ba7e66
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/netlink/utils.h
Expand Up @@ -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

/**
* 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().
*/
NL_CAPABILITY_ROUTE_LINK_VETH_GET_PEER_OWN_REFERENCE = 2,
#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)
};
Expand Down
5 changes: 3 additions & 2 deletions lib/route/link/veth.c
Expand Up @@ -167,7 +167,7 @@ static int veth_alloc(struct rtnl_link *link)

static void veth_free(struct rtnl_link *link)
{
struct rtnl_link *peer = rtnl_link_veth_get_peer(link);
struct rtnl_link *peer = link->l_info;
if (peer) {
link->l_info = NULL;
/* avoid calling this recursively */
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -278,7 +279,7 @@ int rtnl_link_veth_add(struct nl_sock *sock, const char *name,

if (!(link = rtnl_link_veth_alloc()))
return -NLE_NOMEM;
peer = rtnl_link_veth_get_peer(link);
peer = link->l_info;

if (name && peer_name) {
rtnl_link_set_name(link, name);
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.c
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/test-create-veth.c
Expand Up @@ -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);
Expand Down

0 comments on commit 0ba7e66

Please sign in to comment.