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