LCOV - code coverage report
Current view: top level - app/validate-bpf - parse_decl.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 173 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 18 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 <stdlib.h>
       9                 :            : 
      10                 :            : #include <rte_ether.h>
      11                 :            : #include <rte_ip.h>
      12                 :            : #include <rte_mbuf_core.h>
      13                 :            : #include <rte_tcp.h>
      14                 :            : #include <rte_udp.h>
      15                 :            : 
      16                 :            : #define RETURN_TEXT_ERROR(text, text_start, message, ...) do {                \
      17                 :            :         const int _offset = (text) - (text_start);                            \
      18                 :            :         VALIDATE_BPF_LOG(ERR, "at offset %d: " message,                       \
      19                 :            :                 _offset, ## __VA_ARGS__);                                     \
      20                 :            :         VALIDATE_BPF_LOG(NOTICE, "%s", text_start);                           \
      21                 :            :         VALIDATE_BPF_LOG(NOTICE, "%*c", _offset + 1, '^');                    \
      22                 :            :         return -1;                                                            \
      23                 :            : } while (0)
      24                 :            : 
      25                 :            : /* Used in place of any signature, this is not really being checked. */
      26                 :            : static uint64_t
      27                 :          0 : dummy_function(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4,
      28                 :            :         uint64_t arg5)
      29                 :            : {
      30                 :            :         RTE_SET_USED(arg1);
      31                 :            :         RTE_SET_USED(arg2);
      32                 :            :         RTE_SET_USED(arg3);
      33                 :            :         RTE_SET_USED(arg4);
      34                 :            :         RTE_SET_USED(arg5);
      35                 :          0 :         return 0;
      36                 :            : }
      37                 :            : 
      38                 :            : 
      39                 :            : /* TOKENS AND TYPES */
      40                 :            : 
      41                 :            : enum token {
      42                 :            :         TOKEN_UNRECOGNIZED = -1,
      43                 :            :         TOKEN_END = 0,
      44                 :            : 
      45                 :            :         TOKEN_ASTERISK,
      46                 :            :         TOKEN_BRACKET_CLOSE,
      47                 :            :         TOKEN_BRACKET_OPEN,
      48                 :            :         TOKEN_COMMA,
      49                 :            :         TOKEN_PARENTHESIS_CLOSE,
      50                 :            :         TOKEN_PARENTHESIS_OPEN,
      51                 :            :         TOKEN_STRUCT,
      52                 :            : 
      53                 :            :         TOKEN_TYPES_BEGIN,
      54                 :            : 
      55                 :            :         TOKEN_TYPE_CHAR,
      56                 :            :         TOKEN_TYPE_ETHER_HEADER,
      57                 :            :         TOKEN_TYPE_INT32_T,
      58                 :            :         TOKEN_TYPE_IP_HEADER,
      59                 :            :         TOKEN_TYPE_IP_HEADERS,
      60                 :            :         TOKEN_TYPE_RTE_MBUF,
      61                 :            :         TOKEN_TYPE_TCP_HEADER,
      62                 :            :         TOKEN_TYPE_TCP_HEADERS,
      63                 :            :         TOKEN_TYPE_UDP_HEADER,
      64                 :            :         TOKEN_TYPE_UDP_HEADERS,
      65                 :            :         TOKEN_TYPE_UINT32_T,
      66                 :            :         TOKEN_TYPE_UINT64_T,
      67                 :            :         TOKEN_TYPE_UINTPTR_T,
      68                 :            :         TOKEN_TYPE_VOID,
      69                 :            : 
      70                 :            :         TOKEN_TYPES_END,
      71                 :            : };
      72                 :            : 
      73                 :            : /* Return true if the token is a type token */
      74                 :            : static bool
      75                 :            : is_type_token(enum token token)
      76                 :            : {
      77                 :          0 :         return token > TOKEN_TYPES_BEGIN && token < TOKEN_TYPES_END;
      78                 :            : }
      79                 :            : 
      80                 :            : struct text_token {
      81                 :            :         const char *text;
      82                 :            :         unsigned int length;  /* Not size_t to fit this struct in 2 regs. */
      83                 :            :         enum token token;
      84                 :            : };
      85                 :            : 
      86                 :            : #define TEXT_TOKEN_DEF(text_, token_) {                                       \
      87                 :            :         .text = text_,                                                        \
      88                 :            :         .length = sizeof(text_) - 1,                                          \
      89                 :            :         .token = token_,                                                      \
      90                 :            : }
      91                 :            : 
      92                 :            : /* Token search table, MUST BE SORTED! */
      93                 :            : const struct text_token text_tokens[] = {
      94                 :            :         TEXT_TOKEN_DEF("(",                   TOKEN_PARENTHESIS_OPEN),
      95                 :            :         TEXT_TOKEN_DEF(")",                   TOKEN_PARENTHESIS_CLOSE),
      96                 :            :         TEXT_TOKEN_DEF("*",                   TOKEN_ASTERISK),
      97                 :            :         TEXT_TOKEN_DEF(",",                   TOKEN_COMMA),
      98                 :            :         TEXT_TOKEN_DEF("[",                   TOKEN_BRACKET_OPEN),
      99                 :            :         TEXT_TOKEN_DEF("]",                   TOKEN_BRACKET_CLOSE),
     100                 :            :         TEXT_TOKEN_DEF("char",                        TOKEN_TYPE_CHAR),
     101                 :            :         TEXT_TOKEN_DEF("ether_header",                TOKEN_TYPE_ETHER_HEADER),
     102                 :            :         TEXT_TOKEN_DEF("int32_t",             TOKEN_TYPE_INT32_T),
     103                 :            :         TEXT_TOKEN_DEF("ip_header",           TOKEN_TYPE_IP_HEADER),
     104                 :            :         TEXT_TOKEN_DEF("ip_headers",          TOKEN_TYPE_IP_HEADERS),
     105                 :            :         TEXT_TOKEN_DEF("rte_ether_hdr",               TOKEN_TYPE_ETHER_HEADER),
     106                 :            :         TEXT_TOKEN_DEF("rte_ipv4_hdr",                TOKEN_TYPE_IP_HEADER),
     107                 :            :         TEXT_TOKEN_DEF("rte_mbuf",            TOKEN_TYPE_RTE_MBUF),
     108                 :            :         TEXT_TOKEN_DEF("rte_tcp_hdr",         TOKEN_TYPE_TCP_HEADER),
     109                 :            :         TEXT_TOKEN_DEF("rte_udp_hdr",         TOKEN_TYPE_UDP_HEADER),
     110                 :            :         TEXT_TOKEN_DEF("struct",              TOKEN_STRUCT),
     111                 :            :         TEXT_TOKEN_DEF("tcp_header",          TOKEN_TYPE_TCP_HEADER),
     112                 :            :         TEXT_TOKEN_DEF("tcp_headers",         TOKEN_TYPE_TCP_HEADERS),
     113                 :            :         TEXT_TOKEN_DEF("udp_header",          TOKEN_TYPE_UDP_HEADER),
     114                 :            :         TEXT_TOKEN_DEF("udp_headers",         TOKEN_TYPE_UDP_HEADERS),
     115                 :            :         TEXT_TOKEN_DEF("uint32_t",            TOKEN_TYPE_UINT32_T),
     116                 :            :         TEXT_TOKEN_DEF("uint64_t",            TOKEN_TYPE_UINT64_T),
     117                 :            :         TEXT_TOKEN_DEF("uintptr_t",           TOKEN_TYPE_UINTPTR_T),
     118                 :            :         TEXT_TOKEN_DEF("void",                        TOKEN_TYPE_VOID),
     119                 :            : };
     120                 :            : 
     121                 :            : const struct text_token text_token_unrecognized = { "", 0, TOKEN_UNRECOGNIZED };
     122                 :            : const struct text_token text_token_end = { "", 0, TOKEN_END };
     123                 :            : 
     124                 :            : /* IP header maximum possible size with options. */
     125                 :            : #define IP_HEADER_MAX_SIZE 60
     126                 :            : #define IP_HEADERS_MAX_SIZE (sizeof(struct rte_ether_hdr) + IP_HEADER_MAX_SIZE)
     127                 :            : #define TCP_HEADERS_MAX_SIZE (IP_HEADERS_MAX_SIZE + sizeof(struct rte_tcp_hdr))
     128                 :            : #define UDP_HEADERS_MAX_SIZE (IP_HEADERS_MAX_SIZE + sizeof(struct rte_udp_hdr))
     129                 :            : 
     130                 :            : #define POINTER_SIZE (sizeof(char *))
     131                 :            : 
     132                 :            : const size_t type_token_size[] = {
     133                 :            :         [TOKEN_TYPE_CHAR]               = sizeof(char),
     134                 :            :         [TOKEN_TYPE_ETHER_HEADER]       = sizeof(struct rte_ether_hdr),
     135                 :            :         [TOKEN_TYPE_INT32_T]            = sizeof(int32_t),
     136                 :            :         [TOKEN_TYPE_IP_HEADERS]         = IP_HEADERS_MAX_SIZE,
     137                 :            :         [TOKEN_TYPE_IP_HEADER]          = sizeof(struct rte_ipv4_hdr),
     138                 :            :         [TOKEN_TYPE_TCP_HEADERS]        = TCP_HEADERS_MAX_SIZE,
     139                 :            :         [TOKEN_TYPE_TCP_HEADER]         = sizeof(struct rte_tcp_hdr),
     140                 :            :         [TOKEN_TYPE_UDP_HEADERS]        = UDP_HEADERS_MAX_SIZE,
     141                 :            :         [TOKEN_TYPE_UDP_HEADER]         = sizeof(struct rte_udp_hdr),
     142                 :            :         [TOKEN_TYPE_UINT32_T]           = sizeof(uint32_t),
     143                 :            :         [TOKEN_TYPE_UINT64_T]           = sizeof(uint64_t),
     144                 :            :         [TOKEN_TYPE_UINTPTR_T]          = sizeof(uintptr_t),
     145                 :            : };
     146                 :            : 
     147                 :            : /*
     148                 :            :  * Struct rte_bpf_arg augmented with a type of pointer to it, since rte_bpf_arg
     149                 :            :  * by itself may not contain enough information to determine its pointer type.
     150                 :            :  */
     151                 :            : struct arg_info {
     152                 :            :         struct rte_bpf_arg value;
     153                 :            :         enum rte_bpf_arg_type ptr_type;
     154                 :            :         bool is_array;
     155                 :            : };
     156                 :            : 
     157                 :            : /* Return arg_info struct described by the specified type token. */
     158                 :            : static struct arg_info
     159                 :            : get_type_token_arg(enum token type)
     160                 :            : {
     161                 :            :         RTE_ASSERT(is_type_token(type));
     162                 :          0 :         switch (type) {
     163                 :            :         case TOKEN_TYPE_VOID:
     164                 :            :                 return (struct arg_info){
     165                 :            :                         .value = { .type = RTE_BPF_ARG_UNDEF },
     166                 :            :                         .ptr_type = RTE_BPF_ARG_PTR,
     167                 :            :                 };
     168                 :          0 :         case TOKEN_TYPE_RTE_MBUF:
     169                 :          0 :                 return (struct arg_info){
     170                 :            :                         .value = {
     171                 :            :                                 .type = RTE_BPF_ARG_RAW,
     172                 :            :                                 .size = sizeof(struct rte_mbuf),
     173                 :            :                                 .buf_size = RTE_MBUF_DEFAULT_BUF_SIZE,
     174                 :            :                         },
     175                 :            :                         .ptr_type = RTE_BPF_ARG_PTR_MBUF,
     176                 :            :                 };
     177                 :          0 :         default:
     178                 :            :                 /* Should only reach here for normal sized types. */
     179                 :            :                 RTE_ASSERT((size_t)type < RTE_DIM(type_token_size) &&
     180                 :            :                         type_token_size[type] != 0);
     181                 :          0 :                 return (struct arg_info){
     182                 :            :                         .value = {
     183                 :            :                                 .type = RTE_BPF_ARG_RAW,
     184                 :          0 :                                 .size = type_token_size[type],
     185                 :            :                         },
     186                 :            :                         .ptr_type = RTE_BPF_ARG_PTR,
     187                 :            :                 };
     188                 :            :         }
     189                 :            : }
     190                 :            : 
     191                 :            : 
     192                 :            : /* PARSING TEXT INTO TOKENS */
     193                 :            : 
     194                 :            : /* Return true if character matches [a-zA-Z0-9_] in regex */
     195                 :            : static bool
     196                 :          0 : iswordchar(char character)
     197                 :            : {
     198                 :          0 :         return isalnum(character) || character == '_';
     199                 :            : }
     200                 :            : 
     201                 :            : /*
     202                 :            :  * Compare pointer to text with pointer to struct text_token to determine
     203                 :            :  * if text starts with the specified token.
     204                 :            :  */
     205                 :            : static int
     206                 :          0 : text_and_text_token_cmp(const void *text_void_ptr, const void *text_token_void_ptr)
     207                 :            : {
     208                 :            :         int result;
     209                 :            :         const char * const text = text_void_ptr;
     210                 :            :         const struct text_token * const text_token = text_token_void_ptr;
     211                 :            : 
     212                 :          0 :         result = strncmp(text, text_token->text, text_token->length);
     213                 :            : 
     214                 :            :         /* Checking the case of a partial word match. */
     215                 :          0 :         if (result == 0 && iswordchar(text[text_token->length - 1]) &&
     216                 :          0 :                         iswordchar(text[text_token->length]))
     217                 :            :                 /* Text word is longer than the token. */
     218                 :            :                 result = 1;
     219                 :            : 
     220                 :          0 :         return result;
     221                 :            : }
     222                 :            : 
     223                 :            : /* Advance pointed character pointer to the first non-space character. */
     224                 :            : static void
     225                 :          0 : skip_space(const char **text_ptr)
     226                 :            : {
     227                 :          0 :         while (isspace(**text_ptr))
     228                 :          0 :                 ++*text_ptr;
     229                 :          0 : }
     230                 :            : 
     231                 :            : /*
     232                 :            :  * Recognize and return a token starting text.
     233                 :            :  * Return text_token_end if text is empty.
     234                 :            :  * Return text_token_unrecognized if text starts with unknown token.
     235                 :            :  */
     236                 :            : static struct text_token
     237                 :          0 : peek_text_token(const char *text)
     238                 :            : {
     239                 :          0 :         if (*text == '\0')
     240                 :          0 :                 return text_token_end;
     241                 :            :         const struct text_token * const text_token = bsearch(text, text_tokens,
     242                 :            :                 RTE_DIM(text_tokens), sizeof(text_tokens[0]),
     243                 :            :                 text_and_text_token_cmp);
     244                 :          0 :         if (text_token == NULL)
     245                 :          0 :                 return text_token_unrecognized;
     246                 :          0 :         return *text_token;
     247                 :            : }
     248                 :            : 
     249                 :            : /*
     250                 :            :  * Advance pointed text starting with the specified token to the first
     251                 :            :  * non-space character after it.
     252                 :            :  * Do nothing if called for text_token_end or text_token_unrecognized.
     253                 :            :  */
     254                 :            : static void
     255                 :            : consume_text_token(const char **text_ptr, struct text_token text_token)
     256                 :            : {
     257                 :            :         RTE_ASSERT(memcmp(*text_ptr, text_token.text, text_token.length) == 0);
     258                 :          0 :         *text_ptr += text_token.length;
     259                 :          0 :         skip_space(text_ptr);
     260                 :            : }
     261                 :            : 
     262                 :            : /* Return length of the word starting at `text`. */
     263                 :            : static size_t
     264                 :            : find_word_length(const char *text)
     265                 :            : {
     266                 :            :         size_t word_length = 0;
     267                 :          0 :         while (iswordchar(text[word_length]))
     268                 :          0 :                 ++word_length;
     269                 :            :         return word_length;
     270                 :            : }
     271                 :            : 
     272                 :            : /* Create a copy of the word starting text, advance to next token. */
     273                 :            : static const char *
     274                 :          0 : take_name(const char **text_ptr, struct alloc_list *alloc_list)
     275                 :            : {
     276                 :          0 :         const size_t word_length = find_word_length(*text_ptr);
     277                 :          0 :         if (word_length == 0)
     278                 :            :                 /* Text does not start with a word. */
     279                 :            :                 return NULL;
     280                 :            : 
     281                 :            :         /* Allocate memory for the word and add it to the alloc_list */
     282                 :          0 :         char *word = malloc(word_length + 1);
     283                 :          0 :         RTE_VERIFY(word != NULL);
     284                 :          0 :         alloc_list_append(alloc_list, word);
     285                 :            : 
     286                 :            :         /* Copy and terminate word contents. */
     287                 :          0 :         memcpy(word, *text_ptr, word_length);
     288                 :          0 :         word[word_length] = '\0';
     289                 :            : 
     290                 :            :         /* Advance text pointer. */
     291                 :          0 :         *text_ptr += word_length;
     292                 :          0 :         skip_space(text_ptr);
     293                 :            : 
     294                 :          0 :         return word;
     295                 :            : }
     296                 :            : 
     297                 :            : /* Read a number starting text, advance to next token. */
     298                 :            : static int
     299                 :          0 : take_number(size_t *number, const char **text_ptr)
     300                 :            : {
     301                 :            :         /* Read a word and let strtoull to decide if it's a number. */
     302                 :          0 :         const size_t word_length = find_word_length(*text_ptr);
     303                 :          0 :         if (word_length == 0)
     304                 :            :                 /* Text does not start with a word. */
     305                 :            :                 return -ENOENT;
     306                 :            : 
     307                 :          0 :         errno = 0;
     308                 :            :         char *number_end;
     309                 :          0 :         unsigned long long_number = strtoul(*text_ptr, &number_end, 0);
     310                 :          0 :         if (errno > 0)
     311                 :          0 :                 return -errno;
     312                 :          0 :         if (number_end != *text_ptr + word_length)
     313                 :            :                 /* Could not parse whole word. */
     314                 :            :                 return -EINVAL;
     315                 :            : 
     316                 :          0 :         *number = long_number;
     317                 :            : 
     318                 :            :         /* Advance text pointer. */
     319                 :          0 :         *text_ptr += word_length;
     320                 :          0 :         skip_space(text_ptr);
     321                 :            : 
     322                 :          0 :         return 0;
     323                 :            : }
     324                 :            : 
     325                 :            : 
     326                 :            : /* PARSING DECLARATION PARTS */
     327                 :            : 
     328                 :            : /* Change arg into a reference. */
     329                 :            : static void
     330                 :          0 : change_into_reference(struct arg_info *arg)
     331                 :            : {
     332                 :          0 :         if (RTE_BPF_ARG_PTR_TYPE(arg->value.type) != 0) {
     333                 :          0 :                 VALIDATE_BPF_LOG(WARNING,
     334                 :            :                         "After taking reference to a pointer the latter "
     335                 :            :                         "will be described as an opaque pointer-size blob.");
     336                 :            :                 RTE_ASSERT(arg->ptr_type == RTE_BPF_ARG_PTR);
     337                 :          0 :                 arg->value.size = POINTER_SIZE;
     338                 :            :         }
     339                 :          0 :         arg->value.type = arg->ptr_type;
     340                 :          0 :         arg->ptr_type = RTE_BPF_ARG_PTR;
     341                 :          0 :         arg->is_array = false;
     342                 :          0 : }
     343                 :            : 
     344                 :            : /*
     345                 :            :  * Recognize and consume arg starting text, advance to next token.
     346                 :            :  */
     347                 :            : static int
     348                 :          0 : take_arg(struct arg_info *arg, const char **text_ptr, const char *text_start)
     349                 :            : {
     350                 :          0 :         struct text_token next = peek_text_token(*text_ptr);
     351                 :            : 
     352                 :          0 :         if (next.token == TOKEN_STRUCT) {
     353                 :            :                 consume_text_token(text_ptr, next);
     354                 :          0 :                 next = peek_text_token(*text_ptr);
     355                 :            :         }
     356                 :            : 
     357                 :          0 :         if (!is_type_token(next.token))
     358                 :          0 :                 RETURN_TEXT_ERROR(*text_ptr, text_start, "expect type");
     359                 :            :         const enum token type = next.token;
     360                 :            :         consume_text_token(text_ptr, next);
     361                 :          0 :         next = peek_text_token(*text_ptr);
     362                 :            : 
     363                 :          0 :         *arg = get_type_token_arg(type);
     364                 :            : 
     365                 :          0 :         while (next.token == TOKEN_ASTERISK) {
     366                 :            :                 consume_text_token(text_ptr, next);
     367                 :          0 :                 next = peek_text_token(*text_ptr);
     368                 :          0 :                 change_into_reference(arg);
     369                 :            :         }
     370                 :            : 
     371                 :          0 :         while (next.token == TOKEN_BRACKET_OPEN) {
     372                 :            :                 consume_text_token(text_ptr, next);
     373                 :            : 
     374                 :            :                 /* Initialize to zero to avoid spurious compiler warnings. */
     375                 :          0 :                 size_t array_length = 0;
     376                 :          0 :                 if (take_number(&array_length, text_ptr) < 0)
     377                 :          0 :                         RETURN_TEXT_ERROR(*text_ptr, text_start, "expect length");
     378                 :          0 :                 next = peek_text_token(*text_ptr);
     379                 :            : 
     380                 :          0 :                 if (arg->value.size != 0 &&
     381                 :          0 :                                 array_length > SIZE_MAX / arg->value.size)
     382                 :          0 :                         RETURN_TEXT_ERROR(*text_ptr, text_start, "type too big");
     383                 :            : 
     384                 :          0 :                 if (next.token != TOKEN_BRACKET_CLOSE)
     385                 :          0 :                         RETURN_TEXT_ERROR(*text_ptr, text_start, "expect ']'");
     386                 :            :                 consume_text_token(text_ptr, next);
     387                 :          0 :                 next = peek_text_token(*text_ptr);
     388                 :            : 
     389                 :          0 :                 change_into_reference(arg);
     390                 :          0 :                 arg->value.size *= array_length;
     391                 :          0 :                 arg->is_array = true;
     392                 :            :         }
     393                 :            : 
     394                 :            :         return 0;
     395                 :            : }
     396                 :            : 
     397                 :            : /* Fill struct rte_bpf_arg within xsym trying not to unzero the padding. */
     398                 :            : static void
     399                 :            : fill_xsym_arg(struct rte_bpf_arg *target, struct rte_bpf_arg source)
     400                 :            : {
     401                 :            :         /* Copy fields individually to try and prevent copying the padding. */
     402                 :          0 :         target->type = source.type;
     403                 :          0 :         target->size = source.size;
     404                 :          0 :         target->buf_size = source.buf_size;
     405                 :            : }
     406                 :            : 
     407                 :            : /* Build and return struct rte_bpf_arg of type RTE_BPF_XTYPE_VAR */
     408                 :            : static int
     409                 :          0 : fill_var_xsym(struct rte_bpf_xsym *xsym, struct arg_info arg, const char *name,
     410                 :            :         struct alloc_list *alloc_list)
     411                 :            : {
     412                 :            :         /* Variables are passed by reference, except for arrays. */
     413                 :          0 :         if (!arg.is_array) {
     414                 :          0 :                 if (RTE_BPF_ARG_PTR_TYPE(arg.value.type) != 0)
     415                 :          0 :                         VALIDATE_BPF_LOG(WARNING,
     416                 :            :                                 "External pointers may not work as expected "
     417                 :            :                                 "because all external variables are passed by "
     418                 :            :                                 "reference but there is currently no way to "
     419                 :            :                                 "describe double pointer to the validator.");
     420                 :          0 :                 change_into_reference(&arg);
     421                 :            :         }
     422                 :            : 
     423                 :            :         /* Allocate something to assign to a val pointer. */
     424                 :          0 :         void * const val = calloc(1, RTE_MAX(1u, arg.value.size));
     425                 :          0 :         if (val == NULL) {
     426                 :          0 :                 VALIDATE_BPF_LOG(ERR, "could not allocate enough memory");
     427                 :          0 :                 return -1;
     428                 :            :         }
     429                 :          0 :         alloc_list_append(alloc_list, val);
     430                 :            : 
     431                 :            :         /* Need all padding and unused fields to be zero-filled. */
     432                 :            :         memset(xsym, 0, sizeof(*xsym));
     433                 :          0 :         xsym->name = name;
     434                 :          0 :         xsym->type = RTE_BPF_XTYPE_VAR;
     435                 :          0 :         xsym->var.val = val;
     436                 :            :         fill_xsym_arg(&xsym->var.desc, arg.value);
     437                 :            : 
     438                 :          0 :         return 0;
     439                 :            : }
     440                 :            : 
     441                 :            : /* Build and return struct rte_bpf_arg of type RTE_BPF_XTYPE_FUNC */
     442                 :            : static void
     443                 :            : fill_func_xsym(struct rte_bpf_xsym *xsym, struct arg_info arg, const char *name)
     444                 :            : {
     445                 :            :         /* Need all padding and unused fields to be zero-filled. */
     446                 :            :         memset(xsym, 0, sizeof(*xsym));
     447                 :          0 :         xsym->name = name;
     448                 :            :         xsym->type = RTE_BPF_XTYPE_FUNC;
     449                 :          0 :         xsym->func.val = &dummy_function;
     450                 :            :         fill_xsym_arg(&xsym->func.ret, arg.value);
     451                 :            : }
     452                 :            : 
     453                 :            : /*
     454                 :            :  * Parse and store function arguments, advance to next token after ')'.
     455                 :            :  * Value of *text_ptr should point to the next token after '('.
     456                 :            :  */
     457                 :            : static int
     458                 :          0 : take_func_xsym_args(struct rte_bpf_xsym *xsym, const char **text_ptr,
     459                 :            :         const char *text_start)
     460                 :            : {
     461                 :            :         struct arg_info arg;
     462                 :          0 :         struct text_token delimiter = peek_text_token(*text_ptr);
     463                 :            : 
     464                 :          0 :         while (delimiter.token != TOKEN_PARENTHESIS_CLOSE) {
     465                 :          0 :                 if (xsym->func.nb_args == RTE_BPF_FUNC_MAX_ARGS)
     466                 :          0 :                         RETURN_TEXT_ERROR(*text_ptr, text_start,
     467                 :            :                                 "too many arguments, maximum %d allowed",
     468                 :            :                                 RTE_BPF_FUNC_MAX_ARGS);
     469                 :            : 
     470                 :          0 :                 if (take_arg(&arg, text_ptr, text_start) < 0)
     471                 :            :                         return -1;
     472                 :          0 :                 if (arg.value.type == RTE_BPF_ARG_UNDEF &&
     473                 :          0 :                                 xsym->func.nb_args != 0)
     474                 :          0 :                         RETURN_TEXT_ERROR(*text_ptr, text_start,
     475                 :            :                                 "arguments of type void are not allowed");
     476                 :          0 :                 fill_xsym_arg(&xsym->func.args[xsym->func.nb_args++],
     477                 :            :                         arg.value);
     478                 :            : 
     479                 :          0 :                 delimiter = peek_text_token(*text_ptr);
     480                 :          0 :                 switch (delimiter.token) {
     481                 :          0 :                 case TOKEN_COMMA:
     482                 :            :                         consume_text_token(text_ptr, delimiter);
     483                 :          0 :                         continue;
     484                 :            :                 case TOKEN_PARENTHESIS_CLOSE:
     485                 :            :                         break;
     486                 :          0 :                 default:
     487                 :          0 :                         RETURN_TEXT_ERROR(*text_ptr, text_start,
     488                 :            :                                 "expect ')' or ','");
     489                 :            :                 }
     490                 :            :         }
     491                 :            :         consume_text_token(text_ptr, delimiter);
     492                 :            : 
     493                 :            :         /* Special case of single void argument. */
     494                 :          0 :         if (xsym->func.nb_args == 1 &&
     495                 :          0 :                         xsym->func.args[0].type == RTE_BPF_ARG_UNDEF) {
     496                 :          0 :                 xsym->func.nb_args = 0;
     497                 :            :                 /* Need all padding and unused fields to be zero-filled. */
     498                 :            :                 fill_xsym_arg(&xsym->func.args[0], (struct rte_bpf_arg){});
     499                 :            :         }
     500                 :            : 
     501                 :            :         return 0;
     502                 :            : }
     503                 :            : 
     504                 :            : /* Make sure text has ended. */
     505                 :            : static int
     506                 :          0 : ensure_end(const char *text, const char *text_start)
     507                 :            : {
     508                 :          0 :         if (peek_text_token(text).token != TOKEN_END)
     509                 :          0 :                 RETURN_TEXT_ERROR(text, text_start, "trailing garbage");
     510                 :            :         return 0;
     511                 :            : }
     512                 :            : 
     513                 :            : /* Parse and store xsym, advance to next token. */
     514                 :            : static int
     515                 :          0 : take_xsym(struct rte_bpf_xsym *xsym, const char **text_ptr,
     516                 :            :         const char *text_start, struct alloc_list *alloc_list)
     517                 :            : {
     518                 :            :         struct arg_info arg;
     519                 :            : 
     520                 :          0 :         if (take_arg(&arg, text_ptr, text_start) < 0)
     521                 :            :                 return -1;
     522                 :            : 
     523                 :          0 :         const char * const name = take_name(text_ptr, alloc_list);
     524                 :          0 :         if (name == NULL)
     525                 :          0 :                 RETURN_TEXT_ERROR(*text_ptr, text_start, "expect name");
     526                 :            : 
     527                 :          0 :         const struct text_token next = peek_text_token(*text_ptr);
     528                 :          0 :         switch (next.token) {
     529                 :          0 :         case TOKEN_END:
     530                 :          0 :                 if (fill_var_xsym(xsym, arg, name, alloc_list) < 0)
     531                 :          0 :                         return -1;
     532                 :            :                 break;
     533                 :          0 :         case TOKEN_PARENTHESIS_OPEN:
     534                 :            :                 consume_text_token(text_ptr, next);
     535                 :            :                 fill_func_xsym(xsym, arg, name);
     536                 :          0 :                 if (take_func_xsym_args(xsym, text_ptr, text_start) < 0)
     537                 :          0 :                         return -1;
     538                 :            :                 break;
     539                 :          0 :         default:
     540                 :          0 :                 RETURN_TEXT_ERROR(*text_ptr, text_start,
     541                 :            :                         "expect '(' or text end");
     542                 :            :         }
     543                 :            :         return 0;
     544                 :            : }
     545                 :            : 
     546                 :            : 
     547                 :            : /* PUBLIC FUNCTIONS */
     548                 :            : 
     549                 :            : void
     550                 :          0 : print_supported_types(void)
     551                 :            : {
     552                 :            :         printf("TYPE: [struct] BASIC_TYPE [*]... [[N]]...\n");
     553                 :            :         printf("BASIC_TYPE: one of\n");
     554                 :          0 :         for (int tti = 0; tti != RTE_DIM(text_tokens); ++tti) {
     555                 :          0 :                 if (is_type_token(text_tokens[tti].token))
     556                 :          0 :                         printf("\t%s\n", text_tokens[tti].text);
     557                 :            :         }
     558                 :          0 : }
     559                 :            : 
     560                 :            : int
     561                 :          0 : parse_arg(struct rte_bpf_arg *arg, const char *text)
     562                 :            : {
     563                 :            :         struct arg_info arg_info;
     564                 :          0 :         const char * const text_start = text;
     565                 :          0 :         skip_space(&text);
     566                 :          0 :         if (take_arg(&arg_info, &text, text_start) < 0)
     567                 :            :                 return -1;
     568                 :          0 :         if (ensure_end(text, text_start) < 0)
     569                 :            :                 return -1;
     570                 :          0 :         if (arg_info.ptr_type != RTE_BPF_ARG_PTR)
     571                 :          0 :                 VALIDATE_BPF_LOG(WARNING,
     572                 :            :                         "`%s` has a special pointer type which was left unused; "
     573                 :            :                         "argument was set to an opaque blob.",
     574                 :            :                         text_start);
     575                 :          0 :         *arg = arg_info.value;
     576                 :          0 :         return 0;
     577                 :            : }
     578                 :            : 
     579                 :            : int
     580                 :          0 : parse_xsym(struct rte_bpf_xsym *xsym, const char *text,
     581                 :            :         struct alloc_list *alloc_list)
     582                 :            : {
     583                 :          0 :         const char * const text_start = text;
     584                 :          0 :         skip_space(&text);
     585                 :          0 :         if (take_xsym(xsym, &text, text_start, alloc_list) < 0)
     586                 :            :                 return -1;
     587                 :          0 :         if (ensure_end(text, text_start) < 0)
     588                 :          0 :                 return -1;
     589                 :            :         return 0;
     590                 :            : }
     591                 :            : 
     592                 :            : void
     593                 :          0 : adjust_arg_buf_size(struct rte_bpf_arg *arg, size_t mbuf_buf_size)
     594                 :            : {
     595                 :          0 :         if (arg->buf_size != 0)
     596                 :            :                 /* Non-zero buf_size indicates mbuf or a pointer to it. */
     597                 :          0 :                 arg->buf_size = mbuf_buf_size;
     598                 :          0 : }
     599                 :            : 
     600                 :            : void
     601                 :          0 : adjust_xsym_buf_size(struct rte_bpf_xsym *xsym, size_t mbuf_buf_size)
     602                 :            : {
     603                 :          0 :         switch (xsym->type) {
     604                 :            :         case RTE_BPF_XTYPE_FUNC:
     605                 :          0 :                 for (uint32_t argi = 0; argi != xsym->func.nb_args; ++argi)
     606                 :            :                         adjust_arg_buf_size(&xsym->func.args[argi],
     607                 :            :                                 mbuf_buf_size);
     608                 :            :                 adjust_arg_buf_size(&xsym->func.ret, mbuf_buf_size);
     609                 :            :                 break;
     610                 :          0 :         case RTE_BPF_XTYPE_VAR:
     611                 :            :                 adjust_arg_buf_size(&xsym->var.desc, mbuf_buf_size);
     612                 :            :                 break;
     613                 :          0 :         default:
     614                 :          0 :                 rte_panic("Unexpected xsym type %d\n", xsym->type);
     615                 :            :         }
     616                 :          0 : }

Generated by: LCOV version 1.14