From: Daniel Lezcano Date: Sun, 22 Mar 2009 21:52:17 +0000 (+0100) Subject: set mtu for netdev X-Git-Tag: lxc_0_6_1~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75d09f83b82f35a610f4922e06ad897692062fab;p=thirdparty%2Flxc.git 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: Daniel Lezcano --- diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c index c45f232a9..058e1ad25 100644 --- a/src/lxc/lxc_conf.c +++ b/src/lxc/lxc_conf.c @@ -1388,14 +1388,7 @@ static int instanciate_veth(const char *directory, const char *file, pid_t pid) 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 (lxc_configure_veth(veth1, veth2, bridge, mtu)) { + if (lxc_configure_veth(veth1, veth2, bridge)) { lxc_log_error("failed to create %s-%s/%s", veth1, veth2, bridge); goto out; } @@ -1416,6 +1409,23 @@ static int instanciate_veth(const char *directory, const char *file, pid_t pid) 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 (device_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 { char path[MAXPATHLEN], *strindex = NULL, *peer = NULL; char link[IFNAMSIZ]; + char strmtu[MAXMTULEN]; int ifindex, ret = -1; if (!asprintf(&peer, "%s~%d", file, pid)) { diff --git a/src/lxc/network.c b/src/lxc/network.c index d987147ad..92b1ef4cc 100644 --- a/src/lxc/network.c +++ b/src/lxc/network.c @@ -220,6 +220,54 @@ out: 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) { return device_set_flag(name, IFF_UP); @@ -281,7 +329,7 @@ out: 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 nlmsg *nlmsg = NULL, *answer = NULL; @@ -344,10 +392,6 @@ int veth_create(const char *name1, const char *name2, const int mtu) if (nla_put_string(nlmsg, IFLA_IFNAME, name1)) goto out; - if (mtu) - if (nla_put_u32(nlmsg, IFLA_MTU, mtu)) - goto out; - if (netlink_transaction(&nlh, nlmsg, answer)) goto out; @@ -694,11 +738,10 @@ int bridge_detach(const char *bridge, const char *ifname) return bridge_add_del_interface(bridge, ifname, 1); } -int lxc_configure_veth(const char *veth1, const char *veth2, - const char *bridge, const int mtu) +int lxc_configure_veth(const char *veth1, const char *veth2, const char *bridge) { int err = -1; - if (veth_create(veth1, veth2, mtu)) { + if (veth_create(veth1, veth2)) { fprintf(stderr, "failed to create veth interfaces %s/%s\n", veth1, veth2); return -1; diff --git a/src/lxc/network.h b/src/lxc/network.h index 86f163b23..cb65003c0 100644 --- a/src/lxc/network.h +++ b/src/lxc/network.h @@ -32,7 +32,7 @@ extern int lxc_configure_macvlan(const char *link, const char *peer); * Create a veth pair virtual device */ 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 @@ -64,10 +64,15 @@ extern int device_down(const char *name); */ 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 */ -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