Skip to content

Commit e19c591

Browse files
mkubecekdavem330
authored andcommitted
ethtool: set device channel counts with CHANNELS_SET request
Implement CHANNELS_SET netlink request to set channel counts of a network device. These are traditionally set with ETHTOOL_SCHANNELS ioctl request. Like the ioctl implementation, the generic ethtool code checks if supplied values do not exceed driver defined limits; if they do, first offending attribute is reported using extack. Checks preventing removing channels used for RX indirection table or zerocopy AF_XDP socket are also implemented. Move ethtool_get_max_rxfh_channel() helper into common.c so that it can be used by both ioctl and netlink code. v2: - fix netdev reference leak in error path (found by Jakub Kicinsky) Signed-off-by: Michal Kubecek <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 0c84979 commit e19c591

File tree

8 files changed

+177
-32
lines changed

8 files changed

+177
-32
lines changed

Documentation/networking/ethtool-netlink.rst

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Userspace to kernel:
196196
``ETHTOOL_MSG_RINGS_GET`` get ring sizes
197197
``ETHTOOL_MSG_RINGS_SET`` set ring sizes
198198
``ETHTOOL_MSG_CHANNELS_GET`` get channel counts
199+
``ETHTOOL_MSG_CHANNELS_SET`` set channel counts
199200
===================================== ================================
200201

201202
Kernel to userspace:
@@ -723,6 +724,26 @@ Kernel response contents:
723724
===================================== ====== ==========================
724725

725726

727+
CHANNELS_SET
728+
============
729+
730+
Sets channel counts like ``ETHTOOL_SCHANNELS`` ioctl request.
731+
732+
Request contents:
733+
734+
===================================== ====== ==========================
735+
``ETHTOOL_A_CHANNELS_HEADER`` nested request header
736+
``ETHTOOL_A_CHANNELS_RX_COUNT`` u32 receive channel count
737+
``ETHTOOL_A_CHANNELS_TX_COUNT`` u32 transmit channel count
738+
``ETHTOOL_A_CHANNELS_OTHER_COUNT`` u32 other channel count
739+
``ETHTOOL_A_CHANNELS_COMBINED_COUNT`` u32 combined channel count
740+
===================================== ====== ==========================
741+
742+
Kernel checks that requested channel counts do not exceed limits reported by
743+
driver. Driver may impose additional constraints and may not suspport all
744+
attributes.
745+
746+
726747
Request translation
727748
===================
728749

@@ -794,7 +815,7 @@ have their netlink replacement yet.
794815
``ETHTOOL_GFEATURES`` ``ETHTOOL_MSG_FEATURES_GET``
795816
``ETHTOOL_SFEATURES`` ``ETHTOOL_MSG_FEATURES_SET``
796817
``ETHTOOL_GCHANNELS`` ``ETHTOOL_MSG_CHANNELS_GET``
797-
``ETHTOOL_SCHANNELS`` n/a
818+
``ETHTOOL_SCHANNELS`` ``ETHTOOL_MSG_CHANNELS_SET``
798819
``ETHTOOL_SET_DUMP`` n/a
799820
``ETHTOOL_GET_DUMP_FLAG`` n/a
800821
``ETHTOOL_GET_DUMP_DATA`` n/a

