blob: 9c1d7316db7746a19d425ea43ac184b3e330bb45 [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 Ranns4a58e492020-12-21 13:19:10 +000022#include <vnet/adj/adj_dp.h>
Neale Ranns41a85c02022-08-09 01:24:41 +000023#include <vnet/mpls/mpls.api_enum.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010024
25typedef struct {
26 /* Adjacency taken. */
27 u32 adj_index;
28 u32 flow_hash;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010029} mpls_output_trace_t;
30
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +053031typedef enum {
32 MPLS_OUTPUT_MODE,
33 MPLS_OUTPUT_MIDCHAIN_MODE
34}mpls_output_mode_t;
35
Neale Ranns696e88d2017-03-16 07:34:55 -040036#define foreach_mpls_output_next \
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +053037_(DROP, "error-drop") \
Ole Troaneb284a12019-10-09 13:33:19 +020038_(FRAG, "mpls-frag")
Neale Ranns696e88d2017-03-16 07:34:55 -040039
40typedef enum {
41#define _(s,n) MPLS_OUTPUT_NEXT_##s,
42 foreach_mpls_output_next
43#undef _
44 MPLS_OUTPUT_N_NEXT,
45} mpls_output_next_t;
46
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047static u8 *
48format_mpls_output_trace (u8 * s, va_list * args)
49{
50 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
51 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
52 mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010053
54 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
55 t->adj_index,
Neale Rannsb80c5362016-10-08 13:03:40 +010056 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010057 t->flow_hash);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010058 return s;
59}
60
61static inline uword
62mpls_output_inline (vlib_main_t * vm,
63 vlib_node_runtime_t * node,
John Loaeb06f42016-10-15 17:45:35 -040064 vlib_frame_t * from_frame,
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +053065 mpls_output_mode_t mode)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010066{
Damjan Marion586afd72017-04-05 19:18:20 +020067 u32 n_left_from, next_index, * from, * to_next, thread_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010068 vlib_node_runtime_t * error_node;
Neale Ranns9ca18c62016-12-10 21:08:09 +000069 u32 n_left_to_next;
Neale Rannsb069a692017-03-15 12:34:25 -040070 mpls_main_t *mm;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071
Damjan Marion586afd72017-04-05 19:18:20 +020072 thread_index = vlib_get_thread_index();
Neale Ranns0bfe5d82016-08-25 15:29:12 +010073 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
74 from = vlib_frame_vector_args (from_frame);
75 n_left_from = from_frame->n_vectors;
76 next_index = node->cached_next_index;
Neale Rannsb069a692017-03-15 12:34:25 -040077 mm = &mpls_main;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010078
79 while (n_left_from > 0)
80 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010081 vlib_get_next_frame (vm, node, next_index,
82 to_next, n_left_to_next);
83
Neale Ranns9ca18c62016-12-10 21:08:09 +000084 while (n_left_from >= 4 && n_left_to_next >= 2)
85 {
86 ip_adjacency_t * adj0;
87 mpls_unicast_header_t *hdr0;
88 vlib_buffer_t * p0;
Benoît Gannec257e072019-06-17 14:42:47 +020089 u32 pi0, adj_index0, next0, error0;
90 word rw_len0;
Neale Ranns9ca18c62016-12-10 21:08:09 +000091
92 ip_adjacency_t * adj1;
93 mpls_unicast_header_t *hdr1;
94 vlib_buffer_t * p1;
Benoît Gannec257e072019-06-17 14:42:47 +020095 u32 pi1, adj_index1, next1, error1;
96 word rw_len1;
Neale Ranns9ca18c62016-12-10 21:08:09 +000097
98 /* Prefetch next iteration. */
99 {
100 vlib_buffer_t * p2, * p3;
101
102 p2 = vlib_get_buffer (vm, from[2]);
103 p3 = vlib_get_buffer (vm, from[3]);
104
105 vlib_prefetch_buffer_header (p2, STORE);
106 vlib_prefetch_buffer_header (p3, STORE);
107
108 CLIB_PREFETCH (p2->data, sizeof (hdr0[0]), STORE);
109 CLIB_PREFETCH (p3->data, sizeof (hdr1[0]), STORE);
110 }
111
112 pi0 = to_next[0] = from[0];
113 pi1 = to_next[1] = from[1];
114
115 from += 2;
116 n_left_from -= 2;
117 to_next += 2;
118 n_left_to_next -= 2;
119
120 p0 = vlib_get_buffer (vm, pi0);
121 p1 = vlib_get_buffer (vm, pi1);
122
123 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
124 adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
125
Neale Ranns9ca18c62016-12-10 21:08:09 +0000126 adj0 = adj_get(adj_index0);
127 adj1 = adj_get(adj_index1);
128 hdr0 = vlib_buffer_get_current (p0);
129 hdr1 = vlib_buffer_get_current (p1);
130
131 /* Guess we are only writing on simple Ethernet header. */
132 vnet_rewrite_two_headers (adj0[0], adj1[0], hdr0, hdr1,
133 sizeof (ethernet_header_t));
134
135 /* Update packet buffer attributes/set output interface. */
136 rw_len0 = adj0[0].rewrite_header.data_bytes;
137 rw_len1 = adj1[0].rewrite_header.data_bytes;
Neale Ranns039cbfe2018-02-27 03:45:38 -0800138 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
139 vnet_buffer (p1)->mpls.save_rewrite_length = rw_len1;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000140
Neale Ranns044183f2017-01-24 01:34:25 -0800141 /* Bump the adj counters for packet and bytes */
142 vlib_increment_combined_counter
143 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200144 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800145 adj_index0,
146 1,
147 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
148 vlib_increment_combined_counter
149 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200150 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800151 adj_index1,
152 1,
153 vlib_buffer_length_in_chain (vm, p1) + rw_len1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000154
155 /* Check MTU of outgoing interface. */
156 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
157 adj0[0].rewrite_header.max_l3_packet_bytes))
158 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400159 vlib_buffer_advance(p0, -rw_len0);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000160
161 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
162 adj0[0].rewrite_header.sw_if_index;
163 next0 = adj0[0].rewrite_header.next_index;
164 error0 = IP4_ERROR_NONE;
165
Neale Rannsb069a692017-03-15 12:34:25 -0400166 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
167 vnet_feature_arc_start (mm->output_feature_arc_index,
168 adj0[0].rewrite_header.sw_if_index,
169 &next0, p0);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000170 }
171 else
172 {
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530173 error0 = IP4_ERROR_MTU_EXCEEDED;
Ole Troaneb284a12019-10-09 13:33:19 +0200174 next0 = MPLS_OUTPUT_NEXT_FRAG;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530175 vlib_node_increment_counter (vm, mpls_output_node.index,
176 MPLS_ERROR_PKTS_NEED_FRAG,
177 1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000178 }
179 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p1) <=
180 adj1[0].rewrite_header.max_l3_packet_bytes))
181 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400182 vlib_buffer_advance(p1, -rw_len1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000183
184 vnet_buffer (p1)->sw_if_index[VLIB_TX] =
185 adj1[0].rewrite_header.sw_if_index;
186 next1 = adj1[0].rewrite_header.next_index;
187 error1 = IP4_ERROR_NONE;
188
Neale Rannsb069a692017-03-15 12:34:25 -0400189 if (PREDICT_FALSE(adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
190 vnet_feature_arc_start (mm->output_feature_arc_index,
191 adj1[0].rewrite_header.sw_if_index,
192 &next1, p1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000193 }
194 else
195 {
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530196 error1 = IP4_ERROR_MTU_EXCEEDED;
Ole Troaneb284a12019-10-09 13:33:19 +0200197 next1 = MPLS_OUTPUT_NEXT_FRAG;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530198 vlib_node_increment_counter (vm, mpls_output_node.index,
199 MPLS_ERROR_PKTS_NEED_FRAG,
200 1);
Neale Ranns9ca18c62016-12-10 21:08:09 +0000201 }
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530202 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
Neale Rannsb069a692017-03-15 12:34:25 -0400203 {
Neale Ranns4a58e492020-12-21 13:19:10 +0000204 adj_midchain_fixup (vm, adj0, p0, VNET_LINK_MPLS);
205 adj_midchain_fixup (vm, adj1, p1, VNET_LINK_MPLS);
206 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000207
Neale Ranns4a58e492020-12-21 13:19:10 +0000208 p0->error = error_node->errors[error0];
209 p1->error = error_node->errors[error1];
Neale Ranns9ca18c62016-12-10 21:08:09 +0000210
Neale Ranns4a58e492020-12-21 13:19:10 +0000211 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
212 {
213 mpls_output_trace_t *tr =
214 vlib_add_trace (vm, node, p0, sizeof (*tr));
215 tr->adj_index = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
216 tr->flow_hash = vnet_buffer (p0)->ip.flow_hash;
217 }
218 if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED))
219 {
220 mpls_output_trace_t *tr =
221 vlib_add_trace (vm, node, p1, sizeof (*tr));
222 tr->adj_index = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
223 tr->flow_hash = vnet_buffer (p1)->ip.flow_hash;
224 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000225
Neale Ranns4a58e492020-12-21 13:19:10 +0000226 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
227 n_left_to_next, pi0, pi1, next0,
228 next1);
229 }
Neale Ranns9ca18c62016-12-10 21:08:09 +0000230
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100231 while (n_left_from > 0 && n_left_to_next > 0)
232 {
233 ip_adjacency_t * adj0;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530234 mpls_unicast_header_t *hdr0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100235 vlib_buffer_t * p0;
Benoît Gannec257e072019-06-17 14:42:47 +0200236 u32 pi0, adj_index0, next0, error0;
237 word rw_len0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100238
239 pi0 = to_next[0] = from[0];
240
241 p0 = vlib_get_buffer (vm, pi0);
242
243 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
244
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100245 adj0 = adj_get(adj_index0);
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530246 hdr0 = vlib_buffer_get_current (p0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100247
248 /* Guess we are only writing on simple Ethernet header. */
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400249 vnet_rewrite_one_header (adj0[0], hdr0,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100250 sizeof (ethernet_header_t));
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400251
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100252 /* Update packet buffer attributes/set output interface. */
253 rw_len0 = adj0[0].rewrite_header.data_bytes;
Neale Ranns039cbfe2018-02-27 03:45:38 -0800254 vnet_buffer (p0)->mpls.save_rewrite_length = rw_len0;
255
Neale Ranns044183f2017-01-24 01:34:25 -0800256 vlib_increment_combined_counter
257 (&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200258 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800259 adj_index0,
260 1,
261 vlib_buffer_length_in_chain (vm, p0) + rw_len0);
Zhiyong Yang734d4302019-05-29 22:38:19 -0400262
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100263 /* Check MTU of outgoing interface. */
Neale Ranns9ca18c62016-12-10 21:08:09 +0000264 if (PREDICT_TRUE(vlib_buffer_length_in_chain (vm, p0) <=
265 adj0[0].rewrite_header.max_l3_packet_bytes))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100266 {
Zhiyong Yang734d4302019-05-29 22:38:19 -0400267 vlib_buffer_advance(p0, -rw_len0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100268
269 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
270 adj0[0].rewrite_header.sw_if_index;
271 next0 = adj0[0].rewrite_header.next_index;
Neale Ranns9ca18c62016-12-10 21:08:09 +0000272 error0 = IP4_ERROR_NONE;
John Loaeb06f42016-10-15 17:45:35 -0400273
Neale Rannsb069a692017-03-15 12:34:25 -0400274 if (PREDICT_FALSE(adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
275 vnet_feature_arc_start (mm->output_feature_arc_index,
276 adj0[0].rewrite_header.sw_if_index,
277 &next0, p0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100278 }
279 else
280 {
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530281 error0 = IP4_ERROR_MTU_EXCEEDED;
Ole Troaneb284a12019-10-09 13:33:19 +0200282 next0 = MPLS_OUTPUT_NEXT_FRAG;
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530283 vlib_node_increment_counter (vm, mpls_output_node.index,
284 MPLS_ERROR_PKTS_NEED_FRAG,
285 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100286 }
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530287 if (mode == MPLS_OUTPUT_MIDCHAIN_MODE)
Neale Rannsb069a692017-03-15 12:34:25 -0400288 {
Neale Ranns4a58e492020-12-21 13:19:10 +0000289 adj_midchain_fixup (vm, adj0, p0, VNET_LINK_MPLS);
290 }
Neale Rannsb069a692017-03-15 12:34:25 -0400291
Neale Ranns4a58e492020-12-21 13:19:10 +0000292 p0->error = error_node->errors[error0];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100293
294 from += 1;
295 n_left_from -= 1;
296 to_next += 1;
297 n_left_to_next -= 1;
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400298
299 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100300 {
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400301 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100302 p0, sizeof (*tr));
303 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
304 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
305 }
306
307 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
308 to_next, n_left_to_next,
309 pi0, next0);
310 }
311
312 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
313 }
314 vlib_node_increment_counter (vm, mpls_output_node.index,
315 MPLS_ERROR_PKTS_ENCAP,
316 from_frame->n_vectors);
317
318 return from_frame->n_vectors;
319}
320
Filip Tehlar17fcd982019-03-05 04:32:11 -0800321VLIB_NODE_FN (mpls_output_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100322 vlib_node_runtime_t * node,
323 vlib_frame_t * from_frame)
324{
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530325 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MODE));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100326}
327
328VLIB_REGISTER_NODE (mpls_output_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100329 .name = "mpls-output",
330 /* Takes a vector of packets. */
331 .vector_size = sizeof (u32),
332 .n_errors = MPLS_N_ERROR,
Neale Ranns41a85c02022-08-09 01:24:41 +0000333 .error_counters = mpls_error_counters,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100334
335 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
336 .next_nodes = {
Ole Troaneb284a12019-10-09 13:33:19 +0200337 [MPLS_OUTPUT_NEXT_DROP] = "mpls-drop",
338 [MPLS_OUTPUT_NEXT_FRAG] = "mpls-frag",
339 },
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100340 .format_trace = format_mpls_output_trace,
341};
342
Filip Tehlar17fcd982019-03-05 04:32:11 -0800343VLIB_NODE_FN (mpls_midchain_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100344 vlib_node_runtime_t * node,
345 vlib_frame_t * from_frame)
346{
Rajesh Goeld6f1c9c2019-10-06 13:17:36 +0530347 return (mpls_output_inline(vm, node, from_frame, MPLS_OUTPUT_MIDCHAIN_MODE));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100348}
349
350VLIB_REGISTER_NODE (mpls_midchain_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100351 .name = "mpls-midchain",
352 .vector_size = sizeof (u32),
353
Ole Troaneb284a12019-10-09 13:33:19 +0200354 .n_errors = MPLS_N_ERROR,
Neale Ranns41a85c02022-08-09 01:24:41 +0000355 .error_counters = mpls_error_counters,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100356
357 .sibling_of = "mpls-output",
Ole Troaneb284a12019-10-09 13:33:19 +0200358 .format_trace = format_mpls_output_trace,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100359};
360
Ole Troaneb284a12019-10-09 13:33:19 +0200361typedef struct mpls_frag_trace_t_
362{
363 u16 pkt_size;
364 u16 mtu;
365} mpls_frag_trace_t;
366
367typedef enum
368{
Neale Rannsec5371e2022-03-04 11:45:41 +0000369 MPLS_FRAG_NEXT_REWRITE,
370 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN,
371 MPLS_FRAG_NEXT_ICMP4_ERROR,
372 MPLS_FRAG_NEXT_ICMP6_ERROR,
373 MPLS_FRAG_NEXT_DROP,
374 MPLS_FRAG_N_NEXT,
Ole Troaneb284a12019-10-09 13:33:19 +0200375} mpls_frag_next_t;
376
377static uword
378mpls_frag (vlib_main_t * vm,
379 vlib_node_runtime_t * node,
380 vlib_frame_t * frame)
381{
382 u32 n_left_from, next_index, * from, * to_next, n_left_to_next, *frags;
Ole Troaneb284a12019-10-09 13:33:19 +0200383
Ole Troaneb284a12019-10-09 13:33:19 +0200384 from = vlib_frame_vector_args (frame);
385 n_left_from = frame->n_vectors;
386 next_index = node->cached_next_index;
387 frags = NULL;
388
389 while (n_left_from > 0)
390 {
391 vlib_get_next_frame (vm, node, next_index,
392 to_next, n_left_to_next);
393
394 while (n_left_from > 0 && n_left_to_next > 0)
395 {
396 ip_adjacency_t * adj0;
397 vlib_buffer_t * p0;
398 mpls_frag_next_t next0;
399 u32 pi0, adj_index0;
400 ip_frag_error_t error0 = IP_FRAG_ERROR_NONE;
Neale Rannsec5371e2022-03-04 11:45:41 +0000401 i16 encap_size, mtu;
Neale Rannsc396d232022-02-24 10:30:12 +0000402 u8 is_ip4;
Ole Troaneb284a12019-10-09 13:33:19 +0200403
Neale Rannsc396d232022-02-24 10:30:12 +0000404 pi0 = to_next[0] = from[0];
405 p0 = vlib_get_buffer (vm, pi0);
406 from += 1;
407 n_left_from -= 1;
408 is_ip4 = vnet_buffer (p0)->mpls.pyld_proto == DPO_PROTO_IP4;
Ole Troaneb284a12019-10-09 13:33:19 +0200409
Neale Rannsc396d232022-02-24 10:30:12 +0000410 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
411 adj0 = adj_get (adj_index0);
Neale Rannsec5371e2022-03-04 11:45:41 +0000412
Neale Rannsc396d232022-02-24 10:30:12 +0000413 /* the size of the MPLS stack */
414 encap_size = vnet_buffer (p0)->l3_hdr_offset - p0->current_data;
415 mtu = adj0->rewrite_header.max_l3_packet_bytes - encap_size;
Ole Troaneb284a12019-10-09 13:33:19 +0200416
Neale Rannsc396d232022-02-24 10:30:12 +0000417 /* IP fragmentation */
418 if (is_ip4)
419 error0 = ip4_frag_do_fragment (vm, pi0, mtu, encap_size, &frags);
420 else
Neale Rannsec5371e2022-03-04 11:45:41 +0000421 {
422 if (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED))
423 {
424 /* only fragment locally generated IPv6 */
425 error0 = IP_FRAG_ERROR_DONT_FRAGMENT_SET;
426 }
427 else
428 {
429 error0 =
430 ip6_frag_do_fragment (vm, pi0, mtu, encap_size, &frags);
431 }
432 }
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400433
Neale Rannsc396d232022-02-24 10:30:12 +0000434 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
435 {
436 mpls_frag_trace_t *tr =
437 vlib_add_trace (vm, node, p0, sizeof (*tr));
438 tr->mtu = mtu;
439 tr->pkt_size = vlib_buffer_length_in_chain (vm, p0);
440 }
Ole Troaneb284a12019-10-09 13:33:19 +0200441
Neale Rannsc396d232022-02-24 10:30:12 +0000442 if (PREDICT_TRUE (error0 == IP_FRAG_ERROR_NONE))
443 {
444 /* Free original buffer chain */
Neale Rannsec5371e2022-03-04 11:45:41 +0000445 vlib_buffer_free_one (vm, pi0);
Neale Rannsc396d232022-02-24 10:30:12 +0000446 next0 = (IP_LOOKUP_NEXT_MIDCHAIN == adj0->lookup_next_index ?
447 MPLS_FRAG_NEXT_REWRITE_MIDCHAIN :
448 MPLS_FRAG_NEXT_REWRITE);
449 }
Neale Rannsc396d232022-02-24 10:30:12 +0000450 else
451 {
Neale Rannsec5371e2022-03-04 11:45:41 +0000452 vlib_error_count (vm, node->node_index, error0, 1);
453
454 if (error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
455 {
456 vlib_buffer_advance (p0, encap_size);
457 if (is_ip4)
458 {
459 icmp4_error_set_vnet_buffer (
460 p0, ICMP4_destination_unreachable,
461 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
Neale Rannsfbc633f2022-03-18 13:05:09 +0000462 mtu);
Neale Rannsec5371e2022-03-04 11:45:41 +0000463 next0 = MPLS_FRAG_NEXT_ICMP4_ERROR;
464 }
465 else
466 {
Neale Rannsfbc633f2022-03-18 13:05:09 +0000467 icmp6_error_set_vnet_buffer (p0, ICMP6_packet_too_big,
468 0, mtu);
Neale Rannsec5371e2022-03-04 11:45:41 +0000469 next0 = MPLS_FRAG_NEXT_ICMP6_ERROR;
470 }
471 }
472 else
473 {
474 next0 = MPLS_FRAG_NEXT_DROP;
475 }
476
477 /* Get rid of the original buffer */
478 vec_add1 (frags, pi0);
Neale Rannsc396d232022-02-24 10:30:12 +0000479 }
Ole Troaneb284a12019-10-09 13:33:19 +0200480
Neale Rannsc396d232022-02-24 10:30:12 +0000481 /* Send fragments that were added in the frame */
482 u32 *frag_from, frag_left;
Ole Troaneb284a12019-10-09 13:33:19 +0200483
Neale Rannsc396d232022-02-24 10:30:12 +0000484 frag_from = frags;
485 frag_left = vec_len (frags);
Ole Troaneb284a12019-10-09 13:33:19 +0200486
Neale Rannsc396d232022-02-24 10:30:12 +0000487 while (frag_left > 0)
488 {
489 while (frag_left > 0 && n_left_to_next > 0)
490 {
491 u32 i;
492 i = to_next[0] = frag_from[0];
493 frag_from += 1;
494 frag_left -= 1;
495 to_next += 1;
496 n_left_to_next -= 1;
Ole Troaneb284a12019-10-09 13:33:19 +0200497
Neale Rannsc396d232022-02-24 10:30:12 +0000498 vlib_validate_buffer_enqueue_x1 (
499 vm, node, next_index, to_next, n_left_to_next, i, next0);
500 }
501 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
502 vlib_get_next_frame (vm, node, next_index, to_next,
503 n_left_to_next);
504 }
505 vec_reset_length (frags);
Ole Troaneb284a12019-10-09 13:33:19 +0200506 }
507 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
508 }
509 vec_free (frags);
510
511 return frame->n_vectors;
512}
513
514static u8 *
515format_mpls_frag_trace (u8 * s, va_list * args)
516{
517 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
518 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
519 mpls_frag_trace_t *t = va_arg (*args, mpls_frag_trace_t *);
520
521 s = format (s, "mtu:%d pkt-size:%d", t->mtu, t->pkt_size);
522 return s;
523}
524
525VLIB_REGISTER_NODE (mpls_frag_node) = {
Neale Rannsec5371e2022-03-04 11:45:41 +0000526 .function = mpls_frag,
527 .name = "mpls-frag",
528 .vector_size = sizeof (u32),
529 .format_trace = format_mpls_frag_trace,
530 .type = VLIB_NODE_TYPE_INTERNAL,
Ole Troaneb284a12019-10-09 13:33:19 +0200531
Neale Rannsec5371e2022-03-04 11:45:41 +0000532 .n_errors = IP_FRAG_N_ERROR,
Neale Rannse22a7042022-08-09 03:03:29 +0000533 .error_counters = ip_frag_error_counters,
Ole Troaneb284a12019-10-09 13:33:19 +0200534
Neale Rannsec5371e2022-03-04 11:45:41 +0000535 .n_next_nodes = MPLS_FRAG_N_NEXT,
536 .next_nodes = { [MPLS_FRAG_NEXT_REWRITE] = "mpls-output",
537 [MPLS_FRAG_NEXT_REWRITE_MIDCHAIN] = "mpls-midchain",
538 [MPLS_FRAG_NEXT_ICMP4_ERROR] = "ip4-icmp-error",
539 [MPLS_FRAG_NEXT_ICMP6_ERROR] = "ip6-icmp-error",
540 [MPLS_FRAG_NEXT_DROP] = "mpls-drop" },
Ole Troaneb284a12019-10-09 13:33:19 +0200541};
542
543/*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100544 * @brief Next index values from the MPLS incomplete adj node
545 */
546#define foreach_mpls_adj_incomplete_next \
547_(DROP, "error-drop") \
548_(IP4, "ip4-arp") \
549_(IP6, "ip6-discover-neighbor")
550
551typedef enum {
552#define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
553 foreach_mpls_adj_incomplete_next
554#undef _
555 MPLS_ADJ_INCOMPLETE_N_NEXT,
556} mpls_adj_incomplete_next_t;
557
558/**
559 * @brief A struct to hold tracing information for the MPLS label imposition
560 * node.
561 */
562typedef struct mpls_adj_incomplete_trace_t_
563{
564 u32 next;
565} mpls_adj_incomplete_trace_t;
566
567
568/**
569 * @brief Graph node for incomplete MPLS adjacency.
570 * This node will push traffic to either the v4-arp or v6-nd node
571 * based on the next-hop proto of the adj.
572 * We pay a cost for this 'routing' node, but an incomplete adj is the
573 * exception case.
574 */
Filip Tehlar17fcd982019-03-05 04:32:11 -0800575VLIB_NODE_FN (mpls_adj_incomplete_node) (vlib_main_t * vm,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100576 vlib_node_runtime_t * node,
577 vlib_frame_t * from_frame)
578{
579 u32 n_left_from, next_index, * from, * to_next;
580
581 from = vlib_frame_vector_args (from_frame);
582 n_left_from = from_frame->n_vectors;
583 next_index = node->cached_next_index;
584
585 while (n_left_from > 0)
586 {
587 u32 n_left_to_next;
588
589 vlib_get_next_frame (vm, node, next_index,
590 to_next, n_left_to_next);
591
592 while (n_left_from > 0 && n_left_to_next > 0)
593 {
594 u32 pi0, next0, adj_index0;
595 ip_adjacency_t * adj0;
596 vlib_buffer_t * p0;
597
598 pi0 = to_next[0] = from[0];
599 p0 = vlib_get_buffer (vm, pi0);
600 from += 1;
601 n_left_from -= 1;
602 to_next += 1;
603 n_left_to_next -= 1;
604
605 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100606
607 adj0 = adj_get(adj_index0);
608
609 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
610 {
611 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
612 }
613 else
614 {
615 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400616 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100617
Dave Barach9cbfb4c2020-04-08 10:19:29 -0400618 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100619 {
620 mpls_adj_incomplete_trace_t *tr =
621 vlib_add_trace (vm, node, p0, sizeof (*tr));
622 tr->next = next0;
623 }
624
625 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
626 to_next, n_left_to_next,
627 pi0, next0);
628 }
629
630 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
631 }
632
633 return from_frame->n_vectors;
634}
635
636static u8 *
637format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
638{
639 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
640 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
641 mpls_adj_incomplete_trace_t * t;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200642 u32 indent;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100643
644 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
645 indent = format_get_indent (s);
646
647 s = format (s, "%Unext:%d",
648 format_white_space, indent,
649 t->next);
650 return (s);
651}
652
653VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100654 .name = "mpls-adj-incomplete",
655 .format_trace = format_mpls_adj_incomplete_trace,
656 /* Takes a vector of packets. */
657 .vector_size = sizeof (u32),
658 .n_errors = MPLS_N_ERROR,
Neale Ranns41a85c02022-08-09 01:24:41 +0000659 .error_counters = mpls_error_counters,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100660
661 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
662 .next_nodes = {
663#define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
664 foreach_mpls_adj_incomplete_next
665#undef _
666 },
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100667};