learn: Avoid 1-byte buffer underrun in learn_format().
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010, 2011 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 <sys/socket.h>
24 #include <linux/openvswitch.h>
25 #include <linux/rtnetlink.h>
26 #include <net/if.h>
27 #include <sys/ioctl.h>
28
29 #include "byte-order.h"
30 #include "daemon.h"
31 #include "dirs.h"
32 #include "dpif-linux.h"
33 #include "hash.h"
34 #include "hmap.h"
35 #include "list.h"
36 #include "netdev-linux.h"
37 #include "netdev-provider.h"
38 #include "netlink.h"
39 #include "netlink-notifier.h"
40 #include "netlink-socket.h"
41 #include "ofpbuf.h"
42 #include "openvswitch/tunnel.h"
43 #include "packets.h"
44 #include "route-table.h"
45 #include "shash.h"
46 #include "socket-util.h"
47 #include "unaligned.h"
48 #include "vlog.h"
49
50 VLOG_DEFINE_THIS_MODULE(netdev_vport);
51
52 struct netdev_dev_vport {
53     struct netdev_dev netdev_dev;
54     struct ofpbuf *options;
55     int dp_ifindex;             /* -1 if unknown. */
56     uint32_t port_no;           /* UINT32_MAX if unknown. */
57     unsigned int change_seq;
58 };
59
60 struct netdev_vport {
61     struct netdev netdev;
62 };
63
64 struct vport_class {
65     enum ovs_vport_type type;
66     struct netdev_class netdev_class;
67     int (*parse_config)(const char *name, const char *type,
68                         const struct shash *args, struct ofpbuf *options);
69     int (*unparse_config)(const char *name, const char *type,
70                           const struct nlattr *options, size_t options_len,
71                           struct shash *args);
72 };
73
74 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
75
76 static int netdev_vport_create(const struct netdev_class *, const char *,
77                                struct netdev_dev **);
78 static void netdev_vport_poll_notify(const struct netdev *);
79 static int tnl_port_config_from_nlattr(const struct nlattr *options,
80                                        size_t options_len,
81                                        struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1]);
82
83 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
84
85 static bool
86 is_vport_class(const struct netdev_class *class)
87 {
88     return class->create == netdev_vport_create;
89 }
90
91 static const struct vport_class *
92 vport_class_cast(const struct netdev_class *class)
93 {
94     assert(is_vport_class(class));
95     return CONTAINER_OF(class, struct vport_class, netdev_class);
96 }
97
98 static struct netdev_dev_vport *
99 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
100 {
101     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
102     return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
103 }
104
105 static struct netdev_vport *
106 netdev_vport_cast(const struct netdev *netdev)
107 {
108     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
109     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
110     return CONTAINER_OF(netdev, struct netdev_vport, netdev);
111 }
112
113 /* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
114  * options to include in OVS_VPORT_ATTR_OPTIONS for configuring that vport.
115  * Otherwise returns NULL. */
116 const struct ofpbuf *
117 netdev_vport_get_options(const struct netdev *netdev)
118 {
119     const struct netdev_dev *dev = netdev_get_dev(netdev);
120
121     return (is_vport_class(netdev_dev_get_class(dev))
122             ? netdev_dev_vport_cast(dev)->options
123             : NULL);
124 }
125
126 enum ovs_vport_type
127 netdev_vport_get_vport_type(const struct netdev *netdev)
128 {
129     const struct netdev_dev *dev = netdev_get_dev(netdev);
130     const struct netdev_class *class = netdev_dev_get_class(dev);
131
132     return (is_vport_class(class) ? vport_class_cast(class)->type
133             : class == &netdev_internal_class ? OVS_VPORT_TYPE_INTERNAL
134             : (class == &netdev_linux_class ||
135                class == &netdev_tap_class) ? OVS_VPORT_TYPE_NETDEV
136             : OVS_VPORT_TYPE_UNSPEC);
137 }
138
139 const char *
140 netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
141 {
142     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
143
144     switch (vport->type) {
145     case OVS_VPORT_TYPE_UNSPEC:
146         break;
147
148     case OVS_VPORT_TYPE_NETDEV:
149         return "system";
150
151     case OVS_VPORT_TYPE_INTERNAL:
152         return "internal";
153
154     case OVS_VPORT_TYPE_PATCH:
155         return "patch";
156
157     case OVS_VPORT_TYPE_GRE:
158         if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
159                                         a)) {
160             break;
161         }
162         return (nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
163                 ? "ipsec_gre" : "gre");
164
165     case OVS_VPORT_TYPE_CAPWAP:
166         return "capwap";
167
168     case __OVS_VPORT_TYPE_MAX:
169         break;
170     }
171
172     VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
173                  vport->dp_ifindex, vport->name, (unsigned int) vport->type);
174     return "unknown";
175 }
176
177 static int
178 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
179                     struct netdev_dev **netdev_devp)
180 {
181     struct netdev_dev_vport *dev;
182
183     dev = xmalloc(sizeof *dev);
184     netdev_dev_init(&dev->netdev_dev, name, netdev_class);
185     dev->options = NULL;
186     dev->dp_ifindex = -1;
187     dev->port_no = UINT32_MAX;
188     dev->change_seq = 1;
189
190     *netdev_devp = &dev->netdev_dev;
191     route_table_register();
192
193     return 0;
194 }
195
196 static void
197 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
198 {
199     struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
200
201     route_table_unregister();
202     free(netdev_dev);
203 }
204
205 static int
206 netdev_vport_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
207 {
208     struct netdev_vport *netdev;
209
210     netdev = xmalloc(sizeof *netdev);
211     netdev_init(&netdev->netdev, netdev_dev_);
212
213     *netdevp = &netdev->netdev;
214     return 0;
215 }
216
217 static void
218 netdev_vport_close(struct netdev *netdev_)
219 {
220     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
221     free(netdev);
222 }
223
224 static int
225 netdev_vport_get_config(struct netdev_dev *dev_, struct shash *args)
226 {
227     const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
228     const struct vport_class *vport_class = vport_class_cast(netdev_class);
229     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
230     const char *name = netdev_dev_get_name(dev_);
231     int error;
232
233     if (!dev->options) {
234         struct dpif_linux_vport reply;
235         struct ofpbuf *buf;
236
237         error = dpif_linux_vport_get(name, &reply, &buf);
238         if (error) {
239             VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
240                         name, strerror(error));
241             return error;
242         }
243
244         dev->options = ofpbuf_clone_data(reply.options, reply.options_len);
245         dev->dp_ifindex = reply.dp_ifindex;
246         dev->port_no = reply.port_no;
247         ofpbuf_delete(buf);
248     }
249
250     error = vport_class->unparse_config(name, netdev_class->type,
251                                         dev->options->data,
252                                         dev->options->size,
253                                         args);
254     if (error) {
255         VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
256                     name, strerror(error));
257     }
258     return error;
259 }
260
261 static int
262 netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
263 {
264     const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
265     const struct vport_class *vport_class = vport_class_cast(netdev_class);
266     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
267     const char *name = netdev_dev_get_name(dev_);
268     struct ofpbuf *options;
269     int error;
270
271     options = ofpbuf_new(64);
272     error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
273                                       args, options);
274     if (!error
275         && (!dev->options
276             || options->size != dev->options->size
277             || memcmp(options->data, dev->options->data, options->size))) {
278         struct dpif_linux_vport vport;
279
280         dpif_linux_vport_init(&vport);
281         vport.cmd = OVS_VPORT_CMD_SET;
282         vport.name = name;
283         vport.options = options->data;
284         vport.options_len = options->size;
285         error = dpif_linux_vport_transact(&vport, NULL, NULL);
286         if (!error || error == ENODEV) {
287             /* Either reconfiguration succeeded or this vport is not installed
288              * in the kernel (e.g. it hasn't been added to a dpif yet with
289              * dpif_port_add()). */
290             ofpbuf_delete(dev->options);
291             dev->options = options;
292             options = NULL;
293             error = 0;
294         }
295     }
296     ofpbuf_delete(options);
297
298     return error;
299 }
300
301 static int
302 netdev_vport_send(struct netdev *netdev, const void *data, size_t size)
303 {
304     struct netdev_dev *dev_ = netdev_get_dev(netdev);
305     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
306
307     if (dev->dp_ifindex == -1) {
308         const char *name = netdev_get_name(netdev);
309         struct dpif_linux_vport reply;
310         struct ofpbuf *buf;
311         int error;
312
313         error = dpif_linux_vport_get(name, &reply, &buf);
314         if (error) {
315             VLOG_ERR_RL(&rl, "%s: failed to query vport for send (%s)",
316                         name, strerror(error));
317             return error;
318         }
319         dev->dp_ifindex = reply.dp_ifindex;
320         dev->port_no = reply.port_no;
321         ofpbuf_delete(buf);
322     }
323
324     return dpif_linux_vport_send(dev->dp_ifindex, dev->port_no, data, size);
325 }
326
327 static int
328 netdev_vport_set_etheraddr(struct netdev *netdev,
329                            const uint8_t mac[ETH_ADDR_LEN])
330 {
331     struct dpif_linux_vport vport;
332     int error;
333
334     dpif_linux_vport_init(&vport);
335     vport.cmd = OVS_VPORT_CMD_SET;
336     vport.name = netdev_get_name(netdev);
337     vport.address = mac;
338
339     error = dpif_linux_vport_transact(&vport, NULL, NULL);
340     if (!error) {
341         netdev_vport_poll_notify(netdev);
342     }
343     return error;
344 }
345
346 static int
347 netdev_vport_get_etheraddr(const struct netdev *netdev,
348                            uint8_t mac[ETH_ADDR_LEN])
349 {
350     struct dpif_linux_vport reply;
351     struct ofpbuf *buf;
352     int error;
353
354     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
355     if (!error) {
356         if (reply.address) {
357             memcpy(mac, reply.address, ETH_ADDR_LEN);
358         } else {
359             error = EOPNOTSUPP;
360         }
361         ofpbuf_delete(buf);
362     }
363     return error;
364 }
365
366 /* Copies 'src' into 'dst', performing format conversion in the process.
367  *
368  * 'src' is allowed to be misaligned. */
369 static void
370 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
371                                   const struct ovs_vport_stats *src)
372 {
373     dst->rx_packets = get_unaligned_u64(&src->rx_packets);
374     dst->tx_packets = get_unaligned_u64(&src->tx_packets);
375     dst->rx_bytes = get_unaligned_u64(&src->rx_bytes);
376     dst->tx_bytes = get_unaligned_u64(&src->tx_bytes);
377     dst->rx_errors = get_unaligned_u64(&src->rx_errors);
378     dst->tx_errors = get_unaligned_u64(&src->tx_errors);
379     dst->rx_dropped = get_unaligned_u64(&src->rx_dropped);
380     dst->tx_dropped = get_unaligned_u64(&src->tx_dropped);
381     dst->multicast = 0;
382     dst->collisions = 0;
383     dst->rx_length_errors = 0;
384     dst->rx_over_errors = 0;
385     dst->rx_crc_errors = 0;
386     dst->rx_frame_errors = 0;
387     dst->rx_fifo_errors = 0;
388     dst->rx_missed_errors = 0;
389     dst->tx_aborted_errors = 0;
390     dst->tx_carrier_errors = 0;
391     dst->tx_fifo_errors = 0;
392     dst->tx_heartbeat_errors = 0;
393     dst->tx_window_errors = 0;
394 }
395
396 /* Copies 'src' into 'dst', performing format conversion in the process. */
397 static void
398 netdev_stats_to_ovs_vport_stats(struct ovs_vport_stats *dst,
399                                 const struct netdev_stats *src)
400 {
401     dst->rx_packets = src->rx_packets;
402     dst->tx_packets = src->tx_packets;
403     dst->rx_bytes = src->rx_bytes;
404     dst->tx_bytes = src->tx_bytes;
405     dst->rx_errors = src->rx_errors;
406     dst->tx_errors = src->tx_errors;
407     dst->rx_dropped = src->rx_dropped;
408     dst->tx_dropped = src->tx_dropped;
409 }
410
411 int
412 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
413 {
414     struct dpif_linux_vport reply;
415     struct ofpbuf *buf;
416     int error;
417
418     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
419     if (error) {
420         return error;
421     } else if (!reply.stats) {
422         ofpbuf_delete(buf);
423         return EOPNOTSUPP;
424     }
425
426     netdev_stats_from_ovs_vport_stats(stats, reply.stats);
427
428     ofpbuf_delete(buf);
429
430     return 0;
431 }
432
433 int
434 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
435 {
436     struct ovs_vport_stats rtnl_stats;
437     struct dpif_linux_vport vport;
438     int err;
439
440     netdev_stats_to_ovs_vport_stats(&rtnl_stats, stats);
441
442     dpif_linux_vport_init(&vport);
443     vport.cmd = OVS_VPORT_CMD_SET;
444     vport.name = netdev_get_name(netdev);
445     vport.stats = &rtnl_stats;
446
447     err = dpif_linux_vport_transact(&vport, NULL, NULL);
448
449     /* If the vport layer doesn't know about the device, that doesn't mean it
450      * doesn't exist (after all were able to open it when netdev_open() was
451      * called), it just means that it isn't attached and we'll be getting
452      * stats a different way. */
453     if (err == ENODEV) {
454         err = EOPNOTSUPP;
455     }
456
457     return err;
458 }
459
460 static int
461 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
462 {
463     const char *iface = netdev_vport_get_tnl_iface(netdev);
464
465     if (iface) {
466         struct netdev *egress_netdev;
467
468         shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
469
470         if (!netdev_open(iface, "system", &egress_netdev)) {
471             shash_add(sh, "tunnel_egress_iface_carrier",
472                       xstrdup(netdev_get_carrier(egress_netdev)
473                               ? "up" : "down"));
474             netdev_close(egress_netdev);
475         }
476     }
477
478     return 0;
479 }
480
481 static int
482 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
483                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
484                         enum netdev_flags *old_flagsp)
485 {
486     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
487         return EOPNOTSUPP;
488     }
489
490     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
491     return 0;
492 }
493
494 static unsigned int
495 netdev_vport_change_seq(const struct netdev *netdev)
496 {
497     return netdev_dev_vport_cast(netdev_get_dev(netdev))->change_seq;
498 }
499
500 static void
501 netdev_vport_run(void)
502 {
503     route_table_run();
504 }
505
506 static void
507 netdev_vport_wait(void)
508 {
509     route_table_wait();
510 }
511 \f
512 /* get_tnl_iface() implementation. */
513 static const char *
514 netdev_vport_get_tnl_iface(const struct netdev *netdev)
515 {
516     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
517     ovs_be32 route;
518     struct netdev_dev_vport *ndv;
519     static char name[IFNAMSIZ];
520
521     ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
522     if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
523                                     a)) {
524         return NULL;
525     }
526     route = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
527
528     if (route_table_get_name(route, name)) {
529         return name;
530     }
531
532     return NULL;
533 }
534 \f
535 /* Helper functions. */
536
537 static void
538 netdev_vport_poll_notify(const struct netdev *netdev)
539 {
540     struct netdev_dev_vport *ndv;
541
542     ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
543
544     ndv->change_seq++;
545     if (!ndv->change_seq) {
546         ndv->change_seq++;
547     }
548 }
549 \f
550 /* Code specific to individual vport types. */
551
552 static void
553 set_key(const struct shash *args, const char *name, uint16_t type,
554         struct ofpbuf *options)
555 {
556     const char *s;
557
558     s = shash_find_data(args, name);
559     if (!s) {
560         s = shash_find_data(args, "key");
561         if (!s) {
562             s = "0";
563         }
564     }
565
566     if (!strcmp(s, "flow")) {
567         /* This is the default if no attribute is present. */
568     } else {
569         nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
570     }
571 }
572
573 static int
574 parse_tunnel_config(const char *name, const char *type,
575                     const struct shash *args, struct ofpbuf *options)
576 {
577     bool is_gre = false;
578     bool is_ipsec = false;
579     struct shash_node *node;
580     bool ipsec_mech_set = false;
581     ovs_be32 daddr = htonl(0);
582     uint32_t flags;
583
584     flags = TNL_F_DF_DEFAULT | TNL_F_PMTUD | TNL_F_HDR_CACHE;
585     if (!strcmp(type, "gre")) {
586         is_gre = true;
587     } else if (!strcmp(type, "ipsec_gre")) {
588         is_gre = true;
589         is_ipsec = true;
590         flags |= TNL_F_IPSEC;
591         flags &= ~TNL_F_HDR_CACHE;
592     }
593
594     SHASH_FOR_EACH (node, args) {
595         if (!strcmp(node->name, "remote_ip")) {
596             struct in_addr in_addr;
597             if (lookup_ip(node->data, &in_addr)) {
598                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
599             } else {
600                 daddr = in_addr.s_addr;
601             }
602         } else if (!strcmp(node->name, "local_ip")) {
603             struct in_addr in_addr;
604             if (lookup_ip(node->data, &in_addr)) {
605                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
606             } else {
607                 nl_msg_put_be32(options, OVS_TUNNEL_ATTR_SRC_IPV4,
608                                 in_addr.s_addr);
609             }
610         } else if (!strcmp(node->name, "tos")) {
611             if (!strcmp(node->data, "inherit")) {
612                 flags |= TNL_F_TOS_INHERIT;
613             } else {
614                 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TOS, atoi(node->data));
615             }
616         } else if (!strcmp(node->name, "ttl")) {
617             if (!strcmp(node->data, "inherit")) {
618                 flags |= TNL_F_TTL_INHERIT;
619             } else {
620                 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TTL, atoi(node->data));
621             }
622         } else if (!strcmp(node->name, "csum") && is_gre) {
623             if (!strcmp(node->data, "true")) {
624                 flags |= TNL_F_CSUM;
625             }
626         } else if (!strcmp(node->name, "df_inherit")) {
627             if (!strcmp(node->data, "true")) {
628                 flags |= TNL_F_DF_INHERIT;
629             }
630         } else if (!strcmp(node->name, "df_default")) {
631             if (!strcmp(node->data, "false")) {
632                 flags &= ~TNL_F_DF_DEFAULT;
633             }
634         } else if (!strcmp(node->name, "pmtud")) {
635             if (!strcmp(node->data, "false")) {
636                 flags &= ~TNL_F_PMTUD;
637             }
638         } else if (!strcmp(node->name, "header_cache")) {
639             if (!strcmp(node->data, "false")) {
640                 flags &= ~TNL_F_HDR_CACHE;
641             }
642         } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
643             if (shash_find(args, "certificate")) {
644                 ipsec_mech_set = true;
645             } else {
646                 const char *use_ssl_cert;
647
648                 /* If the "use_ssl_cert" is true, then "certificate" and
649                  * "private_key" will be pulled from the SSL table.  The
650                  * use of this option is strongly discouraged, since it
651                  * will like be removed when multiple SSL configurations
652                  * are supported by OVS.
653                  */
654                 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
655                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
656                     VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
657                              name);
658                     return EINVAL;
659                 }
660                 ipsec_mech_set = true;
661             }
662         } else if (!strcmp(node->name, "psk") && is_ipsec) {
663             ipsec_mech_set = true;
664         } else if (is_ipsec
665                 && (!strcmp(node->name, "certificate")
666                     || !strcmp(node->name, "private_key")
667                     || !strcmp(node->name, "use_ssl_cert"))) {
668             /* Ignore options not used by the netdev. */
669         } else if (!strcmp(node->name, "key") ||
670                    !strcmp(node->name, "in_key") ||
671                    !strcmp(node->name, "out_key")) {
672             /* Handled separately below. */
673         } else {
674             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
675         }
676     }
677
678     if (is_ipsec) {
679         char *file_name = xasprintf("%s/%s", ovs_rundir(),
680                 "ovs-monitor-ipsec.pid");
681         pid_t pid = read_pidfile(file_name);
682         free(file_name);
683         if (pid < 0) {
684             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
685                      name);
686             return EINVAL;
687         }
688
689         if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
690             VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
691             return EINVAL;
692         }
693
694         if (!ipsec_mech_set) {
695             VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
696                      name);
697             return EINVAL;
698         }
699     }
700
701     set_key(args, "in_key", OVS_TUNNEL_ATTR_IN_KEY, options);
702     set_key(args, "out_key", OVS_TUNNEL_ATTR_OUT_KEY, options);
703
704     if (!daddr) {
705         VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
706                  name, type);
707         return EINVAL;
708     }
709     nl_msg_put_be32(options, OVS_TUNNEL_ATTR_DST_IPV4, daddr);
710
711     nl_msg_put_u32(options, OVS_TUNNEL_ATTR_FLAGS, flags);
712
713     return 0;
714 }
715
716 static int
717 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
718                             struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1])
719 {
720     static const struct nl_policy ovs_tunnel_policy[] = {
721         [OVS_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
722         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
723         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
724         [OVS_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
725         [OVS_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
726         [OVS_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
727         [OVS_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
728     };
729     struct ofpbuf buf;
730
731     ofpbuf_use_const(&buf, options, options_len);
732     if (!nl_policy_parse(&buf, 0, ovs_tunnel_policy,
733                          a, ARRAY_SIZE(ovs_tunnel_policy))) {
734         return EINVAL;
735     }
736     return 0;
737 }
738
739 static uint64_t
740 get_be64_or_zero(const struct nlattr *a)
741 {
742     return a ? ntohll(nl_attr_get_be64(a)) : 0;
743 }
744
745 static int
746 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
747                       const struct nlattr *options, size_t options_len,
748                       struct shash *args)
749 {
750     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
751     ovs_be32 daddr;
752     uint32_t flags;
753     int error;
754
755     error = tnl_port_config_from_nlattr(options, options_len, a);
756     if (error) {
757         return error;
758     }
759
760     flags = nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]);
761     if (!(flags & TNL_F_HDR_CACHE) == !(flags & TNL_F_IPSEC)) {
762         smap_add(args, "header_cache",
763                  flags & TNL_F_HDR_CACHE ? "true" : "false");
764     }
765
766     daddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
767     shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&daddr)));
768
769     if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
770         ovs_be32 saddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
771         shash_add(args, "local_ip", xasprintf(IP_FMT, IP_ARGS(&saddr)));
772     }
773
774     if (!a[OVS_TUNNEL_ATTR_IN_KEY] && !a[OVS_TUNNEL_ATTR_OUT_KEY]) {
775         smap_add(args, "key", "flow");
776     } else {
777         uint64_t in_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_IN_KEY]);
778         uint64_t out_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_OUT_KEY]);
779
780         if (in_key && in_key == out_key) {
781             shash_add(args, "key", xasprintf("%"PRIu64, in_key));
782         } else {
783             if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
784                 smap_add(args, "in_key", "flow");
785             } else if (in_key) {
786                 shash_add(args, "in_key", xasprintf("%"PRIu64, in_key));
787             }
788
789             if (!a[OVS_TUNNEL_ATTR_OUT_KEY]) {
790                 smap_add(args, "out_key", "flow");
791             } else if (out_key) {
792                 shash_add(args, "out_key", xasprintf("%"PRIu64, out_key));
793             }
794         }
795     }
796
797     if (flags & TNL_F_TTL_INHERIT) {
798         smap_add(args, "tos", "inherit");
799     } else if (a[OVS_TUNNEL_ATTR_TTL]) {
800         int ttl = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
801         shash_add(args, "tos", xasprintf("%d", ttl));
802     }
803
804     if (flags & TNL_F_TOS_INHERIT) {
805         smap_add(args, "tos", "inherit");
806     } else if (a[OVS_TUNNEL_ATTR_TOS]) {
807         int tos = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
808         shash_add(args, "tos", xasprintf("%d", tos));
809     }
810
811     if (flags & TNL_F_CSUM) {
812         smap_add(args, "csum", "true");
813     }
814     if (flags & TNL_F_DF_INHERIT) {
815         smap_add(args, "df_inherit", "true");
816     }
817     if (!(flags & TNL_F_DF_DEFAULT)) {
818         smap_add(args, "df_default", "false");
819     }
820     if (!(flags & TNL_F_PMTUD)) {
821         smap_add(args, "pmtud", "false");
822     }
823
824     return 0;
825 }
826
827 static int
828 parse_patch_config(const char *name, const char *type OVS_UNUSED,
829                    const struct shash *args, struct ofpbuf *options)
830 {
831     const char *peer;
832
833     peer = shash_find_data(args, "peer");
834     if (!peer) {
835         VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
836         return EINVAL;
837     }
838
839     if (shash_count(args) > 1) {
840         VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
841         return EINVAL;
842     }
843
844     if (strlen(peer) >= IFNAMSIZ) {
845         VLOG_ERR("%s: patch 'peer' arg too long", name);
846         return EINVAL;
847     }
848
849     if (!strcmp(name, peer)) {
850         VLOG_ERR("%s: patch peer must not be self", name);
851         return EINVAL;
852     }
853
854     nl_msg_put_string(options, OVS_PATCH_ATTR_PEER, peer);
855
856     return 0;
857 }
858
859 static int
860 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
861                      const struct nlattr *options, size_t options_len,
862                      struct shash *args)
863 {
864     static const struct nl_policy ovs_patch_policy[] = {
865         [OVS_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
866                                .max_len = IFNAMSIZ,
867                                .optional = false }
868     };
869
870     struct nlattr *a[ARRAY_SIZE(ovs_patch_policy)];
871     struct ofpbuf buf;
872
873     ofpbuf_use_const(&buf, options, options_len);
874     if (!nl_policy_parse(&buf, 0, ovs_patch_policy,
875                          a, ARRAY_SIZE(ovs_patch_policy))) {
876         return EINVAL;
877     }
878
879     smap_add(args, "peer", nl_attr_get_string(a[OVS_PATCH_ATTR_PEER]));
880     return 0;
881 }
882 \f
883 #define VPORT_FUNCTIONS(GET_STATUS)                         \
884     NULL,                                                   \
885     netdev_vport_run,                                       \
886     netdev_vport_wait,                                      \
887                                                             \
888     netdev_vport_create,                                    \
889     netdev_vport_destroy,                                   \
890     netdev_vport_get_config,                                \
891     netdev_vport_set_config,                                \
892                                                             \
893     netdev_vport_open,                                      \
894     netdev_vport_close,                                     \
895                                                             \
896     NULL,                       /* listen */                \
897     NULL,                       /* recv */                  \
898     NULL,                       /* recv_wait */             \
899     NULL,                       /* drain */                 \
900                                                             \
901     netdev_vport_send,          /* send */                  \
902     NULL,                       /* send_wait */             \
903                                                             \
904     netdev_vport_set_etheraddr,                             \
905     netdev_vport_get_etheraddr,                             \
906     NULL,                       /* get_mtu */               \
907     NULL,                       /* set_mtu */               \
908     NULL,                       /* get_ifindex */           \
909     NULL,                       /* get_carrier */           \
910     NULL,                       /* get_carrier_resets */    \
911     NULL,                       /* get_miimon */            \
912     netdev_vport_get_stats,                                 \
913     netdev_vport_set_stats,                                 \
914                                                             \
915     NULL,                       /* get_features */          \
916     NULL,                       /* set_advertisements */    \
917     NULL,                       /* get_vlan_vid */          \
918                                                             \
919     NULL,                       /* set_policing */          \
920     NULL,                       /* get_qos_types */         \
921     NULL,                       /* get_qos_capabilities */  \
922     NULL,                       /* get_qos */               \
923     NULL,                       /* set_qos */               \
924     NULL,                       /* get_queue */             \
925     NULL,                       /* set_queue */             \
926     NULL,                       /* delete_queue */          \
927     NULL,                       /* get_queue_stats */       \
928     NULL,                       /* dump_queues */           \
929     NULL,                       /* dump_queue_stats */      \
930                                                             \
931     NULL,                       /* get_in4 */               \
932     NULL,                       /* set_in4 */               \
933     NULL,                       /* get_in6 */               \
934     NULL,                       /* add_router */            \
935     NULL,                       /* get_next_hop */          \
936     GET_STATUS,                                             \
937     NULL,                       /* arp_lookup */            \
938                                                             \
939     netdev_vport_update_flags,                              \
940                                                             \
941     netdev_vport_change_seq
942
943 void
944 netdev_vport_register(void)
945 {
946     static const struct vport_class vport_classes[] = {
947         { OVS_VPORT_TYPE_GRE,
948           { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
949           parse_tunnel_config, unparse_tunnel_config },
950
951         { OVS_VPORT_TYPE_GRE,
952           { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
953           parse_tunnel_config, unparse_tunnel_config },
954
955         { OVS_VPORT_TYPE_CAPWAP,
956           { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
957           parse_tunnel_config, unparse_tunnel_config },
958
959         { OVS_VPORT_TYPE_PATCH,
960           { "patch", VPORT_FUNCTIONS(NULL) },
961           parse_patch_config, unparse_patch_config }
962     };
963
964     int i;
965
966     for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
967         netdev_register_provider(&vport_classes[i].netdev_class);
968     }
969 }