X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev-pltap.c;h=62f23f3fda02321664eb89cb61fc3d3f7085916a;hb=f31282b9c152d5e993c8fe0068b7a4ed8c8d98c8;hp=c9252e50ef71e4a8993864e88caff001fd3812db;hpb=8dbbeaa2a927b5a0faca32e9bbc4236746adbb60;p=sliver-openvswitch.git diff --git a/lib/netdev-pltap.c b/lib/netdev-pltap.c index c9252e50e..62f23f3fd 100644 --- a/lib/netdev-pltap.c +++ b/lib/netdev-pltap.c @@ -48,21 +48,26 @@ VLOG_DEFINE_THIS_MODULE(netdev_pltap); struct netdev_dev_pltap { struct netdev_dev netdev_dev; char *real_name; - char *error; struct netdev_stats stats; + enum netdev_flags new_flags; enum netdev_flags flags; int fd; struct sockaddr_in local_addr; int local_netmask; bool valid_local_ip; bool valid_local_netmask; - bool finalized; + bool sync_flags_needed; + struct list sync_list; unsigned int change_seq; }; +static struct list sync_list; + struct netdev_pltap { struct netdev netdev; -} ; +}; + +static int af_inet_sock = -1; static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); @@ -71,9 +76,14 @@ static struct shash pltap_netdev_devs = SHASH_INITIALIZER(&pltap_netdev_devs); static int netdev_pltap_create(const struct netdev_class *, const char *, struct netdev_dev **); -static struct shash pltap_creating = SHASH_INITIALIZER(&pltap_creating); - static void netdev_pltap_update_seq(struct netdev_dev_pltap *); +static int get_flags(struct netdev_dev_pltap *dev, enum netdev_flags *flags); + +static bool +netdev_pltap_finalized(struct netdev_dev_pltap *dev) +{ + return dev->valid_local_ip && dev->valid_local_netmask; +} static bool is_pltap_class(const struct netdev_class *class) @@ -84,7 +94,7 @@ is_pltap_class(const struct netdev_class *class) static struct netdev_dev_pltap * netdev_dev_pltap_cast(const struct netdev_dev *netdev_dev) { - assert(is_pltap_class(netdev_dev_get_class(netdev_dev))); + ovs_assert(is_pltap_class(netdev_dev_get_class(netdev_dev))); return CONTAINER_OF(netdev_dev, struct netdev_dev_pltap, netdev_dev); } @@ -92,10 +102,29 @@ static struct netdev_pltap * netdev_pltap_cast(const struct netdev *netdev) { struct netdev_dev *netdev_dev = netdev_get_dev(netdev); - assert(is_pltap_class(netdev_dev_get_class(netdev_dev))); + ovs_assert(is_pltap_class(netdev_dev_get_class(netdev_dev))); return CONTAINER_OF(netdev, struct netdev_pltap, netdev); } +static void sync_needed(struct netdev_dev_pltap *dev) +{ + if (dev->sync_flags_needed) + return; + + dev->sync_flags_needed = true; + list_insert(&sync_list, &dev->sync_list); + +} + +static void sync_done(struct netdev_dev_pltap *dev) +{ + if (!dev->sync_flags_needed) + return; + + (void) list_remove(&dev->sync_list); + dev->sync_flags_needed = false; +} + static int netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *name, struct netdev_dev **netdev_devp) @@ -106,11 +135,12 @@ netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *nam netdev_dev = xzalloc(sizeof *netdev_dev); netdev_dev->real_name = xzalloc(IFNAMSIZ + 1); - netdev_dev->error = NULL; memset(&netdev_dev->local_addr, 0, sizeof(netdev_dev->local_addr)); netdev_dev->valid_local_ip = false; netdev_dev->valid_local_netmask = false; - netdev_dev->finalized = false; + netdev_dev->flags = 0; + netdev_dev->sync_flags_needed = false; + list_init(&netdev_dev->sync_list); /* Open tap device. */ @@ -146,6 +176,8 @@ netdev_pltap_destroy(struct netdev_dev *netdev_dev_) if (netdev_dev->fd != -1) close(netdev_dev->fd); + sync_done(netdev_dev); + shash_find_and_delete(&pltap_netdev_devs, netdev_dev_get_name(netdev_dev_)); free(netdev_dev); @@ -170,45 +202,52 @@ netdev_pltap_close(struct netdev *netdev_) free(netdev); } -static int -netdev_pltap_create_finalize(struct netdev_dev_pltap *dev) +static int vsys_transaction(const char *script, + const char **preply, char *format, ...) { + char *msg = NULL, *reply = NULL; + const size_t reply_size = 1024; int ifd = -1, ofd = -1, maxfd; - size_t bytes_to_write, bytes_to_read = 1024, + size_t bytes_to_write, bytes_to_read, bytes_written = 0, bytes_read = 0; int error = 0; - char *msg = NULL, *reply = NULL; + char *ofname = NULL, *ifname = NULL; + va_list args; - if (dev->finalized) - return 0; - if (!dev->valid_local_ip || !dev->valid_local_netmask) - return 0; - - ofd = open("/vsys/vif_up.out", O_RDONLY | O_NONBLOCK); + va_start(args, format); + msg = xvasprintf(format, args); + va_end(args); + reply = (char*)xmalloc(reply_size); + if (!msg || !reply) { + VLOG_ERR("Out of memory"); + error = ENOMEM; + goto cleanup; + } + + ofname = xasprintf("/vsys/%s.out", script); + ifname = xasprintf("/vsys/%s.in", script); + if (!ofname || !ifname) { + VLOG_ERR("Out of memory"); + error = ENOMEM; + goto cleanup; + } + + ofd = open(ofname, O_RDONLY | O_NONBLOCK); if (ofd < 0) { - VLOG_ERR("Cannot open vif_up.out: %s", strerror(errno)); + VLOG_ERR("Cannot open %s: %s", ofname, strerror(errno)); error = errno; goto cleanup; } - ifd = open("/vsys/vif_up.in", O_WRONLY | O_NONBLOCK); + ifd = open(ifname, O_WRONLY | O_NONBLOCK); if (ifd < 0) { - VLOG_ERR("Cannot open vif_up.in: %s", strerror(errno)); + VLOG_ERR("Cannot open %s: %s", ifname, strerror(errno)); error = errno; goto cleanup; } maxfd = (ifd < ofd) ? ofd : ifd; - msg = xasprintf("%s\n"IP_FMT"\n%d\n", - dev->real_name, - IP_ARGS(&dev->local_addr.sin_addr), - dev->local_netmask); - reply = (char*)xmalloc(bytes_to_read); - if (!msg || !reply) { - VLOG_ERR("Out of memory"); - error = ENOMEM; - goto cleanup; - } bytes_to_write = strlen(msg); + bytes_to_read = reply_size; while (bytes_to_write || bytes_to_read) { fd_set readset, writeset, errorset; @@ -236,7 +275,7 @@ netdev_pltap_create_finalize(struct netdev_dev_pltap *dev) ssize_t n = write(ifd, msg + bytes_written, bytes_to_write); if (n < 0) { if (errno != EAGAIN && errno != EINTR) { - VLOG_ERR("write on vif_up.in: %s", strerror(errno)); + VLOG_ERR("write on %s: %s", ifname, strerror(errno)); error = errno; goto cleanup; } @@ -251,7 +290,7 @@ netdev_pltap_create_finalize(struct netdev_dev_pltap *dev) ssize_t n = read(ofd, reply + bytes_read, bytes_to_read); if (n < 0) { if (errno != EAGAIN && errno != EINTR) { - VLOG_ERR("read on vif_up.out: %s", strerror(errno)); + VLOG_ERR("read on %s: %s", ofname, strerror(errno)); error = errno; goto cleanup; } @@ -265,25 +304,92 @@ netdev_pltap_create_finalize(struct netdev_dev_pltap *dev) } if (bytes_read) { reply[bytes_read] = '\0'; - VLOG_ERR("vif_up returned: %s", reply); - dev->error = reply; - reply = NULL; + if (preply) { + *preply = reply; + reply = NULL; /* prevent freeing the reply msg */ + } else { + VLOG_ERR("%s returned: %s", script, reply); + } error = EAGAIN; goto cleanup; } - dev->finalized = true; - free(dev->error); - dev->error = NULL; - netdev_pltap_update_seq(dev); cleanup: free(msg); free(reply); + free(ofname); + free(ifname); close(ifd); close(ofd); return error; } +static int +netdev_pltap_up(struct netdev_dev_pltap *dev) +{ + if (!netdev_pltap_finalized(dev)) { + return 0; + } + + return vsys_transaction("vif_up", NULL, "%s\n"IP_FMT"\n%d\n", + dev->real_name, + IP_ARGS(dev->local_addr.sin_addr.s_addr), + dev->local_netmask); +} + +static int +netdev_pltap_down(struct netdev_dev_pltap *dev) +{ + if (!netdev_pltap_finalized(dev)) { + return 0; + } + + return vsys_transaction("vif_down", NULL, "%s\n", dev->real_name); +} + +static int +netdev_pltap_promisc(struct netdev_dev_pltap *dev, bool promisc) +{ + if (!netdev_pltap_finalized(dev)) { + return 0; + } + + return vsys_transaction("promisc", NULL, "%s\n%s", + dev->real_name, + (promisc ? "" : "-\n")); +} + +static void +netdev_pltap_sync_flags(struct netdev_dev_pltap *dev) +{ + + if (dev->fd < 0 || !netdev_pltap_finalized(dev)) { + sync_done(dev); + return; + } + + VLOG_DBG("sync_flags(%s): current: %s %s target: %s %s", + dev->real_name, + (dev->flags & NETDEV_UP ? "UP" : "-"), + (dev->flags & NETDEV_PROMISC ? "PROMISC" : "-"), + (dev->new_flags & NETDEV_UP ? "UP" : "-"), + (dev->new_flags & NETDEV_PROMISC ? "PROMISC" : "-")); + + if ((dev->new_flags & NETDEV_UP) && !(dev->flags & NETDEV_UP)) { + (void) netdev_pltap_up(dev); + } else if (!(dev->new_flags & NETDEV_UP) && (dev->flags & NETDEV_UP)) { + (void) netdev_pltap_down(dev); + } + + if ((dev->new_flags & NETDEV_PROMISC) ^ (dev->flags & NETDEV_PROMISC)) { + (void) netdev_pltap_promisc(dev, dev->new_flags & NETDEV_PROMISC); + } + + netdev_pltap_update_seq(dev); + sync_done(dev); +} + + static int netdev_pltap_get_config(struct netdev_dev *dev_, struct smap *args) { @@ -291,11 +397,11 @@ netdev_pltap_get_config(struct netdev_dev *dev_, struct smap *args) if (netdev_dev->valid_local_ip) smap_add_format(args, "local_ip", IP_FMT, - IP_ARGS(&netdev_dev->local_addr.sin_addr)); + IP_ARGS(netdev_dev->local_addr.sin_addr.s_addr)); if (netdev_dev->valid_local_netmask) smap_add_format(args, "local_netmask", "%"PRIu32, ntohs(netdev_dev->local_netmask)); - return netdev_pltap_create_finalize(netdev_dev); + return 0; } static int @@ -324,13 +430,21 @@ netdev_pltap_set_config(struct netdev_dev *dev_, const struct smap *args) netdev_dev_get_name(dev_), node->name); } } - return netdev_pltap_create_finalize(netdev_dev); + if (netdev_pltap_finalized(netdev_dev)) { + netdev_dev->new_flags |= NETDEV_UP; + sync_needed(netdev_dev); + } + return 0; } static int netdev_pltap_listen(struct netdev *netdev_ OVS_UNUSED) { - return 0; + struct netdev_dev_pltap *dev = + netdev_dev_pltap_cast(netdev_get_dev(netdev_)); + if (!netdev_pltap_finalized(dev)) + return 0; + return netdev_pltap_up(dev); } static int @@ -343,8 +457,6 @@ netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size) { .iov_base = prefix, .iov_len = 4 }, { .iov_base = buffer, .iov_len = size } }; - if (!dev->finalized) - return -EAGAIN; for (;;) { ssize_t retval; retval = readv(dev->fd, iov, 2); @@ -369,7 +481,7 @@ netdev_pltap_recv_wait(struct netdev *netdev_) { struct netdev_dev_pltap *dev = netdev_dev_pltap_cast(netdev_get_dev(netdev_)); - if (dev->finalized && dev->fd >= 0) { + if (dev->fd >= 0 && netdev_pltap_finalized(dev)) { poll_fd_wait(dev->fd, POLLIN); } } @@ -382,9 +494,9 @@ netdev_pltap_send(struct netdev *netdev_, const void *buffer, size_t size) char prefix[4] = { 0, 0, 8, 6 }; struct iovec iov[2] = { { .iov_base = prefix, .iov_len = 4 }, - { .iov_base = buffer, .iov_len = size } + { .iov_base = (char*) buffer, .iov_len = size } }; - if (dev->fd < 0 || !dev->finalized) + if (dev->fd < 0) return EAGAIN; for (;;) { ssize_t retval; @@ -410,7 +522,7 @@ netdev_pltap_send_wait(struct netdev *netdev_) { struct netdev_dev_pltap *dev = netdev_dev_pltap_cast(netdev_get_dev(netdev_)); - if (dev->finalized && dev->fd >= 0) { + if (dev->fd >= 0 && netdev_pltap_finalized(dev)) { poll_fd_wait(dev->fd, POLLOUT); } } @@ -423,8 +535,8 @@ netdev_pltap_drain(struct netdev *netdev_) char buffer[128]; int error; - if (dev->fd < 0 || !dev->finalized) - return 0; + if (dev->fd < 0) + return EAGAIN; for (;;) { error = recv(dev->fd, buffer, 128, MSG_TRUNC); if (error) { @@ -444,49 +556,60 @@ netdev_pltap_set_etheraddr(struct netdev *netdevi OVS_UNUSED, return ENOTSUP; } + // XXX from netdev-linux.c static int -get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]) +get_etheraddr(struct netdev_dev_pltap *dev, uint8_t ea[ETH_ADDR_LEN]) { struct ifreq ifr; int hwaddr_family; - int af_inet_sock; - - /* Create AF_INET socket. */ - af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0); - if (af_inet_sock < 0) { - VLOG_ERR("failed to create inet socket: %s", strerror(errno)); - } memset(&ifr, 0, sizeof ifr); - ovs_strzcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name); + ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name); if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) { /* ENODEV probably means that a vif disappeared asynchronously and * hasn't been removed from the database yet, so reduce the log level * to INFO for that case. */ VLOG(errno == ENODEV ? VLL_INFO : VLL_ERR, "ioctl(SIOCGIFHWADDR) on %s device failed: %s", - netdev_name, strerror(errno)); + dev->real_name, strerror(errno)); return errno; } hwaddr_family = ifr.ifr_hwaddr.sa_family; if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) { VLOG_WARN("%s device has unknown hardware address family %d", - netdev_name, hwaddr_family); + dev->real_name, hwaddr_family); } memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN); return 0; } +static int +get_flags(struct netdev_dev_pltap *dev, enum netdev_flags *flags) +{ + struct ifreq ifr; + + memset(&ifr, 0, sizeof ifr); + ovs_strzcpy(ifr.ifr_name, dev->real_name, sizeof ifr.ifr_name); + if (ioctl(af_inet_sock, SIOCGIFFLAGS, &ifr) < 0) + return errno; + *flags = 0; + if (ifr.ifr_flags & IFF_UP) + *flags |= NETDEV_UP; + if (ifr.ifr_flags & IFF_PROMISC) + *flags |= NETDEV_PROMISC; + return 0; +} + static int netdev_pltap_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN]) { struct netdev_dev_pltap *dev = netdev_dev_pltap_cast(netdev_get_dev(netdev)); - if (dev->fd < 0 || !dev->finalized) + if (dev->fd < 0) return EAGAIN; - return get_etheraddr(dev->real_name, mac); + return get_etheraddr(dev, mac); } @@ -503,6 +626,7 @@ netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_sta return ENOTSUP; } + static int netdev_pltap_update_flags(struct netdev *netdev, enum netdev_flags off, enum netdev_flags on, @@ -510,19 +634,24 @@ netdev_pltap_update_flags(struct netdev *netdev, { struct netdev_dev_pltap *dev = netdev_dev_pltap_cast(netdev_get_dev(netdev)); + int error = 0; if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) { return EINVAL; } - // XXX should we actually do something with these flags? + if (netdev_pltap_finalized(dev)) { + error = get_flags(dev, &dev->flags); + } *old_flagsp = dev->flags; - dev->flags |= on; - dev->flags &= ~off; - if (*old_flagsp != dev->flags) { - netdev_pltap_update_seq(dev); + dev->new_flags |= on; + dev->new_flags &= ~off; + if (dev->flags != dev->new_flags) { + /* we cannot sync here, since we may be in a signal handler */ + sync_needed(dev); } - return 0; + + return error; } static unsigned int @@ -553,8 +682,8 @@ netdev_pltap_get_real_name(struct unixctl_conn *conn, unixctl_command_reply_error(conn, "no such pltap netdev"); return; } - if (pltap_dev->error) { - unixctl_command_reply_error(conn, pltap_dev->error); + if (pltap_dev->fd < 0) { + unixctl_command_reply_error(conn, "no real device attached"); return; } @@ -564,21 +693,45 @@ netdev_pltap_get_real_name(struct unixctl_conn *conn, static int netdev_pltap_init(void) { + list_init(&sync_list); + af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0); + if (af_inet_sock < 0) { + VLOG_ERR("failed to create inet socket: %s", strerror(errno)); + } unixctl_command_register("netdev-pltap/get-tapname", "port", 1, 1, netdev_pltap_get_real_name, NULL); return 0; } +static void +netdev_pltap_run(void) +{ + struct netdev_dev_pltap *iter, *next; + LIST_FOR_EACH_SAFE(iter, next, sync_list, &sync_list) { + netdev_pltap_sync_flags(iter); + } +} + +static void +netdev_pltap_wait(void) +{ + if (!list_is_empty(&sync_list)) { + VLOG_DBG("netdev_pltap: scheduling sync"); + poll_immediate_wake(); + } +} + const struct netdev_class netdev_pltap_class = { "pltap", netdev_pltap_init, - NULL, - NULL, + netdev_pltap_run, + netdev_pltap_wait, netdev_pltap_create, netdev_pltap_destroy, netdev_pltap_get_config, netdev_pltap_set_config, + NULL, /* get_tunnel_config */ netdev_pltap_open, netdev_pltap_close,