1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "netlink.h"
4 #include "common.h"
5 
6 struct rings_req_info {
7 	struct ethnl_req_info		base;
8 };
9 
10 struct rings_reply_data {
11 	struct ethnl_reply_data		base;
12 	struct ethtool_ringparam	ringparam;
13 	struct kernel_ethtool_ringparam	kernel_ringparam;
14 };
15 
16 #define RINGS_REPDATA(__reply_base) \
17 	container_of(__reply_base, struct rings_reply_data, base)
18 
19 const struct nla_policy ethnl_rings_get_policy[] = {
20 	[ETHTOOL_A_RINGS_HEADER]		=
21 		NLA_POLICY_NESTED(ethnl_header_policy),
22 };
23 
rings_prepare_data(const struct ethnl_req_info * req_base,struct ethnl_reply_data * reply_base,struct genl_info * info)24 static int rings_prepare_data(const struct ethnl_req_info *req_base,
25 			      struct ethnl_reply_data *reply_base,
26 			      struct genl_info *info)
27 {
28 	struct rings_reply_data *data = RINGS_REPDATA(reply_base);
29 	struct netlink_ext_ack *extack = info ? info->extack : NULL;
30 	struct net_device *dev = reply_base->dev;
31 	int ret;
32 
33 	if (!dev->ethtool_ops->get_ringparam)
34 		return -EOPNOTSUPP;
35 	ret = ethnl_ops_begin(dev);
36 	if (ret < 0)
37 		return ret;
38 	dev->ethtool_ops->get_ringparam(dev, &data->ringparam,
39 					&data->kernel_ringparam, extack);
40 	ethnl_ops_complete(dev);
41 
42 	return 0;
43 }
44 
rings_reply_size(const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)45 static int rings_reply_size(const struct ethnl_req_info *req_base,
46 			    const struct ethnl_reply_data *reply_base)
47 {
48 	return nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MAX */
49 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI_MAX */
50 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO_MAX */
51 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX_MAX */
52 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX */
53 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI */
54 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO */
55 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX */
56 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_BUF_LEN */
57 	       nla_total_size(sizeof(u8))  +	/* _RINGS_TCP_DATA_SPLIT */
58 	       nla_total_size(sizeof(u32)  +	/* _RINGS_CQE_SIZE */
59 	       nla_total_size(sizeof(u8)));	/* _RINGS_TX_PUSH */
60 }
61 
rings_fill_reply(struct sk_buff * skb,const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)62 static int rings_fill_reply(struct sk_buff *skb,
63 			    const struct ethnl_req_info *req_base,
64 			    const struct ethnl_reply_data *reply_base)
65 {
66 	const struct rings_reply_data *data = RINGS_REPDATA(reply_base);
67 	const struct kernel_ethtool_ringparam *kr = &data->kernel_ringparam;
68 	const struct ethtool_ringparam *ringparam = &data->ringparam;
69 
70 	WARN_ON(kr->tcp_data_split > ETHTOOL_TCP_DATA_SPLIT_ENABLED);
71 
72 	if ((ringparam->rx_max_pending &&
73 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MAX,
74 			  ringparam->rx_max_pending) ||
75 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX,
76 			  ringparam->rx_pending))) ||
77 	    (ringparam->rx_mini_max_pending &&
78 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI_MAX,
79 			  ringparam->rx_mini_max_pending) ||
80 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI,
81 			  ringparam->rx_mini_pending))) ||
82 	    (ringparam->rx_jumbo_max_pending &&
83 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO_MAX,
84 			  ringparam->rx_jumbo_max_pending) ||
85 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO,
86 			  ringparam->rx_jumbo_pending))) ||
87 	    (ringparam->tx_max_pending &&
88 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_MAX,
89 			  ringparam->tx_max_pending) ||
90 	      nla_put_u32(skb, ETHTOOL_A_RINGS_TX,
91 			  ringparam->tx_pending)))  ||
92 	    (kr->rx_buf_len &&
93 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_BUF_LEN, kr->rx_buf_len))) ||
94 	    (kr->tcp_data_split &&
95 	     (nla_put_u8(skb, ETHTOOL_A_RINGS_TCP_DATA_SPLIT,
96 			 kr->tcp_data_split))) ||
97 	    (kr->cqe_size &&
98 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_CQE_SIZE, kr->cqe_size))) ||
99 	    nla_put_u8(skb, ETHTOOL_A_RINGS_TX_PUSH, !!kr->tx_push))
100 		return -EMSGSIZE;
101 
102 	return 0;
103 }
104 
105 const struct ethnl_request_ops ethnl_rings_request_ops = {
106 	.request_cmd		= ETHTOOL_MSG_RINGS_GET,
107 	.reply_cmd		= ETHTOOL_MSG_RINGS_GET_REPLY,
108 	.hdr_attr		= ETHTOOL_A_RINGS_HEADER,
109 	.req_info_size		= sizeof(struct rings_req_info),
110 	.reply_data_size	= sizeof(struct rings_reply_data),
111 
112 	.prepare_data		= rings_prepare_data,
113 	.reply_size		= rings_reply_size,
114 	.fill_reply		= rings_fill_reply,
115 };
116 
117 /* RINGS_SET */
118 
119 const struct nla_policy ethnl_rings_set_policy[] = {
120 	[ETHTOOL_A_RINGS_HEADER]		=
121 		NLA_POLICY_NESTED(ethnl_header_policy),
122 	[ETHTOOL_A_RINGS_RX]			= { .type = NLA_U32 },
123 	[ETHTOOL_A_RINGS_RX_MINI]		= { .type = NLA_U32 },
124 	[ETHTOOL_A_RINGS_RX_JUMBO]		= { .type = NLA_U32 },
125 	[ETHTOOL_A_RINGS_TX]			= { .type = NLA_U32 },
126 	[ETHTOOL_A_RINGS_RX_BUF_LEN]            = NLA_POLICY_MIN(NLA_U32, 1),
127 	[ETHTOOL_A_RINGS_CQE_SIZE]		= NLA_POLICY_MIN(NLA_U32, 1),
128 	[ETHTOOL_A_RINGS_TX_PUSH]		= NLA_POLICY_MAX(NLA_U8, 1),
129 };
130 
ethnl_set_rings(struct sk_buff * skb,struct genl_info * info)131 int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info)
132 {
133 	struct kernel_ethtool_ringparam kernel_ringparam = {};
134 	struct ethtool_ringparam ringparam = {};
135 	struct ethnl_req_info req_info = {};
136 	struct nlattr **tb = info->attrs;
137 	const struct nlattr *err_attr;
138 	const struct ethtool_ops *ops;
139 	struct net_device *dev;
140 	bool mod = false;
141 	int ret;
142 
143 	ret = ethnl_parse_header_dev_get(&req_info,
144 					 tb[ETHTOOL_A_RINGS_HEADER],
145 					 genl_info_net(info), info->extack,
146 					 true);
147 	if (ret < 0)
148 		return ret;
149 	dev = req_info.dev;
150 	ops = dev->ethtool_ops;
151 	ret = -EOPNOTSUPP;
152 	if (!ops->get_ringparam || !ops->set_ringparam)
153 		goto out_dev;
154 
155 	if (tb[ETHTOOL_A_RINGS_RX_BUF_LEN] &&
156 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_RX_BUF_LEN)) {
157 		ret = -EOPNOTSUPP;
158 		NL_SET_ERR_MSG_ATTR(info->extack,
159 				    tb[ETHTOOL_A_RINGS_RX_BUF_LEN],
160 				    "setting rx buf len not supported");
161 		goto out_dev;
162 	}
163 
164 	if (tb[ETHTOOL_A_RINGS_CQE_SIZE] &&
165 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_CQE_SIZE)) {
166 		ret = -EOPNOTSUPP;
167 		NL_SET_ERR_MSG_ATTR(info->extack,
168 				    tb[ETHTOOL_A_RINGS_CQE_SIZE],
169 				    "setting cqe size not supported");
170 		goto out_dev;
171 	}
172 
173 	if (tb[ETHTOOL_A_RINGS_TX_PUSH] &&
174 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_TX_PUSH)) {
175 		ret = -EOPNOTSUPP;
176 		NL_SET_ERR_MSG_ATTR(info->extack,
177 				    tb[ETHTOOL_A_RINGS_TX_PUSH],
178 				    "setting tx push not supported");
179 		goto out_dev;
180 	}
181 
182 	rtnl_lock();
183 	ret = ethnl_ops_begin(dev);
184 	if (ret < 0)
185 		goto out_rtnl;
186 	ops->get_ringparam(dev, &ringparam, &kernel_ringparam, info->extack);
187 
188 	ethnl_update_u32(&ringparam.rx_pending, tb[ETHTOOL_A_RINGS_RX], &mod);
189 	ethnl_update_u32(&ringparam.rx_mini_pending,
190 			 tb[ETHTOOL_A_RINGS_RX_MINI], &mod);
191 	ethnl_update_u32(&ringparam.rx_jumbo_pending,
192 			 tb[ETHTOOL_A_RINGS_RX_JUMBO], &mod);
193 	ethnl_update_u32(&ringparam.tx_pending, tb[ETHTOOL_A_RINGS_TX], &mod);
194 	ethnl_update_u32(&kernel_ringparam.rx_buf_len,
195 			 tb[ETHTOOL_A_RINGS_RX_BUF_LEN], &mod);
196 	ethnl_update_u32(&kernel_ringparam.cqe_size,
197 			 tb[ETHTOOL_A_RINGS_CQE_SIZE], &mod);
198 	ethnl_update_u8(&kernel_ringparam.tx_push,
199 			tb[ETHTOOL_A_RINGS_TX_PUSH], &mod);
200 	ret = 0;
201 	if (!mod)
202 		goto out_ops;
203 
204 	/* ensure new ring parameters are within limits */
205 	if (ringparam.rx_pending > ringparam.rx_max_pending)
206 		err_attr = tb[ETHTOOL_A_RINGS_RX];
207 	else if (ringparam.rx_mini_pending > ringparam.rx_mini_max_pending)
208 		err_attr = tb[ETHTOOL_A_RINGS_RX_MINI];
209 	else if (ringparam.rx_jumbo_pending > ringparam.rx_jumbo_max_pending)
210 		err_attr = tb[ETHTOOL_A_RINGS_RX_JUMBO];
211 	else if (ringparam.tx_pending > ringparam.tx_max_pending)
212 		err_attr = tb[ETHTOOL_A_RINGS_TX];
213 	else
214 		err_attr = NULL;
215 	if (err_attr) {
216 		ret = -EINVAL;
217 		NL_SET_ERR_MSG_ATTR(info->extack, err_attr,
218 				    "requested ring size exceeds maximum");
219 		goto out_ops;
220 	}
221 
222 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
223 					      &kernel_ringparam, info->extack);
224 	if (ret < 0)
225 		goto out_ops;
226 	ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
227 
228 out_ops:
229 	ethnl_ops_complete(dev);
230 out_rtnl:
231 	rtnl_unlock();
232 out_dev:
233 	ethnl_parse_header_dev_put(&req_info);
234 	return ret;
235 }
236