]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
factor out networking configuration code
authorDaniel Lezcano <daniel.lezcano@free.fr>
Mon, 7 Mar 2011 01:08:47 +0000 (02:08 +0100)
committerDaniel Lezcano <dlezcano@users.sourceforge.net>
Mon, 7 Mar 2011 01:08:47 +0000 (02:08 +0100)
Change the name of the functions and factor some of them.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/conf.c
src/lxc/network.c
src/lxc/network.h

index 0fe8e26211106d4e902fd7550222e1628fef45c8..326e714b8bb90bf6b4e2bf796804b692cb1d683b 100644 (file)
@@ -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);
        }
 }
 
index a17296894b2fae01962c6bd8968d3cfcecd7772e..4ffafd706b4be8d174f97698a84f772e2f836a05 100644 (file)
@@ -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;
index fa7c6ff4ec870f04ca4fc9c0ba9a3582a797087d..c9237bd66cdc76d2f6b8fce4b7fb365c33c8c914 100644 (file)
@@ -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
  */