2 * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "connectivity.h"
24 #include "dpif-netdev.h"
27 #include "netdev-provider.h"
28 #include "netdev-vport.h"
30 #include "ofp-print.h"
33 #include "pcap-file.h"
34 #include "poll-loop.h"
39 #include "unaligned.h"
42 #include "reconnect.h"
45 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
49 struct dummy_packet_stream {
50 struct stream *stream;
55 enum dummy_packet_conn_type {
56 NONE, /* No connection is configured. */
57 PASSIVE, /* Listener. */
58 ACTIVE /* Connect to listener. */
61 struct dummy_packet_pconn {
62 struct pstream *pstream;
63 struct dummy_packet_stream *streams;
67 struct dummy_packet_rconn {
68 struct dummy_packet_stream *rstream;
69 struct reconnect *reconnect;
72 struct dummy_packet_conn {
73 enum dummy_packet_conn_type type;
75 struct dummy_packet_pconn pconn;
76 struct dummy_packet_rconn rconn;
80 /* Protects 'dummy_list'. */
81 static struct ovs_mutex dummy_list_mutex = OVS_MUTEX_INITIALIZER;
83 /* Contains all 'struct dummy_dev's. */
84 static struct list dummy_list OVS_GUARDED_BY(dummy_list_mutex)
85 = LIST_INITIALIZER(&dummy_list);
91 struct list list_node OVS_GUARDED_BY(dummy_list_mutex);
93 /* Protects all members below. */
94 struct ovs_mutex mutex OVS_ACQ_AFTER(dummy_list_mutex);
96 uint8_t hwaddr[ETH_ADDR_LEN] OVS_GUARDED;
98 struct netdev_stats stats OVS_GUARDED;
99 enum netdev_flags flags OVS_GUARDED;
100 int ifindex OVS_GUARDED;
102 struct dummy_packet_conn conn OVS_GUARDED;
104 FILE *tx_pcap, *rx_pcap OVS_GUARDED;
106 struct list rxes OVS_GUARDED; /* List of child "netdev_rx_dummy"s. */
109 /* Max 'recv_queue_len' in struct netdev_dummy. */
110 #define NETDEV_DUMMY_MAX_QUEUE 100
112 struct netdev_rx_dummy {
114 struct list node; /* In netdev_dummy's "rxes" list. */
115 struct list recv_queue;
116 int recv_queue_len; /* list_size(&recv_queue). */
117 struct seq *seq; /* Reports newly queued packets. */
120 static unixctl_cb_func netdev_dummy_set_admin_state;
121 static int netdev_dummy_construct(struct netdev *);
122 static void netdev_dummy_queue_packet(struct netdev_dummy *, struct ofpbuf *);
124 static void dummy_packet_stream_close(struct dummy_packet_stream *);
127 is_dummy_class(const struct netdev_class *class)
129 return class->construct == netdev_dummy_construct;
132 static struct netdev_dummy *
133 netdev_dummy_cast(const struct netdev *netdev)
135 ovs_assert(is_dummy_class(netdev_get_class(netdev)));
136 return CONTAINER_OF(netdev, struct netdev_dummy, up);
139 static struct netdev_rx_dummy *
140 netdev_rx_dummy_cast(const struct netdev_rx *rx)
142 ovs_assert(is_dummy_class(netdev_get_class(rx->netdev)));
143 return CONTAINER_OF(rx, struct netdev_rx_dummy, up);
147 dummy_packet_stream_init(struct dummy_packet_stream *s, struct stream *stream)
149 int rxbuf_size = stream ? 2048 : 0;
151 ofpbuf_init(&s->rxbuf, rxbuf_size);
155 static struct dummy_packet_stream *
156 dummy_packet_stream_create(struct stream *stream)
158 struct dummy_packet_stream *s;
160 s = xzalloc(sizeof *s);
161 dummy_packet_stream_init(s, stream);
167 dummy_packet_stream_wait(struct dummy_packet_stream *s)
169 stream_run_wait(s->stream);
170 if (!list_is_empty(&s->txq)) {
171 stream_send_wait(s->stream);
173 stream_recv_wait(s->stream);
177 dummy_packet_stream_send(struct dummy_packet_stream *s, const void *buffer, size_t size)
179 if (list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) {
182 b = ofpbuf_clone_data_with_headroom(buffer, size, 2);
183 put_unaligned_be16(ofpbuf_push_uninit(b, 2), htons(size));
184 list_push_back(&s->txq, &b->list_node);
189 dummy_packet_stream_run(struct netdev_dummy *dev, struct dummy_packet_stream *s)
194 stream_run(s->stream);
196 if (!list_is_empty(&s->txq)) {
197 struct ofpbuf *txbuf;
200 txbuf = ofpbuf_from_list(list_front(&s->txq));
201 retval = stream_send(s->stream, txbuf->data, txbuf->size);
204 ofpbuf_pull(txbuf, retval);
206 list_remove(&txbuf->list_node);
207 ofpbuf_delete(txbuf);
209 } else if (retval != -EAGAIN) {
215 if (s->rxbuf.size < 2) {
216 n = 2 - s->rxbuf.size;
220 frame_len = ntohs(get_unaligned_be16(s->rxbuf.data));
221 if (frame_len < ETH_HEADER_LEN) {
225 n = (2 + frame_len) - s->rxbuf.size;
232 ofpbuf_prealloc_tailroom(&s->rxbuf, n);
233 retval = stream_recv(s->stream, ofpbuf_tail(&s->rxbuf), n);
236 s->rxbuf.size += retval;
237 if (retval == n && s->rxbuf.size > 2) {
238 ofpbuf_pull(&s->rxbuf, 2);
239 netdev_dummy_queue_packet(dev,
240 ofpbuf_clone(&s->rxbuf));
241 ofpbuf_clear(&s->rxbuf);
243 } else if (retval != -EAGAIN) {
244 error = (retval < 0 ? -retval
245 : s->rxbuf.size ? EPROTO
254 dummy_packet_stream_close(struct dummy_packet_stream *s)
256 stream_close(s->stream);
257 ofpbuf_uninit(&s->rxbuf);
258 ofpbuf_list_delete(&s->txq);
262 dummy_packet_conn_init(struct dummy_packet_conn *conn)
264 memset(conn, 0, sizeof *conn);
269 dummy_packet_conn_get_config(struct dummy_packet_conn *conn, struct smap *args)
272 switch (conn->type) {
274 smap_add(args, "pstream", pstream_get_name(conn->u.pconn.pstream));
278 smap_add(args, "stream", stream_get_name(conn->u.rconn.rstream->stream));
288 dummy_packet_conn_close(struct dummy_packet_conn *conn)
291 struct dummy_packet_pconn *pconn = &conn->u.pconn;
292 struct dummy_packet_rconn *rconn = &conn->u.rconn;
294 switch (conn->type) {
296 pstream_close(pconn->pstream);
297 for (i = 0; i < pconn->n_streams; i++) {
298 dummy_packet_stream_close(&pconn->streams[i]);
300 free(pconn->streams);
301 pconn->pstream = NULL;
302 pconn->streams = NULL;
306 dummy_packet_stream_close(rconn->rstream);
307 free(rconn->rstream);
308 rconn->rstream = NULL;
309 reconnect_destroy(rconn->reconnect);
310 rconn->reconnect = NULL;
319 memset(conn, 0, sizeof *conn);
323 dummy_packet_conn_set_config(struct dummy_packet_conn *conn,
324 const struct smap *args)
326 const char *pstream = smap_get(args, "pstream");
327 const char *stream = smap_get(args, "stream");
329 if (pstream && stream) {
330 VLOG_WARN("Open failed: both %s and %s are configured",
335 switch (conn->type) {
337 if (!strcmp(pstream_get_name(conn->u.pconn.pstream), pstream)) {
340 dummy_packet_conn_close(conn);
343 if (!strcmp(stream_get_name(conn->u.rconn.rstream->stream), stream)) {
346 dummy_packet_conn_close(conn);
356 error = pstream_open(pstream, &conn->u.pconn.pstream, DSCP_DEFAULT);
358 VLOG_WARN("%s: open failed (%s)", pstream, ovs_strerror(error));
360 conn->type = PASSIVE;
366 struct stream *active_stream;
367 struct reconnect *reconnect;;
369 reconnect = reconnect_create(time_msec());
370 reconnect_set_name(reconnect, stream);
371 reconnect_set_passive(reconnect, false, time_msec());
372 reconnect_enable(reconnect, time_msec());
373 reconnect_set_backoff(reconnect, 100, INT_MAX);
374 reconnect_set_probe_interval(reconnect, 0);
375 conn->u.rconn.reconnect = reconnect;
378 error = stream_open(stream, &active_stream, DSCP_DEFAULT);
379 conn->u.rconn.rstream = dummy_packet_stream_create(active_stream);
383 reconnect_connected(reconnect, time_msec());
387 reconnect_connecting(reconnect, time_msec());
391 reconnect_connect_failed(reconnect, time_msec(), error);
392 stream_close(active_stream);
393 conn->u.rconn.rstream->stream = NULL;
400 dummy_pconn_run(struct netdev_dummy *dev)
401 OVS_REQUIRES(dev->mutex)
403 struct stream *new_stream;
404 struct dummy_packet_pconn *pconn = &dev->conn.u.pconn;
408 error = pstream_accept(pconn->pstream, &new_stream);
410 struct dummy_packet_stream *s;
412 pconn->streams = xrealloc(pconn->streams,
413 ((pconn->n_streams + 1)
415 s = &pconn->streams[pconn->n_streams++];
416 dummy_packet_stream_init(s, new_stream);
417 } else if (error != EAGAIN) {
418 VLOG_WARN("%s: accept failed (%s)",
419 pstream_get_name(pconn->pstream), ovs_strerror(error));
420 pstream_close(pconn->pstream);
421 pconn->pstream = NULL;
422 dev->conn.type = NONE;
425 for (i = 0; i < pconn->n_streams; i++) {
426 struct dummy_packet_stream *s = &pconn->streams[i];
428 error = dummy_packet_stream_run(dev, s);
430 VLOG_DBG("%s: closing connection (%s)",
431 stream_get_name(s->stream),
432 ovs_retval_to_string(error));
433 dummy_packet_stream_close(s);
434 pconn->streams[i] = pconn->streams[--pconn->n_streams];
440 dummy_rconn_run(struct netdev_dummy *dev)
441 OVS_REQUIRES(dev->mutex)
443 struct dummy_packet_rconn *rconn = &dev->conn.u.rconn;
445 switch (reconnect_run(rconn->reconnect, time_msec())) {
446 case RECONNECT_CONNECT:
450 if (rconn->rstream->stream) {
451 error = stream_connect(rconn->rstream->stream);
453 error = stream_open(reconnect_get_name(rconn->reconnect),
454 &rconn->rstream->stream, DSCP_DEFAULT);
459 reconnect_connected(rconn->reconnect, time_msec());
463 reconnect_connecting(rconn->reconnect, time_msec());
467 reconnect_connect_failed(rconn->reconnect, time_msec(), error);
468 stream_close(rconn->rstream->stream);
469 rconn->rstream->stream = NULL;
475 case RECONNECT_DISCONNECT:
476 case RECONNECT_PROBE:
481 if (reconnect_is_connected(rconn->reconnect)) {
484 err = dummy_packet_stream_run(dev, rconn->rstream);
487 reconnect_disconnected(rconn->reconnect, time_msec(), err);
488 stream_close(rconn->rstream->stream);
489 rconn->rstream->stream = NULL;
495 dummy_packet_conn_run(struct netdev_dummy *dev)
496 OVS_REQUIRES(dev->mutex)
498 switch (dev->conn.type) {
500 dummy_pconn_run(dev);
504 dummy_rconn_run(dev);
514 dummy_packet_conn_wait(struct dummy_packet_conn *conn)
517 switch (conn->type) {
519 pstream_wait(conn->u.pconn.pstream);
520 for (i = 0; i < conn->u.pconn.n_streams; i++) {
521 struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
522 dummy_packet_stream_wait(s);
526 if (reconnect_is_connected(conn->u.rconn.reconnect)) {
527 dummy_packet_stream_wait(conn->u.rconn.rstream);
538 dummy_packet_conn_send(struct dummy_packet_conn *conn,
539 const void *buffer, size_t size)
543 switch (conn->type) {
545 for (i = 0; i < conn->u.pconn.n_streams; i++) {
546 struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
548 dummy_packet_stream_send(s, buffer, size);
549 pstream_wait(conn->u.pconn.pstream);
554 if (reconnect_is_connected(conn->u.rconn.reconnect)) {
555 dummy_packet_stream_send(conn->u.rconn.rstream, buffer, size);
556 dummy_packet_stream_wait(conn->u.rconn.rstream);
567 netdev_dummy_run(void)
569 struct netdev_dummy *dev;
571 ovs_mutex_lock(&dummy_list_mutex);
572 LIST_FOR_EACH (dev, list_node, &dummy_list) {
573 ovs_mutex_lock(&dev->mutex);
574 dummy_packet_conn_run(dev);
575 ovs_mutex_unlock(&dev->mutex);
577 ovs_mutex_unlock(&dummy_list_mutex);
581 netdev_dummy_wait(void)
583 struct netdev_dummy *dev;
585 ovs_mutex_lock(&dummy_list_mutex);
586 LIST_FOR_EACH (dev, list_node, &dummy_list) {
587 ovs_mutex_lock(&dev->mutex);
588 dummy_packet_conn_wait(&dev->conn);
589 ovs_mutex_unlock(&dev->mutex);
591 ovs_mutex_unlock(&dummy_list_mutex);
594 static struct netdev *
595 netdev_dummy_alloc(void)
597 struct netdev_dummy *netdev = xzalloc(sizeof *netdev);
602 netdev_dummy_construct(struct netdev *netdev_)
604 static atomic_uint next_n = ATOMIC_VAR_INIT(0xaa550000);
605 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
608 atomic_add(&next_n, 1, &n);
610 ovs_mutex_init(&netdev->mutex);
611 ovs_mutex_lock(&netdev->mutex);
612 netdev->hwaddr[0] = 0xaa;
613 netdev->hwaddr[1] = 0x55;
614 netdev->hwaddr[2] = n >> 24;
615 netdev->hwaddr[3] = n >> 16;
616 netdev->hwaddr[4] = n >> 8;
617 netdev->hwaddr[5] = n;
620 netdev->ifindex = -EOPNOTSUPP;
622 dummy_packet_conn_init(&netdev->conn);
624 list_init(&netdev->rxes);
625 ovs_mutex_unlock(&netdev->mutex);
627 ovs_mutex_lock(&dummy_list_mutex);
628 list_push_back(&dummy_list, &netdev->list_node);
629 ovs_mutex_unlock(&dummy_list_mutex);
635 netdev_dummy_destruct(struct netdev *netdev_)
637 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
639 ovs_mutex_lock(&dummy_list_mutex);
640 list_remove(&netdev->list_node);
641 ovs_mutex_unlock(&dummy_list_mutex);
643 ovs_mutex_lock(&netdev->mutex);
644 dummy_packet_conn_close(&netdev->conn);
645 netdev->conn.type = NONE;
647 ovs_mutex_unlock(&netdev->mutex);
648 ovs_mutex_destroy(&netdev->mutex);
652 netdev_dummy_dealloc(struct netdev *netdev_)
654 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
660 netdev_dummy_get_config(const struct netdev *netdev_, struct smap *args)
662 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
664 ovs_mutex_lock(&netdev->mutex);
666 if (netdev->ifindex >= 0) {
667 smap_add_format(args, "ifindex", "%d", netdev->ifindex);
670 dummy_packet_conn_get_config(&netdev->conn, args);
672 ovs_mutex_unlock(&netdev->mutex);
677 netdev_dummy_set_config(struct netdev *netdev_, const struct smap *args)
679 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
682 ovs_mutex_lock(&netdev->mutex);
683 netdev->ifindex = smap_get_int(args, "ifindex", -EOPNOTSUPP);
685 dummy_packet_conn_set_config(&netdev->conn, args);
687 if (netdev->rx_pcap) {
688 fclose(netdev->rx_pcap);
690 if (netdev->tx_pcap && netdev->tx_pcap != netdev->rx_pcap) {
691 fclose(netdev->tx_pcap);
693 netdev->rx_pcap = netdev->tx_pcap = NULL;
694 pcap = smap_get(args, "pcap");
696 netdev->rx_pcap = netdev->tx_pcap = ovs_pcap_open(pcap, "ab");
698 const char *rx_pcap = smap_get(args, "rx_pcap");
699 const char *tx_pcap = smap_get(args, "tx_pcap");
702 netdev->rx_pcap = ovs_pcap_open(rx_pcap, "ab");
705 netdev->tx_pcap = ovs_pcap_open(tx_pcap, "ab");
709 ovs_mutex_unlock(&netdev->mutex);
714 static struct netdev_rx *
715 netdev_dummy_rx_alloc(void)
717 struct netdev_rx_dummy *rx = xzalloc(sizeof *rx);
722 netdev_dummy_rx_construct(struct netdev_rx *rx_)
724 struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
725 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
727 ovs_mutex_lock(&netdev->mutex);
728 list_push_back(&netdev->rxes, &rx->node);
729 list_init(&rx->recv_queue);
730 rx->recv_queue_len = 0;
731 rx->seq = seq_create();
732 ovs_mutex_unlock(&netdev->mutex);
738 netdev_dummy_rx_destruct(struct netdev_rx *rx_)
740 struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
741 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
743 ovs_mutex_lock(&netdev->mutex);
744 list_remove(&rx->node);
745 ofpbuf_list_delete(&rx->recv_queue);
746 ovs_mutex_unlock(&netdev->mutex);
747 seq_destroy(rx->seq);
751 netdev_dummy_rx_dealloc(struct netdev_rx *rx_)
753 struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
759 netdev_dummy_rx_recv(struct netdev_rx *rx_, struct ofpbuf **arr, int *c)
761 struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
762 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
763 struct ofpbuf *packet;
765 ovs_mutex_lock(&netdev->mutex);
766 if (!list_is_empty(&rx->recv_queue)) {
767 packet = ofpbuf_from_list(list_pop_front(&rx->recv_queue));
768 rx->recv_queue_len--;
772 ovs_mutex_unlock(&netdev->mutex);
777 ovs_mutex_lock(&netdev->mutex);
778 netdev->stats.rx_packets++;
779 netdev->stats.rx_bytes += packet->size;
780 ovs_mutex_unlock(&netdev->mutex);
782 dp_packet_pad(packet);
789 netdev_dummy_rx_wait(struct netdev_rx *rx_)
791 struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
792 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
793 uint64_t seq = seq_read(rx->seq);
795 ovs_mutex_lock(&netdev->mutex);
796 if (!list_is_empty(&rx->recv_queue)) {
797 poll_immediate_wake();
799 seq_wait(rx->seq, seq);
801 ovs_mutex_unlock(&netdev->mutex);
805 netdev_dummy_rx_drain(struct netdev_rx *rx_)
807 struct netdev_rx_dummy *rx = netdev_rx_dummy_cast(rx_);
808 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
810 ovs_mutex_lock(&netdev->mutex);
811 ofpbuf_list_delete(&rx->recv_queue);
812 rx->recv_queue_len = 0;
813 ovs_mutex_unlock(&netdev->mutex);
821 netdev_dummy_send(struct netdev *netdev, struct ofpbuf *pkt, bool may_steal)
823 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
824 const void *buffer = pkt->data;
825 size_t size = pkt->size;
827 if (size < ETH_HEADER_LEN) {
830 const struct eth_header *eth = buffer;
833 ovs_mutex_lock(&dev->mutex);
834 max_size = dev->mtu + ETH_HEADER_LEN;
835 ovs_mutex_unlock(&dev->mutex);
837 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
838 max_size += VLAN_HEADER_LEN;
840 if (size > max_size) {
845 ovs_mutex_lock(&dev->mutex);
846 dev->stats.tx_packets++;
847 dev->stats.tx_bytes += size;
849 dummy_packet_conn_send(&dev->conn, buffer, size);
852 struct ofpbuf packet;
854 ofpbuf_use_const(&packet, buffer, size);
855 ovs_pcap_write(dev->tx_pcap, &packet);
856 fflush(dev->tx_pcap);
859 ovs_mutex_unlock(&dev->mutex);
868 netdev_dummy_set_etheraddr(struct netdev *netdev,
869 const uint8_t mac[ETH_ADDR_LEN])
871 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
873 ovs_mutex_lock(&dev->mutex);
874 if (!eth_addr_equals(dev->hwaddr, mac)) {
875 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
876 seq_change(connectivity_seq_get());
878 ovs_mutex_unlock(&dev->mutex);
884 netdev_dummy_get_etheraddr(const struct netdev *netdev,
885 uint8_t mac[ETH_ADDR_LEN])
887 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
889 ovs_mutex_lock(&dev->mutex);
890 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
891 ovs_mutex_unlock(&dev->mutex);
897 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
899 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
901 ovs_mutex_lock(&dev->mutex);
903 ovs_mutex_unlock(&dev->mutex);
909 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
911 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
913 ovs_mutex_lock(&dev->mutex);
915 ovs_mutex_unlock(&dev->mutex);
921 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
923 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
925 ovs_mutex_lock(&dev->mutex);
927 ovs_mutex_unlock(&dev->mutex);
933 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
935 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
937 ovs_mutex_lock(&dev->mutex);
939 ovs_mutex_unlock(&dev->mutex);
945 netdev_dummy_get_ifindex(const struct netdev *netdev)
947 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
950 ovs_mutex_lock(&dev->mutex);
951 ifindex = dev->ifindex;
952 ovs_mutex_unlock(&dev->mutex);
958 netdev_dummy_update_flags__(struct netdev_dummy *netdev,
959 enum netdev_flags off, enum netdev_flags on,
960 enum netdev_flags *old_flagsp)
961 OVS_REQUIRES(netdev->mutex)
963 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
967 *old_flagsp = netdev->flags;
969 netdev->flags &= ~off;
970 if (*old_flagsp != netdev->flags) {
971 seq_change(connectivity_seq_get());
978 netdev_dummy_update_flags(struct netdev *netdev_,
979 enum netdev_flags off, enum netdev_flags on,
980 enum netdev_flags *old_flagsp)
982 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
985 ovs_mutex_lock(&netdev->mutex);
986 error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp);
987 ovs_mutex_unlock(&netdev->mutex);
992 /* Helper functions. */
994 static const struct netdev_class dummy_class = {
1001 netdev_dummy_construct,
1002 netdev_dummy_destruct,
1003 netdev_dummy_dealloc,
1004 netdev_dummy_get_config,
1005 netdev_dummy_set_config,
1006 NULL, /* get_tunnel_config */
1008 netdev_dummy_send, /* send */
1009 NULL, /* send_wait */
1011 netdev_dummy_set_etheraddr,
1012 netdev_dummy_get_etheraddr,
1013 netdev_dummy_get_mtu,
1014 netdev_dummy_set_mtu,
1015 netdev_dummy_get_ifindex,
1016 NULL, /* get_carrier */
1017 NULL, /* get_carrier_resets */
1018 NULL, /* get_miimon */
1019 netdev_dummy_get_stats,
1020 netdev_dummy_set_stats,
1022 NULL, /* get_features */
1023 NULL, /* set_advertisements */
1025 NULL, /* set_policing */
1026 NULL, /* get_qos_types */
1027 NULL, /* get_qos_capabilities */
1030 NULL, /* get_queue */
1031 NULL, /* set_queue */
1032 NULL, /* delete_queue */
1033 NULL, /* get_queue_stats */
1034 NULL, /* queue_dump_start */
1035 NULL, /* queue_dump_next */
1036 NULL, /* queue_dump_done */
1037 NULL, /* dump_queue_stats */
1042 NULL, /* add_router */
1043 NULL, /* get_next_hop */
1044 NULL, /* get_status */
1045 NULL, /* arp_lookup */
1047 netdev_dummy_update_flags,
1049 netdev_dummy_rx_alloc,
1050 netdev_dummy_rx_construct,
1051 netdev_dummy_rx_destruct,
1052 netdev_dummy_rx_dealloc,
1053 netdev_dummy_rx_recv,
1054 netdev_dummy_rx_wait,
1055 netdev_dummy_rx_drain,
1058 static struct ofpbuf *
1059 eth_from_packet_or_flow(const char *s)
1061 enum odp_key_fitness fitness;
1062 struct ofpbuf *packet;
1063 struct ofpbuf odp_key;
1067 if (!eth_from_hex(s, &packet)) {
1071 /* Convert string to datapath key.
1073 * It would actually be nicer to parse an OpenFlow-like flow key here, but
1074 * the code for that currently calls exit() on parse error. We have to
1075 * settle for parsing a datapath key for now.
1077 ofpbuf_init(&odp_key, 0);
1078 error = odp_flow_from_string(s, NULL, &odp_key, NULL);
1080 ofpbuf_uninit(&odp_key);
1084 /* Convert odp_key to flow. */
1085 fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
1086 if (fitness == ODP_FIT_ERROR) {
1087 ofpbuf_uninit(&odp_key);
1091 packet = ofpbuf_new(0);
1092 flow_compose(packet, &flow);
1094 ofpbuf_uninit(&odp_key);
1099 netdev_dummy_queue_packet__(struct netdev_rx_dummy *rx, struct ofpbuf *packet)
1101 list_push_back(&rx->recv_queue, &packet->list_node);
1102 rx->recv_queue_len++;
1103 seq_change(rx->seq);
1107 netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct ofpbuf *packet)
1108 OVS_REQUIRES(dummy->mutex)
1110 struct netdev_rx_dummy *rx, *prev;
1112 if (dummy->rx_pcap) {
1113 ovs_pcap_write(dummy->rx_pcap, packet);
1114 fflush(dummy->rx_pcap);
1117 LIST_FOR_EACH (rx, node, &dummy->rxes) {
1118 if (rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
1120 netdev_dummy_queue_packet__(prev, ofpbuf_clone(packet));
1126 netdev_dummy_queue_packet__(prev, packet);
1128 ofpbuf_delete(packet);
1133 netdev_dummy_receive(struct unixctl_conn *conn,
1134 int argc, const char *argv[], void *aux OVS_UNUSED)
1136 struct netdev_dummy *dummy_dev;
1137 struct netdev *netdev;
1140 netdev = netdev_from_name(argv[1]);
1141 if (!netdev || !is_dummy_class(netdev->netdev_class)) {
1142 unixctl_command_reply_error(conn, "no such dummy netdev");
1145 dummy_dev = netdev_dummy_cast(netdev);
1147 for (i = 2; i < argc; i++) {
1148 struct ofpbuf *packet;
1150 packet = eth_from_packet_or_flow(argv[i]);
1152 unixctl_command_reply_error(conn, "bad packet syntax");
1156 ovs_mutex_lock(&dummy_dev->mutex);
1157 netdev_dummy_queue_packet(dummy_dev, packet);
1158 ovs_mutex_unlock(&dummy_dev->mutex);
1161 unixctl_command_reply(conn, NULL);
1164 netdev_close(netdev);
1168 netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state)
1169 OVS_REQUIRES(dev->mutex)
1171 enum netdev_flags old_flags;
1174 netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1176 netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1181 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
1182 const char *argv[], void *aux OVS_UNUSED)
1186 if (!strcasecmp(argv[argc - 1], "up")) {
1188 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1191 unixctl_command_reply_error(conn, "Invalid Admin State");
1196 struct netdev *netdev = netdev_from_name(argv[1]);
1197 if (netdev && is_dummy_class(netdev->netdev_class)) {
1198 struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1200 ovs_mutex_lock(&dummy_dev->mutex);
1201 netdev_dummy_set_admin_state__(dummy_dev, up);
1202 ovs_mutex_unlock(&dummy_dev->mutex);
1204 netdev_close(netdev);
1206 unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1207 netdev_close(netdev);
1211 struct netdev_dummy *netdev;
1213 ovs_mutex_lock(&dummy_list_mutex);
1214 LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1215 ovs_mutex_lock(&netdev->mutex);
1216 netdev_dummy_set_admin_state__(netdev, up);
1217 ovs_mutex_unlock(&netdev->mutex);
1219 ovs_mutex_unlock(&dummy_list_mutex);
1221 unixctl_command_reply(conn, "OK");
1225 netdev_dummy_register(bool override)
1227 unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
1228 2, INT_MAX, netdev_dummy_receive, NULL);
1229 unixctl_command_register("netdev-dummy/set-admin-state",
1230 "[netdev] up|down", 1, 2,
1231 netdev_dummy_set_admin_state, NULL);
1238 netdev_enumerate_types(&types);
1239 SSET_FOR_EACH (type, &types) {
1240 if (!strcmp(type, "patch")) {
1243 if (!netdev_unregister_provider(type)) {
1244 struct netdev_class *class;
1247 class = xmemdup(&dummy_class, sizeof dummy_class);
1248 class->type = xstrdup(type);
1249 error = netdev_register_provider(class);
1251 VLOG_ERR("%s: failed to register netdev provider (%s)",
1252 type, ovs_strerror(error));
1253 free(CONST_CAST(char *, class->type));
1258 sset_destroy(&types);
1260 netdev_register_provider(&dummy_class);
1262 netdev_vport_tunnel_register();