X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev-pltap.c;h=62f23f3fda02321664eb89cb61fc3d3f7085916a;hb=780325b5b8d4c0552b4b7719e0a38200d99f6b08;hp=257cc6d1354889162800289dd2754946e3843b56;hpb=c096cdc8b7eccbd361e88148a8be2e8d3b018524;p=sliver-openvswitch.git diff --git a/lib/netdev-pltap.c b/lib/netdev-pltap.c index 257cc6d13..62f23f3fd 100644 --- a/lib/netdev-pltap.c +++ b/lib/netdev-pltap.c @@ -94,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); } @@ -102,7 +102,7 @@ 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); } @@ -203,13 +203,26 @@ netdev_pltap_close(struct netdev *netdev_) } 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 +304,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,46 +327,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; + 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; } - netdev_pltap_update_seq(dev); + + return vsys_transaction("vif_down", NULL, "%s\n", dev->real_name); +} -cleanup: - free(msg); - free(reply); +static int +netdev_pltap_promisc(struct netdev_dev_pltap *dev, bool promisc) +{ + if (!netdev_pltap_finalized(dev)) { + return 0; + } - 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) { - int error = 0; - char *msg = NULL, *reply = NULL; - const size_t reply_size = 1024; - 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, @@ -357,31 +377,16 @@ netdev_pltap_sync_flags(struct netdev_dev_pltap *dev) if ((dev->new_flags & NETDEV_UP) && !(dev->flags & NETDEV_UP)) { (void) netdev_pltap_up(dev); - } /* TODO: implement the down case */ + } 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)) { - msg = xasprintf("%s\n%s", - dev->real_name, - (dev->new_flags & NETDEV_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); + (void) netdev_pltap_promisc(dev, dev->new_flags & NETDEV_PROMISC); } -cleanup: - + netdev_pltap_update_seq(dev); sync_done(dev); - free(msg); - free(reply); - - return error; } @@ -392,7 +397,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)); @@ -635,7 +640,9 @@ netdev_pltap_update_flags(struct netdev *netdev, return EINVAL; } - error = get_flags(dev, &dev->flags); + if (netdev_pltap_finalized(dev)) { + error = get_flags(dev, &dev->flags); + } *old_flagsp = dev->flags; dev->new_flags |= on; dev->new_flags &= ~off; @@ -724,6 +731,7 @@ 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,