#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 "SNAPSHOT.h"
+#include "version.h"
#include "list.h"
#include "mnlg.h"
-#include "json_writer.h"
+#include "json_print.h"
#include "utils.h"
#include "namespace.h"
#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)
struct dl_opts {
uint64_t present; /* flags of present items */
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;
};
struct dl {
[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_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_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,
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)
{
*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 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, },
+};
+
struct dl_args_metadata {
uint64_t o_flag;
char err_msg[DL_ARGS_REQUIRED_MAX_ERR_LEN];
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, §ionstr);
+ 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);
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);
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_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);
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);
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 ]\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,
open_json_object(buf);
}
} else {
- pr_out("%s:", buf);
+ 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 const char *reload_action_name(uint8_t reload_action)
+{
+ switch (reload_action) {
+ case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
+ return "driver_reinit";
+ case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
+ return "fw_activate";
+ default:
+ return "<unknown reload action>";
+ }
+}
+
+static const char *reload_limit_name(uint8_t reload_limit)
+{
+ switch (reload_limit) {
+ case DEVLINK_RELOAD_LIMIT_UNSPEC:
+ return "unspecified";
+ case DEVLINK_RELOAD_LIMIT_NO_RESET:
+ return "no_reset";
+ default:
+ return "<unknown reload action>";
+ }
+}
+
static const char *eswitch_mode_name(uint32_t mode)
{
switch (mode) {
.vstr = "flash",
.vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH,
},
+ {
+ .name = "fw_load_policy",
+ .vstr = "disk",
+ .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DISK,
+ },
{
.name = "reset_dev_on_drv_probe",
.vstr = "unknown",
struct param_ctx ctx = {};
struct nlmsghdr *nlh;
bool conv_exists;
- uint32_t val_u32;
+ uint32_t val_u32 = 0;
uint16_t val_u16;
uint8_t val_u8;
bool val_bool;
pr_err("Command \"%s\" not found\n", dl_argv(dl));
return -ENOENT;
}
-static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data)
+
+static void pr_out_action_stats(struct dl *dl, struct nlattr *action_stats)
{
- struct dl *dl = data;
+ struct nlattr *tb_stats_entry[DEVLINK_ATTR_MAX + 1] = {};
+ struct nlattr *nla_reload_stats_entry, *nla_limit, *nla_value;
+ enum devlink_reload_limit limit;
+ uint32_t value;
+ int err;
+
+ mnl_attr_for_each_nested(nla_reload_stats_entry, action_stats) {
+ err = mnl_attr_parse_nested(nla_reload_stats_entry, attr_cb,
+ tb_stats_entry);
+ if (err != MNL_CB_OK)
+ return;
+
+ nla_limit = tb_stats_entry[DEVLINK_ATTR_RELOAD_STATS_LIMIT];
+ nla_value = tb_stats_entry[DEVLINK_ATTR_RELOAD_STATS_VALUE];
+ if (!nla_limit || !nla_value)
+ return;
+
+ check_indent_newline(dl);
+ limit = mnl_attr_get_u8(nla_limit);
+ value = mnl_attr_get_u32(nla_value);
+ print_uint_name_value(reload_limit_name(limit), value);
+ }
+}
+
+static void pr_out_reload_stats(struct dl *dl, struct nlattr *reload_stats)
+{
+ struct nlattr *nla_action_info, *nla_action, *nla_action_stats;
struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
- struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
- uint8_t reload_failed = 0;
+ enum devlink_reload_action action;
+ int err;
- mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
- if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
- return MNL_CB_ERROR;
+ mnl_attr_for_each_nested(nla_action_info, reload_stats) {
+ err = mnl_attr_parse_nested(nla_action_info, attr_cb, tb);
+ if (err != MNL_CB_OK)
+ return;
+ nla_action = tb[DEVLINK_ATTR_RELOAD_ACTION];
+ nla_action_stats = tb[DEVLINK_ATTR_RELOAD_ACTION_STATS];
+ if (!nla_action || !nla_action_stats)
+ return;
+
+ action = mnl_attr_get_u8(nla_action);
+ pr_out_object_start(dl, reload_action_name(action));
+ pr_out_action_stats(dl, nla_action_stats);
+ pr_out_object_end(dl);
+ }
+}
+
+static void pr_out_reload_data(struct dl *dl, struct nlattr **tb)
+{
+ struct nlattr *nla_reload_stats, *nla_remote_reload_stats;
+ struct nlattr *tb_stats[DEVLINK_ATTR_MAX + 1] = {};
+ uint8_t reload_failed = 0;
+ int err;
if (tb[DEVLINK_ATTR_RELOAD_FAILED])
reload_failed = mnl_attr_get_u8(tb[DEVLINK_ATTR_RELOAD_FAILED]);
if (reload_failed) {
- __pr_out_handle_start(dl, tb, true, false);
check_indent_newline(dl);
print_bool(PRINT_ANY, "reload_failed", "reload_failed %s", true);
+ }
+ if (!tb[DEVLINK_ATTR_DEV_STATS] || !dl->stats)
+ return;
+ err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_DEV_STATS], attr_cb,
+ tb_stats);
+ if (err != MNL_CB_OK)
+ return;
+
+ pr_out_object_start(dl, "stats");
+
+ nla_reload_stats = tb_stats[DEVLINK_ATTR_RELOAD_STATS];
+ if (nla_reload_stats) {
+ pr_out_object_start(dl, "reload");
+ pr_out_reload_stats(dl, nla_reload_stats);
+ pr_out_object_end(dl);
+ }
+ nla_remote_reload_stats = tb_stats[DEVLINK_ATTR_REMOTE_RELOAD_STATS];
+ if (nla_remote_reload_stats) {
+ pr_out_object_start(dl, "remote_reload");
+ pr_out_reload_stats(dl, nla_remote_reload_stats);
+ pr_out_object_end(dl);
+ }
+
+ pr_out_object_end(dl);
+}
+
+
+static void pr_out_dev(struct dl *dl, struct nlattr **tb)
+{
+ if ((tb[DEVLINK_ATTR_RELOAD_FAILED] && mnl_attr_get_u8(tb[DEVLINK_ATTR_RELOAD_FAILED])) ||
+ (tb[DEVLINK_ATTR_DEV_STATS] && dl->stats)) {
+ __pr_out_handle_start(dl, tb, true, false);
+ pr_out_reload_data(dl, tb);
pr_out_handle_end(dl);
} else {
pr_out_handle(dl, tb);
}
+}
+
+static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct dl *dl = data;
+ struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
+ struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
+
+ mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
+ if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
+ return MNL_CB_ERROR;
+ pr_out_dev(dl, tb);
return MNL_CB_OK;
}
return err;
}
+static void pr_out_reload_actions_performed(struct dl *dl, struct nlattr **tb)
+{
+ struct nlattr *nla_actions_performed;
+ struct nla_bitfield32 *actions;
+ uint32_t actions_performed;
+ uint16_t len;
+ int action;
+
+ if (!tb[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED])
+ return;
+
+ nla_actions_performed = tb[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED];
+ len = mnl_attr_get_payload_len(nla_actions_performed);
+ if (len != sizeof(*actions))
+ return;
+ actions = mnl_attr_get_payload(nla_actions_performed);
+ if (!actions)
+ return;
+ g_new_line_count = 1; /* Avoid extra new line in non-json print */
+ pr_out_array_start(dl, "reload_actions_performed");
+ actions_performed = actions->value & actions->selector;
+ for (action = 0; action <= DEVLINK_RELOAD_ACTION_MAX; action++) {
+ if (BIT(action) & actions_performed) {
+ check_indent_newline(dl);
+ print_string(PRINT_ANY, NULL, "%s",
+ reload_action_name(action));
+ }
+ }
+ pr_out_array_end(dl);
+ if (!dl->json_output)
+ __pr_out_newline();
+}
+
+static int cmd_dev_reload_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
+ struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
+ struct dl *dl = data;
+
+ mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
+ if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
+ !tb[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED])
+ return MNL_CB_ERROR;
+
+ pr_out_section_start(dl, "reload");
+ pr_out_reload_actions_performed(dl, tb);
+ pr_out_section_end(dl);
+
+ return MNL_CB_OK;
+}
+
static int cmd_dev_reload(struct dl *dl)
{
struct nlmsghdr *nlh;
nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RELOAD,
NLM_F_REQUEST | NLM_F_ACK);
- err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_NETNS);
+ err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE,
+ DL_OPT_NETNS | DL_OPT_RELOAD_ACTION |
+ DL_OPT_RELOAD_LIMIT);
if (err)
return err;
- return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
+ return _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_reload_cb, dl);
}
static void pr_out_versions_single(struct dl *dl, const struct nlmsghdr *nlh,
print_string(PRINT_ANY, "serial_number", "serial_number %s",
mnl_attr_get_str(nla_sn));
}
+
+ if (tb[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER]) {
+ struct nlattr *nla_bsn = tb[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER];
+
+ if (!dl->json_output)
+ __pr_out_newline();
+ check_indent_newline(dl);
+ print_string(PRINT_ANY, "board.serial_number", "board.serial_number %s",
+ mnl_attr_get_str(nla_bsn));
+ }
__pr_out_indent_dec();
if (has_versions) {
tb[DEVLINK_ATTR_INFO_VERSION_STORED];
has_info = tb[DEVLINK_ATTR_INFO_DRIVER_NAME] ||
tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER] ||
+ tb[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER] ||
has_versions;
if (has_info)
struct cmd_dev_flash_status_ctx {
struct dl *dl;
+ struct timespec time_of_last_status;
+ uint64_t status_msg_timeout;
+ size_t elapsed_time_msg_len;
char *last_msg;
char *last_component;
uint8_t not_first:1,
return str1 ? 1 : -1;
}
+static void cmd_dev_flash_clear_elapsed_time(struct cmd_dev_flash_status_ctx *ctx)
+{
+ int i;
+
+ for (i = 0; i < ctx->elapsed_time_msg_len; i++)
+ pr_out_tty("\b \b");
+
+ ctx->elapsed_time_msg_len = 0;
+}
+
static int cmd_dev_flash_status_cb(const struct nlmsghdr *nlh, void *data)
{
struct cmd_dev_flash_status_ctx *ctx = data;
const char *bus_name;
const char *dev_name;
+ cmd_dev_flash_clear_elapsed_time(ctx);
+
if (genl->cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS &&
genl->cmd != DEVLINK_CMD_FLASH_UPDATE_END)
return MNL_CB_STOP;
done = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE]);
if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL])
total = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL]);
+ if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT])
+ ctx->status_msg_timeout = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT]);
+ else
+ ctx->status_msg_timeout = 0;
if (!nullstrcmp(msg, ctx->last_msg) &&
!nullstrcmp(component, ctx->last_component) &&
ctx->last_pc && ctx->not_first) {
pr_out_tty("\b\b\b\b\b"); /* clean percentage */
} else {
+ /* only update the last status timestamp if the message changed */
+ clock_gettime(CLOCK_MONOTONIC, &ctx->time_of_last_status);
+
if (ctx->not_first)
pr_out("\n");
if (component) {
return MNL_CB_STOP;
}
+static void cmd_dev_flash_time_elapsed(struct cmd_dev_flash_status_ctx *ctx)
+{
+ struct timespec now, res;
+
+ clock_gettime(CLOCK_MONOTONIC, &now);
+
+ res.tv_sec = now.tv_sec - ctx->time_of_last_status.tv_sec;
+ res.tv_nsec = now.tv_nsec - ctx->time_of_last_status.tv_nsec;
+ if (res.tv_nsec < 0) {
+ res.tv_sec--;
+ res.tv_nsec += 1000000000L;
+ }
+
+ /* Only begin displaying an elapsed time message if we've waited a few
+ * seconds with no response, or the status message included a timeout
+ * value.
+ */
+ if (res.tv_sec > 2 || ctx->status_msg_timeout) {
+ uint64_t elapsed_m, elapsed_s;
+ char msg[128];
+ size_t len;
+
+ /* clear the last elapsed time message, if we have one */
+ cmd_dev_flash_clear_elapsed_time(ctx);
+
+ elapsed_m = res.tv_sec / 60;
+ elapsed_s = res.tv_sec % 60;
+
+ /**
+ * If we've elapsed a few seconds without receiving any status
+ * notification from the device, we display a time elapsed
+ * message. This has a few possible formats:
+ *
+ * 1) just time elapsed, when no timeout was provided
+ * " ( Xm Ys )"
+ * 2) time elapsed out of a timeout that came from the device
+ * driver via DEVLINK_CMD_FLASH_UPDATE_STATUS_TIMEOUT
+ * " ( Xm Ys : Am Ys)"
+ * 3) time elapsed if we still receive no status after
+ * reaching the provided timeout.
+ * " ( Xm Ys : timeout reached )"
+ */
+ if (!ctx->status_msg_timeout) {
+ len = snprintf(msg, sizeof(msg),
+ " ( %lum %lus )", elapsed_m, elapsed_s);
+ } else if (res.tv_sec <= ctx->status_msg_timeout) {
+ uint64_t timeout_m, timeout_s;
+
+ timeout_m = ctx->status_msg_timeout / 60;
+ timeout_s = ctx->status_msg_timeout % 60;
+
+ len = snprintf(msg, sizeof(msg),
+ " ( %lum %lus : %lum %lus )",
+ elapsed_m, elapsed_s, timeout_m, timeout_s);
+ } else {
+ len = snprintf(msg, sizeof(msg),
+ " ( %lum %lus : timeout reached )", elapsed_m, elapsed_s);
+ }
+
+ ctx->elapsed_time_msg_len = len;
+
+ pr_out_tty("%s", msg);
+ fflush(stdout);
+ }
+}
+
static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx *ctx,
struct mnlg_socket *nlg_ntf,
int pipe_r)
{
int nlfd = mnlg_socket_get_fd(nlg_ntf);
+ struct timeval timeout;
fd_set fds[3];
int fdmax;
int i;
if (nlfd >= fdmax)
fdmax = nlfd + 1;
- while (select(fdmax, &fds[0], &fds[1], &fds[2], NULL) < 0) {
+ /* select only for a short while (1/10th of a second) in order to
+ * allow periodically updating the screen with an elapsed time
+ * indicator.
+ */
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 100000;
+
+ while (select(fdmax, &fds[0], &fds[1], &fds[2], &timeout) < 0) {
if (errno == EINTR)
continue;
pr_err("select() failed\n");
return err2;
ctx->flash_done = 1;
}
+ cmd_dev_flash_time_elapsed(ctx);
return 0;
}
NLM_F_REQUEST | NLM_F_ACK);
err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_FLASH_FILE_NAME,
- DL_OPT_FLASH_COMPONENT);
+ DL_OPT_FLASH_COMPONENT | DL_OPT_FLASH_OVERWRITE);
if (err)
return err;
err = _mnlg_socket_group_add(nlg_ntf, DEVLINK_GENL_MCGRP_CONFIG_NAME);
if (err)
- return err;
+ goto err_socket;
err = pipe(pipe_fds);
- if (err == -1)
- return -errno;
+ if (err == -1) {
+ err = -errno;
+ goto err_socket;
+ }
pipe_r = pipe_fds[0];
pipe_w = pipe_fds[1];
pid = fork();
if (pid == -1) {
- close(pipe_r);
close(pipe_w);
- return -errno;
+ err = -errno;
+ goto out;
} else if (!pid) {
/* In child, just execute the flash and pass returned
* value through pipe once it is done.
}
close(pipe_w);
+ /* initialize starting time to allow comparison for when to begin
+ * displaying a time elapsed message.
+ */
+ clock_gettime(CLOCK_MONOTONIC, &ctx.time_of_last_status);
+
do {
err = cmd_dev_flash_fds_process(&ctx, nlg_ntf, pipe_r);
if (err)
err = _mnlg_socket_recv_run(dl->nlg, NULL, NULL);
out:
close(pipe_r);
+err_socket:
mnlg_socket_close(nlg_ntf);
return err;
}
pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
pr_err(" devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
+ pr_err(" devlink port health show [ DEV/PORT_INDEX reporter REPORTER_NAME ]\n");
}
static const char *port_type_name(uint32_t type)
static const char *port_flavour_name(uint16_t flavour)
{
- switch (flavour) {
- case DEVLINK_PORT_FLAVOUR_PHYSICAL:
- return "physical";
- case DEVLINK_PORT_FLAVOUR_CPU:
- 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>";
- }
+ const char *str;
+
+ str = str_map_lookup_u16(port_flavour_map, flavour);
+ return str ? str : "<unknown flavour>";
}
-static void pr_out_port_pfvf_num(struct dl *dl, struct nlattr **tb)
+static void pr_out_port_pfvfsf_num(struct dl *dl, struct nlattr **tb)
{
uint16_t fn_num;
+ if (tb[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER])
+ print_uint(PRINT_ANY, "controller", " controller %u",
+ mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]));
if (tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
print_uint(PRINT_ANY, "pfnum", " pfnum %u", fn_num);
fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_VF_NUMBER]);
print_uint(PRINT_ANY, "vfnum", " vfnum %u", fn_num);
}
+ if (tb[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
+ fn_num = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
+ print_uint(PRINT_ANY, "sfnum", " sfnum %u", fn_num);
+ }
+ if (tb[DEVLINK_ATTR_PORT_EXTERNAL]) {
+ uint8_t external;
+
+ external = mnl_attr_get_u8(tb[DEVLINK_ATTR_PORT_EXTERNAL]);
+ print_bool(PRINT_ANY, "external", " external %s", external);
+ }
}
static void pr_out_port_function(struct dl *dl, struct nlattr **tb_port)
switch (port_flavour) {
case DEVLINK_PORT_FLAVOUR_PCI_PF:
case DEVLINK_PORT_FLAVOUR_PCI_VF:
- pr_out_port_pfvf_num(dl, tb);
+ case DEVLINK_PORT_FLAVOUR_PCI_SF:
+ pr_out_port_pfvfsf_num(dl, tb);
break;
default:
break;
if (tb[DEVLINK_ATTR_PORT_SPLIT_GROUP])
print_uint(PRINT_ANY, "split_group", " split_group %u",
mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_SPLIT_GROUP]));
+ if (tb[DEVLINK_ATTR_PORT_SPLITTABLE])
+ print_bool(PRINT_ANY, "splittable", " splittable %s",
+ mnl_attr_get_u8(tb[DEVLINK_ATTR_PORT_SPLITTABLE]));
+ if (tb[DEVLINK_ATTR_PORT_LANES])
+ print_uint(PRINT_ANY, "lanes", " lanes %u",
+ mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_LANES]));
+
pr_out_port_function(dl, tb);
pr_out_port_handle_end(dl);
}
return -ENOENT;
}
+static int cmd_health(struct dl *dl);
+static int __cmd_health_show(struct dl *dl, bool show_device, bool show_port);
+
static int cmd_port(struct dl *dl)
{
if (dl_argv_match(dl, "help")) {
} else if (dl_argv_match(dl, "function")) {
dl_arg_inc(dl);
return cmd_port_function(dl);
+ } else if (dl_argv_match(dl, "health")) {
+ dl_arg_inc(dl);
+ if (dl_argv_match(dl, "list") || dl_no_arg(dl)
+ || (dl_argv_match(dl, "show") && dl_argc(dl) == 1)) {
+ dl_arg_inc(dl);
+ return __cmd_health_show(dl, false, true);
+ } else {
+ return cmd_health(dl);
+ }
}
pr_err("Command \"%s\" not found\n", dl_argv(dl));
return -ENOENT;
}
static void pr_out_region(struct dl *dl, struct nlattr **tb);
-static void pr_out_health(struct dl *dl, struct nlattr **tb_health);
+static void pr_out_health(struct dl *dl, struct nlattr **tb_health,
+ bool show_device, bool show_port);
static void pr_out_trap(struct dl *dl, struct nlattr **tb, bool array);
static void pr_out_trap_group(struct dl *dl, struct nlattr **tb, bool array);
static void pr_out_trap_policer(struct dl *dl, struct nlattr **tb, bool array);
if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
return MNL_CB_ERROR;
pr_out_mon_header(genl->cmd);
- pr_out_handle(dl, tb);
+ dl->stats = true;
+ pr_out_dev(dl, tb);
pr_out_mon_footer();
break;
case DEVLINK_CMD_PORT_GET: /* fall through */
!tb[DEVLINK_ATTR_HEALTH_REPORTER])
return MNL_CB_ERROR;
pr_out_mon_header(genl->cmd);
- pr_out_health(dl, tb);
+ pr_out_health(dl, tb, true, true);
pr_out_mon_footer();
break;
case DEVLINK_CMD_TRAP_GET: /* fall through */
pr_out_trap_policer(dl, tb, false);
break;
}
+ fflush(stdout);
return MNL_CB_OK;
}
nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_SET,
NLM_F_REQUEST | NLM_F_ACK);
- err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME,
+ err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP | DL_OPT_HEALTH_REPORTER_NAME,
DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD |
DL_OPT_HEALTH_REPORTER_AUTO_RECOVER |
DL_OPT_HEALTH_REPORTER_AUTO_DUMP);
NLM_F_REQUEST | NLM_F_ACK);
err = dl_argv_parse_put(nlh, dl,
- DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME, 0);
+ DL_OPT_HANDLE | DL_OPT_HANDLEP |
+ DL_OPT_HEALTH_REPORTER_NAME, 0);
if (err)
return err;
nlh = mnlg_msg_prepare(dl->nlg, cmd, flags | NLM_F_REQUEST | NLM_F_ACK);
err = dl_argv_parse_put(nlh, dl,
- DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME, 0);
+ DL_OPT_HANDLE | DL_OPT_HANDLEP |
+ DL_OPT_HEALTH_REPORTER_NAME, 0);
if (err)
return err;
0);
}
+static int cmd_health_test(struct dl *dl)
+{
+ return cmd_health_object_common(dl,
+ DEVLINK_CMD_HEALTH_REPORTER_TEST,
+ 0);
+}
+
static int cmd_health_recover(struct dl *dl)
{
struct nlmsghdr *nlh;
NLM_F_REQUEST | NLM_F_ACK);
err = dl_argv_parse_put(nlh, dl,
- DL_OPT_HANDLE | DL_OPT_HEALTH_REPORTER_NAME, 0);
+ DL_OPT_HANDLE | DL_OPT_HANDLEP |
+ DL_OPT_HEALTH_REPORTER_NAME, 0);
if (err)
return err;
print_string(PRINT_ANY, "last_dump_time", " last_dump_time %s", dump_time);
}
-static void pr_out_health(struct dl *dl, struct nlattr **tb_health)
+static void pr_out_health(struct dl *dl, struct nlattr **tb_health,
+ bool print_device, bool print_port)
{
struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
enum devlink_health_reporter_state state;
!tb[DEVLINK_ATTR_HEALTH_REPORTER_STATE])
return;
- pr_out_handle_start_arr(dl, tb_health);
+ if (!print_device && !print_port)
+ return;
+ if (print_port) {
+ if (!print_device && !tb_health[DEVLINK_ATTR_PORT_INDEX])
+ return;
+ else if (tb_health[DEVLINK_ATTR_PORT_INDEX])
+ pr_out_port_handle_start_arr(dl, tb_health, false);
+ }
+ if (print_device) {
+ if (!print_port && tb_health[DEVLINK_ATTR_PORT_INDEX])
+ return;
+ else if (!tb_health[DEVLINK_ATTR_PORT_INDEX])
+ pr_out_handle_start_arr(dl, tb_health);
+ }
check_indent_newline(dl);
print_string(PRINT_ANY, "reporter", "reporter %s",
pr_out_handle_end(dl);
}
+struct health_ctx {
+ struct dl *dl;
+ bool show_device;
+ bool show_port;
+};
+
static int cmd_health_show_cb(const struct nlmsghdr *nlh, void *data)
{
struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
- struct dl *dl = data;
+ struct health_ctx *ctx = data;
+ struct dl *dl = ctx->dl;
mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
!tb[DEVLINK_ATTR_HEALTH_REPORTER])
return MNL_CB_ERROR;
- pr_out_health(dl, tb);
+ pr_out_health(dl, tb, ctx->show_device, ctx->show_port);
return MNL_CB_OK;
}
-static int cmd_health_show(struct dl *dl)
+static int __cmd_health_show(struct dl *dl, bool show_device, bool show_port)
{
struct nlmsghdr *nlh;
+ struct health_ctx ctx = { dl, show_device, show_port };
uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
int err;
flags);
if (dl_argc(dl) > 0) {
+ ctx.show_port = true;
err = dl_argv_parse_put(nlh, dl,
- DL_OPT_HANDLE |
+ DL_OPT_HANDLE | DL_OPT_HANDLEP |
DL_OPT_HEALTH_REPORTER_NAME, 0);
if (err)
return err;
}
pr_out_section_start(dl, "health");
- err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_health_show_cb, dl);
+ err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_health_show_cb, &ctx);
pr_out_section_end(dl);
return err;
}
static void cmd_health_help(void)
{
- pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
- pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
- pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
- pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
- pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
- pr_err(" devlink health set DEV reporter REPORTER_NAME\n");
+ pr_err("Usage: devlink health show [ { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME ]\n");
+ pr_err(" devlink health recover { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
+ pr_err(" devlink health diagnose { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
+ pr_err(" devlink health test { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
+ pr_err(" devlink health dump show { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
+ pr_err(" devlink health dump clear { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
+ pr_err(" devlink health set { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
pr_err(" [ grace_period MSEC ]\n");
pr_err(" [ auto_recover { true | false } ]\n");
pr_err(" [ auto_dump { true | false } ]\n");
} else if (dl_argv_match(dl, "show") ||
dl_argv_match(dl, "list") || dl_no_arg(dl)) {
dl_arg_inc(dl);
- return cmd_health_show(dl);
+ return __cmd_health_show(dl, true, true);
} else if (dl_argv_match(dl, "recover")) {
dl_arg_inc(dl);
return cmd_health_recover(dl);
} else if (dl_argv_match(dl, "diagnose")) {
dl_arg_inc(dl);
return cmd_health_diagnose(dl);
+ } else if (dl_argv_match(dl, "test")) {
+ dl_arg_inc(dl);
+ return cmd_health_test(dl);
} else if (dl_argv_match(dl, "dump")) {
dl_arg_inc(dl);
if (dl_argv_match(dl, "show")) {
return "drop";
case DEVLINK_TRAP_TYPE_EXCEPTION:
return "exception";
+ case DEVLINK_TRAP_TYPE_CONTROL:
+ return "control";
default:
return "<unknown type>";
}
return "drop";
case DEVLINK_TRAP_ACTION_TRAP:
return "trap";
+ case DEVLINK_TRAP_ACTION_MIRROR:
+ return "mirror";
default:
return "<unknown action>";
}
static void cmd_trap_help(void)
{
- pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
+ pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
pr_err(" devlink trap show [ DEV trap TRAP ]\n");
- pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
+ pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
pr_err(" [ policer POLICER ] [ nopolicer ]\n");
pr_err(" devlink trap group show [ DEV group GROUP ]\n");
pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
free(dl);
}
-static int dl_batch(struct dl *dl, const char *name, bool force)
+static int dl_batch_cmd(int argc, char *argv[], void *data)
{
- char *line = NULL;
- size_t len = 0;
- int ret = EXIT_SUCCESS;
-
- if (name && strcmp(name, "-") != 0) {
- if (freopen(name, "r", stdin) == NULL) {
- fprintf(stderr,
- "Cannot open file \"%s\" for reading: %s\n",
- name, strerror(errno));
- return EXIT_FAILURE;
- }
- }
-
- cmdlineno = 0;
- while (getcmdline(&line, &len, stdin) != -1) {
- char *largv[100];
- int largc;
-
- largc = makeargs(line, largv, 100);
- if (!largc)
- continue; /* blank line */
-
- if (dl_cmd(dl, largc, largv)) {
- fprintf(stderr, "Command failed %s:%d\n",
- name, cmdlineno);
- ret = EXIT_FAILURE;
- if (!force)
- break;
- }
- }
+ struct dl *dl = data;
- if (line)
- free(line);
+ return dl_cmd(dl, argc, argv);
+}
- return ret;
+static int dl_batch(struct dl *dl, const char *name, bool force)
+{
+ return do_batch(name, force, dl_batch_cmd, dl);
}
int main(int argc, char **argv)
switch (opt) {
case 'V':
- printf("devlink utility, iproute2-ss%s\n", SNAPSHOT);
+ printf("devlink utility, iproute2-%s\n", version);
ret = EXIT_SUCCESS;
goto dl_free;
case 'f':