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