Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / netdev-dummy.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 Nicira, Inc.
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 "sset.h"
33 #include "unixctl.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
37
38 struct 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;
44     unsigned int change_seq;
45
46     struct list devs;           /* List of child "netdev_dummy"s. */
47 };
48
49 struct netdev_dummy {
50     struct netdev netdev;
51     struct list node;           /* In netdev_dev_dummy's "devs" list. */
52     struct list recv_queue;
53     bool listening;
54 };
55
56 static struct shash dummy_netdev_devs = SHASH_INITIALIZER(&dummy_netdev_devs);
57
58 static int netdev_dummy_create(const struct netdev_class *, const char *,
59                                struct netdev_dev **);
60 static void netdev_dummy_poll_notify(const struct netdev *);
61
62 static bool
63 is_dummy_class(const struct netdev_class *class)
64 {
65     return class->create == netdev_dummy_create;
66 }
67
68 static struct netdev_dev_dummy *
69 netdev_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
75 static struct netdev_dummy *
76 netdev_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
83 static int
84 netdev_dummy_create(const struct netdev_class *class, const char *name,
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);
91     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
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;
100     netdev_dev->change_seq = 1;
101     list_init(&netdev_dev->devs);
102
103     shash_add(&dummy_netdev_devs, name, netdev_dev);
104
105     n++;
106
107     *netdev_devp = &netdev_dev->netdev_dev;
108
109     return 0;
110 }
111
112 static void
113 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
114 {
115     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
116
117     shash_find_and_delete(&dummy_netdev_devs,
118                           netdev_dev_get_name(netdev_dev_));
119     free(netdev_dev);
120 }
121
122 static int
123 netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
124 {
125     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
126     struct netdev_dummy *netdev;
127
128     netdev = xmalloc(sizeof *netdev);
129     netdev_init(&netdev->netdev, netdev_dev_);
130     list_init(&netdev->recv_queue);
131     netdev->listening = false;
132
133     *netdevp = &netdev->netdev;
134     list_push_back(&netdev_dev->devs, &netdev->node);
135     return 0;
136 }
137
138 static void
139 netdev_dummy_close(struct netdev *netdev_)
140 {
141     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
142     list_remove(&netdev->node);
143     ofpbuf_list_delete(&netdev->recv_queue);
144     free(netdev);
145 }
146
147 static int
148 netdev_dummy_listen(struct netdev *netdev_)
149 {
150     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
151     netdev->listening = true;
152     return 0;
153 }
154
155 static int
156 netdev_dummy_recv(struct netdev *netdev_, void *buffer, size_t size)
157 {
158     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
159     struct ofpbuf *packet;
160     size_t packet_size;
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     }
170     packet_size = packet->size;
171
172     memcpy(buffer, packet->data, packet->size);
173     ofpbuf_delete(packet);
174
175     return packet_size;
176 }
177
178 static void
179 netdev_dummy_recv_wait(struct netdev *netdev_)
180 {
181     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
182     if (!list_is_empty(&netdev->recv_queue)) {
183         poll_immediate_wake();
184     }
185 }
186
187 static int
188 netdev_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;
193 }
194
195 static int
196 netdev_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
210 static int
211 netdev_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
221 static int
222 netdev_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
231 static int
232 netdev_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
241 static int
242 netdev_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
251 static int
252 netdev_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
261 static int
262 netdev_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
282 static unsigned int
283 netdev_dummy_change_seq(const struct netdev *netdev)
284 {
285     return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
286 }
287 \f
288 /* Helper functions. */
289
290 static void
291 netdev_dummy_poll_notify(const struct netdev *netdev)
292 {
293     struct netdev_dev_dummy *dev =
294         netdev_dev_dummy_cast(netdev_get_dev(netdev));
295
296     dev->change_seq++;
297     if (!dev->change_seq) {
298         dev->change_seq++;
299     }
300 }
301
302 static 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,
310     NULL,                       /* get_config */
311     NULL,                       /* set_config */
312
313     netdev_dummy_open,
314     netdev_dummy_close,
315
316     netdev_dummy_listen,
317     netdev_dummy_recv,
318     netdev_dummy_recv_wait,
319     netdev_dummy_drain,
320
321     NULL,                       /* send */
322     NULL,                       /* send_wait */
323
324     netdev_dummy_set_etheraddr,
325     netdev_dummy_get_etheraddr,
326     netdev_dummy_get_mtu,
327     netdev_dummy_set_mtu,
328     NULL,                       /* get_ifindex */
329     NULL,                       /* get_carrier */
330     NULL,                       /* get_carrier_resets */
331     NULL,                       /* get_miimon */
332     netdev_dummy_get_stats,
333     netdev_dummy_set_stats,
334
335     NULL,                       /* get_features */
336     NULL,                       /* set_advertisements */
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 */
355     NULL,                       /* get_drv_info */
356     NULL,                       /* arp_lookup */
357
358     netdev_dummy_update_flags,
359
360     netdev_dummy_change_seq
361 };
362
363 static struct ofpbuf *
364 eth_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
403 static void
404 netdev_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) {
413         unixctl_command_reply_error(conn, "no such dummy netdev");
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) {
424             unixctl_command_reply_error(conn, "bad packet syntax");
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) {
440         unixctl_command_reply(conn, "packets queued but nobody listened");
441     } else {
442         unixctl_command_reply(conn, "success");
443     }
444 }
445
446 void
447 netdev_dummy_register(bool override)
448 {
449     unixctl_command_register("netdev-dummy/receive", "NAME PACKET|FLOW...",
450                              2, INT_MAX, netdev_dummy_receive, NULL);
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);
471 }