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