devlink: Introduce and use string to number mapper
Instead of using static mapping in code, introduce a helper routine to map a value to string. Signed-off-by: Parav Pandit <parav@nvidia.com> Signed-off-by: David Ahern <dsahern@kernel.org>
This commit is contained in:
parent
1e61902180
commit
a9642c5fa6
|
|
@ -1383,6 +1383,16 @@ static int reload_limit_get(struct dl *dl, const char *limitstr,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct str_num_map port_flavour_map[] = {
|
||||||
|
{ .str = "physical", .num = DEVLINK_PORT_FLAVOUR_PHYSICAL },
|
||||||
|
{ .str = "cpu", .num = DEVLINK_PORT_FLAVOUR_CPU },
|
||||||
|
{ .str = "dsa", .num = DEVLINK_PORT_FLAVOUR_DSA },
|
||||||
|
{ .str = "pcipf", .num = DEVLINK_PORT_FLAVOUR_PCI_PF },
|
||||||
|
{ .str = "pcivf", .num = DEVLINK_PORT_FLAVOUR_PCI_VF },
|
||||||
|
{ .str = "virtual", .num = DEVLINK_PORT_FLAVOUR_VIRTUAL},
|
||||||
|
{ .str = NULL, },
|
||||||
|
};
|
||||||
|
|
||||||
struct dl_args_metadata {
|
struct dl_args_metadata {
|
||||||
uint64_t o_flag;
|
uint64_t o_flag;
|
||||||
char err_msg[DL_ARGS_REQUIRED_MAX_ERR_LEN];
|
char err_msg[DL_ARGS_REQUIRED_MAX_ERR_LEN];
|
||||||
|
|
@ -3717,22 +3727,10 @@ static const char *port_type_name(uint32_t type)
|
||||||
|
|
||||||
static const char *port_flavour_name(uint16_t flavour)
|
static const char *port_flavour_name(uint16_t flavour)
|
||||||
{
|
{
|
||||||
switch (flavour) {
|
const char *str;
|
||||||
case DEVLINK_PORT_FLAVOUR_PHYSICAL:
|
|
||||||
return "physical";
|
str = str_map_lookup_u16(port_flavour_map, flavour);
|
||||||
case DEVLINK_PORT_FLAVOUR_CPU:
|
return str ? str : "<unknown flavour>";
|
||||||
return "cpu";
|
|
||||||
case DEVLINK_PORT_FLAVOUR_DSA:
|
|
||||||
return "dsa";
|
|
||||||
case DEVLINK_PORT_FLAVOUR_PCI_PF:
|
|
||||||
return "pcipf";
|
|
||||||
case DEVLINK_PORT_FLAVOUR_PCI_VF:
|
|
||||||
return "pcivf";
|
|
||||||
case DEVLINK_PORT_FLAVOUR_VIRTUAL:
|
|
||||||
return "virtual";
|
|
||||||
default:
|
|
||||||
return "<unknown flavour>";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pr_out_port_pfvf_num(struct dl *dl, struct nlattr **tb)
|
static void pr_out_port_pfvf_num(struct dl *dl, struct nlattr **tb)
|
||||||
|
|
|
||||||
|
|
@ -340,4 +340,12 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
|
||||||
int (*mapping_cb)(__u32 key, char *value, void *data),
|
int (*mapping_cb)(__u32 key, char *value, void *data),
|
||||||
void *mapping_cb_data);
|
void *mapping_cb_data);
|
||||||
|
|
||||||
|
struct str_num_map {
|
||||||
|
const char *str;
|
||||||
|
int num;
|
||||||
|
};
|
||||||
|
|
||||||
|
int str_map_lookup_str(const struct str_num_map *map, const char *needle);
|
||||||
|
const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val);
|
||||||
|
|
||||||
#endif /* __UTILS_H__ */
|
#endif /* __UTILS_H__ */
|
||||||
|
|
|
||||||
28
lib/utils.c
28
lib/utils.c
|
|
@ -1937,3 +1937,31 @@ int parse_mapping(int *argcp, char ***argvp, bool allow_all,
|
||||||
return parse_mapping_gen(argcp, argvp, parse_mapping_num,
|
return parse_mapping_gen(argcp, argvp, parse_mapping_num,
|
||||||
mapping_cb, mapping_cb_data);
|
mapping_cb, mapping_cb_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int str_map_lookup_str(const struct str_num_map *map, const char *needle)
|
||||||
|
{
|
||||||
|
if (!needle)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
/* Process array which is NULL terminated by the string. */
|
||||||
|
while (map && map->str) {
|
||||||
|
if (strcmp(map->str, needle) == 0)
|
||||||
|
return map->num;
|
||||||
|
|
||||||
|
map++;
|
||||||
|
}
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val)
|
||||||
|
{
|
||||||
|
int num = val;
|
||||||
|
|
||||||
|
while (map && map->str) {
|
||||||
|
if (num == map->num)
|
||||||
|
return map->str;
|
||||||
|
|
||||||
|
map++;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue