blob: 2f2142ed385666cde1d280fd0c792a35917853a1 [file] [log] [blame]
/*
* devlink.c Devlink tool
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Jiri Pirko <jiri@mellanox.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <limits.h>
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/sysinfo.h>
#define _LINUX_SYSINFO_H /* avoid collision with musl header */
#include <linux/genetlink.h>
#include <linux/devlink.h>
#include <linux/netlink.h>
#include <libmnl/libmnl.h>
#include <netinet/ether.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <rt_names.h>
#include "version.h"
#include "list.h"
#include "mnlg.h"
#include "mnl_utils.h"
#include "json_print.h"
#include "utils.h"
#include "namespace.h"
#define ESWITCH_MODE_LEGACY "legacy"
#define ESWITCH_MODE_SWITCHDEV "switchdev"
#define ESWITCH_INLINE_MODE_NONE "none"
#define ESWITCH_INLINE_MODE_LINK "link"
#define ESWITCH_INLINE_MODE_NETWORK "network"
#define ESWITCH_INLINE_MODE_TRANSPORT "transport"
#define ESWITCH_ENCAP_MODE_NONE "none"
#define ESWITCH_ENCAP_MODE_BASIC "basic"
#define PARAM_CMODE_RUNTIME_STR "runtime"
#define PARAM_CMODE_DRIVERINIT_STR "driverinit"
#define PARAM_CMODE_PERMANENT_STR "permanent"
#define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
#define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
#define HEALTH_REPORTER_STATE_ERROR_STR "error"
#define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
static int g_new_line_count;
static int g_indent_level;
static bool g_indent_newline;
#define INDENT_STR_STEP 2
#define INDENT_STR_MAXLEN 32
static char g_indent_str[INDENT_STR_MAXLEN + 1] = "";
static bool use_iec = false;
static void __attribute__((format(printf, 1, 2)))
pr_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static void __attribute__((format(printf, 1, 2)))
pr_out(const char *fmt, ...)
{
va_list ap;
if (g_indent_newline) {
printf("%s", g_indent_str);
g_indent_newline = false;
}
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
g_new_line_count = 0;
}
static void __attribute__((format(printf, 2, 3)))
pr_out_sp(unsigned int num, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vprintf(fmt, ap);
va_end(ap);
if (ret < num)
printf("%*s", num - ret, "");
g_new_line_count = 0; \
}
static void __attribute__((format(printf, 1, 2)))
pr_out_tty(const char *fmt, ...)
{
va_list ap;
if (!isatty(STDOUT_FILENO))
return;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
static void __pr_out_indent_inc(void)
{
if (g_indent_level + INDENT_STR_STEP > INDENT_STR_MAXLEN)
return;
g_indent_level += INDENT_STR_STEP;
memset(g_indent_str, ' ', sizeof(g_indent_str));
g_indent_str[g_indent_level] = '\0';
}
static void __pr_out_indent_dec(void)
{
if (g_indent_level - INDENT_STR_STEP < 0)
return;
g_indent_level -= INDENT_STR_STEP;
g_indent_str[g_indent_level] = '\0';
}
static void __pr_out_newline(void)
{
if (g_new_line_count < 1) {
pr_out("\n");
g_indent_newline = true;
}
g_new_line_count++;
}
static void dummy_signal_handler(int signum)
{
}
static int _mnlg_socket_recv_run_intr(struct mnlu_gen_socket *nlg,
mnl_cb_t data_cb, void *data)
{
struct sigaction act, oact;
int err;
act.sa_handler = dummy_signal_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_NODEFER;
sigaction(SIGINT, &act, &oact);
err = mnlu_gen_socket_recv_run(nlg, data_cb, data);
sigaction(SIGINT, &oact, NULL);
if (err < 0 && errno != EINTR) {
pr_err("devlink answers: %s\n", strerror(errno));
return -errno;
}
return 0;
}
static int _mnlg_socket_send(struct mnlu_gen_socket *nlg,
const struct nlmsghdr *nlh)
{
int err;
err = mnlg_socket_send(nlg, nlh);
if (err < 0) {
pr_err("Failed to call mnlg_socket_send\n");
return -errno;
}
return 0;
}
static int _mnlg_socket_group_add(struct mnlu_gen_socket *nlg,
const char *group_name)
{
int err;
err = mnlg_socket_group_add(nlg, group_name);
if (err < 0) {
pr_err("Failed to call mnlg_socket_group_add\n");
return -errno;
}
return 0;
}
struct ifname_map {
struct list_head list;
char *bus_name;
char *dev_name;
uint32_t port_index;
char *ifname;
};
static struct ifname_map *ifname_map_alloc(const char *bus_name,
const char *dev_name,
uint32_t port_index,
const char *ifname)
{
struct ifname_map *ifname_map;
ifname_map = calloc(1, sizeof(*ifname_map));
if (!ifname_map)
return NULL;
ifname_map->bus_name = strdup(bus_name);
ifname_map->dev_name = strdup(dev_name);
ifname_map->port_index = port_index;
ifname_map->ifname = strdup(ifname);
if (!ifname_map->bus_name || !ifname_map->dev_name ||
!ifname_map->ifname) {
free(ifname_map->ifname);
free(ifname_map->dev_name);
free(ifname_map->bus_name);
free(ifname_map);
return NULL;
}
return ifname_map;
}
static void ifname_map_free(struct ifname_map *ifname_map)
{
free(ifname_map->ifname);
free(ifname_map->dev_name);
free(ifname_map->bus_name);
free(ifname_map);
}
#define DL_OPT_HANDLE BIT(0)
#define DL_OPT_HANDLEP BIT(1)
#define DL_OPT_PORT_TYPE BIT(2)
#define DL_OPT_PORT_COUNT BIT(3)
#define DL_OPT_SB BIT(4)
#define DL_OPT_SB_POOL BIT(5)
#define DL_OPT_SB_SIZE BIT(6)
#define DL_OPT_SB_TYPE BIT(7)
#define DL_OPT_SB_THTYPE BIT(8)
#define DL_OPT_SB_TH BIT(9)
#define DL_OPT_SB_TC BIT(10)
#define DL_OPT_ESWITCH_MODE BIT(11)
#define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
#define DL_OPT_DPIPE_TABLE_NAME BIT(13)
#define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
#define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
#define DL_OPT_RESOURCE_PATH BIT(16)
#define DL_OPT_RESOURCE_SIZE BIT(17)
#define DL_OPT_PARAM_NAME BIT(18)
#define DL_OPT_PARAM_VALUE BIT(19)
#define DL_OPT_PARAM_CMODE BIT(20)
#define DL_OPT_HANDLE_REGION BIT(21)
#define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
#define DL_OPT_REGION_ADDRESS BIT(23)
#define DL_OPT_REGION_LENGTH BIT(24)
#define DL_OPT_FLASH_FILE_NAME BIT(25)
#define DL_OPT_FLASH_COMPONENT BIT(26)
#define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
#define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
#define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
#define DL_OPT_TRAP_NAME BIT(30)
#define DL_OPT_TRAP_ACTION BIT(31)
#define DL_OPT_TRAP_GROUP_NAME BIT(32)
#define DL_OPT_NETNS BIT(33)
#define DL_OPT_TRAP_POLICER_ID BIT(34)
#define DL_OPT_TRAP_POLICER_RATE BIT(35)
#define DL_OPT_TRAP_POLICER_BURST BIT(36)
#define DL_OPT_HEALTH_REPORTER_AUTO_DUMP BIT(37)
#define DL_OPT_PORT_FUNCTION_HW_ADDR BIT(38)
#define DL_OPT_FLASH_OVERWRITE BIT(39)
#define DL_OPT_RELOAD_ACTION BIT(40)
#define DL_OPT_RELOAD_LIMIT BIT(41)
#define DL_OPT_PORT_FLAVOUR BIT(42)
#define DL_OPT_PORT_PFNUMBER BIT(43)
#define DL_OPT_PORT_SFNUMBER BIT(44)
#define DL_OPT_PORT_FUNCTION_STATE BIT(45)
#define DL_OPT_PORT_CONTROLLER BIT(46)
#define DL_OPT_PORT_FN_RATE_TYPE BIT(47)
#define DL_OPT_PORT_FN_RATE_TX_SHARE BIT(48)
#define DL_OPT_PORT_FN_RATE_TX_MAX BIT(49)
#define DL_OPT_PORT_FN_RATE_NODE_NAME BIT(50)
#define DL_OPT_PORT_FN_RATE_PARENT BIT(51)
struct dl_opts {
uint64_t present; /* flags of present items */
char *bus_name;
char *dev_name;
uint32_t port_index;
enum devlink_port_type port_type;
uint32_t port_count;
uint32_t sb_index;
uint16_t sb_pool_index;
uint32_t sb_pool_size;
enum devlink_sb_pool_type sb_pool_type;
enum devlink_sb_threshold_type sb_pool_thtype;
uint32_t sb_threshold;
uint16_t sb_tc_index;
enum devlink_eswitch_mode eswitch_mode;
enum devlink_eswitch_inline_mode eswitch_inline_mode;
const char *dpipe_table_name;
bool dpipe_counters_enabled;
enum devlink_eswitch_encap_mode eswitch_encap_mode;
const char *resource_path;
uint64_t resource_size;
uint32_t resource_id;
bool resource_id_valid;
const char *param_name;
const char *param_value;
enum devlink_param_cmode cmode;
char *region_name;
uint32_t region_snapshot_id;
uint64_t region_address;
uint64_t region_length;
const char *flash_file_name;
const char *flash_component;
const char *reporter_name;
uint64_t reporter_graceful_period;
bool reporter_auto_recover;
bool reporter_auto_dump;
const char *trap_name;
const char *trap_group_name;
enum devlink_trap_action trap_action;
bool netns_is_pid;
uint32_t netns;
uint32_t trap_policer_id;
uint64_t trap_policer_rate;
uint64_t trap_policer_burst;
char port_function_hw_addr[MAX_ADDR_LEN];
uint32_t port_function_hw_addr_len;
uint32_t overwrite_mask;
enum devlink_reload_action reload_action;
enum devlink_reload_limit reload_limit;
uint32_t port_controller;
uint32_t port_sfnumber;
uint16_t port_flavour;
uint16_t port_pfnumber;
uint8_t port_fn_state;
uint16_t rate_type;
uint64_t rate_tx_share;
uint64_t rate_tx_max;
char *rate_node_name;
const char *rate_parent_node;
};
struct dl {
struct mnlu_gen_socket nlg;
struct list_head ifname_map_list;
int argc;
char **argv;
bool no_nice_names;
struct dl_opts opts;
bool json_output;
bool pretty_output;
bool verbose;
bool stats;
struct {
bool present;
char *bus_name;
char *dev_name;
uint32_t port_index;
} arr_last;
};
static int dl_argc(struct dl *dl)
{
return dl->argc;
}
static char *dl_argv(struct dl *dl)
{
if (dl_argc(dl) == 0)
return NULL;
return *dl->argv;
}
static void dl_arg_inc(struct dl *dl)
{
if (dl_argc(dl) == 0)
return;
dl->argc--;
dl->argv++;
}
static void dl_arg_dec(struct dl *dl)
{
dl->argc++;
dl->argv--;
}
static char *dl_argv_next(struct dl *dl)
{
char *ret;
if (dl_argc(dl) == 0)
return NULL;
ret = *dl->argv;
dl_arg_inc(dl);
return ret;
}
static char *dl_argv_index(struct dl *dl, unsigned int index)
{
if (index >= dl_argc(dl))
return NULL;
return dl->argv[index];
}
static int strcmpx(const char *str1, const char *str2)
{
if (strlen(str1) > strlen(str2))
return -1;
return strncmp(str1, str2, strlen(str1));
}
static bool dl_argv_match(struct dl *dl, const char *pattern)
{
if (dl_argc(dl) == 0)
return false;
return strcmpx(dl_argv(dl), pattern) == 0;
}
static bool dl_no_arg(struct dl *dl)
{
return dl_argc(dl) == 0;
}
static void __pr_out_indent_newline(struct dl *dl)
{
if (!g_indent_newline && !dl->json_output)
pr_out(" ");
}
static bool is_binary_eol(int i)
{
return !(i%16);
}
static void pr_out_binary_value(struct dl *dl, uint8_t *data, uint32_t len)
{
int i = 0;
while (i < len) {
if (dl->json_output)
print_int(PRINT_JSON, NULL, NULL, data[i]);
else
pr_out("%02x ", data[i]);
i++;
if (!dl->json_output && is_binary_eol(i))
__pr_out_newline();
}
if (!dl->json_output && !is_binary_eol(i))
__pr_out_newline();
}
static void pr_out_name(struct dl *dl, const char *name)
{
__pr_out_indent_newline(dl);
if (dl->json_output)
print_string(PRINT_JSON, name, NULL, NULL);
else
pr_out("%s:", name);
}
static void pr_out_u64(struct dl *dl, const char *name, uint64_t val)
{
__pr_out_indent_newline(dl);
if (val == (uint64_t) -1)
return print_string_name_value(name, "unlimited");
if (dl->json_output)
print_u64(PRINT_JSON, name, NULL, val);
else
pr_out("%s %"PRIu64, name, val);
}
static void pr_out_section_start(struct dl *dl, const char *name)
{
if (dl->json_output) {
open_json_object(NULL);
open_json_object(name);
}
}
static void pr_out_section_end(struct dl *dl)
{
if (dl->json_output) {
if (dl->arr_last.present)
close_json_array(PRINT_JSON, NULL);
close_json_object();
close_json_object();
}
}
static void pr_out_array_start(struct dl *dl, const char *name)
{
if (dl->json_output) {
open_json_array(PRINT_JSON, name);
} else {
__pr_out_indent_inc();
__pr_out_newline();
pr_out("%s:", name);
__pr_out_indent_inc();
__pr_out_newline();
}
}
static void pr_out_array_end(struct dl *dl)
{
if (dl->json_output) {
close_json_array(PRINT_JSON, NULL);
} else {
__pr_out_indent_dec();
__pr_out_indent_dec();
}
}
static void pr_out_object_start(struct dl *dl, const char *name)
{
if (dl->json_output) {
open_json_object(name);
} else {
__pr_out_indent_inc();
__pr_out_newline();
pr_out("%s:", name);
__pr_out_indent_inc();
__pr_out_newline();
}
}
static void pr_out_object_end(struct dl *dl)
{
if (dl->json_output) {
close_json_object();
} else {
__pr_out_indent_dec();
__pr_out_indent_dec();
}
}
static void pr_out_entry_start(struct dl *dl)
{
if (dl->json_output)
open_json_object(NULL);
}
static void pr_out_entry_end(struct dl *dl)
{
if (dl->json_output)
close_json_object();
else
__pr_out_newline();
}
static void check_indent_newline(struct dl *dl)
{
__pr_out_indent_newline(dl);
if (g_indent_newline && !is_json_context()) {
printf("%s", g_indent_str);
g_indent_newline = false;
}
g_new_line_count = 0;
}
static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = {
[DEVLINK_ATTR_BUS_NAME] = MNL_TYPE_NUL_STRING,
[DEVLINK_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING,
[DEVLINK_ATTR_PORT_INDEX] = MNL_TYPE_U32,
[DEVLINK_ATTR_PORT_TYPE] = MNL_TYPE_U16,
[DEVLINK_ATTR_PORT_DESIRED_TYPE] = MNL_TYPE_U16,
[DEVLINK_ATTR_PORT_NETDEV_IFINDEX] = MNL_TYPE_U32,
[DEVLINK_ATTR_PORT_NETDEV_NAME] = MNL_TYPE_NUL_STRING,
[DEVLINK_ATTR_PORT_IBDEV_NAME] = MNL_TYPE_NUL_STRING,
[DEVLINK_ATTR_PORT_LANES] = MNL_TYPE_U32,
[DEVLINK_ATTR_PORT_SPLITTABLE] = MNL_TYPE_U8,
[DEVLINK_ATTR_SB_INDEX] = MNL_TYPE_U32,
[DEVLINK_ATTR_SB_SIZE] = MNL_TYPE_U32,
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT] = MNL_TYPE_U16,
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT] = MNL_TYPE_U16,
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT] = MNL_TYPE_U16,
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT] = MNL_TYPE_U16,
[DEVLINK_ATTR_SB_POOL_INDEX] = MNL_TYPE_U16,
[DEVLINK_ATTR_SB_POOL_TYPE] = MNL_TYPE_U8,
[DEVLINK_ATTR_SB_POOL_SIZE] = MNL_TYPE_U32,
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = MNL_TYPE_U8,
[DEVLINK_ATTR_SB_THRESHOLD] = MNL_TYPE_U32,
[DEVLINK_ATTR_SB_TC_INDEX] = MNL_TYPE_U16,
[DEVLINK_ATTR_SB_OCC_CUR] = MNL_TYPE_U32,
[DEVLINK_ATTR_SB_OCC_MAX] = MNL_TYPE_U32,
[DEVLINK_ATTR_ESWITCH_MODE] = MNL_TYPE_U16,
[DEVLINK_ATTR_ESWITCH_INLINE_MODE] = MNL_TYPE_U8,
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = MNL_TYPE_U8,
[DEVLINK_ATTR_DPIPE_TABLES] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_TABLE] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_TABLE_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_DPIPE_TABLE_SIZE] = MNL_TYPE_U64,
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = MNL_TYPE_U8,
[DEVLINK_ATTR_DPIPE_ENTRIES] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_ENTRY] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX] = MNL_TYPE_U64,
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER] = MNL_TYPE_U64,
[DEVLINK_ATTR_DPIPE_MATCH] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_MATCH_VALUE] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_MATCH_TYPE] = MNL_TYPE_U32,
[DEVLINK_ATTR_DPIPE_ACTION] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_ACTION_VALUE] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_ACTION_TYPE] = MNL_TYPE_U32,
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING] = MNL_TYPE_U32,
[DEVLINK_ATTR_DPIPE_HEADERS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_HEADER] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_HEADER_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_DPIPE_HEADER_ID] = MNL_TYPE_U32,
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL] = MNL_TYPE_U8,
[DEVLINK_ATTR_DPIPE_HEADER_INDEX] = MNL_TYPE_U32,
[DEVLINK_ATTR_DPIPE_FIELD] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_DPIPE_FIELD_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_DPIPE_FIELD_ID] = MNL_TYPE_U32,
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] = MNL_TYPE_U32,
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE] = MNL_TYPE_U32,
[DEVLINK_ATTR_PARAM] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_PARAM_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_PARAM_TYPE] = MNL_TYPE_U8,
[DEVLINK_ATTR_PARAM_VALUES_LIST] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_PARAM_VALUE] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_PARAM_VALUE_CMODE] = MNL_TYPE_U8,
[DEVLINK_ATTR_REGION_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_REGION_SIZE] = MNL_TYPE_U64,
[DEVLINK_ATTR_REGION_SNAPSHOTS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_REGION_SNAPSHOT] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_REGION_SNAPSHOT_ID] = MNL_TYPE_U32,
[DEVLINK_ATTR_REGION_CHUNKS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_REGION_CHUNK] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_REGION_CHUNK_DATA] = MNL_TYPE_BINARY,
[DEVLINK_ATTR_REGION_CHUNK_ADDR] = MNL_TYPE_U64,
[DEVLINK_ATTR_REGION_CHUNK_LEN] = MNL_TYPE_U64,
[DEVLINK_ATTR_INFO_DRIVER_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_INFO_SERIAL_NUMBER] = MNL_TYPE_STRING,
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER] = MNL_TYPE_STRING,
[DEVLINK_ATTR_INFO_VERSION_FIXED] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_INFO_VERSION_RUNNING] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_INFO_VERSION_STORED] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_INFO_VERSION_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_INFO_VERSION_VALUE] = MNL_TYPE_STRING,
[DEVLINK_ATTR_HEALTH_REPORTER] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_HEALTH_REPORTER_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_HEALTH_REPORTER_STATE] = MNL_TYPE_U8,
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT] = MNL_TYPE_U64,
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT] = MNL_TYPE_U64,
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS] = MNL_TYPE_U64,
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = MNL_TYPE_U64,
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = MNL_TYPE_STRING,
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG] = MNL_TYPE_STRING,
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE] = MNL_TYPE_U64,
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL] = MNL_TYPE_U64,
[DEVLINK_ATTR_STATS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_TRAP_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_TRAP_ACTION] = MNL_TYPE_U8,
[DEVLINK_ATTR_TRAP_TYPE] = MNL_TYPE_U8,
[DEVLINK_ATTR_TRAP_GENERIC] = MNL_TYPE_FLAG,
[DEVLINK_ATTR_TRAP_METADATA] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_TRAP_GROUP_NAME] = MNL_TYPE_STRING,
[DEVLINK_ATTR_RELOAD_FAILED] = MNL_TYPE_U8,
[DEVLINK_ATTR_DEV_STATS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_RELOAD_STATS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_RELOAD_STATS_ENTRY] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_RELOAD_ACTION] = MNL_TYPE_U8,
[DEVLINK_ATTR_RELOAD_STATS_LIMIT] = MNL_TYPE_U8,
[DEVLINK_ATTR_RELOAD_STATS_VALUE] = MNL_TYPE_U32,
[DEVLINK_ATTR_REMOTE_RELOAD_STATS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_RELOAD_ACTION_INFO] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_RELOAD_ACTION_STATS] = MNL_TYPE_NESTED,
[DEVLINK_ATTR_TRAP_POLICER_ID] = MNL_TYPE_U32,
[DEVLINK_ATTR_TRAP_POLICER_RATE] = MNL_TYPE_U64,
[DEVLINK_ATTR_TRAP_POLICER_BURST] = MNL_TYPE_U64,
};
static const enum mnl_attr_data_type
devlink_stats_policy[DEVLINK_ATTR_STATS_MAX + 1] = {
[DEVLINK_ATTR_STATS_RX_PACKETS] = MNL_TYPE_U64,
[DEVLINK_ATTR_STATS_RX_BYTES] = MNL_TYPE_U64,
[DEVLINK_ATTR_STATS_RX_DROPPED] = MNL_TYPE_U64,
};
static int attr_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;
int type;
if (mnl_attr_type_valid(attr, DEVLINK_ATTR_MAX) < 0)
return MNL_CB_OK;
type = mnl_attr_get_type(attr);
if (mnl_attr_validate(attr, devlink_policy[type]) < 0)
return MNL_CB_ERROR;
tb[type] = attr;
return MNL_CB_OK;
}
static int attr_stats_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;
int type;
/* Allow the tool to work on top of newer kernels that might contain
* more attributes.
*/
if (mnl_attr_type_valid(attr, DEVLINK_ATTR_STATS_MAX) < 0)
return MNL_CB_OK;
type = mnl_attr_get_type(attr);
if (mnl_attr_validate(attr, devlink_stats_policy[type]) < 0)
return MNL_CB_ERROR;
tb[type] = attr;
return MNL_CB_OK;
}
static const enum mnl_attr_data_type
devlink_function_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR ] = MNL_TYPE_BINARY,
[DEVLINK_PORT_FN_ATTR_STATE] = MNL_TYPE_U8,
};
static int function_attr_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;
int type;
/* Allow the tool to work on top of newer kernels that might contain
* more attributes.
*/
if (mnl_attr_type_valid(attr, DEVLINK_PORT_FUNCTION_ATTR_MAX) < 0)
return MNL_CB_OK;
type = mnl_attr_get_type(attr);
if (mnl_attr_validate(attr, devlink_function_policy[type]) < 0)
return MNL_CB_ERROR;
tb[type] = attr;
return MNL_CB_OK;
}
static int ifname_map_cb(const struct nlmsghdr *nlh, void *data)
{
struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
struct dl *dl = data;
struct ifname_map *ifname_map;
const char *bus_name;
const char *dev_name;
uint32_t port_ifindex;
const char *port_ifname;
mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
!tb[DEVLINK_ATTR_PORT_INDEX])
return MNL_CB_ERROR;
if (!tb[DEVLINK_ATTR_PORT_NETDEV_NAME])
return MNL_CB_OK;
bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
port_ifindex = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
port_ifname = mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME]);
ifname_map = ifname_map_alloc(bus_name, dev_name,
port_ifindex, port_ifname);
if (!ifname_map)
return MNL_CB_ERROR;
list_add(&ifname_map->list, &dl->ifname_map_list);
return MNL_CB_OK;
}
static void ifname_map_fini(struct dl *dl)
{
struct ifname_map *ifname_map, *tmp;
list_for_each_entry_safe(ifname_map, tmp,
&dl->ifname_map_list, list) {
list_del(&ifname_map->list);
ifname_map_free(ifname_map);
}
}
static int ifname_map_init(struct dl *dl)
{
struct nlmsghdr *nlh;
int err;
INIT_LIST_HEAD(&dl->ifname_map_list);
nlh = mnlu_gen_socket_cmd_prepare(&dl->nlg, DEVLINK_CMD_PORT_GET,
NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
err = mnlu_gen_socket_sndrcv(&dl->nlg, nlh, ifname_map_cb, dl);
if (err) {
ifname_map_fini(dl);
return err;
}
return 0;
}
static int ifname_map_lookup(struct dl *dl, const char *ifname,
char **p_bus_name, char **p_dev_name,
uint32_t *p_port_index)
{
struct ifname_map *ifname_map;
list_for_each_entry(ifname_map, &dl->ifname_map_list, list) {
if (strcmp(ifname, ifname_map->ifname) == 0) {
*p_bus_name = ifname_map->bus_name;
*p_dev_name = ifname_map->dev_name;
*p_port_index = ifname_map->port_index;
return 0;
}
}
return -ENOENT;
}
static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name,
const char *dev_name, uint32_t port_index,
char **p_ifname)
{
struct ifname_map *ifname_map;
list_for_each_entry(ifname_map, &dl->ifname_map_list, list) {
if (strcmp(bus_name, ifname_map->bus_name) == 0 &&
strcmp(dev_name, ifname_map->dev_name) == 0 &&
port_index == ifname_map->port_index) {
*p_ifname = ifname_map->ifname;
return 0;
}
}
return -ENOENT;
}
static int strtouint64_t(const char *str, uint64_t *p_val)
{
char *endptr;
unsigned long long int val;
val = strtoull(str, &endptr, 10);
if (endptr == str || *endptr != '\0')
return -EINVAL;
if (val > ULONG_MAX)
return -ERANGE;
*p_val = val;
return 0;
}
static int strtouint32_t(const char *str, uint32_t *p_val)
{
char *endptr;
unsigned long int val;
val = strtoul(str, &endptr, 10);
if (endptr == str || *endptr != '\0')
return -EINVAL;
if (val > UINT_MAX)
return -ERANGE;
*p_val = val;
return 0;
}
static int strtouint16_t(const char *str, uint16_t *p_val)
{
char *endptr;
unsigned long int val;
val = strtoul(str, &endptr, 10);
if (endptr == str || *endptr != '\0')
return -EINVAL;
if (val > USHRT_MAX)
return -ERANGE;
*p_val = val;
return 0;
}
static int strtouint8_t(const char *str, uint8_t *p_val)
{
char *endptr;
unsigned long int val;
val = strtoul(str, &endptr, 10);
if (endptr == str || *endptr != '\0')
return -EINVAL;
if (val > UCHAR_MAX)
return -ERANGE;
*p_val = val;
return 0;
}
static int strtobool(const char *str, bool *p_val)
{
bool val;
if (!strcmp(str, "true") || !strcmp(str, "1") ||
!strcmp(str, "enable"))
val = true;
else if (!strcmp(str, "false") || !strcmp(str, "0") ||
!strcmp(str, "disable"))
val = false;
else
return -EINVAL;
*p_val = val;
return 0;
}
static int ident_str_validate(char *str, unsigned int expected)
{
if (!str)
return -EINVAL;
if (get_str_char_count(str, '/') != expected) {
pr_err("Wrong identification string format.\n");
return -EINVAL;
}
return 0;
}
static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name)
{
int err;
err = str_split_by_char(str, p_bus_name, p_dev_name, '/');
if (err) {
pr_err("Devlink identification (\"bus_name/dev_name\") \"%s\" is invalid\n", str);
return err;
}
return 0;
}
static int dl_argv_handle(struct dl *dl, char **p_bus_name, char **p_dev_name)
{
char *str = dl_argv_next(dl);
int err;
err = ident_str_validate(str, 1);
if (err) {
pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
return err;
}
return __dl_argv_handle(str, p_bus_name, p_dev_name);
}
static int __dl_argv_handle_port(char *str,
char **p_bus_name, char **p_dev_name,
uint32_t *p_port_index)
{
char *handlestr;
char *portstr;
int err;
err = str_split_by_char(str, &handlestr, &portstr, '/');
if (err) {
pr_err("Port identification \"%s\" is invalid\n", str);
return err;
}
err = strtouint32_t(portstr, p_port_index);
if (err) {
pr_err("Port index \"%s\" is not a number or not within range\n",
portstr);
return err;
}
err = str_split_by_char(handlestr, p_bus_name, p_dev_name, '/');
if (err) {
pr_err("Port identification \"%s\" is invalid\n", str);
return err;
}
return 0;
}
static int __dl_argv_handle_port_ifname(struct dl *dl, char *str,
char **p_bus_name, char **p_dev_name,
uint32_t *p_port_index)
{
int err;
err = ifname_map_lookup(dl, str, p_bus_name, p_dev_name,
p_port_index);
if (err) {
pr_err("Netdevice \"%s\" not found\n", str);
return err;
}
return 0;
}
static int dl_argv_handle_port(struct dl *dl, char **p_bus_name,
char **p_dev_name, uint32_t *p_port_index)
{
char *str = dl_argv_next(dl);
unsigned int slash_count;
if (!str) {
pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
return -EINVAL;
}
slash_count = get_str_char_count(str, '/');
switch (slash_count) {
case 0:
return __dl_argv_handle_port_ifname(dl, str, p_bus_name,
p_dev_name, p_port_index);
case 2:
return __dl_argv_handle_port(str, p_bus_name,
p_dev_name, p_port_index);
default:
pr_err("Wrong port identification string format.\n");
pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
return -EINVAL;
}
}
static int dl_argv_handle_both(struct dl *dl, char **p_bus_name,
char **p_dev_name, uint32_t *p_port_index,
uint64_t *p_handle_bit)
{
char *str = dl_argv_next(dl);
unsigned int slash_count;
int err;
if (!str) {
pr_err("One of following identifications expected:\n"
"Devlink identification (\"bus_name/dev_name\")\n"
"Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
return -EINVAL;
}
slash_count = get_str_char_count(str, '/');
if (slash_count == 1) {
err = __dl_argv_handle(str, p_bus_name, p_dev_name);
if (err)
return err;
*p_handle_bit = DL_OPT_HANDLE;
} else if (slash_count == 2) {
err = __dl_argv_handle_port(str, p_bus_name,
p_dev_name, p_port_index);
if (err)
return err;
*p_handle_bit = DL_OPT_HANDLEP;
} else if (slash_count == 0) {
err = __dl_argv_handle_port_ifname(dl, str, p_bus_name,
p_dev_name, p_port_index);
if (err)
return err;
*p_handle_bit = DL_OPT_HANDLEP;
} else {
pr_err("Wrong port identification string format.\n");
pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
return -EINVAL;
}
return 0;
}
static int __dl_argv_handle_name(char *str, char **p_bus_name,
char **p_dev_name, char **p_name)
{
char *handlestr;
int err;
err = str_split_by_char(str, &handlestr, p_name, '/');
if (err)
return err;
return str_split_by_char(handlestr, p_bus_name, p_dev_name, '/');
}
static int dl_argv_handle_region(struct dl *dl, char **p_bus_name,
char **p_dev_name, char **p_region)
{
char *str = dl_argv_next(dl);
int err;
err = ident_str_validate(str, 2);
if (err) {
pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
return err;
}
err = __dl_argv_handle_name(str, p_bus_name, p_dev_name, p_region);
if (err)
pr_err("Region identification \"%s\" is invalid\n", str);
return err;
}
static int dl_argv_handle_rate_node(struct dl *dl, char **p_bus_name,
char **p_dev_name, char **p_node)
{
char *str = dl_argv_next(dl);
int err;
err = ident_str_validate(str, 2);
if (err) {
pr_err("Expected \"bus_name/dev_name/node\" identification.\n");
return err;
}
err = __dl_argv_handle_name(str, p_bus_name, p_dev_name, p_node);
if (err) {
pr_err("Node identification \"%s\" is invalid\n", str);
return err;
}
if (!**p_node || strspn(*p_node, "0123456789") == strlen(*p_node)) {
err = -EINVAL;
pr_err("Node name cannot be a devlink port index or empty.\n");
}
return err;
}
static int dl_argv_handle_rate(struct dl *dl, char **p_bus_name,
char **p_dev_name, uint32_t *p_port_index,
char **p_node_name, uint64_t *p_handle_bit)
{
char *str = dl_argv_next(dl);
char *identifier;
int err;
err = ident_str_validate(str, 2);
if (err) {
pr_err("Expected \"bus_name/dev_name/node\" or "
"\"bus_name/dev_name/port_index\" identification.\n");
return err;
}
err = __dl_argv_handle_name(str, p_bus_name, p_dev_name, &identifier);
if (err) {
pr_err("Identification \"%s\" is invalid\n", str);
return err;
}
if (!*identifier) {
pr_err("Identifier cannot be empty");
return -EINVAL;
}
if (strspn(identifier, "0123456789") == strlen(identifier)) {
err = strtouint32_t(identifier, p_port_index);
if (err) {
pr_err("Port index \"%s\" is not a number"
" or not within range\n", identifier);
return err;
}
*p_handle_bit = DL_OPT_HANDLEP;
} else {
*p_handle_bit = DL_OPT_PORT_FN_RATE_NODE_NAME;
*p_node_name = identifier;
}
return 0;
}
static int dl_argv_uint64_t(struct dl *dl, uint64_t *p_val)
{
char *str = dl_argv_next(dl);
int err;
if (!str) {
pr_err("Unsigned number argument expected\n");
return -EINVAL;
}
err = strtouint64_t(str, p_val);
if (err) {
pr_err("\"%s\" is not a number or not within range\n", str);
return err;
}
return 0;
}
static int dl_argv_uint32_t(struct dl *dl, uint32_t *p_val)
{
char *str = dl_argv_next(dl);
int err;
if (!str) {
pr_err("Unsigned number argument expected\n");
return -EINVAL;
}
err = strtouint32_t(str, p_val);
if (err) {
pr_err("\"%s\" is not a number or not within range\n", str);
return err;
}
return 0;
}
static int dl_argv_uint16_t(struct dl *dl, uint16_t *p_val)
{
char *str = dl_argv_next(dl);
int err;
if (!str) {
pr_err("Unsigned number argument expected\n");
return -EINVAL;
}
err = strtouint16_t(str, p_val);
if (err) {
pr_err("\"%s\" is not a number or not within range\n", str);
return err;
}
return 0;
}
static int dl_argv_bool(struct dl *dl, bool *p_val)
{
char *str = dl_argv_next(dl);
int err;
if (!str) {
pr_err("Boolean argument expected\n");
return -EINVAL;
}
err = strtobool(str, p_val);
if (err) {
pr_err("\"%s\" is not a valid boolean value\n", str);
return err;
}
return 0;
}
static int dl_argv_str(struct dl *dl, const char **p_str)
{
const char *str = dl_argv_next(dl);
if (!str) {
pr_err("String parameter expected\n");
return -EINVAL;
}
*p_str = str;
return 0;
}
static int port_type_get(const char *typestr, enum devlink_port_type *p_type)
{
if (strcmp(typestr, "auto") == 0) {
*p_type = DEVLINK_PORT_TYPE_AUTO;
} else if (strcmp(typestr, "eth") == 0) {
*p_type = DEVLINK_PORT_TYPE_ETH;
} else if (strcmp(typestr, "ib") == 0) {
*p_type = DEVLINK_PORT_TYPE_IB;
} else {
pr_err("Unknown port type \"%s\"\n", typestr);
return -EINVAL;
}
return 0;
}
static int pool_type_get(const char *typestr, enum devlink_sb_pool_type *p_type)
{
if (strcmp(typestr, "ingress") == 0) {
*p_type = DEVLINK_SB_POOL_TYPE_INGRESS;
} else if (strcmp(typestr, "egress") == 0) {
*p_type = DEVLINK_SB_POOL_TYPE_EGRESS;
} else {
pr_err("Unknown pool type \"%s\"\n", typestr);
return -EINVAL;
}
return 0;
}
static int threshold_type_get(const char *typestr,
enum devlink_sb_threshold_type *p_type)
{
if (strcmp(typestr, "static") == 0) {
*p_type = DEVLINK_SB_THRESHOLD_TYPE_STATIC;
} else if (strcmp(typestr, "dynamic") == 0) {
*p_type = DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC;
} else {
pr_err("Unknown threshold type \"%s\"\n", typestr);
return -EINVAL;
}
return 0;
}
static int eswitch_mode_get(const char *typestr,
enum devlink_eswitch_mode *p_mode)
{
if (strcmp(typestr, ESWITCH_MODE_LEGACY) == 0) {
*p_mode = DEVLINK_ESWITCH_MODE_LEGACY;
} else if (strcmp(typestr, ESWITCH_MODE_SWITCHDEV) == 0) {
*p_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
} else {
pr_err("Unknown eswitch mode \"%s\"\n", typestr);
return -EINVAL;
}
return 0;
}
static int eswitch_inline_mode_get(const char *typestr,
enum devlink_eswitch_inline_mode *p_mode)
{
if (strcmp(typestr, ESWITCH_INLINE_MODE_NONE) == 0) {
*p_mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
} else if (strcmp(typestr, ESWITCH_INLINE_MODE_LINK) == 0) {
*p_mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
} else if (strcmp(typestr, ESWITCH_INLINE_MODE_NETWORK) == 0) {
*p_mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
} else if (strcmp(typestr, ESWITCH_INLINE_MODE_TRANSPORT) == 0) {
*p_mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
} else {
pr_err("Unknown eswitch inline mode \"%s\"\n", typestr);
return -EINVAL;
}
return 0;
}
static int
eswitch_encap_mode_get(const char *typestr,
enum devlink_eswitch_encap_mode *p_encap_mode)
{
/* The initial implementation incorrectly accepted "enable"/"disable".
* Carry it to maintain backward compatibility.
*/
if (strcmp(typestr, "disable") == 0 ||
strcmp(typestr, ESWITCH_ENCAP_MODE_NONE) == 0) {
*p_encap_mode = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
} else if (strcmp(typestr, "enable") == 0 ||
strcmp(typestr, ESWITCH_ENCAP_MODE_BASIC) == 0) {
*p_encap_mode = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
} else {
pr_err("Unknown eswitch encap mode \"%s\"\n", typestr);
return -EINVAL;
}
return 0;
}
static int flash_overwrite_section_get(const char *sectionstr, uint32_t *mask)
{
if (strcmp(sectionstr, "settings") == 0) {
*mask |= DEVLINK_FLASH_OVERWRITE_SETTINGS;
} else if (strcmp(sectionstr, "identifiers") == 0) {
*mask |= DEVLINK_FLASH_OVERWRITE_IDENTIFIERS;
} else {
pr_err("Unknown overwrite section \"%s\"\n", sectionstr);
return -EINVAL;
}
return 0;
}
static int param_cmode_get(const char *cmodestr,
enum devlink_param_cmode *cmode)
{
if (strcmp(cmodestr, PARAM_CMODE_RUNTIME_STR) == 0) {
*cmode = DEVLINK_PARAM_CMODE_RUNTIME;
} else if (strcmp(cmodestr, PARAM_CMODE_DRIVERINIT_STR) == 0) {
*cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
} else if (strcmp(cmodestr, PARAM_CMODE_PERMANENT_STR) == 0) {
*cmode = DEVLINK_PARAM_CMODE_PERMANENT;
} else {
pr_err("Unknown configuration mode \"%s\"\n", cmodestr);
return -EINVAL;
}
return 0;
}
static int trap_action_get(const char *actionstr,
enum devlink_trap_action *p_action)
{
if (strcmp(actionstr, "drop") == 0) {
*p_action = DEVLINK_TRAP_ACTION_DROP;
} else if (strcmp(actionstr, "trap") == 0) {
*p_action = DEVLINK_TRAP_ACTION_TRAP;
} else if (strcmp(actionstr, "mirror") == 0) {
*p_action = DEVLINK_TRAP_ACTION_MIRROR;
} else {
pr_err("Unknown trap action \"%s\"\n", actionstr);
return -EINVAL;
}
return 0;
}
static int hw_addr_parse(const char *addrstr, char *hw_addr, uint32_t *len)
{
int alen;
alen = ll_addr_a2n(hw_addr, MAX_ADDR_LEN, addrstr);
if (alen < 0)
return -EINVAL;
*len = alen;
return 0;
}
static int reload_action_get(struct dl *dl, const char *actionstr,
enum devlink_reload_action *action)
{
if (strcmp(actionstr, "driver_reinit") == 0) {
*action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
} else if (strcmp(actionstr, "fw_activate") == 0) {
*action = DEVLINK_RELOAD_ACTION_FW_ACTIVATE;
} else {
pr_err("Unknown reload action \"%s\"\n", actionstr);
return -EINVAL;
}
return 0;
}
static int reload_limit_get(struct dl *dl, const char *limitstr,
enum devlink_reload_limit *limit)
{
if (strcmp(limitstr, "no_reset") == 0) {
*limit = DEVLINK_RELOAD_LIMIT_NO_RESET;
} else {
pr_err("Unknown reload limit \"%s\"\n", limitstr);
return -EINVAL;
}
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 = "pcisf", .num = DEVLINK_PORT_FLAVOUR_PCI_SF },
{ .str = "virtual", .num = DEVLINK_PORT_FLAVOUR_VIRTUAL},
{ .str = NULL, },
};
static struct str_num_map port_fn_state_map[] = {
{ .str = "inactive", .num = DEVLINK_PORT_FN_STATE_INACTIVE},
{ .str = "active", .num = DEVLINK_PORT_FN_STATE_ACTIVE },
{ .str = NULL, }
};
static struct str_num_map port_fn_opstate_map[] = {
{ .str = "attached", .num = DEVLINK_PORT_FN_OPSTATE_ATTACHED},
{ .str = "detached", .num = DEVLINK_PORT_FN_OPSTATE_DETACHED},
{ .str = NULL, }
};
static int port_flavour_parse(const char *flavour, uint16_t *value)
{
int num;
num = str_map_lookup_str(port_flavour_map, flavour);
if (num < 0) {
invarg("unknown flavour", flavour);
return num;
}
*value = num;
return 0;
}
static int port_fn_state_parse(const char *statestr, uint8_t *state)
{
int num;
num = str_map_lookup_str(port_fn_state_map, statestr);
if (num < 0) {
invarg("unknown state", statestr);
return num;
}
*state = num;
return 0;
}
static int port_fn_rate_type_get(const char *typestr, uint16_t *type)
{
if (!strcmp(typestr, "leaf"))
*type = DEVLINK_RATE_TYPE_LEAF;
else if (!strcmp(typestr, "node"))
*type = DEVLINK_RATE_TYPE_NODE;
else
return -EINVAL;
return 0;
}
static int port_fn_rate_value_get(struct dl *dl, uint64_t *rate)
{
const char *ratestr;
__u64 rate64;
int err;
err = dl_argv_str(dl, &ratestr);
if (err)
return err;
err = get_rate64(&rate64, ratestr);
if (err) {
pr_err("Invalid rate value: \"%s\"\n", ratestr);
return -EINVAL;
}
*rate = rate64;
return 0;
}
struct dl_args_metadata {
uint64_t o_flag;
char err_msg[DL_ARGS_REQUIRED_MAX_ERR_LEN];
};
static const struct dl_args_metadata dl_args_required[] = {
{DL_OPT_PORT_TYPE, "Port type not set."},
{DL_OPT_PORT_COUNT, "Port split count option expected."},
{DL_OPT_SB_POOL, "Pool index option expected."},
{DL_OPT_SB_SIZE, "Pool size option expected."},
{DL_OPT_SB_TYPE, "Pool type option expected."},
{DL_OPT_SB_THTYPE, "Pool threshold type option expected."},
{DL_OPT_SB_TH, "Threshold option expected."},
{DL_OPT_SB_TC, "TC index option expected."},
{DL_OPT_ESWITCH_MODE, "E-Switch mode option expected."},
{DL_OPT_ESWITCH_INLINE_MODE, "E-Switch inline-mode option expected."},
{DL_OPT_DPIPE_TABLE_NAME, "Dpipe table name expected."},
{DL_OPT_DPIPE_TABLE_COUNTERS, "Dpipe table counter state expected."},
{DL_OPT_ESWITCH_ENCAP_MODE, "E-Switch encapsulation option expected."},
{DL_OPT_RESOURCE_PATH, "Resource path expected."},
{DL_OPT_RESOURCE_SIZE, "Resource size expected."},
{DL_OPT_PARAM_NAME, "Parameter name expected."},
{DL_OPT_PARAM_VALUE, "Value to set expected."},
{DL_OPT_PARAM_CMODE, "Configuration mode expected."},
{DL_OPT_REGION_SNAPSHOT_ID, "Region snapshot id expected."},
{DL_OPT_REGION_ADDRESS, "Region address value expected."},
{DL_OPT_REGION_LENGTH, "Region length value expected."},
{DL_OPT_HEALTH_REPORTER_NAME, "Reporter's name is expected."},
{DL_OPT_TRAP_NAME, "Trap's name is expected."},
{DL_OPT_TRAP_GROUP_NAME, "Trap group's name is expected."},
{DL_OPT_PORT_FUNCTION_HW_ADDR, "Port function's hardware address is expected."},
{DL_OPT_PORT_FLAVOUR, "Port flavour is expected."},
{DL_OPT_PORT_PFNUMBER, "Port PCI PF number is expected."},
};
static int dl_args_finding_required_validate(uint64_t o_required,
uint64_t o_found)
{
uint64_t o_flag;
int i;
for (i = 0; i < ARRAY_SIZE(dl_args_required); i++) {
o_flag = dl_args_required[i].o_flag;
if ((o_required & o_flag) && !(o_found & o_flag)) {
pr_err("%s\n", dl_args_required[i].err_msg);
return -EINVAL;
}
}
if (o_required & ~o_found) {
pr_err("BUG: unknown argument required but not found\n");
return -EINVAL;
}
return 0;
}
static int dl_argv_parse(struct dl *dl, uint64_t o_required,
uint64_t o_optional)
{
struct dl_opts *opts = &dl->opts;
uint64_t o_all = o_required | o_optional;
uint64_t o_found = 0;
int err;
if (o_required & DL_OPT_HANDLE && o_required & DL_OPT_HANDLEP) {
uint64_t handle_bit;
err = dl_argv_handle_both(dl, &opts->bus_name, &opts->dev_name,
&opts->port_index, &handle_bit);
if (err)
return err;
o_required &= ~(DL_OPT_HANDLE | DL_OPT_HANDLEP) | handle_bit;
o_found |= handle_bit;
} else if (o_required & DL_OPT_HANDLEP &&
o_required & DL_OPT_PORT_FN_RATE_NODE_NAME) {
uint64_t handle_bit;
err = dl_argv_handle_rate(dl, &opts->bus_name, &opts->dev_name,
&opts->port_index,
&opts->rate_node_name,
&handle_bit);
if (err)
return err;
o_required &= ~(DL_OPT_HANDLEP | DL_OPT_PORT_FN_RATE_NODE_NAME) |
handle_bit;
o_found |= handle_bit;
} else if (o_required & DL_OPT_HANDLE) {
err = dl_argv_handle(dl, &opts->bus_name, &opts->dev_name);
if (err)
return err;
o_found |= DL_OPT_HANDLE;
} else if (o_required & DL_OPT_HANDLEP) {
err = dl_argv_handle_port(dl, &opts->bus_name, &opts->dev_name,
&opts->port_index);
if (err)
return err;
o_found |= DL_OPT_HANDLEP;
} else if (o_required & DL_OPT_HANDLE_REGION) {
err = dl_argv_handle_region(dl, &opts->bus_name,
&opts->dev_name,
&opts->region_name);
if (err)
return err;
o_found |= DL_OPT_HANDLE_REGION;
} else if (o_required & DL_OPT_PORT_FN_RATE_NODE_NAME) {
err = dl_argv_handle_rate_node(dl, &opts->bus_name,
&opts->dev_name,
&opts->rate_node_name);
if (err)
return err;
o_found |= DL_OPT_PORT_FN_RATE_NODE_NAME;
}
while (dl_argc(dl)) {
if (dl_argv_match(dl, "type") &&
(o_all & DL_OPT_PORT_TYPE)) {
const char *typestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &typestr);
if (err)
return err;
err = port_type_get(typestr, &opts->port_type);
if (err)
return err;
o_found |= DL_OPT_PORT_TYPE;
} else if (dl_argv_match(dl, "count") &&
(o_all & DL_OPT_PORT_COUNT)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->port_count);
if (err)
return err;
o_found |= DL_OPT_PORT_COUNT;
} else if (dl_argv_match(dl, "sb") &&
(o_all & DL_OPT_SB)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->sb_index);
if (err)
return err;
o_found |= DL_OPT_SB;
} else if (dl_argv_match(dl, "pool") &&
(o_all & DL_OPT_SB_POOL)) {
dl_arg_inc(dl);
err = dl_argv_uint16_t(dl, &opts->sb_pool_index);
if (err)
return err;
o_found |= DL_OPT_SB_POOL;
} else if (dl_argv_match(dl, "size") &&
(o_all & DL_OPT_SB_SIZE)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->sb_pool_size);
if (err)
return err;
o_found |= DL_OPT_SB_SIZE;
} else if (dl_argv_match(dl, "type") &&
(o_all & DL_OPT_SB_TYPE)) {
const char *typestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &typestr);
if (err)
return err;
err = pool_type_get(typestr, &opts->sb_pool_type);
if (err)
return err;
o_found |= DL_OPT_SB_TYPE;
} else if (dl_argv_match(dl, "thtype") &&
(o_all & DL_OPT_SB_THTYPE)) {
const char *typestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &typestr);
if (err)
return err;
err = threshold_type_get(typestr,
&opts->sb_pool_thtype);
if (err)
return err;
o_found |= DL_OPT_SB_THTYPE;
} else if (dl_argv_match(dl, "th") &&
(o_all & DL_OPT_SB_TH)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->sb_threshold);
if (err)
return err;
o_found |= DL_OPT_SB_TH;
} else if (dl_argv_match(dl, "tc") &&
(o_all & DL_OPT_SB_TC)) {
dl_arg_inc(dl);
err = dl_argv_uint16_t(dl, &opts->sb_tc_index);
if (err)
return err;
o_found |= DL_OPT_SB_TC;
} else if (dl_argv_match(dl, "mode") &&
(o_all & DL_OPT_ESWITCH_MODE)) {
const char *typestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &typestr);
if (err)
return err;
err = eswitch_mode_get(typestr, &opts->eswitch_mode);
if (err)
return err;
o_found |= DL_OPT_ESWITCH_MODE;
} else if (dl_argv_match(dl, "inline-mode") &&
(o_all & DL_OPT_ESWITCH_INLINE_MODE)) {
const char *typestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &typestr);
if (err)
return err;
err = eswitch_inline_mode_get(
typestr, &opts->eswitch_inline_mode);
if (err)
return err;
o_found |= DL_OPT_ESWITCH_INLINE_MODE;
} else if (dl_argv_match(dl, "name") &&
(o_all & DL_OPT_DPIPE_TABLE_NAME)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->dpipe_table_name);
if (err)
return err;
o_found |= DL_OPT_DPIPE_TABLE_NAME;
} else if ((dl_argv_match(dl, "counters") ||
dl_argv_match(dl, "counters_enabled")) &&
(o_all & DL_OPT_DPIPE_TABLE_COUNTERS)) {
dl_arg_inc(dl);
err = dl_argv_bool(dl, &opts->dpipe_counters_enabled);
if (err)
return err;
o_found |= DL_OPT_DPIPE_TABLE_COUNTERS;
} else if ((dl_argv_match(dl, "encap") || /* Original incorrect implementation */
dl_argv_match(dl, "encap-mode")) &&
(o_all & DL_OPT_ESWITCH_ENCAP_MODE)) {
const char *typestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &typestr);
if (err)
return err;
err = eswitch_encap_mode_get(typestr,
&opts->eswitch_encap_mode);
if (err)
return err;
o_found |= DL_OPT_ESWITCH_ENCAP_MODE;
} else if (dl_argv_match(dl, "path") &&
(o_all & DL_OPT_RESOURCE_PATH)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->resource_path);
if (err)
return err;
o_found |= DL_OPT_RESOURCE_PATH;
} else if (dl_argv_match(dl, "size") &&
(o_all & DL_OPT_RESOURCE_SIZE)) {
dl_arg_inc(dl);
err = dl_argv_uint64_t(dl, &opts->resource_size);
if (err)
return err;
o_found |= DL_OPT_RESOURCE_SIZE;
} else if (dl_argv_match(dl, "name") &&
(o_all & DL_OPT_PARAM_NAME)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->param_name);
if (err)
return err;
o_found |= DL_OPT_PARAM_NAME;
} else if (dl_argv_match(dl, "value") &&
(o_all & DL_OPT_PARAM_VALUE)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->param_value);
if (err)
return err;
o_found |= DL_OPT_PARAM_VALUE;
} else if (dl_argv_match(dl, "cmode") &&
(o_all & DL_OPT_PARAM_CMODE)) {
const char *cmodestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &cmodestr);
if (err)
return err;
err = param_cmode_get(cmodestr, &opts->cmode);
if (err)
return err;
o_found |= DL_OPT_PARAM_CMODE;
} else if (dl_argv_match(dl, "snapshot") &&
(o_all & DL_OPT_REGION_SNAPSHOT_ID)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->region_snapshot_id);
if (err)
return err;
o_found |= DL_OPT_REGION_SNAPSHOT_ID;
} else if (dl_argv_match(dl, "address") &&
(o_all & DL_OPT_REGION_ADDRESS)) {
dl_arg_inc(dl);
err = dl_argv_uint64_t(dl, &opts->region_address);
if (err)
return err;
o_found |= DL_OPT_REGION_ADDRESS;
} else if (dl_argv_match(dl, "length") &&
(o_all & DL_OPT_REGION_LENGTH)) {
dl_arg_inc(dl);
err = dl_argv_uint64_t(dl, &opts->region_length);
if (err)
return err;
o_found |= DL_OPT_REGION_LENGTH;
} else if (dl_argv_match(dl, "file") &&
(o_all & DL_OPT_FLASH_FILE_NAME)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->flash_file_name);
if (err)
return err;
o_found |= DL_OPT_FLASH_FILE_NAME;
} else if (dl_argv_match(dl, "component") &&
(o_all & DL_OPT_FLASH_COMPONENT)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->flash_component);
if (err)
return err;
o_found |= DL_OPT_FLASH_COMPONENT;
} else if (dl_argv_match(dl, "overwrite") &&
(o_all & DL_OPT_FLASH_OVERWRITE)) {
const char *sectionstr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &sectionstr);
if(err)
return err;
err = flash_overwrite_section_get(sectionstr,
&opts->overwrite_mask);
if (err)
return err;
o_found |= DL_OPT_FLASH_OVERWRITE;
} else if (dl_argv_match(dl, "reporter") &&
(o_all & DL_OPT_HEALTH_REPORTER_NAME)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->reporter_name);
if (err)
return err;
o_found |= DL_OPT_HEALTH_REPORTER_NAME;
} else if (dl_argv_match(dl, "grace_period") &&
(o_all & DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD)) {
dl_arg_inc(dl);
err = dl_argv_uint64_t(dl,
&opts->reporter_graceful_period);
if (err)
return err;
o_found |= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD;
} else if (dl_argv_match(dl, "auto_recover") &&
(o_all & DL_OPT_HEALTH_REPORTER_AUTO_RECOVER)) {
dl_arg_inc(dl);
err = dl_argv_bool(dl, &opts->reporter_auto_recover);
if (err)
return err;
o_found |= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER;
} else if (dl_argv_match(dl, "auto_dump") &&
(o_all & DL_OPT_HEALTH_REPORTER_AUTO_DUMP)) {
dl_arg_inc(dl);
err = dl_argv_bool(dl, &opts->reporter_auto_dump);
if (err)
return err;
o_found |= DL_OPT_HEALTH_REPORTER_AUTO_DUMP;
} else if (dl_argv_match(dl, "trap") &&
(o_all & DL_OPT_TRAP_NAME)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->trap_name);
if (err)
return err;
o_found |= DL_OPT_TRAP_NAME;
} else if (dl_argv_match(dl, "group") &&
(o_all & DL_OPT_TRAP_GROUP_NAME)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->trap_group_name);
if (err)
return err;
o_found |= DL_OPT_TRAP_GROUP_NAME;
} else if (dl_argv_match(dl, "action") &&
(o_all & DL_OPT_TRAP_ACTION)) {
const char *actionstr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &actionstr);
if (err)
return err;
err = trap_action_get(actionstr, &opts->trap_action);
if (err)
return err;
o_found |= DL_OPT_TRAP_ACTION;
} else if (dl_argv_match(dl, "netns") &&
(o_all & DL_OPT_NETNS)) {
const char *netns_str;
dl_arg_inc(dl);
err = dl_argv_str(dl, &netns_str);
if (err)
return err;
opts->netns = netns_get_fd(netns_str);
if ((int)opts->netns < 0) {
dl_arg_dec(dl);
err = dl_argv_uint32_t(dl, &opts->netns);
if (err)
return err;
opts->netns_is_pid = true;
}
o_found |= DL_OPT_NETNS;
} else if (dl_argv_match(dl, "action") &&
(o_all & DL_OPT_RELOAD_ACTION)) {
const char *actionstr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &actionstr);
if (err)
return err;
err = reload_action_get(dl, actionstr, &opts->reload_action);
if (err)
return err;
o_found |= DL_OPT_RELOAD_ACTION;
} else if (dl_argv_match(dl, "limit") &&
(o_all & DL_OPT_RELOAD_LIMIT)) {
const char *limitstr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &limitstr);
if (err)
return err;
err = reload_limit_get(dl, limitstr, &opts->reload_limit);
if (err)
return err;
o_found |= DL_OPT_RELOAD_LIMIT;
} else if (dl_argv_match(dl, "policer") &&
(o_all & DL_OPT_TRAP_POLICER_ID)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->trap_policer_id);
if (err)
return err;
o_found |= DL_OPT_TRAP_POLICER_ID;
} else if (dl_argv_match(dl, "nopolicer") &&
(o_all & DL_OPT_TRAP_POLICER_ID)) {
dl_arg_inc(dl);
opts->trap_policer_id = 0;
o_found |= DL_OPT_TRAP_POLICER_ID;
} else if (dl_argv_match(dl, "rate") &&
(o_all & DL_OPT_TRAP_POLICER_RATE)) {
dl_arg_inc(dl);
err = dl_argv_uint64_t(dl, &opts->trap_policer_rate);
if (err)
return err;
o_found |= DL_OPT_TRAP_POLICER_RATE;
} else if (dl_argv_match(dl, "burst") &&
(o_all & DL_OPT_TRAP_POLICER_BURST)) {
dl_arg_inc(dl);
err = dl_argv_uint64_t(dl, &opts->trap_policer_burst);
if (err)
return err;
o_found |= DL_OPT_TRAP_POLICER_BURST;
} else if (dl_argv_match(dl, "hw_addr") &&
(o_all & DL_OPT_PORT_FUNCTION_HW_ADDR)) {
const char *addrstr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &addrstr);
if (err)
return err;
err = hw_addr_parse(addrstr, opts->port_function_hw_addr,
&opts->port_function_hw_addr_len);
if (err)
return err;
o_found |= DL_OPT_PORT_FUNCTION_HW_ADDR;
} else if (dl_argv_match(dl, "state") &&
(o_all & DL_OPT_PORT_FUNCTION_STATE)) {
const char *statestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &statestr);
if (err)
return err;
err = port_fn_state_parse(statestr, &opts->port_fn_state);
if (err)
return err;
o_found |= DL_OPT_PORT_FUNCTION_STATE;
} else if (dl_argv_match(dl, "flavour") && (o_all & DL_OPT_PORT_FLAVOUR)) {
const char *flavourstr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &flavourstr);
if (err)
return err;
err = port_flavour_parse(flavourstr, &opts->port_flavour);
if (err)
return err;
o_found |= DL_OPT_PORT_FLAVOUR;
} else if (dl_argv_match(dl, "pfnum") && (o_all & DL_OPT_PORT_PFNUMBER)) {
dl_arg_inc(dl);
err = dl_argv_uint16_t(dl, &opts->port_pfnumber);
if (err)
return err;
o_found |= DL_OPT_PORT_PFNUMBER;
} else if (dl_argv_match(dl, "sfnum") && (o_all & DL_OPT_PORT_SFNUMBER)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->port_sfnumber);
if (err)
return err;
o_found |= DL_OPT_PORT_SFNUMBER;
} else if (dl_argv_match(dl, "controller") && (o_all & DL_OPT_PORT_CONTROLLER)) {
dl_arg_inc(dl);
err = dl_argv_uint32_t(dl, &opts->port_controller);
if (err)
return err;
o_found |= DL_OPT_PORT_CONTROLLER;
} else if (dl_argv_match(dl, "type") &&
(o_all & DL_OPT_PORT_FN_RATE_TYPE)) {
const char *typestr;
dl_arg_inc(dl);
err = dl_argv_str(dl, &typestr);
if (err)
return err;
err = port_fn_rate_type_get(typestr, &opts->rate_type);
if (err)
return err;
o_found |= DL_OPT_PORT_FN_RATE_TYPE;
} else if (dl_argv_match(dl, "tx_share") &&
(o_all & DL_OPT_PORT_FN_RATE_TX_SHARE)) {
dl_arg_inc(dl);
err = port_fn_rate_value_get(dl, &opts->rate_tx_share);
if (err)
return err;
o_found |= DL_OPT_PORT_FN_RATE_TX_SHARE;
} else if (dl_argv_match(dl, "tx_max") &&
(o_all & DL_OPT_PORT_FN_RATE_TX_MAX)) {
dl_arg_inc(dl);
err = port_fn_rate_value_get(dl, &opts->rate_tx_max);
if (err)
return err;
o_found |= DL_OPT_PORT_FN_RATE_TX_MAX;
} else if (dl_argv_match(dl, "parent") &&
(o_all & DL_OPT_PORT_FN_RATE_PARENT)) {
dl_arg_inc(dl);
err = dl_argv_str(dl, &opts->rate_parent_node);
if (err)
return err;
o_found |= DL_OPT_PORT_FN_RATE_PARENT;
} else if (dl_argv_match(dl, "noparent") &&
(o_all & DL_OPT_PORT_FN_RATE_PARENT)) {
dl_arg_inc(dl);
opts->rate_parent_node = "";
o_found |= DL_OPT_PORT_FN_RATE_PARENT;
} else {
pr_err("Unknown option \"%s\"\n", dl_argv(dl));
return -EINVAL;
}
}
opts->present = o_found;
if ((o_optional & DL_OPT_SB) && !(o_found & DL_OPT_SB)) {
opts->sb_index = 0;
opts->present |= DL_OPT_SB;
}
return dl_args_finding_required_validate(o_required, o_found);
}
static void
dl_function_attr_put(struct nlmsghdr *nlh, const struct dl_opts *opts)
{
struct nlattr *nest;
nest = mnl_attr_nest_start(nlh, DEVLINK_ATTR_PORT_FUNCTION);
if (opts->present & DL_OPT_PORT_FUNCTION_HW_ADDR)
mnl_attr_put(nlh, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR,
opts->port_function_hw_addr_len,
opts->port_function_hw_addr);
if (opts->present & DL_OPT_PORT_FUNCTION_STATE)
mnl_attr_put_u8(nlh, DEVLINK_PORT_FN_ATTR_STATE,
opts->port_fn_state);
mnl_attr_nest_end(nlh, nest);
}
static void
dl_flash_update_overwrite_put(struct nlmsghdr *nlh, const struct dl_opts *opts)
{
struct nla_bitfield32 overwrite_mask;
overwrite_mask.selector = DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS;
overwrite_mask.value = opts->overwrite_mask;
mnl_attr_put(nlh, DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK,
sizeof(overwrite_mask), &overwrite_mask);
}
static void
dl_reload_limits_put(struct nlmsghdr *nlh, const struct dl_opts *opts)
{
struct nla_bitfield32 limits;
limits.selector = DEVLINK_RELOAD_LIMITS_VALID_MASK;
limits.value = BIT(opts->reload_limit);
mnl_attr_put(nlh, DEVLINK_ATTR_RELOAD_LIMITS, sizeof(limits), &limits);
}
static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
{
struct dl_opts *opts = &dl->opts;
if (opts->present & DL_OPT_HANDLE) {
mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
} else if (opts->present & DL_OPT_HANDLEP) {
mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_INDEX,
opts->port_index);
} else if (opts->present & DL_OPT_HANDLE_REGION) {
mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
mnl_attr_put_strz(nlh, DEVLINK_ATTR_REGION_NAME,
opts->region_name);
} else if (opts->present & DL_OPT_PORT_FN_RATE_NODE_NAME) {
mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
mnl_attr_put_strz(nlh, DEVLINK_ATTR_RATE_NODE_NAME,
opts->rate_node_name);
}
if (opts->present & DL_OPT_PORT_TYPE)
mnl_attr_put_u16(nlh, DEVLINK_ATTR_PORT_TYPE,
opts->port_type);
if (opts->present & DL_OPT_PORT_COUNT)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_SPLIT_COUNT,
opts->port_count);
if (opts->present & DL_OPT_SB)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_INDEX,
opts->sb_index);
if (opts->present & DL_OPT_SB_POOL)
mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_POOL_INDEX,
opts->sb_pool_index);
if (opts->present & DL_OPT_SB_SIZE)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_POOL_SIZE,
opts->sb_pool_size);
if (opts->present & DL_OPT_SB_TYPE)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_TYPE,
opts->sb_pool_type);
if (opts->present & DL_OPT_SB_THTYPE)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
opts->sb_pool_thtype);
if (opts->present & DL_OPT_SB_TH)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_THRESHOLD,
opts->sb_threshold);
if (opts->present & DL_OPT_SB_TC)
mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_TC_INDEX,
opts->sb_tc_index);
if (opts->present & DL_OPT_ESWITCH_MODE)
mnl_attr_put_u16(nlh, DEVLINK_ATTR_ESWITCH_MODE,
opts->eswitch_mode);
if (opts->present & DL_OPT_ESWITCH_INLINE_MODE)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
opts->eswitch_inline_mode);
if (opts->present & DL_OPT_DPIPE_TABLE_NAME)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_DPIPE_TABLE_NAME,
opts->dpipe_table_name);
if (opts->present & DL_OPT_DPIPE_TABLE_COUNTERS)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
opts->dpipe_counters_enabled);
if (opts->present & DL_OPT_ESWITCH_ENCAP_MODE)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_ENCAP_MODE,
opts->eswitch_encap_mode);
if ((opts->present & DL_OPT_RESOURCE_PATH) && opts->resource_id_valid)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_ID,
opts->resource_id);
if (opts->present & DL_OPT_RESOURCE_SIZE)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_SIZE,
opts->resource_size);
if (opts->present & DL_OPT_PARAM_NAME)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_NAME,
opts->param_name);
if (opts->present & DL_OPT_PARAM_CMODE)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE,
opts->cmode);
if (opts->present & DL_OPT_REGION_SNAPSHOT_ID)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
opts->region_snapshot_id);
if (opts->present & DL_OPT_REGION_ADDRESS)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_REGION_CHUNK_ADDR,
opts->region_address);
if (opts->present & DL_OPT_REGION_LENGTH)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_REGION_CHUNK_LEN,
opts->region_length);
if (opts->present & DL_OPT_FLASH_FILE_NAME)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME,
opts->flash_file_name);
if (opts->present & DL_OPT_FLASH_COMPONENT)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
opts->flash_component);
if (opts->present & DL_OPT_FLASH_OVERWRITE)
dl_flash_update_overwrite_put(nlh, opts);
if (opts->present & DL_OPT_HEALTH_REPORTER_NAME)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
opts->reporter_name);
if (opts->present & DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD)
mnl_attr_put_u64(nlh,
DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
opts->reporter_graceful_period);
if (opts->present & DL_OPT_HEALTH_REPORTER_AUTO_RECOVER)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
opts->reporter_auto_recover);
if (opts->present & DL_OPT_HEALTH_REPORTER_AUTO_DUMP)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
opts->reporter_auto_dump);
if (opts->present & DL_OPT_TRAP_NAME)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_TRAP_NAME,
opts->trap_name);
if (opts->present & DL_OPT_TRAP_GROUP_NAME)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_TRAP_GROUP_NAME,
opts->trap_group_name);
if (opts->present & DL_OPT_TRAP_ACTION)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_TRAP_ACTION,
opts->trap_action);
if (opts->present & DL_OPT_NETNS)
mnl_attr_put_u32(nlh,
opts->netns_is_pid ? DEVLINK_ATTR_NETNS_PID :
DEVLINK_ATTR_NETNS_FD,
opts->netns);
if (opts->present & DL_OPT_RELOAD_ACTION)
mnl_attr_put_u8(nlh, DEVLINK_ATTR_RELOAD_ACTION,
opts->reload_action);
if (opts->present & DL_OPT_RELOAD_LIMIT)
dl_reload_limits_put(nlh, opts);
if (opts->present & DL_OPT_TRAP_POLICER_ID)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_TRAP_POLICER_ID,
opts->trap_policer_id);
if (opts->present & DL_OPT_TRAP_POLICER_RATE)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_TRAP_POLICER_RATE,
opts->trap_policer_rate);
if (opts->present & DL_OPT_TRAP_POLICER_BURST)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_TRAP_POLICER_BURST,
opts->trap_policer_burst);
if (opts->present & (DL_OPT_PORT_FUNCTION_HW_ADDR | DL_OPT_PORT_FUNCTION_STATE))
dl_function_attr_put(nlh, opts);
if (opts->present & DL_OPT_PORT_FLAVOUR)
mnl_attr_put_u16(nlh, DEVLINK_ATTR_PORT_FLAVOUR, opts->port_flavour);
if (opts->present & DL_OPT_PORT_PFNUMBER)
mnl_attr_put_u16(nlh, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, opts->port_pfnumber);
if (opts->present & DL_OPT_PORT_SFNUMBER)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_PCI_SF_NUMBER, opts->port_sfnumber);
if (opts->present & DL_OPT_PORT_CONTROLLER)
mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
opts->port_controller);
if (opts->present & DL_OPT_PORT_FN_RATE_TYPE)
mnl_attr_put_u16(nlh, DEVLINK_ATTR_RATE_TYPE,
opts->rate_type);
if (opts->present & DL_OPT_PORT_FN_RATE_TX_SHARE)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_RATE_TX_SHARE,
opts->rate_tx_share);
if (opts->present & DL_OPT_PORT_FN_RATE_TX_MAX)
mnl_attr_put_u64(nlh, DEVLINK_ATTR_RATE_TX_MAX,
opts->rate_tx_max);
if (opts->present & DL_OPT_PORT_FN_RATE_PARENT)
mnl_attr_put_strz(nlh, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
opts->rate_parent_node);
}
static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl,
uint64_t o_required, uint64_t o_optional)
{
int err;
err = dl_argv_parse(dl, o_required, o_optional);
if (err)
return err;
dl_opts_put(nlh, dl);
return 0;
}
static bool dl_dump_filter(struct dl *dl, struct nlattr **tb)
{
struct dl_opts *opts = &dl->opts;
struct nlattr *attr_bus_name = tb[DEVLINK_ATTR_BUS_NAME];
struct nlattr *attr_dev_name = tb[DEVLINK_ATTR_DEV_NAME];
struct nlattr *attr_port_index = tb[DEVLINK_ATTR_PORT_INDEX];
struct nlattr *attr_sb_index = tb[DEVLINK_ATTR_SB_INDEX];
if (opts->present & DL_OPT_HANDLE &&
attr_bus_name && attr_dev_name) {
const char *bus_name = mnl_attr_get_str(attr_bus_name);
const char *dev_name = mnl_attr_get_str(attr_dev_name);
if (strcmp(bus_name, opts->bus_name) != 0 ||
strcmp(dev_name, opts->dev_name) != 0)
return false;
}
if (opts->present & DL_OPT_HANDLEP &&
attr_bus_name && attr_dev_name && attr_port_index) {
const char *bus_name = mnl_attr_get_str(attr_bus_name);
const char *dev_name = mnl_attr_get_str(attr_dev_name);
uint32_t port_index = mnl_attr_get_u32(attr_port_index);
if (strcmp(bus_name, opts->bus_name) != 0 ||
strcmp(dev_name, opts->dev_name) != 0 ||
port_index != opts->port_index)
return false;
}
if (opts->present & DL_OPT_SB && attr_sb_index) {
uint32_t sb_index = mnl_attr_get_u32(attr_sb_index);
if (sb_index != opts->sb_index)
return false;
}
return true;
}
static void cmd_dev_help(void)
{
pr_err("Usage: devlink dev show [ DEV ]\n");
pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
pr_err(" [ inline-mode { none | link | network | transport } ]\n");
pr_err(" [ encap-mode { none | basic } ]\n");
pr_err(" devlink dev eswitch show DEV\n");
pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
pr_err(" devlink dev param show [DEV name PARAMETER]\n");
pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
pr_err(" [ action { driver_reinit | fw_activate } ] [ limit no_reset ]\n");
pr_err(" devlink dev info [ DEV ]\n");
pr_err(" devlink dev flash DEV file PATH [ component NAME ] [ overwrite SECTION ]\n");
}
static bool cmp_arr_last_handle(struct dl *dl, const char *bus_name,
const char *dev_name)
{
if (!dl->arr_last.present)
return false;
return strcmp(dl->arr_last.bus_name, bus_name) == 0 &&
strcmp(dl->arr_last.dev_name, dev_name) == 0;
}
static void arr_last_handle_set(struct dl *dl, const char *bus_name,
const char *dev_name)
{
dl->arr_last.present = true;
free(dl->arr_last.dev_name);
free(dl->arr_last.bus_name);
dl->arr_last.bus_name = strdup(bus_name);
dl->arr_last.dev_name = strdup(dev_name);
}
static bool should_arr_last_handle_start(struct dl *dl, const char *bus_name,
const char *dev_name)
{
return !cmp_arr_last_handle(dl, bus_name, dev_name);
}
static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
const char *dev_name)
{
return dl->arr_last.present &&
!cmp_arr_last_handle(dl, bus_name, dev_name);
}
static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb,
bool content, bool array)
{
const char *bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
char buf[64];
sprintf(buf, "%s/%s", bus_name, dev_name);
if (dl->json_output) {
if (array) {
if (should_arr_last_handle_end(dl, bus_name, dev_name))
close_json_array(PRINT_JSON, NULL);
if (should_arr_last_handle_start(dl, bus_name,
dev_name)) {
open_json_array(PRINT_JSON, buf);
open_json_object(NULL);
arr_last_handle_set(dl, bus_name, dev_name);
} else {
open_json_object(NULL);
}
} else {
open_json_object(buf);
}
} else {
if (array) {
if (should_arr_last_handle_end(dl, bus_name, dev_name))
__pr_out_indent_dec();
if (should_arr_last_handle_start(dl, bus_name,
dev_name)) {
pr_out("%s%s", buf, content ? ":" : "");
__pr_out_newline();
__pr_out_indent_inc();
arr_last_handle_set(dl, bus_name, dev_name);
}
} else {
pr_out("%s%s", buf, content ? ":" : "");
}
}
}
static void pr_out_handle_start_arr(struct dl *dl, struct nlattr **tb)
{
__pr_out_handle_start(dl, tb, true, true);
}
static void pr_out_handle_end(struct dl *dl)
{
if (dl->json_output)
close_json_object();
else
__pr_out_newline();
}
static void pr_out_handle(struct dl *dl, struct nlattr **tb)
{
__pr_out_handle_start(dl, tb, false, false);
pr_out_handle_end(dl);
}
static bool cmp_arr_last_port_handle(struct dl *dl, const char *bus_name,
const char *dev_name, uint32_t port_index)
{
return cmp_arr_last_handle(dl, bus_name, dev_name) &&
dl->arr_last.port_index == port_index;
}
static void arr_last_port_handle_set(struct dl *dl, const char *bus_name,
const char *dev_name, uint32_t port_index)
{
arr_last_handle_set(dl, bus_name, dev_name);
dl->arr_last.port_index = port_index;
}
static bool should_arr_last_port_handle_start(struct dl *dl,
const char *bus_name,
const char *dev_name,
uint32_t port_index)
{
return !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index);
}
static bool should_arr_last_port_handle_end(struct dl *dl,
const char *bus_name,
const char *dev_name,
uint32_t port_index)
{
return dl->arr_last.present &&
!cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index);
}
static void __pr_out_port_handle_start(struct dl *dl, const char *bus_name,
const char *dev_name,
uint32_t port_index, bool try_nice,
bool array)
{
static char buf[64];
char *ifname = NULL;
if (dl->no_nice_names || !try_nice ||
ifname_map_rev_lookup(dl, bus_name, dev_name,
port_index, &ifname) != 0)
sprintf(buf, "%s/%s/%d", bus_name, dev_name, port_index);
else
sprintf(buf, "%s", ifname);
if (dl->json_output) {
if (array) {
if (should_arr_last_port_handle_end(dl, bus_name,
dev_name,
port_index))
close_json_array(PRINT_JSON, NULL);
if (should_arr_last_port_handle_start(dl, bus_name,
dev_name,
port_index)) {
open_json_array(PRINT_JSON, buf);
open_json_object(NULL);
arr_last_port_handle_set(dl, bus_name, dev_name,
port_index);
} else {
open_json_object(NULL);
}
} else {
open_json_object(buf);
}
} else {
if (array) {
if (should_arr_last_port_handle_end(dl, bus_name, dev_name, port_index))
__pr_out_indent_dec();
if (should_arr_last_port_handle_start(dl, bus_name,
dev_name, port_index)) {
pr_out("%s:", buf);
__pr_out_newline();
__pr_out_indent_inc();
arr_last_port_handle_set(dl, bus_name, dev_name, port_index);
}
} else {
pr_out("%s:", buf);
}
}
}
static void pr_out_port_handle_start(struct dl *dl, struct nlattr **tb, bool try_nice)
{
const char *bus_name;
const char *dev_name;
uint32_t port_index;
bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
__pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, false);
}
static void pr_out_port_handle_start_arr(struct dl *dl, struct nlattr **tb, bool try_nice)
{
const char *bus_name;
const char *dev_name;
uint32_t port_index;
bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
__pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, true);
}
static void pr_out_port_handle_end(struct dl *dl)
{
if (dl->json_output)
close_json_object();
else
pr_out("\n");
}
static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr)
{
if (dl->json_output) {
print_uint(PRINT_JSON, "address", NULL, addr);
open_json_array(PRINT_JSON, "data"