blob: 4783a7ef3497378d6ab4cbf8f28e4f70fd6e4b3f [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>
Neale Ranns1357f3b2016-10-16 12:01:42 -070019#include <vnet/ip/ip.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010020#include <vnet/mpls/mpls.h>
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +053021#include <vnet/ip/ip_frag.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022
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
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +053029typedef enum {
30 MPLS_OUTPUT_MODE,
31 MPLS_OUTPUT_MIDCHAIN_MODE
32}mpls_output_mode_t;
33
Neale Ranns696e88d2017-03-16 07:34:55 -040034#define foreach_mpls_output_next \
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +053035_(DROP, "error-drop") \
Ole Troaneb284a12019-10-09 13:33:19 +020036_(FRAG, "mpls-frag")
Neale Ranns696e88d2017-03-16 07:34:55 -040037
38typedef enum {
39#define _(s,n) MPLS_OUTPUT_NEXT_##s,
40 foreach_mpls_output_next
41#undef _
42 MPLS_OUTPUT_N_NEXT,
43} mpls_output_next_t;
44
Neale Ranns0bfe5d82016-08-25 15:29:12 +010045static u8 *
46format_mpls_output_trace (u8 * s, va_list * args)
47{
48 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
49 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
50 mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010051
52 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
53 t->adj_index,
Neale Rannsb80c5362016-10-08 13:03:40 +010054 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010055 t->flow_hash);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010056 return s;
57}
58
59static inline uword
60mpls_output_inline (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
John Loaeb06f42016-10-15 17:45:35 -040062 vlib_frame_t * from_frame,
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +053063 mpls_output_mode_t mode)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010064{
Damjan Marion586afd72017-04-05 19:18:20 +020065 u32 n_left_from, next_index, * from, * to_next, thread_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010066 vlib_node_runtime_t * error_node;
Neale Ranns9ca18c62016-12-10 21:08:09 +000067 u32 n_left_to_next;
Neale Rannsb069a692017-03-15 12:34:25 -040068 mpls_main_t *mm;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010069
Damjan Marion586afd72017-04-05 19:18:20 +020070 thread_index = vlib_get_thread_index();
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
72 from = vlib_frame_vector_args (from_frame);
73 n_left_from = from_frame->n_vectors;
74 next_index = node->cached_next_index;
Neale Rannsb069a692017-03-15 12:34:25 -040075 mm = &mpls_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010076
77 while (n_left_from > 0)
78 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010079 vlib_get_next_frame (vm, node, next_index,
80 to_next, n_left_to_next);
81
Neale Ranns9ca18c62016-12-10 21:08:09 +000082 while (n_left_from >= 4 && n_left_to_next >= 2)
83 {
84 ip_adjacency_t * adj0;
85 mpls_unicast_header_t *hdr0;
86 vlib_buffer_t * p0;
Benoît Gannec257e072019-06-17 14:42:47 +020087 u32 pi0, adj_index0, next0, error0;
88 word rw_len0;
Neale Ranns9ca18c62016-12-10 21:08:09 +000089
90 ip_adjacency_t * adj1;
91 mpls_unicast_header_t *hdr1;
92 vlib_buffer_t * p1;
Benoît Gannec257e072019-06-17 14:42:47 +020093 u32 pi1, adj_index1, next1, error1;
94 word rw_len1;
Neale Ranns9ca18c62016-12-10 21:08:09 +000095
96 /* Prefetch next iteration. */
97 {
98 vlib_buffer_t * p2, * p3;
99
100 p2 = vlib_get_buffer (vm, from[2]);
101 p3 = vlib_get_buffer (vm, from[3]);
102
103 vlib_prefetch_buffer_header (p2, STORE);
104 vlib_prefetch_buffer_header (p3, STORE);
105
106 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
107 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
108 }
109
110 pi0 = to_next[0] = from[0];
111 pi1 = to_next[1] = from[1];
112
113 from += 2;
114 n_left_from -= 2;
115 to_next += 2;
116 n_left_to_next -= 2;
117
118 p0 = vlib_get_buffer (vm, pi0);
119 p1 = vlib_get_buffer (vm, pi1);
120
121 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
122 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
123
Neale Ranns9ca18c62016-12-10 21:08:09 +0000124 adj0 = adj_get(adj_index0);
125 adj1 = adj_get(adj_index1);
126 hdr0 = vlib_buffer_get_current (p0);
127 hdr1 = vlib_buffer_get_current (p1);
128
129 /* Guess we are only writing on simple Ethernet header. */
130 vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
131 sizeof (ethernet_header_t));
132
133 /* Update packet buffer attributes/set output interface. */
134 rw_len0 = adj0[0].rewrite_header.data_bytes;
135 rw_len1 = adj1[0].rewrite_header.data_bytes;
Neale Ranns039cbfe2018-02-27 03:45:38 -0800136 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
137 vnet_buffer (p1)->mpls.save_rewrite_length = rw_len1;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000138
Neale Ranns044183f2017-01-24 01:34:25 -0800139 /* Bump the adj counters for packet and bytes */
140 vlib_increment_combined_counter
141 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200142 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800143 adj_index0,
144 1,
145 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
146 vlib_increment_combined_counter
147 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200148 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800149 adj_index1,
150 1,
151 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000152
153 /* Check MTU of outgoing interface. */
154 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
155 adj0[0].rewrite_header.max_l3_packet_bytes))
156 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400157 vlib_buffer_advance(p0, -rw_len0);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000158
159 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
160 adj0[0].rewrite_header.sw_if_index;
161 next0 = adj0[0].rewrite_header.next_index;
162 error0 = IP4_ERROR_NONE;
163
Neale Rannsb069a692017-03-15 12:34:25 -0400164 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
165 vnet_feature_arc_start (mm->output_feature_arc_index,
166 adj0[0].rewrite_header.sw_if_index,
167 &next0, p0);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000168 }
169 else
170 {
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530171 error0 = IP4_ERROR_MTU_EXCEEDED;
Ole Troaneb284a12019-10-09 13:33:19 +0200172 next0 = MPLS_OUTPUT_NEXT_FRAG;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530173 vlib_node_increment_counter (vm, mpls_output_node.index,
174 MPLS_ERROR_PKTS_NEED_FRAG,
175 1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000176 }
177 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
178 adj1[0].rewrite_header.max_l3_packet_bytes))
179 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400180 vlib_buffer_advance(p1, -rw_len1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000181
182 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
183 adj1[0].rewrite_header.sw_if_index;
184 next1 = adj1[0].rewrite_header.next_index;
185 error1 = IP4_ERROR_NONE;
186
Neale Rannsb069a692017-03-15 12:34:25 -0400187 if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
188 vnet_feature_arc_start (mm->output_feature_arc_index,
189 adj1[0].rewrite_header.sw_if_index,
190 &next1, p1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000191 }
192 else
193 {
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530194 error1 = IP4_ERROR_MTU_EXCEEDED;
Ole Troaneb284a12019-10-09 13:33:19 +0200195 next1 = MPLS_OUTPUT_NEXT_FRAG;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530196 vlib_node_increment_counter (vm, mpls_output_node.index,
197 MPLS_ERROR_PKTS_NEED_FRAG,
198 1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000199 }
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530200 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
Neale Rannsb069a692017-03-15 12:34:25 -0400201 {
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800202 adj0->sub_type.midchain.fixup_func
203 (vm, adj0, p0,
204 adj0->sub_type.midchain.fixup_data);
205 adj1->sub_type.midchain.fixup_func
206 (vm, adj1, p1,
207 adj1->sub_type.midchain.fixup_data);
Neale Rannsb069a692017-03-15 12:34:25 -0400208 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000209
210 p0->error = error_node->errors[error0];
211 p1->error = error_node->errors[error1];
212
213 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
214 {
215 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
216 p0, sizeof (*tr));
217 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
218 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
219 }
220 if (PREDICT_FALSE(p1->flags & VLIB_BUFFER_IS_TRACED))
221 {
222 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
223 p1, sizeof (*tr));
224 tr->adj_index = vnet_buffer(p1)->ip.adj_index[VLIB_TX];
225 tr->flow_hash = vnet_buffer(p1)->ip.flow_hash;
226 }
227
228 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
229 to_next, n_left_to_next,
230 pi0, pi1, next0, next1);
231 }
232
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100233 while (n_left_from > 0 && n_left_to_next > 0)
234 {
235 ip_adjacency_t * adj0;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530236 mpls_unicast_header_t *hdr0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100237 vlib_buffer_t * p0;
Benoît Gannec257e072019-06-17 14:42:47 +0200238 u32 pi0, adj_index0, next0, error0;
239 word rw_len0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240
241 pi0 = to_next[0] = from[0];
242
243 p0 = vlib_get_buffer (vm, pi0);
244
245 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
246
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100247 adj0 = adj_get(adj_index0);
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530248 hdr0 = vlib_buffer_get_current (p0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100249
250 /* Guess we are only writing on simple Ethernet header. */
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400251 vnet_rewrite_one_header (adj0[0], hdr0,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100252 sizeof (ethernet_header_t));
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400253
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100254 /* Update packet buffer attributes/set output interface. */
255 rw_len0 = adj0[0].rewrite_header.data_bytes;
Neale Ranns039cbfe2018-02-27 03:45:38 -0800256 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
257
Neale Ranns044183f2017-01-24 01:34:25 -0800258 vlib_increment_combined_counter
259 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200260 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800261 adj_index0,
262 1,
263 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
Zhiyong Yang734d4302019-05-29 22:38:19 -0400264
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100265 /* Check MTU of outgoing interface. */
Neale Ranns9ca18c62016-12-10 21:08:09 +0000266 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
267 adj0[0].rewrite_header.max_l3_packet_bytes))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100268 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400269 vlib_buffer_advance(p0, -rw_len0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270
271 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
272 adj0[0].rewrite_header.sw_if_index;
273 next0 = adj0[0].rewrite_header.next_index;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000274 error0 = IP4_ERROR_NONE;
John Loaeb06f42016-10-15 17:45:35 -0400275
Neale Rannsb069a692017-03-15 12:34:25 -0400276 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
277 vnet_feature_arc_start (mm->output_feature_arc_index,
278 adj0[0].rewrite_header.sw_if_index,
279 &next0, p0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100280 }
281 else
282 {
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530283 error0 = IP4_ERROR_MTU_EXCEEDED;
Ole Troaneb284a12019-10-09 13:33:19 +0200284 next0 = MPLS_OUTPUT_NEXT_FRAG;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530285 vlib_node_increment_counter (vm, mpls_output_node.index,
286 MPLS_ERROR_PKTS_NEED_FRAG,
287 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100288 }
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530289 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
Neale Rannsb069a692017-03-15 12:34:25 -0400290 {
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800291 adj0->sub_type.midchain.fixup_func
292 (vm, adj0, p0,
293 adj0->sub_type.midchain.fixup_data);
Neale Rannsb069a692017-03-15 12:34:25 -0400294 }
295
296 p0->error = error_node->errors[error0];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100297
298 from += 1;
299 n_left_from -= 1;
300 to_next += 1;
301 n_left_to_next -= 1;
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400302
303 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100304 {
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400305 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100306 p0, sizeof (*tr));
307 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
308 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
309 }
310
311 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
312 to_next, n_left_to_next,
313 pi0, next0);
314 }
315
316 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
317 }
318 vlib_node_increment_counter (vm, mpls_output_node.index,
319 MPLS_ERROR_PKTS_ENCAP,
320 from_frame->n_vectors);
321
322 return from_frame->n_vectors;
323}
324
325static char * mpls_error_strings[] = {
326#define mpls_error(n,s) s,
327#include "error.def"
328#undef mpls_error
329};
330
Filip Tehlar17fcd982019-03-05 04:32:11 -0800331VLIB_NODE_FN (mpls_output_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100332 vlib_node_runtime_t * node,
333 vlib_frame_t * from_frame)
334{
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530335 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MODE));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100336}
337
338VLIB_REGISTER_NODE (mpls_output_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100339 .name = "mpls-output",
340 /* Takes a vector of packets. */
341 .vector_size = sizeof (u32),
342 .n_errors = MPLS_N_ERROR,
343 .error_strings = mpls_error_strings,
344
345 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
346 .next_nodes = {
Ole Troaneb284a12019-10-09 13:33:19 +0200347 [MPLS_OUTPUT_NEXT_DROP] = "mpls-drop",
348 [MPLS_OUTPUT_NEXT_FRAG] = "mpls-frag",
349 },
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100350 .format_trace = format_mpls_output_trace,
351};
352
Filip Tehlar17fcd982019-03-05 04:32:11 -0800353VLIB_NODE_FN (mpls_midchain_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100354 vlib_node_runtime_t * node,
355 vlib_frame_t * from_frame)
356{
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530357 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MIDCHAIN_MODE));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100358}
359
360VLIB_REGISTER_NODE (mpls_midchain_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100361 .name = "mpls-midchain",
362 .vector_size = sizeof (u32),
363
Ole Troaneb284a12019-10-09 13:33:19 +0200364 .n_errors = MPLS_N_ERROR,
365 .error_strings = mpls_error_strings,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100366
367 .sibling_of = "mpls-output",
Ole Troaneb284a12019-10-09 13:33:19 +0200368 .format_trace = format_mpls_output_trace,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100369};
370
Ole Troaneb284a12019-10-09 13:33:19 +0200371static char *mpls_frag_error_strings[] = {
372#define _(sym,string) string,
373 foreach_ip_frag_error
374#undef _
375};
376
377typedef struct mpls_frag_trace_t_
378{
379 u16 pkt_size;
380 u16 mtu;
381} mpls_frag_trace_t;
382
383typedef enum
384{
385 MPLS_FRAG_NEXT_REWRITE,
386 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN,
387 MPLS_FRAG_NEXT_ICMP_ERROR,
388 MPLS_FRAG_NEXT_DROP,
389 MPLS_FRAG_N_NEXT,
390} mpls_frag_next_t;
391
392static uword
393mpls_frag (vlib_main_t * vm,
394 vlib_node_runtime_t * node,
395 vlib_frame_t * frame)
396{
397 u32 n_left_from, next_index, * from, * to_next, n_left_to_next, *frags;
398 vlib_node_runtime_t * error_node;
399
400 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
401 from = vlib_frame_vector_args (frame);
402 n_left_from = frame->n_vectors;
403 next_index = node->cached_next_index;
404 frags = NULL;
405
406 while (n_left_from > 0)
407 {
408 vlib_get_next_frame (vm, node, next_index,
409 to_next, n_left_to_next);
410
411 while (n_left_from > 0 && n_left_to_next > 0)
412 {
413 ip_adjacency_t * adj0;
414 vlib_buffer_t * p0;
415 mpls_frag_next_t next0;
416 u32 pi0, adj_index0;
417 ip_frag_error_t error0 = IP_FRAG_ERROR_NONE;
418 i16 encap_size;
419 u8 is_ip4;
420
421 pi0 = to_next[0] = from[0];
422 p0 = vlib_get_buffer (vm, pi0);
423 from += 1;
424 n_left_from -= 1;
425 is_ip4 = vnet_buffer (p0)->mpls.pyld_proto == DPO_PROTO_IP4;
426
427 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
428 adj0 = adj_get(adj_index0);
429
430 /* the size of the MPLS stack */
431 encap_size = vnet_buffer(p0)->l3_hdr_offset - p0->current_data;
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400432
Ole Troaneb284a12019-10-09 13:33:19 +0200433 /* IP fragmentation */
434 if (is_ip4)
435 error0 = ip4_frag_do_fragment (vm, pi0,
436 adj0->rewrite_header.max_l3_packet_bytes,
437 encap_size, &frags);
438 else
439 error0 = ip6_frag_do_fragment (vm, pi0,
440 adj0->rewrite_header.max_l3_packet_bytes,
441 encap_size, &frags);
442
443 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
444 {
445 mpls_frag_trace_t *tr =
446 vlib_add_trace (vm, node, p0, sizeof (*tr));
447 tr->mtu = adj0->rewrite_header.max_l3_packet_bytes;
448 tr->pkt_size = vlib_buffer_length_in_chain(vm, p0);
449 }
450
451 if (PREDICT_TRUE(error0 == IP_FRAG_ERROR_NONE))
452 {
453 /* Free original buffer chain */
454 vlib_buffer_free_one (vm, pi0); /* Free original packet */
455 next0 = (IP_LOOKUP_NEXT_MIDCHAIN == adj0->lookup_next_index ?
456 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN :
457 MPLS_FRAG_NEXT_REWRITE);
458 }
459 else if (is_ip4 && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
460 {
461 icmp4_error_set_vnet_buffer (
462 p0, ICMP4_destination_unreachable,
463 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
464 vnet_buffer (p0)->ip_frag.mtu);
465 next0 = MPLS_FRAG_NEXT_ICMP_ERROR;
466 }
467 else
468 {
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400469 vlib_error_count (vm, mpls_output_node.index, error0, 1);
Ole Troaneb284a12019-10-09 13:33:19 +0200470 vec_add1 (frags, pi0); /* Get rid of the original buffer */
471 next0 = MPLS_FRAG_NEXT_DROP;
472 }
473
474 /* Send fragments that were added in the frame */
475 u32 *frag_from, frag_left;
476
477 frag_from = frags;
478 frag_left = vec_len (frags);
479
480 while (frag_left > 0)
481 {
482 while (frag_left > 0 && n_left_to_next > 0)
483 {
484 u32 i;
485 i = to_next[0] = frag_from[0];
486 frag_from += 1;
487 frag_left -= 1;
488 to_next += 1;
489 n_left_to_next -= 1;
490
491 p0 = vlib_get_buffer (vm, i);
492 p0->error = error_node->errors[error0];
493
494 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
495 to_next, n_left_to_next, i,
496 next0);
497 }
498 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
499 vlib_get_next_frame (vm, node, next_index, to_next,
500 n_left_to_next);
501 }
502 vec_reset_length (frags);
503 }
504 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
505 }
506 vec_free (frags);
507
508 return frame->n_vectors;
509}
510
511static u8 *
512format_mpls_frag_trace (u8 * s, va_list * args)
513{
514 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
515 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
516 mpls_frag_trace_t *t = va_arg (*args, mpls_frag_trace_t *);
517
518 s = format (s, "mtu:%d pkt-size:%d", t->mtu, t->pkt_size);
519 return s;
520}
521
522VLIB_REGISTER_NODE (mpls_frag_node) = {
523 .function = mpls_frag,
524 .name = "mpls-frag",
525 .vector_size = sizeof (u32),
526 .format_trace = format_mpls_frag_trace,
527 .type = VLIB_NODE_TYPE_INTERNAL,
528
529 .n_errors = IP_FRAG_N_ERROR,
530 .error_strings = mpls_frag_error_strings,
531
532 .n_next_nodes = MPLS_FRAG_N_NEXT,
533 .next_nodes = {
534 [MPLS_FRAG_NEXT_REWRITE] = "mpls-output",
535 [MPLS_FRAG_NEXT_REWRITE_MIDCHAIN] = "mpls-midchain",
536 [MPLS_FRAG_NEXT_ICMP_ERROR] = "ip4-icmp-error",
537 [MPLS_FRAG_NEXT_DROP] = "mpls-drop"
538 },
539};
540
541/*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100542 * @brief Next index values from the MPLS incomplete adj node
543 */
544#define foreach_mpls_adj_incomplete_next \
545_(DROP, "error-drop") \
546_(IP4, "ip4-arp") \
547_(IP6, "ip6-discover-neighbor")
548
549typedef enum {
550#define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
551 foreach_mpls_adj_incomplete_next
552#undef _
553 MPLS_ADJ_INCOMPLETE_N_NEXT,
554} mpls_adj_incomplete_next_t;
555
556/**
557 * @brief A struct to hold tracing information for the MPLS label imposition
558 * node.
559 */
560typedef struct mpls_adj_incomplete_trace_t_
561{
562 u32 next;
563} mpls_adj_incomplete_trace_t;
564
565
566/**
567 * @brief Graph node for incomplete MPLS adjacency.
568 * This node will push traffic to either the v4-arp or v6-nd node
569 * based on the next-hop proto of the adj.
570 * We pay a cost for this 'routing' node, but an incomplete adj is the
571 * exception case.
572 */
Filip Tehlar17fcd982019-03-05 04:32:11 -0800573VLIB_NODE_FN (mpls_adj_incomplete_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100574 vlib_node_runtime_t * node,
575 vlib_frame_t * from_frame)
576{
577 u32 n_left_from, next_index, * from, * to_next;
578
579 from = vlib_frame_vector_args (from_frame);
580 n_left_from = from_frame->n_vectors;
581 next_index = node->cached_next_index;
582
583 while (n_left_from > 0)
584 {
585 u32 n_left_to_next;
586
587 vlib_get_next_frame (vm, node, next_index,
588 to_next, n_left_to_next);
589
590 while (n_left_from > 0 && n_left_to_next > 0)
591 {
592 u32 pi0, next0, adj_index0;
593 ip_adjacency_t * adj0;
594 vlib_buffer_t * p0;
595
596 pi0 = to_next[0] = from[0];
597 p0 = vlib_get_buffer (vm, pi0);
598 from += 1;
599 n_left_from -= 1;
600 to_next += 1;
601 n_left_to_next -= 1;
602
603 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100604
605 adj0 = adj_get(adj_index0);
606
607 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
608 {
609 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
610 }
611 else
612 {
613 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400614 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100615
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400616 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100617 {
618 mpls_adj_incomplete_trace_t *tr =
619 vlib_add_trace (vm, node, p0, sizeof (*tr));
620 tr->next = next0;
621 }
622
623 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
624 to_next, n_left_to_next,
625 pi0, next0);
626 }
627
628 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
629 }
630
631 return from_frame->n_vectors;
632}
633
634static u8 *
635format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
636{
637 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
638 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
639 mpls_adj_incomplete_trace_t * t;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200640 u32 indent;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100641
642 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
643 indent = format_get_indent (s);
644
645 s = format (s, "%Unext:%d",
646 format_white_space, indent,
647 t->next);
648 return (s);
649}
650
651VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100652 .name = "mpls-adj-incomplete",
653 .format_trace = format_mpls_adj_incomplete_trace,
654 /* Takes a vector of packets. */
655 .vector_size = sizeof (u32),
656 .n_errors = MPLS_N_ERROR,
657 .error_strings = mpls_error_strings,
658
659 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
660 .next_nodes = {
661#define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
662 foreach_mpls_adj_incomplete_next
663#undef _
664 },
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100665};