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