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