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