Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * node.c: gre packet processing |
| 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 <vlib/vlib.h> |
| 19 | #include <vnet/pg/pg.h> |
| 20 | #include <vnet/gre/gre.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 21 | #include <vnet/mpls/mpls.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | #include <vppinfra/sparse_vec.h> |
| 23 | |
| 24 | #define foreach_gre_input_next \ |
| 25 | _(PUNT, "error-punt") \ |
| 26 | _(DROP, "error-drop") \ |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 27 | _(ETHERNET_INPUT, "ethernet-input") \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | _(IP4_INPUT, "ip4-input") \ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 29 | _(IP6_INPUT, "ip6-input") \ |
| 30 | _(MPLS_INPUT, "mpls-input") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 32 | typedef enum |
| 33 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | #define _(s,n) GRE_INPUT_NEXT_##s, |
| 35 | foreach_gre_input_next |
| 36 | #undef _ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 37 | GRE_INPUT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 38 | } gre_input_next_t; |
| 39 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 40 | typedef struct |
| 41 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | u32 tunnel_id; |
| 43 | u32 length; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 44 | ip46_address_t src; |
| 45 | ip46_address_t dst; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | } gre_rx_trace_t; |
| 47 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 48 | extern u8 *format_gre_rx_trace (u8 * s, va_list * args); |
| 49 | |
| 50 | #ifndef CLIB_MARCH_VARIANT |
| 51 | u8 * |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 52 | format_gre_rx_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | { |
| 54 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 55 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 56 | gre_rx_trace_t *t = va_arg (*args, gre_rx_trace_t *); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 57 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | s = format (s, "GRE: tunnel %d len %d src %U dst %U", |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 59 | t->tunnel_id, clib_net_to_host_u16 (t->length), |
| 60 | format_ip46_address, &t->src, IP46_TYPE_ANY, |
| 61 | format_ip46_address, &t->dst, IP46_TYPE_ANY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | return s; |
| 63 | } |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 64 | #endif /* CLIB_MARCH_VARIANT */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 66 | typedef struct |
| 67 | { |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 68 | /* Sparse vector mapping gre protocol in network byte order |
| 69 | to next index. */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 70 | u16 *next_by_protocol; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 71 | } gre_input_runtime_t; |
| 72 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 73 | always_inline void |
| 74 | gre_trace (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b, |
| 75 | u32 tun_sw_if_index, const ip6_header_t * ip6, |
| 76 | const ip4_header_t * ip4, int is_ipv6) |
| 77 | { |
| 78 | gre_rx_trace_t *tr = vlib_add_trace (vm, node, |
| 79 | b, sizeof (*tr)); |
| 80 | tr->tunnel_id = tun_sw_if_index; |
| 81 | if (is_ipv6) |
| 82 | { |
| 83 | tr->length = ip6->payload_length; |
| 84 | tr->src.ip6.as_u64[0] = ip6->src_address.as_u64[0]; |
| 85 | tr->src.ip6.as_u64[1] = ip6->src_address.as_u64[1]; |
| 86 | tr->dst.ip6.as_u64[0] = ip6->dst_address.as_u64[0]; |
| 87 | tr->dst.ip6.as_u64[1] = ip6->dst_address.as_u64[1]; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | tr->length = ip4->length; |
| 92 | tr->src.as_u64[0] = tr->src.as_u64[1] = 0; |
| 93 | tr->dst.as_u64[0] = tr->dst.as_u64[1] = 0; |
| 94 | tr->src.ip4.as_u32 = ip4->src_address.as_u32; |
| 95 | tr->dst.ip4.as_u32 = ip4->dst_address.as_u32; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | always_inline void |
| 100 | gre_tunnel_get (const gre_main_t * gm, vlib_node_runtime_t * node, |
| 101 | vlib_buffer_t * b, u16 * next, const gre_tunnel_key_t * key, |
| 102 | gre_tunnel_key_t * cached_key, u32 * tun_sw_if_index, |
| 103 | u32 * cached_tun_sw_if_index, int is_ipv6) |
| 104 | { |
| 105 | const uword *p; |
| 106 | p = is_ipv6 ? hash_get_mem (gm->tunnel_by_key6, &key->gtk_v6) |
| 107 | : hash_get_mem (gm->tunnel_by_key4, &key->gtk_v4); |
| 108 | if (PREDICT_FALSE (!p)) |
| 109 | { |
| 110 | *next = GRE_INPUT_NEXT_DROP; |
| 111 | b->error = node->errors[GRE_ERROR_NO_SUCH_TUNNEL]; |
| 112 | *tun_sw_if_index = ~0; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | const gre_tunnel_t *tun; |
| 117 | tun = pool_elt_at_index (gm->tunnels, *p); |
| 118 | *cached_tun_sw_if_index = *tun_sw_if_index = tun->sw_if_index; |
| 119 | if (is_ipv6) |
| 120 | cached_key->gtk_v6 = key->gtk_v6; |
| 121 | else |
| 122 | cached_key->gtk_v4 = key->gtk_v4; |
| 123 | } |
| 124 | } |
| 125 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 126 | always_inline uword |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | gre_input (vlib_main_t * vm, |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 128 | vlib_node_runtime_t * node, vlib_frame_t * frame, |
| 129 | const int is_ipv6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 131 | gre_main_t *gm = &gre_main; |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 132 | u32 *from, n_left_from; |
| 133 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 134 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 135 | u16 cached_protocol = ~0; |
| 136 | u32 cached_next_index = SPARSE_VEC_INVALID_INDEX; |
| 137 | u32 cached_tun_sw_if_index = ~0; |
| 138 | gre_tunnel_key_t cached_key; |
Neale Ranns | 33ce60d | 2017-12-14 08:51:32 -0800 | [diff] [blame] | 139 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 140 | from = vlib_frame_vector_args (frame); |
| 141 | n_left_from = frame->n_vectors; |
| 142 | vlib_get_buffers (vm, from, bufs, n_left_from); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 144 | if (is_ipv6) |
| 145 | clib_memset (&cached_key.gtk_v6, 0xff, sizeof (cached_key.gtk_v6)); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 146 | else |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 147 | clib_memset (&cached_key.gtk_v4, 0xff, sizeof (cached_key.gtk_v4)); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 148 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 149 | while (n_left_from >= 2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | { |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 151 | const ip6_header_t *ip6[2]; |
| 152 | const ip4_header_t *ip4[2]; |
| 153 | const gre_header_t *gre[2]; |
| 154 | u32 nidx[2]; |
| 155 | next_info_t ni[2]; |
| 156 | u8 type[2]; |
| 157 | u16 version[2]; |
| 158 | u32 len[2]; |
| 159 | gre_tunnel_key_t key[2]; |
| 160 | u8 matched[2]; |
| 161 | u32 tun_sw_if_index[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 163 | if (PREDICT_TRUE (n_left_from >= 6)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | { |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 165 | vlib_prefetch_buffer_data (b[2], LOAD); |
| 166 | vlib_prefetch_buffer_data (b[3], LOAD); |
| 167 | vlib_prefetch_buffer_header (b[4], STORE); |
| 168 | vlib_prefetch_buffer_header (b[5], STORE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | } |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 170 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 171 | if (is_ipv6) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | { |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 173 | /* ip6_local hands us the ip header, not the gre header */ |
| 174 | ip6[0] = vlib_buffer_get_current (b[0]); |
| 175 | ip6[1] = vlib_buffer_get_current (b[1]); |
| 176 | gre[0] = (void *) (ip6[0] + 1); |
| 177 | gre[1] = (void *) (ip6[1] + 1); |
| 178 | vlib_buffer_advance (b[0], sizeof (*ip6[0]) + sizeof (*gre[0])); |
| 179 | vlib_buffer_advance (b[1], sizeof (*ip6[0]) + sizeof (*gre[0])); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | /* ip4_local hands us the ip header, not the gre header */ |
| 184 | ip4[0] = vlib_buffer_get_current (b[0]); |
| 185 | ip4[1] = vlib_buffer_get_current (b[1]); |
| 186 | gre[0] = (void *) (ip4[0] + 1); |
| 187 | gre[1] = (void *) (ip4[1] + 1); |
| 188 | vlib_buffer_advance (b[0], sizeof (*ip4[0]) + sizeof (*gre[0])); |
| 189 | vlib_buffer_advance (b[1], sizeof (*ip4[0]) + sizeof (*gre[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 192 | if (PREDICT_TRUE (cached_protocol == gre[0]->protocol)) |
| 193 | { |
| 194 | nidx[0] = cached_next_index; |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | cached_next_index = nidx[0] = |
| 199 | sparse_vec_index (gm->next_by_protocol, gre[0]->protocol); |
| 200 | cached_protocol = gre[0]->protocol; |
| 201 | } |
| 202 | if (PREDICT_TRUE (cached_protocol == gre[1]->protocol)) |
| 203 | { |
| 204 | nidx[1] = cached_next_index; |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | cached_next_index = nidx[1] = |
| 209 | sparse_vec_index (gm->next_by_protocol, gre[1]->protocol); |
| 210 | cached_protocol = gre[1]->protocol; |
| 211 | } |
| 212 | |
| 213 | ni[0] = vec_elt (gm->next_by_protocol, nidx[0]); |
| 214 | ni[1] = vec_elt (gm->next_by_protocol, nidx[1]); |
| 215 | next[0] = ni[0].next_index; |
| 216 | next[1] = ni[1].next_index; |
| 217 | type[0] = ni[0].tunnel_type; |
| 218 | type[1] = ni[1].tunnel_type; |
| 219 | |
| 220 | b[0]->error = nidx[0] == SPARSE_VEC_INVALID_INDEX |
| 221 | ? node->errors[GRE_ERROR_UNKNOWN_PROTOCOL] |
| 222 | : node->errors[GRE_ERROR_NONE]; |
| 223 | b[1]->error = nidx[1] == SPARSE_VEC_INVALID_INDEX |
| 224 | ? node->errors[GRE_ERROR_UNKNOWN_PROTOCOL] |
| 225 | : node->errors[GRE_ERROR_NONE]; |
| 226 | |
| 227 | version[0] = clib_net_to_host_u16 (gre[0]->flags_and_version); |
| 228 | version[1] = clib_net_to_host_u16 (gre[1]->flags_and_version); |
| 229 | version[0] &= GRE_VERSION_MASK; |
| 230 | version[1] &= GRE_VERSION_MASK; |
| 231 | |
| 232 | b[0]->error = version[0] |
| 233 | ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[0]->error; |
| 234 | next[0] = version[0] ? GRE_INPUT_NEXT_DROP : next[0]; |
| 235 | b[1]->error = version[1] |
| 236 | ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[1]->error; |
| 237 | next[1] = version[1] ? GRE_INPUT_NEXT_DROP : next[1]; |
| 238 | |
| 239 | len[0] = vlib_buffer_length_in_chain (vm, b[0]); |
| 240 | len[1] = vlib_buffer_length_in_chain (vm, b[1]); |
| 241 | |
Neale Ranns | 4c16d80 | 2019-12-17 20:15:03 +0000 | [diff] [blame] | 242 | /* always search for P2P types in the DP */ |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 243 | if (is_ipv6) |
| 244 | { |
| 245 | gre_mk_key6 (&ip6[0]->dst_address, |
| 246 | &ip6[0]->src_address, |
| 247 | vnet_buffer (b[0])->ip.fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 248 | type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v6); |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 249 | gre_mk_key6 (&ip6[1]->dst_address, |
| 250 | &ip6[1]->src_address, |
| 251 | vnet_buffer (b[1])->ip.fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 252 | type[1], TUNNEL_MODE_P2P, 0, &key[1].gtk_v6); |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 253 | matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6); |
| 254 | matched[1] = gre_match_key6 (&cached_key.gtk_v6, &key[1].gtk_v6); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | gre_mk_key4 (ip4[0]->dst_address, |
| 259 | ip4[0]->src_address, |
| 260 | vnet_buffer (b[0])->ip.fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 261 | type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v4); |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 262 | gre_mk_key4 (ip4[1]->dst_address, |
| 263 | ip4[1]->src_address, |
| 264 | vnet_buffer (b[1])->ip.fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 265 | type[1], TUNNEL_MODE_P2P, 0, &key[1].gtk_v4); |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 266 | matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4); |
| 267 | matched[1] = gre_match_key4 (&cached_key.gtk_v4, &key[1].gtk_v4); |
| 268 | } |
| 269 | |
| 270 | tun_sw_if_index[0] = cached_tun_sw_if_index; |
| 271 | tun_sw_if_index[1] = cached_tun_sw_if_index; |
| 272 | if (PREDICT_FALSE (!matched[0])) |
| 273 | gre_tunnel_get (gm, node, b[0], &next[0], &key[0], &cached_key, |
| 274 | &tun_sw_if_index[0], &cached_tun_sw_if_index, |
| 275 | is_ipv6); |
| 276 | if (PREDICT_FALSE (!matched[1])) |
| 277 | gre_tunnel_get (gm, node, b[1], &next[1], &key[1], &cached_key, |
| 278 | &tun_sw_if_index[1], &cached_tun_sw_if_index, |
| 279 | is_ipv6); |
| 280 | |
| 281 | if (PREDICT_TRUE (next[0] > GRE_INPUT_NEXT_DROP)) |
| 282 | { |
| 283 | vlib_increment_combined_counter (&gm->vnet_main-> |
| 284 | interface_main.combined_sw_if_counters |
| 285 | [VNET_INTERFACE_COUNTER_RX], |
| 286 | vm->thread_index, |
| 287 | tun_sw_if_index[0], |
| 288 | 1 /* packets */ , |
| 289 | len[0] /* bytes */ ); |
| 290 | vnet_buffer (b[0])->sw_if_index[VLIB_RX] = tun_sw_if_index[0]; |
| 291 | } |
| 292 | if (PREDICT_TRUE (next[1] > GRE_INPUT_NEXT_DROP)) |
| 293 | { |
| 294 | vlib_increment_combined_counter (&gm->vnet_main-> |
| 295 | interface_main.combined_sw_if_counters |
| 296 | [VNET_INTERFACE_COUNTER_RX], |
| 297 | vm->thread_index, |
| 298 | tun_sw_if_index[1], |
| 299 | 1 /* packets */ , |
| 300 | len[1] /* bytes */ ); |
| 301 | vnet_buffer (b[1])->sw_if_index[VLIB_RX] = tun_sw_if_index[1]; |
| 302 | } |
| 303 | |
Stanislav Zaikin | 328b5da | 2021-07-15 16:27:29 +0200 | [diff] [blame^] | 304 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = (u32) ~0; |
| 305 | vnet_buffer (b[1])->sw_if_index[VLIB_TX] = (u32) ~0; |
| 306 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 307 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 308 | gre_trace (vm, node, b[0], tun_sw_if_index[0], ip6[0], ip4[0], |
| 309 | is_ipv6); |
| 310 | if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED)) |
| 311 | gre_trace (vm, node, b[1], tun_sw_if_index[1], ip6[1], ip4[1], |
| 312 | is_ipv6); |
| 313 | |
| 314 | b += 2; |
| 315 | next += 2; |
| 316 | n_left_from -= 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | } |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 318 | |
| 319 | while (n_left_from >= 1) |
| 320 | { |
| 321 | const ip6_header_t *ip6[1]; |
| 322 | const ip4_header_t *ip4[1]; |
| 323 | const gre_header_t *gre[1]; |
| 324 | u32 nidx[1]; |
| 325 | next_info_t ni[1]; |
| 326 | u8 type[1]; |
| 327 | u16 version[1]; |
| 328 | u32 len[1]; |
| 329 | gre_tunnel_key_t key[1]; |
| 330 | u8 matched[1]; |
| 331 | u32 tun_sw_if_index[1]; |
| 332 | |
| 333 | if (PREDICT_TRUE (n_left_from >= 3)) |
| 334 | { |
| 335 | vlib_prefetch_buffer_data (b[1], LOAD); |
| 336 | vlib_prefetch_buffer_header (b[2], STORE); |
| 337 | } |
| 338 | |
| 339 | if (is_ipv6) |
| 340 | { |
| 341 | /* ip6_local hands us the ip header, not the gre header */ |
| 342 | ip6[0] = vlib_buffer_get_current (b[0]); |
| 343 | gre[0] = (void *) (ip6[0] + 1); |
| 344 | vlib_buffer_advance (b[0], sizeof (*ip6[0]) + sizeof (*gre[0])); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | /* ip4_local hands us the ip header, not the gre header */ |
| 349 | ip4[0] = vlib_buffer_get_current (b[0]); |
| 350 | gre[0] = (void *) (ip4[0] + 1); |
| 351 | vlib_buffer_advance (b[0], sizeof (*ip4[0]) + sizeof (*gre[0])); |
| 352 | } |
| 353 | |
| 354 | if (PREDICT_TRUE (cached_protocol == gre[0]->protocol)) |
| 355 | { |
| 356 | nidx[0] = cached_next_index; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | cached_next_index = nidx[0] = |
| 361 | sparse_vec_index (gm->next_by_protocol, gre[0]->protocol); |
| 362 | cached_protocol = gre[0]->protocol; |
| 363 | } |
| 364 | |
| 365 | ni[0] = vec_elt (gm->next_by_protocol, nidx[0]); |
| 366 | next[0] = ni[0].next_index; |
| 367 | type[0] = ni[0].tunnel_type; |
| 368 | |
| 369 | b[0]->error = nidx[0] == SPARSE_VEC_INVALID_INDEX |
| 370 | ? node->errors[GRE_ERROR_UNKNOWN_PROTOCOL] |
| 371 | : node->errors[GRE_ERROR_NONE]; |
| 372 | |
| 373 | version[0] = clib_net_to_host_u16 (gre[0]->flags_and_version); |
| 374 | version[0] &= GRE_VERSION_MASK; |
| 375 | |
| 376 | b[0]->error = version[0] |
| 377 | ? node->errors[GRE_ERROR_UNSUPPORTED_VERSION] : b[0]->error; |
| 378 | next[0] = version[0] ? GRE_INPUT_NEXT_DROP : next[0]; |
| 379 | |
| 380 | len[0] = vlib_buffer_length_in_chain (vm, b[0]); |
| 381 | |
| 382 | if (is_ipv6) |
| 383 | { |
| 384 | gre_mk_key6 (&ip6[0]->dst_address, |
| 385 | &ip6[0]->src_address, |
| 386 | vnet_buffer (b[0])->ip.fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 387 | type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v6); |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 388 | matched[0] = gre_match_key6 (&cached_key.gtk_v6, &key[0].gtk_v6); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | gre_mk_key4 (ip4[0]->dst_address, |
| 393 | ip4[0]->src_address, |
| 394 | vnet_buffer (b[0])->ip.fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 395 | type[0], TUNNEL_MODE_P2P, 0, &key[0].gtk_v4); |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 396 | matched[0] = gre_match_key4 (&cached_key.gtk_v4, &key[0].gtk_v4); |
| 397 | } |
| 398 | |
| 399 | tun_sw_if_index[0] = cached_tun_sw_if_index; |
| 400 | if (PREDICT_FALSE (!matched[0])) |
| 401 | gre_tunnel_get (gm, node, b[0], &next[0], &key[0], &cached_key, |
| 402 | &tun_sw_if_index[0], &cached_tun_sw_if_index, |
| 403 | is_ipv6); |
| 404 | |
| 405 | if (PREDICT_TRUE (next[0] > GRE_INPUT_NEXT_DROP)) |
| 406 | { |
| 407 | vlib_increment_combined_counter (&gm->vnet_main-> |
| 408 | interface_main.combined_sw_if_counters |
| 409 | [VNET_INTERFACE_COUNTER_RX], |
| 410 | vm->thread_index, |
| 411 | tun_sw_if_index[0], |
| 412 | 1 /* packets */ , |
| 413 | len[0] /* bytes */ ); |
| 414 | vnet_buffer (b[0])->sw_if_index[VLIB_RX] = tun_sw_if_index[0]; |
| 415 | } |
| 416 | |
Stanislav Zaikin | 328b5da | 2021-07-15 16:27:29 +0200 | [diff] [blame^] | 417 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = (u32) ~0; |
| 418 | |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 419 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 420 | gre_trace (vm, node, b[0], tun_sw_if_index[0], ip6[0], ip4[0], |
| 421 | is_ipv6); |
| 422 | |
| 423 | b += 1; |
| 424 | next += 1; |
| 425 | n_left_from -= 1; |
| 426 | } |
| 427 | |
| 428 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 429 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 430 | vlib_node_increment_counter (vm, |
Benoît Ganne | 8e22054 | 2019-03-01 14:19:55 +0100 | [diff] [blame] | 431 | is_ipv6 ? gre6_input_node.index : |
| 432 | gre4_input_node.index, GRE_ERROR_PKTS_DECAP, |
| 433 | n_left_from); |
| 434 | |
| 435 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 438 | VLIB_NODE_FN (gre4_input_node) (vlib_main_t * vm, |
| 439 | vlib_node_runtime_t * node, |
| 440 | vlib_frame_t * from_frame) |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 441 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 442 | return gre_input (vm, node, from_frame, /* is_ip6 */ 0); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 445 | VLIB_NODE_FN (gre6_input_node) (vlib_main_t * vm, |
| 446 | vlib_node_runtime_t * node, |
| 447 | vlib_frame_t * from_frame) |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 448 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 449 | return gre_input (vm, node, from_frame, /* is_ip6 */ 1); |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 452 | static char *gre_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | #define gre_error(n,s) s, |
| 454 | #include "error.def" |
| 455 | #undef gre_error |
| 456 | }; |
| 457 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 458 | /* *INDENT-OFF* */ |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 459 | VLIB_REGISTER_NODE (gre4_input_node) = { |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 460 | .name = "gre4-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | /* Takes a vector of packets. */ |
| 462 | .vector_size = sizeof (u32), |
| 463 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | .n_errors = GRE_N_ERROR, |
| 465 | .error_strings = gre_error_strings, |
| 466 | |
| 467 | .n_next_nodes = GRE_INPUT_N_NEXT, |
| 468 | .next_nodes = { |
| 469 | #define _(s,n) [GRE_INPUT_NEXT_##s] = n, |
| 470 | foreach_gre_input_next |
| 471 | #undef _ |
| 472 | }, |
| 473 | |
| 474 | .format_buffer = format_gre_header_with_length, |
| 475 | .format_trace = format_gre_rx_trace, |
| 476 | .unformat_buffer = unformat_gre_header, |
| 477 | }; |
| 478 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 479 | VLIB_REGISTER_NODE (gre6_input_node) = { |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 480 | .name = "gre6-input", |
| 481 | /* Takes a vector of packets. */ |
| 482 | .vector_size = sizeof (u32), |
| 483 | |
| 484 | .runtime_data_bytes = sizeof (gre_input_runtime_t), |
| 485 | |
| 486 | .n_errors = GRE_N_ERROR, |
| 487 | .error_strings = gre_error_strings, |
| 488 | |
| 489 | .n_next_nodes = GRE_INPUT_N_NEXT, |
| 490 | .next_nodes = { |
| 491 | #define _(s,n) [GRE_INPUT_NEXT_##s] = n, |
| 492 | foreach_gre_input_next |
| 493 | #undef _ |
| 494 | }, |
| 495 | |
| 496 | .format_buffer = format_gre_header_with_length, |
| 497 | .format_trace = format_gre_rx_trace, |
| 498 | .unformat_buffer = unformat_gre_header, |
| 499 | }; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 500 | /* *INDENT-ON* */ |
| 501 | |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 502 | #ifndef CLIB_MARCH_VARIANT |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 503 | void |
| 504 | gre_register_input_protocol (vlib_main_t * vm, |
| 505 | gre_protocol_t protocol, u32 node_index, |
| 506 | gre_tunnel_type_t tunnel_type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 508 | gre_main_t *em = &gre_main; |
| 509 | gre_protocol_info_t *pi; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 510 | next_info_t *n; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 511 | u32 i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | |
| 513 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 514 | clib_error_t *error = vlib_call_init_function (vm, gre_input_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | if (error) |
| 516 | clib_error_report (error); |
| 517 | } |
| 518 | |
| 519 | pi = gre_get_protocol_info (em, protocol); |
| 520 | pi->node_index = node_index; |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 521 | pi->tunnel_type = tunnel_type; |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 522 | pi->next_index = vlib_node_add_next (vm, gre4_input_node.index, node_index); |
| 523 | i = vlib_node_add_next (vm, gre6_input_node.index, node_index); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 524 | ASSERT (i == pi->next_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | |
| 526 | /* Setup gre protocol -> next index sparse vector mapping. */ |
Damjan Marion | 63d5bae | 2017-04-04 01:28:26 +0200 | [diff] [blame] | 527 | n = sparse_vec_validate (em->next_by_protocol, |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 528 | clib_host_to_net_u16 (protocol)); |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 529 | n->next_index = pi->next_index; |
| 530 | n->tunnel_type = tunnel_type; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static void |
| 534 | gre_setup_node (vlib_main_t * vm, u32 node_index) |
| 535 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 536 | vlib_node_t *n = vlib_get_node (vm, node_index); |
| 537 | pg_node_t *pn = pg_get_node (node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 538 | |
| 539 | n->format_buffer = format_gre_header_with_length; |
| 540 | n->unformat_buffer = unformat_gre_header; |
| 541 | pn->unformat_edit = unformat_pg_gre_header; |
| 542 | } |
| 543 | |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 544 | static clib_error_t * |
| 545 | gre_input_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 547 | gre_main_t *gm = &gre_main; |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 548 | vlib_node_t *ethernet_input, *ip4_input, *ip6_input, *mpls_unicast_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 549 | |
| 550 | { |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 551 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | error = vlib_call_init_function (vm, gre_init); |
| 553 | if (error) |
| 554 | clib_error_report (error); |
| 555 | } |
| 556 | |
Ciara Loftus | 7eac916 | 2016-09-30 15:47:03 +0100 | [diff] [blame] | 557 | gre_setup_node (vm, gre4_input_node.index); |
| 558 | gre_setup_node (vm, gre6_input_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | |
Damjan Marion | 63d5bae | 2017-04-04 01:28:26 +0200 | [diff] [blame] | 560 | gm->next_by_protocol = sparse_vec_new |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 561 | ( /* elt bytes */ sizeof (gm->next_by_protocol[0]), |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | /* bits in index */ BITS (((gre_header_t *) 0)->protocol)); |
| 563 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | /* These could be moved to the supported protocol input node defn's */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 565 | ethernet_input = vlib_get_node_by_name (vm, (u8 *) "ethernet-input"); |
| 566 | ASSERT (ethernet_input); |
| 567 | ip4_input = vlib_get_node_by_name (vm, (u8 *) "ip4-input"); |
| 568 | ASSERT (ip4_input); |
| 569 | ip6_input = vlib_get_node_by_name (vm, (u8 *) "ip6-input"); |
| 570 | ASSERT (ip6_input); |
| 571 | mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *) "mpls-input"); |
| 572 | ASSERT (mpls_unicast_input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 574 | gre_register_input_protocol (vm, GRE_PROTOCOL_teb, |
| 575 | ethernet_input->index, GRE_TUNNEL_TYPE_TEB); |
David Hotham | a8cd309 | 2016-09-19 09:55:07 -0700 | [diff] [blame] | 576 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 577 | gre_register_input_protocol (vm, GRE_PROTOCOL_ip4, |
| 578 | ip4_input->index, GRE_TUNNEL_TYPE_L3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 579 | |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 580 | gre_register_input_protocol (vm, GRE_PROTOCOL_ip6, |
| 581 | ip6_input->index, GRE_TUNNEL_TYPE_L3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | |
| 583 | gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast, |
John Lo | a43ccae | 2018-02-13 17:15:23 -0500 | [diff] [blame] | 584 | mpls_unicast_input->index, GRE_TUNNEL_TYPE_L3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 585 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | VLIB_INIT_FUNCTION (gre_input_init); |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 590 | |
Filip Tehlar | 0fce11f | 2019-03-04 09:21:59 -0800 | [diff] [blame] | 591 | #endif /* CLIB_MARCH_VARIANT */ |
Swarup Nayak | 9ff647a | 2017-11-27 10:27:43 +0530 | [diff] [blame] | 592 | /* |
| 593 | * fd.io coding-style-patch-verification: ON |
| 594 | * |
| 595 | * Local Variables: |
| 596 | * eval: (c-set-style "gnu") |
| 597 | * End: |
| 598 | */ |