]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - devlink/devlink.c
devlink: Refactor validation of finding required arguments
[mirror_iproute2.git] / devlink / devlink.c
index 3651e90c115975d077c726c6611e2f0f9f9dc2cb..a774b196b7fdb24e1a2cd1bc79ccefdaa63b1c9c 100644 (file)
@@ -39,6 +39,7 @@
 #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
 
 static int g_new_line_count;
 
@@ -199,6 +200,8 @@ static void ifname_map_free(struct ifname_map *ifname_map)
 #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)
 
 struct dl_opts {
        uint32_t present; /* flags of present items */
@@ -230,6 +233,8 @@ struct dl_opts {
        uint32_t region_snapshot_id;
        uint64_t region_address;
        uint64_t region_length;
+       const char *flash_file_name;
+       const char *flash_component;
 };
 
 struct dl {
@@ -383,6 +388,13 @@ static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = {
        [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_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,
 };
 
 static int attr_cb(const struct nlattr *attr, void *data)
@@ -943,6 +955,49 @@ static int param_cmode_get(const char *cmodestr,
        return 0;
 }
 
+struct dl_args_metadata {
+       uint32_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_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."},
+};
+
+static int dl_args_finding_required_validate(uint32_t o_required,
+                                            uint32_t o_found)
+{
+       uint32_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;
+               }
+       }
+       return 0;
+}
+
 static int dl_argv_parse(struct dl *dl, uint32_t o_required,
                         uint32_t o_optional)
 {
@@ -1178,6 +1233,20 @@ static int dl_argv_parse(struct dl *dl, uint32_t o_required,
                        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 {
                        pr_err("Unknown option \"%s\"\n", dl_argv(dl));
                        return -EINVAL;
@@ -1191,114 +1260,7 @@ static int dl_argv_parse(struct dl *dl, uint32_t o_required,
                opts->present |= DL_OPT_SB;
        }
 
-       if ((o_required & DL_OPT_PORT_TYPE) && !(o_found & DL_OPT_PORT_TYPE)) {
-               pr_err("Port type option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_PORT_COUNT) &&
-           !(o_found & DL_OPT_PORT_COUNT)) {
-               pr_err("Port split count option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_SB_POOL) && !(o_found & DL_OPT_SB_POOL)) {
-               pr_err("Pool index option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_SB_SIZE) && !(o_found & DL_OPT_SB_SIZE)) {
-               pr_err("Pool size option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_SB_TYPE) && !(o_found & DL_OPT_SB_TYPE)) {
-               pr_err("Pool type option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_SB_THTYPE) && !(o_found & DL_OPT_SB_THTYPE)) {
-               pr_err("Pool threshold type option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_SB_TH) && !(o_found & DL_OPT_SB_TH)) {
-               pr_err("Threshold option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_SB_TC) && !(o_found & DL_OPT_SB_TC)) {
-               pr_err("TC index option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_ESWITCH_MODE) &&
-           !(o_found & DL_OPT_ESWITCH_MODE)) {
-               pr_err("E-Switch mode option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_ESWITCH_INLINE_MODE) &&
-           !(o_found & DL_OPT_ESWITCH_INLINE_MODE)) {
-               pr_err("E-Switch inline-mode option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_DPIPE_TABLE_NAME) &&
-           !(o_found & DL_OPT_DPIPE_TABLE_NAME)) {
-               pr_err("Dpipe table name expected\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_DPIPE_TABLE_COUNTERS) &&
-           !(o_found & DL_OPT_DPIPE_TABLE_COUNTERS)) {
-               pr_err("Dpipe table counter state expected\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_ESWITCH_ENCAP_MODE) &&
-           !(o_found & DL_OPT_ESWITCH_ENCAP_MODE)) {
-               pr_err("E-Switch encapsulation option expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_PARAM_NAME) &&
-           !(o_found & DL_OPT_PARAM_NAME)) {
-               pr_err("Parameter name expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_PARAM_VALUE) &&
-           !(o_found & DL_OPT_PARAM_VALUE)) {
-               pr_err("Value to set expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_PARAM_CMODE) &&
-           !(o_found & DL_OPT_PARAM_CMODE)) {
-               pr_err("Configuration mode expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_REGION_SNAPSHOT_ID) &&
-           !(o_found & DL_OPT_REGION_SNAPSHOT_ID)) {
-               pr_err("Region snapshot id expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_REGION_ADDRESS) &&
-           !(o_found & DL_OPT_REGION_ADDRESS)) {
-               pr_err("Region address value expected.\n");
-               return -EINVAL;
-       }
-
-       if ((o_required & DL_OPT_REGION_LENGTH) &&
-           !(o_found & DL_OPT_REGION_LENGTH)) {
-               pr_err("Region length value expected.\n");
-               return -EINVAL;
-       }
-
-       return 0;
+       return dl_args_finding_required_validate(o_required, o_found);
 }
 
 static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
@@ -1382,6 +1344,12 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
        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);
 }
 
 static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl,
@@ -1443,6 +1411,8 @@ static void cmd_dev_help(void)
        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\n");
+       pr_err("       devlink dev info [ DEV ]\n");
+       pr_err("       devlink dev flash DEV file PATH [ component NAME ]\n");
 }
 
 static bool cmp_arr_last_handle(struct dl *dl, const char *bus_name,
@@ -1775,6 +1745,30 @@ static void pr_out_array_end(struct dl *dl)
        }
 }
 
+static void pr_out_object_start(struct dl *dl, const char *name)
+{
+       if (dl->json_output) {
+               jsonw_name(dl->jw, name);
+               jsonw_start_object(dl->jw);
+       } 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) {
+               jsonw_end_object(dl->jw);
+       } else {
+               __pr_out_indent_dec();
+               __pr_out_indent_dec();
+       }
+}
+
 static void pr_out_entry_start(struct dl *dl)
 {
        if (dl->json_output)
@@ -2415,6 +2409,168 @@ static int cmd_dev_reload(struct dl *dl)
        return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
 }
 
+static void pr_out_versions_single(struct dl *dl, const struct nlmsghdr *nlh,
+                                  const char *name, int type)
+{
+       struct nlattr *version;
+
+       mnl_attr_for_each(version, nlh, sizeof(struct genlmsghdr)) {
+               struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
+               const char *ver_value;
+               const char *ver_name;
+               int err;
+
+               if (mnl_attr_get_type(version) != type)
+                       continue;
+
+               err = mnl_attr_parse_nested(version, attr_cb, tb);
+               if (err != MNL_CB_OK)
+                       continue;
+
+               if (!tb[DEVLINK_ATTR_INFO_VERSION_NAME] ||
+                   !tb[DEVLINK_ATTR_INFO_VERSION_VALUE])
+                       continue;
+
+               if (name) {
+                       pr_out_object_start(dl, name);
+                       name = NULL;
+               }
+
+               ver_name = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_NAME]);
+               ver_value = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_VALUE]);
+
+               pr_out_str(dl, ver_name, ver_value);
+               if (!dl->json_output)
+                       __pr_out_newline();
+       }
+
+       if (!name)
+               pr_out_object_end(dl);
+}
+
+static void pr_out_info(struct dl *dl, const struct nlmsghdr *nlh,
+                       struct nlattr **tb, bool has_versions)
+{
+       __pr_out_handle_start(dl, tb, true, false);
+
+       __pr_out_indent_inc();
+       if (tb[DEVLINK_ATTR_INFO_DRIVER_NAME]) {
+               struct nlattr *nla_drv = tb[DEVLINK_ATTR_INFO_DRIVER_NAME];
+
+               if (!dl->json_output)
+                       __pr_out_newline();
+               pr_out_str(dl, "driver", mnl_attr_get_str(nla_drv));
+       }
+
+       if (tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER]) {
+               struct nlattr *nla_sn = tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER];
+
+               if (!dl->json_output)
+                       __pr_out_newline();
+               pr_out_str(dl, "serial_number", mnl_attr_get_str(nla_sn));
+       }
+       __pr_out_indent_dec();
+
+       if (has_versions) {
+               pr_out_object_start(dl, "versions");
+
+               pr_out_versions_single(dl, nlh, "fixed",
+                                      DEVLINK_ATTR_INFO_VERSION_FIXED);
+               pr_out_versions_single(dl, nlh, "running",
+                                      DEVLINK_ATTR_INFO_VERSION_RUNNING);
+               pr_out_versions_single(dl, nlh, "stored",
+                                      DEVLINK_ATTR_INFO_VERSION_STORED);
+
+               pr_out_object_end(dl);
+       }
+
+       pr_out_handle_end(dl);
+}
+
+static int cmd_versions_show_cb(const struct nlmsghdr *nlh, void *data)
+{
+       struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
+       struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
+       bool has_versions, has_info;
+       struct dl *dl = data;
+
+       mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
+
+       if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
+               return MNL_CB_ERROR;
+
+       has_versions = tb[DEVLINK_ATTR_INFO_VERSION_FIXED] ||
+               tb[DEVLINK_ATTR_INFO_VERSION_RUNNING] ||
+               tb[DEVLINK_ATTR_INFO_VERSION_STORED];
+       has_info = tb[DEVLINK_ATTR_INFO_DRIVER_NAME] ||
+               tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER] ||
+               has_versions;
+
+       if (has_info)
+               pr_out_info(dl, nlh, tb, has_versions);
+
+       return MNL_CB_OK;
+}
+
+static void cmd_dev_info_help(void)
+{
+       pr_err("Usage: devlink dev info [ DEV ]\n");
+}
+
+static int cmd_dev_info(struct dl *dl)
+{
+       struct nlmsghdr *nlh;
+       uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
+       int err;
+
+       if (dl_argv_match(dl, "help")) {
+               cmd_dev_info_help();
+               return 0;
+       }
+
+       if (dl_argc(dl) == 0)
+               flags |= NLM_F_DUMP;
+
+       nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_INFO_GET, flags);
+
+       if (dl_argc(dl) > 0) {
+               err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
+               if (err)
+                       return err;
+       }
+
+       pr_out_section_start(dl, "info");
+       err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_versions_show_cb, dl);
+       pr_out_section_end(dl);
+       return err;
+}
+
+static void cmd_dev_flash_help(void)
+{
+       pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
+}
+
+static int cmd_dev_flash(struct dl *dl)
+{
+       struct nlmsghdr *nlh;
+       int err;
+
+       if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
+               cmd_dev_flash_help();
+               return 0;
+       }
+
+       nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_FLASH_UPDATE,
+                              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);
+       if (err)
+               return err;
+
+       return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
+}
+
 static int cmd_dev(struct dl *dl)
 {
        if (dl_argv_match(dl, "help")) {
@@ -2433,6 +2589,12 @@ static int cmd_dev(struct dl *dl)
        } else if (dl_argv_match(dl, "param")) {
                dl_arg_inc(dl);
                return cmd_dev_param(dl);
+       } else if (dl_argv_match(dl, "info")) {
+               dl_arg_inc(dl);
+               return cmd_dev_info(dl);
+       } else if (dl_argv_match(dl, "flash")) {
+               dl_arg_inc(dl);
+               return cmd_dev_flash(dl);
        }
        pr_err("Command \"%s\" not found\n", dl_argv(dl));
        return -ENOENT;
@@ -2722,6 +2884,9 @@ static void pr_out_sb_pool(struct dl *dl, struct nlattr **tb)
                    mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE]));
        pr_out_str(dl, "thtype",
                   threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])));
