1e4a1717bSMichal Kubecek // SPDX-License-Identifier: GPL-2.0-only 2e4a1717bSMichal Kubecek 3e4a1717bSMichal Kubecek #include "netlink.h" 4e4a1717bSMichal Kubecek #include "common.h" 5e4a1717bSMichal Kubecek 6e4a1717bSMichal Kubecek struct rings_req_info { 7e4a1717bSMichal Kubecek struct ethnl_req_info base; 8e4a1717bSMichal Kubecek }; 9e4a1717bSMichal Kubecek 10e4a1717bSMichal Kubecek struct rings_reply_data { 11e4a1717bSMichal Kubecek struct ethnl_reply_data base; 12e4a1717bSMichal Kubecek struct ethtool_ringparam ringparam; 13e4a1717bSMichal Kubecek }; 14e4a1717bSMichal Kubecek 15e4a1717bSMichal Kubecek #define RINGS_REPDATA(__reply_base) \ 16e4a1717bSMichal Kubecek container_of(__reply_base, struct rings_reply_data, base) 17e4a1717bSMichal Kubecek 18e4a1717bSMichal Kubecek static const struct nla_policy 19e4a1717bSMichal Kubecek rings_get_policy[ETHTOOL_A_RINGS_MAX + 1] = { 20e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_UNSPEC] = { .type = NLA_REJECT }, 21e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_HEADER] = { .type = NLA_NESTED }, 22e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_RX_MAX] = { .type = NLA_REJECT }, 23e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_RX_MINI_MAX] = { .type = NLA_REJECT }, 24e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_RX_JUMBO_MAX] = { .type = NLA_REJECT }, 25e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_TX_MAX] = { .type = NLA_REJECT }, 26e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_RX] = { .type = NLA_REJECT }, 27e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_RX_MINI] = { .type = NLA_REJECT }, 28e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_RX_JUMBO] = { .type = NLA_REJECT }, 29e4a1717bSMichal Kubecek [ETHTOOL_A_RINGS_TX] = { .type = NLA_REJECT }, 30e4a1717bSMichal Kubecek }; 31e4a1717bSMichal Kubecek 32e4a1717bSMichal Kubecek static int rings_prepare_data(const struct ethnl_req_info *req_base, 33e4a1717bSMichal Kubecek struct ethnl_reply_data *reply_base, 34e4a1717bSMichal Kubecek struct genl_info *info) 35e4a1717bSMichal Kubecek { 36e4a1717bSMichal Kubecek struct rings_reply_data *data = RINGS_REPDATA(reply_base); 37e4a1717bSMichal Kubecek struct net_device *dev = reply_base->dev; 38e4a1717bSMichal Kubecek int ret; 39e4a1717bSMichal Kubecek 40e4a1717bSMichal Kubecek if (!dev->ethtool_ops->get_ringparam) 41e4a1717bSMichal Kubecek return -EOPNOTSUPP; 42e4a1717bSMichal Kubecek ret = ethnl_ops_begin(dev); 43e4a1717bSMichal Kubecek if (ret < 0) 44e4a1717bSMichal Kubecek return ret; 45e4a1717bSMichal Kubecek dev->ethtool_ops->get_ringparam(dev, &data->ringparam); 46e4a1717bSMichal Kubecek ethnl_ops_complete(dev); 47e4a1717bSMichal Kubecek 48e4a1717bSMichal Kubecek return 0; 49e4a1717bSMichal Kubecek } 50e4a1717bSMichal Kubecek 51e4a1717bSMichal Kubecek static int rings_reply_size(const struct ethnl_req_info *req_base, 52e4a1717bSMichal Kubecek const struct ethnl_reply_data *reply_base) 53e4a1717bSMichal Kubecek { 54e4a1717bSMichal Kubecek return nla_total_size(sizeof(u32)) + /* _RINGS_RX_MAX */ 55e4a1717bSMichal Kubecek nla_total_size(sizeof(u32)) + /* _RINGS_RX_MINI_MAX */ 56e4a1717bSMichal Kubecek nla_total_size(sizeof(u32)) + /* _RINGS_RX_JUMBO_MAX */ 57e4a1717bSMichal Kubecek nla_total_size(sizeof(u32)) + /* _RINGS_TX_MAX */ 58e4a1717bSMichal Kubecek nla_total_size(sizeof(u32)) + /* _RINGS_RX */ 59e4a1717bSMichal Kubecek nla_total_size(sizeof(u32)) + /* _RINGS_RX_MINI */ 60e4a1717bSMichal Kubecek nla_total_size(sizeof(u32)) + /* _RINGS_RX_JUMBO */ 61e4a1717bSMichal Kubecek nla_total_size(sizeof(u32)); /* _RINGS_TX */ 62e4a1717bSMichal Kubecek } 63e4a1717bSMichal Kubecek 64e4a1717bSMichal Kubecek static int rings_fill_reply(struct sk_buff *skb, 65e4a1717bSMichal Kubecek const struct ethnl_req_info *req_base, 66e4a1717bSMichal Kubecek const struct ethnl_reply_data *reply_base) 67e4a1717bSMichal Kubecek { 68e4a1717bSMichal Kubecek const struct rings_reply_data *data = RINGS_REPDATA(reply_base); 69e4a1717bSMichal Kubecek const struct ethtool_ringparam *ringparam = &data->ringparam; 70e4a1717bSMichal Kubecek 71e4a1717bSMichal Kubecek if ((ringparam->rx_max_pending && 72e4a1717bSMichal Kubecek (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MAX, 73e4a1717bSMichal Kubecek ringparam->rx_max_pending) || 74e4a1717bSMichal Kubecek nla_put_u32(skb, ETHTOOL_A_RINGS_RX, 75e4a1717bSMichal Kubecek ringparam->rx_pending))) || 76e4a1717bSMichal Kubecek (ringparam->rx_mini_max_pending && 77e4a1717bSMichal Kubecek (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI_MAX, 78e4a1717bSMichal Kubecek ringparam->rx_mini_max_pending) || 79e4a1717bSMichal Kubecek nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI, 80e4a1717bSMichal Kubecek ringparam->rx_mini_pending))) || 81e4a1717bSMichal Kubecek (ringparam->rx_jumbo_max_pending && 82e4a1717bSMichal Kubecek (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO_MAX, 83e4a1717bSMichal Kubecek ringparam->rx_jumbo_max_pending) || 84e4a1717bSMichal Kubecek nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO, 85e4a1717bSMichal Kubecek ringparam->rx_jumbo_pending))) || 86e4a1717bSMichal Kubecek (ringparam->tx_max_pending && 87e4a1717bSMichal Kubecek (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_MAX, 88e4a1717bSMichal Kubecek ringparam->tx_max_pending) || 89e4a1717bSMichal Kubecek nla_put_u32(skb, ETHTOOL_A_RINGS_TX, 90e4a1717bSMichal Kubecek ringparam->tx_pending)))) 91e4a1717bSMichal Kubecek return -EMSGSIZE; 92e4a1717bSMichal Kubecek 93e4a1717bSMichal Kubecek return 0; 94e4a1717bSMichal Kubecek } 95e4a1717bSMichal Kubecek 96e4a1717bSMichal Kubecek const struct ethnl_request_ops ethnl_rings_request_ops = { 97e4a1717bSMichal Kubecek .request_cmd = ETHTOOL_MSG_RINGS_GET, 98e4a1717bSMichal Kubecek .reply_cmd = ETHTOOL_MSG_RINGS_GET_REPLY, 99e4a1717bSMichal Kubecek .hdr_attr = ETHTOOL_A_RINGS_HEADER, 100e4a1717bSMichal Kubecek .max_attr = ETHTOOL_A_RINGS_MAX, 101e4a1717bSMichal Kubecek .req_info_size = sizeof(struct rings_req_info), 102e4a1717bSMichal Kubecek .reply_data_size = sizeof(struct rings_reply_data), 103e4a1717bSMichal Kubecek .request_policy = rings_get_policy, 104e4a1717bSMichal Kubecek 105e4a1717bSMichal Kubecek .prepare_data = rings_prepare_data, 106e4a1717bSMichal Kubecek .reply_size = rings_reply_size, 107e4a1717bSMichal Kubecek .fill_reply = rings_fill_reply, 108e4a1717bSMichal Kubecek }; 109*2fc2929eSMichal Kubecek 110*2fc2929eSMichal Kubecek /* RINGS_SET */ 111*2fc2929eSMichal Kubecek 112*2fc2929eSMichal Kubecek static const struct nla_policy 113*2fc2929eSMichal Kubecek rings_set_policy[ETHTOOL_A_RINGS_MAX + 1] = { 114*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_UNSPEC] = { .type = NLA_REJECT }, 115*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_HEADER] = { .type = NLA_NESTED }, 116*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_RX_MAX] = { .type = NLA_REJECT }, 117*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_RX_MINI_MAX] = { .type = NLA_REJECT }, 118*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_RX_JUMBO_MAX] = { .type = NLA_REJECT }, 119*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_TX_MAX] = { .type = NLA_REJECT }, 120*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_RX] = { .type = NLA_U32 }, 121*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_RX_MINI] = { .type = NLA_U32 }, 122*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_RX_JUMBO] = { .type = NLA_U32 }, 123*2fc2929eSMichal Kubecek [ETHTOOL_A_RINGS_TX] = { .type = NLA_U32 }, 124*2fc2929eSMichal Kubecek }; 125*2fc2929eSMichal Kubecek 126*2fc2929eSMichal Kubecek int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info) 127*2fc2929eSMichal Kubecek { 128*2fc2929eSMichal Kubecek struct nlattr *tb[ETHTOOL_A_RINGS_MAX + 1]; 129*2fc2929eSMichal Kubecek struct ethtool_ringparam ringparam = {}; 130*2fc2929eSMichal Kubecek struct ethnl_req_info req_info = {}; 131*2fc2929eSMichal Kubecek const struct nlattr *err_attr; 132*2fc2929eSMichal Kubecek const struct ethtool_ops *ops; 133*2fc2929eSMichal Kubecek struct net_device *dev; 134*2fc2929eSMichal Kubecek bool mod = false; 135*2fc2929eSMichal Kubecek int ret; 136*2fc2929eSMichal Kubecek 137*2fc2929eSMichal Kubecek ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb, 138*2fc2929eSMichal Kubecek ETHTOOL_A_RINGS_MAX, rings_set_policy, 139*2fc2929eSMichal Kubecek info->extack); 140*2fc2929eSMichal Kubecek if (ret < 0) 141*2fc2929eSMichal Kubecek return ret; 142*2fc2929eSMichal Kubecek ret = ethnl_parse_header_dev_get(&req_info, 143*2fc2929eSMichal Kubecek tb[ETHTOOL_A_RINGS_HEADER], 144*2fc2929eSMichal Kubecek genl_info_net(info), info->extack, 145*2fc2929eSMichal Kubecek true); 146*2fc2929eSMichal Kubecek if (ret < 0) 147*2fc2929eSMichal Kubecek return ret; 148*2fc2929eSMichal Kubecek dev = req_info.dev; 149*2fc2929eSMichal Kubecek ops = dev->ethtool_ops; 150*2fc2929eSMichal Kubecek ret = -EOPNOTSUPP; 151*2fc2929eSMichal Kubecek if (!ops->get_ringparam || !ops->set_ringparam) 152*2fc2929eSMichal Kubecek goto out_dev; 153*2fc2929eSMichal Kubecek 154*2fc2929eSMichal Kubecek rtnl_lock(); 155*2fc2929eSMichal Kubecek ret = ethnl_ops_begin(dev); 156*2fc2929eSMichal Kubecek if (ret < 0) 157*2fc2929eSMichal Kubecek goto out_rtnl; 158*2fc2929eSMichal Kubecek ops->get_ringparam(dev, &ringparam); 159*2fc2929eSMichal Kubecek 160*2fc2929eSMichal Kubecek ethnl_update_u32(&ringparam.rx_pending, tb[ETHTOOL_A_RINGS_RX], &mod); 161*2fc2929eSMichal Kubecek ethnl_update_u32(&ringparam.rx_mini_pending, 162*2fc2929eSMichal Kubecek tb[ETHTOOL_A_RINGS_RX_MINI], &mod); 163*2fc2929eSMichal Kubecek ethnl_update_u32(&ringparam.rx_jumbo_pending, 164*2fc2929eSMichal Kubecek tb[ETHTOOL_A_RINGS_RX_JUMBO], &mod); 165*2fc2929eSMichal Kubecek ethnl_update_u32(&ringparam.tx_pending, tb[ETHTOOL_A_RINGS_TX], &mod); 166*2fc2929eSMichal Kubecek ret = 0; 167*2fc2929eSMichal Kubecek if (!mod) 168*2fc2929eSMichal Kubecek goto out_ops; 169*2fc2929eSMichal Kubecek 170*2fc2929eSMichal Kubecek /* ensure new ring parameters are within limits */ 171*2fc2929eSMichal Kubecek if (ringparam.rx_pending > ringparam.rx_max_pending) 172*2fc2929eSMichal Kubecek err_attr = tb[ETHTOOL_A_RINGS_RX]; 173*2fc2929eSMichal Kubecek else if (ringparam.rx_mini_pending > ringparam.rx_mini_max_pending) 174*2fc2929eSMichal Kubecek err_attr = tb[ETHTOOL_A_RINGS_RX_MINI]; 175*2fc2929eSMichal Kubecek else if (ringparam.rx_jumbo_pending > ringparam.rx_jumbo_max_pending) 176*2fc2929eSMichal Kubecek err_attr = tb[ETHTOOL_A_RINGS_RX_JUMBO]; 177*2fc2929eSMichal Kubecek else if (ringparam.tx_pending > ringparam.tx_max_pending) 178*2fc2929eSMichal Kubecek err_attr = tb[ETHTOOL_A_RINGS_TX]; 179*2fc2929eSMichal Kubecek else 180*2fc2929eSMichal Kubecek err_attr = NULL; 181*2fc2929eSMichal Kubecek if (err_attr) { 182*2fc2929eSMichal Kubecek ret = -EINVAL; 183*2fc2929eSMichal Kubecek NL_SET_ERR_MSG_ATTR(info->extack, err_attr, 184*2fc2929eSMichal Kubecek "requested ring size exceeeds maximum"); 185*2fc2929eSMichal Kubecek goto out_ops; 186*2fc2929eSMichal Kubecek } 187*2fc2929eSMichal Kubecek 188*2fc2929eSMichal Kubecek ret = dev->ethtool_ops->set_ringparam(dev, &ringparam); 189*2fc2929eSMichal Kubecek 190*2fc2929eSMichal Kubecek out_ops: 191*2fc2929eSMichal Kubecek ethnl_ops_complete(dev); 192*2fc2929eSMichal Kubecek out_rtnl: 193*2fc2929eSMichal Kubecek rtnl_unlock(); 194*2fc2929eSMichal Kubecek out_dev: 195*2fc2929eSMichal Kubecek dev_put(dev); 196*2fc2929eSMichal Kubecek return ret; 197*2fc2929eSMichal Kubecek } 198