netdev-vport: Actually destroy the vport when "destroy" is called
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010 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 "netdev-vport.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <net/if.h>
24 #include <sys/ioctl.h>
25
26 #include "list.h"
27 #include "netdev-provider.h"
28 #include "openvswitch/datapath-protocol.h"
29 #include "openvswitch/tunnel.h"
30 #include "packets.h"
31 #include "shash.h"
32 #include "socket-util.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(netdev_vport)
36
37 struct netdev_vport_notifier {
38     struct netdev_notifier notifier;
39     struct list list_node;
40     struct shash_node *shash_node;
41 };
42
43 struct netdev_dev_vport {
44     struct netdev_dev netdev_dev;
45 };
46
47 struct netdev_vport {
48     struct netdev netdev;
49 };
50
51 struct vport_info {
52     const char *devname;
53     const char *type;
54     void *config;
55 };
56
57 struct vport_class {
58     const struct netdev_class netdev_class;
59     int (*parse_config)(struct vport_info *port, const struct shash *args);
60 };
61
62 static struct shash netdev_vport_notifiers =
63                                     SHASH_INITIALIZER(&netdev_vport_notifiers);
64
65 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
66
67 static int netdev_vport_do_ioctl(int cmd, void *arg);
68 static int netdev_vport_create(const struct netdev_class *, const char *,
69                                const struct shash *, struct netdev_dev **);
70 static void netdev_vport_poll_notify(const struct netdev *);
71
72 static bool
73 is_vport_class(const struct netdev_class *class)
74 {
75     return class->create == netdev_vport_create;
76 }
77
78 static const struct vport_class *
79 vport_class_cast(const struct netdev_class *class)
80 {
81     assert(is_vport_class(class));
82     return CONTAINER_OF(class, struct vport_class, netdev_class);
83 }
84
85 static struct netdev_dev_vport *
86 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
87 {
88     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
89     return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
90 }
91
92 static struct netdev_vport *
93 netdev_vport_cast(const struct netdev *netdev)
94 {
95     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
96     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
97     return CONTAINER_OF(netdev, struct netdev_vport, netdev);
98 }
99
100 static int
101 netdev_vport_parse_config(const struct netdev_class *netdev_class,
102                           const char *name, const struct shash *args,
103                           void **configp)
104 {
105     const struct vport_class *c = vport_class_cast(netdev_class);
106     if (c->parse_config) {
107         struct vport_info info;
108         int error;
109
110         info.devname = name;
111         info.type = netdev_class->type;
112         error = (c->parse_config)(&info, args);
113         *configp = info.config;
114         return error;
115     } else {
116         if (!shash_is_empty(args)) {
117             VLOG_WARN("%s: arguments for %s vports should be empty",
118                       name, netdev_class->type);
119         }
120         *configp = NULL;
121         return 0;
122     }
123 }
124
125 static int
126 netdev_vport_create(const struct netdev_class *class, const char *name,
127                     const struct shash *args OVS_UNUSED,
128                     struct netdev_dev **netdev_devp)
129 {
130     int err;
131     struct odp_vport_add ova;
132     struct netdev_dev_vport *netdev_dev;
133
134     ovs_strlcpy(ova.port_type, class->type, sizeof ova.port_type);
135     ovs_strlcpy(ova.devname, name, sizeof ova.devname);
136     err = netdev_vport_parse_config(class, name, args, &ova.config);
137     if (err) {
138         goto exit;
139     }
140
141     err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
142
143     if (err == EBUSY) {
144         VLOG_WARN("%s: destroying existing device", name);
145
146         err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
147         if (err) {
148             goto exit;
149         }
150
151         err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
152     }
153     if (err) {
154         goto exit;
155     }
156
157     netdev_dev = xmalloc(sizeof *netdev_dev);
158     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
159
160     *netdev_devp = &netdev_dev->netdev_dev;
161
162 exit:
163     free(ova.config);
164     return err;
165 }
166
167 static void
168 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
169 {
170     struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
171
172     netdev_vport_do_ioctl(ODP_VPORT_DEL, 
173                           (char *)netdev_dev_get_name(netdev_dev_));
174     free(netdev_dev);
175 }
176
177 static int
178 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
179                 struct netdev **netdevp)
180 {
181     struct netdev_vport *netdev;
182
183     netdev = xmalloc(sizeof *netdev);
184     netdev_init(&netdev->netdev, netdev_dev_);
185
186     *netdevp = &netdev->netdev;
187     return 0;
188 }
189
190 static void
191 netdev_vport_close(struct netdev *netdev_)
192 {
193     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
194     free(netdev);
195 }
196
197 static int
198 netdev_vport_reconfigure(struct netdev_dev *netdev_dev,
199                          const struct shash *args)
200 {
201     const char *name = netdev_dev_get_name(netdev_dev);
202     struct odp_vport_mod ovm;
203     int err;
204
205     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
206     err = netdev_vport_parse_config(netdev_dev_get_class(netdev_dev), name,
207                                     args, &ovm.config);
208     if (err) {
209         return err;
210     }
211
212     err = netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
213     free(ovm.config);
214     return err;
215 }
216
217 static int
218 netdev_vport_set_etheraddr(struct netdev *netdev,
219                            const uint8_t mac[ETH_ADDR_LEN])
220 {
221     struct odp_vport_ether vport_ether;
222     int err;
223
224     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
225                 sizeof vport_ether.devname);
226
227     memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
228
229     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
230     if (err) {
231         return err;
232     }
233
234     netdev_vport_poll_notify(netdev);
235     return 0;
236 }
237
238 static int
239 netdev_vport_get_etheraddr(const struct netdev *netdev,
240                            uint8_t mac[ETH_ADDR_LEN])
241 {
242     struct odp_vport_ether vport_ether;
243     int err;
244
245     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
246                 sizeof vport_ether.devname);
247
248     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
249     if (err) {
250         return err;
251     }
252
253     memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
254     return 0;
255 }
256
257 static int
258 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
259 {
260     struct odp_vport_mtu vport_mtu;
261     int err;
262
263     ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
264                 sizeof vport_mtu.devname);
265
266     err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
267     if (err) {
268         return err;
269     }
270
271     *mtup = vport_mtu.mtu;
272     return 0;
273 }
274
275 static int
276 netdev_vport_get_carrier(const struct netdev *netdev OVS_UNUSED, bool *carrier)
277 {
278     *carrier = true;
279     return 0;
280 }
281
282 int
283 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
284 {
285     const char *name = netdev_get_name(netdev);
286     struct odp_vport_stats_req ovsr;
287     int err;
288
289     ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
290     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
291     if (err) {
292         return err;
293     }
294
295     stats->rx_packets = ovsr.stats.rx_packets;
296     stats->tx_packets = ovsr.stats.tx_packets;
297     stats->rx_bytes = ovsr.stats.rx_bytes;
298     stats->tx_bytes = ovsr.stats.tx_bytes;
299     stats->rx_errors = ovsr.stats.rx_errors;
300     stats->tx_errors = ovsr.stats.tx_errors;
301     stats->rx_dropped = ovsr.stats.rx_dropped;
302     stats->tx_dropped = ovsr.stats.tx_dropped;
303     stats->multicast = UINT64_MAX;
304     stats->collisions = ovsr.stats.collisions;
305     stats->rx_length_errors = UINT64_MAX;
306     stats->rx_over_errors = ovsr.stats.rx_over_err;
307     stats->rx_crc_errors = ovsr.stats.rx_crc_err;
308     stats->rx_frame_errors = ovsr.stats.rx_frame_err;
309     stats->rx_fifo_errors = UINT64_MAX;
310     stats->rx_missed_errors = UINT64_MAX;
311     stats->tx_aborted_errors = UINT64_MAX;
312     stats->tx_carrier_errors = UINT64_MAX;
313     stats->tx_fifo_errors = UINT64_MAX;
314     stats->tx_heartbeat_errors = UINT64_MAX;
315     stats->tx_window_errors = UINT64_MAX;
316
317     return 0;
318 }
319
320 int
321 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
322 {
323     struct odp_vport_stats_req ovsr;
324     int err;
325
326     ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
327
328     ovsr.stats.rx_packets = stats->rx_packets;
329     ovsr.stats.tx_packets = stats->tx_packets;
330     ovsr.stats.rx_bytes = stats->rx_bytes;
331     ovsr.stats.tx_bytes = stats->tx_bytes;
332     ovsr.stats.rx_errors = stats->rx_errors;
333     ovsr.stats.tx_errors = stats->tx_errors;
334     ovsr.stats.rx_dropped = stats->rx_dropped;
335     ovsr.stats.tx_dropped = stats->tx_dropped;
336     ovsr.stats.collisions = stats->collisions;
337     ovsr.stats.rx_over_err = stats->rx_over_errors;
338     ovsr.stats.rx_crc_err = stats->rx_crc_errors;
339     ovsr.stats.rx_frame_err = stats->rx_frame_errors;
340
341     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
342
343     /* If the vport layer doesn't know about the device, that doesn't mean it
344      * doesn't exist (after all were able to open it when netdev_open() was
345      * called), it just means that it isn't attached and we'll be getting
346      * stats a different way. */
347     if (err == ENODEV) {
348         err = EOPNOTSUPP;
349     }
350
351     return err;
352 }
353
354 static int
355 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
356                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
357                         enum netdev_flags *old_flagsp)
358 {
359     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
360         return EOPNOTSUPP;
361     }
362
363     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
364     return 0;
365 }
366
367 static char *
368 make_poll_name(const struct netdev *netdev)
369 {
370     return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
371 }
372
373 static int
374 netdev_vport_poll_add(struct netdev *netdev,
375                       void (*cb)(struct netdev_notifier *), void *aux,
376                       struct netdev_notifier **notifierp)
377 {
378     char *poll_name = make_poll_name(netdev);
379     struct netdev_vport_notifier *notifier;
380     struct list *list;
381     struct shash_node *shash_node;
382
383     shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
384     if (!shash_node) {
385         list = xmalloc(sizeof *list);
386         list_init(list);
387         shash_node = shash_add(&netdev_vport_notifiers,
388                                netdev_get_name(netdev), list);
389     } else {
390         list = shash_node->data;
391     }
392
393     notifier = xmalloc(sizeof *notifier);
394     netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
395     list_push_back(list, &notifier->list_node);
396     notifier->shash_node = shash_node;
397
398     *notifierp = &notifier->notifier;
399     free(poll_name);
400
401     return 0;
402 }
403
404 static void
405 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
406 {
407     struct netdev_vport_notifier *notifier =
408                 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
409
410     struct list *list;
411
412     list = list_remove(&notifier->list_node);
413     if (list_is_empty(list)) {
414         shash_delete(&netdev_vport_notifiers, notifier->shash_node);
415         free(list);
416     }
417
418     free(notifier);
419 }
420 \f
421 /* Helper functions. */
422
423 static int
424 netdev_vport_do_ioctl(int cmd, void *arg)
425 {
426     static int ioctl_fd = -1;
427
428     if (ioctl_fd < 0) {
429         ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
430         if (ioctl_fd < 0) {
431             VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
432             return errno;
433         }
434     }
435
436     return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
437 }
438
439 static void
440 netdev_vport_poll_notify(const struct netdev *netdev)
441 {
442     char *poll_name = make_poll_name(netdev);
443     struct list *list = shash_find_data(&netdev_vport_notifiers,
444                                         poll_name);
445
446     if (list) {
447         struct netdev_vport_notifier *notifier;
448
449         LIST_FOR_EACH (notifier, list_node, list) {
450             struct netdev_notifier *n = &notifier->notifier;
451             n->cb(n);
452         }
453     }
454
455     free(poll_name);
456 }
457 \f
458 /* Code specific to individual vport types. */
459
460 static int
461 parse_tunnel_config(struct vport_info *port, const struct shash *args)
462 {
463     const char *name = port->devname;
464     bool is_gre = !strcmp(port->type, "gre");
465     struct tnl_port_config *config;
466     struct shash_node *node;
467     bool ipsec_ip_set = false;
468     bool ipsec_mech_set = false;
469
470     config = port->config = xzalloc(sizeof *config);
471     config->flags |= TNL_F_PMTUD;
472     config->flags |= TNL_F_HDR_CACHE;
473
474     SHASH_FOR_EACH (node, args) {
475         if (!strcmp(node->name, "remote_ip")) {
476             struct in_addr in_addr;
477             if (lookup_ip(node->data, &in_addr)) {
478                 VLOG_WARN("%s: bad %s 'remote_ip'", name, port->type);
479             } else {
480                 config->daddr = in_addr.s_addr;
481             }
482         } else if (!strcmp(node->name, "local_ip")) {
483             struct in_addr in_addr;
484             if (lookup_ip(node->data, &in_addr)) {
485                 VLOG_WARN("%s: bad %s 'local_ip'", name, port->type);
486             } else {
487                 config->saddr = in_addr.s_addr;
488             }
489         } else if (!strcmp(node->name, "key") && is_gre) {
490             if (!strcmp(node->data, "flow")) {
491                 config->flags |= TNL_F_IN_KEY_MATCH;
492                 config->flags |= TNL_F_OUT_KEY_ACTION;
493             } else {
494                 config->out_key = config->in_key = htonl(atoi(node->data));
495             }
496         } else if (!strcmp(node->name, "in_key") && is_gre) {
497             if (!strcmp(node->data, "flow")) {
498                 config->flags |= TNL_F_IN_KEY_MATCH;
499             } else {
500                 config->in_key = htonl(atoi(node->data));
501             }
502         } else if (!strcmp(node->name, "out_key") && is_gre) {
503             if (!strcmp(node->data, "flow")) {
504                 config->flags |= TNL_F_OUT_KEY_ACTION;
505             } else {
506                 config->out_key = htonl(atoi(node->data));
507             }
508         } else if (!strcmp(node->name, "tos")) {
509             if (!strcmp(node->data, "inherit")) {
510                 config->flags |= TNL_F_TOS_INHERIT;
511             } else {
512                 config->tos = atoi(node->data);
513             }
514         } else if (!strcmp(node->name, "ttl")) {
515             if (!strcmp(node->data, "inherit")) {
516                 config->flags |= TNL_F_TTL_INHERIT;
517             } else {
518                 config->ttl = atoi(node->data);
519             }
520         } else if (!strcmp(node->name, "csum") && is_gre) {
521             if (!strcmp(node->data, "true")) {
522                 config->flags |= TNL_F_CSUM;
523             }
524         } else if (!strcmp(node->name, "pmtud")) {
525             if (!strcmp(node->data, "false")) {
526                 config->flags &= ~TNL_F_PMTUD;
527             }
528         } else if (!strcmp(node->name, "header_cache")) {
529             if (!strcmp(node->data, "false")) {
530                 config->flags &= ~TNL_F_HDR_CACHE;
531             }
532         } else if (!strcmp(node->name, "ipsec_local_ip")) {
533             ipsec_ip_set = true;
534         } else if (!strcmp(node->name, "ipsec_cert")
535                    || !strcmp(node->name, "ipsec_psk")) {
536             ipsec_mech_set = true;
537         } else {
538             VLOG_WARN("%s: unknown %s argument '%s'",
539                       name, port->type, node->name);
540         }
541     }
542
543    /* IPsec doesn't work when header caching is enabled.  Disable it if the
544     * IPsec local IP address and authentication mechanism have been defined. */
545     if (ipsec_ip_set && ipsec_mech_set) {
546         VLOG_INFO("%s: header caching disabled due to use of IPsec", name);
547         config->flags &= ~TNL_F_HDR_CACHE;
548     }
549
550     if (!config->daddr) {
551         VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
552                   name, port->type);
553         return EINVAL;
554     }
555
556     return 0;
557 }
558
559 static int
560 parse_patch_config(struct vport_info *port, const struct shash *args)
561 {
562     const char *name = port->devname;
563     const char *peer;
564
565     peer = shash_find_data(args, "peer");
566     if (!peer) {
567         VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
568         return EINVAL;
569     }
570
571     if (shash_count(args) > 1) {
572         VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
573         return EINVAL;
574     }
575
576     if (strlen(peer) >= IFNAMSIZ) {
577         VLOG_WARN("%s: patch 'peer' arg too long", name);
578         return EINVAL;
579     }
580
581     if (!strcmp(name, peer)) {
582         VLOG_WARN("%s: patch peer must not be self", name);
583         return EINVAL;
584     }
585
586     port->config = xstrdup(peer);
587
588     return 0;
589 }
590 \f
591 #define VPORT_FUNCTIONS                                     \
592     NULL,                       /* init */                  \
593     NULL,                       /* run */                   \
594     NULL,                       /* wait */                  \
595                                                             \
596     netdev_vport_create,                                    \
597     netdev_vport_destroy,                                   \
598     netdev_vport_reconfigure,                               \
599                                                             \
600     netdev_vport_open,                                      \
601     netdev_vport_close,                                     \
602                                                             \
603     NULL,                       /* enumerate */             \
604                                                             \
605     NULL,                       /* recv */                  \
606     NULL,                       /* recv_wait */             \
607     NULL,                       /* drain */                 \
608                                                             \
609     NULL,                       /* send */                  \
610     NULL,                       /* send_wait */             \
611                                                             \
612     netdev_vport_set_etheraddr,                             \
613     netdev_vport_get_etheraddr,                             \
614     netdev_vport_get_mtu,                                   \
615     NULL,                       /* get_ifindex */           \
616     netdev_vport_get_carrier,                               \
617     netdev_vport_get_stats,                                 \
618     netdev_vport_set_stats,                                 \
619                                                             \
620     NULL,                       /* get_features */          \
621     NULL,                       /* set_advertisements */    \
622     NULL,                       /* get_vlan_vid */          \
623                                                             \
624     NULL,                       /* set_policing */          \
625     NULL,                       /* get_qos_types */         \
626     NULL,                       /* get_qos_capabilities */  \
627     NULL,                       /* get_qos */               \
628     NULL,                       /* set_qos */               \
629     NULL,                       /* get_queue */             \
630     NULL,                       /* set_queue */             \
631     NULL,                       /* delete_queue */          \
632     NULL,                       /* get_queue_stats */       \
633     NULL,                       /* dump_queues */           \
634     NULL,                       /* dump_queue_stats */      \
635                                                             \
636     NULL,                       /* get_in4 */               \
637     NULL,                       /* set_in4 */               \
638     NULL,                       /* get_in6 */               \
639     NULL,                       /* add_router */            \
640     NULL,                       /* get_next_hop */          \
641     NULL,                       /* arp_lookup */            \
642                                                             \
643     netdev_vport_update_flags,                              \
644                                                             \
645     netdev_vport_poll_add,                                  \
646     netdev_vport_poll_remove,
647
648 static const struct vport_class vport_gre_class
649         = { { "gre", VPORT_FUNCTIONS }, parse_tunnel_config };
650
651 static const struct vport_class vport_capwap_class
652         = { { "capwap", VPORT_FUNCTIONS }, parse_tunnel_config };
653
654 static const struct vport_class vport_patch_class
655         = { { "patch", VPORT_FUNCTIONS }, parse_patch_config };
656
657 void
658 netdev_vport_register(void)
659 {
660     netdev_register_provider(&vport_gre_class.netdev_class);
661     netdev_register_provider(&vport_capwap_class.netdev_class);
662     netdev_register_provider(&vport_patch_class.netdev_class);
663 }