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;
49 static struct shash netdev_dummy_notifiers =
50 SHASH_INITIALIZER(&netdev_dummy_notifiers);
52 static int netdev_dummy_create(const struct netdev_class *, const char *,
53 const struct shash *, struct netdev_dev **);
54 static void netdev_dummy_poll_notify(const struct netdev *);
57 is_dummy_class(const struct netdev_class *class)
59 return class->create == netdev_dummy_create;
62 static struct netdev_dev_dummy *
63 netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev)
65 assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
66 return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev);
69 static struct netdev_dummy *
70 netdev_dummy_cast(const struct netdev *netdev)
72 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
73 assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
74 return CONTAINER_OF(netdev, struct netdev_dummy, netdev);
78 netdev_dummy_create(const struct netdev_class *class, const char *name,
79 const struct shash *args OVS_UNUSED,
80 struct netdev_dev **netdev_devp)
82 static unsigned int n = 0xaa550000;
83 struct netdev_dev_dummy *netdev_dev;
85 netdev_dev = xzalloc(sizeof *netdev_dev);
86 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
87 netdev_dev->hwaddr[0] = 0xaa;
88 netdev_dev->hwaddr[1] = 0x55;
89 netdev_dev->hwaddr[2] = n >> 24;
90 netdev_dev->hwaddr[3] = n >> 16;
91 netdev_dev->hwaddr[4] = n >> 8;
92 netdev_dev->hwaddr[5] = n;
93 netdev_dev->mtu = 1500;
94 netdev_dev->flags = 0;
98 *netdev_devp = &netdev_dev->netdev_dev;
104 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
106 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
112 netdev_dummy_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
113 struct netdev **netdevp)
115 struct netdev_dummy *netdev;
117 netdev = xmalloc(sizeof *netdev);
118 netdev_init(&netdev->netdev, netdev_dev_);
120 *netdevp = &netdev->netdev;
125 netdev_dummy_close(struct netdev *netdev_)
127 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
132 netdev_dummy_set_etheraddr(struct netdev *netdev,
133 const uint8_t mac[ETH_ADDR_LEN])
135 struct netdev_dev_dummy *dev =
136 netdev_dev_dummy_cast(netdev_get_dev(netdev));
138 if (!eth_addr_equals(dev->hwaddr, mac)) {
139 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
140 netdev_dummy_poll_notify(netdev);
147 netdev_dummy_get_etheraddr(const struct netdev *netdev,
148 uint8_t mac[ETH_ADDR_LEN])
150 const struct netdev_dev_dummy *dev =
151 netdev_dev_dummy_cast(netdev_get_dev(netdev));
153 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
158 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
160 const struct netdev_dev_dummy *dev =
161 netdev_dev_dummy_cast(netdev_get_dev(netdev));
168 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
170 const struct netdev_dev_dummy *dev =
171 netdev_dev_dummy_cast(netdev_get_dev(netdev));
178 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
180 struct netdev_dev_dummy *dev =
181 netdev_dev_dummy_cast(netdev_get_dev(netdev));
188 netdev_dummy_update_flags(struct netdev *netdev,
189 enum netdev_flags off, enum netdev_flags on,
190 enum netdev_flags *old_flagsp)
192 struct netdev_dev_dummy *dev =
193 netdev_dev_dummy_cast(netdev_get_dev(netdev));
195 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
199 *old_flagsp = dev->flags;
202 if (*old_flagsp != dev->flags) {
203 netdev_dummy_poll_notify(netdev);
209 netdev_dummy_poll_add(struct netdev *netdev,
210 void (*cb)(struct netdev_notifier *), void *aux,
211 struct netdev_notifier **notifierp)
213 const char *name = netdev_get_name(netdev);
214 struct netdev_dummy_notifier *notifier;
216 struct shash_node *shash_node;
218 shash_node = shash_find_data(&netdev_dummy_notifiers, name);
220 list = xmalloc(sizeof *list);
222 shash_node = shash_add(&netdev_dummy_notifiers, name, list);
224 list = shash_node->data;
227 notifier = xmalloc(sizeof *notifier);
228 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
229 list_push_back(list, ¬ifier->list_node);
230 notifier->shash_node = shash_node;
232 *notifierp = ¬ifier->notifier;
238 netdev_dummy_poll_remove(struct netdev_notifier *notifier_)
240 struct netdev_dummy_notifier *notifier =
241 CONTAINER_OF(notifier_, struct netdev_dummy_notifier, notifier);
245 list = list_remove(¬ifier->list_node);
246 if (list_is_empty(list)) {
247 shash_delete(&netdev_dummy_notifiers, notifier->shash_node);
254 /* Helper functions. */
257 netdev_dummy_poll_notify(const struct netdev *netdev)
259 const char *name = netdev_get_name(netdev);
260 struct list *list = shash_find_data(&netdev_dummy_notifiers, name);
263 struct netdev_dummy_notifier *notifier;
265 LIST_FOR_EACH (notifier, list_node, list) {
266 struct netdev_notifier *n = ¬ifier->notifier;
272 static const struct netdev_class dummy_class = {
279 netdev_dummy_destroy,
285 NULL, /* enumerate */
288 NULL, /* recv_wait */
292 NULL, /* send_wait */
294 netdev_dummy_set_etheraddr,
295 netdev_dummy_get_etheraddr,
296 netdev_dummy_get_mtu,
297 NULL, /* get_ifindex */
298 NULL, /* get_carrier */
299 NULL, /* get_miimon */
300 netdev_dummy_get_stats,
301 netdev_dummy_set_stats,
303 NULL, /* get_features */
304 NULL, /* set_advertisements */
305 NULL, /* get_vlan_vid */
307 NULL, /* set_policing */
308 NULL, /* get_qos_types */
309 NULL, /* get_qos_capabilities */
312 NULL, /* get_queue */
313 NULL, /* set_queue */
314 NULL, /* delete_queue */
315 NULL, /* get_queue_stats */
316 NULL, /* dump_queues */
317 NULL, /* dump_queue_stats */
322 NULL, /* add_router */
323 NULL, /* get_next_hop */
324 NULL, /* get_status */
325 NULL, /* arp_lookup */
327 netdev_dummy_update_flags,
329 netdev_dummy_poll_add,
330 netdev_dummy_poll_remove,
334 netdev_dummy_register(void)
336 netdev_register_provider(&dummy_class);