Skip to content

Commit

Permalink
net: allow c/r of empty bridges in the container
Browse files Browse the repository at this point in the history
Implementing c/r of bridges with slaves shouldn't be too hard (viz. the
comment), but this is all I need to for right now.

v2: remove extra debug statement
v3: * remember to close fd in dump_bridge
    * use "known" buffer length and snprintf for spath in dump_bridge
    * change brace style

Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
  • Loading branch information
Tycho Andersen authored and xemul committed Nov 12, 2015
1 parent b67bde8 commit 8a95be0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
2 changes: 2 additions & 0 deletions include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ static inline bool dir_dots(struct dirent *de)
return !strcmp(de->d_name, ".") || !strcmp(de->d_name, "..");
}

extern int is_empty_dir(int dirfd);

/*
* Size of buffer to carry the worst case or /proc/self/fd/N
* path. Since fd is an integer, we can easily estimate one :)
Expand Down
22 changes: 5 additions & 17 deletions mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,31 +1357,19 @@ static int fusectl_dump(struct mount_info *pm)
static int dump_empty_fs(struct mount_info *pm)
{
int fd, ret = -1;
struct dirent *de;
DIR *fdir = NULL;
fd = open_mountpoint(pm);

if (fd < 0)
return -1;

fdir = fdopendir(fd);
if (fdir == NULL) {
close(fd);
ret = is_empty_dir(fd);
close(fd);
if (ret < 0) {
pr_err("%s isn't empty\n", pm->fstype->name);
return -1;
}

while ((de = readdir(fdir))) {
if (dir_dots(de))
continue;

pr_err("%s isn't empty: %s\n", pm->fstype->name, de->d_name);
goto out;
}

ret = 0;
out:
closedir(fdir);
return ret;
return ret ? 0 : -1;
}

/*
Expand Down
51 changes: 51 additions & 0 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,41 @@ static int dump_unknown_device(struct ifinfomsg *ifi, char *kind,
return -1;
}

static int dump_bridge(NetDeviceEntry *nde, struct cr_imgset *imgset)
{
char spath[IFNAMSIZ + 16]; /* len("class/net//brif") + 1 for null */
int ret, fd;

ret = snprintf(spath, sizeof(spath), "class/net/%s/brif", nde->name);
if (ret < 0 || ret >= sizeof(spath))
return -1;

/* Let's only allow dumping empty bridges for now. To do a full bridge
* restore, we need to make sure the bridge and slaves are restored in
* the right order and attached correctly. It looks like the veth code
* supports this, but we need some way to do ordering.
*/
fd = openat(ns_sysfs_fd, spath, O_DIRECTORY, 0);
if (fd < 0) {
pr_perror("opening %s failed", spath);
return -1;
}

ret = is_empty_dir(fd);
close(fd);
if (ret < 0) {
pr_perror("problem testing %s for emptiness", spath);
return -1;
}

if (!ret) {
pr_err("dumping bridges with attached slaves not supported currently\n");
return -1;
}

return write_netdev_img(nde, imgset);
}

static int dump_one_ethernet(struct ifinfomsg *ifi, char *kind,
struct rtattr **tb, struct cr_imgset *fds)
{
Expand All @@ -240,6 +275,8 @@ static int dump_one_ethernet(struct ifinfomsg *ifi, char *kind,
return dump_one_netdev(ND_TYPE__VETH, ifi, tb, fds, NULL);
if (!strcmp(kind, "tun"))
return dump_one_netdev(ND_TYPE__TUN, ifi, tb, fds, dump_tun_link);
if (!strcmp(kind, "bridge"))
return dump_one_netdev(ND_TYPE__BRIDGE, ifi, tb, fds, dump_bridge);

return dump_unknown_device(ifi, kind, tb, fds);
}
Expand Down Expand Up @@ -465,6 +502,17 @@ static int venet_link_info(NetDeviceEntry *nde, struct newlink_req *req)
return 0;
}

static int bridge_link_info(NetDeviceEntry *nde, struct newlink_req *req)
{
struct rtattr *bridge_data;

bridge_data = NLMSG_TAIL(&req->h);
addattr_l(&req->h, sizeof(*req), IFLA_INFO_KIND, "bridge", sizeof("bridge"));
bridge_data->rta_len = (void *)NLMSG_TAIL(&req->h) - (void *)bridge_data;

return 0;
}

static int restore_link(NetDeviceEntry *nde, int nlsk)
{
pr_info("Restoring link %s type %d\n", nde->name, nde->type);
Expand All @@ -479,6 +527,9 @@ static int restore_link(NetDeviceEntry *nde, int nlsk)
return restore_one_link(nde, nlsk, veth_link_info);
case ND_TYPE__TUN:
return restore_one_tun(nde, nlsk);
case ND_TYPE__BRIDGE:
return restore_one_link(nde, nlsk, bridge_link_info);

default:
pr_err("Unsupported link type %d\n", nde->type);
break;
Expand Down
1 change: 1 addition & 0 deletions protobuf/netdev.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum nd_type {
* Virtuozzo specific device.
*/
VENET = 5;
BRIDGE = 6;
}

message net_device_entry {
Expand Down
23 changes: 23 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,29 @@ int is_root_user()
return 1;
}

int is_empty_dir(int dirfd)
{
int ret = 0;
DIR *fdir = NULL;
struct dirent *de;

fdir = fdopendir(dirfd);
if (!fdir)
return -1;

while ((de = readdir(fdir))) {
if (dir_dots(de))
continue;

goto out;
}

ret = 1;
out:
closedir(fdir);
return ret;
}

int vaddr_to_pfn(unsigned long vaddr, u64 *pfn)
{
int fd, ret = -1;
Expand Down

0 comments on commit 8a95be0

Please sign in to comment.