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