blob: c06cf917b88966ed166678326669a5d75bb3ecae [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;
27
28 /* Packet data, possibly *after* rewrite. */
29 u8 packet_data[64 - 1*sizeof(u32)];
30} mpls_output_trace_t;
31
32static u8 *
33format_mpls_output_trace (u8 * s, va_list * args)
34{
35 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37 mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
38 vnet_main_t * vnm = vnet_get_main();
39 uword indent = format_get_indent (s);
40
41 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
42 t->adj_index,
Neale Rannsb80c5362016-10-08 13:03:40 +010043 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010044 t->flow_hash);
45 s = format (s, "\n%U%U",
46 format_white_space, indent,
47 format_ip_adjacency_packet_data,
48 vnm, t->adj_index,
49 t->packet_data, sizeof (t->packet_data));
50 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{
59 u32 n_left_from, next_index, * from, * to_next, cpu_index;
60 vlib_node_runtime_t * error_node;
Neale Ranns9ca18c62016-12-10 21:08:09 +000061 u32 n_left_to_next;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010062
63 cpu_index = os_get_cpu_number();
64 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
65 from = vlib_frame_vector_args (from_frame);
66 n_left_from = from_frame->n_vectors;
67 next_index = node->cached_next_index;
68
69 while (n_left_from > 0)
70 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071 vlib_get_next_frame (vm, node, next_index,
72 to_next, n_left_to_next);
73
Neale Ranns9ca18c62016-12-10 21:08:09 +000074 while (n_left_from >= 4 && n_left_to_next >= 2)
75 {
76 ip_adjacency_t * adj0;
77 mpls_unicast_header_t *hdr0;
78 vlib_buffer_t * p0;
79 u32 pi0, rw_len0, adj_index0, next0, error0;
80
81 ip_adjacency_t * adj1;
82 mpls_unicast_header_t *hdr1;
83 vlib_buffer_t * p1;
84 u32 pi1, rw_len1, adj_index1, next1, error1;
85
86 /* Prefetch next iteration. */
87 {
88 vlib_buffer_t * p2, * p3;
89
90 p2 = vlib_get_buffer (vm, from[2]);
91 p3 = vlib_get_buffer (vm, from[3]);
92
93 vlib_prefetch_buffer_header (p2, STORE);
94 vlib_prefetch_buffer_header (p3, STORE);
95
96 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
97 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
98 }
99
100 pi0 = to_next[0] = from[0];
101 pi1 = to_next[1] = from[1];
102
103 from += 2;
104 n_left_from -= 2;
105 to_next += 2;
106 n_left_to_next -= 2;
107
108 p0 = vlib_get_buffer (vm, pi0);
109 p1 = vlib_get_buffer (vm, pi1);
110
111 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
112 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
113
114 /* We should never rewrite a pkt using the MISS adjacency */
115 ASSERT(adj_index0);
116 ASSERT(adj_index1);
117
118 adj0 = adj_get(adj_index0);
119 adj1 = adj_get(adj_index1);
120 hdr0 = vlib_buffer_get_current (p0);
121 hdr1 = vlib_buffer_get_current (p1);
122
123 /* Guess we are only writing on simple Ethernet header. */
124 vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
125 sizeof (ethernet_header_t));
126
127 /* Update packet buffer attributes/set output interface. */
128 rw_len0 = adj0[0].rewrite_header.data_bytes;
129 rw_len1 = adj1[0].rewrite_header.data_bytes;
130
Neale Ranns044183f2017-01-24 01:34:25 -0800131 /* Bump the adj counters for packet and bytes */
132 vlib_increment_combined_counter
133 (&adjacency_counters,
134 cpu_index,
135 adj_index0,
136 1,
137 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
138 vlib_increment_combined_counter
139 (&adjacency_counters,
140 cpu_index,
141 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 {
149 p0->current_data -= rw_len0;
150 p0->current_length += rw_len0;
151
152 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
153 adj0[0].rewrite_header.sw_if_index;
154 next0 = adj0[0].rewrite_header.next_index;
155 error0 = IP4_ERROR_NONE;
156
157 if (is_midchain)
158 {
159 adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
160 }
161 }
162 else
163 {
164 error0 = IP4_ERROR_MTU_EXCEEDED;
165 next0 = MPLS_OUTPUT_NEXT_DROP;
166 }
167 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
168 adj1[0].rewrite_header.max_l3_packet_bytes))
169 {
170 p1->current_data -= rw_len1;
171 p1->current_length += rw_len1;
172
173 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
174 adj1[0].rewrite_header.sw_if_index;
175 next1 = adj1[0].rewrite_header.next_index;
176 error1 = IP4_ERROR_NONE;
177
178 if (is_midchain)
179 {
180 adj1->sub_type.midchain.fixup_func(vm, adj1, p1);
181 }
182 }
183 else
184 {
185 error1 = IP4_ERROR_MTU_EXCEEDED;
186 next1 = MPLS_OUTPUT_NEXT_DROP;
187 }
188
189 p0->error = error_node->errors[error0];
190 p1->error = error_node->errors[error1];
191
192 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
193 {
194 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
195 p0, sizeof (*tr));
196 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
197 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
198 }
199 if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
200 {
201 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
202 p1, sizeof (*tr));
203 tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
204 tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
205 }
206
207 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
208 to_next, n_left_to_next,
209 pi0, pi1, next0, next1);
210 }
211
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100212 while (n_left_from > 0 && n_left_to_next > 0)
213 {
214 ip_adjacency_t * adj0;
215 mpls_unicast_header_t *hdr0;
216 vlib_buffer_t * p0;
217 u32 pi0, rw_len0, adj_index0, next0, error0;
218
219 pi0 = to_next[0] = from[0];
220
221 p0 = vlib_get_buffer (vm, pi0);
222
223 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
224
225 /* We should never rewrite a pkt using the MISS adjacency */
226 ASSERT(adj_index0);
227
228 adj0 = adj_get(adj_index0);
229 hdr0 = vlib_buffer_get_current (p0);
230
231 /* Guess we are only writing on simple Ethernet header. */
232 vnet_rewrite_one_header (adj0[0], hdr0,
233 sizeof (ethernet_header_t));
234
235 /* Update packet buffer attributes/set output interface. */
236 rw_len0 = adj0[0].rewrite_header.data_bytes;
237
Neale Ranns044183f2017-01-24 01:34:25 -0800238 vlib_increment_combined_counter
239 (&adjacency_counters,
240 cpu_index,
241 adj_index0,
242 1,
243 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244
245 /* Check MTU of outgoing interface. */
Neale Ranns9ca18c62016-12-10 21:08:09 +0000246 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
247 adj0[0].rewrite_header.max_l3_packet_bytes))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100248 {
249 p0->current_data -= rw_len0;
250 p0->current_length += rw_len0;
251
252 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
253 adj0[0].rewrite_header.sw_if_index;
254 next0 = adj0[0].rewrite_header.next_index;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000255 error0 = IP4_ERROR_NONE;
John Loaeb06f42016-10-15 17:45:35 -0400256
257 if (is_midchain)
258 {
259 adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
260 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100261 }
262 else
263 {
Neale Ranns9ca18c62016-12-10 21:08:09 +0000264 error0 = IP4_ERROR_MTU_EXCEEDED;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100265 next0 = MPLS_OUTPUT_NEXT_DROP;
266 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000267 p0->error = error_node->errors[error0];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100268
269 from += 1;
270 n_left_from -= 1;
271 to_next += 1;
272 n_left_to_next -= 1;
273
274 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
275 {
276 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
277 p0, sizeof (*tr));
278 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
279 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
280 }
281
282 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
283 to_next, n_left_to_next,
284 pi0, next0);
285 }
286
287 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
288 }
289 vlib_node_increment_counter (vm, mpls_output_node.index,
290 MPLS_ERROR_PKTS_ENCAP,
291 from_frame->n_vectors);
292
293 return from_frame->n_vectors;
294}
295
296static char * mpls_error_strings[] = {
297#define mpls_error(n,s) s,
298#include "error.def"
299#undef mpls_error
300};
301
302static inline uword
303mpls_output (vlib_main_t * vm,
304 vlib_node_runtime_t * node,
305 vlib_frame_t * from_frame)
306{
John Loaeb06f42016-10-15 17:45:35 -0400307 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 0));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100308}
309
310VLIB_REGISTER_NODE (mpls_output_node) = {
311 .function = mpls_output,
312 .name = "mpls-output",
313 /* Takes a vector of packets. */
314 .vector_size = sizeof (u32),
315 .n_errors = MPLS_N_ERROR,
316 .error_strings = mpls_error_strings,
317
318 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
319 .next_nodes = {
320#define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
321 foreach_mpls_output_next
322#undef _
323 },
324
325 .format_trace = format_mpls_output_trace,
326};
327
328VLIB_NODE_FUNCTION_MULTIARCH (mpls_output_node, mpls_output)
329
330static inline uword
331mpls_midchain (vlib_main_t * vm,
332 vlib_node_runtime_t * node,
333 vlib_frame_t * from_frame)
334{
John Loaeb06f42016-10-15 17:45:35 -0400335 return (mpls_output_inline(vm, node, from_frame, /* is_midchain */ 1));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100336}
337
338VLIB_REGISTER_NODE (mpls_midchain_node) = {
John Loaeb06f42016-10-15 17:45:35 -0400339 .function = mpls_midchain,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100340 .name = "mpls-midchain",
341 .vector_size = sizeof (u32),
342
343 .format_trace = format_mpls_output_trace,
344
345 .sibling_of = "mpls-output",
346};
347
348VLIB_NODE_FUNCTION_MULTIARCH (mpls_midchain_node, mpls_midchain)
349
350/**
351 * @brief Next index values from the MPLS incomplete adj node
352 */
353#define foreach_mpls_adj_incomplete_next \
354_(DROP, "error-drop") \
355_(IP4, "ip4-arp") \
356_(IP6, "ip6-discover-neighbor")
357
358typedef enum {
359#define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
360 foreach_mpls_adj_incomplete_next
361#undef _
362 MPLS_ADJ_INCOMPLETE_N_NEXT,
363} mpls_adj_incomplete_next_t;
364
365/**
366 * @brief A struct to hold tracing information for the MPLS label imposition
367 * node.
368 */
369typedef struct mpls_adj_incomplete_trace_t_
370{
371 u32 next;
372} mpls_adj_incomplete_trace_t;
373
374
375/**
376 * @brief Graph node for incomplete MPLS adjacency.
377 * This node will push traffic to either the v4-arp or v6-nd node
378 * based on the next-hop proto of the adj.
379 * We pay a cost for this 'routing' node, but an incomplete adj is the
380 * exception case.
381 */
382static inline uword
383mpls_adj_incomplete (vlib_main_t * vm,
384 vlib_node_runtime_t * node,
385 vlib_frame_t * from_frame)
386{
387 u32 n_left_from, next_index, * from, * to_next;
388
389 from = vlib_frame_vector_args (from_frame);
390 n_left_from = from_frame->n_vectors;
391 next_index = node->cached_next_index;
392
393 while (n_left_from > 0)
394 {
395 u32 n_left_to_next;
396
397 vlib_get_next_frame (vm, node, next_index,
398 to_next, n_left_to_next);
399
400 while (n_left_from > 0 && n_left_to_next > 0)
401 {
402 u32 pi0, next0, adj_index0;
403 ip_adjacency_t * adj0;
404 vlib_buffer_t * p0;
405
406 pi0 = to_next[0] = from[0];
407 p0 = vlib_get_buffer (vm, pi0);
408 from += 1;
409 n_left_from -= 1;
410 to_next += 1;
411 n_left_to_next -= 1;
412
413 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
414 ASSERT(adj_index0);
415
416 adj0 = adj_get(adj_index0);
417
418 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
419 {
420 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
421 }
422 else
423 {
424 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
425 }
426
427 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
428 {
429 mpls_adj_incomplete_trace_t *tr =
430 vlib_add_trace (vm, node, p0, sizeof (*tr));
431 tr->next = next0;
432 }
433
434 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
435 to_next, n_left_to_next,
436 pi0, next0);
437 }
438
439 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
440 }
441
442 return from_frame->n_vectors;
443}
444
445static u8 *
446format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
447{
448 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
449 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
450 mpls_adj_incomplete_trace_t * t;
451 uword indent;
452
453 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
454 indent = format_get_indent (s);
455
456 s = format (s, "%Unext:%d",
457 format_white_space, indent,
458 t->next);
459 return (s);
460}
461
462VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
463 .function = mpls_adj_incomplete,
464 .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
479VLIB_NODE_FUNCTION_MULTIARCH (mpls_adj_incomplete_node,
480 mpls_adj_incomplete)