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