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