1 /*
2 * rss.c - netlink implementation of RSS context commands
3 *
4 * Implementation of "ethtool -x <dev>"
5 */
6
7 #include <errno.h>
8 #include <string.h>
9 #include <stdio.h>
10
11 #include "../internal.h"
12 #include "../common.h"
13 #include "netlink.h"
14 #include "strset.h"
15 #include "parser.h"
16
17 struct cb_args {
18 struct nl_context *nlctx;
19 u32 num_rings;
20 };
21
dump_json_rss_info(struct cmd_context * ctx,u32 * indir_table,u32 indir_size,u8 * hkey,u32 hkey_size,const struct stringset * hash_funcs,u8 hfunc,u32 input_xfrm)22 void dump_json_rss_info(struct cmd_context *ctx, u32 *indir_table,
23 u32 indir_size, u8 *hkey, u32 hkey_size,
24 const struct stringset *hash_funcs, u8 hfunc,
25 u32 input_xfrm)
26 {
27 unsigned int i;
28
29 open_json_object(NULL);
30 print_string(PRINT_JSON, "ifname", NULL, ctx->devname);
31 if (indir_size) {
32 open_json_array("rss-indirection-table", NULL);
33 for (i = 0; i < indir_size; i++)
34 print_uint(PRINT_JSON, NULL, NULL, indir_table[i]);
35 close_json_array("\n");
36 }
37
38 if (hkey_size) {
39 open_json_array("rss-hash-key", NULL);
40 for (i = 0; i < hkey_size; i++)
41 print_uint(PRINT_JSON, NULL, NULL, (u8)hkey[i]);
42 close_json_array("\n");
43 }
44
45 if (hfunc) {
46 for (i = 0; i < get_count(hash_funcs); i++) {
47 if (hfunc & (1 << i)) {
48 print_string(PRINT_JSON, "rss-hash-function",
49 NULL, get_string(hash_funcs, i));
50 open_json_object("rss-input-transformation");
51 print_bool(PRINT_JSON, "symmetric-xor", NULL,
52 (input_xfrm & RXH_XFRM_SYM_XOR) ?
53 true : false);
54
55 close_json_object();
56 break;
57 }
58 }
59 }
60
61 close_json_object();
62 }
63
64 /* There is no netlink equivalent for ETHTOOL_GRXRINGS. */
get_num_rings(struct cb_args * args)65 static int get_num_rings(struct cb_args *args)
66 {
67 struct nl_context *nlctx = args->nlctx;
68 struct cmd_context *ctx = nlctx->ctx;
69 struct ethtool_rxnfc ring_count = {
70 .cmd = ETHTOOL_GRXRINGS,
71 };
72 int ret;
73
74 ret = ioctl_init(ctx, false);
75 if (ret)
76 return ret;
77
78 ret = send_ioctl(ctx, &ring_count);
79 if (ret) {
80 perror("Cannot get RX ring count");
81 return ret;
82 }
83
84 args->num_rings = (u32)ring_count.data;
85
86 return 0;
87 }
88
rss_reply_cb(const struct nlmsghdr * nlhdr,void * data)89 int rss_reply_cb(const struct nlmsghdr *nlhdr, void *data)
90 {
91 const struct nlattr *tb[ETHTOOL_A_RSS_MAX + 1] = {};
92 unsigned int indir_bytes = 0, hkey_bytes = 0;
93 DECLARE_ATTR_TB_INFO(tb);
94 struct cb_args *args = data;
95 struct nl_context *nlctx = args->nlctx;
96 const struct stringset *hash_funcs;
97 u32 rss_hfunc = 0, indir_size;
98 u32 *indir_table = NULL;
99 u32 input_xfrm = 0;
100 u8 *hkey = NULL;
101 bool silent;
102 int err_ret;
103 int ret;
104
105 silent = nlctx->is_dump || nlctx->is_monitor;
106 err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
107 ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
108 if (ret < 0)
109 return err_ret;
110 nlctx->devname = get_dev_name(tb[ETHTOOL_A_RSS_HEADER]);
111 if (!dev_ok(nlctx))
112 return err_ret;
113
114 show_cr();
115
116 if (tb[ETHTOOL_A_RSS_HFUNC])
117 rss_hfunc = mnl_attr_get_u32(tb[ETHTOOL_A_RSS_HFUNC]);
118
119 if (tb[ETHTOOL_A_RSS_INDIR]) {
120 indir_bytes = mnl_attr_get_payload_len(tb[ETHTOOL_A_RSS_INDIR]);
121 indir_table = mnl_attr_get_payload(tb[ETHTOOL_A_RSS_INDIR]);
122 }
123
124 if (tb[ETHTOOL_A_RSS_HKEY]) {
125 hkey_bytes = mnl_attr_get_payload_len(tb[ETHTOOL_A_RSS_HKEY]);
126 hkey = mnl_attr_get_payload(tb[ETHTOOL_A_RSS_HKEY]);
127 }
128
129 if (tb[ETHTOOL_A_RSS_INPUT_XFRM])
130 input_xfrm = mnl_attr_get_u32(tb[ETHTOOL_A_RSS_INPUT_XFRM]);
131
132 /* Fetch RSS hash functions and their status and print */
133 if (!nlctx->is_monitor) {
134 ret = netlink_init_ethnl2_socket(nlctx);
135 if (ret < 0)
136 return MNL_CB_ERROR;
137 }
138 hash_funcs = global_stringset(ETH_SS_RSS_HASH_FUNCS,
139 nlctx->ethnl2_socket);
140
141 ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
142 if (ret < 0)
143 return silent ? MNL_CB_OK : MNL_CB_ERROR;
144
145 ret = get_num_rings(args);
146 if (ret < 0)
147 return MNL_CB_ERROR;
148
149 indir_size = indir_bytes / sizeof(u32);
150 if (is_json_context()) {
151 dump_json_rss_info(nlctx->ctx, (u32 *)indir_table, indir_size,
152 hkey, hkey_bytes, hash_funcs, rss_hfunc,
153 input_xfrm);
154 } else {
155 print_indir_table(nlctx->ctx, args->num_rings,
156 indir_size, (u32 *)indir_table);
157 print_rss_hkey(hkey, hkey_bytes);
158 printf("RSS hash function:\n");
159 if (!rss_hfunc) {
160 printf(" Operation not supported\n");
161 return 0;
162 }
163 for (unsigned int i = 0; i < get_count(hash_funcs); i++) {
164 printf(" %s: %s\n", get_string(hash_funcs, i),
165 (rss_hfunc & (1 << i)) ? "on" : "off");
166 }
167 printf("RSS input transformation:\n");
168 printf(" symmetric-xor: %s\n",
169 (input_xfrm & RXH_XFRM_SYM_XOR) ? "on" : "off");
170 }
171
172 return MNL_CB_OK;
173 }
174
175 /* RSS_GET */
176 static const struct param_parser grss_params[] = {
177 {
178 .arg = "context",
179 .type = ETHTOOL_A_RSS_CONTEXT,
180 .handler = nl_parse_direct_u32,
181 .min_argc = 1,
182 },
183 {}
184 };
185
nl_grss(struct cmd_context * ctx)186 int nl_grss(struct cmd_context *ctx)
187 {
188 struct nl_context *nlctx = ctx->nlctx;
189 struct nl_socket *nlsk = nlctx->ethnl_socket;
190 struct nl_msg_buff *msgbuff;
191 struct cb_args args = {};
192 int ret;
193
194 nlctx->cmd = "-x";
195 nlctx->argp = ctx->argp;
196 nlctx->argc = ctx->argc;
197 nlctx->devname = ctx->devname;
198 nlsk = nlctx->ethnl_socket;
199 msgbuff = &nlsk->msgbuff;
200
201 if (netlink_cmd_check(ctx, ETHTOOL_MSG_RSS_GET, true))
202 return -EOPNOTSUPP;
203
204 ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_RSS_GET,
205 NLM_F_REQUEST | NLM_F_ACK);
206 if (ret < 0)
207 return 1;
208
209 if (ethnla_fill_header(msgbuff, ETHTOOL_A_RSS_HEADER,
210 ctx->devname, 0))
211 return -EMSGSIZE;
212
213 ret = nl_parser(nlctx, grss_params, NULL, PARSER_GROUP_NONE, NULL);
214 if (ret < 0)
215 goto err;
216
217 ret = nlsock_sendmsg(nlsk, NULL);
218 if (ret < 0)
219 goto err;
220
221 args.nlctx = nlctx;
222 new_json_obj(ctx->json);
223 ret = nlsock_process_reply(nlsk, rss_reply_cb, &args);
224 delete_json_obj();
225
226 if (ret == 0)
227 return 0;
228 err:
229 return nlctx->exit_code ?: 1;
230 }
231