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/mpls.h>
21 #include <vnet/feature/feature.h>
25 u32 label_net_byte_order;
28 #define foreach_mpls_input_next \
29 _(DROP, "error-drop") \
30 _(LOOKUP, "mpls-lookup")
33 #define _(s,n) MPLS_INPUT_NEXT_##s,
34 foreach_mpls_input_next
40 format_mpls_input_trace (u8 * s, va_list * args)
42 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
43 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
44 mpls_input_trace_t * t = va_arg (*args, mpls_input_trace_t *);
48 label = clib_net_to_host_u32(t->label_net_byte_order);
50 #define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b;
51 foreach_mpls_input_next;
54 s = format (s, "MPLS: next %s[%d] label %d ttl %d exp %d",
55 next_name, t->next_index,
56 vnet_mpls_uc_get_label(label),
57 vnet_mpls_uc_get_ttl(label),
58 vnet_mpls_uc_get_exp(label));
63 vlib_node_registration_t mpls_input_node;
67 u32 last_inner_fib_index;
68 u32 last_outer_fib_index;
69 mpls_main_t * mpls_main;
70 } mpls_input_runtime_t;
73 mpls_input_inline (vlib_main_t * vm,
74 vlib_node_runtime_t * node,
75 vlib_frame_t * from_frame)
77 u32 n_left_from, next_index, * from, * to_next;
78 mpls_main_t * mm = &mpls_main;
79 u32 thread_index = vlib_get_thread_index();
80 vlib_simple_counter_main_t * cm;
81 vnet_main_t * vnm = vnet_get_main();
83 from = vlib_frame_vector_args (from_frame);
84 n_left_from = from_frame->n_vectors;
86 next_index = node->cached_next_index;
88 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
89 VNET_INTERFACE_COUNTER_MPLS);
91 while (n_left_from > 0)
95 vlib_get_next_frame (vm, node, next_index,
96 to_next, n_left_to_next);
98 while (n_left_from >= 4 && n_left_to_next >= 2)
100 u32 bi0, next0, sw_if_index0;
101 u32 bi1, next1, sw_if_index1;
102 vlib_buffer_t *b0, *b1;
105 /* Prefetch next iteration. */
107 vlib_buffer_t * p2, * p3;
109 p2 = vlib_get_buffer (vm, from[2]);
110 p3 = vlib_get_buffer (vm, from[3]);
112 vlib_prefetch_buffer_header (p2, STORE);
113 vlib_prefetch_buffer_header (p3, STORE);
115 CLIB_PREFETCH (p2->data, sizeof (h0[0]), STORE);
116 CLIB_PREFETCH (p3->data, sizeof (h1[0]), STORE);
119 bi0 = to_next[0] = from[0];
120 bi1 = to_next[1] = from[1];
127 b0 = vlib_get_buffer (vm, bi0);
128 b1 = vlib_get_buffer (vm, bi1);
130 h0 = vlib_buffer_get_current (b0);
131 h1 = vlib_buffer_get_current (b1);
133 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
134 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
137 if (PREDICT_FALSE(h0[3] == 0))
139 next0 = MPLS_INPUT_NEXT_DROP;
140 b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
144 next0 = MPLS_INPUT_NEXT_LOOKUP;
145 vnet_feature_arc_start(mm->input_feature_arc_index,
146 sw_if_index0, &next0, b0);
147 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
150 if (PREDICT_FALSE(h1[3] == 0))
152 next1 = MPLS_INPUT_NEXT_DROP;
153 b1->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
157 next1 = MPLS_INPUT_NEXT_LOOKUP;
158 vnet_feature_arc_start(mm->input_feature_arc_index,
159 sw_if_index1, &next1, b1);
160 vlib_increment_simple_counter (cm, thread_index, sw_if_index1, 1);
163 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
165 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
167 tr->next_index = next0;
168 tr->label_net_byte_order = *((u32*)h0);
170 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
172 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
174 tr->next_index = next1;
175 tr->label_net_byte_order = *((u32*)h1);
178 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
179 to_next, n_left_to_next,
184 while (n_left_from > 0 && n_left_to_next > 0)
186 u32 sw_if_index0, next0, bi0;
197 b0 = vlib_get_buffer (vm, bi0);
198 h0 = vlib_buffer_get_current (b0);
199 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
202 if (PREDICT_FALSE(h0[3] == 0))
204 next0 = MPLS_INPUT_NEXT_DROP;
205 b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
209 next0 = MPLS_INPUT_NEXT_LOOKUP;
210 vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index0, &next0, b0);
211 vlib_increment_simple_counter (cm, thread_index, sw_if_index0, 1);
214 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
216 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
218 tr->next_index = next0;
219 tr->label_net_byte_order = *(u32*)h0;
222 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
223 to_next, n_left_to_next,
227 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
229 vlib_node_increment_counter (vm, mpls_input_node.index,
230 MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
231 return from_frame->n_vectors;
235 mpls_input (vlib_main_t * vm,
236 vlib_node_runtime_t * node,
237 vlib_frame_t * from_frame)
239 return mpls_input_inline (vm, node, from_frame);
242 static char * mpls_error_strings[] = {
243 #define mpls_error(n,s) s,
248 VLIB_REGISTER_NODE (mpls_input_node) = {
249 .function = mpls_input,
250 .name = "mpls-input",
251 /* Takes a vector of packets. */
252 .vector_size = sizeof (u32),
254 .runtime_data_bytes = sizeof(mpls_input_runtime_t),
256 .n_errors = MPLS_N_ERROR,
257 .error_strings = mpls_error_strings,
259 .n_next_nodes = MPLS_INPUT_N_NEXT,
261 #define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
262 foreach_mpls_input_next
266 .format_buffer = format_mpls_unicast_header_net_byte_order,
267 .format_trace = format_mpls_input_trace,
270 VLIB_NODE_FUNCTION_MULTIARCH (mpls_input_node, mpls_input)
273 mpls_setup_nodes (vlib_main_t * vm)
277 pn = pg_get_node (mpls_input_node.index);
278 pn->unformat_edit = unformat_pg_mpls_header;
280 ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS,
281 mpls_input_node.index);
284 static clib_error_t * mpls_input_init (vlib_main_t * vm)
286 clib_error_t * error;
288 error = vlib_call_init_function (vm, mpls_init);
290 clib_error_report (error);
292 mpls_setup_nodes (vm);
297 VLIB_INIT_FUNCTION (mpls_input_init);