From 07f7d55dd235c2ad590f86a34eb699fe12b2ec7d Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Tue, 28 Jan 2014 16:06:35 +0100 Subject: [PATCH] kvargs: use the new library in pmd_pcap The rte_kvargs library is a reworked copy of rte_eth_pcap_arg_parser, so it provides the same service. Therefore we can use it and remove the code of rte_eth_pcap_arg_parser. Signed-off-by: Olivier Matz Acked-by: Bruce Richardson --- lib/librte_pmd_pcap/Makefile | 8 +- lib/librte_pmd_pcap/rte_eth_pcap.c | 29 +- lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.c | 254 ------------------ lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.h | 71 ----- 4 files changed, 19 insertions(+), 343 deletions(-) delete mode 100644 lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.c delete mode 100644 lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.h diff --git a/lib/librte_pmd_pcap/Makefile b/lib/librte_pmd_pcap/Makefile index 48bb5fa09d..5218f28885 100644 --- a/lib/librte_pmd_pcap/Makefile +++ b/lib/librte_pmd_pcap/Makefile @@ -1,6 +1,7 @@ # BSD LICENSE # # Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +# Copyright(c) 2014 6WIND S.A. # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -43,17 +44,16 @@ CFLAGS += $(WERROR_FLAGS) # all source are stored in SRCS-y # SRCS-$(CONFIG_RTE_LIBRTE_PMD_PCAP) += rte_eth_pcap.c -SRCS-$(CONFIG_RTE_LIBRTE_PMD_PCAP) += rte_eth_pcap_arg_parser.c - # # Export include files # SYMLINK-y-include += rte_eth_pcap.h -SYMLINK-y-include += rte_eth_pcap_arg_parser.h # this lib depends upon: -DEPDIRS-$(CONFIG_RTE_LIBRTE_PMD_PCAP) += lib/librte_mbuf lib/librte_ether +DEPDIRS-$(CONFIG_RTE_LIBRTE_PMD_PCAP) += lib/librte_mbuf +DEPDIRS-$(CONFIG_RTE_LIBRTE_PMD_PCAP) += lib/librte_ether DEPDIRS-$(CONFIG_RTE_LIBRTE_PMD_PCAP) += lib/librte_malloc +DEPDIRS-$(CONFIG_RTE_LIBRTE_PMD_PCAP) += lib/librte_kvargs include $(RTE_SDK)/mk/rte.lib.mk diff --git a/lib/librte_pmd_pcap/rte_eth_pcap.c b/lib/librte_pmd_pcap/rte_eth_pcap.c index ff6d8b012b..11ad6c94a9 100644 --- a/lib/librte_pmd_pcap/rte_eth_pcap.c +++ b/lib/librte_pmd_pcap/rte_eth_pcap.c @@ -2,6 +2,7 @@ * BSD LICENSE * * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. + * Copyright(c) 2014 6WIND S.A. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,9 +39,9 @@ #include #include #include +#include #include "rte_eth_pcap.h" -#include "rte_eth_pcap_arg_parser.h" #define RTE_ETH_PCAP_SNAPSHOT_LEN 65535 #define RTE_ETH_PCAP_SNAPLEN 4096 @@ -669,11 +670,11 @@ rte_pmd_pcap_init(const char *name, const char *params) { unsigned numa_node, using_dumpers = 0; int ret; - struct args_dict dict; + struct rte_kvargs kvlist; struct rx_pcaps pcaps; struct tx_pcaps dumpers; - rte_eth_pcap_init_args_dict(&dict); + rte_kvargs_init(&kvlist); numa_node = rte_socket_id(); @@ -681,16 +682,16 @@ rte_pmd_pcap_init(const char *name, const char *params) start_cycles = rte_get_timer_cycles(); hz = rte_get_timer_hz(); - if (rte_eth_pcap_parse_args(&dict, name, params, valid_arguments) < 0) + if (rte_kvargs_parse(&kvlist, name, params, valid_arguments) < 0) return -1; /* * If iface argument is passed we open the NICs and use them for * reading / writing */ - if (rte_eth_pcap_num_of_args(&dict, ETH_PCAP_IFACE_ARG) == 1) { + if (rte_kvargs_count(&kvlist, ETH_PCAP_IFACE_ARG) == 1) { - ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_IFACE_ARG, + ret = rte_kvargs_process(&kvlist, ETH_PCAP_IFACE_ARG, &open_rx_tx_iface, &pcaps.pcaps[0]); if (ret < 0) return -1; @@ -702,13 +703,13 @@ rte_pmd_pcap_init(const char *name, const char *params) * We check whether we want to open a RX stream from a real NIC or a * pcap file */ - if ((pcaps.num_of_rx = rte_eth_pcap_num_of_args(&dict, ETH_PCAP_RX_PCAP_ARG))) { - ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_RX_PCAP_ARG, + if ((pcaps.num_of_rx = rte_kvargs_count(&kvlist, ETH_PCAP_RX_PCAP_ARG))) { + ret = rte_kvargs_process(&kvlist, ETH_PCAP_RX_PCAP_ARG, &open_rx_pcap, &pcaps); } else { - pcaps.num_of_rx = rte_eth_pcap_num_of_args(&dict, + pcaps.num_of_rx = rte_kvargs_count(&kvlist, ETH_PCAP_RX_IFACE_ARG); - ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_RX_IFACE_ARG, + ret = rte_kvargs_process(&kvlist, ETH_PCAP_RX_IFACE_ARG, &open_rx_iface, &pcaps); } @@ -719,15 +720,15 @@ rte_pmd_pcap_init(const char *name, const char *params) * We check whether we want to open a TX stream to a real NIC or a * pcap file */ - if ((dumpers.num_of_tx = rte_eth_pcap_num_of_args(&dict, + if ((dumpers.num_of_tx = rte_kvargs_count(&kvlist, ETH_PCAP_TX_PCAP_ARG))) { - ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_TX_PCAP_ARG, + ret = rte_kvargs_process(&kvlist, ETH_PCAP_TX_PCAP_ARG, &open_tx_pcap, &dumpers); using_dumpers = 1; } else { - dumpers.num_of_tx = rte_eth_pcap_num_of_args(&dict, + dumpers.num_of_tx = rte_kvargs_count(&kvlist, ETH_PCAP_TX_IFACE_ARG); - ret = rte_eth_pcap_post_process_arguments(&dict, ETH_PCAP_TX_IFACE_ARG, + ret = rte_kvargs_process(&kvlist, ETH_PCAP_TX_IFACE_ARG, &open_tx_iface, &dumpers); } diff --git a/lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.c b/lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.c deleted file mode 100644 index 2f447e0ca9..0000000000 --- a/lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.c +++ /dev/null @@ -1,254 +0,0 @@ -/*- - * BSD LICENSE - * - * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Intel Corporation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include -#include - -#include -#include -#include - -#include "rte_eth_pcap_arg_parser.h" - -/* - * Initializes a non NULL dictionary reference to be used later on. - */ -inline int -rte_eth_pcap_init_args_dict(struct args_dict *dict) -{ - dict->index = 0; - dict->size = RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS; - memset(dict->pairs, 0, dict->size); - return 0; -} - -/* - * Adds a key-value pair to a given non-NULL dictionary reference. - * The final key will be the name+key. - * Returns error in case the dictionary is full or if the key is duplicated. - */ -inline int -rte_eth_pcap_add_pair_to_dict(struct args_dict *dict, - char *key, - char *val) -{ - unsigned i; - struct key_value* entry; - - /* is the dictionary full? */ - if (dict->index >= dict->size) { - RTE_LOG(ERR, PMD, "Couldn't add %s, dictionary is full\n", key); - return -1; - } - - /* Check if the key is duplicated */ - for (i = 0; i < dict->index; i++) { - entry = &dict->pairs[i]; - if (strcmp(entry->key, key) == 0) { - RTE_LOG(ERR, PMD, "Couldn't add %s, duplicated key\n", key); - return -1; - } - } - - entry = &dict->pairs[dict->index]; - entry->key = key; - entry->value = val; - dict->index++; - return 0; - -} - -#define RTE_ETH_PCAP_PAIRS_DELIM ';' -#define RTE_ETH_PCAP_KEY_VALUE_DELIM '=' -/* - * Receives a string with a list of arguments following the pattern - * key=value;key=value;... and inserts them into the non NULL dictionary. - * strtok is used so the params string will be copied to be modified. - */ -inline int -rte_eth_pcap_tokenize_args(struct args_dict *dict, - const char *name, - const char *params) -{ - int i; - char *args; - char *pairs[RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS]; - char *pair[2]; - int num_of_pairs; - - /* If params are empty, nothing to do */ - if (params == NULL || params[0] == 0) { - RTE_LOG(ERR, PMD, "Couldn't parse %s device, empty arguments\n", name); - return -1; - } - - /* Copy the const char *params to a modifiable string - * to pass to rte_strsplit - */ - args = strdup(params); - if(args == NULL){ - RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name); - return -1; - } - - num_of_pairs = rte_strsplit(args, strnlen(args, sysconf(_SC_ARG_MAX)), pairs, - RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS, RTE_ETH_PCAP_PAIRS_DELIM); - - for (i = 0; i < num_of_pairs; i++) { - pair[0] = NULL; - pair[1] = NULL; - - rte_strsplit(pairs[i], strnlen(pairs[i], sysconf(_SC_ARG_MAX)), pair, 2, - RTE_ETH_PCAP_KEY_VALUE_DELIM); - - if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0 - || pair[1][0] == 0) { - RTE_LOG(ERR, PMD, - "Couldn't parse %s device, wrong key or value \n", name); - goto error; - } - - if (rte_eth_pcap_add_pair_to_dict(dict, pair[0], pair[1]) < 0) - goto error; - } - return 0; - -error: - rte_free(args); - return -1; -} - -/* - * Determines whether a key is valid or not by looking - * into a list of valid keys. - */ -static inline int -is_valid_key(const char *valid[], - struct key_value *pair) -{ - const char **valid_ptr; - - for (valid_ptr = valid; *valid_ptr != NULL; valid_ptr++) - if (strstr(pair->key, *valid_ptr) != NULL) - return 1; - return 0; -} - -/* - * Determines whether all keys are valid or not by looking - * into a list of valid keys. - */ -static inline int -check_for_valid_keys(struct args_dict *dict, - const char *valid[]) -{ - unsigned k_index, ret; - struct key_value *pair; - - for (k_index = 0; k_index < dict->index; k_index++) { - pair = &dict->pairs[k_index]; - ret = is_valid_key(valid, pair); - if (!ret) { - RTE_LOG(ERR, PMD, - "Error parsing device, invalid key %s\n", pair->key); - return -1; - } - } - return 0; -} - -/* - * Returns the number of times a given arg_name exists on a dictionary. - * E.g. given a dict = { rx0 = 0, rx1 = 1, tx0 = 2 } the number of args for - * arg "rx" will be 2. - */ -inline unsigned -rte_eth_pcap_num_of_args(struct args_dict *dict, const char *arg_name) -{ - unsigned k_index; - struct key_value *pair; - unsigned num_of_keys; - - num_of_keys = 0; - for (k_index = 0; k_index < dict->index; k_index++) { - pair = &dict->pairs[k_index]; - if (strcmp(pair->key, arg_name) == 0) - num_of_keys++; - } - - return num_of_keys; -} - -/* - * Calls the handler function for a given arg_name passing the - * value on the dictionary for that key and a given extra argument. - */ -inline int -rte_eth_pcap_post_process_arguments(struct args_dict *dict, - const char *arg_name, - arg_handler_t handler, - void *extra_args) -{ - unsigned k_index; - struct key_value *pair; - - for (k_index = 0; k_index < dict->index; k_index++) { - pair = &dict->pairs[k_index]; - if (strstr(pair->key, arg_name) != NULL) { - if ((*handler)(pair->value, extra_args) < 0) - return -1; - } - } - return 0; -} - -/* - * Parses the arguments "key=value;key=value;..." string and returns - * a simple dictionary implementation containing these pairs. It also - * checks if only valid keys were used. - */ -inline int -rte_eth_pcap_parse_args(struct args_dict *dict, - const char *name, - const char *args, - const char *valids[]) -{ - - int ret; - - ret = rte_eth_pcap_tokenize_args(dict, name, args); - if (ret < 0) - return ret; - - return check_for_valid_keys(dict, valids); -} - diff --git a/lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.h b/lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.h deleted file mode 100644 index c52430c900..0000000000 --- a/lib/librte_pmd_pcap/rte_eth_pcap_arg_parser.h +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * BSD LICENSE - * - * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Intel Corporation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _RTE_ETH_ARG_PARSER_H_ -#define _RTE_ETH_ARG_PARSER_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#define RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS 32 - -typedef int (*arg_handler_t)(char*, void*); - -struct key_value { - char *key; - char *value; -}; - -struct args_dict { - unsigned index; - size_t size; - struct key_value pairs[RTE_ETH_PCAP_ARG_PARSER_MAX_ARGS]; -}; - -int rte_eth_pcap_tokenize_args(struct args_dict *dict, const char *name, - const char *args); -int rte_eth_pcap_init_args_dict(struct args_dict *dict); -int rte_eth_pcap_add_pair_to_dict(struct args_dict *dict, char *key, char *val); -int rte_eth_pcap_parse_args(struct args_dict *dict, const char* name, - const char *args, const char *valids[]); -int rte_eth_pcap_post_process_arguments(struct args_dict *dict, - const char *arg_name, arg_handler_t handler, void *extra_args); -unsigned rte_eth_pcap_num_of_args(struct args_dict *dict, const char *key); -void rte_eth_pcap_free_dict(struct args_dict *dict); - -#ifdef __cplusplus -} -#endif - -#endif -- 2.20.1