2 * Copyright (c) 2010, 2011 Nicira Networks.
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"
35 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
37 struct netdev_dev_dummy {
38 struct netdev_dev netdev_dev;
39 uint8_t hwaddr[ETH_ADDR_LEN];
41 struct netdev_stats stats;
42 enum netdev_flags flags;
43 unsigned int change_seq;
45 struct list devs; /* List of child "netdev_dummy"s. */
50 struct list node; /* In netdev_dev_dummy's "devs" list. */
51 struct list recv_queue;
55 static struct shash dummy_netdev_devs = SHASH_INITIALIZER(&dummy_netdev_devs);
57 static int netdev_dummy_create(const struct netdev_class *, const char *,
58 struct netdev_dev **);
59 static void netdev_dummy_poll_notify(const struct netdev *);
62 is_dummy_class(const struct netdev_class *class)
64 return class->create == netdev_dummy_create;
67 static struct netdev_dev_dummy *
68 netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev)
70 assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
71 return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev);
74 static struct netdev_dummy *
75 netdev_dummy_cast(const struct netdev *netdev)
77 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
78 assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
79 return CONTAINER_OF(netdev, struct netdev_dummy, netdev);
83 netdev_dummy_create(const struct netdev_class *class, const char *name,
84 struct netdev_dev **netdev_devp)
86 static unsigned int n = 0xaa550000;
87 struct netdev_dev_dummy *netdev_dev;
89 netdev_dev = xzalloc(sizeof *netdev_dev);
90 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
91 netdev_dev->hwaddr[0] = 0xaa;
92 netdev_dev->hwaddr[1] = 0x55;
93 netdev_dev->hwaddr[2] = n >> 24;
94 netdev_dev->hwaddr[3] = n >> 16;
95 netdev_dev->hwaddr[4] = n >> 8;
96 netdev_dev->hwaddr[5] = n;
97 netdev_dev->mtu = 1500;
98 netdev_dev->flags = 0;
99 netdev_dev->change_seq = 1;
100 list_init(&netdev_dev->devs);
102 shash_add(&dummy_netdev_devs, name, netdev_dev);
106 *netdev_devp = &netdev_dev->netdev_dev;
112 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
114 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
116 shash_find_and_delete(&dummy_netdev_devs,
117 netdev_dev_get_name(netdev_dev_));
122 netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
124 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
125 struct netdev_dummy *netdev;
127 netdev = xmalloc(sizeof *netdev);
128 netdev_init(&netdev->netdev, netdev_dev_);
129 list_init(&netdev->recv_queue);
130 netdev->listening = false;
132 *netdevp = &netdev->netdev;
133 list_push_back(&netdev_dev->devs, &netdev->node);
138 netdev_dummy_close(struct netdev *netdev_)
140 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
141 list_remove(&netdev->node);
142 ofpbuf_list_delete(&netdev->recv_queue);
147 netdev_dummy_listen(struct netdev *netdev_)
149 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
150 netdev->listening = true;
155 netdev_dummy_recv(struct netdev *netdev_, void *buffer, size_t size)
157 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
158 struct ofpbuf *packet;
160 if (list_is_empty(&netdev->recv_queue)) {
164 packet = ofpbuf_from_list(list_pop_front(&netdev->recv_queue));
165 if (packet->size > size) {
169 memcpy(buffer, packet->data, packet->size);
170 ofpbuf_delete(packet);
176 netdev_dummy_recv_wait(struct netdev *netdev_)
178 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
179 if (!list_is_empty(&netdev->recv_queue)) {
180 poll_immediate_wake();
185 netdev_dummy_drain(struct netdev *netdev_)
187 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
188 ofpbuf_list_delete(&netdev->recv_queue);
193 netdev_dummy_set_etheraddr(struct netdev *netdev,
194 const uint8_t mac[ETH_ADDR_LEN])
196 struct netdev_dev_dummy *dev =
197 netdev_dev_dummy_cast(netdev_get_dev(netdev));
199 if (!eth_addr_equals(dev->hwaddr, mac)) {
200 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
201 netdev_dummy_poll_notify(netdev);
208 netdev_dummy_get_etheraddr(const struct netdev *netdev,
209 uint8_t mac[ETH_ADDR_LEN])
211 const struct netdev_dev_dummy *dev =
212 netdev_dev_dummy_cast(netdev_get_dev(netdev));
214 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
219 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
221 const struct netdev_dev_dummy *dev =
222 netdev_dev_dummy_cast(netdev_get_dev(netdev));
229 netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
231 struct netdev_dev_dummy *dev =
232 netdev_dev_dummy_cast(netdev_get_dev(netdev));
239 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
241 const struct netdev_dev_dummy *dev =
242 netdev_dev_dummy_cast(netdev_get_dev(netdev));
249 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
251 struct netdev_dev_dummy *dev =
252 netdev_dev_dummy_cast(netdev_get_dev(netdev));
259 netdev_dummy_update_flags(struct netdev *netdev,
260 enum netdev_flags off, enum netdev_flags on,
261 enum netdev_flags *old_flagsp)
263 struct netdev_dev_dummy *dev =
264 netdev_dev_dummy_cast(netdev_get_dev(netdev));
266 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
270 *old_flagsp = dev->flags;
273 if (*old_flagsp != dev->flags) {
274 netdev_dummy_poll_notify(netdev);
280 netdev_dummy_change_seq(const struct netdev *netdev)
282 return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
285 /* Helper functions. */
288 netdev_dummy_poll_notify(const struct netdev *netdev)
290 struct netdev_dev_dummy *dev =
291 netdev_dev_dummy_cast(netdev_get_dev(netdev));
294 if (!dev->change_seq) {
299 static const struct netdev_class dummy_class = {
306 netdev_dummy_destroy,
307 NULL, /* get_config */
308 NULL, /* set_config */
315 netdev_dummy_recv_wait,
319 NULL, /* send_wait */
321 netdev_dummy_set_etheraddr,
322 netdev_dummy_get_etheraddr,
323 netdev_dummy_get_mtu,
324 netdev_dummy_set_mtu,
325 NULL, /* get_ifindex */
326 NULL, /* get_carrier */
327 NULL, /* get_carrier_resets */
328 NULL, /* get_miimon */
329 netdev_dummy_get_stats,
330 netdev_dummy_set_stats,
332 NULL, /* get_features */
333 NULL, /* set_advertisements */
335 NULL, /* set_policing */
336 NULL, /* get_qos_types */
337 NULL, /* get_qos_capabilities */
340 NULL, /* get_queue */
341 NULL, /* set_queue */
342 NULL, /* delete_queue */
343 NULL, /* get_queue_stats */
344 NULL, /* dump_queues */
345 NULL, /* dump_queue_stats */
350 NULL, /* add_router */
351 NULL, /* get_next_hop */
352 NULL, /* get_status */
353 NULL, /* arp_lookup */
355 netdev_dummy_update_flags,
357 netdev_dummy_change_seq
360 static struct ofpbuf *
361 eth_from_packet_or_flow(const char *s)
363 enum odp_key_fitness fitness;
364 struct ofpbuf *packet;
365 struct ofpbuf odp_key;
369 if (!eth_from_hex(s, &packet)) {
373 /* Convert string to datapath key.
375 * It would actually be nicer to parse an OpenFlow-like flow key here, but
376 * the code for that currently calls exit() on parse error. We have to
377 * settle for parsing a datapath key for now.
379 ofpbuf_init(&odp_key, 0);
380 error = odp_flow_key_from_string(s, NULL, &odp_key);
382 ofpbuf_uninit(&odp_key);
386 /* Convert odp_key to flow. */
387 fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
388 if (fitness == ODP_FIT_ERROR) {
389 ofpbuf_uninit(&odp_key);
393 packet = ofpbuf_new(0);
394 flow_compose(packet, &flow);
396 ofpbuf_uninit(&odp_key);
401 netdev_dummy_receive(struct unixctl_conn *conn,
402 int argc, const char *argv[], void *aux OVS_UNUSED)
404 struct netdev_dev_dummy *dummy_dev;
408 dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]);
410 unixctl_command_reply(conn, 501, "no such dummy netdev");
415 for (i = 2; i < argc; i++) {
416 struct netdev_dummy *dev;
417 struct ofpbuf *packet;
419 packet = eth_from_packet_or_flow(argv[i]);
421 unixctl_command_reply(conn, 501, "bad packet syntax");
426 LIST_FOR_EACH (dev, node, &dummy_dev->devs) {
427 if (dev->listening) {
428 struct ofpbuf *copy = ofpbuf_clone(packet);
429 list_push_back(&dev->recv_queue, ©->list_node);
433 ofpbuf_delete(packet);
437 unixctl_command_reply(conn, 202, "packets queued but nobody listened");
439 unixctl_command_reply(conn, 200, "success");
444 netdev_dummy_register(void)
446 netdev_register_provider(&dummy_class);
447 unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
448 2, INT_MAX, netdev_dummy_receive, NULL);