2 * node.c: mpls-o-gre decap processing
4 * Copyright (c) 2012-2014 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/mpls-gre/mpls.h>
26 u32 label_host_byte_order;
29 u8 * format_mpls_rx_trace (u8 * s, va_list * args)
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 mpls_rx_trace_t * t = va_arg (*args, mpls_rx_trace_t *);
38 #define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b;
39 foreach_mpls_input_next;
42 s = format (s, "MPLS: next %s, lookup fib index %d, decap index %d\n",
43 next_name, t->next_index, t->tx_fib_index, t->decap_index);
44 if (t->decap_index != ~0)
46 s = format (s, " label %d",
47 vnet_mpls_uc_get_label(t->label_host_byte_order));
52 vlib_node_registration_t mpls_input_node;
56 u32 last_inner_fib_index;
57 u32 last_outer_fib_index;
58 mpls_main_t * mpls_main;
59 } mpls_input_runtime_t;
62 mpls_input_inline (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
64 vlib_frame_t * from_frame, int is_mpls_o_gre)
66 u32 n_left_from, next_index, * from, * to_next;
67 ip4_main_t * im = &ip4_main;
68 from = vlib_frame_vector_args (from_frame);
69 n_left_from = from_frame->n_vectors;
70 mpls_input_runtime_t * rt;
73 rt = vlib_node_get_runtime_data (vm, mpls_input_node.index);
76 * Force an initial lookup every time, in case the control-plane
77 * changed the label->FIB mapping.
81 next_index = node->cached_next_index;
83 while (n_left_from > 0)
87 vlib_get_next_frame (vm, node, next_index,
88 to_next, n_left_to_next);
91 while (n_left_from >= 4 && n_left_to_next >= 2)
94 vlib_buffer_t * b0, * b1;
95 mpls_unicast_header_t * h0, * h1;
101 u32 fib_index0, fib_index1;
103 /* Prefetch next iteration. */
105 vlib_buffer_t * p2, * p3;
107 p2 = vlib_get_buffer (vm, from[2]);
108 p3 = vlib_get_buffer (vm, from[3]);
110 vlib_prefetch_buffer_header (p2, LOAD);
111 vlib_prefetch_buffer_header (p3, LOAD);
113 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
114 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
126 b0 = vlib_get_buffer (vm, bi0);
127 b1 = vlib_get_buffer (vm, bi1);
129 /* $$$$$ dual loop me */
131 vlib_buffer_advance (b0, sizeof (*h0));
132 vlib_buffer_advance (b1, sizeof (*h1));
134 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
135 to_next, n_left_to_next,
136 bi0, bi1, next0, next1);
141 while (n_left_from > 0 && n_left_to_next > 0)
145 mpls_unicast_header_t * h0;
160 b0 = vlib_get_buffer (vm, bi0);
161 h0 = vlib_buffer_get_current (b0);
165 rx_fib_index0 = vec_elt (im->fib_index_by_sw_if_index,
166 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
171 /* If separate RX numbering spaces are required... */
172 rx_fib_index0 = vec_elt (mm->fib_index_by_sw_if_index,
173 vnet_buffer(b0)->sw_if_index[VLIB_RX]);
182 * Expect the control-plane team to squeal like pigs.
183 * If they don't program a decap label entry for each
184 * and every label in the stack, packets go into the trash...
189 label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
191 if (PREDICT_FALSE(vnet_mpls_uc_get_ttl (label0) == 0))
193 next0 = MPLS_INPUT_NEXT_DROP;
194 b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
198 key0 = ((u64)rx_fib_index0<<32)
199 | ((u64)vnet_mpls_uc_get_label (label0)<<12)
200 | ((u64)vnet_mpls_uc_get_s (label0)<<8);
203 * The architecture crew claims that we won't need
204 * separate ip4, ip6, mpls-o-ethernet label numbering
205 * spaces. Use the low 8 key bits as a discriminator.
208 p0 = hash_get (mm->mpls_decap_by_rx_fib_and_label, key0);
211 next0 = MPLS_INPUT_NEXT_DROP;
212 b0->error = node->errors[MPLS_ERROR_BAD_LABEL];
215 d0 = pool_elt_at_index (mm->decaps, p0[0]);
216 next0 = d0->next_index;
217 vnet_buffer(b0)->sw_if_index[VLIB_TX] = d0->tx_fib_index;
218 vlib_buffer_advance (b0, sizeof (*h0));
219 h0 = vlib_buffer_get_current (b0);
220 } while (!vnet_mpls_uc_get_s(label0));
222 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
224 mpls_rx_trace_t *tr = vlib_add_trace (vm, node,
226 tr->next_index = next0;
227 tr->decap_index = d0 ? d0 - mm->decaps : ~0;
228 tr->tx_fib_index = vnet_buffer(b0)->sw_if_index[VLIB_TX];
229 tr->label_host_byte_order = label0;
232 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
233 to_next, n_left_to_next,
237 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
239 vlib_node_increment_counter (vm, mpls_input_node.index,
240 MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
241 return from_frame->n_vectors;
245 mpls_input (vlib_main_t * vm,
246 vlib_node_runtime_t * node,
247 vlib_frame_t * from_frame)
249 return mpls_input_inline (vm, node, from_frame, 1 /* is mpls-o-gre */);
252 static char * mpls_error_strings[] = {
253 #define mpls_error(n,s) s,
258 VLIB_REGISTER_NODE (mpls_input_node) = {
259 .function = mpls_input,
260 .name = "mpls-gre-input",
261 /* Takes a vector of packets. */
262 .vector_size = sizeof (u32),
264 .runtime_data_bytes = sizeof(mpls_input_runtime_t),
266 .n_errors = MPLS_N_ERROR,
267 .error_strings = mpls_error_strings,
269 .n_next_nodes = MPLS_INPUT_N_NEXT,
271 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
272 foreach_mpls_input_next
276 .format_buffer = format_mpls_gre_header_with_length,
277 .format_trace = format_mpls_rx_trace,
278 .unformat_buffer = unformat_mpls_gre_header,
282 mpls_ethernet_input (vlib_main_t * vm,
283 vlib_node_runtime_t * node,
284 vlib_frame_t * from_frame)
286 return mpls_input_inline (vm, node, from_frame, 0 /* is mpls-o-gre */);
290 VLIB_REGISTER_NODE (mpls_ethernet_input_node) = {
291 .function = mpls_ethernet_input,
292 .name = "mpls-ethernet-input",
293 /* Takes a vector of packets. */
294 .vector_size = sizeof (u32),
296 .runtime_data_bytes = sizeof(mpls_input_runtime_t),
298 .n_errors = MPLS_N_ERROR,
299 .error_strings = mpls_error_strings,
301 .n_next_nodes = MPLS_INPUT_N_NEXT,
303 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
304 foreach_mpls_input_next
308 .format_buffer = format_mpls_eth_header_with_length,
309 .format_trace = format_mpls_rx_trace,
310 .unformat_buffer = unformat_mpls_gre_header,
314 mpls_setup_nodes (vlib_main_t * vm)
316 vlib_node_t * n = vlib_get_node (vm, mpls_input_node.index);
317 pg_node_t * pn = pg_get_node (mpls_input_node.index);
318 mpls_input_runtime_t * rt;
320 n->format_buffer = format_mpls_gre_header_with_length;
321 n->unformat_buffer = unformat_mpls_gre_header;
322 pn->unformat_edit = unformat_pg_mpls_header;
324 rt = vlib_node_get_runtime_data (vm, mpls_input_node.index);
325 rt->last_label = (u32) ~0;
326 rt->last_inner_fib_index = 0;
327 rt->last_outer_fib_index = 0;
328 rt->mpls_main = &mpls_main;
330 n = vlib_get_node (vm, mpls_ethernet_input_node.index);
332 n->format_buffer = format_mpls_eth_header_with_length;
334 n->unformat_buffer = 0; /* unformat_mpls_ethernet_header; */
336 rt = vlib_node_get_runtime_data (vm, mpls_ethernet_input_node.index);
337 rt->last_label = (u32) ~0;
338 rt->last_inner_fib_index = 0;
339 rt->last_outer_fib_index = 0;
340 rt->mpls_main = &mpls_main;
342 ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS_UNICAST,
343 mpls_ethernet_input_node.index);
346 static clib_error_t * mpls_input_init (vlib_main_t * vm)
348 clib_error_t * error;
350 error = vlib_call_init_function (vm, mpls_init);
352 clib_error_report (error);
354 mpls_setup_nodes (vm);
359 VLIB_INIT_FUNCTION (mpls_input_init);