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