LCOV - code coverage report
Current view: top level - app/test - test_fib.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 280 293 95.6 %
Date: 2026-12-01 19:01:22 Functions: 15 17 88.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 110 198 55.6 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
       3                 :            :  * Copyright(c) 2019 Intel Corporation
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <stdio.h>
       7                 :            : #include <stdint.h>
       8                 :            : #include <stdlib.h>
       9                 :            : 
      10                 :            : #include <rte_ip.h>
      11                 :            : #include <rte_log.h>
      12                 :            : #include <rte_fib.h>
      13                 :            : #include <rte_malloc.h>
      14                 :            : 
      15                 :            : #include "test.h"
      16                 :            : 
      17                 :            : typedef int32_t (*rte_fib_test)(void);
      18                 :            : 
      19                 :            : static int32_t test_create_invalid(void);
      20                 :            : static int32_t test_multiple_create(void);
      21                 :            : static int32_t test_free_null(void);
      22                 :            : static int32_t test_add_del_invalid(void);
      23                 :            : static int32_t test_get_invalid(void);
      24                 :            : static int32_t test_lookup(void);
      25                 :            : static int32_t test_wraparound(void);
      26                 :            : static int32_t test_invalid_rcu(void);
      27                 :            : static int32_t test_fib_rcu_sync_rw(void);
      28                 :            : 
      29                 :            : #define MAX_ROUTES      (1 << 16)
      30                 :            : #define MAX_TBL8        (1 << 15)
      31                 :            : 
      32                 :            : /*
      33                 :            :  * Check that rte_fib_create fails gracefully for incorrect user input
      34                 :            :  * arguments
      35                 :            :  */
      36                 :            : int32_t
      37                 :          1 : test_create_invalid(void)
      38                 :            : {
      39                 :            :         struct rte_fib *fib = NULL;
      40                 :          1 :         struct rte_fib_conf config = { 0 };
      41                 :            : 
      42                 :          1 :         config.max_routes = MAX_ROUTES;
      43                 :            :         config.rib_ext_sz = 0;
      44                 :            :         config.default_nh = 0;
      45                 :            :         config.type = RTE_FIB_DUMMY;
      46                 :            : 
      47                 :            :         /* rte_fib_create: fib name == NULL */
      48                 :          1 :         fib = rte_fib_create(NULL, SOCKET_ID_ANY, &config);
      49         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib == NULL,
      50                 :            :                 "Call succeeded with invalid parameters\n");
      51                 :            : 
      52                 :            :         /* rte_fib_create: config == NULL */
      53                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, NULL);
      54         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib == NULL,
      55                 :            :                 "Call succeeded with invalid parameters\n");
      56                 :            : 
      57                 :            :         /* socket_id < -1 is invalid */
      58                 :          1 :         fib = rte_fib_create(__func__, -2, &config);
      59         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib == NULL,
      60                 :            :                 "Call succeeded with invalid parameters\n");
      61                 :            : 
      62                 :            :         /* rte_fib_create: max_routes = 0 */
      63                 :          1 :         config.max_routes = 0;
      64                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
      65         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib == NULL,
      66                 :            :                 "Call succeeded with invalid parameters\n");
      67                 :          1 :         config.max_routes = MAX_ROUTES;
      68                 :            : 
      69                 :          1 :         config.type = RTE_FIB_DIR24_8 + 1;
      70                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
      71         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib == NULL,
      72                 :            :                 "Call succeeded with invalid parameters\n");
      73                 :            : 
      74                 :          1 :         config.type = RTE_FIB_DIR24_8;
      75                 :          1 :         config.dir24_8.num_tbl8 = MAX_TBL8;
      76                 :            : 
      77                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_8B + 1;
      78                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
      79         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib == NULL,
      80                 :            :                 "Call succeeded with invalid parameters\n");
      81                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_8B;
      82                 :            : 
      83                 :          1 :         config.dir24_8.num_tbl8 = 0;
      84                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
      85         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib == NULL,
      86                 :            :                 "Call succeeded with invalid parameters\n");
      87                 :            : 
      88                 :            :         return TEST_SUCCESS;
      89                 :            : }
      90                 :            : 
      91                 :            : /*
      92                 :            :  * Create fib table then delete fib table 10 times
      93                 :            :  * Use a slightly different rules size each time
      94                 :            :  */
      95                 :            : int32_t
      96                 :          0 : test_multiple_create(void)
      97                 :            : {
      98                 :            :         struct rte_fib *fib = NULL;
      99                 :          0 :         struct rte_fib_conf config = { 0 };
     100                 :            :         int32_t i;
     101                 :            : 
     102                 :            :         config.rib_ext_sz = 0;
     103                 :            :         config.default_nh = 0;
     104                 :            :         config.type = RTE_FIB_DUMMY;
     105                 :            : 
     106         [ #  # ]:          0 :         for (i = 0; i < 100; i++) {
     107                 :          0 :                 config.max_routes = MAX_ROUTES - i;
     108                 :          0 :                 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     109         [ #  # ]:          0 :                 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     110                 :          0 :                 rte_fib_free(fib);
     111                 :            :         }
     112                 :            :         /* Can not test free so return success */
     113                 :            :         return TEST_SUCCESS;
     114                 :            : }
     115                 :            : 
     116                 :            : /*
     117                 :            :  * Call rte_fib_free for NULL pointer user input. Note: free has no return and
     118                 :            :  * therefore it is impossible to check for failure but this test is added to
     119                 :            :  * increase function coverage metrics and to validate that freeing null does
     120                 :            :  * not crash.
     121                 :            :  */
     122                 :            : int32_t
     123                 :          1 : test_free_null(void)
     124                 :            : {
     125                 :            :         struct rte_fib *fib = NULL;
     126                 :          1 :         struct rte_fib_conf config = { 0 };
     127                 :            : 
     128                 :          1 :         config.max_routes = MAX_ROUTES;
     129                 :            :         config.rib_ext_sz = 0;
     130                 :            :         config.default_nh = 0;
     131                 :            :         config.type = RTE_FIB_DUMMY;
     132                 :            : 
     133                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     134         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     135                 :            : 
     136                 :          1 :         rte_fib_free(fib);
     137                 :          1 :         rte_fib_free(NULL);
     138                 :          1 :         return TEST_SUCCESS;
     139                 :            : }
     140                 :            : 
     141                 :            : /*
     142                 :            :  * Check that rte_fib_add and rte_fib_delete fails gracefully
     143                 :            :  * for incorrect user input arguments
     144                 :            :  */
     145                 :            : int32_t
     146                 :          1 : test_add_del_invalid(void)
     147                 :            : {
     148                 :            :         struct rte_fib *fib = NULL;
     149                 :          1 :         struct rte_fib_conf config = { 0 };
     150                 :            :         uint64_t nh = 100;
     151                 :            :         uint32_t ip = RTE_IPV4(0, 0, 0, 0);
     152                 :            :         int ret;
     153                 :            :         uint8_t depth = 24;
     154                 :            : 
     155                 :          1 :         config.max_routes = MAX_ROUTES;
     156                 :            :         config.rib_ext_sz = 0;
     157                 :            :         config.default_nh = 0;
     158                 :            :         config.type = RTE_FIB_DUMMY;
     159                 :            : 
     160                 :            :         /* rte_fib_add: fib == NULL */
     161                 :          1 :         ret = rte_fib_add(NULL, ip, depth, nh);
     162         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret < 0,
     163                 :            :                 "Call succeeded with invalid parameters\n");
     164                 :            : 
     165                 :            :         /* rte_fib_delete: fib == NULL */
     166                 :          1 :         ret = rte_fib_delete(NULL, ip, depth);
     167         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret < 0,
     168                 :            :                 "Call succeeded with invalid parameters\n");
     169                 :            : 
     170                 :            :         /*Create valid fib to use in rest of test. */
     171                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     172         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     173                 :            : 
     174                 :            :         /* rte_fib_add: depth > RTE_FIB_MAXDEPTH */
     175                 :          1 :         ret = rte_fib_add(fib, ip, RTE_FIB_MAXDEPTH + 1, nh);
     176         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret < 0,
     177                 :            :                 "Call succeeded with invalid parameters\n");
     178                 :            : 
     179                 :            :         /* rte_fib_delete: depth > RTE_FIB_MAXDEPTH */
     180                 :          1 :         ret = rte_fib_delete(fib, ip, RTE_FIB_MAXDEPTH + 1);
     181         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret < 0,
     182                 :            :                 "Call succeeded with invalid parameters\n");
     183                 :            : 
     184                 :          1 :         rte_fib_free(fib);
     185                 :            : 
     186                 :          1 :         return TEST_SUCCESS;
     187                 :            : }
     188                 :            : 
     189                 :            : /*
     190                 :            :  * Check that rte_fib_get_dp and rte_fib_get_rib fails gracefully
     191                 :            :  * for incorrect user input arguments
     192                 :            :  */
     193                 :            : int32_t
     194                 :          1 : test_get_invalid(void)
     195                 :            : {
     196                 :            :         void *p;
     197                 :            : 
     198                 :          1 :         p = rte_fib_get_dp(NULL);
     199         [ -  + ]:          1 :         RTE_TEST_ASSERT(p == NULL,
     200                 :            :                 "Call succeeded with invalid parameters\n");
     201                 :            : 
     202                 :          1 :         p = rte_fib_get_rib(NULL);
     203         [ -  + ]:          1 :         RTE_TEST_ASSERT(p == NULL,
     204                 :            :                 "Call succeeded with invalid parameters\n");
     205                 :            : 
     206                 :            :         return TEST_SUCCESS;
     207                 :            : }
     208                 :            : 
     209                 :            : /*
     210                 :            :  * Add routes for one supernet with all possible depths and do lookup
     211                 :            :  * on each step
     212                 :            :  * After delete routes with doing lookup on each step
     213                 :            :  */
     214                 :            : static int
     215                 :        315 : lookup_and_check_asc(struct rte_fib *fib, uint32_t ip_arr[RTE_FIB_MAXDEPTH],
     216                 :            :         uint32_t ip_missing, uint64_t def_nh, uint32_t n)
     217                 :            : {
     218                 :            :         uint64_t nh_arr[RTE_FIB_MAXDEPTH];
     219                 :            :         int ret;
     220                 :            :         uint32_t i = 0;
     221                 :            : 
     222                 :        315 :         ret = rte_fib_lookup_bulk(fib, ip_arr, nh_arr, RTE_FIB_MAXDEPTH);
     223         [ -  + ]:        315 :         RTE_TEST_ASSERT(ret == 0, "Failed to lookup\n");
     224                 :            : 
     225         [ +  + ]:       5590 :         for (; i <= RTE_FIB_MAXDEPTH - n; i++)
     226         [ -  + ]:       5275 :                 RTE_TEST_ASSERT(nh_arr[i] == n,
     227                 :            :                         "Failed to get proper nexthop\n");
     228                 :            : 
     229         [ +  + ]:       5120 :         for (; i < RTE_FIB_MAXDEPTH; i++)
     230         [ -  + ]:       4805 :                 RTE_TEST_ASSERT(nh_arr[i] == --n,
     231                 :            :                         "Failed to get proper nexthop\n");
     232                 :            : 
     233                 :        315 :         ret = rte_fib_lookup_bulk(fib, &ip_missing, nh_arr, 1);
     234   [ +  -  -  + ]:        315 :         RTE_TEST_ASSERT((ret == 0) && (nh_arr[0] == def_nh),
     235                 :            :                 "Failed to get proper nexthop\n");
     236                 :            : 
     237                 :            :         return TEST_SUCCESS;
     238                 :            : }
     239                 :            : 
     240                 :            : static int
     241                 :        330 : lookup_and_check_desc(struct rte_fib *fib, uint32_t ip_arr[RTE_FIB_MAXDEPTH],
     242                 :            :         uint32_t ip_missing, uint64_t def_nh, uint32_t n)
     243                 :            : {
     244                 :            :         uint64_t nh_arr[RTE_FIB_MAXDEPTH];
     245                 :            :         int ret;
     246                 :            :         uint32_t i = 0;
     247                 :            : 
     248                 :        330 :         ret = rte_fib_lookup_bulk(fib, ip_arr, nh_arr, RTE_FIB_MAXDEPTH);
     249         [ -  + ]:        330 :         RTE_TEST_ASSERT(ret == 0, "Failed to lookup\n");
     250                 :            : 
     251         [ +  + ]:       5450 :         for (; i < n; i++)
     252         [ -  + ]:       5120 :                 RTE_TEST_ASSERT(nh_arr[i] == RTE_FIB_MAXDEPTH - i,
     253                 :            :                         "Failed to get proper nexthop\n");
     254                 :            : 
     255         [ +  + ]:       5770 :         for (; i < RTE_FIB_MAXDEPTH; i++)
     256         [ -  + ]:       5440 :                 RTE_TEST_ASSERT(nh_arr[i] == def_nh,
     257                 :            :                         "Failed to get proper nexthop\n");
     258                 :            : 
     259                 :        330 :         ret = rte_fib_lookup_bulk(fib, &ip_missing, nh_arr, 1);
     260   [ +  -  -  + ]:        330 :         RTE_TEST_ASSERT((ret == 0) && (nh_arr[0] == def_nh),
     261                 :            :                 "Failed to get proper nexthop\n");
     262                 :            : 
     263                 :            :         return TEST_SUCCESS;
     264                 :            : }
     265                 :            : 
     266                 :            : static int
     267                 :          5 : check_fib(struct rte_fib *fib)
     268                 :            : {
     269                 :            :         uint64_t def_nh = 100;
     270                 :            :         uint32_t ip_arr[RTE_FIB_MAXDEPTH];
     271                 :            :         uint32_t ip_add = RTE_IPV4(128, 0, 0, 0);
     272                 :            :         uint32_t i, ip_missing = RTE_IPV4(127, 255, 255, 255);
     273                 :            :         int ret;
     274                 :            : 
     275         [ +  + ]:        165 :         for (i = 0; i < RTE_FIB_MAXDEPTH; i++)
     276                 :        160 :                 ip_arr[i] = ip_add + (1ULL << i) - 1;
     277                 :            : 
     278                 :          5 :         ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh, 0);
     279         [ -  + ]:          5 :         RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
     280                 :            : 
     281         [ +  + ]:        165 :         for (i = 1; i <= RTE_FIB_MAXDEPTH; i++) {
     282                 :        160 :                 ret = rte_fib_add(fib, ip_add, i, i);
     283         [ -  + ]:        160 :                 RTE_TEST_ASSERT(ret == 0, "Failed to add a route\n");
     284                 :        160 :                 ret = lookup_and_check_asc(fib, ip_arr, ip_missing,
     285                 :            :                                 def_nh, i);
     286         [ -  + ]:        160 :                 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
     287                 :            :         }
     288                 :            : 
     289         [ +  + ]:        160 :         for (i = RTE_FIB_MAXDEPTH; i > 1; i--) {
     290                 :        155 :                 ret = rte_fib_delete(fib, ip_add, i);
     291         [ -  + ]:        155 :                 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
     292                 :        155 :                 ret = lookup_and_check_asc(fib, ip_arr, ip_missing,
     293                 :            :                         def_nh, i - 1);
     294                 :            : 
     295         [ -  + ]:        155 :                 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
     296                 :            :         }
     297                 :          5 :         ret = rte_fib_delete(fib, ip_add, i);
     298         [ -  + ]:          5 :         RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
     299                 :          5 :         ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh, 0);
     300         [ -  + ]:          5 :         RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
     301                 :            : 
     302         [ +  + ]:        165 :         for (i = 0; i < RTE_FIB_MAXDEPTH; i++) {
     303                 :        160 :                 ret = rte_fib_add(fib, ip_add, RTE_FIB_MAXDEPTH - i,
     304                 :        160 :                         RTE_FIB_MAXDEPTH - i);
     305         [ -  + ]:        160 :                 RTE_TEST_ASSERT(ret == 0, "Failed to add a route\n");
     306                 :        160 :                 ret = lookup_and_check_desc(fib, ip_arr, ip_missing,
     307                 :            :                         def_nh, i + 1);
     308         [ -  + ]:        160 :                 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
     309                 :            :         }
     310                 :            : 
     311         [ +  + ]:        165 :         for (i = 1; i <= RTE_FIB_MAXDEPTH; i++) {
     312                 :        160 :                 ret = rte_fib_delete(fib, ip_add, i);
     313         [ -  + ]:        160 :                 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
     314                 :        160 :                 ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh,
     315                 :            :                         RTE_FIB_MAXDEPTH - i);
     316         [ -  + ]:        160 :                 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
     317                 :            :         }
     318                 :            : 
     319                 :            :         return TEST_SUCCESS;
     320                 :            : }
     321                 :            : 
     322                 :            : int32_t
     323                 :          1 : test_lookup(void)
     324                 :            : {
     325                 :            :         struct rte_fib *fib = NULL;
     326                 :          1 :         struct rte_fib_conf config = { 0 };
     327                 :            :         uint64_t def_nh = 100;
     328                 :            :         int ret;
     329                 :            : 
     330                 :          1 :         config.max_routes = MAX_ROUTES;
     331                 :            :         config.rib_ext_sz = 0;
     332                 :          1 :         config.default_nh = def_nh;
     333                 :            :         config.type = RTE_FIB_DUMMY;
     334                 :            : 
     335                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     336         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     337                 :          1 :         ret = check_fib(fib);
     338         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == TEST_SUCCESS,
     339                 :            :                 "Check_fib fails for DUMMY type\n");
     340                 :          1 :         rte_fib_free(fib);
     341                 :            : 
     342                 :          1 :         config.type = RTE_FIB_DIR24_8;
     343                 :            : 
     344                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_1B;
     345                 :          1 :         config.dir24_8.num_tbl8 = 127;
     346                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     347         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     348                 :          1 :         ret = check_fib(fib);
     349         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == TEST_SUCCESS,
     350                 :            :                 "Check_fib fails for DIR24_8_1B type\n");
     351                 :          1 :         rte_fib_free(fib);
     352                 :            : 
     353                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_2B;
     354                 :          1 :         config.dir24_8.num_tbl8 = MAX_TBL8 - 1;
     355                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     356         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     357                 :          1 :         ret = check_fib(fib);
     358         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == TEST_SUCCESS,
     359                 :            :                 "Check_fib fails for DIR24_8_2B type\n");
     360                 :          1 :         rte_fib_free(fib);
     361                 :            : 
     362                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
     363                 :          1 :         config.dir24_8.num_tbl8 = MAX_TBL8;
     364                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     365         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     366                 :          1 :         ret = check_fib(fib);
     367         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == TEST_SUCCESS,
     368                 :            :                 "Check_fib fails for DIR24_8_4B type\n");
     369                 :          1 :         rte_fib_free(fib);
     370                 :            : 
     371                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_8B;
     372                 :          1 :         config.dir24_8.num_tbl8 = MAX_TBL8;
     373                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     374         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     375                 :          1 :         ret = check_fib(fib);
     376         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == TEST_SUCCESS,
     377                 :            :                 "Check_fib fails for DIR24_8_8B type\n");
     378                 :          1 :         rte_fib_free(fib);
     379                 :            : 
     380                 :          1 :         return TEST_SUCCESS;
     381                 :            : }
     382                 :            : 
     383                 :            : /*
     384                 :            :  * Reproducer for the address wraparound bug in modify_fib(). When the
     385                 :            :  * more-specific routes under a modified prefix cover its range up to
     386                 :            :  * 255.255.255.255, and the last of them starts exactly at the current
     387                 :            :  * left edge, the left edge wraps to 0 and the entire table must not
     388                 :            :  * be overwritten.
     389                 :            :  *
     390                 :            :  *   Case A: ADD 0.0.0.0/0, 0.0.0.0/1 and 128.0.0.0/1, then DEL
     391                 :            :  *           0.0.0.0/0. Both /1 routes must still resolve.
     392                 :            :  *   Case B: ADD 10.0.0.0/8, 255.255.255.0/25 and 255.255.255.128/25,
     393                 :            :  *           then ADD their covering 255.255.255.0/24. No lookup result
     394                 :            :  *           may change.
     395                 :            :  */
     396                 :            : static int32_t
     397                 :          1 : test_wraparound(void)
     398                 :            : {
     399                 :          1 :         struct rte_fib_conf config = { 0 };
     400                 :            :         struct rte_fib *fib;
     401                 :            :         uint64_t def_nh = 100;
     402                 :            :         uint32_t ip[4];
     403                 :            :         uint64_t nh[4];
     404                 :            :         int ret;
     405                 :            : 
     406                 :          1 :         config.max_routes = 1024;
     407                 :            :         config.rib_ext_sz = 0;
     408                 :          1 :         config.default_nh = def_nh;
     409                 :          1 :         config.type = RTE_FIB_DIR24_8;
     410                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
     411                 :          1 :         config.dir24_8.num_tbl8 = 256;
     412                 :            : 
     413                 :            :         /* Case A */
     414                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     415         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     416                 :            : 
     417                 :          1 :         ret = rte_fib_add(fib, RTE_IPV4(0, 0, 0, 0), 0, 1);
     418         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0, "ADD 0.0.0.0/0 failed (ret=%d)\n", ret);
     419                 :          1 :         ret = rte_fib_add(fib, RTE_IPV4(0, 0, 0, 0), 1, 2);
     420         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0, "ADD 0.0.0.0/1 failed (ret=%d)\n", ret);
     421                 :          1 :         ret = rte_fib_add(fib, RTE_IPV4(128, 0, 0, 0), 1, 3);
     422         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0, "ADD 128.0.0.0/1 failed (ret=%d)\n", ret);
     423                 :            : 
     424                 :          1 :         ret = rte_fib_delete(fib, RTE_IPV4(0, 0, 0, 0), 0);
     425         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0, "DEL 0.0.0.0/0 failed (ret=%d)\n", ret);
     426                 :            : 
     427                 :          1 :         ip[0] = RTE_IPV4(1, 1, 1, 1);
     428                 :          1 :         ip[1] = RTE_IPV4(200, 1, 1, 1);
     429                 :          1 :         ret = rte_fib_lookup_bulk(fib, ip, nh, 2);
     430         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0, "Lookup failed (ret=%d)\n", ret);
     431         [ -  + ]:          1 :         RTE_TEST_ASSERT(nh[0] == 2,
     432                 :            :                 "1.1.1.1: got nh %" PRIu64 ", want 2\n", nh[0]);
     433         [ -  + ]:          1 :         RTE_TEST_ASSERT(nh[1] == 3,
     434                 :            :                 "200.1.1.1: got nh %" PRIu64 ", want 3\n", nh[1]);
     435                 :            : 
     436                 :          1 :         rte_fib_free(fib);
     437                 :            : 
     438                 :            :         /* Case B */
     439                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     440         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     441                 :            : 
     442                 :          1 :         ret = rte_fib_add(fib, RTE_IPV4(10, 0, 0, 0), 8, 10);
     443         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0, "ADD 10.0.0.0/8 failed (ret=%d)\n", ret);
     444                 :          1 :         ret = rte_fib_add(fib, RTE_IPV4(255, 255, 255, 0), 25, 5);
     445         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0,
     446                 :            :                 "ADD 255.255.255.0/25 failed (ret=%d)\n", ret);
     447                 :          1 :         ret = rte_fib_add(fib, RTE_IPV4(255, 255, 255, 128), 25, 6);
     448         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0,
     449                 :            :                 "ADD 255.255.255.128/25 failed (ret=%d)\n", ret);
     450                 :            : 
     451                 :          1 :         ret = rte_fib_add(fib, RTE_IPV4(255, 255, 255, 0), 24, 7);
     452         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0,
     453                 :            :                 "ADD 255.255.255.0/24 failed (ret=%d)\n", ret);
     454                 :            : 
     455                 :          1 :         ip[0] = RTE_IPV4(10, 1, 1, 1);
     456                 :          1 :         ip[1] = RTE_IPV4(1, 2, 3, 4);
     457                 :          1 :         ip[2] = RTE_IPV4(255, 255, 255, 1);
     458                 :          1 :         ip[3] = RTE_IPV4(255, 255, 255, 129);
     459                 :          1 :         ret = rte_fib_lookup_bulk(fib, ip, nh, 4);
     460         [ -  + ]:          1 :         RTE_TEST_ASSERT(ret == 0, "Lookup failed (ret=%d)\n", ret);
     461         [ -  + ]:          1 :         RTE_TEST_ASSERT(nh[0] == 10,
     462                 :            :                 "10.1.1.1: got nh %" PRIu64 ", want 10\n", nh[0]);
     463         [ -  + ]:          1 :         RTE_TEST_ASSERT(nh[1] == def_nh,
     464                 :            :                 "1.2.3.4: got nh %" PRIu64 ", want %" PRIu64 "\n",
     465                 :            :                 nh[1], def_nh);
     466         [ -  + ]:          1 :         RTE_TEST_ASSERT(nh[2] == 5,
     467                 :            :                 "255.255.255.1: got nh %" PRIu64 ", want 5\n", nh[2]);
     468         [ -  + ]:          1 :         RTE_TEST_ASSERT(nh[3] == 6,
     469                 :            :                 "255.255.255.129: got nh %" PRIu64 ", want 6\n", nh[3]);
     470                 :            : 
     471                 :          1 :         rte_fib_free(fib);
     472                 :          1 :         return TEST_SUCCESS;
     473                 :            : }
     474                 :            : 
     475                 :            : /*
     476                 :            :  * rte_fib_rcu_qsbr_add positive and negative tests.
     477                 :            :  *  - Add RCU QSBR variable to FIB
     478                 :            :  *  - Add another RCU QSBR variable to FIB
     479                 :            :  *  - Check returns
     480                 :            :  */
     481                 :            : int32_t
     482                 :          1 : test_invalid_rcu(void)
     483                 :            : {
     484                 :            :         struct rte_fib *fib = NULL;
     485                 :          1 :         struct rte_fib_conf config = { 0 };
     486                 :            :         size_t sz;
     487                 :            :         struct rte_rcu_qsbr *qsv;
     488                 :            :         struct rte_rcu_qsbr *qsv2;
     489                 :            :         int32_t status;
     490                 :          1 :         struct rte_fib_rcu_config rcu_cfg = {0};
     491                 :            :         uint64_t def_nh = 100;
     492                 :            : 
     493                 :          1 :         config.max_routes = MAX_ROUTES;
     494                 :            :         config.rib_ext_sz = 0;
     495                 :          1 :         config.default_nh = def_nh;
     496                 :            : 
     497                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     498         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     499                 :            : 
     500                 :            :         /* Create RCU QSBR variable */
     501                 :          1 :         sz = rte_rcu_qsbr_get_memsize(RTE_MAX_LCORE);
     502                 :          1 :         qsv = (struct rte_rcu_qsbr *)rte_zmalloc_socket(NULL, sz, RTE_CACHE_LINE_SIZE,
     503                 :            :                 SOCKET_ID_ANY);
     504         [ -  + ]:          1 :         RTE_TEST_ASSERT(qsv != NULL, "Can not allocate memory for RCU\n");
     505                 :            : 
     506                 :          1 :         status = rte_rcu_qsbr_init(qsv, RTE_MAX_LCORE);
     507         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == 0, "Can not initialize RCU\n");
     508                 :            : 
     509                 :          1 :         rcu_cfg.v = qsv;
     510                 :            : 
     511                 :            :         /* adding rcu to RTE_FIB_DUMMY FIB type */
     512                 :          1 :         config.type = RTE_FIB_DUMMY;
     513                 :          1 :         rcu_cfg.mode = RTE_FIB_QSBR_MODE_SYNC;
     514                 :          1 :         status = rte_fib_rcu_qsbr_add(fib, &rcu_cfg);
     515         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == -ENOTSUP,
     516                 :            :                 "rte_fib_rcu_qsbr_add returned wrong error status when called with DUMMY type FIB\n");
     517                 :          1 :         rte_fib_free(fib);
     518                 :            : 
     519                 :          1 :         config.type = RTE_FIB_DIR24_8;
     520                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
     521                 :          1 :         config.dir24_8.num_tbl8 = MAX_TBL8;
     522                 :          1 :         fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     523         [ -  + ]:          1 :         RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
     524                 :            : 
     525                 :            :         /* Call rte_fib_rcu_qsbr_add without fib or config */
     526                 :          1 :         status = rte_fib_rcu_qsbr_add(NULL, &rcu_cfg);
     527         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == -EINVAL, "RCU added without fib\n");
     528                 :          1 :         status = rte_fib_rcu_qsbr_add(fib, NULL);
     529         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == -EINVAL, "RCU added without config\n");
     530                 :            : 
     531                 :            :         /* Invalid QSBR mode */
     532                 :          1 :         rcu_cfg.mode = 2;
     533                 :          1 :         status = rte_fib_rcu_qsbr_add(fib, &rcu_cfg);
     534         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == -EINVAL, "RCU added with incorrect mode\n");
     535                 :            : 
     536                 :          1 :         rcu_cfg.mode = RTE_FIB_QSBR_MODE_DQ;
     537                 :            : 
     538                 :            :         /* Attach RCU QSBR to FIB to check for double attach */
     539                 :          1 :         status = rte_fib_rcu_qsbr_add(fib, &rcu_cfg);
     540         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == 0, "Can not attach RCU to FIB\n");
     541                 :            : 
     542                 :            :         /* Create and attach another RCU QSBR to FIB table */
     543                 :          1 :         qsv2 = (struct rte_rcu_qsbr *)rte_zmalloc_socket(NULL, sz, RTE_CACHE_LINE_SIZE,
     544                 :            :                 SOCKET_ID_ANY);
     545         [ -  + ]:          1 :         RTE_TEST_ASSERT(qsv2 != NULL, "Can not allocate memory for RCU\n");
     546                 :            : 
     547                 :          1 :         rcu_cfg.v = qsv2;
     548                 :          1 :         rcu_cfg.mode = RTE_FIB_QSBR_MODE_SYNC;
     549                 :          1 :         status = rte_fib_rcu_qsbr_add(fib, &rcu_cfg);
     550         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == -EEXIST, "Secondary RCU was mistakenly attached\n");
     551                 :            : 
     552                 :          1 :         rte_fib_free(fib);
     553                 :          1 :         rte_free(qsv);
     554                 :          1 :         rte_free(qsv2);
     555                 :            : 
     556                 :          1 :         return TEST_SUCCESS;
     557                 :            : }
     558                 :            : 
     559                 :            : static struct rte_fib *g_fib;
     560                 :            : static struct rte_rcu_qsbr *g_v;
     561                 :            : static uint32_t g_ip = RTE_IPV4(192, 0, 2, 100);
     562                 :            : static volatile uint8_t writer_done;
     563                 :            : /* Report quiescent state interval every 1024 lookups. Larger critical
     564                 :            :  * sections in reader will result in writer polling multiple times.
     565                 :            :  */
     566                 :            : #define QSBR_REPORTING_INTERVAL 1024
     567                 :            : #define WRITER_ITERATIONS       512
     568                 :            : 
     569                 :            : /*
     570                 :            :  * Reader thread using rte_fib data structure with RCU.
     571                 :            :  */
     572                 :            : static int
     573                 :          1 : test_fib_rcu_qsbr_reader(void *arg)
     574                 :            : {
     575                 :            :         int i;
     576                 :          1 :         uint64_t next_hop_return = 0;
     577                 :            : 
     578                 :            :         RTE_SET_USED(arg);
     579                 :            :         /* Register this thread to report quiescent state */
     580                 :          1 :         rte_rcu_qsbr_thread_register(g_v, 0);
     581                 :          1 :         rte_rcu_qsbr_thread_online(g_v, 0);
     582                 :            : 
     583                 :            :         do {
     584         [ +  + ]:     632425 :                 for (i = 0; i < QSBR_REPORTING_INTERVAL; i++)
     585                 :     631808 :                         rte_fib_lookup_bulk(g_fib, &g_ip, &next_hop_return, 1);
     586                 :            : 
     587                 :            :                 /* Update quiescent state */
     588         [ +  + ]:        617 :                 rte_rcu_qsbr_quiescent(g_v, 0);
     589         [ +  + ]:        617 :         } while (!writer_done);
     590                 :            : 
     591                 :          1 :         rte_rcu_qsbr_thread_offline(g_v, 0);
     592                 :          1 :         rte_rcu_qsbr_thread_unregister(g_v, 0);
     593                 :            : 
     594                 :          1 :         return 0;
     595                 :            : }
     596                 :            : 
     597                 :            : /*
     598                 :            :  * rte_fib_rcu_qsbr_add sync mode functional test.
     599                 :            :  * 1 Reader and 1 writer. They cannot be in the same thread in this test.
     600                 :            :  *  - Create FIB which supports 1 tbl8 group at max
     601                 :            :  *  - Add RCU QSBR variable with sync mode to FIB
     602                 :            :  *  - Register a reader thread. Reader keeps looking up a specific rule.
     603                 :            :  *  - Writer keeps adding and deleting a specific rule with depth=28 (> 24)
     604                 :            :  */
     605                 :            : int32_t
     606                 :          1 : test_fib_rcu_sync_rw(void)
     607                 :            : {
     608                 :          1 :         struct rte_fib_conf config = { 0 };
     609                 :            :         size_t sz;
     610                 :            :         int32_t status;
     611                 :            :         uint32_t i, next_hop;
     612                 :            :         uint8_t depth;
     613                 :          1 :         struct rte_fib_rcu_config rcu_cfg = {0};
     614                 :            :         uint64_t def_nh = 100;
     615                 :            : 
     616         [ -  + ]:          1 :         if (rte_lcore_count() < 2) {
     617                 :            :                 printf("Not enough cores for %s, expecting at least 2\n", __func__);
     618                 :          0 :                 return TEST_SKIPPED;
     619                 :            :         }
     620                 :            : 
     621                 :          1 :         config.max_routes = MAX_ROUTES;
     622                 :          1 :         config.rib_ext_sz = 0;
     623                 :          1 :         config.default_nh = def_nh;
     624                 :          1 :         config.type = RTE_FIB_DIR24_8;
     625                 :          1 :         config.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
     626                 :          1 :         config.dir24_8.num_tbl8 = 1;
     627                 :            : 
     628                 :          1 :         g_fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
     629         [ -  + ]:          1 :         RTE_TEST_ASSERT(g_fib != NULL, "Failed to create FIB\n");
     630                 :            : 
     631                 :            :         /* Create RCU QSBR variable */
     632                 :          1 :         sz = rte_rcu_qsbr_get_memsize(1);
     633                 :          1 :         g_v = (struct rte_rcu_qsbr *)rte_zmalloc_socket(NULL, sz, RTE_CACHE_LINE_SIZE,
     634                 :            :                 SOCKET_ID_ANY);
     635         [ -  + ]:          1 :         RTE_TEST_ASSERT(g_v != NULL, "Can not allocate memory for RCU\n");
     636                 :            : 
     637                 :          1 :         status = rte_rcu_qsbr_init(g_v, 1);
     638         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == 0, "Can not initialize RCU\n");
     639                 :            : 
     640                 :          1 :         rcu_cfg.v = g_v;
     641                 :          1 :         rcu_cfg.mode = RTE_FIB_QSBR_MODE_SYNC;
     642                 :            :         /* Attach RCU QSBR to FIB table */
     643                 :          1 :         status = rte_fib_rcu_qsbr_add(g_fib, &rcu_cfg);
     644         [ -  + ]:          1 :         RTE_TEST_ASSERT(status == 0, "Can not attach RCU to FIB\n");
     645                 :            : 
     646                 :          1 :         writer_done = 0;
     647                 :            :         /* Launch reader thread */
     648                 :          1 :         rte_eal_remote_launch(test_fib_rcu_qsbr_reader, NULL, rte_get_next_lcore(-1, 1, 0));
     649                 :            : 
     650                 :            :         depth = 28;
     651                 :            :         next_hop = 1;
     652                 :          1 :         status = rte_fib_add(g_fib, g_ip, depth, next_hop);
     653         [ -  + ]:          1 :         if (status != 0) {
     654                 :            :                 printf("%s: Failed to add rule\n", __func__);
     655                 :          0 :                 goto error;
     656                 :            :         }
     657                 :            : 
     658                 :            :         /* Writer update */
     659         [ +  + ]:        513 :         for (i = 0; i < WRITER_ITERATIONS; i++) {
     660                 :        512 :                 status = rte_fib_delete(g_fib, g_ip, depth);
     661         [ -  + ]:        512 :                 if (status != 0) {
     662                 :            :                         printf("%s: Failed to delete rule at iteration %d\n", __func__, i);
     663                 :          0 :                         goto error;
     664                 :            :                 }
     665                 :            : 
     666                 :        512 :                 status = rte_fib_add(g_fib, g_ip, depth, next_hop);
     667         [ -  + ]:        512 :                 if (status != 0) {
     668                 :            :                         printf("%s: Failed to add rule at iteration %d\n", __func__, i);
     669                 :          0 :                         goto error;
     670                 :            :                 }
     671                 :            :         }
     672                 :            : 
     673                 :          1 : error:
     674                 :          1 :         writer_done = 1;
     675                 :            :         /* Wait until reader exited. */
     676                 :          1 :         rte_eal_mp_wait_lcore();
     677                 :            : 
     678                 :          1 :         rte_fib_free(g_fib);
     679                 :          1 :         rte_free(g_v);
     680                 :            : 
     681         [ -  + ]:          1 :         return status == 0 ? TEST_SUCCESS : TEST_FAILED;
     682                 :            : }
     683                 :            : 
     684                 :            : static struct unit_test_suite fib_fast_tests = {
     685                 :            :         .suite_name = "fib autotest",
     686                 :            :         .setup = NULL,
     687                 :            :         .teardown = NULL,
     688                 :            :         .unit_test_cases = {
     689                 :            :         TEST_CASE(test_create_invalid),
     690                 :            :         TEST_CASE(test_free_null),
     691                 :            :         TEST_CASE(test_add_del_invalid),
     692                 :            :         TEST_CASE(test_get_invalid),
     693                 :            :         TEST_CASE(test_lookup),
     694                 :            :         TEST_CASE(test_wraparound),
     695                 :            :         TEST_CASE(test_invalid_rcu),
     696                 :            :         TEST_CASE(test_fib_rcu_sync_rw),
     697                 :            :         TEST_CASES_END()
     698                 :            :         }
     699                 :            : };
     700                 :            : 
     701                 :            : static struct unit_test_suite fib_slow_tests = {
     702                 :            :         .suite_name = "fib slow autotest",
     703                 :            :         .setup = NULL,
     704                 :            :         .teardown = NULL,
     705                 :            :         .unit_test_cases = {
     706                 :            :         TEST_CASE(test_multiple_create),
     707                 :            :         TEST_CASES_END()
     708                 :            :         }
     709                 :            : };
     710                 :            : 
     711                 :            : /*
     712                 :            :  * Do all unit tests.
     713                 :            :  */
     714                 :            : static int
     715                 :          1 : test_fib(void)
     716                 :            : {
     717                 :          1 :         return unit_test_suite_runner(&fib_fast_tests);
     718                 :            : }
     719                 :            : 
     720                 :            : static int
     721                 :          0 : test_slow_fib(void)
     722                 :            : {
     723                 :          0 :         return unit_test_suite_runner(&fib_slow_tests);
     724                 :            : }
     725                 :            : 
     726                 :        306 : REGISTER_FAST_TEST(fib_autotest, NOHUGE_OK, ASAN_OK, test_fib);
     727                 :        306 : REGISTER_PERF_TEST(fib_slow_autotest, test_slow_fib);

Generated by: LCOV version 1.14