]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - rdma/utils.c
rdma: Perform single .doit call to query specific objects
[mirror_iproute2.git] / rdma / utils.c
index c7023367001eff091050b4bbef1dc5ba63d75689..cff5297db93805ec7880c1e3c29684b88f9c9958 100644 (file)
@@ -1,11 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
 /*
  * utils.c     RDMA 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:     Leon Romanovsky <leonro@mellanox.com>
  */
 
@@ -18,14 +13,14 @@ int rd_argc(struct rd *rd)
        return rd->argc;
 }
 
-static char *rd_argv(struct rd *rd)
+char *rd_argv(struct rd *rd)
 {
        if (!rd_argc(rd))
                return NULL;
        return *rd->argv;
 }
 
-static int strcmpx(const char *str1, const char *str2)
+int strcmpx(const char *str1, const char *str2)
 {
        if (strlen(str1) > strlen(str2))
                return -1;
@@ -39,7 +34,7 @@ static bool rd_argv_match(struct rd *rd, const char *pattern)
        return strcmpx(rd_argv(rd), pattern) == 0;
 }
 
-static void rd_arg_inc(struct rd *rd)
+void rd_arg_inc(struct rd *rd)
 {
        if (!rd_argc(rd))
                return;
@@ -47,7 +42,7 @@ static void rd_arg_inc(struct rd *rd)
        rd->argv++;
 }
 
-static bool rd_no_arg(struct rd *rd)
+bool rd_no_arg(struct rd *rd)
 {
        return rd_argc(rd) == 0;
 }
@@ -126,6 +121,7 @@ static int add_filter(struct rd *rd, char *key, char *value,
        struct filter_entry *fe;
        bool key_found = false;
        int idx = 0;
+       char *endp;
        int ret;
 
        fe = calloc(1, sizeof(*fe));
@@ -168,6 +164,11 @@ static int add_filter(struct rd *rd, char *key, char *value,
                goto err_alloc;
        }
 
+       errno = 0;
+       strtol(fe->value, &endp, 10);
+       if (valid_filters[idx].is_doit && !errno && *endp == '\0')
+               fe->is_doit = true;
+
        for (idx = 0; idx < strlen(fe->value); idx++)
                fe->value[idx] = tolower(fe->value[idx]);
 
@@ -182,6 +183,20 @@ err:
        return ret;
 }
 
+bool rd_doit_index(struct rd *rd, uint32_t *idx)
+{
+       struct filter_entry *fe;
+
+       list_for_each_entry(fe, &rd->filter_list, list) {
+               if (fe->is_doit) {
+                       *idx = atoi(fe->value);
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 int rd_build_filter(struct rd *rd, const struct filters valid_filters[])
 {
        int ret = 0;
@@ -404,7 +419,7 @@ static const enum mnl_attr_data_type nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
        [RDMA_NLDEV_ATTR_DRIVER_U64] = MNL_TYPE_U64,
 };
 
-static int rd_attr_check(const struct nlattr *attr, int *typep)
+int rd_attr_check(const struct nlattr *attr, int *typep)
 {
        int type;
 
@@ -577,6 +592,16 @@ out:
        return ret;
 }
 
+int rd_exec_require_dev(struct rd *rd, int (*cb)(struct rd *rd))
+{
+       if (rd_no_arg(rd)) {
+               pr_err("Please provide device name.\n");
+               return -EINVAL;
+       }
+
+       return rd_exec_dev(rd, cb);
+}
+
 int rd_exec_cmd(struct rd *rd, const struct rd_cmd *cmds, const char *str)
 {
        const struct rd_cmd *c;
@@ -696,7 +721,7 @@ void newline(struct rd *rd)
                pr_out("\n");
 }
 
-static void newline_indent(struct rd *rd)
+void newline_indent(struct rd *rd)
 {
        newline(rd);
        if (!rd->json_output)