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