projects/linux·blob assoc-array-fixes · refs

222 lines · 6.2 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2
3#include "common.h"
4#include "netlink.h"
5
6struct pause_req_info {
7 struct ethnl_req_info base;
8 enum ethtool_mac_stats_src src;
9};
10
11#define PAUSE_REQINFO(__req_base) \
12 container_of(__req_base, struct pause_req_info, base)
13
14struct pause_reply_data {
15 struct ethnl_reply_data base;
16 struct ethtool_pauseparam pauseparam;
17 struct ethtool_pause_stats pausestat;
18};
19
20#define PAUSE_REPDATA(__reply_base) \
21 container_of(__reply_base, struct pause_reply_data, base)
22
23const struct nla_policy ethnl_pause_get_policy[] = {
24 [ETHTOOL_A_PAUSE_HEADER] =
25 NLA_POLICY_NESTED(ethnl_header_policy_stats),
26 [ETHTOOL_A_PAUSE_STATS_SRC] =
27 NLA_POLICY_MAX(NLA_U32, ETHTOOL_MAC_STATS_SRC_PMAC),
28};
29
30static int pause_parse_request(struct ethnl_req_info *req_base,
31 const struct genl_info *info,
32 struct nlattr **tb,
33 struct netlink_ext_ack *extack)
34{
35 enum ethtool_mac_stats_src src = ETHTOOL_MAC_STATS_SRC_AGGREGATE;
36 struct pause_req_info *req_info = PAUSE_REQINFO(req_base);
37
38 if (tb[ETHTOOL_A_PAUSE_STATS_SRC]) {
39 if (!(req_base->flags & ETHTOOL_FLAG_STATS)) {
40 NL_SET_ERR_MSG_MOD(extack,
41 "ETHTOOL_FLAG_STATS must be set when requesting a source of stats");
42 return -EINVAL;
43 }
44
45 src = nla_get_u32(tb[ETHTOOL_A_PAUSE_STATS_SRC]);
46 }
47
48 req_info->src = src;
49
50 return 0;
51}
52
53static int pause_prepare_data(const struct ethnl_req_info *req_base,
54 struct ethnl_reply_data *reply_base,
55 const struct genl_info *info)
56{
57 const struct pause_req_info *req_info = PAUSE_REQINFO(req_base);
58 struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
59 enum ethtool_mac_stats_src src = req_info->src;
60 struct net_device *dev = reply_base->dev;
61 int ret;
62
63 if (!dev->ethtool_ops->get_pauseparam)
64 return -EOPNOTSUPP;
65
66 ethtool_stats_init((u64 *)&data->pausestat,
67 sizeof(data->pausestat) / 8);
68 data->pausestat.src = src;
69
70 ret = ethnl_ops_begin(dev);
71 if (ret < 0)
72 return ret;
73
74 if ((src == ETHTOOL_MAC_STATS_SRC_EMAC ||
75 src == ETHTOOL_MAC_STATS_SRC_PMAC) &&
76 !__ethtool_dev_mm_supported(dev)) {
77 NL_SET_ERR_MSG_MOD(info->extack,
78 "Device does not support MAC merge layer");
79 ethnl_ops_complete(dev);
80 return -EOPNOTSUPP;
81 }
82
83 dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam);
84 if (req_base->flags & ETHTOOL_FLAG_STATS &&
85 dev->ethtool_ops->get_pause_stats)
86 dev->ethtool_ops->get_pause_stats(dev, &data->pausestat);
87
88 ethnl_ops_complete(dev);
89
90 return 0;
91}
92
93static int pause_reply_size(const struct ethnl_req_info *req_base,
94 const struct ethnl_reply_data *reply_base)
95{
96 int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */
97 nla_total_size(sizeof(u8)) + /* _PAUSE_RX */
98 nla_total_size(sizeof(u8)); /* _PAUSE_TX */
99
100 if (req_base->flags & ETHTOOL_FLAG_STATS)
101 n += nla_total_size(0) + /* _PAUSE_STATS */
102 nla_total_size(sizeof(u32)) + /* _PAUSE_STATS_SRC */
103 nla_total_size_64bit(sizeof(u64)) * ETHTOOL_PAUSE_STAT_CNT;
104 return n;
105}
106
107static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype,
108 u16 padtype)
109{
110 if (val == ETHTOOL_STAT_NOT_SET)
111 return 0;
112 if (nla_put_u64_64bit(skb, attrtype, val, padtype))
113 return -EMSGSIZE;
114
115 return 0;
116}
117
118static int pause_put_stats(struct sk_buff *skb,
119 const struct ethtool_pause_stats *pause_stats)
120{
121 const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD;
122 struct nlattr *nest;
123
124 if (nla_put_u32(skb, ETHTOOL_A_PAUSE_STATS_SRC, pause_stats->src))
125 return -EMSGSIZE;
126
127 nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS);
128 if (!nest)
129 return -EMSGSIZE;
130
131 if (ethtool_put_stat(skb, pause_stats->tx_pause_frames,
132 ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) ||
133 ethtool_put_stat(skb, pause_stats->rx_pause_frames,
134 ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad) ||
135 ethtool_put_stat(skb, pause_stats->tx_pause_storm_events,
136 ETHTOOL_A_PAUSE_STAT_TX_PAUSE_STORM_EVENTS, pad))
137 goto err_cancel;
138
139 nla_nest_end(skb, nest);
140 return 0;
141
142err_cancel:
143 nla_nest_cancel(skb, nest);
144 return -EMSGSIZE;
145}
146
147static int pause_fill_reply(struct sk_buff *skb,
148 const struct ethnl_req_info *req_base,
149 const struct ethnl_reply_data *reply_base)
150{
151 const struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
152 const struct ethtool_pauseparam *pauseparam = &data->pauseparam;
153
154 if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) ||
155 nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) ||
156 nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause))
157 return -EMSGSIZE;
158
159 if (req_base->flags & ETHTOOL_FLAG_STATS &&
160 pause_put_stats(skb, &data->pausestat))
161 return -EMSGSIZE;
162
163 return 0;
164}
165
166/* PAUSE_SET */
167
168const struct nla_policy ethnl_pause_set_policy[] = {
169 [ETHTOOL_A_PAUSE_HEADER] =
170 NLA_POLICY_NESTED(ethnl_header_policy),
171 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 },
172 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 },
173 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 },
174 [ETHTOOL_A_PAUSE_STATS_SRC] = { .type = NLA_REJECT },
175};
176
177static int
178ethnl_set_pause_validate(struct ethnl_req_info *req_info,
179 struct genl_info *info)
180{
181 const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
182
183 return ops->get_pauseparam && ops->set_pauseparam ? 1 : -EOPNOTSUPP;
184}
185
186static int
187ethnl_set_pause(struct ethnl_req_info *req_info, struct genl_info *info)
188{
189 struct net_device *dev = req_info->dev;
190 struct ethtool_pauseparam params = {};
191 struct nlattr **tb = info->attrs;
192 bool mod = false;
193 int ret;
194
195 dev->ethtool_ops->get_pauseparam(dev, &params);
196
197 ethnl_update_bool32(&params.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod);
198 ethnl_update_bool32(&params.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod);
199 ethnl_update_bool32(&params.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod);
200 if (!mod)
201 return 0;
202
203 ret = dev->ethtool_ops->set_pauseparam(dev, &params);
204 return ret < 0 ? ret : 1;
205}
206
207const struct ethnl_request_ops ethnl_pause_request_ops = {
208 .request_cmd = ETHTOOL_MSG_PAUSE_GET,
209 .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY,
210 .hdr_attr = ETHTOOL_A_PAUSE_HEADER,
211 .req_info_size = sizeof(struct pause_req_info),
212 .reply_data_size = sizeof(struct pause_reply_data),
213
214 .parse_request = pause_parse_request,
215 .prepare_data = pause_prepare_data,
216 .reply_size = pause_reply_size,
217 .fill_reply = pause_fill_reply,
218
219 .set_validate = ethnl_set_pause_validate,
220 .set = ethnl_set_pause,
221 .set_ntf_cmd = ETHTOOL_MSG_PAUSE_NTF,
222};