X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetdev-dummy.c;h=5c312109ba289b3405f45b6e53c24a033e681ecd;hb=fa04edcedfe5285fd8ad3a4d70fecb38df18293d;hp=6aa4084b25e26094f51b9a013bda3901230db41e;hpb=95d4ec339d25373e04e5218235d3d0d477f0766e;p=sliver-openvswitch.git diff --git a/lib/netdev-dummy.c b/lib/netdev-dummy.c index 6aa4084b2..5c312109b 100644 --- a/lib/netdev-dummy.c +++ b/lib/netdev-dummy.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2011, 2012 Nicira, Inc. + * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ #include "flow.h" #include "list.h" #include "netdev-provider.h" +#include "netdev-vport.h" #include "odp-util.h" #include "ofp-print.h" #include "ofpbuf.h" @@ -30,170 +31,481 @@ #include "poll-loop.h" #include "shash.h" #include "sset.h" +#include "stream.h" +#include "unaligned.h" #include "unixctl.h" #include "vlog.h" VLOG_DEFINE_THIS_MODULE(netdev_dummy); -struct netdev_dev_dummy { - struct netdev_dev netdev_dev; +struct dummy_stream { + struct stream *stream; + struct ofpbuf rxbuf; + struct list txq; +}; + +/* Protects 'dummy_list'. */ +static struct ovs_mutex dummy_list_mutex = OVS_MUTEX_INITIALIZER; + +/* Contains all 'struct dummy_dev's. */ +static struct list dummy_list OVS_GUARDED_BY(dummy_list_mutex) + = LIST_INITIALIZER(&dummy_list); + +struct netdev_dummy { + struct netdev up; + + /* In dummy_list. */ + struct list list_node OVS_GUARDED_BY(dummy_list_mutex); + + /* Protects all members below. */ + struct ovs_mutex mutex OVS_ACQ_AFTER(dummy_list_mutex); + uint8_t hwaddr[ETH_ADDR_LEN]; int mtu; struct netdev_stats stats; enum netdev_flags flags; unsigned int change_seq; + int ifindex; + + struct pstream *pstream; + struct dummy_stream *streams; + size_t n_streams; - struct list devs; /* List of child "netdev_dummy"s. */ + struct list rxes; /* List of child "netdev_rx_dummy"s. */ }; -struct netdev_dummy { - struct netdev netdev; - struct list node; /* In netdev_dev_dummy's "devs" list. */ +/* Max 'recv_queue_len' in struct netdev_dummy. */ +#define NETDEV_DUMMY_MAX_QUEUE 100 + +struct netdev_rx_dummy { + struct netdev_rx up; + struct list node; /* In netdev_dummy's "rxes" list. */ struct list recv_queue; + int recv_queue_len; /* list_size(&recv_queue). */ bool listening; }; -static struct shash dummy_netdev_devs = SHASH_INITIALIZER(&dummy_netdev_devs); - static unixctl_cb_func netdev_dummy_set_admin_state; -static int netdev_dummy_create(const struct netdev_class *, const char *, - struct netdev_dev **); -static void netdev_dev_dummy_poll_notify(struct netdev_dev_dummy *); -static int netdev_dev_dummy_update_flags(struct netdev_dev_dummy *, - enum netdev_flags off, - enum netdev_flags on, - enum netdev_flags *old_flagsp); +static int netdev_dummy_construct(struct netdev *); +static void netdev_dummy_poll_notify(struct netdev_dummy *netdev) + OVS_REQUIRES(netdev->mutex); +static void netdev_dummy_queue_packet(struct netdev_dummy *, struct ofpbuf *); + +static void dummy_stream_close(struct dummy_stream *); static bool is_dummy_class(const struct netdev_class *class) { - return class->create == netdev_dummy_create; + return class->construct == netdev_dummy_construct; } -static struct netdev_dev_dummy * -netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev) +static struct netdev_dummy * +netdev_dummy_cast(const struct netdev *netdev) { - assert(is_dummy_class(netdev_dev_get_class(netdev_dev))); - return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev); + ovs_assert(is_dummy_class(netdev_get_class(netdev))); + return CONTAINER_OF(netdev, struct netdev_dummy, up); } -static struct netdev_dummy * -netdev_dummy_cast(const struct netdev *netdev) +static struct netdev_rx_dummy * +netdev_rx_dummy_cast(const struct netdev_rx *rx) { - struct netdev_dev *netdev_dev = netdev_get_dev(netdev); - assert(is_dummy_class(netdev_dev_get_class(netdev_dev))); - return CONTAINER_OF(netdev, struct netdev_dummy, netdev); + ovs_assert(is_dummy_class(netdev_get_class(rx->netdev))); + return CONTAINER_OF(rx, struct netdev_rx_dummy, up); } -static int -netdev_dummy_create(const struct netdev_class *class, const char *name, - struct netdev_dev **netdev_devp) +static void +netdev_dummy_run(void) { - static unsigned int n = 0xaa550000; - struct netdev_dev_dummy *netdev_dev; + struct netdev_dummy *dev; + + ovs_mutex_lock(&dummy_list_mutex); + LIST_FOR_EACH (dev, list_node, &dummy_list) { + size_t i; + + ovs_mutex_lock(&dev->mutex); + + if (dev->pstream) { + struct stream *new_stream; + int error; + + error = pstream_accept(dev->pstream, &new_stream); + if (!error) { + struct dummy_stream *s; + + dev->streams = xrealloc(dev->streams, + ((dev->n_streams + 1) + * sizeof *dev->streams)); + s = &dev->streams[dev->n_streams++]; + s->stream = new_stream; + ofpbuf_init(&s->rxbuf, 2048); + list_init(&s->txq); + } else if (error != EAGAIN) { + VLOG_WARN("%s: accept failed (%s)", + pstream_get_name(dev->pstream), ovs_strerror(error)); + pstream_close(dev->pstream); + dev->pstream = NULL; + } + } - netdev_dev = xzalloc(sizeof *netdev_dev); - netdev_dev_init(&netdev_dev->netdev_dev, name, class); - netdev_dev->hwaddr[0] = 0xaa; - netdev_dev->hwaddr[1] = 0x55; - netdev_dev->hwaddr[2] = n >> 24; - netdev_dev->hwaddr[3] = n >> 16; - netdev_dev->hwaddr[4] = n >> 8; - netdev_dev->hwaddr[5] = n; - netdev_dev->mtu = 1500; - netdev_dev->flags = 0; - netdev_dev->change_seq = 1; - list_init(&netdev_dev->devs); + for (i = 0; i < dev->n_streams; i++) { + struct dummy_stream *s = &dev->streams[i]; + int error = 0; + size_t n; + + stream_run(s->stream); + + if (!list_is_empty(&s->txq)) { + struct ofpbuf *txbuf; + int retval; + + txbuf = ofpbuf_from_list(list_front(&s->txq)); + retval = stream_send(s->stream, txbuf->data, txbuf->size); + if (retval > 0) { + ofpbuf_pull(txbuf, retval); + if (!txbuf->size) { + list_remove(&txbuf->list_node); + ofpbuf_delete(txbuf); + } + } else if (retval != -EAGAIN) { + error = -retval; + } + } - shash_add(&dummy_netdev_devs, name, netdev_dev); + if (!error) { + if (s->rxbuf.size < 2) { + n = 2 - s->rxbuf.size; + } else { + uint16_t frame_len; + + frame_len = ntohs(get_unaligned_be16(s->rxbuf.data)); + if (frame_len < ETH_HEADER_LEN) { + error = EPROTO; + n = 0; + } else { + n = (2 + frame_len) - s->rxbuf.size; + } + } + } + if (!error) { + int retval; + + ofpbuf_prealloc_tailroom(&s->rxbuf, n); + retval = stream_recv(s->stream, ofpbuf_tail(&s->rxbuf), n); + if (retval > 0) { + s->rxbuf.size += retval; + if (retval == n && s->rxbuf.size > 2) { + ofpbuf_pull(&s->rxbuf, 2); + netdev_dummy_queue_packet(dev, + ofpbuf_clone(&s->rxbuf)); + ofpbuf_clear(&s->rxbuf); + } + } else if (retval != -EAGAIN) { + error = (retval < 0 ? -retval + : s->rxbuf.size ? EPROTO + : EOF); + } + } - n++; + if (error) { + VLOG_DBG("%s: closing connection (%s)", + stream_get_name(s->stream), + ovs_retval_to_string(error)); + dummy_stream_close(&dev->streams[i]); + dev->streams[i] = dev->streams[--dev->n_streams]; + } + } - *netdev_devp = &netdev_dev->netdev_dev; + ovs_mutex_unlock(&dev->mutex); + } + ovs_mutex_unlock(&dummy_list_mutex); +} - return 0; +static void +dummy_stream_close(struct dummy_stream *s) +{ + stream_close(s->stream); + ofpbuf_uninit(&s->rxbuf); + ofpbuf_list_delete(&s->txq); } static void -netdev_dummy_destroy(struct netdev_dev *netdev_dev_) +netdev_dummy_wait(void) { - struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_); + struct netdev_dummy *dev; - shash_find_and_delete(&dummy_netdev_devs, - netdev_dev_get_name(netdev_dev_)); - free(netdev_dev); + ovs_mutex_lock(&dummy_list_mutex); + LIST_FOR_EACH (dev, list_node, &dummy_list) { + size_t i; + + ovs_mutex_lock(&dev->mutex); + if (dev->pstream) { + pstream_wait(dev->pstream); + } + for (i = 0; i < dev->n_streams; i++) { + struct dummy_stream *s = &dev->streams[i]; + + stream_run_wait(s->stream); + if (!list_is_empty(&s->txq)) { + stream_send_wait(s->stream); + } + stream_recv_wait(s->stream); + } + ovs_mutex_unlock(&dev->mutex); + } + ovs_mutex_unlock(&dummy_list_mutex); +} + +static struct netdev * +netdev_dummy_alloc(void) +{ + struct netdev_dummy *netdev = xzalloc(sizeof *netdev); + return &netdev->up; } static int -netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp) +netdev_dummy_construct(struct netdev *netdev_) { - struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_); - struct netdev_dummy *netdev; + static atomic_uint next_n = ATOMIC_VAR_INIT(0xaa550000); + struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); + unsigned int n; + + atomic_add(&next_n, 1, &n); + + ovs_mutex_init(&netdev->mutex, PTHREAD_MUTEX_NORMAL); + netdev->hwaddr[0] = 0xaa; + netdev->hwaddr[1] = 0x55; + netdev->hwaddr[2] = n >> 24; + netdev->hwaddr[3] = n >> 16; + netdev->hwaddr[4] = n >> 8; + netdev->hwaddr[5] = n; + netdev->mtu = 1500; + netdev->flags = 0; + netdev->change_seq = 1; + netdev->ifindex = -EOPNOTSUPP; - netdev = xmalloc(sizeof *netdev); - netdev_init(&netdev->netdev, netdev_dev_); - list_init(&netdev->recv_queue); - netdev->listening = false; + netdev->pstream = NULL; + netdev->streams = NULL; + netdev->n_streams = 0; + + list_init(&netdev->rxes); + + ovs_mutex_lock(&dummy_list_mutex); + list_push_back(&dummy_list, &netdev->list_node); + ovs_mutex_unlock(&dummy_list_mutex); - *netdevp = &netdev->netdev; - list_push_back(&netdev_dev->devs, &netdev->node); return 0; } static void -netdev_dummy_close(struct netdev *netdev_) +netdev_dummy_destruct(struct netdev *netdev_) { struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); - list_remove(&netdev->node); - ofpbuf_list_delete(&netdev->recv_queue); + size_t i; + + ovs_mutex_lock(&dummy_list_mutex); + list_remove(&netdev->list_node); + ovs_mutex_unlock(&dummy_list_mutex); + + pstream_close(netdev->pstream); + for (i = 0; i < netdev->n_streams; i++) { + dummy_stream_close(&netdev->streams[i]); + } + free(netdev->streams); + ovs_mutex_destroy(&netdev->mutex); +} + +static void +netdev_dummy_dealloc(struct netdev *netdev_) +{ + struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); + free(netdev); } static int -netdev_dummy_listen(struct netdev *netdev_) +netdev_dummy_get_config(const struct netdev *netdev_, struct smap *args) { struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); - netdev->listening = true; + + if (netdev->ifindex >= 0) { + smap_add_format(args, "ifindex", "%d", netdev->ifindex); + } + if (netdev->pstream) { + smap_add(args, "pstream", pstream_get_name(netdev->pstream)); + } return 0; } static int -netdev_dummy_recv(struct netdev *netdev_, void *buffer, size_t size) +netdev_dummy_set_config(struct netdev *netdev_, const struct smap *args) { struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); + const char *pstream; + + ovs_mutex_lock(&netdev->mutex); + netdev->ifindex = smap_get_int(args, "ifindex", -EOPNOTSUPP); + + pstream = smap_get(args, "pstream"); + if (!pstream + || !netdev->pstream + || strcmp(pstream_get_name(netdev->pstream), pstream)) { + pstream_close(netdev->pstream); + netdev->pstream = NULL; + + if (pstream) { + int error; + + error = pstream_open(pstream, &netdev->pstream, DSCP_DEFAULT); + if (error) { + VLOG_WARN("%s: open failed (%s)", + pstream, ovs_strerror(error)); + } + } + } + ovs_mutex_unlock(&netdev->mutex); + + return 0; +} + +static struct netdev_rx * +netdev_dummy_rx_alloc(void) +{ + struct netdev_rx_dummy *rx = xzalloc(sizeof *rx); + return &rx->up; +} + +static int +netdev_dummy_rx_construct(struct netdev_rx *rx_) +{ + struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_); + struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev); + + ovs_mutex_lock(&netdev->mutex); + list_push_back(&netdev->rxes, &rx->node); + list_init(&rx->recv_queue); + rx->recv_queue_len = 0; + ovs_mutex_unlock(&netdev->mutex); + + return 0; +} + +static void +netdev_dummy_rx_destruct(struct netdev_rx *rx_) +{ + struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_); + struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev); + + ovs_mutex_lock(&netdev->mutex); + list_remove(&rx->node); + ofpbuf_list_delete(&rx->recv_queue); + ovs_mutex_unlock(&netdev->mutex); +} + +static void +netdev_dummy_rx_dealloc(struct netdev_rx *rx_) +{ + struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_); + + free(rx); +} + +static int +netdev_dummy_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size) +{ + struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_); + struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev); struct ofpbuf *packet; - size_t packet_size; + int retval; - if (list_is_empty(&netdev->recv_queue)) { - return -EAGAIN; + ovs_mutex_lock(&netdev->mutex); + if (!list_is_empty(&rx->recv_queue)) { + packet = ofpbuf_from_list(list_pop_front(&rx->recv_queue)); + rx->recv_queue_len--; + } else { + packet = NULL; } + ovs_mutex_unlock(&netdev->mutex); - packet = ofpbuf_from_list(list_pop_front(&netdev->recv_queue)); - if (packet->size > size) { - return -EMSGSIZE; + if (!packet) { + return -EAGAIN; } - packet_size = packet->size; - memcpy(buffer, packet->data, packet->size); + if (packet->size <= size) { + memcpy(buffer, packet->data, packet->size); + retval = packet->size; + } else { + retval = -EMSGSIZE; + } ofpbuf_delete(packet); - return packet_size; + return retval; } static void -netdev_dummy_recv_wait(struct netdev *netdev_) +netdev_dummy_rx_wait(struct netdev_rx *rx_) { - struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); - if (!list_is_empty(&netdev->recv_queue)) { + struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_); + struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev); + + ovs_mutex_lock(&netdev->mutex); + if (!list_is_empty(&rx->recv_queue)) { poll_immediate_wake(); } + ovs_mutex_unlock(&netdev->mutex); } static int -netdev_dummy_drain(struct netdev *netdev_) +netdev_dummy_rx_drain(struct netdev_rx *rx_) { - struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); - ofpbuf_list_delete(&netdev->recv_queue); + struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_); + struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev); + + ovs_mutex_lock(&netdev->mutex); + ofpbuf_list_delete(&rx->recv_queue); + rx->recv_queue_len = 0; + ovs_mutex_unlock(&netdev->mutex); + + return 0; +} + +static int +netdev_dummy_send(struct netdev *netdev, const void *buffer, size_t size) +{ + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + size_t i; + + if (size < ETH_HEADER_LEN) { + return EMSGSIZE; + } else { + const struct eth_header *eth = buffer; + int max_size; + + max_size = dev->mtu + ETH_HEADER_LEN; + if (eth->eth_type == htons(ETH_TYPE_VLAN)) { + max_size += VLAN_HEADER_LEN; + } + if (size > max_size) { + return EMSGSIZE; + } + } + + ovs_mutex_lock(&dev->mutex); + dev->stats.tx_packets++; + dev->stats.tx_bytes += size; + + for (i = 0; i < dev->n_streams; i++) { + struct dummy_stream *s = &dev->streams[i]; + + if (list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) { + struct ofpbuf *b; + + b = ofpbuf_clone_data_with_headroom(buffer, size, 2); + put_unaligned_be16(ofpbuf_push_uninit(b, 2), htons(size)); + list_push_back(&s->txq, &b->list_node); + } + } + ovs_mutex_unlock(&dev->mutex); + return 0; } @@ -201,13 +513,14 @@ static int netdev_dummy_set_etheraddr(struct netdev *netdev, const uint8_t mac[ETH_ADDR_LEN]) { - struct netdev_dev_dummy *dev = - netdev_dev_dummy_cast(netdev_get_dev(netdev)); + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + ovs_mutex_lock(&dev->mutex); if (!eth_addr_equals(dev->hwaddr, mac)) { memcpy(dev->hwaddr, mac, ETH_ADDR_LEN); - netdev_dev_dummy_poll_notify(dev); + netdev_dummy_poll_notify(dev); } + ovs_mutex_unlock(&dev->mutex); return 0; } @@ -216,92 +529,128 @@ static int netdev_dummy_get_etheraddr(const struct netdev *netdev, uint8_t mac[ETH_ADDR_LEN]) { - const struct netdev_dev_dummy *dev = - netdev_dev_dummy_cast(netdev_get_dev(netdev)); + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + ovs_mutex_lock(&dev->mutex); memcpy(mac, dev->hwaddr, ETH_ADDR_LEN); + ovs_mutex_unlock(&dev->mutex); + return 0; } static int netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup) { - const struct netdev_dev_dummy *dev = - netdev_dev_dummy_cast(netdev_get_dev(netdev)); + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + ovs_mutex_lock(&dev->mutex); *mtup = dev->mtu; + ovs_mutex_unlock(&dev->mutex); + return 0; } static int netdev_dummy_set_mtu(const struct netdev *netdev, int mtu) { - struct netdev_dev_dummy *dev = - netdev_dev_dummy_cast(netdev_get_dev(netdev)); + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + ovs_mutex_lock(&dev->mutex); dev->mtu = mtu; + ovs_mutex_unlock(&dev->mutex); + return 0; } static int netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats) { - const struct netdev_dev_dummy *dev = - netdev_dev_dummy_cast(netdev_get_dev(netdev)); + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + ovs_mutex_lock(&dev->mutex); *stats = dev->stats; + ovs_mutex_unlock(&dev->mutex); + return 0; } static int netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats) { - struct netdev_dev_dummy *dev = - netdev_dev_dummy_cast(netdev_get_dev(netdev)); + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + ovs_mutex_lock(&dev->mutex); dev->stats = *stats; + ovs_mutex_unlock(&dev->mutex); + return 0; } static int -netdev_dummy_update_flags(struct netdev *netdev, - enum netdev_flags off, enum netdev_flags on, - enum netdev_flags *old_flagsp) +netdev_dummy_get_ifindex(const struct netdev *netdev) { - struct netdev_dev_dummy *dev = - netdev_dev_dummy_cast(netdev_get_dev(netdev)); + struct netdev_dummy *dev = netdev_dummy_cast(netdev); + int ifindex; + + ovs_mutex_lock(&dev->mutex); + ifindex = dev->ifindex; + ovs_mutex_unlock(&dev->mutex); - return netdev_dev_dummy_update_flags(dev, off, on, old_flagsp); + return ifindex; } static int -netdev_dev_dummy_update_flags(struct netdev_dev_dummy *dev, - enum netdev_flags off, enum netdev_flags on, - enum netdev_flags *old_flagsp) +netdev_dummy_update_flags__(struct netdev_dummy *netdev, + enum netdev_flags off, enum netdev_flags on, + enum netdev_flags *old_flagsp) + OVS_REQUIRES(netdev->mutex) { if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) { return EINVAL; } - *old_flagsp = dev->flags; - dev->flags |= on; - dev->flags &= ~off; - if (*old_flagsp != dev->flags) { - netdev_dev_dummy_poll_notify(dev); + *old_flagsp = netdev->flags; + netdev->flags |= on; + netdev->flags &= ~off; + if (*old_flagsp != netdev->flags) { + netdev_dummy_poll_notify(netdev); } + return 0; } +static int +netdev_dummy_update_flags(struct netdev *netdev_, + enum netdev_flags off, enum netdev_flags on, + enum netdev_flags *old_flagsp) +{ + struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); + int error; + + ovs_mutex_lock(&netdev->mutex); + error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp); + ovs_mutex_unlock(&netdev->mutex); + + return error; +} + static unsigned int -netdev_dummy_change_seq(const struct netdev *netdev) +netdev_dummy_change_seq(const struct netdev *netdev_) { - return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq; + struct netdev_dummy *netdev = netdev_dummy_cast(netdev_); + unsigned int change_seq; + + ovs_mutex_lock(&netdev->mutex); + change_seq = netdev->change_seq; + ovs_mutex_unlock(&netdev->mutex); + + return change_seq; } /* Helper functions. */ static void -netdev_dev_dummy_poll_notify(struct netdev_dev_dummy *dev) +netdev_dummy_poll_notify(struct netdev_dummy *dev) { dev->change_seq++; if (!dev->change_seq) { @@ -312,30 +661,25 @@ netdev_dev_dummy_poll_notify(struct netdev_dev_dummy *dev) static const struct netdev_class dummy_class = { "dummy", NULL, /* init */ - NULL, /* run */ - NULL, /* wait */ - - netdev_dummy_create, - netdev_dummy_destroy, - NULL, /* get_config */ - NULL, /* set_config */ - - netdev_dummy_open, - netdev_dummy_close, - - netdev_dummy_listen, - netdev_dummy_recv, - netdev_dummy_recv_wait, - netdev_dummy_drain, - - NULL, /* send */ + netdev_dummy_run, + netdev_dummy_wait, + + netdev_dummy_alloc, + netdev_dummy_construct, + netdev_dummy_destruct, + netdev_dummy_dealloc, + netdev_dummy_get_config, + netdev_dummy_set_config, + NULL, /* get_tunnel_config */ + + netdev_dummy_send, /* send */ NULL, /* send_wait */ netdev_dummy_set_etheraddr, netdev_dummy_get_etheraddr, netdev_dummy_get_mtu, netdev_dummy_set_mtu, - NULL, /* get_ifindex */ + netdev_dummy_get_ifindex, NULL, /* get_carrier */ NULL, /* get_carrier_resets */ NULL, /* get_miimon */ @@ -362,12 +706,20 @@ static const struct netdev_class dummy_class = { NULL, /* get_in6 */ NULL, /* add_router */ NULL, /* get_next_hop */ - NULL, /* get_drv_info */ + NULL, /* get_status */ NULL, /* arp_lookup */ netdev_dummy_update_flags, - netdev_dummy_change_seq + netdev_dummy_change_seq, + + netdev_dummy_rx_alloc, + netdev_dummy_rx_construct, + netdev_dummy_rx_destruct, + netdev_dummy_rx_dealloc, + netdev_dummy_rx_recv, + netdev_dummy_rx_wait, + netdev_dummy_rx_drain, }; static struct ofpbuf * @@ -390,7 +742,7 @@ eth_from_packet_or_flow(const char *s) * settle for parsing a datapath key for now. */ ofpbuf_init(&odp_key, 0); - error = odp_flow_key_from_string(s, NULL, &odp_key); + error = odp_flow_from_string(s, NULL, &odp_key, NULL); if (error) { ofpbuf_uninit(&odp_key); return NULL; @@ -410,59 +762,81 @@ eth_from_packet_or_flow(const char *s) return packet; } +static void +netdev_dummy_queue_packet__(struct netdev_rx_dummy *rx, struct ofpbuf *packet) +{ + list_push_back(&rx->recv_queue, &packet->list_node); + rx->recv_queue_len++; +} + +static void +netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct ofpbuf *packet) +{ + struct netdev_rx_dummy *rx, *prev; + + prev = NULL; + LIST_FOR_EACH (rx, node, &dummy->rxes) { + if (rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) { + if (prev) { + netdev_dummy_queue_packet__(prev, ofpbuf_clone(packet)); + } + prev = rx; + } + } + if (prev) { + netdev_dummy_queue_packet__(prev, packet); + } else { + ofpbuf_delete(packet); + } +} + static void netdev_dummy_receive(struct unixctl_conn *conn, int argc, const char *argv[], void *aux OVS_UNUSED) { - struct netdev_dev_dummy *dummy_dev; - int n_listeners; + struct netdev_dummy *dummy_dev; + struct netdev *netdev; int i; - dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]); - if (!dummy_dev) { + netdev = netdev_from_name(argv[1]); + if (!netdev || !is_dummy_class(netdev->netdev_class)) { unixctl_command_reply_error(conn, "no such dummy netdev"); - return; + goto exit; } + dummy_dev = netdev_dummy_cast(netdev); - n_listeners = 0; for (i = 2; i < argc; i++) { - struct netdev_dummy *dev; struct ofpbuf *packet; packet = eth_from_packet_or_flow(argv[i]); if (!packet) { unixctl_command_reply_error(conn, "bad packet syntax"); - return; + goto exit; } - n_listeners = 0; - LIST_FOR_EACH (dev, node, &dummy_dev->devs) { - if (dev->listening) { - struct ofpbuf *copy = ofpbuf_clone(packet); - list_push_back(&dev->recv_queue, ©->list_node); - n_listeners++; - } - } - ofpbuf_delete(packet); + ovs_mutex_lock(&dummy_dev->mutex); + dummy_dev->stats.rx_packets++; + dummy_dev->stats.rx_bytes += packet->size; + netdev_dummy_queue_packet(dummy_dev, packet); + ovs_mutex_unlock(&dummy_dev->mutex); } - if (!n_listeners) { - unixctl_command_reply(conn, "packets queued but nobody listened"); - } else { - unixctl_command_reply(conn, "success"); - } + unixctl_command_reply(conn, NULL); + +exit: + netdev_close(netdev); } static void -netdev_dev_dummy_set_admin_state(struct netdev_dev_dummy *dev, - bool admin_state) +netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state) + OVS_REQUIRES(dev->mutex) { enum netdev_flags old_flags; if (admin_state) { - netdev_dev_dummy_update_flags(dev, 0, NETDEV_UP, &old_flags); + netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags); } else { - netdev_dev_dummy_update_flags(dev, NETDEV_UP, 0, &old_flags); + netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags); } } @@ -482,21 +856,30 @@ netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc, } if (argc > 2) { - struct netdev_dev_dummy *dummy_dev; + struct netdev *netdev = netdev_from_name(argv[1]); + if (netdev && is_dummy_class(netdev->netdev_class)) { + struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev); - dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]); - if (dummy_dev) { - netdev_dev_dummy_set_admin_state(dummy_dev, up); + ovs_mutex_lock(&dummy_dev->mutex); + netdev_dummy_set_admin_state__(dummy_dev, up); + ovs_mutex_unlock(&dummy_dev->mutex); + + netdev_close(netdev); } else { unixctl_command_reply_error(conn, "Unknown Dummy Interface"); + netdev_close(netdev); return; } } else { - struct shash_node *node; + struct netdev_dummy *netdev; - SHASH_FOR_EACH (node, &dummy_netdev_devs) { - netdev_dev_dummy_set_admin_state(node->data, up); + ovs_mutex_lock(&dummy_list_mutex); + LIST_FOR_EACH (netdev, list_node, &dummy_list) { + ovs_mutex_lock(&netdev->mutex); + netdev_dummy_set_admin_state__(netdev, up); + ovs_mutex_unlock(&netdev->mutex); } + ovs_mutex_unlock(&dummy_list_mutex); } unixctl_command_reply(conn, "OK"); } @@ -519,14 +902,22 @@ netdev_dummy_register(bool override) SSET_FOR_EACH (type, &types) { if (!netdev_unregister_provider(type)) { struct netdev_class *class; + int error; - class = xmalloc(sizeof *class); - *class = dummy_class; + class = xmemdup(&dummy_class, sizeof dummy_class); class->type = xstrdup(type); - netdev_register_provider(class); + error = netdev_register_provider(class); + if (error) { + VLOG_ERR("%s: failed to register netdev provider (%s)", + type, ovs_strerror(error)); + free(CONST_CAST(char *, class->type)); + free(class); + } } } sset_destroy(&types); } netdev_register_provider(&dummy_class); + + netdev_vport_tunnel_register(); }