Michal Kubecek | 0c84979 | 2020-03-12 21:08:38 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 3 | #include <net/xdp_sock_drv.h> |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 4 | |
Michal Kubecek | 0c84979 | 2020-03-12 21:08:38 +0100 | [diff] [blame] | 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 | |
Jakub Kicinski | ff419af | 2020-10-05 15:07:35 -0700 | [diff] [blame] | 20 | const struct nla_policy ethnl_channels_get_policy[] = { |
Jakub Kicinski | 329d9c3 | 2020-10-05 15:07:36 -0700 | [diff] [blame] | 21 | [ETHTOOL_A_CHANNELS_HEADER] = |
| 22 | NLA_POLICY_NESTED(ethnl_header_policy), |
Michal Kubecek | 0c84979 | 2020-03-12 21:08:38 +0100 | [diff] [blame] | 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 | |
| 89 | const struct ethnl_request_ops ethnl_channels_request_ops = { |
| 90 | .request_cmd = ETHTOOL_MSG_CHANNELS_GET, |
| 91 | .reply_cmd = ETHTOOL_MSG_CHANNELS_GET_REPLY, |
| 92 | .hdr_attr = ETHTOOL_A_CHANNELS_HEADER, |
Michal Kubecek | 0c84979 | 2020-03-12 21:08:38 +0100 | [diff] [blame] | 93 | .req_info_size = sizeof(struct channels_req_info), |
| 94 | .reply_data_size = sizeof(struct channels_reply_data), |
Michal Kubecek | 0c84979 | 2020-03-12 21:08:38 +0100 | [diff] [blame] | 95 | |
| 96 | .prepare_data = channels_prepare_data, |
| 97 | .reply_size = channels_reply_size, |
| 98 | .fill_reply = channels_fill_reply, |
| 99 | }; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 100 | |
| 101 | /* CHANNELS_SET */ |
| 102 | |
Jakub Kicinski | ff419af | 2020-10-05 15:07:35 -0700 | [diff] [blame] | 103 | const struct nla_policy ethnl_channels_set_policy[] = { |
Jakub Kicinski | 329d9c3 | 2020-10-05 15:07:36 -0700 | [diff] [blame] | 104 | [ETHTOOL_A_CHANNELS_HEADER] = |
| 105 | NLA_POLICY_NESTED(ethnl_header_policy), |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 106 | [ETHTOOL_A_CHANNELS_RX_COUNT] = { .type = NLA_U32 }, |
| 107 | [ETHTOOL_A_CHANNELS_TX_COUNT] = { .type = NLA_U32 }, |
| 108 | [ETHTOOL_A_CHANNELS_OTHER_COUNT] = { .type = NLA_U32 }, |
| 109 | [ETHTOOL_A_CHANNELS_COMBINED_COUNT] = { .type = NLA_U32 }, |
| 110 | }; |
| 111 | |
| 112 | int ethnl_set_channels(struct sk_buff *skb, struct genl_info *info) |
| 113 | { |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 114 | unsigned int from_channel, old_total, i; |
Jakub Kicinski | 7be9251 | 2020-05-15 12:49:00 -0700 | [diff] [blame] | 115 | bool mod = false, mod_combined = false; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 116 | struct ethtool_channels channels = {}; |
| 117 | struct ethnl_req_info req_info = {}; |
Jakub Kicinski | 5028588b | 2020-10-05 15:07:34 -0700 | [diff] [blame] | 118 | struct nlattr **tb = info->attrs; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 119 | const struct nlattr *err_attr; |
| 120 | const struct ethtool_ops *ops; |
| 121 | struct net_device *dev; |
| 122 | u32 max_rx_in_use = 0; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 123 | int ret; |
| 124 | |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 125 | ret = ethnl_parse_header_dev_get(&req_info, |
| 126 | tb[ETHTOOL_A_CHANNELS_HEADER], |
| 127 | genl_info_net(info), info->extack, |
| 128 | true); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | dev = req_info.dev; |
| 132 | ops = dev->ethtool_ops; |
| 133 | ret = -EOPNOTSUPP; |
| 134 | if (!ops->get_channels || !ops->set_channels) |
| 135 | goto out_dev; |
| 136 | |
| 137 | rtnl_lock(); |
| 138 | ret = ethnl_ops_begin(dev); |
| 139 | if (ret < 0) |
| 140 | goto out_rtnl; |
| 141 | ops->get_channels(dev, &channels); |
| 142 | old_total = channels.combined_count + |
| 143 | max(channels.rx_count, channels.tx_count); |
| 144 | |
| 145 | ethnl_update_u32(&channels.rx_count, tb[ETHTOOL_A_CHANNELS_RX_COUNT], |
| 146 | &mod); |
| 147 | ethnl_update_u32(&channels.tx_count, tb[ETHTOOL_A_CHANNELS_TX_COUNT], |
| 148 | &mod); |
| 149 | ethnl_update_u32(&channels.other_count, |
| 150 | tb[ETHTOOL_A_CHANNELS_OTHER_COUNT], &mod); |
| 151 | ethnl_update_u32(&channels.combined_count, |
Jakub Kicinski | 7be9251 | 2020-05-15 12:49:00 -0700 | [diff] [blame] | 152 | tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT], &mod_combined); |
| 153 | mod |= mod_combined; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 154 | ret = 0; |
| 155 | if (!mod) |
| 156 | goto out_ops; |
| 157 | |
| 158 | /* ensure new channel counts are within limits */ |
| 159 | if (channels.rx_count > channels.max_rx) |
| 160 | err_attr = tb[ETHTOOL_A_CHANNELS_RX_COUNT]; |
| 161 | else if (channels.tx_count > channels.max_tx) |
| 162 | err_attr = tb[ETHTOOL_A_CHANNELS_TX_COUNT]; |
| 163 | else if (channels.other_count > channels.max_other) |
| 164 | err_attr = tb[ETHTOOL_A_CHANNELS_OTHER_COUNT]; |
| 165 | else if (channels.combined_count > channels.max_combined) |
| 166 | err_attr = tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT]; |
| 167 | else |
| 168 | err_attr = NULL; |
| 169 | if (err_attr) { |
| 170 | ret = -EINVAL; |
| 171 | NL_SET_ERR_MSG_ATTR(info->extack, err_attr, |
Colin Ian King | 5ec82c4 | 2020-03-13 11:25:34 +0000 | [diff] [blame] | 172 | "requested channel count exceeds maximum"); |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 173 | goto out_ops; |
| 174 | } |
| 175 | |
Jakub Kicinski | 7be9251 | 2020-05-15 12:49:00 -0700 | [diff] [blame] | 176 | /* ensure there is at least one RX and one TX channel */ |
| 177 | if (!channels.combined_count && !channels.rx_count) |
| 178 | err_attr = tb[ETHTOOL_A_CHANNELS_RX_COUNT]; |
| 179 | else if (!channels.combined_count && !channels.tx_count) |
| 180 | err_attr = tb[ETHTOOL_A_CHANNELS_TX_COUNT]; |
| 181 | else |
| 182 | err_attr = NULL; |
| 183 | if (err_attr) { |
| 184 | if (mod_combined) |
| 185 | err_attr = tb[ETHTOOL_A_CHANNELS_COMBINED_COUNT]; |
| 186 | ret = -EINVAL; |
| 187 | NL_SET_ERR_MSG_ATTR(info->extack, err_attr, "requested channel counts would result in no RX or TX channel being configured"); |
| 188 | goto out_ops; |
| 189 | } |
| 190 | |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 191 | /* ensure the new Rx count fits within the configured Rx flow |
| 192 | * indirection table settings |
| 193 | */ |
| 194 | if (netif_is_rxfh_configured(dev) && |
| 195 | !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) && |
| 196 | (channels.combined_count + channels.rx_count) <= max_rx_in_use) { |
Ivan Vecera | ef72cd3 | 2020-12-15 10:08:10 +0100 | [diff] [blame] | 197 | ret = -EINVAL; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 198 | GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing indirection table settings"); |
Ivan Vecera | ef72cd3 | 2020-12-15 10:08:10 +0100 | [diff] [blame] | 199 | goto out_ops; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* Disabling channels, query zero-copy AF_XDP sockets */ |
| 203 | from_channel = channels.combined_count + |
| 204 | min(channels.rx_count, channels.tx_count); |
| 205 | for (i = from_channel; i < old_total; i++) |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 206 | if (xsk_get_pool_from_qid(dev, i)) { |
Ivan Vecera | ef72cd3 | 2020-12-15 10:08:10 +0100 | [diff] [blame] | 207 | ret = -EINVAL; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 208 | GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing zerocopy AF_XDP sockets"); |
Ivan Vecera | ef72cd3 | 2020-12-15 10:08:10 +0100 | [diff] [blame] | 209 | goto out_ops; |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | ret = dev->ethtool_ops->set_channels(dev, &channels); |
Michal Kubecek | 546379b | 2020-03-12 21:08:48 +0100 | [diff] [blame] | 213 | if (ret < 0) |
| 214 | goto out_ops; |
| 215 | ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL); |
Michal Kubecek | e19c591 | 2020-03-12 21:08:43 +0100 | [diff] [blame] | 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 | } |