ipfix: Support tunnel information for Flow IPFIX.
[cascardo/ovs.git] / lib / netdev-dummy.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013, 2015, 2016 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 #include <unistd.h>
23
24 #include "dp-packet.h"
25 #include "dpif-netdev.h"
26 #include "flow.h"
27 #include "netdev-provider.h"
28 #include "netdev-vport.h"
29 #include "odp-util.h"
30 #include "openvswitch/dynamic-string.h"
31 #include "openvswitch/list.h"
32 #include "openvswitch/ofp-print.h"
33 #include "openvswitch/ofpbuf.h"
34 #include "openvswitch/vlog.h"
35 #include "ovs-atomic.h"
36 #include "packets.h"
37 #include "pcap-file.h"
38 #include "poll-loop.h"
39 #include "shash.h"
40 #include "sset.h"
41 #include "stream.h"
42 #include "unaligned.h"
43 #include "timeval.h"
44 #include "unixctl.h"
45 #include "reconnect.h"
46
47 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
48
49 struct reconnect;
50
51 struct dummy_packet_stream {
52     struct stream *stream;
53     struct dp_packet rxbuf;
54     struct ovs_list txq;
55 };
56
57 enum dummy_packet_conn_type {
58     NONE,       /* No connection is configured. */
59     PASSIVE,    /* Listener. */
60     ACTIVE      /* Connect to listener. */
61 };
62
63 enum dummy_netdev_conn_state {
64     CONN_STATE_CONNECTED,      /* Listener connected. */
65     CONN_STATE_NOT_CONNECTED,  /* Listener not connected.  */
66     CONN_STATE_UNKNOWN,        /* No relavent information.  */
67 };
68
69 struct dummy_packet_pconn {
70     struct pstream *pstream;
71     struct dummy_packet_stream *streams;
72     size_t n_streams;
73 };
74
75 struct dummy_packet_rconn {
76     struct dummy_packet_stream *rstream;
77     struct reconnect *reconnect;
78 };
79
80 struct dummy_packet_conn {
81     enum dummy_packet_conn_type type;
82     union {
83         struct dummy_packet_pconn pconn;
84         struct dummy_packet_rconn rconn;
85     } u;
86 };
87
88 struct pkt_list_node {
89     struct dp_packet *pkt;
90     struct ovs_list list_node;
91 };
92
93 /* Protects 'dummy_list'. */
94 static struct ovs_mutex dummy_list_mutex = OVS_MUTEX_INITIALIZER;
95
96 /* Contains all 'struct dummy_dev's. */
97 static struct ovs_list dummy_list OVS_GUARDED_BY(dummy_list_mutex)
98     = OVS_LIST_INITIALIZER(&dummy_list);
99
100 struct netdev_dummy {
101     struct netdev up;
102
103     /* In dummy_list. */
104     struct ovs_list list_node OVS_GUARDED_BY(dummy_list_mutex);
105
106     /* Protects all members below. */
107     struct ovs_mutex mutex OVS_ACQ_AFTER(dummy_list_mutex);
108
109     struct eth_addr hwaddr OVS_GUARDED;
110     int mtu OVS_GUARDED;
111     struct netdev_stats stats OVS_GUARDED;
112     enum netdev_flags flags OVS_GUARDED;
113     int ifindex OVS_GUARDED;
114
115     struct dummy_packet_conn conn OVS_GUARDED;
116
117     FILE *tx_pcap, *rxq_pcap OVS_GUARDED;
118
119     struct in_addr address, netmask;
120     struct in6_addr ipv6, ipv6_mask;
121     struct ovs_list rxes OVS_GUARDED; /* List of child "netdev_rxq_dummy"s. */
122
123     /* The following properties are for dummy-pmd and they cannot be changed
124      * when a device is running, so we remember the request and update them
125      * next time netdev_dummy_reconfigure() is called. */
126     int requested_n_txq;
127     int requested_n_rxq;
128 };
129
130 /* Max 'recv_queue_len' in struct netdev_dummy. */
131 #define NETDEV_DUMMY_MAX_QUEUE 100
132
133 struct netdev_rxq_dummy {
134     struct netdev_rxq up;
135     struct ovs_list node;       /* In netdev_dummy's "rxes" list. */
136     struct ovs_list recv_queue;
137     int recv_queue_len;         /* ovs_list_size(&recv_queue). */
138     struct seq *seq;            /* Reports newly queued packets. */
139 };
140
141 static unixctl_cb_func netdev_dummy_set_admin_state;
142 static int netdev_dummy_construct(struct netdev *);
143 static void netdev_dummy_queue_packet(struct netdev_dummy *,
144                                       struct dp_packet *, int);
145
146 static void dummy_packet_stream_close(struct dummy_packet_stream *);
147
148 static void pkt_list_delete(struct ovs_list *);
149
150 static bool
151 is_dummy_class(const struct netdev_class *class)
152 {
153     return class->construct == netdev_dummy_construct;
154 }
155
156 static struct netdev_dummy *
157 netdev_dummy_cast(const struct netdev *netdev)
158 {
159     ovs_assert(is_dummy_class(netdev_get_class(netdev)));
160     return CONTAINER_OF(netdev, struct netdev_dummy, up);
161 }
162
163 static struct netdev_rxq_dummy *
164 netdev_rxq_dummy_cast(const struct netdev_rxq *rx)
165 {
166     ovs_assert(is_dummy_class(netdev_get_class(rx->netdev)));
167     return CONTAINER_OF(rx, struct netdev_rxq_dummy, up);
168 }
169
170 static void
171 dummy_packet_stream_init(struct dummy_packet_stream *s, struct stream *stream)
172 {
173     int rxbuf_size = stream ? 2048 : 0;
174     s->stream = stream;
175     dp_packet_init(&s->rxbuf, rxbuf_size);
176     ovs_list_init(&s->txq);
177 }
178
179 static struct dummy_packet_stream *
180 dummy_packet_stream_create(struct stream *stream)
181 {
182     struct dummy_packet_stream *s;
183
184     s = xzalloc(sizeof *s);
185     dummy_packet_stream_init(s, stream);
186
187     return s;
188 }
189
190 static void
191 dummy_packet_stream_wait(struct dummy_packet_stream *s)
192 {
193     stream_run_wait(s->stream);
194     if (!ovs_list_is_empty(&s->txq)) {
195         stream_send_wait(s->stream);
196     }
197     stream_recv_wait(s->stream);
198 }
199
200 static void
201 dummy_packet_stream_send(struct dummy_packet_stream *s, const void *buffer, size_t size)
202 {
203     if (ovs_list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) {
204         struct dp_packet *b;
205         struct pkt_list_node *node;
206
207         b = dp_packet_clone_data_with_headroom(buffer, size, 2);
208         put_unaligned_be16(dp_packet_push_uninit(b, 2), htons(size));
209
210         node = xmalloc(sizeof *node);
211         node->pkt = b;
212         ovs_list_push_back(&s->txq, &node->list_node);
213     }
214 }
215
216 static int
217 dummy_packet_stream_run(struct netdev_dummy *dev, struct dummy_packet_stream *s)
218 {
219     int error = 0;
220     size_t n;
221
222     stream_run(s->stream);
223
224     if (!ovs_list_is_empty(&s->txq)) {
225         struct pkt_list_node *txbuf_node;
226         struct dp_packet *txbuf;
227         int retval;
228
229         ASSIGN_CONTAINER(txbuf_node, ovs_list_front(&s->txq), list_node);
230         txbuf = txbuf_node->pkt;
231         retval = stream_send(s->stream, dp_packet_data(txbuf), dp_packet_size(txbuf));
232
233         if (retval > 0) {
234             dp_packet_pull(txbuf, retval);
235             if (!dp_packet_size(txbuf)) {
236                 ovs_list_remove(&txbuf_node->list_node);
237                 free(txbuf_node);
238                 dp_packet_delete(txbuf);
239             }
240         } else if (retval != -EAGAIN) {
241             error = -retval;
242         }
243     }
244
245     if (!error) {
246         if (dp_packet_size(&s->rxbuf) < 2) {
247             n = 2 - dp_packet_size(&s->rxbuf);
248         } else {
249             uint16_t frame_len;
250
251             frame_len = ntohs(get_unaligned_be16(dp_packet_data(&s->rxbuf)));
252             if (frame_len < ETH_HEADER_LEN) {
253                 error = EPROTO;
254                 n = 0;
255             } else {
256                 n = (2 + frame_len) - dp_packet_size(&s->rxbuf);
257             }
258         }
259     }
260     if (!error) {
261         int retval;
262
263         dp_packet_prealloc_tailroom(&s->rxbuf, n);
264         retval = stream_recv(s->stream, dp_packet_tail(&s->rxbuf), n);
265
266         if (retval > 0) {
267             dp_packet_set_size(&s->rxbuf, dp_packet_size(&s->rxbuf) + retval);
268             if (retval == n && dp_packet_size(&s->rxbuf) > 2) {
269                 dp_packet_pull(&s->rxbuf, 2);
270                 netdev_dummy_queue_packet(dev,
271                                           dp_packet_clone(&s->rxbuf), 0);
272                 dp_packet_clear(&s->rxbuf);
273             }
274         } else if (retval != -EAGAIN) {
275             error = (retval < 0 ? -retval
276                      : dp_packet_size(&s->rxbuf) ? EPROTO
277                      : EOF);
278         }
279     }
280
281     return error;
282 }
283
284 static void
285 dummy_packet_stream_close(struct dummy_packet_stream *s)
286 {
287     stream_close(s->stream);
288     dp_packet_uninit(&s->rxbuf);
289     pkt_list_delete(&s->txq);
290 }
291
292 static void
293 dummy_packet_conn_init(struct dummy_packet_conn *conn)
294 {
295     memset(conn, 0, sizeof *conn);
296     conn->type = NONE;
297 }
298
299 static void
300 dummy_packet_conn_get_config(struct dummy_packet_conn *conn, struct smap *args)
301 {
302
303     switch (conn->type) {
304     case PASSIVE:
305         smap_add(args, "pstream", pstream_get_name(conn->u.pconn.pstream));
306         break;
307
308     case ACTIVE:
309         smap_add(args, "stream", stream_get_name(conn->u.rconn.rstream->stream));
310         break;
311
312     case NONE:
313     default:
314         break;
315     }
316 }
317
318 static void
319 dummy_packet_conn_close(struct dummy_packet_conn *conn)
320 {
321     int i;
322     struct dummy_packet_pconn *pconn = &conn->u.pconn;
323     struct dummy_packet_rconn *rconn = &conn->u.rconn;
324
325     switch (conn->type) {
326     case PASSIVE:
327         pstream_close(pconn->pstream);
328         for (i = 0; i < pconn->n_streams; i++) {
329             dummy_packet_stream_close(&pconn->streams[i]);
330         }
331         free(pconn->streams);
332         pconn->pstream = NULL;
333         pconn->streams = NULL;
334         break;
335
336     case ACTIVE:
337         dummy_packet_stream_close(rconn->rstream);
338         free(rconn->rstream);
339         rconn->rstream = NULL;
340         reconnect_destroy(rconn->reconnect);
341         rconn->reconnect = NULL;
342         break;
343
344     case NONE:
345     default:
346         break;
347     }
348
349     conn->type = NONE;
350     memset(conn, 0, sizeof *conn);
351 }
352
353 static void
354 dummy_packet_conn_set_config(struct dummy_packet_conn *conn,
355                              const struct smap *args)
356 {
357     const char *pstream = smap_get(args, "pstream");
358     const char *stream = smap_get(args, "stream");
359
360     if (pstream && stream) {
361          VLOG_WARN("Open failed: both %s and %s are configured",
362                    pstream, stream);
363          return;
364     }
365
366     switch (conn->type) {
367     case PASSIVE:
368         if (pstream &&
369             !strcmp(pstream_get_name(conn->u.pconn.pstream), pstream)) {
370             return;
371         }
372         dummy_packet_conn_close(conn);
373         break;
374     case ACTIVE:
375         if (stream &&
376             !strcmp(stream_get_name(conn->u.rconn.rstream->stream), stream)) {
377             return;
378         }
379         dummy_packet_conn_close(conn);
380         break;
381     case NONE:
382     default:
383         break;
384     }
385
386     if (pstream) {
387         int error;
388
389         error = pstream_open(pstream, &conn->u.pconn.pstream, DSCP_DEFAULT);
390         if (error) {
391             VLOG_WARN("%s: open failed (%s)", pstream, ovs_strerror(error));
392         } else {
393             conn->type = PASSIVE;
394         }
395     }
396
397     if (stream) {
398         int error;
399         struct stream *active_stream;
400         struct reconnect *reconnect;
401
402         reconnect = reconnect_create(time_msec());
403         reconnect_set_name(reconnect, stream);
404         reconnect_set_passive(reconnect, false, time_msec());
405         reconnect_enable(reconnect, time_msec());
406         reconnect_set_backoff(reconnect, 100, INT_MAX);
407         reconnect_set_probe_interval(reconnect, 0);
408         conn->u.rconn.reconnect = reconnect;
409         conn->type = ACTIVE;
410
411         error = stream_open(stream, &active_stream, DSCP_DEFAULT);
412         conn->u.rconn.rstream = dummy_packet_stream_create(active_stream);
413
414         switch (error) {
415         case 0:
416             reconnect_connected(reconnect, time_msec());
417             break;
418
419         case EAGAIN:
420             reconnect_connecting(reconnect, time_msec());
421             break;
422
423         default:
424             reconnect_connect_failed(reconnect, time_msec(), error);
425             stream_close(active_stream);
426             conn->u.rconn.rstream->stream = NULL;
427             break;
428         }
429     }
430 }
431
432 static void
433 dummy_pconn_run(struct netdev_dummy *dev)
434     OVS_REQUIRES(dev->mutex)
435 {
436     struct stream *new_stream;
437     struct dummy_packet_pconn *pconn = &dev->conn.u.pconn;
438     int error;
439     size_t i;
440
441     error = pstream_accept(pconn->pstream, &new_stream);
442     if (!error) {
443         struct dummy_packet_stream *s;
444
445         pconn->streams = xrealloc(pconn->streams,
446                                 ((pconn->n_streams + 1)
447                                  * sizeof *s));
448         s = &pconn->streams[pconn->n_streams++];
449         dummy_packet_stream_init(s, new_stream);
450     } else if (error != EAGAIN) {
451         VLOG_WARN("%s: accept failed (%s)",
452                   pstream_get_name(pconn->pstream), ovs_strerror(error));
453         pstream_close(pconn->pstream);
454         pconn->pstream = NULL;
455         dev->conn.type = NONE;
456     }
457
458     for (i = 0; i < pconn->n_streams; i++) {
459         struct dummy_packet_stream *s = &pconn->streams[i];
460
461         error = dummy_packet_stream_run(dev, s);
462         if (error) {
463             VLOG_DBG("%s: closing connection (%s)",
464                      stream_get_name(s->stream),
465                      ovs_retval_to_string(error));
466             dummy_packet_stream_close(s);
467             pconn->streams[i] = pconn->streams[--pconn->n_streams];
468         }
469     }
470 }
471
472 static void
473 dummy_rconn_run(struct netdev_dummy *dev)
474 OVS_REQUIRES(dev->mutex)
475 {
476     struct dummy_packet_rconn *rconn = &dev->conn.u.rconn;
477
478     switch (reconnect_run(rconn->reconnect, time_msec())) {
479     case RECONNECT_CONNECT:
480         {
481             int error;
482
483             if (rconn->rstream->stream) {
484                 error = stream_connect(rconn->rstream->stream);
485             } else {
486                 error = stream_open(reconnect_get_name(rconn->reconnect),
487                                     &rconn->rstream->stream, DSCP_DEFAULT);
488             }
489
490             switch (error) {
491             case 0:
492                 reconnect_connected(rconn->reconnect, time_msec());
493                 break;
494
495             case EAGAIN:
496                 reconnect_connecting(rconn->reconnect, time_msec());
497                 break;
498
499             default:
500                 reconnect_connect_failed(rconn->reconnect, time_msec(), error);
501                 stream_close(rconn->rstream->stream);
502                 rconn->rstream->stream = NULL;
503                 break;
504             }
505         }
506         break;
507
508     case RECONNECT_DISCONNECT:
509     case RECONNECT_PROBE:
510     default:
511         break;
512     }
513
514     if (reconnect_is_connected(rconn->reconnect)) {
515         int err;
516
517         err = dummy_packet_stream_run(dev, rconn->rstream);
518
519         if (err) {
520             reconnect_disconnected(rconn->reconnect, time_msec(), err);
521             stream_close(rconn->rstream->stream);
522             rconn->rstream->stream = NULL;
523         }
524     }
525 }
526
527 static void
528 dummy_packet_conn_run(struct netdev_dummy *dev)
529     OVS_REQUIRES(dev->mutex)
530 {
531     switch (dev->conn.type) {
532     case PASSIVE:
533         dummy_pconn_run(dev);
534         break;
535
536     case ACTIVE:
537         dummy_rconn_run(dev);
538         break;
539
540     case NONE:
541     default:
542         break;
543     }
544 }
545
546 static void
547 dummy_packet_conn_wait(struct dummy_packet_conn *conn)
548 {
549     int i;
550     switch (conn->type) {
551     case PASSIVE:
552         pstream_wait(conn->u.pconn.pstream);
553         for (i = 0; i < conn->u.pconn.n_streams; i++) {
554             struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
555             dummy_packet_stream_wait(s);
556         }
557         break;
558     case ACTIVE:
559         if (reconnect_is_connected(conn->u.rconn.reconnect)) {
560             dummy_packet_stream_wait(conn->u.rconn.rstream);
561         }
562         break;
563
564     case NONE:
565     default:
566         break;
567     }
568 }
569
570 static void
571 dummy_packet_conn_send(struct dummy_packet_conn *conn,
572                        const void *buffer, size_t size)
573 {
574     int i;
575
576     switch (conn->type) {
577     case PASSIVE:
578         for (i = 0; i < conn->u.pconn.n_streams; i++) {
579             struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
580
581             dummy_packet_stream_send(s, buffer, size);
582             pstream_wait(conn->u.pconn.pstream);
583         }
584         break;
585
586     case ACTIVE:
587         if (reconnect_is_connected(conn->u.rconn.reconnect)) {
588             dummy_packet_stream_send(conn->u.rconn.rstream, buffer, size);
589             dummy_packet_stream_wait(conn->u.rconn.rstream);
590         }
591         break;
592
593     case NONE:
594     default:
595         break;
596     }
597 }
598
599 static enum dummy_netdev_conn_state
600 dummy_netdev_get_conn_state(struct dummy_packet_conn *conn)
601 {
602     enum dummy_netdev_conn_state state;
603
604     if (conn->type == ACTIVE) {
605         if (reconnect_is_connected(conn->u.rconn.reconnect)) {
606             state = CONN_STATE_CONNECTED;
607         } else {
608             state = CONN_STATE_NOT_CONNECTED;
609         }
610     } else {
611         state = CONN_STATE_UNKNOWN;
612     }
613
614     return state;
615 }
616
617 static void
618 netdev_dummy_run(void)
619 {
620     struct netdev_dummy *dev;
621
622     ovs_mutex_lock(&dummy_list_mutex);
623     LIST_FOR_EACH (dev, list_node, &dummy_list) {
624         ovs_mutex_lock(&dev->mutex);
625         dummy_packet_conn_run(dev);
626         ovs_mutex_unlock(&dev->mutex);
627     }
628     ovs_mutex_unlock(&dummy_list_mutex);
629 }
630
631 static void
632 netdev_dummy_wait(void)
633 {
634     struct netdev_dummy *dev;
635
636     ovs_mutex_lock(&dummy_list_mutex);
637     LIST_FOR_EACH (dev, list_node, &dummy_list) {
638         ovs_mutex_lock(&dev->mutex);
639         dummy_packet_conn_wait(&dev->conn);
640         ovs_mutex_unlock(&dev->mutex);
641     }
642     ovs_mutex_unlock(&dummy_list_mutex);
643 }
644
645 static struct netdev *
646 netdev_dummy_alloc(void)
647 {
648     struct netdev_dummy *netdev = xzalloc(sizeof *netdev);
649     return &netdev->up;
650 }
651
652 static int
653 netdev_dummy_construct(struct netdev *netdev_)
654 {
655     static atomic_count next_n = ATOMIC_COUNT_INIT(0xaa550000);
656     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
657     unsigned int n;
658
659     n = atomic_count_inc(&next_n);
660
661     ovs_mutex_init(&netdev->mutex);
662     ovs_mutex_lock(&netdev->mutex);
663     netdev->hwaddr.ea[0] = 0xaa;
664     netdev->hwaddr.ea[1] = 0x55;
665     netdev->hwaddr.ea[2] = n >> 24;
666     netdev->hwaddr.ea[3] = n >> 16;
667     netdev->hwaddr.ea[4] = n >> 8;
668     netdev->hwaddr.ea[5] = n;
669     netdev->mtu = 1500;
670     netdev->flags = 0;
671     netdev->ifindex = -EOPNOTSUPP;
672     netdev->requested_n_rxq = netdev_->n_rxq;
673     netdev->requested_n_txq = netdev_->n_txq;
674
675     dummy_packet_conn_init(&netdev->conn);
676
677     ovs_list_init(&netdev->rxes);
678     ovs_mutex_unlock(&netdev->mutex);
679
680     ovs_mutex_lock(&dummy_list_mutex);
681     ovs_list_push_back(&dummy_list, &netdev->list_node);
682     ovs_mutex_unlock(&dummy_list_mutex);
683
684     return 0;
685 }
686
687 static void
688 netdev_dummy_destruct(struct netdev *netdev_)
689 {
690     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
691
692     ovs_mutex_lock(&dummy_list_mutex);
693     ovs_list_remove(&netdev->list_node);
694     ovs_mutex_unlock(&dummy_list_mutex);
695
696     ovs_mutex_lock(&netdev->mutex);
697     dummy_packet_conn_close(&netdev->conn);
698     netdev->conn.type = NONE;
699
700     ovs_mutex_unlock(&netdev->mutex);
701     ovs_mutex_destroy(&netdev->mutex);
702 }
703
704 static void
705 netdev_dummy_dealloc(struct netdev *netdev_)
706 {
707     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
708
709     free(netdev);
710 }
711
712 static int
713 netdev_dummy_get_config(const struct netdev *dev, struct smap *args)
714 {
715     struct netdev_dummy *netdev = netdev_dummy_cast(dev);
716
717     ovs_mutex_lock(&netdev->mutex);
718
719     if (netdev->ifindex >= 0) {
720         smap_add_format(args, "ifindex", "%d", netdev->ifindex);
721     }
722
723     dummy_packet_conn_get_config(&netdev->conn, args);
724
725     /* 'dummy-pmd' specific config. */
726     if (!netdev_is_pmd(dev)) {
727         goto exit;
728     }
729     smap_add_format(args, "requested_rx_queues", "%d", netdev->requested_n_rxq);
730     smap_add_format(args, "configured_rx_queues", "%d", dev->n_rxq);
731     smap_add_format(args, "requested_tx_queues", "%d", netdev->requested_n_txq);
732     smap_add_format(args, "configured_tx_queues", "%d", dev->n_txq);
733
734 exit:
735     ovs_mutex_unlock(&netdev->mutex);
736     return 0;
737 }
738
739 static int
740 netdev_dummy_get_addr_list(const struct netdev *netdev_, struct in6_addr **paddr,
741                            struct in6_addr **pmask, int *n_addr)
742 {
743     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
744     int cnt = 0, i = 0, err = 0;
745     struct in6_addr *addr, *mask;
746
747     ovs_mutex_lock(&netdev->mutex);
748     if (netdev->address.s_addr != INADDR_ANY) {
749         cnt++;
750     }
751
752     if (ipv6_addr_is_set(&netdev->ipv6)) {
753         cnt++;
754     }
755     if (!cnt) {
756         err = EADDRNOTAVAIL;
757         goto out;
758     }
759     addr = xmalloc(sizeof *addr * cnt);
760     mask = xmalloc(sizeof *mask * cnt);
761     if (netdev->address.s_addr != INADDR_ANY) {
762         in6_addr_set_mapped_ipv4(&addr[i], netdev->address.s_addr);
763         in6_addr_set_mapped_ipv4(&mask[i], netdev->netmask.s_addr);
764         i++;
765     }
766
767     if (ipv6_addr_is_set(&netdev->ipv6)) {
768         memcpy(&addr[i], &netdev->ipv6, sizeof *addr);
769         memcpy(&mask[i], &netdev->ipv6_mask, sizeof *mask);
770         i++;
771     }
772     if (paddr) {
773         *paddr = addr;
774         *pmask = mask;
775         *n_addr = cnt;
776     } else {
777         free(addr);
778         free(mask);
779     }
780 out:
781     ovs_mutex_unlock(&netdev->mutex);
782
783     return err;
784 }
785
786 static int
787 netdev_dummy_set_in4(struct netdev *netdev_, struct in_addr address,
788                      struct in_addr netmask)
789 {
790     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
791
792     ovs_mutex_lock(&netdev->mutex);
793     netdev->address = address;
794     netdev->netmask = netmask;
795     netdev_change_seq_changed(netdev_);
796     ovs_mutex_unlock(&netdev->mutex);
797
798     return 0;
799 }
800
801 static int
802 netdev_dummy_set_in6(struct netdev *netdev_, struct in6_addr *in6,
803                      struct in6_addr *mask)
804 {
805     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
806
807     ovs_mutex_lock(&netdev->mutex);
808     netdev->ipv6 = *in6;
809     netdev->ipv6_mask = *mask;
810     netdev_change_seq_changed(netdev_);
811     ovs_mutex_unlock(&netdev->mutex);
812
813     return 0;
814 }
815
816 static int
817 netdev_dummy_set_config(struct netdev *netdev_, const struct smap *args)
818 {
819     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
820     const char *pcap;
821     int new_n_rxq;
822
823     ovs_mutex_lock(&netdev->mutex);
824     netdev->ifindex = smap_get_int(args, "ifindex", -EOPNOTSUPP);
825
826     dummy_packet_conn_set_config(&netdev->conn, args);
827
828     if (netdev->rxq_pcap) {
829         fclose(netdev->rxq_pcap);
830     }
831     if (netdev->tx_pcap && netdev->tx_pcap != netdev->rxq_pcap) {
832         fclose(netdev->tx_pcap);
833     }
834     netdev->rxq_pcap = netdev->tx_pcap = NULL;
835     pcap = smap_get(args, "pcap");
836     if (pcap) {
837         netdev->rxq_pcap = netdev->tx_pcap = ovs_pcap_open(pcap, "ab");
838     } else {
839         const char *rxq_pcap = smap_get(args, "rxq_pcap");
840         const char *tx_pcap = smap_get(args, "tx_pcap");
841
842         if (rxq_pcap) {
843             netdev->rxq_pcap = ovs_pcap_open(rxq_pcap, "ab");
844         }
845         if (tx_pcap) {
846             netdev->tx_pcap = ovs_pcap_open(tx_pcap, "ab");
847         }
848     }
849
850     netdev_change_seq_changed(netdev_);
851
852     /* 'dummy-pmd' specific config. */
853     if (!netdev_->netdev_class->is_pmd) {
854         goto exit;
855     }
856
857     new_n_rxq = MAX(smap_get_int(args, "n_rxq", netdev->requested_n_rxq), 1);
858     if (new_n_rxq != netdev->requested_n_rxq) {
859         netdev->requested_n_rxq = new_n_rxq;
860         netdev_request_reconfigure(netdev_);
861     }
862
863 exit:
864     ovs_mutex_unlock(&netdev->mutex);
865     return 0;
866 }
867
868 static int
869 netdev_dummy_get_numa_id(const struct netdev *netdev_ OVS_UNUSED)
870 {
871     return 0;
872 }
873
874 /* Requests the number of tx queues for the dummy PMD interface. */
875 static int
876 netdev_dummy_set_tx_multiq(struct netdev *netdev_, unsigned int n_txq)
877 {
878     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
879
880     ovs_mutex_lock(&netdev->mutex);
881
882     if (netdev_->n_txq == n_txq) {
883         goto out;
884     }
885
886     netdev->requested_n_txq = n_txq;
887     netdev_request_reconfigure(netdev_);
888
889 out:
890     ovs_mutex_unlock(&netdev->mutex);
891     return 0;
892 }
893
894 /* Sets the number of tx queues and rx queues for the dummy PMD interface. */
895 static int
896 netdev_dummy_reconfigure(struct netdev *netdev_)
897 {
898     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
899
900     ovs_mutex_lock(&netdev->mutex);
901
902     netdev_->n_txq = netdev->requested_n_txq;
903     netdev_->n_rxq = netdev->requested_n_rxq;
904
905     ovs_mutex_unlock(&netdev->mutex);
906     return 0;
907 }
908
909 static struct netdev_rxq *
910 netdev_dummy_rxq_alloc(void)
911 {
912     struct netdev_rxq_dummy *rx = xzalloc(sizeof *rx);
913     return &rx->up;
914 }
915
916 static int
917 netdev_dummy_rxq_construct(struct netdev_rxq *rxq_)
918 {
919     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
920     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
921
922     ovs_mutex_lock(&netdev->mutex);
923     ovs_list_push_back(&netdev->rxes, &rx->node);
924     ovs_list_init(&rx->recv_queue);
925     rx->recv_queue_len = 0;
926     rx->seq = seq_create();
927     ovs_mutex_unlock(&netdev->mutex);
928
929     return 0;
930 }
931
932 static void
933 netdev_dummy_rxq_destruct(struct netdev_rxq *rxq_)
934 {
935     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
936     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
937
938     ovs_mutex_lock(&netdev->mutex);
939     ovs_list_remove(&rx->node);
940     pkt_list_delete(&rx->recv_queue);
941     ovs_mutex_unlock(&netdev->mutex);
942     seq_destroy(rx->seq);
943 }
944
945 static void
946 netdev_dummy_rxq_dealloc(struct netdev_rxq *rxq_)
947 {
948     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
949
950     free(rx);
951 }
952
953 static int
954 netdev_dummy_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet **arr,
955                       int *c)
956 {
957     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
958     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
959     struct dp_packet *packet;
960
961     ovs_mutex_lock(&netdev->mutex);
962     if (!ovs_list_is_empty(&rx->recv_queue)) {
963         struct pkt_list_node *pkt_node;
964
965         ASSIGN_CONTAINER(pkt_node, ovs_list_pop_front(&rx->recv_queue), list_node);
966         packet = pkt_node->pkt;
967         free(pkt_node);
968         rx->recv_queue_len--;
969     } else {
970         packet = NULL;
971     }
972     ovs_mutex_unlock(&netdev->mutex);
973
974     if (!packet) {
975         if (netdev_is_pmd(&netdev->up)) {
976             /* If 'netdev' is a PMD device, this is called as part of the PMD
977              * thread busy loop.  We yield here (without quiescing) for two
978              * reasons:
979              *
980              * - To reduce the CPU utilization during the testsuite
981              * - To give valgrind a chance to switch thread. According
982              *   to the valgrind documentation, there's a big lock that
983              *   prevents multiple thread from being executed at the same
984              *   time.  On my system, without this sleep, the pmd threads
985              *   testcases fail under valgrind, because ovs-vswitchd becomes
986              *   unresponsive. */
987             sched_yield();
988         }
989         return EAGAIN;
990     }
991     ovs_mutex_lock(&netdev->mutex);
992     netdev->stats.rx_packets++;
993     netdev->stats.rx_bytes += dp_packet_size(packet);
994     ovs_mutex_unlock(&netdev->mutex);
995
996     dp_packet_pad(packet);
997
998     arr[0] = packet;
999     *c = 1;
1000     return 0;
1001 }
1002
1003 static void
1004 netdev_dummy_rxq_wait(struct netdev_rxq *rxq_)
1005 {
1006     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
1007     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
1008     uint64_t seq = seq_read(rx->seq);
1009
1010     ovs_mutex_lock(&netdev->mutex);
1011     if (!ovs_list_is_empty(&rx->recv_queue)) {
1012         poll_immediate_wake();
1013     } else {
1014         seq_wait(rx->seq, seq);
1015     }
1016     ovs_mutex_unlock(&netdev->mutex);
1017 }
1018
1019 static int
1020 netdev_dummy_rxq_drain(struct netdev_rxq *rxq_)
1021 {
1022     struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
1023     struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
1024
1025     ovs_mutex_lock(&netdev->mutex);
1026     pkt_list_delete(&rx->recv_queue);
1027     rx->recv_queue_len = 0;
1028     ovs_mutex_unlock(&netdev->mutex);
1029
1030     seq_change(rx->seq);
1031
1032     return 0;
1033 }
1034
1035 static int
1036 netdev_dummy_send(struct netdev *netdev, int qid OVS_UNUSED,
1037                   struct dp_packet **pkts, int cnt, bool may_steal)
1038 {
1039     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1040     int error = 0;
1041     int i;
1042
1043     for (i = 0; i < cnt; i++) {
1044         const void *buffer = dp_packet_data(pkts[i]);
1045         size_t size = dp_packet_size(pkts[i]);
1046
1047         if (size < ETH_HEADER_LEN) {
1048             error = EMSGSIZE;
1049             break;
1050         } else {
1051             const struct eth_header *eth = buffer;
1052             int max_size;
1053
1054             ovs_mutex_lock(&dev->mutex);
1055             max_size = dev->mtu + ETH_HEADER_LEN;
1056             ovs_mutex_unlock(&dev->mutex);
1057
1058             if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
1059                 max_size += VLAN_HEADER_LEN;
1060             }
1061             if (size > max_size) {
1062                 error = EMSGSIZE;
1063                 break;
1064             }
1065         }
1066
1067         ovs_mutex_lock(&dev->mutex);
1068         dev->stats.tx_packets++;
1069         dev->stats.tx_bytes += size;
1070
1071         dummy_packet_conn_send(&dev->conn, buffer, size);
1072
1073         /* Reply to ARP requests for 'dev''s assigned IP address. */
1074         if (dev->address.s_addr) {
1075             struct dp_packet packet;
1076             struct flow flow;
1077
1078             dp_packet_use_const(&packet, buffer, size);
1079             flow_extract(&packet, &flow);
1080             if (flow.dl_type == htons(ETH_TYPE_ARP)
1081                 && flow.nw_proto == ARP_OP_REQUEST
1082                 && flow.nw_dst == dev->address.s_addr) {
1083                 struct dp_packet *reply = dp_packet_new(0);
1084                 compose_arp(reply, ARP_OP_REPLY, dev->hwaddr, flow.dl_src,
1085                             false, flow.nw_dst, flow.nw_src);
1086                 netdev_dummy_queue_packet(dev, reply, 0);
1087             }
1088         }
1089
1090         if (dev->tx_pcap) {
1091             struct dp_packet packet;
1092
1093             dp_packet_use_const(&packet, buffer, size);
1094             ovs_pcap_write(dev->tx_pcap, &packet);
1095             fflush(dev->tx_pcap);
1096         }
1097
1098         ovs_mutex_unlock(&dev->mutex);
1099     }
1100
1101     if (may_steal) {
1102         for (i = 0; i < cnt; i++) {
1103             dp_packet_delete(pkts[i]);
1104         }
1105     }
1106
1107     return error;
1108 }
1109
1110 static int
1111 netdev_dummy_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
1112 {
1113     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1114
1115     ovs_mutex_lock(&dev->mutex);
1116     if (!eth_addr_equals(dev->hwaddr, mac)) {
1117         dev->hwaddr = mac;
1118         netdev_change_seq_changed(netdev);
1119     }
1120     ovs_mutex_unlock(&dev->mutex);
1121
1122     return 0;
1123 }
1124
1125 static int
1126 netdev_dummy_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
1127 {
1128     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1129
1130     ovs_mutex_lock(&dev->mutex);
1131     *mac = dev->hwaddr;
1132     ovs_mutex_unlock(&dev->mutex);
1133
1134     return 0;
1135 }
1136
1137 static int
1138 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
1139 {
1140     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1141
1142     ovs_mutex_lock(&dev->mutex);
1143     *mtup = dev->mtu;
1144     ovs_mutex_unlock(&dev->mutex);
1145
1146     return 0;
1147 }
1148
1149 static int
1150 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
1151 {
1152     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1153
1154     ovs_mutex_lock(&dev->mutex);
1155     dev->mtu = mtu;
1156     ovs_mutex_unlock(&dev->mutex);
1157
1158     return 0;
1159 }
1160
1161 static int
1162 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
1163 {
1164     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1165
1166     ovs_mutex_lock(&dev->mutex);
1167     /* Passing only collected counters */
1168     stats->tx_packets = dev->stats.tx_packets;
1169     stats->tx_bytes = dev->stats.tx_bytes;
1170     stats->rx_packets = dev->stats.rx_packets;
1171     stats->rx_bytes = dev->stats.rx_bytes;
1172     ovs_mutex_unlock(&dev->mutex);
1173
1174     return 0;
1175 }
1176
1177 static int
1178 netdev_dummy_get_queue(const struct netdev *netdev OVS_UNUSED,
1179                        unsigned int queue_id, struct smap *details OVS_UNUSED)
1180 {
1181     if (queue_id == 0) {
1182         return 0;
1183     } else {
1184         return EINVAL;
1185     }
1186 }
1187
1188 static void
1189 netdev_dummy_init_queue_stats(struct netdev_queue_stats *stats)
1190 {
1191     *stats = (struct netdev_queue_stats) {
1192         .tx_bytes = UINT64_MAX,
1193         .tx_packets = UINT64_MAX,
1194         .tx_errors = UINT64_MAX,
1195         .created = LLONG_MIN,
1196     };
1197 }
1198
1199 static int
1200 netdev_dummy_get_queue_stats(const struct netdev *netdev OVS_UNUSED,
1201                              unsigned int queue_id,
1202                              struct netdev_queue_stats *stats)
1203 {
1204     if (queue_id == 0) {
1205         netdev_dummy_init_queue_stats(stats);
1206         return 0;
1207     } else {
1208         return EINVAL;
1209     }
1210 }
1211
1212 struct netdev_dummy_queue_state {
1213     unsigned int next_queue;
1214 };
1215
1216 static int
1217 netdev_dummy_queue_dump_start(const struct netdev *netdev OVS_UNUSED,
1218                               void **statep)
1219 {
1220     struct netdev_dummy_queue_state *state = xmalloc(sizeof *state);
1221     state->next_queue = 0;
1222     *statep = state;
1223     return 0;
1224 }
1225
1226 static int
1227 netdev_dummy_queue_dump_next(const struct netdev *netdev OVS_UNUSED,
1228                              void *state_,
1229                              unsigned int *queue_id,
1230                              struct smap *details OVS_UNUSED)
1231 {
1232     struct netdev_dummy_queue_state *state = state_;
1233     if (state->next_queue == 0) {
1234         *queue_id = 0;
1235         state->next_queue++;
1236         return 0;
1237     } else {
1238         return EOF;
1239     }
1240 }
1241
1242 static int
1243 netdev_dummy_queue_dump_done(const struct netdev *netdev OVS_UNUSED,
1244                              void *state)
1245 {
1246     free(state);
1247     return 0;
1248 }
1249
1250 static int
1251 netdev_dummy_dump_queue_stats(const struct netdev *netdev OVS_UNUSED,
1252                               void (*cb)(unsigned int queue_id,
1253                                          struct netdev_queue_stats *,
1254                                          void *aux),
1255                               void *aux)
1256 {
1257     struct netdev_queue_stats stats;
1258     netdev_dummy_init_queue_stats(&stats);
1259     cb(0, &stats, aux);
1260     return 0;
1261 }
1262
1263 static int
1264 netdev_dummy_get_ifindex(const struct netdev *netdev)
1265 {
1266     struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1267     int ifindex;
1268
1269     ovs_mutex_lock(&dev->mutex);
1270     ifindex = dev->ifindex;
1271     ovs_mutex_unlock(&dev->mutex);
1272
1273     return ifindex;
1274 }
1275
1276 static int
1277 netdev_dummy_update_flags__(struct netdev_dummy *netdev,
1278                             enum netdev_flags off, enum netdev_flags on,
1279                             enum netdev_flags *old_flagsp)
1280     OVS_REQUIRES(netdev->mutex)
1281 {
1282     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1283         return EINVAL;
1284     }
1285
1286     *old_flagsp = netdev->flags;
1287     netdev->flags |= on;
1288     netdev->flags &= ~off;
1289     if (*old_flagsp != netdev->flags) {
1290         netdev_change_seq_changed(&netdev->up);
1291     }
1292
1293     return 0;
1294 }
1295
1296 static int
1297 netdev_dummy_update_flags(struct netdev *netdev_,
1298                           enum netdev_flags off, enum netdev_flags on,
1299                           enum netdev_flags *old_flagsp)
1300 {
1301     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
1302     int error;
1303
1304     ovs_mutex_lock(&netdev->mutex);
1305     error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp);
1306     ovs_mutex_unlock(&netdev->mutex);
1307
1308     return error;
1309 }
1310 \f
1311 /* Helper functions. */
1312
1313 #define NETDEV_DUMMY_CLASS(NAME, PMD, TX_MULTIQ, RECOFIGURE)       \
1314 {                                                               \
1315     NAME,                                                       \
1316     PMD,                        /* is_pmd */                    \
1317     NULL,                       /* init */                      \
1318     netdev_dummy_run,                                           \
1319     netdev_dummy_wait,                                          \
1320                                                                 \
1321     netdev_dummy_alloc,                                         \
1322     netdev_dummy_construct,                                     \
1323     netdev_dummy_destruct,                                      \
1324     netdev_dummy_dealloc,                                       \
1325     netdev_dummy_get_config,                                    \
1326     netdev_dummy_set_config,                                    \
1327     NULL,                       /* get_tunnel_config */         \
1328     NULL,                       /* build header */              \
1329     NULL,                       /* push header */               \
1330     NULL,                       /* pop header */                \
1331     netdev_dummy_get_numa_id,                                   \
1332     TX_MULTIQ,                                                  \
1333                                                                 \
1334     netdev_dummy_send,          /* send */                      \
1335     NULL,                       /* send_wait */                 \
1336                                                                 \
1337     netdev_dummy_set_etheraddr,                                 \
1338     netdev_dummy_get_etheraddr,                                 \
1339     netdev_dummy_get_mtu,                                       \
1340     netdev_dummy_set_mtu,                                       \
1341     netdev_dummy_get_ifindex,                                   \
1342     NULL,                       /* get_carrier */               \
1343     NULL,                       /* get_carrier_resets */        \
1344     NULL,                       /* get_miimon */                \
1345     netdev_dummy_get_stats,                                     \
1346                                                                 \
1347     NULL,                       /* get_features */              \
1348     NULL,                       /* set_advertisements */        \
1349                                                                 \
1350     NULL,                       /* set_policing */              \
1351     NULL,                       /* get_qos_types */             \
1352     NULL,                       /* get_qos_capabilities */      \
1353     NULL,                       /* get_qos */                   \
1354     NULL,                       /* set_qos */                   \
1355     netdev_dummy_get_queue,                                     \
1356     NULL,                       /* set_queue */                 \
1357     NULL,                       /* delete_queue */              \
1358     netdev_dummy_get_queue_stats,                               \
1359     netdev_dummy_queue_dump_start,                              \
1360     netdev_dummy_queue_dump_next,                               \
1361     netdev_dummy_queue_dump_done,                               \
1362     netdev_dummy_dump_queue_stats,                              \
1363                                                                 \
1364     NULL,                       /* set_in4 */                   \
1365     netdev_dummy_get_addr_list,                                 \
1366     NULL,                       /* add_router */                \
1367     NULL,                       /* get_next_hop */              \
1368     NULL,                       /* get_status */                \
1369     NULL,                       /* arp_lookup */                \
1370                                                                 \
1371     netdev_dummy_update_flags,                                  \
1372     RECOFIGURE,                                                 \
1373                                                                 \
1374     netdev_dummy_rxq_alloc,                                     \
1375     netdev_dummy_rxq_construct,                                 \
1376     netdev_dummy_rxq_destruct,                                  \
1377     netdev_dummy_rxq_dealloc,                                   \
1378     netdev_dummy_rxq_recv,                                      \
1379     netdev_dummy_rxq_wait,                                      \
1380     netdev_dummy_rxq_drain,                                     \
1381 }
1382
1383 static const struct netdev_class dummy_class =
1384     NETDEV_DUMMY_CLASS("dummy", false, NULL, NULL);
1385
1386 static const struct netdev_class dummy_pmd_class =
1387     NETDEV_DUMMY_CLASS("dummy-pmd", true,
1388                        netdev_dummy_set_tx_multiq,
1389                        netdev_dummy_reconfigure);
1390
1391 static void
1392 pkt_list_delete(struct ovs_list *l)
1393 {
1394     struct pkt_list_node *pkt;
1395
1396     LIST_FOR_EACH_POP(pkt, list_node, l) {
1397         dp_packet_delete(pkt->pkt);
1398         free(pkt);
1399     }
1400 }
1401
1402 static struct dp_packet *
1403 eth_from_packet_or_flow(const char *s)
1404 {
1405     enum odp_key_fitness fitness;
1406     struct dp_packet *packet;
1407     struct ofpbuf odp_key;
1408     struct flow flow;
1409     int error;
1410
1411     if (!eth_from_hex(s, &packet)) {
1412         return packet;
1413     }
1414
1415     /* Convert string to datapath key.
1416      *
1417      * It would actually be nicer to parse an OpenFlow-like flow key here, but
1418      * the code for that currently calls exit() on parse error.  We have to
1419      * settle for parsing a datapath key for now.
1420      */
1421     ofpbuf_init(&odp_key, 0);
1422     error = odp_flow_from_string(s, NULL, &odp_key, NULL);
1423     if (error) {
1424         ofpbuf_uninit(&odp_key);
1425         return NULL;
1426     }
1427
1428     /* Convert odp_key to flow. */
1429     fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
1430     if (fitness == ODP_FIT_ERROR) {
1431         ofpbuf_uninit(&odp_key);
1432         return NULL;
1433     }
1434
1435     packet = dp_packet_new(0);
1436     flow_compose(packet, &flow);
1437
1438     ofpbuf_uninit(&odp_key);
1439     return packet;
1440 }
1441
1442 static void
1443 netdev_dummy_queue_packet__(struct netdev_rxq_dummy *rx, struct dp_packet *packet)
1444 {
1445     struct pkt_list_node *pkt_node = xmalloc(sizeof *pkt_node);
1446
1447     pkt_node->pkt = packet;
1448     ovs_list_push_back(&rx->recv_queue, &pkt_node->list_node);
1449     rx->recv_queue_len++;
1450     seq_change(rx->seq);
1451 }
1452
1453 static void
1454 netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct dp_packet *packet,
1455                           int queue_id)
1456     OVS_REQUIRES(dummy->mutex)
1457 {
1458     struct netdev_rxq_dummy *rx, *prev;
1459
1460     if (dummy->rxq_pcap) {
1461         ovs_pcap_write(dummy->rxq_pcap, packet);
1462         fflush(dummy->rxq_pcap);
1463     }
1464     prev = NULL;
1465     LIST_FOR_EACH (rx, node, &dummy->rxes) {
1466         if (rx->up.queue_id == queue_id &&
1467             rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
1468             if (prev) {
1469                 netdev_dummy_queue_packet__(prev, dp_packet_clone(packet));
1470             }
1471             prev = rx;
1472         }
1473     }
1474     if (prev) {
1475         netdev_dummy_queue_packet__(prev, packet);
1476     } else {
1477         dp_packet_delete(packet);
1478     }
1479 }
1480
1481 static void
1482 netdev_dummy_receive(struct unixctl_conn *conn,
1483                      int argc, const char *argv[], void *aux OVS_UNUSED)
1484 {
1485     struct netdev_dummy *dummy_dev;
1486     struct netdev *netdev;
1487     int i, k = 1, rx_qid = 0;
1488
1489     netdev = netdev_from_name(argv[k++]);
1490     if (!netdev || !is_dummy_class(netdev->netdev_class)) {
1491         unixctl_command_reply_error(conn, "no such dummy netdev");
1492         goto exit_netdev;
1493     }
1494     dummy_dev = netdev_dummy_cast(netdev);
1495
1496     ovs_mutex_lock(&dummy_dev->mutex);
1497
1498     if (argc > k + 1 && !strcmp(argv[k], "--qid")) {
1499         rx_qid = strtol(argv[k + 1], NULL, 10);
1500         if (rx_qid < 0 || rx_qid >= netdev->n_rxq) {
1501             unixctl_command_reply_error(conn, "bad rx queue id.");
1502             goto exit;
1503         }
1504         k += 2;
1505     }
1506
1507     for (i = k; i < argc; i++) {
1508         struct dp_packet *packet;
1509
1510         packet = eth_from_packet_or_flow(argv[i]);
1511         if (!packet) {
1512             unixctl_command_reply_error(conn, "bad packet syntax");
1513             goto exit;
1514         }
1515
1516         netdev_dummy_queue_packet(dummy_dev, packet, rx_qid);
1517     }
1518
1519     unixctl_command_reply(conn, NULL);
1520
1521 exit:
1522     ovs_mutex_unlock(&dummy_dev->mutex);
1523 exit_netdev:
1524     netdev_close(netdev);
1525 }
1526
1527 static void
1528 netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state)
1529     OVS_REQUIRES(dev->mutex)
1530 {
1531     enum netdev_flags old_flags;
1532
1533     if (admin_state) {
1534         netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1535     } else {
1536         netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1537     }
1538 }
1539
1540 static void
1541 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
1542                              const char *argv[], void *aux OVS_UNUSED)
1543 {
1544     bool up;
1545
1546     if (!strcasecmp(argv[argc - 1], "up")) {
1547         up = true;
1548     } else if ( !strcasecmp(argv[argc - 1], "down")) {
1549         up = false;
1550     } else {
1551         unixctl_command_reply_error(conn, "Invalid Admin State");
1552         return;
1553     }
1554
1555     if (argc > 2) {
1556         struct netdev *netdev = netdev_from_name(argv[1]);
1557         if (netdev && is_dummy_class(netdev->netdev_class)) {
1558             struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1559
1560             ovs_mutex_lock(&dummy_dev->mutex);
1561             netdev_dummy_set_admin_state__(dummy_dev, up);
1562             ovs_mutex_unlock(&dummy_dev->mutex);
1563
1564             netdev_close(netdev);
1565         } else {
1566             unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1567             netdev_close(netdev);
1568             return;
1569         }
1570     } else {
1571         struct netdev_dummy *netdev;
1572
1573         ovs_mutex_lock(&dummy_list_mutex);
1574         LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1575             ovs_mutex_lock(&netdev->mutex);
1576             netdev_dummy_set_admin_state__(netdev, up);
1577             ovs_mutex_unlock(&netdev->mutex);
1578         }
1579         ovs_mutex_unlock(&dummy_list_mutex);
1580     }
1581     unixctl_command_reply(conn, "OK");
1582 }
1583
1584 static void
1585 display_conn_state__(struct ds *s, const char *name,
1586                      enum dummy_netdev_conn_state state)
1587 {
1588     ds_put_format(s, "%s: ", name);
1589
1590     switch (state) {
1591     case CONN_STATE_CONNECTED:
1592         ds_put_cstr(s, "connected\n");
1593         break;
1594
1595     case CONN_STATE_NOT_CONNECTED:
1596         ds_put_cstr(s, "disconnected\n");
1597         break;
1598
1599     case CONN_STATE_UNKNOWN:
1600     default:
1601         ds_put_cstr(s, "unknown\n");
1602         break;
1603     };
1604 }
1605
1606 static void
1607 netdev_dummy_conn_state(struct unixctl_conn *conn, int argc,
1608                         const char *argv[], void *aux OVS_UNUSED)
1609 {
1610     enum dummy_netdev_conn_state state = CONN_STATE_UNKNOWN;
1611     struct ds s;
1612
1613     ds_init(&s);
1614
1615     if (argc > 1) {
1616         const char *dev_name = argv[1];
1617         struct netdev *netdev = netdev_from_name(dev_name);
1618
1619         if (netdev && is_dummy_class(netdev->netdev_class)) {
1620             struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1621
1622             ovs_mutex_lock(&dummy_dev->mutex);
1623             state = dummy_netdev_get_conn_state(&dummy_dev->conn);
1624             ovs_mutex_unlock(&dummy_dev->mutex);
1625
1626             netdev_close(netdev);
1627         }
1628         display_conn_state__(&s, dev_name, state);
1629     } else {
1630         struct netdev_dummy *netdev;
1631
1632         ovs_mutex_lock(&dummy_list_mutex);
1633         LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1634             ovs_mutex_lock(&netdev->mutex);
1635             state = dummy_netdev_get_conn_state(&netdev->conn);
1636             ovs_mutex_unlock(&netdev->mutex);
1637             if (state != CONN_STATE_UNKNOWN) {
1638                 display_conn_state__(&s, netdev->up.name, state);
1639             }
1640         }
1641         ovs_mutex_unlock(&dummy_list_mutex);
1642     }
1643
1644     unixctl_command_reply(conn, ds_cstr(&s));
1645     ds_destroy(&s);
1646 }
1647
1648 static void
1649 netdev_dummy_ip4addr(struct unixctl_conn *conn, int argc OVS_UNUSED,
1650                      const char *argv[], void *aux OVS_UNUSED)
1651 {
1652     struct netdev *netdev = netdev_from_name(argv[1]);
1653
1654     if (netdev && is_dummy_class(netdev->netdev_class)) {
1655         struct in_addr ip, mask;
1656         char *error;
1657
1658         error = ip_parse_masked(argv[2], &ip.s_addr, &mask.s_addr);
1659         if (!error) {
1660             netdev_dummy_set_in4(netdev, ip, mask);
1661             unixctl_command_reply(conn, "OK");
1662         } else {
1663             unixctl_command_reply_error(conn, error);
1664             free(error);
1665         }
1666     } else {
1667         unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1668     }
1669
1670     netdev_close(netdev);
1671 }
1672
1673 static void
1674 netdev_dummy_ip6addr(struct unixctl_conn *conn, int argc OVS_UNUSED,
1675                      const char *argv[], void *aux OVS_UNUSED)
1676 {
1677     struct netdev *netdev = netdev_from_name(argv[1]);
1678
1679     if (netdev && is_dummy_class(netdev->netdev_class)) {
1680         struct in6_addr ip6;
1681         char *error;
1682         uint32_t plen;
1683
1684         error = ipv6_parse_cidr(argv[2], &ip6, &plen);
1685         if (!error) {
1686             struct in6_addr mask;
1687
1688             mask = ipv6_create_mask(plen);
1689             netdev_dummy_set_in6(netdev, &ip6, &mask);
1690             unixctl_command_reply(conn, "OK");
1691         } else {
1692             unixctl_command_reply_error(conn, error);
1693             free(error);
1694         }
1695         netdev_close(netdev);
1696     } else {
1697         unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1698     }
1699
1700     netdev_close(netdev);
1701 }
1702
1703
1704 static void
1705 netdev_dummy_override(const char *type)
1706 {
1707     if (!netdev_unregister_provider(type)) {
1708         struct netdev_class *class;
1709         int error;
1710
1711         class = xmemdup(&dummy_class, sizeof dummy_class);
1712         class->type = xstrdup(type);
1713         error = netdev_register_provider(class);
1714         if (error) {
1715             VLOG_ERR("%s: failed to register netdev provider (%s)",
1716                      type, ovs_strerror(error));
1717             free(CONST_CAST(char *, class->type));
1718             free(class);
1719         }
1720     }
1721 }
1722
1723 void
1724 netdev_dummy_register(enum dummy_level level)
1725 {
1726     unixctl_command_register("netdev-dummy/receive",
1727                              "name [--qid queue_id] packet|flow...",
1728                              2, INT_MAX, netdev_dummy_receive, NULL);
1729     unixctl_command_register("netdev-dummy/set-admin-state",
1730                              "[netdev] up|down", 1, 2,
1731                              netdev_dummy_set_admin_state, NULL);
1732     unixctl_command_register("netdev-dummy/conn-state",
1733                              "[netdev]", 0, 1,
1734                              netdev_dummy_conn_state, NULL);
1735     unixctl_command_register("netdev-dummy/ip4addr",
1736                              "[netdev] ipaddr/mask-prefix-len", 2, 2,
1737                              netdev_dummy_ip4addr, NULL);
1738     unixctl_command_register("netdev-dummy/ip6addr",
1739                              "[netdev] ip6addr", 2, 2,
1740                              netdev_dummy_ip6addr, NULL);
1741
1742     if (level == DUMMY_OVERRIDE_ALL) {
1743         struct sset types;
1744         const char *type;
1745
1746         sset_init(&types);
1747         netdev_enumerate_types(&types);
1748         SSET_FOR_EACH (type, &types) {
1749             if (strcmp(type, "patch")) {
1750                 netdev_dummy_override(type);
1751             }
1752         }
1753         sset_destroy(&types);
1754     } else if (level == DUMMY_OVERRIDE_SYSTEM) {
1755         netdev_dummy_override("system");
1756     }
1757     netdev_register_provider(&dummy_class);
1758     netdev_register_provider(&dummy_pmd_class);
1759
1760     netdev_vport_tunnel_register();
1761 }