xref: /openbmc/linux/net/ethtool/rss.c (revision 3ddc8b84)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "netlink.h"
4 #include "common.h"
5 
6 struct rss_req_info {
7 	struct ethnl_req_info		base;
8 	u32				rss_context;
9 };
10 
11 struct rss_reply_data {
12 	struct ethnl_reply_data		base;
13 	u32				indir_size;
14 	u32				hkey_size;
15 	u32				hfunc;
16 	u32				*indir_table;
17 	u8				*hkey;
18 };
19 
20 #define RSS_REQINFO(__req_base) \
21 	container_of(__req_base, struct rss_req_info, base)
22 
23 #define RSS_REPDATA(__reply_base) \
24 	container_of(__reply_base, struct rss_reply_data, base)
25 
26 const struct nla_policy ethnl_rss_get_policy[] = {
27 	[ETHTOOL_A_RSS_HEADER] = NLA_POLICY_NESTED(ethnl_header_policy),
28 	[ETHTOOL_A_RSS_CONTEXT] = { .type = NLA_U32 },
29 };
30 
31 static int
32 rss_parse_request(struct ethnl_req_info *req_info, struct nlattr **tb,
33 		  struct netlink_ext_ack *extack)
34 {
35 	struct rss_req_info *request = RSS_REQINFO(req_info);
36 
37 	if (tb[ETHTOOL_A_RSS_CONTEXT])
38 		request->rss_context = nla_get_u32(tb[ETHTOOL_A_RSS_CONTEXT]);
39 
40 	return 0;
41 }
42 
43 static int
44 rss_prepare_data(const struct ethnl_req_info *req_base,
45 		 struct ethnl_reply_data *reply_base,
46 		 const struct genl_info *info)
47 {
48 	struct rss_reply_data *data = RSS_REPDATA(reply_base);
49 	struct rss_req_info *request = RSS_REQINFO(req_base);
50 	struct net_device *dev = reply_base->dev;
51 	const struct ethtool_ops *ops;
52 	u32 total_size, indir_bytes;
53 	u8 dev_hfunc = 0;
54 	u8 *rss_config;
55 	int ret;
56 
57 	ops = dev->ethtool_ops;
58 	if (!ops->get_rxfh)
59 		return -EOPNOTSUPP;
60 
61 	/* Some drivers don't handle rss_context */
62 	if (request->rss_context && !ops->get_rxfh_context)
63 		return -EOPNOTSUPP;
64 
65 	ret = ethnl_ops_begin(dev);
66 	if (ret < 0)
67 		return ret;
68 
69 	data->indir_size = 0;
70 	data->hkey_size = 0;
71 	if (ops->get_rxfh_indir_size)
72 		data->indir_size = ops->get_rxfh_indir_size(dev);
73 	if (ops->get_rxfh_key_size)
74 		data->hkey_size = ops->get_rxfh_key_size(dev);
75 
76 	indir_bytes = data->indir_size * sizeof(u32);
77 	total_size = indir_bytes + data->hkey_size;
78 	rss_config = kzalloc(total_size, GFP_KERNEL);
79 	if (!rss_config) {
80 		ret = -ENOMEM;
81 		goto out_ops;
82 	}
83 
84 	if (data->indir_size)
85 		data->indir_table = (u32 *)rss_config;
86 
87 	if (data->hkey_size)
88 		data->hkey = rss_config + indir_bytes;
89 
90 	if (request->rss_context)
91 		ret = ops->get_rxfh_context(dev, data->indir_table, data->hkey,
92 					    &dev_hfunc, request->rss_context);
93 	else
94 		ret = ops->get_rxfh(dev, data->indir_table, data->hkey,
95 				    &dev_hfunc);
96 
97 	if (ret)
98 		goto out_ops;
99 
100 	data->hfunc = dev_hfunc;
101 out_ops:
102 	ethnl_ops_complete(dev);
103 	return ret;
104 }
105 
106 static int
107 rss_reply_size(const struct ethnl_req_info *req_base,
108 	       const struct ethnl_reply_data *reply_base)
109 {
110 	const struct rss_reply_data *data = RSS_REPDATA(reply_base);
111 	int len;
112 
113 	len = nla_total_size(sizeof(u32)) +	/* _RSS_HFUNC */
114 	      nla_total_size(sizeof(u32) * data->indir_size) + /* _RSS_INDIR */
115 	      nla_total_size(data->hkey_size);	/* _RSS_HKEY */
116 
117 	return len;
118 }
119 
120 static int
121 rss_fill_reply(struct sk_buff *skb, const struct ethnl_req_info *req_base,
122 	       const struct ethnl_reply_data *reply_base)
123 {
124 	const struct rss_reply_data *data = RSS_REPDATA(reply_base);
125 
126 	if ((data->hfunc &&
127 	     nla_put_u32(skb, ETHTOOL_A_RSS_HFUNC, data->hfunc)) ||
128 	    (data->indir_size &&
129 	     nla_put(skb, ETHTOOL_A_RSS_INDIR,
130 		     sizeof(u32) * data->indir_size, data->indir_table)) ||
131 	    (data->hkey_size &&
132 	     nla_put(skb, ETHTOOL_A_RSS_HKEY, data->hkey_size, data->hkey)))
133 		return -EMSGSIZE;
134 
135 	return 0;
136 }
137 
138 static void rss_cleanup_data(struct ethnl_reply_data *reply_base)
139 {
140 	const struct rss_reply_data *data = RSS_REPDATA(reply_base);
141 
142 	kfree(data->indir_table);
143 }
144 
145 const struct ethnl_request_ops ethnl_rss_request_ops = {
146 	.request_cmd		= ETHTOOL_MSG_RSS_GET,
147 	.reply_cmd		= ETHTOOL_MSG_RSS_GET_REPLY,
148 	.hdr_attr		= ETHTOOL_A_RSS_HEADER,
149 	.req_info_size		= sizeof(struct rss_req_info),
150 	.reply_data_size	= sizeof(struct rss_reply_data),
151 
152 	.parse_request		= rss_parse_request,
153 	.prepare_data		= rss_prepare_data,
154 	.reply_size		= rss_reply_size,
155 	.fill_reply		= rss_fill_reply,
156 	.cleanup_data		= rss_cleanup_data,
157 };
158