Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 8484d7e

Browse files
authored
Merge pull request #261 from amshinde/set-mtu-upstream
Add ability to set MTU for network interface
2 parents 1ca70ae + 37dc1eb commit 8484d7e

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

src/net.c

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,20 @@ static int hyper_setup_route(struct rtnl_handle *rth,
405405
return 0;
406406
}
407407

408-
static int hyper_set_interface_name(struct rtnl_handle *rth,
408+
static int hyper_set_interface_attr(struct rtnl_handle *rth,
409409
int ifindex,
410-
char *new_device_name)
410+
void *data,
411+
int len,
412+
int type)
411413
{
412414
struct {
413-
struct nlmsghdr n;
414-
struct ifinfomsg i;
415-
char buf[1024];
416-
} req;
415+
struct nlmsghdr n;
416+
struct ifinfomsg i;
417+
char buf[1024];
418+
} req;
417419

418-
if (ifindex < 0 || !new_device_name) {
420+
if (!rth || ifindex < 0)
419421
return -1;
420-
}
421422

422423
memset(&req, 0, sizeof(req));
423424
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
@@ -428,21 +429,46 @@ static int hyper_set_interface_name(struct rtnl_handle *rth,
428429
req.i.ifi_change = 0xFFFFFFFF;
429430
req.i.ifi_index = ifindex;
430431

431-
if (addattr_l(&req.n, sizeof(req), IFLA_IFNAME,
432-
new_device_name,
433-
strlen(new_device_name) + 1)) {
432+
if (addattr_l(&req.n, sizeof(req), type,
433+
data,
434+
len)) {
434435
fprintf(stderr, "setup attr failed\n");
435436
return -1;
436437
}
437438

438-
if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) {
439+
if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0){
439440
perror("rtnl_talk failed");
440441
return -1;
441442
}
442443

443444
return 0;
444445
}
445446

447+
static int hyper_set_interface_name(struct rtnl_handle *rth,
448+
int ifindex,
449+
char *new_device_name)
450+
{
451+
if ( !rth || ifindex < 0 || !new_device_name) {
452+
return -1;
453+
}
454+
return hyper_set_interface_attr(rth, ifindex,
455+
new_device_name,
456+
strlen(new_device_name)+1,
457+
IFLA_IFNAME);
458+
}
459+
460+
static int hyper_set_interface_mtu(struct rtnl_handle *rth,
461+
int ifindex,
462+
unsigned int mtu)
463+
{
464+
if (!rth || ifindex < 0) {
465+
return -1;
466+
}
467+
return hyper_set_interface_attr(rth, ifindex, &mtu,
468+
sizeof(mtu),
469+
IFLA_MTU);
470+
}
471+
446472
static int hyper_setup_interface(struct rtnl_handle *rth,
447473
struct hyper_interface *iface)
448474
{
@@ -505,6 +531,15 @@ static int hyper_setup_interface(struct rtnl_handle *rth,
505531
hyper_set_interface_name(rth, ifindex, iface->new_device_name);
506532
}
507533

534+
if (iface->mtu > 0) {
535+
fprintf(stdout, "Setting interface MTU to %d\n", iface->mtu);
536+
if (hyper_set_interface_mtu(rth, ifindex, iface->mtu) < 0) {
537+
fprintf(stderr, "set mtu failed for interface %s\n",
538+
iface->device);
539+
return -1;
540+
}
541+
}
542+
508543
if (hyper_up_nic(rth, ifindex) < 0) {
509544
fprintf(stderr, "up device %d failed\n", ifindex);
510545
return -1;

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct hyper_interface {
3030
char *device;
3131
struct list_head ipaddresses;
3232
char *new_device_name;
33+
unsigned int mtu;
3334
};
3435

3536
struct hyper_route {

src/parse.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,9 @@ static int hyper_parse_interface(struct hyper_interface *iface,
856856
}
857857
ipaddr_oldf->mask = (json_token_str(json, &toks[++i]));
858858
dbg_pr(stdout, "net mask is %s\n", ipaddr_oldf->mask);
859+
} else if (json_token_streq(json, &toks[i], "mtu")) {
860+
iface->mtu = (json_token_int(json, &toks[++i]));
861+
dbg_pr(stdout, "mtu is %d\n", iface->mtu);
859862
} else {
860863
hyper_print_unknown_key(json, &toks[i]);
861864
goto fail;

0 commit comments

Comments
 (0)