X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev-pltap.c;h=e9722244db1d63cf694fd57881eb744b7dae2279;hb=eced3be9ddbf20bddb4e10dfaa6f40558187ab1f;hp=42b38a2cf985687e2b4bffc8801fe37319767433;hpb=8e833b5a5322bf4daf7d9dd7fc0fc32ee8bf69e2;p=sliver-openvswitch.git diff --git a/lib/netdev-pltap.c b/lib/netdev-pltap.c index 42b38a2cf..e9722244d 100644 --- a/lib/netdev-pltap.c +++ b/lib/netdev-pltap.c @@ -61,12 +61,19 @@ struct netdev_dev_pltap { unsigned int change_seq; }; +static const struct netdev_rx_class netdev_rx_pltap_class; + static struct list sync_list; struct netdev_pltap { struct netdev netdev; }; +struct netdev_rx_pltap { + struct netdev_rx up; + int fd; +}; + static int af_inet_sock = -1; static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); @@ -86,7 +93,7 @@ netdev_pltap_finalized(struct netdev_dev_pltap *dev) } static bool -is_pltap_class(const struct netdev_class *class) +is_netdev_pltap_class(const struct netdev_class *class) { return class->create == netdev_pltap_create; } @@ -94,7 +101,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_netdev_pltap_class(netdev_dev_get_class(netdev_dev))); return CONTAINER_OF(netdev_dev, struct netdev_dev_pltap, netdev_dev); } @@ -102,10 +109,17 @@ 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_netdev_pltap_class(netdev_dev_get_class(netdev_dev))); return CONTAINER_OF(netdev, struct netdev_pltap, netdev); } +static struct netdev_rx_pltap* +netdev_rx_pltap_cast(const struct netdev_rx *rx) +{ + netdev_rx_assert_class(rx, &netdev_rx_pltap_class); + return CONTAINER_OF(rx, struct netdev_rx_pltap, up); +} + static void sync_needed(struct netdev_dev_pltap *dev) { if (dev->sync_flags_needed) @@ -202,14 +216,60 @@ netdev_pltap_close(struct netdev *netdev_) free(netdev); } + +static int netdev_pltap_up(struct netdev_dev_pltap *dev); + +static int +netdev_pltap_rx_open(struct netdev *netdev_, struct netdev_rx **rxp) +{ + struct netdev_dev_pltap *netdev_dev = + netdev_dev_pltap_cast(netdev_get_dev(netdev_)); + struct netdev_rx_pltap *rx; + int err; + + rx = xmalloc(sizeof *rx); + netdev_rx_init(&rx->up, netdev_get_dev(netdev_), &netdev_rx_pltap_class); + rx->fd = netdev_dev->fd; + *rxp = &rx->up; + if (!netdev_pltap_finalized(netdev_dev)) + return 0; + err = netdev_pltap_up(netdev_dev); + if (err) { + free(rx); + *rxp = NULL; + return err; + } + return 0; +} + +static void +netdev_rx_pltap_destroy(struct netdev_rx *rx_) +{ + struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); + free(rx); +} + static int vsys_transaction(const char *script, - const char *msg, char *reply, size_t reply_size) + 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, bytes_written = 0, bytes_read = 0; int error = 0; char *ofname = NULL, *ifname = NULL; + va_list args; + + 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); @@ -291,12 +351,19 @@ static int vsys_transaction(const char *script, } if (bytes_read) { reply[bytes_read] = '\0'; - VLOG_ERR("%s returned: %s", script, reply); + if (preply) { + *preply = reply; + reply = NULL; /* prevent freeing the reply msg */ + } else { + VLOG_ERR("%s returned: %s", script, reply); + } error = EAGAIN; goto cleanup; } cleanup: + free(msg); + free(reply); free(ofname); free(ifname); close(ifd); @@ -307,106 +374,46 @@ cleanup: static int netdev_pltap_up(struct netdev_dev_pltap *dev) { - int error; - char *msg = NULL, *reply = NULL; - const size_t reply_size = 1024; - if (!netdev_pltap_finalized(dev)) { return 0; } - 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(reply_size); - if (!msg || !reply) { - VLOG_ERR("Out of memory\n"); - error = ENOMEM; - goto cleanup; - } - error = vsys_transaction("vif_up", msg, reply, reply_size); - if (error) { - goto cleanup; - } - netdev_pltap_update_seq(dev); - -cleanup: - free(msg); - free(reply); - - return error; + 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) { - int error; - char *msg = NULL, *reply = NULL; - const size_t reply_size = 1024; - if (!netdev_pltap_finalized(dev)) { return 0; } - msg = xasprintf("%s\n", dev->real_name); - reply = (char*)xmalloc(reply_size); - if (!msg || !reply) { - VLOG_ERR("Out of memory\n"); - error = ENOMEM; - goto cleanup; - } - error = vsys_transaction("vif_down", msg, reply, reply_size); - if (error) { - goto cleanup; - } - netdev_pltap_update_seq(dev); - -cleanup: - free(msg); - free(reply); - - return error; + return vsys_transaction("vif_down", NULL, "%s\n", dev->real_name); } static int netdev_pltap_promisc(struct netdev_dev_pltap *dev, bool promisc) { - int error = 0; - char *msg = NULL, *reply = NULL; - const size_t reply_size = 1024; - if (!netdev_pltap_finalized(dev)) { return 0; } - msg = xasprintf("%s\n%s", - dev->real_name, - (promisc ? "" : "-\n")); - reply = (char*)xmalloc(reply_size); - if (!msg || !reply) { - VLOG_ERR("Out of memory\n"); - goto cleanup; - } - error = vsys_transaction("promisc", msg, reply, reply_size); - if (error) { - goto cleanup; - } - netdev_pltap_update_seq(dev); - -cleanup: - free(msg); - free(reply); - - return error; + 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)) + 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, @@ -425,6 +432,7 @@ netdev_pltap_sync_flags(struct netdev_dev_pltap *dev) (void) netdev_pltap_promisc(dev, dev->new_flags & NETDEV_PROMISC); } + netdev_pltap_update_seq(dev); sync_done(dev); } @@ -436,7 +444,7 @@ 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)); @@ -477,20 +485,9 @@ netdev_pltap_set_config(struct netdev_dev *dev_, const struct smap *args) } static int -netdev_pltap_listen(struct netdev *netdev_ OVS_UNUSED) -{ - 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 -netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size) +netdev_rx_pltap_recv(struct netdev_rx *rx_, void *buffer, size_t size) { - struct netdev_dev_pltap *dev = - netdev_dev_pltap_cast(netdev_get_dev(netdev_)); + struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); char prefix[4]; struct iovec iov[2] = { { .iov_base = prefix, .iov_len = 4 }, @@ -498,7 +495,7 @@ netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size) }; for (;;) { ssize_t retval; - retval = readv(dev->fd, iov, 2); + retval = readv(rx->fd, iov, 2); if (retval >= 0) { if (retval <= size) { return retval; @@ -508,7 +505,7 @@ netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size) } else if (errno != EINTR) { if (errno != EAGAIN) { VLOG_WARN_RL(&rl, "error receiveing Ethernet packet on %s: %s", - netdev_get_name(netdev_), strerror(errno)); + netdev_rx_get_name(rx_), strerror(errno)); } return -errno; } @@ -516,12 +513,13 @@ netdev_pltap_recv(struct netdev *netdev_, void *buffer, size_t size) } static void -netdev_pltap_recv_wait(struct netdev *netdev_) +netdev_rx_pltap_wait(struct netdev_rx *rx_) { - struct netdev_dev_pltap *dev = - netdev_dev_pltap_cast(netdev_get_dev(netdev_)); - if (dev->fd >= 0 && netdev_pltap_finalized(dev)) { - poll_fd_wait(dev->fd, POLLIN); + struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); + struct netdev_dev_pltap *netdev_dev = + netdev_dev_pltap_cast(rx->up.netdev_dev); + if (rx->fd >= 0 && netdev_pltap_finalized(netdev_dev)) { + poll_fd_wait(rx->fd, POLLIN); } } @@ -567,17 +565,16 @@ netdev_pltap_send_wait(struct netdev *netdev_) } static int -netdev_pltap_drain(struct netdev *netdev_) +netdev_rx_pltap_drain(struct netdev_rx *rx_) { - struct netdev_dev_pltap *dev = - netdev_dev_pltap_cast(netdev_get_dev(netdev_)); + struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); char buffer[128]; int error; - if (dev->fd < 0) + if (rx->fd < 0) return EAGAIN; for (;;) { - error = recv(dev->fd, buffer, 128, MSG_TRUNC); + error = recv(rx->fd, buffer, 128, MSG_TRUNC); if (error) { if (error == -EAGAIN) break; @@ -667,27 +664,27 @@ netdev_pltap_set_stats(struct netdev *netdev OVS_UNUSED, const struct netdev_sta static int -netdev_pltap_update_flags(struct netdev *netdev, +netdev_pltap_update_flags(struct netdev_dev *dev_, enum netdev_flags off, enum netdev_flags on, enum netdev_flags *old_flagsp) { - struct netdev_dev_pltap *dev = - netdev_dev_pltap_cast(netdev_get_dev(netdev)); + struct netdev_dev_pltap *netdev_dev = + netdev_dev_pltap_cast(dev_); int error = 0; if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) { return EINVAL; } - if (netdev_pltap_finalized(dev)) { - error = get_flags(dev, &dev->flags); + if (netdev_pltap_finalized(netdev_dev)) { + error = get_flags(netdev_dev, &netdev_dev->flags); } - *old_flagsp = dev->flags; - dev->new_flags |= on; - dev->new_flags &= ~off; - if (dev->flags != dev->new_flags) { + *old_flagsp = netdev_dev->flags; + netdev_dev->new_flags |= on; + netdev_dev->new_flags &= ~off; + if (netdev_dev->flags != netdev_dev->new_flags) { /* we cannot sync here, since we may be in a signal handler */ - sync_needed(dev); + sync_needed(netdev_dev); } return error; @@ -770,14 +767,12 @@ const struct netdev_class netdev_pltap_class = { netdev_pltap_destroy, netdev_pltap_get_config, netdev_pltap_set_config, + NULL, /* get_tunnel_config */ netdev_pltap_open, netdev_pltap_close, - netdev_pltap_listen, - netdev_pltap_recv, - netdev_pltap_recv_wait, - netdev_pltap_drain, + netdev_pltap_rx_open, netdev_pltap_send, netdev_pltap_send_wait, @@ -820,3 +815,11 @@ const struct netdev_class netdev_pltap_class = { netdev_pltap_change_seq }; + +static const struct netdev_rx_class netdev_rx_pltap_class = { + netdev_rx_pltap_destroy, + netdev_rx_pltap_recv, + netdev_rx_pltap_wait, + netdev_rx_pltap_drain, + +};