blob: a9fab9c80f11a1fb6ce969df360e89f010832c4d [file] [log] [blame]
Neale Rannsd792d9c2017-10-21 10:53:20 -07001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/buffer.h>
17#include <vnet/vnet.h>
18
19#include <vnet/bier/bier_fmask.h>
20#include <vnet/bier/bier_hdr_inlines.h>
21#include <vnet/bier/bier_table.h>
22#include <vnet/bier/bier_fmask.h>
23
24/**
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070025 * Struct maintaining the per-worker thread data for BIER lookups
Neale Rannsd792d9c2017-10-21 10:53:20 -070026 */
27typedef struct bier_lookup_main_t_
28{
29 /* per-cpu vector of cloned packets */
30 u32 **blm_clones;
31 /* per-cpu vector of BIER fmasks */
32 u32 **blm_fmasks;
33} bier_lookup_main_t;
34
35/**
36 * Single instance of the lookup main
37 */
38static bier_lookup_main_t bier_lookup_main;
39
40static char * bier_lookup_error_strings[] = {
41#define bier_error(n,s) s,
42#include <vnet/bier/bier_lookup_error.def>
43#undef bier_error
44};
45
46/*
Paul Vinciguerrae6eefb62019-05-13 15:56:41 -040047 * Keep these values semantically the same as BIER lookup
Neale Rannsd792d9c2017-10-21 10:53:20 -070048 */
49#define foreach_bier_lookup_next \
50 _(DROP, "bier-drop") \
51 _(OUTPUT, "bier-output")
52
53typedef enum {
54#define _(s,n) BIER_LOOKUP_NEXT_##s,
55 foreach_bier_lookup_next
56#undef _
57 BIER_LOOKUP_N_NEXT,
58} bier_lookup_next_t;
59
60typedef enum {
61#define bier_error(n,s) BIER_LOOKUP_ERROR_##n,
62#include <vnet/bier/bier_lookup_error.def>
63#undef bier_error
64 BIER_LOOKUP_N_ERROR,
65} bier_lookup_error_t;
66
67vlib_node_registration_t bier_lookup_node;
68
69/**
Jim Thompsonf324dec2019-04-08 03:22:21 -050070 * @brief Packet trace record for a BIER lookup
Neale Rannsd792d9c2017-10-21 10:53:20 -070071 */
72typedef struct bier_lookup_trace_t_
73{
74 u32 next_index;
75 index_t bt_index;
76 index_t bfm_index;
77} bier_lookup_trace_t;
78
79static uword
80bier_lookup (vlib_main_t * vm,
81 vlib_node_runtime_t * node,
82 vlib_frame_t * from_frame)
83{
84 u32 n_left_from, next_index, * from, * to_next;
85 bier_lookup_main_t *blm = &bier_lookup_main;
86 u32 thread_index = vlib_get_thread_index();
Neale Rannsf0510722018-01-31 11:35:41 -080087 bier_bit_mask_bucket_t buckets_copy[BIER_HDR_BUCKETS_4096];
Neale Rannsd792d9c2017-10-21 10:53:20 -070088
89 from = vlib_frame_vector_args (from_frame);
90 n_left_from = from_frame->n_vectors;
91 next_index = BIER_LOOKUP_NEXT_DROP;
92
93 while (n_left_from > 0)
94 {
95 u32 n_left_to_next;
96
97 vlib_get_next_frame (vm, node, next_index,
98 to_next, n_left_to_next);
99
100 while (n_left_from > 0 && n_left_to_next > 0)
101 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700102 u32 next0, bi0, n_bytes, bti0, bfmi0;
103 const bier_fmask_t *bfm0;
104 const bier_table_t *bt0;
105 u16 index, num_buckets;
106 const bier_hdr_t *bh0;
107 bier_bit_string_t bbs;
108 vlib_buffer_t *b0;
109 bier_bp_t fbs;
110 int bucket;
111
112 bi0 = from[0];
113 from += 1;
114 n_left_from -= 1;
115
116 b0 = vlib_get_buffer (vm, bi0);
117 bh0 = vlib_buffer_get_current (b0);
118 bti0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
119
120 /*
121 * default to drop so that if no bits are matched then
122 * that is where we go - DROP.
123 */
124 next0 = BIER_LOOKUP_NEXT_DROP;
125
126 /*
127 * At the imposition or input node,
128 * we stored the BIER Table index in the TX adjacency
129 */
130 bt0 = bier_table_get(vnet_buffer(b0)->ip.adj_index[VLIB_TX]);
131
132 /*
133 * we should only forward via one for the ECMP tables
134 */
135 ASSERT(!bier_table_is_main(bt0));
136
137 /*
138 * number of integer sized buckets
139 */
140 n_bytes = bier_hdr_len_id_to_num_buckets(bt0->bt_id.bti_hdr_len);
Neale Ranns91286372017-12-05 13:24:04 -0800141 vnet_buffer(b0)->mpls.bier.n_bytes = n_bytes;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700142 vnet_buffer(b0)->sw_if_index[VLIB_TX] = ~0;
143 num_buckets = n_bytes / sizeof(int);
144 bier_bit_string_init(&bbs,
145 bt0->bt_id.bti_hdr_len,
146 buckets_copy);
147 memcpy(bbs.bbs_buckets, bh0->bh_bit_string, bbs.bbs_len);
148
149 /*
150 * reset the fmask and clone storage vectors
151 */
152 vec_reset_length (blm->blm_fmasks[thread_index]);
153 vec_reset_length (blm->blm_clones[thread_index]);
154
155 /*
156 * Loop through the buckets in the header
157 */
158 for (index = 0; index < num_buckets; index++) {
159 /*
160 * loop through each bit in the bucket
161 */
162 bucket = ((int*)bbs.bbs_buckets)[index];
163
164 while (bucket) {
165 fbs = bier_find_first_bit_string_set(bucket);
166 fbs += (((num_buckets - 1) - index) *
167 BIER_BIT_MASK_BITS_PER_INT);
168
169 bfmi0 = bier_table_fwd_lookup(bt0, fbs);
170
171 /*
172 * whatever happens, the bit we just looked for
173 * MUST be cleared from the packet
174 * otherwise we could be in this loop a while ...
175 */
176 bier_bit_string_clear_bit(&bbs, fbs);
177
178 if (PREDICT_TRUE(INDEX_INVALID != bfmi0))
179 {
180 bfm0 = bier_fmask_get(bfmi0);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700181
182 /*
183 * use the bit-string on the fmask to reset
184 * the bits in the header we are walking
185 */
186 bier_bit_string_clear_string(
187 &bfm0->bfm_bits.bfmb_input_reset_string,
188 &bbs);
189 bucket = ((int*)bbs.bbs_buckets)[index];
190
191 /*
192 * the fmask is resolved so replicate a
193 * packet its way
194 */
195 next0 = BIER_LOOKUP_NEXT_OUTPUT;
196
197 vec_add1 (blm->blm_fmasks[thread_index], bfmi0);
198 } else {
199 /*
200 * go to the next bit-position set
201 */
Neale Rannsf0510722018-01-31 11:35:41 -0800202 vlib_node_increment_counter(
203 vm, node->node_index,
204 BIER_LOOKUP_ERROR_FMASK_UNRES, 1);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700205 bucket = ((int*)bbs.bbs_buckets)[index];
206 continue;
207 }
208 }
209 }
210
211 /*
212 * Full mask now processed.
213 * Create the number of clones we need based on the number
214 * of fmasks we are sending to.
215 */
Neale Rannsf0510722018-01-31 11:35:41 -0800216 u16 num_cloned, clone;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700217 u32 n_clones;
218
219 n_clones = vec_len(blm->blm_fmasks[thread_index]);
220
221 if (PREDICT_TRUE(0 != n_clones))
222 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700223 num_cloned = vlib_buffer_clone(vm, bi0,
224 blm->blm_clones[thread_index],
Neale Rannsf0510722018-01-31 11:35:41 -0800225 n_clones,
Damjan Marionbd0da972018-10-31 10:59:02 +0100226 VLIB_BUFFER_CLONE_HEAD_SIZE);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700227
Neale Rannsf0510722018-01-31 11:35:41 -0800228 if (num_cloned != vec_len(blm->blm_fmasks[thread_index]))
Neale Rannsd792d9c2017-10-21 10:53:20 -0700229 {
230 vlib_node_increment_counter
231 (vm, node->node_index,
232 BIER_LOOKUP_ERROR_BUFFER_ALLOCATION_FAILURE, 1);
233 }
234
235 for (clone = 0; clone < num_cloned; clone++)
236 {
237 vlib_buffer_t *c0;
238 u32 ci0;
239
240 ci0 = blm->blm_clones[thread_index][clone];
241 c0 = vlib_get_buffer(vm, ci0);
Neale Ranns91286372017-12-05 13:24:04 -0800242 vnet_buffer(c0)->ip.adj_index[VLIB_TX] =
243 blm->blm_fmasks[thread_index][clone];
Neale Rannsd792d9c2017-10-21 10:53:20 -0700244
245 to_next[0] = ci0;
246 to_next += 1;
247 n_left_to_next -= 1;
248
249 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
250 {
251 bier_lookup_trace_t *tr;
252
Neale Rannsd792d9c2017-10-21 10:53:20 -0700253 tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
254 tr->bt_index = bti0;
255 tr->bfm_index = blm->blm_fmasks[thread_index][clone];
Neale Rannsd792d9c2017-10-21 10:53:20 -0700256 }
257
258 vlib_validate_buffer_enqueue_x1(vm, node, next_index,
259 to_next, n_left_to_next,
260 ci0, next0);
261
262 /*
263 * After the enqueue it is possible that we over-flow the
264 * frame of the to-next node. When this happens we need to
265 * 'put' that full frame to the node and get a fresh empty
266 * one. Note that these are macros with side effects that
267 * change to_next & n_left_to_next
268 */
269 if (PREDICT_FALSE(0 == n_left_to_next))
270 {
271 vlib_put_next_frame (vm, node, next_index,
272 n_left_to_next);
273 vlib_get_next_frame (vm, node, next_index,
274 to_next, n_left_to_next);
275 }
276 }
277 }
278 else
279 {
280 /*
281 * no clones/replications required. drop this packet
282 */
283 next0 = BIER_LOOKUP_NEXT_DROP;
284 to_next[0] = bi0;
285 to_next += 1;
286 n_left_to_next -= 1;
287
288 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
289 {
290 bier_lookup_trace_t *tr;
291
292 tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
293
294 tr->bt_index = bti0;
295 tr->bfm_index = ~0;
296 }
297
298 vlib_validate_buffer_enqueue_x1(vm, node, next_index,
299 to_next, n_left_to_next,
300 bi0, next0);
301 }
302 }
303
Neale Rannsf0510722018-01-31 11:35:41 -0800304 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700305 }
306
Neale Rannsf0510722018-01-31 11:35:41 -0800307 vlib_node_increment_counter(vm, bier_lookup_node.index,
308 BIER_LOOKUP_ERROR_NONE,
309 from_frame->n_vectors);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700310 return (from_frame->n_vectors);
311}
312
313static u8 *
314format_bier_lookup_trace (u8 * s, va_list * args)
315{
316 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
317 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
318 bier_lookup_trace_t * t = va_arg (*args, bier_lookup_trace_t *);
319
320 s = format (s, "BIER: next [%d], tbl:%d BFM:%d",
321 t->next_index,
322 t->bt_index,
323 t->bfm_index);
324 return s;
325}
326
327VLIB_REGISTER_NODE (bier_lookup_node) = {
328 .function = bier_lookup,
329 .name = "bier-lookup",
330 /* Takes a vector of packets. */
331 .vector_size = sizeof (u32),
332
333 .n_errors = BIER_LOOKUP_N_ERROR,
334 .error_strings = bier_lookup_error_strings,
335
336 .format_trace = format_bier_lookup_trace,
337 .n_next_nodes = BIER_LOOKUP_N_NEXT,
338 .next_nodes = {
339 [BIER_LOOKUP_NEXT_DROP] = "bier-drop",
340 [BIER_LOOKUP_NEXT_OUTPUT] = "bier-output",
341 },
342};
343
344clib_error_t *
345bier_lookup_module_init (vlib_main_t * vm)
346{
347 bier_lookup_main_t *blm = &bier_lookup_main;
348 u32 thread_index;
349
350 vec_validate (blm->blm_clones, vlib_num_workers());
351 vec_validate (blm->blm_fmasks, vlib_num_workers());
352
353 for (thread_index = 0;
354 thread_index <= vlib_num_workers();
355 thread_index++)
356 {
357 /*
Neale Rannsf0510722018-01-31 11:35:41 -0800358 * 1024 is the most we will ever need to support
359 * a Bit-Mask length of 1024
Neale Rannsd792d9c2017-10-21 10:53:20 -0700360 */
Neale Rannsf0510722018-01-31 11:35:41 -0800361 vec_validate(blm->blm_fmasks[thread_index], 1023);
362 vec_validate(blm->blm_clones[thread_index], 1023);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700363 }
364
365 return 0;
366}
367
368VLIB_INIT_FUNCTION (bier_lookup_module_init);