2 * nsh.c: nsh packet processing
4 * Copyright (c) 2013 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:
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.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe.h>
22 vlib_node_registration_t nsh_vxlan_gpe_input_node;
25 u8 * format_nsh_header_with_length (u8 * s, va_list * args);
32 } nsh_vxlan_gpe_rx_trace_t;
34 static u8 * format_nsh_vxlan_gpe_rx_trace (u8 * s, va_list * args)
36 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38 nsh_vxlan_gpe_rx_trace_t * t = va_arg (*args, nsh_vxlan_gpe_rx_trace_t *);
40 if (t->tunnel_index != ~0)
42 s = format (s, "NSH-VXLAN: tunnel %d next %d error %d", t->tunnel_index,
43 t->next_index, t->error);
47 s = format (s, "NSH-VXLAN: no tunnel next %d error %d\n", t->next_index,
50 s = format (s, "\n %U", format_nsh_header_with_length, &t->h,
51 (u32) sizeof (t->h) /* max size */);
56 nsh_vxlan_gpe_input (vlib_main_t * vm,
57 vlib_node_runtime_t * node,
58 vlib_frame_t * from_frame)
60 u32 n_left_from, next_index, * from, * to_next;
61 nsh_vxlan_gpe_main_t * ngm = &nsh_vxlan_gpe_main;
62 vnet_main_t * vnm = ngm->vnet_main;
63 vnet_interface_main_t * im = &vnm->interface_main;
64 u32 last_tunnel_index = ~0;
65 nsh_vxlan_gpe_tunnel_key_t last_key;
66 u32 pkts_decapsulated = 0;
67 u32 cpu_index = os_get_cpu_number();
68 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
70 memset (&last_key, 0xff, sizeof (last_key));
72 from = vlib_frame_vector_args (from_frame);
73 n_left_from = from_frame->n_vectors;
75 next_index = node->cached_next_index;
76 stats_sw_if_index = node->runtime_data[0];
77 stats_n_packets = stats_n_bytes = 0;
79 while (n_left_from > 0)
83 vlib_get_next_frame (vm, node, next_index,
84 to_next, n_left_to_next);
86 while (n_left_from >= 4 && n_left_to_next >= 2)
89 vlib_buffer_t * b0, * b1;
91 ip4_vxlan_gpe_and_nsh_header_t * iuvn0, * iuvn1;
93 u32 tunnel_index0, tunnel_index1;
94 nsh_vxlan_gpe_tunnel_t * t0, * t1;
95 nsh_vxlan_gpe_tunnel_key_t key0, key1;
97 u32 sw_if_index0, sw_if_index1, len0, len1;
99 /* Prefetch next iteration. */
101 vlib_buffer_t * p2, * p3;
103 p2 = vlib_get_buffer (vm, from[2]);
104 p3 = vlib_get_buffer (vm, from[3]);
106 vlib_prefetch_buffer_header (p2, LOAD);
107 vlib_prefetch_buffer_header (p3, LOAD);
109 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
110 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
122 b0 = vlib_get_buffer (vm, bi0);
123 b1 = vlib_get_buffer (vm, bi1);
125 /* udp leaves current_data pointing at the vxlan header */
127 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
129 (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
131 iuvn0 = vlib_buffer_get_current (b0);
132 iuvn1 = vlib_buffer_get_current (b1);
134 /* pop (ip, udp, vxlan, nsh) */
135 vlib_buffer_advance (b0, sizeof (*iuvn0));
136 vlib_buffer_advance (b1, sizeof (*iuvn1));
140 next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
144 next1 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
146 key0.src = iuvn0->ip4.src_address.as_u32;
147 key0.vni = iuvn0->vxlan.vni_res;
148 key0.spi_si = iuvn0->nsh.spi_si;
151 if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
152 || (key0.as_u64[1] != last_key.as_u64[1])))
154 p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
158 error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
162 last_key.as_u64[0] = key0.as_u64[0];
163 last_key.as_u64[1] = key0.as_u64[1];
164 tunnel_index0 = last_tunnel_index = p0[0];
167 tunnel_index0 = last_tunnel_index;
169 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
171 next0 = t0->decap_next_index;
172 sw_if_index0 = t0->sw_if_index;
173 len0 = vlib_buffer_length_in_chain(vm, b0);
175 /* Required to make the l2 tag push / pop code work on l2 subifs */
176 vnet_update_l2_len (b0);
178 if (next0 == NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP)
181 * Functioning as SFF (ie "half NSH tunnel mode")
182 * If ingress (we are in decap.c) with NSH header, and 'decap next nsh-vxlan-gpe' then "NSH switch"
183 * 1. Take DST, remap to SRC, remap other keys in place
184 * 2. Look up new t0 as per above
185 * 3. Set sw_if_index[VLIB_TX] to be t0->sw_if_index
188 nsh_vxlan_gpe_tunnel_t * next_t0;
189 nsh_vxlan_gpe_tunnel_key_t next_key0;
191 next_key0.src = iuvn0->ip4.dst_address.as_u32;
192 next_key0.vni = iuvn0->vxlan.vni_res;
193 next_key0.spi_si = iuvn0->nsh.spi_si;
196 next_p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &next_key0);
200 error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
203 next_t0 = pool_elt_at_index (ngm->tunnels, next_p0[0]);
204 vnet_buffer(b0)->sw_if_index[VLIB_TX] = next_t0->sw_if_index;
210 * ip[46] lookup in the configured FIB
211 * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
213 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
217 stats_n_packets += 1;
218 stats_n_bytes += len0;
220 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
222 stats_n_packets -= 1;
223 stats_n_bytes -= len0;
225 vlib_increment_combined_counter(
226 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
227 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
229 stats_n_bytes = len0;
230 stats_sw_if_index = sw_if_index0;
234 b0->error = error0 ? node->errors[error0] : 0;
236 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
238 nsh_vxlan_gpe_rx_trace_t *tr
239 = vlib_add_trace (vm, node, b0, sizeof (*tr));
240 tr->next_index = next0;
242 tr->tunnel_index = tunnel_index0;
246 key1.src = iuvn1->ip4.src_address.as_u32;
247 key1.vni = iuvn1->vxlan.vni_res;
248 key1.spi_si = iuvn1->nsh.spi_si;
251 if (PREDICT_FALSE ((key1.as_u64[0] != last_key.as_u64[0])
252 || (key1.as_u64[1] != last_key.as_u64[1])))
254 p1 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key1);
258 error1 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
262 last_key.as_u64[0] = key1.as_u64[0];
263 last_key.as_u64[1] = key1.as_u64[1];
264 tunnel_index1 = last_tunnel_index = p1[0];
267 tunnel_index1 = last_tunnel_index;
269 t1 = pool_elt_at_index (ngm->tunnels, tunnel_index1);
271 next1 = t1->decap_next_index;
272 sw_if_index1 = t1->sw_if_index;
273 len1 = vlib_buffer_length_in_chain(vm, b1);
275 /* Required to make the l2 tag push / pop code work on l2 subifs */
276 vnet_update_l2_len (b1);
278 if (next1 == NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP)
281 * Functioning as SFF (ie "half NSH tunnel mode")
282 * If ingress (we are in decap.c) with NSH header, and 'decap next nsh-vxlan-gpe' then "NSH switch"
283 * 1. Take DST, remap to SRC, remap other keys in place
284 * 2. Look up new t0 as per above
285 * 3. Set sw_if_index[VLIB_TX] to be t0->sw_if_index
288 nsh_vxlan_gpe_tunnel_t * next_t1;
289 nsh_vxlan_gpe_tunnel_key_t next_key1;
291 next_key1.src = iuvn1->ip4.dst_address.as_u32;
292 next_key1.vni = iuvn1->vxlan.vni_res;
293 next_key1.spi_si = iuvn1->nsh.spi_si;
296 next_p1 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &next_key1);
300 error1 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
303 next_t1 = pool_elt_at_index (ngm->tunnels, next_p1[0]);
304 vnet_buffer(b1)->sw_if_index[VLIB_TX] = next_t1->sw_if_index;
310 * ip[46] lookup in the configured FIB
311 * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
313 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
317 stats_n_packets += 1;
318 stats_n_bytes += len1;
319 /* Batch stats increment on the same vxlan tunnel so counter
320 is not incremented per packet */
321 if (PREDICT_FALSE(sw_if_index1 != stats_sw_if_index))
323 stats_n_packets -= 1;
324 stats_n_bytes -= len1;
326 vlib_increment_combined_counter(
327 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
328 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
330 stats_n_bytes = len1;
331 stats_sw_if_index = sw_if_index1;
333 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
336 b1->error = error1 ? node->errors[error1] : 0;
338 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
340 nsh_vxlan_gpe_rx_trace_t *tr
341 = vlib_add_trace (vm, node, b1, sizeof (*tr));
342 tr->next_index = next1;
344 tr->tunnel_index = tunnel_index1;
348 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
349 to_next, n_left_to_next,
350 bi0, bi1, next0, next1);
353 while (n_left_from > 0 && n_left_to_next > 0)
358 ip4_vxlan_gpe_and_nsh_header_t * iuvn0;
361 nsh_vxlan_gpe_tunnel_t * t0;
362 nsh_vxlan_gpe_tunnel_key_t key0;
364 u32 sw_if_index0, len0;
373 b0 = vlib_get_buffer (vm, bi0);
375 /* udp leaves current_data pointing at the vxlan header */
377 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
379 iuvn0 = vlib_buffer_get_current (b0);
381 /* pop (ip, udp, vxlan, nsh) */
382 vlib_buffer_advance (b0, sizeof (*iuvn0));
386 next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
388 key0.src = iuvn0->ip4.src_address.as_u32;
389 key0.vni = iuvn0->vxlan.vni_res;
390 key0.spi_si = iuvn0->nsh.spi_si;
393 if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
394 || (key0.as_u64[1] != last_key.as_u64[1])))
396 p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
400 error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
404 last_key.as_u64[0] = key0.as_u64[0];
405 last_key.as_u64[1] = key0.as_u64[1];
406 tunnel_index0 = last_tunnel_index = p0[0];
409 tunnel_index0 = last_tunnel_index;
411 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
413 next0 = t0->decap_next_index;
414 sw_if_index0 = t0->sw_if_index;
415 len0 = vlib_buffer_length_in_chain(vm, b0);
417 /* Required to make the l2 tag push / pop code work on l2 subifs */
418 vnet_update_l2_len (b0);
420 if (next0 == NSH_VXLAN_GPE_INPUT_NEXT_NSH_VXLAN_GPE_ENCAP)
423 * Functioning as SFF (ie "half NSH tunnel mode")
424 * If ingress (we are in decap.c) with NSH header, and 'decap next nsh-vxlan-gpe' then "NSH switch"
425 * 1. Take DST, remap to SRC, remap other keys in place
426 * 2. Look up new t0 as per above
427 * 3. Set sw_if_index[VLIB_TX] to be t0->sw_if_index
430 nsh_vxlan_gpe_tunnel_t * next_t0;
431 nsh_vxlan_gpe_tunnel_key_t next_key0;
433 next_key0.src = iuvn0->ip4.dst_address.as_u32;
434 next_key0.vni = iuvn0->vxlan.vni_res;
435 next_key0.spi_si = iuvn0->nsh.spi_si;
438 next_p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &next_key0);
442 error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
445 next_t0 = pool_elt_at_index (ngm->tunnels, next_p0[0]);
446 vnet_buffer(b0)->sw_if_index[VLIB_TX] = next_t0->sw_if_index;
452 * ip[46] lookup in the configured FIB
453 * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
455 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
458 pkts_decapsulated ++;
460 stats_n_packets += 1;
461 stats_n_bytes += len0;
463 /* Batch stats increment on the same nsh-vxlan-gpe tunnel so counter
464 is not incremented per packet */
465 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
467 stats_n_packets -= 1;
468 stats_n_bytes -= len0;
470 vlib_increment_combined_counter(
471 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
472 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
474 stats_n_bytes = len0;
475 stats_sw_if_index = sw_if_index0;
479 b0->error = error0 ? node->errors[error0] : 0;
481 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
483 nsh_vxlan_gpe_rx_trace_t *tr
484 = vlib_add_trace (vm, node, b0, sizeof (*tr));
485 tr->next_index = next0;
487 tr->tunnel_index = tunnel_index0;
490 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
491 to_next, n_left_to_next,
495 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
497 vlib_node_increment_counter (vm, nsh_vxlan_gpe_input_node.index,
498 NSH_VXLAN_GPE_ERROR_DECAPSULATED,
500 /* Increment any remaining batch stats */
503 vlib_increment_combined_counter(
504 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, cpu_index,
505 stats_sw_if_index, stats_n_packets, stats_n_bytes);
506 node->runtime_data[0] = stats_sw_if_index;
508 return from_frame->n_vectors;
511 static char * nsh_vxlan_gpe_error_strings[] = {
512 #define nsh_vxlan_gpe_error(n,s) s,
513 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe_error.def>
514 #undef nsh_vxlan_gpe_error
518 VLIB_REGISTER_NODE (nsh_vxlan_gpe_input_node) = {
519 .function = nsh_vxlan_gpe_input,
520 .name = "nsh-vxlan-gpe-input",
521 /* Takes a vector of packets. */
522 .vector_size = sizeof (u32),
524 .n_errors = NSH_VXLAN_GPE_N_ERROR,
525 .error_strings = nsh_vxlan_gpe_error_strings,
527 .n_next_nodes = NSH_VXLAN_GPE_INPUT_N_NEXT,
529 #define _(s,n) [NSH_VXLAN_GPE_INPUT_NEXT_##s] = n,
530 foreach_nsh_vxlan_gpe_input_next
534 .format_buffer = format_nsh_header_with_length,
535 .format_trace = format_nsh_vxlan_gpe_rx_trace,
536 // $$$$ .unformat_buffer = unformat_nsh_vxlan_gpe_header,