netdev-dummy: Add pcap feature.
[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     } else {
474         retval = -EMSGSIZE;
475     }
476     ofpbuf_delete(packet);
477
478     return retval;
479 }
480
481 static void
482 netdev_dummy_rx_wait(struct netdev_rx *rx_)
483 {
484     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
485     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
486
487     ovs_mutex_lock(&netdev->mutex);
488     if (!list_is_empty(&rx->recv_queue)) {
489         poll_immediate_wake();
490     }
491     ovs_mutex_unlock(&netdev->mutex);
492 }
493
494 static int
495 netdev_dummy_rx_drain(struct netdev_rx *rx_)
496 {
497     struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
498     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
499
500     ovs_mutex_lock(&netdev->mutex);
501     ofpbuf_list_delete(&rx->recv_queue);
502     rx->recv_queue_len = 0;
503     ovs_mutex_unlock(&netdev->mutex);
504
505     return 0;
506 }
507
508 static int
509 netdev_dummy_send(struct netdev *netdev, const void *buffer, size_t size)
510 {
511     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
512     size_t i;
513
514     if (size < ETH_HEADER_LEN) {
515         return EMSGSIZE;
516     } else {
517         const struct eth_header *eth = buffer;
518         int max_size;
519
520         ovs_mutex_lock(&dev->mutex);
521         max_size = dev->mtu + ETH_HEADER_LEN;
522         ovs_mutex_unlock(&dev->mutex);
523
524         if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
525             max_size += VLAN_HEADER_LEN;
526         }
527         if (size > max_size) {
528             return EMSGSIZE;
529         }
530     }
531
532     ovs_mutex_lock(&dev->mutex);
533     dev->stats.tx_packets++;
534     dev->stats.tx_bytes += size;
535
536     if (dev->tx_pcap) {
537         struct ofpbuf packet;
538
539         ofpbuf_use_const(&packet, buffer, size);
540         pcap_write(dev->tx_pcap, &packet);
541         fflush(dev->tx_pcap);
542     }
543
544     for (i = 0; i < dev->n_streams; i++) {
545         struct dummy_stream *s = &dev->streams[i];
546
547         if (list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) {
548             struct ofpbuf *b;
549
550             b = ofpbuf_clone_data_with_headroom(buffer, size, 2);
551             put_unaligned_be16(ofpbuf_push_uninit(b, 2), htons(size));
552             list_push_back(&s->txq, &b->list_node);
553         }
554     }
555     ovs_mutex_unlock(&dev->mutex);
556
557     return 0;
558 }
559
560 static int
561 netdev_dummy_set_etheraddr(struct netdev *netdev,
562                            const uint8_t mac[ETH_ADDR_LEN])
563 {
564     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
565
566     ovs_mutex_lock(&dev->mutex);
567     if (!eth_addr_equals(dev->hwaddr, mac)) {
568         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
569         netdev_dummy_poll_notify(dev);
570     }
571     ovs_mutex_unlock(&dev->mutex);
572
573     return 0;
574 }
575
576 static int
577 netdev_dummy_get_etheraddr(const struct netdev *netdev,
578                            uint8_t mac[ETH_ADDR_LEN])
579 {
580     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
581
582     ovs_mutex_lock(&dev->mutex);
583     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
584     ovs_mutex_unlock(&dev->mutex);
585
586     return 0;
587 }
588
589 static int
590 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
591 {
592     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
593
594     ovs_mutex_lock(&dev->mutex);
595     *mtup = dev->mtu;
596     ovs_mutex_unlock(&dev->mutex);
597
598     return 0;
599 }
600
601 static int
602 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
603 {
604     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
605
606     ovs_mutex_lock(&dev->mutex);
607     dev->mtu = mtu;
608     ovs_mutex_unlock(&dev->mutex);
609
610     return 0;
611 }
612
613 static int
614 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
615 {
616     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
617
618     ovs_mutex_lock(&dev->mutex);
619     *stats = dev->stats;
620     ovs_mutex_unlock(&dev->mutex);
621
622     return 0;
623 }
624
625 static int
626 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
627 {
628     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
629
630     ovs_mutex_lock(&dev->mutex);
631     dev->stats = *stats;
632     ovs_mutex_unlock(&dev->mutex);
633
634     return 0;
635 }
636
637 static int
638 netdev_dummy_get_ifindex(const struct netdev *netdev)
639 {
640     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
641     int ifindex;
642
643     ovs_mutex_lock(&dev->mutex);
644     ifindex = dev->ifindex;
645     ovs_mutex_unlock(&dev->mutex);
646
647     return ifindex;
648 }
649
650 static int
651 netdev_dummy_update_flags__(struct netdev_dummy *netdev,
652                             enum netdev_flags off, enum netdev_flags on,
653                             enum netdev_flags *old_flagsp)
654     OVS_REQUIRES(netdev->mutex)
655 {
656     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
657         return EINVAL;
658     }
659
660     *old_flagsp = netdev->flags;
661     netdev->flags |= on;
662     netdev->flags &= ~off;
663     if (*old_flagsp != netdev->flags) {
664         netdev_dummy_poll_notify(netdev);
665     }
666
667     return 0;
668 }
669
670 static int
671 netdev_dummy_update_flags(struct netdev *netdev_,
672                           enum netdev_flags off, enum netdev_flags on,
673                           enum netdev_flags *old_flagsp)
674 {
675     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
676     int error;
677
678     ovs_mutex_lock(&netdev->mutex);
679     error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp);
680     ovs_mutex_unlock(&netdev->mutex);
681
682     return error;
683 }
684
685 static unsigned int
686 netdev_dummy_change_seq(const struct netdev *netdev_)
687 {
688     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
689     unsigned int change_seq;
690
691     ovs_mutex_lock(&netdev->mutex);
692     change_seq = netdev->change_seq;
693     ovs_mutex_unlock(&netdev->mutex);
694
695     return change_seq;
696 }
697 \f
698 /* Helper functions. */
699
700 static void
701 netdev_dummy_poll_notify(struct netdev_dummy *dev)
702 {
703     dev->change_seq++;
704     if (!dev->change_seq) {
705         dev->change_seq++;
706     }
707 }
708
709 static const struct netdev_class dummy_class = {
710     "dummy",
711     NULL,                       /* init */
712     netdev_dummy_run,
713     netdev_dummy_wait,
714
715     netdev_dummy_alloc,
716     netdev_dummy_construct,
717     netdev_dummy_destruct,
718     netdev_dummy_dealloc,
719     netdev_dummy_get_config,
720     netdev_dummy_set_config,
721     NULL,                       /* get_tunnel_config */
722
723     netdev_dummy_send,          /* send */
724     NULL,                       /* send_wait */
725
726     netdev_dummy_set_etheraddr,
727     netdev_dummy_get_etheraddr,
728     netdev_dummy_get_mtu,
729     netdev_dummy_set_mtu,
730     netdev_dummy_get_ifindex,
731     NULL,                       /* get_carrier */
732     NULL,                       /* get_carrier_resets */
733     NULL,                       /* get_miimon */
734     netdev_dummy_get_stats,
735     netdev_dummy_set_stats,
736
737     NULL,                       /* get_features */
738     NULL,                       /* set_advertisements */
739
740     NULL,                       /* set_policing */
741     NULL,                       /* get_qos_types */
742     NULL,                       /* get_qos_capabilities */
743     NULL,                       /* get_qos */
744     NULL,                       /* set_qos */
745     NULL,                       /* get_queue */
746     NULL,                       /* set_queue */
747     NULL,                       /* delete_queue */
748     NULL,                       /* get_queue_stats */
749     NULL,                       /* queue_dump_start */
750     NULL,                       /* queue_dump_next */
751     NULL,                       /* queue_dump_done */
752     NULL,                       /* dump_queue_stats */
753
754     NULL,                       /* get_in4 */
755     NULL,                       /* set_in4 */
756     NULL,                       /* get_in6 */
757     NULL,                       /* add_router */
758     NULL,                       /* get_next_hop */
759     NULL,                       /* get_status */
760     NULL,                       /* arp_lookup */
761
762     netdev_dummy_update_flags,
763
764     netdev_dummy_change_seq,
765
766     netdev_dummy_rx_alloc,
767     netdev_dummy_rx_construct,
768     netdev_dummy_rx_destruct,
769     netdev_dummy_rx_dealloc,
770     netdev_dummy_rx_recv,
771     netdev_dummy_rx_wait,
772     netdev_dummy_rx_drain,
773 };
774
775 static struct ofpbuf *
776 eth_from_packet_or_flow(const char *s)
777 {
778     enum odp_key_fitness fitness;
779     struct ofpbuf *packet;
780     struct ofpbuf odp_key;
781     struct flow flow;
782     int error;
783
784     if (!eth_from_hex(s, &packet)) {
785         return packet;
786     }
787
788     /* Convert string to datapath key.
789      *
790      * It would actually be nicer to parse an OpenFlow-like flow key here, but
791      * the code for that currently calls exit() on parse error.  We have to
792      * settle for parsing a datapath key for now.
793      */
794     ofpbuf_init(&odp_key, 0);
795     error = odp_flow_from_string(s, NULL, &odp_key, NULL);
796     if (error) {
797         ofpbuf_uninit(&odp_key);
798         return NULL;
799     }
800
801     /* Convert odp_key to flow. */
802     fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
803     if (fitness == ODP_FIT_ERROR) {
804         ofpbuf_uninit(&odp_key);
805         return NULL;
806     }
807
808     packet = ofpbuf_new(0);
809     flow_compose(packet, &flow);
810
811     ofpbuf_uninit(&odp_key);
812     return packet;
813 }
814
815 static void
816 netdev_dummy_queue_packet__(struct netdev_rx_dummy *rx, struct ofpbuf *packet)
817 {
818     list_push_back(&rx->recv_queue, &packet->list_node);
819     rx->recv_queue_len++;
820 }
821
822 static void
823 netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct ofpbuf *packet)
824     OVS_REQUIRES(dummy->mutex)
825 {
826     struct netdev_rx_dummy *rx, *prev;
827
828     if (dummy->rx_pcap) {
829         pcap_write(dummy->rx_pcap, packet);
830         fflush(dummy->rx_pcap);
831     }
832     prev = NULL;
833     LIST_FOR_EACH (rx, node, &dummy->rxes) {
834         if (rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
835             if (prev) {
836                 netdev_dummy_queue_packet__(prev, ofpbuf_clone(packet));
837             }
838             prev = rx;
839         }
840     }
841     if (prev) {
842         netdev_dummy_queue_packet__(prev, packet);
843     } else {
844         ofpbuf_delete(packet);
845     }
846 }
847
848 static void
849 netdev_dummy_receive(struct unixctl_conn *conn,
850                      int argc, const char *argv[], void *aux OVS_UNUSED)
851 {
852     struct netdev_dummy *dummy_dev;
853     struct netdev *netdev;
854     int i;
855
856     netdev = netdev_from_name(argv[1]);
857     if (!netdev || !is_dummy_class(netdev->netdev_class)) {
858         unixctl_command_reply_error(conn, "no such dummy netdev");
859         goto exit;
860     }
861     dummy_dev = netdev_dummy_cast(netdev);
862
863     for (i = 2; i < argc; i++) {
864         struct ofpbuf *packet;
865
866         packet = eth_from_packet_or_flow(argv[i]);
867         if (!packet) {
868             unixctl_command_reply_error(conn, "bad packet syntax");
869             goto exit;
870         }
871
872         ovs_mutex_lock(&dummy_dev->mutex);
873         dummy_dev->stats.rx_packets++;
874         dummy_dev->stats.rx_bytes += packet->size;
875         netdev_dummy_queue_packet(dummy_dev, packet);
876         ovs_mutex_unlock(&dummy_dev->mutex);
877     }
878
879     unixctl_command_reply(conn, NULL);
880
881 exit:
882     netdev_close(netdev);
883 }
884
885 static void
886 netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state)
887     OVS_REQUIRES(dev->mutex)
888 {
889     enum netdev_flags old_flags;
890
891     if (admin_state) {
892         netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags);
893     } else {
894         netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags);
895     }
896 }
897
898 static void
899 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
900                              const char *argv[], void *aux OVS_UNUSED)
901 {
902     bool up;
903
904     if (!strcasecmp(argv[argc - 1], "up")) {
905         up = true;
906     } else if ( !strcasecmp(argv[argc - 1], "down")) {
907         up = false;
908     } else {
909         unixctl_command_reply_error(conn, "Invalid Admin State");
910         return;
911     }
912
913     if (argc > 2) {
914         struct netdev *netdev = netdev_from_name(argv[1]);
915         if (netdev && is_dummy_class(netdev->netdev_class)) {
916             struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
917
918             ovs_mutex_lock(&dummy_dev->mutex);
919             netdev_dummy_set_admin_state__(dummy_dev, up);
920             ovs_mutex_unlock(&dummy_dev->mutex);
921
922             netdev_close(netdev);
923         } else {
924             unixctl_command_reply_error(conn, "Unknown Dummy Interface");
925             netdev_close(netdev);
926             return;
927         }
928     } else {
929         struct netdev_dummy *netdev;
930
931         ovs_mutex_lock(&dummy_list_mutex);
932         LIST_FOR_EACH (netdev, list_node, &dummy_list) {
933             ovs_mutex_lock(&netdev->mutex);
934             netdev_dummy_set_admin_state__(netdev, up);
935             ovs_mutex_unlock(&netdev->mutex);
936         }
937         ovs_mutex_unlock(&dummy_list_mutex);
938     }
939     unixctl_command_reply(conn, "OK");
940 }
941
942 void
943 netdev_dummy_register(bool override)
944 {
945     unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
946                              2, INT_MAX, netdev_dummy_receive, NULL);
947     unixctl_command_register("netdev-dummy/set-admin-state",
948                              "[netdev] up|down", 1, 2,
949                              netdev_dummy_set_admin_state, NULL);
950
951     if (override) {
952         struct sset types;
953         const char *type;
954
955         sset_init(&types);
956         netdev_enumerate_types(&types);
957         SSET_FOR_EACH (type, &types) {
958             if (!strcmp(type, "patch")) {
959                 continue;
960             }
961             if (!netdev_unregister_provider(type)) {
962                 struct netdev_class *class;
963                 int error;
964
965                 class = xmemdup(&dummy_class, sizeof dummy_class);
966                 class->type = xstrdup(type);
967                 error = netdev_register_provider(class);
968                 if (error) {
969                     VLOG_ERR("%s: failed to register netdev provider (%s)",
970                              type, ovs_strerror(error));
971                     free(CONST_CAST(char *, class->type));
972                     free(class);
973                 }
974             }
975         }
976         sset_destroy(&types);
977     }
978     netdev_register_provider(&dummy_class);
979
980     netdev_vport_tunnel_register();
981 }