include/uapi/linux/ethtool_netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum {
3131
ETHTOOL_MSG_RINGS_GET,
3232
ETHTOOL_MSG_RINGS_SET,
3333
ETHTOOL_MSG_CHANNELS_GET,
34+
ETHTOOL_MSG_CHANNELS_SET,
3435

3536
/* add new constants above here */
3637
__ETHTOOL_MSG_USER_CNT,

net/ethtool/channels.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

3+
#include <net/xdp_sock.h>
4+
35
#include "netlink.h"
46
#include "common.h"
57

@@ -106,3 +108,117 @@ const struct ethnl_request_ops ethnl_channels_request_ops = {
106108
.reply_size = channels_reply_size,
107109
.fill_reply = channels_fill_reply,
108110
};
111+
112+
/* CHANNELS_SET */
113+
114+
static const struct nla_policy
115+
channels_set_policy[ETHTOOL_A_CHANNELS_MAX + 1] = {
116+
[ETHTOOL_A_CHANNELS_UNSPEC] = { .type = NLA_REJECT },
117+
[ETHTOOL_A_CHANNELS_HEADER] = { .type = NLA_NESTED },
118+
[ETHTOOL_A_CHANNELS_RX_MAX] = { .type = NLA_REJECT },
119+
[ETHTOOL_A_CHANNELS_TX_MAX] = { .type = NLA_REJECT },
120+
[ETHTOOL_A_CHANNELS_OTHER_MAX] = { .type = NLA_REJECT },
121+
[ETHTOOL_A_CHANNELS_COMBINED_MAX] = { .type = NLA_REJECT },
122+
[ETHTOOL_A_CHANNELS_RX_COUNT] = { .type = NLA_U32 },
123+
[ETHTOOL_A_CHANNELS_TX_COUNT] = { .type = NLA_U32 },
124+
[ETHTOOL_A_CHANNELS_OTHER_COUNT] = { .type = NLA_U32 },
125+
[ETHTOOL_A_CHANNELS_COMBINED_COUNT] = { .type = NLA_U32 },
126+
};
127+
128+
int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info)
129+
{
130+
struct nlattr *tb[ETHTOOL_A_CHANNELS_MAX + 1];
131+
unsigned int from_channel, old_total, i;
132+
struct ethtool_channels channels = {};
133+
struct ethnl_req_info req_info = {};
134+
const struct nlattr *err_attr;
135+
const struct ethtool_ops *ops;
136+
struct net_device *dev;
137+
u32 max_rx_in_use = 0;
138+
bool mod = false;
139+
int ret;
140+
141+
ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb,
142+
ETHTOOL_A_CHANNELS_MAX, channels_set_policy,
143+
info->extack);
144+
if (ret < 0)
145+
return ret;
146+
ret = ethnl_parse_header_dev_get(&req_info,
147+
tb[ETHTOOL_A_CHANNELS_HEADER],
148+
genl_info_net(info), info->extack,
149+
true);
150+
if (ret < 0)
151+
return ret;
152+
dev = req_info.dev;
153+
ops = dev->ethtool_ops;
154+
ret = -EOPNOTSUPP;
155+
if (!ops->get_channels || !ops->set_channels)
156+
goto out_dev;
157+
158+
rtnl_lock();
159+
ret = ethnl_ops_begin(dev);
160+
if (ret < 0)
161+
goto out_rtnl;
162+
ops->get_channels(dev, &channels);
163+
old_total = channels.combined_count +
164+
max(channels.rx_count, channels.tx_count);
165+
166+
ethnl_update_u32(&channels.rx_count, tb[ETHTOOL_A_CHANNELS_RX_COUNT],
167+
&mod);
168+
ethnl_update_u32(&channels.tx_count, tb[ETHTOOL_A_CHANNELS_TX_COUNT],
169+
&mod);
170+
ethnl_update_u32(&channels.other_count,
171+
tb[ETHTOOL_A_CHANNELS_OTHER_COUNT], &mod);
172+
ethnl_update_u32(&channels.combined_count,
173+
tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT], &mod);
174+
ret = 0;
175+
if (!mod)
176+
goto out_ops;
177+
178+
/* ensure new channel counts are within limits */
179+
if (channels.rx_count > channels.max_rx)
180+
err_attr = tb[ETHTOOL_A_CHANNELS_RX_COUNT];
181+
else if (channels.tx_count > channels.max_tx)
182+
err_attr = tb[ETHTOOL_A_CHANNELS_TX_COUNT];
183+
else if (channels.other_count > channels.max_other)
184+
err_attr = tb[ETHTOOL_A_CHANNELS_OTHER_COUNT];
185+
else if (channels.combined_count > channels.max_combined)
186+
err_attr = tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT];
187+
else
188+
err_attr = NULL;
189+
if (err_attr) {
190+
ret = -EINVAL;
191+
NL_SET_ERR_MSG_ATTR(info->extack, err_attr,
192+
"requested channel count exceeeds maximum");
193+
goto out_ops;
194+
}
195+
196+
/* ensure the new Rx count fits within the configured Rx flow
197+
* indirection table settings
198+
*/
199+
if (netif_is_rxfh_configured(dev) &&
200+
!ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
201+
(channels.combined_count + channels.rx_count) <= max_rx_in_use) {
202+
GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing indirection table settings");
203+
return -EINVAL;
204+
}
205+
206+
/* Disabling channels, query zero-copy AF_XDP sockets */
207+
from_channel = channels.combined_count +
208+
min(channels.rx_count, channels.tx_count);
209+
for (i = from_channel; i < old_total; i++)
210+
if (xdp_get_umem_from_qid(dev, i)) {
211+
GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing zerocopy AF_XDP sockets");
212+
return -EINVAL;
213+
}
214+
215+
ret = dev->ethtool_ops->set_channels(dev, &channels);
216+
217+
out_ops:
218+
ethnl_ops_complete(dev);
219+
out_rtnl:
220+
rtnl_unlock();
221+
out_dev:
222+
dev_put(dev);
223+
return ret;
224+
}

