Merge branch 'mainstream'
[sliver-openvswitch.git] / lib / netdev-dummy.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "dummy.h"
20
21 #include <errno.h>
22
23 #include "flow.h"
24 #include "list.h"
25 #include "netdev-provider.h"
26 #include "netdev-vport.h"
27 #include "odp-util.h"
28 #include "ofp-print.h"
29 #include "ofpbuf.h"
30 #include "packets.h"
31 #include "pcap-file.h"
32 #include "poll-loop.h"
33 #include "shash.h"
34 #include "sset.h"
35 #include "stream.h"
36 #include "unaligned.h"
37 #include "timeval.h"
38 #include "unixctl.h"
39 #include "vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
42
43 struct dummy_stream {
44     struct stream *stream;
45     struct ofpbuf rxbuf;
46     struct list txq;
47 };
48
49 /* Protects 'dummy_list'. */
50 static struct ovs_mutex dummy_list_mutex = OVS_MUTEX_INITIALIZER;
51
52 /* Contains all 'struct dummy_dev's. */
53 static struct list dummy_list OVS_GUARDED_BY(dummy_list_mutex)
54     = LIST_INITIALIZER(&dummy_list);
55
56 struct netdev_dummy {
57     struct netdev up;
58
59     /* In dummy_list. */
60     struct list list_node OVS_GUARDED_BY(dummy_list_mutex);
61
62     /* Protects all members below. */
63     struct ovs_mutex mutex OVS_ACQ_AFTER(dummy_list_mutex);
64
65     uint8_t hwaddr[ETH_ADDR_LEN] OVS_GUARDED;
66     int mtu OVS_GUARDED;
67     struct netdev_stats stats OVS_GUARDED;
68     enum netdev_flags flags OVS_GUARDED;
69     unsigned int change_seq OVS_GUARDED;
70     int ifindex OVS_GUARDED;
71
72     struct pstream *pstream OVS_GUARDED;
73     struct dummy_stream *streams OVS_GUARDED;
74     size_t n_streams OVS_GUARDED;
75
76     FILE *tx_pcap, *rx_pcap OVS_GUARDED;
77
78     struct list rxes OVS_GUARDED; /* List of child "netdev_rx_dummy"s. */
79 };
80
81 /* Max 'recv_queue_len' in struct netdev_dummy. */
82 #define NETDEV_DUMMY_MAX_QUEUE 100
83
84 struct netdev_rx_dummy {
85     struct netdev_rx up;
86     struct list node;           /* In netdev_dummy's "rxes" list. */
87     struct list recv_queue;
88     int recv_queue_len;         /* list_size(&recv_queue). */
89     bool listening;
90 };
91
92 static unixctl_cb_func netdev_dummy_set_admin_state;
93 static int netdev_dummy_construct(struct netdev *);
94 static void netdev_dummy_poll_notify(struct netdev_dummy *netdev)
95     OVS_REQUIRES(netdev->mutex);
96 static void netdev_dummy_queue_packet(struct netdev_dummy *, struct ofpbuf *);
97
98 static void dummy_stream_close(struct dummy_stream *);
99
100 static bool
101 is_dummy_class(const struct netdev_class *class)
102 {
103     return class->construct == netdev_dummy_construct;
104 }
105
106 static struct netdev_dummy *
107 netdev_dummy_cast(const struct netdev *netdev)
108 {
109     ovs_assert(is_dummy_class(netdev_get_class(netdev)));
110     return CONTAINER_OF(netdev, struct netdev_dummy, up);
111 }
112
113 static struct netdev_rx_dummy *
114 netdev_rx_dummy_cast(const struct netdev_rx *rx)
115 {
116     ovs_assert(is_dummy_class(netdev_get_class(rx->netdev)));
117     return CONTAINER_OF(rx, struct netdev_rx_dummy, up);
118 }
119
120 static void
121 netdev_dummy_run(void)
122 {
123     struct netdev_dummy *dev;
124
125     ovs_mutex_lock(&dummy_list_mutex);
126     LIST_FOR_EACH (dev, list_node, &dummy_list) {
127         size_t i;
128
129         ovs_mutex_lock(&dev->mutex);
130
131         if (dev->pstream) {
132             struct stream *new_stream;
133             int error;
134
135             error = pstream_accept(dev->pstream, &new_stream);
136             if (!error) {
137                 struct dummy_stream *s;
138
139                 dev->streams = xrealloc(dev->streams,
140                                         ((dev->n_streams + 1)
141                                          * sizeof *dev->streams));
142                 s = &dev->streams[dev->n_streams++];
143                 s->stream = new_stream;
144                 ofpbuf_init(&s->rxbuf, 2048);
145                 list_init(&s->txq);
146             } else if (error != EAGAIN) {
147                 VLOG_WARN("%s: accept failed (%s)",
148                           pstream_get_name(dev->pstream), ovs_strerror(error));
149                 pstream_close(dev->pstream);
150                 dev->pstream = NULL;
151             }
152         }
153
154         for (i = 0; i < dev->n_streams; i++) {
155             struct dummy_stream *s = &dev->streams[i];
156             int error = 0;
157             size_t n;
158
159             stream_run(s->stream);
160
161             if (!list_is_empty(&s->txq)) {
162                 struct ofpbuf *txbuf;
163                 int retval;
164
165                 txbuf = ofpbuf_from_list(list_front(&s->txq));
166                 retval = stream_send(s->stream, txbuf->data, txbuf->size);
167                 if (retval > 0) {
168                     ofpbuf_pull(txbuf, retval);
169                     if (!txbuf->size) {
170                         list_remove(&txbuf->list_node);
171                         ofpbuf_delete(txbuf);
172                     }
173                 } else if (retval != -EAGAIN) {
174                     error = -retval;
175                 }
176             }
177
178             if (!error) {
179                 if (s->rxbuf.size < 2) {
180                     n = 2 - s->rxbuf.size;
181                 } else {
182                     uint16_t frame_len;
183
184                     frame_len = ntohs(get_unaligned_be16(s->rxbuf.data));
185                     if (frame_len < ETH_HEADER_LEN) {
186                         error = EPROTO;
187                         n = 0;
188                     } else {
189                         n = (2 + frame_len) - s->rxbuf.size;
190                     }
191                 }
192             }
193             if (!error) {
194                 int retval;
195
196                 ofpbuf_prealloc_tailroom(&s->rxbuf, n);
197                 retval = stream_recv(s->stream, ofpbuf_tail(&s->rxbuf), n);
198                 if (retval > 0) {
199                     s->rxbuf.size += retval;
200                     if (retval == n && s->rxbuf.size > 2) {
201                         ofpbuf_pull(&s->rxbuf, 2);
202                         netdev_dummy_queue_packet(dev,
203                                                   ofpbuf_clone(&s->rxbuf));
204                         ofpbuf_clear(&s->rxbuf);
205                     }
206                 } else if (retval != -EAGAIN) {
207                     error = (retval < 0 ? -retval
208                              : s->rxbuf.size ? EPROTO
209                              : EOF);
210                 }
211             }
212
213             if (error) {
214                 VLOG_DBG("%s: closing connection (%s)",
215                          stream_get_name(s->stream),
216                          ovs_retval_to_string(error));
217                 dummy_stream_close(&dev->streams[i]);
218                 dev->streams[i] = dev->streams[--dev->n_streams];
219             }
220         }
221
222         ovs_mutex_unlock(&dev->mutex);
223     }
224     ovs_mutex_unlock(&dummy_list_mutex);
225 }
226
227 static void
228 dummy_stream_close(struct dummy_stream *s)
229 {
230     stream_close(s->stream);
231     ofpbuf_uninit(&s->rxbuf);
232     ofpbuf_list_delete(&s->txq);
233 }
234
235 static void
236 netdev_dummy_wait(void)
237 {
238     struct netdev_dummy *dev;
239
240     ovs_mutex_lock(&dummy_list_mutex);
241     LIST_FOR_EACH (dev, list_node, &dummy_list) {
242         size_t i;
243
244         ovs_mutex_lock(&dev->mutex);
245         if (dev->pstream) {
246             pstream_wait(dev->pstream);
247         }
248         for (i = 0; i < dev->n_streams; i++) {
249             struct dummy_stream *s = &dev->streams[i];
250
251             stream_run_wait(s->stream);
252             if (!list_is_empty(&s->txq)) {
253                 stream_send_wait(s->stream);
254             }
255             stream_recv_wait(s->stream);
256         }
257         ovs_mutex_unlock(&dev->mutex);
258     }
259     ovs_mutex_unlock(&dummy_list_mutex);
260 }
261
262 static struct netdev *
263 netdev_dummy_alloc(void)
264 {
265     struct netdev_dummy *netdev = xzalloc(sizeof *netdev);
266     return &netdev->up;
267 }
268
269 static int
270 netdev_dummy_construct(struct netdev *netdev_)
271 {
272     static atomic_uint next_n = ATOMIC_VAR_INIT(0xaa550000);
273     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
274     unsigned int n;
275
276     atomic_add(&next_n, 1, &n);
277
278     ovs_mutex_init(&netdev->mutex);
279     ovs_mutex_lock(&netdev->mutex);
280     netdev->hwaddr[0] = 0xaa;
281     netdev->hwaddr[1] = 0x55;
282     netdev->hwaddr[2] = n >> 24;
283     netdev->hwaddr[3] = n >> 16;
284     netdev->hwaddr[4] = n >> 8;
285     netdev->hwaddr[5] = n;
286     netdev->mtu = 1500;
287     netdev->flags = 0;
288     netdev->change_seq = 1;
289     netdev->ifindex = -EOPNOTSUPP;
290
291     netdev->pstream = NULL;
292     netdev->streams = NULL;
293     netdev->n_streams = 0;
294
295     list_init(&netdev->rxes);
296     ovs_mutex_unlock(&netdev->mutex);
297
298     ovs_mutex_lock(&dummy_list_mutex);
299     list_push_back(&dummy_list, &netdev->list_node);
300     ovs_mutex_unlock(&dummy_list_mutex);
301
302     return 0;
303 }
304
305 static void
306 netdev_dummy_destruct(struct netdev *netdev_)
307 {
308     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
309     size_t i;
310
311     ovs_mutex_lock(&dummy_list_mutex);
312     list_remove(&netdev->list_node);
313     ovs_mutex_unlock(&dummy_list_mutex);
314
315     ovs_mutex_lock(&netdev->mutex);
316     pstream_close(netdev->pstream);
317     for (i = 0; i < netdev->n_streams; i++) {
318         dummy_stream_close(&netdev->streams[i]);
319     }
320     free(netdev->streams);
321     ovs_mutex_unlock(&netdev->mutex);
322     ovs_mutex_destroy(&netdev->mutex);
323 }
324
325 static void
326 netdev_dummy_dealloc(struct netdev *netdev_)
327 {
328     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
329
330     free(netdev);
331 }
332
333 static int
334 netdev_dummy_get_config(const struct netdev *netdev_, struct smap *args)
335 {
336     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
337
338     ovs_mutex_lock(&netdev->mutex);
339
340     if (netdev->ifindex >= 0) {
341         smap_add_format(args, "ifindex", "%d", netdev->ifindex);
342     }
343
344     if (netdev->pstream) {
345         smap_add(args, "pstream", pstream_get_name(netdev->pstream));
346     }
347
348     ovs_mutex_unlock(&netdev->mutex);
349     return 0;
350 }
351
352 static int
353 netdev_dummy_set_config(struct netdev *netdev_, const struct smap *args)
354 {
355     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
356     const char *pstream;
357     const char *pcap;
358
359     ovs_mutex_lock(&netdev->mutex);
360     netdev->ifindex = smap_get_int(args, "ifindex", -EOPNOTSUPP);
361
362     pstream = smap_get(args, "pstream");
363     if (!pstream
364         || !netdev->pstream
365         || strcmp(pstream_get_name(netdev->pstream), pstream)) {
366         pstream_close(netdev->pstream);
367         netdev->pstream = NULL;
368
369         if (pstream) {
370             int error;
371
372             error = pstream_open(pstream, &netdev->pstream, DSCP_DEFAULT);
373             if (error) {
374                 VLOG_WARN("%s: open failed (%s)",
375                           pstream, ovs_strerror(error));
376             }
377         }
378     }
379
380     if (netdev->rx_pcap) {
381         fclose(netdev->rx_pcap);
382     }
383     if (netdev->tx_pcap && netdev->tx_pcap != netdev->rx_pcap) {
384         fclose(netdev->tx_pcap);
385     }
386     netdev->rx_pcap = netdev->tx_pcap = NULL;
387     pcap = smap_get(args, "pcap");
388     if (pcap) {
389         netdev->rx_pcap = netdev->tx_pcap = pcap_open(pcap, "ab");
390     } else {
391         const char *rx_pcap = smap_get(args, "rx_pcap");
392         const char *tx_pcap = smap_get(args, "tx_pcap");
393
394         if (rx_pcap) {
395             netdev->rx_pcap = pcap_open(rx_pcap, "ab");
396         }
397         if (tx_pcap) {
398             netdev->tx_pcap = pcap_open(tx_pcap, "ab");
399         }
400     }
401
402     ovs_mutex_unlock(&netdev->mutex);
403
404     return 0;
405 }
406
407 static struct netdev_rx *
408 netdev_dummy_rx_alloc(void)
409 {
410     struct netdev_rx_dummy *rx = xzalloc(sizeof *rx);
411     return &rx->up;
412 }
413
414 static int
415 netdev_dummy_rx_construct(struct netdev_rx *rx_)
416 {
417     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
418     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
419
420     ovs_mutex_lock(&netdev->mutex);
421     list_push_back(&netdev->rxes, &rx->node);
422     list_init(&rx->recv_queue);
423     rx->recv_queue_len = 0;
424     ovs_mutex_unlock(&netdev->mutex);
425
426     return 0;
427 }
428
429 static void
430 netdev_dummy_rx_destruct(struct netdev_rx *rx_)
431 {
432     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
433     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
434
435     ovs_mutex_lock(&netdev->mutex);
436     list_remove(&rx->node);
437     ofpbuf_list_delete(&rx->recv_queue);
438     ovs_mutex_unlock(&netdev->mutex);
439 }
440
441 static void
442 netdev_dummy_rx_dealloc(struct netdev_rx *rx_)
443 {
444     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
445
446     free(rx);
447 }
448
449 static int
450 netdev_dummy_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size)
451 {
452     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
453     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
454     struct ofpbuf *packet;
455     int retval;
456
457     ovs_mutex_lock(&netdev->mutex);
458     if (!list_is_empty(&rx->recv_queue)) {
459         packet = ofpbuf_from_list(list_pop_front(&rx->recv_queue));
460         rx->recv_queue_len--;
461     } else {
462         packet = NULL;
463     }
464     ovs_mutex_unlock(&netdev->mutex);
465
466     if (!packet) {
467         return -EAGAIN;
468     }
469
470     if (packet->size <= size) {
471         memcpy(buffer, packet->data, packet->size);
472         retval = packet->size;
473
474         ovs_mutex_lock(&netdev->mutex);
475         netdev->stats.rx_packets++;
476         netdev->stats.rx_bytes += packet->size;
477         ovs_mutex_unlock(&netdev->mutex);
478     } else {
479         retval = -EMSGSIZE;
480     }
481     ofpbuf_delete(packet);
482
483     return retval;
484 }
485
486 static void
487 netdev_dummy_rx_wait(struct netdev_rx *rx_)
488 {
489     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
490     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
491
492     ovs_mutex_lock(&netdev->mutex);
493     if (!list_is_empty(&rx->recv_queue)) {
494         poll_immediate_wake();
495     }
496     ovs_mutex_unlock(&netdev->mutex);
497 }
498
499 static int
500 netdev_dummy_rx_drain(struct netdev_rx *rx_)
501 {
502     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
503     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
504
505     ovs_mutex_lock(&netdev->mutex);
506     ofpbuf_list_delete(&rx->recv_queue);
507     rx->recv_queue_len = 0;
508     ovs_mutex_unlock(&netdev->mutex);
509
510     return 0;
511 }
512
513 static int
514 netdev_dummy_send(struct netdev *netdev, const void *buffer, size_t size)
515 {
516     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
517     size_t i;
518
519     if (size < ETH_HEADER_LEN) {
520         return EMSGSIZE;
521     } else {
522         const struct eth_header *eth = buffer;
523         int max_size;
524
525         ovs_mutex_lock(&dev->mutex);
526         max_size = dev->mtu + ETH_HEADER_LEN;
527         ovs_mutex_unlock(&dev->mutex);
528
529         if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
530             max_size += VLAN_HEADER_LEN;
531         }
532         if (size > max_size) {
533             return EMSGSIZE;
534         }
535     }
536
537     ovs_mutex_lock(&dev->mutex);
538     dev->stats.tx_packets++;
539     dev->stats.tx_bytes += size;
540
541     if (dev->tx_pcap) {
542         struct ofpbuf packet;
543
544         ofpbuf_use_const(&packet, buffer, size);
545         pcap_write(dev->tx_pcap, &packet);
546         fflush(dev->tx_pcap);
547     }
548
549     for (i = 0; i < dev->n_streams; i++) {
550         struct dummy_stream *s = &dev->streams[i];
551
552         if (list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) {
553             struct ofpbuf *b;
554
555             b = ofpbuf_clone_data_with_headroom(buffer, size, 2);
556             put_unaligned_be16(ofpbuf_push_uninit(b, 2), htons(size));
557             list_push_back(&s->txq, &b->list_node);
558         }
559     }
560     ovs_mutex_unlock(&dev->mutex);
561
562     return 0;
563 }
564
565 static int
566 netdev_dummy_set_etheraddr(struct netdev *netdev,
567                            const uint8_t mac[ETH_ADDR_LEN])
568 {
569     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
570
571     ovs_mutex_lock(&dev->mutex);
572     if (!eth_addr_equals(dev->hwaddr, mac)) {
573         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
574         netdev_dummy_poll_notify(dev);
575     }
576     ovs_mutex_unlock(&dev->mutex);
577
578     return 0;
579 }
580
581 static int
582 netdev_dummy_get_etheraddr(const struct netdev *netdev,
583                            uint8_t mac[ETH_ADDR_LEN])
584 {
585     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
586
587     ovs_mutex_lock(&dev->mutex);
588     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
589     ovs_mutex_unlock(&dev->mutex);
590
591     return 0;
592 }
593
594 static int
595 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
596 {
597     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
598
599     ovs_mutex_lock(&dev->mutex);
600     *mtup = dev->mtu;
601     ovs_mutex_unlock(&dev->mutex);
602
603     return 0;
604 }
605
606 static int
607 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
608 {
609     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
610
611     ovs_mutex_lock(&dev->mutex);
612     dev->mtu = mtu;
613     ovs_mutex_unlock(&dev->mutex);
614
615     return 0;
616 }
617
618 static int
619 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
620 {
621     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
622
623     ovs_mutex_lock(&dev->mutex);
624     *stats = dev->stats;
625     ovs_mutex_unlock(&dev->mutex);
626
627     return 0;
628 }
629
630 static int
631 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
632 {
633     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
634
635     ovs_mutex_lock(&dev->mutex);
636     dev->stats = *stats;
637     ovs_mutex_unlock(&dev->mutex);
638
639     return 0;
640 }
641
642 static int
643 netdev_dummy_get_ifindex(const struct netdev *netdev)
644 {
645     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
646     int ifindex;
647
648     ovs_mutex_lock(&dev->mutex);
649     ifindex = dev->ifindex;
650     ovs_mutex_unlock(&dev->mutex);
651
652     return ifindex;
653 }
654
655 static int
656 netdev_dummy_update_flags__(struct netdev_dummy *netdev,
657                             enum netdev_flags off, enum netdev_flags on,
658                             enum netdev_flags *old_flagsp)
659     OVS_REQUIRES(netdev->mutex)
660 {
661     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
662         return EINVAL;
663     }
664
665     *old_flagsp = netdev->flags;
666     netdev->flags |= on;
667     netdev->flags &= ~off;
668     if (*old_flagsp != netdev->flags) {
669         netdev_dummy_poll_notify(netdev);
670     }
671
672     return 0;
673 }
674
675 static int
676 netdev_dummy_update_flags(struct netdev *netdev_,
677                           enum netdev_flags off, enum netdev_flags on,
678                           enum netdev_flags *old_flagsp)
679 {
680     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
681     int error;
682
683     ovs_mutex_lock(&netdev->mutex);
684     error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp);
685     ovs_mutex_unlock(&netdev->mutex);
686
687     return error;
688 }
689
690 static unsigned int
691 netdev_dummy_change_seq(const struct netdev *netdev_)
692 {
693     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
694     unsigned int change_seq;
695
696     ovs_mutex_lock(&netdev->mutex);
697     change_seq = netdev->change_seq;
698     ovs_mutex_unlock(&netdev->mutex);
699
700     return change_seq;
701 }
702 \f
703 /* Helper functions. */
704
705 static void
706 netdev_dummy_poll_notify(struct netdev_dummy *dev)
707 {
708     dev->change_seq++;
709     if (!dev->change_seq) {
710         dev->change_seq++;
711     }
712 }
713
714 static const struct netdev_class dummy_class = {
715     "dummy",
716     NULL,                       /* init */
717     netdev_dummy_run,
718     netdev_dummy_wait,
719
720     netdev_dummy_alloc,
721     netdev_dummy_construct,
722     netdev_dummy_destruct,
723     netdev_dummy_dealloc,
724     netdev_dummy_get_config,
725     netdev_dummy_set_config,
726     NULL,                       /* get_tunnel_config */
727
728     netdev_dummy_send,          /* send */
729     NULL,                       /* send_wait */
730
731     netdev_dummy_set_etheraddr,
732     netdev_dummy_get_etheraddr,
733     netdev_dummy_get_mtu,
734     netdev_dummy_set_mtu,
735     netdev_dummy_get_ifindex,
736     NULL,                       /* get_carrier */
737     NULL,                       /* get_carrier_resets */
738     NULL,                       /* get_miimon */
739     netdev_dummy_get_stats,
740     netdev_dummy_set_stats,
741
742     NULL,                       /* get_features */
743     NULL,                       /* set_advertisements */
744
745     NULL,                       /* set_policing */
746     NULL,                       /* get_qos_types */
747     NULL,                       /* get_qos_capabilities */
748     NULL,                       /* get_qos */
749     NULL,                       /* set_qos */
750     NULL,                       /* get_queue */
751     NULL,                       /* set_queue */
752     NULL,                       /* delete_queue */
753     NULL,                       /* get_queue_stats */
754     NULL,                       /* queue_dump_start */
755     NULL,                       /* queue_dump_next */
756     NULL,                       /* queue_dump_done */
757     NULL,                       /* dump_queue_stats */
758
759     NULL,                       /* get_in4 */
760     NULL,                       /* set_in4 */
761     NULL,                       /* get_in6 */
762     NULL,                       /* add_router */
763     NULL,                       /* get_next_hop */
764     NULL,                       /* get_status */
765     NULL,                       /* arp_lookup */
766
767     netdev_dummy_update_flags,
768
769     netdev_dummy_change_seq,
770
771     netdev_dummy_rx_alloc,
772     netdev_dummy_rx_construct,
773     netdev_dummy_rx_destruct,
774     netdev_dummy_rx_dealloc,
775     netdev_dummy_rx_recv,
776     netdev_dummy_rx_wait,
777     netdev_dummy_rx_drain,
778 };
779
780 static struct ofpbuf *
781 eth_from_packet_or_flow(const char *s)
782 {
783     enum odp_key_fitness fitness;
784     struct ofpbuf *packet;
785     struct ofpbuf odp_key;
786     struct flow flow;
787     int error;
788
789     if (!eth_from_hex(s, &packet)) {
790         return packet;
791     }
792
793     /* Convert string to datapath key.
794      *
795      * It would actually be nicer to parse an OpenFlow-like flow key here, but
796      * the code for that currently calls exit() on parse error.  We have to
797      * settle for parsing a datapath key for now.
798      */
799     ofpbuf_init(&odp_key, 0);
800     error = odp_flow_from_string(s, NULL, &odp_key, NULL);
801     if (error) {
802         ofpbuf_uninit(&odp_key);
803         return NULL;
804     }
805
806     /* Convert odp_key to flow. */
807     fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
808     if (fitness == ODP_FIT_ERROR) {
809         ofpbuf_uninit(&odp_key);
810         return NULL;
811     }
812
813     packet = ofpbuf_new(0);
814     flow_compose(packet, &flow);
815
816     ofpbuf_uninit(&odp_key);
817     return packet;
818 }
819
820 static void
821 netdev_dummy_queue_packet__(struct netdev_rx_dummy *rx, struct ofpbuf *packet)
822 {
823     list_push_back(&rx->recv_queue, &packet->list_node);
824     rx->recv_queue_len++;
825 }
826
827 static void
828 netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct ofpbuf *packet)
829     OVS_REQUIRES(dummy->mutex)
830 {
831     struct netdev_rx_dummy *rx, *prev;
832
833     if (dummy->rx_pcap) {
834         pcap_write(dummy->rx_pcap, packet);
835         fflush(dummy->rx_pcap);
836     }
837     prev = NULL;
838     LIST_FOR_EACH (rx, node, &dummy->rxes) {
839         if (rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
840             if (prev) {
841                 netdev_dummy_queue_packet__(prev, ofpbuf_clone(packet));
842             }
843             prev = rx;
844         }
845     }
846     if (prev) {
847         netdev_dummy_queue_packet__(prev, packet);
848     } else {
849         ofpbuf_delete(packet);
850     }
851 }
852
853 static void
854 netdev_dummy_receive(struct unixctl_conn *conn,
855                      int argc, const char *argv[], void *aux OVS_UNUSED)
856 {
857     struct netdev_dummy *dummy_dev;
858     struct netdev *netdev;
859     int i;
860
861     netdev = netdev_from_name(argv[1]);
862     if (!netdev || !is_dummy_class(netdev->netdev_class)) {
863         unixctl_command_reply_error(conn, "no such dummy netdev");
864         goto exit;
865     }
866     dummy_dev = netdev_dummy_cast(netdev);
867
868     for (i = 2; i < argc; i++) {
869         struct ofpbuf *packet;
870
871         packet = eth_from_packet_or_flow(argv[i]);
872         if (!packet) {
873             unixctl_command_reply_error(conn, "bad packet syntax");
874             goto exit;
875         }
876
877         ovs_mutex_lock(&dummy_dev->mutex);
878         netdev_dummy_queue_packet(dummy_dev, packet);
879         ovs_mutex_unlock(&dummy_dev->mutex);
880     }
881
882     unixctl_command_reply(conn, NULL);
883
884 exit:
885     netdev_close(netdev);
886 }
887
888 static void
889 netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state)
890     OVS_REQUIRES(dev->mutex)
891 {
892     enum netdev_flags old_flags;
893
894     if (admin_state) {
895         netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags);
896     } else {
897         netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags);
898     }
899 }
900
901 static void
902 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
903                              const char *argv[], void *aux OVS_UNUSED)
904 {
905     bool up;
906
907     if (!strcasecmp(argv[argc - 1], "up")) {
908         up = true;
909     } else if ( !strcasecmp(argv[argc - 1], "down")) {
910         up = false;
911     } else {
912         unixctl_command_reply_error(conn, "Invalid Admin State");
913         return;
914     }
915
916     if (argc > 2) {
917         struct netdev *netdev = netdev_from_name(argv[1]);
918         if (netdev && is_dummy_class(netdev->netdev_class)) {
919             struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
920
921             ovs_mutex_lock(&dummy_dev->mutex);
922             netdev_dummy_set_admin_state__(dummy_dev, up);
923             ovs_mutex_unlock(&dummy_dev->mutex);
924
925             netdev_close(netdev);
926         } else {
927             unixctl_command_reply_error(conn, "Unknown Dummy Interface");
928             netdev_close(netdev);
929             return;
930         }
931     } else {
932         struct netdev_dummy *netdev;
933
934         ovs_mutex_lock(&dummy_list_mutex);
935         LIST_FOR_EACH (netdev, list_node, &dummy_list) {
936             ovs_mutex_lock(&netdev->mutex);
937             netdev_dummy_set_admin_state__(netdev, up);
938             ovs_mutex_unlock(&netdev->mutex);
939         }
940         ovs_mutex_unlock(&dummy_list_mutex);
941     }
942     unixctl_command_reply(conn, "OK");
943 }
944
945 void
946 netdev_dummy_register(bool override)
947 {
948     unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
949                              2, INT_MAX, netdev_dummy_receive, NULL);
950     unixctl_command_register("netdev-dummy/set-admin-state",
951                              "[netdev] up|down", 1, 2,
952                              netdev_dummy_set_admin_state, NULL);
953
954     if (override) {
955         struct sset types;
956         const char *type;
957
958         sset_init(&types);
959         netdev_enumerate_types(&types);
960         SSET_FOR_EACH (type, &types) {
961             if (!strcmp(type, "patch")) {
962                 continue;
963             }
964             if (!netdev_unregister_provider(type)) {
965                 struct netdev_class *class;
966                 int error;
967
968                 class = xmemdup(&dummy_class, sizeof dummy_class);
969                 class->type = xstrdup(type);
970                 error = netdev_register_provider(class);
971                 if (error) {
972                     VLOG_ERR("%s: failed to register netdev provider (%s)",
973                              type, ovs_strerror(error));
974                     free(CONST_CAST(char *, class->type));
975                     free(class);
976                 }
977             }
978         }
979         sset_destroy(&types);
980     }
981     netdev_register_provider(&dummy_class);
982
983     netdev_vport_tunnel_register();
984 }