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