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