LCOV - code coverage report
Current view: top level - app/validate-bpf - args.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 85 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 5 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2025 Huawei Technologies Co., Ltd
       3                 :            :  */
       4                 :            : 
       5                 :            : #include "internal.h"
       6                 :            : 
       7                 :            : #include <ctype.h>
       8                 :            : #include <getopt.h>
       9                 :            : #include <stdlib.h>
      10                 :            : 
      11                 :            : #include <rte_errno.h>
      12                 :            : 
      13                 :            : 
      14                 :            : /* Values to be used in getopt_long option.val. */
      15                 :            : enum app_args {
      16                 :            :         ARG_UNRECOGNIZED = '?',
      17                 :            :         ARG_AUTO = 0,   /* Value set by getopt_long when flag is non-NULL. */
      18                 :            :         ARG_HELP,       /* Keep this one in the beginning for UX reasons. */
      19                 :            :         ARG_DEBUG,
      20                 :            :         ARG_MBUF_BUF_SIZE,
      21                 :            :         ARG_NO_PROG_ARGS,
      22                 :            :         ARG_PROG_ARG,
      23                 :            :         ARG_SECTION,
      24                 :            :         ARG_XSYM,
      25                 :            : };
      26                 :            : 
      27                 :            : /* Program options. */
      28                 :            : static struct option app_options[] = {
      29                 :            :         {
      30                 :            :                 .name = "help",
      31                 :            :                 .val = ARG_HELP,
      32                 :            :         },
      33                 :            :         {
      34                 :            :                 .name = "debug",
      35                 :            :                 .val = ARG_DEBUG,
      36                 :            :         },
      37                 :            :         {
      38                 :            :                 .name = "mbuf-buf-size",
      39                 :            :                 .has_arg = required_argument,
      40                 :            :                 .val = ARG_MBUF_BUF_SIZE,
      41                 :            :         },
      42                 :            :         {
      43                 :            :                 .name = "no-prog-arg",
      44                 :            :                 .val = ARG_NO_PROG_ARGS,
      45                 :            :         },
      46                 :            :         {
      47                 :            :                 .name = "no-prog-args",
      48                 :            :                 .val = ARG_NO_PROG_ARGS,
      49                 :            :         },
      50                 :            :         {
      51                 :            :                 .name = "prog-arg",
      52                 :            :                 .has_arg = required_argument,
      53                 :            :                 .val = ARG_PROG_ARG,
      54                 :            :         },
      55                 :            :         {
      56                 :            :                 .name = "section",
      57                 :            :                 .has_arg = required_argument,
      58                 :            :                 .val = ARG_SECTION,
      59                 :            :         },
      60                 :            :         {
      61                 :            :                 .name = "xsym",
      62                 :            :                 .has_arg = required_argument,
      63                 :            :                 .val = ARG_XSYM,
      64                 :            :         },
      65                 :            :         { /* terminating zero record */ }
      66                 :            : };
      67                 :            : 
      68                 :            : /* Default args value. */
      69                 :            : static const struct args args_default = {
      70                 :            :         .bpf_prm = {
      71                 :            :                 .sz = sizeof(struct rte_bpf_prm_ex),
      72                 :            :                 .origin = RTE_BPF_ORIGIN_ELF_FILE,
      73                 :            :                 .elf_file.section = ".text",
      74                 :            :         },
      75                 :            :         .mbuf_buf_size = RTE_MBUF_DEFAULT_BUF_SIZE,
      76                 :            : };
      77                 :            : 
      78                 :            : /* Default --prog-arg argument value. */
      79                 :            : static const char * const prog_arg_default = "struct rte_mbuf *";
      80                 :            : 
      81                 :            : 
      82                 :            : void
      83                 :          0 : print_usage(const char *program_name)
      84                 :            : {
      85                 :            :         static const char *const options[][2] = {
      86                 :            :                 { "--help", "Display help and exit." },
      87                 :            :                 { "--debug", "Enable interactive debug mode." },
      88                 :            :                 { "--mbuf-buf-size=MBUF_BUF_SIZE", "Size of the mbuf data buffer (in bytes)." },
      89                 :            :                 { "--prog-arg=TYPE", "Expected type of the next BPF program argument (up to 5)." },
      90                 :            :                 { "--no-prog-args", "BPF program does not take any arguments." },
      91                 :            :                 { "--section=SECTION", "ELF section name in the BPF object file to load." },
      92                 :            :                 { "--xsym='TYPE NAME[(TYPE, ...)]'", "External symbol BPF program can access." },
      93                 :            :         };
      94                 :            : 
      95                 :            :         printf("USAGE: %s [OPTIONS]... BPF_PATH\n", program_name);
      96                 :            :         printf("OPTIONS:\n");
      97                 :          0 :         for (int oi = 0; oi != RTE_DIM(options); ++oi)
      98                 :          0 :                 printf("\t%-31s %s\n", options[oi][0], options[oi][1]);
      99                 :          0 : }
     100                 :            : 
     101                 :            : void
     102                 :          0 : print_defaults(void)
     103                 :            : {
     104                 :            :         printf("DEFAULTS:"
     105                 :            :                         " --mbuf-buf-size=%#zx"
     106                 :            :                         " --prog-arg='%s'"
     107                 :            :                         " --section='%s'\n",
     108                 :            :                 (size_t)RTE_MBUF_DEFAULT_BUF_SIZE,
     109                 :            :                 prog_arg_default,
     110                 :            :                 args_default.bpf_prm.elf_file.section);
     111                 :          0 : }
     112                 :            : 
     113                 :            : static int
     114                 :          0 : parse_size(size_t *result, const char *text)
     115                 :            : {
     116                 :          0 :         char *parse_end = NULL;
     117                 :            : 
     118                 :          0 :         errno = 0;
     119                 :          0 :         const unsigned long strtoul_result = strtoul(text, &parse_end, 0);
     120                 :          0 :         if (errno != 0 || *parse_end != '\0' || strtoul_result == 0)
     121                 :            :                 return -1;
     122                 :            : 
     123                 :          0 :         *result = strtoul_result;
     124                 :          0 :         return 0;
     125                 :            : }
     126                 :            : 
     127                 :            : struct args *
     128                 :          0 : args_parse(int argc, char *argv[])
     129                 :            : {
     130                 :            :         int val;
     131                 :            :         size_t xsym_alloc_index;
     132                 :            :         struct rte_bpf_xsym *xsym = NULL;
     133                 :          0 :         const char * const program_name = argv[0];
     134                 :            : 
     135                 :            :         /* Allocate args and set aliases for some of its members. */
     136                 :          0 :         struct args * const args = malloc(sizeof(*args));
     137                 :          0 :         RTE_VERIFY(args != NULL);
     138                 :          0 :         struct alloc_list * const alloc_list = &args->_alloc_list;
     139                 :            :         struct rte_bpf_prm_ex * const bpf_prm = &args->bpf_prm;
     140                 :            : 
     141                 :            :         /* Set default values. */
     142                 :          0 :         *args = args_default;
     143                 :          0 :         RTE_VERIFY(parse_arg(&bpf_prm->prog_arg[bpf_prm->nb_prog_arg++],
     144                 :            :                 prog_arg_default) >= 0);
     145                 :            :         bool default_prog_args = true;
     146                 :            : 
     147                 :            :         /* Reserve space for xsym in alloc_list. */
     148                 :          0 :         xsym_alloc_index = alloc_list_append(alloc_list, xsym);
     149                 :            : 
     150                 :          0 :         while ((val = getopt_long(argc, argv, "", app_options, NULL)) != EOF) {
     151                 :            :                 int rc = 0;
     152                 :          0 :                 switch (val) {
     153                 :            :                 case ARG_AUTO:
     154                 :            :                         /* getopt_long made the assignment, nothing to do */
     155                 :            :                         break;
     156                 :          0 :                 case ARG_HELP:
     157                 :          0 :                         args->show_help = true;
     158                 :          0 :                         break;
     159                 :          0 :                 case ARG_DEBUG:
     160                 :          0 :                         if (bpf_prm->debug == NULL)
     161                 :          0 :                                 bpf_prm->debug = debug_create();
     162                 :          0 :                         if (bpf_prm->debug == NULL) {
     163                 :          0 :                                 rc = -rte_errno;
     164                 :          0 :                                 fprintf(stderr,
     165                 :            :                                         "%s: error %d creating debug session\n",
     166                 :            :                                         program_name, -rc);
     167                 :            :                         }
     168                 :            :                         break;
     169                 :          0 :                 case ARG_MBUF_BUF_SIZE:
     170                 :          0 :                         rc = parse_size(&args->mbuf_buf_size, optarg);
     171                 :          0 :                         if (rc < 0)
     172                 :          0 :                                 fprintf(stderr,
     173                 :            :                                         "%s: invalid mbuf buf size '%s'\n",
     174                 :            :                                         program_name, optarg);
     175                 :            :                         break;
     176                 :          0 :                 case ARG_NO_PROG_ARGS:
     177                 :          0 :                         bpf_prm->nb_prog_arg = 0;
     178                 :            :                         default_prog_args = false;
     179                 :          0 :                         break;
     180                 :          0 :                 case ARG_PROG_ARG:
     181                 :          0 :                         if (default_prog_args) {
     182                 :          0 :                                 bpf_prm->nb_prog_arg = 0;
     183                 :            :                                 default_prog_args = false;
     184                 :            :                         }
     185                 :            : 
     186                 :          0 :                         if (bpf_prm->nb_prog_arg == RTE_DIM(bpf_prm->prog_arg)) {
     187                 :          0 :                                 fprintf(stderr,
     188                 :            :                                         "%s: at most %d program arguments allowed\n",
     189                 :            :                                         program_name,
     190                 :            :                                         (int)RTE_DIM(bpf_prm->prog_arg));
     191                 :            :                                 rc = -EINVAL;
     192                 :          0 :                                 break;
     193                 :            :                         }
     194                 :            : 
     195                 :          0 :                         rc = parse_arg(&bpf_prm->prog_arg[bpf_prm->nb_prog_arg++],
     196                 :            :                                 optarg);
     197                 :          0 :                         if (rc < 0)
     198                 :          0 :                                 fprintf(stderr,
     199                 :            :                                         "%s: unrecognized prog arg '%s'\n",
     200                 :            :                                         program_name, optarg);
     201                 :            : 
     202                 :            :                         break;
     203                 :          0 :                 case ARG_SECTION:
     204                 :          0 :                         bpf_prm->elf_file.section = optarg;
     205                 :          0 :                         break;
     206                 :          0 :                 case ARG_XSYM:
     207                 :          0 :                         bpf_prm->xsym = xsym = realloc(xsym,
     208                 :          0 :                                 sizeof(xsym[0]) * (bpf_prm->nb_xsym + 1));
     209                 :          0 :                         RTE_VERIFY(xsym != NULL);
     210                 :          0 :                         alloc_list_replace(alloc_list, xsym_alloc_index, xsym);
     211                 :          0 :                         rc = parse_xsym(&xsym[bpf_prm->nb_xsym++], optarg,
     212                 :            :                                         alloc_list);
     213                 :          0 :                         if (rc < 0)
     214                 :          0 :                                 fprintf(stderr, "%s: invalid xsym '%s'\n",
     215                 :            :                                         program_name, optarg);
     216                 :            :                         break;
     217                 :          0 :                 case ARG_UNRECOGNIZED:
     218                 :          0 :                         args_destroy(args);
     219                 :          0 :                         return NULL;
     220                 :          0 :                 default:
     221                 :          0 :                         rte_panic("Unexpected getopt_long return value %d\n",
     222                 :            :                                 val);
     223                 :            :                 }
     224                 :          0 :                 if (rc < 0) {
     225                 :          0 :                         args_destroy(args);
     226                 :          0 :                         return NULL;
     227                 :            :                 }
     228                 :            :         }
     229                 :            : 
     230                 :            :         /* Set buf_size to the value specified in command line arguments. */
     231                 :          0 :         adjust_arg_buf_size(bpf_prm->prog_arg, args->mbuf_buf_size);
     232                 :          0 :         for (size_t xsymi = 0; xsymi != bpf_prm->nb_xsym; ++xsymi)
     233                 :          0 :                 adjust_xsym_buf_size(&xsym[xsymi], args->mbuf_buf_size);
     234                 :            : 
     235                 :            :         /* getopt_long moves all non-options to the end, starting at optind. */
     236                 :          0 :         const int nb_bpf_path = argc - optind;
     237                 :          0 :         switch (nb_bpf_path) {
     238                 :            :         case 0:
     239                 :            :                 break;
     240                 :          0 :         case 1:
     241                 :          0 :                 bpf_prm->elf_file.path = argv[optind];
     242                 :          0 :                 break;
     243                 :          0 :         default:
     244                 :          0 :                 fprintf(stderr, "%s: too many positional arguments\n",
     245                 :            :                         program_name);
     246                 :          0 :                 args_destroy(args);
     247                 :          0 :                 return NULL;
     248                 :            :         }
     249                 :            : 
     250                 :            :         return args;
     251                 :            : }
     252                 :            : 
     253                 :            : void
     254                 :          0 : args_destroy(struct args *args)
     255                 :            : {
     256                 :          0 :         if (args == NULL)
     257                 :            :                 return;
     258                 :          0 :         if (args->bpf_prm.debug != NULL)
     259                 :          0 :                 debug_destroy(args->bpf_prm.debug);
     260                 :          0 :         alloc_list_free_all(&args->_alloc_list);
     261                 :          0 :         free(args);
     262                 :            : }

Generated by: LCOV version 1.14