1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include "netlink.h" 4 #include "common.h" 5 6 struct pause_req_info { 7 struct ethnl_req_info base; 8 }; 9 10 struct pause_reply_data { 11 struct ethnl_reply_data base; 12 struct ethtool_pauseparam pauseparam; 13 struct ethtool_pause_stats pausestat; 14 }; 15 16 #define PAUSE_REPDATA(__reply_base) \ 17 container_of(__reply_base, struct pause_reply_data, base) 18 19 const struct nla_policy ethnl_pause_get_policy[ETHTOOL_A_PAUSE_MAX + 1] = { 20 [ETHTOOL_A_PAUSE_UNSPEC] = { .type = NLA_REJECT }, 21 [ETHTOOL_A_PAUSE_HEADER] = { .type = NLA_NESTED }, 22 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_REJECT }, 23 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_REJECT }, 24 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_REJECT }, 25 [ETHTOOL_A_PAUSE_STATS] = { .type = NLA_REJECT }, 26 }; 27 28 static void ethtool_stats_init(u64 *stats, unsigned int n) 29 { 30 while (n--) 31 stats[n] = ETHTOOL_STAT_NOT_SET; 32 } 33 34 static int pause_prepare_data(const struct ethnl_req_info *req_base, 35 struct ethnl_reply_data *reply_base, 36 struct genl_info *info) 37 { 38 struct pause_reply_data *data = PAUSE_REPDATA(reply_base); 39 struct net_device *dev = reply_base->dev; 40 int ret; 41 42 if (!dev->ethtool_ops->get_pauseparam) 43 return -EOPNOTSUPP; 44 45 ret = ethnl_ops_begin(dev); 46 if (ret < 0) 47 return ret; 48 dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam); 49 if (req_base->flags & ETHTOOL_FLAG_STATS && 50 dev->ethtool_ops->get_pause_stats) { 51 ethtool_stats_init((u64 *)&data->pausestat, 52 sizeof(data->pausestat) / 8); 53 dev->ethtool_ops->get_pause_stats(dev, &data->pausestat); 54 } 55 ethnl_ops_complete(dev); 56 57 return 0; 58 } 59 60 static int pause_reply_size(const struct ethnl_req_info *req_base, 61 const struct ethnl_reply_data *reply_base) 62 { 63 int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */ 64 nla_total_size(sizeof(u8)) + /* _PAUSE_RX */ 65 nla_total_size(sizeof(u8)); /* _PAUSE_TX */ 66 67 if (req_base->flags & ETHTOOL_FLAG_STATS) 68 n += nla_total_size(0) + /* _PAUSE_STATS */ 69 nla_total_size_64bit(sizeof(u64)) * 70 (ETHTOOL_A_PAUSE_STAT_MAX - 2); 71 return n; 72 } 73 74 static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype, 75 u16 padtype) 76 { 77 if (val == ETHTOOL_STAT_NOT_SET) 78 return 0; 79 if (nla_put_u64_64bit(skb, attrtype, val, padtype)) 80 return -EMSGSIZE; 81 82 return 0; 83 } 84 85 static int pause_put_stats(struct sk_buff *skb, 86 const struct ethtool_pause_stats *pause_stats) 87 { 88 const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD; 89 struct nlattr *nest; 90 91 nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS); 92 if (!nest) 93 return -EMSGSIZE; 94 95 if (ethtool_put_stat(skb, pause_stats->tx_pause_frames, 96 ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) || 97 ethtool_put_stat(skb, pause_stats->rx_pause_frames, 98 ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad)) 99 goto err_cancel; 100 101 nla_nest_end(skb, nest); 102 return 0; 103 104 err_cancel: 105 nla_nest_cancel(skb, nest); 106 return -EMSGSIZE; 107 } 108 109 static int pause_fill_reply(struct sk_buff *skb, 110 const struct ethnl_req_info *req_base, 111 const struct ethnl_reply_data *reply_base) 112 { 113 const struct pause_reply_data *data = PAUSE_REPDATA(reply_base); 114 const struct ethtool_pauseparam *pauseparam = &data->pauseparam; 115 116 if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) || 117 nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) || 118 nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause)) 119 return -EMSGSIZE; 120 121 if (req_base->flags & ETHTOOL_FLAG_STATS && 122 pause_put_stats(skb, &data->pausestat)) 123 return -EMSGSIZE; 124 125 return 0; 126 } 127 128 const struct ethnl_request_ops ethnl_pause_request_ops = { 129 .request_cmd = ETHTOOL_MSG_PAUSE_GET, 130 .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY, 131 .hdr_attr = ETHTOOL_A_PAUSE_HEADER, 132 .req_info_size = sizeof(struct pause_req_info), 133 .reply_data_size = sizeof(struct pause_reply_data), 134 135 .prepare_data = pause_prepare_data, 136 .reply_size = pause_reply_size, 137 .fill_reply = pause_fill_reply, 138 }; 139 140 /* PAUSE_SET */ 141 142 static const struct nla_policy 143 pause_set_policy[ETHTOOL_A_PAUSE_MAX + 1] = { 144 [ETHTOOL_A_PAUSE_UNSPEC] = { .type = NLA_REJECT }, 145 [ETHTOOL_A_PAUSE_HEADER] = { .type = NLA_NESTED }, 146 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 }, 147 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 }, 148 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 }, 149 [ETHTOOL_A_PAUSE_STATS] = { .type = NLA_REJECT }, 150 }; 151 152 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info) 153 { 154 struct nlattr *tb[ETHTOOL_A_PAUSE_MAX + 1]; 155 struct ethtool_pauseparam params = {}; 156 struct ethnl_req_info req_info = {}; 157 const struct ethtool_ops *ops; 158 struct net_device *dev; 159 bool mod = false; 160 int ret; 161 162 ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb, ETHTOOL_A_PAUSE_MAX, 163 pause_set_policy, info->extack); 164 if (ret < 0) 165 return ret; 166 ret = ethnl_parse_header_dev_get(&req_info, 167 tb[ETHTOOL_A_PAUSE_HEADER], 168 genl_info_net(info), info->extack, 169 true); 170 if (ret < 0) 171 return ret; 172 dev = req_info.dev; 173 ops = dev->ethtool_ops; 174 ret = -EOPNOTSUPP; 175 if (!ops->get_pauseparam || !ops->set_pauseparam) 176 goto out_dev; 177 178 rtnl_lock(); 179 ret = ethnl_ops_begin(dev); 180 if (ret < 0) 181 goto out_rtnl; 182 ops->get_pauseparam(dev, ¶ms); 183 184 ethnl_update_bool32(¶ms.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod); 185 ethnl_update_bool32(¶ms.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod); 186 ethnl_update_bool32(¶ms.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod); 187 ret = 0; 188 if (!mod) 189 goto out_ops; 190 191 ret = dev->ethtool_ops->set_pauseparam(dev, ¶ms); 192 if (ret < 0) 193 goto out_ops; 194 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); 195 196 out_ops: 197 ethnl_ops_complete(dev); 198 out_rtnl: 199 rtnl_unlock(); 200 out_dev: 201 dev_put(dev); 202 return ret; 203 } 204