blob: f9949e051f49170814479c2bfa1d5015b4cbe3d1 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexander Duyck2f90b862008-11-20 20:52:10 -08002/*
Mark Rustad698e1d22011-03-14 09:01:02 +00003 * Copyright (c) 2008-2011, Intel Corporation.
Alexander Duyck2f90b862008-11-20 20:52:10 -08004 *
Paul Gortmaker36b9ad802015-10-07 17:27:44 -04005 * Description: Data Center Bridging netlink interface
Alexander Duyck2f90b862008-11-20 20:52:10 -08006 * Author: Lucy Liu <lucy.liu@intel.com>
7 */
8
9#include <linux/netdevice.h>
10#include <linux/netlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080012#include <net/netlink.h>
13#include <net/rtnetlink.h>
14#include <linux/dcbnl.h>
John Fastabend96b99682010-12-30 09:26:37 +000015#include <net/dcbevent.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080016#include <linux/rtnetlink.h>
Paul Gortmaker36b9ad802015-10-07 17:27:44 -040017#include <linux/init.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080018#include <net/sock.h>
19
Ben Hutchingsae86b9e2012-07-10 10:55:35 +000020/* Data Center Bridging (DCB) is a collection of Ethernet enhancements
Alexander Duyck2f90b862008-11-20 20:52:10 -080021 * intended to allow network traffic with differing requirements
22 * (highly reliable, no drops vs. best effort vs. low latency) to operate
23 * and co-exist on Ethernet. Current DCB features are:
24 *
25 * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
26 * framework for assigning bandwidth guarantees to traffic classes.
27 *
28 * Priority-based Flow Control (PFC) - provides a flow control mechanism which
29 * can work independently for each 802.1p priority.
30 *
31 * Congestion Notification - provides a mechanism for end-to-end congestion
32 * control for protocols which do not have built-in congestion management.
33 *
34 * More information about the emerging standards for these Ethernet features
35 * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
36 *
37 * This file implements an rtnetlink interface to allow configuration of DCB
38 * features for capable devices.
39 */
40
Alexander Duyck2f90b862008-11-20 20:52:10 -080041/**************** DCB attribute policies *************************************/
42
43/* DCB netlink attributes policy */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000044static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
Alexander Duyck859ee3c2008-11-20 21:10:23 -080045 [DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
46 [DCB_ATTR_STATE] = {.type = NLA_U8},
47 [DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},
48 [DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},
49 [DCB_ATTR_SET_ALL] = {.type = NLA_U8},
Alexander Duyck2f90b862008-11-20 20:52:10 -080050 [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
Alexander Duyck859ee3c2008-11-20 21:10:23 -080051 [DCB_ATTR_CAP] = {.type = NLA_NESTED},
52 [DCB_ATTR_PFC_STATE] = {.type = NLA_U8},
53 [DCB_ATTR_BCN] = {.type = NLA_NESTED},
Yi Zou6fa382a2009-08-31 12:33:20 +000054 [DCB_ATTR_APP] = {.type = NLA_NESTED},
John Fastabend3e290272010-12-30 09:25:46 +000055 [DCB_ATTR_IEEE] = {.type = NLA_NESTED},
Shmulik Ravid6241b622010-12-30 06:26:48 +000056 [DCB_ATTR_DCBX] = {.type = NLA_U8},
Shmulik Ravidea45fe42010-12-30 06:26:55 +000057 [DCB_ATTR_FEATCFG] = {.type = NLA_NESTED},
Alexander Duyck2f90b862008-11-20 20:52:10 -080058};
59
60/* DCB priority flow control to User Priority nested attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000061static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -080062 [DCB_PFC_UP_ATTR_0] = {.type = NLA_U8},
63 [DCB_PFC_UP_ATTR_1] = {.type = NLA_U8},
64 [DCB_PFC_UP_ATTR_2] = {.type = NLA_U8},
65 [DCB_PFC_UP_ATTR_3] = {.type = NLA_U8},
66 [DCB_PFC_UP_ATTR_4] = {.type = NLA_U8},
67 [DCB_PFC_UP_ATTR_5] = {.type = NLA_U8},
68 [DCB_PFC_UP_ATTR_6] = {.type = NLA_U8},
69 [DCB_PFC_UP_ATTR_7] = {.type = NLA_U8},
70 [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
71};
72
73/* DCB priority grouping nested attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000074static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -080075 [DCB_PG_ATTR_TC_0] = {.type = NLA_NESTED},
76 [DCB_PG_ATTR_TC_1] = {.type = NLA_NESTED},
77 [DCB_PG_ATTR_TC_2] = {.type = NLA_NESTED},
78 [DCB_PG_ATTR_TC_3] = {.type = NLA_NESTED},
79 [DCB_PG_ATTR_TC_4] = {.type = NLA_NESTED},
80 [DCB_PG_ATTR_TC_5] = {.type = NLA_NESTED},
81 [DCB_PG_ATTR_TC_6] = {.type = NLA_NESTED},
82 [DCB_PG_ATTR_TC_7] = {.type = NLA_NESTED},
83 [DCB_PG_ATTR_TC_ALL] = {.type = NLA_NESTED},
84 [DCB_PG_ATTR_BW_ID_0] = {.type = NLA_U8},
85 [DCB_PG_ATTR_BW_ID_1] = {.type = NLA_U8},
86 [DCB_PG_ATTR_BW_ID_2] = {.type = NLA_U8},
87 [DCB_PG_ATTR_BW_ID_3] = {.type = NLA_U8},
88 [DCB_PG_ATTR_BW_ID_4] = {.type = NLA_U8},
89 [DCB_PG_ATTR_BW_ID_5] = {.type = NLA_U8},
90 [DCB_PG_ATTR_BW_ID_6] = {.type = NLA_U8},
91 [DCB_PG_ATTR_BW_ID_7] = {.type = NLA_U8},
92 [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
93};
94
95/* DCB traffic class nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000096static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -080097 [DCB_TC_ATTR_PARAM_PGID] = {.type = NLA_U8},
98 [DCB_TC_ATTR_PARAM_UP_MAPPING] = {.type = NLA_U8},
99 [DCB_TC_ATTR_PARAM_STRICT_PRIO] = {.type = NLA_U8},
100 [DCB_TC_ATTR_PARAM_BW_PCT] = {.type = NLA_U8},
101 [DCB_TC_ATTR_PARAM_ALL] = {.type = NLA_FLAG},
102};
103
Alexander Duyck46132182008-11-20 21:05:08 -0800104/* DCB capabilities nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000105static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
Alexander Duyck46132182008-11-20 21:05:08 -0800106 [DCB_CAP_ATTR_ALL] = {.type = NLA_FLAG},
107 [DCB_CAP_ATTR_PG] = {.type = NLA_U8},
108 [DCB_CAP_ATTR_PFC] = {.type = NLA_U8},
109 [DCB_CAP_ATTR_UP2TC] = {.type = NLA_U8},
110 [DCB_CAP_ATTR_PG_TCS] = {.type = NLA_U8},
111 [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
112 [DCB_CAP_ATTR_GSP] = {.type = NLA_U8},
113 [DCB_CAP_ATTR_BCN] = {.type = NLA_U8},
Shmulik Ravid6241b622010-12-30 06:26:48 +0000114 [DCB_CAP_ATTR_DCBX] = {.type = NLA_U8},
Alexander Duyck46132182008-11-20 21:05:08 -0800115};
Alexander Duyck2f90b862008-11-20 20:52:10 -0800116
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800117/* DCB capabilities nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000118static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800119 [DCB_NUMTCS_ATTR_ALL] = {.type = NLA_FLAG},
120 [DCB_NUMTCS_ATTR_PG] = {.type = NLA_U8},
121 [DCB_NUMTCS_ATTR_PFC] = {.type = NLA_U8},
122};
123
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800124/* DCB BCN nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000125static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800126 [DCB_BCN_ATTR_RP_0] = {.type = NLA_U8},
127 [DCB_BCN_ATTR_RP_1] = {.type = NLA_U8},
128 [DCB_BCN_ATTR_RP_2] = {.type = NLA_U8},
129 [DCB_BCN_ATTR_RP_3] = {.type = NLA_U8},
130 [DCB_BCN_ATTR_RP_4] = {.type = NLA_U8},
131 [DCB_BCN_ATTR_RP_5] = {.type = NLA_U8},
132 [DCB_BCN_ATTR_RP_6] = {.type = NLA_U8},
133 [DCB_BCN_ATTR_RP_7] = {.type = NLA_U8},
134 [DCB_BCN_ATTR_RP_ALL] = {.type = NLA_FLAG},
Don Skidmoref4314e82008-12-21 20:10:29 -0800135 [DCB_BCN_ATTR_BCNA_0] = {.type = NLA_U32},
136 [DCB_BCN_ATTR_BCNA_1] = {.type = NLA_U32},
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800137 [DCB_BCN_ATTR_ALPHA] = {.type = NLA_U32},
138 [DCB_BCN_ATTR_BETA] = {.type = NLA_U32},
139 [DCB_BCN_ATTR_GD] = {.type = NLA_U32},
140 [DCB_BCN_ATTR_GI] = {.type = NLA_U32},
141 [DCB_BCN_ATTR_TMAX] = {.type = NLA_U32},
142 [DCB_BCN_ATTR_TD] = {.type = NLA_U32},
143 [DCB_BCN_ATTR_RMIN] = {.type = NLA_U32},
144 [DCB_BCN_ATTR_W] = {.type = NLA_U32},
145 [DCB_BCN_ATTR_RD] = {.type = NLA_U32},
146 [DCB_BCN_ATTR_RU] = {.type = NLA_U32},
147 [DCB_BCN_ATTR_WRTT] = {.type = NLA_U32},
148 [DCB_BCN_ATTR_RI] = {.type = NLA_U32},
149 [DCB_BCN_ATTR_C] = {.type = NLA_U32},
150 [DCB_BCN_ATTR_ALL] = {.type = NLA_FLAG},
151};
152
Yi Zou6fa382a2009-08-31 12:33:20 +0000153/* DCB APP nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000154static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
Yi Zou6fa382a2009-08-31 12:33:20 +0000155 [DCB_APP_ATTR_IDTYPE] = {.type = NLA_U8},
156 [DCB_APP_ATTR_ID] = {.type = NLA_U16},
157 [DCB_APP_ATTR_PRIORITY] = {.type = NLA_U8},
158};
159
John Fastabend3e290272010-12-30 09:25:46 +0000160/* IEEE 802.1Qaz nested attributes. */
161static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
162 [DCB_ATTR_IEEE_ETS] = {.len = sizeof(struct ieee_ets)},
163 [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
164 [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
Amir Vadai08f10af2012-04-04 21:33:30 +0000165 [DCB_ATTR_IEEE_MAXRATE] = {.len = sizeof(struct ieee_maxrate)},
Shani Michaelic93682472015-03-05 20:16:11 +0200166 [DCB_ATTR_IEEE_QCN] = {.len = sizeof(struct ieee_qcn)},
167 [DCB_ATTR_IEEE_QCN_STATS] = {.len = sizeof(struct ieee_qcn_stats)},
Huy Nguyene549f6f2018-02-22 11:57:10 -0600168 [DCB_ATTR_DCB_BUFFER] = {.len = sizeof(struct dcbnl_buffer)},
Daniel Machon6182d582022-11-01 10:48:30 +0100169 [DCB_ATTR_DCB_APP_TRUST_TABLE] = {.type = NLA_NESTED},
John Fastabend3e290272010-12-30 09:25:46 +0000170};
171
Shmulik Ravidea45fe42010-12-30 06:26:55 +0000172/* DCB number of traffic classes nested attributes. */
173static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
174 [DCB_FEATCFG_ATTR_ALL] = {.type = NLA_FLAG},
175 [DCB_FEATCFG_ATTR_PG] = {.type = NLA_U8},
176 [DCB_FEATCFG_ATTR_PFC] = {.type = NLA_U8},
177 [DCB_FEATCFG_ATTR_APP] = {.type = NLA_U8},
178};
179
John Fastabend9ab933a2010-12-30 09:26:31 +0000180static LIST_HEAD(dcb_app_list);
181static DEFINE_SPINLOCK(dcb_lock);
182
Daniel Machonec32c0c2022-11-01 10:48:29 +0100183static enum ieee_attrs_app dcbnl_app_attr_type_get(u8 selector)
184{
185 switch (selector) {
186 case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
187 case IEEE_8021QAZ_APP_SEL_STREAM:
188 case IEEE_8021QAZ_APP_SEL_DGRAM:
189 case IEEE_8021QAZ_APP_SEL_ANY:
190 case IEEE_8021QAZ_APP_SEL_DSCP:
191 return DCB_ATTR_IEEE_APP;
192 case DCB_APP_SEL_PCP:
193 return DCB_ATTR_DCB_APP;
194 default:
195 return DCB_ATTR_IEEE_APP_UNSPEC;
196 }
197}
198
199static bool dcbnl_app_attr_type_validate(enum ieee_attrs_app type)
200{
201 switch (type) {
202 case DCB_ATTR_IEEE_APP:
203 case DCB_ATTR_DCB_APP:
204 return true;
205 default:
206 return false;
207 }
208}
209
210static bool dcbnl_app_selector_validate(enum ieee_attrs_app type, u8 selector)
211{
212 return dcbnl_app_attr_type_get(selector) == type;
213}
214
Thomas Graf33a03aa2012-06-13 02:54:54 +0000215static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
216 u32 flags, struct nlmsghdr **nlhp)
217{
218 struct sk_buff *skb;
219 struct dcbmsg *dcb;
220 struct nlmsghdr *nlh;
221
222 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
223 if (!skb)
224 return NULL;
225
226 nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
Thomas Grafb3908e22012-06-13 22:40:15 +0000227 BUG_ON(!nlh);
Thomas Graf33a03aa2012-06-13 02:54:54 +0000228
229 dcb = nlmsg_data(nlh);
230 dcb->dcb_family = AF_UNSPEC;
231 dcb->cmd = cmd;
232 dcb->dcb_pad = 0;
233
234 if (nlhp)
235 *nlhp = nlh;
236
237 return skb;
238}
239
Thomas Graf7be99412012-06-13 02:54:55 +0000240static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
241 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800242{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800243 /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
244 if (!netdev->dcbnl_ops->getstate)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000245 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800246
Thomas Graf7be99412012-06-13 02:54:55 +0000247 return nla_put_u8(skb, DCB_ATTR_STATE,
248 netdev->dcbnl_ops->getstate(netdev));
Alexander Duyck2f90b862008-11-20 20:52:10 -0800249}
250
Thomas Graf7be99412012-06-13 02:54:55 +0000251static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
252 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800253{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800254 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
255 u8 value;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000256 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800257 int i;
258 int getall = 0;
259
Thomas Graf3d1f4862012-06-13 02:54:58 +0000260 if (!tb[DCB_ATTR_PFC_CFG])
261 return -EINVAL;
262
263 if (!netdev->dcbnl_ops->getpfccfg)
264 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800265
Johannes Berg8cb08172019-04-26 14:07:28 +0200266 ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
267 tb[DCB_ATTR_PFC_CFG],
268 dcbnl_pfc_up_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800269 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000270 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800271
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200272 nest = nla_nest_start_noflag(skb, DCB_ATTR_PFC_CFG);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800273 if (!nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000274 return -EMSGSIZE;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800275
276 if (data[DCB_PFC_UP_ATTR_ALL])
277 getall = 1;
278
279 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
280 if (!getall && !data[i])
281 continue;
282
283 netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
284 &value);
Thomas Graf7be99412012-06-13 02:54:55 +0000285 ret = nla_put_u8(skb, i, value);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800286 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000287 nla_nest_cancel(skb, nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000288 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800289 }
290 }
Thomas Graf7be99412012-06-13 02:54:55 +0000291 nla_nest_end(skb, nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800292
293 return 0;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800294}
295
Thomas Graf7be99412012-06-13 02:54:55 +0000296static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
297 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800298{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800299 u8 perm_addr[MAX_ADDR_LEN];
Alexander Duyck2f90b862008-11-20 20:52:10 -0800300
301 if (!netdev->dcbnl_ops->getpermhwaddr)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000302 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800303
Mathias Krause29cd8ae2013-03-09 05:52:21 +0000304 memset(perm_addr, 0, sizeof(perm_addr));
Alexander Duyck2f90b862008-11-20 20:52:10 -0800305 netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
306
Thomas Graf7be99412012-06-13 02:54:55 +0000307 return nla_put(skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr), perm_addr);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800308}
309
Thomas Graf7be99412012-06-13 02:54:55 +0000310static int dcbnl_getcap(struct net_device *netdev, struct nlmsghdr *nlh,
311 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck46132182008-11-20 21:05:08 -0800312{
Alexander Duyck46132182008-11-20 21:05:08 -0800313 struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
314 u8 value;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000315 int ret;
Alexander Duyck46132182008-11-20 21:05:08 -0800316 int i;
317 int getall = 0;
318
Thomas Graf3d1f4862012-06-13 02:54:58 +0000319 if (!tb[DCB_ATTR_CAP])
320 return -EINVAL;
321
322 if (!netdev->dcbnl_ops->getcap)
323 return -EOPNOTSUPP;
Alexander Duyck46132182008-11-20 21:05:08 -0800324
Johannes Berg8cb08172019-04-26 14:07:28 +0200325 ret = nla_parse_nested_deprecated(data, DCB_CAP_ATTR_MAX,
326 tb[DCB_ATTR_CAP], dcbnl_cap_nest,
327 NULL);
Alexander Duyck46132182008-11-20 21:05:08 -0800328 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000329 return ret;
Alexander Duyck46132182008-11-20 21:05:08 -0800330
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200331 nest = nla_nest_start_noflag(skb, DCB_ATTR_CAP);
Alexander Duyck46132182008-11-20 21:05:08 -0800332 if (!nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000333 return -EMSGSIZE;
Alexander Duyck46132182008-11-20 21:05:08 -0800334
335 if (data[DCB_CAP_ATTR_ALL])
336 getall = 1;
337
338 for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
339 if (!getall && !data[i])
340 continue;
341
342 if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
Thomas Graf7be99412012-06-13 02:54:55 +0000343 ret = nla_put_u8(skb, i, value);
Alexander Duyck46132182008-11-20 21:05:08 -0800344 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000345 nla_nest_cancel(skb, nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000346 return ret;
Alexander Duyck46132182008-11-20 21:05:08 -0800347 }
348 }
349 }
Thomas Graf7be99412012-06-13 02:54:55 +0000350 nla_nest_end(skb, nest);
Alexander Duyck46132182008-11-20 21:05:08 -0800351
352 return 0;
Alexander Duyck46132182008-11-20 21:05:08 -0800353}
354
Thomas Graf7be99412012-06-13 02:54:55 +0000355static int dcbnl_getnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
356 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800357{
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800358 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
359 u8 value;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000360 int ret;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800361 int i;
362 int getall = 0;
363
Thomas Graf3d1f4862012-06-13 02:54:58 +0000364 if (!tb[DCB_ATTR_NUMTCS])
365 return -EINVAL;
366
367 if (!netdev->dcbnl_ops->getnumtcs)
368 return -EOPNOTSUPP;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800369
Johannes Berg8cb08172019-04-26 14:07:28 +0200370 ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
371 tb[DCB_ATTR_NUMTCS],
372 dcbnl_numtcs_nest, NULL);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000373 if (ret)
374 return ret;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800375
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200376 nest = nla_nest_start_noflag(skb, DCB_ATTR_NUMTCS);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000377 if (!nest)
378 return -EMSGSIZE;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800379
380 if (data[DCB_NUMTCS_ATTR_ALL])
381 getall = 1;
382
383 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
384 if (!getall && !data[i])
385 continue;
386
387 ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
388 if (!ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000389 ret = nla_put_u8(skb, i, value);
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800390 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000391 nla_nest_cancel(skb, nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000392 return ret;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800393 }
Thomas Graf3d1f4862012-06-13 02:54:58 +0000394 } else
395 return -EINVAL;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800396 }
Thomas Graf7be99412012-06-13 02:54:55 +0000397 nla_nest_end(skb, nest);
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800398
399 return 0;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800400}
401
Thomas Graf7be99412012-06-13 02:54:55 +0000402static int dcbnl_setnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
403 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800404{
405 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
Thomas Graf3d1f4862012-06-13 02:54:58 +0000406 int ret;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800407 u8 value;
408 int i;
409
Thomas Graf3d1f4862012-06-13 02:54:58 +0000410 if (!tb[DCB_ATTR_NUMTCS])
411 return -EINVAL;
412
413 if (!netdev->dcbnl_ops->setnumtcs)
414 return -EOPNOTSUPP;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800415
Johannes Berg8cb08172019-04-26 14:07:28 +0200416 ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
417 tb[DCB_ATTR_NUMTCS],
418 dcbnl_numtcs_nest, NULL);
Thomas Graf7be99412012-06-13 02:54:55 +0000419 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000420 return ret;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800421
422 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
423 if (data[i] == NULL)
424 continue;
425
426 value = nla_get_u8(data[i]);
427
428 ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800429 if (ret)
Thomas Graf7be99412012-06-13 02:54:55 +0000430 break;
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800431 }
432
Thomas Graf7be99412012-06-13 02:54:55 +0000433 return nla_put_u8(skb, DCB_ATTR_NUMTCS, !!ret);
Alexander Duyck33dbabc42008-11-20 21:08:19 -0800434}
435
Thomas Graf7be99412012-06-13 02:54:55 +0000436static int dcbnl_getpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
437 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800438{
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800439 if (!netdev->dcbnl_ops->getpfcstate)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000440 return -EOPNOTSUPP;
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800441
Thomas Graf7be99412012-06-13 02:54:55 +0000442 return nla_put_u8(skb, DCB_ATTR_PFC_STATE,
443 netdev->dcbnl_ops->getpfcstate(netdev));
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800444}
445
Thomas Graf7be99412012-06-13 02:54:55 +0000446static int dcbnl_setpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
447 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800448{
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800449 u8 value;
450
Thomas Graf3d1f4862012-06-13 02:54:58 +0000451 if (!tb[DCB_ATTR_PFC_STATE])
Thomas Graf7be99412012-06-13 02:54:55 +0000452 return -EINVAL;
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800453
Thomas Graf3d1f4862012-06-13 02:54:58 +0000454 if (!netdev->dcbnl_ops->setpfcstate)
455 return -EOPNOTSUPP;
456
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800457 value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
458
459 netdev->dcbnl_ops->setpfcstate(netdev, value);
460
Thomas Graf7be99412012-06-13 02:54:55 +0000461 return nla_put_u8(skb, DCB_ATTR_PFC_STATE, 0);
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800462}
463
Thomas Graf7be99412012-06-13 02:54:55 +0000464static int dcbnl_getapp(struct net_device *netdev, struct nlmsghdr *nlh,
465 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Yi Zou57949682009-08-31 12:33:40 +0000466{
Yi Zou57949682009-08-31 12:33:40 +0000467 struct nlattr *app_nest;
468 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
469 u16 id;
470 u8 up, idtype;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000471 int ret;
Yi Zou57949682009-08-31 12:33:40 +0000472
John Fastabend3dce38a2011-01-21 16:35:18 +0000473 if (!tb[DCB_ATTR_APP])
Thomas Graf3d1f4862012-06-13 02:54:58 +0000474 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000475
Johannes Berg8cb08172019-04-26 14:07:28 +0200476 ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
477 tb[DCB_ATTR_APP], dcbnl_app_nest,
478 NULL);
Yi Zou57949682009-08-31 12:33:40 +0000479 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000480 return ret;
Yi Zou57949682009-08-31 12:33:40 +0000481
Yi Zou57949682009-08-31 12:33:40 +0000482 /* all must be non-null */
483 if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
484 (!app_tb[DCB_APP_ATTR_ID]))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000485 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000486
487 /* either by eth type or by socket number */
488 idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
489 if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
490 (idtype != DCB_APP_IDTYPE_PORTNUM))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000491 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000492
493 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
John Fastabend3dce38a2011-01-21 16:35:18 +0000494
495 if (netdev->dcbnl_ops->getapp) {
Anish Bhattc2659472014-07-16 22:32:39 -0700496 ret = netdev->dcbnl_ops->getapp(netdev, idtype, id);
497 if (ret < 0)
498 return ret;
499 else
500 up = ret;
John Fastabend3dce38a2011-01-21 16:35:18 +0000501 } else {
502 struct dcb_app app = {
503 .selector = idtype,
504 .protocol = id,
505 };
506 up = dcb_getapp(netdev, &app);
507 }
Yi Zou57949682009-08-31 12:33:40 +0000508
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200509 app_nest = nla_nest_start_noflag(skb, DCB_ATTR_APP);
Thomas Graf7be99412012-06-13 02:54:55 +0000510 if (!app_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000511 return -EMSGSIZE;
Yi Zou57949682009-08-31 12:33:40 +0000512
Thomas Graf7be99412012-06-13 02:54:55 +0000513 ret = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE, idtype);
Yi Zou57949682009-08-31 12:33:40 +0000514 if (ret)
515 goto out_cancel;
516
Thomas Graf7be99412012-06-13 02:54:55 +0000517 ret = nla_put_u16(skb, DCB_APP_ATTR_ID, id);
Yi Zou57949682009-08-31 12:33:40 +0000518 if (ret)
519 goto out_cancel;
520
Thomas Graf7be99412012-06-13 02:54:55 +0000521 ret = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY, up);
Yi Zou57949682009-08-31 12:33:40 +0000522 if (ret)
523 goto out_cancel;
524
Thomas Graf7be99412012-06-13 02:54:55 +0000525 nla_nest_end(skb, app_nest);
Yi Zou57949682009-08-31 12:33:40 +0000526
Thomas Graf3d1f4862012-06-13 02:54:58 +0000527 return 0;
Yi Zou57949682009-08-31 12:33:40 +0000528
529out_cancel:
Thomas Graf7be99412012-06-13 02:54:55 +0000530 nla_nest_cancel(skb, app_nest);
Yi Zou57949682009-08-31 12:33:40 +0000531 return ret;
532}
533
Thomas Graf7be99412012-06-13 02:54:55 +0000534static int dcbnl_setapp(struct net_device *netdev, struct nlmsghdr *nlh,
535 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Yi Zou57949682009-08-31 12:33:40 +0000536{
Thomas Graf3d1f4862012-06-13 02:54:58 +0000537 int ret;
Yi Zou57949682009-08-31 12:33:40 +0000538 u16 id;
539 u8 up, idtype;
540 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
541
John Fastabend9ab933a2010-12-30 09:26:31 +0000542 if (!tb[DCB_ATTR_APP])
Thomas Graf3d1f4862012-06-13 02:54:58 +0000543 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000544
Johannes Berg8cb08172019-04-26 14:07:28 +0200545 ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
546 tb[DCB_ATTR_APP], dcbnl_app_nest,
547 NULL);
Yi Zou57949682009-08-31 12:33:40 +0000548 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000549 return ret;
Yi Zou57949682009-08-31 12:33:40 +0000550
Yi Zou57949682009-08-31 12:33:40 +0000551 /* all must be non-null */
552 if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
553 (!app_tb[DCB_APP_ATTR_ID]) ||
554 (!app_tb[DCB_APP_ATTR_PRIORITY]))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000555 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000556
557 /* either by eth type or by socket number */
558 idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
559 if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
560 (idtype != DCB_APP_IDTYPE_PORTNUM))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000561 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000562
563 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
564 up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
565
John Fastabend9ab933a2010-12-30 09:26:31 +0000566 if (netdev->dcbnl_ops->setapp) {
Thomas Graf3d1f4862012-06-13 02:54:58 +0000567 ret = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
Anish Bhattc2659472014-07-16 22:32:39 -0700568 if (ret < 0)
569 return ret;
John Fastabend9ab933a2010-12-30 09:26:31 +0000570 } else {
571 struct dcb_app app;
572 app.selector = idtype;
573 app.protocol = id;
574 app.priority = up;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000575 ret = dcb_setapp(netdev, &app);
John Fastabend9ab933a2010-12-30 09:26:31 +0000576 }
577
Thomas Graf7be99412012-06-13 02:54:55 +0000578 ret = nla_put_u8(skb, DCB_ATTR_APP, ret);
John Fastabend08157982012-04-20 09:49:23 +0000579 dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SAPP, seq, 0);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000580
Yi Zou57949682009-08-31 12:33:40 +0000581 return ret;
582}
583
Thomas Graf7be99412012-06-13 02:54:55 +0000584static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
585 struct nlattr **tb, struct sk_buff *skb, int dir)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800586{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800587 struct nlattr *pg_nest, *param_nest, *data;
588 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
589 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
590 u8 prio, pgid, tc_pct, up_map;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000591 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800592 int getall = 0;
593 int i;
594
Thomas Graf3d1f4862012-06-13 02:54:58 +0000595 if (!tb[DCB_ATTR_PG_CFG])
596 return -EINVAL;
597
598 if (!netdev->dcbnl_ops->getpgtccfgtx ||
Alexander Duyck2f90b862008-11-20 20:52:10 -0800599 !netdev->dcbnl_ops->getpgtccfgrx ||
600 !netdev->dcbnl_ops->getpgbwgcfgtx ||
601 !netdev->dcbnl_ops->getpgbwgcfgrx)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000602 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800603
Johannes Berg8cb08172019-04-26 14:07:28 +0200604 ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
605 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
606 NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800607 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000608 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800609
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200610 pg_nest = nla_nest_start_noflag(skb, DCB_ATTR_PG_CFG);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800611 if (!pg_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000612 return -EMSGSIZE;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800613
614 if (pg_tb[DCB_PG_ATTR_TC_ALL])
615 getall = 1;
616
617 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
618 if (!getall && !pg_tb[i])
619 continue;
620
621 if (pg_tb[DCB_PG_ATTR_TC_ALL])
622 data = pg_tb[DCB_PG_ATTR_TC_ALL];
623 else
624 data = pg_tb[i];
Johannes Berg8cb08172019-04-26 14:07:28 +0200625 ret = nla_parse_nested_deprecated(param_tb,
626 DCB_TC_ATTR_PARAM_MAX, data,
627 dcbnl_tc_param_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800628 if (ret)
629 goto err_pg;
630
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200631 param_nest = nla_nest_start_noflag(skb, i);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800632 if (!param_nest)
633 goto err_pg;
634
635 pgid = DCB_ATTR_VALUE_UNDEFINED;
636 prio = DCB_ATTR_VALUE_UNDEFINED;
637 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
638 up_map = DCB_ATTR_VALUE_UNDEFINED;
639
640 if (dir) {
641 /* Rx */
642 netdev->dcbnl_ops->getpgtccfgrx(netdev,
643 i - DCB_PG_ATTR_TC_0, &prio,
644 &pgid, &tc_pct, &up_map);
645 } else {
646 /* Tx */
647 netdev->dcbnl_ops->getpgtccfgtx(netdev,
648 i - DCB_PG_ATTR_TC_0, &prio,
649 &pgid, &tc_pct, &up_map);
650 }
651
652 if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
653 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000654 ret = nla_put_u8(skb,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800655 DCB_TC_ATTR_PARAM_PGID, pgid);
656 if (ret)
657 goto err_param;
658 }
659 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
660 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000661 ret = nla_put_u8(skb,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800662 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
663 if (ret)
664 goto err_param;
665 }
666 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
667 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000668 ret = nla_put_u8(skb,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800669 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
670 if (ret)
671 goto err_param;
672 }
673 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
674 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000675 ret = nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800676 tc_pct);
677 if (ret)
678 goto err_param;
679 }
Thomas Graf7be99412012-06-13 02:54:55 +0000680 nla_nest_end(skb, param_nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800681 }
682
683 if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
684 getall = 1;
685 else
686 getall = 0;
687
688 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
689 if (!getall && !pg_tb[i])
690 continue;
691
692 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
693
694 if (dir) {
695 /* Rx */
696 netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
697 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
698 } else {
699 /* Tx */
700 netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
701 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
702 }
Thomas Graf7be99412012-06-13 02:54:55 +0000703 ret = nla_put_u8(skb, i, tc_pct);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800704 if (ret)
705 goto err_pg;
706 }
707
Thomas Graf7be99412012-06-13 02:54:55 +0000708 nla_nest_end(skb, pg_nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800709
710 return 0;
711
712err_param:
Thomas Graf7be99412012-06-13 02:54:55 +0000713 nla_nest_cancel(skb, param_nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800714err_pg:
Thomas Graf7be99412012-06-13 02:54:55 +0000715 nla_nest_cancel(skb, pg_nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000716
717 return -EMSGSIZE;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800718}
719
Thomas Graf7be99412012-06-13 02:54:55 +0000720static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
721 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800722{
Thomas Graf7be99412012-06-13 02:54:55 +0000723 return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800724}
725
Thomas Graf7be99412012-06-13 02:54:55 +0000726static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
727 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800728{
Thomas Graf7be99412012-06-13 02:54:55 +0000729 return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 1);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800730}
731
Thomas Graf7be99412012-06-13 02:54:55 +0000732static int dcbnl_setstate(struct net_device *netdev, struct nlmsghdr *nlh,
733 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800734{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800735 u8 value;
736
Thomas Graf3d1f4862012-06-13 02:54:58 +0000737 if (!tb[DCB_ATTR_STATE])
Thomas Graf7be99412012-06-13 02:54:55 +0000738 return -EINVAL;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800739
Thomas Graf3d1f4862012-06-13 02:54:58 +0000740 if (!netdev->dcbnl_ops->setstate)
741 return -EOPNOTSUPP;
742
Alexander Duyck2f90b862008-11-20 20:52:10 -0800743 value = nla_get_u8(tb[DCB_ATTR_STATE]);
744
Thomas Graf7be99412012-06-13 02:54:55 +0000745 return nla_put_u8(skb, DCB_ATTR_STATE,
746 netdev->dcbnl_ops->setstate(netdev, value));
Alexander Duyck2f90b862008-11-20 20:52:10 -0800747}
748
Thomas Graf7be99412012-06-13 02:54:55 +0000749static int dcbnl_setpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
750 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800751{
752 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
753 int i;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000754 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800755 u8 value;
756
Thomas Graf3d1f4862012-06-13 02:54:58 +0000757 if (!tb[DCB_ATTR_PFC_CFG])
758 return -EINVAL;
759
760 if (!netdev->dcbnl_ops->setpfccfg)
761 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800762
Johannes Berg8cb08172019-04-26 14:07:28 +0200763 ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
764 tb[DCB_ATTR_PFC_CFG],
765 dcbnl_pfc_up_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800766 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000767 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800768
769 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
770 if (data[i] == NULL)
771 continue;
772 value = nla_get_u8(data[i]);
773 netdev->dcbnl_ops->setpfccfg(netdev,
774 data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
775 }
776
Thomas Graf7be99412012-06-13 02:54:55 +0000777 return nla_put_u8(skb, DCB_ATTR_PFC_CFG, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800778}
779
Thomas Graf7be99412012-06-13 02:54:55 +0000780static int dcbnl_setall(struct net_device *netdev, struct nlmsghdr *nlh,
781 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800782{
Thomas Graf3d1f4862012-06-13 02:54:58 +0000783 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800784
Thomas Graf3d1f4862012-06-13 02:54:58 +0000785 if (!tb[DCB_ATTR_SET_ALL])
786 return -EINVAL;
787
788 if (!netdev->dcbnl_ops->setall)
789 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800790
Thomas Graf7be99412012-06-13 02:54:55 +0000791 ret = nla_put_u8(skb, DCB_ATTR_SET_ALL,
792 netdev->dcbnl_ops->setall(netdev));
John Fastabend08157982012-04-20 09:49:23 +0000793 dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SET_ALL, seq, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800794
795 return ret;
796}
797
Thomas Graf7be99412012-06-13 02:54:55 +0000798static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
799 u32 seq, struct nlattr **tb, struct sk_buff *skb,
800 int dir)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800801{
802 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
803 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
Thomas Graf3d1f4862012-06-13 02:54:58 +0000804 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800805 int i;
806 u8 pgid;
807 u8 up_map;
808 u8 prio;
809 u8 tc_pct;
810
Thomas Graf3d1f4862012-06-13 02:54:58 +0000811 if (!tb[DCB_ATTR_PG_CFG])
812 return -EINVAL;
813
814 if (!netdev->dcbnl_ops->setpgtccfgtx ||
Alexander Duyck2f90b862008-11-20 20:52:10 -0800815 !netdev->dcbnl_ops->setpgtccfgrx ||
816 !netdev->dcbnl_ops->setpgbwgcfgtx ||
817 !netdev->dcbnl_ops->setpgbwgcfgrx)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000818 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800819
Johannes Berg8cb08172019-04-26 14:07:28 +0200820 ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
821 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
822 NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800823 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000824 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800825
826 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
827 if (!pg_tb[i])
828 continue;
829
Johannes Berg8cb08172019-04-26 14:07:28 +0200830 ret = nla_parse_nested_deprecated(param_tb,
831 DCB_TC_ATTR_PARAM_MAX,
832 pg_tb[i],
833 dcbnl_tc_param_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800834 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000835 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800836
837 pgid = DCB_ATTR_VALUE_UNDEFINED;
838 prio = DCB_ATTR_VALUE_UNDEFINED;
839 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
840 up_map = DCB_ATTR_VALUE_UNDEFINED;
841
842 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
843 prio =
844 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
845
846 if (param_tb[DCB_TC_ATTR_PARAM_PGID])
847 pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
848
849 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
850 tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
851
852 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
853 up_map =
854 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
855
856 /* dir: Tx = 0, Rx = 1 */
857 if (dir) {
858 /* Rx */
859 netdev->dcbnl_ops->setpgtccfgrx(netdev,
860 i - DCB_PG_ATTR_TC_0,
861 prio, pgid, tc_pct, up_map);
862 } else {
863 /* Tx */
864 netdev->dcbnl_ops->setpgtccfgtx(netdev,
865 i - DCB_PG_ATTR_TC_0,
866 prio, pgid, tc_pct, up_map);
867 }
868 }
869
870 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
871 if (!pg_tb[i])
872 continue;
873
874 tc_pct = nla_get_u8(pg_tb[i]);
875
876 /* dir: Tx = 0, Rx = 1 */
877 if (dir) {
878 /* Rx */
879 netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
880 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
881 } else {
882 /* Tx */
883 netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
884 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
885 }
886 }
887
John Fastabendbb1dfef2012-06-20 19:56:21 +0000888 return nla_put_u8(skb, DCB_ATTR_PG_CFG, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800889}
890
Thomas Graf7be99412012-06-13 02:54:55 +0000891static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
892 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800893{
Thomas Graf7be99412012-06-13 02:54:55 +0000894 return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800895}
896
Thomas Graf7be99412012-06-13 02:54:55 +0000897static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
898 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800899{
Thomas Graf7be99412012-06-13 02:54:55 +0000900 return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800901}
902
Thomas Graf7be99412012-06-13 02:54:55 +0000903static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
904 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800905{
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800906 struct nlattr *bcn_nest;
907 struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
908 u8 value_byte;
909 u32 value_integer;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000910 int ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800911 bool getall = false;
912 int i;
913
Thomas Graf3d1f4862012-06-13 02:54:58 +0000914 if (!tb[DCB_ATTR_BCN])
915 return -EINVAL;
916
917 if (!netdev->dcbnl_ops->getbcnrp ||
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800918 !netdev->dcbnl_ops->getbcncfg)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000919 return -EOPNOTSUPP;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800920
Johannes Berg8cb08172019-04-26 14:07:28 +0200921 ret = nla_parse_nested_deprecated(bcn_tb, DCB_BCN_ATTR_MAX,
922 tb[DCB_ATTR_BCN], dcbnl_bcn_nest,
923 NULL);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800924 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000925 return ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800926
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200927 bcn_nest = nla_nest_start_noflag(skb, DCB_ATTR_BCN);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800928 if (!bcn_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000929 return -EMSGSIZE;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800930
931 if (bcn_tb[DCB_BCN_ATTR_ALL])
932 getall = true;
933
934 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
935 if (!getall && !bcn_tb[i])
936 continue;
937
938 netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
939 &value_byte);
Thomas Graf7be99412012-06-13 02:54:55 +0000940 ret = nla_put_u8(skb, i, value_byte);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800941 if (ret)
942 goto err_bcn;
943 }
944
Don Skidmoref4314e82008-12-21 20:10:29 -0800945 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800946 if (!getall && !bcn_tb[i])
947 continue;
948
949 netdev->dcbnl_ops->getbcncfg(netdev, i,
950 &value_integer);
Thomas Graf7be99412012-06-13 02:54:55 +0000951 ret = nla_put_u32(skb, i, value_integer);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800952 if (ret)
953 goto err_bcn;
954 }
955
Thomas Graf7be99412012-06-13 02:54:55 +0000956 nla_nest_end(skb, bcn_nest);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800957
958 return 0;
959
960err_bcn:
Thomas Graf7be99412012-06-13 02:54:55 +0000961 nla_nest_cancel(skb, bcn_nest);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800962 return ret;
963}
964
Thomas Graf7be99412012-06-13 02:54:55 +0000965static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
966 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800967{
968 struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
969 int i;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000970 int ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800971 u8 value_byte;
972 u32 value_int;
973
Thomas Graf3d1f4862012-06-13 02:54:58 +0000974 if (!tb[DCB_ATTR_BCN])
975 return -EINVAL;
976
977 if (!netdev->dcbnl_ops->setbcncfg ||
Joe Perchesf64f9e72009-11-29 16:55:45 -0800978 !netdev->dcbnl_ops->setbcnrp)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000979 return -EOPNOTSUPP;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800980
Johannes Berg8cb08172019-04-26 14:07:28 +0200981 ret = nla_parse_nested_deprecated(data, DCB_BCN_ATTR_MAX,
982 tb[DCB_ATTR_BCN], dcbnl_pfc_up_nest,
983 NULL);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800984 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000985 return ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800986
987 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
988 if (data[i] == NULL)
989 continue;
990 value_byte = nla_get_u8(data[i]);
991 netdev->dcbnl_ops->setbcnrp(netdev,
992 data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
993 }
994
Don Skidmoref4314e82008-12-21 20:10:29 -0800995 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800996 if (data[i] == NULL)
997 continue;
998 value_int = nla_get_u32(data[i]);
999 netdev->dcbnl_ops->setbcncfg(netdev,
1000 i, value_int);
1001 }
1002
Thomas Graf3d1f4862012-06-13 02:54:58 +00001003 return nla_put_u8(skb, DCB_ATTR_BCN, 0);
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001004}
1005
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001006static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
1007 int app_nested_type, int app_info_type,
1008 int app_entry_type)
Shmulik Ravideed84712011-02-27 05:04:31 +00001009{
1010 struct dcb_peer_app_info info;
1011 struct dcb_app *table = NULL;
1012 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1013 u16 app_count;
1014 int err;
1015
1016
1017 /**
1018 * retrieve the peer app configuration form the driver. If the driver
1019 * handlers fail exit without doing anything
1020 */
1021 err = ops->peer_getappinfo(netdev, &info, &app_count);
1022 if (!err && app_count) {
Kees Cook6da2ec52018-06-12 13:55:00 -07001023 table = kmalloc_array(app_count, sizeof(struct dcb_app),
1024 GFP_KERNEL);
Shmulik Ravideed84712011-02-27 05:04:31 +00001025 if (!table)
1026 return -ENOMEM;
1027
1028 err = ops->peer_getapptable(netdev, table);
1029 }
1030
1031 if (!err) {
1032 u16 i;
1033 struct nlattr *app;
1034
1035 /**
1036 * build the message, from here on the only possible failure
1037 * is due to the skb size
1038 */
1039 err = -EMSGSIZE;
1040
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001041 app = nla_nest_start_noflag(skb, app_nested_type);
Shmulik Ravideed84712011-02-27 05:04:31 +00001042 if (!app)
1043 goto nla_put_failure;
1044
David S. Miller1eb4c972012-04-01 20:03:01 -04001045 if (app_info_type &&
1046 nla_put(skb, app_info_type, sizeof(info), &info))
1047 goto nla_put_failure;
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001048
David S. Miller1eb4c972012-04-01 20:03:01 -04001049 for (i = 0; i < app_count; i++) {
1050 if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
1051 &table[i]))
1052 goto nla_put_failure;
1053 }
Shmulik Ravideed84712011-02-27 05:04:31 +00001054 nla_nest_end(skb, app);
1055 }
1056 err = 0;
1057
1058nla_put_failure:
1059 kfree(table);
1060 return err;
1061}
John Fastabend3e290272010-12-30 09:25:46 +00001062
Daniel Machon7eba4502022-11-14 10:29:50 +01001063static int dcbnl_getapptrust(struct net_device *netdev, struct sk_buff *skb)
1064{
1065 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1066 enum ieee_attrs_app type;
1067 struct nlattr *apptrust;
1068 int nselectors, err, i;
1069 u8 *selectors;
1070
1071 selectors = kzalloc(IEEE_8021QAZ_APP_SEL_MAX + 1, GFP_KERNEL);
1072 if (!selectors)
1073 return -ENOMEM;
1074
1075 err = ops->dcbnl_getapptrust(netdev, selectors, &nselectors);
1076 if (err) {
1077 err = 0;
1078 goto out;
1079 }
1080
1081 apptrust = nla_nest_start(skb, DCB_ATTR_DCB_APP_TRUST_TABLE);
1082 if (!apptrust) {
1083 err = -EMSGSIZE;
1084 goto out;
1085 }
1086
1087 for (i = 0; i < nselectors; i++) {
1088 type = dcbnl_app_attr_type_get(selectors[i]);
1089 err = nla_put_u8(skb, type, selectors[i]);
1090 if (err) {
1091 nla_nest_cancel(skb, apptrust);
1092 goto out;
1093 }
1094 }
1095 nla_nest_end(skb, apptrust);
1096
1097out:
1098 kfree(selectors);
1099 return err;
1100}
1101
Shani Michaelic93682472015-03-05 20:16:11 +02001102/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb GET commands. */
John Fastabend314b4772011-06-21 07:34:37 +00001103static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
John Fastabend3e290272010-12-30 09:25:46 +00001104{
John Fastabend3e290272010-12-30 09:25:46 +00001105 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
Daniel Machon7eba4502022-11-14 10:29:50 +01001106 struct nlattr *ieee, *app;
Daniel Machon6182d582022-11-01 10:48:30 +01001107 struct dcb_app_type *itr;
John Fastabendc7797ba2011-06-21 07:34:31 +00001108 int dcbx;
Thomas Graf3d1f4862012-06-13 02:54:58 +00001109 int err;
John Fastabend3e290272010-12-30 09:25:46 +00001110
David S. Miller1eb4c972012-04-01 20:03:01 -04001111 if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001112 return -EMSGSIZE;
1113
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001114 ieee = nla_nest_start_noflag(skb, DCB_ATTR_IEEE);
John Fastabend3e290272010-12-30 09:25:46 +00001115 if (!ieee)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001116 return -EMSGSIZE;
John Fastabend3e290272010-12-30 09:25:46 +00001117
1118 if (ops->ieee_getets) {
1119 struct ieee_ets ets;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001120 memset(&ets, 0, sizeof(ets));
John Fastabend3e290272010-12-30 09:25:46 +00001121 err = ops->ieee_getets(netdev, &ets);
David S. Miller1eb4c972012-04-01 20:03:01 -04001122 if (!err &&
1123 nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001124 return -EMSGSIZE;
John Fastabend3e290272010-12-30 09:25:46 +00001125 }
1126
Amir Vadai08f10af2012-04-04 21:33:30 +00001127 if (ops->ieee_getmaxrate) {
1128 struct ieee_maxrate maxrate;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001129 memset(&maxrate, 0, sizeof(maxrate));
Amir Vadai08f10af2012-04-04 21:33:30 +00001130 err = ops->ieee_getmaxrate(netdev, &maxrate);
1131 if (!err) {
1132 err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
1133 sizeof(maxrate), &maxrate);
1134 if (err)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001135 return -EMSGSIZE;
Amir Vadai08f10af2012-04-04 21:33:30 +00001136 }
1137 }
1138
Shani Michaelic93682472015-03-05 20:16:11 +02001139 if (ops->ieee_getqcn) {
1140 struct ieee_qcn qcn;
1141
1142 memset(&qcn, 0, sizeof(qcn));
1143 err = ops->ieee_getqcn(netdev, &qcn);
1144 if (!err) {
1145 err = nla_put(skb, DCB_ATTR_IEEE_QCN,
1146 sizeof(qcn), &qcn);
1147 if (err)
1148 return -EMSGSIZE;
1149 }
1150 }
1151
1152 if (ops->ieee_getqcnstats) {
1153 struct ieee_qcn_stats qcn_stats;
1154
1155 memset(&qcn_stats, 0, sizeof(qcn_stats));
1156 err = ops->ieee_getqcnstats(netdev, &qcn_stats);
1157 if (!err) {
1158 err = nla_put(skb, DCB_ATTR_IEEE_QCN_STATS,
1159 sizeof(qcn_stats), &qcn_stats);
1160 if (err)
1161 return -EMSGSIZE;
1162 }
1163 }
1164
John Fastabend3e290272010-12-30 09:25:46 +00001165 if (ops->ieee_getpfc) {
1166 struct ieee_pfc pfc;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001167 memset(&pfc, 0, sizeof(pfc));
John Fastabend3e290272010-12-30 09:25:46 +00001168 err = ops->ieee_getpfc(netdev, &pfc);
David S. Miller1eb4c972012-04-01 20:03:01 -04001169 if (!err &&
1170 nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001171 return -EMSGSIZE;
John Fastabend3e290272010-12-30 09:25:46 +00001172 }
1173
Huy Nguyene549f6f2018-02-22 11:57:10 -06001174 if (ops->dcbnl_getbuffer) {
1175 struct dcbnl_buffer buffer;
1176
1177 memset(&buffer, 0, sizeof(buffer));
1178 err = ops->dcbnl_getbuffer(netdev, &buffer);
1179 if (!err &&
1180 nla_put(skb, DCB_ATTR_DCB_BUFFER, sizeof(buffer), &buffer))
1181 return -EMSGSIZE;
1182 }
1183
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001184 app = nla_nest_start_noflag(skb, DCB_ATTR_IEEE_APP_TABLE);
John Fastabend9ab933a2010-12-30 09:26:31 +00001185 if (!app)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001186 return -EMSGSIZE;
John Fastabend9ab933a2010-12-30 09:26:31 +00001187
Anish Bhatt52cff742014-11-14 16:38:31 -08001188 spin_lock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00001189 list_for_each_entry(itr, &dcb_app_list, list) {
Mark Rustade290ed82011-10-06 08:52:33 +00001190 if (itr->ifindex == netdev->ifindex) {
Daniel Machonec32c0c2022-11-01 10:48:29 +01001191 enum ieee_attrs_app type =
1192 dcbnl_app_attr_type_get(itr->app.selector);
1193 err = nla_put(skb, type, sizeof(itr->app), &itr->app);
Dan Carpenter70bfa2d22011-01-04 21:03:12 +00001194 if (err) {
Anish Bhatt52cff742014-11-14 16:38:31 -08001195 spin_unlock_bh(&dcb_lock);
Thomas Graf3d1f4862012-06-13 02:54:58 +00001196 return -EMSGSIZE;
Dan Carpenter70bfa2d22011-01-04 21:03:12 +00001197 }
1198 }
John Fastabend9ab933a2010-12-30 09:26:31 +00001199 }
John Fastabendc7797ba2011-06-21 07:34:31 +00001200
1201 if (netdev->dcbnl_ops->getdcbx)
1202 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1203 else
1204 dcbx = -EOPNOTSUPP;
1205
Anish Bhatt52cff742014-11-14 16:38:31 -08001206 spin_unlock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00001207 nla_nest_end(skb, app);
1208
Daniel Machon6182d582022-11-01 10:48:30 +01001209 if (ops->dcbnl_getapptrust) {
Daniel Machon7eba4502022-11-14 10:29:50 +01001210 err = dcbnl_getapptrust(netdev, skb);
1211 if (err)
1212 return err;
Daniel Machon6182d582022-11-01 10:48:30 +01001213 }
1214
Shmulik Ravideed84712011-02-27 05:04:31 +00001215 /* get peer info if available */
1216 if (ops->ieee_peer_getets) {
1217 struct ieee_ets ets;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001218 memset(&ets, 0, sizeof(ets));
Shmulik Ravideed84712011-02-27 05:04:31 +00001219 err = ops->ieee_peer_getets(netdev, &ets);
David S. Miller1eb4c972012-04-01 20:03:01 -04001220 if (!err &&
1221 nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001222 return -EMSGSIZE;
Shmulik Ravideed84712011-02-27 05:04:31 +00001223 }
1224
1225 if (ops->ieee_peer_getpfc) {
1226 struct ieee_pfc pfc;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001227 memset(&pfc, 0, sizeof(pfc));
Shmulik Ravideed84712011-02-27 05:04:31 +00001228 err = ops->ieee_peer_getpfc(netdev, &pfc);
David S. Miller1eb4c972012-04-01 20:03:01 -04001229 if (!err &&
1230 nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001231 return -EMSGSIZE;
Shmulik Ravideed84712011-02-27 05:04:31 +00001232 }
1233
1234 if (ops->peer_getappinfo && ops->peer_getapptable) {
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001235 err = dcbnl_build_peer_app(netdev, skb,
1236 DCB_ATTR_IEEE_PEER_APP,
1237 DCB_ATTR_IEEE_APP_UNSPEC,
1238 DCB_ATTR_IEEE_APP);
Shmulik Ravideed84712011-02-27 05:04:31 +00001239 if (err)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001240 return -EMSGSIZE;
Shmulik Ravideed84712011-02-27 05:04:31 +00001241 }
1242
John Fastabend3e290272010-12-30 09:25:46 +00001243 nla_nest_end(skb, ieee);
John Fastabendc7797ba2011-06-21 07:34:31 +00001244 if (dcbx >= 0) {
1245 err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1246 if (err)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001247 return -EMSGSIZE;
John Fastabendc7797ba2011-06-21 07:34:31 +00001248 }
John Fastabend3e290272010-12-30 09:25:46 +00001249
John Fastabend314b4772011-06-21 07:34:37 +00001250 return 0;
John Fastabend3e290272010-12-30 09:25:46 +00001251}
1252
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001253static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
1254 int dir)
1255{
1256 u8 pgid, up_map, prio, tc_pct;
1257 const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1258 int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001259 struct nlattr *pg = nla_nest_start_noflag(skb, i);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001260
1261 if (!pg)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001262 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001263
1264 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001265 struct nlattr *tc_nest = nla_nest_start_noflag(skb, i);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001266
1267 if (!tc_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001268 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001269
1270 pgid = DCB_ATTR_VALUE_UNDEFINED;
1271 prio = DCB_ATTR_VALUE_UNDEFINED;
1272 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1273 up_map = DCB_ATTR_VALUE_UNDEFINED;
1274
1275 if (!dir)
1276 ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
1277 &prio, &pgid, &tc_pct, &up_map);
1278 else
1279 ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
1280 &prio, &pgid, &tc_pct, &up_map);
1281
David S. Miller1eb4c972012-04-01 20:03:01 -04001282 if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
1283 nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
1284 nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
1285 nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001286 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001287 nla_nest_end(skb, tc_nest);
1288 }
1289
1290 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
1291 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1292
1293 if (!dir)
1294 ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
1295 &tc_pct);
1296 else
1297 ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
1298 &tc_pct);
David S. Miller1eb4c972012-04-01 20:03:01 -04001299 if (nla_put_u8(skb, i, tc_pct))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001300 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001301 }
1302 nla_nest_end(skb, pg);
1303 return 0;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001304}
1305
1306static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
1307{
1308 struct nlattr *cee, *app;
1309 struct dcb_app_type *itr;
1310 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1311 int dcbx, i, err = -EMSGSIZE;
1312 u8 value;
1313
David S. Miller1eb4c972012-04-01 20:03:01 -04001314 if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
1315 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001316 cee = nla_nest_start_noflag(skb, DCB_ATTR_CEE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001317 if (!cee)
1318 goto nla_put_failure;
1319
1320 /* local pg */
1321 if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
1322 err = dcbnl_cee_pg_fill(skb, netdev, 1);
1323 if (err)
1324 goto nla_put_failure;
1325 }
1326
1327 if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
1328 err = dcbnl_cee_pg_fill(skb, netdev, 0);
1329 if (err)
1330 goto nla_put_failure;
1331 }
1332
1333 /* local pfc */
1334 if (ops->getpfccfg) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001335 struct nlattr *pfc_nest = nla_nest_start_noflag(skb,
1336 DCB_ATTR_CEE_PFC);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001337
1338 if (!pfc_nest)
1339 goto nla_put_failure;
1340
1341 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
1342 ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
David S. Miller1eb4c972012-04-01 20:03:01 -04001343 if (nla_put_u8(skb, i, value))
1344 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001345 }
1346 nla_nest_end(skb, pfc_nest);
1347 }
1348
1349 /* local app */
Anish Bhatt52cff742014-11-14 16:38:31 -08001350 spin_lock_bh(&dcb_lock);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001351 app = nla_nest_start_noflag(skb, DCB_ATTR_CEE_APP_TABLE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001352 if (!app)
Dan Carpenter40f5d722011-07-07 21:27:24 +00001353 goto dcb_unlock;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001354
1355 list_for_each_entry(itr, &dcb_app_list, list) {
Mark Rustade290ed82011-10-06 08:52:33 +00001356 if (itr->ifindex == netdev->ifindex) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001357 struct nlattr *app_nest = nla_nest_start_noflag(skb,
1358 DCB_ATTR_APP);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001359 if (!app_nest)
1360 goto dcb_unlock;
1361
1362 err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
1363 itr->app.selector);
1364 if (err)
1365 goto dcb_unlock;
1366
1367 err = nla_put_u16(skb, DCB_APP_ATTR_ID,
1368 itr->app.protocol);
1369 if (err)
1370 goto dcb_unlock;
1371
1372 err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
1373 itr->app.priority);
1374 if (err)
1375 goto dcb_unlock;
1376
1377 nla_nest_end(skb, app_nest);
1378 }
1379 }
1380 nla_nest_end(skb, app);
1381
1382 if (netdev->dcbnl_ops->getdcbx)
1383 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1384 else
1385 dcbx = -EOPNOTSUPP;
1386
Anish Bhatt52cff742014-11-14 16:38:31 -08001387 spin_unlock_bh(&dcb_lock);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001388
1389 /* features flags */
1390 if (ops->getfeatcfg) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001391 struct nlattr *feat = nla_nest_start_noflag(skb,
1392 DCB_ATTR_CEE_FEAT);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001393 if (!feat)
1394 goto nla_put_failure;
1395
1396 for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
1397 i++)
David S. Miller1eb4c972012-04-01 20:03:01 -04001398 if (!ops->getfeatcfg(netdev, i, &value) &&
1399 nla_put_u8(skb, i, value))
1400 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001401
1402 nla_nest_end(skb, feat);
1403 }
1404
1405 /* peer info if available */
1406 if (ops->cee_peer_getpg) {
1407 struct cee_pg pg;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001408 memset(&pg, 0, sizeof(pg));
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001409 err = ops->cee_peer_getpg(netdev, &pg);
David S. Miller1eb4c972012-04-01 20:03:01 -04001410 if (!err &&
1411 nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
1412 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001413 }
1414
1415 if (ops->cee_peer_getpfc) {
1416 struct cee_pfc pfc;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001417 memset(&pfc, 0, sizeof(pfc));
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001418 err = ops->cee_peer_getpfc(netdev, &pfc);
David S. Miller1eb4c972012-04-01 20:03:01 -04001419 if (!err &&
1420 nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
1421 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001422 }
1423
1424 if (ops->peer_getappinfo && ops->peer_getapptable) {
1425 err = dcbnl_build_peer_app(netdev, skb,
1426 DCB_ATTR_CEE_PEER_APP_TABLE,
1427 DCB_ATTR_CEE_PEER_APP_INFO,
1428 DCB_ATTR_CEE_PEER_APP);
1429 if (err)
1430 goto nla_put_failure;
1431 }
1432 nla_nest_end(skb, cee);
1433
1434 /* DCBX state */
1435 if (dcbx >= 0) {
1436 err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1437 if (err)
1438 goto nla_put_failure;
1439 }
1440 return 0;
1441
1442dcb_unlock:
Anish Bhatt52cff742014-11-14 16:38:31 -08001443 spin_unlock_bh(&dcb_lock);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001444nla_put_failure:
Pan Bianc66ebf22016-12-03 21:49:08 +08001445 err = -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001446 return err;
1447}
1448
1449static int dcbnl_notify(struct net_device *dev, int event, int cmd,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001450 u32 seq, u32 portid, int dcbx_ver)
John Fastabend314b4772011-06-21 07:34:37 +00001451{
1452 struct net *net = dev_net(dev);
1453 struct sk_buff *skb;
1454 struct nlmsghdr *nlh;
John Fastabend314b4772011-06-21 07:34:37 +00001455 const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1456 int err;
1457
1458 if (!ops)
1459 return -EOPNOTSUPP;
1460
Eric W. Biederman15e47302012-09-07 20:12:54 +00001461 skb = dcbnl_newmsg(event, cmd, portid, seq, 0, &nlh);
John Fastabend314b4772011-06-21 07:34:37 +00001462 if (!skb)
Zheng Yongjunb923cda2021-06-01 22:13:58 +08001463 return -ENOMEM;
John Fastabend314b4772011-06-21 07:34:37 +00001464
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001465 if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
1466 err = dcbnl_ieee_fill(skb, dev);
1467 else
1468 err = dcbnl_cee_fill(skb, dev);
1469
John Fastabend314b4772011-06-21 07:34:37 +00001470 if (err < 0) {
1471 /* Report error to broadcast listeners */
Thomas Grafab6d4702012-06-13 02:54:57 +00001472 nlmsg_free(skb);
John Fastabend314b4772011-06-21 07:34:37 +00001473 rtnl_set_sk_err(net, RTNLGRP_DCB, err);
1474 } else {
1475 /* End nlmsg and notify broadcast listeners */
1476 nlmsg_end(skb, nlh);
1477 rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
1478 }
1479
1480 return err;
1481}
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001482
1483int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001484 u32 seq, u32 portid)
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001485{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001486 return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_IEEE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001487}
1488EXPORT_SYMBOL(dcbnl_ieee_notify);
1489
1490int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001491 u32 seq, u32 portid)
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001492{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001493 return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_CEE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001494}
1495EXPORT_SYMBOL(dcbnl_cee_notify);
John Fastabend314b4772011-06-21 07:34:37 +00001496
Shani Michaelic93682472015-03-05 20:16:11 +02001497/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb SET commands.
1498 * If any requested operation can not be completed
1499 * the entire msg is aborted and error value is returned.
John Fastabend314b4772011-06-21 07:34:37 +00001500 * No attempt is made to reconcile the case where only part of the
1501 * cmd can be completed.
1502 */
Thomas Graf7be99412012-06-13 02:54:55 +00001503static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
1504 u32 seq, struct nlattr **tb, struct sk_buff *skb)
John Fastabend314b4772011-06-21 07:34:37 +00001505{
1506 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1507 struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
Petr Machata297e77e2020-09-10 14:09:05 +02001508 int prio;
Thomas Graf3d1f4862012-06-13 02:54:58 +00001509 int err;
John Fastabend314b4772011-06-21 07:34:37 +00001510
1511 if (!ops)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001512 return -EOPNOTSUPP;
John Fastabend314b4772011-06-21 07:34:37 +00001513
John Fastabend4003b652011-06-21 07:35:04 +00001514 if (!tb[DCB_ATTR_IEEE])
1515 return -EINVAL;
1516
Johannes Berg8cb08172019-04-26 14:07:28 +02001517 err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
1518 tb[DCB_ATTR_IEEE],
1519 dcbnl_ieee_policy, NULL);
John Fastabend314b4772011-06-21 07:34:37 +00001520 if (err)
1521 return err;
1522
1523 if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
1524 struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
1525 err = ops->ieee_setets(netdev, ets);
1526 if (err)
1527 goto err;
1528 }
1529
Amir Vadai08f10af2012-04-04 21:33:30 +00001530 if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
1531 struct ieee_maxrate *maxrate =
1532 nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
1533 err = ops->ieee_setmaxrate(netdev, maxrate);
1534 if (err)
1535 goto err;
1536 }
1537
Shani Michaelic93682472015-03-05 20:16:11 +02001538 if (ieee[DCB_ATTR_IEEE_QCN] && ops->ieee_setqcn) {
1539 struct ieee_qcn *qcn =
1540 nla_data(ieee[DCB_ATTR_IEEE_QCN]);
1541
1542 err = ops->ieee_setqcn(netdev, qcn);
1543 if (err)
1544 goto err;
1545 }
1546
John Fastabend314b4772011-06-21 07:34:37 +00001547 if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
1548 struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
1549 err = ops->ieee_setpfc(netdev, pfc);
1550 if (err)
1551 goto err;
1552 }
1553
Huy Nguyene549f6f2018-02-22 11:57:10 -06001554 if (ieee[DCB_ATTR_DCB_BUFFER] && ops->dcbnl_setbuffer) {
1555 struct dcbnl_buffer *buffer =
1556 nla_data(ieee[DCB_ATTR_DCB_BUFFER]);
1557
Petr Machata297e77e2020-09-10 14:09:05 +02001558 for (prio = 0; prio < ARRAY_SIZE(buffer->prio2buffer); prio++) {
1559 if (buffer->prio2buffer[prio] >= DCBX_MAX_BUFFERS) {
1560 err = -EINVAL;
1561 goto err;
1562 }
1563 }
1564
Huy Nguyene549f6f2018-02-22 11:57:10 -06001565 err = ops->dcbnl_setbuffer(netdev, buffer);
1566 if (err)
1567 goto err;
1568 }
1569
John Fastabend314b4772011-06-21 07:34:37 +00001570 if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1571 struct nlattr *attr;
1572 int rem;
1573
1574 nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
Daniel Machonec32c0c2022-11-01 10:48:29 +01001575 enum ieee_attrs_app type = nla_type(attr);
John Fastabend314b4772011-06-21 07:34:37 +00001576 struct dcb_app *app_data;
stephen hemminger332b4fc2017-05-19 09:55:48 -07001577
Daniel Machonec32c0c2022-11-01 10:48:29 +01001578 if (!dcbnl_app_attr_type_validate(type))
John Fastabend314b4772011-06-21 07:34:37 +00001579 continue;
stephen hemminger332b4fc2017-05-19 09:55:48 -07001580
1581 if (nla_len(attr) < sizeof(struct dcb_app)) {
1582 err = -ERANGE;
1583 goto err;
1584 }
1585
John Fastabend314b4772011-06-21 07:34:37 +00001586 app_data = nla_data(attr);
Daniel Machonec32c0c2022-11-01 10:48:29 +01001587
1588 if (!dcbnl_app_selector_validate(type,
1589 app_data->selector)) {
1590 err = -EINVAL;
1591 goto err;
1592 }
1593
John Fastabend314b4772011-06-21 07:34:37 +00001594 if (ops->ieee_setapp)
1595 err = ops->ieee_setapp(netdev, app_data);
1596 else
John Fastabendb6db2172011-06-21 07:34:42 +00001597 err = dcb_ieee_setapp(netdev, app_data);
John Fastabend314b4772011-06-21 07:34:37 +00001598 if (err)
1599 goto err;
1600 }
1601 }
1602
Daniel Machon6182d582022-11-01 10:48:30 +01001603 if (ieee[DCB_ATTR_DCB_APP_TRUST_TABLE]) {
1604 u8 selectors[IEEE_8021QAZ_APP_SEL_MAX + 1] = {0};
1605 struct nlattr *attr;
1606 int nselectors = 0;
1607 int rem;
1608
1609 if (!ops->dcbnl_setapptrust) {
1610 err = -EOPNOTSUPP;
1611 goto err;
1612 }
1613
1614 nla_for_each_nested(attr, ieee[DCB_ATTR_DCB_APP_TRUST_TABLE],
1615 rem) {
1616 enum ieee_attrs_app type = nla_type(attr);
1617 u8 selector;
1618 int i;
1619
1620 if (!dcbnl_app_attr_type_validate(type) ||
1621 nla_len(attr) != 1 ||
1622 nselectors >= sizeof(selectors)) {
1623 err = -EINVAL;
1624 goto err;
1625 }
1626
1627 selector = nla_get_u8(attr);
1628
1629 if (!dcbnl_app_selector_validate(type, selector)) {
1630 err = -EINVAL;
1631 goto err;
1632 }
1633
1634 /* Duplicate selector ? */
1635 for (i = 0; i < nselectors; i++) {
1636 if (selectors[i] == selector) {
1637 err = -EINVAL;
1638 goto err;
1639 }
1640 }
1641
1642 selectors[nselectors++] = selector;
1643 }
1644
1645 err = ops->dcbnl_setapptrust(netdev, selectors, nselectors);
1646 if (err)
1647 goto err;
1648 }
1649
John Fastabend314b4772011-06-21 07:34:37 +00001650err:
Thomas Graf7be99412012-06-13 02:54:55 +00001651 err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001652 dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
John Fastabend314b4772011-06-21 07:34:37 +00001653 return err;
1654}
1655
Thomas Graf7be99412012-06-13 02:54:55 +00001656static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1657 u32 seq, struct nlattr **tb, struct sk_buff *skb)
John Fastabend314b4772011-06-21 07:34:37 +00001658{
John Fastabend314b4772011-06-21 07:34:37 +00001659 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
John Fastabend314b4772011-06-21 07:34:37 +00001660
1661 if (!ops)
1662 return -EOPNOTSUPP;
1663
Thomas Graf7be99412012-06-13 02:54:55 +00001664 return dcbnl_ieee_fill(skb, netdev);
John Fastabend314b4772011-06-21 07:34:37 +00001665}
John Fastabendf9ae7e42011-06-21 07:34:48 +00001666
Thomas Graf7be99412012-06-13 02:54:55 +00001667static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
1668 u32 seq, struct nlattr **tb, struct sk_buff *skb)
John Fastabendf9ae7e42011-06-21 07:34:48 +00001669{
1670 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1671 struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
Thomas Graf3d1f4862012-06-13 02:54:58 +00001672 int err;
John Fastabendf9ae7e42011-06-21 07:34:48 +00001673
1674 if (!ops)
1675 return -EOPNOTSUPP;
1676
1677 if (!tb[DCB_ATTR_IEEE])
1678 return -EINVAL;
1679
Johannes Berg8cb08172019-04-26 14:07:28 +02001680 err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
1681 tb[DCB_ATTR_IEEE],
1682 dcbnl_ieee_policy, NULL);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001683 if (err)
1684 return err;
1685
1686 if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1687 struct nlattr *attr;
1688 int rem;
1689
1690 nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
Daniel Machonec32c0c2022-11-01 10:48:29 +01001691 enum ieee_attrs_app type = nla_type(attr);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001692 struct dcb_app *app_data;
1693
Daniel Machonec32c0c2022-11-01 10:48:29 +01001694 if (!dcbnl_app_attr_type_validate(type))
John Fastabendf9ae7e42011-06-21 07:34:48 +00001695 continue;
Daniel Machonec32c0c2022-11-01 10:48:29 +01001696
John Fastabendf9ae7e42011-06-21 07:34:48 +00001697 app_data = nla_data(attr);
Daniel Machonec32c0c2022-11-01 10:48:29 +01001698
1699 if (!dcbnl_app_selector_validate(type,
1700 app_data->selector)) {
1701 err = -EINVAL;
1702 goto err;
1703 }
1704
John Fastabendf9ae7e42011-06-21 07:34:48 +00001705 if (ops->ieee_delapp)
1706 err = ops->ieee_delapp(netdev, app_data);
1707 else
1708 err = dcb_ieee_delapp(netdev, app_data);
1709 if (err)
1710 goto err;
1711 }
1712 }
1713
1714err:
Thomas Graf7be99412012-06-13 02:54:55 +00001715 err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001716 dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001717 return err;
1718}
1719
1720
Shmulik Ravid6241b622010-12-30 06:26:48 +00001721/* DCBX configuration */
Thomas Graf7be99412012-06-13 02:54:55 +00001722static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1723 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravid6241b622010-12-30 06:26:48 +00001724{
Shmulik Ravid6241b622010-12-30 06:26:48 +00001725 if (!netdev->dcbnl_ops->getdcbx)
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001726 return -EOPNOTSUPP;
Shmulik Ravid6241b622010-12-30 06:26:48 +00001727
Thomas Graf7be99412012-06-13 02:54:55 +00001728 return nla_put_u8(skb, DCB_ATTR_DCBX,
1729 netdev->dcbnl_ops->getdcbx(netdev));
Shmulik Ravid6241b622010-12-30 06:26:48 +00001730}
1731
Thomas Graf7be99412012-06-13 02:54:55 +00001732static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1733 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravid6241b622010-12-30 06:26:48 +00001734{
Shmulik Ravid6241b622010-12-30 06:26:48 +00001735 u8 value;
1736
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001737 if (!netdev->dcbnl_ops->setdcbx)
1738 return -EOPNOTSUPP;
1739
1740 if (!tb[DCB_ATTR_DCBX])
1741 return -EINVAL;
Shmulik Ravid6241b622010-12-30 06:26:48 +00001742
1743 value = nla_get_u8(tb[DCB_ATTR_DCBX]);
1744
Thomas Graf7be99412012-06-13 02:54:55 +00001745 return nla_put_u8(skb, DCB_ATTR_DCBX,
1746 netdev->dcbnl_ops->setdcbx(netdev, value));
Shmulik Ravid6241b622010-12-30 06:26:48 +00001747}
1748
Thomas Graf7be99412012-06-13 02:54:55 +00001749static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1750 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001751{
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001752 struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
1753 u8 value;
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001754 int ret, i;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001755 int getall = 0;
1756
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001757 if (!netdev->dcbnl_ops->getfeatcfg)
1758 return -EOPNOTSUPP;
1759
1760 if (!tb[DCB_ATTR_FEATCFG])
1761 return -EINVAL;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001762
Johannes Berg8cb08172019-04-26 14:07:28 +02001763 ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
1764 tb[DCB_ATTR_FEATCFG],
1765 dcbnl_featcfg_nest, NULL);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001766 if (ret)
Thomas Graf7be99412012-06-13 02:54:55 +00001767 return ret;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001768
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001769 nest = nla_nest_start_noflag(skb, DCB_ATTR_FEATCFG);
Thomas Graf7be99412012-06-13 02:54:55 +00001770 if (!nest)
1771 return -EMSGSIZE;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001772
1773 if (data[DCB_FEATCFG_ATTR_ALL])
1774 getall = 1;
1775
1776 for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1777 if (!getall && !data[i])
1778 continue;
1779
1780 ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001781 if (!ret)
Thomas Graf7be99412012-06-13 02:54:55 +00001782 ret = nla_put_u8(skb, i, value);
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001783
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001784 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +00001785 nla_nest_cancel(skb, nest);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001786 goto nla_put_failure;
1787 }
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001788 }
Thomas Graf7be99412012-06-13 02:54:55 +00001789 nla_nest_end(skb, nest);
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001790
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001791nla_put_failure:
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001792 return ret;
1793}
1794
Thomas Graf7be99412012-06-13 02:54:55 +00001795static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1796 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001797{
1798 struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001799 int ret, i;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001800 u8 value;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001801
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001802 if (!netdev->dcbnl_ops->setfeatcfg)
1803 return -ENOTSUPP;
1804
1805 if (!tb[DCB_ATTR_FEATCFG])
1806 return -EINVAL;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001807
Johannes Berg8cb08172019-04-26 14:07:28 +02001808 ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
1809 tb[DCB_ATTR_FEATCFG],
1810 dcbnl_featcfg_nest, NULL);
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001811
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001812 if (ret)
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001813 goto err;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001814
1815 for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1816 if (data[i] == NULL)
1817 continue;
1818
1819 value = nla_get_u8(data[i]);
1820
1821 ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
1822
1823 if (ret)
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001824 goto err;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001825 }
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001826err:
Thomas Graf7be99412012-06-13 02:54:55 +00001827 ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001828
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001829 return ret;
1830}
1831
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001832/* Handle CEE DCBX GET commands. */
Thomas Graf7be99412012-06-13 02:54:55 +00001833static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1834 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001835{
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001836 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001837
1838 if (!ops)
1839 return -EOPNOTSUPP;
1840
Thomas Graf7be99412012-06-13 02:54:55 +00001841 return dcbnl_cee_fill(skb, netdev);
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001842}
1843
Thomas Graf33a03aa2012-06-13 02:54:54 +00001844struct reply_func {
1845 /* reply netlink message type */
1846 int type;
1847
1848 /* function to fill message contents */
1849 int (*cb)(struct net_device *, struct nlmsghdr *, u32,
1850 struct nlattr **, struct sk_buff *);
1851};
1852
1853static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
Thomas Graf7be99412012-06-13 02:54:55 +00001854 [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
1855 [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
1856 [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
1857 [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
1858 [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
1859 [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
1860 [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
1861 [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
1862 [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
1863 [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
1864 [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
1865 [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
1866 [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
1867 [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
1868 [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
1869 [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
1870 [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
1871 [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
1872 [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
1873 [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
1874 [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
1875 [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
1876 [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
1877 [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
1878 [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
1879 [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
1880 [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
Thomas Graf33a03aa2012-06-13 02:54:54 +00001881};
1882
David Ahernc21ef3e2017-04-16 09:48:24 -07001883static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1884 struct netlink_ext_ack *extack)
Alexander Duyck2f90b862008-11-20 20:52:10 -08001885{
1886 struct net *net = sock_net(skb->sk);
1887 struct net_device *netdev;
Thomas Graf7a282bc2012-06-13 02:55:01 +00001888 struct dcbmsg *dcb = nlmsg_data(nlh);
Alexander Duyck2f90b862008-11-20 20:52:10 -08001889 struct nlattr *tb[DCB_ATTR_MAX + 1];
Gaurav Singh0cc55e62020-06-22 22:50:39 -04001890 u32 portid = NETLINK_CB(skb).portid;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001891 int ret = -EINVAL;
Thomas Graf33a03aa2012-06-13 02:54:54 +00001892 struct sk_buff *reply_skb;
Thomas Graf39912f92012-06-13 22:34:03 +00001893 struct nlmsghdr *reply_nlh = NULL;
Thomas Graf33a03aa2012-06-13 02:54:54 +00001894 const struct reply_func *fn;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001895
Eric W. Biederman90f62cf32014-04-23 14:29:27 -07001896 if ((nlh->nlmsg_type == RTM_SETDCB) && !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001897 return -EPERM;
1898
Johannes Berg8cb08172019-04-26 14:07:28 +02001899 ret = nlmsg_parse_deprecated(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
1900 dcbnl_rtnl_policy, extack);
Alexander Duyck2f90b862008-11-20 20:52:10 -08001901 if (ret < 0)
1902 return ret;
1903
Thomas Graf33a03aa2012-06-13 02:54:54 +00001904 if (dcb->cmd > DCB_CMD_MAX)
1905 return -EINVAL;
1906
1907 /* check if a reply function has been defined for the command */
1908 fn = &reply_funcs[dcb->cmd];
1909 if (!fn->cb)
1910 return -EOPNOTSUPP;
Petr Machatadf85bc12021-01-11 18:07:07 +01001911 if (fn->type == RTM_SETDCB && !netlink_capable(skb, CAP_NET_ADMIN))
Petr Machata826f3282020-12-22 22:49:44 +01001912 return -EPERM;
Thomas Graf33a03aa2012-06-13 02:54:54 +00001913
Alexander Duyck2f90b862008-11-20 20:52:10 -08001914 if (!tb[DCB_ATTR_IFNAME])
1915 return -EINVAL;
1916
Ying Xued9ac62b2014-01-15 10:23:39 +08001917 netdev = __dev_get_by_name(net, nla_data(tb[DCB_ATTR_IFNAME]));
Alexander Duyck2f90b862008-11-20 20:52:10 -08001918 if (!netdev)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001919 return -ENODEV;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001920
Ying Xued9ac62b2014-01-15 10:23:39 +08001921 if (!netdev->dcbnl_ops)
1922 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001923
Eric W. Biederman15e47302012-09-07 20:12:54 +00001924 reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, portid, nlh->nlmsg_seq,
Thomas Graf33a03aa2012-06-13 02:54:54 +00001925 nlh->nlmsg_flags, &reply_nlh);
Ying Xued9ac62b2014-01-15 10:23:39 +08001926 if (!reply_skb)
Zheng Yongjunb923cda2021-06-01 22:13:58 +08001927 return -ENOMEM;
Thomas Graf33a03aa2012-06-13 02:54:54 +00001928
1929 ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
1930 if (ret < 0) {
1931 nlmsg_free(reply_skb);
1932 goto out;
1933 }
1934
1935 nlmsg_end(reply_skb, reply_nlh);
1936
John Fastabend7c77ab22012-12-09 20:48:13 +00001937 ret = rtnl_unicast(reply_skb, net, portid);
Alexander Duyck2f90b862008-11-20 20:52:10 -08001938out:
Alexander Duyck2f90b862008-11-20 20:52:10 -08001939 return ret;
1940}
1941
Thomas Graf716b31a2012-06-13 02:54:59 +00001942static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
1943 int ifindex, int prio)
1944{
1945 struct dcb_app_type *itr;
1946
1947 list_for_each_entry(itr, &dcb_app_list, list) {
1948 if (itr->app.selector == app->selector &&
1949 itr->app.protocol == app->protocol &&
1950 itr->ifindex == ifindex &&
Petr Machata08193d12018-07-27 15:26:55 +03001951 ((prio == -1) || itr->app.priority == prio))
Thomas Graf716b31a2012-06-13 02:54:59 +00001952 return itr;
1953 }
1954
1955 return NULL;
1956}
1957
Thomas Graf4e4f2f62012-06-13 02:55:00 +00001958static int dcb_app_add(const struct dcb_app *app, int ifindex)
1959{
1960 struct dcb_app_type *entry;
1961
1962 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1963 if (!entry)
1964 return -ENOMEM;
1965
1966 memcpy(&entry->app, app, sizeof(*app));
1967 entry->ifindex = ifindex;
1968 list_add(&entry->list, &dcb_app_list);
1969
1970 return 0;
1971}
1972
John Fastabend9ab933a2010-12-30 09:26:31 +00001973/**
1974 * dcb_getapp - retrieve the DCBX application user priority
Andrew Lunna89a5012020-10-28 02:09:13 +01001975 * @dev: network interface
1976 * @app: application to get user priority of
John Fastabend9ab933a2010-12-30 09:26:31 +00001977 *
1978 * On success returns a non-zero 802.1p user priority bitmap
1979 * otherwise returns 0 as the invalid user priority bitmap to
1980 * indicate an error.
1981 */
1982u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
1983{
1984 struct dcb_app_type *itr;
1985 u8 prio = 0;
1986
Anish Bhatt52cff742014-11-14 16:38:31 -08001987 spin_lock_bh(&dcb_lock);
Petr Machata08193d12018-07-27 15:26:55 +03001988 itr = dcb_app_lookup(app, dev->ifindex, -1);
1989 if (itr)
Thomas Graf716b31a2012-06-13 02:54:59 +00001990 prio = itr->app.priority;
Anish Bhatt52cff742014-11-14 16:38:31 -08001991 spin_unlock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00001992
1993 return prio;
1994}
1995EXPORT_SYMBOL(dcb_getapp);
1996
1997/**
John Fastabendb6db2172011-06-21 07:34:42 +00001998 * dcb_setapp - add CEE dcb application data to app list
Andrew Lunna89a5012020-10-28 02:09:13 +01001999 * @dev: network interface
2000 * @new: application data to add
John Fastabend9ab933a2010-12-30 09:26:31 +00002001 *
John Fastabendb6db2172011-06-21 07:34:42 +00002002 * Priority 0 is an invalid priority in CEE spec. This routine
2003 * removes applications from the app list if the priority is
Anish Bhatt16eecd92014-07-28 20:57:07 -07002004 * set to zero. Priority is expected to be 8-bit 802.1p user priority bitmap
John Fastabend9ab933a2010-12-30 09:26:31 +00002005 */
John Fastabendab6baf92011-06-21 07:34:58 +00002006int dcb_setapp(struct net_device *dev, struct dcb_app *new)
John Fastabend9ab933a2010-12-30 09:26:31 +00002007{
2008 struct dcb_app_type *itr;
John Fastabend7ec79272011-01-31 12:00:59 +00002009 struct dcb_app_type event;
Thomas Graf4e4f2f62012-06-13 02:55:00 +00002010 int err = 0;
John Fastabend7ec79272011-01-31 12:00:59 +00002011
Mark Rustade290ed82011-10-06 08:52:33 +00002012 event.ifindex = dev->ifindex;
John Fastabend7ec79272011-01-31 12:00:59 +00002013 memcpy(&event.app, new, sizeof(event.app));
John Fastabend6bd0e1c2011-10-06 08:52:38 +00002014 if (dev->dcbnl_ops->getdcbx)
2015 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
John Fastabend9ab933a2010-12-30 09:26:31 +00002016
Anish Bhatt52cff742014-11-14 16:38:31 -08002017 spin_lock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00002018 /* Search for existing match and replace */
Petr Machata08193d12018-07-27 15:26:55 +03002019 itr = dcb_app_lookup(new, dev->ifindex, -1);
2020 if (itr) {
Thomas Graf716b31a2012-06-13 02:54:59 +00002021 if (new->priority)
2022 itr->app.priority = new->priority;
2023 else {
2024 list_del(&itr->list);
2025 kfree(itr);
John Fastabend9ab933a2010-12-30 09:26:31 +00002026 }
Thomas Graf716b31a2012-06-13 02:54:59 +00002027 goto out;
John Fastabend9ab933a2010-12-30 09:26:31 +00002028 }
2029 /* App type does not exist add new application type */
Thomas Graf4e4f2f62012-06-13 02:55:00 +00002030 if (new->priority)
2031 err = dcb_app_add(new, dev->ifindex);
John Fastabend9ab933a2010-12-30 09:26:31 +00002032out:
Anish Bhatt52cff742014-11-14 16:38:31 -08002033 spin_unlock_bh(&dcb_lock);
Thomas Graf4e4f2f62012-06-13 02:55:00 +00002034 if (!err)
2035 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
2036 return err;
John Fastabend9ab933a2010-12-30 09:26:31 +00002037}
2038EXPORT_SYMBOL(dcb_setapp);
2039
John Fastabendb6db2172011-06-21 07:34:42 +00002040/**
John Fastabenda364c8c2011-06-21 07:34:53 +00002041 * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
Andrew Lunna89a5012020-10-28 02:09:13 +01002042 * @dev: network interface
2043 * @app: where to store the retrieve application data
John Fastabenda364c8c2011-06-21 07:34:53 +00002044 *
2045 * Helper routine which on success returns a non-zero 802.1Qaz user
2046 * priority bitmap otherwise returns 0 to indicate the dcb_app was
2047 * not found in APP list.
2048 */
2049u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
2050{
2051 struct dcb_app_type *itr;
2052 u8 prio = 0;
2053
Anish Bhatt52cff742014-11-14 16:38:31 -08002054 spin_lock_bh(&dcb_lock);
Petr Machata08193d12018-07-27 15:26:55 +03002055 itr = dcb_app_lookup(app, dev->ifindex, -1);
2056 if (itr)
Thomas Graf716b31a2012-06-13 02:54:59 +00002057 prio |= 1 << itr->app.priority;
Anish Bhatt52cff742014-11-14 16:38:31 -08002058 spin_unlock_bh(&dcb_lock);
John Fastabenda364c8c2011-06-21 07:34:53 +00002059
2060 return prio;
2061}
2062EXPORT_SYMBOL(dcb_ieee_getapp_mask);
2063
2064/**
John Fastabendb6db2172011-06-21 07:34:42 +00002065 * dcb_ieee_setapp - add IEEE dcb application data to app list
Andrew Lunna89a5012020-10-28 02:09:13 +01002066 * @dev: network interface
2067 * @new: application data to add
John Fastabendb6db2172011-06-21 07:34:42 +00002068 *
2069 * This adds Application data to the list. Multiple application
2070 * entries may exists for the same selector and protocol as long
Anish Bhatt16eecd92014-07-28 20:57:07 -07002071 * as the priorities are different. Priority is expected to be a
2072 * 3-bit unsigned integer
John Fastabendb6db2172011-06-21 07:34:42 +00002073 */
2074int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
2075{
John Fastabendb6db2172011-06-21 07:34:42 +00002076 struct dcb_app_type event;
2077 int err = 0;
2078
Mark Rustade290ed82011-10-06 08:52:33 +00002079 event.ifindex = dev->ifindex;
John Fastabendb6db2172011-06-21 07:34:42 +00002080 memcpy(&event.app, new, sizeof(event.app));
John Fastabend6bd0e1c2011-10-06 08:52:38 +00002081 if (dev->dcbnl_ops->getdcbx)
2082 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
John Fastabendb6db2172011-06-21 07:34:42 +00002083
Anish Bhatt52cff742014-11-14 16:38:31 -08002084 spin_lock_bh(&dcb_lock);
John Fastabendb6db2172011-06-21 07:34:42 +00002085 /* Search for existing match and abort if found */
Thomas Graf716b31a2012-06-13 02:54:59 +00002086 if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
2087 err = -EEXIST;
2088 goto out;
John Fastabendb6db2172011-06-21 07:34:42 +00002089 }
2090
Thomas Graf4e4f2f62012-06-13 02:55:00 +00002091 err = dcb_app_add(new, dev->ifindex);
John Fastabendb6db2172011-06-21 07:34:42 +00002092out:
Anish Bhatt52cff742014-11-14 16:38:31 -08002093 spin_unlock_bh(&dcb_lock);
John Fastabendb6db2172011-06-21 07:34:42 +00002094 if (!err)
2095 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
2096 return err;
2097}
2098EXPORT_SYMBOL(dcb_ieee_setapp);
2099
John Fastabendf9ae7e42011-06-21 07:34:48 +00002100/**
2101 * dcb_ieee_delapp - delete IEEE dcb application data from list
Andrew Lunna89a5012020-10-28 02:09:13 +01002102 * @dev: network interface
2103 * @del: application data to delete
John Fastabendf9ae7e42011-06-21 07:34:48 +00002104 *
2105 * This removes a matching APP data from the APP list
2106 */
2107int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
2108{
2109 struct dcb_app_type *itr;
2110 struct dcb_app_type event;
2111 int err = -ENOENT;
2112
Mark Rustade290ed82011-10-06 08:52:33 +00002113 event.ifindex = dev->ifindex;
John Fastabendf9ae7e42011-06-21 07:34:48 +00002114 memcpy(&event.app, del, sizeof(event.app));
John Fastabend6bd0e1c2011-10-06 08:52:38 +00002115 if (dev->dcbnl_ops->getdcbx)
2116 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
John Fastabendf9ae7e42011-06-21 07:34:48 +00002117
Anish Bhatt52cff742014-11-14 16:38:31 -08002118 spin_lock_bh(&dcb_lock);
John Fastabendf9ae7e42011-06-21 07:34:48 +00002119 /* Search for existing match and remove it. */
Thomas Graf716b31a2012-06-13 02:54:59 +00002120 if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
2121 list_del(&itr->list);
2122 kfree(itr);
2123 err = 0;
John Fastabendf9ae7e42011-06-21 07:34:48 +00002124 }
2125
Anish Bhatt52cff742014-11-14 16:38:31 -08002126 spin_unlock_bh(&dcb_lock);
John Fastabendf9ae7e42011-06-21 07:34:48 +00002127 if (!err)
2128 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
2129 return err;
2130}
2131EXPORT_SYMBOL(dcb_ieee_delapp);
2132
Andrew Lunna89a5012020-10-28 02:09:13 +01002133/*
Petr Machatab67c540b82018-07-27 15:26:56 +03002134 * dcb_ieee_getapp_prio_dscp_mask_map - For a given device, find mapping from
2135 * priorities to the DSCP values assigned to that priority. Initialize p_map
2136 * such that each map element holds a bit mask of DSCP values configured for
2137 * that priority by APP entries.
2138 */
2139void dcb_ieee_getapp_prio_dscp_mask_map(const struct net_device *dev,
2140 struct dcb_ieee_app_prio_map *p_map)
2141{
2142 int ifindex = dev->ifindex;
2143 struct dcb_app_type *itr;
2144 u8 prio;
2145
2146 memset(p_map->map, 0, sizeof(p_map->map));
2147
2148 spin_lock_bh(&dcb_lock);
2149 list_for_each_entry(itr, &dcb_app_list, list) {
2150 if (itr->ifindex == ifindex &&
2151 itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
2152 itr->app.protocol < 64 &&
2153 itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
2154 prio = itr->app.priority;
2155 p_map->map[prio] |= 1ULL << itr->app.protocol;
2156 }
2157 }
2158 spin_unlock_bh(&dcb_lock);
2159}
2160EXPORT_SYMBOL(dcb_ieee_getapp_prio_dscp_mask_map);
2161
Andrew Lunna89a5012020-10-28 02:09:13 +01002162/*
Petr Machatab67c540b82018-07-27 15:26:56 +03002163 * dcb_ieee_getapp_dscp_prio_mask_map - For a given device, find mapping from
2164 * DSCP values to the priorities assigned to that DSCP value. Initialize p_map
2165 * such that each map element holds a bit mask of priorities configured for a
2166 * given DSCP value by APP entries.
2167 */
2168void
2169dcb_ieee_getapp_dscp_prio_mask_map(const struct net_device *dev,
2170 struct dcb_ieee_app_dscp_map *p_map)
2171{
2172 int ifindex = dev->ifindex;
2173 struct dcb_app_type *itr;
2174
2175 memset(p_map->map, 0, sizeof(p_map->map));
2176
2177 spin_lock_bh(&dcb_lock);
2178 list_for_each_entry(itr, &dcb_app_list, list) {
2179 if (itr->ifindex == ifindex &&
2180 itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
2181 itr->app.protocol < 64 &&
2182 itr->app.priority < IEEE_8021QAZ_MAX_TCS)
2183 p_map->map[itr->app.protocol] |= 1 << itr->app.priority;
2184 }
2185 spin_unlock_bh(&dcb_lock);
2186}
2187EXPORT_SYMBOL(dcb_ieee_getapp_dscp_prio_mask_map);
2188
Andrew Lunna89a5012020-10-28 02:09:13 +01002189/*
Petr Machatab67c540b82018-07-27 15:26:56 +03002190 * Per 802.1Q-2014, the selector value of 1 is used for matching on Ethernet
2191 * type, with valid PID values >= 1536. A special meaning is then assigned to
2192 * protocol value of 0: "default priority. For use when priority is not
2193 * otherwise specified".
2194 *
2195 * dcb_ieee_getapp_default_prio_mask - For a given device, find all APP entries
2196 * of the form {$PRIO, ETHERTYPE, 0} and construct a bit mask of all default
2197 * priorities set by these entries.
2198 */
2199u8 dcb_ieee_getapp_default_prio_mask(const struct net_device *dev)
2200{
2201 int ifindex = dev->ifindex;
2202 struct dcb_app_type *itr;
2203 u8 mask = 0;
2204
2205 spin_lock_bh(&dcb_lock);
2206 list_for_each_entry(itr, &dcb_app_list, list) {
2207 if (itr->ifindex == ifindex &&
2208 itr->app.selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
2209 itr->app.protocol == 0 &&
2210 itr->app.priority < IEEE_8021QAZ_MAX_TCS)
2211 mask |= 1 << itr->app.priority;
2212 }
2213 spin_unlock_bh(&dcb_lock);
2214
2215 return mask;
2216}
2217EXPORT_SYMBOL(dcb_ieee_getapp_default_prio_mask);
2218
Vladimir Oltean91b03832022-02-24 18:01:54 +02002219static void dcbnl_flush_dev(struct net_device *dev)
2220{
2221 struct dcb_app_type *itr, *tmp;
2222
Vladimir Oltean10b6bb62022-03-02 21:39:39 +02002223 spin_lock_bh(&dcb_lock);
Vladimir Oltean91b03832022-02-24 18:01:54 +02002224
2225 list_for_each_entry_safe(itr, tmp, &dcb_app_list, list) {
2226 if (itr->ifindex == dev->ifindex) {
2227 list_del(&itr->list);
2228 kfree(itr);
2229 }
2230 }
2231
Vladimir Oltean10b6bb62022-03-02 21:39:39 +02002232 spin_unlock_bh(&dcb_lock);
Vladimir Oltean91b03832022-02-24 18:01:54 +02002233}
2234
2235static int dcbnl_netdevice_event(struct notifier_block *nb,
2236 unsigned long event, void *ptr)
2237{
2238 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2239
2240 switch (event) {
2241 case NETDEV_UNREGISTER:
2242 if (!dev->dcbnl_ops)
2243 return NOTIFY_DONE;
2244
2245 dcbnl_flush_dev(dev);
2246
2247 return NOTIFY_OK;
2248 default:
2249 return NOTIFY_DONE;
2250 }
2251}
2252
2253static struct notifier_block dcbnl_nb __read_mostly = {
2254 .notifier_call = dcbnl_netdevice_event,
2255};
2256
Alexander Duyck2f90b862008-11-20 20:52:10 -08002257static int __init dcbnl_init(void)
2258{
Vladimir Oltean91b03832022-02-24 18:01:54 +02002259 int err;
2260
2261 err = register_netdevice_notifier(&dcbnl_nb);
2262 if (err)
2263 return err;
2264
Florian Westphalb97bac62017-08-09 20:41:48 +02002265 rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, 0);
2266 rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -08002267
2268 return 0;
2269}
Paul Gortmaker36b9ad802015-10-07 17:27:44 -04002270device_initcall(dcbnl_init);