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