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