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 const struct nla_policy ethnl_pause_set_policy[ETHTOOL_A_PAUSE_MAX + 1] = { 143 [ETHTOOL_A_PAUSE_UNSPEC] = { .type = NLA_REJECT }, 144 [ETHTOOL_A_PAUSE_HEADER] = { .type = NLA_NESTED }, 145 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 }, 146 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 }, 147 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 }, 148 [ETHTOOL_A_PAUSE_STATS] = { .type = NLA_REJECT }, 149 }; 150 151 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info) 152 { 153 struct ethtool_pauseparam params = {}; 154 struct ethnl_req_info req_info = {}; 155 struct nlattr **tb = info->attrs; 156 const struct ethtool_ops *ops; 157 struct net_device *dev; 158 bool mod = false; 159 int ret; 160 161 ret = ethnl_parse_header_dev_get(&req_info, 162 tb[ETHTOOL_A_PAUSE_HEADER], 163 genl_info_net(info), info->extack, 164 true); 165 if (ret < 0) 166 return ret; 167 dev = req_info.dev; 168 ops = dev->ethtool_ops; 169 ret = -EOPNOTSUPP; 170 if (!ops->get_pauseparam || !ops->set_pauseparam) 171 goto out_dev; 172 173 rtnl_lock(); 174 ret = ethnl_ops_begin(dev); 175 if (ret < 0) 176 goto out_rtnl; 177 ops->get_pauseparam(dev, ¶ms); 178 179 ethnl_update_bool32(¶ms.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod); 180 ethnl_update_bool32(¶ms.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod); 181 ethnl_update_bool32(¶ms.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod); 182 ret = 0; 183 if (!mod) 184 goto out_ops; 185 186 ret = dev->ethtool_ops->set_pauseparam(dev, ¶ms); 187 if (ret < 0) 188 goto out_ops; 189 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL); 190 191 out_ops: 192 ethnl_ops_complete(dev); 193 out_rtnl: 194 rtnl_unlock(); 195 out_dev: 196 dev_put(dev); 197 return ret; 198 } 199