2 * Copyright (c) 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "netdev-vport.h"
23 #include <sys/socket.h>
25 #include <sys/ioctl.h>
27 #include "byte-order.h"
32 #include "dp-packet.h"
33 #include "dynamic-string.h"
38 #include "netdev-provider.h"
39 #include "odp-netlink.h"
40 #include "dp-packet.h"
41 #include "ovs-router.h"
43 #include "poll-loop.h"
44 #include "route-table.h"
46 #include "socket-util.h"
47 #include "openvswitch/vlog.h"
48 #include "unaligned.h"
52 VLOG_DEFINE_THIS_MODULE(netdev_vport);
53 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(60, 5);
55 #define GENEVE_DST_PORT 6081
56 #define VXLAN_DST_PORT 4789
57 #define LISP_DST_PORT 4341
58 #define STT_DST_PORT 7471
60 #define VXLAN_HLEN (sizeof(struct eth_header) + \
61 sizeof(struct ip_header) + \
62 sizeof(struct udp_header) + \
63 sizeof(struct vxlanhdr))
65 #define GENEVE_BASE_HLEN (sizeof(struct eth_header) + \
66 sizeof(struct ip_header) + \
67 sizeof(struct udp_header) + \
68 sizeof(struct genevehdr))
70 #define DEFAULT_TTL 64
75 /* Protects all members below. */
76 struct ovs_mutex mutex;
78 uint8_t etheraddr[ETH_ADDR_LEN];
79 struct netdev_stats stats;
82 struct netdev_tunnel_config tnl_cfg;
83 char egress_iface[IFNAMSIZ];
91 const char *dpif_port;
92 struct netdev_class netdev_class;
95 /* Last read of the route-table's change number. */
96 static uint64_t rt_change_seqno;
98 static int netdev_vport_construct(struct netdev *);
99 static int get_patch_config(const struct netdev *netdev, struct smap *args);
100 static int get_tunnel_config(const struct netdev *, struct smap *args);
101 static bool tunnel_check_status_change__(struct netdev_vport *);
103 static uint16_t tnl_udp_port_min = 32768;
104 static uint16_t tnl_udp_port_max = 61000;
107 is_vport_class(const struct netdev_class *class)
109 return class->construct == netdev_vport_construct;
113 netdev_vport_is_vport_class(const struct netdev_class *class)
115 return is_vport_class(class);
118 static const struct vport_class *
119 vport_class_cast(const struct netdev_class *class)
121 ovs_assert(is_vport_class(class));
122 return CONTAINER_OF(class, struct vport_class, netdev_class);
125 static struct netdev_vport *
126 netdev_vport_cast(const struct netdev *netdev)
128 ovs_assert(is_vport_class(netdev_get_class(netdev)));
129 return CONTAINER_OF(netdev, struct netdev_vport, up);
132 static const struct netdev_tunnel_config *
133 get_netdev_tunnel_config(const struct netdev *netdev)
135 return &netdev_vport_cast(netdev)->tnl_cfg;
139 netdev_vport_is_patch(const struct netdev *netdev)
141 const struct netdev_class *class = netdev_get_class(netdev);
143 return class->get_config == get_patch_config;
147 netdev_vport_is_layer3(const struct netdev *dev)
149 const char *type = netdev_get_type(dev);
151 return (!strcmp("lisp", type));
155 netdev_vport_needs_dst_port(const struct netdev *dev)
157 const struct netdev_class *class = netdev_get_class(dev);
158 const char *type = netdev_get_type(dev);
160 return (class->get_config == get_tunnel_config &&
161 (!strcmp("geneve", type) || !strcmp("vxlan", type) ||
162 !strcmp("lisp", type) || !strcmp("stt", type)) );
166 netdev_vport_class_get_dpif_port(const struct netdev_class *class)
168 return is_vport_class(class) ? vport_class_cast(class)->dpif_port : NULL;
172 netdev_vport_get_dpif_port(const struct netdev *netdev,
173 char namebuf[], size_t bufsize)
175 const struct netdev_class *class = netdev_get_class(netdev);
176 const char *dpif_port = netdev_vport_class_get_dpif_port(class);
179 return netdev_get_name(netdev);
182 if (netdev_vport_needs_dst_port(netdev)) {
183 const struct netdev_vport *vport = netdev_vport_cast(netdev);
186 * Note: IFNAMSIZ is 16 bytes long. Implementations should choose
187 * a dpif port name that is short enough to fit including any
188 * port numbers but assert just in case.
190 BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
191 ovs_assert(strlen(dpif_port) + 6 < IFNAMSIZ);
192 snprintf(namebuf, bufsize, "%s_%d", dpif_port,
193 ntohs(vport->tnl_cfg.dst_port));
201 netdev_vport_get_dpif_port_strdup(const struct netdev *netdev)
203 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
205 return xstrdup(netdev_vport_get_dpif_port(netdev, namebuf,
209 /* Whenever the route-table change number is incremented,
210 * netdev_vport_route_changed() should be called to update
211 * the corresponding tunnel interface status. */
213 netdev_vport_route_changed(void)
215 struct netdev **vports;
218 vports = netdev_get_vports(&n_vports);
219 for (i = 0; i < n_vports; i++) {
220 struct netdev *netdev_ = vports[i];
221 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
223 ovs_mutex_lock(&netdev->mutex);
224 /* Finds all tunnel vports. */
225 if (netdev->tnl_cfg.ip_dst) {
226 if (tunnel_check_status_change__(netdev)) {
227 netdev_change_seq_changed(netdev_);
230 ovs_mutex_unlock(&netdev->mutex);
232 netdev_close(netdev_);
238 static struct netdev *
239 netdev_vport_alloc(void)
241 struct netdev_vport *netdev = xzalloc(sizeof *netdev);
246 netdev_vport_construct(struct netdev *netdev_)
248 struct netdev_vport *dev = netdev_vport_cast(netdev_);
249 const char *type = netdev_get_type(netdev_);
251 ovs_mutex_init(&dev->mutex);
252 eth_addr_random(dev->etheraddr);
254 /* Add a default destination port for tunnel ports if none specified. */
255 if (!strcmp(type, "geneve")) {
256 dev->tnl_cfg.dst_port = htons(GENEVE_DST_PORT);
257 } else if (!strcmp(type, "vxlan")) {
258 dev->tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
259 } else if (!strcmp(type, "lisp")) {
260 dev->tnl_cfg.dst_port = htons(LISP_DST_PORT);
261 } else if (!strcmp(type, "stt")) {
262 dev->tnl_cfg.dst_port = htons(STT_DST_PORT);
265 dev->tnl_cfg.dont_fragment = true;
266 dev->tnl_cfg.ttl = DEFAULT_TTL;
271 netdev_vport_destruct(struct netdev *netdev_)
273 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
276 ovs_mutex_destroy(&netdev->mutex);
280 netdev_vport_dealloc(struct netdev *netdev_)
282 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
287 netdev_vport_set_etheraddr(struct netdev *netdev_,
288 const uint8_t mac[ETH_ADDR_LEN])
290 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
292 ovs_mutex_lock(&netdev->mutex);
293 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
294 ovs_mutex_unlock(&netdev->mutex);
295 netdev_change_seq_changed(netdev_);
301 netdev_vport_get_etheraddr(const struct netdev *netdev_,
302 uint8_t mac[ETH_ADDR_LEN])
304 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
306 ovs_mutex_lock(&netdev->mutex);
307 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
308 ovs_mutex_unlock(&netdev->mutex);
313 /* Checks if the tunnel status has changed and returns a boolean.
314 * Updates the tunnel status if it has changed. */
316 tunnel_check_status_change__(struct netdev_vport *netdev)
317 OVS_REQUIRES(netdev->mutex)
319 char iface[IFNAMSIZ];
325 route = netdev->tnl_cfg.ip_dst;
326 if (ovs_router_lookup(route, iface, &gw)) {
327 struct netdev *egress_netdev;
329 if (!netdev_open(iface, "system", &egress_netdev)) {
330 status = netdev_get_carrier(egress_netdev);
331 netdev_close(egress_netdev);
335 if (strcmp(netdev->egress_iface, iface)
336 || netdev->carrier_status != status) {
337 ovs_strlcpy(netdev->egress_iface, iface, IFNAMSIZ);
338 netdev->carrier_status = status;
347 tunnel_get_status(const struct netdev *netdev_, struct smap *smap)
349 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
351 if (netdev->egress_iface[0]) {
352 smap_add(smap, "tunnel_egress_iface", netdev->egress_iface);
354 smap_add(smap, "tunnel_egress_iface_carrier",
355 netdev->carrier_status ? "up" : "down");
362 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
363 enum netdev_flags off,
364 enum netdev_flags on OVS_UNUSED,
365 enum netdev_flags *old_flagsp)
367 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
371 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
376 netdev_vport_run(void)
381 seq = route_table_get_change_seq();
382 if (rt_change_seqno != seq) {
383 rt_change_seqno = seq;
384 netdev_vport_route_changed();
389 netdev_vport_wait(void)
394 seq = route_table_get_change_seq();
395 if (rt_change_seqno != seq) {
396 poll_immediate_wake();
400 /* Code specific to tunnel types. */
403 parse_key(const struct smap *args, const char *name,
404 bool *present, bool *flow)
411 s = smap_get(args, name);
413 s = smap_get(args, "key");
421 if (!strcmp(s, "flow")) {
425 return htonll(strtoull(s, NULL, 0));
430 set_tunnel_config(struct netdev *dev_, const struct smap *args)
432 struct netdev_vport *dev = netdev_vport_cast(dev_);
433 const char *name = netdev_get_name(dev_);
434 const char *type = netdev_get_type(dev_);
435 bool ipsec_mech_set, needs_dst_port, has_csum;
436 struct netdev_tunnel_config tnl_cfg;
437 struct smap_node *node;
439 has_csum = strstr(type, "gre") || strstr(type, "geneve") ||
440 strstr(type, "stt") || strstr(type, "vxlan");
441 ipsec_mech_set = false;
442 memset(&tnl_cfg, 0, sizeof tnl_cfg);
444 /* Add a default destination port for tunnel ports if none specified. */
445 if (!strcmp(type, "geneve")) {
446 tnl_cfg.dst_port = htons(GENEVE_DST_PORT);
449 if (!strcmp(type, "vxlan")) {
450 tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
453 if (!strcmp(type, "lisp")) {
454 tnl_cfg.dst_port = htons(LISP_DST_PORT);
457 if (!strcmp(type, "stt")) {
458 tnl_cfg.dst_port = htons(STT_DST_PORT);
461 needs_dst_port = netdev_vport_needs_dst_port(dev_);
462 tnl_cfg.ipsec = strstr(type, "ipsec");
463 tnl_cfg.dont_fragment = true;
465 SMAP_FOR_EACH (node, args) {
466 if (!strcmp(node->key, "remote_ip")) {
467 struct in_addr in_addr;
468 if (!strcmp(node->value, "flow")) {
469 tnl_cfg.ip_dst_flow = true;
470 tnl_cfg.ip_dst = htonl(0);
471 } else if (lookup_ip(node->value, &in_addr)) {
472 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
473 } else if (ip_is_multicast(in_addr.s_addr)) {
474 VLOG_WARN("%s: multicast remote_ip="IP_FMT" not allowed",
475 name, IP_ARGS(in_addr.s_addr));
478 tnl_cfg.ip_dst = in_addr.s_addr;
480 } else if (!strcmp(node->key, "local_ip")) {
481 struct in_addr in_addr;
482 if (!strcmp(node->value, "flow")) {
483 tnl_cfg.ip_src_flow = true;
484 tnl_cfg.ip_src = htonl(0);
485 } else if (lookup_ip(node->value, &in_addr)) {
486 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
488 tnl_cfg.ip_src = in_addr.s_addr;
490 } else if (!strcmp(node->key, "tos")) {
491 if (!strcmp(node->value, "inherit")) {
492 tnl_cfg.tos_inherit = true;
496 tos = strtol(node->value, &endptr, 0);
497 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
500 VLOG_WARN("%s: invalid TOS %s", name, node->value);
503 } else if (!strcmp(node->key, "ttl")) {
504 if (!strcmp(node->value, "inherit")) {
505 tnl_cfg.ttl_inherit = true;
507 tnl_cfg.ttl = atoi(node->value);
509 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
510 tnl_cfg.dst_port = htons(atoi(node->value));
511 } else if (!strcmp(node->key, "csum") && has_csum) {
512 if (!strcmp(node->value, "true")) {
515 } else if (!strcmp(node->key, "df_default")) {
516 if (!strcmp(node->value, "false")) {
517 tnl_cfg.dont_fragment = false;
519 } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
520 if (smap_get(args, "certificate")) {
521 ipsec_mech_set = true;
523 const char *use_ssl_cert;
525 /* If the "use_ssl_cert" is true, then "certificate" and
526 * "private_key" will be pulled from the SSL table. The
527 * use of this option is strongly discouraged, since it
528 * will like be removed when multiple SSL configurations
529 * are supported by OVS.
531 use_ssl_cert = smap_get(args, "use_ssl_cert");
532 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
533 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
537 ipsec_mech_set = true;
539 } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
540 ipsec_mech_set = true;
541 } else if (tnl_cfg.ipsec
542 && (!strcmp(node->key, "certificate")
543 || !strcmp(node->key, "private_key")
544 || !strcmp(node->key, "use_ssl_cert"))) {
545 /* Ignore options not used by the netdev. */
546 } else if (!strcmp(node->key, "key") ||
547 !strcmp(node->key, "in_key") ||
548 !strcmp(node->key, "out_key")) {
549 /* Handled separately below. */
550 } else if (!strcmp(node->key, "exts")) {
551 char *str = xstrdup(node->value);
552 char *ext, *save_ptr = NULL;
556 ext = strtok_r(str, ",", &save_ptr);
558 if (!strcmp(type, "vxlan") && !strcmp(ext, "gbp")) {
559 tnl_cfg.exts |= (1 << OVS_VXLAN_EXT_GBP);
561 VLOG_WARN("%s: unknown extension '%s'", name, ext);
564 ext = strtok_r(NULL, ",", &save_ptr);
569 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
574 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
575 static pid_t pid = 0;
578 ovs_mutex_lock(&mutex);
580 char *file_name = xasprintf("%s/%s", ovs_rundir(),
581 "ovs-monitor-ipsec.pid");
582 pid = read_pidfile(file_name);
585 ovs_mutex_unlock(&mutex);
589 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
594 if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
595 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
599 if (!ipsec_mech_set) {
600 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
606 if (!tnl_cfg.ip_dst && !tnl_cfg.ip_dst_flow) {
607 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
611 if (tnl_cfg.ip_src_flow && !tnl_cfg.ip_dst_flow) {
612 VLOG_ERR("%s: %s type requires 'remote_ip=flow' with 'local_ip=flow'",
617 tnl_cfg.ttl = DEFAULT_TTL;
620 tnl_cfg.in_key = parse_key(args, "in_key",
621 &tnl_cfg.in_key_present,
622 &tnl_cfg.in_key_flow);
624 tnl_cfg.out_key = parse_key(args, "out_key",
625 &tnl_cfg.out_key_present,
626 &tnl_cfg.out_key_flow);
628 ovs_mutex_lock(&dev->mutex);
629 if (memcmp(&dev->tnl_cfg, &tnl_cfg, sizeof tnl_cfg)) {
630 dev->tnl_cfg = tnl_cfg;
631 tunnel_check_status_change__(dev);
632 netdev_change_seq_changed(dev_);
634 ovs_mutex_unlock(&dev->mutex);
640 get_tunnel_config(const struct netdev *dev, struct smap *args)
642 struct netdev_vport *netdev = netdev_vport_cast(dev);
643 struct netdev_tunnel_config tnl_cfg;
645 ovs_mutex_lock(&netdev->mutex);
646 tnl_cfg = netdev->tnl_cfg;
647 ovs_mutex_unlock(&netdev->mutex);
649 if (tnl_cfg.ip_dst) {
650 smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_dst));
651 } else if (tnl_cfg.ip_dst_flow) {
652 smap_add(args, "remote_ip", "flow");
655 if (tnl_cfg.ip_src) {
656 smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(tnl_cfg.ip_src));
657 } else if (tnl_cfg.ip_src_flow) {
658 smap_add(args, "local_ip", "flow");
661 if (tnl_cfg.in_key_flow && tnl_cfg.out_key_flow) {
662 smap_add(args, "key", "flow");
663 } else if (tnl_cfg.in_key_present && tnl_cfg.out_key_present
664 && tnl_cfg.in_key == tnl_cfg.out_key) {
665 smap_add_format(args, "key", "%"PRIu64, ntohll(tnl_cfg.in_key));
667 if (tnl_cfg.in_key_flow) {
668 smap_add(args, "in_key", "flow");
669 } else if (tnl_cfg.in_key_present) {
670 smap_add_format(args, "in_key", "%"PRIu64,
671 ntohll(tnl_cfg.in_key));
674 if (tnl_cfg.out_key_flow) {
675 smap_add(args, "out_key", "flow");
676 } else if (tnl_cfg.out_key_present) {
677 smap_add_format(args, "out_key", "%"PRIu64,
678 ntohll(tnl_cfg.out_key));
682 if (tnl_cfg.ttl_inherit) {
683 smap_add(args, "ttl", "inherit");
684 } else if (tnl_cfg.ttl != DEFAULT_TTL) {
685 smap_add_format(args, "ttl", "%"PRIu8, tnl_cfg.ttl);
688 if (tnl_cfg.tos_inherit) {
689 smap_add(args, "tos", "inherit");
690 } else if (tnl_cfg.tos) {
691 smap_add_format(args, "tos", "0x%x", tnl_cfg.tos);
694 if (tnl_cfg.dst_port) {
695 uint16_t dst_port = ntohs(tnl_cfg.dst_port);
696 const char *type = netdev_get_type(dev);
698 if ((!strcmp("geneve", type) && dst_port != GENEVE_DST_PORT) ||
699 (!strcmp("vxlan", type) && dst_port != VXLAN_DST_PORT) ||
700 (!strcmp("lisp", type) && dst_port != LISP_DST_PORT) ||
701 (!strcmp("stt", type) && dst_port != STT_DST_PORT)) {
702 smap_add_format(args, "dst_port", "%d", dst_port);
707 smap_add(args, "csum", "true");
710 if (!tnl_cfg.dont_fragment) {
711 smap_add(args, "df_default", "false");
717 /* Code specific to patch ports. */
719 /* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
720 * string that the caller must free.
722 * If 'netdev' is not a patch port, returns NULL. */
724 netdev_vport_patch_peer(const struct netdev *netdev_)
728 if (netdev_vport_is_patch(netdev_)) {
729 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
731 ovs_mutex_lock(&netdev->mutex);
733 peer = xstrdup(netdev->peer);
735 ovs_mutex_unlock(&netdev->mutex);
742 netdev_vport_inc_rx(const struct netdev *netdev,
743 const struct dpif_flow_stats *stats)
745 if (is_vport_class(netdev_get_class(netdev))) {
746 struct netdev_vport *dev = netdev_vport_cast(netdev);
748 ovs_mutex_lock(&dev->mutex);
749 dev->stats.rx_packets += stats->n_packets;
750 dev->stats.rx_bytes += stats->n_bytes;
751 ovs_mutex_unlock(&dev->mutex);
756 netdev_vport_inc_tx(const struct netdev *netdev,
757 const struct dpif_flow_stats *stats)
759 if (is_vport_class(netdev_get_class(netdev))) {
760 struct netdev_vport *dev = netdev_vport_cast(netdev);
762 ovs_mutex_lock(&dev->mutex);
763 dev->stats.tx_packets += stats->n_packets;
764 dev->stats.tx_bytes += stats->n_bytes;
765 ovs_mutex_unlock(&dev->mutex);
770 get_patch_config(const struct netdev *dev_, struct smap *args)
772 struct netdev_vport *dev = netdev_vport_cast(dev_);
774 ovs_mutex_lock(&dev->mutex);
776 smap_add(args, "peer", dev->peer);
778 ovs_mutex_unlock(&dev->mutex);
784 set_patch_config(struct netdev *dev_, const struct smap *args)
786 struct netdev_vport *dev = netdev_vport_cast(dev_);
787 const char *name = netdev_get_name(dev_);
790 peer = smap_get(args, "peer");
792 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
796 if (smap_count(args) > 1) {
797 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
801 if (!strcmp(name, peer)) {
802 VLOG_ERR("%s: patch peer must not be self", name);
806 ovs_mutex_lock(&dev->mutex);
807 if (!dev->peer || strcmp(dev->peer, peer)) {
809 dev->peer = xstrdup(peer);
810 netdev_change_seq_changed(dev_);
812 ovs_mutex_unlock(&dev->mutex);
818 get_stats(const struct netdev *netdev, struct netdev_stats *stats)
820 struct netdev_vport *dev = netdev_vport_cast(netdev);
822 ovs_mutex_lock(&dev->mutex);
824 ovs_mutex_unlock(&dev->mutex);
830 /* Tunnel push pop ops. */
832 static struct ip_header *
835 return (void *)((char *)eth + sizeof (struct eth_header));
838 static struct gre_base_hdr *
839 gre_hdr(struct ip_header *ip)
841 return (void *)((char *)ip + sizeof (struct ip_header));
845 ip_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl)
847 struct ip_header *nh;
850 nh = dp_packet_l3(packet);
851 l4 = dp_packet_l4(packet);
857 tnl->ip_src = get_16aligned_be32(&nh->ip_src);
858 tnl->ip_dst = get_16aligned_be32(&nh->ip_dst);
859 tnl->ip_tos = nh->ip_tos;
860 tnl->ip_ttl = nh->ip_ttl;
865 /* Pushes the 'size' bytes of 'header' into the headroom of 'packet',
866 * reallocating the packet if necessary. 'header' should contain an Ethernet
867 * header, followed by an IPv4 header (without options), and an L4 header.
869 * This function sets the IP header's ip_tot_len field (which should be zeroed
870 * as part of 'header') and puts its value into '*ip_tot_size' as well. Also
871 * updates IP header checksum.
873 * Return pointer to the L4 header added to 'packet'. */
875 push_ip_header(struct dp_packet *packet,
876 const void *header, int size, int *ip_tot_size)
878 struct eth_header *eth;
879 struct ip_header *ip;
881 eth = dp_packet_push_uninit(packet, size);
882 *ip_tot_size = dp_packet_size(packet) - sizeof (struct eth_header);
884 memcpy(eth, header, size);
886 ip->ip_tot_len = htons(*ip_tot_size);
889 ip->ip_csum = recalc_csum16(ip->ip_csum, 0, ip->ip_tot_len);
895 udp_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl)
897 struct udp_header *udp;
899 udp = ip_extract_tnl_md(packet, tnl);
905 uint32_t csum = packet_csum_pseudoheader(dp_packet_l3(packet));
907 csum = csum_continue(csum, udp, dp_packet_size(packet) -
908 ((const unsigned char *)udp -
909 (const unsigned char *)dp_packet_l2(packet)));
910 if (csum_finish(csum)) {
913 tnl->flags |= FLOW_TNL_F_CSUM;
916 tnl->tp_src = udp->udp_src;
917 tnl->tp_dst = udp->udp_dst;
923 get_src_port(struct dp_packet *packet)
927 hash = dp_packet_get_rss_hash(packet);
929 return htons((((uint64_t) hash * (tnl_udp_port_max - tnl_udp_port_min)) >> 32) +
934 push_udp_header(struct dp_packet *packet,
935 const struct ovs_action_push_tnl *data)
937 struct udp_header *udp;
940 udp = push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
942 /* set udp src port */
943 udp->udp_src = get_src_port(packet);
944 udp->udp_len = htons(ip_tot_size - sizeof (struct ip_header));
947 uint32_t csum = packet_csum_pseudoheader(ip_hdr(dp_packet_data(packet)));
949 csum = csum_continue(csum, udp,
950 ip_tot_size - sizeof (struct ip_header));
951 udp->udp_csum = csum_finish(csum);
953 if (!udp->udp_csum) {
954 udp->udp_csum = htons(0xffff);
960 udp_build_header(struct netdev_tunnel_config *tnl_cfg,
961 const struct flow *tnl_flow,
962 struct ovs_action_push_tnl *data)
964 struct ip_header *ip;
965 struct udp_header *udp;
967 ip = ip_hdr(data->header);
968 ip->ip_proto = IPPROTO_UDP;
970 udp = (struct udp_header *) (ip + 1);
971 udp->udp_dst = tnl_cfg->dst_port;
973 if (tnl_flow->tunnel.flags & FLOW_TNL_F_CSUM) {
974 /* Write a value in now to mark that we should compute the checksum
975 * later. 0xffff is handy because it is transparent to the
977 udp->udp_csum = htons(0xffff);
984 gre_header_len(ovs_be16 flags)
986 int hlen = sizeof(struct eth_header) +
987 sizeof(struct ip_header) + 4;
989 if (flags & htons(GRE_CSUM)) {
992 if (flags & htons(GRE_KEY)) {
995 if (flags & htons(GRE_SEQ)) {
1002 parse_gre_header(struct dp_packet *packet,
1003 struct flow_tnl *tnl)
1005 const struct gre_base_hdr *greh;
1006 ovs_16aligned_be32 *options;
1009 greh = ip_extract_tnl_md(packet, tnl);
1014 if (greh->flags & ~(htons(GRE_CSUM | GRE_KEY | GRE_SEQ))) {
1018 if (greh->protocol != htons(ETH_TYPE_TEB)) {
1022 hlen = gre_header_len(greh->flags);
1023 if (hlen > dp_packet_size(packet)) {
1027 options = (ovs_16aligned_be32 *)(greh + 1);
1028 if (greh->flags & htons(GRE_CSUM)) {
1031 pkt_csum = csum(greh, dp_packet_size(packet) -
1032 ((const unsigned char *)greh -
1033 (const unsigned char *)dp_packet_l2(packet)));
1037 tnl->flags = FLOW_TNL_F_CSUM;
1041 if (greh->flags & htons(GRE_KEY)) {
1042 tnl->tun_id = (OVS_FORCE ovs_be64) ((OVS_FORCE uint64_t)(get_16aligned_be32(options)) << 32);
1043 tnl->flags |= FLOW_TNL_F_KEY;
1047 if (greh->flags & htons(GRE_SEQ)) {
1055 netdev_gre_pop_header(struct dp_packet *packet)
1057 struct pkt_metadata *md = &packet->md;
1058 struct flow_tnl *tnl = &md->tunnel;
1059 int hlen = sizeof(struct eth_header) +
1060 sizeof(struct ip_header) + 4;
1062 memset(md, 0, sizeof *md);
1063 if (hlen > dp_packet_size(packet)) {
1067 hlen = parse_gre_header(packet, tnl);
1072 dp_packet_reset_packet(packet, hlen);
1078 netdev_gre_push_header(struct dp_packet *packet,
1079 const struct ovs_action_push_tnl *data)
1081 struct gre_base_hdr *greh;
1084 greh = push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
1086 if (greh->flags & htons(GRE_CSUM)) {
1087 ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1);
1088 *csum_opt = csum(greh, ip_tot_size - sizeof (struct ip_header));
1093 netdev_gre_build_header(const struct netdev *netdev,
1094 struct ovs_action_push_tnl *data,
1095 const struct flow *tnl_flow)
1097 struct netdev_vport *dev = netdev_vport_cast(netdev);
1098 struct netdev_tunnel_config *tnl_cfg;
1099 struct ip_header *ip;
1100 struct gre_base_hdr *greh;
1101 ovs_16aligned_be32 *options;
1104 /* XXX: RCUfy tnl_cfg. */
1105 ovs_mutex_lock(&dev->mutex);
1106 tnl_cfg = &dev->tnl_cfg;
1108 ip = ip_hdr(data->header);
1109 ip->ip_proto = IPPROTO_GRE;
1112 greh->protocol = htons(ETH_TYPE_TEB);
1115 options = (ovs_16aligned_be32 *) (greh + 1);
1116 if (tnl_flow->tunnel.flags & FLOW_TNL_F_CSUM) {
1117 greh->flags |= htons(GRE_CSUM);
1118 put_16aligned_be32(options, 0);
1122 if (tnl_cfg->out_key_present) {
1123 greh->flags |= htons(GRE_KEY);
1124 put_16aligned_be32(options, (OVS_FORCE ovs_be32)
1125 ((OVS_FORCE uint64_t) tnl_flow->tunnel.tun_id >> 32));
1129 ovs_mutex_unlock(&dev->mutex);
1131 hlen = (uint8_t *) options - (uint8_t *) greh;
1133 data->header_len = sizeof(struct eth_header) +
1134 sizeof(struct ip_header) + hlen;
1135 data->tnl_type = OVS_VPORT_TYPE_GRE;
1140 netdev_vxlan_pop_header(struct dp_packet *packet)
1142 struct pkt_metadata *md = &packet->md;
1143 struct flow_tnl *tnl = &md->tunnel;
1144 struct vxlanhdr *vxh;
1146 memset(md, 0, sizeof *md);
1147 if (VXLAN_HLEN > dp_packet_size(packet)) {
1151 vxh = udp_extract_tnl_md(packet, tnl);
1156 if (get_16aligned_be32(&vxh->vx_flags) != htonl(VXLAN_FLAGS) ||
1157 (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) {
1158 VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n",
1159 ntohl(get_16aligned_be32(&vxh->vx_flags)),
1160 ntohl(get_16aligned_be32(&vxh->vx_vni)));
1163 tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
1164 tnl->flags |= FLOW_TNL_F_KEY;
1166 dp_packet_reset_packet(packet, VXLAN_HLEN);
1172 netdev_vxlan_build_header(const struct netdev *netdev,
1173 struct ovs_action_push_tnl *data,
1174 const struct flow *tnl_flow)
1176 struct netdev_vport *dev = netdev_vport_cast(netdev);
1177 struct netdev_tunnel_config *tnl_cfg;
1178 struct vxlanhdr *vxh;
1180 /* XXX: RCUfy tnl_cfg. */
1181 ovs_mutex_lock(&dev->mutex);
1182 tnl_cfg = &dev->tnl_cfg;
1184 vxh = udp_build_header(tnl_cfg, tnl_flow, data);
1186 put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS));
1187 put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
1189 ovs_mutex_unlock(&dev->mutex);
1190 data->header_len = VXLAN_HLEN;
1191 data->tnl_type = OVS_VPORT_TYPE_VXLAN;
1196 netdev_geneve_pop_header(struct dp_packet *packet)
1198 struct pkt_metadata *md = &packet->md;
1199 struct flow_tnl *tnl = &md->tunnel;
1200 struct genevehdr *gnh;
1203 memset(md, 0, sizeof *md);
1204 if (GENEVE_BASE_HLEN > dp_packet_size(packet)) {
1205 VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%u\n",
1206 (unsigned int)GENEVE_BASE_HLEN, dp_packet_size(packet));
1210 gnh = udp_extract_tnl_md(packet, tnl);
1215 hlen = GENEVE_BASE_HLEN + gnh->opt_len * 4;
1216 if (hlen > dp_packet_size(packet)) {
1217 VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n",
1218 hlen, dp_packet_size(packet));
1222 if (gnh->ver != 0) {
1223 VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver);
1227 if (gnh->opt_len && gnh->critical) {
1228 VLOG_WARN_RL(&err_rl, "unknown geneve critical options: %"PRIu8" bytes\n",
1233 if (gnh->proto_type != htons(ETH_TYPE_TEB)) {
1234 VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n",
1235 ntohs(gnh->proto_type));
1239 tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0;
1240 tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
1241 tnl->flags |= FLOW_TNL_F_KEY;
1243 dp_packet_reset_packet(packet, hlen);
1249 netdev_geneve_build_header(const struct netdev *netdev,
1250 struct ovs_action_push_tnl *data,
1251 const struct flow *tnl_flow)
1253 struct netdev_vport *dev = netdev_vport_cast(netdev);
1254 struct netdev_tunnel_config *tnl_cfg;
1255 struct genevehdr *gnh;
1257 /* XXX: RCUfy tnl_cfg. */
1258 ovs_mutex_lock(&dev->mutex);
1259 tnl_cfg = &dev->tnl_cfg;
1261 gnh = udp_build_header(tnl_cfg, tnl_flow, data);
1263 gnh->oam = !!(tnl_flow->tunnel.flags & FLOW_TNL_F_OAM);
1264 gnh->proto_type = htons(ETH_TYPE_TEB);
1265 put_16aligned_be32(&gnh->vni, htonl(ntohll(tnl_flow->tunnel.tun_id) << 8));
1267 ovs_mutex_unlock(&dev->mutex);
1268 data->header_len = GENEVE_BASE_HLEN;
1269 data->tnl_type = OVS_VPORT_TYPE_GENEVE;
1274 netdev_vport_range(struct unixctl_conn *conn, int argc,
1275 const char *argv[], void *aux OVS_UNUSED)
1280 struct ds ds = DS_EMPTY_INITIALIZER;
1282 ds_put_format(&ds, "Tunnel UDP source port range: %"PRIu16"-%"PRIu16"\n",
1283 tnl_udp_port_min, tnl_udp_port_max);
1285 unixctl_command_reply(conn, ds_cstr(&ds));
1294 val1 = atoi(argv[1]);
1295 if (val1 <= 0 || val1 > UINT16_MAX) {
1296 unixctl_command_reply(conn, "Invalid min.");
1299 val2 = atoi(argv[2]);
1300 if (val2 <= 0 || val2 > UINT16_MAX) {
1301 unixctl_command_reply(conn, "Invalid max.");
1306 tnl_udp_port_min = val2;
1307 tnl_udp_port_max = val1;
1309 tnl_udp_port_min = val1;
1310 tnl_udp_port_max = val2;
1312 seq_change(tnl_conf_seq);
1314 unixctl_command_reply(conn, "OK");
1318 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG, \
1319 GET_TUNNEL_CONFIG, GET_STATUS, \
1321 PUSH_HEADER, POP_HEADER) \
1324 netdev_vport_wait, \
1326 netdev_vport_alloc, \
1327 netdev_vport_construct, \
1328 netdev_vport_destruct, \
1329 netdev_vport_dealloc, \
1332 GET_TUNNEL_CONFIG, \
1336 NULL, /* get_numa_id */ \
1337 NULL, /* set_multiq */ \
1340 NULL, /* send_wait */ \
1342 netdev_vport_set_etheraddr, \
1343 netdev_vport_get_etheraddr, \
1344 NULL, /* get_mtu */ \
1345 NULL, /* set_mtu */ \
1346 NULL, /* get_ifindex */ \
1347 NULL, /* get_carrier */ \
1348 NULL, /* get_carrier_resets */ \
1349 NULL, /* get_miimon */ \
1352 NULL, /* get_features */ \
1353 NULL, /* set_advertisements */ \
1355 NULL, /* set_policing */ \
1356 NULL, /* get_qos_types */ \
1357 NULL, /* get_qos_capabilities */ \
1358 NULL, /* get_qos */ \
1359 NULL, /* set_qos */ \
1360 NULL, /* get_queue */ \
1361 NULL, /* set_queue */ \
1362 NULL, /* delete_queue */ \
1363 NULL, /* get_queue_stats */ \
1364 NULL, /* queue_dump_start */ \
1365 NULL, /* queue_dump_next */ \
1366 NULL, /* queue_dump_done */ \
1367 NULL, /* dump_queue_stats */ \
1369 NULL, /* get_in4 */ \
1370 NULL, /* set_in4 */ \
1371 NULL, /* get_in6 */ \
1372 NULL, /* add_router */ \
1373 NULL, /* get_next_hop */ \
1375 NULL, /* arp_lookup */ \
1377 netdev_vport_update_flags, \
1379 NULL, /* rx_alloc */ \
1380 NULL, /* rx_construct */ \
1381 NULL, /* rx_destruct */ \
1382 NULL, /* rx_dealloc */ \
1383 NULL, /* rx_recv */ \
1384 NULL, /* rx_wait */ \
1385 NULL, /* rx_drain */
1388 #define TUNNEL_CLASS(NAME, DPIF_PORT, BUILD_HEADER, PUSH_HEADER, POP_HEADER) \
1390 { NAME, VPORT_FUNCTIONS(get_tunnel_config, \
1391 set_tunnel_config, \
1392 get_netdev_tunnel_config, \
1393 tunnel_get_status, \
1394 BUILD_HEADER, PUSH_HEADER, POP_HEADER) }}
1397 netdev_vport_tunnel_register(void)
1399 /* The name of the dpif_port should be short enough to accomodate adding
1400 * a port number to the end if one is necessary. */
1401 static const struct vport_class vport_classes[] = {
1402 TUNNEL_CLASS("geneve", "genev_sys", netdev_geneve_build_header,
1404 netdev_geneve_pop_header),
1405 TUNNEL_CLASS("gre", "gre_sys", netdev_gre_build_header,
1406 netdev_gre_push_header,
1407 netdev_gre_pop_header),
1408 TUNNEL_CLASS("ipsec_gre", "gre_sys", NULL, NULL, NULL),
1409 TUNNEL_CLASS("gre64", "gre64_sys", NULL, NULL, NULL),
1410 TUNNEL_CLASS("ipsec_gre64", "gre64_sys", NULL, NULL, NULL),
1411 TUNNEL_CLASS("vxlan", "vxlan_sys", netdev_vxlan_build_header,
1413 netdev_vxlan_pop_header),
1414 TUNNEL_CLASS("lisp", "lisp_sys", NULL, NULL, NULL),
1415 TUNNEL_CLASS("stt", "stt_sys", NULL, NULL, NULL),
1417 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1419 if (ovsthread_once_start(&once)) {
1422 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1423 netdev_register_provider(&vport_classes[i].netdev_class);
1426 unixctl_command_register("tnl/egress_port_range", "min max", 0, 2,
1427 netdev_vport_range, NULL);
1429 ovsthread_once_done(&once);
1434 netdev_vport_patch_register(void)
1436 static const struct vport_class patch_class =
1438 { "patch", VPORT_FUNCTIONS(get_patch_config,
1441 NULL, NULL, NULL, NULL) }};
1442 netdev_register_provider(&patch_class.netdev_class);