diff --git a/src/net.c b/src/net.c index f119de77..cd0d99df 100644 --- a/src/net.c +++ b/src/net.c @@ -405,19 +405,20 @@ static int hyper_setup_route(struct rtnl_handle *rth, return 0; } -static int hyper_set_interface_name(struct rtnl_handle *rth, +static int hyper_set_interface_attr(struct rtnl_handle *rth, int ifindex, - char *new_device_name) + void *data, + int len, + int type) { struct { - struct nlmsghdr n; - struct ifinfomsg i; - char buf[1024]; - } req; + struct nlmsghdr n; + struct ifinfomsg i; + char buf[1024]; + } req; - if (ifindex < 0 || !new_device_name) { + if (!rth || ifindex < 0) return -1; - } memset(&req, 0, sizeof(req)); req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); @@ -428,14 +429,14 @@ static int hyper_set_interface_name(struct rtnl_handle *rth, 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)) { + if (addattr_l(&req.n, sizeof(req), type, + data, + len)) { fprintf(stderr, "setup attr failed\n"); return -1; } - if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) { + if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0){ perror("rtnl_talk failed"); return -1; } @@ -443,6 +444,31 @@ static int hyper_set_interface_name(struct rtnl_handle *rth, return 0; } +static int hyper_set_interface_name(struct rtnl_handle *rth, + int ifindex, + char *new_device_name) +{ + if ( !rth || ifindex < 0 || !new_device_name) { + return -1; + } + return hyper_set_interface_attr(rth, ifindex, + new_device_name, + strlen(new_device_name)+1, + IFLA_IFNAME); +} + +static int hyper_set_interface_mtu(struct rtnl_handle *rth, + int ifindex, + unsigned int mtu) +{ + if (!rth || ifindex < 0) { + return -1; + } + return hyper_set_interface_attr(rth, ifindex, &mtu, + sizeof(mtu), + IFLA_MTU); +} + static int hyper_setup_interface(struct rtnl_handle *rth, struct hyper_interface *iface) { @@ -505,6 +531,15 @@ static int hyper_setup_interface(struct rtnl_handle *rth, hyper_set_interface_name(rth, ifindex, iface->new_device_name); } + if (iface->mtu > 0) { + fprintf(stdout, "Setting interface MTU to %d\n", iface->mtu); + if (hyper_set_interface_mtu(rth, ifindex, iface->mtu) < 0) { + fprintf(stderr, "set mtu failed for interface %s\n", + iface->device); + return -1; + } + } + 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 fb1f4079..b6571d8e 100644 --- a/src/net.h +++ b/src/net.h @@ -30,6 +30,7 @@ struct hyper_interface { char *device; struct list_head ipaddresses; char *new_device_name; + unsigned int mtu; }; struct hyper_route { diff --git a/src/parse.c b/src/parse.c index fec94b27..4111bc68 100644 --- a/src/parse.c +++ b/src/parse.c @@ -856,6 +856,9 @@ static int hyper_parse_interface(struct hyper_interface *iface, } ipaddr_oldf->mask = (json_token_str(json, &toks[++i])); dbg_pr(stdout, "net mask is %s\n", ipaddr_oldf->mask); + } else if (json_token_streq(json, &toks[i], "mtu")) { + iface->mtu = (json_token_int(json, &toks[++i])); + dbg_pr(stdout, "mtu is %d\n", iface->mtu); } else { hyper_print_unknown_key(json, &toks[i]); goto fail;