netdev-vport: Fix ToS/TTL mixup in unparsing.
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 Nicira, Inc.
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 smap *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 smap *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     ofpbuf_delete(netdev_dev->options);
202     route_table_unregister();
203     free(netdev_dev);
204 }
205
206 static int
207 netdev_vport_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
208 {
209     struct netdev_vport *netdev;
210
211     netdev = xmalloc(sizeof *netdev);
212     netdev_init(&netdev->netdev, netdev_dev_);
213
214     *netdevp = &netdev->netdev;
215     return 0;
216 }
217
218 static void
219 netdev_vport_close(struct netdev *netdev_)
220 {
221     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
222     free(netdev);
223 }
224
225 static int
226 netdev_vport_get_config(struct netdev_dev *dev_, struct smap *args)
227 {
228     const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
229     const struct vport_class *vport_class = vport_class_cast(netdev_class);
230     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
231     const char *name = netdev_dev_get_name(dev_);
232     int error;
233
234     if (!dev->options) {
235         struct dpif_linux_vport reply;
236         struct ofpbuf *buf;
237
238         error = dpif_linux_vport_get(name, &reply, &buf);
239         if (error) {
240             VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
241                         name, strerror(error));
242             return error;
243         }
244
245         dev->options = ofpbuf_clone_data(reply.options, reply.options_len);
246         dev->dp_ifindex = reply.dp_ifindex;
247         dev->port_no = reply.port_no;
248         ofpbuf_delete(buf);
249     }
250
251     error = vport_class->unparse_config(name, netdev_class->type,
252                                         dev->options->data,
253                                         dev->options->size,
254                                         args);
255     if (error) {
256         VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
257                     name, strerror(error));
258     }
259     return error;
260 }
261
262 static int
263 netdev_vport_set_config(struct netdev_dev *dev_, const struct smap *args)
264 {
265     const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
266     const struct vport_class *vport_class = vport_class_cast(netdev_class);
267     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
268     const char *name = netdev_dev_get_name(dev_);
269     struct ofpbuf *options;
270     int error;
271
272     options = ofpbuf_new(64);
273     error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
274                                       args, options);
275     if (!error
276         && (!dev->options
277             || options->size != dev->options->size
278             || memcmp(options->data, dev->options->data, options->size))) {
279         struct dpif_linux_vport vport;
280
281         dpif_linux_vport_init(&vport);
282         vport.cmd = OVS_VPORT_CMD_SET;
283         vport.name = name;
284         vport.options = options->data;
285         vport.options_len = options->size;
286         error = dpif_linux_vport_transact(&vport, NULL, NULL);
287         if (!error || error == ENODEV) {
288             /* Either reconfiguration succeeded or this vport is not installed
289              * in the kernel (e.g. it hasn't been added to a dpif yet with
290              * dpif_port_add()). */
291             ofpbuf_delete(dev->options);
292             dev->options = options;
293             options = NULL;
294             error = 0;
295         }
296     }
297     ofpbuf_delete(options);
298
299     return error;
300 }
301
302 static int
303 netdev_vport_send(struct netdev *netdev, const void *data, size_t size)
304 {
305     struct netdev_dev *dev_ = netdev_get_dev(netdev);
306     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
307
308     if (dev->dp_ifindex == -1) {
309         const char *name = netdev_get_name(netdev);
310         struct dpif_linux_vport reply;
311         struct ofpbuf *buf;
312         int error;
313
314         error = dpif_linux_vport_get(name, &reply, &buf);
315         if (error) {
316             VLOG_ERR_RL(&rl, "%s: failed to query vport for send (%s)",
317                         name, strerror(error));
318             return error;
319         }
320         dev->dp_ifindex = reply.dp_ifindex;
321         dev->port_no = reply.port_no;
322         ofpbuf_delete(buf);
323     }
324
325     return dpif_linux_vport_send(dev->dp_ifindex, dev->port_no, data, size);
326 }
327
328 static int
329 netdev_vport_set_etheraddr(struct netdev *netdev,
330                            const uint8_t mac[ETH_ADDR_LEN])
331 {
332     struct dpif_linux_vport vport;
333     int error;
334
335     dpif_linux_vport_init(&vport);
336     vport.cmd = OVS_VPORT_CMD_SET;
337     vport.name = netdev_get_name(netdev);
338     vport.address = mac;
339
340     error = dpif_linux_vport_transact(&vport, NULL, NULL);
341     if (!error) {
342         netdev_vport_poll_notify(netdev);
343     }
344     return error;
345 }
346
347 static int
348 netdev_vport_get_etheraddr(const struct netdev *netdev,
349                            uint8_t mac[ETH_ADDR_LEN])
350 {
351     struct dpif_linux_vport reply;
352     struct ofpbuf *buf;
353     int error;
354
355     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
356     if (!error) {
357         if (reply.address) {
358             memcpy(mac, reply.address, ETH_ADDR_LEN);
359         } else {
360             error = EOPNOTSUPP;
361         }
362         ofpbuf_delete(buf);
363     }
364     return error;
365 }
366
367 /* Copies 'src' into 'dst', performing format conversion in the process.
368  *
369  * 'src' is allowed to be misaligned. */
370 static void
371 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
372                                   const struct ovs_vport_stats *src)
373 {
374     dst->rx_packets = get_unaligned_u64(&src->rx_packets);
375     dst->tx_packets = get_unaligned_u64(&src->tx_packets);
376     dst->rx_bytes = get_unaligned_u64(&src->rx_bytes);
377     dst->tx_bytes = get_unaligned_u64(&src->tx_bytes);
378     dst->rx_errors = get_unaligned_u64(&src->rx_errors);
379     dst->tx_errors = get_unaligned_u64(&src->tx_errors);
380     dst->rx_dropped = get_unaligned_u64(&src->rx_dropped);
381     dst->tx_dropped = get_unaligned_u64(&src->tx_dropped);
382     dst->multicast = 0;
383     dst->collisions = 0;
384     dst->rx_length_errors = 0;
385     dst->rx_over_errors = 0;
386     dst->rx_crc_errors = 0;
387     dst->rx_frame_errors = 0;
388     dst->rx_fifo_errors = 0;
389     dst->rx_missed_errors = 0;
390     dst->tx_aborted_errors = 0;
391     dst->tx_carrier_errors = 0;
392     dst->tx_fifo_errors = 0;
393     dst->tx_heartbeat_errors = 0;
394     dst->tx_window_errors = 0;
395 }
396
397 /* Copies 'src' into 'dst', performing format conversion in the process. */
398 static void
399 netdev_stats_to_ovs_vport_stats(struct ovs_vport_stats *dst,
400                                 const struct netdev_stats *src)
401 {
402     dst->rx_packets = src->rx_packets;
403     dst->tx_packets = src->tx_packets;
404     dst->rx_bytes = src->rx_bytes;
405     dst->tx_bytes = src->tx_bytes;
406     dst->rx_errors = src->rx_errors;
407     dst->tx_errors = src->tx_errors;
408     dst->rx_dropped = src->rx_dropped;
409     dst->tx_dropped = src->tx_dropped;
410 }
411
412 int
413 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
414 {
415     struct dpif_linux_vport reply;
416     struct ofpbuf *buf;
417     int error;
418
419     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
420     if (error) {
421         return error;
422     } else if (!reply.stats) {
423         ofpbuf_delete(buf);
424         return EOPNOTSUPP;
425     }
426
427     netdev_stats_from_ovs_vport_stats(stats, reply.stats);
428
429     ofpbuf_delete(buf);
430
431     return 0;
432 }
433
434 int
435 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
436 {
437     struct ovs_vport_stats rtnl_stats;
438     struct dpif_linux_vport vport;
439     int err;
440
441     netdev_stats_to_ovs_vport_stats(&rtnl_stats, stats);
442
443     dpif_linux_vport_init(&vport);
444     vport.cmd = OVS_VPORT_CMD_SET;
445     vport.name = netdev_get_name(netdev);
446     vport.stats = &rtnl_stats;
447
448     err = dpif_linux_vport_transact(&vport, NULL, NULL);
449
450     /* If the vport layer doesn't know about the device, that doesn't mean it
451      * doesn't exist (after all were able to open it when netdev_open() was
452      * called), it just means that it isn't attached and we'll be getting
453      * stats a different way. */
454     if (err == ENODEV) {
455         err = EOPNOTSUPP;
456     }
457
458     return err;
459 }
460
461 static int
462 netdev_vport_get_drv_info(const struct netdev *netdev, struct smap *smap)
463 {
464     const char *iface = netdev_vport_get_tnl_iface(netdev);
465
466     if (iface) {
467         struct netdev *egress_netdev;
468
469         smap_add(smap, "tunnel_egress_iface", iface);
470
471         if (!netdev_open(iface, "system", &egress_netdev)) {
472             smap_add(smap, "tunnel_egress_iface_carrier",
473                      netdev_get_carrier(egress_netdev) ? "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 smap *args, const char *name, uint16_t type,
554         struct ofpbuf *options)
555 {
556     const char *s;
557
558     s = smap_get(args, name);
559     if (!s) {
560         s = smap_get(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 smap *args, struct ofpbuf *options)
576 {
577     bool is_gre = false;
578     bool is_ipsec = false;
579     struct smap_node *node;
580     bool ipsec_mech_set = false;
581     ovs_be32 daddr = htonl(0);
582     ovs_be32 saddr = htonl(0);
583     uint32_t flags;
584
585     flags = TNL_F_DF_DEFAULT | TNL_F_PMTUD | TNL_F_HDR_CACHE;
586     if (!strcmp(type, "gre")) {
587         is_gre = true;
588     } else if (!strcmp(type, "ipsec_gre")) {
589         is_gre = true;
590         is_ipsec = true;
591         flags |= TNL_F_IPSEC;
592         flags &= ~TNL_F_HDR_CACHE;
593     }
594
595     SMAP_FOR_EACH (node, args) {
596         if (!strcmp(node->key, "remote_ip")) {
597             struct in_addr in_addr;
598             if (lookup_ip(node->value, &in_addr)) {
599                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
600             } else {
601                 daddr = in_addr.s_addr;
602             }
603         } else if (!strcmp(node->key, "local_ip")) {
604             struct in_addr in_addr;
605             if (lookup_ip(node->value, &in_addr)) {
606                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
607             } else {
608                 saddr = in_addr.s_addr;
609             }
610         } else if (!strcmp(node->key, "tos")) {
611             if (!strcmp(node->value, "inherit")) {
612                 flags |= TNL_F_TOS_INHERIT;
613             } else {
614                 char *endptr;
615                 int tos;
616                 tos = strtol(node->value, &endptr, 0);
617                 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
618                     nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TOS, tos);
619                 } else {
620                     VLOG_WARN("%s: invalid TOS %s", name, node->value);
621                 }
622             }
623         } else if (!strcmp(node->key, "ttl")) {
624             if (!strcmp(node->value, "inherit")) {
625                 flags |= TNL_F_TTL_INHERIT;
626             } else {
627                 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TTL, atoi(node->value));
628             }
629         } else if (!strcmp(node->key, "csum") && is_gre) {
630             if (!strcmp(node->value, "true")) {
631                 flags |= TNL_F_CSUM;
632             }
633         } else if (!strcmp(node->key, "df_inherit")) {
634             if (!strcmp(node->value, "true")) {
635                 flags |= TNL_F_DF_INHERIT;
636             }
637         } else if (!strcmp(node->key, "df_default")) {
638             if (!strcmp(node->value, "false")) {
639                 flags &= ~TNL_F_DF_DEFAULT;
640             }
641         } else if (!strcmp(node->key, "pmtud")) {
642             if (!strcmp(node->value, "false")) {
643                 flags &= ~TNL_F_PMTUD;
644             }
645         } else if (!strcmp(node->key, "header_cache")) {
646             if (!strcmp(node->value, "false")) {
647                 flags &= ~TNL_F_HDR_CACHE;
648             }
649         } else if (!strcmp(node->key, "peer_cert") && is_ipsec) {
650             if (smap_get(args, "certificate")) {
651                 ipsec_mech_set = true;
652             } else {
653                 const char *use_ssl_cert;
654
655                 /* If the "use_ssl_cert" is true, then "certificate" and
656                  * "private_key" will be pulled from the SSL table.  The
657                  * use of this option is strongly discouraged, since it
658                  * will like be removed when multiple SSL configurations
659                  * are supported by OVS.
660                  */
661                 use_ssl_cert = smap_get(args, "use_ssl_cert");
662                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
663                     VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
664                              name);
665                     return EINVAL;
666                 }
667                 ipsec_mech_set = true;
668             }
669         } else if (!strcmp(node->key, "psk") && is_ipsec) {
670             ipsec_mech_set = true;
671         } else if (is_ipsec
672                 && (!strcmp(node->key, "certificate")
673                     || !strcmp(node->key, "private_key")
674                     || !strcmp(node->key, "use_ssl_cert"))) {
675             /* Ignore options not used by the netdev. */
676         } else if (!strcmp(node->key, "key") ||
677                    !strcmp(node->key, "in_key") ||
678                    !strcmp(node->key, "out_key")) {
679             /* Handled separately below. */
680         } else {
681             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
682         }
683     }
684
685     if (is_ipsec) {
686         static pid_t pid = 0;
687         if (pid <= 0) {
688             char *file_name = xasprintf("%s/%s", ovs_rundir(),
689                                         "ovs-monitor-ipsec.pid");
690             pid = read_pidfile(file_name);
691             free(file_name);
692         }
693
694         if (pid < 0) {
695             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
696                      name);
697             return EINVAL;
698         }
699
700         if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
701             VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
702             return EINVAL;
703         }
704
705         if (!ipsec_mech_set) {
706             VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
707                      name);
708             return EINVAL;
709         }
710     }
711
712     set_key(args, "in_key", OVS_TUNNEL_ATTR_IN_KEY, options);
713     set_key(args, "out_key", OVS_TUNNEL_ATTR_OUT_KEY, options);
714
715     if (!daddr) {
716         VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
717                  name, type);
718         return EINVAL;
719     }
720     nl_msg_put_be32(options, OVS_TUNNEL_ATTR_DST_IPV4, daddr);
721
722     if (saddr) {
723         if (ip_is_multicast(daddr)) {
724             VLOG_WARN("%s: remote_ip is multicast, ignoring local_ip", name);
725         } else {
726             nl_msg_put_be32(options, OVS_TUNNEL_ATTR_SRC_IPV4, saddr);
727         }
728     }
729
730     nl_msg_put_u32(options, OVS_TUNNEL_ATTR_FLAGS, flags);
731
732     return 0;
733 }
734
735 static int
736 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
737                             struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1])
738 {
739     static const struct nl_policy ovs_tunnel_policy[] = {
740         [OVS_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
741         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
742         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
743         [OVS_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
744         [OVS_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
745         [OVS_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
746         [OVS_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
747     };
748     struct ofpbuf buf;
749
750     ofpbuf_use_const(&buf, options, options_len);
751     if (!nl_policy_parse(&buf, 0, ovs_tunnel_policy,
752                          a, ARRAY_SIZE(ovs_tunnel_policy))) {
753         return EINVAL;
754     }
755     return 0;
756 }
757
758 static uint64_t
759 get_be64_or_zero(const struct nlattr *a)
760 {
761     return a ? ntohll(nl_attr_get_be64(a)) : 0;
762 }
763
764 static int
765 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
766                       const struct nlattr *options, size_t options_len,
767                       struct smap *args)
768 {
769     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
770     ovs_be32 daddr;
771     uint32_t flags;
772     int error;
773
774     error = tnl_port_config_from_nlattr(options, options_len, a);
775     if (error) {
776         return error;
777     }
778
779     flags = nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]);
780     if (!(flags & TNL_F_HDR_CACHE) == !(flags & TNL_F_IPSEC)) {
781         smap_add(args, "header_cache",
782                  flags & TNL_F_HDR_CACHE ? "true" : "false");
783     }
784
785     daddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
786     smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(&daddr));
787
788     if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
789         ovs_be32 saddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
790         smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(&saddr));
791     }
792
793     if (!a[OVS_TUNNEL_ATTR_IN_KEY] && !a[OVS_TUNNEL_ATTR_OUT_KEY]) {
794         smap_add(args, "key", "flow");
795     } else {
796         uint64_t in_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_IN_KEY]);
797         uint64_t out_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_OUT_KEY]);
798
799         if (in_key && in_key == out_key) {
800             smap_add_format(args, "key", "%"PRIu64, in_key);
801         } else {
802             if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
803                 smap_add(args, "in_key", "flow");
804             } else if (in_key) {
805                 smap_add_format(args, "in_key", "%"PRIu64, in_key);
806             }
807
808             if (!a[OVS_TUNNEL_ATTR_OUT_KEY]) {
809                 smap_add(args, "out_key", "flow");
810             } else if (out_key) {
811                 smap_add_format(args, "out_key", "%"PRIu64, out_key);
812             }
813         }
814     }
815
816     if (flags & TNL_F_TTL_INHERIT) {
817         smap_add(args, "ttl", "inherit");
818     } else if (a[OVS_TUNNEL_ATTR_TTL]) {
819         int ttl = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
820         smap_add_format(args, "ttl", "%d", ttl);
821     }
822
823     if (flags & TNL_F_TOS_INHERIT) {
824         smap_add(args, "tos", "inherit");
825     } else if (a[OVS_TUNNEL_ATTR_TOS]) {
826         int tos = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
827         smap_add_format(args, "tos", "0x%x", tos);
828     }
829
830     if (flags & TNL_F_CSUM) {
831         smap_add(args, "csum", "true");
832     }
833     if (flags & TNL_F_DF_INHERIT) {
834         smap_add(args, "df_inherit", "true");
835     }
836     if (!(flags & TNL_F_DF_DEFAULT)) {
837         smap_add(args, "df_default", "false");
838     }
839     if (!(flags & TNL_F_PMTUD)) {
840         smap_add(args, "pmtud", "false");
841     }
842
843     return 0;
844 }
845
846 static int
847 parse_patch_config(const char *name, const char *type OVS_UNUSED,
848                    const struct smap *args, struct ofpbuf *options)
849 {
850     const char *peer;
851
852     peer = smap_get(args, "peer");
853     if (!peer) {
854         VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
855         return EINVAL;
856     }
857
858     if (smap_count(args) > 1) {
859         VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
860         return EINVAL;
861     }
862
863     if (strlen(peer) >= IFNAMSIZ) {
864         VLOG_ERR("%s: patch 'peer' arg too long", name);
865         return EINVAL;
866     }
867
868     if (!strcmp(name, peer)) {
869         VLOG_ERR("%s: patch peer must not be self", name);
870         return EINVAL;
871     }
872
873     nl_msg_put_string(options, OVS_PATCH_ATTR_PEER, peer);
874
875     return 0;
876 }
877
878 static int
879 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
880                      const struct nlattr *options, size_t options_len,
881                      struct smap *args)
882 {
883     static const struct nl_policy ovs_patch_policy[] = {
884         [OVS_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
885                                .max_len = IFNAMSIZ,
886                                .optional = false }
887     };
888
889     struct nlattr *a[ARRAY_SIZE(ovs_patch_policy)];
890     struct ofpbuf buf;
891
892     ofpbuf_use_const(&buf, options, options_len);
893     if (!nl_policy_parse(&buf, 0, ovs_patch_policy,
894                          a, ARRAY_SIZE(ovs_patch_policy))) {
895         return EINVAL;
896     }
897
898     smap_add(args, "peer", nl_attr_get_string(a[OVS_PATCH_ATTR_PEER]));
899     return 0;
900 }
901 \f
902 #define VPORT_FUNCTIONS(GET_STATUS)                         \
903     NULL,                                                   \
904     netdev_vport_run,                                       \
905     netdev_vport_wait,                                      \
906                                                             \
907     netdev_vport_create,                                    \
908     netdev_vport_destroy,                                   \
909     netdev_vport_get_config,                                \
910     netdev_vport_set_config,                                \
911                                                             \
912     netdev_vport_open,                                      \
913     netdev_vport_close,                                     \
914                                                             \
915     NULL,                       /* listen */                \
916     NULL,                       /* recv */                  \
917     NULL,                       /* recv_wait */             \
918     NULL,                       /* drain */                 \
919                                                             \
920     netdev_vport_send,          /* send */                  \
921     NULL,                       /* send_wait */             \
922                                                             \
923     netdev_vport_set_etheraddr,                             \
924     netdev_vport_get_etheraddr,                             \
925     NULL,                       /* get_mtu */               \
926     NULL,                       /* set_mtu */               \
927     NULL,                       /* get_ifindex */           \
928     NULL,                       /* get_carrier */           \
929     NULL,                       /* get_carrier_resets */    \
930     NULL,                       /* get_miimon */            \
931     netdev_vport_get_stats,                                 \
932     netdev_vport_set_stats,                                 \
933                                                             \
934     NULL,                       /* get_features */          \
935     NULL,                       /* set_advertisements */    \
936                                                             \
937     NULL,                       /* set_policing */          \
938     NULL,                       /* get_qos_types */         \
939     NULL,                       /* get_qos_capabilities */  \
940     NULL,                       /* get_qos */               \
941     NULL,                       /* set_qos */               \
942     NULL,                       /* get_queue */             \
943     NULL,                       /* set_queue */             \
944     NULL,                       /* delete_queue */          \
945     NULL,                       /* get_queue_stats */       \
946     NULL,                       /* dump_queues */           \
947     NULL,                       /* dump_queue_stats */      \
948                                                             \
949     NULL,                       /* get_in4 */               \
950     NULL,                       /* set_in4 */               \
951     NULL,                       /* get_in6 */               \
952     NULL,                       /* add_router */            \
953     NULL,                       /* get_next_hop */          \
954     GET_STATUS,                                             \
955     NULL,                       /* arp_lookup */            \
956                                                             \
957     netdev_vport_update_flags,                              \
958                                                             \
959     netdev_vport_change_seq
960
961 void
962 netdev_vport_register(void)
963 {
964     static const struct vport_class vport_classes[] = {
965         { OVS_VPORT_TYPE_GRE,
966           { "gre", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
967           parse_tunnel_config, unparse_tunnel_config },
968
969         { OVS_VPORT_TYPE_GRE,
970           { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
971           parse_tunnel_config, unparse_tunnel_config },
972
973         { OVS_VPORT_TYPE_CAPWAP,
974           { "capwap", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
975           parse_tunnel_config, unparse_tunnel_config },
976
977         { OVS_VPORT_TYPE_PATCH,
978           { "patch", VPORT_FUNCTIONS(NULL) },
979           parse_patch_config, unparse_patch_config }
980     };
981
982     int i;
983
984     for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
985         netdev_register_provider(&vport_classes[i].netdev_class);
986     }
987 }