+       if (tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE])
+               pr_out_uint(dl, "cell_size",
+                           mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE]));
        pr_out_handle_end(dl);
 }
 
@@ -4351,7 +4516,8 @@ static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl)
        size = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE]);
        counters_enabled = !!mnl_attr_get_u8(nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
 
-       resource_valid = !!nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID];
+       resource_valid = nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID] &&
+                        ctx->resources;
        if (resource_valid) {
                table->resource_id = mnl_attr_get_u64(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID]);
                table->resource_valid = true;
@@ -4467,12 +4633,9 @@ static int cmd_dpipe_table_show(struct dl *dl)
        dl_opts_put(nlh, dl);
        err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb,
                                  &resource_ctx);
-       if (err) {
-               pr_err("error get resources %s\n", strerror(resource_ctx.err));
-               goto err_resource_dump;
-       }
+       if (!err)
+               dpipe_ctx.resources = resource_ctx.resources;
 
-       dpipe_ctx.resources = resource_ctx.resources;
        flags = NLM_F_REQUEST | NLM_F_ACK;
        nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET, flags);
        dl_opts_put(nlh, dl);
@@ -4485,8 +4648,6 @@ static int cmd_dpipe_table_show(struct dl *dl)
        dpipe_ctx_fini(&dpipe_ctx);
        return 0;
 
-err_resource_dump:
-       resource_ctx_fini(&resource_ctx);
 err_resource_ctx_init:
 err_headers_get:
        dpipe_ctx_fini(&dpipe_ctx);