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