2 * Copyright (c) 2010 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.
24 #include "netdev-provider.h"
29 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
31 struct netdev_dummy_notifier {
32 struct netdev_notifier notifier;
33 struct list list_node;
34 struct shash_node *shash_node;
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;
50 static struct shash netdev_dummy_notifiers =
51 SHASH_INITIALIZER(&netdev_dummy_notifiers);
53 static int netdev_dummy_create(const struct netdev_class *, const char *,
54 const struct shash *, struct netdev_dev **);
55 static void netdev_dummy_poll_notify(const struct netdev *);
58 is_dummy_class(const struct netdev_class *class)
60 return class->create == netdev_dummy_create;
63 static struct netdev_dev_dummy *
64 netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev)
66 assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
67 return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev);
70 static struct netdev_dummy *
71 netdev_dummy_cast(const struct netdev *netdev)
73 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
74 assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
75 return CONTAINER_OF(netdev, struct netdev_dummy, netdev);
79 netdev_dummy_create(const struct netdev_class *class, const char *name,
80 const struct shash *args,
81 struct netdev_dev **netdev_devp)
83 static unsigned int n = 0xaa550000;
84 struct netdev_dev_dummy *netdev_dev;
86 netdev_dev = xzalloc(sizeof *netdev_dev);
87 netdev_dev_init(&netdev_dev->netdev_dev, name, args, class);
88 netdev_dev->hwaddr[0] = 0xaa;
89 netdev_dev->hwaddr[1] = 0x55;
90 netdev_dev->hwaddr[2] = n >> 24;
91 netdev_dev->hwaddr[3] = n >> 16;
92 netdev_dev->hwaddr[4] = n >> 8;
93 netdev_dev->hwaddr[5] = n;
94 netdev_dev->mtu = 1500;
95 netdev_dev->flags = 0;
96 netdev_dev->change_seq = 1;
100 *netdev_devp = &netdev_dev->netdev_dev;
106 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
108 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
114 netdev_dummy_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
115 struct netdev **netdevp)
117 struct netdev_dummy *netdev;
119 netdev = xmalloc(sizeof *netdev);
120 netdev_init(&netdev->netdev, netdev_dev_);
122 *netdevp = &netdev->netdev;
127 netdev_dummy_close(struct netdev *netdev_)
129 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
134 netdev_dummy_set_etheraddr(struct netdev *netdev,
135 const uint8_t mac[ETH_ADDR_LEN])
137 struct netdev_dev_dummy *dev =
138 netdev_dev_dummy_cast(netdev_get_dev(netdev));
140 if (!eth_addr_equals(dev->hwaddr, mac)) {
141 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
142 netdev_dummy_poll_notify(netdev);
149 netdev_dummy_get_etheraddr(const struct netdev *netdev,
150 uint8_t mac[ETH_ADDR_LEN])
152 const struct netdev_dev_dummy *dev =
153 netdev_dev_dummy_cast(netdev_get_dev(netdev));
155 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
160 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
162 const struct netdev_dev_dummy *dev =
163 netdev_dev_dummy_cast(netdev_get_dev(netdev));
170 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
172 const struct netdev_dev_dummy *dev =
173 netdev_dev_dummy_cast(netdev_get_dev(netdev));
180 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
182 struct netdev_dev_dummy *dev =
183 netdev_dev_dummy_cast(netdev_get_dev(netdev));
190 netdev_dummy_update_flags(struct netdev *netdev,
191 enum netdev_flags off, enum netdev_flags on,
192 enum netdev_flags *old_flagsp)
194 struct netdev_dev_dummy *dev =
195 netdev_dev_dummy_cast(netdev_get_dev(netdev));
197 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
201 *old_flagsp = dev->flags;
204 if (*old_flagsp != dev->flags) {
205 netdev_dummy_poll_notify(netdev);
211 netdev_dummy_poll_add(struct netdev *netdev,
212 void (*cb)(struct netdev_notifier *), void *aux,
213 struct netdev_notifier **notifierp)
215 const char *name = netdev_get_name(netdev);
216 struct netdev_dummy_notifier *notifier;
218 struct shash_node *shash_node;
220 shash_node = shash_find_data(&netdev_dummy_notifiers, name);
222 list = xmalloc(sizeof *list);
224 shash_node = shash_add(&netdev_dummy_notifiers, name, list);
226 list = shash_node->data;
229 notifier = xmalloc(sizeof *notifier);
230 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
231 list_push_back(list, ¬ifier->list_node);
232 notifier->shash_node = shash_node;
234 *notifierp = ¬ifier->notifier;
240 netdev_dummy_poll_remove(struct netdev_notifier *notifier_)
242 struct netdev_dummy_notifier *notifier =
243 CONTAINER_OF(notifier_, struct netdev_dummy_notifier, notifier);
247 list = list_remove(¬ifier->list_node);
248 if (list_is_empty(list)) {
249 shash_delete(&netdev_dummy_notifiers, notifier->shash_node);
257 netdev_dummy_change_seq(const struct netdev *netdev)
259 return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
262 /* Helper functions. */
265 netdev_dummy_poll_notify(const struct netdev *netdev)
267 const char *name = netdev_get_name(netdev);
268 struct list *list = shash_find_data(&netdev_dummy_notifiers, name);
269 struct netdev_dev_dummy *dev =
270 netdev_dev_dummy_cast(netdev_get_dev(netdev));
273 struct netdev_dummy_notifier *notifier;
275 LIST_FOR_EACH (notifier, list_node, list) {
276 struct netdev_notifier *n = ¬ifier->notifier;
282 if (!dev->change_seq) {
287 static const struct netdev_class dummy_class = {
294 netdev_dummy_destroy,
300 NULL, /* enumerate */
303 NULL, /* recv_wait */
307 NULL, /* send_wait */
309 netdev_dummy_set_etheraddr,
310 netdev_dummy_get_etheraddr,
311 netdev_dummy_get_mtu,
312 NULL, /* get_ifindex */
313 NULL, /* get_carrier */
314 NULL, /* get_miimon */
315 netdev_dummy_get_stats,
316 netdev_dummy_set_stats,
318 NULL, /* get_features */
319 NULL, /* set_advertisements */
320 NULL, /* get_vlan_vid */
322 NULL, /* set_policing */
323 NULL, /* get_qos_types */
324 NULL, /* get_qos_capabilities */
327 NULL, /* get_queue */
328 NULL, /* set_queue */
329 NULL, /* delete_queue */
330 NULL, /* get_queue_stats */
331 NULL, /* dump_queues */
332 NULL, /* dump_queue_stats */
337 NULL, /* add_router */
338 NULL, /* get_next_hop */
339 NULL, /* get_status */
340 NULL, /* arp_lookup */
342 netdev_dummy_update_flags,
344 netdev_dummy_poll_add,
345 netdev_dummy_poll_remove,
346 netdev_dummy_change_seq
350 netdev_dummy_register(void)
352 netdev_register_provider(&dummy_class);