Commit 75d09f83 by Daniel Lezcano

set mtu for netdev

When setting the mtu size at the veth creation, the mtu is only set on one side of the veth tunnel, the one attached to the bridge. I changed a little the code and added the device_set_mtu function so it is called after the veth has been created on both side. That moves the mtu veth specific code inside the veth function creation. Hopefully this code could be reused later for different future network configuration (eg. ip tunnel). The mtu option will be simply ignored in case of macvlan network configuration because the macvlan network device inherit the mtu of the physical link. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 442cbbe6
...@@ -1388,14 +1388,7 @@ static int instanciate_veth(const char *directory, const char *file, pid_t pid) ...@@ -1388,14 +1388,7 @@ static int instanciate_veth(const char *directory, const char *file, pid_t pid)
goto out; goto out;
} }
if (!read_info(path, "mtu", strmtu, MAXMTULEN)) { if (lxc_configure_veth(veth1, veth2, bridge)) {
if (sscanf(strmtu, "%u", &mtu) < 1) {
lxc_log_error("invalid mtu size '%d'", mtu);
goto out;
}
}
if (lxc_configure_veth(veth1, veth2, bridge, mtu)) {
lxc_log_error("failed to create %s-%s/%s", veth1, veth2, bridge); lxc_log_error("failed to create %s-%s/%s", veth1, veth2, bridge);
goto out; goto out;
} }
...@@ -1416,6 +1409,23 @@ static int instanciate_veth(const char *directory, const char *file, pid_t pid) ...@@ -1416,6 +1409,23 @@ static int instanciate_veth(const char *directory, const char *file, pid_t pid)
goto out; goto out;
} }
if (!read_info(path, "mtu", strmtu, MAXMTULEN)) {
if (sscanf(strmtu, "%u", &mtu) < 1) {
lxc_log_error("invalid mtu size '%d'", mtu);
goto out;
}
if (device_set_mtu(veth1, mtu)) {
lxc_log_error("failed to set mtu for '%s'", veth1);
goto out;
}
if (device_set_mtu(veth2, mtu)) {
lxc_log_error("failed to set mtu for '%s'", veth1);
goto out;
}
}
if (!read_info(path, "up", strindex, sizeof(strindex))) { if (!read_info(path, "up", strindex, sizeof(strindex))) {
if (device_up(veth1)) { if (device_up(veth1)) {
lxc_log_error("failed to set %s up", veth1); lxc_log_error("failed to set %s up", veth1);
...@@ -1435,6 +1445,7 @@ static int instanciate_macvlan(const char *directory, const char *file, pid_t pi ...@@ -1435,6 +1445,7 @@ static int instanciate_macvlan(const char *directory, const char *file, pid_t pi
{ {
char path[MAXPATHLEN], *strindex = NULL, *peer = NULL; char path[MAXPATHLEN], *strindex = NULL, *peer = NULL;
char link[IFNAMSIZ]; char link[IFNAMSIZ];
char strmtu[MAXMTULEN];
int ifindex, ret = -1; int ifindex, ret = -1;
if (!asprintf(&peer, "%s~%d", file, pid)) { if (!asprintf(&peer, "%s~%d", file, pid)) {
......
...@@ -220,6 +220,54 @@ out: ...@@ -220,6 +220,54 @@ out:
return err; return err;
} }
extern int device_set_mtu(const char *name, int mtu)
{
struct nl_handler nlh;
struct nlmsg *nlmsg = NULL, *answer = NULL;
struct link_req *link_req;
int index, len, err = -1;
if (netlink_open(&nlh, NETLINK_ROUTE))
return -1;
len = strlen(name);
if (len == 1 || len > IFNAMSIZ)
goto out;
nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
if (!nlmsg)
goto out;
answer = nlmsg_alloc(NLMSG_GOOD_SIZE);
if (!answer)
goto out;
index = if_nametoindex(name);
if (!index)
goto out;
link_req = (struct link_req *)nlmsg;
link_req->ifinfomsg.ifi_family = AF_UNSPEC;
link_req->ifinfomsg.ifi_index = index;
nlmsg->nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
nlmsg->nlmsghdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
nlmsg->nlmsghdr.nlmsg_type = RTM_NEWLINK;
if (nla_put_u32(nlmsg, IFLA_MTU, mtu))
goto out;
err = netlink_transaction(&nlh, nlmsg, answer);
if (err < 0)
goto out;
err = 0;
out:
netlink_close(&nlh);
nlmsg_free(nlmsg);
nlmsg_free(answer);
return err;
}
int device_up(const char *name) int device_up(const char *name)
{ {
return device_set_flag(name, IFF_UP); return device_set_flag(name, IFF_UP);
...@@ -281,7 +329,7 @@ out: ...@@ -281,7 +329,7 @@ out:
return err; return err;
} }
int veth_create(const char *name1, const char *name2, const int mtu) int veth_create(const char *name1, const char *name2)
{ {
struct nl_handler nlh; struct nl_handler nlh;
struct nlmsg *nlmsg = NULL, *answer = NULL; struct nlmsg *nlmsg = NULL, *answer = NULL;
...@@ -344,10 +392,6 @@ int veth_create(const char *name1, const char *name2, const int mtu) ...@@ -344,10 +392,6 @@ int veth_create(const char *name1, const char *name2, const int mtu)
if (nla_put_string(nlmsg, IFLA_IFNAME, name1)) if (nla_put_string(nlmsg, IFLA_IFNAME, name1))
goto out; goto out;
if (mtu)
if (nla_put_u32(nlmsg, IFLA_MTU, mtu))
goto out;
if (netlink_transaction(&nlh, nlmsg, answer)) if (netlink_transaction(&nlh, nlmsg, answer))
goto out; goto out;
...@@ -694,11 +738,10 @@ int bridge_detach(const char *bridge, const char *ifname) ...@@ -694,11 +738,10 @@ int bridge_detach(const char *bridge, const char *ifname)
return bridge_add_del_interface(bridge, ifname, 1); return bridge_add_del_interface(bridge, ifname, 1);
} }
int lxc_configure_veth(const char *veth1, const char *veth2, int lxc_configure_veth(const char *veth1, const char *veth2, const char *bridge)
const char *bridge, const int mtu)
{ {
int err = -1; int err = -1;
if (veth_create(veth1, veth2, mtu)) { if (veth_create(veth1, veth2)) {
fprintf(stderr, "failed to create veth interfaces %s/%s\n", fprintf(stderr, "failed to create veth interfaces %s/%s\n",
veth1, veth2); veth1, veth2);
return -1; return -1;
......
...@@ -32,7 +32,7 @@ extern int lxc_configure_macvlan(const char *link, const char *peer); ...@@ -32,7 +32,7 @@ extern int lxc_configure_macvlan(const char *link, const char *peer);
* Create a veth pair virtual device * Create a veth pair virtual device
*/ */
extern int lxc_configure_veth(const char *veth1, const char *veth2, extern int lxc_configure_veth(const char *veth1, const char *veth2,
const char *bridge, const int mtu); const char *bridge);
/* /*
* Convert a string mac address to a socket structure * Convert a string mac address to a socket structure
...@@ -65,9 +65,14 @@ extern int device_down(const char *name); ...@@ -65,9 +65,14 @@ extern int device_down(const char *name);
extern int device_rename(const char *oldname, const char *newname); extern int device_rename(const char *oldname, const char *newname);
/* /*
* Change the mtu size for the specified device
*/
extern int device_set_mtu(const char *name, int mtu);
/*
* Create a veth network device * Create a veth network device
*/ */
extern int veth_create(const char *name1, const char *name2, const int mtu); extern int veth_create(const char *name1, const char *name2);
/* /*
* Create a macvlan network device * Create a macvlan network device
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment