Commit | Line | Data |
---|---|---|
0c84979c MK |
1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | ||
a71506a4 | 3 | #include <net/xdp_sock_drv.h> |
e19c591e | 4 | |
0c84979c MK |
5 | #include "netlink.h" |
6 | #include "common.h" | |
7 | ||
8 | struct channels_req_info { | |
9 | struct ethnl_req_info base; | |
10 | }; | |
11 | ||
12 | struct channels_reply_data { | |
13 | struct ethnl_reply_data base; | |
14 | struct ethtool_channels channels; | |
15 | }; | |
16 | ||
17 | #define CHANNELS_REPDATA(__reply_base) \ | |
18 | container_of(__reply_base, struct channels_reply_data, base) | |
19 | ||
ff419afa | 20 | const struct nla_policy ethnl_channels_get_policy[] = { |
329d9c33 JK |
21 | [ETHTOOL_A_CHANNELS_HEADER] = |
22 | NLA_POLICY_NESTED(ethnl_header_policy), | |
0c84979c MK |
23 | }; |
24 | ||
25 | static int channels_prepare_data(const struct ethnl_req_info *req_base, | |
26 | struct ethnl_reply_data *reply_base, | |
27 | struct genl_info *info) | |
28 | { | |
29 | struct channels_reply_data *data = CHANNELS_REPDATA(reply_base); | |
30 | struct net_device *dev = reply_base->dev; | |
31 | int ret; | |
32 | ||
33 | if (!dev->ethtool_ops->get_channels) | |
34 | return -EOPNOTSUPP; | |
35 | ret = ethnl_ops_begin(dev); | |
36 | if (ret < 0) | |
37 | return ret; | |
38 | dev->ethtool_ops->get_channels(dev, &data->channels); | |
39 | ethnl_ops_complete(dev); | |
40 | ||
41 | return 0; | |
42 | } | |
43 | ||
44 | static int channels_reply_size(const struct ethnl_req_info *req_base, | |
45 | const struct ethnl_reply_data *reply_base) | |
46 | { | |
47 | return nla_total_size(sizeof(u32)) + /* _CHANNELS_RX_MAX */ | |
48 | nla_total_size(sizeof(u32)) + /* _CHANNELS_TX_MAX */ | |
49 | nla_total_size(sizeof(u32)) + /* _CHANNELS_OTHER_MAX */ | |
50 | nla_total_size(sizeof(u32)) + /* _CHANNELS_COMBINED_MAX */ | |
51 | nla_total_size(sizeof(u32)) + /* _CHANNELS_RX_COUNT */ | |
52 | nla_total_size(sizeof(u32)) + /* _CHANNELS_TX_COUNT */ | |
53 | nla_total_size(sizeof(u32)) + /* _CHANNELS_OTHER_COUNT */ | |
54 | nla_total_size(sizeof(u32)); /* _CHANNELS_COMBINED_COUNT */ | |
55 | } | |
56 | ||
57 | static int channels_fill_reply(struct sk_buff *skb, | |
58 | const struct ethnl_req_info *req_base, | |
59 | const struct ethnl_reply_data *reply_base) | |
60 | { | |
61 | const struct channels_reply_data *data = CHANNELS_REPDATA(reply_base); | |
62 | const struct ethtool_channels *channels = &data->channels; | |
63 | ||
64 | if ((channels->max_rx && | |
65 | (nla_put_u32(skb, ETHTOOL_A_CHANNELS_RX_MAX, | |
66 | channels->max_rx) || | |
67 | nla_put_u32(skb, ETHTOOL_A_CHANNELS_RX_COUNT, | |
68 | channels->rx_count))) || | |
69 | (channels->max_tx && | |
70 | (nla_put_u32(skb, ETHTOOL_A_CHANNELS_TX_MAX, | |
71 | channels->max_tx) || | |
72 | nla_put_u32(skb, ETHTOOL_A_CHANNELS_TX_COUNT, | |
73 | channels->tx_count))) || | |
74 | (channels->max_other && | |
75 | (nla_put_u32(skb, ETHTOOL_A_CHANNELS_OTHER_MAX, | |
76 | channels->max_other) || | |
77 | nla_put_u32(skb, ETHTOOL_A_CHANNELS_OTHER_COUNT, | |
78 | channels->other_count))) || | |
79 | (channels->max_combined && | |
80 | (nla_put_u32(skb, ETHTOOL_A_CHANNELS_COMBINED_MAX, | |
81 | channels->max_combined) || | |
82 | nla_put_u32(skb, ETHTOOL_A_CHANNELS_COMBINED_COUNT, | |
83 | channels->combined_count)))) | |
84 | return -EMSGSIZE; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
e19c591e MK |
89 | /* CHANNELS_SET */ |
90 | ||
ff419afa | 91 | const struct nla_policy ethnl_channels_set_policy[] = { |
329d9c33 JK |
92 | [ETHTOOL_A_CHANNELS_HEADER] = |
93 | NLA_POLICY_NESTED(ethnl_header_policy), | |
e19c591e MK |
94 | [ETHTOOL_A_CHANNELS_RX_COUNT] = { .type = NLA_U32 }, |
95 | [ETHTOOL_A_CHANNELS_TX_COUNT] = { .type = NLA_U32 }, | |
96 | [ETHTOOL_A_CHANNELS_OTHER_COUNT] = { .type = NLA_U32 }, | |
97 | [ETHTOOL_A_CHANNELS_COMBINED_COUNT] = { .type = NLA_U32 }, | |
98 | }; | |
99 | ||
04007961 JK |
100 | static int |
101 | ethnl_set_channels_validate(struct ethnl_req_info *req_info, | |
102 | struct genl_info *info) | |
103 | { | |
104 | const struct ethtool_ops *ops = req_info->dev->ethtool_ops; | |
105 | ||
106 | return ops->get_channels && ops->set_channels ? 1 : -EOPNOTSUPP; | |
107 | } | |
108 | ||
109 | static int | |
110 | ethnl_set_channels(struct ethnl_req_info *req_info, struct genl_info *info) | |
e19c591e | 111 | { |
e19c591e | 112 | unsigned int from_channel, old_total, i; |
7be92514 | 113 | bool mod = false, mod_combined = false; |
04007961 | 114 | struct net_device *dev = req_info->dev; |
e19c591e | 115 | struct ethtool_channels channels = {}; |
5028588b | 116 | struct nlattr **tb = info->attrs; |
47f3ecf4 | 117 | u32 err_attr, max_rxfh_in_use; |
47f3ecf4 | 118 | u64 max_rxnfc_in_use; |
e19c591e MK |
119 | int ret; |
120 | ||
04007961 | 121 | dev->ethtool_ops->get_channels(dev, &channels); |
e19c591e MK |
122 | old_total = channels.combined_count + |
123 | max(channels.rx_count, channels.tx_count); | |
124 | ||
125 | ethnl_update_u32(&channels.rx_count, tb[ETHTOOL_A_CHANNELS_RX_COUNT], | |
126 | &mod); | |
127 | ethnl_update_u32(&channels.tx_count, tb[ETHTOOL_A_CHANNELS_TX_COUNT], | |
128 | &mod); | |
129 | ethnl_update_u32(&channels.other_count, | |
130 | tb[ETHTOOL_A_CHANNELS_OTHER_COUNT], &mod); | |
131 | ethnl_update_u32(&channels.combined_count, | |
7be92514 JK |
132 | tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT], &mod_combined); |
133 | mod |= mod_combined; | |
e19c591e | 134 | if (!mod) |
04007961 | 135 | return 0; |
e19c591e MK |
136 | |
137 | /* ensure new channel counts are within limits */ | |
138 | if (channels.rx_count > channels.max_rx) | |
a4fc088a | 139 | err_attr = ETHTOOL_A_CHANNELS_RX_COUNT; |
e19c591e | 140 | else if (channels.tx_count > channels.max_tx) |
a4fc088a | 141 | err_attr = ETHTOOL_A_CHANNELS_TX_COUNT; |
e19c591e | 142 | else if (channels.other_count > channels.max_other) |
a4fc088a | 143 | err_attr = ETHTOOL_A_CHANNELS_OTHER_COUNT; |
e19c591e | 144 | else if (channels.combined_count > channels.max_combined) |
a4fc088a | 145 | err_attr = ETHTOOL_A_CHANNELS_COMBINED_COUNT; |
e19c591e | 146 | else |
a4fc088a | 147 | err_attr = 0; |
e19c591e | 148 | if (err_attr) { |
a4fc088a | 149 | NL_SET_ERR_MSG_ATTR(info->extack, tb[err_attr], |
5ec82c49 | 150 | "requested channel count exceeds maximum"); |
04007961 | 151 | return -EINVAL; |
e19c591e MK |
152 | } |
153 | ||
7be92514 JK |
154 | /* ensure there is at least one RX and one TX channel */ |
155 | if (!channels.combined_count && !channels.rx_count) | |
a4fc088a | 156 | err_attr = ETHTOOL_A_CHANNELS_RX_COUNT; |
7be92514 | 157 | else if (!channels.combined_count && !channels.tx_count) |
a4fc088a | 158 | err_attr = ETHTOOL_A_CHANNELS_TX_COUNT; |
7be92514 | 159 | else |
a4fc088a | 160 | err_attr = 0; |
7be92514 JK |
161 | if (err_attr) { |
162 | if (mod_combined) | |
a4fc088a | 163 | err_attr = ETHTOOL_A_CHANNELS_COMBINED_COUNT; |
a4fc088a YZ |
164 | NL_SET_ERR_MSG_ATTR(info->extack, tb[err_attr], |
165 | "requested channel counts would result in no RX or TX channel being configured"); | |
04007961 | 166 | return -EINVAL; |
7be92514 JK |
167 | } |
168 | ||
e19c591e | 169 | /* ensure the new Rx count fits within the configured Rx flow |
47f3ecf4 | 170 | * indirection table/rxnfc settings |
e19c591e | 171 | */ |
47f3ecf4 GP |
172 | if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use)) |
173 | max_rxnfc_in_use = 0; | |
174 | if (!netif_is_rxfh_configured(dev) || | |
175 | ethtool_get_max_rxfh_channel(dev, &max_rxfh_in_use)) | |
176 | max_rxfh_in_use = 0; | |
177 | if (channels.combined_count + channels.rx_count <= max_rxfh_in_use) { | |
e19c591e | 178 | GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing indirection table settings"); |
04007961 | 179 | return -EINVAL; |
e19c591e | 180 | } |
47f3ecf4 | 181 | if (channels.combined_count + channels.rx_count <= max_rxnfc_in_use) { |
47f3ecf4 | 182 | GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing ntuple filter settings"); |
04007961 | 183 | return -EINVAL; |
47f3ecf4 | 184 | } |
e19c591e MK |
185 | |
186 | /* Disabling channels, query zero-copy AF_XDP sockets */ | |
187 | from_channel = channels.combined_count + | |
188 | min(channels.rx_count, channels.tx_count); | |
189 | for (i = from_channel; i < old_total; i++) | |
c4655761 | 190 | if (xsk_get_pool_from_qid(dev, i)) { |
e19c591e | 191 | GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing zerocopy AF_XDP sockets"); |
04007961 | 192 | return -EINVAL; |
e19c591e MK |
193 | } |
194 | ||
195 | ret = dev->ethtool_ops->set_channels(dev, &channels); | |
04007961 | 196 | return ret < 0 ? ret : 1; |
e19c591e | 197 | } |
04007961 JK |
198 | |
199 | const struct ethnl_request_ops ethnl_channels_request_ops = { | |
200 | .request_cmd = ETHTOOL_MSG_CHANNELS_GET, | |
201 | .reply_cmd = ETHTOOL_MSG_CHANNELS_GET_REPLY, | |
202 | .hdr_attr = ETHTOOL_A_CHANNELS_HEADER, | |
203 | .req_info_size = sizeof(struct channels_req_info), | |
204 | .reply_data_size = sizeof(struct channels_reply_data), | |
205 | ||
206 | .prepare_data = channels_prepare_data, | |
207 | .reply_size = channels_reply_size, | |
208 | .fill_reply = channels_fill_reply, | |
209 | ||
210 | .set_validate = ethnl_set_channels_validate, | |
211 | .set = ethnl_set_channels, | |
212 | .set_ntf_cmd = ETHTOOL_MSG_CHANNELS_NTF, | |
213 | }; |