[Patch v2 4/4] veth: use nl_object_clone() to deep copy rtnl_link object

Cong Wang xiyou.wangcong at gmail.com
Fri Mar 28 21:08:18 EDT 2014


Currently we use memcpy() to copy the peer rtnl_link for veth device,
this is wrong, we should do deep copy by calling nl_object_clone()
recursively. We should be careful and need to make sure we only call
it once.

Signed-off-by: Cong Wang <xiyou.wangcong at gmail.com>
---
 lib/route/link/veth.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/lib/route/link/veth.c b/lib/route/link/veth.c
index c6a4cc7..817a268 100644
--- a/lib/route/link/veth.c
+++ b/lib/route/link/veth.c
@@ -98,19 +98,19 @@ static void veth_dump_details(struct rtnl_link *link, struct nl_dump_params *p)
 
 static int veth_clone(struct rtnl_link *dst, struct rtnl_link *src)
 {
-	struct rtnl_link *dst_peer , *src_peer = src->l_info;
-	int err;
-
-	dst_peer = dst->l_info = rtnl_link_alloc();
-	if (!dst_peer || !src_peer)
-		return -NLE_NOMEM;
-	if ((err = rtnl_link_set_type(dst, "veth")) < 0) {
-		rtnl_link_put(dst_peer);
-		return err;
+	struct rtnl_link *dst_peer = NULL, *src_peer = src->l_info;
+
+	/* we are calling nl_object_clone() recursively, this should
+	 * happen only once */
+	if (src_peer) {
+		src_peer->l_info = NULL;
+		dst_peer = (struct rtnl_link *)nl_object_clone(OBJ_CAST(src_peer));
+		if (!dst_peer)
+			return -NLE_NOMEM;
+		src_peer->l_info = src;
+		dst_peer->l_info = dst;
 	}
-
-	memcpy(dst_peer, src_peer, sizeof(struct rtnl_link));
-
+	dst->l_info = dst_peer;
 	return 0;
 }
 
-- 
1.7.11.7




More information about the libnl mailing list