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