f101b938c172edf5fc6a01c6b5d13153f8cb2335
[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     memset(&config, 0, sizeof config);
442     config.flags |= TNL_F_PMTUD;
443     config.flags |= TNL_F_HDR_CACHE;
444
445     SHASH_FOR_EACH (node, args) {
446         if (!strcmp(node->name, "remote_ip")) {
447             struct in_addr in_addr;
448             if (lookup_ip(node->data, &in_addr)) {
449                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
450             } else {
451                 config.daddr = in_addr.s_addr;
452             }
453         } else if (!strcmp(node->name, "local_ip")) {
454             struct in_addr in_addr;
455             if (lookup_ip(node->data, &in_addr)) {
456                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
457             } else {
458                 config.saddr = in_addr.s_addr;
459             }
460         } else if (!strcmp(node->name, "key") && is_gre) {
461             if (!strcmp(node->data, "flow")) {
462                 config.flags |= TNL_F_IN_KEY_MATCH;
463                 config.flags |= TNL_F_OUT_KEY_ACTION;
464             } else {
465                 config.out_key = config.in_key = htonl(atoi(node->data));
466             }
467         } else if (!strcmp(node->name, "in_key") && is_gre) {
468             if (!strcmp(node->data, "flow")) {
469                 config.flags |= TNL_F_IN_KEY_MATCH;
470             } else {
471                 config.in_key = htonl(atoi(node->data));
472             }
473         } else if (!strcmp(node->name, "out_key") && is_gre) {
474             if (!strcmp(node->data, "flow")) {
475                 config.flags |= TNL_F_OUT_KEY_ACTION;
476             } else {
477                 config.out_key = htonl(atoi(node->data));
478             }
479         } else if (!strcmp(node->name, "tos")) {
480             if (!strcmp(node->data, "inherit")) {
481                 config.flags |= TNL_F_TOS_INHERIT;
482             } else {
483                 config.tos = atoi(node->data);
484             }
485         } else if (!strcmp(node->name, "ttl")) {
486             if (!strcmp(node->data, "inherit")) {
487                 config.flags |= TNL_F_TTL_INHERIT;
488             } else {
489                 config.ttl = atoi(node->data);
490             }
491         } else if (!strcmp(node->name, "csum") && is_gre) {
492             if (!strcmp(node->data, "true")) {
493                 config.flags |= TNL_F_CSUM;
494             }
495         } else if (!strcmp(node->name, "pmtud")) {
496             if (!strcmp(node->data, "false")) {
497                 config.flags &= ~TNL_F_PMTUD;
498             }
499         } else if (!strcmp(node->name, "header_cache")) {
500             if (!strcmp(node->data, "false")) {
501                 config.flags &= ~TNL_F_HDR_CACHE;
502             }
503         } else if (!strcmp(node->name, "ipsec_local_ip")) {
504             ipsec_ip_set = true;
505         } else if (!strcmp(node->name, "ipsec_cert")
506                    || !strcmp(node->name, "ipsec_psk")) {
507             ipsec_mech_set = true;
508         } else {
509             VLOG_WARN("%s: unknown %s argument '%s'",
510                       name, type, node->name);
511         }
512     }
513
514    /* IPsec doesn't work when header caching is enabled.  Disable it if the
515     * IPsec local IP address and authentication mechanism have been defined. */
516     if (ipsec_ip_set && ipsec_mech_set) {
517         VLOG_INFO("%s: header caching disabled due to use of IPsec", name);
518         config.flags &= ~TNL_F_HDR_CACHE;
519     }
520
521     if (!config.daddr) {
522         VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
523                   name, type);
524         return EINVAL;
525     }
526
527     BUILD_ASSERT(sizeof config <= VPORT_CONFIG_SIZE);
528     memcpy(configp, &config, sizeof config);
529     return 0;
530 }
531
532 static int
533 parse_patch_config(const struct netdev_dev *dev, const struct shash *args,
534                    void *configp)
535 {
536     const char *name = netdev_dev_get_name(dev);
537     const char *peer;
538
539     peer = shash_find_data(args, "peer");
540     if (!peer) {
541         VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
542         return EINVAL;
543     }
544
545     if (shash_count(args) > 1) {
546         VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
547         return EINVAL;
548     }
549
550     if (strlen(peer) >= MIN(IFNAMSIZ, VPORT_CONFIG_SIZE)) {
551         VLOG_WARN("%s: patch 'peer' arg too long", name);
552         return EINVAL;
553     }
554
555     if (!strcmp(name, peer)) {
556         VLOG_WARN("%s: patch peer must not be self", name);
557         return EINVAL;
558     }
559
560     strncpy(configp, peer, VPORT_CONFIG_SIZE);
561
562     return 0;
563 }
564 \f
565 #define VPORT_FUNCTIONS                                     \
566     NULL,                       /* init */                  \
567     NULL,                       /* run */                   \
568     NULL,                       /* wait */                  \
569                                                             \
570     netdev_vport_create,                                    \
571     netdev_vport_destroy,                                   \
572     netdev_vport_reconfigure,                               \
573                                                             \
574     netdev_vport_open,                                      \
575     netdev_vport_close,                                     \
576                                                             \
577     NULL,                       /* enumerate */             \
578                                                             \
579     NULL,                       /* recv */                  \
580     NULL,                       /* recv_wait */             \
581     NULL,                       /* drain */                 \
582                                                             \
583     NULL,                       /* send */                  \
584     NULL,                       /* send_wait */             \
585                                                             \
586     netdev_vport_set_etheraddr,                             \
587     netdev_vport_get_etheraddr,                             \
588     netdev_vport_get_mtu,                                   \
589     NULL,                       /* get_ifindex */           \
590     NULL,                       /* get_carrier */           \
591     netdev_vport_get_stats,                                 \
592     netdev_vport_set_stats,                                 \
593                                                             \
594     NULL,                       /* get_features */          \
595     NULL,                       /* set_advertisements */    \
596     NULL,                       /* get_vlan_vid */          \
597                                                             \
598     NULL,                       /* set_policing */          \
599     NULL,                       /* get_qos_types */         \
600     NULL,                       /* get_qos_capabilities */  \
601     NULL,                       /* get_qos */               \
602     NULL,                       /* set_qos */               \
603     NULL,                       /* get_queue */             \
604     NULL,                       /* set_queue */             \
605     NULL,                       /* delete_queue */          \
606     NULL,                       /* get_queue_stats */       \
607     NULL,                       /* dump_queues */           \
608     NULL,                       /* dump_queue_stats */      \
609                                                             \
610     NULL,                       /* get_in4 */               \
611     NULL,                       /* set_in4 */               \
612     NULL,                       /* get_in6 */               \
613     NULL,                       /* add_router */            \
614     NULL,                       /* get_next_hop */          \
615     NULL,                       /* arp_lookup */            \
616                                                             \
617     netdev_vport_update_flags,                              \
618                                                             \
619     netdev_vport_poll_add,                                  \
620     netdev_vport_poll_remove,
621
622 void
623 netdev_vport_register(void)
624 {
625     static const struct vport_class vport_classes[] = {
626         { { "gre", VPORT_FUNCTIONS }, parse_tunnel_config },
627         { { "capwap", VPORT_FUNCTIONS }, parse_tunnel_config },
628         { { "patch", VPORT_FUNCTIONS }, parse_patch_config }
629     };
630
631     int i;
632
633     for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
634         netdev_register_provider(&vport_classes[i].netdev_class);
635     }
636 }