blob: 14018c1a38e6150ef200cc636c8d24b25596348f [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
2 * mpls_output.c: MPLS Adj rewrite
3 *
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>
Neale Ranns1357f3b2016-10-16 12:01:42 -070020#include <vnet/ip/ip.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/mpls/mpls.h>
22
23typedef struct {
24 /* Adjacency taken. */
25 u32 adj_index;
26 u32 flow_hash;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010027} mpls_output_trace_t;
28
Neale Ranns696e88d2017-03-16 07:34:55 -040029#define foreach_mpls_output_next \
30_(DROP, "error-drop")
31
32typedef enum {
33#define _(s,n) MPLS_OUTPUT_NEXT_##s,
34 foreach_mpls_output_next
35#undef _
36 MPLS_OUTPUT_N_NEXT,
37} mpls_output_next_t;
38
Neale Ranns0bfe5d82016-08-25 15:29:12 +010039static u8 *
40format_mpls_output_trace (u8 * s, va_list * args)
41{
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_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010045
46 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
47 t->adj_index,
Neale Rannsb80c5362016-10-08 13:03:40 +010048 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010049 t->flow_hash);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010050 return s;
51}
52
53static inline uword
54mpls_output_inline (vlib_main_t * vm,
55 vlib_node_runtime_t * node,
John Loaeb06f42016-10-15 17:45:35 -040056 vlib_frame_t * from_frame,
57 int is_midchain)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010058{
Damjan Marion586afd72017-04-05 19:18:20 +020059 u32 n_left_from, next_index, * from, * to_next, thread_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010060 vlib_node_runtime_t * error_node;
Neale Ranns9ca18c62016-12-10 21:08:09 +000061 u32 n_left_to_next;
Neale Rannsb069a692017-03-15 12:34:25 -040062 mpls_main_t *mm;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010063
Damjan Marion586afd72017-04-05 19:18:20 +020064 thread_index = vlib_get_thread_index();
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
66 from = vlib_frame_vector_args (from_frame);
67 n_left_from = from_frame->n_vectors;
68 next_index = node->cached_next_index;
Neale Rannsb069a692017-03-15 12:34:25 -040069 mm = &mpls_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070
71 while (n_left_from > 0)
72 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073 vlib_get_next_frame (vm, node, next_index,
74 to_next, n_left_to_next);
75
Neale Ranns9ca18c62016-12-10 21:08:09 +000076 while (n_left_from >= 4 && n_left_to_next >= 2)
77 {
78 ip_adjacency_t * adj0;
79 mpls_unicast_header_t *hdr0;
80 vlib_buffer_t * p0;
81 u32 pi0, rw_len0, adj_index0, next0, error0;
82
83 ip_adjacency_t * adj1;
84 mpls_unicast_header_t *hdr1;
85 vlib_buffer_t * p1;
86 u32 pi1, rw_len1, adj_index1, next1, error1;
87
88 /* Prefetch next iteration. */
89 {
90 vlib_buffer_t * p2, * p3;
91
92 p2 = vlib_get_buffer (vm, from[2]);
93 p3 = vlib_get_buffer (vm, from[3]);
94
95 vlib_prefetch_buffer_header (p2, STORE);
96 vlib_prefetch_buffer_header (p3, STORE);
97
98 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
99 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
100 }
101
102 pi0 = to_next[0] = from[0];
103 pi1 = to_next[1] = from[1];
104
105 from += 2;
106 n_left_from -= 2;
107 to_next += 2;
108 n_left_to_next -= 2;
109
110 p0 = vlib_get_buffer (vm, pi0);
111 p1 = vlib_get_buffer (vm, pi1);
112
113 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
114 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
115
Neale Ranns9ca18c62016-12-10 21:08:09 +0000116 adj0 = adj_get(adj_index0);
117 adj1 = adj_get(adj_index1);
118 hdr0 = vlib_buffer_get_current (p0);
119 hdr1 = vlib_buffer_get_current (p1);
120
121 /* Guess we are only writing on simple Ethernet header. */
122 vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
123 sizeof (ethernet_header_t));
124
125 /* Update packet buffer attributes/set output interface. */
126 rw_len0 = adj0[0].rewrite_header.data_bytes;
127 rw_len1 = adj1[0].rewrite_header.data_bytes;
Neale Ranns039cbfe2018-02-27 03:45:38 -0800128 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
129 vnet_buffer (p1)->mpls.save_rewrite_length = rw_len1;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000130
Neale Ranns044183f2017-01-24 01:34:25 -0800131 /* Bump the adj counters for packet and bytes */
132 vlib_increment_combined_counter
133 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200134 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800135 adj_index0,
136 1,
137 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
138 vlib_increment_combined_counter
139 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200140 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800141 adj_index1,
142 1,
143 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000144
145 /* Check MTU of outgoing interface. */
146 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
147 adj0[0].rewrite_header.max_l3_packet_bytes))
148 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400149 vlib_buffer_advance(p0, -rw_len0);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000150
151 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
152 adj0[0].rewrite_header.sw_if_index;
153 next0 = adj0[0].rewrite_header.next_index;
154 error0 = IP4_ERROR_NONE;
155
Neale Rannsb069a692017-03-15 12:34:25 -0400156 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
157 vnet_feature_arc_start (mm->output_feature_arc_index,
158 adj0[0].rewrite_header.sw_if_index,
159 &next0, p0);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000160 }
161 else
162 {
163 error0 = IP4_ERROR_MTU_EXCEEDED;
164 next0 = MPLS_OUTPUT_NEXT_DROP;
165 }
166 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
167 adj1[0].rewrite_header.max_l3_packet_bytes))
168 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400169 vlib_buffer_advance(p1, -rw_len1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000170
171 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
172 adj1[0].rewrite_header.sw_if_index;
173 next1 = adj1[0].rewrite_header.next_index;
174 error1 = IP4_ERROR_NONE;
175
Neale Rannsb069a692017-03-15 12:34:25 -0400176 if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
177 vnet_feature_arc_start (mm->output_feature_arc_index,
178 adj1[0].rewrite_header.sw_if_index,
179 &next1, p1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000180 }
181 else
182 {
183 error1 = IP4_ERROR_MTU_EXCEEDED;
184 next1 = MPLS_OUTPUT_NEXT_DROP;
185 }
Neale Rannsb069a692017-03-15 12:34:25 -0400186 if (is_midchain)
187 {
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800188 adj0->sub_type.midchain.fixup_func
189 (vm, adj0, p0,
190 adj0->sub_type.midchain.fixup_data);
191 adj1->sub_type.midchain.fixup_func
192 (vm, adj1, p1,
193 adj1->sub_type.midchain.fixup_data);
Neale Rannsb069a692017-03-15 12:34:25 -0400194 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000195
196 p0->error = error_node->errors[error0];
197 p1->error = error_node->errors[error1];
198
199 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
200 {
201 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
202 p0, sizeof (*tr));
203 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
204 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
205 }
206 if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
207 {
208 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
209 p1, sizeof (*tr));
210 tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
211 tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
212 }
213
214 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
215 to_next, n_left_to_next,
216 pi0, pi1, next0, next1);
217 }
218
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100219 while (n_left_from > 0 && n_left_to_next > 0)
220 {
221 ip_adjacency_t * adj0;
222 mpls_unicast_header_t *hdr0;
223 vlib_buffer_t * p0;
224 u32 pi0, rw_len0, adj_index0, next0, error0;
225
226 pi0 = to_next[0] = from[0];
227
228 p0 = vlib_get_buffer (vm, pi0);
229
230 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
231
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100232 adj0 = adj_get(adj_index0);
233 hdr0 = vlib_buffer_get_current (p0);
234
235 /* Guess we are only writing on simple Ethernet header. */
236 vnet_rewrite_one_header (adj0[0], hdr0,
237 sizeof (ethernet_header_t));
238
239 /* Update packet buffer attributes/set output interface. */
240 rw_len0 = adj0[0].rewrite_header.data_bytes;
Neale Ranns039cbfe2018-02-27 03:45:38 -0800241 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
242
Neale Ranns044183f2017-01-24 01:34:25 -0800243 vlib_increment_combined_counter
244 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200245 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800246 adj_index0,
247 1,
248 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
Zhiyong Yang734d4302019-05-29 22:38:19 -0400249
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100250 /* Check MTU of outgoing interface. */
Neale Ranns9ca18c62016-12-10 21:08:09 +0000251 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
252 adj0[0].rewrite_header.max_l3_packet_bytes))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100253 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400254 vlib_buffer_advance(p0, -rw_len0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100255
256 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
257 adj0[0].rewrite_header.sw_if_index;
258 next0 = adj0[0].rewrite_header.next_index;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000259 error0 = IP4_ERROR_NONE;
John Loaeb06f42016-10-15 17:45:35 -0400260
Neale Rannsb069a692017-03-15 12:34:25 -0400261 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
262 vnet_feature_arc_start (mm->output_feature_arc_index,
263 adj0[0].rewrite_header.sw_if_index,
264 &next0, p0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100265 }
266 else
267 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000268 error0 = IP4_ERROR_MTU_EXCEEDED;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100269 next0 = MPLS_OUTPUT_NEXT_DROP;
270 }
Neale Rannsb069a692017-03-15 12:34:25 -0400271 if (is_midchain)
272 {
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800273 adj0->sub_type.midchain.fixup_func
274 (vm, adj0, p0,
275 adj0->sub_type.midchain.fixup_data);
Neale Rannsb069a692017-03-15 12:34:25 -0400276 }
277
278 p0->error = error_node->errors[error0];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100279
280 from += 1;
281 n_left_from -= 1;
282 to_next += 1;
283 n_left_to_next -= 1;
284
285 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
286 {
287 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
288 p0, sizeof (*tr));
289 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
290 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
291 }
292
293 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
294 to_next, n_left_to_next,
295 pi0, next0);
296 }
297
298 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
299 }
300 vlib_node_increment_counter (vm, mpls_output_node.index,
301 MPLS_ERROR_PKTS_ENCAP,
302 from_frame->n_vectors);
303
304 return from_frame->n_vectors;
305}
306
307static char * mpls_error_strings[] = {
308#define mpls_error(n,s) s,
309#include "error.def"
310#undef mpls_error
311};
312
Filip Tehlar17fcd982019-03-05 04:32:11 -0800313VLIB_NODE_FN (mpls_output_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100314 vlib_node_runtime_t * node,
315 vlib_frame_t * from_frame)
316{
John Loaeb06f42016-10-15 17:45:35 -0400317 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100318}
319
320VLIB_REGISTER_NODE (mpls_output_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100321 .name = "mpls-output",
322 /* Takes a vector of packets. */
323 .vector_size = sizeof (u32),
324 .n_errors = MPLS_N_ERROR,
325 .error_strings = mpls_error_strings,
326
327 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
328 .next_nodes = {
329#define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
330 foreach_mpls_output_next
331#undef _
332 },
333
334 .format_trace = format_mpls_output_trace,
335};
336
Filip Tehlar17fcd982019-03-05 04:32:11 -0800337VLIB_NODE_FN (mpls_midchain_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100338 vlib_node_runtime_t * node,
339 vlib_frame_t * from_frame)
340{
John Loaeb06f42016-10-15 17:45:35 -0400341 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100342}
343
344VLIB_REGISTER_NODE (mpls_midchain_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100345 .name = "mpls-midchain",
346 .vector_size = sizeof (u32),
347
348 .format_trace = format_mpls_output_trace,
349
350 .sibling_of = "mpls-output",
351};
352
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100353/**
354 * @brief Next index values from the MPLS incomplete adj node
355 */
356#define foreach_mpls_adj_incomplete_next \
357_(DROP, "error-drop") \
358_(IP4, "ip4-arp") \
359_(IP6, "ip6-discover-neighbor")
360
361typedef enum {
362#define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
363 foreach_mpls_adj_incomplete_next
364#undef _
365 MPLS_ADJ_INCOMPLETE_N_NEXT,
366} mpls_adj_incomplete_next_t;
367
368/**
369 * @brief A struct to hold tracing information for the MPLS label imposition
370 * node.
371 */
372typedef struct mpls_adj_incomplete_trace_t_
373{
374 u32 next;
375} mpls_adj_incomplete_trace_t;
376
377
378/**
379 * @brief Graph node for incomplete MPLS adjacency.
380 * This node will push traffic to either the v4-arp or v6-nd node
381 * based on the next-hop proto of the adj.
382 * We pay a cost for this 'routing' node, but an incomplete adj is the
383 * exception case.
384 */
Filip Tehlar17fcd982019-03-05 04:32:11 -0800385VLIB_NODE_FN (mpls_adj_incomplete_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100386 vlib_node_runtime_t * node,
387 vlib_frame_t * from_frame)
388{
389 u32 n_left_from, next_index, * from, * to_next;
390
391 from = vlib_frame_vector_args (from_frame);
392 n_left_from = from_frame->n_vectors;
393 next_index = node->cached_next_index;
394
395 while (n_left_from > 0)
396 {
397 u32 n_left_to_next;
398
399 vlib_get_next_frame (vm, node, next_index,
400 to_next, n_left_to_next);
401
402 while (n_left_from > 0 && n_left_to_next > 0)
403 {
404 u32 pi0, next0, adj_index0;
405 ip_adjacency_t * adj0;
406 vlib_buffer_t * p0;
407
408 pi0 = to_next[0] = from[0];
409 p0 = vlib_get_buffer (vm, pi0);
410 from += 1;
411 n_left_from -= 1;
412 to_next += 1;
413 n_left_to_next -= 1;
414
415 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100416
417 adj0 = adj_get(adj_index0);
418
419 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
420 {
421 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
422 }
423 else
424 {
425 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
426 }
427
428 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
429 {
430 mpls_adj_incomplete_trace_t *tr =
431 vlib_add_trace (vm, node, p0, sizeof (*tr));
432 tr->next = next0;
433 }
434
435 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
436 to_next, n_left_to_next,
437 pi0, next0);
438 }
439
440 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
441 }
442
443 return from_frame->n_vectors;
444}
445
446static u8 *
447format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
448{
449 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
450 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
451 mpls_adj_incomplete_trace_t * t;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200452 u32 indent;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100453
454 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
455 indent = format_get_indent (s);
456
457 s = format (s, "%Unext:%d",
458 format_white_space, indent,
459 t->next);
460 return (s);
461}
462
463VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100464 .name = "mpls-adj-incomplete",
465 .format_trace = format_mpls_adj_incomplete_trace,
466 /* Takes a vector of packets. */
467 .vector_size = sizeof (u32),
468 .n_errors = MPLS_N_ERROR,
469 .error_strings = mpls_error_strings,
470
471 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
472 .next_nodes = {
473#define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
474 foreach_mpls_adj_incomplete_next
475#undef _
476 },
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100477};
478