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