blob: 9009f412151e7878bed59ae4dec9d0b91382241a [file] [log] [blame]
Michal Kubecek7f59fb32020-03-28 00:01:23 +01001// SPDX-License-Identifier: GPL-2.0-only
2
3#include "netlink.h"
4#include "common.h"
5
6struct pause_req_info {
7 struct ethnl_req_info base;
8};
9
10struct pause_reply_data {
11 struct ethnl_reply_data base;
12 struct ethtool_pauseparam pauseparam;
Jakub Kicinski9a27a332020-09-14 17:11:52 -070013 struct ethtool_pause_stats pausestat;
Michal Kubecek7f59fb32020-03-28 00:01:23 +010014};
15
16#define PAUSE_REPDATA(__reply_base) \
17 container_of(__reply_base, struct pause_reply_data, base)
18
Jakub Kicinskiff419af2020-10-05 15:07:35 -070019const struct nla_policy ethnl_pause_get_policy[] = {
Jakub Kicinski329d9c32020-10-05 15:07:36 -070020 [ETHTOOL_A_PAUSE_HEADER] =
Jakub Kicinskia0de1cd2020-10-05 15:07:39 -070021 NLA_POLICY_NESTED(ethnl_header_policy_stats),
Michal Kubecek7f59fb32020-03-28 00:01:23 +010022};
23
24static int pause_prepare_data(const struct ethnl_req_info *req_base,
25 struct ethnl_reply_data *reply_base,
26 struct genl_info *info)
27{
28 struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
29 struct net_device *dev = reply_base->dev;
30 int ret;
31
32 if (!dev->ethtool_ops->get_pauseparam)
33 return -EOPNOTSUPP;
Jakub Kicinski9a27a332020-09-14 17:11:52 -070034
Jakub Kicinski16756d32021-04-13 20:46:14 -070035 ethtool_stats_init((u64 *)&data->pausestat,
36 sizeof(data->pausestat) / 8);
37
Michal Kubecek7f59fb32020-03-28 00:01:23 +010038 ret = ethnl_ops_begin(dev);
39 if (ret < 0)
40 return ret;
41 dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam);
Jakub Kicinski9a27a332020-09-14 17:11:52 -070042 if (req_base->flags & ETHTOOL_FLAG_STATS &&
Jakub Kicinski16756d32021-04-13 20:46:14 -070043 dev->ethtool_ops->get_pause_stats)
Jakub Kicinski9a27a332020-09-14 17:11:52 -070044 dev->ethtool_ops->get_pause_stats(dev, &data->pausestat);
Michal Kubecek7f59fb32020-03-28 00:01:23 +010045 ethnl_ops_complete(dev);
46
47 return 0;
48}
49
50static int pause_reply_size(const struct ethnl_req_info *req_base,
51 const struct ethnl_reply_data *reply_base)
52{
Jakub Kicinski9a27a332020-09-14 17:11:52 -070053 int n = nla_total_size(sizeof(u8)) + /* _PAUSE_AUTONEG */
Michal Kubecek7f59fb32020-03-28 00:01:23 +010054 nla_total_size(sizeof(u8)) + /* _PAUSE_RX */
55 nla_total_size(sizeof(u8)); /* _PAUSE_TX */
Jakub Kicinski9a27a332020-09-14 17:11:52 -070056
57 if (req_base->flags & ETHTOOL_FLAG_STATS)
58 n += nla_total_size(0) + /* _PAUSE_STATS */
59 nla_total_size_64bit(sizeof(u64)) *
60 (ETHTOOL_A_PAUSE_STAT_MAX - 2);
61 return n;
62}
63
64static int ethtool_put_stat(struct sk_buff *skb, u64 val, u16 attrtype,
65 u16 padtype)
66{
67 if (val == ETHTOOL_STAT_NOT_SET)
68 return 0;
69 if (nla_put_u64_64bit(skb, attrtype, val, padtype))
70 return -EMSGSIZE;
71
72 return 0;
73}
74
75static int pause_put_stats(struct sk_buff *skb,
76 const struct ethtool_pause_stats *pause_stats)
77{
78 const u16 pad = ETHTOOL_A_PAUSE_STAT_PAD;
79 struct nlattr *nest;
80
81 nest = nla_nest_start(skb, ETHTOOL_A_PAUSE_STATS);
82 if (!nest)
83 return -EMSGSIZE;
84
85 if (ethtool_put_stat(skb, pause_stats->tx_pause_frames,
86 ETHTOOL_A_PAUSE_STAT_TX_FRAMES, pad) ||
87 ethtool_put_stat(skb, pause_stats->rx_pause_frames,
88 ETHTOOL_A_PAUSE_STAT_RX_FRAMES, pad))
89 goto err_cancel;
90
91 nla_nest_end(skb, nest);
92 return 0;
93
94err_cancel:
95 nla_nest_cancel(skb, nest);
96 return -EMSGSIZE;
Michal Kubecek7f59fb32020-03-28 00:01:23 +010097}
98
99static int pause_fill_reply(struct sk_buff *skb,
100 const struct ethnl_req_info *req_base,
101 const struct ethnl_reply_data *reply_base)
102{
103 const struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
104 const struct ethtool_pauseparam *pauseparam = &data->pauseparam;
105
106 if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) ||
107 nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) ||
108 nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause))
109 return -EMSGSIZE;
110
Jakub Kicinski9a27a332020-09-14 17:11:52 -0700111 if (req_base->flags & ETHTOOL_FLAG_STATS &&
112 pause_put_stats(skb, &data->pausestat))
113 return -EMSGSIZE;
114
Michal Kubecek7f59fb32020-03-28 00:01:23 +0100115 return 0;
116}
117
118const struct ethnl_request_ops ethnl_pause_request_ops = {
119 .request_cmd = ETHTOOL_MSG_PAUSE_GET,
120 .reply_cmd = ETHTOOL_MSG_PAUSE_GET_REPLY,
121 .hdr_attr = ETHTOOL_A_PAUSE_HEADER,
Michal Kubecek7f59fb32020-03-28 00:01:23 +0100122 .req_info_size = sizeof(struct pause_req_info),
123 .reply_data_size = sizeof(struct pause_reply_data),
Michal Kubecek7f59fb32020-03-28 00:01:23 +0100124
125 .prepare_data = pause_prepare_data,
126 .reply_size = pause_reply_size,
127 .fill_reply = pause_fill_reply,
128};
Michal Kubecek3ab87992020-03-28 00:01:28 +0100129
130/* PAUSE_SET */
131
Jakub Kicinskiff419af2020-10-05 15:07:35 -0700132const struct nla_policy ethnl_pause_set_policy[] = {
Jakub Kicinski329d9c32020-10-05 15:07:36 -0700133 [ETHTOOL_A_PAUSE_HEADER] =
134 NLA_POLICY_NESTED(ethnl_header_policy),
Michal Kubecek3ab87992020-03-28 00:01:28 +0100135 [ETHTOOL_A_PAUSE_AUTONEG] = { .type = NLA_U8 },
136 [ETHTOOL_A_PAUSE_RX] = { .type = NLA_U8 },
137 [ETHTOOL_A_PAUSE_TX] = { .type = NLA_U8 },
138};
139
140int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info)
141{
Michal Kubecek3ab87992020-03-28 00:01:28 +0100142 struct ethtool_pauseparam params = {};
143 struct ethnl_req_info req_info = {};
Jakub Kicinski5028588b2020-10-05 15:07:34 -0700144 struct nlattr **tb = info->attrs;
Michal Kubecek3ab87992020-03-28 00:01:28 +0100145 const struct ethtool_ops *ops;
146 struct net_device *dev;
147 bool mod = false;
148 int ret;
149
Michal Kubecek3ab87992020-03-28 00:01:28 +0100150 ret = ethnl_parse_header_dev_get(&req_info,
151 tb[ETHTOOL_A_PAUSE_HEADER],
152 genl_info_net(info), info->extack,
153 true);
154 if (ret < 0)
155 return ret;
156 dev = req_info.dev;
157 ops = dev->ethtool_ops;
158 ret = -EOPNOTSUPP;
159 if (!ops->get_pauseparam || !ops->set_pauseparam)
160 goto out_dev;
161
162 rtnl_lock();
163 ret = ethnl_ops_begin(dev);
164 if (ret < 0)
165 goto out_rtnl;
166 ops->get_pauseparam(dev, &params);
167
168 ethnl_update_bool32(&params.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod);
169 ethnl_update_bool32(&params.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod);
170 ethnl_update_bool32(&params.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod);
171 ret = 0;
172 if (!mod)
173 goto out_ops;
174
175 ret = dev->ethtool_ops->set_pauseparam(dev, &params);
Michal Kubecekbf37faa2020-03-28 00:01:33 +0100176 if (ret < 0)
177 goto out_ops;
178 ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
Michal Kubecek3ab87992020-03-28 00:01:28 +0100179
180out_ops:
181 ethnl_ops_complete(dev);
182out_rtnl:
183 rtnl_unlock();
184out_dev:
185 dev_put(dev);
186 return ret;
187}