1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include "netlink.h" 4 #include "common.h" 5 6 struct coalesce_req_info { 7 struct ethnl_req_info base; 8 }; 9 10 struct coalesce_reply_data { 11 struct ethnl_reply_data base; 12 struct ethtool_coalesce coalesce; 13 struct kernel_ethtool_coalesce kernel_coalesce; 14 u32 supported_params; 15 }; 16 17 #define COALESCE_REPDATA(__reply_base) \ 18 container_of(__reply_base, struct coalesce_reply_data, base) 19 20 #define __SUPPORTED_OFFSET ETHTOOL_A_COALESCE_RX_USECS 21 static u32 attr_to_mask(unsigned int attr_type) 22 { 23 return BIT(attr_type - __SUPPORTED_OFFSET); 24 } 25 26 /* build time check that indices in ethtool_ops::supported_coalesce_params 27 * match corresponding attribute types with an offset 28 */ 29 #define __CHECK_SUPPORTED_OFFSET(x) \ 30 static_assert((ETHTOOL_ ## x) == \ 31 BIT((ETHTOOL_A_ ## x) - __SUPPORTED_OFFSET)) 32 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS); 33 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES); 34 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_IRQ); 35 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_IRQ); 36 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS); 37 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES); 38 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_IRQ); 39 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_IRQ); 40 __CHECK_SUPPORTED_OFFSET(COALESCE_STATS_BLOCK_USECS); 41 __CHECK_SUPPORTED_OFFSET(COALESCE_USE_ADAPTIVE_RX); 42 __CHECK_SUPPORTED_OFFSET(COALESCE_USE_ADAPTIVE_TX); 43 __CHECK_SUPPORTED_OFFSET(COALESCE_PKT_RATE_LOW); 44 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_LOW); 45 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_LOW); 46 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_LOW); 47 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_LOW); 48 __CHECK_SUPPORTED_OFFSET(COALESCE_PKT_RATE_HIGH); 49 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_HIGH); 50 __CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_HIGH); 51 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_HIGH); 52 __CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_HIGH); 53 __CHECK_SUPPORTED_OFFSET(COALESCE_RATE_SAMPLE_INTERVAL); 54 55 const struct nla_policy ethnl_coalesce_get_policy[] = { 56 [ETHTOOL_A_COALESCE_HEADER] = 57 NLA_POLICY_NESTED(ethnl_header_policy), 58 }; 59 60 static int coalesce_prepare_data(const struct ethnl_req_info *req_base, 61 struct ethnl_reply_data *reply_base, 62 struct genl_info *info) 63 { 64 struct coalesce_reply_data *data = COALESCE_REPDATA(reply_base); 65 struct netlink_ext_ack *extack = info ? info->extack : NULL; 66 struct net_device *dev = reply_base->dev; 67 int ret; 68 69 if (!dev->ethtool_ops->get_coalesce) 70 return -EOPNOTSUPP; 71 data->supported_params = dev->ethtool_ops->supported_coalesce_params; 72 ret = ethnl_ops_begin(dev); 73 if (ret < 0) 74 return ret; 75 ret = dev->ethtool_ops->get_coalesce(dev, &data->coalesce, 76 &data->kernel_coalesce, extack); 77 ethnl_ops_complete(dev); 78 79 return ret; 80 } 81 82 static int coalesce_reply_size(const struct ethnl_req_info *req_base, 83 const struct ethnl_reply_data *reply_base) 84 { 85 return nla_total_size(sizeof(u32)) + /* _RX_USECS */ 86 nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES */ 87 nla_total_size(sizeof(u32)) + /* _RX_USECS_IRQ */ 88 nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES_IRQ */ 89 nla_total_size(sizeof(u32)) + /* _TX_USECS */ 90 nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES */ 91 nla_total_size(sizeof(u32)) + /* _TX_USECS_IRQ */ 92 nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES_IRQ */ 93 nla_total_size(sizeof(u32)) + /* _STATS_BLOCK_USECS */ 94 nla_total_size(sizeof(u8)) + /* _USE_ADAPTIVE_RX */ 95 nla_total_size(sizeof(u8)) + /* _USE_ADAPTIVE_TX */ 96 nla_total_size(sizeof(u32)) + /* _PKT_RATE_LOW */ 97 nla_total_size(sizeof(u32)) + /* _RX_USECS_LOW */ 98 nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES_LOW */ 99 nla_total_size(sizeof(u32)) + /* _TX_USECS_LOW */ 100 nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES_LOW */ 101 nla_total_size(sizeof(u32)) + /* _PKT_RATE_HIGH */ 102 nla_total_size(sizeof(u32)) + /* _RX_USECS_HIGH */ 103 nla_total_size(sizeof(u32)) + /* _RX_MAX_FRAMES_HIGH */ 104 nla_total_size(sizeof(u32)) + /* _TX_USECS_HIGH */ 105 nla_total_size(sizeof(u32)) + /* _TX_MAX_FRAMES_HIGH */ 106 nla_total_size(sizeof(u32)) + /* _RATE_SAMPLE_INTERVAL */ 107 nla_total_size(sizeof(u8)) + /* _USE_CQE_MODE_TX */ 108 nla_total_size(sizeof(u8)) + /* _USE_CQE_MODE_RX */ 109 nla_total_size(sizeof(u32)) + /* _TX_AGGR_MAX_BYTES */ 110 nla_total_size(sizeof(u32)) + /* _TX_AGGR_MAX_FRAMES */ 111 nla_total_size(sizeof(u32)); /* _TX_AGGR_TIME_USECS */ 112 } 113 114 static bool coalesce_put_u32(struct sk_buff *skb, u16 attr_type, u32 val, 115 u32 supported_params) 116 { 117 if (!val && !(supported_params & attr_to_mask(attr_type))) 118 return false; 119 return nla_put_u32(skb, attr_type, val); 120 } 121 122 static bool coalesce_put_bool(struct sk_buff *skb, u16 attr_type, u32 val, 123 u32 supported_params) 124 { 125 if (!val && !(supported_params & attr_to_mask(attr_type))) 126 return false; 127 return nla_put_u8(skb, attr_type, !!val); 128 } 129 130 static int coalesce_fill_reply(struct sk_buff *skb, 131 const struct ethnl_req_info *req_base, 132 const struct ethnl_reply_data *reply_base) 133 { 134 const struct coalesce_reply_data *data = COALESCE_REPDATA(reply_base); 135 const struct kernel_ethtool_coalesce *kcoal = &data->kernel_coalesce; 136 const struct ethtool_coalesce *coal = &data->coalesce; 137 u32 supported = data->supported_params; 138 139 if (coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS, 140 coal->rx_coalesce_usecs, supported) || 141 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES, 142 coal->rx_max_coalesced_frames, supported) || 143 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_IRQ, 144 coal->rx_coalesce_usecs_irq, supported) || 145 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ, 146 coal->rx_max_coalesced_frames_irq, supported) || 147 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS, 148 coal->tx_coalesce_usecs, supported) || 149 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES, 150 coal->tx_max_coalesced_frames, supported) || 151 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_IRQ, 152 coal->tx_coalesce_usecs_irq, supported) || 153 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ, 154 coal->tx_max_coalesced_frames_irq, supported) || 155 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_STATS_BLOCK_USECS, 156 coal->stats_block_coalesce_usecs, supported) || 157 coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX, 158 coal->use_adaptive_rx_coalesce, supported) || 159 coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX, 160 coal->use_adaptive_tx_coalesce, supported) || 161 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_PKT_RATE_LOW, 162 coal->pkt_rate_low, supported) || 163 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_LOW, 164 coal->rx_coalesce_usecs_low, supported) || 165 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW, 166 coal->rx_max_coalesced_frames_low, supported) || 167 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_LOW, 168 coal->tx_coalesce_usecs_low, supported) || 169 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW, 170 coal->tx_max_coalesced_frames_low, supported) || 171 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_PKT_RATE_HIGH, 172 coal->pkt_rate_high, supported) || 173 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_HIGH, 174 coal->rx_coalesce_usecs_high, supported) || 175 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH, 176 coal->rx_max_coalesced_frames_high, supported) || 177 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_HIGH, 178 coal->tx_coalesce_usecs_high, supported) || 179 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH, 180 coal->tx_max_coalesced_frames_high, supported) || 181 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL, 182 coal->rate_sample_interval, supported) || 183 coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_CQE_MODE_TX, 184 kcoal->use_cqe_mode_tx, supported) || 185 coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_CQE_MODE_RX, 186 kcoal->use_cqe_mode_rx, supported) || 187 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES, 188 kcoal->tx_aggr_max_bytes, supported) || 189 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES, 190 kcoal->tx_aggr_max_frames, supported) || 191 coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS, 192 kcoal->tx_aggr_time_usecs, supported)) 193 return -EMSGSIZE; 194 195 return 0; 196 } 197 198 const struct ethnl_request_ops ethnl_coalesce_request_ops = { 199 .request_cmd = ETHTOOL_MSG_COALESCE_GET, 200 .reply_cmd = ETHTOOL_MSG_COALESCE_GET_REPLY, 201 .hdr_attr = ETHTOOL_A_COALESCE_HEADER, 202 .req_info_size = sizeof(struct coalesce_req_info), 203 .reply_data_size = sizeof(struct coalesce_reply_data), 204 205 .prepare_data = coalesce_prepare_data, 206 .reply_size = coalesce_reply_size, 207 .fill_reply = coalesce_fill_reply, 208 }; 209 210 /* COALESCE_SET */ 211 212 const struct nla_policy ethnl_coalesce_set_policy[] = { 213 [ETHTOOL_A_COALESCE_HEADER] = 214 NLA_POLICY_NESTED(ethnl_header_policy), 215 [ETHTOOL_A_COALESCE_RX_USECS] = { .type = NLA_U32 }, 216 [ETHTOOL_A_COALESCE_RX_MAX_FRAMES] = { .type = NLA_U32 }, 217 [ETHTOOL_A_COALESCE_RX_USECS_IRQ] = { .type = NLA_U32 }, 218 [ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ] = { .type = NLA_U32 }, 219 [ETHTOOL_A_COALESCE_TX_USECS] = { .type = NLA_U32 }, 220 [ETHTOOL_A_COALESCE_TX_MAX_FRAMES] = { .type = NLA_U32 }, 221 [ETHTOOL_A_COALESCE_TX_USECS_IRQ] = { .type = NLA_U32 }, 222 [ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ] = { .type = NLA_U32 }, 223 [ETHTOOL_A_COALESCE_STATS_BLOCK_USECS] = { .type = NLA_U32 }, 224 [ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX] = { .type = NLA_U8 }, 225 [ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX] = { .type = NLA_U8 }, 226 [ETHTOOL_A_COALESCE_PKT_RATE_LOW] = { .type = NLA_U32 }, 227 [ETHTOOL_A_COALESCE_RX_USECS_LOW] = { .type = NLA_U32 }, 228 [ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW] = { .type = NLA_U32 }, 229 [ETHTOOL_A_COALESCE_TX_USECS_LOW] = { .type = NLA_U32 }, 230 [ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW] = { .type = NLA_U32 }, 231 [ETHTOOL_A_COALESCE_PKT_RATE_HIGH] = { .type = NLA_U32 }, 232 [ETHTOOL_A_COALESCE_RX_USECS_HIGH] = { .type = NLA_U32 }, 233 [ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH] = { .type = NLA_U32 }, 234 [ETHTOOL_A_COALESCE_TX_USECS_HIGH] = { .type = NLA_U32 }, 235 [ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH] = { .type = NLA_U32 }, 236 [ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL] = { .type = NLA_U32 }, 237 [ETHTOOL_A_COALESCE_USE_CQE_MODE_TX] = NLA_POLICY_MAX(NLA_U8, 1), 238 [ETHTOOL_A_COALESCE_USE_CQE_MODE_RX] = NLA_POLICY_MAX(NLA_U8, 1), 239 [ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES] = { .type = NLA_U32 }, 240 [ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES] = { .type = NLA_U32 }, 241 [ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS] = { .type = NLA_U32 }, 242 }; 243 244 int ethnl_set_coalesce(struct sk_buff *skb, struct genl_info *info) 245 { 246 struct kernel_ethtool_coalesce kernel_coalesce = {}; 247 struct ethtool_coalesce coalesce = {}; 248 struct ethnl_req_info req_info = {}; 249 struct nlattr **tb = info->attrs; 250 const struct ethtool_ops *ops; 251 struct net_device *dev; 252 u32 supported_params; 253 bool mod = false; 254 int ret; 255 u16 a; 256 257 ret = ethnl_parse_header_dev_get(&req_info, 258 tb[ETHTOOL_A_COALESCE_HEADER], 259 genl_info_net(info), info->extack, 260 true); 261 if (ret < 0) 262 return ret; 263 dev = req_info.dev; 264 ops = dev->ethtool_ops; 265 ret = -EOPNOTSUPP; 266 if (!ops->get_coalesce || !ops->set_coalesce) 267 goto out_dev; 268 269 /* make sure that only supported parameters are present */ 270 supported_params = ops->supported_coalesce_params; 271 for (a = ETHTOOL_A_COALESCE_RX_USECS; a < __ETHTOOL_A_COALESCE_CNT; a++) 272 if (tb[a] && !(supported_params & attr_to_mask(a))) { 273 ret = -EINVAL; 274 NL_SET_ERR_MSG_ATTR(info->extack, tb[a], 275 "cannot modify an unsupported parameter"); 276 goto out_dev; 277 } 278 279 rtnl_lock(); 280 ret = ethnl_ops_begin(dev); 281 if (ret < 0) 282 goto out_rtnl; 283 ret = ops->get_coalesce(dev, &coalesce, &kernel_coalesce, 284 info->extack); 285 if (ret < 0) 286 goto out_ops; 287 288 ethnl_update_u32(&coalesce.rx_coalesce_usecs, 289 tb[ETHTOOL_A_COALESCE_RX_USECS], &mod); 290 ethnl_update_u32(&coalesce.rx_max_coalesced_frames, 291 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES], &mod); 292 ethnl_update_u32(&coalesce.rx_coalesce_usecs_irq, 293 tb[ETHTOOL_A_COALESCE_RX_USECS_IRQ], &mod); 294 ethnl_update_u32(&coalesce.rx_max_coalesced_frames_irq, 295 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ], &mod); 296 ethnl_update_u32(&coalesce.tx_coalesce_usecs, 297 tb[ETHTOOL_A_COALESCE_TX_USECS], &mod); 298 ethnl_update_u32(&coalesce.tx_max_coalesced_frames, 299 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES], &mod); 300 ethnl_update_u32(&coalesce.tx_coalesce_usecs_irq, 301 tb[ETHTOOL_A_COALESCE_TX_USECS_IRQ], &mod); 302 ethnl_update_u32(&coalesce.tx_max_coalesced_frames_irq, 303 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ], &mod); 304 ethnl_update_u32(&coalesce.stats_block_coalesce_usecs, 305 tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS], &mod); 306 ethnl_update_bool32(&coalesce.use_adaptive_rx_coalesce, 307 tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX], &mod); 308 ethnl_update_bool32(&coalesce.use_adaptive_tx_coalesce, 309 tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX], &mod); 310 ethnl_update_u32(&coalesce.pkt_rate_low, 311 tb[ETHTOOL_A_COALESCE_PKT_RATE_LOW], &mod); 312 ethnl_update_u32(&coalesce.rx_coalesce_usecs_low, 313 tb[ETHTOOL_A_COALESCE_RX_USECS_LOW], &mod); 314 ethnl_update_u32(&coalesce.rx_max_coalesced_frames_low, 315 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW], &mod); 316 ethnl_update_u32(&coalesce.tx_coalesce_usecs_low, 317 tb[ETHTOOL_A_COALESCE_TX_USECS_LOW], &mod); 318 ethnl_update_u32(&coalesce.tx_max_coalesced_frames_low, 319 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW], &mod); 320 ethnl_update_u32(&coalesce.pkt_rate_high, 321 tb[ETHTOOL_A_COALESCE_PKT_RATE_HIGH], &mod); 322 ethnl_update_u32(&coalesce.rx_coalesce_usecs_high, 323 tb[ETHTOOL_A_COALESCE_RX_USECS_HIGH], &mod); 324 ethnl_update_u32(&coalesce.rx_max_coalesced_frames_high, 325 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH], &mod); 326 ethnl_update_u32(&coalesce.tx_coalesce_usecs_high, 327 tb[ETHTOOL_A_COALESCE_TX_USECS_HIGH], &mod); 328 ethnl_update_u32(&coalesce.tx_max_coalesced_frames_high, 329 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH], &mod); 330 ethnl_update_u32(&coalesce.rate_sample_interval, 331 tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL], &mod); 332 ethnl_update_u8(&kernel_coalesce.use_cqe_mode_tx, 333 tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_TX], &mod); 334 ethnl_update_u8(&kernel_coalesce.use_cqe_mode_rx, 335 tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_RX], &mod); 336 ethnl_update_u32(&kernel_coalesce.tx_aggr_max_bytes, 337 tb[ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES], &mod); 338 ethnl_update_u32(&kernel_coalesce.tx_aggr_max_frames, 339 tb[ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES], &mod); 340 ethnl_update_u32(&kernel_coalesce.tx_aggr_time_usecs, 341 tb[ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS], &mod); 342 ret = 0; 343 if (!mod) 344 goto out_ops; 345 346 ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce, 347 info->extack); 348 if (ret < 0) 349 goto out_ops; 350 ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL); 351 352 out_ops: 353 ethnl_ops_complete(dev); 354 out_rtnl: 355 rtnl_unlock(); 356 out_dev: 357 ethnl_parse_header_dev_put(&req_info); 358 return ret; 359 } 360