Commit 82d5ae15 by Daniel Lezcano

Use the configuration structure for the network

We don't want to use anymore the configuration directory, let's use the configuration structure. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 571e6ec8
...@@ -76,6 +76,7 @@ struct lxc_route6 { ...@@ -76,6 +76,7 @@ struct lxc_route6 {
*/ */
struct lxc_netdev { struct lxc_netdev {
int flags; int flags;
int ifindex;
char *ifname; char *ifname;
char *newname; char *newname;
char *hwaddr; char *hwaddr;
...@@ -168,9 +169,8 @@ extern int lxc_configure(const char *name, struct lxc_conf *conf); ...@@ -168,9 +169,8 @@ extern int lxc_configure(const char *name, struct lxc_conf *conf);
*/ */
extern int lxc_unconfigure(const char *name); extern int lxc_unconfigure(const char *name);
extern int conf_create_network(const char *name, pid_t pid); extern int lxc_create_network(struct lxc_list *networks);
extern int lxc_assign_network(struct lxc_list *networks, pid_t pid);
extern int conf_destroy_network(const char *name);
extern int lxc_create_tty(const char *name, struct lxc_tty_info *tty_info); extern int lxc_create_tty(const char *name, struct lxc_tty_info *tty_info);
extern void lxc_delete_tty(struct lxc_tty_info *tty_info); extern void lxc_delete_tty(struct lxc_tty_info *tty_info);
......
...@@ -114,6 +114,7 @@ static int config_network_type(const char *key, char *value, struct lxc_conf *lx ...@@ -114,6 +114,7 @@ static int config_network_type(const char *key, char *value, struct lxc_conf *lx
return -1; return -1;
} }
memset(netdev, 0, sizeof(*netdev));
lxc_list_init(&netdev->ipv4); lxc_list_init(&netdev->ipv4);
lxc_list_init(&netdev->ipv6); lxc_list_init(&netdev->ipv6);
lxc_list_init(&netdev->route4); lxc_list_init(&netdev->route4);
......
...@@ -81,31 +81,23 @@ struct ip_req { ...@@ -81,31 +81,23 @@ struct ip_req {
struct ifaddrmsg ifa; struct ifaddrmsg ifa;
}; };
int lxc_device_move(const char *name, pid_t pid) int lxc_device_move(int ifindex, pid_t pid)
{ {
struct nl_handler nlh; struct nl_handler nlh;
struct nlmsg *nlmsg = NULL; struct nlmsg *nlmsg = NULL;
struct link_req *link_req; struct link_req *link_req;
int index, len, err = -1; int len, err = -1;
if (netlink_open(&nlh, NETLINK_ROUTE)) if (netlink_open(&nlh, NETLINK_ROUTE))
return -1; return -1;
len = strlen(name);
if (len == 1 || len > IFNAMSIZ)
goto out;
nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
if (!nlmsg) if (!nlmsg)
goto out; goto out;
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_REQUEST|NLM_F_ACK; nlmsg->nlmsghdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
nlmsg->nlmsghdr.nlmsg_type = RTM_NEWLINK; nlmsg->nlmsghdr.nlmsg_type = RTM_NEWLINK;
...@@ -113,9 +105,6 @@ int lxc_device_move(const char *name, pid_t pid) ...@@ -113,9 +105,6 @@ int lxc_device_move(const char *name, pid_t pid)
if (nla_put_u32(nlmsg, IFLA_NET_NS_PID, pid)) if (nla_put_u32(nlmsg, IFLA_NET_NS_PID, pid))
goto out; goto out;
if (nla_put_string(nlmsg, IFLA_IFNAME, name))
goto out;
if (netlink_transaction(&nlh, nlmsg, nlmsg)) if (netlink_transaction(&nlh, nlmsg, nlmsg))
goto out; goto out;
...@@ -576,25 +565,17 @@ int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr) ...@@ -576,25 +565,17 @@ int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr)
return 0; return 0;
} }
int lxc_ip_addr_add(const char *ifname, const char *addr, int lxc_ip_addr_add(int ifindex, struct in_addr in_addr,
int prefix, const char *bcast) int prefix, struct in_addr in_bcast)
{ {
struct nl_handler nlh; struct nl_handler nlh;
struct in_addr in_addr;
/* struct in_addr in_bcast; */
struct nlmsg *nlmsg = NULL, *answer = NULL; struct nlmsg *nlmsg = NULL, *answer = NULL;
struct ip_req *ip_req; struct ip_req *ip_req;
int ifindex, err = -1; int err = -1;
if (netlink_open(&nlh, NETLINK_ROUTE)) if (netlink_open(&nlh, NETLINK_ROUTE))
return -1; return -1;
if (inet_pton(AF_INET, addr, (void *)&in_addr) < 0)
goto out;
/* if (inet_pton(AF_INET, bcast, (void *)&in_bcast) < 0) */
/* goto out; */
nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
if (!nlmsg) if (!nlmsg)
goto out; goto out;
...@@ -603,10 +584,6 @@ int lxc_ip_addr_add(const char *ifname, const char *addr, ...@@ -603,10 +584,6 @@ int lxc_ip_addr_add(const char *ifname, const char *addr,
if (!answer) if (!answer)
goto out; goto out;
ifindex = if_nametoindex(ifname);
if (!ifindex)
goto out;
ip_req = (struct ip_req *)nlmsg; ip_req = (struct ip_req *)nlmsg;
ip_req->nlmsg.nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); ip_req->nlmsg.nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
ip_req->nlmsg.nlmsghdr.nlmsg_flags = ip_req->nlmsg.nlmsghdr.nlmsg_flags =
...@@ -639,26 +616,17 @@ out: ...@@ -639,26 +616,17 @@ out:
return err; return err;
} }
int lxc_ip6_addr_add(const char *ifname, const char *addr, int lxc_ip6_addr_add(int ifindex, struct in6_addr in6_addr,
int prefix, const char *bcast) int prefix, struct in6_addr in6_bcast)
{ {
struct nl_handler nlh; struct nl_handler nlh;
struct in6_addr in6_addr;
/* struct in6_addr in6_bcast; */
struct nlmsg *nlmsg = NULL, *answer = NULL; struct nlmsg *nlmsg = NULL, *answer = NULL;
struct ip_req *ip_req; struct ip_req *ip_req;
int ifindex, err = -1; int err = -1;
if (netlink_open(&nlh, NETLINK_ROUTE)) if (netlink_open(&nlh, NETLINK_ROUTE))
return -1; return -1;
if (inet_pton(AF_INET6, addr, (void *)&in6_addr) < 0)
goto out;
/* if (inet_pton(AF_INET6, bcast, (void *)&in6_bcast) < 0) */
/* goto out; */
nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE); nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
if (!nlmsg) if (!nlmsg)
goto out; goto out;
...@@ -667,10 +635,6 @@ int lxc_ip6_addr_add(const char *ifname, const char *addr, ...@@ -667,10 +635,6 @@ int lxc_ip6_addr_add(const char *ifname, const char *addr,
if (!answer) if (!answer)
goto out; goto out;
ifindex = if_nametoindex(ifname);
if (!ifindex)
goto out;
ip_req = (struct ip_req *)nlmsg; ip_req = (struct ip_req *)nlmsg;
ip_req->nlmsg.nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); ip_req->nlmsg.nlmsghdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
ip_req->nlmsg.nlmsghdr.nlmsg_flags = ip_req->nlmsg.nlmsghdr.nlmsg_flags =
......
...@@ -31,7 +31,7 @@ extern int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr); ...@@ -31,7 +31,7 @@ extern int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr);
/* /*
* Move a device between namespaces * Move a device between namespaces
*/ */
extern int lxc_device_move(const char *name, pid_t pid); extern int lxc_device_move(int ifindex, pid_t pid);
/* /*
* Delete a network device * Delete a network device
...@@ -81,11 +81,11 @@ extern int lxc_ip_forward_off(const char *name, int family); ...@@ -81,11 +81,11 @@ extern int lxc_ip_forward_off(const char *name, int family);
/* /*
* Set ip address * Set ip address
*/ */
extern int lxc_ip_addr_add(const char *ifname, const char *addr, extern int lxc_ip_addr_add(int ifindex, struct in_addr addr,
int prefix, const char *bcast); int prefix, struct in_addr bcast);
extern int lxc_ip6_addr_add(const char *ifname, const char *addr, extern int lxc_ip6_addr_add(int ifindex, struct in6_addr addr,
int prefix, const char *bcast); int prefix, struct in6_addr bcast);
/* /*
* Attach an interface to the bridge * Attach an interface to the bridge
......
...@@ -425,9 +425,19 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) ...@@ -425,9 +425,19 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
} }
clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
if (conf_has_network(name)) if (!lxc_list_empty(&handler->conf.networks)) {
clone_flags |= CLONE_NEWNET; clone_flags |= CLONE_NEWNET;
/* that should be done before the clone because we will
* fill the netdev index and use them in the child
*/
if (lxc_create_network(&handler->conf.networks)) {
ERROR("failed to create the network");
goto out_close;
}
}
/* Create a process in a new set of namespaces */ /* Create a process in a new set of namespaces */
handler->pid = lxc_clone(do_start, &start_arg, clone_flags); handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
if (handler->pid < 0) { if (handler->pid < 0) {
...@@ -447,10 +457,11 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) ...@@ -447,10 +457,11 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
goto out_abort; goto out_abort;
/* Create the network configuration */ /* Create the network configuration */
if (clone_flags & CLONE_NEWNET && if (clone_flags & CLONE_NEWNET) {
conf_create_network(name, handler->pid)) { if (lxc_assign_network(&handler->conf.networks, handler->pid)) {
ERROR("failed to create the configured network"); ERROR("failed to create the configured network");
goto out_abort; goto out_abort;
}
} }
/* Tell the child to continue its initialization */ /* Tell the child to continue its initialization */
......
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