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 u32 last_tunnel_index = ~0;
63 nsh_vxlan_gpe_tunnel_key_t last_key;
64 u32 pkts_decapsulated = 0;
66 memset (&last_key, 0xff, sizeof (last_key));
68 from = vlib_frame_vector_args (from_frame);
69 n_left_from = from_frame->n_vectors;
71 next_index = node->cached_next_index;
73 while (n_left_from > 0)
77 vlib_get_next_frame (vm, node, next_index,
78 to_next, n_left_to_next);
80 while (n_left_from >= 4 && n_left_to_next >= 2)
83 vlib_buffer_t * b0, * b1;
85 ip4_vxlan_gpe_and_nsh_header_t * iuvn0, * iuvn1;
87 u32 tunnel_index0, tunnel_index1;
88 nsh_vxlan_gpe_tunnel_t * t0, * t1;
89 nsh_vxlan_gpe_tunnel_key_t key0, key1;
92 /* Prefetch next iteration. */
94 vlib_buffer_t * p2, * p3;
96 p2 = vlib_get_buffer (vm, from[2]);
97 p3 = vlib_get_buffer (vm, from[3]);
99 vlib_prefetch_buffer_header (p2, LOAD);
100 vlib_prefetch_buffer_header (p3, LOAD);
102 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
103 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
115 b0 = vlib_get_buffer (vm, bi0);
116 b1 = vlib_get_buffer (vm, bi1);
118 /* udp leaves current_data pointing at the vxlan header */
120 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
122 (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
124 iuvn0 = vlib_buffer_get_current (b0);
125 iuvn1 = vlib_buffer_get_current (b1);
127 /* pop (ip, udp, vxlan, nsh) */
128 vlib_buffer_advance (b0, sizeof (*iuvn0));
129 vlib_buffer_advance (b1, sizeof (*iuvn1));
133 next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
137 next1 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
139 key0.src = iuvn0->ip4.src_address.as_u32;
140 key0.vni = iuvn0->vxlan.vni_res;
141 key0.spi_si = iuvn0->nsh.spi_si;
144 if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
145 || (key0.as_u64[1] != last_key.as_u64[1])))
147 p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
151 error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
155 last_key.as_u64[0] = key0.as_u64[0];
156 last_key.as_u64[1] = key0.as_u64[1];
157 tunnel_index0 = last_tunnel_index = p0[0];
160 tunnel_index0 = last_tunnel_index;
162 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
164 next0 = t0->decap_next_index;
166 /* Required to make the l2 tag push / pop code work on l2 subifs */
167 vnet_update_l2_len (b0);
170 * ip[46] lookup in the configured FIB
171 * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
173 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
176 b0->error = error0 ? node->errors[error0] : 0;
178 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
180 nsh_vxlan_gpe_rx_trace_t *tr
181 = vlib_add_trace (vm, node, b0, sizeof (*tr));
182 tr->next_index = next0;
184 tr->tunnel_index = tunnel_index0;
188 key1.src = iuvn1->ip4.src_address.as_u32;
189 key1.vni = iuvn1->vxlan.vni_res;
190 key1.spi_si = iuvn1->nsh.spi_si;
193 if (PREDICT_FALSE ((key1.as_u64[0] != last_key.as_u64[0])
194 || (key1.as_u64[1] != last_key.as_u64[1])))
196 p1 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key1);
200 error1 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
204 last_key.as_u64[0] = key1.as_u64[0];
205 last_key.as_u64[1] = key1.as_u64[1];
206 tunnel_index1 = last_tunnel_index = p1[0];
209 tunnel_index1 = last_tunnel_index;
211 t1 = pool_elt_at_index (ngm->tunnels, tunnel_index1);
213 next1 = t1->decap_next_index;
215 /* Required to make the l2 tag push / pop code work on l2 subifs */
216 vnet_update_l2_len (b1);
219 * ip[46] lookup in the configured FIB
220 * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
222 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->decap_fib_index;
223 pkts_decapsulated += 2;
226 b1->error = error1 ? node->errors[error1] : 0;
228 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
230 nsh_vxlan_gpe_rx_trace_t *tr
231 = vlib_add_trace (vm, node, b1, sizeof (*tr));
232 tr->next_index = next1;
234 tr->tunnel_index = tunnel_index1;
238 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
239 to_next, n_left_to_next,
240 bi0, bi1, next0, next1);
243 while (n_left_from > 0 && n_left_to_next > 0)
248 ip4_vxlan_gpe_and_nsh_header_t * iuvn0;
251 nsh_vxlan_gpe_tunnel_t * t0;
252 nsh_vxlan_gpe_tunnel_key_t key0;
262 b0 = vlib_get_buffer (vm, bi0);
264 /* udp leaves current_data pointing at the vxlan header */
266 (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t)));
268 iuvn0 = vlib_buffer_get_current (b0);
270 /* pop (ip, udp, vxlan, nsh) */
271 vlib_buffer_advance (b0, sizeof (*iuvn0));
275 next0 = NSH_VXLAN_GPE_INPUT_NEXT_DROP;
277 key0.src = iuvn0->ip4.src_address.as_u32;
278 key0.vni = iuvn0->vxlan.vni_res;
279 key0.spi_si = iuvn0->nsh.spi_si;
282 if (PREDICT_FALSE ((key0.as_u64[0] != last_key.as_u64[0])
283 || (key0.as_u64[1] != last_key.as_u64[1])))
285 p0 = hash_get_mem (ngm->nsh_vxlan_gpe_tunnel_by_key, &key0);
289 error0 = NSH_VXLAN_GPE_ERROR_NO_SUCH_TUNNEL;
293 last_key.as_u64[0] = key0.as_u64[0];
294 last_key.as_u64[1] = key0.as_u64[1];
295 tunnel_index0 = last_tunnel_index = p0[0];
298 tunnel_index0 = last_tunnel_index;
300 t0 = pool_elt_at_index (ngm->tunnels, tunnel_index0);
302 next0 = t0->decap_next_index;
304 /* Required to make the l2 tag push / pop code work on l2 subifs */
305 vnet_update_l2_len (b0);
308 * ip[46] lookup in the configured FIB
309 * nsh-vxlan-gpe-encap, here's the encap tunnel sw_if_index
311 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->decap_fib_index;
312 pkts_decapsulated ++;
315 b0->error = error0 ? node->errors[error0] : 0;
317 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
319 nsh_vxlan_gpe_rx_trace_t *tr
320 = vlib_add_trace (vm, node, b0, sizeof (*tr));
321 tr->next_index = next0;
323 tr->tunnel_index = tunnel_index0;
326 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
327 to_next, n_left_to_next,
331 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
333 vlib_node_increment_counter (vm, nsh_vxlan_gpe_input_node.index,
334 NSH_VXLAN_GPE_ERROR_DECAPSULATED,
336 return from_frame->n_vectors;
339 static char * nsh_vxlan_gpe_error_strings[] = {
340 #define nsh_vxlan_gpe_error(n,s) s,
341 #include <vnet/nsh-vxlan-gpe/nsh_vxlan_gpe_error.def>
342 #undef nsh_vxlan_gpe_error
346 VLIB_REGISTER_NODE (nsh_vxlan_gpe_input_node) = {
347 .function = nsh_vxlan_gpe_input,
348 .name = "nsh-vxlan-gpe-input",
349 /* Takes a vector of packets. */
350 .vector_size = sizeof (u32),
352 .n_errors = NSH_VXLAN_GPE_N_ERROR,
353 .error_strings = nsh_vxlan_gpe_error_strings,
355 .n_next_nodes = NSH_VXLAN_GPE_INPUT_N_NEXT,
357 #define _(s,n) [NSH_VXLAN_GPE_INPUT_NEXT_##s] = n,
358 foreach_nsh_vxlan_gpe_input_next
362 .format_buffer = format_nsh_header_with_length,
363 .format_trace = format_nsh_vxlan_gpe_rx_trace,
364 // $$$$ .unformat_buffer = unformat_nsh_vxlan_gpe_header,