Commit edfb9a57 by Daniel Lezcano Committed by Daniel Lezcano

factor out networking configuration code

Change the name of the functions and factor some of them. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent daa5d3fd
...@@ -1241,7 +1241,7 @@ static int setup_netdev(struct lxc_netdev *netdev) ...@@ -1241,7 +1241,7 @@ static int setup_netdev(struct lxc_netdev *netdev)
netdev->link : "eth%d"; netdev->link : "eth%d";
/* rename the interface name */ /* rename the interface name */
err = lxc_device_rename(ifname, netdev->name); err = lxc_netdev_rename_by_name(ifname, netdev->name);
if (err) { if (err) {
ERROR("failed to rename %s->%s : %s", ifname, netdev->name, ERROR("failed to rename %s->%s : %s", ifname, netdev->name,
strerror(-err)); strerror(-err));
...@@ -1425,7 +1425,7 @@ static int instanciate_veth(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -1425,7 +1425,7 @@ static int instanciate_veth(struct lxc_handler *handler, struct lxc_netdev *netd
return 0; return 0;
out_delete: out_delete:
lxc_device_delete(veth1); lxc_netdev_delete_by_name(veth1);
return -1; return -1;
} }
...@@ -1458,7 +1458,7 @@ static int instanciate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n ...@@ -1458,7 +1458,7 @@ static int instanciate_macvlan(struct lxc_handler *handler, struct lxc_netdev *n
netdev->ifindex = if_nametoindex(peer); netdev->ifindex = if_nametoindex(peer);
if (!netdev->ifindex) { if (!netdev->ifindex) {
ERROR("failed to retrieve the index for %s", peer); ERROR("failed to retrieve the index for %s", peer);
lxc_device_delete(peer); lxc_netdev_delete_by_name(peer);
return -1; return -1;
} }
...@@ -1498,7 +1498,7 @@ static int instanciate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd ...@@ -1498,7 +1498,7 @@ static int instanciate_vlan(struct lxc_handler *handler, struct lxc_netdev *netd
netdev->ifindex = if_nametoindex(peer); netdev->ifindex = if_nametoindex(peer);
if (!netdev->ifindex) { if (!netdev->ifindex) {
ERROR("failed to retrieve the ifindex for %s", peer); ERROR("failed to retrieve the ifindex for %s", peer);
lxc_device_delete(peer); lxc_netdev_delete_by_name(peer);
return -1; return -1;
} }
...@@ -1579,7 +1579,7 @@ void lxc_delete_network(struct lxc_list *network) ...@@ -1579,7 +1579,7 @@ void lxc_delete_network(struct lxc_list *network)
lxc_list_for_each(iterator, network) { lxc_list_for_each(iterator, network) {
netdev = iterator->elem; netdev = iterator->elem;
if (netdev->ifindex > 0 && netdev->type != LXC_NET_PHYS) if (netdev->ifindex > 0 && netdev->type != LXC_NET_PHYS)
lxc_device_delete_index(netdev->ifindex); lxc_netdev_delete_by_index(netdev->ifindex);
} }
} }
......
...@@ -122,22 +122,17 @@ out: ...@@ -122,22 +122,17 @@ out:
return err; return err;
} }
int lxc_device_delete(const char *name) int lxc_netdev_delete_by_index(int ifindex)
{ {
struct nl_handler nlh; struct nl_handler nlh;
struct nlmsg *nlmsg = NULL, *answer = NULL; struct nlmsg *nlmsg = NULL, *answer = NULL;
struct link_req *link_req; struct link_req *link_req;
int index, len, err; int err;
err = netlink_open(&nlh, NETLINK_ROUTE); err = netlink_open(&nlh, NETLINK_ROUTE);
if (err) if (err)
return err; return err;
err = -EINVAL;
len = strlen(name);
if (len == 1 || len > IFNAMSIZ)
goto out;
err = -ENOMEM; err = -ENOMEM;
nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
if (!nlmsg) if (!nlmsg)
...@@ -147,21 +142,13 @@ int lxc_device_delete(const char *name) ...@@ -147,21 +142,13 @@ int lxc_device_delete(const char *name)
if (!answer) if (!answer)
goto out; goto out;
err = -EINVAL;
index = if_nametoindex(name);
if (!index)
goto out;
link_req = (struct link_req *)nlmsg; link_req = (struct link_req *)nlmsg;
link_req->ifinfomsg.ifi_family = AF_UNSPEC; 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_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
nlmsg->nlmsghdr.nlmsg_flags = NLM_F_ACK|NLM_F_REQUEST; nlmsg->nlmsghdr.nlmsg_flags = NLM_F_ACK|NLM_F_REQUEST;
nlmsg->nlmsghdr.nlmsg_type = RTM_DELLINK; nlmsg->nlmsghdr.nlmsg_type = RTM_DELLINK;
if (nla_put_string(nlmsg, IFLA_IFNAME, name))
goto out;
err = netlink_transaction(&nlh, nlmsg, answer); err = netlink_transaction(&nlh, nlmsg, answer);
out: out:
netlink_close(&nlh); netlink_close(&nlh);
...@@ -170,17 +157,32 @@ out: ...@@ -170,17 +157,32 @@ out:
return err; 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 nl_handler nlh;
struct nlmsg *nlmsg = NULL, *answer = NULL; struct nlmsg *nlmsg = NULL, *answer = NULL;
struct link_req *link_req; struct link_req *link_req;
int err; int len, err;
err = netlink_open(&nlh, NETLINK_ROUTE); err = netlink_open(&nlh, NETLINK_ROUTE);
if (err) if (err)
return err; return err;
len = strlen(newname);
if (len == 1 || len > IFNAMSIZ)
goto out;
err = -ENOMEM; err = -ENOMEM;
nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
if (!nlmsg) if (!nlmsg)
...@@ -195,7 +197,10 @@ int lxc_device_delete_index(int ifindex) ...@@ -195,7 +197,10 @@ int lxc_device_delete_index(int ifindex)
link_req->ifinfomsg.ifi_index = ifindex; link_req->ifinfomsg.ifi_index = ifindex;
nlmsg->nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); nlmsg->nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
nlmsg->nlmsghdr.nlmsg_flags = NLM_F_ACK|NLM_F_REQUEST; 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); err = netlink_transaction(&nlh, nlmsg, answer);
out: out:
...@@ -205,6 +210,21 @@ out: ...@@ -205,6 +210,21 @@ out:
return err; 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) static int device_set_flag(const char *name, int flag)
{ {
struct nl_handler nlh; struct nl_handler nlh;
...@@ -310,58 +330,6 @@ int lxc_device_down(const char *name) ...@@ -310,58 +330,6 @@ int lxc_device_down(const char *name)
return device_set_flag(name, 0); 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) int lxc_veth_create(const char *name1, const char *name2)
{ {
struct nl_handler nlh; struct nl_handler nlh;
......
...@@ -36,12 +36,14 @@ extern int lxc_device_move(int ifindex, pid_t pid); ...@@ -36,12 +36,14 @@ extern int lxc_device_move(int ifindex, pid_t pid);
/* /*
* Delete a network device * 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 * Set the device network up
...@@ -54,11 +56,6 @@ extern int lxc_device_up(const char *name); ...@@ -54,11 +56,6 @@ extern int lxc_device_up(const char *name);
extern int lxc_device_down(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 * Change the mtu size for the specified device
*/ */
extern int lxc_device_set_mtu(const char *name, int mtu); extern int lxc_device_set_mtu(const char *name, int mtu);
......
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