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