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