]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-dummy.c
datapath: Enforce mutual exclusion between bridge and brcompat_mod.
[mirror_ovs.git] / lib / netdev-dummy.c
CommitLineData
614c4892 1/*
78945f19 2 * Copyright (c) 2010, 2011, 2012 Nicira Networks.
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
fbac791a 23#include "flow.h"
614c4892
BP
24#include "list.h"
25#include "netdev-provider.h"
fbac791a
BP
26#include "odp-util.h"
27#include "ofp-print.h"
28#include "ofpbuf.h"
614c4892 29#include "packets.h"
fbac791a 30#include "poll-loop.h"
614c4892 31#include "shash.h"
fbac791a 32#include "unixctl.h"
614c4892
BP
33#include "vlog.h"
34
35VLOG_DEFINE_THIS_MODULE(netdev_dummy);
36
614c4892
BP
37struct netdev_dev_dummy {
38 struct netdev_dev netdev_dev;
39 uint8_t hwaddr[ETH_ADDR_LEN];
40 int mtu;
41 struct netdev_stats stats;
42 enum netdev_flags flags;
ac4d3bcb 43 unsigned int change_seq;
fbac791a
BP
44
45 struct list devs; /* List of child "netdev_dummy"s. */
614c4892
BP
46};
47
48struct netdev_dummy {
49 struct netdev netdev;
fbac791a
BP
50 struct list node; /* In netdev_dev_dummy's "devs" list. */
51 struct list recv_queue;
52 bool listening;
614c4892
BP
53};
54
fbac791a
BP
55static struct shash dummy_netdev_devs = SHASH_INITIALIZER(&dummy_netdev_devs);
56
614c4892 57static int netdev_dummy_create(const struct netdev_class *, const char *,
de5cdb90 58 struct netdev_dev **);
614c4892
BP
59static void netdev_dummy_poll_notify(const struct netdev *);
60
61static bool
62is_dummy_class(const struct netdev_class *class)
63{
64 return class->create == netdev_dummy_create;
65}
66
67static struct netdev_dev_dummy *
68netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev)
69{
70 assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
71 return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev);
72}
73
74static struct netdev_dummy *
75netdev_dummy_cast(const struct netdev *netdev)
76{
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);
80}
81
82static int
83netdev_dummy_create(const struct netdev_class *class, const char *name,
614c4892
BP
84 struct netdev_dev **netdev_devp)
85{
86 static unsigned int n = 0xaa550000;
87 struct netdev_dev_dummy *netdev_dev;
88
89 netdev_dev = xzalloc(sizeof *netdev_dev);
de5cdb90 90 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
614c4892
BP
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;
ac4d3bcb 99 netdev_dev->change_seq = 1;
fbac791a
BP
100 list_init(&netdev_dev->devs);
101
102 shash_add(&dummy_netdev_devs, name, netdev_dev);
614c4892
BP
103
104 n++;
105
106 *netdev_devp = &netdev_dev->netdev_dev;
107
108 return 0;
109}
110
111static void
112netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
113{
114 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
115
fbac791a
BP
116 shash_find_and_delete(&dummy_netdev_devs,
117 netdev_dev_get_name(netdev_dev_));
614c4892
BP
118 free(netdev_dev);
119}
120
121static int
7b6b0ef4 122netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
614c4892 123{
fbac791a 124 struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
614c4892
BP
125 struct netdev_dummy *netdev;
126
127 netdev = xmalloc(sizeof *netdev);
128 netdev_init(&netdev->netdev, netdev_dev_);
fbac791a
BP
129 list_init(&netdev->recv_queue);
130 netdev->listening = false;
614c4892
BP
131
132 *netdevp = &netdev->netdev;
fbac791a 133 list_push_back(&netdev_dev->devs, &netdev->node);
614c4892
BP
134 return 0;
135}
136
137static void
138netdev_dummy_close(struct netdev *netdev_)
139{
140 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
fbac791a
BP
141 list_remove(&netdev->node);
142 ofpbuf_list_delete(&netdev->recv_queue);
614c4892
BP
143 free(netdev);
144}
145
7b6b0ef4 146static int
fbac791a 147netdev_dummy_listen(struct netdev *netdev_)
7b6b0ef4 148{
fbac791a
BP
149 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
150 netdev->listening = true;
7b6b0ef4
BP
151 return 0;
152}
153
154static int
fbac791a
BP
155netdev_dummy_recv(struct netdev *netdev_, void *buffer, size_t size)
156{
157 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
158 struct ofpbuf *packet;
78945f19 159 size_t packet_size;
fbac791a
BP
160
161 if (list_is_empty(&netdev->recv_queue)) {
162 return -EAGAIN;
163 }
164
165 packet = ofpbuf_from_list(list_pop_front(&netdev->recv_queue));
166 if (packet->size > size) {
167 return -EMSGSIZE;
168 }
78945f19 169 packet_size = packet->size;
fbac791a
BP
170
171 memcpy(buffer, packet->data, packet->size);
172 ofpbuf_delete(packet);
173
78945f19 174 return packet_size;
fbac791a
BP
175}
176
177static void
178netdev_dummy_recv_wait(struct netdev *netdev_)
7b6b0ef4 179{
fbac791a
BP
180 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
181 if (!list_is_empty(&netdev->recv_queue)) {
182 poll_immediate_wake();
183 }
184}
185
186static int
187netdev_dummy_drain(struct netdev *netdev_)
188{
189 struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
190 ofpbuf_list_delete(&netdev->recv_queue);
191 return 0;
7b6b0ef4
BP
192}
193
614c4892
BP
194static int
195netdev_dummy_set_etheraddr(struct netdev *netdev,
196 const uint8_t mac[ETH_ADDR_LEN])
197{
198 struct netdev_dev_dummy *dev =
199 netdev_dev_dummy_cast(netdev_get_dev(netdev));
200
201 if (!eth_addr_equals(dev->hwaddr, mac)) {
202 memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
203 netdev_dummy_poll_notify(netdev);
204 }
205
206 return 0;
207}
208
209static int
210netdev_dummy_get_etheraddr(const struct netdev *netdev,
211 uint8_t mac[ETH_ADDR_LEN])
212{
213 const struct netdev_dev_dummy *dev =
214 netdev_dev_dummy_cast(netdev_get_dev(netdev));
215
216 memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
217 return 0;
218}
219
220static int
221netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
222{
223 const struct netdev_dev_dummy *dev =
224 netdev_dev_dummy_cast(netdev_get_dev(netdev));
225
226 *mtup = dev->mtu;
227 return 0;
228}
229
9b020780
PS
230static int
231netdev_dummy_set_mtu(const struct netdev *netdev, int mtu)
232{
233 struct netdev_dev_dummy *dev =
234 netdev_dev_dummy_cast(netdev_get_dev(netdev));
235
236 dev->mtu = mtu;
237 return 0;
238}
239
614c4892
BP
240static int
241netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
242{
243 const struct netdev_dev_dummy *dev =
244 netdev_dev_dummy_cast(netdev_get_dev(netdev));
245
246 *stats = dev->stats;
247 return 0;
248}
249
250static int
251netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
252{
253 struct netdev_dev_dummy *dev =
254 netdev_dev_dummy_cast(netdev_get_dev(netdev));
255
256 dev->stats = *stats;
257 return 0;
258}
259
260static int
261netdev_dummy_update_flags(struct netdev *netdev,
262 enum netdev_flags off, enum netdev_flags on,
263 enum netdev_flags *old_flagsp)
264{
265 struct netdev_dev_dummy *dev =
266 netdev_dev_dummy_cast(netdev_get_dev(netdev));
267
268 if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
269 return EINVAL;
270 }
271
272 *old_flagsp = dev->flags;
273 dev->flags |= on;
274 dev->flags &= ~off;
275 if (*old_flagsp != dev->flags) {
276 netdev_dummy_poll_notify(netdev);
277 }
278 return 0;
279}
280
ac4d3bcb
EJ
281static unsigned int
282netdev_dummy_change_seq(const struct netdev *netdev)
283{
284 return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
285}
614c4892
BP
286\f
287/* Helper functions. */
288
289static void
290netdev_dummy_poll_notify(const struct netdev *netdev)
291{
ac4d3bcb
EJ
292 struct netdev_dev_dummy *dev =
293 netdev_dev_dummy_cast(netdev_get_dev(netdev));
614c4892 294
ac4d3bcb
EJ
295 dev->change_seq++;
296 if (!dev->change_seq) {
297 dev->change_seq++;
298 }
614c4892
BP
299}
300
301static const struct netdev_class dummy_class = {
302 "dummy",
303 NULL, /* init */
304 NULL, /* run */
305 NULL, /* wait */
306
307 netdev_dummy_create,
308 netdev_dummy_destroy,
de5cdb90 309 NULL, /* get_config */
aebf4235 310 NULL, /* set_config */
614c4892
BP
311
312 netdev_dummy_open,
313 netdev_dummy_close,
314
fbac791a
BP
315 netdev_dummy_listen,
316 netdev_dummy_recv,
317 netdev_dummy_recv_wait,
318 netdev_dummy_drain,
614c4892
BP
319
320 NULL, /* send */
321 NULL, /* send_wait */
322
323 netdev_dummy_set_etheraddr,
324 netdev_dummy_get_etheraddr,
325 netdev_dummy_get_mtu,
9b020780 326 netdev_dummy_set_mtu,
614c4892
BP
327 NULL, /* get_ifindex */
328 NULL, /* get_carrier */
65c3058c 329 NULL, /* get_carrier_resets */
63331829 330 NULL, /* get_miimon */
614c4892
BP
331 netdev_dummy_get_stats,
332 netdev_dummy_set_stats,
333
334 NULL, /* get_features */
335 NULL, /* set_advertisements */
614c4892
BP
336
337 NULL, /* set_policing */
338 NULL, /* get_qos_types */
339 NULL, /* get_qos_capabilities */
340 NULL, /* get_qos */
341 NULL, /* set_qos */
342 NULL, /* get_queue */
343 NULL, /* set_queue */
344 NULL, /* delete_queue */
345 NULL, /* get_queue_stats */
346 NULL, /* dump_queues */
347 NULL, /* dump_queue_stats */
348
349 NULL, /* get_in4 */
350 NULL, /* set_in4 */
351 NULL, /* get_in6 */
352 NULL, /* add_router */
353 NULL, /* get_next_hop */
ea763e0e 354 NULL, /* get_status */
614c4892
BP
355 NULL, /* arp_lookup */
356
357 netdev_dummy_update_flags,
358
ac4d3bcb 359 netdev_dummy_change_seq
614c4892
BP
360};
361
fbac791a
BP
362static struct ofpbuf *
363eth_from_packet_or_flow(const char *s)
364{
365 enum odp_key_fitness fitness;
366 struct ofpbuf *packet;
367 struct ofpbuf odp_key;
368 struct flow flow;
369 int error;
370
371 if (!eth_from_hex(s, &packet)) {
372 return packet;
373 }
374
375 /* Convert string to datapath key.
376 *
377 * It would actually be nicer to parse an OpenFlow-like flow key here, but
378 * the code for that currently calls exit() on parse error. We have to
379 * settle for parsing a datapath key for now.
380 */
381 ofpbuf_init(&odp_key, 0);
382 error = odp_flow_key_from_string(s, NULL, &odp_key);
383 if (error) {
384 ofpbuf_uninit(&odp_key);
385 return NULL;
386 }
387
388 /* Convert odp_key to flow. */
389 fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
390 if (fitness == ODP_FIT_ERROR) {
391 ofpbuf_uninit(&odp_key);
392 return NULL;
393 }
394
395 packet = ofpbuf_new(0);
396 flow_compose(packet, &flow);
397
398 ofpbuf_uninit(&odp_key);
399 return packet;
400}
401
402static void
403netdev_dummy_receive(struct unixctl_conn *conn,
404 int argc, const char *argv[], void *aux OVS_UNUSED)
405{
406 struct netdev_dev_dummy *dummy_dev;
407 int n_listeners;
408 int i;
409
410 dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]);
411 if (!dummy_dev) {
412 unixctl_command_reply(conn, 501, "no such dummy netdev");
413 return;
414 }
415
416 n_listeners = 0;
417 for (i = 2; i < argc; i++) {
418 struct netdev_dummy *dev;
419 struct ofpbuf *packet;
420
421 packet = eth_from_packet_or_flow(argv[i]);
422 if (!packet) {
423 unixctl_command_reply(conn, 501, "bad packet syntax");
424 return;
425 }
426
427 n_listeners = 0;
428 LIST_FOR_EACH (dev, node, &dummy_dev->devs) {
429 if (dev->listening) {
430 struct ofpbuf *copy = ofpbuf_clone(packet);
431 list_push_back(&dev->recv_queue, &copy->list_node);
432 n_listeners++;
433 }
434 }
435 ofpbuf_delete(packet);
436 }
437
438 if (!n_listeners) {
439 unixctl_command_reply(conn, 202, "packets queued but nobody listened");
440 } else {
441 unixctl_command_reply(conn, 200, "success");
442 }
443}
444
614c4892
BP
445void
446netdev_dummy_register(void)
447{
448 netdev_register_provider(&dummy_class);
fbac791a
BP
449 unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
450 2, INT_MAX, netdev_dummy_receive, NULL);
614c4892 451}