BIER API and load-balancing fixes
[vpp.git] / src / vnet / bier / bier_test.c
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
17 #include <vnet/mpls/mpls.h>
18 #include <vnet/bier/bier_table.h>
19 #include <vnet/bier/bier_entry.h>
20 #include <vnet/bier/bier_fmask.h>
21 #include <vnet/bier/bier_bit_string.h>
22 #include <vnet/bier/bier_imp.h>
23 #include <vnet/bier/bier_disp_table.h>
24 #include <vnet/bier/bier_disp_entry.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/mpls_fib.h>
28 #include <vnet/dpo/load_balance.h>
29 #include <vnet/dpo/drop_dpo.h>
30 #include <vnet/dpo/lookup_dpo.h>
31 #include <vnet/mfib/mfib_table.h>
32
33 #include <vnet/fib/fib_test.h>
34
35 /*
36  * Add debugs for passing tests
37  */
38 static int bier_test_do_debug;
39
40 #define BIER_TEST_I(_cond, _comment, _args...)                  \
41 ({                                                              \
42     int _evald = (_cond);                                       \
43     if (!(_evald)) {                                            \
44         fformat(stderr, "FAIL:%d: " _comment "\n",              \
45                 __LINE__, ##_args);                             \
46         res = 1;                                                \
47     } else {                                                    \
48         if (bier_test_do_debug)                                 \
49             fformat(stderr, "PASS:%d: " _comment "\n",          \
50                     __LINE__, ##_args);                         \
51     }                                                           \
52     res;                                                        \
53 })
54 #define BIER_TEST(_cond, _comment, _args...)                    \
55 {                                                               \
56     if (BIER_TEST_I(_cond, _comment, ##_args)) {                \
57         return 1;                                               \
58         ASSERT(!("FAIL: " _comment));                           \
59     }                                                           \
60 }
61
62 /**
63  * A 'i'm not fussed is this is not efficient' store of test data
64  */
65 typedef struct test_main_t_ {
66     /**
67      * HW if indicies
68      */
69     u32 hw_if_indicies[4];
70     /**
71      * HW interfaces
72      */
73     vnet_hw_interface_t * hw[4];
74
75 } test_main_t;
76 static test_main_t test_main;
77
78 /* fake ethernet device class, distinct from "fake-ethX" */
79 static u8 * format_test_interface_name (u8 * s, va_list * args)
80 {
81   u32 dev_instance = va_arg (*args, u32);
82   return format (s, "test-eth%d", dev_instance);
83 }
84
85 static uword dummy_interface_tx (vlib_main_t * vm,
86                                  vlib_node_runtime_t * node,
87                                  vlib_frame_t * frame)
88 {
89   clib_warning ("you shouldn't be here, leaking buffers...");
90   return frame->n_vectors;
91 }
92
93 VNET_DEVICE_CLASS (test_interface_device_class,static) = {
94   .name = "Test interface",
95   .format_device_name = format_test_interface_name,
96   .tx_function = dummy_interface_tx,
97 };
98
99 static u8 *hw_address;
100
101 static int
102 bier_test_mk_intf (u32 ninterfaces)
103 {
104     clib_error_t * error = NULL;
105     test_main_t *tm = &test_main;
106     u8 byte;
107     int res;
108     u32 i;
109
110     res = 0;
111     ASSERT(ninterfaces <= ARRAY_LEN(tm->hw_if_indicies));
112
113     for (i=0; i<6; i++)
114     {
115         byte = 0xd0+i;
116         vec_add1(hw_address, byte);
117     }
118
119     for (i = 0; i < ninterfaces; i++)
120     {
121         hw_address[5] = i;
122
123         error = ethernet_register_interface(vnet_get_main(),
124                                             test_interface_device_class.index,
125                                             i /* instance */,
126                                             hw_address,
127                                             &tm->hw_if_indicies[i],
128                                             /* flag change */ 0);
129
130         BIER_TEST((NULL == error), "ADD interface %d", i);
131
132         tm->hw[i] = vnet_get_hw_interface(vnet_get_main(),
133                                           tm->hw_if_indicies[i]);
134         vec_validate (ip4_main.fib_index_by_sw_if_index, tm->hw[i]->sw_if_index);
135         vec_validate (ip6_main.fib_index_by_sw_if_index, tm->hw[i]->sw_if_index);
136         ip4_main.fib_index_by_sw_if_index[tm->hw[i]->sw_if_index] = 0;
137         ip6_main.fib_index_by_sw_if_index[tm->hw[i]->sw_if_index] = 0;
138         error = vnet_sw_interface_set_flags(vnet_get_main(),
139                                             tm->hw[i]->sw_if_index,
140                                             VNET_SW_INTERFACE_FLAG_ADMIN_UP);
141         BIER_TEST((NULL == error), "UP interface %d", i);
142     }
143     /*
144      * re-eval after the inevitable realloc
145      */
146     for (i = 0; i < ninterfaces; i++)
147     {
148         tm->hw[i] = vnet_get_hw_interface(vnet_get_main(),
149                                           tm->hw_if_indicies[i]);
150     }
151
152     return (res);
153 }
154
155 #define BIER_TEST_LB(_cond, _comment, _args...)                 \
156 {                                                               \
157     if (BIER_TEST_I(_cond, _comment, ##_args)) {                \
158         return (1);                                             \
159     }                                                           \
160 }
161
162 static int
163 bier_test_validate_entry (index_t bei,
164                           int n_buckets,
165                           ...)
166 {
167     dpo_id_t dpo = DPO_INVALID;
168     const load_balance_t *lb;
169     va_list ap;
170     int res;
171
172     va_start(ap, n_buckets);
173
174     res = 0;
175     bier_entry_contribute_forwarding(bei, &dpo);
176
177     res = BIER_TEST_I((DPO_LOAD_BALANCE == dpo.dpoi_type),
178                       "Entry links to %U",
179                       format_dpo_type, dpo.dpoi_type);
180
181     if (!res)
182     {
183         lb = load_balance_get(dpo.dpoi_index);
184         res = fib_test_validate_lb_v(lb, n_buckets, &ap);
185     }
186
187     dpo_reset(&dpo);
188     va_end(ap);
189
190     return (res);
191 }
192
193 static int
194 bier_test_mpls_spf (void)
195 {
196     fib_node_index_t lfei, fei, bti;
197     u32 mpls_fib_index;
198     test_main_t *tm;
199     int lb_count;
200     int res;
201
202     res = 0;
203     lb_count = pool_elts(load_balance_pool);
204     tm = &test_main;
205 #define N_BIER_ECMP_TABLES 16
206     int ii;
207
208     /*
209      * Add the BIER Main table
210      */
211     const bier_table_id_t bt_0_0_0_256 = {
212         .bti_set = 0,
213         .bti_sub_domain = 0,
214         .bti_hdr_len = BIER_HDR_LEN_256,
215         .bti_type = BIER_TABLE_MPLS_SPF,
216         .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
217     };
218
219     bti = bier_table_add_or_lock(&bt_0_0_0_256, 1600);
220
221     fib_test_lb_bucket_t l_o_bt[N_BIER_ECMP_TABLES];
222     bier_table_id_t bt_ecmp_0_0_0_256 = bt_0_0_0_256;
223
224     for (ii = 0; ii < N_BIER_ECMP_TABLES; ii++)
225     {
226         bt_ecmp_0_0_0_256.bti_ecmp = ii;
227
228         l_o_bt[ii].type = FT_LB_BIER_TABLE;
229         l_o_bt[ii].bier.table =
230             bier_table_ecmp_create_and_lock(&bt_ecmp_0_0_0_256);
231     };
232     const fib_prefix_t pfx_1600_neos = {
233         .fp_len = 21,
234         .fp_proto = FIB_PROTOCOL_MPLS,
235         .fp_label = 1600,
236         .fp_eos = MPLS_NON_EOS,
237         .fp_payload_proto = DPO_PROTO_BIER,
238     };
239     const fib_prefix_t pfx_1600_eos = {
240         .fp_len = 21,
241         .fp_proto = FIB_PROTOCOL_MPLS,
242         .fp_label = 1600,
243         .fp_eos = MPLS_EOS,
244         .fp_payload_proto = DPO_PROTO_BIER,
245     };
246
247     mpls_fib_index = fib_table_find(FIB_PROTOCOL_MPLS,
248                                     MPLS_FIB_DEFAULT_TABLE_ID);
249
250     lfei = fib_table_lookup(mpls_fib_index, &pfx_1600_neos);
251     BIER_TEST(FIB_NODE_INDEX_INVALID == lfei, "1600/0 is not present");
252
253     lfei = fib_table_lookup(mpls_fib_index, &pfx_1600_eos);
254     BIER_TEST(!fib_test_validate_entry(lfei, FIB_FORW_CHAIN_TYPE_MPLS_EOS,
255                                        16,
256                                        &l_o_bt[0],
257                                        &l_o_bt[1],
258                                        &l_o_bt[2],
259                                        &l_o_bt[3],
260                                        &l_o_bt[4],
261                                        &l_o_bt[5],
262                                        &l_o_bt[6],
263                                        &l_o_bt[7],
264                                        &l_o_bt[8],
265                                        &l_o_bt[9],
266                                        &l_o_bt[10],
267                                        &l_o_bt[11],
268                                        &l_o_bt[12],
269                                        &l_o_bt[13],
270                                        &l_o_bt[14],
271                                        &l_o_bt[15]),
272               "1600/1 LB stacks on BIER table %d", bti);
273
274     /*
275      * modify the table's local label - keep the lock count accurate
276      */
277     const fib_prefix_t pfx_1601_eos = {
278         .fp_len = 21,
279         .fp_proto = FIB_PROTOCOL_MPLS,
280         .fp_label = 1601,
281         .fp_eos = MPLS_EOS,
282         .fp_payload_proto = DPO_PROTO_BIER,
283     };
284     bti = bier_table_add_or_lock(&bt_0_0_0_256, 1601);
285     bier_table_unlock(&bt_0_0_0_256);
286
287     lfei = fib_table_lookup(mpls_fib_index, &pfx_1600_eos);
288     BIER_TEST(FIB_NODE_INDEX_INVALID == lfei, "1600/1 is deleted");
289
290     lfei = fib_table_lookup(mpls_fib_index, &pfx_1601_eos);
291     BIER_TEST(!fib_test_validate_entry(lfei, FIB_FORW_CHAIN_TYPE_MPLS_EOS,
292                                        16,
293                                        &l_o_bt[0],
294                                        &l_o_bt[1],
295                                        &l_o_bt[2],
296                                        &l_o_bt[3],
297                                        &l_o_bt[4],
298                                        &l_o_bt[5],
299                                        &l_o_bt[6],
300                                        &l_o_bt[7],
301                                        &l_o_bt[8],
302                                        &l_o_bt[9],
303                                        &l_o_bt[10],
304                                        &l_o_bt[11],
305                                        &l_o_bt[12],
306                                        &l_o_bt[13],
307                                        &l_o_bt[14],
308                                        &l_o_bt[15]),
309               "1601/1 LB stacks on BIER table %d", bti);
310
311     /*
312      * add a route to the table. the via IP route does not exist.
313      */
314     const ip46_address_t nh_1_1_1_1 = {
315         .ip4 = {
316             .as_u32 = clib_host_to_net_u32(0x01010101),
317         },
318     };
319     fib_route_path_t *paths_1_1_1_1 = NULL, *input_paths_1_1_1_1;
320     fib_route_path_t path_1_1_1_1 = {
321         .frp_addr = nh_1_1_1_1,
322         .frp_bier_fib_index = bti,
323         .frp_sw_if_index = ~0,
324     };
325     fib_mpls_label_t fml_500 = {
326         .fml_value = 500,
327     };
328     vec_add1(path_1_1_1_1.frp_label_stack, fml_500);
329     vec_add1(paths_1_1_1_1, path_1_1_1_1);
330     const fib_prefix_t pfx_1_1_1_1_s_32 = {
331         .fp_addr = nh_1_1_1_1,
332         .fp_len = 32,
333         .fp_proto = FIB_PROTOCOL_IP4,
334     };
335     index_t bei_1;
336
337     input_paths_1_1_1_1 = vec_dup(paths_1_1_1_1);
338     bier_table_route_path_add(&bt_0_0_0_256, 1, input_paths_1_1_1_1);
339     bei_1 = bier_table_lookup(bier_table_get(bti), 1);
340
341     BIER_TEST((INDEX_INVALID != bei_1), "BP:1 present");
342
343     /*
344      * the newly created fmask should stack on the non-eos chain
345      * of the via-fib-entry
346      */
347     dpo_id_t neos_dpo_1_1_1_1 = DPO_INVALID;
348     bier_fmask_t *bfm_1_1_1_1;
349     index_t bfmi_1_1_1_1;
350
351     fei = fib_table_lookup_exact_match(0, &pfx_1_1_1_1_s_32);
352     fib_entry_contribute_forwarding(fei,
353                                     FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
354                                     &neos_dpo_1_1_1_1);
355
356     bfmi_1_1_1_1 = bier_fmask_db_find(bti, &path_1_1_1_1);
357     bfm_1_1_1_1 = bier_fmask_get(bfmi_1_1_1_1);
358
359     BIER_TEST(!dpo_cmp(drop_dpo_get(DPO_PROTO_MPLS),
360                        &bfm_1_1_1_1->bfm_dpo),
361               "Fmask via 1.1.1.1 stacks on MPLS drop");
362
363     /*
364      * The BIER entry should stack on the forwarding chain of the fmask
365      */
366     const fib_test_lb_bucket_t dpo_o_bfm_1_1_1_1 = {
367         .type = FT_LB_BIER_FMASK,
368         .bier = {
369             .fmask = bfmi_1_1_1_1,
370         },
371     };
372     dpo_id_t dpo_bei = DPO_INVALID;
373     bier_entry_contribute_forwarding(bei_1, &dpo_bei);
374
375     BIER_TEST(!dpo_cmp(&dpo_bei, drop_dpo_get(DPO_PROTO_BIER)),
376               "BP:1 stacks on bier drop");
377
378     /*
379      * give 1.1.1.1/32 a path and hence a interesting n-eos chain
380      */
381     ip46_address_t nh_10_10_10_1 = {
382         .ip4 = {
383             .as_u32 = clib_host_to_net_u32(0x0a0a0a01),
384         },
385     };
386     adj_index_t ai_mpls_10_10_10_1;
387     ai_mpls_10_10_10_1 = adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
388                                              VNET_LINK_MPLS,
389                                              &nh_10_10_10_1,
390                                              tm->hw[0]->sw_if_index);
391
392     fib_test_lb_bucket_t bucket_neos_99_via_10_10_10_1 = {
393         .type = FT_LB_LABEL_O_ADJ,
394         .label_o_adj = {
395             .label = 99,
396             .eos = MPLS_NON_EOS,
397             .adj = ai_mpls_10_10_10_1,
398             .ttl = 255,
399         },
400     };
401     fib_mpls_label_t *out_lbl_99 = NULL, fml_99 = {
402         .fml_value = 99,
403     };
404     vec_add1(out_lbl_99, fml_99);
405
406     fei = fib_table_entry_update_one_path(0,
407                                           &pfx_1_1_1_1_s_32,
408                                           FIB_SOURCE_API,
409                                           FIB_ENTRY_FLAG_NONE,
410                                           DPO_PROTO_IP4,
411                                           &nh_10_10_10_1,
412                                           tm->hw[0]->sw_if_index,
413                                           ~0, // invalid fib index
414                                           1,
415                                           out_lbl_99,
416                                           FIB_ROUTE_PATH_FLAG_NONE);
417     fib_entry_contribute_forwarding(fei,
418                                     FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
419                                     &neos_dpo_1_1_1_1);
420     BIER_TEST(!fib_test_validate_lb(&neos_dpo_1_1_1_1, 1,
421                                     &bucket_neos_99_via_10_10_10_1),
422               "1.1.1.1/32 n-eos LB 1 buckets via: 99 + 10.10.10.1");
423     BIER_TEST(!dpo_cmp(&neos_dpo_1_1_1_1,
424                        &bfm_1_1_1_1->bfm_dpo),
425               "Fmask via 1.1.1.1 stacks on updated non-eos of 1.1.1.1/32");
426     bier_entry_contribute_forwarding(bei_1, &dpo_bei);
427     BIER_TEST((dpo_bei.dpoi_index == bfmi_1_1_1_1),
428               "BP:1  stacks on fmask 1.1.1.1");
429
430     /*
431      * add another path to the via entry.
432      * this makes the via-entry instantiate a new load-balance with
433      * 2 buckets. and the back-walk to the BIER entry will need to
434      * re-stack on it.
435      */
436     ip46_address_t nh_10_10_10_2 = {
437         .ip4 = {
438             .as_u32 = clib_host_to_net_u32(0x0a0a0a02),
439         },
440     };
441     adj_index_t ai_mpls_10_10_10_2;
442
443     ai_mpls_10_10_10_2 = adj_nbr_add_or_lock(FIB_PROTOCOL_IP4,
444                                              VNET_LINK_MPLS,
445                                              &nh_10_10_10_2,
446                                              tm->hw[0]->sw_if_index);
447
448     fib_test_lb_bucket_t bucket_neos_100_via_10_10_10_2 = {
449         .type = FT_LB_LABEL_O_ADJ,
450         .label_o_adj = {
451             .label = 100,
452             .eos = MPLS_NON_EOS,
453             .adj = ai_mpls_10_10_10_2,
454             .ttl = 255,
455         },
456     };
457     fib_mpls_label_t *out_lbl_100 = NULL, fml_100 = {
458         .fml_value = 100,
459     };
460     vec_add1(out_lbl_100, fml_100);
461
462     fei = fib_table_entry_path_add(0,
463                                    &pfx_1_1_1_1_s_32,
464                                    FIB_SOURCE_API,
465                                    FIB_ENTRY_FLAG_NONE,
466                                    DPO_PROTO_IP4,
467                                    &nh_10_10_10_2,
468                                    tm->hw[0]->sw_if_index,
469                                    ~0, // invalid fib index
470                                    1,
471                                    out_lbl_100,
472                                    FIB_ROUTE_PATH_FLAG_NONE);
473
474     fib_entry_contribute_forwarding(fei,
475                                     FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
476                                     &neos_dpo_1_1_1_1);
477     BIER_TEST(!fib_test_validate_lb(&neos_dpo_1_1_1_1, 2,
478                                     &bucket_neos_99_via_10_10_10_1,
479                                     &bucket_neos_100_via_10_10_10_2),
480               "1.1.1.1/32 n-eos LB 2 buckets "
481               "via: 99 + 10.10.10.1, "
482               "via: 100 + 10.10.10.2");
483     BIER_TEST(!dpo_cmp(&neos_dpo_1_1_1_1,
484                        &bfm_1_1_1_1->bfm_dpo),
485               "Fmask via 1.1.1.1 stacks on updated non-eos of 1.1.1.1/32");
486
487     /*
488      * add another bier bit-position via the same next-hop
489      * since its the same next hop, the two bit-positions should link
490      * to the same fmask
491      */
492     index_t bei_2;
493
494     input_paths_1_1_1_1 = vec_dup(paths_1_1_1_1);
495     bier_table_route_path_add(&bt_0_0_0_256, 2, input_paths_1_1_1_1);
496     bei_2 = bier_table_lookup(bier_table_get(bti), 2);
497
498     bier_entry_contribute_forwarding(bei_2, &dpo_bei);
499     BIER_TEST((dpo_bei.dpoi_index == bfmi_1_1_1_1),
500               "BP:2  stacks on fmask 1.1.1.1");
501
502     /*
503      * now add a bit-position via a different next hop and expect to
504      * link via a different fmask
505      */
506     const ip46_address_t nh_1_1_1_2 = {
507         .ip4 = {
508             .as_u32 = clib_host_to_net_u32(0x01010102),
509         },
510     };
511     const fib_prefix_t pfx_1_1_1_2_s_32 = {
512         .fp_addr = nh_1_1_1_2,
513         .fp_len = 32,
514         .fp_proto = FIB_PROTOCOL_IP4,
515     };
516     fib_route_path_t *paths_1_1_1_2 = NULL, *input_paths_1_1_1_2, path_1_1_1_2 = {
517         .frp_addr = nh_1_1_1_2,
518         .frp_bier_fib_index = bti,
519         .frp_sw_if_index = ~0,
520     };
521     fib_mpls_label_t fml_501 = {
522         .fml_value = 501,
523     };
524     vec_add1(path_1_1_1_2.frp_label_stack, fml_501);
525     vec_add1(paths_1_1_1_2, path_1_1_1_2);
526     input_paths_1_1_1_2 = vec_dup(paths_1_1_1_2);
527     index_t bei_3;
528
529     fib_mpls_label_t *out_lbl_101 = NULL, fml_101 = {
530         .fml_value = 101,
531     };
532     vec_add1(out_lbl_101, fml_101);
533     fei = fib_table_entry_path_add(0,
534                                    &pfx_1_1_1_2_s_32,
535                                    FIB_SOURCE_API,
536                                    FIB_ENTRY_FLAG_NONE,
537                                    DPO_PROTO_IP4,
538                                    &nh_10_10_10_2,
539                                    tm->hw[0]->sw_if_index,
540                                    ~0, // invalid fib index
541                                    1,
542                                    out_lbl_101,
543                                    FIB_ROUTE_PATH_FLAG_NONE);
544     bier_table_route_path_update(&bt_0_0_0_256, 3, input_paths_1_1_1_2);
545     bei_3 = bier_table_lookup(bier_table_get(bti), 3);
546
547     BIER_TEST((INDEX_INVALID != bei_3), "BP:3 present");
548
549     /*
550      * the newly created fmask should stack on the non-eos chain
551      * of the via-fib-entry
552      */
553     dpo_id_t neos_dpo_1_1_1_2 = DPO_INVALID;
554     bier_fmask_t *bfm_1_1_1_2;
555     index_t bfmi_1_1_1_2;
556
557     fei = fib_table_lookup_exact_match(0, &pfx_1_1_1_2_s_32);
558     fib_entry_contribute_forwarding(fei,
559                                     FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
560                                     &neos_dpo_1_1_1_2);
561
562     bfmi_1_1_1_2 = bier_fmask_db_find(bti, &path_1_1_1_2);
563     bfm_1_1_1_2 = bier_fmask_get(bfmi_1_1_1_2);
564
565     BIER_TEST(!dpo_cmp(&neos_dpo_1_1_1_2,
566                        &bfm_1_1_1_2->bfm_dpo),
567               "Fmask via 1.1.1.2 stacks on non-eos of 1.1.1.2/32");
568
569     /*
570      * The BIER entry should stack on the forwarding chain of the fmask
571      */
572     const fib_test_lb_bucket_t dpo_o_bfm_1_1_1_2 = {
573         .type = FT_LB_BIER_FMASK,
574         .bier = {
575             .fmask = bfmi_1_1_1_2,
576         },
577     };
578     bier_entry_contribute_forwarding(bei_3, &dpo_bei);
579     BIER_TEST((dpo_bei.dpoi_index == bfmi_1_1_1_2),
580               "BP:2 stacks on fmask 1.1.1.2");
581
582     /*
583      * Load-balance BP:3 over both next-hops
584      */
585     paths_1_1_1_1[0] = path_1_1_1_1;
586     input_paths_1_1_1_1 = vec_dup(paths_1_1_1_1);
587     bier_table_route_path_add(&bt_0_0_0_256, 3, input_paths_1_1_1_1);
588
589     BIER_TEST(!bier_test_validate_entry(bei_3, 2,
590                                         &dpo_o_bfm_1_1_1_1,
591                                         &dpo_o_bfm_1_1_1_2),
592               "BP:3 stacks on fmask 1.1.1.2 & 1.1.1.1");
593
594     /*
595      * test that the ECMP choices for BP:3 have been spread over the
596      * ECMP tables
597      */
598     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[0].bier.table), 3) ==
599                bfmi_1_1_1_1),
600               "fwd lookup for BP:3 ECMP:0 is 1.1.1.1");
601     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[1].bier.table), 3) ==
602                bfmi_1_1_1_2),
603               "fwd lookup for BP:3 ECMP:1 is 1.1.1.2");
604
605     /*
606      * Withdraw one of the via FIB and thus bring down the fmask
607      * expect the bier-entry forwarding to remove this from the set
608      */
609     fib_table_entry_delete(0, &pfx_1_1_1_2_s_32, FIB_SOURCE_API);
610
611     bier_entry_contribute_forwarding(bei_3, &dpo_bei);
612     BIER_TEST((dpo_bei.dpoi_index == bfmi_1_1_1_1),
613               "BP:3 stacks on fmask 1.1.1.1");
614
615     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[0].bier.table), 3) ==
616                bfmi_1_1_1_1),
617               "fwd lookup for BP:3 ECMP:0 is 1.1.1.1");
618     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[1].bier.table), 3) ==
619                bfmi_1_1_1_1),
620               "fwd lookup for BP:3 ECMP:1 is 1.1.1.1");
621
622     /*
623      * add the via back
624      */
625     out_lbl_101 = NULL;
626     vec_add1(out_lbl_101, fml_101);
627     fei = fib_table_entry_path_add(0,
628                                    &pfx_1_1_1_2_s_32,
629                                    FIB_SOURCE_API,
630                                    FIB_ENTRY_FLAG_NONE,
631                                    DPO_PROTO_IP4,
632                                    &nh_10_10_10_2,
633                                    tm->hw[0]->sw_if_index,
634                                    ~0, // invalid fib index
635                                    1,
636                                    out_lbl_101,
637                                    FIB_ROUTE_PATH_FLAG_NONE);
638     /* suspend so the update walk kicks int */
639     vlib_process_suspend(vlib_get_main(), 1e-5);
640
641     BIER_TEST(!bier_test_validate_entry(bei_3, 2,
642                                         &dpo_o_bfm_1_1_1_1,
643                                         &dpo_o_bfm_1_1_1_2),
644               "BP:3 stacks on fmask 1.1.1.2 & 1.1.1.1");
645     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[0].bier.table), 3) ==
646                bfmi_1_1_1_1),
647               "fwd lookup for BP:3 ECMP:0 is 1.1.1.1");
648     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[1].bier.table), 3) ==
649                bfmi_1_1_1_2),
650               "fwd lookup for BP:3 ECMP:1 is 1.1.1.2");
651
652     /*
653      * remove the original 1.1.1.2 fmask from BP:3
654      */
655     input_paths_1_1_1_2 = vec_dup(paths_1_1_1_2);
656     bier_table_route_path_remove(&bt_0_0_0_256, 3, input_paths_1_1_1_2);
657     bier_entry_contribute_forwarding(bei_3, &dpo_bei);
658     BIER_TEST((dpo_bei.dpoi_index == bfmi_1_1_1_1),
659               "BP:3 stacks on fmask 1.1.1.1");
660
661     /*
662      * test that the ECMP choices for BP:3 have been updated
663      */
664     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[0].bier.table), 3) ==
665                bfmi_1_1_1_1),
666               "fwd lookup for BP:3 ECMP:0 is 1.1.1.1");
667     BIER_TEST((bier_table_fwd_lookup(bier_table_get(l_o_bt[1].bier.table), 3) ==
668                bfmi_1_1_1_1),
669               "fwd lookup for BP:3 ECMP:1 is 1.1.1.1");
670
671     /*
672      * remove the routes added
673      */
674     input_paths_1_1_1_1 = vec_dup(paths_1_1_1_1);
675     bier_table_route_path_remove(&bt_0_0_0_256, 2, input_paths_1_1_1_1);
676     input_paths_1_1_1_2 = vec_dup(paths_1_1_1_2);
677     bier_table_route_path_remove(&bt_0_0_0_256, 3, input_paths_1_1_1_2);
678     input_paths_1_1_1_1 = vec_dup(paths_1_1_1_1);
679     bier_table_route_path_remove(&bt_0_0_0_256, 3, input_paths_1_1_1_1);
680
681     bier_table_route_delete(&bt_0_0_0_256, 1);
682
683     /*
684      * delete the table
685      */
686     bier_table_unlock(&bt_0_0_0_256);
687
688     /*
689      * test resources are freed
690      */
691     dpo_reset(&dpo_bei);
692     for (ii = 0; ii < N_BIER_ECMP_TABLES; ii++)
693     {
694         bier_table_ecmp_unlock(l_o_bt[ii].bier.table);
695     };
696     BIER_TEST(0 == pool_elts(bier_table_pool), "BIER table pool empty");
697     BIER_TEST(0 == pool_elts(bier_fmask_pool), "BIER fmask pool empty");
698     BIER_TEST(0 == pool_elts(bier_entry_pool), "BIER entry pool empty");
699
700     adj_unlock(ai_mpls_10_10_10_1);
701     adj_unlock(ai_mpls_10_10_10_2);
702     dpo_reset(&neos_dpo_1_1_1_1);
703     dpo_reset(&neos_dpo_1_1_1_2);
704     fib_table_entry_delete(0, &pfx_1_1_1_1_s_32, FIB_SOURCE_API);
705     fib_table_entry_delete(0, &pfx_1_1_1_2_s_32, FIB_SOURCE_API);
706
707     /* +1 to account for the one time alloc'd drop LB in the MPLS fibs */
708     BIER_TEST(lb_count+1 == pool_elts(load_balance_pool),
709               "Load-balance resources freed ");
710     BIER_TEST((0 == adj_nbr_db_size()), "ADJ DB size is %d",
711              adj_nbr_db_size());
712
713     vec_free(paths_1_1_1_1);
714     vec_free(paths_1_1_1_2);
715     vec_free(input_paths_1_1_1_1);
716     vec_free(input_paths_1_1_1_2);
717
718     return (0);
719 }
720
721 static int
722 bier_test_mpls_imp (void)
723 {
724     fib_node_index_t bii;
725     int res;
726
727     /*
728      * Add the BIER Main table
729      */
730     const bier_table_id_t bt_0_0_0_256 = {
731         .bti_set = 0,
732         .bti_sub_domain = 0,
733         .bti_hdr_len = BIER_HDR_LEN_256,
734         .bti_type = BIER_TABLE_MPLS_SPF,
735         .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
736     };
737
738     bier_table_add_or_lock(&bt_0_0_0_256, 1600);
739
740     /*
741      * A bit-string for imp 1.
742      */
743     bier_bit_string_t bbs_256;
744     u8 buckets[BIER_HDR_BUCKETS_256];
745     memset(buckets, 0x5, BIER_HDR_BUCKETS_256);
746
747     res = 0;
748     bier_bit_string_init(&bbs_256, BIER_HDR_LEN_256, buckets);
749
750     bii = bier_imp_add_or_lock(&bt_0_0_0_256, 1, &bbs_256);
751
752     /*
753      * An mfib entry that resolves via the BIER imposition
754      */
755     const mfib_prefix_t pfx_1_1_1_1_c_239_1_1_1 = {
756         .fp_len = 64,
757         .fp_proto = FIB_PROTOCOL_IP4,
758         .fp_grp_addr = {
759             .ip4.as_u32 = clib_host_to_net_u32(0xef010101),
760         },
761         .fp_src_addr = {
762             .ip4.as_u32 = clib_host_to_net_u32(0x01010101),
763         },
764     };
765     fib_route_path_t path_via_bier_imp_1 = {
766         .frp_proto = DPO_PROTO_BIER,
767         .frp_bier_imp = bii,
768         .frp_weight = 0,
769         .frp_flags = FIB_ROUTE_PATH_BIER_IMP,
770     };
771     mfib_table_entry_path_update(0, // default table
772                                  &pfx_1_1_1_1_c_239_1_1_1 ,
773                                  MFIB_SOURCE_API,
774                                  &path_via_bier_imp_1,
775                                  MFIB_ITF_FLAG_FORWARD);
776     mfib_table_entry_delete(0,
777                             &pfx_1_1_1_1_c_239_1_1_1 ,
778                             MFIB_SOURCE_API);
779
780     bier_imp_unlock(bii);
781     bier_table_unlock(&bt_0_0_0_256);
782
783     BIER_TEST(0 == pool_elts(bier_imp_pool),
784               "BIER imposition resources freed ");
785     BIER_TEST(0 == pool_elts(bier_table_pool),
786               "BIER table resources freed ");
787
788     return (0);
789 }
790
791 static int
792 bier_test_mpls_disp (void)
793 {
794     /*
795      * Add the BIER Main table
796      */
797     const bier_table_id_t bt_0_0_0_256 = {
798         .bti_set = 0,
799         .bti_sub_domain = 0,
800         .bti_hdr_len = BIER_HDR_LEN_256,
801         .bti_type = BIER_TABLE_MPLS_SPF,
802         .bti_ecmp = BIER_ECMP_TABLE_ID_MAIN,
803     };
804     index_t bti;
805     int res;
806
807     res = 0;
808     bti = bier_table_add_or_lock(&bt_0_0_0_256, 1600);
809
810     /*
811      * Add a BIER dispoition table
812      */
813     const u32 bier_disp_tbl_id = 1;
814     index_t bdti1;
815
816     bdti1 = bier_disp_table_add_or_lock(bier_disp_tbl_id);
817
818     /*
819      * add a bit-poistion in the table that resolves via
820      * DISP table, i.e. a for-us bit-position
821      */
822     fib_route_path_t *paths_via_disp = NULL, path_via_disp = {
823         // .frp_addr = all-zeros
824         .frp_proto = DPO_PROTO_BIER,
825         .frp_bier_fib_index = bdti1,
826         .frp_sw_if_index = ~0,
827     };
828     vec_add1(paths_via_disp, path_via_disp);
829
830     bier_table_route_path_add(&bt_0_0_0_256, 3, paths_via_disp);
831
832     /*
833      * the fmask should stack on the BIER disp table
834      */
835     bier_fmask_t *bfm_0_0_0_0;
836     index_t bfmi_0_0_0_0;
837     dpo_id_t dpo_disp_tbl_1 = DPO_INVALID;
838
839     bier_disp_table_contribute_forwarding(bdti1, &dpo_disp_tbl_1);
840
841     bfmi_0_0_0_0 = bier_fmask_db_find(bti, &path_via_disp);
842     bfm_0_0_0_0 = bier_fmask_get(bfmi_0_0_0_0);
843
844     BIER_TEST(!dpo_cmp(&dpo_disp_tbl_1, &bfm_0_0_0_0->bfm_dpo),
845               "Fmask via 0.0.0.0 stacks on BIER disp table 1");
846
847     /*
848      * and a deag entry into the disposition table
849      */
850     fib_route_path_t *rpaths = NULL, path_via_mfib = {
851         .frp_proto = DPO_PROTO_IP4,
852         .frp_addr = zero_addr,
853         .frp_fib_index = 0, // default MFIB table
854         .frp_rpf_id = 9, // some non-zero value
855         .frp_flags = FIB_ROUTE_PATH_RPF_ID,
856     };
857     bier_hdr_src_id_t src = 99;
858     vec_add1(rpaths, path_via_mfib);
859     bier_disp_table_entry_path_add(bier_disp_tbl_id, src,
860                                    BIER_HDR_PROTO_IPV4, rpaths);
861
862     /* which should stack on a lookup in the mfib table */
863     const dpo_id_t *dpo_disp_entry_v4;
864     bier_disp_entry_t *bde_99;
865     index_t bdei;
866
867     bdei = bier_disp_table_lookup(bdti1, clib_host_to_net_u16(src));
868     bde_99 = bier_disp_entry_get(bdei);
869     dpo_disp_entry_v4 = &bde_99->bde_fwd[BIER_HDR_PROTO_IPV4].bde_dpo;
870
871     lookup_dpo_t *lkd = lookup_dpo_get(dpo_disp_entry_v4->dpoi_index);
872
873     BIER_TEST((bdti1 == lkd->lkd_fib_index),
874               "disp is deag in %d %U",
875               lkd->lkd_fib_index,
876               format_dpo_id, dpo_disp_entry_v4, 0);
877     BIER_TEST((LOOKUP_INPUT_DST_ADDR == lkd->lkd_input),
878               "disp is destination deag in %d %U",
879               lkd->lkd_input,
880               format_dpo_id, dpo_disp_entry_v4, 0);
881     BIER_TEST((LOOKUP_MULTICAST == lkd->lkd_cast),
882               "disp is multicast deag in %d %U",
883               lkd->lkd_input,
884               format_dpo_id, dpo_disp_entry_v4, 0);
885
886     /*
887      * cleanup
888      */
889     dpo_reset(&dpo_disp_tbl_1);
890
891     bier_disp_table_entry_path_remove(bier_disp_tbl_id, src,
892                                       BIER_HDR_PROTO_IPV4, rpaths);
893     bier_table_route_path_remove(&bt_0_0_0_256, 3, paths_via_disp);
894
895     bier_disp_table_unlock_w_table_id(bier_disp_tbl_id);
896
897     bier_table_unlock(&bt_0_0_0_256);
898
899     BIER_TEST(0 == pool_elts(bier_fmask_pool),
900               "BIER fmask resources freed ");
901     BIER_TEST(0 == pool_elts(bier_table_pool),
902               "BIER table resources freed ");
903     BIER_TEST(0 == pool_elts(bier_disp_table_pool),
904               "BIER Disposition table resources freed ");
905     BIER_TEST(0 == pool_elts(bier_disp_entry_pool),
906               "BIER Disposition entry resources freed ");
907
908     vec_free(paths_via_disp);
909     return (0);
910 }
911
912 static clib_error_t *
913 bier_test (vlib_main_t * vm,
914            unformat_input_t * input,
915            vlib_cli_command_t * cmd_arg)
916 {
917     int res = 0;
918
919     res += bier_test_mk_intf(4);
920
921     if (unformat (input, "debug"))
922     {
923         bier_test_do_debug = 1;
924     }
925
926     if (unformat (input, "mid"))
927         res += bier_test_mpls_spf();
928     else if (unformat (input, "head"))
929         res += bier_test_mpls_imp();
930     else if (unformat (input, "tail"))
931         res += bier_test_mpls_disp();
932     else
933     {
934         res += bier_test_mpls_spf();
935         res += bier_test_mpls_imp();
936         res += bier_test_mpls_disp();
937     }
938
939     if (res)
940     {
941         return clib_error_return(0, "BIER Unit Test Failed");
942     }
943     else
944     {
945         return (NULL);
946     }
947 }
948
949 VLIB_CLI_COMMAND (test_route_command, static) = {
950     .path = "test bier",
951     .short_help = "bier unit tests",
952     .function = bier_test,
953 };
954
955 clib_error_t *
956 bier_test_init (vlib_main_t *vm)
957 {
958     return 0;
959 }
960
961 VLIB_INIT_FUNCTION (bier_test_init);