1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2023 Marvell. 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 #include <cmdline_parse.h> 10 #include <cmdline_parse_num.h> 11 #include <cmdline_parse_string.h> 12 #include <cmdline_socket.h> 13 #include <rte_common.h> 14 #include <rte_mbuf.h> 15 16 #include "mempool_priv.h" 17 #include "module_api.h" 18 19 static const char 20 cmd_mempool_help[] = "mempool <mempool_name> size <mbuf_size> buffers <number_of_buffers> " 21 "cache <cache_size> numa <numa_id>"; 22 23 struct mempools mpconfig; 24 25 int 26 mempool_process(struct mempool_config *config) 27 { 28 struct rte_mempool *mp; 29 uint8_t nb_pools; 30 31 nb_pools = mpconfig.nb_pools; 32 rte_strscpy(mpconfig.config[nb_pools].name, config->name, RTE_MEMPOOL_NAMESIZE); 33 mpconfig.config[nb_pools].pool_size = config->pool_size; 34 mpconfig.config[nb_pools].buffer_size = config->buffer_size; 35 mpconfig.config[nb_pools].cache_size = config->cache_size; 36 mpconfig.config[nb_pools].numa_node = config->numa_node; 37 38 mp = rte_pktmbuf_pool_create(config->name, config->pool_size, config->cache_size, 39 128, config->buffer_size, config->numa_node); 40 if (!mp) 41 return -EINVAL; 42 43 mpconfig.mp[nb_pools] = mp; 44 nb_pools++; 45 mpconfig.nb_pools = nb_pools; 46 47 return 0; 48 } 49 50 static void 51 cli_mempool_help(__rte_unused void *parsed_result, __rte_unused struct cmdline *cl, 52 __rte_unused void *data) 53 { 54 size_t len; 55 56 len = strlen(conn->msg_out); 57 conn->msg_out += len; 58 snprintf(conn->msg_out, conn->msg_out_len_max, "\n%s\n%s\n", 59 "----------------------------- mempool command help -----------------------------", 60 cmd_mempool_help); 61 62 len = strlen(conn->msg_out); 63 conn->msg_out_len_max -= len; 64 } 65 66 static void 67 cli_mempool(void *parsed_result, __rte_unused struct cmdline *cl, __rte_unused void *data) 68 { 69 struct mempool_config_cmd_tokens *res = parsed_result; 70 struct mempool_config config; 71 int rc = -EINVAL; 72 73 74 rte_strscpy(config.name, res->name, RTE_MEMPOOL_NAMESIZE); 75 config.name[strlen(res->name)] = '\0'; 76 config.pool_size = res->nb_bufs; 77 config.buffer_size = res->buf_sz; 78 config.cache_size = res->cache_size; 79 config.numa_node = res->node; 80 81 rc = mempool_process(&config); 82 if (rc < 0) 83 printf(MSG_CMD_FAIL, "mempool"); 84 } 85 86 cmdline_parse_token_string_t mempool_config_add_mempool = 87 TOKEN_STRING_INITIALIZER(struct mempool_config_cmd_tokens, mempool, "mempool"); 88 cmdline_parse_token_string_t mempool_config_add_name = 89 TOKEN_STRING_INITIALIZER(struct mempool_config_cmd_tokens, name, NULL); 90 cmdline_parse_token_string_t mempool_config_add_size = 91 TOKEN_STRING_INITIALIZER(struct mempool_config_cmd_tokens, size, "size"); 92 cmdline_parse_token_num_t mempool_config_add_buf_sz = 93 TOKEN_NUM_INITIALIZER(struct mempool_config_cmd_tokens, buf_sz, RTE_UINT16); 94 cmdline_parse_token_string_t mempool_config_add_buffers = 95 TOKEN_STRING_INITIALIZER(struct mempool_config_cmd_tokens, buffers, "buffers"); 96 cmdline_parse_token_num_t mempool_config_add_nb_bufs = 97 TOKEN_NUM_INITIALIZER(struct mempool_config_cmd_tokens, nb_bufs, RTE_UINT16); 98 cmdline_parse_token_string_t mempool_config_add_cache = 99 TOKEN_STRING_INITIALIZER(struct mempool_config_cmd_tokens, cache, "cache"); 100 cmdline_parse_token_num_t mempool_config_add_cache_size = 101 TOKEN_NUM_INITIALIZER(struct mempool_config_cmd_tokens, cache_size, RTE_UINT16); 102 cmdline_parse_token_string_t mempool_config_add_numa = 103 TOKEN_STRING_INITIALIZER(struct mempool_config_cmd_tokens, numa, "numa"); 104 cmdline_parse_token_num_t mempool_config_add_node = 105 TOKEN_NUM_INITIALIZER(struct mempool_config_cmd_tokens, node, RTE_UINT16); 106 107 cmdline_parse_inst_t mempool_config_cmd_ctx = { 108 .f = cli_mempool, 109 .data = NULL, 110 .help_str = cmd_mempool_help, 111 .tokens = { 112 (void *)&mempool_config_add_mempool, 113 (void *)&mempool_config_add_name, 114 (void *)&mempool_config_add_size, 115 (void *)&mempool_config_add_buf_sz, 116 (void *)&mempool_config_add_buffers, 117 (void *)&mempool_config_add_nb_bufs, 118 (void *)&mempool_config_add_cache, 119 (void *)&mempool_config_add_cache_size, 120 (void *)&mempool_config_add_numa, 121 (void *)&mempool_config_add_node, 122 NULL, 123 }, 124 }; 125 126 cmdline_parse_token_string_t mempool_help_cmd = 127 TOKEN_STRING_INITIALIZER(struct mempool_help_cmd_tokens, help, "help"); 128 cmdline_parse_token_string_t mempool_help_mempool = 129 TOKEN_STRING_INITIALIZER(struct mempool_help_cmd_tokens, mempool, "mempool"); 130 131 cmdline_parse_inst_t mempool_help_cmd_ctx = { 132 .f = cli_mempool_help, 133 .data = NULL, 134 .help_str = "", 135 .tokens = { 136 (void *)&mempool_help_cmd, 137 (void *)&mempool_help_mempool, 138 NULL, 139 }, 140 }; 141