From 467d776b150844849c3b6872dc0215e4ea04130c Mon Sep 17 00:00:00 2001 From: Archana Shinde Date: Mon, 10 Oct 2016 22:37:10 +0000 Subject: [PATCH] Add ability to change interface name while setting up an interface. Signed-off-by: Archana Shinde --- src/net.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/net.h | 1 + src/parse.c | 4 ++++ 3 files changed, 48 insertions(+) diff --git a/src/net.c b/src/net.c index d255fb7..dd90dba 100644 --- a/src/net.c +++ b/src/net.c @@ -567,6 +567,44 @@ static int hyper_cleanup_route(struct rtnl_handle *rth, struct hyper_route *rt) return 0; } +static int hyper_set_interface_name(struct rtnl_handle *rth, + int ifindex, + char *new_device_name) +{ + struct { + struct nlmsghdr n; + struct ifinfomsg i; + char buf[1024]; + } req; + + if (ifindex < 0 || !new_device_name) { + return -1; + } + + memset(&req, 0, sizeof(req)); + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); + req.n.nlmsg_flags = NLM_F_REQUEST; + req.n.nlmsg_type = RTM_SETLINK; + + req.i.ifi_family = AF_UNSPEC; + req.i.ifi_change = 0xFFFFFFFF; + req.i.ifi_index = ifindex; + + if (addattr_l(&req.n, sizeof(req), IFLA_IFNAME, + new_device_name, + strlen(new_device_name) + 1)) { + fprintf(stderr, "setup attr failed\n"); + return -1; + } + + if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) { + perror("rtnl_talk failed"); + return -1; + } + + return 0; +} + static int hyper_setup_interface(struct rtnl_handle *rth, struct hyper_interface *iface) { @@ -621,6 +659,11 @@ static int hyper_setup_interface(struct rtnl_handle *rth, return -1; } + if (iface->new_device_name && strcmp(iface->new_device_name, iface->device)) { + fprintf(stdout, "Setting interface name to %s\n", iface->new_device_name); + hyper_set_interface_name(rth, ifindex, iface->new_device_name); + } + if (hyper_up_nic(rth, ifindex) < 0) { fprintf(stderr, "up device %d failed\n", ifindex); return -1; diff --git a/src/net.h b/src/net.h index 43524b2..8ebdd36 100644 --- a/src/net.h +++ b/src/net.h @@ -30,6 +30,7 @@ struct hyper_interface { char *device; char *ipaddr; char *mask; + char *new_device_name; }; struct hyper_route { diff --git a/src/parse.c b/src/parse.c index 4fb73a2..54f9f3b 100644 --- a/src/parse.c +++ b/src/parse.c @@ -773,6 +773,9 @@ static int hyper_parse_interface(struct hyper_interface *iface, } else if (json_token_streq(json, &toks[i], "netMask")) { iface->mask = (json_token_str(json, &toks[++i])); fprintf(stdout, "net mask is %s\n", iface->mask); + } else if (json_token_streq(json, &toks[i], "newDeviceName")) { + iface->new_device_name = (json_token_str(json, &toks[++i])); + fprintf(stdout, "new interface name is %s\n", iface->new_device_name); } else { fprintf(stderr, "get unknown section %s in interfaces\n", json_token_str(json, &toks[i])); @@ -786,6 +789,7 @@ fail: free(iface->device); free(iface->ipaddr); free(iface->mask); + free(iface->new_device_name); return -1; }