net/ethtool/common.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,34 @@ int __ethtool_get_link(struct net_device *dev)
258258

259259
return netif_running(dev) && dev->ethtool_ops->get_link(dev);
260260
}
261+
262+
int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
263+
{
264+
u32 dev_size, current_max = 0;
265+
u32 *indir;
266+
int ret;
267+
268+
if (!dev->ethtool_ops->get_rxfh_indir_size ||
269+
!dev->ethtool_ops->get_rxfh)
270+
return -EOPNOTSUPP;
271+
dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
272+
if (dev_size == 0)
273+
return -EOPNOTSUPP;
274+
275+
indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
276+
if (!indir)
277+
return -ENOMEM;
278+
279+
ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
280+
if (ret)
281+
goto out;
282+
283+
while (dev_size--)
284+
current_max = max(current_max, indir[dev_size]);
285+
286+
*max = current_max;
287+
288+
out:
289+
kfree(indir);
290+
return ret;
291+
}

net/ethtool/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ int __ethtool_get_link(struct net_device *dev);
2929
bool convert_legacy_settings_to_link_ksettings(
3030
struct ethtool_link_ksettings *link_ksettings,
3131
const struct ethtool_cmd *legacy_settings);
32+
int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max);
3233

3334
#endif /* _ETHTOOL_COMMON_H */

net/ethtool/ioctl.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -929,37 +929,6 @@ void netdev_rss_key_fill(void *buffer, size_t len)
929929
}
930930
EXPORT_SYMBOL(netdev_rss_key_fill);
931931

932-
static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
933-
{
934-
u32 dev_size, current_max = 0;
935-
u32 *indir;
936-
int ret;
937-
938-
if (!dev->ethtool_ops->get_rxfh_indir_size ||
939-
!dev->ethtool_ops->get_rxfh)
940-
return -EOPNOTSUPP;
941-
dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
942-
if (dev_size == 0)
943-
return -EOPNOTSUPP;
944-
945-
indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
946-
if (!indir)
947-
return -ENOMEM;
948-
949-
ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
950-
if (ret)
951-
goto out;
952-
953-
while (dev_size--)
954-
current_max = max(current_max, indir[dev_size]);
955-
956-
*max = current_max;
957-
958-
out:
959-
kfree(indir);
960-
return ret;
961-
}
962-
963932
static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
964933
void __user *useraddr)
965934
{

net/ethtool/netlink.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,11 @@ static const struct genl_ops ethtool_genl_ops[] = {
771771
.dumpit = ethnl_default_dumpit,
772772
.done = ethnl_default_done,
773773
},
774+
{
775+
.cmd = ETHTOOL_MSG_CHANNELS_SET,
776+
.flags = GENL_UNS_ADMIN_PERM,
777+
.doit = ethnl_set_channels,
778+
},
774779
};
775780

776781
static const struct genl_multicast_group ethtool_nl_mcgrps[] = {

net/ethtool/netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,5 +349,6 @@ int ethnl_set_wol(struct sk_buff *skb, struct genl_info *info);
349349
int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
350350
int ethnl_set_privflags(struct sk_buff *skb, struct genl_info *info);
351351
int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info);
352+
int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info);
352353

353354
#endif /* _NET_ETHTOOL_NETLINK_H */

0 commit comments

Comments
 (0)