2234de78150c6fb47b7c9f2a756ff0f63c50b662
[cascardo/ovs.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013, 2014 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 <net/if.h>
25 #include <netinet/ip6.h>
26 #include <sys/ioctl.h>
27
28 #include "byte-order.h"
29 #include "csum.h"
30 #include "daemon.h"
31 #include "dirs.h"
32 #include "dpif.h"
33 #include "dp-packet.h"
34 #include "dynamic-string.h"
35 #include "flow.h"
36 #include "hash.h"
37 #include "hmap.h"
38 #include "list.h"
39 #include "netdev-provider.h"
40 #include "odp-netlink.h"
41 #include "dp-packet.h"
42 #include "ovs-router.h"
43 #include "packets.h"
44 #include "poll-loop.h"
45 #include "route-table.h"
46 #include "shash.h"
47 #include "socket-util.h"
48 #include "openvswitch/vlog.h"
49 #include "unaligned.h"
50 #include "unixctl.h"
51 #include "util.h"
52
53 VLOG_DEFINE_THIS_MODULE(netdev_vport);
54 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(60, 5);
55
56 #define GENEVE_DST_PORT 6081
57 #define VXLAN_DST_PORT 4789
58 #define LISP_DST_PORT 4341
59 #define STT_DST_PORT 7471
60
61 #define VXLAN_HLEN   (sizeof(struct udp_header) +         \
62                       sizeof(struct vxlanhdr))
63
64 #define GENEVE_BASE_HLEN   (sizeof(struct udp_header) +         \
65                             sizeof(struct genevehdr))
66
67 #define DEFAULT_TTL 64
68
69 struct netdev_vport {
70     struct netdev up;
71
72     /* Protects all members below. */
73     struct ovs_mutex mutex;
74
75     struct eth_addr etheraddr;
76     struct netdev_stats stats;
77
78     /* Tunnels. */
79     struct netdev_tunnel_config tnl_cfg;
80     char egress_iface[IFNAMSIZ];
81     bool carrier_status;
82
83     /* Patch Ports. */
84     char *peer;
85 };
86
87 struct vport_class {
88     const char *dpif_port;
89     struct netdev_class netdev_class;
90 };
91
92 /* Last read of the route-table's change number. */
93 static uint64_t rt_change_seqno;
94
95 static int netdev_vport_construct(struct netdev *);
96 static int get_patch_config(const struct netdev *netdev, struct smap *args);
97 static int get_tunnel_config(const struct netdev *, struct smap *args);
98 static bool tunnel_check_status_change__(struct netdev_vport *);
99
100 static uint16_t tnl_udp_port_min = 32768;
101 static uint16_t tnl_udp_port_max = 61000;
102
103 static bool
104 is_vport_class(const struct netdev_class *class)
105 {
106     return class->construct == netdev_vport_construct;
107 }
108
109 bool
110 netdev_vport_is_vport_class(const struct netdev_class *class)
111 {
112     return is_vport_class(class);
113 }
114
115 static const struct vport_class *
116 vport_class_cast(const struct netdev_class *class)
117 {
118     ovs_assert(is_vport_class(class));
119     return CONTAINER_OF(class, struct vport_class, netdev_class);
120 }
121
122 static struct netdev_vport *
123 netdev_vport_cast(const struct netdev *netdev)
124 {
125     ovs_assert(is_vport_class(netdev_get_class(netdev)));
126     return CONTAINER_OF(netdev, struct netdev_vport, up);
127 }
128
129 static const struct netdev_tunnel_config *
130 get_netdev_tunnel_config(const struct netdev *netdev)
131 {
132     return &netdev_vport_cast(netdev)->tnl_cfg;
133 }
134
135 bool
136 netdev_vport_is_patch(const struct netdev *netdev)
137 {
138     const struct netdev_class *class = netdev_get_class(netdev);
139
140     return class->get_config == get_patch_config;
141 }
142
143 bool
144 netdev_vport_is_layer3(const struct netdev *dev)
145 {
146     const char *type = netdev_get_type(dev);
147
148     return (!strcmp("lisp", type));
149 }
150
151 static bool
152 netdev_vport_needs_dst_port(const struct netdev *dev)
153 {
154     const struct netdev_class *class = netdev_get_class(dev);
155     const char *type = netdev_get_type(dev);
156
157     return (class->get_config == get_tunnel_config &&
158             (!strcmp("geneve", type) || !strcmp("vxlan", type) ||
159              !strcmp("lisp", type) || !strcmp("stt", type)) );
160 }
161
162 const char *
163 netdev_vport_class_get_dpif_port(const struct netdev_class *class)
164 {
165     return is_vport_class(class) ? vport_class_cast(class)->dpif_port : NULL;
166 }
167
168 const char *
169 netdev_vport_get_dpif_port(const struct netdev *netdev,
170                            char namebuf[], size_t bufsize)
171 {
172     const struct netdev_class *class = netdev_get_class(netdev);
173     const char *dpif_port = netdev_vport_class_get_dpif_port(class);
174
175     if (!dpif_port) {
176         return netdev_get_name(netdev);
177     }
178
179     if (netdev_vport_needs_dst_port(netdev)) {
180         const struct netdev_vport *vport = netdev_vport_cast(netdev);
181
182         /*
183          * Note: IFNAMSIZ is 16 bytes long. Implementations should choose
184          * a dpif port name that is short enough to fit including any
185          * port numbers but assert just in case.
186          */
187         BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
188         ovs_assert(strlen(dpif_port) + 6 < IFNAMSIZ);
189         snprintf(namebuf, bufsize, "%s_%d", dpif_port,
190                  ntohs(vport->tnl_cfg.dst_port));
191         return namebuf;
192     } else {
193         return dpif_port;
194     }
195 }
196
197 char *
198 netdev_vport_get_dpif_port_strdup(const struct netdev *netdev)
199 {
200     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
201
202     return xstrdup(netdev_vport_get_dpif_port(netdev, namebuf,
203                                               sizeof namebuf));
204 }
205
206 /* Whenever the route-table change number is incremented,
207  * netdev_vport_route_changed() should be called to update
208  * the corresponding tunnel interface status. */
209 static void
210 netdev_vport_route_changed(void)
211 {
212     struct netdev **vports;
213     size_t i, n_vports;
214
215     vports = netdev_get_vports(&n_vports);
216     for (i = 0; i < n_vports; i++) {
217         struct netdev *netdev_ = vports[i];
218         struct netdev_vport *netdev = netdev_vport_cast(netdev_);
219
220         ovs_mutex_lock(&netdev->mutex);
221         /* Finds all tunnel vports. */
222         if (ipv6_addr_is_set(&netdev->tnl_cfg.ipv6_dst)) {
223             if (tunnel_check_status_change__(netdev)) {
224                 netdev_change_seq_changed(netdev_);
225             }
226         }
227         ovs_mutex_unlock(&netdev->mutex);
228
229         netdev_close(netdev_);
230     }
231
232     free(vports);
233 }
234
235 static struct netdev *
236 netdev_vport_alloc(void)
237 {
238     struct netdev_vport *netdev = xzalloc(sizeof *netdev);
239     return &netdev->up;
240 }
241
242 static int
243 netdev_vport_construct(struct netdev *netdev_)
244 {
245     struct netdev_vport *dev = netdev_vport_cast(netdev_);
246     const char *type = netdev_get_type(netdev_);
247
248     ovs_mutex_init(&dev->mutex);
249     eth_addr_random(&dev->etheraddr);
250
251     /* Add a default destination port for tunnel ports if none specified. */
252     if (!strcmp(type, "geneve")) {
253         dev->tnl_cfg.dst_port = htons(GENEVE_DST_PORT);
254     } else if (!strcmp(type, "vxlan")) {
255         dev->tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
256     } else if (!strcmp(type, "lisp")) {
257         dev->tnl_cfg.dst_port = htons(LISP_DST_PORT);
258     } else if (!strcmp(type, "stt")) {
259         dev->tnl_cfg.dst_port = htons(STT_DST_PORT);
260     }
261
262     dev->tnl_cfg.dont_fragment = true;
263     dev->tnl_cfg.ttl = DEFAULT_TTL;
264     return 0;
265 }
266
267 static void
268 netdev_vport_destruct(struct netdev *netdev_)
269 {
270     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
271
272     free(netdev->peer);
273     ovs_mutex_destroy(&netdev->mutex);
274 }
275
276 static void
277 netdev_vport_dealloc(struct netdev *netdev_)
278 {
279     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
280     free(netdev);
281 }
282
283 static int
284 netdev_vport_set_etheraddr(struct netdev *netdev_, const struct eth_addr mac)
285 {
286     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
287
288     ovs_mutex_lock(&netdev->mutex);
289     netdev->etheraddr = mac;
290     ovs_mutex_unlock(&netdev->mutex);
291     netdev_change_seq_changed(netdev_);
292
293     return 0;
294 }
295
296 static int
297 netdev_vport_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac)
298 {
299     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
300
301     ovs_mutex_lock(&netdev->mutex);
302     *mac = netdev->etheraddr;
303     ovs_mutex_unlock(&netdev->mutex);
304
305     return 0;
306 }
307
308 /* Checks if the tunnel status has changed and returns a boolean.
309  * Updates the tunnel status if it has changed. */
310 static bool
311 tunnel_check_status_change__(struct netdev_vport *netdev)
312     OVS_REQUIRES(netdev->mutex)
313 {
314     char iface[IFNAMSIZ];
315     bool status = false;
316     struct in6_addr *route;
317     struct in6_addr gw;
318
319     iface[0] = '\0';
320     route = &netdev->tnl_cfg.ipv6_dst;
321     if (ovs_router_lookup(route, iface, &gw)) {
322         struct netdev *egress_netdev;
323
324         if (!netdev_open(iface, "system", &egress_netdev)) {
325             status = netdev_get_carrier(egress_netdev);
326             netdev_close(egress_netdev);
327         }
328     }
329
330     if (strcmp(netdev->egress_iface, iface)
331         || netdev->carrier_status != status) {
332         ovs_strlcpy(netdev->egress_iface, iface, IFNAMSIZ);
333         netdev->carrier_status = status;
334
335         return true;
336     }
337
338     return false;
339 }
340
341 static int
342 tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
343 {
344     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
345
346     if (netdev->egress_iface[0]) {
347         smap_add(smap, "tunnel_egress_iface", netdev->egress_iface);
348
349         smap_add(smap, "tunnel_egress_iface_carrier",
350                  netdev->carrier_status ? "up" : "down");
351     }
352
353     return 0;
354 }
355
356 static int
357 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
358                           enum netdev_flags off,
359                           enum netdev_flags on OVS_UNUSED,
360                           enum netdev_flags *old_flagsp)
361 {
362     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
363         return EOPNOTSUPP;
364     }
365
366     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
367     return 0;
368 }
369
370 static void
371 netdev_vport_run(void)
372 {
373     uint64_t seq;
374
375     route_table_run();
376     seq = route_table_get_change_seq();
377     if (rt_change_seqno != seq) {
378         rt_change_seqno = seq;
379         netdev_vport_route_changed();
380     }
381 }
382
383 static void
384 netdev_vport_wait(void)
385 {
386     uint64_t seq;
387
388     route_table_wait();
389     seq = route_table_get_change_seq();
390     if (rt_change_seqno != seq) {
391         poll_immediate_wake();
392     }
393 }
394 \f
395 /* Code specific to tunnel types. */
396
397 static ovs_be64
398 parse_key(const struct smap *args, const char *name,
399           bool *present, bool *flow)
400 {
401     const char *s;
402
403     *present = false;
404     *flow = false;
405
406     s = smap_get(args, name);
407     if (!s) {
408         s = smap_get(args, "key");
409         if (!s) {
410             return 0;
411         }
412     }
413
414     *present = true;
415
416     if (!strcmp(s, "flow")) {
417         *flow = true;
418         return 0;
419     } else {
420         return htonll(strtoull(s, NULL, 0));
421     }
422 }
423
424 static int
425 parse_tunnel_ip(const char *value, bool accept_mcast, bool *flow,
426                 struct in6_addr *ipv6, uint16_t *protocol)
427 {
428     if (!strcmp(value, "flow")) {
429         *flow = true;
430         *protocol = 0;
431         return 0;
432     }
433     if (addr_is_ipv6(value)) {
434         if (lookup_ipv6(value, ipv6)) {
435             return ENOENT;
436         }
437         if (!accept_mcast && ipv6_addr_is_multicast(ipv6)) {
438             return EINVAL;
439         }
440         *protocol = ETH_TYPE_IPV6;
441     } else {
442         struct in_addr ip;
443         if (lookup_ip(value, &ip)) {
444             return ENOENT;
445         }
446         if (!accept_mcast && ip_is_multicast(ip.s_addr)) {
447             return EINVAL;
448         }
449         in6_addr_set_mapped_ipv4(ipv6, ip.s_addr);
450         *protocol = ETH_TYPE_IP;
451     }
452     return 0;
453 }
454
455 static int
456 set_tunnel_config(struct netdev *dev_, const struct smap *args)
457 {
458     struct netdev_vport *dev = netdev_vport_cast(dev_);
459     const char *name = netdev_get_name(dev_);
460     const char *type = netdev_get_type(dev_);
461     bool ipsec_mech_set, needs_dst_port, has_csum;
462     uint16_t dst_proto = 0, src_proto = 0;
463     struct netdev_tunnel_config tnl_cfg;
464     struct smap_node *node;
465
466     has_csum = strstr(type, "gre") || strstr(type, "geneve") ||
467                strstr(type, "stt") || strstr(type, "vxlan");
468     ipsec_mech_set = false;
469     memset(&tnl_cfg, 0, sizeof tnl_cfg);
470
471     /* Add a default destination port for tunnel ports if none specified. */
472     if (!strcmp(type, "geneve")) {
473         tnl_cfg.dst_port = htons(GENEVE_DST_PORT);
474     }
475
476     if (!strcmp(type, "vxlan")) {
477         tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
478     }
479
480     if (!strcmp(type, "lisp")) {
481         tnl_cfg.dst_port = htons(LISP_DST_PORT);
482     }
483
484     if (!strcmp(type, "stt")) {
485         tnl_cfg.dst_port = htons(STT_DST_PORT);
486     }
487
488     needs_dst_port = netdev_vport_needs_dst_port(dev_);
489     tnl_cfg.ipsec = strstr(type, "ipsec");
490     tnl_cfg.dont_fragment = true;
491
492     SMAP_FOR_EACH (node, args) {
493         if (!strcmp(node->key, "remote_ip")) {
494             int err;
495             err = parse_tunnel_ip(node->value, false, &tnl_cfg.ip_dst_flow,
496                                   &tnl_cfg.ipv6_dst, &dst_proto);
497             switch (err) {
498             case ENOENT:
499                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
500                 break;
501             case EINVAL:
502                 VLOG_WARN("%s: multicast remote_ip=%s not allowed",
503                           name, node->value);
504                 return EINVAL;
505             }
506             if (dst_proto == ETH_TYPE_IPV6) {
507                 VLOG_WARN("%s: IPv6 'remote_ip' is not supported", name);
508                 return EOPNOTSUPP;
509             }
510         } else if (!strcmp(node->key, "local_ip")) {
511             int err;
512             err = parse_tunnel_ip(node->value, true, &tnl_cfg.ip_src_flow,
513                                   &tnl_cfg.ipv6_src, &src_proto);
514             switch (err) {
515             case ENOENT:
516                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
517                 break;
518             }
519             if (src_proto == ETH_TYPE_IPV6) {
520                 VLOG_WARN("%s: IPv6 'local_ip' is not supported", name);
521                 return EOPNOTSUPP;
522             }
523         } else if (!strcmp(node->key, "tos")) {
524             if (!strcmp(node->value, "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                     tnl_cfg.tos = tos;
532                 } else {
533                     VLOG_WARN("%s: invalid TOS %s", name, node->value);
534                 }
535             }
536         } else if (!strcmp(node->key, "ttl")) {
537             if (!strcmp(node->value, "inherit")) {
538                 tnl_cfg.ttl_inherit = true;
539             } else {
540                 tnl_cfg.ttl = atoi(node->value);
541             }
542         } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
543             tnl_cfg.dst_port = htons(atoi(node->value));
544         } else if (!strcmp(node->key, "csum") && has_csum) {
545             if (!strcmp(node->value, "true")) {
546                 tnl_cfg.csum = true;
547             }
548         } else if (!strcmp(node->key, "df_default")) {
549             if (!strcmp(node->value, "false")) {
550                 tnl_cfg.dont_fragment = false;
551             }
552         } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
553             if (smap_get(args, "certificate")) {
554                 ipsec_mech_set = true;
555             } else {
556                 const char *use_ssl_cert;
557
558                 /* If the "use_ssl_cert" is true, then "certificate" and
559                  * "private_key" will be pulled from the SSL table.  The
560                  * use of this option is strongly discouraged, since it
561                  * will like be removed when multiple SSL configurations
562                  * are supported by OVS.
563                  */
564                 use_ssl_cert = smap_get(args, "use_ssl_cert");
565                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
566                     VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
567                              name);
568                     return EINVAL;
569                 }
570                 ipsec_mech_set = true;
571             }
572         } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
573             ipsec_mech_set = true;
574         } else if (tnl_cfg.ipsec
575                 && (!strcmp(node->key, "certificate")
576                     || !strcmp(node->key, "private_key")
577                     || !strcmp(node->key, "use_ssl_cert"))) {
578             /* Ignore options not used by the netdev. */
579         } else if (!strcmp(node->key, "key") ||
580                    !strcmp(node->key, "in_key") ||
581                    !strcmp(node->key, "out_key")) {
582             /* Handled separately below. */
583         } else if (!strcmp(node->key, "exts")) {
584             char *str = xstrdup(node->value);
585             char *ext, *save_ptr = NULL;
586
587             tnl_cfg.exts = 0;
588
589             ext = strtok_r(str, ",", &save_ptr);
590             while (ext) {
591                 if (!strcmp(type, "vxlan") && !strcmp(ext, "gbp")) {
592                     tnl_cfg.exts |= (1 << OVS_VXLAN_EXT_GBP);
593                 } else {
594                     VLOG_WARN("%s: unknown extension '%s'", name, ext);
595                 }
596
597                 ext = strtok_r(NULL, ",", &save_ptr);
598             }
599
600             free(str);
601         } else {
602             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
603         }
604     }
605
606     if (tnl_cfg.ipsec) {
607         static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
608         static pid_t pid = 0;
609
610 #ifndef _WIN32
611         ovs_mutex_lock(&mutex);
612         if (pid <= 0) {
613             char *file_name = xasprintf("%s/%s", ovs_rundir(),
614                                         "ovs-monitor-ipsec.pid");
615             pid = read_pidfile(file_name);
616             free(file_name);
617         }
618         ovs_mutex_unlock(&mutex);
619 #endif
620
621         if (pid < 0) {
622             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
623                      name);
624             return EINVAL;
625         }
626
627         if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
628             VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
629             return EINVAL;
630         }
631
632         if (!ipsec_mech_set) {
633             VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
634                      name);
635             return EINVAL;
636         }
637     }
638
639     if (!ipv6_addr_is_set(&tnl_cfg.ipv6_dst) && !tnl_cfg.ip_dst_flow) {
640         VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
641                  name, type);
642         return EINVAL;
643     }
644     if (tnl_cfg.ip_src_flow && !tnl_cfg.ip_dst_flow) {
645         VLOG_ERR("%s: %s type requires 'remote_ip=flow' with 'local_ip=flow'",
646                  name, type);
647         return EINVAL;
648     }
649     if (src_proto && dst_proto && src_proto != dst_proto) {
650         VLOG_ERR("%s: 'remote_ip' and 'local_ip' has to be of the same address family",
651                  name);
652         return EINVAL;
653     }
654     if (!tnl_cfg.ttl) {
655         tnl_cfg.ttl = DEFAULT_TTL;
656     }
657
658     tnl_cfg.in_key = parse_key(args, "in_key",
659                                &tnl_cfg.in_key_present,
660                                &tnl_cfg.in_key_flow);
661
662     tnl_cfg.out_key = parse_key(args, "out_key",
663                                &tnl_cfg.out_key_present,
664                                &tnl_cfg.out_key_flow);
665
666     ovs_mutex_lock(&dev->mutex);
667     if (memcmp(&dev->tnl_cfg, &tnl_cfg, sizeof tnl_cfg)) {
668         dev->tnl_cfg = tnl_cfg;
669         tunnel_check_status_change__(dev);
670         netdev_change_seq_changed(dev_);
671     }
672     ovs_mutex_unlock(&dev->mutex);
673
674     return 0;
675 }
676
677 static int
678 get_tunnel_config(const struct netdev *dev, struct smap *args)
679 {
680     struct netdev_vport *netdev = netdev_vport_cast(dev);
681     struct netdev_tunnel_config tnl_cfg;
682
683     ovs_mutex_lock(&netdev->mutex);
684     tnl_cfg = netdev->tnl_cfg;
685     ovs_mutex_unlock(&netdev->mutex);
686
687     if (ipv6_addr_is_set(&tnl_cfg.ipv6_dst)) {
688         smap_add_ipv6(args, "remote_ip", &tnl_cfg.ipv6_dst);
689     } else if (tnl_cfg.ip_dst_flow) {
690         smap_add(args, "remote_ip", "flow");
691     }
692
693     if (ipv6_addr_is_set(&tnl_cfg.ipv6_src)) {
694         smap_add_ipv6(args, "local_ip", &tnl_cfg.ipv6_src);
695     } else if (tnl_cfg.ip_src_flow) {
696         smap_add(args, "local_ip", "flow");
697     }
698
699     if (tnl_cfg.in_key_flow && tnl_cfg.out_key_flow) {
700         smap_add(args, "key", "flow");
701     } else if (tnl_cfg.in_key_present && tnl_cfg.out_key_present
702                && tnl_cfg.in_key == tnl_cfg.out_key) {
703         smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg.in_key));
704     } else {
705         if (tnl_cfg.in_key_flow) {
706             smap_add(args, "in_key", "flow");
707         } else if (tnl_cfg.in_key_present) {
708             smap_add_format(args, "in_key", "%"PRIu64,
709                             ntohll(tnl_cfg.in_key));
710         }
711
712         if (tnl_cfg.out_key_flow) {
713             smap_add(args, "out_key", "flow");
714         } else if (tnl_cfg.out_key_present) {
715             smap_add_format(args, "out_key", "%"PRIu64,
716                             ntohll(tnl_cfg.out_key));
717         }
718     }
719
720     if (tnl_cfg.ttl_inherit) {
721         smap_add(args, "ttl", "inherit");
722     } else if (tnl_cfg.ttl != DEFAULT_TTL) {
723         smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg.ttl);
724     }
725
726     if (tnl_cfg.tos_inherit) {
727         smap_add(args, "tos", "inherit");
728     } else if (tnl_cfg.tos) {
729         smap_add_format(args, "tos", "0x%x", tnl_cfg.tos);
730     }
731
732     if (tnl_cfg.dst_port) {
733         uint16_t dst_port = ntohs(tnl_cfg.dst_port);
734         const char *type = netdev_get_type(dev);
735
736         if ((!strcmp("geneve", type) && dst_port != GENEVE_DST_PORT) ||
737             (!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
738             (!strcmp("lisp", type) && dst_port != LISP_DST_PORT) ||
739             (!strcmp("stt", type) && dst_port != STT_DST_PORT)) {
740             smap_add_format(args, "dst_port", "%d", dst_port);
741         }
742     }
743
744     if (tnl_cfg.csum) {
745         smap_add(args, "csum", "true");
746     }
747
748     if (!tnl_cfg.dont_fragment) {
749         smap_add(args, "df_default", "false");
750     }
751
752     return 0;
753 }
754 \f
755 /* Code specific to patch ports. */
756
757 /* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
758  * string that the caller must free.
759  *
760  * If 'netdev' is not a patch port, returns NULL. */
761 char *
762 netdev_vport_patch_peer(const struct netdev *netdev_)
763 {
764     char *peer = NULL;
765
766     if (netdev_vport_is_patch(netdev_)) {
767         struct netdev_vport *netdev = netdev_vport_cast(netdev_);
768
769         ovs_mutex_lock(&netdev->mutex);
770         if (netdev->peer) {
771             peer = xstrdup(netdev->peer);
772         }
773         ovs_mutex_unlock(&netdev->mutex);
774     }
775
776     return peer;
777 }
778
779 void
780 netdev_vport_inc_rx(const struct netdev *netdev,
781                     const struct dpif_flow_stats *stats)
782 {
783     if (is_vport_class(netdev_get_class(netdev))) {
784         struct netdev_vport *dev = netdev_vport_cast(netdev);
785
786         ovs_mutex_lock(&dev->mutex);
787         dev->stats.rx_packets += stats->n_packets;
788         dev->stats.rx_bytes += stats->n_bytes;
789         ovs_mutex_unlock(&dev->mutex);
790     }
791 }
792
793 void
794 netdev_vport_inc_tx(const struct netdev *netdev,
795                     const struct dpif_flow_stats *stats)
796 {
797     if (is_vport_class(netdev_get_class(netdev))) {
798         struct netdev_vport *dev = netdev_vport_cast(netdev);
799
800         ovs_mutex_lock(&dev->mutex);
801         dev->stats.tx_packets += stats->n_packets;
802         dev->stats.tx_bytes += stats->n_bytes;
803         ovs_mutex_unlock(&dev->mutex);
804     }
805 }
806
807 static int
808 get_patch_config(const struct netdev *dev_, struct smap *args)
809 {
810     struct netdev_vport *dev = netdev_vport_cast(dev_);
811
812     ovs_mutex_lock(&dev->mutex);
813     if (dev->peer) {
814         smap_add(args, "peer", dev->peer);
815     }
816     ovs_mutex_unlock(&dev->mutex);
817
818     return 0;
819 }
820
821 static int
822 set_patch_config(struct netdev *dev_, const struct smap *args)
823 {
824     struct netdev_vport *dev = netdev_vport_cast(dev_);
825     const char *name = netdev_get_name(dev_);
826     const char *peer;
827
828     peer = smap_get(args, "peer");
829     if (!peer) {
830         VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
831         return EINVAL;
832     }
833
834     if (smap_count(args) > 1) {
835         VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
836         return EINVAL;
837     }
838
839     if (!strcmp(name, peer)) {
840         VLOG_ERR("%s: patch peer must not be self", name);
841         return EINVAL;
842     }
843
844     ovs_mutex_lock(&dev->mutex);
845     if (!dev->peer || strcmp(dev->peer, peer)) {
846         free(dev->peer);
847         dev->peer = xstrdup(peer);
848         netdev_change_seq_changed(dev_);
849     }
850     ovs_mutex_unlock(&dev->mutex);
851
852     return 0;
853 }
854
855 static int
856 get_stats(const struct netdev *netdev, struct netdev_stats *stats)
857 {
858     struct netdev_vport *dev = netdev_vport_cast(netdev);
859
860     ovs_mutex_lock(&dev->mutex);
861     *stats = dev->stats;
862     ovs_mutex_unlock(&dev->mutex);
863
864     return 0;
865 }
866
867 \f
868 /* Tunnel push pop ops. */
869
870 static struct ip_header *
871 ip_hdr(void *eth)
872 {
873     return (void *)((char *)eth + sizeof (struct eth_header));
874 }
875
876 static struct ovs_16aligned_ip6_hdr *
877 ipv6_hdr(void *eth)
878 {
879     return (void *)((char *)eth + sizeof (struct eth_header));
880 }
881
882 static void *
883 ip_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl,
884                   unsigned int *hlen)
885 {
886     void *nh;
887     struct ip_header *ip;
888     struct ovs_16aligned_ip6_hdr *ip6;
889     void *l4;
890     int l3_size;
891
892     nh = dp_packet_l3(packet);
893     ip = nh;
894     ip6 = nh;
895     l4 = dp_packet_l4(packet);
896
897     if (!nh || !l4) {
898         return NULL;
899     }
900
901     *hlen = sizeof(struct eth_header);
902
903     l3_size = dp_packet_size(packet) -
904               ((char *)nh - (char *)dp_packet_data(packet));
905
906     if (IP_VER(ip->ip_ihl_ver) == 4) {
907
908         ovs_be32 ip_src, ip_dst;
909
910         if (csum(ip, IP_IHL(ip->ip_ihl_ver) * 4)) {
911             VLOG_WARN_RL(&err_rl, "ip packet has invalid checksum");
912             return NULL;
913         }
914
915         if (ntohs(ip->ip_tot_len) > l3_size) {
916             VLOG_WARN_RL(&err_rl, "ip packet is truncated (IP length %d, actual %d)",
917                          ntohs(ip->ip_tot_len), l3_size);
918             return NULL;
919         }
920         if (IP_IHL(ip->ip_ihl_ver) * 4 > sizeof(struct ip_header)) {
921             VLOG_WARN_RL(&err_rl, "ip options not supported on tunnel packets "
922                          "(%d bytes)", IP_IHL(ip->ip_ihl_ver) * 4);
923             return NULL;
924         }
925
926         ip_src = get_16aligned_be32(&ip->ip_src);
927         ip_dst = get_16aligned_be32(&ip->ip_dst);
928
929         tnl->ip_src = ip_src;
930         tnl->ip_dst = ip_dst;
931         tnl->ip_tos = ip->ip_tos;
932         tnl->ip_ttl = ip->ip_ttl;
933
934         *hlen += IP_HEADER_LEN;
935
936     } else if (IP_VER(ip->ip_ihl_ver) == 6) {
937
938         memcpy(tnl->ipv6_src.s6_addr, ip6->ip6_src.be16, sizeof ip6->ip6_src);
939         memcpy(tnl->ipv6_dst.s6_addr, ip6->ip6_dst.be16, sizeof ip6->ip6_dst);
940         tnl->ip_tos = 0;
941         tnl->ip_ttl = ip6->ip6_hlim;
942
943         *hlen += IPV6_HEADER_LEN;
944
945     } else {
946         VLOG_WARN_RL(&err_rl, "ipv4 packet has invalid version (%d)",
947                      IP_VER(ip->ip_ihl_ver));
948         return NULL;
949     }
950
951     return l4;
952 }
953
954 static bool
955 is_header_ipv6(const void *header)
956 {
957     const struct eth_header *eth;
958     eth = header;
959     return eth->eth_type == htons(ETH_TYPE_IPV6);
960 }
961
962 /* Pushes the 'size' bytes of 'header' into the headroom of 'packet',
963  * reallocating the packet if necessary.  'header' should contain an Ethernet
964  * header, followed by an IPv4 header (without options), and an L4 header.
965  *
966  * This function sets the IP header's ip_tot_len field (which should be zeroed
967  * as part of 'header') and puts its value into '*ip_tot_size' as well.  Also
968  * updates IP header checksum.
969  *
970  * Return pointer to the L4 header added to 'packet'. */
971 static void *
972 push_ip_header(struct dp_packet *packet,
973                const void *header, int size, int *ip_tot_size)
974 {
975     struct eth_header *eth;
976     struct ip_header *ip;
977     struct ovs_16aligned_ip6_hdr *ip6;
978
979     eth = dp_packet_push_uninit(packet, size);
980     *ip_tot_size = dp_packet_size(packet) - sizeof (struct eth_header);
981
982     memcpy(eth, header, size);
983
984     if (is_header_ipv6(header)) {
985         ip6 = ipv6_hdr(eth);
986         *ip_tot_size -= IPV6_HEADER_LEN;
987         ip6->ip6_plen = htons(*ip_tot_size);
988         return ip6 + 1;
989     } else {
990         ip = ip_hdr(eth);
991         ip->ip_tot_len = htons(*ip_tot_size);
992         ip->ip_csum = recalc_csum16(ip->ip_csum, 0, ip->ip_tot_len);
993         *ip_tot_size -= IP_HEADER_LEN;
994         return ip + 1;
995     }
996 }
997
998 static void *
999 udp_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl,
1000                    unsigned int *hlen)
1001 {
1002     struct udp_header *udp;
1003
1004     udp = ip_extract_tnl_md(packet, tnl, hlen);
1005     if (!udp) {
1006         return NULL;
1007     }
1008
1009     if (udp->udp_csum) {
1010         uint32_t csum;
1011         if (is_header_ipv6(dp_packet_data(packet))) {
1012             csum = packet_csum_pseudoheader6(dp_packet_l3(packet));
1013         } else {
1014             csum = packet_csum_pseudoheader(dp_packet_l3(packet));
1015         }
1016
1017         csum = csum_continue(csum, udp, dp_packet_size(packet) -
1018                              ((const unsigned char *)udp -
1019                               (const unsigned char *)dp_packet_l2(packet)));
1020         if (csum_finish(csum)) {
1021             return NULL;
1022         }
1023         tnl->flags |= FLOW_TNL_F_CSUM;
1024     }
1025
1026     tnl->tp_src = udp->udp_src;
1027     tnl->tp_dst = udp->udp_dst;
1028
1029     return udp + 1;
1030 }
1031
1032 static ovs_be16
1033 get_src_port(struct dp_packet *packet)
1034 {
1035     uint32_t hash;
1036
1037     hash = dp_packet_get_rss_hash(packet);
1038
1039     return htons((((uint64_t) hash * (tnl_udp_port_max - tnl_udp_port_min)) >> 32) +
1040                  tnl_udp_port_min);
1041 }
1042
1043 static void
1044 push_udp_header(struct dp_packet *packet,
1045                 const struct ovs_action_push_tnl *data)
1046 {
1047     struct udp_header *udp;
1048     int ip_tot_size;
1049
1050     udp = push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
1051
1052     /* set udp src port */
1053     udp->udp_src = get_src_port(packet);
1054     udp->udp_len = htons(ip_tot_size);
1055
1056     if (udp->udp_csum) {
1057         uint32_t csum;
1058         if (is_header_ipv6(dp_packet_data(packet))) {
1059             csum = packet_csum_pseudoheader6(ipv6_hdr(dp_packet_data(packet)));
1060         } else {
1061             csum = packet_csum_pseudoheader(ip_hdr(dp_packet_data(packet)));
1062         }
1063
1064         csum = csum_continue(csum, udp, ip_tot_size);
1065         udp->udp_csum = csum_finish(csum);
1066
1067         if (!udp->udp_csum) {
1068             udp->udp_csum = htons(0xffff);
1069         }
1070     }
1071 }
1072
1073 static void *
1074 udp_build_header(struct netdev_tunnel_config *tnl_cfg,
1075                  const struct flow *tnl_flow,
1076                  struct ovs_action_push_tnl *data,
1077                  unsigned int *hlen)
1078 {
1079     struct ip_header *ip;
1080     struct ovs_16aligned_ip6_hdr *ip6;
1081     struct udp_header *udp;
1082     bool is_ipv6;
1083
1084     *hlen = sizeof(struct eth_header);
1085
1086     is_ipv6 = is_header_ipv6(data->header);
1087
1088     if (is_ipv6) {
1089         ip6 = ipv6_hdr(data->header);
1090         ip6->ip6_nxt = IPPROTO_UDP;
1091         udp = (struct udp_header *) (ip6 + 1);
1092         *hlen += IPV6_HEADER_LEN;
1093     } else {
1094         ip = ip_hdr(data->header);
1095         ip->ip_proto = IPPROTO_UDP;
1096         udp = (struct udp_header *) (ip + 1);
1097         *hlen += IP_HEADER_LEN;
1098     }
1099
1100     udp->udp_dst = tnl_cfg->dst_port;
1101
1102     if (is_ipv6 || tnl_flow->tunnel.flags & FLOW_TNL_F_CSUM) {
1103         /* Write a value in now to mark that we should compute the checksum
1104          * later. 0xffff is handy because it is transparent to the
1105          * calculation. */
1106         udp->udp_csum = htons(0xffff);
1107     }
1108
1109     return udp + 1;
1110 }
1111
1112 static int
1113 gre_header_len(ovs_be16 flags)
1114 {
1115     int hlen = 4;
1116
1117     if (flags & htons(GRE_CSUM)) {
1118         hlen += 4;
1119     }
1120     if (flags & htons(GRE_KEY)) {
1121         hlen += 4;
1122     }
1123     if (flags & htons(GRE_SEQ)) {
1124         hlen += 4;
1125     }
1126     return hlen;
1127 }
1128
1129 static int
1130 parse_gre_header(struct dp_packet *packet,
1131                  struct flow_tnl *tnl)
1132 {
1133     const struct gre_base_hdr *greh;
1134     ovs_16aligned_be32 *options;
1135     int hlen;
1136     unsigned int ulen;
1137
1138     greh = ip_extract_tnl_md(packet, tnl, &ulen);
1139     if (!greh) {
1140         return -EINVAL;
1141     }
1142
1143     if (greh->flags & ~(htons(GRE_CSUM | GRE_KEY | GRE_SEQ))) {
1144         return -EINVAL;
1145     }
1146
1147     if (greh->protocol != htons(ETH_TYPE_TEB)) {
1148         return -EINVAL;
1149     }
1150
1151     hlen = ulen + gre_header_len(greh->flags);
1152     if (hlen > dp_packet_size(packet)) {
1153         return -EINVAL;
1154     }
1155
1156     options = (ovs_16aligned_be32 *)(greh + 1);
1157     if (greh->flags & htons(GRE_CSUM)) {
1158         ovs_be16 pkt_csum;
1159
1160         pkt_csum = csum(greh, dp_packet_size(packet) -
1161                               ((const unsigned char *)greh -
1162                                (const unsigned char *)dp_packet_l2(packet)));
1163         if (pkt_csum) {
1164             return -EINVAL;
1165         }
1166         tnl->flags = FLOW_TNL_F_CSUM;
1167         options++;
1168     }
1169
1170     if (greh->flags & htons(GRE_KEY)) {
1171         tnl->tun_id = (OVS_FORCE ovs_be64) ((OVS_FORCE uint64_t)(get_16aligned_be32(options)) << 32);
1172         tnl->flags |= FLOW_TNL_F_KEY;
1173         options++;
1174     }
1175
1176     if (greh->flags & htons(GRE_SEQ)) {
1177         options++;
1178     }
1179
1180     return hlen;
1181 }
1182
1183 static void
1184 pkt_metadata_init_tnl(struct pkt_metadata *md)
1185 {
1186     /* Zero up through the tunnel metadata options. The length and table
1187      * are before this and as long as they are empty, the options won't
1188      * be looked at. */
1189     memset(md, 0, offsetof(struct pkt_metadata, tunnel.metadata.opts));
1190 }
1191
1192 static int
1193 netdev_gre_pop_header(struct dp_packet *packet)
1194 {
1195     struct pkt_metadata *md = &packet->md;
1196     struct flow_tnl *tnl = &md->tunnel;
1197     int hlen = sizeof(struct eth_header) + 4;
1198
1199     hlen += is_header_ipv6(dp_packet_data(packet)) ?
1200             IPV6_HEADER_LEN : IP_HEADER_LEN;
1201
1202     pkt_metadata_init_tnl(md);
1203     if (hlen > dp_packet_size(packet)) {
1204         return EINVAL;
1205     }
1206
1207     hlen = parse_gre_header(packet, tnl);
1208     if (hlen < 0) {
1209         return -hlen;
1210     }
1211
1212     dp_packet_reset_packet(packet, hlen);
1213
1214     return 0;
1215 }
1216
1217 static void
1218 netdev_gre_push_header(struct dp_packet *packet,
1219                        const struct ovs_action_push_tnl *data)
1220 {
1221     struct gre_base_hdr *greh;
1222     int ip_tot_size;
1223
1224     greh = push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
1225
1226     if (greh->flags & htons(GRE_CSUM)) {
1227         ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1);
1228         *csum_opt = csum(greh, ip_tot_size);
1229     }
1230 }
1231
1232 static int
1233 netdev_gre_build_header(const struct netdev *netdev,
1234                         struct ovs_action_push_tnl *data,
1235                         const struct flow *tnl_flow)
1236 {
1237     struct netdev_vport *dev = netdev_vport_cast(netdev);
1238     struct netdev_tunnel_config *tnl_cfg;
1239     struct ip_header *ip;
1240     struct ovs_16aligned_ip6_hdr *ip6;
1241     struct gre_base_hdr *greh;
1242     ovs_16aligned_be32 *options;
1243     int hlen;
1244     bool is_ipv6;
1245
1246     is_ipv6 = is_header_ipv6(data->header);
1247
1248     /* XXX: RCUfy tnl_cfg. */
1249     ovs_mutex_lock(&dev->mutex);
1250     tnl_cfg = &dev->tnl_cfg;
1251
1252     if (is_ipv6) {
1253         ip6 = ipv6_hdr(data->header);
1254         ip6->ip6_nxt = IPPROTO_GRE;
1255         greh = (struct gre_base_hdr *) (ip6 + 1);
1256     } else {
1257         ip = ip_hdr(data->header);
1258         ip->ip_proto = IPPROTO_GRE;
1259         greh = (struct gre_base_hdr *) (ip + 1);
1260     }
1261
1262     greh->protocol = htons(ETH_TYPE_TEB);
1263     greh->flags = 0;
1264
1265     options = (ovs_16aligned_be32 *) (greh + 1);
1266     if (tnl_flow->tunnel.flags & FLOW_TNL_F_CSUM) {
1267         greh->flags |= htons(GRE_CSUM);
1268         put_16aligned_be32(options, 0);
1269         options++;
1270     }
1271
1272     if (tnl_cfg->out_key_present) {
1273         greh->flags |= htons(GRE_KEY);
1274         put_16aligned_be32(options, (OVS_FORCE ovs_be32)
1275                                     ((OVS_FORCE uint64_t) tnl_flow->tunnel.tun_id >> 32));
1276         options++;
1277     }
1278
1279     ovs_mutex_unlock(&dev->mutex);
1280
1281     hlen = (uint8_t *) options - (uint8_t *) greh;
1282
1283     data->header_len = sizeof(struct eth_header) + hlen +
1284                        (is_ipv6 ? IPV6_HEADER_LEN : IP_HEADER_LEN);
1285     data->tnl_type = OVS_VPORT_TYPE_GRE;
1286     return 0;
1287 }
1288
1289 static int
1290 netdev_vxlan_pop_header(struct dp_packet *packet)
1291 {
1292     struct pkt_metadata *md = &packet->md;
1293     struct flow_tnl *tnl = &md->tunnel;
1294     struct vxlanhdr *vxh;
1295     unsigned int hlen;
1296
1297     pkt_metadata_init_tnl(md);
1298     if (VXLAN_HLEN > dp_packet_l4_size(packet)) {
1299         return EINVAL;
1300     }
1301
1302     vxh = udp_extract_tnl_md(packet, tnl, &hlen);
1303     if (!vxh) {
1304         return EINVAL;
1305     }
1306
1307     if (get_16aligned_be32(&vxh->vx_flags) != htonl(VXLAN_FLAGS) ||
1308        (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) {
1309         VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n",
1310                      ntohl(get_16aligned_be32(&vxh->vx_flags)),
1311                      ntohl(get_16aligned_be32(&vxh->vx_vni)));
1312         return EINVAL;
1313     }
1314     tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
1315     tnl->flags |= FLOW_TNL_F_KEY;
1316
1317     dp_packet_reset_packet(packet, hlen + VXLAN_HLEN);
1318
1319     return 0;
1320 }
1321
1322 static int
1323 netdev_vxlan_build_header(const struct netdev *netdev,
1324                           struct ovs_action_push_tnl *data,
1325                           const struct flow *tnl_flow)
1326 {
1327     struct netdev_vport *dev = netdev_vport_cast(netdev);
1328     struct netdev_tunnel_config *tnl_cfg;
1329     struct vxlanhdr *vxh;
1330     unsigned int hlen;
1331
1332     /* XXX: RCUfy tnl_cfg. */
1333     ovs_mutex_lock(&dev->mutex);
1334     tnl_cfg = &dev->tnl_cfg;
1335
1336     vxh = udp_build_header(tnl_cfg, tnl_flow, data, &hlen);
1337
1338     put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS));
1339     put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
1340
1341     ovs_mutex_unlock(&dev->mutex);
1342     data->header_len = hlen + VXLAN_HLEN;
1343     data->tnl_type = OVS_VPORT_TYPE_VXLAN;
1344     return 0;
1345 }
1346
1347 static int
1348 netdev_geneve_pop_header(struct dp_packet *packet)
1349 {
1350     struct pkt_metadata *md = &packet->md;
1351     struct flow_tnl *tnl = &md->tunnel;
1352     struct genevehdr *gnh;
1353     unsigned int hlen, opts_len, ulen;
1354
1355     pkt_metadata_init_tnl(md);
1356     if (GENEVE_BASE_HLEN > dp_packet_l4_size(packet)) {
1357         VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%"PRIuSIZE"\n",
1358                      (unsigned int)GENEVE_BASE_HLEN, dp_packet_l4_size(packet));
1359         return EINVAL;
1360     }
1361
1362     gnh = udp_extract_tnl_md(packet, tnl, &ulen);
1363     if (!gnh) {
1364         return EINVAL;
1365     }
1366
1367     opts_len = gnh->opt_len * 4;
1368     hlen = ulen + GENEVE_BASE_HLEN + opts_len;
1369     if (hlen > dp_packet_size(packet)) {
1370         VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n",
1371                      hlen, dp_packet_size(packet));
1372         return EINVAL;
1373     }
1374
1375     if (gnh->ver != 0) {
1376         VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver);
1377         return EINVAL;
1378     }
1379
1380     if (gnh->proto_type != htons(ETH_TYPE_TEB)) {
1381         VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n",
1382                      ntohs(gnh->proto_type));
1383         return EINVAL;
1384     }
1385
1386     tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0;
1387     tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
1388     tnl->flags |= FLOW_TNL_F_KEY;
1389
1390     memcpy(tnl->metadata.opts.gnv, gnh->options, opts_len);
1391     tnl->metadata.present.len = opts_len;
1392     tnl->flags |= FLOW_TNL_F_UDPIF;
1393
1394     dp_packet_reset_packet(packet, hlen);
1395
1396     return 0;
1397 }
1398
1399 static int
1400 netdev_geneve_build_header(const struct netdev *netdev,
1401                            struct ovs_action_push_tnl *data,
1402                            const struct flow *tnl_flow)
1403 {
1404     struct netdev_vport *dev = netdev_vport_cast(netdev);
1405     struct netdev_tunnel_config *tnl_cfg;
1406     struct genevehdr *gnh;
1407     int opt_len;
1408     bool crit_opt;
1409     unsigned int hlen;
1410
1411     /* XXX: RCUfy tnl_cfg. */
1412     ovs_mutex_lock(&dev->mutex);
1413     tnl_cfg = &dev->tnl_cfg;
1414
1415     gnh = udp_build_header(tnl_cfg, tnl_flow, data, &hlen);
1416
1417     put_16aligned_be32(&gnh->vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
1418
1419     ovs_mutex_unlock(&dev->mutex);
1420
1421     opt_len = tun_metadata_to_geneve_header(&tnl_flow->tunnel,
1422                                             gnh->options, &crit_opt);
1423
1424     gnh->opt_len = opt_len / 4;
1425     gnh->oam = !!(tnl_flow->tunnel.flags & FLOW_TNL_F_OAM);
1426     gnh->critical = crit_opt ? 1 : 0;
1427     gnh->proto_type = htons(ETH_TYPE_TEB);
1428
1429     data->header_len = hlen + GENEVE_BASE_HLEN + opt_len;
1430     data->tnl_type = OVS_VPORT_TYPE_GENEVE;
1431     return 0;
1432 }
1433
1434 static void
1435 netdev_vport_range(struct unixctl_conn *conn, int argc,
1436                    const char *argv[], void *aux OVS_UNUSED)
1437 {
1438     int val1, val2;
1439
1440     if (argc < 3) {
1441         struct ds ds = DS_EMPTY_INITIALIZER;
1442
1443         ds_put_format(&ds, "Tunnel UDP source port range: %"PRIu16"-%"PRIu16"\n",
1444                             tnl_udp_port_min, tnl_udp_port_max);
1445
1446         unixctl_command_reply(conn, ds_cstr(&ds));
1447         ds_destroy(&ds);
1448         return;
1449     }
1450
1451     if (argc != 3) {
1452         return;
1453     }
1454
1455     val1 = atoi(argv[1]);
1456     if (val1 <= 0 || val1 > UINT16_MAX) {
1457         unixctl_command_reply(conn, "Invalid min.");
1458         return;
1459     }
1460     val2 = atoi(argv[2]);
1461     if (val2 <= 0 || val2 > UINT16_MAX) {
1462         unixctl_command_reply(conn, "Invalid max.");
1463         return;
1464     }
1465
1466     if (val1 > val2) {
1467         tnl_udp_port_min = val2;
1468         tnl_udp_port_max = val1;
1469     } else {
1470         tnl_udp_port_min = val1;
1471         tnl_udp_port_max = val2;
1472     }
1473     seq_change(tnl_conf_seq);
1474
1475     unixctl_command_reply(conn, "OK");
1476 }
1477
1478 \f
1479 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG,             \
1480                         GET_TUNNEL_CONFIG, GET_STATUS,      \
1481                         BUILD_HEADER,                       \
1482                         PUSH_HEADER, POP_HEADER)            \
1483     NULL,                                                   \
1484     netdev_vport_run,                                       \
1485     netdev_vport_wait,                                      \
1486                                                             \
1487     netdev_vport_alloc,                                     \
1488     netdev_vport_construct,                                 \
1489     netdev_vport_destruct,                                  \
1490     netdev_vport_dealloc,                                   \
1491     GET_CONFIG,                                             \
1492     SET_CONFIG,                                             \
1493     GET_TUNNEL_CONFIG,                                      \
1494     BUILD_HEADER,                                           \
1495     PUSH_HEADER,                                            \
1496     POP_HEADER,                                             \
1497     NULL,                       /* get_numa_id */           \
1498     NULL,                       /* set_multiq */            \
1499                                                             \
1500     NULL,                       /* send */                  \
1501     NULL,                       /* send_wait */             \
1502                                                             \
1503     netdev_vport_set_etheraddr,                             \
1504     netdev_vport_get_etheraddr,                             \
1505     NULL,                       /* get_mtu */               \
1506     NULL,                       /* set_mtu */               \
1507     NULL,                       /* get_ifindex */           \
1508     NULL,                       /* get_carrier */           \
1509     NULL,                       /* get_carrier_resets */    \
1510     NULL,                       /* get_miimon */            \
1511     get_stats,                                              \
1512                                                             \
1513     NULL,                       /* get_features */          \
1514     NULL,                       /* set_advertisements */    \
1515                                                             \
1516     NULL,                       /* set_policing */          \
1517     NULL,                       /* get_qos_types */         \
1518     NULL,                       /* get_qos_capabilities */  \
1519     NULL,                       /* get_qos */               \
1520     NULL,                       /* set_qos */               \
1521     NULL,                       /* get_queue */             \
1522     NULL,                       /* set_queue */             \
1523     NULL,                       /* delete_queue */          \
1524     NULL,                       /* get_queue_stats */       \
1525     NULL,                       /* queue_dump_start */      \
1526     NULL,                       /* queue_dump_next */       \
1527     NULL,                       /* queue_dump_done */       \
1528     NULL,                       /* dump_queue_stats */      \
1529                                                             \
1530     NULL,                       /* get_in4 */               \
1531     NULL,                       /* set_in4 */               \
1532     NULL,                       /* get_in6 */               \
1533     NULL,                       /* add_router */            \
1534     NULL,                       /* get_next_hop */          \
1535     GET_STATUS,                                             \
1536     NULL,                       /* arp_lookup */            \
1537                                                             \
1538     netdev_vport_update_flags,                              \
1539                                                             \
1540     NULL,                   /* rx_alloc */                  \
1541     NULL,                   /* rx_construct */              \
1542     NULL,                   /* rx_destruct */               \
1543     NULL,                   /* rx_dealloc */                \
1544     NULL,                   /* rx_recv */                   \
1545     NULL,                   /* rx_wait */                   \
1546     NULL,                   /* rx_drain */
1547
1548
1549 #define TUNNEL_CLASS(NAME, DPIF_PORT, BUILD_HEADER, PUSH_HEADER, POP_HEADER)   \
1550     { DPIF_PORT,                                                               \
1551         { NAME, VPORT_FUNCTIONS(get_tunnel_config,                             \
1552                                 set_tunnel_config,                             \
1553                                 get_netdev_tunnel_config,                      \
1554                                 tunnel_get_status,                             \
1555                                 BUILD_HEADER, PUSH_HEADER, POP_HEADER) }}
1556
1557 void
1558 netdev_vport_tunnel_register(void)
1559 {
1560     /* The name of the dpif_port should be short enough to accomodate adding
1561      * a port number to the end if one is necessary. */
1562     static const struct vport_class vport_classes[] = {
1563         TUNNEL_CLASS("geneve", "genev_sys", netdev_geneve_build_header,
1564                                             push_udp_header,
1565                                             netdev_geneve_pop_header),
1566         TUNNEL_CLASS("gre", "gre_sys", netdev_gre_build_header,
1567                                        netdev_gre_push_header,
1568                                        netdev_gre_pop_header),
1569         TUNNEL_CLASS("ipsec_gre", "gre_sys", NULL, NULL, NULL),
1570         TUNNEL_CLASS("vxlan", "vxlan_sys", netdev_vxlan_build_header,
1571                                            push_udp_header,
1572                                            netdev_vxlan_pop_header),
1573         TUNNEL_CLASS("lisp", "lisp_sys", NULL, NULL, NULL),
1574         TUNNEL_CLASS("stt", "stt_sys", NULL, NULL, NULL),
1575     };
1576     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1577
1578     if (ovsthread_once_start(&once)) {
1579         int i;
1580
1581         for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1582             netdev_register_provider(&vport_classes[i].netdev_class);
1583         }
1584
1585         unixctl_command_register("tnl/egress_port_range", "min max", 0, 2,
1586                                  netdev_vport_range, NULL);
1587
1588         ovsthread_once_done(&once);
1589     }
1590 }
1591
1592 void
1593 netdev_vport_patch_register(void)
1594 {
1595     static const struct vport_class patch_class =
1596         { NULL,
1597             { "patch", VPORT_FUNCTIONS(get_patch_config,
1598                                        set_patch_config,
1599                                        NULL,
1600                                        NULL, NULL, NULL, NULL) }};
1601     netdev_register_provider(&patch_class.netdev_class);
1602 }