[ovs-dev] [oftest 4/5] netdev-dummy: Add "pstream" option for connecting a dummy to a process.

Ben Pfaff blp at nicira.com
Fri Feb 1 22:23:57 UTC 2013


Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 lib/netdev-dummy.c |  284 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 272 insertions(+), 12 deletions(-)

diff --git a/lib/netdev-dummy.c b/lib/netdev-dummy.c
index 2884a80..30bd794 100644
--- a/lib/netdev-dummy.c
+++ b/lib/netdev-dummy.c
@@ -31,6 +31,8 @@
 #include "poll-loop.h"
 #include "shash.h"
 #include "sset.h"
+#include "stream.h"
+#include "unaligned.h"
 #include "unixctl.h"
 #include "vlog.h"
 
@@ -42,6 +44,12 @@ VLOG_DEFINE_THIS_MODULE(netdev_dummy);
 #define FREE_BSD 0
 #endif
 
+struct dummy_stream {
+    struct stream *stream;
+    struct ofpbuf rxbuf;
+    struct list txq;
+};
+
 struct netdev_dev_dummy {
     struct netdev_dev netdev_dev;
     uint8_t hwaddr[ETH_ADDR_LEN];
@@ -50,6 +58,10 @@ struct netdev_dev_dummy {
     enum netdev_flags flags;
     unsigned int change_seq;
 
+    struct pstream *pstream;
+    struct dummy_stream *streams;
+    size_t n_streams;
+
     struct list devs;           /* List of child "netdev_dummy"s. */
 };
 
@@ -74,6 +86,10 @@ 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 void netdev_dummy_queue_packet(struct netdev_dev_dummy *,
+                                      struct ofpbuf *);
+
+static void dummy_stream_close(struct dummy_stream *);
 
 static bool
 is_dummy_class(const struct netdev_class *class)
@@ -96,6 +112,140 @@ netdev_dummy_cast(const struct netdev *netdev)
     return CONTAINER_OF(netdev, struct netdev_dummy, netdev);
 }
 
+static void
+netdev_dummy_run(void)
+{
+    struct shash_node *node;
+
+    SHASH_FOR_EACH (node, &dummy_netdev_devs) {
+        struct netdev_dev_dummy *dev = node->data;
+        size_t i;
+
+        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), strerror(error));
+                pstream_close(dev->pstream);
+                dev->pstream = NULL;
+            }
+        }
+
+        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;
+                }
+            }
+
+            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);
+                }
+            }
+
+            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];
+            }
+        }
+    }
+}
+
+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_wait(void)
+{
+    struct shash_node *node;
+
+    SHASH_FOR_EACH (node, &dummy_netdev_devs) {
+        struct netdev_dev_dummy *dev = node->data;
+        size_t i;
+
+        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);
+        }
+    }
+}
+
 static int
 netdev_dummy_create(const struct netdev_class *class, const char *name,
                     struct netdev_dev **netdev_devp)
