ofproto: Do not update stats on fake bond interface.
[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 "dpif-netdev.h"
24 #include "dynamic-string.h"
25 #include "flow.h"
26 #include "list.h"
27 #include "netdev-provider.h"
28 #include "netdev-vport.h"
29 #include "odp-util.h"
30 #include "ofp-print.h"
31 #include "ofpbuf.h"
32 #include "packet-dpif.h"
33 #include "packets.h"
34 #include "pcap-file.h"
35 #include "poll-loop.h"
36 #include "shash.h"
37 #include "sset.h"
38 #include "stream.h"
39 #include "unaligned.h"
40 #include "timeval.h"
41 #include "unixctl.h"
42 #include "reconnect.h"
43 #include "vlog.h"
44
45 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
46
47 struct reconnect;
48
49 struct dummy_packet_stream {
50     struct stream *stream;
51     struct ofpbuf rxbuf;
52     struct list txq;
53 };
54
55 enum dummy_packet_conn_type {
56     NONE,       /* No connection is configured. */
57     PASSIVE,    /* Listener. */
58     ACTIVE      /* Connect to listener. */
59 };
60
61 enum dummy_netdev_conn_state {
62     CONN_STATE_CONNECTED,      /* Listener connected. */
63     CONN_STATE_NOT_CONNECTED,  /* Listener not connected.  */
64     CONN_STATE_UNKNOWN,        /* No relavent information.  */
65 };
66
67 struct dummy_packet_pconn {
68     struct pstream *pstream;
69     struct dummy_packet_stream *streams;
70     size_t n_streams;
71 };
72
73 struct dummy_packet_rconn {
74     struct dummy_packet_stream *rstream;
75     struct reconnect *reconnect;
76 };
77
78 struct dummy_packet_conn {
79     enum dummy_packet_conn_type type;
80     union {
81         struct dummy_packet_pconn pconn;
82         struct dummy_packet_rconn rconn;
83     } u;
84 };
85
86 /* Protects 'dummy_list'. */
87 static struct ovs_mutex dummy_list_mutex = OVS_MUTEX_INITIALIZER;
88
89 /* Contains all 'struct dummy_dev's. */
90 static struct list dummy_list OVS_GUARDED_BY(dummy_list_mutex)
91     = LIST_INITIALIZER(&dummy_list);
92
93 struct netdev_dummy {
94     struct netdev up;
95
96     /* In dummy_list. */
97     struct list list_node OVS_GUARDED_BY(dummy_list_mutex);
98
99     /* Protects all members below. */
100     struct ovs_mutex mutex OVS_ACQ_AFTER(dummy_list_mutex);
101
102     uint8_t hwaddr[ETH_ADDR_LEN] OVS_GUARDED;
103     int mtu OVS_GUARDED;
104     struct netdev_stats stats OVS_GUARDED;
105     enum netdev_flags flags OVS_GUARDED;
106     int ifindex OVS_GUARDED;
107
108     struct dummy_packet_conn conn OVS_GUARDED;
109
110     FILE *tx_pcap, *rxq_pcap OVS_GUARDED;
111
112     struct list rxes OVS_GUARDED; /* List of child "netdev_rxq_dummy"s. */
113 };
114
115 /* Max 'recv_queue_len' in struct netdev_dummy. */
116 #define NETDEV_DUMMY_MAX_QUEUE 100
117
118 struct netdev_rxq_dummy {
119     struct netdev_rxq up;
120     struct list node;           /* In netdev_dummy's "rxes" list. */
121     struct list recv_queue;
122     int recv_queue_len;         /* list_size(&recv_queue). */
123     struct seq *seq;            /* Reports newly queued packets. */
124 };
125
126 static unixctl_cb_func netdev_dummy_set_admin_state;
127 static int netdev_dummy_construct(struct netdev *);
128 static void netdev_dummy_queue_packet(struct netdev_dummy *, struct ofpbuf *);
129
130 static void dummy_packet_stream_close(struct dummy_packet_stream *);
131
132 static bool
133 is_dummy_class(const struct netdev_class *class)
134 {
135     return class->construct == netdev_dummy_construct;
136 }
137
138 static struct netdev_dummy *
139 netdev_dummy_cast(const struct netdev *netdev)
140 {
141     ovs_assert(is_dummy_class(netdev_get_class(netdev)));
142     return CONTAINER_OF(netdev, struct netdev_dummy, up);
143 }
144
145 static struct netdev_rxq_dummy *
146 netdev_rxq_dummy_cast(const struct netdev_rxq *rx)
147 {
148     ovs_assert(is_dummy_class(netdev_get_class(rx->netdev)));
149     return CONTAINER_OF(rx, struct netdev_rxq_dummy, up);
150 }
151
152 static void
153 dummy_packet_stream_init(struct dummy_packet_stream *s, struct stream *stream)
154 {
155     int rxbuf_size = stream ? 2048 : 0;
156     s->stream = stream;
157     ofpbuf_init(&s->rxbuf, rxbuf_size);
158     list_init(&s->txq);
159 }
160
161 static struct dummy_packet_stream *
162 dummy_packet_stream_create(struct stream *stream)
163 {
164     struct dummy_packet_stream *s;
165
166     s = xzalloc(sizeof *s);
167     dummy_packet_stream_init(s, stream);
168
169     return s;
170 }
171
172 static void
173 dummy_packet_stream_wait(struct dummy_packet_stream *s)
174 {
175     stream_run_wait(s->stream);
176     if (!list_is_empty(&s->txq)) {
177         stream_send_wait(s->stream);
178     }
179     stream_recv_wait(s->stream);
180 }
181
182 static void
183 dummy_packet_stream_send(struct dummy_packet_stream *s, const void *buffer, size_t size)
184 {
185     if (list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) {
186         struct ofpbuf *b;
187
188         b = ofpbuf_clone_data_with_headroom(buffer, size, 2);
189         put_unaligned_be16(ofpbuf_push_uninit(b, 2), htons(size));
190         list_push_back(&s->txq, &b->list_node);
191     }
192 }
193
194 static int
195 dummy_packet_stream_run(struct netdev_dummy *dev, struct dummy_packet_stream *s)
196 {
197     int error = 0;
198     size_t n;
199
200     stream_run(s->stream);
201
202     if (!list_is_empty(&s->txq)) {
203         struct ofpbuf *txbuf;
204         int retval;
205
206         txbuf = ofpbuf_from_list(list_front(&s->txq));
207         retval = stream_send(s->stream, ofpbuf_data(txbuf), ofpbuf_size(txbuf));
208
209         if (retval > 0) {
210             ofpbuf_pull(txbuf, retval);
211             if (!ofpbuf_size(txbuf)) {
212                 list_remove(&txbuf->list_node);
213                 ofpbuf_delete(txbuf);
214             }
215         } else if (retval != -EAGAIN) {
216             error = -retval;
217         }
218     }
219
220     if (!error) {
221         if (ofpbuf_size(&s->rxbuf) < 2) {
222             n = 2 - ofpbuf_size(&s->rxbuf);
223         } else {
224             uint16_t frame_len;
225
226             frame_len = ntohs(get_unaligned_be16(ofpbuf_data(&s->rxbuf)));
227             if (frame_len < ETH_HEADER_LEN) {
228                 error = EPROTO;
229                 n = 0;
230             } else {
231                 n = (2 + frame_len) - ofpbuf_size(&s->rxbuf);
232             }
233         }
234     }
235     if (!error) {
236         int retval;
237
238         ofpbuf_prealloc_tailroom(&s->rxbuf, n);
239         retval = stream_recv(s->stream, ofpbuf_tail(&s->rxbuf), n);
240
241         if (retval > 0) {
242             ofpbuf_set_size(&s->rxbuf, ofpbuf_size(&s->rxbuf) + retval);
243             if (retval == n && ofpbuf_size(&s->rxbuf) > 2) {
244                 ofpbuf_pull(&s->rxbuf, 2);
245                 netdev_dummy_queue_packet(dev,
246                                           ofpbuf_clone(&s->rxbuf));
247                 ofpbuf_clear(&s->rxbuf);
248             }
249         } else if (retval != -EAGAIN) {
250             error = (retval < 0 ? -retval
251                      : ofpbuf_size(&s->rxbuf) ? EPROTO
252                      : EOF);
253         }
254     }
255
256     return error;
257 }
258
259 static void
260 dummy_packet_stream_close(struct dummy_packet_stream *s)
261 {
262     stream_close(s->stream);
263     ofpbuf_uninit(&s->rxbuf);
264     ofpbuf_list_delete(&s->txq);
265 }
266
267 static void
268 dummy_packet_conn_init(struct dummy_packet_conn *conn)
269 {
270     memset(conn, 0, sizeof *conn);
271     conn->type = NONE;
272 }
273
274 static void
275 dummy_packet_conn_get_config(struct dummy_packet_conn *conn, struct smap *args)
276 {
277
278     switch (conn->type) {
279     case PASSIVE:
280         smap_add(args, "pstream", pstream_get_name(conn->u.pconn.pstream));
281         break;
282
283     case ACTIVE:
284         smap_add(args, "stream", stream_get_name(conn->u.rconn.rstream->stream));
285         break;
286
287     case NONE:
288     default:
289         break;
290     }
291 }
292
293 static void
294 dummy_packet_conn_close(struct dummy_packet_conn *conn)
295 {
296     int i;
297     struct dummy_packet_pconn *pconn = &conn->u.pconn;
298     struct dummy_packet_rconn *rconn = &conn->u.rconn;
299
300     switch (conn->type) {
301     case PASSIVE:
302         pstream_close(pconn->pstream);
303         for (i = 0; i < pconn->n_streams; i++) {
304             dummy_packet_stream_close(&pconn->streams[i]);
305         }
306         free(pconn->streams);
307         pconn->pstream = NULL;
308         pconn->streams = NULL;
309         break;
310
311     case ACTIVE:
312         dummy_packet_stream_close(rconn->rstream);
313         free(rconn->rstream);
314         rconn->rstream = NULL;
315         reconnect_destroy(rconn->reconnect);
316         rconn->reconnect = NULL;
317         break;
318
319     case NONE:
320     default:
321         break;
322     }
323
324     conn->type = NONE;
325     memset(conn, 0, sizeof *conn);
326 }
327
328 static void
329 dummy_packet_conn_set_config(struct dummy_packet_conn *conn,
330                              const struct smap *args)
331 {
332     const char *pstream = smap_get(args, "pstream");
333     const char *stream = smap_get(args, "stream");
334
335     if (pstream && stream) {
336          VLOG_WARN("Open failed: both %s and %s are configured",
337                    pstream, stream);
338          return;
339     }
340
341     switch (conn->type) {
342     case PASSIVE:
343         if (!strcmp(pstream_get_name(conn->u.pconn.pstream), pstream)) {
344             return;
345         }
346         dummy_packet_conn_close(conn);
347         break;
348     case ACTIVE:
349         if (!strcmp(stream_get_name(conn->u.rconn.rstream->stream), stream)) {
350             return;
351         }
352         dummy_packet_conn_close(conn);
353         break;
354     case NONE:
355     default:
356         break;
357     }
358
359     if (pstream) {
360         int error;
361
362         error = pstream_open(pstream, &conn->u.pconn.pstream, DSCP_DEFAULT);
363         if (error) {
364             VLOG_WARN("%s: open failed (%s)", pstream, ovs_strerror(error));
365         } else {
366             conn->type = PASSIVE;
367         }
368     }
369
370     if (stream) {
371         int error;
372         struct stream *active_stream;
373         struct reconnect *reconnect;;
374
375         reconnect = reconnect_create(time_msec());
376         reconnect_set_name(reconnect, stream);
377         reconnect_set_passive(reconnect, false, time_msec());
378         reconnect_enable(reconnect, time_msec());
379         reconnect_set_backoff(reconnect, 100, INT_MAX);
380         reconnect_set_probe_interval(reconnect, 0);
381         conn->u.rconn.reconnect = reconnect;
382         conn->type = ACTIVE;
383
384         error = stream_open(stream, &active_stream, DSCP_DEFAULT);
385         conn->u.rconn.rstream = dummy_packet_stream_create(active_stream);
386
387         switch (error) {
388         case 0:
389             reconnect_connected(reconnect, time_msec());
390             break;
391
392         case EAGAIN:
393             reconnect_connecting(reconnect, time_msec());
394             break;
395
396         default:
397             reconnect_connect_failed(reconnect, time_msec(), error);
398             stream_close(active_stream);
399             conn->u.rconn.rstream->stream = NULL;
400             break;
401         }
402     }
403 }
404
405 static void
406 dummy_pconn_run(struct netdev_dummy *dev)
407     OVS_REQUIRES(dev->mutex)
408 {
409     struct stream *new_stream;
410     struct dummy_packet_pconn *pconn = &dev->conn.u.pconn;
411     int error;
412     size_t i;
413
414     error = pstream_accept(pconn->pstream, &new_stream);
415     if (!error) {
416         struct dummy_packet_stream *s;
417
418         pconn->streams = xrealloc(pconn->streams,
419                                 ((pconn->n_streams + 1)
420                                  * sizeof *s));
421         s = &pconn->streams[pconn->n_streams++];
422         dummy_packet_stream_init(s, new_stream);
423     } else if (error != EAGAIN) {
424         VLOG_WARN("%s: accept failed (%s)",
425                   pstream_get_name(pconn->pstream), ovs_strerror(error));
426         pstream_close(pconn->pstream);
427         pconn->pstream = NULL;
428         dev->conn.type = NONE;
429     }
430
431     for (i = 0; i < pconn->n_streams; i++) {
432         struct dummy_packet_stream *s = &pconn->streams[i];
433
434         error = dummy_packet_stream_run(dev, s);
435         if (error) {
436             VLOG_DBG("%s: closing connection (%s)",
437                      stream_get_name(s->stream),
438                      ovs_retval_to_string(error));
439             dummy_packet_stream_close(s);
440             pconn->streams[i] = pconn->streams[--pconn->n_streams];
441         }
442     }
443 }
444
445 static void
446 dummy_rconn_run(struct netdev_dummy *dev)
447 OVS_REQUIRES(dev->mutex)
448 {
449     struct dummy_packet_rconn *rconn = &dev->conn.u.rconn;
450
451     switch (reconnect_run(rconn->reconnect, time_msec())) {
452     case RECONNECT_CONNECT:
453         {
454             int error;
455
456             if (rconn->rstream->stream) {
457                 error = stream_connect(rconn->rstream->stream);
458             } else {
459                 error = stream_open(reconnect_get_name(rconn->reconnect),
460                                     &rconn->rstream->stream, DSCP_DEFAULT);
461             }
462
463             switch (error) {
464             case 0:
465                 reconnect_connected(rconn->reconnect, time_msec());
466                 break;
467
468             case EAGAIN:
469                 reconnect_connecting(rconn->reconnect, time_msec());
470                 break;
471
472             default:
473                 reconnect_connect_failed(rconn->reconnect, time_msec(), error);
474                 stream_close(rconn->rstream->stream);
475                 rconn->rstream->stream = NULL;
476                 break;
477             }
478         }
479         break;
480
481     case RECONNECT_DISCONNECT:
482     case RECONNECT_PROBE:
483     default:
484         break;
485     }
486
487     if (reconnect_is_connected(rconn->reconnect)) {
488         int err;
489
490         err = dummy_packet_stream_run(dev, rconn->rstream);
491
492         if (err) {
493             reconnect_disconnected(rconn->reconnect, time_msec(), err);
494             stream_close(rconn->rstream->stream);
495             rconn->rstream->stream = NULL;
496         }
497     }
498 }
499
500 static void
501 dummy_packet_conn_run(struct netdev_dummy *dev)
502     OVS_REQUIRES(dev->mutex)
503 {
504     switch (dev->conn.type) {
505     case PASSIVE:
506         dummy_pconn_run(dev);
507         break;
508
509     case ACTIVE:
510         dummy_rconn_run(dev);
511         break;
512
513     case NONE:
514     default:
515         break;
516     }
517 }
518
519 static void
520 dummy_packet_conn_wait(struct dummy_packet_conn *conn)
521 {
522     int i;
523     switch (conn->type) {
524     case PASSIVE:
525         pstream_wait(conn->u.pconn.pstream);
526         for (i = 0; i < conn->u.pconn.n_streams; i++) {
527             struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
528             dummy_packet_stream_wait(s);
529         }
530         break;
531     case ACTIVE:
532         if (reconnect_is_connected(conn->u.rconn.reconnect)) {
533             dummy_packet_stream_wait(conn->u.rconn.rstream);
534         }
535         break;
536
537     case NONE:
538     default:
539         break;
540     }
541 }
542
543 static void
544 dummy_packet_conn_send(struct dummy_packet_conn *conn,
545                        const void *buffer, size_t size)
546 {
547     int i;
548
549     switch (conn->type) {
550     case PASSIVE:
551         for (i = 0; i < conn->u.pconn.n_streams; i++) {
552             struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
553
554             dummy_packet_stream_send(s, buffer, size);
555             pstream_wait(conn->u.pconn.pstream);
556         }
557         break;
558
559     case ACTIVE:
560         if (reconnect_is_connected(conn->u.rconn.reconnect)) {
561             dummy_packet_stream_send(conn->u.rconn.rstream, buffer, size);
562             dummy_packet_stream_wait(conn->u.rconn.rstream);
563         }
564         break;
565
566     case NONE:
567     default:
568         break;
569     }
570 }
571
572 static enum dummy_netdev_conn_state
573 dummy_netdev_get_conn_state(struct dummy_packet_conn *conn)
574 {
575     enum dummy_netdev_conn_state state;
576
577     if (conn->type == ACTIVE) {
578         if (reconnect_is_connected(conn->u.rconn.reconnect)) {
579             state = CONN_STATE_CONNECTED;
580         } else {
581             state = CONN_STATE_NOT_CONNECTED;
582         }
583     } else {
584         state = CONN_STATE_UNKNOWN;
585     }
586
587     return state;
588 }
589
590 static void
591 netdev_dummy_run(void)
592 {
593     struct netdev_dummy *dev;
594
595     ovs_mutex_lock(&dummy_list_mutex);
596     LIST_FOR_EACH (dev, list_node, &dummy_list) {
597         ovs_mutex_lock(&dev->mutex);
598         dummy_packet_conn_run(dev);
599         ovs_mutex_unlock(&dev->mutex);
600     }
601     ovs_mutex_unlock(&dummy_list_mutex);
602 }
603
604 static void
605 netdev_dummy_wait(void)
606 {
607     struct netdev_dummy *dev;
608
609     ovs_mutex_lock(&dummy_list_mutex);
610     LIST_FOR_EACH (dev, list_node, &dummy_list) {
611         ovs_mutex_lock(&dev->mutex);
612         dummy_packet_conn_wait(&dev->conn);
613         ovs_mutex_unlock(&dev->mutex);
614     }
615     ovs_mutex_unlock(&dummy_list_mutex);
616 }
617
618 static struct netdev *
619 netdev_dummy_alloc(void)
620 {
621     struct netdev_dummy *netdev = xzalloc(sizeof *netdev);
622     return &netdev->up;
623 }
624
625 static int
626 netdev_dummy_construct(struct netdev *netdev_)
627 {
628     static atomic_count next_n = ATOMIC_COUNT_INIT(0xaa550000);
629     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
630     unsigned int n;
631
632     n = atomic_count_inc(&next_n);
633
634     ovs_mutex_init(&netdev->mutex);
635     ovs_mutex_lock(&netdev->mutex);
636     netdev->hwaddr[0] = 0xaa;
637     netdev->hwaddr[1] = 0x55;
638     netdev->hwaddr[2] = n >> 24;
639     netdev->hwaddr[3] = n >> 16;
640     netdev->hwaddr[4] = n >> 8;
641     netdev->hwaddr[5] = n;
642     netdev->mtu = 1500;
643     netdev->flags = 0;
644     netdev->ifindex = -EOPNOTSUPP;
645
646     dummy_packet_conn_init(&netdev->conn);
647
648     list_init(&netdev->rxes);
649     ovs_mutex_unlock(&netdev->mutex);
650
651     ovs_mutex_lock(&dummy_list_mutex);
652     list_push_back(&dummy_list, &netdev->list_node);
653     ovs_mutex_unlock(&dummy_list_mutex);
654
655     return 0;
656 }
657
658 static void
659 netdev_dummy_destruct(struct netdev *netdev_)
660 {
661     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
662
663     ovs_mutex_lock(&dummy_list_mutex);
664     list_remove(&netdev->list_node);
665     ovs_mutex_unlock(&dummy_list_mutex);
666
667     ovs_mutex_lock(&netdev->mutex);
668     dummy_packet_conn_close(&netdev->conn);
669     netdev->conn.type = NONE;
670
671     ovs_mutex_unlock(&netdev->mutex);
672     ovs_mutex_destroy(&netdev->mutex);
673 }
674
675 static void
676 netdev_dummy_dealloc(struct netdev *netdev_)
677 {
678     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
679
680     free(netdev);
681 }
682
683 static int
684 netdev_dummy_get_config(const struct netdev *netdev_, struct smap *args)
685 {
686     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
687
688     ovs_mutex_lock(&netdev->mutex);
689
690     if (netdev->ifindex >= 0) {
691         smap_add_format(args, "ifindex", "%d", netdev->ifindex);
692     }
693
694     dummy_packet_conn_get_config(&netdev->conn, args);
695
696     ovs_mutex_unlock(&netdev->mutex);
697     return 0;
698 }
699
700 static int
701 netdev_dummy_set_config(struct netdev *netdev_, const struct smap *args)
702 {
703     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
704     const char *pcap;
705
706     ovs_mutex_lock(&netdev->mutex);
707     netdev->ifindex = smap_get_int(args, "ifindex", -EOPNOTSUPP);
708
709     dummy_packet_conn_set_config(&netdev->conn, args);
710
711     if (netdev->rxq_pcap) {
712         fclose(netdev->rxq_pcap);
713     }
714     if (netdev->tx_pcap && netdev->tx_pcap != netdev->rxq_pcap) {
715         fclose(netdev->tx_pcap);
716     }
717     netdev->rxq_pcap = netdev->tx_pcap = NULL;
718     pcap = smap_get(args, "pcap");
719     if (pcap) {
720         netdev->rxq_pcap = netdev->tx_pcap = ovs_pcap_open(pcap, "ab");
721     } else {
722         const char *rxq_pcap = smap_get(args, "rxq_pcap");
723         const char *tx_pcap = smap_get(args, "tx_pcap");
724
725         if (rxq_pcap) {
726             netdev->rxq_pcap = ovs_pcap_open(rxq_pcap, "ab");
727         }
728         if (tx_pcap) {
729             netdev->tx_pcap = ovs_pcap_open(tx_pcap, "ab");
730         }
731     }
732
733     ovs_mutex_unlock(&netdev->mutex);
734
735     return 0;
736 }
737
738 static struct netdev_rxq *
739 netdev_dummy_rxq_alloc(void)
740 {
741     struct netdev_rxq_dummy *rx = xzalloc(sizeof *rx);
742     return &rx->up;
743 }
744
745 static int
746 netdev_dummy_rxq_construct(struct netdev_rxq *rxq_)
747 {
748     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
749     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
750
751     ovs_mutex_lock(&netdev->mutex);
752     list_push_back(&netdev->rxes, &rx->node);
753     list_init(&rx->recv_queue);
754     rx->recv_queue_len = 0;
755     rx->seq = seq_create();
756     ovs_mutex_unlock(&netdev->mutex);
757
758     return 0;
759 }
760
761 static void
762 netdev_dummy_rxq_destruct(struct netdev_rxq *rxq_)
763 {
764     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
765     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
766
767     ovs_mutex_lock(&netdev->mutex);
768     list_remove(&rx->node);
769     ofpbuf_list_delete(&rx->recv_queue);
770     ovs_mutex_unlock(&netdev->mutex);
771     seq_destroy(rx->seq);
772 }
773
774 static void
775 netdev_dummy_rxq_dealloc(struct netdev_rxq *rxq_)
776 {
777     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
778
779     free(rx);
780 }
781
782 static int
783 netdev_dummy_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **arr,
784                       int *c)
785 {
786     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
787     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
788     struct ofpbuf *packet;
789
790     ovs_mutex_lock(&netdev->mutex);
791     if (!list_is_empty(&rx->recv_queue)) {
792         packet = ofpbuf_from_list(list_pop_front(&rx->recv_queue));
793         rx->recv_queue_len--;
794     } else {
795         packet = NULL;
796     }
797     ovs_mutex_unlock(&netdev->mutex);
798
799     if (!packet) {
800         return EAGAIN;
801     }
802     ovs_mutex_lock(&netdev->mutex);
803     netdev->stats.rx_packets++;
804     netdev->stats.rx_bytes += ofpbuf_size(packet);
805     ovs_mutex_unlock(&netdev->mutex);
806
807     dp_packet_pad(packet);
808
809     /* This performs a (sometimes unnecessary) copy */
810     arr[0] = dpif_packet_clone_from_ofpbuf(packet);
811     dpif_packet_set_dp_hash(arr[0], 0);
812     ofpbuf_delete(packet);
813     *c = 1;
814     return 0;
815 }
816
817 static void
818 netdev_dummy_rxq_wait(struct netdev_rxq *rxq_)
819 {
820     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
821     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
822     uint64_t seq = seq_read(rx->seq);
823
824     ovs_mutex_lock(&netdev->mutex);
825     if (!list_is_empty(&rx->recv_queue)) {
826         poll_immediate_wake();
827     } else {
828         seq_wait(rx->seq, seq);
829     }
830     ovs_mutex_unlock(&netdev->mutex);
831 }
832
833 static int
834 netdev_dummy_rxq_drain(struct netdev_rxq *rxq_)
835 {
836     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
837     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
838
839     ovs_mutex_lock(&netdev->mutex);
840     ofpbuf_list_delete(&rx->recv_queue);
841     rx->recv_queue_len = 0;
842     ovs_mutex_unlock(&netdev->mutex);
843
844     seq_change(rx->seq);
845
846     return 0;
847 }
848
849 static int
850 netdev_dummy_send(struct netdev *netdev, int qid OVS_UNUSED,
851                   struct dpif_packet **pkts, int cnt, bool may_steal)
852 {
853     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
854     int error = 0;
855     int i;
856
857     for (i = 0; i < cnt; i++) {
858         const void *buffer = ofpbuf_data(&pkts[i]->ofpbuf);
859         size_t size = ofpbuf_size(&pkts[i]->ofpbuf);
860
861         if (size < ETH_HEADER_LEN) {
862             error = EMSGSIZE;
863             break;
864         } else {
865             const struct eth_header *eth = buffer;
866             int max_size;
867
868             ovs_mutex_lock(&dev->mutex);
869             max_size = dev->mtu + ETH_HEADER_LEN;
870             ovs_mutex_unlock(&dev->mutex);
871
872             if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
873                 max_size += VLAN_HEADER_LEN;
874             }
875             if (size > max_size) {
876                 error = EMSGSIZE;
877                 break;
878             }
879         }
880
881         ovs_mutex_lock(&dev->mutex);
882         dev->stats.tx_packets++;
883         dev->stats.tx_bytes += size;
884
885         dummy_packet_conn_send(&dev->conn, buffer, size);
886
887         if (dev->tx_pcap) {
888             struct ofpbuf packet;
889
890             ofpbuf_use_const(&packet, buffer, size);
891             ovs_pcap_write(dev->tx_pcap, &packet);
892             fflush(dev->tx_pcap);
893         }
894
895         ovs_mutex_unlock(&dev->mutex);
896     }
897
898     if (may_steal) {
899         for (i = 0; i < cnt; i++) {
900             dpif_packet_delete(pkts[i]);
901         }
902     }
903
904     return error;
905 }
906
907 static int
908 netdev_dummy_set_etheraddr(struct netdev *netdev,
909                            const uint8_t mac[ETH_ADDR_LEN])
910 {
911     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
912
913     ovs_mutex_lock(&dev->mutex);
914     if (!eth_addr_equals(dev->hwaddr, mac)) {
915         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
916         netdev_change_seq_changed(netdev);
917     }
918     ovs_mutex_unlock(&dev->mutex);
919
920     return 0;
921 }
922
923 static int
924 netdev_dummy_get_etheraddr(const struct netdev *netdev,
925                            uint8_t mac[ETH_ADDR_LEN])
926 {
927     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
928
929     ovs_mutex_lock(&dev->mutex);
930     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
931     ovs_mutex_unlock(&dev->mutex);
932
933     return 0;
934 }
935
936 static int
937 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
938 {
939     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
940
941     ovs_mutex_lock(&dev->mutex);
942     *mtup = dev->mtu;
943     ovs_mutex_unlock(&dev->mutex);
944
945     return 0;
946 }
947
948 static int
949 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
950 {
951     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
952
953     ovs_mutex_lock(&dev->mutex);
954     dev->mtu = mtu;
955     ovs_mutex_unlock(&dev->mutex);
956
957     return 0;
958 }
959
960 static int
961 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
962 {
963     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
964
965     ovs_mutex_lock(&dev->mutex);
966     *stats = dev->stats;
967     ovs_mutex_unlock(&dev->mutex);
968
969     return 0;
970 }
971
972 static int
973 netdev_dummy_get_ifindex(const struct netdev *netdev)
974 {
975     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
976     int ifindex;
977
978     ovs_mutex_lock(&dev->mutex);
979     ifindex = dev->ifindex;
980     ovs_mutex_unlock(&dev->mutex);
981
982     return ifindex;
983 }
984
985 static int
986 netdev_dummy_update_flags__(struct netdev_dummy *netdev,
987                             enum netdev_flags off, enum netdev_flags on,
988                             enum netdev_flags *old_flagsp)
989     OVS_REQUIRES(netdev->mutex)
990 {
991     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
992         return EINVAL;
993     }
994
995     *old_flagsp = netdev->flags;
996     netdev->flags |= on;
997     netdev->flags &= ~off;
998     if (*old_flagsp != netdev->flags) {
999         netdev_change_seq_changed(&netdev->up);
1000     }
1001
1002     return 0;
1003 }
1004
1005 static int
1006 netdev_dummy_update_flags(struct netdev *netdev_,
1007                           enum netdev_flags off, enum netdev_flags on,
1008                           enum netdev_flags *old_flagsp)
1009 {
1010     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
1011     int error;
1012
1013     ovs_mutex_lock(&netdev->mutex);
1014     error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp);
1015     ovs_mutex_unlock(&netdev->mutex);
1016
1017     return error;
1018 }
1019 \f
1020 /* Helper functions. */
1021
1022 static const struct netdev_class dummy_class = {
1023     "dummy",
1024     NULL,                       /* init */
1025     netdev_dummy_run,
1026     netdev_dummy_wait,
1027
1028     netdev_dummy_alloc,
1029     netdev_dummy_construct,
1030     netdev_dummy_destruct,
1031     netdev_dummy_dealloc,
1032     netdev_dummy_get_config,
1033     netdev_dummy_set_config,
1034     NULL,                       /* get_tunnel_config */
1035     NULL,                       /* get_numa_id */
1036
1037     netdev_dummy_send,          /* send */
1038     NULL,                       /* send_wait */
1039
1040     netdev_dummy_set_etheraddr,
1041     netdev_dummy_get_etheraddr,
1042     netdev_dummy_get_mtu,
1043     netdev_dummy_set_mtu,
1044     netdev_dummy_get_ifindex,
1045     NULL,                       /* get_carrier */
1046     NULL,                       /* get_carrier_resets */
1047     NULL,                       /* get_miimon */
1048     netdev_dummy_get_stats,
1049
1050     NULL,                       /* get_features */
1051     NULL,                       /* set_advertisements */
1052
1053     NULL,                       /* set_policing */
1054     NULL,                       /* get_qos_types */
1055     NULL,                       /* get_qos_capabilities */
1056     NULL,                       /* get_qos */
1057     NULL,                       /* set_qos */
1058     NULL,                       /* get_queue */
1059     NULL,                       /* set_queue */
1060     NULL,                       /* delete_queue */
1061     NULL,                       /* get_queue_stats */
1062     NULL,                       /* queue_dump_start */
1063     NULL,                       /* queue_dump_next */
1064     NULL,                       /* queue_dump_done */
1065     NULL,                       /* dump_queue_stats */
1066
1067     NULL,                       /* get_in4 */
1068     NULL,                       /* set_in4 */
1069     NULL,                       /* get_in6 */
1070     NULL,                       /* add_router */
1071     NULL,                       /* get_next_hop */
1072     NULL,                       /* get_status */
1073     NULL,                       /* arp_lookup */
1074
1075     netdev_dummy_update_flags,
1076
1077     netdev_dummy_rxq_alloc,
1078     netdev_dummy_rxq_construct,
1079     netdev_dummy_rxq_destruct,
1080     netdev_dummy_rxq_dealloc,
1081     netdev_dummy_rxq_recv,
1082     netdev_dummy_rxq_wait,
1083     netdev_dummy_rxq_drain,
1084 };
1085
1086 static struct ofpbuf *
1087 eth_from_packet_or_flow(const char *s)
1088 {
1089     enum odp_key_fitness fitness;
1090     struct ofpbuf *packet;
1091     struct ofpbuf odp_key;
1092     struct flow flow;
1093     int error;
1094
1095     if (!eth_from_hex(s, &packet)) {
1096         return packet;
1097     }
1098
1099     /* Convert string to datapath key.
1100      *
1101      * It would actually be nicer to parse an OpenFlow-like flow key here, but
1102      * the code for that currently calls exit() on parse error.  We have to
1103      * settle for parsing a datapath key for now.
1104      */
1105     ofpbuf_init(&odp_key, 0);
1106     error = odp_flow_from_string(s, NULL, &odp_key, NULL);
1107     if (error) {
1108         ofpbuf_uninit(&odp_key);
1109         return NULL;
1110     }
1111
1112     /* Convert odp_key to flow. */
1113     fitness = odp_flow_key_to_flow(ofpbuf_data(&odp_key), ofpbuf_size(&odp_key), &flow);
1114     if (fitness == ODP_FIT_ERROR) {
1115         ofpbuf_uninit(&odp_key);
1116         return NULL;
1117     }
1118
1119     packet = ofpbuf_new(0);
1120     flow_compose(packet, &flow);
1121
1122     ofpbuf_uninit(&odp_key);
1123     return packet;
1124 }
1125
1126 static void
1127 netdev_dummy_queue_packet__(struct netdev_rxq_dummy *rx, struct ofpbuf *packet)
1128 {
1129     list_push_back(&rx->recv_queue, &packet->list_node);
1130     rx->recv_queue_len++;
1131     seq_change(rx->seq);
1132 }
1133
1134 static void
1135 netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct ofpbuf *packet)
1136     OVS_REQUIRES(dummy->mutex)
1137 {
1138     struct netdev_rxq_dummy *rx, *prev;
1139
1140     if (dummy->rxq_pcap) {
1141         ovs_pcap_write(dummy->rxq_pcap, packet);
1142         fflush(dummy->rxq_pcap);
1143     }
1144     prev = NULL;
1145     LIST_FOR_EACH (rx, node, &dummy->rxes) {
1146         if (rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
1147             if (prev) {
1148                 netdev_dummy_queue_packet__(prev, ofpbuf_clone(packet));
1149             }
1150             prev = rx;
1151         }
1152     }
1153     if (prev) {
1154         netdev_dummy_queue_packet__(prev, packet);
1155     } else {
1156         ofpbuf_delete(packet);
1157     }
1158 }
1159
1160 static void
1161 netdev_dummy_receive(struct unixctl_conn *conn,
1162                      int argc, const char *argv[], void *aux OVS_UNUSED)
1163 {
1164     struct netdev_dummy *dummy_dev;
1165     struct netdev *netdev;
1166     int i;
1167
1168     netdev = netdev_from_name(argv[1]);
1169     if (!netdev || !is_dummy_class(netdev->netdev_class)) {
1170         unixctl_command_reply_error(conn, "no such dummy netdev");
1171         goto exit;
1172     }
1173     dummy_dev = netdev_dummy_cast(netdev);
1174
1175     for (i = 2; i < argc; i++) {
1176         struct ofpbuf *packet;
1177
1178         packet = eth_from_packet_or_flow(argv[i]);
1179         if (!packet) {
1180             unixctl_command_reply_error(conn, "bad packet syntax");
1181             goto exit;
1182         }
1183
1184         ovs_mutex_lock(&dummy_dev->mutex);
1185         netdev_dummy_queue_packet(dummy_dev, packet);
1186         ovs_mutex_unlock(&dummy_dev->mutex);
1187     }
1188
1189     unixctl_command_reply(conn, NULL);
1190
1191 exit:
1192     netdev_close(netdev);
1193 }
1194
1195 static void
1196 netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state)
1197     OVS_REQUIRES(dev->mutex)
1198 {
1199     enum netdev_flags old_flags;
1200
1201     if (admin_state) {
1202         netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1203     } else {
1204         netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1205     }
1206 }
1207
1208 static void
1209 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
1210                              const char *argv[], void *aux OVS_UNUSED)
1211 {
1212     bool up;
1213
1214     if (!strcasecmp(argv[argc - 1], "up")) {
1215         up = true;
1216     } else if ( !strcasecmp(argv[argc - 1], "down")) {
1217         up = false;
1218     } else {
1219         unixctl_command_reply_error(conn, "Invalid Admin State");
1220         return;
1221     }
1222
1223     if (argc > 2) {
1224         struct netdev *netdev = netdev_from_name(argv[1]);
1225         if (netdev && is_dummy_class(netdev->netdev_class)) {
1226             struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1227
1228             ovs_mutex_lock(&dummy_dev->mutex);
1229             netdev_dummy_set_admin_state__(dummy_dev, up);
1230             ovs_mutex_unlock(&dummy_dev->mutex);
1231
1232             netdev_close(netdev);
1233         } else {
1234             unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1235             netdev_close(netdev);
1236             return;
1237         }
1238     } else {
1239         struct netdev_dummy *netdev;
1240
1241         ovs_mutex_lock(&dummy_list_mutex);
1242         LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1243             ovs_mutex_lock(&netdev->mutex);
1244             netdev_dummy_set_admin_state__(netdev, up);
1245             ovs_mutex_unlock(&netdev->mutex);
1246         }
1247         ovs_mutex_unlock(&dummy_list_mutex);
1248     }
1249     unixctl_command_reply(conn, "OK");
1250 }
1251
1252 static void
1253 display_conn_state__(struct ds *s, const char *name,
1254                      enum dummy_netdev_conn_state state)
1255 {
1256     ds_put_format(s, "%s: ", name);
1257
1258     switch (state) {
1259     case CONN_STATE_CONNECTED:
1260         ds_put_cstr(s, "connected\n");
1261         break;
1262
1263     case CONN_STATE_NOT_CONNECTED:
1264         ds_put_cstr(s, "disconnected\n");
1265         break;
1266
1267     case CONN_STATE_UNKNOWN:
1268     default:
1269         ds_put_cstr(s, "unknown\n");
1270         break;
1271     };
1272 }
1273
1274 static void
1275 netdev_dummy_conn_state(struct unixctl_conn *conn, int argc,
1276                         const char *argv[], void *aux OVS_UNUSED)
1277 {
1278     enum dummy_netdev_conn_state state = CONN_STATE_UNKNOWN;
1279     struct ds s;
1280
1281     ds_init(&s);
1282
1283     if (argc > 1) {
1284         const char *dev_name = argv[1];
1285         struct netdev *netdev = netdev_from_name(dev_name);
1286
1287         if (netdev && is_dummy_class(netdev->netdev_class)) {
1288             struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1289
1290             ovs_mutex_lock(&dummy_dev->mutex);
1291             state = dummy_netdev_get_conn_state(&dummy_dev->conn);
1292             ovs_mutex_unlock(&dummy_dev->mutex);
1293
1294             netdev_close(netdev);
1295         }
1296         display_conn_state__(&s, dev_name, state);
1297     } else {
1298         struct netdev_dummy *netdev;
1299
1300         ovs_mutex_lock(&dummy_list_mutex);
1301         LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1302             ovs_mutex_lock(&netdev->mutex);
1303             state = dummy_netdev_get_conn_state(&netdev->conn);
1304             ovs_mutex_unlock(&netdev->mutex);
1305             if (state != CONN_STATE_UNKNOWN) {
1306                 display_conn_state__(&s, netdev->up.name, state);
1307             }
1308         }
1309         ovs_mutex_unlock(&dummy_list_mutex);
1310     }
1311
1312     unixctl_command_reply(conn, ds_cstr(&s));
1313     ds_destroy(&s);
1314 }
1315
1316 void
1317 netdev_dummy_register(bool override)
1318 {
1319     unixctl_command_register("netdev-dummy/receive", "name packet|flow...",
1320                              2, INT_MAX, netdev_dummy_receive, NULL);
1321     unixctl_command_register("netdev-dummy/set-admin-state",
1322                              "[netdev] up|down", 1, 2,
1323                              netdev_dummy_set_admin_state, NULL);
1324     unixctl_command_register("netdev-dummy/conn-state",
1325                              "[netdev]", 0, 1,
1326                              netdev_dummy_conn_state, NULL);
1327
1328     if (override) {
1329         struct sset types;
1330         const char *type;
1331
1332         sset_init(&types);
1333         netdev_enumerate_types(&types);
1334         SSET_FOR_EACH (type, &types) {
1335             if (!strcmp(type, "patch")) {
1336                 continue;
1337             }
1338             if (!netdev_unregister_provider(type)) {
1339                 struct netdev_class *class;
1340                 int error;
1341
1342                 class = xmemdup(&dummy_class, sizeof dummy_class);
1343                 class->type = xstrdup(type);
1344                 error = netdev_register_provider(class);
1345                 if (error) {
1346                     VLOG_ERR("%s: failed to register netdev provider (%s)",
1347                              type, ovs_strerror(error));
1348                     free(CONST_CAST(char *, class->type));
1349                     free(class);
1350                 }
1351             }
1352         }
1353         sset_destroy(&types);
1354     }
1355     netdev_register_provider(&dummy_class);
1356
1357     netdev_vport_tunnel_register();
1358 }