FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
bier_lookup.c
Go to the documentation of this file.
1 /*
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>
21 #include <vnet/bier/bier_table.h>
22 #include <vnet/bier/bier_fmask.h>
23 
24 /**
25  * Struct maintaining the per-worker thread data for BIER lookups
26  */
27 typedef struct bier_lookup_main_t_
28 {
29  /* per-cpu vector of cloned packets */
31  /* per-cpu vector of BIER fmasks */
34 
35 /**
36  * Single instance of the lookup main
37  */
39 
40 static char * bier_lookup_error_strings[] = {
41 #define bier_error(n,s) s,
43 #undef bier_error
44 };
45 
46 /*
47  * Keep these values semantically the same as BIER lookup
48  */
49 #define foreach_bier_lookup_next \
50  _(DROP, "bier-drop") \
51  _(OUTPUT, "bier-output")
52 
53 typedef enum {
54 #define _(s,n) BIER_LOOKUP_NEXT_##s,
56 #undef _
59 
60 typedef enum {
61 #define bier_error(n,s) BIER_LOOKUP_ERROR_##n,
63 #undef bier_error
66 
68 
69 /**
70  * @brief Packet trace record for a BIER lookup
71  */
72 typedef struct bier_lookup_trace_t_
73 {
78 
79 static uword
83 {
84  u32 n_left_from, next_index, * from, * to_next;
88 
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 
98  to_next, n_left_to_next);
99 
100  while (n_left_from > 0 && n_left_to_next > 0)
101  {
102  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  */
141  vnet_buffer(b0)->mpls.bier.n_bytes = n_bytes;
142  vnet_buffer(b0)->sw_if_index[VLIB_TX] = ~0;
143  num_buckets = n_bytes / sizeof(int);
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 storage vector
151  */
153 
154  /*
155  * Loop through the buckets in the header
156  */
157  for (index = 0; index < num_buckets; index++) {
158  /*
159  * loop through each bit in the bucket
160  */
161  bucket = ((int*)bbs.bbs_buckets)[index];
162 
163  while (bucket) {
164  fbs = bier_find_first_bit_string_set(bucket);
165  fbs += (((num_buckets - 1) - index) *
167 
168  bfmi0 = bier_table_fwd_lookup(bt0, fbs);
169 
170  /*
171  * whatever happens, the bit we just looked for
172  * MUST be cleared from the packet
173  * otherwise we could be in this loop a while ...
174  */
175  bier_bit_string_clear_bit(&bbs, fbs);
176 
177  if (PREDICT_TRUE(INDEX_INVALID != bfmi0))
178  {
179  bfm0 = bier_fmask_get(bfmi0);
180 
181  /*
182  * use the bit-string on the fmask to reset
183  * the bits in the header we are walking
184  */
187  &bbs);
188  bucket = ((int*)bbs.bbs_buckets)[index];
189 
190  /*
191  * the fmask is resolved so replicate a
192  * packet its way
193  */
194  next0 = BIER_LOOKUP_NEXT_OUTPUT;
195 
196  vec_add1 (blm->blm_fmasks[thread_index], bfmi0);
197  } else {
198  /*
199  * go to the next bit-position set
200  */
202  vm, node->node_index,
203  BIER_LOOKUP_ERROR_FMASK_UNRES, 1);
204  bucket = ((int*)bbs.bbs_buckets)[index];
205  continue;
206  }
207  }
208  }
209 
210  /*
211  * Full mask now processed.
212  * Create the number of clones we need based on the number
213  * of fmasks we are sending to.
214  */
215  u16 num_cloned, clone;
216  u32 n_clones;
217 
218  n_clones = vec_len(blm->blm_fmasks[thread_index]);
219 
220  if (PREDICT_TRUE(0 != n_clones))
221  {
222  vec_set_len(blm->blm_clones[thread_index], n_clones);
223  num_cloned = vlib_buffer_clone(vm, bi0,
224  blm->blm_clones[thread_index],
225  n_clones,
227 
228 
229  if (num_cloned != n_clones)
230  {
231  vec_set_len(blm->blm_clones[thread_index], num_cloned);
233  (vm, node->node_index,
234  BIER_LOOKUP_ERROR_BUFFER_ALLOCATION_FAILURE, 1);
235  }
236 
237  for (clone = 0; clone < num_cloned; clone++)
238  {
239  vlib_buffer_t *c0;
240  u32 ci0;
241 
242  ci0 = blm->blm_clones[thread_index][clone];
243  c0 = vlib_get_buffer(vm, ci0);
244  vnet_buffer(c0)->ip.adj_index[VLIB_TX] =
245  blm->blm_fmasks[thread_index][clone];
246 
247  to_next[0] = ci0;
248  to_next += 1;
249  n_left_to_next -= 1;
250 
251  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
252  {
254 
255  tr = vlib_add_trace (vm, node, c0, sizeof (*tr));
256  tr->bt_index = bti0;
257  tr->bfm_index = blm->blm_fmasks[thread_index][clone];
258  }
259 
261  to_next, n_left_to_next,
262  ci0, next0);
263 
264  /*
265  * After the enqueue it is possible that we over-flow the
266  * frame of the to-next node. When this happens we need to
267  * 'put' that full frame to the node and get a fresh empty
268  * one. Note that these are macros with side effects that
269  * change to_next & n_left_to_next
270  */
271  if (PREDICT_FALSE(0 == n_left_to_next))
272  {
274  n_left_to_next);
276  to_next, n_left_to_next);
277  }
278  }
279  }
280  else
281  {
282  /*
283  * no clones/replications required. drop this packet
284  */
285  next0 = BIER_LOOKUP_NEXT_DROP;
286  to_next[0] = bi0;
287  to_next += 1;
288  n_left_to_next -= 1;
289 
290  if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
291  {
293 
294  tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
295 
296  tr->bt_index = bti0;
297  tr->bfm_index = ~0;
298  }
299 
301  to_next, n_left_to_next,
302  bi0, next0);
303  }
304  }
305 
306  vlib_put_next_frame(vm, node, next_index, n_left_to_next);
307  }
308 
310  BIER_LOOKUP_ERROR_NONE,
311  from_frame->n_vectors);
312  return (from_frame->n_vectors);
313 }
314 
315 static u8 *
316 format_bier_lookup_trace (u8 * s, va_list * args)
317 {
318  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
319  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
320  bier_lookup_trace_t * t = va_arg (*args, bier_lookup_trace_t *);
321 
322  s = format (s, "BIER: next [%d], tbl:%d BFM:%d",
323  t->next_index,
324  t->bt_index,
325  t->bfm_index);
326  return s;
327 }
328 
330  .function = bier_lookup,
331  .name = "bier-lookup",
332  /* Takes a vector of packets. */
333  .vector_size = sizeof (u32),
334 
335  .n_errors = BIER_LOOKUP_N_ERROR,
336  .error_strings = bier_lookup_error_strings,
337 
338  .format_trace = format_bier_lookup_trace,
339  .n_next_nodes = BIER_LOOKUP_N_NEXT,
340  .next_nodes = {
341  [BIER_LOOKUP_NEXT_DROP] = "bier-drop",
342  [BIER_LOOKUP_NEXT_OUTPUT] = "bier-output",
343  },
344 };
345 
346 clib_error_t *
348 {
351 
354 
355  for (thread_index = 0;
357  thread_index++)
358  {
359  /*
360  * 1024 is the most we will ever need to support
361  * a Bit-Mask length of 1024
362  */
363  vec_validate(blm->blm_fmasks[thread_index], 1023);
364  vec_validate(blm->blm_clones[thread_index], 1023);
365  }
366 
367  return 0;
368 }
369 
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
bier_bit_string_clear_string
static void bier_bit_string_clear_string(const bier_bit_string_t *src, bier_bit_string_t *dest)
Definition: bier_bit_string.h:69
vlib_num_workers
static u32 vlib_num_workers()
Definition: threads.h:333
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:495
foreach_bier_lookup_next
#define foreach_bier_lookup_next
Definition: bier_lookup.c:49
bier_lookup_trace_t_::next_index
u32 next_index
Definition: bier_lookup.c:74
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
bier_fmask_bits_t_::bfmb_input_reset_string
bier_bit_string_t bfmb_input_reset_string
each bit in the mask needs to be reference counted and set/cleared on the 0->1 and 1->0 transitions.
Definition: bier_fmask.h:50
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
buffer.h
bier_lookup_trace_t_::bt_index
index_t bt_index
Definition: bier_lookup.c:75
bier_lookup_main_t
struct bier_lookup_main_t_ bier_lookup_main_t
Struct maintaining the per-worker thread data for BIER lookups.
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
bier_bit_string_clear_bit
void bier_bit_string_clear_bit(bier_bit_string_t *bit_string, bier_bp_t bp)
Definition: bier_bit_string.c:66
bier_hdr_len_id_to_num_buckets
u32 bier_hdr_len_id_to_num_buckets(bier_hdr_len_id_t id)
Conversion functions for the enumerated bit-string length values, to bit and bytes.
Definition: bier_types.c:60
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
bier_hdr_inlines.h
VLIB_BUFFER_CLONE_HEAD_SIZE
#define VLIB_BUFFER_CLONE_HEAD_SIZE
Definition: buffer.h:61
from_frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * from_frame
Definition: esp_encrypt.c:1328
bier_bp_t
u32 bier_bp_t
A bit positon as assigned to egress PEs.
Definition: bier_types.h:294
vlib_frame_t
Definition: node.h:372
bier_lookup_module_init
clib_error_t * bier_lookup_module_init(vlib_main_t *vm)
Definition: bier_lookup.c:347
bier_lookup_trace_t_
Packet trace record for a BIER lookup.
Definition: bier_lookup.c:72
bier_bit_string_t_::bbs_len
u16 bbs_len
The length of the string in BYTES.
Definition: bier_types.h:282
bier_table.h
bier_bit_string_init
static void bier_bit_string_init(bier_bit_string_t *bbs, bier_hdr_len_id_t len, bier_bit_mask_bucket_t *buckets)
Definition: bier_bit_string.h:99
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
bier_lookup
static uword bier_lookup(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: bier_lookup.c:80
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
bier_table_is_main
int bier_table_is_main(const bier_table_t *bt)
Definition: bier_table.c:53
BIER_LOOKUP_N_ERROR
@ BIER_LOOKUP_N_ERROR
Definition: bier_lookup.c:64
bier_bit_mask_bucket_t
u8 bier_bit_mask_bucket_t
A bucket is a byte.
Definition: bier_types.h:257
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:441
bier_lookup_main_t_::blm_clones
u32 ** blm_clones
Definition: bier_lookup.c:30
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_get_thread_index
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:187
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
index_t
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:43
uword
u64 uword
Definition: types.h:112
bier_lookup_error_t
bier_lookup_error_t
Definition: bier_lookup.c:60
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
bier_table_get
static bier_table_t * bier_table_get(index_t bti)
Definition: bier_table.h:160
vlib_buffer_clone
static u16 vlib_buffer_clone(vlib_main_t *vm, u32 src_buffer, u32 *buffers, u16 n_buffers, u16 head_end_offset)
Create multiple clones of buffer and store them in the supplied array.
Definition: buffer_funcs.h:1325
bier_lookup_next_t
bier_lookup_next_t
Definition: bier_lookup.c:53
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
bier_table_id_t_::bti_hdr_len
bier_hdr_len_id_t bti_hdr_len
The size of the bit string processed by this table.
Definition: bier_types.h:419
bier_lookup_error_strings
static char * bier_lookup_error_strings[]
Definition: bier_lookup.c:40
BIER_HDR_BUCKETS_4096
@ BIER_HDR_BUCKETS_4096
Definition: bier_types.h:174
vec_set_len
#define vec_set_len(v, l)
Set vector length to a user-defined value.
Definition: vec_bootstrap.h:179
BIER_LOOKUP_N_NEXT
@ BIER_LOOKUP_N_NEXT
Definition: bier_lookup.c:57
vlib_validate_buffer_enqueue_x1
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
bier_lookup_main
static bier_lookup_main_t bier_lookup_main
Single instance of the lookup main.
Definition: bier_lookup.c:38
index
u32 index
Definition: flow_types.api:221
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
n_bytes
u32 n_bytes
Definition: interface_output.c:421
bier_lookup_main_t_::blm_fmasks
u32 ** blm_fmasks
Definition: bier_lookup.c:32
bier_lookup_trace_t
struct bier_lookup_trace_t_ bier_lookup_trace_t
Packet trace record for a BIER lookup.
bier_table_t_::bt_id
bier_table_id_t bt_id
The identity/key or the table.
Definition: bier_table.h:76
bier_lookup_main_t_
Struct maintaining the per-worker thread data for BIER lookups.
Definition: bier_lookup.c:27
bier_bit_string_t_::bbs_buckets
bier_bit_mask_bucket_t * bbs_buckets
The buckets in the string.
Definition: bier_types.h:287
vlib_main_t
Definition: main.h:102
bier_fmask_t_::bfm_bits
bier_fmask_bits_t bfm_bits
The bits, and their ref counts, that are set on this mask This mask changes as BIER entries link to a...
Definition: bier_fmask.h:119
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
bier_lookup_error.def
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ip
vl_api_address_t ip
Definition: l2.api:558
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
bier_lookup_trace_t_::bfm_index
index_t bfm_index
Definition: bier_lookup.c:76
BIER_BIT_MASK_BITS_PER_INT
#define BIER_BIT_MASK_BITS_PER_INT
Definition: bier_bit_string.h:27
bier_fmask.h
bier_hdr_t_
A BIER header of variable length The encoding follows: https://tools.ietf.org/html/draft-ietf-bier-mp...
Definition: bier_types.h:321
bier_lookup_node
vlib_node_registration_t bier_lookup_node
(constructor) VLIB_REGISTER_NODE (bier_lookup_node)
Definition: bier_lookup.c:67
bier_table_t_
A BIER Table is the bit-indexed forwarding table.
Definition: bier_table.h:38
bier_hdr_t_::bh_bit_string
bier_bit_mask_bucket_t bh_bit_string[0]
The variable length bit-string.
Definition: bier_types.h:359
vnet.h
vlib_node_runtime_t
Definition: node.h:454
bier_fmask_t_
An outgoing BIER mask.
Definition: bier_fmask.h:99
from
from
Definition: nat44_ei_hairpinning.c:415
format_bier_lookup_trace
static u8 * format_bier_lookup_trace(u8 *s, va_list *args)
Definition: bier_lookup.c:316
INDEX_INVALID
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:49
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
vlib_get_next_frame
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:395
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
bier_bit_string_t_
A Variable length BitString.
Definition: bier_types.h:278
bier_fmask_get
static bier_fmask_t * bier_fmask_get(u32 index)
Definition: bier_fmask.h:177
bier_find_first_bit_string_set
static int bier_find_first_bit_string_set(int mask)
Definition: bier_bit_string.h:35
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
bier_table_fwd_lookup
static const index_t bier_table_fwd_lookup(const bier_table_t *bt, bier_bp_t bp)
Definition: bier_table.h:173
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169