blob: 1810091252ee96b9b73c14088656e21bfb9bcd5e [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
Neale Ranns1357f3b2016-10-16 12:01:42 -07002 * node.c: MPLS input
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vlib/vlib.h>
19#include <vnet/pg/pg.h>
20#include <vnet/mpls/mpls.h>
Damjan Marion22311502016-10-28 20:30:15 +020021#include <vnet/feature/feature.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022
23typedef struct {
24 u32 next_index;
25 u32 label_host_byte_order;
26} mpls_input_trace_t;
27
28static u8 *
29format_mpls_input_trace (u8 * s, va_list * args)
30{
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_input_trace_t * t = va_arg (*args, mpls_input_trace_t *);
34 char * next_name;
35
36 next_name = "BUG!";
37
38#define _(a,b) if (t->next_index == MPLS_INPUT_NEXT_##a) next_name = b;
39 foreach_mpls_input_next;
40#undef _
41
42 s = format (s, "MPLS: next %s[%d] label %d ttl %d",
43 next_name, t->next_index,
44 vnet_mpls_uc_get_label(t->label_host_byte_order),
45 vnet_mpls_uc_get_ttl(t->label_host_byte_order));
46
47 return s;
48}
49
50vlib_node_registration_t mpls_input_node;
51
52typedef struct {
53 u32 last_label;
54 u32 last_inner_fib_index;
55 u32 last_outer_fib_index;
56 mpls_main_t * mpls_main;
57} mpls_input_runtime_t;
58
59static inline uword
60mpls_input_inline (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
62 vlib_frame_t * from_frame)
63{
64 u32 n_left_from, next_index, * from, * to_next;
65 mpls_input_runtime_t * rt;
66 mpls_main_t * mm;
67 u32 cpu_index = os_get_cpu_number();
68 vlib_simple_counter_main_t * cm;
69 vnet_main_t * vnm = vnet_get_main();
70
71 from = vlib_frame_vector_args (from_frame);
72 n_left_from = from_frame->n_vectors;
73 rt = vlib_node_get_runtime_data (vm, mpls_input_node.index);
74 mm = rt->mpls_main;
75 /*
76 * Force an initial lookup every time, in case the control-plane
77 * changed the label->FIB mapping.
78 */
79 rt->last_label = ~0;
80
81 next_index = node->cached_next_index;
82
83 cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
84 VNET_INTERFACE_COUNTER_MPLS);
85
86 while (n_left_from > 0)
87 {
88 u32 n_left_to_next;
89
90 vlib_get_next_frame (vm, node, next_index,
91 to_next, n_left_to_next);
92
Neale Ranns9ca18c62016-12-10 21:08:09 +000093 while (n_left_from >= 4 && n_left_to_next >= 2)
94 {
95 u32 label0, bi0, next0, sw_if_index0;
96 u32 label1, bi1, next1, sw_if_index1;
97 mpls_unicast_header_t *h0, *h1;
98 vlib_buffer_t *b0, *b1;
99
100 /* Prefetch next iteration. */
101 {
102 vlib_buffer_t * p2, * p3;
103
104 p2 = vlib_get_buffer (vm, from[2]);
105 p3 = vlib_get_buffer (vm, from[3]);
106
107 vlib_prefetch_buffer_header (p2, STORE);
108 vlib_prefetch_buffer_header (p3, STORE);
109
110 CLIB_PREFETCH (p2->data, sizeof (h0[0]), STORE);
111 CLIB_PREFETCH (p3->data, sizeof (h1[0]), STORE);
112 }
113
114
115 bi0 = to_next[0] = from[0];
116 bi1 = to_next[1] = from[1];
117
118 from += 2;
119 to_next += 2;
120 n_left_from -= 2;
121 n_left_to_next -= 2;
122
123 b0 = vlib_get_buffer (vm, bi0);
124 b1 = vlib_get_buffer (vm, bi1);
125
126 h0 = vlib_buffer_get_current (b0);
127 h1 = vlib_buffer_get_current (b1);
128
129 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
130 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
131
132 label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
133 label1 = clib_net_to_host_u32 (h1->label_exp_s_ttl);
134
135 /* TTL expired? */
136 if (PREDICT_FALSE(vnet_mpls_uc_get_ttl (label0) == 0))
137 {
138 next0 = MPLS_INPUT_NEXT_DROP;
139 b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
140 }
141 else
142 {
143 next0 = MPLS_INPUT_NEXT_LOOKUP;
144 vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index0, &next0, b0);
145 vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
146 }
147
148 if (PREDICT_FALSE(vnet_mpls_uc_get_ttl (label1) == 0))
149 {
150 next1 = MPLS_INPUT_NEXT_DROP;
151 b1->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
152 }
153 else
154 {
155 next1 = MPLS_INPUT_NEXT_LOOKUP;
156 vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index1, &next1, b1);
157 vlib_increment_simple_counter (cm, cpu_index, sw_if_index1, 1);
158 }
159
160 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
161 {
162 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
163 b0, sizeof (*tr));
164 tr->next_index = next0;
165 tr->label_host_byte_order = label0;
166 }
167 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
168 {
169 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
170 b1, sizeof (*tr));
171 tr->next_index = next1;
172 tr->label_host_byte_order = label1;
173 }
174
175 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
176 to_next, n_left_to_next,
177 bi0, bi1, next0, next1);
178 }
179
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100180 while (n_left_from > 0 && n_left_to_next > 0)
181 {
182 u32 bi0;
183 vlib_buffer_t * b0;
184 mpls_unicast_header_t * h0;
185 u32 label0;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100186 u32 next0 = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100187 u32 sw_if_index0;
188
189 bi0 = from[0];
190 to_next[0] = bi0;
191 from += 1;
192 to_next += 1;
193 n_left_from -= 1;
194 n_left_to_next -= 1;
195
196 b0 = vlib_get_buffer (vm, bi0);
197 h0 = vlib_buffer_get_current (b0);
198 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
199
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100200 label0 = clib_net_to_host_u32 (h0->label_exp_s_ttl);
201 /* TTL expired? */
202 if (PREDICT_FALSE(vnet_mpls_uc_get_ttl (label0) == 0))
203 {
204 next0 = MPLS_INPUT_NEXT_DROP;
205 b0->error = node->errors[MPLS_ERROR_TTL_EXPIRED];
206 }
207 else
208 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100209 vnet_feature_arc_start(mm->input_feature_arc_index, sw_if_index0, &next0, b0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100210 vlib_increment_simple_counter (cm, cpu_index, sw_if_index0, 1);
211 }
212
213 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
214 {
215 mpls_input_trace_t *tr = vlib_add_trace (vm, node,
216 b0, sizeof (*tr));
217 tr->next_index = next0;
218 tr->label_host_byte_order = label0;
219 }
220
221 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
222 to_next, n_left_to_next,
223 bi0, next0);
224 }
225
226 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
227 }
228 vlib_node_increment_counter (vm, mpls_input_node.index,
229 MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
230 return from_frame->n_vectors;
231}
232
233static uword
234mpls_input (vlib_main_t * vm,
235 vlib_node_runtime_t * node,
236 vlib_frame_t * from_frame)
237{
238 return mpls_input_inline (vm, node, from_frame);
239}
240
241static char * mpls_error_strings[] = {
242#define mpls_error(n,s) s,
243#include "error.def"
244#undef mpls_error
245};
246
247VLIB_REGISTER_NODE (mpls_input_node) = {
248 .function = mpls_input,
249 .name = "mpls-input",
250 /* Takes a vector of packets. */
251 .vector_size = sizeof (u32),
252
253 .runtime_data_bytes = sizeof(mpls_input_runtime_t),
254
255 .n_errors = MPLS_N_ERROR,
256 .error_strings = mpls_error_strings,
257
258 .n_next_nodes = MPLS_INPUT_N_NEXT,
259 .next_nodes = {
260#define _(s,n) [MPLS_INPUT_NEXT_##s] = n,
261 foreach_mpls_input_next
262#undef _
263 },
264
265 .format_buffer = format_mpls_unicast_header_net_byte_order,
266 .format_trace = format_mpls_input_trace,
267};
268
269VLIB_NODE_FUNCTION_MULTIARCH (mpls_input_node, mpls_input)
270
271static void
272mpls_setup_nodes (vlib_main_t * vm)
273{
274 mpls_input_runtime_t * rt;
275 pg_node_t * pn;
276
277 pn = pg_get_node (mpls_input_node.index);
278 pn->unformat_edit = unformat_pg_mpls_header;
279
280 rt = vlib_node_get_runtime_data (vm, mpls_input_node.index);
281 rt->last_label = (u32) ~0;
282 rt->last_inner_fib_index = 0;
283 rt->last_outer_fib_index = 0;
284 rt->mpls_main = &mpls_main;
285
286 ethernet_register_input_type (vm, ETHERNET_TYPE_MPLS_UNICAST,
287 mpls_input_node.index);
288}
289
290static clib_error_t * mpls_input_init (vlib_main_t * vm)
291{
292 clib_error_t * error;
293
294 error = vlib_call_init_function (vm, mpls_init);
295 if (error)
296 clib_error_report (error);
297
298 mpls_setup_nodes (vm);
299
Damjan Marion8b3191e2016-11-09 19:54:20 +0100300 return 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100301}
302
303VLIB_INIT_FUNCTION (mpls_input_init);