[CRIU] [PATCH 07/12] net: give ns_id to link_info functions

Andrei Vagin avagin at openvz.org
Tue Feb 28 15:53:03 PST 2017


From: Andrei Vagin <avagin at virtuozzo.com>

It will be used to restore links in different net namesapces.

Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 criu/net.c | 62 ++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 32 insertions(+), 30 deletions(-)

diff --git a/criu/net.c b/criu/net.c
index e893c0c..f889403 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -921,8 +921,11 @@ struct newlink_extras {
 	int target_netns;	/* IFLA_NET_NS_FD */
 };
 
-static int populate_newlink_req(struct newlink_req *req, int msg_type, NetDeviceEntry *nde,
-		int (*link_info)(NetDeviceEntry *, struct newlink_req *), struct newlink_extras *extras)
+typedef int (*link_info_t)(struct ns_id *ns, NetDeviceEntry *, struct newlink_req *);
+
+static int populate_newlink_req(struct ns_id *ns, struct newlink_req *req,
+			int msg_type, NetDeviceEntry *nde,
+			link_info_t link_info, struct newlink_extras *extras)
 {
 	memset(req, 0, sizeof(*req));
 
@@ -966,7 +969,7 @@ static int populate_newlink_req(struct newlink_req *req, int msg_type, NetDevice
 		linkinfo = NLMSG_TAIL(&req->h);
 		addattr_l(&req->h, sizeof(*req), IFLA_LINKINFO, NULL, 0);
 
-		ret = link_info(nde, req);
+		ret = link_info(ns, nde, req);
 		if (ret < 0)
 			return ret;
 
@@ -976,13 +979,13 @@ static int populate_newlink_req(struct newlink_req *req, int msg_type, NetDevice
 	return 0;
 }
 
-static int do_rtm_link_req(int msg_type, NetDeviceEntry *nde, int nlsk,
-		int (*link_info)(NetDeviceEntry *, struct newlink_req *),
-		struct newlink_extras *extras)
+static int do_rtm_link_req(int msg_type,
+			NetDeviceEntry *nde, int nlsk, struct ns_id *ns,
+			link_info_t link_info, struct newlink_extras *extras)
 {
 	struct newlink_req req;
 
-	if (populate_newlink_req(&req, msg_type, nde, link_info, extras) < 0)
+	if (populate_newlink_req(ns, &req, msg_type, nde, link_info, extras) < 0)
 		return -1;
 
 	return do_rtnl_req(nlsk, &req, req.h.nlmsg_len, restore_link_cb, NULL, NULL, NULL);
@@ -990,15 +993,14 @@ static int do_rtm_link_req(int msg_type, NetDeviceEntry *nde, int nlsk,
 
 int restore_link_parms(NetDeviceEntry *nde, int nlsk)
 {
-	return do_rtm_link_req(RTM_SETLINK, nde, nlsk, NULL, NULL);
+	return do_rtm_link_req(RTM_SETLINK, nde, nlsk, NULL, NULL, NULL);
 }
 
-static int restore_one_link(NetDeviceEntry *nde, int nlsk,
-		int (*link_info)(NetDeviceEntry *, struct newlink_req *),
-		struct newlink_extras *extras)
+static int restore_one_link(struct ns_id *ns, NetDeviceEntry *nde, int nlsk,
+			link_info_t link_info, struct newlink_extras *extras)
 {
 	pr_info("Restoring netdev %s idx %d\n", nde->name, nde->ifindex);
-	return do_rtm_link_req(RTM_NEWLINK, nde, nlsk, link_info, extras);
+	return do_rtm_link_req(RTM_NEWLINK, nde, nlsk, ns, link_info, extras);
 }
 
 #ifndef VETH_INFO_MAX
@@ -1029,7 +1031,7 @@ static void veth_peer_info(NetDeviceEntry *nde, struct newlink_req *req)
 	}
 }
 
-static int veth_link_info(NetDeviceEntry *nde, struct newlink_req *req)
+static int veth_link_info(struct ns_id *ns, NetDeviceEntry *nde, struct newlink_req *req)
 {
 	int ns_fd = get_service_fd(NS_FD_OFF);
 	struct rtattr *veth_data, *peer_data;
@@ -1052,7 +1054,7 @@ static int veth_link_info(NetDeviceEntry *nde, struct newlink_req *req)
 	return 0;
 }
 
-static int venet_link_info(NetDeviceEntry *nde, struct newlink_req *req)
+static int venet_link_info(struct ns_id *ns, NetDeviceEntry *nde, struct newlink_req *req)
 {
 	int ns_fd = get_service_fd(NS_FD_OFF);
 	struct rtattr *venet_data;
@@ -1068,7 +1070,7 @@ static int venet_link_info(NetDeviceEntry *nde, struct newlink_req *req)
 	return 0;
 }
 
-static int bridge_link_info(NetDeviceEntry *nde, struct newlink_req *req)
+static int bridge_link_info(struct ns_id *ns, NetDeviceEntry *nde, struct newlink_req *req)
 {
 	struct rtattr *bridge_data;
 
@@ -1094,7 +1096,7 @@ static int changeflags(int s, char *name, short flags)
 	return 0;
 }
 
-static int macvlan_link_info(NetDeviceEntry *nde, struct newlink_req *req)
+static int macvlan_link_info(struct ns_id *ns, NetDeviceEntry *nde, struct newlink_req *req)
 {
 	struct rtattr *macvlan_data;
 	MacvlanLinkEntry *macvlan = nde->macvlan;
@@ -1148,7 +1150,7 @@ out:
 	return ret;
 }
 
-static int restore_one_macvlan(NetDeviceEntry *nde, int nlsk, int criu_nlsk)
+static int restore_one_macvlan(struct ns_id *ns, NetDeviceEntry *nde, int nlsk, int criu_nlsk)
 {
 	struct newlink_extras extras = {
 		.link = -1,
@@ -1183,7 +1185,7 @@ static int restore_one_macvlan(NetDeviceEntry *nde, int nlsk, int criu_nlsk)
 	{
 		struct newlink_req req;
 
-		if (populate_newlink_req(&req, RTM_NEWLINK, nde, macvlan_link_info, &extras) < 0)
+		if (populate_newlink_req(ns, &req, RTM_NEWLINK, nde, macvlan_link_info, &extras) < 0)
 			goto out;
 
 		if (userns_call(userns_restore_one_link, 0, &req, sizeof(req), my_netns) < 0) {
@@ -1199,7 +1201,7 @@ out:
 	return ret;
 }
 
-static int restore_link(NetDeviceEntry *nde, int nlsk, int criu_nlsk)
+static int restore_link(struct ns_id *ns, NetDeviceEntry *nde, int nlsk, int criu_nlsk)
 {
 	pr_info("Restoring link %s type %d\n", nde->name, nde->type);
 
@@ -1208,15 +1210,15 @@ static int restore_link(NetDeviceEntry *nde, int nlsk, int criu_nlsk)
 	case ND_TYPE__EXTLINK:  /* see comment in images/netdev.proto */
 		return restore_link_parms(nde, nlsk);
 	case ND_TYPE__VENET:
-		return restore_one_link(nde, nlsk, venet_link_info, NULL);
+		return restore_one_link(ns, nde, nlsk, venet_link_info, NULL);
 	case ND_TYPE__VETH:
-		return restore_one_link(nde, nlsk, veth_link_info, NULL);
+		return restore_one_link(ns, nde, nlsk, veth_link_info, NULL);
 	case ND_TYPE__TUN:
 		return restore_one_tun(nde, nlsk);
 	case ND_TYPE__BRIDGE:
-		return restore_one_link(nde, nlsk, bridge_link_info, NULL);
+		return restore_one_link(ns, nde, nlsk, bridge_link_info, NULL);
 	case ND_TYPE__MACVLAN:
-		return restore_one_macvlan(nde, nlsk, criu_nlsk);
+		return restore_one_macvlan(ns, nde, nlsk, criu_nlsk);
 	default:
 		pr_err("Unsupported link type %d\n", nde->type);
 		break;
@@ -1225,13 +1227,13 @@ static int restore_link(NetDeviceEntry *nde, int nlsk, int criu_nlsk)
 	return -1;
 }
 
-static int restore_links(int pid, NetnsEntry **netns)
+static int restore_links(struct ns_id *ns, NetnsEntry **netns)
 {
-	int nlsk, criu_nlsk = -1, ret = -1;
+	int nlsk, criu_nlsk = -1, ret = -1, id = ns->id;
 	struct cr_img *img;
 	NetDeviceEntry *nde;
 
-	img = open_image(CR_FD_NETDEV, O_RSTR, pid);
+	img = open_image(CR_FD_NETDEV, O_RSTR, id);
 	if (!img)
 		return -1;
 
@@ -1249,7 +1251,7 @@ static int restore_links(int pid, NetnsEntry **netns)
 		if (ret <= 0)
 			break;
 
-		ret = restore_link(nde, nlsk, criu_nlsk);
+		ret = restore_link(ns, nde, nlsk, criu_nlsk);
 		if (ret) {
 			pr_err("Can't restore link\n");
 			goto exit;
@@ -1812,7 +1814,7 @@ static int prepare_net_ns(struct ns_id *ns)
 		if (!ret)
 			ret = restore_netns_ids(ns, netns);
 		if (!ret)
-			ret = restore_links(nsid, &netns);
+			ret = restore_links(ns, &netns);
 		if (netns)
 			netns_entry__free_unpacked(netns, NULL);
 
@@ -2469,7 +2471,7 @@ int net_get_nsid(int rtsk, int pid, int *nsid)
 }
 
 
-static int nsid_link_info(NetDeviceEntry *nde, struct newlink_req *req)
+static int nsid_link_info(struct ns_id *ns, NetDeviceEntry *nde, struct newlink_req *req)
 {
 	struct rtattr *veth_data, *peer_data;
 	struct ifinfomsg ifm;
@@ -2578,7 +2580,7 @@ int kerndat_link_nsid()
 		nde.has_peer_ifindex = true;
 		nde.has_peer_nsid = true;
 
-		ret = restore_one_link(&nde, sk, nsid_link_info, NULL);
+		ret = restore_one_link(NULL, &nde, sk, nsid_link_info, NULL);
 		if (ret) {
 			pr_err("Unable to create a veth pair: %d\n", ret);
 			exit(1);
-- 
2.7.4



More information about the CRIU mailing list