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