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-gre/nsh_gre.h>
21 #include <vnet/nsh-gre/nsh_gre_packet.h>
23 vlib_node_registration_t nsh_input_node;
33 u8 * format_nsh_header_with_length (u8 * s, va_list * args)
35 nsh_header_t * h = va_arg (*args, nsh_header_t *);
36 u32 max_header_bytes = va_arg (*args, u32);
37 u32 tmp, header_bytes;
39 header_bytes = sizeof (h[0]);
40 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
41 return format (s, "gre-nsh header truncated");
43 s = format (s, "ver %d ", h->ver_o_c>>6);
45 if (h->ver_o_c & NSH_GRE_O_BIT)
46 s = format (s, "O-set ");
48 if (h->ver_o_c & NSH_GRE_C_BIT)
49 s = format (s, "C-set ");
51 s = format (s, "len %d (%d bytes) md_type %d next_protocol %d\n",
52 h->length, h->length * 4, h->md_type, h->next_protocol);
54 tmp = clib_net_to_host_u32 (h->spi_si);
56 s = format (s, " spi %d si %d ",
57 (tmp>>NSH_GRE_SPI_SHIFT) & NSH_GRE_SPI_MASK,
58 tmp & NSH_GRE_SINDEX_MASK);
60 s = format (s, "c1 %u c2 %u c3 %u c4 %u",
61 clib_net_to_host_u32 (h->c1),
62 clib_net_to_host_u32 (h->c2),
63 clib_net_to_host_u32 (h->c3),
64 clib_net_to_host_u32 (h->c4));
70 u8 * format_nsh_rx_trace (u8 * s, va_list * args)
72 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74 nsh_rx_trace_t * t = va_arg (*args, nsh_rx_trace_t *);
76 if (t->tunnel_index != ~0)
78 s = format (s, "NSH: tunnel %d next %d error %d", t->tunnel_index,
79 t->next_index, t->error);
83 s = format (s, "NSH: no tunnel next %d error %d\n", t->next_index,
86 s = format (s, "\n %U", format_nsh_header_with_length, &t->h,
87 (u32) sizeof (t->h) /* max size */);
92 nsh_gre_input (vlib_main_t * vm,
93 vlib_node_runtime_t * node,
94 vlib_frame_t * from_frame)
96 u32 n_left_from, next_index, * from, * to_next;
97 nsh_gre_main_t * ngm = &nsh_gre_main;
98 vnet_main_t * vnm = ngm->vnet_main;
99 vnet_interface_main_t * im = &vnm->interface_main;
100 u32 last_tunnel_index = ~0;
101 u64 last_key = ~0ULL;
102 u32 pkts_decapsulated = 0;
103 u32 cpu_index = os_get_cpu_number();
104 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
106 from = vlib_frame_vector_args (from_frame);
107 n_left_from = from_frame->n_vectors;
109 next_index = node->cached_next_index;
110 stats_sw_if_index = node->runtime_data[0];
111 stats_n_packets = stats_n_bytes = 0;
113 while (n_left_from > 0)
117 vlib_get_next_frame (vm, node, next_index,
118 to_next, n_left_to_next);
120 while (n_left_from >= 4 && n_left_to_next >= 2)
123 vlib_buffer_t * b0, * b1;
125 nsh_header_t * h0, * h1;
127 u32 tunnel_index0, tunnel_index1;
128 nsh_gre_tunnel_t * t0, * t1;
131 u32 sw_if_index0, sw_if_index1, len0, len1;
133 /* Prefetch next iteration. */
135 vlib_buffer_t * p2, * p3;
137 p2 = vlib_get_buffer (vm, from[2]);
138 p3 = vlib_get_buffer (vm, from[3]);
140 vlib_prefetch_buffer_header (p2, LOAD);
141 vlib_prefetch_buffer_header (p3, LOAD);
143 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
144 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
156 b0 = vlib_get_buffer (vm, bi0);
157 b1 = vlib_get_buffer (vm, bi1);
159 h0 = vlib_buffer_get_current (b0);
160 h1 = vlib_buffer_get_current (b1);
162 /* gre stashed the src ip4 address for us... */
163 key0 = (((u64)(vnet_buffer(b0)->gre.src))<<32) | h0->spi_si;
164 key1 = (((u64)(vnet_buffer(b1)->gre.src))<<32) | h1->spi_si;
166 /* "pop" nsh header */
167 vlib_buffer_advance (b0, sizeof (*h0));
168 vlib_buffer_advance (b1, sizeof (*h1));
174 next0 = NSH_INPUT_NEXT_DROP;
175 next1 = NSH_INPUT_NEXT_DROP;
177 if (PREDICT_FALSE(key0 != last_key))
179 p0 = hash_get (ngm->nsh_gre_tunnel_by_src_address, key0);
183 error0 = NSH_GRE_ERROR_NO_SUCH_TUNNEL;
188 tunnel_index0 = last_tunnel_index = p0[0];
191 tunnel_index0 = last_tunnel_index;
193 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
195 next0 = t0->decap_next_index;
196 sw_if_index0 = t0->sw_if_index;
197 len0 = vlib_buffer_length_in_chain(vm, b0);
199 /* Required to make the l2 tag push / pop code work on l2 subifs */
200 vnet_update_l2_len (b0);
202 next0 = t0->decap_next_index;
204 /* ip[46] lookup in the configured FIB, otherwise an opaque */
205 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
208 stats_n_packets += 1;
209 stats_n_bytes += len0;
211 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
213 stats_n_packets -= 1;
214 stats_n_bytes -= len0;
216 vlib_increment_combined_counter(
217 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
218 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
220 stats_n_bytes = len0;
221 stats_sw_if_index = sw_if_index0;
225 b0->error = error0 ? node->errors[error0] : 0;
227 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
229 nsh_rx_trace_t *tr = vlib_add_trace (vm, node,
231 tr->next_index = next0;
233 tr->tunnel_index = tunnel_index0;
237 if (PREDICT_FALSE(key1 != last_key))
239 p1 = hash_get (ngm->nsh_gre_tunnel_by_src_address, key1);
243 error1 = NSH_GRE_ERROR_NO_SUCH_TUNNEL;
248 tunnel_index1 = last_tunnel_index = p1[0];
251 tunnel_index1 = last_tunnel_index;
253 t1 = pool_elt_at_index (ngm->tunnels, tunnel_index1);
255 next1 = t1->decap_next_index;
256 sw_if_index1 = t1->sw_if_index;
257 len1 = vlib_buffer_length_in_chain(vm, b1);
259 /* Required to make the l2 tag push / pop code work on l2 subifs */
260 vnet_update_l2_len (b1);
262 next1 = t1->decap_next_index;
264 /* ip[46] lookup in the configured FIB, otherwise an opaque */
265 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
268 stats_n_packets += 1;
269 stats_n_bytes += len1;
270 /* Batch stats increment on the same nsh-gre tunnel so counter
271 is not incremented per packet */
272 if (PREDICT_FALSE(sw_if_index1 != stats_sw_if_index))
274 stats_n_packets -= 1;
275 stats_n_bytes -= len1;
277 vlib_increment_combined_counter(
278 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
279 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
281 stats_n_bytes = len1;
282 stats_sw_if_index = sw_if_index1;
284 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
287 b1->error = error1 ? node->errors[error1] : 0;
289 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
291 nsh_rx_trace_t *tr = vlib_add_trace (vm, node,
293 tr->next_index = next1;
295 tr->tunnel_index = tunnel_index1;
299 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
300 to_next, n_left_to_next,
301 bi0, bi1, next0, next1);
304 while (n_left_from > 0 && n_left_to_next > 0)
312 nsh_gre_tunnel_t * t0;
315 u32 sw_if_index0, len0;
324 b0 = vlib_get_buffer (vm, bi0);
325 h0 = vlib_buffer_get_current (b0);
327 /* gre stashed the src ip4 address for us... */
328 key0 = (((u64)(vnet_buffer(b0)->gre.src))<<32) | h0->spi_si;
330 /* "pop" nsh header */
331 vlib_buffer_advance (b0, sizeof (*h0));
335 next0 = NSH_INPUT_NEXT_DROP;
337 if (PREDICT_FALSE(key0 != last_key))
339 p0 = hash_get (ngm->nsh_gre_tunnel_by_src_address, key0);
343 error0 = NSH_GRE_ERROR_NO_SUCH_TUNNEL;
348 tunnel_index0 = last_tunnel_index = p0[0];
351 tunnel_index0 = last_tunnel_index;
353 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
355 next0 = t0->decap_next_index;
356 sw_if_index0 = t0->sw_if_index;
357 len0 = vlib_buffer_length_in_chain(vm, b0);
359 /* Required to make the l2 tag push / pop code work on l2 subifs */
360 vnet_update_l2_len (b0);
362 next0 = t0->decap_next_index;
364 /* ip[46] lookup in the configured FIB, otherwise an opaque */
365 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
366 pkts_decapsulated ++;
368 stats_n_packets += 1;
369 stats_n_bytes += len0;
371 /* Batch stats increment on the same nsh-gre tunnel so counter
372 is not incremented per packet */
373 if (PREDICT_FALSE(sw_if_index0 != stats_sw_if_index))
375 stats_n_packets -= 1;
376 stats_n_bytes -= len0;
378 vlib_increment_combined_counter(
379 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
380 cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
382 stats_n_bytes = len0;
383 stats_sw_if_index = sw_if_index0;
387 b0->error = error0 ? node->errors[error0] : 0;
389 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
391 nsh_rx_trace_t *tr = vlib_add_trace (vm, node,
393 tr->next_index = next0;
395 tr->tunnel_index = tunnel_index0;
398 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
399 to_next, n_left_to_next,
403 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
405 vlib_node_increment_counter (vm, nsh_gre_input_node.index,
406 NSH_GRE_ERROR_DECAPSULATED,
408 /* Increment any remaining batch stats */
411 vlib_increment_combined_counter(
412 im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, cpu_index,
413 stats_sw_if_index, stats_n_packets, stats_n_bytes);
414 node->runtime_data[0] = stats_sw_if_index;
416 return from_frame->n_vectors;
419 static char * nsh_error_strings[] = {
420 #define nsh_gre_error(n,s) s,
421 #include <vnet/nsh-gre/nsh_gre_error.def>
426 VLIB_REGISTER_NODE (nsh_gre_input_node) = {
427 .function = nsh_gre_input,
428 .name = "nsh-gre-input",
429 /* Takes a vector of packets. */
430 .vector_size = sizeof (u32),
432 .n_errors = NSH_GRE_N_ERROR,
433 .error_strings = nsh_error_strings,
435 .n_next_nodes = NSH_INPUT_N_NEXT,
437 #define _(s,n) [NSH_INPUT_NEXT_##s] = n,
438 foreach_nsh_gre_input_next
442 .format_buffer = format_nsh_header_with_length,
443 .format_trace = format_nsh_rx_trace,
444 // $$$$ .unformat_buffer = unformat_nsh_gre_header,