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 "dp-packet.h"
24 #include "dpif-netdev.h"
25 #include "dynamic-string.h"
28 #include "netdev-provider.h"
29 #include "netdev-vport.h"
31 #include "ofp-print.h"
34 #include "pcap-file.h"
35 #include "poll-loop.h"
39 #include "unaligned.h"
42 #include "reconnect.h"
43 #include "openvswitch/vlog.h"
45 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
49 struct dummy_packet_stream {
50 struct stream *stream;
51 struct dp_packet rxbuf;
55 enum dummy_packet_conn_type {
56 NONE, /* No connection is configured. */
57 PASSIVE, /* Listener. */
58 ACTIVE /* Connect to listener. */
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. */
67 struct dummy_packet_pconn {
68 struct pstream *pstream;
69 struct dummy_packet_stream *streams;
73 struct dummy_packet_rconn {
74 struct dummy_packet_stream *rstream;
75 struct reconnect *reconnect;
78 struct dummy_packet_conn {
79 enum dummy_packet_conn_type type;
81 struct dummy_packet_pconn pconn;
82 struct dummy_packet_rconn rconn;
86 /* Protects 'dummy_list'. */
87 static struct ovs_mutex dummy_list_mutex = OVS_MUTEX_INITIALIZER;
89 /* Contains all 'struct dummy_dev's. */
90 static struct ovs_list dummy_list OVS_GUARDED_BY(dummy_list_mutex)
91 = OVS_LIST_INITIALIZER(&dummy_list);
97 struct ovs_list list_node OVS_GUARDED_BY(dummy_list_mutex);
99 /* Protects all members below. */
100 struct ovs_mutex mutex OVS_ACQ_AFTER(dummy_list_mutex);
102 uint8_t hwaddr[ETH_ADDR_LEN] OVS_GUARDED;
104 struct netdev_stats stats OVS_GUARDED;
105 enum netdev_flags flags OVS_GUARDED;
106 int ifindex OVS_GUARDED;
108 struct dummy_packet_conn conn OVS_GUARDED;
110 FILE *tx_pcap, *rxq_pcap OVS_GUARDED;
112 struct in_addr address, netmask;
113 struct ovs_list rxes OVS_GUARDED; /* List of child "netdev_rxq_dummy"s. */
116 /* Max 'recv_queue_len' in struct netdev_dummy. */
117 #define NETDEV_DUMMY_MAX_QUEUE 100
119 struct netdev_rxq_dummy {
120 struct netdev_rxq up;
121 struct ovs_list node; /* In netdev_dummy's "rxes" list. */
122 struct ovs_list recv_queue;
123 int recv_queue_len; /* list_size(&recv_queue). */
124 struct seq *seq; /* Reports newly queued packets. */
127 static unixctl_cb_func netdev_dummy_set_admin_state;
128 static int netdev_dummy_construct(struct netdev *);
129 static void netdev_dummy_queue_packet(struct netdev_dummy *, struct dp_packet *);
131 static void dummy_packet_stream_close(struct dummy_packet_stream *);
134 is_dummy_class(const struct netdev_class *class)
136 return class->construct == netdev_dummy_construct;
139 static struct netdev_dummy *
140 netdev_dummy_cast(const struct netdev *netdev)
142 ovs_assert(is_dummy_class(netdev_get_class(netdev)));
143 return CONTAINER_OF(netdev, struct netdev_dummy, up);
146 static struct netdev_rxq_dummy *
147 netdev_rxq_dummy_cast(const struct netdev_rxq *rx)
149 ovs_assert(is_dummy_class(netdev_get_class(rx->netdev)));
150 return CONTAINER_OF(rx, struct netdev_rxq_dummy, up);
154 dummy_packet_stream_init(struct dummy_packet_stream *s, struct stream *stream)
156 int rxbuf_size = stream ? 2048 : 0;
158 dp_packet_init(&s->rxbuf, rxbuf_size);
162 static struct dummy_packet_stream *
163 dummy_packet_stream_create(struct stream *stream)
165 struct dummy_packet_stream *s;
167 s = xzalloc(sizeof *s);
168 dummy_packet_stream_init(s, stream);
174 dummy_packet_stream_wait(struct dummy_packet_stream *s)
176 stream_run_wait(s->stream);
177 if (!list_is_empty(&s->txq)) {
178 stream_send_wait(s->stream);
180 stream_recv_wait(s->stream);
184 dummy_packet_stream_send(struct dummy_packet_stream *s, const void *buffer, size_t size)
186 if (list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) {
189 b = dp_packet_clone_data_with_headroom(buffer, size, 2);
190 put_unaligned_be16(dp_packet_push_uninit(b, 2), htons(size));
191 list_push_back(&s->txq, &b->list_node);
196 dummy_packet_stream_run(struct netdev_dummy *dev, struct dummy_packet_stream *s)
201 stream_run(s->stream);
203 if (!list_is_empty(&s->txq)) {
204 struct dp_packet *txbuf;
207 txbuf = dp_packet_from_list(list_front(&s->txq));
208 retval = stream_send(s->stream, dp_packet_data(txbuf), dp_packet_size(txbuf));
211 dp_packet_pull(txbuf, retval);
212 if (!dp_packet_size(txbuf)) {
213 list_remove(&txbuf->list_node);
214 dp_packet_delete(txbuf);
216 } else if (retval != -EAGAIN) {
222 if (dp_packet_size(&s->rxbuf) < 2) {
223 n = 2 - dp_packet_size(&s->rxbuf);
227 frame_len = ntohs(get_unaligned_be16(dp_packet_data(&s->rxbuf)));
228 if (frame_len < ETH_HEADER_LEN) {
232 n = (2 + frame_len) - dp_packet_size(&s->rxbuf);
239 dp_packet_prealloc_tailroom(&s->rxbuf, n);
240 retval = stream_recv(s->stream, dp_packet_tail(&s->rxbuf), n);
243 dp_packet_set_size(&s->rxbuf, dp_packet_size(&s->rxbuf) + retval);
244 if (retval == n && dp_packet_size(&s->rxbuf) > 2) {
245 dp_packet_pull(&s->rxbuf, 2);
246 netdev_dummy_queue_packet(dev,
247 dp_packet_clone(&s->rxbuf));
248 dp_packet_clear(&s->rxbuf);
250 } else if (retval != -EAGAIN) {
251 error = (retval < 0 ? -retval
252 : dp_packet_size(&s->rxbuf) ? EPROTO
261 dummy_packet_stream_close(struct dummy_packet_stream *s)
263 stream_close(s->stream);
264 dp_packet_uninit(&s->rxbuf);
265 dp_packet_list_delete(&s->txq);
269 dummy_packet_conn_init(struct dummy_packet_conn *conn)
271 memset(conn, 0, sizeof *conn);
276 dummy_packet_conn_get_config(struct dummy_packet_conn *conn, struct smap *args)
279 switch (conn->type) {
281 smap_add(args, "pstream", pstream_get_name(conn->u.pconn.pstream));
285 smap_add(args, "stream", stream_get_name(conn->u.rconn.rstream->stream));
295 dummy_packet_conn_close(struct dummy_packet_conn *conn)
298 struct dummy_packet_pconn *pconn = &conn->u.pconn;
299 struct dummy_packet_rconn *rconn = &conn->u.rconn;
301 switch (conn->type) {
303 pstream_close(pconn->pstream);
304 for (i = 0; i < pconn->n_streams; i++) {
305 dummy_packet_stream_close(&pconn->streams[i]);
307 free(pconn->streams);
308 pconn->pstream = NULL;
309 pconn->streams = NULL;
313 dummy_packet_stream_close(rconn->rstream);
314 free(rconn->rstream);
315 rconn->rstream = NULL;
316 reconnect_destroy(rconn->reconnect);
317 rconn->reconnect = NULL;
326 memset(conn, 0, sizeof *conn);
330 dummy_packet_conn_set_config(struct dummy_packet_conn *conn,
331 const struct smap *args)
333 const char *pstream = smap_get(args, "pstream");
334 const char *stream = smap_get(args, "stream");
336 if (pstream && stream) {
337 VLOG_WARN("Open failed: both %s and %s are configured",
342 switch (conn->type) {
344 if (!strcmp(pstream_get_name(conn->u.pconn.pstream), pstream)) {
347 dummy_packet_conn_close(conn);
350 if (!strcmp(stream_get_name(conn->u.rconn.rstream->stream), stream)) {
353 dummy_packet_conn_close(conn);
363 error = pstream_open(pstream, &conn->u.pconn.pstream, DSCP_DEFAULT);
365 VLOG_WARN("%s: open failed (%s)", pstream, ovs_strerror(error));
367 conn->type = PASSIVE;
373 struct stream *active_stream;
374 struct reconnect *reconnect;;
376 reconnect = reconnect_create(time_msec());
377 reconnect_set_name(reconnect, stream);
378 reconnect_set_passive(reconnect, false, time_msec());
379 reconnect_enable(reconnect, time_msec());
380 reconnect_set_backoff(reconnect, 100, INT_MAX);
381 reconnect_set_probe_interval(reconnect, 0);
382 conn->u.rconn.reconnect = reconnect;
385 error = stream_open(stream, &active_stream, DSCP_DEFAULT);
386 conn->u.rconn.rstream = dummy_packet_stream_create(active_stream);
390 reconnect_connected(reconnect, time_msec());
394 reconnect_connecting(reconnect, time_msec());
398 reconnect_connect_failed(reconnect, time_msec(), error);
399 stream_close(active_stream);
400 conn->u.rconn.rstream->stream = NULL;
407 dummy_pconn_run(struct netdev_dummy *dev)
408 OVS_REQUIRES(dev->mutex)
410 struct stream *new_stream;
411 struct dummy_packet_pconn *pconn = &dev->conn.u.pconn;
415 error = pstream_accept(pconn->pstream, &new_stream);
417 struct dummy_packet_stream *s;
419 pconn->streams = xrealloc(pconn->streams,
420 ((pconn->n_streams + 1)
422 s = &pconn->streams[pconn->n_streams++];
423 dummy_packet_stream_init(s, new_stream);
424 } else if (error != EAGAIN) {
425 VLOG_WARN("%s: accept failed (%s)",
426 pstream_get_name(pconn->pstream), ovs_strerror(error));
427 pstream_close(pconn->pstream);
428 pconn->pstream = NULL;
429 dev->conn.type = NONE;
432 for (i = 0; i < pconn->n_streams; i++) {
433 struct dummy_packet_stream *s = &pconn->streams[i];
435 error = dummy_packet_stream_run(dev, s);
437 VLOG_DBG("%s: closing connection (%s)",
438 stream_get_name(s->stream),
439 ovs_retval_to_string(error));
440 dummy_packet_stream_close(s);
441 pconn->streams[i] = pconn->streams[--pconn->n_streams];
447 dummy_rconn_run(struct netdev_dummy *dev)
448 OVS_REQUIRES(dev->mutex)
450 struct dummy_packet_rconn *rconn = &dev->conn.u.rconn;
452 switch (reconnect_run(rconn->reconnect, time_msec())) {
453 case RECONNECT_CONNECT:
457 if (rconn->rstream->stream) {
458 error = stream_connect(rconn->rstream->stream);
460 error = stream_open(reconnect_get_name(rconn->reconnect),
461 &rconn->rstream->stream, DSCP_DEFAULT);
466 reconnect_connected(rconn->reconnect, time_msec());
470 reconnect_connecting(rconn->reconnect, time_msec());
474 reconnect_connect_failed(rconn->reconnect, time_msec(), error);
475 stream_close(rconn->rstream->stream);
476 rconn->rstream->stream = NULL;
482 case RECONNECT_DISCONNECT:
483 case RECONNECT_PROBE:
488 if (reconnect_is_connected(rconn->reconnect)) {
491 err = dummy_packet_stream_run(dev, rconn->rstream);
494 reconnect_disconnected(rconn->reconnect, time_msec(), err);
495 stream_close(rconn->rstream->stream);
496 rconn->rstream->stream = NULL;
502 dummy_packet_conn_run(struct netdev_dummy *dev)
503 OVS_REQUIRES(dev->mutex)
505 switch (dev->conn.type) {
507 dummy_pconn_run(dev);
511 dummy_rconn_run(dev);
521 dummy_packet_conn_wait(struct dummy_packet_conn *conn)
524 switch (conn->type) {
526 pstream_wait(conn->u.pconn.pstream);
527 for (i = 0; i < conn->u.pconn.n_streams; i++) {
528 struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
529 dummy_packet_stream_wait(s);
533 if (reconnect_is_connected(conn->u.rconn.reconnect)) {
534 dummy_packet_stream_wait(conn->u.rconn.rstream);
545 dummy_packet_conn_send(struct dummy_packet_conn *conn,
546 const void *buffer, size_t size)
550 switch (conn->type) {
552 for (i = 0; i < conn->u.pconn.n_streams; i++) {
553 struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
555 dummy_packet_stream_send(s, buffer, size);
556 pstream_wait(conn->u.pconn.pstream);
561 if (reconnect_is_connected(conn->u.rconn.reconnect)) {
562 dummy_packet_stream_send(conn->u.rconn.rstream, buffer, size);
563 dummy_packet_stream_wait(conn->u.rconn.rstream);
573 static enum dummy_netdev_conn_state
574 dummy_netdev_get_conn_state(struct dummy_packet_conn *conn)
576 enum dummy_netdev_conn_state state;
578 if (conn->type == ACTIVE) {
579 if (reconnect_is_connected(conn->u.rconn.reconnect)) {
580 state = CONN_STATE_CONNECTED;
582 state = CONN_STATE_NOT_CONNECTED;
585 state = CONN_STATE_UNKNOWN;
592 netdev_dummy_run(void)
594 struct netdev_dummy *dev;
596 ovs_mutex_lock(&dummy_list_mutex);
597 LIST_FOR_EACH (dev, list_node, &dummy_list) {
598 ovs_mutex_lock(&dev->mutex);
599 dummy_packet_conn_run(dev);
600 ovs_mutex_unlock(&dev->mutex);
602 ovs_mutex_unlock(&dummy_list_mutex);
606 netdev_dummy_wait(void)
608 struct netdev_dummy *dev;
610 ovs_mutex_lock(&dummy_list_mutex);
611 LIST_FOR_EACH (dev, list_node, &dummy_list) {
612 ovs_mutex_lock(&dev->mutex);
613 dummy_packet_conn_wait(&dev->conn);
614 ovs_mutex_unlock(&dev->mutex);
616 ovs_mutex_unlock(&dummy_list_mutex);
619 static struct netdev *
620 netdev_dummy_alloc(void)
622 struct netdev_dummy *netdev = xzalloc(sizeof *netdev);
627 netdev_dummy_construct(struct netdev *netdev_)
629 static atomic_count next_n = ATOMIC_COUNT_INIT(0xaa550000);
630 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
633 n = atomic_count_inc(&next_n);
635 ovs_mutex_init(&netdev->mutex);
636 ovs_mutex_lock(&netdev->mutex);
637 netdev->hwaddr[0] = 0xaa;
638 netdev->hwaddr[1] = 0x55;
639 netdev->hwaddr[2] = n >> 24;
640 netdev->hwaddr[3] = n >> 16;
641 netdev->hwaddr[4] = n >> 8;
642 netdev->hwaddr[5] = n;
645 netdev->ifindex = -EOPNOTSUPP;
647 dummy_packet_conn_init(&netdev->conn);
649 list_init(&netdev->rxes);
650 ovs_mutex_unlock(&netdev->mutex);
652 ovs_mutex_lock(&dummy_list_mutex);
653 list_push_back(&dummy_list, &netdev->list_node);
654 ovs_mutex_unlock(&dummy_list_mutex);
660 netdev_dummy_destruct(struct netdev *netdev_)
662 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
664 ovs_mutex_lock(&dummy_list_mutex);
665 list_remove(&netdev->list_node);
666 ovs_mutex_unlock(&dummy_list_mutex);
668 ovs_mutex_lock(&netdev->mutex);
669 dummy_packet_conn_close(&netdev->conn);
670 netdev->conn.type = NONE;
672 ovs_mutex_unlock(&netdev->mutex);
673 ovs_mutex_destroy(&netdev->mutex);
677 netdev_dummy_dealloc(struct netdev *netdev_)
679 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
685 netdev_dummy_get_config(const struct netdev *netdev_, struct smap *args)
687 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
689 ovs_mutex_lock(&netdev->mutex);
691 if (netdev->ifindex >= 0) {
692 smap_add_format(args, "ifindex", "%d", netdev->ifindex);
695 dummy_packet_conn_get_config(&netdev->conn, args);
697 ovs_mutex_unlock(&netdev->mutex);
702 netdev_dummy_get_in4(const struct netdev *netdev_,
703 struct in_addr *address, struct in_addr *netmask)
705 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
707 ovs_mutex_lock(&netdev->mutex);
708 *address = netdev->address;
709 *netmask = netdev->netmask;
710 ovs_mutex_unlock(&netdev->mutex);
715 netdev_dummy_set_in4(struct netdev *netdev_, struct in_addr address,
716 struct in_addr netmask)
718 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
720 ovs_mutex_lock(&netdev->mutex);
721 netdev->address = address;
722 netdev->netmask = netmask;
723 ovs_mutex_unlock(&netdev->mutex);
729 netdev_dummy_set_config(struct netdev *netdev_, const struct smap *args)
731 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
734 ovs_mutex_lock(&netdev->mutex);
735 netdev->ifindex = smap_get_int(args, "ifindex", -EOPNOTSUPP);
737 dummy_packet_conn_set_config(&netdev->conn, args);
739 if (netdev->rxq_pcap) {
740 fclose(netdev->rxq_pcap);
742 if (netdev->tx_pcap && netdev->tx_pcap != netdev->rxq_pcap) {
743 fclose(netdev->tx_pcap);
745 netdev->rxq_pcap = netdev->tx_pcap = NULL;
746 pcap = smap_get(args, "pcap");
748 netdev->rxq_pcap = netdev->tx_pcap = ovs_pcap_open(pcap, "ab");
750 const char *rxq_pcap = smap_get(args, "rxq_pcap");
751 const char *tx_pcap = smap_get(args, "tx_pcap");
754 netdev->rxq_pcap = ovs_pcap_open(rxq_pcap, "ab");
757 netdev->tx_pcap = ovs_pcap_open(tx_pcap, "ab");
761 ovs_mutex_unlock(&netdev->mutex);
766 static struct netdev_rxq *
767 netdev_dummy_rxq_alloc(void)
769 struct netdev_rxq_dummy *rx = xzalloc(sizeof *rx);
774 netdev_dummy_rxq_construct(struct netdev_rxq *rxq_)
776 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
777 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
779 ovs_mutex_lock(&netdev->mutex);
780 list_push_back(&netdev->rxes, &rx->node);
781 list_init(&rx->recv_queue);
782 rx->recv_queue_len = 0;
783 rx->seq = seq_create();
784 ovs_mutex_unlock(&netdev->mutex);
790 netdev_dummy_rxq_destruct(struct netdev_rxq *rxq_)
792 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
793 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
795 ovs_mutex_lock(&netdev->mutex);
796 list_remove(&rx->node);
797 dp_packet_list_delete(&rx->recv_queue);
798 ovs_mutex_unlock(&netdev->mutex);
799 seq_destroy(rx->seq);
803 netdev_dummy_rxq_dealloc(struct netdev_rxq *rxq_)
805 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
811 netdev_dummy_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet **arr,
814 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
815 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
816 struct dp_packet *packet;
818 ovs_mutex_lock(&netdev->mutex);
819 if (!list_is_empty(&rx->recv_queue)) {
820 packet = dp_packet_from_list(list_pop_front(&rx->recv_queue));
821 rx->recv_queue_len--;
825 ovs_mutex_unlock(&netdev->mutex);
830 ovs_mutex_lock(&netdev->mutex);
831 netdev->stats.rx_packets++;
832 netdev->stats.rx_bytes += dp_packet_size(packet);
833 ovs_mutex_unlock(&netdev->mutex);
835 dp_packet_pad(packet);
836 dp_packet_set_dp_hash(packet, 0);
844 netdev_dummy_rxq_wait(struct netdev_rxq *rxq_)
846 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
847 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
848 uint64_t seq = seq_read(rx->seq);
850 ovs_mutex_lock(&netdev->mutex);
851 if (!list_is_empty(&rx->recv_queue)) {
852 poll_immediate_wake();
854 seq_wait(rx->seq, seq);
856 ovs_mutex_unlock(&netdev->mutex);
860 netdev_dummy_rxq_drain(struct netdev_rxq *rxq_)
862 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
863 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
865 ovs_mutex_lock(&netdev->mutex);
866 dp_packet_list_delete(&rx->recv_queue);
867 rx->recv_queue_len = 0;
868 ovs_mutex_unlock(&netdev->mutex);
876 netdev_dummy_send(struct netdev *netdev, int qid OVS_UNUSED,
877 struct dp_packet **pkts, int cnt, bool may_steal)
879 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
883 for (i = 0; i < cnt; i++) {
884 const void *buffer = dp_packet_data(pkts[i]);
885 size_t size = dp_packet_size(pkts[i]);
887 if (size < ETH_HEADER_LEN) {
891 const struct eth_header *eth = buffer;
894 ovs_mutex_lock(&dev->mutex);
895 max_size = dev->mtu + ETH_HEADER_LEN;
896 ovs_mutex_unlock(&dev->mutex);
898 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
899 max_size += VLAN_HEADER_LEN;
901 if (size > max_size) {
907 ovs_mutex_lock(&dev->mutex);
908 dev->stats.tx_packets++;
909 dev->stats.tx_bytes += size;
911 dummy_packet_conn_send(&dev->conn, buffer, size);
914 struct dp_packet packet;
916 dp_packet_use_const(&packet, buffer, size);
917 ovs_pcap_write(dev->tx_pcap, &packet);
918 fflush(dev->tx_pcap);
921 ovs_mutex_unlock(&dev->mutex);
925 for (i = 0; i < cnt; i++) {
926 dp_packet_delete(pkts[i]);
934 netdev_dummy_set_etheraddr(struct netdev *netdev,
935 const uint8_t mac[ETH_ADDR_LEN])
937 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
939 ovs_mutex_lock(&dev->mutex);
940 if (!eth_addr_equals(dev->hwaddr, mac)) {
941 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
942 netdev_change_seq_changed(netdev);
944 ovs_mutex_unlock(&dev->mutex);
950 netdev_dummy_get_etheraddr(const struct netdev *netdev,
951 uint8_t mac[ETH_ADDR_LEN])
953 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
955 ovs_mutex_lock(&dev->mutex);
956 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
957 ovs_mutex_unlock(&dev->mutex);
963 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
965 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
967 ovs_mutex_lock(&dev->mutex);
969 ovs_mutex_unlock(&dev->mutex);
975 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
977 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
979 ovs_mutex_lock(&dev->mutex);
981 ovs_mutex_unlock(&dev->mutex);
987 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
989 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
991 ovs_mutex_lock(&dev->mutex);
993 ovs_mutex_unlock(&dev->mutex);
999 netdev_dummy_get_ifindex(const struct netdev *netdev)
1001 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
1004 ovs_mutex_lock(&dev->mutex);
1005 ifindex = dev->ifindex;
1006 ovs_mutex_unlock(&dev->mutex);
1012 netdev_dummy_update_flags__(struct netdev_dummy *netdev,
1013 enum netdev_flags off, enum netdev_flags on,
1014 enum netdev_flags *old_flagsp)
1015 OVS_REQUIRES(netdev->mutex)
1017 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
1021 *old_flagsp = netdev->flags;
1022 netdev->flags |= on;
1023 netdev->flags &= ~off;
1024 if (*old_flagsp != netdev->flags) {
1025 netdev_change_seq_changed(&netdev->up);
1032 netdev_dummy_update_flags(struct netdev *netdev_,
1033 enum netdev_flags off, enum netdev_flags on,
1034 enum netdev_flags *old_flagsp)
1036 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
1039 ovs_mutex_lock(&netdev->mutex);
1040 error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp);
1041 ovs_mutex_unlock(&netdev->mutex);
1046 /* Helper functions. */
1048 static const struct netdev_class dummy_class = {
1055 netdev_dummy_construct,
1056 netdev_dummy_destruct,
1057 netdev_dummy_dealloc,
1058 netdev_dummy_get_config,
1059 netdev_dummy_set_config,
1060 NULL, /* get_tunnel_config */
1061 NULL, /* build header */
1062 NULL, /* push header */
1063 NULL, /* pop header */
1064 NULL, /* get_numa_id */
1065 NULL, /* set_multiq */
1067 netdev_dummy_send, /* send */
1068 NULL, /* send_wait */
1070 netdev_dummy_set_etheraddr,
1071 netdev_dummy_get_etheraddr,
1072 netdev_dummy_get_mtu,
1073 netdev_dummy_set_mtu,
1074 netdev_dummy_get_ifindex,
1075 NULL, /* get_carrier */
1076 NULL, /* get_carrier_resets */
1077 NULL, /* get_miimon */
1078 netdev_dummy_get_stats,
1080 NULL, /* get_features */
1081 NULL, /* set_advertisements */
1083 NULL, /* set_policing */
1084 NULL, /* get_qos_types */
1085 NULL, /* get_qos_capabilities */
1088 NULL, /* get_queue */
1089 NULL, /* set_queue */
1090 NULL, /* delete_queue */
1091 NULL, /* get_queue_stats */
1092 NULL, /* queue_dump_start */
1093 NULL, /* queue_dump_next */
1094 NULL, /* queue_dump_done */
1095 NULL, /* dump_queue_stats */
1097 netdev_dummy_get_in4, /* get_in4 */
1100 NULL, /* add_router */
1101 NULL, /* get_next_hop */
1102 NULL, /* get_status */
1103 NULL, /* arp_lookup */
1105 netdev_dummy_update_flags,
1107 netdev_dummy_rxq_alloc,
1108 netdev_dummy_rxq_construct,
1109 netdev_dummy_rxq_destruct,
1110 netdev_dummy_rxq_dealloc,
1111 netdev_dummy_rxq_recv,
1112 netdev_dummy_rxq_wait,
1113 netdev_dummy_rxq_drain,
1116 static struct dp_packet *
1117 eth_from_packet_or_flow(const char *s)
1119 enum odp_key_fitness fitness;
1120 struct dp_packet *packet;
1121 struct ofpbuf odp_key;
1125 if (!eth_from_hex(s, &packet)) {
1129 /* Convert string to datapath key.
1131 * It would actually be nicer to parse an OpenFlow-like flow key here, but
1132 * the code for that currently calls exit() on parse error. We have to
1133 * settle for parsing a datapath key for now.
1135 ofpbuf_init(&odp_key, 0);
1136 error = odp_flow_from_string(s, NULL, &odp_key, NULL);
1138 ofpbuf_uninit(&odp_key);
1142 /* Convert odp_key to flow. */
1143 fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
1144 if (fitness == ODP_FIT_ERROR) {
1145 ofpbuf_uninit(&odp_key);
1149 packet = dp_packet_new(0);
1150 flow_compose(packet, &flow);
1152 ofpbuf_uninit(&odp_key);
1157 netdev_dummy_queue_packet__(struct netdev_rxq_dummy *rx, struct dp_packet *packet)
1159 list_push_back(&rx->recv_queue, &packet->list_node);
1160 rx->recv_queue_len++;
1161 seq_change(rx->seq);
1165 netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct dp_packet *packet)
1166 OVS_REQUIRES(dummy->mutex)
1168 struct netdev_rxq_dummy *rx, *prev;
1170 if (dummy->rxq_pcap) {
1171 ovs_pcap_write(dummy->rxq_pcap, packet);
1172 fflush(dummy->rxq_pcap);
1175 LIST_FOR_EACH (rx, node, &dummy->rxes) {
1176 if (rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
1178 netdev_dummy_queue_packet__(prev, dp_packet_clone(packet));
1184 netdev_dummy_queue_packet__(prev, packet);
1186 dp_packet_delete(packet);
1191 netdev_dummy_receive(struct unixctl_conn *conn,
1192 int argc, const char *argv[], void *aux OVS_UNUSED)
1194 struct netdev_dummy *dummy_dev;
1195 struct netdev *netdev;
1198 netdev = netdev_from_name(argv[1]);
1199 if (!netdev || !is_dummy_class(netdev->netdev_class)) {
1200 unixctl_command_reply_error(conn, "no such dummy netdev");
1203 dummy_dev = netdev_dummy_cast(netdev);
1205 for (i = 2; i < argc; i++) {
1206 struct dp_packet *packet;
1208 packet = eth_from_packet_or_flow(argv[i]);
1210 unixctl_command_reply_error(conn, "bad packet syntax");
1214 ovs_mutex_lock(&dummy_dev->mutex);
1215 netdev_dummy_queue_packet(dummy_dev, packet);
1216 ovs_mutex_unlock(&dummy_dev->mutex);
1219 unixctl_command_reply(conn, NULL);
1222 netdev_close(netdev);
1226 netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state)
1227 OVS_REQUIRES(dev->mutex)
1229 enum netdev_flags old_flags;
1232 netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1234 netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1239 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
1240 const char *argv[], void *aux OVS_UNUSED)
1244 if (!strcasecmp(argv[argc - 1], "up")) {
1246 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1249 unixctl_command_reply_error(conn, "Invalid Admin State");
1254 struct netdev *netdev = netdev_from_name(argv[1]);
1255 if (netdev && is_dummy_class(netdev->netdev_class)) {
1256 struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1258 ovs_mutex_lock(&dummy_dev->mutex);
1259 netdev_dummy_set_admin_state__(dummy_dev, up);
1260 ovs_mutex_unlock(&dummy_dev->mutex);
1262 netdev_close(netdev);
1264 unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1265 netdev_close(netdev);
1269 struct netdev_dummy *netdev;
1271 ovs_mutex_lock(&dummy_list_mutex);
1272 LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1273 ovs_mutex_lock(&netdev->mutex);
1274 netdev_dummy_set_admin_state__(netdev, up);
1275 ovs_mutex_unlock(&netdev->mutex);
1277 ovs_mutex_unlock(&dummy_list_mutex);
1279 unixctl_command_reply(conn, "OK");
1283 display_conn_state__(struct ds *s, const char *name,
1284 enum dummy_netdev_conn_state state)
1286 ds_put_format(s, "%s: ", name);
1289 case CONN_STATE_CONNECTED:
1290 ds_put_cstr(s, "connected\n");
1293 case CONN_STATE_NOT_CONNECTED:
1294 ds_put_cstr(s, "disconnected\n");
1297 case CONN_STATE_UNKNOWN:
1299 ds_put_cstr(s, "unknown\n");
1305 netdev_dummy_conn_state(struct unixctl_conn *conn, int argc,
1306 const char *argv[], void *aux OVS_UNUSED)
1308 enum dummy_netdev_conn_state state = CONN_STATE_UNKNOWN;
1314 const char *dev_name = argv[1];
1315 struct netdev *netdev = netdev_from_name(dev_name);
1317 if (netdev && is_dummy_class(netdev->netdev_class)) {
1318 struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1320 ovs_mutex_lock(&dummy_dev->mutex);
1321 state = dummy_netdev_get_conn_state(&dummy_dev->conn);
1322 ovs_mutex_unlock(&dummy_dev->mutex);
1324 netdev_close(netdev);
1326 display_conn_state__(&s, dev_name, state);
1328 struct netdev_dummy *netdev;
1330 ovs_mutex_lock(&dummy_list_mutex);
1331 LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1332 ovs_mutex_lock(&netdev->mutex);
1333 state = dummy_netdev_get_conn_state(&netdev->conn);
1334 ovs_mutex_unlock(&netdev->mutex);
1335 if (state != CONN_STATE_UNKNOWN) {
1336 display_conn_state__(&s, netdev->up.name, state);
1339 ovs_mutex_unlock(&dummy_list_mutex);
1342 unixctl_command_reply(conn, ds_cstr(&s));
1347 netdev_dummy_ip4addr(struct unixctl_conn *conn, int argc OVS_UNUSED,
1348 const char *argv[], void *aux OVS_UNUSED)
1350 struct netdev *netdev = netdev_from_name(argv[1]);
1352 if (netdev && is_dummy_class(netdev->netdev_class)) {
1356 if (ovs_scan(argv[2], IP_SCAN_FMT"/%"SCNi16,
1357 IP_SCAN_ARGS(&ip.s_addr), &plen)) {
1358 struct in_addr mask;
1360 mask.s_addr = be32_prefix_mask(plen);
1361 netdev_dummy_set_in4(netdev, ip, mask);
1362 unixctl_command_reply(conn, "OK");
1364 unixctl_command_reply(conn, "Invalid parameters");
1367 netdev_close(netdev);
1369 unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1370 netdev_close(netdev);
1377 netdev_dummy_register(bool override)
1379 unixctl_command_register("netdev-dummy/receive", "name packet|flow...",
1380 2, INT_MAX, netdev_dummy_receive, NULL);
1381 unixctl_command_register("netdev-dummy/set-admin-state",
1382 "[netdev] up|down", 1, 2,
1383 netdev_dummy_set_admin_state, NULL);
1384 unixctl_command_register("netdev-dummy/conn-state",
1386 netdev_dummy_conn_state, NULL);
1387 unixctl_command_register("netdev-dummy/ip4addr",
1388 "[netdev] ipaddr/mask-prefix-len", 2, 2,
1389 netdev_dummy_ip4addr, NULL);
1397 netdev_enumerate_types(&types);
1398 SSET_FOR_EACH (type, &types) {
1399 if (!strcmp(type, "patch")) {
1402 if (!netdev_unregister_provider(type)) {
1403 struct netdev_class *class;
1406 class = xmemdup(&dummy_class, sizeof dummy_class);
1407 class->type = xstrdup(type);
1408 error = netdev_register_provider(class);
1410 VLOG_ERR("%s: failed to register netdev provider (%s)",
1411 type, ovs_strerror(error));
1412 free(CONST_CAST(char *, class->type));
1417 sset_destroy(&types);
1419 netdev_register_provider(&dummy_class);
1421 netdev_vport_tunnel_register();