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