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

Submitted by Andrei Vagin on March 22, 2017, 12:28 a.m.

Details

Message ID 1490142533-20654-8-git-send-email-avagin@openvz.org
State New
Series "Dump and restore internal veth devices"
Headers show

Commit Message

Andrei Vagin March 22, 2017, 12:28 a.m.
From: Andrei Vagin <avagin@virtuozzo.com>

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

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

Patch hide | download patch | download mbox

diff --git a/criu/net.c b/criu/net.c
index 779b166..4abaaa5 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -929,8 +929,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));
 
@@ -974,7 +977,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;
 
@@ -984,13 +987,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);
@@ -998,15 +1001,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
@@ -1037,7 +1039,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;
@@ -1060,7 +1062,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;
@@ -1076,7 +1078,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;
 
@@ -1102,7 +1104,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;
@@ -1156,7 +1158,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,
@@ -1191,7 +1193,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) {
@@ -1207,7 +1209,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);
 
@@ -1216,15 +1218,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;
@@ -1233,13 +1235,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;
 
@@ -1257,7 +1259,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;
@@ -1827,7 +1829,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);
 
@@ -2497,7 +2499,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;
@@ -2606,7 +2608,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);