Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * gre.c: gre |
| 3 | * |
| 4 | * Copyright (c) 2012 Cisco and/or its affiliates. |
| 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: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/gre/gre.h> |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 20 | #include <vnet/adj/adj_midchain.h> |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 21 | #include <vnet/tunnel/tunnel_dp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 23 | extern gre_main_t gre_main; |
| 24 | |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 25 | #ifndef CLIB_MARCH_VARIANT |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | gre_main_t gre_main; |
| 27 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 28 | typedef struct |
| 29 | { |
| 30 | union |
| 31 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | ip4_and_gre_header_t ip4_and_gre; |
| 33 | u64 as_u64[3]; |
| 34 | }; |
| 35 | } ip4_and_gre_union_t; |
| 36 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 37 | typedef struct |
| 38 | { |
| 39 | union |
| 40 | { |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 41 | ip6_and_gre_header_t ip6_and_gre; |
| 42 | u64 as_u64[3]; |
| 43 | }; |
| 44 | } ip6_and_gre_union_t; |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 45 | #endif /* CLIB_MARCH_VARIANT */ |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 46 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
| 48 | /* Packet trace structure */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 49 | typedef struct |
| 50 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | /* Tunnel-id / index in tunnel vector */ |
| 52 | u32 tunnel_id; |
| 53 | |
| 54 | /* pkt length */ |
| 55 | u32 length; |
| 56 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 57 | /* tunnel ip addresses */ |
| 58 | ip46_address_t src; |
| 59 | ip46_address_t dst; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | } gre_tx_trace_t; |
| 61 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 62 | extern u8 *format_gre_tx_trace (u8 * s, va_list * args); |
| 63 | |
| 64 | #ifndef CLIB_MARCH_VARIANT |
| 65 | u8 * |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 66 | format_gre_tx_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | { |
| 68 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 69 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 70 | gre_tx_trace_t *t = va_arg (*args, gre_tx_trace_t *); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 71 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | s = format (s, "GRE: tunnel %d len %d src %U dst %U", |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 73 | t->tunnel_id, t->length, |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 74 | format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 75 | format_ip46_address, &t->dst, IP46_TYPE_ANY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | return s; |
| 77 | } |
| 78 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 79 | u8 * |
| 80 | format_gre_protocol (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | { |
| 82 | gre_protocol_t p = va_arg (*args, u32); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 83 | gre_main_t *gm = &gre_main; |
| 84 | gre_protocol_info_t *pi = gre_get_protocol_info (gm, p); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
| 86 | if (pi) |
| 87 | s = format (s, "%s", pi->name); |
| 88 | else |
| 89 | s = format (s, "0x%04x", p); |
| 90 | |
| 91 | return s; |
| 92 | } |
| 93 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 94 | u8 * |
| 95 | format_gre_header_with_length (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 97 | gre_main_t *gm = &gre_main; |
| 98 | gre_header_t *h = va_arg (*args, gre_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | u32 max_header_bytes = va_arg (*args, u32); |
| 100 | gre_protocol_t p = clib_net_to_host_u16 (h->protocol); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 101 | u32 indent, header_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
| 103 | header_bytes = sizeof (h[0]); |
| 104 | if (max_header_bytes != 0 && header_bytes > max_header_bytes) |
| 105 | return format (s, "gre header truncated"); |
| 106 | |
| 107 | indent = format_get_indent (s); |
| 108 | |
| 109 | s = format (s, "GRE %U", format_gre_protocol, p); |
| 110 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 111 | if (max_header_bytes != 0 && header_bytes < max_header_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 113 | gre_protocol_info_t *pi = gre_get_protocol_info (gm, p); |
| 114 | vlib_node_t *node = vlib_get_node (gm->vlib_main, pi->node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | if (node->format_buffer) |
| 116 | s = format (s, "\n%U%U", |
| 117 | format_white_space, indent, |
| 118 | node->format_buffer, (void *) (h + 1), |
| 119 | max_header_bytes - header_bytes); |
| 120 | } |
| 121 | |
| 122 | return s; |
| 123 | } |
| 124 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 125 | u8 * |
| 126 | format_gre_header (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 128 | gre_header_t *h = va_arg (*args, gre_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | return format (s, "%U", format_gre_header_with_length, h, 0); |
| 130 | } |
| 131 | |
| 132 | /* Returns gre protocol as an int in host byte order. */ |
| 133 | uword |
| 134 | unformat_gre_protocol_host_byte_order (unformat_input_t * input, |
| 135 | va_list * args) |
| 136 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 137 | u16 *result = va_arg (*args, u16 *); |
| 138 | gre_main_t *gm = &gre_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | int i; |
| 140 | |
| 141 | /* Named type. */ |
| 142 | if (unformat_user (input, unformat_vlib_number_by_name, |
| 143 | gm->protocol_info_by_name, &i)) |
| 144 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 145 | gre_protocol_info_t *pi = vec_elt_at_index (gm->protocol_infos, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | *result = pi->protocol; |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | uword |
| 154 | unformat_gre_protocol_net_byte_order (unformat_input_t * input, |
| 155 | va_list * args) |
| 156 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 157 | u16 *result = va_arg (*args, u16 *); |
| 158 | if (!unformat_user (input, unformat_gre_protocol_host_byte_order, result)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | return 0; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 160 | *result = clib_host_to_net_u16 ((u16) * result); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | uword |
| 165 | unformat_gre_header (unformat_input_t * input, va_list * args) |
| 166 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 167 | u8 **result = va_arg (*args, u8 **); |
| 168 | gre_header_t _h, *h = &_h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | u16 p; |
| 170 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 171 | if (!unformat (input, "%U", unformat_gre_protocol_host_byte_order, &p)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | |
| 174 | h->protocol = clib_host_to_net_u16 (p); |
| 175 | |
| 176 | /* Add header to result. */ |
| 177 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 178 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | u32 n_bytes = sizeof (h[0]); |
| 180 | |
| 181 | vec_add2 (*result, p, n_bytes); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 182 | clib_memcpy (p, h, n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | } |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 184 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | return 1; |
| 186 | } |
| 187 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 188 | static int |
| 189 | gre_proto_from_vnet_link (vnet_link_t link) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 191 | switch (link) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 192 | { |
| 193 | case VNET_LINK_IP4: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 194 | return (GRE_PROTOCOL_ip4); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 195 | case VNET_LINK_IP6: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 196 | return (GRE_PROTOCOL_ip6); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 197 | case VNET_LINK_MPLS: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 198 | return (GRE_PROTOCOL_mpls_unicast); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 199 | case VNET_LINK_ETHERNET: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 200 | return (GRE_PROTOCOL_teb); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 201 | case VNET_LINK_ARP: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 202 | return (GRE_PROTOCOL_arp); |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 203 | case VNET_LINK_NSH: |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 204 | ASSERT (0); |
| 205 | break; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 206 | } |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 207 | ASSERT (0); |
| 208 | return (GRE_PROTOCOL_ip4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 211 | static u8 * |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 212 | gre_build_rewrite (vnet_main_t * vnm, |
| 213 | u32 sw_if_index, |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 214 | vnet_link_t link_type, const void *dst_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 216 | gre_main_t *gm = &gre_main; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 217 | const ip46_address_t *dst; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 218 | ip4_and_gre_header_t *h4; |
| 219 | ip6_and_gre_header_t *h6; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 220 | gre_header_t *gre; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 221 | u8 *rewrite = NULL; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 222 | gre_tunnel_t *t; |
| 223 | u32 ti; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 224 | u8 is_ipv6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 226 | dst = dst_address; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 227 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 229 | if (~0 == ti) |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 230 | /* not one of ours */ |
| 231 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 233 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 234 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 235 | is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 237 | if (!is_ipv6) |
| 238 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 239 | vec_validate (rewrite, sizeof (*h4) - 1); |
| 240 | h4 = (ip4_and_gre_header_t *) rewrite; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 241 | gre = &h4->gre; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 242 | h4->ip4.ip_version_and_header_length = 0x45; |
| 243 | h4->ip4.ttl = 254; |
| 244 | h4->ip4.protocol = IP_PROTOCOL_GRE; |
| 245 | /* fixup ip4 header length and checksum after-the-fact */ |
| 246 | h4->ip4.src_address.as_u32 = t->tunnel_src.ip4.as_u32; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 247 | h4->ip4.dst_address.as_u32 = dst->ip4.as_u32; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 248 | h4->ip4.checksum = ip4_header_checksum (&h4->ip4); |
| 249 | } |
| 250 | else |
| 251 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 252 | vec_validate (rewrite, sizeof (*h6) - 1); |
| 253 | h6 = (ip6_and_gre_header_t *) rewrite; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 254 | gre = &h6->gre; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 255 | h6->ip6.ip_version_traffic_class_and_flow_label = |
| 256 | clib_host_to_net_u32 (6 << 28); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 257 | h6->ip6.hop_limit = 255; |
| 258 | h6->ip6.protocol = IP_PROTOCOL_GRE; |
| 259 | /* fixup ip6 header length and checksum after-the-fact */ |
| 260 | h6->ip6.src_address.as_u64[0] = t->tunnel_src.ip6.as_u64[0]; |
| 261 | h6->ip6.src_address.as_u64[1] = t->tunnel_src.ip6.as_u64[1]; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 262 | h6->ip6.dst_address.as_u64[0] = dst->ip6.as_u64[0]; |
| 263 | h6->ip6.dst_address.as_u64[1] = dst->ip6.as_u64[1]; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 264 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 266 | if (PREDICT_FALSE (t->type == GRE_TUNNEL_TYPE_ERSPAN)) |
| 267 | { |
| 268 | gre->protocol = clib_host_to_net_u16 (GRE_PROTOCOL_erspan); |
| 269 | gre->flags_and_version = clib_host_to_net_u16 (GRE_FLAGS_SEQUENCE); |
| 270 | } |
| 271 | else |
| 272 | gre->protocol = |
| 273 | clib_host_to_net_u16 (gre_proto_from_vnet_link (link_type)); |
| 274 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 275 | return (rewrite); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 278 | static void |
| 279 | gre44_fixup (vlib_main_t * vm, |
| 280 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
| 281 | { |
| 282 | tunnel_encap_decap_flags_t flags; |
| 283 | ip4_and_gre_header_t *ip0; |
| 284 | |
| 285 | ip0 = vlib_buffer_get_current (b0); |
| 286 | flags = pointer_to_uword (data); |
| 287 | |
| 288 | /* Fixup the checksum and len fields in the GRE tunnel encap |
| 289 | * that was applied at the midchain node */ |
| 290 | ip0->ip4.length = |
| 291 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); |
| 292 | tunnel_encap_fixup_4o4 (flags, (ip4_header_t *) (ip0 + 1), &ip0->ip4); |
| 293 | ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4); |
| 294 | } |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 295 | |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 296 | static void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 297 | gre64_fixup (vlib_main_t * vm, |
| 298 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
| 299 | { |
| 300 | tunnel_encap_decap_flags_t flags; |
| 301 | ip4_and_gre_header_t *ip0; |
| 302 | |
| 303 | ip0 = vlib_buffer_get_current (b0); |
| 304 | flags = pointer_to_uword (data); |
| 305 | |
| 306 | /* Fixup the checksum and len fields in the GRE tunnel encap |
| 307 | * that was applied at the midchain node */ |
| 308 | ip0->ip4.length = |
| 309 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); |
| 310 | tunnel_encap_fixup_6o4 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip4); |
| 311 | ip0->ip4.checksum = ip4_header_checksum (&ip0->ip4); |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | grex4_fixup (vlib_main_t * vm, |
| 316 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 317 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 318 | ip4_header_t *ip0; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 319 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 320 | ip0 = vlib_buffer_get_current (b0); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 321 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 322 | /* Fixup the checksum and len fields in the GRE tunnel encap |
| 323 | * that was applied at the midchain node */ |
| 324 | ip0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)); |
| 325 | ip0->checksum = ip4_header_checksum (ip0); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Neale Ranns | db14f5a | 2018-01-29 10:43:33 -0800 | [diff] [blame] | 328 | static void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 329 | gre46_fixup (vlib_main_t * vm, |
| 330 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
| 331 | { |
| 332 | tunnel_encap_decap_flags_t flags; |
| 333 | ip6_and_gre_header_t *ip0; |
| 334 | |
| 335 | ip0 = vlib_buffer_get_current (b0); |
| 336 | flags = pointer_to_uword (data); |
| 337 | |
| 338 | /* Fixup the payload length field in the GRE tunnel encap that was applied |
| 339 | * at the midchain node */ |
| 340 | ip0->ip6.payload_length = |
| 341 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) - |
Aloys Augustin | cf86740a | 2020-07-16 11:01:01 +0200 | [diff] [blame] | 342 | sizeof (ip0->ip6)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 343 | tunnel_encap_fixup_4o6 (flags, b0, (ip4_header_t *) (ip0 + 1), &ip0->ip6); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void |
| 347 | gre66_fixup (vlib_main_t * vm, |
| 348 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
| 349 | { |
| 350 | tunnel_encap_decap_flags_t flags; |
| 351 | ip6_and_gre_header_t *ip0; |
| 352 | |
| 353 | ip0 = vlib_buffer_get_current (b0); |
| 354 | flags = pointer_to_uword (data); |
| 355 | |
| 356 | /* Fixup the payload length field in the GRE tunnel encap that was applied |
| 357 | * at the midchain node */ |
| 358 | ip0->ip6.payload_length = |
| 359 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) - |
Aloys Augustin | cf86740a | 2020-07-16 11:01:01 +0200 | [diff] [blame] | 360 | sizeof (ip0->ip6)); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 361 | tunnel_encap_fixup_6o6 (flags, (ip6_header_t *) (ip0 + 1), &ip0->ip6); |
| 362 | } |
| 363 | |
| 364 | static void |
| 365 | grex6_fixup (vlib_main_t * vm, |
| 366 | const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 367 | { |
Aloys Augustin | cf86740a | 2020-07-16 11:01:01 +0200 | [diff] [blame] | 368 | ip6_and_gre_header_t *ip0; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 369 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 370 | ip0 = vlib_buffer_get_current (b0); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 371 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 372 | /* Fixup the payload length field in the GRE tunnel encap that was applied |
| 373 | * at the midchain node */ |
Aloys Augustin | cf86740a | 2020-07-16 11:01:01 +0200 | [diff] [blame] | 374 | ip0->ip6.payload_length = |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 375 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) - |
Aloys Augustin | cf86740a | 2020-07-16 11:01:01 +0200 | [diff] [blame] | 376 | sizeof (ip0->ip6)); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 379 | /** |
| 380 | * return the appropriate fixup function given the overlay (link-type) and |
| 381 | * underlay (fproto) combination |
| 382 | */ |
| 383 | static adj_midchain_fixup_t |
| 384 | gre_get_fixup (fib_protocol_t fproto, vnet_link_t lt) |
| 385 | { |
| 386 | if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP6) |
| 387 | return (gre66_fixup); |
| 388 | if (fproto == FIB_PROTOCOL_IP6 && lt == VNET_LINK_IP4) |
| 389 | return (gre46_fixup); |
| 390 | if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP6) |
| 391 | return (gre64_fixup); |
| 392 | if (fproto == FIB_PROTOCOL_IP4 && lt == VNET_LINK_IP4) |
| 393 | return (gre44_fixup); |
Neale Ranns | 533bf08 | 2020-10-08 08:10:34 +0000 | [diff] [blame] | 394 | if (fproto == FIB_PROTOCOL_IP6) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 395 | return (grex6_fixup); |
Neale Ranns | 533bf08 | 2020-10-08 08:10:34 +0000 | [diff] [blame] | 396 | if (fproto == FIB_PROTOCOL_IP4) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 397 | return (grex4_fixup); |
| 398 | |
| 399 | ASSERT (0); |
| 400 | return (gre44_fixup); |
| 401 | } |
| 402 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 403 | void |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 404 | gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 405 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 406 | gre_main_t *gm = &gre_main; |
| 407 | gre_tunnel_t *t; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 408 | adj_flags_t af; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 409 | u32 ti; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 410 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 411 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 412 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 65d789e | 2021-02-08 15:24:56 +0000 | [diff] [blame] | 413 | af = ADJ_FLAG_NONE; |
Mohammed Hawari | 33c45f5 | 2020-11-30 13:50:24 +0100 | [diff] [blame] | 414 | |
Neale Ranns | 65d789e | 2021-02-08 15:24:56 +0000 | [diff] [blame] | 415 | /* |
| 416 | * the user has not requested that the load-balancing be based on |
| 417 | * a flow hash of the inner packet. so use the stacking to choose |
| 418 | * a path. |
| 419 | */ |
| 420 | if (!(t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH)) |
| 421 | af |= ADJ_FLAG_MIDCHAIN_IP_STACK; |
Neale Ranns | 521a8d7 | 2018-12-06 13:46:49 +0000 | [diff] [blame] | 422 | |
| 423 | if (VNET_LINK_ETHERNET == adj_get_link_type (ai)) |
| 424 | af |= ADJ_FLAG_MIDCHAIN_NO_COUNT; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 425 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 426 | adj_nbr_midchain_update_rewrite |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 427 | (ai, gre_get_fixup (t->tunnel_dst.fp_proto, |
| 428 | adj_get_link_type (ai)), |
| 429 | uword_to_pointer (t->flags, void *), af, |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 430 | gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), |
| 431 | &t->tunnel_dst.fp_addr)); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 432 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 433 | gre_tunnel_stack (ai); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 434 | } |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 435 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 436 | adj_walk_rc_t |
| 437 | mgre_mk_complete_walk (adj_index_t ai, void *data) |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 438 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 439 | mgre_walk_ctx_t *ctx = data; |
Mohammed Hawari | 33c45f5 | 2020-11-30 13:50:24 +0100 | [diff] [blame] | 440 | adj_flags_t af; |
| 441 | |
Neale Ranns | 65d789e | 2021-02-08 15:24:56 +0000 | [diff] [blame] | 442 | af = ADJ_FLAG_NONE; |
| 443 | |
| 444 | /* |
| 445 | * the user has not requested that the load-balancing be based on |
| 446 | * a flow hash of the inner packet. so use the stacking to choose |
| 447 | * a path. |
| 448 | */ |
| 449 | if (!(ctx->t->flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_INNER_HASH)) |
| 450 | af |= ADJ_FLAG_MIDCHAIN_IP_STACK; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 451 | |
| 452 | adj_nbr_midchain_update_rewrite |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 453 | (ai, gre_get_fixup (ctx->t->tunnel_dst.fp_proto, |
| 454 | adj_get_link_type (ai)), |
| 455 | uword_to_pointer (ctx->t->flags, void *), |
Mohammed Hawari | 33c45f5 | 2020-11-30 13:50:24 +0100 | [diff] [blame] | 456 | af, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 457 | gre_build_rewrite (vnet_get_main (), |
| 458 | ctx->t->sw_if_index, |
| 459 | adj_get_link_type (ai), |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 460 | &teib_entry_get_nh (ctx->ne)->fp_addr)); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 461 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 462 | teib_entry_adj_stack (ctx->ne, ai); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 463 | |
| 464 | return (ADJ_WALK_RC_CONTINUE); |
| 465 | } |
| 466 | |
| 467 | adj_walk_rc_t |
| 468 | mgre_mk_incomplete_walk (adj_index_t ai, void *data) |
| 469 | { |
| 470 | gre_tunnel_t *t = data; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 471 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 472 | adj_nbr_midchain_update_rewrite (ai, gre_get_fixup (t->tunnel_dst.fp_proto, |
| 473 | adj_get_link_type (ai)), |
| 474 | NULL, ADJ_FLAG_NONE, NULL); |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 475 | |
| 476 | adj_midchain_delegate_unstack (ai); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 477 | |
| 478 | return (ADJ_WALK_RC_CONTINUE); |
| 479 | } |
| 480 | |
| 481 | void |
| 482 | mgre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai) |
| 483 | { |
| 484 | gre_main_t *gm = &gre_main; |
| 485 | ip_adjacency_t *adj; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 486 | teib_entry_t *ne; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 487 | gre_tunnel_t *t; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 488 | u32 ti; |
| 489 | |
| 490 | adj = adj_get (ai); |
| 491 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 492 | t = pool_elt_at_index (gm->tunnels, ti); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 493 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 494 | ne = teib_entry_find_46 (sw_if_index, |
| 495 | adj->ia_nh_proto, &adj->sub_type.nbr.next_hop); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 496 | |
| 497 | if (NULL == ne) |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 498 | { |
| 499 | // no TEIB entry to provide the next-hop |
| 500 | adj_nbr_midchain_update_rewrite ( |
| 501 | ai, gre_get_fixup (t->tunnel_dst.fp_proto, adj_get_link_type (ai)), |
| 502 | uword_to_pointer (t->flags, void *), ADJ_FLAG_NONE, NULL); |
| 503 | return; |
| 504 | } |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 505 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 506 | mgre_walk_ctx_t ctx = { |
| 507 | .t = t, |
| 508 | .ne = ne |
| 509 | }; |
| 510 | adj_nbr_walk_nh (sw_if_index, |
| 511 | adj->ia_nh_proto, |
| 512 | &adj->sub_type.nbr.next_hop, mgre_mk_complete_walk, &ctx); |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 513 | } |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 514 | #endif /* CLIB_MARCH_VARIANT */ |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 515 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 516 | typedef enum |
| 517 | { |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 518 | GRE_ENCAP_NEXT_L2_MIDCHAIN, |
| 519 | GRE_ENCAP_N_NEXT, |
| 520 | } gre_encap_next_t; |
| 521 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 522 | /** |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 523 | * @brief TX function. Only called for L2 payload including TEB or ERSPAN. |
| 524 | * L3 traffic uses the adj-midchains. |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 525 | */ |
Neale Ranns | 07bbaef | 2020-04-09 07:34:08 -0400 | [diff] [blame] | 526 | static_always_inline u32 |
| 527 | gre_encap_inline (vlib_main_t * vm, |
| 528 | vlib_node_runtime_t * node, |
| 529 | vlib_frame_t * frame, gre_tunnel_type_t type) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 530 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 531 | gre_main_t *gm = &gre_main; |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 532 | u32 *from, n_left_from; |
| 533 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 534 | u32 sw_if_index[2] = { ~0, ~0 }; |
| 535 | const gre_tunnel_t *gt[2] = { 0 }; |
| 536 | adj_index_t adj_index[2] = { ADJ_INDEX_INVALID, ADJ_INDEX_INVALID }; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 537 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 538 | from = vlib_frame_vector_args (frame); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 539 | n_left_from = frame->n_vectors; |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 540 | vlib_get_buffers (vm, from, bufs, n_left_from); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 541 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 542 | while (n_left_from >= 2) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 543 | { |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 544 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 545 | if (PREDICT_FALSE |
| 546 | (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX])) |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 547 | { |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 548 | const vnet_hw_interface_t *hi; |
| 549 | sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
| 550 | hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]); |
| 551 | gt[0] = &gm->tunnels[hi->dev_instance]; |
| 552 | adj_index[0] = gt[0]->l2_adj_index; |
| 553 | } |
| 554 | if (PREDICT_FALSE |
| 555 | (sw_if_index[1] != vnet_buffer (b[1])->sw_if_index[VLIB_TX])) |
| 556 | { |
| 557 | const vnet_hw_interface_t *hi; |
| 558 | sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX]; |
| 559 | hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[1]); |
| 560 | gt[1] = &gm->tunnels[hi->dev_instance]; |
| 561 | adj_index[1] = gt[1]->l2_adj_index; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 562 | } |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 563 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 564 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0]; |
| 565 | vnet_buffer (b[1])->ip.adj_index[VLIB_TX] = adj_index[1]; |
| 566 | |
Neale Ranns | 07bbaef | 2020-04-09 07:34:08 -0400 | [diff] [blame] | 567 | if (type == GRE_TUNNEL_TYPE_ERSPAN) |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 568 | { |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 569 | /* Encap GRE seq# and ERSPAN type II header */ |
| 570 | erspan_t2_t *h0; |
| 571 | u32 seq_num; |
| 572 | u64 hdr; |
| 573 | vlib_buffer_advance (b[0], -sizeof (erspan_t2_t)); |
| 574 | h0 = vlib_buffer_get_current (b[0]); |
| 575 | seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1); |
| 576 | hdr = clib_host_to_net_u64 (ERSPAN_HDR2); |
| 577 | h0->seq_num = clib_host_to_net_u32 (seq_num); |
| 578 | h0->t2_u64 = hdr; |
| 579 | h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id); |
| 580 | } |
Neale Ranns | 07bbaef | 2020-04-09 07:34:08 -0400 | [diff] [blame] | 581 | if (type == GRE_TUNNEL_TYPE_ERSPAN) |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 582 | { |
| 583 | /* Encap GRE seq# and ERSPAN type II header */ |
| 584 | erspan_t2_t *h0; |
| 585 | u32 seq_num; |
| 586 | u64 hdr; |
| 587 | vlib_buffer_advance (b[1], -sizeof (erspan_t2_t)); |
| 588 | h0 = vlib_buffer_get_current (b[1]); |
| 589 | seq_num = clib_atomic_fetch_add (>[1]->gre_sn->seq_num, 1); |
| 590 | hdr = clib_host_to_net_u64 (ERSPAN_HDR2); |
| 591 | h0->seq_num = clib_host_to_net_u32 (seq_num); |
| 592 | h0->t2_u64 = hdr; |
| 593 | h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[1]->session_id); |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 596 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 597 | { |
| 598 | gre_tx_trace_t *tr = vlib_add_trace (vm, node, |
| 599 | b[0], sizeof (*tr)); |
| 600 | tr->tunnel_id = gt[0] - gm->tunnels; |
| 601 | tr->src = gt[0]->tunnel_src; |
| 602 | tr->dst = gt[0]->tunnel_dst.fp_addr; |
| 603 | tr->length = vlib_buffer_length_in_chain (vm, b[0]); |
| 604 | } |
| 605 | if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED)) |
| 606 | { |
| 607 | gre_tx_trace_t *tr = vlib_add_trace (vm, node, |
| 608 | b[1], sizeof (*tr)); |
| 609 | tr->tunnel_id = gt[1] - gm->tunnels; |
| 610 | tr->src = gt[1]->tunnel_src; |
| 611 | tr->dst = gt[1]->tunnel_dst.fp_addr; |
| 612 | tr->length = vlib_buffer_length_in_chain (vm, b[1]); |
| 613 | } |
| 614 | |
| 615 | b += 2; |
| 616 | n_left_from -= 2; |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 619 | while (n_left_from >= 1) |
| 620 | { |
| 621 | |
| 622 | if (PREDICT_FALSE |
| 623 | (sw_if_index[0] != vnet_buffer (b[0])->sw_if_index[VLIB_TX])) |
| 624 | { |
| 625 | const vnet_hw_interface_t *hi; |
| 626 | sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
| 627 | hi = vnet_get_sup_hw_interface (gm->vnet_main, sw_if_index[0]); |
| 628 | gt[0] = &gm->tunnels[hi->dev_instance]; |
| 629 | adj_index[0] = gt[0]->l2_adj_index; |
| 630 | } |
| 631 | |
| 632 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = adj_index[0]; |
| 633 | |
Neale Ranns | 07bbaef | 2020-04-09 07:34:08 -0400 | [diff] [blame] | 634 | if (type == GRE_TUNNEL_TYPE_ERSPAN) |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 635 | { |
| 636 | /* Encap GRE seq# and ERSPAN type II header */ |
| 637 | erspan_t2_t *h0; |
| 638 | u32 seq_num; |
| 639 | u64 hdr; |
Neale Ranns | 07bbaef | 2020-04-09 07:34:08 -0400 | [diff] [blame] | 640 | ASSERT (gt[0]->type == GRE_TUNNEL_TYPE_ERSPAN); |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 641 | vlib_buffer_advance (b[0], -sizeof (erspan_t2_t)); |
| 642 | h0 = vlib_buffer_get_current (b[0]); |
| 643 | seq_num = clib_atomic_fetch_add (>[0]->gre_sn->seq_num, 1); |
| 644 | hdr = clib_host_to_net_u64 (ERSPAN_HDR2); |
| 645 | h0->seq_num = clib_host_to_net_u32 (seq_num); |
| 646 | h0->t2_u64 = hdr; |
| 647 | h0->t2.cos_en_t_session |= clib_host_to_net_u16 (gt[0]->session_id); |
| 648 | } |
| 649 | |
| 650 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 651 | { |
| 652 | gre_tx_trace_t *tr = vlib_add_trace (vm, node, |
| 653 | b[0], sizeof (*tr)); |
| 654 | tr->tunnel_id = gt[0] - gm->tunnels; |
| 655 | tr->src = gt[0]->tunnel_src; |
| 656 | tr->dst = gt[0]->tunnel_dst.fp_addr; |
| 657 | tr->length = vlib_buffer_length_in_chain (vm, b[0]); |
| 658 | } |
| 659 | |
| 660 | b += 1; |
| 661 | n_left_from -= 1; |
| 662 | } |
| 663 | |
| 664 | vlib_buffer_enqueue_to_single_next (vm, node, from, |
| 665 | GRE_ENCAP_NEXT_L2_MIDCHAIN, |
| 666 | frame->n_vectors); |
| 667 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 668 | vlib_node_increment_counter (vm, node->node_index, |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 669 | GRE_ERROR_PKTS_ENCAP, frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 670 | |
| 671 | return frame->n_vectors; |
| 672 | } |
| 673 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 674 | static char *gre_error_strings[] = { |
| 675 | #define gre_error(n,s) s, |
| 676 | #include "error.def" |
| 677 | #undef gre_error |
| 678 | }; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 679 | |
Neale Ranns | 07bbaef | 2020-04-09 07:34:08 -0400 | [diff] [blame] | 680 | VLIB_NODE_FN (gre_teb_encap_node) (vlib_main_t * vm, |
| 681 | vlib_node_runtime_t * node, |
| 682 | vlib_frame_t * frame) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 683 | { |
Neale Ranns | 07bbaef | 2020-04-09 07:34:08 -0400 | [diff] [blame] | 684 | return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_TEB)); |
| 685 | } |
| 686 | |
| 687 | VLIB_NODE_FN (gre_erspan_encap_node) (vlib_main_t * vm, |
| 688 | vlib_node_runtime_t * node, |
| 689 | vlib_frame_t * frame) |
| 690 | { |
| 691 | return (gre_encap_inline (vm, node, frame, GRE_TUNNEL_TYPE_ERSPAN)); |
| 692 | } |
| 693 | |
| 694 | /* *INDENT-OFF* */ |
| 695 | VLIB_REGISTER_NODE (gre_teb_encap_node) = |
| 696 | { |
| 697 | .name = "gre-teb-encap", |
| 698 | .vector_size = sizeof (u32), |
| 699 | .format_trace = format_gre_tx_trace, |
| 700 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 701 | .n_errors = GRE_N_ERROR, |
| 702 | .error_strings = gre_error_strings, |
| 703 | .n_next_nodes = GRE_ENCAP_N_NEXT, |
| 704 | .next_nodes = { |
| 705 | [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain", |
| 706 | }, |
| 707 | }; |
| 708 | VLIB_REGISTER_NODE (gre_erspan_encap_node) = |
| 709 | { |
| 710 | .name = "gre-erspan-encap", |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 711 | .vector_size = sizeof (u32), |
| 712 | .format_trace = format_gre_tx_trace, |
| 713 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 714 | .n_errors = GRE_N_ERROR, |
| 715 | .error_strings = gre_error_strings, |
| 716 | .n_next_nodes = GRE_ENCAP_N_NEXT, |
| 717 | .next_nodes = { |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 718 | [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain", |
| 719 | }, |
| 720 | }; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 721 | /* *INDENT-ON* */ |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 722 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 723 | #ifndef CLIB_MARCH_VARIANT |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 724 | static u8 * |
| 725 | format_gre_tunnel_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 726 | { |
| 727 | u32 dev_instance = va_arg (*args, u32); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 728 | gre_main_t *gm = &gre_main; |
| 729 | gre_tunnel_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 730 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 731 | if (dev_instance >= vec_len (gm->tunnels)) |
| 732 | return format (s, "<improperly-referenced>"); |
| 733 | |
| 734 | t = pool_elt_at_index (gm->tunnels, dev_instance); |
| 735 | return format (s, "gre%d", t->user_instance); |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 738 | static u8 * |
| 739 | format_gre_device (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | { |
| 741 | u32 dev_instance = va_arg (*args, u32); |
| 742 | CLIB_UNUSED (int verbose) = va_arg (*args, int); |
| 743 | |
| 744 | s = format (s, "GRE tunnel: id %d\n", dev_instance); |
| 745 | return s; |
| 746 | } |
| 747 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 748 | static int |
| 749 | gre_tunnel_desc (u32 sw_if_index, |
| 750 | ip46_address_t * src, ip46_address_t * dst, u8 * is_l2) |
| 751 | { |
| 752 | gre_main_t *gm = &gre_main; |
| 753 | gre_tunnel_t *t; |
| 754 | u32 ti; |
| 755 | |
| 756 | ti = gm->tunnel_index_by_sw_if_index[sw_if_index]; |
| 757 | |
| 758 | if (~0 == ti) |
| 759 | /* not one of ours */ |
| 760 | return -1; |
| 761 | |
| 762 | t = pool_elt_at_index (gm->tunnels, ti); |
| 763 | |
| 764 | *src = t->tunnel_src; |
| 765 | *dst = t->tunnel_dst.fp_addr; |
| 766 | *is_l2 = t->type == GRE_TUNNEL_TYPE_TEB; |
| 767 | |
| 768 | return (0); |
| 769 | } |
| 770 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 771 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | VNET_DEVICE_CLASS (gre_device_class) = { |
| 773 | .name = "GRE tunnel device", |
| 774 | .format_device_name = format_gre_tunnel_name, |
| 775 | .format_device = format_gre_device, |
| 776 | .format_tx_trace = format_gre_tx_trace, |
Chris Luke | 393490e | 2016-05-06 17:09:09 -0400 | [diff] [blame] | 777 | .admin_up_down_function = gre_interface_admin_up_down, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 778 | .ip_tun_desc = gre_tunnel_desc, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 779 | #ifdef SOON |
| 780 | .clear counter = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 781 | #endif |
| 782 | }; |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 783 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 784 | VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = { |
| 785 | .name = "GRE", |
| 786 | .format_header = format_gre_header_with_length, |
| 787 | .unformat_header = unformat_gre_header, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 788 | .build_rewrite = gre_build_rewrite, |
| 789 | .update_adjacency = gre_update_adj, |
| 790 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 791 | }; |
Neale Ranns | 5f8f617 | 2019-04-18 10:23:56 +0000 | [diff] [blame] | 792 | |
| 793 | VNET_HW_INTERFACE_CLASS (mgre_hw_interface_class) = { |
| 794 | .name = "mGRE", |
| 795 | .format_header = format_gre_header_with_length, |
| 796 | .unformat_header = unformat_gre_header, |
| 797 | .build_rewrite = gre_build_rewrite, |
| 798 | .update_adjacency = mgre_update_adj, |
| 799 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_NBMA, |
| 800 | }; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 801 | /* *INDENT-ON* */ |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 802 | #endif /* CLIB_MARCH_VARIANT */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 803 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 804 | static void |
| 805 | add_protocol (gre_main_t * gm, gre_protocol_t protocol, char *protocol_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 806 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 807 | gre_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 808 | u32 i; |
| 809 | |
| 810 | vec_add2 (gm->protocol_infos, pi, 1); |
| 811 | i = pi - gm->protocol_infos; |
| 812 | |
| 813 | pi->name = protocol_name; |
| 814 | pi->protocol = protocol; |
| 815 | pi->next_index = pi->node_index = ~0; |
| 816 | |
| 817 | hash_set (gm->protocol_info_by_protocol, protocol, i); |
| 818 | hash_set_mem (gm->protocol_info_by_name, pi->name, i); |
| 819 | } |
| 820 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 821 | static clib_error_t * |
| 822 | gre_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 823 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 824 | gre_main_t *gm = &gre_main; |
| 825 | clib_error_t *error; |
| 826 | ip_main_t *im = &ip_main; |
| 827 | ip_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 829 | clib_memset (gm, 0, sizeof (gm[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | gm->vlib_main = vm; |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 831 | gm->vnet_main = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 832 | |
| 833 | if ((error = vlib_call_init_function (vm, ip_main_init))) |
| 834 | return error; |
| 835 | |
| 836 | if ((error = vlib_call_init_function (vm, ip4_lookup_init))) |
| 837 | return error; |
| 838 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 839 | if ((error = vlib_call_init_function (vm, ip6_lookup_init))) |
| 840 | return error; |
| 841 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | /* Set up the ip packet generator */ |
| 843 | pi = ip_get_protocol_info (im, IP_PROTOCOL_GRE); |
| 844 | pi->format_header = format_gre_header; |
| 845 | pi->unformat_pg_edit = unformat_pg_gre_header; |
| 846 | |
| 847 | gm->protocol_info_by_name = hash_create_string (0, sizeof (uword)); |
| 848 | gm->protocol_info_by_protocol = hash_create (0, sizeof (uword)); |
Neale Ranns | 33ce60d | 2017-12-14 08:51:32 -0800 | [diff] [blame] | 849 | gm->tunnel_by_key4 = |
| 850 | hash_create_mem (0, sizeof (gre_tunnel_key4_t), sizeof (uword)); |
| 851 | gm->tunnel_by_key6 = |
| 852 | hash_create_mem (0, sizeof (gre_tunnel_key6_t), sizeof (uword)); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 853 | gm->seq_num_by_key = |
| 854 | hash_create_mem (0, sizeof (gre_sn_key_t), sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 855 | |
| 856 | #define _(n,s) add_protocol (gm, GRE_PROTOCOL_##s, #s); |
| 857 | foreach_gre_protocol |
| 858 | #undef _ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 859 | return vlib_call_init_function (vm, gre_input_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | VLIB_INIT_FUNCTION (gre_init); |
| 863 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 864 | /* |
| 865 | * fd.io coding-style-patch-verification: ON |
| 866 | * |
| 867 | * Local Variables: |
| 868 | * eval: (c-set-style "gnu") |
| 869 | * End: |
| 870 | */ |