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