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 "dpif-netdev.h"
26 #include "netdev-provider.h"
27 #include "netdev-vport.h"
29 #include "ofp-print.h"
32 #include "pcap-file.h"
33 #include "poll-loop.h"
37 #include "unaligned.h"
40 #include "reconnect.h"
43 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
47 struct dummy_packet_stream {
48 struct stream *stream;
53 enum dummy_packet_conn_type {
54 NONE, /* No connection is configured. */
55 PASSIVE, /* Listener. */
56 ACTIVE /* Connect to listener. */
59 struct dummy_packet_pconn {
60 struct pstream *pstream;
61 struct dummy_packet_stream *streams;
65 struct dummy_packet_rconn {
66 struct dummy_packet_stream *rstream;
67 struct reconnect *reconnect;
70 struct dummy_packet_conn {
71 enum dummy_packet_conn_type type;
73 struct dummy_packet_pconn pconn;
74 struct dummy_packet_rconn rconn;
78 /* Protects 'dummy_list'. */
79 static struct ovs_mutex dummy_list_mutex = OVS_MUTEX_INITIALIZER;
81 /* Contains all 'struct dummy_dev's. */
82 static struct list dummy_list OVS_GUARDED_BY(dummy_list_mutex)
83 = LIST_INITIALIZER(&dummy_list);
89 struct list list_node OVS_GUARDED_BY(dummy_list_mutex);
91 /* Protects all members below. */
92 struct ovs_mutex mutex OVS_ACQ_AFTER(dummy_list_mutex);
94 uint8_t hwaddr[ETH_ADDR_LEN] OVS_GUARDED;
96 struct netdev_stats stats OVS_GUARDED;
97 enum netdev_flags flags OVS_GUARDED;
98 int ifindex OVS_GUARDED;
100 struct dummy_packet_conn conn OVS_GUARDED;
102 FILE *tx_pcap, *rxq_pcap OVS_GUARDED;
104 struct list rxes OVS_GUARDED; /* List of child "netdev_rxq_dummy"s. */
107 /* Max 'recv_queue_len' in struct netdev_dummy. */
108 #define NETDEV_DUMMY_MAX_QUEUE 100
110 struct netdev_rxq_dummy {
111 struct netdev_rxq up;
112 struct list node; /* In netdev_dummy's "rxes" list. */
113 struct list recv_queue;
114 int recv_queue_len; /* list_size(&recv_queue). */
115 struct seq *seq; /* Reports newly queued packets. */
118 static unixctl_cb_func netdev_dummy_set_admin_state;
119 static int netdev_dummy_construct(struct netdev *);
120 static void netdev_dummy_queue_packet(struct netdev_dummy *, struct ofpbuf *);
122 static void dummy_packet_stream_close(struct dummy_packet_stream *);
125 is_dummy_class(const struct netdev_class *class)
127 return class->construct == netdev_dummy_construct;
130 static struct netdev_dummy *
131 netdev_dummy_cast(const struct netdev *netdev)
133 ovs_assert(is_dummy_class(netdev_get_class(netdev)));
134 return CONTAINER_OF(netdev, struct netdev_dummy, up);
137 static struct netdev_rxq_dummy *
138 netdev_rxq_dummy_cast(const struct netdev_rxq *rx)
140 ovs_assert(is_dummy_class(netdev_get_class(rx->netdev)));
141 return CONTAINER_OF(rx, struct netdev_rxq_dummy, up);
145 dummy_packet_stream_init(struct dummy_packet_stream *s, struct stream *stream)
147 int rxbuf_size = stream ? 2048 : 0;
149 ofpbuf_init(&s->rxbuf, rxbuf_size);
153 static struct dummy_packet_stream *
154 dummy_packet_stream_create(struct stream *stream)
156 struct dummy_packet_stream *s;
158 s = xzalloc(sizeof *s);
159 dummy_packet_stream_init(s, stream);
165 dummy_packet_stream_wait(struct dummy_packet_stream *s)
167 stream_run_wait(s->stream);
168 if (!list_is_empty(&s->txq)) {
169 stream_send_wait(s->stream);
171 stream_recv_wait(s->stream);
175 dummy_packet_stream_send(struct dummy_packet_stream *s, const void *buffer, size_t size)
177 if (list_size(&s->txq) < NETDEV_DUMMY_MAX_QUEUE) {
180 b = ofpbuf_clone_data_with_headroom(buffer, size, 2);
181 put_unaligned_be16(ofpbuf_push_uninit(b, 2), htons(size));
182 list_push_back(&s->txq, &b->list_node);
187 dummy_packet_stream_run(struct netdev_dummy *dev, struct dummy_packet_stream *s)
192 stream_run(s->stream);
194 if (!list_is_empty(&s->txq)) {
195 struct ofpbuf *txbuf;
198 txbuf = ofpbuf_from_list(list_front(&s->txq));
199 retval = stream_send(s->stream, ofpbuf_data(txbuf), ofpbuf_size(txbuf));
202 ofpbuf_pull(txbuf, retval);
203 if (!ofpbuf_size(txbuf)) {
204 list_remove(&txbuf->list_node);
205 ofpbuf_delete(txbuf);
207 } else if (retval != -EAGAIN) {
213 if (ofpbuf_size(&s->rxbuf) < 2) {
214 n = 2 - ofpbuf_size(&s->rxbuf);
218 frame_len = ntohs(get_unaligned_be16(ofpbuf_data(&s->rxbuf)));
219 if (frame_len < ETH_HEADER_LEN) {
223 n = (2 + frame_len) - ofpbuf_size(&s->rxbuf);
230 ofpbuf_prealloc_tailroom(&s->rxbuf, n);
231 retval = stream_recv(s->stream, ofpbuf_tail(&s->rxbuf), n);
234 ofpbuf_set_size(&s->rxbuf, ofpbuf_size(&s->rxbuf) + retval);
235 if (retval == n && ofpbuf_size(&s->rxbuf) > 2) {
236 ofpbuf_pull(&s->rxbuf, 2);
237 netdev_dummy_queue_packet(dev,
238 ofpbuf_clone(&s->rxbuf));
239 ofpbuf_clear(&s->rxbuf);
241 } else if (retval != -EAGAIN) {
242 error = (retval < 0 ? -retval
243 : ofpbuf_size(&s->rxbuf) ? EPROTO
252 dummy_packet_stream_close(struct dummy_packet_stream *s)
254 stream_close(s->stream);
255 ofpbuf_uninit(&s->rxbuf);
256 ofpbuf_list_delete(&s->txq);
260 dummy_packet_conn_init(struct dummy_packet_conn *conn)
262 memset(conn, 0, sizeof *conn);
267 dummy_packet_conn_get_config(struct dummy_packet_conn *conn, struct smap *args)
270 switch (conn->type) {
272 smap_add(args, "pstream", pstream_get_name(conn->u.pconn.pstream));
276 smap_add(args, "stream", stream_get_name(conn->u.rconn.rstream->stream));
286 dummy_packet_conn_close(struct dummy_packet_conn *conn)
289 struct dummy_packet_pconn *pconn = &conn->u.pconn;
290 struct dummy_packet_rconn *rconn = &conn->u.rconn;
292 switch (conn->type) {
294 pstream_close(pconn->pstream);
295 for (i = 0; i < pconn->n_streams; i++) {
296 dummy_packet_stream_close(&pconn->streams[i]);
298 free(pconn->streams);
299 pconn->pstream = NULL;
300 pconn->streams = NULL;
304 dummy_packet_stream_close(rconn->rstream);
305 free(rconn->rstream);
306 rconn->rstream = NULL;
307 reconnect_destroy(rconn->reconnect);
308 rconn->reconnect = NULL;
317 memset(conn, 0, sizeof *conn);
321 dummy_packet_conn_set_config(struct dummy_packet_conn *conn,
322 const struct smap *args)
324 const char *pstream = smap_get(args, "pstream");
325 const char *stream = smap_get(args, "stream");
327 if (pstream && stream) {
328 VLOG_WARN("Open failed: both %s and %s are configured",
333 switch (conn->type) {
335 if (!strcmp(pstream_get_name(conn->u.pconn.pstream), pstream)) {
338 dummy_packet_conn_close(conn);
341 if (!strcmp(stream_get_name(conn->u.rconn.rstream->stream), stream)) {
344 dummy_packet_conn_close(conn);
354 error = pstream_open(pstream, &conn->u.pconn.pstream, DSCP_DEFAULT);
356 VLOG_WARN("%s: open failed (%s)", pstream, ovs_strerror(error));
358 conn->type = PASSIVE;
364 struct stream *active_stream;
365 struct reconnect *reconnect;;
367 reconnect = reconnect_create(time_msec());
368 reconnect_set_name(reconnect, stream);
369 reconnect_set_passive(reconnect, false, time_msec());
370 reconnect_enable(reconnect, time_msec());
371 reconnect_set_backoff(reconnect, 100, INT_MAX);
372 reconnect_set_probe_interval(reconnect, 0);
373 conn->u.rconn.reconnect = reconnect;
376 error = stream_open(stream, &active_stream, DSCP_DEFAULT);
377 conn->u.rconn.rstream = dummy_packet_stream_create(active_stream);
381 reconnect_connected(reconnect, time_msec());
385 reconnect_connecting(reconnect, time_msec());
389 reconnect_connect_failed(reconnect, time_msec(), error);
390 stream_close(active_stream);
391 conn->u.rconn.rstream->stream = NULL;
398 dummy_pconn_run(struct netdev_dummy *dev)
399 OVS_REQUIRES(dev->mutex)
401 struct stream *new_stream;
402 struct dummy_packet_pconn *pconn = &dev->conn.u.pconn;
406 error = pstream_accept(pconn->pstream, &new_stream);
408 struct dummy_packet_stream *s;
410 pconn->streams = xrealloc(pconn->streams,
411 ((pconn->n_streams + 1)
413 s = &pconn->streams[pconn->n_streams++];
414 dummy_packet_stream_init(s, new_stream);
415 } else if (error != EAGAIN) {
416 VLOG_WARN("%s: accept failed (%s)",
417 pstream_get_name(pconn->pstream), ovs_strerror(error));
418 pstream_close(pconn->pstream);
419 pconn->pstream = NULL;
420 dev->conn.type = NONE;
423 for (i = 0; i < pconn->n_streams; i++) {
424 struct dummy_packet_stream *s = &pconn->streams[i];
426 error = dummy_packet_stream_run(dev, s);
428 VLOG_DBG("%s: closing connection (%s)",
429 stream_get_name(s->stream),
430 ovs_retval_to_string(error));
431 dummy_packet_stream_close(s);
432 pconn->streams[i] = pconn->streams[--pconn->n_streams];
438 dummy_rconn_run(struct netdev_dummy *dev)
439 OVS_REQUIRES(dev->mutex)
441 struct dummy_packet_rconn *rconn = &dev->conn.u.rconn;
443 switch (reconnect_run(rconn->reconnect, time_msec())) {
444 case RECONNECT_CONNECT:
448 if (rconn->rstream->stream) {
449 error = stream_connect(rconn->rstream->stream);
451 error = stream_open(reconnect_get_name(rconn->reconnect),
452 &rconn->rstream->stream, DSCP_DEFAULT);
457 reconnect_connected(rconn->reconnect, time_msec());
461 reconnect_connecting(rconn->reconnect, time_msec());
465 reconnect_connect_failed(rconn->reconnect, time_msec(), error);
466 stream_close(rconn->rstream->stream);
467 rconn->rstream->stream = NULL;
473 case RECONNECT_DISCONNECT:
474 case RECONNECT_PROBE:
479 if (reconnect_is_connected(rconn->reconnect)) {
482 err = dummy_packet_stream_run(dev, rconn->rstream);
485 reconnect_disconnected(rconn->reconnect, time_msec(), err);
486 stream_close(rconn->rstream->stream);
487 rconn->rstream->stream = NULL;
493 dummy_packet_conn_run(struct netdev_dummy *dev)
494 OVS_REQUIRES(dev->mutex)
496 switch (dev->conn.type) {
498 dummy_pconn_run(dev);
502 dummy_rconn_run(dev);
512 dummy_packet_conn_wait(struct dummy_packet_conn *conn)
515 switch (conn->type) {
517 pstream_wait(conn->u.pconn.pstream);
518 for (i = 0; i < conn->u.pconn.n_streams; i++) {
519 struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
520 dummy_packet_stream_wait(s);
524 if (reconnect_is_connected(conn->u.rconn.reconnect)) {
525 dummy_packet_stream_wait(conn->u.rconn.rstream);
536 dummy_packet_conn_send(struct dummy_packet_conn *conn,
537 const void *buffer, size_t size)
541 switch (conn->type) {
543 for (i = 0; i < conn->u.pconn.n_streams; i++) {
544 struct dummy_packet_stream *s = &conn->u.pconn.streams[i];
546 dummy_packet_stream_send(s, buffer, size);
547 pstream_wait(conn->u.pconn.pstream);
552 if (reconnect_is_connected(conn->u.rconn.reconnect)) {
553 dummy_packet_stream_send(conn->u.rconn.rstream, buffer, size);
554 dummy_packet_stream_wait(conn->u.rconn.rstream);
565 netdev_dummy_run(void)
567 struct netdev_dummy *dev;
569 ovs_mutex_lock(&dummy_list_mutex);
570 LIST_FOR_EACH (dev, list_node, &dummy_list) {
571 ovs_mutex_lock(&dev->mutex);
572 dummy_packet_conn_run(dev);
573 ovs_mutex_unlock(&dev->mutex);
575 ovs_mutex_unlock(&dummy_list_mutex);
579 netdev_dummy_wait(void)
581 struct netdev_dummy *dev;
583 ovs_mutex_lock(&dummy_list_mutex);
584 LIST_FOR_EACH (dev, list_node, &dummy_list) {
585 ovs_mutex_lock(&dev->mutex);
586 dummy_packet_conn_wait(&dev->conn);
587 ovs_mutex_unlock(&dev->mutex);
589 ovs_mutex_unlock(&dummy_list_mutex);
592 static struct netdev *
593 netdev_dummy_alloc(void)
595 struct netdev_dummy *netdev = xzalloc(sizeof *netdev);
600 netdev_dummy_construct(struct netdev *netdev_)
602 static atomic_uint next_n = ATOMIC_VAR_INIT(0xaa550000);
603 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
606 atomic_add(&next_n, 1, &n);
608 ovs_mutex_init(&netdev->mutex);
609 ovs_mutex_lock(&netdev->mutex);
610 netdev->hwaddr[0] = 0xaa;
611 netdev->hwaddr[1] = 0x55;
612 netdev->hwaddr[2] = n >> 24;
613 netdev->hwaddr[3] = n >> 16;
614 netdev->hwaddr[4] = n >> 8;
615 netdev->hwaddr[5] = n;
618 netdev->ifindex = -EOPNOTSUPP;
620 dummy_packet_conn_init(&netdev->conn);
622 list_init(&netdev->rxes);
623 ovs_mutex_unlock(&netdev->mutex);
625 ovs_mutex_lock(&dummy_list_mutex);
626 list_push_back(&dummy_list, &netdev->list_node);
627 ovs_mutex_unlock(&dummy_list_mutex);
633 netdev_dummy_destruct(struct netdev *netdev_)
635 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
637 ovs_mutex_lock(&dummy_list_mutex);
638 list_remove(&netdev->list_node);
639 ovs_mutex_unlock(&dummy_list_mutex);
641 ovs_mutex_lock(&netdev->mutex);
642 dummy_packet_conn_close(&netdev->conn);
643 netdev->conn.type = NONE;
645 ovs_mutex_unlock(&netdev->mutex);
646 ovs_mutex_destroy(&netdev->mutex);
650 netdev_dummy_dealloc(struct netdev *netdev_)
652 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
658 netdev_dummy_get_config(const struct netdev *netdev_, struct smap *args)
660 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
662 ovs_mutex_lock(&netdev->mutex);
664 if (netdev->ifindex >= 0) {
665 smap_add_format(args, "ifindex", "%d", netdev->ifindex);
668 dummy_packet_conn_get_config(&netdev->conn, args);
670 ovs_mutex_unlock(&netdev->mutex);
675 netdev_dummy_set_config(struct netdev *netdev_, const struct smap *args)
677 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
680 ovs_mutex_lock(&netdev->mutex);
681 netdev->ifindex = smap_get_int(args, "ifindex", -EOPNOTSUPP);
683 dummy_packet_conn_set_config(&netdev->conn, args);
685 if (netdev->rxq_pcap) {
686 fclose(netdev->rxq_pcap);
688 if (netdev->tx_pcap && netdev->tx_pcap != netdev->rxq_pcap) {
689 fclose(netdev->tx_pcap);
691 netdev->rxq_pcap = netdev->tx_pcap = NULL;
692 pcap = smap_get(args, "pcap");
694 netdev->rxq_pcap = netdev->tx_pcap = ovs_pcap_open(pcap, "ab");
696 const char *rxq_pcap = smap_get(args, "rxq_pcap");
697 const char *tx_pcap = smap_get(args, "tx_pcap");
700 netdev->rxq_pcap = ovs_pcap_open(rxq_pcap, "ab");
703 netdev->tx_pcap = ovs_pcap_open(tx_pcap, "ab");
707 ovs_mutex_unlock(&netdev->mutex);
712 static struct netdev_rxq *
713 netdev_dummy_rxq_alloc(void)
715 struct netdev_rxq_dummy *rx = xzalloc(sizeof *rx);
720 netdev_dummy_rxq_construct(struct netdev_rxq *rxq_)
722 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
723 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
725 ovs_mutex_lock(&netdev->mutex);
726 list_push_back(&netdev->rxes, &rx->node);
727 list_init(&rx->recv_queue);
728 rx->recv_queue_len = 0;
729 rx->seq = seq_create();
730 ovs_mutex_unlock(&netdev->mutex);
736 netdev_dummy_rxq_destruct(struct netdev_rxq *rxq_)
738 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
739 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
741 ovs_mutex_lock(&netdev->mutex);
742 list_remove(&rx->node);
743 ofpbuf_list_delete(&rx->recv_queue);
744 ovs_mutex_unlock(&netdev->mutex);
745 seq_destroy(rx->seq);
749 netdev_dummy_rxq_dealloc(struct netdev_rxq *rxq_)
751 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
757 netdev_dummy_rxq_recv(struct netdev_rxq *rxq_, struct ofpbuf **arr, int *c)
759 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
760 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
761 struct ofpbuf *packet;
763 ovs_mutex_lock(&netdev->mutex);
764 if (!list_is_empty(&rx->recv_queue)) {
765 packet = ofpbuf_from_list(list_pop_front(&rx->recv_queue));
766 rx->recv_queue_len--;
770 ovs_mutex_unlock(&netdev->mutex);
775 ovs_mutex_lock(&netdev->mutex);
776 netdev->stats.rx_packets++;
777 netdev->stats.rx_bytes += ofpbuf_size(packet);
778 ovs_mutex_unlock(&netdev->mutex);
780 dp_packet_pad(packet);
787 netdev_dummy_rxq_wait(struct netdev_rxq *rxq_)
789 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
790 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
791 uint64_t seq = seq_read(rx->seq);
793 ovs_mutex_lock(&netdev->mutex);
794 if (!list_is_empty(&rx->recv_queue)) {
795 poll_immediate_wake();
797 seq_wait(rx->seq, seq);
799 ovs_mutex_unlock(&netdev->mutex);
803 netdev_dummy_rxq_drain(struct netdev_rxq *rxq_)
805 struct netdev_rxq_dummy *rx = netdev_rxq_dummy_cast(rxq_);
806 struct netdev_dummy *netdev = netdev_dummy_cast(rx->up.netdev);
808 ovs_mutex_lock(&netdev->mutex);
809 ofpbuf_list_delete(&rx->recv_queue);
810 rx->recv_queue_len = 0;
811 ovs_mutex_unlock(&netdev->mutex);
819 netdev_dummy_send(struct netdev *netdev, struct ofpbuf *pkt, bool may_steal)
821 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
822 const void *buffer = ofpbuf_data(pkt);
823 size_t size = ofpbuf_size(pkt);
825 if (size < ETH_HEADER_LEN) {
828 const struct eth_header *eth = buffer;
831 ovs_mutex_lock(&dev->mutex);
832 max_size = dev->mtu + ETH_HEADER_LEN;
833 ovs_mutex_unlock(&dev->mutex);
835 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
836 max_size += VLAN_HEADER_LEN;
838 if (size > max_size) {
843 ovs_mutex_lock(&dev->mutex);
844 dev->stats.tx_packets++;
845 dev->stats.tx_bytes += size;
847 dummy_packet_conn_send(&dev->conn, buffer, size);
850 struct ofpbuf packet;
852 ofpbuf_use_const(&packet, buffer, size);
853 ovs_pcap_write(dev->tx_pcap, &packet);
854 fflush(dev->tx_pcap);
857 ovs_mutex_unlock(&dev->mutex);
866 netdev_dummy_set_etheraddr(struct netdev *netdev,
867 const uint8_t mac[ETH_ADDR_LEN])
869 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
871 ovs_mutex_lock(&dev->mutex);
872 if (!eth_addr_equals(dev->hwaddr, mac)) {
873 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
874 netdev_change_seq_changed(netdev);
876 ovs_mutex_unlock(&dev->mutex);
882 netdev_dummy_get_etheraddr(const struct netdev *netdev,
883 uint8_t mac[ETH_ADDR_LEN])
885 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
887 ovs_mutex_lock(&dev->mutex);
888 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
889 ovs_mutex_unlock(&dev->mutex);
895 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
897 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
899 ovs_mutex_lock(&dev->mutex);
901 ovs_mutex_unlock(&dev->mutex);
907 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
909 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
911 ovs_mutex_lock(&dev->mutex);
913 ovs_mutex_unlock(&dev->mutex);
919 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
921 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
923 ovs_mutex_lock(&dev->mutex);
925 ovs_mutex_unlock(&dev->mutex);
931 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
933 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
935 ovs_mutex_lock(&dev->mutex);
937 ovs_mutex_unlock(&dev->mutex);
943 netdev_dummy_get_ifindex(const struct netdev *netdev)
945 struct netdev_dummy *dev = netdev_dummy_cast(netdev);
948 ovs_mutex_lock(&dev->mutex);
949 ifindex = dev->ifindex;
950 ovs_mutex_unlock(&dev->mutex);
956 netdev_dummy_update_flags__(struct netdev_dummy *netdev,
957 enum netdev_flags off, enum netdev_flags on,
958 enum netdev_flags *old_flagsp)
959 OVS_REQUIRES(netdev->mutex)
961 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
965 *old_flagsp = netdev->flags;
967 netdev->flags &= ~off;
968 if (*old_flagsp != netdev->flags) {
969 netdev_change_seq_changed(&netdev->up);
976 netdev_dummy_update_flags(struct netdev *netdev_,
977 enum netdev_flags off, enum netdev_flags on,
978 enum netdev_flags *old_flagsp)
980 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
983 ovs_mutex_lock(&netdev->mutex);
984 error = netdev_dummy_update_flags__(netdev, off, on, old_flagsp);
985 ovs_mutex_unlock(&netdev->mutex);
990 /* Helper functions. */
992 static const struct netdev_class dummy_class = {
999 netdev_dummy_construct,
1000 netdev_dummy_destruct,
1001 netdev_dummy_dealloc,
1002 netdev_dummy_get_config,
1003 netdev_dummy_set_config,
1004 NULL, /* get_tunnel_config */
1006 netdev_dummy_send, /* send */
1007 NULL, /* send_wait */
1009 netdev_dummy_set_etheraddr,
1010 netdev_dummy_get_etheraddr,
1011 netdev_dummy_get_mtu,
1012 netdev_dummy_set_mtu,
1013 netdev_dummy_get_ifindex,
1014 NULL, /* get_carrier */
1015 NULL, /* get_carrier_resets */
1016 NULL, /* get_miimon */
1017 netdev_dummy_get_stats,
1018 netdev_dummy_set_stats,
1020 NULL, /* get_features */
1021 NULL, /* set_advertisements */
1023 NULL, /* set_policing */
1024 NULL, /* get_qos_types */
1025 NULL, /* get_qos_capabilities */
1028 NULL, /* get_queue */
1029 NULL, /* set_queue */
1030 NULL, /* delete_queue */
1031 NULL, /* get_queue_stats */
1032 NULL, /* queue_dump_start */
1033 NULL, /* queue_dump_next */
1034 NULL, /* queue_dump_done */
1035 NULL, /* dump_queue_stats */
1040 NULL, /* add_router */
1041 NULL, /* get_next_hop */
1042 NULL, /* get_status */
1043 NULL, /* arp_lookup */
1045 netdev_dummy_update_flags,
1047 netdev_dummy_rxq_alloc,
1048 netdev_dummy_rxq_construct,
1049 netdev_dummy_rxq_destruct,
1050 netdev_dummy_rxq_dealloc,
1051 netdev_dummy_rxq_recv,
1052 netdev_dummy_rxq_wait,
1053 netdev_dummy_rxq_drain,
1056 static struct ofpbuf *
1057 eth_from_packet_or_flow(const char *s)
1059 enum odp_key_fitness fitness;
1060 struct ofpbuf *packet;
1061 struct ofpbuf odp_key;
1065 if (!eth_from_hex(s, &packet)) {
1069 /* Convert string to datapath key.
1071 * It would actually be nicer to parse an OpenFlow-like flow key here, but
1072 * the code for that currently calls exit() on parse error. We have to
1073 * settle for parsing a datapath key for now.
1075 ofpbuf_init(&odp_key, 0);
1076 error = odp_flow_from_string(s, NULL, &odp_key, NULL);
1078 ofpbuf_uninit(&odp_key);
1082 /* Convert odp_key to flow. */
1083 fitness = odp_flow_key_to_flow(ofpbuf_data(&odp_key), ofpbuf_size(&odp_key), &flow);
1084 if (fitness == ODP_FIT_ERROR) {
1085 ofpbuf_uninit(&odp_key);
1089 packet = ofpbuf_new(0);
1090 flow_compose(packet, &flow);
1092 ofpbuf_uninit(&odp_key);
1097 netdev_dummy_queue_packet__(struct netdev_rxq_dummy *rx, struct ofpbuf *packet)
1099 list_push_back(&rx->recv_queue, &packet->list_node);
1100 rx->recv_queue_len++;
1101 seq_change(rx->seq);
1105 netdev_dummy_queue_packet(struct netdev_dummy *dummy, struct ofpbuf *packet)
1106 OVS_REQUIRES(dummy->mutex)
1108 struct netdev_rxq_dummy *rx, *prev;
1110 if (dummy->rxq_pcap) {
1111 ovs_pcap_write(dummy->rxq_pcap, packet);
1112 fflush(dummy->rxq_pcap);
1115 LIST_FOR_EACH (rx, node, &dummy->rxes) {
1116 if (rx->recv_queue_len < NETDEV_DUMMY_MAX_QUEUE) {
1118 netdev_dummy_queue_packet__(prev, ofpbuf_clone(packet));
1124 netdev_dummy_queue_packet__(prev, packet);
1126 ofpbuf_delete(packet);
1131 netdev_dummy_receive(struct unixctl_conn *conn,
1132 int argc, const char *argv[], void *aux OVS_UNUSED)
1134 struct netdev_dummy *dummy_dev;
1135 struct netdev *netdev;
1138 netdev = netdev_from_name(argv[1]);
1139 if (!netdev || !is_dummy_class(netdev->netdev_class)) {
1140 unixctl_command_reply_error(conn, "no such dummy netdev");
1143 dummy_dev = netdev_dummy_cast(netdev);
1145 for (i = 2; i < argc; i++) {
1146 struct ofpbuf *packet;
1148 packet = eth_from_packet_or_flow(argv[i]);
1150 unixctl_command_reply_error(conn, "bad packet syntax");
1154 ovs_mutex_lock(&dummy_dev->mutex);
1155 netdev_dummy_queue_packet(dummy_dev, packet);
1156 ovs_mutex_unlock(&dummy_dev->mutex);
1159 unixctl_command_reply(conn, NULL);
1162 netdev_close(netdev);
1166 netdev_dummy_set_admin_state__(struct netdev_dummy *dev, bool admin_state)
1167 OVS_REQUIRES(dev->mutex)
1169 enum netdev_flags old_flags;
1172 netdev_dummy_update_flags__(dev, 0, NETDEV_UP, &old_flags);
1174 netdev_dummy_update_flags__(dev, NETDEV_UP, 0, &old_flags);
1179 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
1180 const char *argv[], void *aux OVS_UNUSED)
1184 if (!strcasecmp(argv[argc - 1], "up")) {
1186 } else if ( !strcasecmp(argv[argc - 1], "down")) {
1189 unixctl_command_reply_error(conn, "Invalid Admin State");
1194 struct netdev *netdev = netdev_from_name(argv[1]);
1195 if (netdev && is_dummy_class(netdev->netdev_class)) {
1196 struct netdev_dummy *dummy_dev = netdev_dummy_cast(netdev);
1198 ovs_mutex_lock(&dummy_dev->mutex);
1199 netdev_dummy_set_admin_state__(dummy_dev, up);
1200 ovs_mutex_unlock(&dummy_dev->mutex);
1202 netdev_close(netdev);
1204 unixctl_command_reply_error(conn, "Unknown Dummy Interface");
1205 netdev_close(netdev);
1209 struct netdev_dummy *netdev;
1211 ovs_mutex_lock(&dummy_list_mutex);
1212 LIST_FOR_EACH (netdev, list_node, &dummy_list) {
1213 ovs_mutex_lock(&netdev->mutex);
1214 netdev_dummy_set_admin_state__(netdev, up);
1215 ovs_mutex_unlock(&netdev->mutex);
1217 ovs_mutex_unlock(&dummy_list_mutex);
1219 unixctl_command_reply(conn, "OK");
1223 netdev_dummy_register(bool override)
1225 unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
1226 2, INT_MAX, netdev_dummy_receive, NULL);
1227 unixctl_command_register("netdev-dummy/set-admin-state",
1228 "[netdev] up|down", 1, 2,
1229 netdev_dummy_set_admin_state, NULL);
1236 netdev_enumerate_types(&types);
1237 SSET_FOR_EACH (type, &types) {
1238 if (!strcmp(type, "patch")) {
1241 if (!netdev_unregister_provider(type)) {
1242 struct netdev_class *class;
1245 class = xmemdup(&dummy_class, sizeof dummy_class);
1246 class->type = xstrdup(type);
1247 error = netdev_register_provider(class);
1249 VLOG_ERR("%s: failed to register netdev provider (%s)",
1250 type, ovs_strerror(error));
1251 free(CONST_CAST(char *, class->type));
1256 sset_destroy(&types);
1258 netdev_register_provider(&dummy_class);
1260 netdev_vport_tunnel_register();