@@ -116,6 +266,10 @@ netdev_dummy_create(const struct netdev_class *class, const char *name,
     netdev_dev->change_seq = 1;
     list_init(&netdev_dev->devs);
 
+    netdev_dev->pstream = NULL;
+    netdev_dev->streams = NULL;
+    netdev_dev->n_streams = 0;
+
     shash_add(&dummy_netdev_devs, name, netdev_dev);
 
     n++;
@@ -129,13 +283,66 @@ static void
 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
 {
     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
+    size_t i;
 
     shash_find_and_delete(&dummy_netdev_devs,
                           netdev_dev_get_name(netdev_dev_));
+    pstream_close(netdev_dev->pstream);
+    for (i = 0; i < netdev_dev->n_streams; i++) {
+        dummy_stream_close(&netdev_dev->streams[i]);
+    }
+    free(netdev_dev->streams);
     free(netdev_dev);
 }
 
 static int
+netdev_dummy_set_config(struct netdev_dev *netdev_dev_,
+                        const struct smap *args)
+{
+    struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
+    const char *name = netdev_dev_get_name(netdev_dev_);
+    const struct smap_node *node;
+    const char *pstream;
+
+    SMAP_FOR_EACH (node, args) {
+        if (strcmp(node->key, "pstream")) {
+            VLOG_WARN("%s: unknown option '%s'", name, node->key);
+            return EINVAL;
+        }
+    }
+
+    pstream = smap_get(args, "pstream");
+    if (!pstream
+        || !netdev_dev->pstream
+        || strcmp(pstream_get_name(netdev_dev->pstream), pstream)) {
+        pstream_close(netdev_dev->pstream);
+        netdev_dev->pstream = NULL;
+
+        if (pstream) {
+            int error;
+
+            error = pstream_open(pstream, &netdev_dev->pstream, DSCP_DEFAULT);
+            if (error) {
+                VLOG_WARN("%s: open failed (%s)", pstream, strerror(error));
+            }
+        }
+    }
+
+    return 0;
+}
+
+static int
+netdev_dummy_get_config(struct netdev_dev *netdev_dev_, struct smap *args)
+{
+    struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
+
+    if (netdev_dev->pstream) {
+        smap_add(args, "pstream", pstream_get_name(netdev_dev->pstream));
+    }
+    return 0;
+}
+
+static int
 netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
 {
     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
@@ -212,6 +419,46 @@ netdev_dummy_drain(struct netdev *netdev_)
 }
 
 static int
+netdev_dummy_send(struct netdev *netdev, const void *buffer, size_t size)
+{
+    struct netdev_dev_dummy *dev =
+        netdev_dev_dummy_cast(netdev_get_dev(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;
+        }
+    }
+
+    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);
+        }
+    }
+
+    return 0;
+}
+
+static int
 netdev_dummy_set_etheraddr(struct netdev *netdev,
                            const uint8_t mac[ETH_ADDR_LEN])
 {
@@ -326,13 +573,13 @@ 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_run,
+    netdev_dummy_wait,
 
     netdev_dummy_create,
     netdev_dummy_destroy,
-    NULL,                       /* get_config */
-    NULL,                       /* set_config */
+    netdev_dummy_get_config,
+    netdev_dummy_set_config,
     NULL,                       /* get_tunnel_config */
 
     netdev_dummy_open,
@@ -343,7 +590,7 @@ static const struct netdev_class dummy_class = {
     netdev_dummy_recv_wait,
     netdev_dummy_drain,
 
-    NULL,                       /* send */
+    netdev_dummy_send,
     NULL,                       /* send_wait */
 
     netdev_dummy_set_etheraddr,
@@ -426,18 +673,32 @@ eth_from_packet_or_flow(const char *s)
 }
 
 static void
+netdev_dummy_queue_packet__(struct netdev_dummy *dev, struct ofpbuf *packet)
+{
+    list_push_back(&dev->recv_queue, &packet->list_node);
+    dev->recv_queue_len++;
+}
+
+static void
 netdev_dummy_queue_packet(struct netdev_dev_dummy *dummy_dev,
-                          const void *data, size_t size)
+                          struct ofpbuf *packet)
 {
-    struct netdev_dummy *dev;
+    struct netdev_dummy *dev, *prev;
 
+    prev = NULL;
     LIST_FOR_EACH (dev, node, &dummy_dev->devs) {
         if (dev->listening && dev->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
-            struct ofpbuf *packet = ofpbuf_clone_data(data, size);
-            list_push_back(&dev->recv_queue, &packet->list_node);
-            dev->recv_queue_len++;
+            if (prev) {
+                netdev_dummy_queue_packet__(prev, ofpbuf_clone(packet));
+            }
+            prev = dev;
         }
     }
+    if (prev) {
+        netdev_dummy_queue_packet__(prev, packet);
+    } else {
+        ofpbuf_delete(packet);
+    }
 }
 
 static void
@@ -464,8 +725,7 @@ netdev_dummy_receive(struct unixctl_conn *conn,
             return;
         }
 
-        netdev_dummy_queue_packet(dummy_dev, packet->data, packet->size);
-        ofpbuf_delete(packet);
+        netdev_dummy_queue_packet(dummy_dev, packet);
     }
 
     unixctl_command_reply(conn, NULL);
-- 
1.7.2.5




More information about the dev mailing list