[CRIU] [PATCH v2 2/6] net: split do_rtm_link_req into two parts

Tycho Andersen tycho.andersen at canonical.com
Thu Oct 13 08:15:41 PDT 2016


For macvlan we need to restore in different ways depending on whether we're
inside or outside a user namespace. We want to share the code that does the
building of the base request, so let's split it out into a populate()
function.

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 criu/net.c | 46 ++++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/criu/net.c b/criu/net.c
index 698d54c..b5f9818 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -831,34 +831,33 @@ struct newlink_req {
 	char buf[1024];
 };
 
-static int do_rtm_link_req(int msg_type, NetDeviceEntry *nde, int nlsk,
+static int populate_newlink_req(struct newlink_req *req, int msg_type, NetDeviceEntry *nde,
 		int (*link_info)(NetDeviceEntry *, struct newlink_req *))
 {
-	struct newlink_req req;
-
-	memset(&req, 0, sizeof(req));
+	memset(req, 0, sizeof(*req));
 
-	req.h.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
-	req.h.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK|NLM_F_CREATE;
-	req.h.nlmsg_type = msg_type;
-	req.h.nlmsg_seq = CR_NLMSG_SEQ;
-	req.i.ifi_family = AF_PACKET;
+	req->h.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
+	req->h.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK|NLM_F_CREATE;
+	req->h.nlmsg_type = msg_type;
+	req->h.nlmsg_seq = CR_NLMSG_SEQ;
+	req->i.ifi_family = AF_PACKET;
 	/*
 	 * SETLINK is called for external devices which may
 	 * have ifindex changed. Thus configure them by their
 	 * name only.
 	 */
 	if (msg_type == RTM_NEWLINK)
-		req.i.ifi_index = nde->ifindex;
-	req.i.ifi_flags = nde->flags;
+		req->i.ifi_index = nde->ifindex;
+	req->i.ifi_flags = nde->flags;
 
-	addattr_l(&req.h, sizeof(req), IFLA_IFNAME, nde->name, strlen(nde->name));
-	addattr_l(&req.h, sizeof(req), IFLA_MTU, &nde->mtu, sizeof(nde->mtu));
+
+	addattr_l(&req->h, sizeof(*req), IFLA_IFNAME, nde->name, strlen(nde->name));
+	addattr_l(&req->h, sizeof(*req), IFLA_MTU, &nde->mtu, sizeof(nde->mtu));
 
 	if (nde->has_address) {
 		pr_debug("Restore ll addr (%02x:../%d) for device\n",
 				(int)nde->address.data[0], (int)nde->address.len);
-		addattr_l(&req.h, sizeof(req), IFLA_ADDRESS,
+		addattr_l(&req->h, sizeof(*req), IFLA_ADDRESS,
 				nde->address.data, nde->address.len);
 	}
 
@@ -866,16 +865,27 @@ static int do_rtm_link_req(int msg_type, NetDeviceEntry *nde, int nlsk,
 		struct rtattr *linkinfo;
 		int ret;
 
-		linkinfo = NLMSG_TAIL(&req.h);
-		addattr_l(&req.h, sizeof(req), IFLA_LINKINFO, NULL, 0);
+		linkinfo = NLMSG_TAIL(&req->h);
+		addattr_l(&req->h, sizeof(*req), IFLA_LINKINFO, NULL, 0);
 
-		ret = link_info(nde, &req);
+		ret = link_info(nde, req);
 		if (ret < 0)
 			return ret;
 
-		linkinfo->rta_len = (void *)NLMSG_TAIL(&req.h) - (void *)linkinfo;
+		linkinfo->rta_len = (void *)NLMSG_TAIL(&req->h) - (void *)linkinfo;
 	}
 
+	return 0;
+}
+
+static int do_rtm_link_req(int msg_type, NetDeviceEntry *nde, int nlsk,
+		int (*link_info)(NetDeviceEntry *, struct newlink_req *))
+{
+	struct newlink_req req;
+
+	if (populate_newlink_req(&req, msg_type, nde, link_info) < 0)
+		return -1;
+
 	return do_rtnl_req(nlsk, &req, req.h.nlmsg_len, restore_link_cb, NULL, NULL);
 }
 
-- 
2.7.4



More information about the CRIU mailing list