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