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