From: Daniel Lezcano Date: Mon, 7 Mar 2011 01:08:47 +0000 (+0100) Subject: factor out networking configuration code X-Git-Tag: lxc-0.7.5~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b84f58b9fb720fa951031e2311cdf8fef8a46a28;p=thirdparty%2Flxc.git factor out networking configuration code Change the name of the functions and factor some of them. Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 0fe8e2621..326e714b8 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -1241,7 +1241,7 @@ static int setup_netdev(struct lxc_netdev *netdev) netdev->link : "eth%d"; /* rename the interface name */ - err = lxc_device_rename(ifname, netdev->name); + err = lxc_netdev_rename_by_name(ifname, netdev->name); if (err) { ERROR("failed to rename %s->%s : %s", ifname, netdev->name, strerror(-err)); @@ -1425,7 +1425,7 @@ static int instanciate_veth(struct lxc_handler *handler, struct lxc_netdev *netd return 0; out_delete: - lxc_device_delete(veth1); + lxc_netdev_delete_by_name(veth1); return -1; } @@ -1458,7 +1458,7 @@ static int instanciate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n netdev->ifindex = if_nametoindex(peer); if (!netdev->ifindex) { ERROR("failed to retrieve the index for %s", peer); - lxc_device_delete(peer); + lxc_netdev_delete_by_name(peer); return -1; } @@ -1498,7 +1498,7 @@ static int instanciate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd netdev->ifindex = if_nametoindex(peer); if (!netdev->ifindex) { ERROR("failed to retrieve the ifindex for %s", peer); - lxc_device_delete(peer); + lxc_netdev_delete_by_name(peer); return -1; } @@ -1579,7 +1579,7 @@ void lxc_delete_network(struct lxc_list *network) lxc_list_for_each(iterator, network) { netdev = iterator->elem; if (netdev->ifindex > 0 && netdev->type != LXC_NET_PHYS) - lxc_device_delete_index(netdev->ifindex); + lxc_netdev_delete_by_index(netdev->ifindex); } } diff --git a/src/lxc/network.c b/src/lxc/network.c index a17296894..4ffafd706 100644 --- a/src/lxc/network.c +++ b/src/lxc/network.c @@ -122,22 +122,17 @@ out: return err; } -int lxc_device_delete(const char *name) +int lxc_netdev_delete_by_index(int ifindex) { struct nl_handler nlh; struct nlmsg *nlmsg = NULL, *answer = NULL; struct link_req *link_req; - int index, len, err; + int err; err = netlink_open(&nlh, NETLINK_ROUTE); if (err) return err; - err = -EINVAL; - len = strlen(name); - if (len == 1 || len > IFNAMSIZ) - goto out; - err = -ENOMEM; nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); if (!nlmsg) @@ -147,21 +142,13 @@ int lxc_device_delete(const char *name) if (!answer) goto out; - err = -EINVAL; - 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; + link_req->ifinfomsg.ifi_index = ifindex; nlmsg->nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nlmsg->nlmsghdr.nlmsg_flags = NLM_F_ACK|NLM_F_REQUEST; nlmsg->nlmsghdr.nlmsg_type = RTM_DELLINK; - if (nla_put_string(nlmsg, IFLA_IFNAME, name)) - goto out; - err = netlink_transaction(&nlh, nlmsg, answer); out: netlink_close(&nlh); @@ -170,17 +157,32 @@ out: return err; } -int lxc_device_delete_index(int ifindex) +int lxc_netdev_delete_by_name(const char *name) +{ + int index; + + index = if_nametoindex(name); + if (!index) + return -EINVAL; + + return lxc_netdev_delete_by_index(index); +} + +int lxc_netdev_rename_by_index(int ifindex, const char *newname) { struct nl_handler nlh; struct nlmsg *nlmsg = NULL, *answer = NULL; struct link_req *link_req; - int err; + int len, err; err = netlink_open(&nlh, NETLINK_ROUTE); if (err) return err; + len = strlen(newname); + if (len == 1 || len > IFNAMSIZ) + goto out; + err = -ENOMEM; nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); if (!nlmsg) @@ -195,7 +197,10 @@ int lxc_device_delete_index(int ifindex) link_req->ifinfomsg.ifi_index = ifindex; nlmsg->nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nlmsg->nlmsghdr.nlmsg_flags = NLM_F_ACK|NLM_F_REQUEST; - nlmsg->nlmsghdr.nlmsg_type = RTM_DELLINK; + nlmsg->nlmsghdr.nlmsg_type = RTM_NEWLINK; + + if (nla_put_string(nlmsg, IFLA_IFNAME, newname)) + goto out; err = netlink_transaction(&nlh, nlmsg, answer); out: @@ -205,6 +210,21 @@ out: return err; } +int lxc_netdev_rename_by_name(const char *oldname, const char *newname) +{ + int len, index; + + len = strlen(oldname); + if (len == 1 || len > IFNAMSIZ) + return -EINVAL; + + index = if_nametoindex(oldname); + if (!index) + return -EINVAL; + + return lxc_netdev_rename_by_index(index, newname); +} + static int device_set_flag(const char *name, int flag) { struct nl_handler nlh; @@ -310,58 +330,6 @@ int lxc_device_down(const char *name) return device_set_flag(name, 0); } -int lxc_device_rename(const char *oldname, const char *newname) -{ - struct nl_handler nlh; - struct nlmsg *nlmsg = NULL, *answer = NULL; - struct link_req *link_req; - int index, len, err; - - err = netlink_open(&nlh, NETLINK_ROUTE); - if (err) - return err; - - err = -EINVAL; - len = strlen(oldname); - if (len == 1 || len > IFNAMSIZ) - goto out; - - len = strlen(newname); - if (len == 1 || len > IFNAMSIZ) - goto out; - - err = -ENOMEM; - nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); - if (!nlmsg) - goto out; - - answer = nlmsg_alloc(NLMSG_GOOD_SIZE); - if (!answer) - goto out; - - err = -EINVAL; - index = if_nametoindex(oldname); - 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_ACK|NLM_F_REQUEST; - nlmsg->nlmsghdr.nlmsg_type = RTM_NEWLINK; - - if (nla_put_string(nlmsg, IFLA_IFNAME, newname)) - goto out; - - err = netlink_transaction(&nlh, nlmsg, answer); -out: - netlink_close(&nlh); - nlmsg_free(answer); - nlmsg_free(nlmsg); - return err; -} - int lxc_veth_create(const char *name1, const char *name2) { struct nl_handler nlh; diff --git a/src/lxc/network.h b/src/lxc/network.h index fa7c6ff4e..c9237bd66 100644 --- a/src/lxc/network.h +++ b/src/lxc/network.h @@ -36,12 +36,14 @@ extern int lxc_device_move(int ifindex, pid_t pid); /* * Delete a network device */ -extern int lxc_device_delete(const char *name); +extern int lxc_netdev_delete_by_name(const char *name); +extern int lxc_netdev_delete_by_index(int ifindex); /* - * Delete a network device by the index + * Change the device name */ -extern int lxc_device_delete_index(int ifindex); +extern int lxc_netdev_rename_by_name(const char *oldname, const char *newname); +extern int lxc_netdev_rename_by_index(int ifindex, const char *newname); /* * Set the device network up @@ -53,11 +55,6 @@ extern int lxc_device_up(const char *name); */ extern int lxc_device_down(const char *name); -/* - * Change the device name - */ -extern int lxc_device_rename(const char *oldname, const char *newname); - /* * Change the mtu size for the specified device */