/* * The dbfile has lines of the format: * user type bridge nicname */ bool get_nic_if_avail(int fd, char *me, char *pid, char *intype, char *br, int allowed, char **nicname) { off_t len, slen; struct stat sb; char *buf = NULL, *newline; int ret, count = 0; cull_entries(fd, me, intype, br); fstat(fd, &sb); len = sb.st_size; if (len != 0) { buf = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { fprintf(stderr, "Failed to create mapping\n"); return false; } count = count_entries(buf, len, me, intype, br); if (count >= allowed) return false; } get_new_nicname(nicname, br, pid); /* me ' ' intype ' ' br ' ' *nicname + '\n' + '\0' */ slen = strlen(me) + strlen(intype) + strlen(br) + strlen(*nicname) + 5; newline = alloca(slen); ret = snprintf(newline, slen, "%s %s %s %s\n", me, intype, br, *nicname); if (ret < 0 || ret >= slen) { if (lxc_netdev_delete_by_name(*nicname) != 0) fprintf(stderr, "Error unlinking %s!\n", *nicname); return false; } if (len) munmap(buf, len); if (ftruncate(fd, len + slen)) fprintf(stderr, "Failed to set new file size\n"); buf = mmap(NULL, len + slen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { fprintf(stderr, "Failed to create mapping after extending: error %d\n", errno); if (lxc_netdev_delete_by_name(*nicname) != 0) fprintf(stderr, "Error unlinking %s!\n", *nicname); return false; } strcpy(buf+len, newline); munmap(buf, len+slen); return true; }
static bool create_nic(char *nic, char *br, int pid, char **cnic) { char *veth1buf, *veth2buf; veth1buf = alloca(IFNAMSIZ); veth2buf = alloca(IFNAMSIZ); int ret, mtu; ret = snprintf(veth1buf, IFNAMSIZ, "%s", nic); if (ret < 0 || ret >= IFNAMSIZ) { fprintf(stderr, "host nic name too long\n"); return false; } /* create the nics */ if (instantiate_veth(veth1buf, &veth2buf) < 0) { fprintf(stderr, "Error creating veth tunnel\n"); return false; } if (strcmp(br, "none") != 0) { /* copy the bridge's mtu to both ends */ mtu = get_mtu(br); if (mtu != -1) { if (lxc_netdev_set_mtu(veth1buf, mtu) < 0 || lxc_netdev_set_mtu(veth2buf, mtu) < 0) { fprintf(stderr, "Failed setting mtu\n"); goto out_del; } } /* attach veth1 to bridge */ if (lxc_bridge_attach(lxcpath, lxcname, br, veth1buf) < 0) { fprintf(stderr, "Error attaching %s to %s\n", veth1buf, br); goto out_del; } } /* pass veth2 to target netns */ ret = lxc_netdev_move_by_name(veth2buf, pid, NULL); if (ret < 0) { fprintf(stderr, "Error moving %s to netns %d\n", veth2buf, pid); goto out_del; } *cnic = strdup(veth2buf); return true; out_del: lxc_netdev_delete_by_name(veth1buf); return false; }
bool create_nic(char *nic, char *br, char *pidstr) { #if ISTEST char path[200]; sprintf(path, "/tmp/lxcnettest/%s", nic); int fd = open(path, O_RDWR|O_CREAT, S_IWUSR | S_IRUSR); if (fd < 0) return false; close(fd); return true; #else // not yet implemented char *veth1buf, *veth2buf; veth1buf = alloca(IFNAMSIZ); veth2buf = alloca(IFNAMSIZ); int ret; int pid = atoi(pidstr); ret = snprintf(veth1buf, IFNAMSIZ, "%s", nic); if (ret < 0 || ret >= IFNAMSIZ) { fprintf(stderr, "nic name too long\n"); exit(1); } /* create the nics */ if (instanciate_veth(veth1buf, &veth2buf) < 0) { fprintf(stderr, "Error creating veth tunnel\n"); return false; } /* attach veth1 to bridge */ if (lxc_bridge_attach(br, veth1buf) < 0) { fprintf(stderr, "Error attaching %s to %s\n", veth1buf, br); goto out_del; } /* pass veth2 to target netns */ ret = lxc_netdev_move(veth2buf, pid); if (ret < 0) { fprintf(stderr, "Error moving %s to netns %d\n", veth2buf, pid); goto out_del; } return true; out_del: lxc_netdev_delete_by_name(veth1buf); exit(1); #endif }
static bool create_nic(char *nic, char *br, int pid, char **cnic) { char *veth1buf, *veth2buf; veth1buf = alloca(IFNAMSIZ); veth2buf = alloca(IFNAMSIZ); int ret; ret = snprintf(veth1buf, IFNAMSIZ, "%s", nic); if (ret < 0 || ret >= IFNAMSIZ) { fprintf(stderr, "host nic name too long\n"); return false; } /* create the nics */ if (instanciate_veth(veth1buf, &veth2buf) < 0) { fprintf(stderr, "Error creating veth tunnel\n"); return false; } /* attach veth1 to bridge */ if (lxc_bridge_attach(br, veth1buf) < 0) { fprintf(stderr, "Error attaching %s to %s\n", veth1buf, br); goto out_del; } /* pass veth2 to target netns */ ret = lxc_netdev_move_by_name(veth2buf, pid); if (ret < 0) { fprintf(stderr, "Error moving %s to netns %d\n", veth2buf, pid); goto out_del; } *cnic = strdup(veth2buf); return true; out_del: lxc_netdev_delete_by_name(veth1buf); return false; }
static int create_nic(char *nic, char *br, int pid, char **cnic) { char veth1buf[IFNAMSIZ], veth2buf[IFNAMSIZ]; int mtu, ret; ret = snprintf(veth1buf, IFNAMSIZ, "%s", nic); if (ret < 0 || ret >= IFNAMSIZ) { usernic_error("%s", "Could not create nic name\n"); return -1; } ret = snprintf(veth2buf, IFNAMSIZ, "%sp", veth1buf); if (ret < 0 || ret >= IFNAMSIZ) { usernic_error("%s\n", "Could not create nic name"); return -1; } /* create the nics */ ret = instantiate_veth(veth1buf, veth2buf); if (ret < 0) { usernic_error("%s", "Error creating veth tunnel\n"); return -1; } if (strcmp(br, "none")) { /* copy the bridge's mtu to both ends */ mtu = get_mtu(br); if (mtu > 0) { ret = lxc_netdev_set_mtu(veth1buf, mtu); if (ret < 0) { usernic_error("Failed to set mtu to %d on %s\n", mtu, veth1buf); goto out_del; } ret = lxc_netdev_set_mtu(veth2buf, mtu); if (ret < 0) { usernic_error("Failed to set mtu to %d on %s\n", mtu, veth2buf); goto out_del; } } /* attach veth1 to bridge */ ret = lxc_bridge_attach(br, veth1buf); if (ret < 0) { usernic_error("Error attaching %s to %s\n", veth1buf, br); goto out_del; } } /* pass veth2 to target netns */ ret = lxc_netdev_move_by_name(veth2buf, pid, NULL); if (ret < 0) { usernic_error("Error moving %s to network namespace of %d\n", veth2buf, pid); goto out_del; } *cnic = strdup(veth2buf); if (!*cnic) { usernic_error("Failed to copy string \"%s\"\n", veth2buf); return -1; } return 0; out_del: lxc_netdev_delete_by_name(veth1buf); return -1; }