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