userspace: Define and use struct eth_addr.
[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     struct eth_addr etheraddr;
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     dev->tnl_cfg.dont_fragment = true;
266     dev->tnl_cfg.ttl = DEFAULT_TTL;
267     return 0;
268 }
269
270 static void
271 netdev_vport_destruct(struct netdev *netdev_)
272 {
273     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
274
275     free(netdev->peer);
276     ovs_mutex_destroy(&netdev->mutex);
277 }
278
279 static void
280 netdev_vport_dealloc(struct netdev *netdev_)
281 {
282     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
283     free(netdev);
284 }
285
286 static int
287 netdev_vport_set_etheraddr(struct netdev *netdev_, const struct eth_addr mac)
288 {
289     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
290
291     ovs_mutex_lock(&netdev->mutex);
292     netdev->etheraddr = mac;
293     ovs_mutex_unlock(&netdev->mutex);
294     netdev_change_seq_changed(netdev_);
295
296     return 0;
297 }
298
299 static int
300 netdev_vport_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac)
301 {
302     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
303
304     ovs_mutex_lock(&netdev->mutex);
305     *mac = netdev->etheraddr;
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 void
1053 pkt_metadata_init_tnl(struct pkt_metadata *md)
1054 {
1055     /* Zero up through the tunnel metadata options. The length and table
1056      * are before this and as long as they are empty, the options won't
1057      * be looked at. */
1058     memset(md, 0, offsetof(struct pkt_metadata, tunnel.metadata.opts));
1059 }
1060
1061 static int
1062 netdev_gre_pop_header(struct dp_packet *packet)
1063 {
1064     struct pkt_metadata *md = &packet->md;
1065     struct flow_tnl *tnl = &md->tunnel;
1066     int hlen = sizeof(struct eth_header) +
1067                sizeof(struct ip_header) + 4;
1068
1069     pkt_metadata_init_tnl(md);
1070     if (hlen > dp_packet_size(packet)) {
1071         return EINVAL;
1072     }
1073
1074     hlen = parse_gre_header(packet, tnl);
1075     if (hlen < 0) {
1076         return -hlen;
1077     }
1078
1079     dp_packet_reset_packet(packet, hlen);
1080
1081     return 0;
1082 }
1083
1084 static void
1085 netdev_gre_push_header(struct dp_packet *packet,
1086                        const struct ovs_action_push_tnl *data)
1087 {
1088     struct gre_base_hdr *greh;
1089     int ip_tot_size;
1090
1091     greh = push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
1092
1093     if (greh->flags & htons(GRE_CSUM)) {
1094         ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1);
1095         *csum_opt = csum(greh, ip_tot_size - sizeof (struct ip_header));
1096     }
1097 }
1098
1099 static int
1100 netdev_gre_build_header(const struct netdev *netdev,
1101                         struct ovs_action_push_tnl *data,
1102                         const struct flow *tnl_flow)
1103 {
1104     struct netdev_vport *dev = netdev_vport_cast(netdev);
1105     struct netdev_tunnel_config *tnl_cfg;
1106     struct ip_header *ip;
1107     struct gre_base_hdr *greh;
1108     ovs_16aligned_be32 *options;
1109     int hlen;
1110
1111     /* XXX: RCUfy tnl_cfg. */
1112     ovs_mutex_lock(&dev->mutex);
1113     tnl_cfg = &dev->tnl_cfg;
1114
1115     ip = ip_hdr(data->header);
1116     ip->ip_proto = IPPROTO_GRE;
1117
1118     greh = gre_hdr(ip);
1119     greh->protocol = htons(ETH_TYPE_TEB);
1120     greh->flags = 0;
1121
1122     options = (ovs_16aligned_be32 *) (greh + 1);
1123     if (tnl_flow->tunnel.flags & FLOW_TNL_F_CSUM) {
1124         greh->flags |= htons(GRE_CSUM);
1125         put_16aligned_be32(options, 0);
1126         options++;
1127     }
1128
1129     if (tnl_cfg->out_key_present) {
1130         greh->flags |= htons(GRE_KEY);
1131         put_16aligned_be32(options, (OVS_FORCE ovs_be32)
1132                                     ((OVS_FORCE uint64_t) tnl_flow->tunnel.tun_id >> 32));
1133         options++;
1134     }
1135
1136     ovs_mutex_unlock(&dev->mutex);
1137
1138     hlen = (uint8_t *) options - (uint8_t *) greh;
1139
1140     data->header_len = sizeof(struct eth_header) +
1141                        sizeof(struct ip_header)  + hlen;
1142     data->tnl_type = OVS_VPORT_TYPE_GRE;
1143     return 0;
1144 }
1145
1146 static int
1147 netdev_vxlan_pop_header(struct dp_packet *packet)
1148 {
1149     struct pkt_metadata *md = &packet->md;
1150     struct flow_tnl *tnl = &md->tunnel;
1151     struct vxlanhdr *vxh;
1152
1153     pkt_metadata_init_tnl(md);
1154     if (VXLAN_HLEN > dp_packet_size(packet)) {
1155         return EINVAL;
1156     }
1157
1158     vxh = udp_extract_tnl_md(packet, tnl);
1159     if (!vxh) {
1160         return EINVAL;
1161     }
1162
1163     if (get_16aligned_be32(&vxh->vx_flags) != htonl(VXLAN_FLAGS) ||
1164        (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) {
1165         VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n",
1166                      ntohl(get_16aligned_be32(&vxh->vx_flags)),
1167                      ntohl(get_16aligned_be32(&vxh->vx_vni)));
1168         return EINVAL;
1169     }
1170     tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
1171     tnl->flags |= FLOW_TNL_F_KEY;
1172
1173     dp_packet_reset_packet(packet, VXLAN_HLEN);
1174
1175     return 0;
1176 }
1177
1178 static int
1179 netdev_vxlan_build_header(const struct netdev *netdev,
1180                           struct ovs_action_push_tnl *data,
1181                           const struct flow *tnl_flow)
1182 {
1183     struct netdev_vport *dev = netdev_vport_cast(netdev);
1184     struct netdev_tunnel_config *tnl_cfg;
1185     struct vxlanhdr *vxh;
1186
1187     /* XXX: RCUfy tnl_cfg. */
1188     ovs_mutex_lock(&dev->mutex);
1189     tnl_cfg = &dev->tnl_cfg;
1190
1191     vxh = udp_build_header(tnl_cfg, tnl_flow, data);
1192
1193     put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS));
1194     put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
1195
1196     ovs_mutex_unlock(&dev->mutex);
1197     data->header_len = VXLAN_HLEN;
1198     data->tnl_type = OVS_VPORT_TYPE_VXLAN;
1199     return 0;
1200 }
1201
1202 static int
1203 netdev_geneve_pop_header(struct dp_packet *packet)
1204 {
1205     struct pkt_metadata *md = &packet->md;
1206     struct flow_tnl *tnl = &md->tunnel;
1207     struct genevehdr *gnh;
1208     unsigned int hlen, opts_len;
1209
1210     pkt_metadata_init_tnl(md);
1211     if (GENEVE_BASE_HLEN > dp_packet_size(packet)) {
1212         VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%u\n",
1213                      (unsigned int)GENEVE_BASE_HLEN, dp_packet_size(packet));
1214         return EINVAL;
1215     }
1216
1217     gnh = udp_extract_tnl_md(packet, tnl);
1218     if (!gnh) {
1219         return EINVAL;
1220     }
1221
1222     opts_len = gnh->opt_len * 4;
1223     hlen = GENEVE_BASE_HLEN + opts_len;
1224     if (hlen > dp_packet_size(packet)) {
1225         VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n",
1226                      hlen, dp_packet_size(packet));
1227         return EINVAL;
1228     }
1229
1230     if (gnh->ver != 0) {
1231         VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver);
1232         return EINVAL;
1233     }
1234
1235     if (gnh->proto_type != htons(ETH_TYPE_TEB)) {
1236         VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n",
1237                      ntohs(gnh->proto_type));
1238         return EINVAL;
1239     }
1240
1241     tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0;
1242     tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
1243     tnl->flags |= FLOW_TNL_F_KEY;
1244
1245     memcpy(tnl->metadata.opts.gnv, gnh->options, opts_len);
1246     tnl->metadata.present.len = opts_len;
1247     tnl->flags |= FLOW_TNL_F_UDPIF;
1248
1249     dp_packet_reset_packet(packet, hlen);
1250
1251     return 0;
1252 }
1253
1254 static int
1255 netdev_geneve_build_header(const struct netdev *netdev,
1256                            struct ovs_action_push_tnl *data,
1257                            const struct flow *tnl_flow)
1258 {
1259     struct netdev_vport *dev = netdev_vport_cast(netdev);
1260     struct netdev_tunnel_config *tnl_cfg;
1261     struct genevehdr *gnh;
1262     int opt_len;
1263     bool crit_opt;
1264
1265     /* XXX: RCUfy tnl_cfg. */
1266     ovs_mutex_lock(&dev->mutex);
1267     tnl_cfg = &dev->tnl_cfg;
1268
1269     gnh = udp_build_header(tnl_cfg, tnl_flow, data);
1270
1271     put_16aligned_be32(&gnh->vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
1272
1273     ovs_mutex_unlock(&dev->mutex);
1274
1275     opt_len = tun_metadata_to_geneve_header(&tnl_flow->tunnel,
1276                                             gnh->options, &crit_opt);
1277
1278     gnh->opt_len = opt_len / 4;
1279     gnh->oam = !!(tnl_flow->tunnel.flags & FLOW_TNL_F_OAM);
1280     gnh->critical = crit_opt ? 1 : 0;
1281     gnh->proto_type = htons(ETH_TYPE_TEB);
1282
1283     data->header_len = GENEVE_BASE_HLEN + opt_len;
1284     data->tnl_type = OVS_VPORT_TYPE_GENEVE;
1285     return 0;
1286 }
1287
1288 static void
1289 netdev_vport_range(struct unixctl_conn *conn, int argc,
1290                    const char *argv[], void *aux OVS_UNUSED)
1291 {
1292     int val1, val2;
1293
1294     if (argc < 3) {
1295         struct ds ds = DS_EMPTY_INITIALIZER;
1296
1297         ds_put_format(&ds, "Tunnel UDP source port range: %"PRIu16"-%"PRIu16"\n",
1298                             tnl_udp_port_min, tnl_udp_port_max);
1299
1300         unixctl_command_reply(conn, ds_cstr(&ds));
1301         ds_destroy(&ds);
1302         return;
1303     }
1304
1305     if (argc != 3) {
1306         return;
1307     }
1308
1309     val1 = atoi(argv[1]);
1310     if (val1 <= 0 || val1 > UINT16_MAX) {
1311         unixctl_command_reply(conn, "Invalid min.");
1312         return;
1313     }
1314     val2 = atoi(argv[2]);
1315     if (val2 <= 0 || val2 > UINT16_MAX) {
1316         unixctl_command_reply(conn, "Invalid max.");
1317         return;
1318     }
1319
1320     if (val1 > val2) {
1321         tnl_udp_port_min = val2;
1322         tnl_udp_port_max = val1;
1323     } else {
1324         tnl_udp_port_min = val1;
1325         tnl_udp_port_max = val2;
1326     }
1327     seq_change(tnl_conf_seq);
1328
1329     unixctl_command_reply(conn, "OK");
1330 }
1331
1332 \f
1333 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG,             \
1334                         GET_TUNNEL_CONFIG, GET_STATUS,      \
1335                         BUILD_HEADER,                       \
1336                         PUSH_HEADER, POP_HEADER)            \
1337     NULL,                                                   \
1338     netdev_vport_run,                                       \
1339     netdev_vport_wait,                                      \
1340                                                             \
1341     netdev_vport_alloc,                                     \
1342     netdev_vport_construct,                                 \
1343     netdev_vport_destruct,                                  \
1344     netdev_vport_dealloc,                                   \
1345     GET_CONFIG,                                             \
1346     SET_CONFIG,                                             \
1347     GET_TUNNEL_CONFIG,                                      \
1348     BUILD_HEADER,                                           \
1349     PUSH_HEADER,                                            \
1350     POP_HEADER,                                             \
1351     NULL,                       /* get_numa_id */           \
1352     NULL,                       /* set_multiq */            \
1353                                                             \
1354     NULL,                       /* send */                  \
1355     NULL,                       /* send_wait */             \
1356                                                             \
1357     netdev_vport_set_etheraddr,                             \
1358     netdev_vport_get_etheraddr,                             \
1359     NULL,                       /* get_mtu */               \
1360     NULL,                       /* set_mtu */               \
1361     NULL,                       /* get_ifindex */           \
1362     NULL,                       /* get_carrier */           \
1363     NULL,                       /* get_carrier_resets */    \
1364     NULL,                       /* get_miimon */            \
1365     get_stats,                                              \
1366                                                             \
1367     NULL,                       /* get_features */          \
1368     NULL,                       /* set_advertisements */    \
1369                                                             \
1370     NULL,                       /* set_policing */          \
1371     NULL,                       /* get_qos_types */         \
1372     NULL,                       /* get_qos_capabilities */  \
1373     NULL,                       /* get_qos */               \
1374     NULL,                       /* set_qos */               \
1375     NULL,                       /* get_queue */             \
1376     NULL,                       /* set_queue */             \
1377     NULL,                       /* delete_queue */          \
1378     NULL,                       /* get_queue_stats */       \
1379     NULL,                       /* queue_dump_start */      \
1380     NULL,                       /* queue_dump_next */       \
1381     NULL,                       /* queue_dump_done */       \
1382     NULL,                       /* dump_queue_stats */      \
1383                                                             \
1384     NULL,                       /* get_in4 */               \
1385     NULL,                       /* set_in4 */               \
1386     NULL,                       /* get_in6 */               \
1387     NULL,                       /* add_router */            \
1388     NULL,                       /* get_next_hop */          \
1389     GET_STATUS,                                             \
1390     NULL,                       /* arp_lookup */            \
1391                                                             \
1392     netdev_vport_update_flags,                              \
1393                                                             \
1394     NULL,                   /* rx_alloc */                  \
1395     NULL,                   /* rx_construct */              \
1396     NULL,                   /* rx_destruct */               \
1397     NULL,                   /* rx_dealloc */                \
1398     NULL,                   /* rx_recv */                   \
1399     NULL,                   /* rx_wait */                   \
1400     NULL,                   /* rx_drain */
1401
1402
1403 #define TUNNEL_CLASS(NAME, DPIF_PORT, BUILD_HEADER, PUSH_HEADER, POP_HEADER)   \
1404     { DPIF_PORT,                                                               \
1405         { NAME, VPORT_FUNCTIONS(get_tunnel_config,                             \
1406                                 set_tunnel_config,                             \
1407                                 get_netdev_tunnel_config,                      \
1408                                 tunnel_get_status,                             \
1409                                 BUILD_HEADER, PUSH_HEADER, POP_HEADER) }}
1410
1411 void
1412 netdev_vport_tunnel_register(void)
1413 {
1414     /* The name of the dpif_port should be short enough to accomodate adding
1415      * a port number to the end if one is necessary. */
1416     static const struct vport_class vport_classes[] = {
1417         TUNNEL_CLASS("geneve", "genev_sys", netdev_geneve_build_header,
1418                                             push_udp_header,
1419                                             netdev_geneve_pop_header),
1420         TUNNEL_CLASS("gre", "gre_sys", netdev_gre_build_header,
1421                                        netdev_gre_push_header,
1422                                        netdev_gre_pop_header),
1423         TUNNEL_CLASS("ipsec_gre", "gre_sys", NULL, NULL, NULL),
1424         TUNNEL_CLASS("vxlan", "vxlan_sys", netdev_vxlan_build_header,
1425                                            push_udp_header,
1426                                            netdev_vxlan_pop_header),
1427         TUNNEL_CLASS("lisp", "lisp_sys", NULL, NULL, NULL),
1428         TUNNEL_CLASS("stt", "stt_sys", NULL, NULL, NULL),
1429     };
1430     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1431
1432     if (ovsthread_once_start(&once)) {
1433         int i;
1434
1435         for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1436             netdev_register_provider(&vport_classes[i].netdev_class);
1437         }
1438
1439         unixctl_command_register("tnl/egress_port_range", "min max", 0, 2,
1440                                  netdev_vport_range, NULL);
1441
1442         ovsthread_once_done(&once);
1443     }
1444 }
1445
1446 void
1447 netdev_vport_patch_register(void)
1448 {
1449     static const struct vport_class patch_class =
1450         { NULL,
1451             { "patch", VPORT_FUNCTIONS(get_patch_config,
1452                                        set_patch_config,
1453                                        NULL,
1454                                        NULL, NULL, NULL, NULL) }};
1455     netdev_register_provider(&patch_class.netdev_class);
1456 }