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.
25 #include "netdev-provider.h"
27 #include "ofp-print.h"
30 #include "poll-loop.h"
36 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
38 struct netdev_dev_dummy {
39 struct netdev_dev netdev_dev;
40 uint8_t hwaddr[ETH_ADDR_LEN];
42 struct netdev_stats stats;
43 enum netdev_flags flags;
44 unsigned int change_seq;
46 struct list devs; /* List of child "netdev_dummy"s. */
51 struct list node; /* In netdev_dev_dummy's "devs" list. */
52 struct list recv_queue;
56 static struct shash dummy_netdev_devs = SHASH_INITIALIZER(&dummy_netdev_devs);
58 static unixctl_cb_func netdev_dummy_set_admin_state;
59 static int netdev_dummy_create(const struct netdev_class *, const char *,
60 struct netdev_dev **);
61 static void netdev_dev_dummy_poll_notify(struct netdev_dev_dummy *);
62 static int netdev_dev_dummy_update_flags(struct netdev_dev_dummy *,
63 enum netdev_flags off,
65 enum netdev_flags *old_flagsp);
68 is_dummy_class(const struct netdev_class *class)
70 return class->create == netdev_dummy_create;
73 static struct netdev_dev_dummy *
74 netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev)
76 ovs_assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
77 return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev);
80 static struct netdev_dummy *
81 netdev_dummy_cast(const struct netdev *netdev)
83 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
84 ovs_assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
85 return CONTAINER_OF(netdev, struct netdev_dummy, netdev);
89 netdev_dummy_create(const struct netdev_class *class, const char *name,
90 struct netdev_dev **netdev_devp)
92 static unsigned int n = 0xaa550000;
93 struct netdev_dev_dummy *netdev_dev;
95 netdev_dev = xzalloc(sizeof *netdev_dev);
96 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
97 netdev_dev->hwaddr[0] = 0xaa;
98 netdev_dev->hwaddr[1] = 0x55;
99 netdev_dev->hwaddr[2] = n >> 24;
100 netdev_dev->hwaddr[3] = n >> 16;
101 netdev_dev->hwaddr[4] = n >> 8;
102 netdev_dev->hwaddr[5] = n;
103 netdev_dev->mtu = 1500;
104 netdev_dev->flags = 0;
105 netdev_dev->change_seq = 1;
106 list_init(&netdev_dev->devs);
108 shash_add(&dummy_netdev_devs, name, netdev_dev);
112 *netdev_devp = &netdev_dev->netdev_dev;
118 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
120 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
122 shash_find_and_delete(&dummy_netdev_devs,
123 netdev_dev_get_name(netdev_dev_));
128 netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
130 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
131 struct netdev_dummy *netdev;
133 netdev = xmalloc(sizeof *netdev);
134 netdev_init(&netdev->netdev, netdev_dev_);
135 list_init(&netdev->recv_queue);
136 netdev->listening = false;
138 *netdevp = &netdev->netdev;
139 list_push_back(&netdev_dev->devs, &netdev->node);
144 netdev_dummy_close(struct netdev *netdev_)
146 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
147 list_remove(&netdev->node);
148 ofpbuf_list_delete(&netdev->recv_queue);
153 netdev_dummy_listen(struct netdev *netdev_)
155 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
156 netdev->listening = true;
161 netdev_dummy_recv(struct netdev *netdev_, void *buffer, size_t size)
163 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
164 struct ofpbuf *packet;
167 if (list_is_empty(&netdev->recv_queue)) {
171 packet = ofpbuf_from_list(list_pop_front(&netdev->recv_queue));
172 if (packet->size > size) {
175 packet_size = packet->size;
177 memcpy(buffer, packet->data, packet->size);
178 ofpbuf_delete(packet);
184 netdev_dummy_recv_wait(struct netdev *netdev_)
186 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
187 if (!list_is_empty(&netdev->recv_queue)) {
188 poll_immediate_wake();
193 netdev_dummy_drain(struct netdev *netdev_)
195 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
196 ofpbuf_list_delete(&netdev->recv_queue);
201 netdev_dummy_set_etheraddr(struct netdev *netdev,
202 const uint8_t mac[ETH_ADDR_LEN])
204 struct netdev_dev_dummy *dev =
205 netdev_dev_dummy_cast(netdev_get_dev(netdev));
207 if (!eth_addr_equals(dev->hwaddr, mac)) {
208 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
209 netdev_dev_dummy_poll_notify(dev);
216 netdev_dummy_get_etheraddr(const struct netdev *netdev,
217 uint8_t mac[ETH_ADDR_LEN])
219 const struct netdev_dev_dummy *dev =
220 netdev_dev_dummy_cast(netdev_get_dev(netdev));
222 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
227 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
229 const struct netdev_dev_dummy *dev =
230 netdev_dev_dummy_cast(netdev_get_dev(netdev));
237 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
239 struct netdev_dev_dummy *dev =
240 netdev_dev_dummy_cast(netdev_get_dev(netdev));
247 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
249 const struct netdev_dev_dummy *dev =
250 netdev_dev_dummy_cast(netdev_get_dev(netdev));
257 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
259 struct netdev_dev_dummy *dev =
260 netdev_dev_dummy_cast(netdev_get_dev(netdev));
267 netdev_dummy_update_flags(struct netdev *netdev,
268 enum netdev_flags off, enum netdev_flags on,
269 enum netdev_flags *old_flagsp)
271 struct netdev_dev_dummy *dev =
272 netdev_dev_dummy_cast(netdev_get_dev(netdev));
274 return netdev_dev_dummy_update_flags(dev, off, on, old_flagsp);
278 netdev_dev_dummy_update_flags(struct netdev_dev_dummy *dev,
279 enum netdev_flags off, enum netdev_flags on,
280 enum netdev_flags *old_flagsp)
282 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
286 *old_flagsp = dev->flags;
289 if (*old_flagsp != dev->flags) {
290 netdev_dev_dummy_poll_notify(dev);
296 netdev_dummy_change_seq(const struct netdev *netdev)
298 return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
301 /* Helper functions. */
304 netdev_dev_dummy_poll_notify(struct netdev_dev_dummy *dev)
307 if (!dev->change_seq) {
312 static const struct netdev_class dummy_class = {
319 netdev_dummy_destroy,
320 NULL, /* get_config */
321 NULL, /* set_config */
322 NULL, /* get_tunnel_config */
329 netdev_dummy_recv_wait,
333 NULL, /* send_wait */
335 netdev_dummy_set_etheraddr,
336 netdev_dummy_get_etheraddr,
337 netdev_dummy_get_mtu,
338 netdev_dummy_set_mtu,
339 NULL, /* get_ifindex */
340 NULL, /* get_carrier */
341 NULL, /* get_carrier_resets */
342 NULL, /* get_miimon */
343 netdev_dummy_get_stats,
344 netdev_dummy_set_stats,
346 NULL, /* get_features */
347 NULL, /* set_advertisements */
349 NULL, /* set_policing */
350 NULL, /* get_qos_types */
351 NULL, /* get_qos_capabilities */
354 NULL, /* get_queue */
355 NULL, /* set_queue */
356 NULL, /* delete_queue */
357 NULL, /* get_queue_stats */
358 NULL, /* dump_queues */
359 NULL, /* dump_queue_stats */
364 NULL, /* add_router */
365 NULL, /* get_next_hop */
366 NULL, /* get_status */
367 NULL, /* arp_lookup */
369 netdev_dummy_update_flags,
371 netdev_dummy_change_seq
374 static struct ofpbuf *
375 eth_from_packet_or_flow(const char *s)
377 enum odp_key_fitness fitness;
378 struct ofpbuf *packet;
379 struct ofpbuf odp_key;
383 if (!eth_from_hex(s, &packet)) {
387 /* Convert string to datapath key.
389 * It would actually be nicer to parse an OpenFlow-like flow key here, but
390 * the code for that currently calls exit() on parse error. We have to
391 * settle for parsing a datapath key for now.
393 ofpbuf_init(&odp_key, 0);
394 error = odp_flow_key_from_string(s, NULL, &odp_key);
396 ofpbuf_uninit(&odp_key);
400 /* Convert odp_key to flow. */
401 fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
402 if (fitness == ODP_FIT_ERROR) {
403 ofpbuf_uninit(&odp_key);
407 packet = ofpbuf_new(0);
408 flow_compose(packet, &flow);
410 ofpbuf_uninit(&odp_key);
415 netdev_dummy_receive(struct unixctl_conn *conn,
416 int argc, const char *argv[], void *aux OVS_UNUSED)
418 struct netdev_dev_dummy *dummy_dev;
422 dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]);
424 unixctl_command_reply_error(conn, "no such dummy netdev");
429 for (i = 2; i < argc; i++) {
430 struct netdev_dummy *dev;
431 struct ofpbuf *packet;
433 packet = eth_from_packet_or_flow(argv[i]);
435 unixctl_command_reply_error(conn, "bad packet syntax");
440 LIST_FOR_EACH (dev, node, &dummy_dev->devs) {
441 if (dev->listening) {
442 struct ofpbuf *copy = ofpbuf_clone(packet);
443 list_push_back(&dev->recv_queue, ©->list_node);
447 ofpbuf_delete(packet);
451 unixctl_command_reply(conn, "packets queued but nobody listened");
453 unixctl_command_reply(conn, "success");
458 netdev_dev_dummy_set_admin_state(struct netdev_dev_dummy *dev,
461 enum netdev_flags old_flags;
464 netdev_dev_dummy_update_flags(dev, 0, NETDEV_UP, &old_flags);
466 netdev_dev_dummy_update_flags(dev, NETDEV_UP, 0, &old_flags);
471 netdev_dummy_set_admin_state(struct unixctl_conn *conn, int argc,
472 const char *argv[], void *aux OVS_UNUSED)
476 if (!strcasecmp(argv[argc - 1], "up")) {
478 } else if ( !strcasecmp(argv[argc - 1], "down")) {
481 unixctl_command_reply_error(conn, "Invalid Admin State");
486 struct netdev_dev_dummy *dummy_dev;
488 dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]);
490 netdev_dev_dummy_set_admin_state(dummy_dev, up);
492 unixctl_command_reply_error(conn, "Unknown Dummy Interface");
496 struct shash_node *node;
498 SHASH_FOR_EACH (node, &dummy_netdev_devs) {
499 netdev_dev_dummy_set_admin_state(node->data, up);
502 unixctl_command_reply(conn, "OK");
506 netdev_dummy_register(bool override)
508 unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
509 2, INT_MAX, netdev_dummy_receive, NULL);
510 unixctl_command_register("netdev-dummy/set-admin-state",
511 "[netdev] up|down", 1, 2,
512 netdev_dummy_set_admin_state, NULL);
519 netdev_enumerate_types(&types);
520 SSET_FOR_EACH (type, &types) {
521 if (!netdev_unregister_provider(type)) {
522 struct netdev_class *class;
524 class = xmalloc(sizeof *class);
525 *class = dummy_class;
526 class->type = xstrdup(type);
527 netdev_register_provider(class);
530 sset_destroy(&types);
532 netdev_register_provider(&dummy_class);