netdev-vport: Merge in netdev-patch and netdev-tunnel.
[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     free(netdev_dev);
173 }
174
175 static int
176 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
177                 struct netdev **netdevp)
178 {
179     struct netdev_vport *netdev;
180
181     netdev = xmalloc(sizeof *netdev);
182     netdev_init(&netdev->netdev, netdev_dev_);
183
184     *netdevp = &netdev->netdev;
185     return 0;
186 }
187
188 static void
189 netdev_vport_close(struct netdev *netdev_)
190 {
191     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
192     free(netdev);
193 }
194
195 static int
196 netdev_vport_reconfigure(struct netdev_dev *netdev_dev,
197                          const struct shash *args)
198 {
199     const char *name = netdev_dev_get_name(netdev_dev);
200     struct odp_vport_mod ovm;
201     int err;
202
203     ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
204     err = netdev_vport_parse_config(netdev_dev_get_class(netdev_dev), name,
205                                     args, &ovm.config);
206     if (err) {
207         return err;
208     }
209
210     err = netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
211     free(ovm.config);
212     return err;
213 }
214
215 static int
216 netdev_vport_set_etheraddr(struct netdev *netdev,
217                            const uint8_t mac[ETH_ADDR_LEN])
218 {
219     struct odp_vport_ether vport_ether;
220     int err;
221
222     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
223                 sizeof vport_ether.devname);
224
225     memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
226
227     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
228     if (err) {
229         return err;
230     }
231
232     netdev_vport_poll_notify(netdev);
233     return 0;
234 }
235
236 static int
237 netdev_vport_get_etheraddr(const struct netdev *netdev,
238                            uint8_t mac[ETH_ADDR_LEN])
239 {
240     struct odp_vport_ether vport_ether;
241     int err;
242
243     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
244                 sizeof vport_ether.devname);
245
246     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
247     if (err) {
248         return err;
249     }
250
251     memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
252     return 0;
253 }
254
255 static int
256 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
257 {
258     struct odp_vport_mtu vport_mtu;
259     int err;
260
261     ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
262                 sizeof vport_mtu.devname);
263
264     err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
265     if (err) {
266         return err;
267     }
268
269     *mtup = vport_mtu.mtu;
270     return 0;
271 }
272
273 static int
274 netdev_vport_get_carrier(const struct netdev *netdev OVS_UNUSED, bool *carrier)
275 {
276     *carrier = true;
277     return 0;
278 }
279
280 int
281 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
282 {
283     const char *name = netdev_get_name(netdev);
284     struct odp_vport_stats_req ovsr;
285     int err;
286
287     ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
288     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
289     if (err) {
290         return err;
291     }
292
293     stats->rx_packets = ovsr.stats.rx_packets;
294     stats->tx_packets = ovsr.stats.tx_packets;
295     stats->rx_bytes = ovsr.stats.rx_bytes;
296     stats->tx_bytes = ovsr.stats.tx_bytes;
297     stats->rx_errors = ovsr.stats.rx_errors;
298     stats->tx_errors = ovsr.stats.tx_errors;
299     stats->rx_dropped = ovsr.stats.rx_dropped;
300     stats->tx_dropped = ovsr.stats.tx_dropped;
301     stats->multicast = UINT64_MAX;
302     stats->collisions = ovsr.stats.collisions;
303     stats->rx_length_errors = UINT64_MAX;
304     stats->rx_over_errors = ovsr.stats.rx_over_err;
305     stats->rx_crc_errors = ovsr.stats.rx_crc_err;
306     stats->rx_frame_errors = ovsr.stats.rx_frame_err;
307     stats->rx_fifo_errors = UINT64_MAX;
308     stats->rx_missed_errors = UINT64_MAX;
309     stats->tx_aborted_errors = UINT64_MAX;
310     stats->tx_carrier_errors = UINT64_MAX;
311     stats->tx_fifo_errors = UINT64_MAX;
312     stats->tx_heartbeat_errors = UINT64_MAX;
313     stats->tx_window_errors = UINT64_MAX;
314
315     return 0;
316 }
317
318 int
319 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
320 {
321     struct odp_vport_stats_req ovsr;
322     int err;
323
324     ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
325
326     ovsr.stats.rx_packets = stats->rx_packets;
327     ovsr.stats.tx_packets = stats->tx_packets;
328     ovsr.stats.rx_bytes = stats->rx_bytes;
329     ovsr.stats.tx_bytes = stats->tx_bytes;
330     ovsr.stats.rx_errors = stats->rx_errors;
331     ovsr.stats.tx_errors = stats->tx_errors;
332     ovsr.stats.rx_dropped = stats->rx_dropped;
333     ovsr.stats.tx_dropped = stats->tx_dropped;
334     ovsr.stats.collisions = stats->collisions;
335     ovsr.stats.rx_over_err = stats->rx_over_errors;
336     ovsr.stats.rx_crc_err = stats->rx_crc_errors;
337     ovsr.stats.rx_frame_err = stats->rx_frame_errors;
338
339     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
340
341     /* If the vport layer doesn't know about the device, that doesn't mean it
342      * doesn't exist (after all were able to open it when netdev_open() was
343      * called), it just means that it isn't attached and we'll be getting
344      * stats a different way. */
345     if (err == ENODEV) {
346         err = EOPNOTSUPP;
347     }
348
349     return err;
350 }
351
352 static int
353 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
354                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
355                         enum netdev_flags *old_flagsp)
356 {
357     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
358         return EOPNOTSUPP;
359     }
360
361     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
362     return 0;
363 }
364
365 static char *
366 make_poll_name(const struct netdev *netdev)
367 {
368     return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
369 }
370
371 static int
372 netdev_vport_poll_add(struct netdev *netdev,
373                       void (*cb)(struct netdev_notifier *), void *aux,
374                       struct netdev_notifier **notifierp)
375 {
376     char *poll_name = make_poll_name(netdev);
377     struct netdev_vport_notifier *notifier;
378     struct list *list;
379     struct shash_node *shash_node;
380
381     shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
382     if (!shash_node) {
383         list = xmalloc(sizeof *list);
384         list_init(list);
385         shash_node = shash_add(&netdev_vport_notifiers,
386                                netdev_get_name(netdev), list);
387     } else {
388         list = shash_node->data;
389     }
390
391     notifier = xmalloc(sizeof *notifier);
392     netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
393     list_push_back(list, &notifier->list_node);
394     notifier->shash_node = shash_node;
395
396     *notifierp = &notifier->notifier;
397     free(poll_name);
398
399     return 0;
400 }
401
402 static void
403 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
404 {
405     struct netdev_vport_notifier *notifier =
406                 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
407
408     struct list *list;
409
410     list = list_remove(&notifier->list_node);
411     if (list_is_empty(list)) {
412         shash_delete(&netdev_vport_notifiers, notifier->shash_node);
413         free(list);
414     }
415
416     free(notifier);
417 }
418 \f
419 /* Helper functions. */
420
421 static int
422 netdev_vport_do_ioctl(int cmd, void *arg)
423 {
424     static int ioctl_fd = -1;
425
426     if (ioctl_fd < 0) {
427         ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
428         if (ioctl_fd < 0) {
429             VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
430             return errno;
431         }
432     }
433
434     return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
435 }
436
437 static void
438 netdev_vport_poll_notify(const struct netdev *netdev)
439 {
440     char *poll_name = make_poll_name(netdev);
441     struct list *list = shash_find_data(&netdev_vport_notifiers,
442                                         poll_name);
443
444     if (list) {
445         struct netdev_vport_notifier *notifier;
446
447         LIST_FOR_EACH (notifier, list_node, list) {
448             struct netdev_notifier *n = &notifier->notifier;
449             n->cb(n);
450         }
451     }
452
453     free(poll_name);
454 }
455 \f
456 /* Code specific to individual vport types. */
457
458 static int
459 parse_tunnel_config(struct vport_info *port, const struct shash *args)
460 {
461     const char *name = port->devname;
462     bool is_gre = !strcmp(port->type, "gre");
463     struct tnl_port_config *config;
464     struct shash_node *node;
465     bool ipsec_ip_set = false;
466     bool ipsec_mech_set = false;
467
468     config = port->config = xzalloc(sizeof *config);
469     config->flags |= TNL_F_PMTUD;
470     config->flags |= TNL_F_HDR_CACHE;
471
472     SHASH_FOR_EACH (node, args) {
473         if (!strcmp(node->name, "remote_ip")) {
474             struct in_addr in_addr;
475             if (lookup_ip(node->data, &in_addr)) {
476                 VLOG_WARN("%s: bad %s 'remote_ip'", name, port->type);
477             } else {
478                 config->daddr = in_addr.s_addr;
479             }
480         } else if (!strcmp(node->name, "local_ip")) {
481             struct in_addr in_addr;
482             if (lookup_ip(node->data, &in_addr)) {
483                 VLOG_WARN("%s: bad %s 'local_ip'", name, port->type);
484             } else {
485                 config->saddr = in_addr.s_addr;
486             }
487         } else if (!strcmp(node->name, "key") && is_gre) {
488             if (!strcmp(node->data, "flow")) {
489                 config->flags |= TNL_F_IN_KEY_MATCH;
490                 config->flags |= TNL_F_OUT_KEY_ACTION;
491             } else {
492                 config->out_key = config->in_key = htonl(atoi(node->data));
493             }
494         } else if (!strcmp(node->name, "in_key") && is_gre) {
495             if (!strcmp(node->data, "flow")) {
496                 config->flags |= TNL_F_IN_KEY_MATCH;
497             } else {
498                 config->in_key = htonl(atoi(node->data));
499             }
500         } else if (!strcmp(node->name, "out_key") && is_gre) {
501             if (!strcmp(node->data, "flow")) {
502                 config->flags |= TNL_F_OUT_KEY_ACTION;
503             } else {
504                 config->out_key = htonl(atoi(node->data));
505             }
506         } else if (!strcmp(node->name, "tos")) {
507             if (!strcmp(node->data, "inherit")) {
508                 config->flags |= TNL_F_TOS_INHERIT;
509             } else {
510                 config->tos = atoi(node->data);
511             }
512         } else if (!strcmp(node->name, "ttl")) {
513             if (!strcmp(node->data, "inherit")) {
514                 config->flags |= TNL_F_TTL_INHERIT;
515             } else {
516                 config->ttl = atoi(node->data);
517             }
518         } else if (!strcmp(node->name, "csum") && is_gre) {
519             if (!strcmp(node->data, "true")) {
520                 config->flags |= TNL_F_CSUM;
521             }
522         } else if (!strcmp(node->name, "pmtud")) {
523             if (!strcmp(node->data, "false")) {
524                 config->flags &= ~TNL_F_PMTUD;
525             }
526         } else if (!strcmp(node->name, "header_cache")) {
527             if (!strcmp(node->data, "false")) {
528                 config->flags &= ~TNL_F_HDR_CACHE;
529             }
530         } else if (!strcmp(node->name, "ipsec_local_ip")) {
531             ipsec_ip_set = true;
532         } else if (!strcmp(node->name, "ipsec_cert")
533                    || !strcmp(node->name, "ipsec_psk")) {
534             ipsec_mech_set = true;
535         } else {
536             VLOG_WARN("%s: unknown %s argument '%s'",
537                       name, port->type, node->name);
538         }
539     }
540
541    /* IPsec doesn't work when header caching is enabled.  Disable it if the
542     * IPsec local IP address and authentication mechanism have been defined. */
543     if (ipsec_ip_set && ipsec_mech_set) {
544         VLOG_INFO("%s: header caching disabled due to use of IPsec", name);
545         config->flags &= ~TNL_F_HDR_CACHE;
546     }
547
548     if (!config->daddr) {
549         VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
550                   name, port->type);
551         return EINVAL;
552     }
553
554     return 0;
555 }
556
557 static int
558 parse_patch_config(struct vport_info *port, const struct shash *args)
559 {
560     const char *name = port->devname;
561     const char *peer;
562
563     peer = shash_find_data(args, "peer");
564     if (!peer) {
565         VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
566         return EINVAL;
567     }
568
569     if (shash_count(args) > 1) {
570         VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
571         return EINVAL;
572     }
573
574     if (strlen(peer) >= IFNAMSIZ) {
575         VLOG_WARN("%s: patch 'peer' arg too long", name);
576         return EINVAL;
577     }
578
579     if (!strcmp(name, peer)) {
580         VLOG_WARN("%s: patch peer must not be self", name);
581         return EINVAL;
582     }
583
584     port->config = xstrdup(peer);
585
586     return 0;
587 }
588 \f
589 #define VPORT_FUNCTIONS                                     \
590     NULL,                       /* init */                  \
591     NULL,                       /* run */                   \
592     NULL,                       /* wait */                  \
593                                                             \
594     netdev_vport_create,                                    \
595     netdev_vport_destroy,                                   \
596     netdev_vport_reconfigure,                               \
597                                                             \
598     netdev_vport_open,                                      \
599     netdev_vport_close,                                     \
600                                                             \
601     NULL,                       /* enumerate */             \
602                                                             \
603     NULL,                       /* recv */                  \
604     NULL,                       /* recv_wait */             \
605     NULL,                       /* drain */                 \
606                                                             \
607     NULL,                       /* send */                  \
608     NULL,                       /* send_wait */             \
609                                                             \
610     netdev_vport_set_etheraddr,                             \
611     netdev_vport_get_etheraddr,                             \
612     netdev_vport_get_mtu,                                   \
613     NULL,                       /* get_ifindex */           \
614     netdev_vport_get_carrier,                               \
615     netdev_vport_get_stats,                                 \
616     netdev_vport_set_stats,                                 \
617                                                             \
618     NULL,                       /* get_features */          \
619     NULL,                       /* set_advertisements */    \
620     NULL,                       /* get_vlan_vid */          \
621                                                             \
622     NULL,                       /* set_policing */          \
623     NULL,                       /* get_qos_types */         \
624     NULL,                       /* get_qos_capabilities */  \
625     NULL,                       /* get_qos */               \
626     NULL,                       /* set_qos */               \
627     NULL,                       /* get_queue */             \
628     NULL,                       /* set_queue */             \
629     NULL,                       /* delete_queue */          \
630     NULL,                       /* get_queue_stats */       \
631     NULL,                       /* dump_queues */           \
632     NULL,                       /* dump_queue_stats */      \
633                                                             \
634     NULL,                       /* get_in4 */               \
635     NULL,                       /* set_in4 */               \
636     NULL,                       /* get_in6 */               \
637     NULL,                       /* add_router */            \
638     NULL,                       /* get_next_hop */          \
639     NULL,                       /* arp_lookup */            \
640                                                             \
641     netdev_vport_update_flags,                              \
642                                                             \
643     netdev_vport_poll_add,                                  \
644     netdev_vport_poll_remove,
645
646 static const struct vport_class vport_gre_class
647         = { { "gre", VPORT_FUNCTIONS }, parse_tunnel_config };
648
649 static const struct vport_class vport_capwap_class
650         = { { "capwap", VPORT_FUNCTIONS }, parse_tunnel_config };
651
652 static const struct vport_class vport_patch_class
653         = { { "patch", VPORT_FUNCTIONS }, parse_patch_config };
654
655 void
656 netdev_vport_register(void)
657 {
658     netdev_register_provider(&vport_gre_class.netdev_class);
659     netdev_register_provider(&vport_capwap_class.netdev_class);
660     netdev_register_provider(&vport_patch_class.netdev_class);
661 }