2 * Copyright (c) 2016 Nicira, Inc.
3 * Copyright (c) 2016 Red Hat, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "netdev-native-tnl.h"
24 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip6.h>
29 #include <sys/ioctl.h>
35 #include "byte-order.h"
37 #include "dp-packet.h"
39 #include "netdev-vport.h"
40 #include "netdev-vport-private.h"
41 #include "odp-netlink.h"
44 #include "unaligned.h"
46 #include "openvswitch/vlog.h"
48 VLOG_DEFINE_THIS_MODULE(native_tnl);
49 static struct vlog_rate_limit err_rl = VLOG_RATE_LIMIT_INIT(60, 5);
51 #define VXLAN_HLEN (sizeof(struct udp_header) + \
52 sizeof(struct vxlanhdr))
54 #define GENEVE_BASE_HLEN (sizeof(struct udp_header) + \
55 sizeof(struct genevehdr))
57 uint16_t tnl_udp_port_min = 32768;
58 uint16_t tnl_udp_port_max = 61000;
61 netdev_tnl_ip_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl,
66 struct ovs_16aligned_ip6_hdr *ip6;
70 nh = dp_packet_l3(packet);
73 l4 = dp_packet_l4(packet);
79 *hlen = sizeof(struct eth_header);
81 l3_size = dp_packet_size(packet) -
82 ((char *)nh - (char *)dp_packet_data(packet));
84 if (IP_VER(ip->ip_ihl_ver) == 4) {
86 ovs_be32 ip_src, ip_dst;
88 if (csum(ip, IP_IHL(ip->ip_ihl_ver) * 4)) {
89 VLOG_WARN_RL(&err_rl, "ip packet has invalid checksum");
93 if (ntohs(ip->ip_tot_len) > l3_size) {
94 VLOG_WARN_RL(&err_rl, "ip packet is truncated (IP length %d, actual %d)",
95 ntohs(ip->ip_tot_len), l3_size);
98 if (IP_IHL(ip->ip_ihl_ver) * 4 > sizeof(struct ip_header)) {
99 VLOG_WARN_RL(&err_rl, "ip options not supported on tunnel packets "
100 "(%d bytes)", IP_IHL(ip->ip_ihl_ver) * 4);
104 ip_src = get_16aligned_be32(&ip->ip_src);
105 ip_dst = get_16aligned_be32(&ip->ip_dst);
107 tnl->ip_src = ip_src;
108 tnl->ip_dst = ip_dst;
109 tnl->ip_tos = ip->ip_tos;
110 tnl->ip_ttl = ip->ip_ttl;
112 *hlen += IP_HEADER_LEN;
114 } else if (IP_VER(ip->ip_ihl_ver) == 6) {
115 ovs_be32 tc_flow = get_16aligned_be32(&ip6->ip6_flow);
117 memcpy(tnl->ipv6_src.s6_addr, ip6->ip6_src.be16, sizeof ip6->ip6_src);
118 memcpy(tnl->ipv6_dst.s6_addr, ip6->ip6_dst.be16, sizeof ip6->ip6_dst);
120 tnl->ip_tos = ntohl(tc_flow) >> 20;
121 tnl->ip_ttl = ip6->ip6_hlim;
123 *hlen += IPV6_HEADER_LEN;
126 VLOG_WARN_RL(&err_rl, "ipv4 packet has invalid version (%d)",
127 IP_VER(ip->ip_ihl_ver));
134 /* Pushes the 'size' bytes of 'header' into the headroom of 'packet',
135 * reallocating the packet if necessary. 'header' should contain an Ethernet
136 * header, followed by an IPv4 header (without options), and an L4 header.
138 * This function sets the IP header's ip_tot_len field (which should be zeroed
139 * as part of 'header') and puts its value into '*ip_tot_size' as well. Also
140 * updates IP header checksum.
142 * Return pointer to the L4 header added to 'packet'. */
144 netdev_tnl_push_ip_header(struct dp_packet *packet,
145 const void *header, int size, int *ip_tot_size)
147 struct eth_header *eth;
148 struct ip_header *ip;
149 struct ovs_16aligned_ip6_hdr *ip6;
151 eth = dp_packet_push_uninit(packet, size);
152 *ip_tot_size = dp_packet_size(packet) - sizeof (struct eth_header);
154 memcpy(eth, header, size);
156 if (netdev_tnl_is_header_ipv6(header)) {
157 ip6 = netdev_tnl_ipv6_hdr(eth);
158 *ip_tot_size -= IPV6_HEADER_LEN;
159 ip6->ip6_plen = htons(*ip_tot_size);
162 ip = netdev_tnl_ip_hdr(eth);
163 ip->ip_tot_len = htons(*ip_tot_size);
164 ip->ip_csum = recalc_csum16(ip->ip_csum, 0, ip->ip_tot_len);
165 *ip_tot_size -= IP_HEADER_LEN;
171 udp_extract_tnl_md(struct dp_packet *packet, struct flow_tnl *tnl,
174 struct udp_header *udp;
176 udp = netdev_tnl_ip_extract_tnl_md(packet, tnl, hlen);
183 if (netdev_tnl_is_header_ipv6(dp_packet_data(packet))) {
184 csum = packet_csum_pseudoheader6(dp_packet_l3(packet));
186 csum = packet_csum_pseudoheader(dp_packet_l3(packet));
189 csum = csum_continue(csum, udp, dp_packet_size(packet) -
190 ((const unsigned char *)udp -
191 (const unsigned char *)dp_packet_l2(packet)));
192 if (csum_finish(csum)) {
195 tnl->flags |= FLOW_TNL_F_CSUM;
198 tnl->tp_src = udp->udp_src;
199 tnl->tp_dst = udp->udp_dst;
206 netdev_tnl_push_udp_header(struct dp_packet *packet,
207 const struct ovs_action_push_tnl *data)
209 struct udp_header *udp;
212 udp = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
214 /* set udp src port */
215 udp->udp_src = netdev_tnl_get_src_port(packet);
216 udp->udp_len = htons(ip_tot_size);
220 if (netdev_tnl_is_header_ipv6(dp_packet_data(packet))) {
221 csum = packet_csum_pseudoheader6(netdev_tnl_ipv6_hdr(dp_packet_data(packet)));
223 csum = packet_csum_pseudoheader(netdev_tnl_ip_hdr(dp_packet_data(packet)));
226 csum = csum_continue(csum, udp, ip_tot_size);
227 udp->udp_csum = csum_finish(csum);
229 if (!udp->udp_csum) {
230 udp->udp_csum = htons(0xffff);
236 eth_build_header(struct ovs_action_push_tnl *data,
237 const struct netdev_tnl_build_header_params *params)
239 uint16_t eth_proto = params->is_ipv6 ? ETH_TYPE_IPV6 : ETH_TYPE_IP;
240 struct eth_header *eth;
242 memset(data->header, 0, sizeof data->header);
244 eth = (struct eth_header *)data->header;
245 eth->eth_dst = params->dmac;
246 eth->eth_src = params->smac;
247 eth->eth_type = htons(eth_proto);
248 data->header_len = sizeof(struct eth_header);
253 netdev_tnl_ip_build_header(struct ovs_action_push_tnl *data,
254 const struct netdev_tnl_build_header_params *params,
259 l3 = eth_build_header(data, params);
260 if (!params->is_ipv6) {
261 ovs_be32 ip_src = in6_addr_get_mapped_ipv4(params->s_ip);
262 struct ip_header *ip;
264 ip = (struct ip_header *) l3;
266 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
267 ip->ip_tos = params->flow->tunnel.ip_tos;
268 ip->ip_ttl = params->flow->tunnel.ip_ttl;
269 ip->ip_proto = next_proto;
270 put_16aligned_be32(&ip->ip_src, ip_src);
271 put_16aligned_be32(&ip->ip_dst, params->flow->tunnel.ip_dst);
273 ip->ip_frag_off = (params->flow->tunnel.flags & FLOW_TNL_F_DONT_FRAGMENT) ?
276 ip->ip_csum = csum(ip, sizeof *ip);
278 data->header_len += IP_HEADER_LEN;
281 struct ovs_16aligned_ip6_hdr *ip6;
283 ip6 = (struct ovs_16aligned_ip6_hdr *) l3;
285 put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) |
286 htonl(params->flow->tunnel.ip_tos << 20));
287 ip6->ip6_hlim = params->flow->tunnel.ip_ttl;
288 ip6->ip6_nxt = next_proto;
289 memcpy(&ip6->ip6_src, params->s_ip, sizeof(ovs_be32[4]));
290 memcpy(&ip6->ip6_dst, ¶ms->flow->tunnel.ipv6_dst, sizeof(ovs_be32[4]));
292 data->header_len += IPV6_HEADER_LEN;
298 udp_build_header(struct netdev_tunnel_config *tnl_cfg,
299 struct ovs_action_push_tnl *data,
300 const struct netdev_tnl_build_header_params *params)
302 struct udp_header *udp;
304 udp = netdev_tnl_ip_build_header(data, params, IPPROTO_UDP);
305 udp->udp_dst = tnl_cfg->dst_port;
307 if (params->is_ipv6 || params->flow->tunnel.flags & FLOW_TNL_F_CSUM) {
308 /* Write a value in now to mark that we should compute the checksum
309 * later. 0xffff is handy because it is transparent to the
311 udp->udp_csum = htons(0xffff);
313 data->header_len += sizeof *udp;
318 gre_header_len(ovs_be16 flags)
322 if (flags & htons(GRE_CSUM)) {
325 if (flags & htons(GRE_KEY)) {
328 if (flags & htons(GRE_SEQ)) {
335 parse_gre_header(struct dp_packet *packet,
336 struct flow_tnl *tnl)
338 const struct gre_base_hdr *greh;
339 ovs_16aligned_be32 *options;
343 greh = netdev_tnl_ip_extract_tnl_md(packet, tnl, &ulen);
348 if (greh->flags & ~(htons(GRE_CSUM | GRE_KEY | GRE_SEQ))) {
352 if (greh->protocol != htons(ETH_TYPE_TEB)) {
356 hlen = ulen + gre_header_len(greh->flags);
357 if (hlen > dp_packet_size(packet)) {
361 options = (ovs_16aligned_be32 *)(greh + 1);
362 if (greh->flags & htons(GRE_CSUM)) {
365 pkt_csum = csum(greh, dp_packet_size(packet) -
366 ((const unsigned char *)greh -
367 (const unsigned char *)dp_packet_l2(packet)));
371 tnl->flags = FLOW_TNL_F_CSUM;
375 if (greh->flags & htons(GRE_KEY)) {
376 tnl->tun_id = be32_to_be64(get_16aligned_be32(options));
377 tnl->flags |= FLOW_TNL_F_KEY;
381 if (greh->flags & htons(GRE_SEQ)) {
389 netdev_gre_pop_header(struct dp_packet *packet)
391 struct pkt_metadata *md = &packet->md;
392 struct flow_tnl *tnl = &md->tunnel;
393 int hlen = sizeof(struct eth_header) + 4;
395 hlen += netdev_tnl_is_header_ipv6(dp_packet_data(packet)) ?
396 IPV6_HEADER_LEN : IP_HEADER_LEN;
398 pkt_metadata_init_tnl(md);
399 if (hlen > dp_packet_size(packet)) {
403 hlen = parse_gre_header(packet, tnl);
408 dp_packet_reset_packet(packet, hlen);
412 dp_packet_delete(packet);
417 netdev_gre_push_header(struct dp_packet *packet,
418 const struct ovs_action_push_tnl *data)
420 struct gre_base_hdr *greh;
423 greh = netdev_tnl_push_ip_header(packet, data->header, data->header_len, &ip_tot_size);
425 if (greh->flags & htons(GRE_CSUM)) {
426 ovs_be16 *csum_opt = (ovs_be16 *) (greh + 1);
427 *csum_opt = csum(greh, ip_tot_size);
432 netdev_gre_build_header(const struct netdev *netdev,
433 struct ovs_action_push_tnl *data,
434 const struct netdev_tnl_build_header_params *params)
436 struct netdev_vport *dev = netdev_vport_cast(netdev);
437 struct netdev_tunnel_config *tnl_cfg;
438 struct gre_base_hdr *greh;
439 ovs_16aligned_be32 *options;
442 /* XXX: RCUfy tnl_cfg. */
443 ovs_mutex_lock(&dev->mutex);
444 tnl_cfg = &dev->tnl_cfg;
446 greh = netdev_tnl_ip_build_header(data, params, IPPROTO_GRE);
448 greh->protocol = htons(ETH_TYPE_TEB);
451 options = (ovs_16aligned_be32 *) (greh + 1);
452 if (params->flow->tunnel.flags & FLOW_TNL_F_CSUM) {
453 greh->flags |= htons(GRE_CSUM);
454 put_16aligned_be32(options, 0);
458 if (tnl_cfg->out_key_present) {
459 greh->flags |= htons(GRE_KEY);
460 put_16aligned_be32(options, be64_to_be32(params->flow->tunnel.tun_id));
464 ovs_mutex_unlock(&dev->mutex);
466 hlen = (uint8_t *) options - (uint8_t *) greh;
468 data->header_len += hlen;
469 data->tnl_type = OVS_VPORT_TYPE_GRE;
474 netdev_vxlan_pop_header(struct dp_packet *packet)
476 struct pkt_metadata *md = &packet->md;
477 struct flow_tnl *tnl = &md->tunnel;
478 struct vxlanhdr *vxh;
481 pkt_metadata_init_tnl(md);
482 if (VXLAN_HLEN > dp_packet_l4_size(packet)) {
486 vxh = udp_extract_tnl_md(packet, tnl, &hlen);
491 if (get_16aligned_be32(&vxh->vx_flags) != htonl(VXLAN_FLAGS) ||
492 (get_16aligned_be32(&vxh->vx_vni) & htonl(0xff))) {
493 VLOG_WARN_RL(&err_rl, "invalid vxlan flags=%#x vni=%#x\n",
494 ntohl(get_16aligned_be32(&vxh->vx_flags)),
495 ntohl(get_16aligned_be32(&vxh->vx_vni)));
498 tnl->tun_id = htonll(ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
499 tnl->flags |= FLOW_TNL_F_KEY;
501 dp_packet_reset_packet(packet, hlen + VXLAN_HLEN);
505 dp_packet_delete(packet);
510 netdev_vxlan_build_header(const struct netdev *netdev,
511 struct ovs_action_push_tnl *data,
512 const struct netdev_tnl_build_header_params *params)
514 struct netdev_vport *dev = netdev_vport_cast(netdev);
515 struct netdev_tunnel_config *tnl_cfg;
516 struct vxlanhdr *vxh;
518 /* XXX: RCUfy tnl_cfg. */
519 ovs_mutex_lock(&dev->mutex);
520 tnl_cfg = &dev->tnl_cfg;
522 vxh = udp_build_header(tnl_cfg, data, params);
524 put_16aligned_be32(&vxh->vx_flags, htonl(VXLAN_FLAGS));
525 put_16aligned_be32(&vxh->vx_vni, htonl(ntohll(params->flow->tunnel.tun_id) << 8));
527 ovs_mutex_unlock(&dev->mutex);
528 data->header_len += sizeof *vxh;
529 data->tnl_type = OVS_VPORT_TYPE_VXLAN;
534 netdev_geneve_pop_header(struct dp_packet *packet)
536 struct pkt_metadata *md = &packet->md;
537 struct flow_tnl *tnl = &md->tunnel;
538 struct genevehdr *gnh;
539 unsigned int hlen, opts_len, ulen;
541 pkt_metadata_init_tnl(md);
542 if (GENEVE_BASE_HLEN > dp_packet_l4_size(packet)) {
543 VLOG_WARN_RL(&err_rl, "geneve packet too small: min header=%u packet size=%"PRIuSIZE"\n",
544 (unsigned int)GENEVE_BASE_HLEN, dp_packet_l4_size(packet));
548 gnh = udp_extract_tnl_md(packet, tnl, &ulen);
553 opts_len = gnh->opt_len * 4;
554 hlen = ulen + GENEVE_BASE_HLEN + opts_len;
555 if (hlen > dp_packet_size(packet)) {
556 VLOG_WARN_RL(&err_rl, "geneve packet too small: header len=%u packet size=%u\n",
557 hlen, dp_packet_size(packet));
562 VLOG_WARN_RL(&err_rl, "unknown geneve version: %"PRIu8"\n", gnh->ver);
566 if (gnh->proto_type != htons(ETH_TYPE_TEB)) {
567 VLOG_WARN_RL(&err_rl, "unknown geneve encapsulated protocol: %#x\n",
568 ntohs(gnh->proto_type));
572 tnl->flags |= gnh->oam ? FLOW_TNL_F_OAM : 0;
573 tnl->tun_id = htonll(ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
574 tnl->flags |= FLOW_TNL_F_KEY;
576 memcpy(tnl->metadata.opts.gnv, gnh->options, opts_len);
577 tnl->metadata.present.len = opts_len;
578 tnl->flags |= FLOW_TNL_F_UDPIF;
580 dp_packet_reset_packet(packet, hlen);
584 dp_packet_delete(packet);
589 netdev_geneve_build_header(const struct netdev *netdev,
590 struct ovs_action_push_tnl *data,
591 const struct netdev_tnl_build_header_params *params)
593 struct netdev_vport *dev = netdev_vport_cast(netdev);
594 struct netdev_tunnel_config *tnl_cfg;
595 struct genevehdr *gnh;
599 /* XXX: RCUfy tnl_cfg. */
600 ovs_mutex_lock(&dev->mutex);
601 tnl_cfg = &dev->tnl_cfg;
603 gnh = udp_build_header(tnl_cfg, data, params);
605 put_16aligned_be32(&gnh->vni, htonl(ntohll(params->flow->tunnel.tun_id) << 8));
607 ovs_mutex_unlock(&dev->mutex);
609 opt_len = tun_metadata_to_geneve_header(¶ms->flow->tunnel,
610 gnh->options, &crit_opt);
612 gnh->opt_len = opt_len / 4;
613 gnh->oam = !!(params->flow->tunnel.flags & FLOW_TNL_F_OAM);
614 gnh->critical = crit_opt ? 1 : 0;
615 gnh->proto_type = htons(ETH_TYPE_TEB);
617 data->header_len += sizeof *gnh + opt_len;
618 data->tnl_type = OVS_VPORT_TYPE_GENEVE;
624 netdev_tnl_egress_port_range(struct unixctl_conn *conn, int argc,
625 const char *argv[], void *aux OVS_UNUSED)
630 struct ds ds = DS_EMPTY_INITIALIZER;
632 ds_put_format(&ds, "Tunnel UDP source port range: %"PRIu16"-%"PRIu16"\n",
633 tnl_udp_port_min, tnl_udp_port_max);
635 unixctl_command_reply(conn, ds_cstr(&ds));
644 val1 = atoi(argv[1]);
645 if (val1 <= 0 || val1 > UINT16_MAX) {
646 unixctl_command_reply(conn, "Invalid min.");
649 val2 = atoi(argv[2]);
650 if (val2 <= 0 || val2 > UINT16_MAX) {
651 unixctl_command_reply(conn, "Invalid max.");
656 tnl_udp_port_min = val2;
657 tnl_udp_port_max = val1;
659 tnl_udp_port_min = val1;
660 tnl_udp_port_max = val2;
662 seq_change(tnl_conf_seq);
664 unixctl_command_reply(conn, "OK");