LCOV - code coverage report
Current view: top level - app/test - test_bitratestats.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 55 58 94.8 %
Date: 2025-03-01 20:23:48 Functions: 14 14 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 14 28 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdint.h>
       7                 :            : #include <string.h>
       8                 :            : 
       9                 :            : #include <errno.h>
      10                 :            : #include <rte_lcore.h>
      11                 :            : #include <rte_memzone.h>
      12                 :            : #include <rte_metrics.h>
      13                 :            : #include <rte_bitrate.h>
      14                 :            : #include <rte_ethdev.h>
      15                 :            : 
      16                 :            : #include "sample_packet_forward.h"
      17                 :            : #include "test.h"
      18                 :            : 
      19                 :            : #define BIT_NUM_PACKETS 10
      20                 :            : #define QUEUE_ID 0
      21                 :            : 
      22                 :            : static uint16_t portid;
      23                 :            : static struct rte_stats_bitrates *bitrate_data;
      24                 :            : static struct rte_ring *ring;
      25                 :            : 
      26                 :            : /* To test whether rte_stats_bitrate_create is successful */
      27                 :            : static int
      28                 :          1 : test_stats_bitrate_create(void)
      29                 :            : {
      30                 :          1 :         bitrate_data = rte_stats_bitrate_create();
      31         [ -  + ]:          1 :         TEST_ASSERT(bitrate_data != NULL, "rte_stats_bitrate_create failed");
      32                 :            : 
      33                 :            :         return TEST_SUCCESS;
      34                 :            : }
      35                 :            : 
      36                 :            : /* To test free the resources from bitrate_create test */
      37                 :            : static int
      38                 :          1 : test_stats_bitrate_free(void)
      39                 :            : {
      40                 :            :         int ret = 0;
      41                 :            : 
      42                 :          1 :         rte_stats_bitrate_free(bitrate_data);
      43                 :            : 
      44                 :          1 :         ret = rte_metrics_deinit();
      45         [ -  + ]:          1 :         TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
      46                 :            : 
      47                 :            :         return TEST_SUCCESS;
      48                 :            : }
      49                 :            : 
      50                 :            : /* To test bit rate registration */
      51                 :            : static int
      52                 :          1 : test_stats_bitrate_reg(void)
      53                 :            : {
      54                 :            :         int ret = 0;
      55                 :            : 
      56                 :            :         /* Test to register bit rate without metrics init */
      57                 :          1 :         ret = rte_stats_bitrate_reg(bitrate_data);
      58         [ -  + ]:          1 :         TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_reg succeeded "
      59                 :            :                         "without metrics init, ret:%d", ret);
      60                 :            : 
      61                 :            :         /* Metrics initialization */
      62                 :          1 :         rte_metrics_init(rte_socket_id());
      63                 :            :         /* Test to register bit rate after metrics init */
      64                 :          1 :         ret = rte_stats_bitrate_reg(bitrate_data);
      65         [ -  + ]:          1 :         TEST_ASSERT((ret >= 0), "Test Failed: rte_stats_bitrate_reg %d", ret);
      66                 :            : 
      67                 :            :         return TEST_SUCCESS;
      68                 :            : }
      69                 :            : 
      70                 :            : /* To test the bit rate registration with invalid pointer */
      71                 :            : static int
      72                 :          1 : test_stats_bitrate_reg_invalidpointer(void)
      73                 :            : {
      74                 :            :         int ret = 0;
      75                 :            : 
      76                 :          1 :         ret = rte_stats_bitrate_reg(NULL);
      77         [ -  + ]:          1 :         TEST_ASSERT(ret < 0, "Test Failed: Expected failure < 0 but "
      78                 :            :                         "got %d", ret);
      79                 :            : 
      80                 :            :         return TEST_SUCCESS;
      81                 :            : }
      82                 :            : 
      83                 :            : /* To test bit rate calculation with invalid bit rate data pointer */
      84                 :            : static int
      85                 :          1 : test_stats_bitrate_calc_invalid_bitrate_data(void)
      86                 :            : {
      87                 :            :         int ret = 0;
      88                 :            : 
      89                 :          1 :         ret = rte_stats_bitrate_calc(NULL, portid);
      90         [ -  + ]:          1 :         TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_calc "
      91                 :            :                         "ret:%d", ret);
      92                 :            : 
      93                 :            :         return TEST_SUCCESS;
      94                 :            : }
      95                 :            : 
      96                 :            : /* To test the bit rate calculation with invalid portid
      97                 :            :  * (higher than max ports)
      98                 :            :  */
      99                 :            : static int
     100                 :          1 : test_stats_bitrate_calc_invalid_portid_1(void)
     101                 :            : {
     102                 :            :         int ret = 0;
     103                 :            : 
     104                 :          1 :         ret = rte_stats_bitrate_calc(bitrate_data, 33);
     105         [ -  + ]:          1 :         TEST_ASSERT(ret == -ENODEV, "Test Failed: Expected -%d for higher "
     106                 :            :                         "portid rte_stats_bitrate_calc ret:%d", ENODEV, ret);
     107                 :            : 
     108                 :            :         return TEST_SUCCESS;
     109                 :            : }
     110                 :            : 
     111                 :            : /* To test the bit rate calculation with invalid portid (lesser than 0) */
     112                 :            : static int
     113                 :          1 : test_stats_bitrate_calc_invalid_portid_2(void)
     114                 :            : {
     115                 :            :         int ret = 0;
     116                 :            : 
     117                 :          1 :         ret = rte_stats_bitrate_calc(bitrate_data, -1);
     118         [ -  + ]:          1 :         TEST_ASSERT(ret == -ENODEV, "Test Failed: Expected -%d for invalid "
     119                 :            :                         "portid rte_stats_bitrate_calc ret:%d", ENODEV, ret);
     120                 :            : 
     121                 :            :         return TEST_SUCCESS;
     122                 :            : }
     123                 :            : 
     124                 :            : /* To test the bit rate calculation with non-existing portid */
     125                 :            : static int
     126                 :          1 : test_stats_bitrate_calc_non_existing_portid(void)
     127                 :            : {
     128                 :            :         int ret = 0;
     129                 :            : 
     130                 :          1 :         ret = rte_stats_bitrate_calc(bitrate_data, 31);
     131         [ -  + ]:          1 :         TEST_ASSERT(ret ==  -ENODEV, "Test Failed: Expected -%d for "
     132                 :            :                         "non-existing portid rte_stats_bitrate_calc ret:%d",
     133                 :            :                         ENODEV, ret);
     134                 :            : 
     135                 :            :         return TEST_SUCCESS;
     136                 :            : }
     137                 :            : 
     138                 :            : /* To test the bit rate calculation with valid bit rate data, valid portid */
     139                 :            : static int
     140                 :          1 : test_stats_bitrate_calc(void)
     141                 :            : {
     142                 :            :         int ret = 0;
     143                 :            : 
     144                 :          1 :         ret = rte_stats_bitrate_calc(bitrate_data, portid);
     145         [ -  + ]:          1 :         TEST_ASSERT(ret >= 0, "Test Failed: Expected >=0 for valid portid "
     146                 :            :                         "rte_stats_bitrate_calc ret:%d", ret);
     147                 :            : 
     148                 :            :         return TEST_SUCCESS;
     149                 :            : }
     150                 :            : 
     151                 :            : static int
     152                 :          1 : test_bit_packet_forward(void)
     153                 :            : {
     154                 :            :         int ret;
     155                 :          1 :         struct rte_mbuf *pbuf[BIT_NUM_PACKETS] = { };
     156                 :            :         struct rte_mempool *mp;
     157                 :          1 :         char poolname[] = "mbuf_pool";
     158                 :          1 :         ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
     159         [ -  + ]:          1 :         if (ret < 0) {
     160                 :            :                 printf("allocate mbuf pool Failed\n");
     161                 :          0 :                 return TEST_FAILED;
     162                 :            :         }
     163                 :          1 :         ret = test_dev_start(portid, mp);
     164         [ -  + ]:          1 :         if (ret < 0) {
     165                 :          0 :                 printf("test_dev_start(%hu, %p) failed, error code: %d\n",
     166                 :            :                         portid, mp, ret);
     167                 :          0 :                 return TEST_FAILED;
     168                 :            :         }
     169                 :            : 
     170                 :          1 :         ret = test_packet_forward(pbuf, portid, QUEUE_ID);
     171         [ -  + ]:          1 :         if (ret < 0)
     172                 :            :                 printf("send pkts Failed\n");
     173                 :            : 
     174                 :          1 :         rte_eth_dev_stop(portid);
     175                 :          1 :         test_put_mbuf_to_pool(mp, pbuf);
     176                 :            : 
     177         [ -  + ]:          1 :         return (ret >= 0) ? TEST_SUCCESS : TEST_FAILED;
     178                 :            : }
     179                 :            : 
     180                 :            : static int
     181                 :          1 : test_bit_ring_setup(void)
     182                 :            : {
     183                 :          1 :         test_ring_setup(&ring, &portid);
     184                 :          1 :         printf("port in ring setup : %d\n", portid);
     185                 :            : 
     186                 :          1 :         return TEST_SUCCESS;
     187                 :            : }
     188                 :            : 
     189                 :            : static void
     190                 :          1 : test_bit_ring_free(void)
     191                 :            : {
     192                 :          1 :         test_ring_free(ring);
     193                 :          1 :         test_vdev_uninit("net_ring_net_ringa");
     194                 :          1 :         rte_memzone_free(rte_memzone_lookup("RTE_METRICS"));
     195                 :          1 : }
     196                 :            : 
     197                 :            : static struct
     198                 :            : unit_test_suite bitratestats_testsuite  = {
     199                 :            :         .suite_name = "BitRate Stats Unit Test Suite",
     200                 :            :         .setup = test_bit_ring_setup,
     201                 :            :         .teardown = test_bit_ring_free,
     202                 :            :         .unit_test_cases = {
     203                 :            :                 /* TEST CASE 1: Test to create bit rate data */
     204                 :            :                 TEST_CASE(test_stats_bitrate_create),
     205                 :            : 
     206                 :            :                 /* TEST CASE 2: Test to register bit rate metrics
     207                 :            :                  * without metrics init and after metrics init
     208                 :            :                  */
     209                 :            :                 TEST_CASE(test_stats_bitrate_reg),
     210                 :            : 
     211                 :            :                 /* TEST CASE 3: Test to register bit rate metrics
     212                 :            :                  * with invalid bit rate data
     213                 :            :                  */
     214                 :            :                 TEST_CASE(test_stats_bitrate_reg_invalidpointer),
     215                 :            : 
     216                 :            :                 /* TEST CASE 4: Test to calculate bit rate data metrics
     217                 :            :                  * with invalid bit rate data
     218                 :            :                  */
     219                 :            :                 TEST_CASE(test_stats_bitrate_calc_invalid_bitrate_data),
     220                 :            : 
     221                 :            :                 /* TEST CASE 5: Test to calculate bit rate data metrics
     222                 :            :                  * with portid exceeding the max ports
     223                 :            :                  */
     224                 :            :                 TEST_CASE(test_stats_bitrate_calc_invalid_portid_1),
     225                 :            : 
     226                 :            :                 /* TEST CASE 6: Test to calculate bit rate data metrics
     227                 :            :                  * with portid less than 0
     228                 :            :                  */
     229                 :            :                 TEST_CASE(test_stats_bitrate_calc_invalid_portid_2),
     230                 :            : 
     231                 :            :                 /* TEST CASE 7: Test to calculate bit rate data metrics
     232                 :            :                  * with non-existing portid
     233                 :            :                  */
     234                 :            :                 TEST_CASE(test_stats_bitrate_calc_non_existing_portid),
     235                 :            : 
     236                 :            :                 /* TEST CASE 8: Test to calculate bit rate data metrics
     237                 :            :                  * with valid portid, valid bit rate data
     238                 :            :                  */
     239                 :            :                 TEST_CASE_ST(test_bit_packet_forward, NULL,
     240                 :            :                                 test_stats_bitrate_calc),
     241                 :            :                 /* TEST CASE 9: Test to do the cleanup w.r.t create */
     242                 :            :                 TEST_CASE(test_stats_bitrate_free),
     243                 :            :                 TEST_CASES_END()
     244                 :            :         }
     245                 :            : };
     246                 :            : 
     247                 :            : static int
     248                 :          1 : test_bitratestats(void)
     249                 :            : {
     250                 :          1 :         return unit_test_suite_runner(&bitratestats_testsuite);
     251                 :            : }
     252                 :        252 : REGISTER_FAST_TEST(bitratestats_autotest, true, true, test_bitratestats);

Generated by: LCOV version 1.14