iproute2/rdma/sys.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
   2/*
   3 * sys.c        RDMA tool
   4 */
   5
   6#include "rdma.h"
   7
   8static int sys_help(struct rd *rd)
   9{
  10        pr_out("Usage: %s system show [ netns ]\n", rd->filename);
  11        pr_out("       %s system set netns { shared | exclusive }\n", rd->filename);
  12        return 0;
  13}
  14
  15static const char *netns_modes_str[] = {
  16        "exclusive",
  17        "shared",
  18};
  19
  20static int sys_show_parse_cb(const struct nlmsghdr *nlh, void *data)
  21{
  22        struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
  23        bool cof = false;
  24
  25        mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
  26
  27        if (tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]) {
  28                const char *mode_str;
  29                uint8_t netns_mode;
  30
  31                netns_mode =
  32                        mnl_attr_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
  33
  34                if (netns_mode < ARRAY_SIZE(netns_modes_str))
  35                        mode_str = netns_modes_str[netns_mode];
  36                else
  37                        mode_str = "unknown";
  38
  39                print_color_string(PRINT_ANY, COLOR_NONE, "netns", "netns %s ",
  40                                   mode_str);
  41        }
  42
  43        if (tb[RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK])
  44                cof = mnl_attr_get_u8(tb[RDMA_NLDEV_SYS_ATTR_COPY_ON_FORK]);
  45
  46        print_color_on_off(PRINT_ANY, COLOR_NONE, "copy-on-fork",
  47                           "copy-on-fork %s\n",
  48                           cof);
  49
  50        return MNL_CB_OK;
  51}
  52
  53static int sys_show_no_args(struct rd *rd)
  54{
  55        uint32_t seq;
  56        int ret;
  57
  58        rd_prepare_msg(rd, RDMA_NLDEV_CMD_SYS_GET,
  59                       &seq, (NLM_F_REQUEST | NLM_F_ACK));
  60        ret = rd_send_msg(rd);
  61        if (ret)
  62                return ret;
  63
  64        return rd_recv_msg(rd, sys_show_parse_cb, rd, seq);
  65}
  66
  67static int sys_show(struct rd *rd)
  68{
  69        const struct rd_cmd cmds[] = {
  70                { NULL,         sys_show_no_args},
  71                { "netns",      sys_show_no_args},
  72                { 0 }
  73        };
  74
  75        return rd_exec_cmd(rd, cmds, "parameter");
  76}
  77
  78static int sys_set_netns_cmd(struct rd *rd, bool enable)
  79{
  80        uint32_t seq;
  81
  82        rd_prepare_msg(rd, RDMA_NLDEV_CMD_SYS_SET,
  83                       &seq, (NLM_F_REQUEST | NLM_F_ACK));
  84        mnl_attr_put_u8(rd->nlh, RDMA_NLDEV_SYS_ATTR_NETNS_MODE, enable);
  85
  86        return rd_sendrecv_msg(rd, seq);
  87}
  88
  89static bool sys_valid_netns_cmd(const char *cmd)
  90{
  91        int i;
  92
  93        for (i = 0; i < ARRAY_SIZE(netns_modes_str); i++) {
  94                if (!strcmp(cmd, netns_modes_str[i]))
  95                        return true;
  96        }
  97        return false;
  98}
  99
 100static int sys_set_netns_args(struct rd *rd)
 101{
 102        bool cmd;
 103
 104        if (rd_no_arg(rd) || !sys_valid_netns_cmd(rd_argv(rd))) {
 105                pr_err("valid options are: { shared | exclusive }\n");
 106                return -EINVAL;
 107        }
 108
 109        cmd = (strcmp(rd_argv(rd), "shared") == 0) ? true : false;
 110
 111        return sys_set_netns_cmd(rd, cmd);
 112}
 113
 114static int sys_set_help(struct rd *rd)
 115{
 116        pr_out("Usage: %s system set [PARAM] value\n", rd->filename);
 117        pr_out("            system set netns { shared | exclusive }\n");
 118        return 0;
 119}
 120
 121static int sys_set(struct rd *rd)
 122{
 123        const struct rd_cmd cmds[] = {
 124                { NULL,                 sys_set_help },
 125                { "help",               sys_set_help },
 126                { "netns",              sys_set_netns_args},
 127                { 0 }
 128        };
 129
 130        return rd_exec_cmd(rd, cmds, "parameter");
 131}
 132
 133int cmd_sys(struct rd *rd)
 134{
 135        const struct rd_cmd cmds[] = {
 136                { NULL,         sys_show },
 137                { "show",       sys_show },
 138                { "set",        sys_set },
 139                { "help",       sys_help },
 140                { 0 }
 141        };
 142
 143        return rd_exec_cmd(rd, cmds, "system command");
 144}
 145