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