blob: ceff9d22deea3f7ebe000a9b6dcb0393f0af772d [file] [log] [blame]
Alexander Duyck2f90b862008-11-20 20:52:10 -08001/*
Mark Rustad698e1d22011-03-14 09:01:02 +00002 * Copyright (c) 2008-2011, Intel Corporation.
Alexander Duyck2f90b862008-11-20 20:52:10 -08003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
Jeff Kirsherc057b192013-12-06 09:13:44 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Alexander Duyck2f90b862008-11-20 20:52:10 -080015 *
Paul Gortmaker36b9ad802015-10-07 17:27:44 -040016 * Description: Data Center Bridging netlink interface
Alexander Duyck2f90b862008-11-20 20:52:10 -080017 * Author: Lucy Liu <lucy.liu@intel.com>
18 */
19
20#include <linux/netdevice.h>
21#include <linux/netlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080023#include <net/netlink.h>
24#include <net/rtnetlink.h>
25#include <linux/dcbnl.h>
John Fastabend96b99682010-12-30 09:26:37 +000026#include <net/dcbevent.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080027#include <linux/rtnetlink.h>
Paul Gortmaker36b9ad802015-10-07 17:27:44 -040028#include <linux/init.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080029#include <net/sock.h>
30
Ben Hutchingsae86b9e2012-07-10 10:55:35 +000031/* Data Center Bridging (DCB) is a collection of Ethernet enhancements
Alexander Duyck2f90b862008-11-20 20:52:10 -080032 * intended to allow network traffic with differing requirements
33 * (highly reliable, no drops vs. best effort vs. low latency) to operate
34 * and co-exist on Ethernet. Current DCB features are:
35 *
36 * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
37 * framework for assigning bandwidth guarantees to traffic classes.
38 *
39 * Priority-based Flow Control (PFC) - provides a flow control mechanism which
40 * can work independently for each 802.1p priority.
41 *
42 * Congestion Notification - provides a mechanism for end-to-end congestion
43 * control for protocols which do not have built-in congestion management.
44 *
45 * More information about the emerging standards for these Ethernet features
46 * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
47 *
48 * This file implements an rtnetlink interface to allow configuration of DCB
49 * features for capable devices.
50 */
51
Alexander Duyck2f90b862008-11-20 20:52:10 -080052/**************** DCB attribute policies *************************************/
53
54/* DCB netlink attributes policy */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000055static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
Alexander Duyck859ee3c2008-11-20 21:10:23 -080056 [DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
57 [DCB_ATTR_STATE] = {.type = NLA_U8},
58 [DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},
59 [DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},
60 [DCB_ATTR_SET_ALL] = {.type = NLA_U8},
Alexander Duyck2f90b862008-11-20 20:52:10 -080061 [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
Alexander Duyck859ee3c2008-11-20 21:10:23 -080062 [DCB_ATTR_CAP] = {.type = NLA_NESTED},
63 [DCB_ATTR_PFC_STATE] = {.type = NLA_U8},
64 [DCB_ATTR_BCN] = {.type = NLA_NESTED},
Yi Zou6fa382a2009-08-31 12:33:20 +000065 [DCB_ATTR_APP] = {.type = NLA_NESTED},
John Fastabend3e290272010-12-30 09:25:46 +000066 [DCB_ATTR_IEEE] = {.type = NLA_NESTED},
Shmulik Ravid6241b622010-12-30 06:26:48 +000067 [DCB_ATTR_DCBX] = {.type = NLA_U8},
Shmulik Ravidea45fe42010-12-30 06:26:55 +000068 [DCB_ATTR_FEATCFG] = {.type = NLA_NESTED},
Alexander Duyck2f90b862008-11-20 20:52:10 -080069};
70
71/* DCB priority flow control to User Priority nested attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000072static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -080073 [DCB_PFC_UP_ATTR_0] = {.type = NLA_U8},
74 [DCB_PFC_UP_ATTR_1] = {.type = NLA_U8},
75 [DCB_PFC_UP_ATTR_2] = {.type = NLA_U8},
76 [DCB_PFC_UP_ATTR_3] = {.type = NLA_U8},
77 [DCB_PFC_UP_ATTR_4] = {.type = NLA_U8},
78 [DCB_PFC_UP_ATTR_5] = {.type = NLA_U8},
79 [DCB_PFC_UP_ATTR_6] = {.type = NLA_U8},
80 [DCB_PFC_UP_ATTR_7] = {.type = NLA_U8},
81 [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
82};
83
84/* DCB priority grouping nested attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000085static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -080086 [DCB_PG_ATTR_TC_0] = {.type = NLA_NESTED},
87 [DCB_PG_ATTR_TC_1] = {.type = NLA_NESTED},
88 [DCB_PG_ATTR_TC_2] = {.type = NLA_NESTED},
89 [DCB_PG_ATTR_TC_3] = {.type = NLA_NESTED},
90 [DCB_PG_ATTR_TC_4] = {.type = NLA_NESTED},
91 [DCB_PG_ATTR_TC_5] = {.type = NLA_NESTED},
92 [DCB_PG_ATTR_TC_6] = {.type = NLA_NESTED},
93 [DCB_PG_ATTR_TC_7] = {.type = NLA_NESTED},
94 [DCB_PG_ATTR_TC_ALL] = {.type = NLA_NESTED},
95 [DCB_PG_ATTR_BW_ID_0] = {.type = NLA_U8},
96 [DCB_PG_ATTR_BW_ID_1] = {.type = NLA_U8},
97 [DCB_PG_ATTR_BW_ID_2] = {.type = NLA_U8},
98 [DCB_PG_ATTR_BW_ID_3] = {.type = NLA_U8},
99 [DCB_PG_ATTR_BW_ID_4] = {.type = NLA_U8},
100 [DCB_PG_ATTR_BW_ID_5] = {.type = NLA_U8},
101 [DCB_PG_ATTR_BW_ID_6] = {.type = NLA_U8},
102 [DCB_PG_ATTR_BW_ID_7] = {.type = NLA_U8},
103 [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
104};
105
106/* DCB traffic class nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000107static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -0800108 [DCB_TC_ATTR_PARAM_PGID] = {.type = NLA_U8},
109 [DCB_TC_ATTR_PARAM_UP_MAPPING] = {.type = NLA_U8},
110 [DCB_TC_ATTR_PARAM_STRICT_PRIO] = {.type = NLA_U8},
111 [DCB_TC_ATTR_PARAM_BW_PCT] = {.type = NLA_U8},
112 [DCB_TC_ATTR_PARAM_ALL] = {.type = NLA_FLAG},
113};
114
Alexander Duyck46132182008-11-20 21:05:08 -0800115/* DCB capabilities nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000116static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
Alexander Duyck46132182008-11-20 21:05:08 -0800117 [DCB_CAP_ATTR_ALL] = {.type = NLA_FLAG},
118 [DCB_CAP_ATTR_PG] = {.type = NLA_U8},
119 [DCB_CAP_ATTR_PFC] = {.type = NLA_U8},
120 [DCB_CAP_ATTR_UP2TC] = {.type = NLA_U8},
121 [DCB_CAP_ATTR_PG_TCS] = {.type = NLA_U8},
122 [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
123 [DCB_CAP_ATTR_GSP] = {.type = NLA_U8},
124 [DCB_CAP_ATTR_BCN] = {.type = NLA_U8},
Shmulik Ravid6241b622010-12-30 06:26:48 +0000125 [DCB_CAP_ATTR_DCBX] = {.type = NLA_U8},
Alexander Duyck46132182008-11-20 21:05:08 -0800126};
Alexander Duyck2f90b862008-11-20 20:52:10 -0800127
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800128/* DCB capabilities nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000129static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800130 [DCB_NUMTCS_ATTR_ALL] = {.type = NLA_FLAG},
131 [DCB_NUMTCS_ATTR_PG] = {.type = NLA_U8},
132 [DCB_NUMTCS_ATTR_PFC] = {.type = NLA_U8},
133};
134
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800135/* DCB BCN nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000136static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800137 [DCB_BCN_ATTR_RP_0] = {.type = NLA_U8},
138 [DCB_BCN_ATTR_RP_1] = {.type = NLA_U8},
139 [DCB_BCN_ATTR_RP_2] = {.type = NLA_U8},
140 [DCB_BCN_ATTR_RP_3] = {.type = NLA_U8},
141 [DCB_BCN_ATTR_RP_4] = {.type = NLA_U8},
142 [DCB_BCN_ATTR_RP_5] = {.type = NLA_U8},
143 [DCB_BCN_ATTR_RP_6] = {.type = NLA_U8},
144 [DCB_BCN_ATTR_RP_7] = {.type = NLA_U8},
145 [DCB_BCN_ATTR_RP_ALL] = {.type = NLA_FLAG},
Don Skidmoref4314e82008-12-21 20:10:29 -0800146 [DCB_BCN_ATTR_BCNA_0] = {.type = NLA_U32},
147 [DCB_BCN_ATTR_BCNA_1] = {.type = NLA_U32},
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800148 [DCB_BCN_ATTR_ALPHA] = {.type = NLA_U32},
149 [DCB_BCN_ATTR_BETA] = {.type = NLA_U32},
150 [DCB_BCN_ATTR_GD] = {.type = NLA_U32},
151 [DCB_BCN_ATTR_GI] = {.type = NLA_U32},
152 [DCB_BCN_ATTR_TMAX] = {.type = NLA_U32},
153 [DCB_BCN_ATTR_TD] = {.type = NLA_U32},
154 [DCB_BCN_ATTR_RMIN] = {.type = NLA_U32},
155 [DCB_BCN_ATTR_W] = {.type = NLA_U32},
156 [DCB_BCN_ATTR_RD] = {.type = NLA_U32},
157 [DCB_BCN_ATTR_RU] = {.type = NLA_U32},
158 [DCB_BCN_ATTR_WRTT] = {.type = NLA_U32},
159 [DCB_BCN_ATTR_RI] = {.type = NLA_U32},
160 [DCB_BCN_ATTR_C] = {.type = NLA_U32},
161 [DCB_BCN_ATTR_ALL] = {.type = NLA_FLAG},
162};
163
Yi Zou6fa382a2009-08-31 12:33:20 +0000164/* DCB APP nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000165static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
Yi Zou6fa382a2009-08-31 12:33:20 +0000166 [DCB_APP_ATTR_IDTYPE] = {.type = NLA_U8},
167 [DCB_APP_ATTR_ID] = {.type = NLA_U16},
168 [DCB_APP_ATTR_PRIORITY] = {.type = NLA_U8},
169};
170
John Fastabend3e290272010-12-30 09:25:46 +0000171/* IEEE 802.1Qaz nested attributes. */
172static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
173 [DCB_ATTR_IEEE_ETS] = {.len = sizeof(struct ieee_ets)},
174 [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
175 [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
Amir Vadai08f10af2012-04-04 21:33:30 +0000176 [DCB_ATTR_IEEE_MAXRATE] = {.len = sizeof(struct ieee_maxrate)},
Shani Michaelic93682472015-03-05 20:16:11 +0200177 [DCB_ATTR_IEEE_QCN] = {.len = sizeof(struct ieee_qcn)},
178 [DCB_ATTR_IEEE_QCN_STATS] = {.len = sizeof(struct ieee_qcn_stats)},
Huy Nguyene549f6f2018-02-22 11:57:10 -0600179 [DCB_ATTR_DCB_BUFFER] = {.len = sizeof(struct dcbnl_buffer)},
John Fastabend3e290272010-12-30 09:25:46 +0000180};
181
Shmulik Ravidea45fe42010-12-30 06:26:55 +0000182/* DCB number of traffic classes nested attributes. */
183static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
184 [DCB_FEATCFG_ATTR_ALL] = {.type = NLA_FLAG},
185 [DCB_FEATCFG_ATTR_PG] = {.type = NLA_U8},
186 [DCB_FEATCFG_ATTR_PFC] = {.type = NLA_U8},
187 [DCB_FEATCFG_ATTR_APP] = {.type = NLA_U8},
188};
189
John Fastabend9ab933a2010-12-30 09:26:31 +0000190static LIST_HEAD(dcb_app_list);
191static DEFINE_SPINLOCK(dcb_lock);
192
Thomas Graf33a03aa2012-06-13 02:54:54 +0000193static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
194 u32 flags, struct nlmsghdr **nlhp)
195{
196 struct sk_buff *skb;
197 struct dcbmsg *dcb;
198 struct nlmsghdr *nlh;
199
200 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
201 if (!skb)
202 return NULL;
203
204 nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
Thomas Grafb3908e22012-06-13 22:40:15 +0000205 BUG_ON(!nlh);
Thomas Graf33a03aa2012-06-13 02:54:54 +0000206
207 dcb = nlmsg_data(nlh);
208 dcb->dcb_family = AF_UNSPEC;
209 dcb->cmd = cmd;
210 dcb->dcb_pad = 0;
211
212 if (nlhp)
213 *nlhp = nlh;
214
215 return skb;
216}
217
Thomas Graf7be99412012-06-13 02:54:55 +0000218static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
219 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800220{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800221 /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
222 if (!netdev->dcbnl_ops->getstate)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000223 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800224
Thomas Graf7be99412012-06-13 02:54:55 +0000225 return nla_put_u8(skb, DCB_ATTR_STATE,
226 netdev->dcbnl_ops->getstate(netdev));
Alexander Duyck2f90b862008-11-20 20:52:10 -0800227}
228
Thomas Graf7be99412012-06-13 02:54:55 +0000229static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
230 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800231{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800232 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
233 u8 value;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000234 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800235 int i;
236 int getall = 0;
237
Thomas Graf3d1f4862012-06-13 02:54:58 +0000238 if (!tb[DCB_ATTR_PFC_CFG])
239 return -EINVAL;
240
241 if (!netdev->dcbnl_ops->getpfccfg)
242 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800243
Johannes Berg8cb08172019-04-26 14:07:28 +0200244 ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
245 tb[DCB_ATTR_PFC_CFG],
246 dcbnl_pfc_up_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800247 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000248 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800249
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200250 nest = nla_nest_start_noflag(skb, DCB_ATTR_PFC_CFG);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800251 if (!nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000252 return -EMSGSIZE;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800253
254 if (data[DCB_PFC_UP_ATTR_ALL])
255 getall = 1;
256
257 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
258 if (!getall && !data[i])
259 continue;
260
261 netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
262 &value);
Thomas Graf7be99412012-06-13 02:54:55 +0000263 ret = nla_put_u8(skb, i, value);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800264 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000265 nla_nest_cancel(skb, nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000266 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800267 }
268 }
Thomas Graf7be99412012-06-13 02:54:55 +0000269 nla_nest_end(skb, nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800270
271 return 0;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800272}
273
Thomas Graf7be99412012-06-13 02:54:55 +0000274static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
275 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800276{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800277 u8 perm_addr[MAX_ADDR_LEN];
Alexander Duyck2f90b862008-11-20 20:52:10 -0800278
279 if (!netdev->dcbnl_ops->getpermhwaddr)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000280 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800281
Mathias Krause29cd8ae2013-03-09 05:52:21 +0000282 memset(perm_addr, 0, sizeof(perm_addr));
Alexander Duyck2f90b862008-11-20 20:52:10 -0800283 netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
284
Thomas Graf7be99412012-06-13 02:54:55 +0000285 return nla_put(skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr), perm_addr);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800286}
287
Thomas Graf7be99412012-06-13 02:54:55 +0000288static int dcbnl_getcap(struct net_device *netdev, struct nlmsghdr *nlh,
289 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck46132182008-11-20 21:05:08 -0800290{
Alexander Duyck46132182008-11-20 21:05:08 -0800291 struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
292 u8 value;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000293 int ret;
Alexander Duyck46132182008-11-20 21:05:08 -0800294 int i;
295 int getall = 0;
296
Thomas Graf3d1f4862012-06-13 02:54:58 +0000297 if (!tb[DCB_ATTR_CAP])
298 return -EINVAL;
299
300 if (!netdev->dcbnl_ops->getcap)
301 return -EOPNOTSUPP;
Alexander Duyck46132182008-11-20 21:05:08 -0800302
Johannes Berg8cb08172019-04-26 14:07:28 +0200303 ret = nla_parse_nested_deprecated(data, DCB_CAP_ATTR_MAX,
304 tb[DCB_ATTR_CAP], dcbnl_cap_nest,
305 NULL);
Alexander Duyck46132182008-11-20 21:05:08 -0800306 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000307 return ret;
Alexander Duyck46132182008-11-20 21:05:08 -0800308
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200309 nest = nla_nest_start_noflag(skb, DCB_ATTR_CAP);
Alexander Duyck46132182008-11-20 21:05:08 -0800310 if (!nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000311 return -EMSGSIZE;
Alexander Duyck46132182008-11-20 21:05:08 -0800312
313 if (data[DCB_CAP_ATTR_ALL])
314 getall = 1;
315
316 for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
317 if (!getall && !data[i])
318 continue;
319
320 if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
Thomas Graf7be99412012-06-13 02:54:55 +0000321 ret = nla_put_u8(skb, i, value);
Alexander Duyck46132182008-11-20 21:05:08 -0800322 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000323 nla_nest_cancel(skb, nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000324 return ret;
Alexander Duyck46132182008-11-20 21:05:08 -0800325 }
326 }
327 }
Thomas Graf7be99412012-06-13 02:54:55 +0000328 nla_nest_end(skb, nest);
Alexander Duyck46132182008-11-20 21:05:08 -0800329
330 return 0;
Alexander Duyck46132182008-11-20 21:05:08 -0800331}
332
Thomas Graf7be99412012-06-13 02:54:55 +0000333static int dcbnl_getnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
334 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800335{
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800336 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
337 u8 value;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000338 int ret;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800339 int i;
340 int getall = 0;
341
Thomas Graf3d1f4862012-06-13 02:54:58 +0000342 if (!tb[DCB_ATTR_NUMTCS])
343 return -EINVAL;
344
345 if (!netdev->dcbnl_ops->getnumtcs)
346 return -EOPNOTSUPP;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800347
Johannes Berg8cb08172019-04-26 14:07:28 +0200348 ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
349 tb[DCB_ATTR_NUMTCS],
350 dcbnl_numtcs_nest, NULL);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000351 if (ret)
352 return ret;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800353
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200354 nest = nla_nest_start_noflag(skb, DCB_ATTR_NUMTCS);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000355 if (!nest)
356 return -EMSGSIZE;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800357
358 if (data[DCB_NUMTCS_ATTR_ALL])
359 getall = 1;
360
361 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
362 if (!getall && !data[i])
363 continue;
364
365 ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
366 if (!ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000367 ret = nla_put_u8(skb, i, value);
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800368 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +0000369 nla_nest_cancel(skb, nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000370 return ret;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800371 }
Thomas Graf3d1f4862012-06-13 02:54:58 +0000372 } else
373 return -EINVAL;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800374 }
Thomas Graf7be99412012-06-13 02:54:55 +0000375 nla_nest_end(skb, nest);
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800376
377 return 0;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800378}
379
Thomas Graf7be99412012-06-13 02:54:55 +0000380static int dcbnl_setnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
381 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800382{
383 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
Thomas Graf3d1f4862012-06-13 02:54:58 +0000384 int ret;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800385 u8 value;
386 int i;
387
Thomas Graf3d1f4862012-06-13 02:54:58 +0000388 if (!tb[DCB_ATTR_NUMTCS])
389 return -EINVAL;
390
391 if (!netdev->dcbnl_ops->setnumtcs)
392 return -EOPNOTSUPP;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800393
Johannes Berg8cb08172019-04-26 14:07:28 +0200394 ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
395 tb[DCB_ATTR_NUMTCS],
396 dcbnl_numtcs_nest, NULL);
Thomas Graf7be99412012-06-13 02:54:55 +0000397 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000398 return ret;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800399
400 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
401 if (data[i] == NULL)
402 continue;
403
404 value = nla_get_u8(data[i]);
405
406 ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800407 if (ret)
Thomas Graf7be99412012-06-13 02:54:55 +0000408 break;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800409 }
410
Thomas Graf7be99412012-06-13 02:54:55 +0000411 return nla_put_u8(skb, DCB_ATTR_NUMTCS, !!ret);
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800412}
413
Thomas Graf7be99412012-06-13 02:54:55 +0000414static int dcbnl_getpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
415 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800416{
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800417 if (!netdev->dcbnl_ops->getpfcstate)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000418 return -EOPNOTSUPP;
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800419
Thomas Graf7be99412012-06-13 02:54:55 +0000420 return nla_put_u8(skb, DCB_ATTR_PFC_STATE,
421 netdev->dcbnl_ops->getpfcstate(netdev));
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800422}
423
Thomas Graf7be99412012-06-13 02:54:55 +0000424static int dcbnl_setpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
425 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800426{
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800427 u8 value;
428
Thomas Graf3d1f4862012-06-13 02:54:58 +0000429 if (!tb[DCB_ATTR_PFC_STATE])
Thomas Graf7be99412012-06-13 02:54:55 +0000430 return -EINVAL;
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800431
Thomas Graf3d1f4862012-06-13 02:54:58 +0000432 if (!netdev->dcbnl_ops->setpfcstate)
433 return -EOPNOTSUPP;
434
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800435 value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
436
437 netdev->dcbnl_ops->setpfcstate(netdev, value);
438
Thomas Graf7be99412012-06-13 02:54:55 +0000439 return nla_put_u8(skb, DCB_ATTR_PFC_STATE, 0);
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800440}
441
Thomas Graf7be99412012-06-13 02:54:55 +0000442static int dcbnl_getapp(struct net_device *netdev, struct nlmsghdr *nlh,
443 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Yi Zou57949682009-08-31 12:33:40 +0000444{
Yi Zou57949682009-08-31 12:33:40 +0000445 struct nlattr *app_nest;
446 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
447 u16 id;
448 u8 up, idtype;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000449 int ret;
Yi Zou57949682009-08-31 12:33:40 +0000450
John Fastabend3dce38a2011-01-21 16:35:18 +0000451 if (!tb[DCB_ATTR_APP])
Thomas Graf3d1f4862012-06-13 02:54:58 +0000452 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000453
Johannes Berg8cb08172019-04-26 14:07:28 +0200454 ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
455 tb[DCB_ATTR_APP], dcbnl_app_nest,
456 NULL);
Yi Zou57949682009-08-31 12:33:40 +0000457 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000458 return ret;
Yi Zou57949682009-08-31 12:33:40 +0000459
Yi Zou57949682009-08-31 12:33:40 +0000460 /* all must be non-null */
461 if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
462 (!app_tb[DCB_APP_ATTR_ID]))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000463 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000464
465 /* either by eth type or by socket number */
466 idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
467 if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
468 (idtype != DCB_APP_IDTYPE_PORTNUM))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000469 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000470
471 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
John Fastabend3dce38a2011-01-21 16:35:18 +0000472
473 if (netdev->dcbnl_ops->getapp) {
Anish Bhattc2659472014-07-16 22:32:39 -0700474 ret = netdev->dcbnl_ops->getapp(netdev, idtype, id);
475 if (ret < 0)
476 return ret;
477 else
478 up = ret;
John Fastabend3dce38a2011-01-21 16:35:18 +0000479 } else {
480 struct dcb_app app = {
481 .selector = idtype,
482 .protocol = id,
483 };
484 up = dcb_getapp(netdev, &app);
485 }
Yi Zou57949682009-08-31 12:33:40 +0000486
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200487 app_nest = nla_nest_start_noflag(skb, DCB_ATTR_APP);
Thomas Graf7be99412012-06-13 02:54:55 +0000488 if (!app_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000489 return -EMSGSIZE;
Yi Zou57949682009-08-31 12:33:40 +0000490
Thomas Graf7be99412012-06-13 02:54:55 +0000491 ret = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE, idtype);
Yi Zou57949682009-08-31 12:33:40 +0000492 if (ret)
493 goto out_cancel;
494
Thomas Graf7be99412012-06-13 02:54:55 +0000495 ret = nla_put_u16(skb, DCB_APP_ATTR_ID, id);
Yi Zou57949682009-08-31 12:33:40 +0000496 if (ret)
497 goto out_cancel;
498
Thomas Graf7be99412012-06-13 02:54:55 +0000499 ret = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY, up);
Yi Zou57949682009-08-31 12:33:40 +0000500 if (ret)
501 goto out_cancel;
502
Thomas Graf7be99412012-06-13 02:54:55 +0000503 nla_nest_end(skb, app_nest);
Yi Zou57949682009-08-31 12:33:40 +0000504
Thomas Graf3d1f4862012-06-13 02:54:58 +0000505 return 0;
Yi Zou57949682009-08-31 12:33:40 +0000506
507out_cancel:
Thomas Graf7be99412012-06-13 02:54:55 +0000508 nla_nest_cancel(skb, app_nest);
Yi Zou57949682009-08-31 12:33:40 +0000509 return ret;
510}
511
Thomas Graf7be99412012-06-13 02:54:55 +0000512static int dcbnl_setapp(struct net_device *netdev, struct nlmsghdr *nlh,
513 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Yi Zou57949682009-08-31 12:33:40 +0000514{
Thomas Graf3d1f4862012-06-13 02:54:58 +0000515 int ret;
Yi Zou57949682009-08-31 12:33:40 +0000516 u16 id;
517 u8 up, idtype;
518 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
519
John Fastabend9ab933a2010-12-30 09:26:31 +0000520 if (!tb[DCB_ATTR_APP])
Thomas Graf3d1f4862012-06-13 02:54:58 +0000521 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000522
Johannes Berg8cb08172019-04-26 14:07:28 +0200523 ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
524 tb[DCB_ATTR_APP], dcbnl_app_nest,
525 NULL);
Yi Zou57949682009-08-31 12:33:40 +0000526 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000527 return ret;
Yi Zou57949682009-08-31 12:33:40 +0000528
Yi Zou57949682009-08-31 12:33:40 +0000529 /* all must be non-null */
530 if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
531 (!app_tb[DCB_APP_ATTR_ID]) ||
532 (!app_tb[DCB_APP_ATTR_PRIORITY]))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000533 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000534
535 /* either by eth type or by socket number */
536 idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
537 if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
538 (idtype != DCB_APP_IDTYPE_PORTNUM))
Thomas Graf3d1f4862012-06-13 02:54:58 +0000539 return -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000540
541 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
542 up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
543
John Fastabend9ab933a2010-12-30 09:26:31 +0000544 if (netdev->dcbnl_ops->setapp) {
Thomas Graf3d1f4862012-06-13 02:54:58 +0000545 ret = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
Anish Bhattc2659472014-07-16 22:32:39 -0700546 if (ret < 0)
547 return ret;
John Fastabend9ab933a2010-12-30 09:26:31 +0000548 } else {
549 struct dcb_app app;
550 app.selector = idtype;
551 app.protocol = id;
552 app.priority = up;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000553 ret = dcb_setapp(netdev, &app);
John Fastabend9ab933a2010-12-30 09:26:31 +0000554 }
555
Thomas Graf7be99412012-06-13 02:54:55 +0000556 ret = nla_put_u8(skb, DCB_ATTR_APP, ret);
John Fastabend08157982012-04-20 09:49:23 +0000557 dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SAPP, seq, 0);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000558
Yi Zou57949682009-08-31 12:33:40 +0000559 return ret;
560}
561
Thomas Graf7be99412012-06-13 02:54:55 +0000562static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
563 struct nlattr **tb, struct sk_buff *skb, int dir)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800564{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800565 struct nlattr *pg_nest, *param_nest, *data;
566 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
567 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
568 u8 prio, pgid, tc_pct, up_map;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000569 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800570 int getall = 0;
571 int i;
572
Thomas Graf3d1f4862012-06-13 02:54:58 +0000573 if (!tb[DCB_ATTR_PG_CFG])
574 return -EINVAL;
575
576 if (!netdev->dcbnl_ops->getpgtccfgtx ||
Alexander Duyck2f90b862008-11-20 20:52:10 -0800577 !netdev->dcbnl_ops->getpgtccfgrx ||
578 !netdev->dcbnl_ops->getpgbwgcfgtx ||
579 !netdev->dcbnl_ops->getpgbwgcfgrx)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000580 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800581
Johannes Berg8cb08172019-04-26 14:07:28 +0200582 ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
583 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
584 NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800585 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000586 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800587
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200588 pg_nest = nla_nest_start_noflag(skb, DCB_ATTR_PG_CFG);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800589 if (!pg_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000590 return -EMSGSIZE;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800591
592 if (pg_tb[DCB_PG_ATTR_TC_ALL])
593 getall = 1;
594
595 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
596 if (!getall && !pg_tb[i])
597 continue;
598
599 if (pg_tb[DCB_PG_ATTR_TC_ALL])
600 data = pg_tb[DCB_PG_ATTR_TC_ALL];
601 else
602 data = pg_tb[i];
Johannes Berg8cb08172019-04-26 14:07:28 +0200603 ret = nla_parse_nested_deprecated(param_tb,
604 DCB_TC_ATTR_PARAM_MAX, data,
605 dcbnl_tc_param_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800606 if (ret)
607 goto err_pg;
608
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200609 param_nest = nla_nest_start_noflag(skb, i);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800610 if (!param_nest)
611 goto err_pg;
612
613 pgid = DCB_ATTR_VALUE_UNDEFINED;
614 prio = DCB_ATTR_VALUE_UNDEFINED;
615 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
616 up_map = DCB_ATTR_VALUE_UNDEFINED;
617
618 if (dir) {
619 /* Rx */
620 netdev->dcbnl_ops->getpgtccfgrx(netdev,
621 i - DCB_PG_ATTR_TC_0, &prio,
622 &pgid, &tc_pct, &up_map);
623 } else {
624 /* Tx */
625 netdev->dcbnl_ops->getpgtccfgtx(netdev,
626 i - DCB_PG_ATTR_TC_0, &prio,
627 &pgid, &tc_pct, &up_map);
628 }
629
630 if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
631 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000632 ret = nla_put_u8(skb,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800633 DCB_TC_ATTR_PARAM_PGID, pgid);
634 if (ret)
635 goto err_param;
636 }
637 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
638 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000639 ret = nla_put_u8(skb,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800640 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
641 if (ret)
642 goto err_param;
643 }
644 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
645 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000646 ret = nla_put_u8(skb,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800647 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
648 if (ret)
649 goto err_param;
650 }
651 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
652 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
Thomas Graf7be99412012-06-13 02:54:55 +0000653 ret = nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800654 tc_pct);
655 if (ret)
656 goto err_param;
657 }
Thomas Graf7be99412012-06-13 02:54:55 +0000658 nla_nest_end(skb, param_nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800659 }
660
661 if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
662 getall = 1;
663 else
664 getall = 0;
665
666 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
667 if (!getall && !pg_tb[i])
668 continue;
669
670 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
671
672 if (dir) {
673 /* Rx */
674 netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
675 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
676 } else {
677 /* Tx */
678 netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
679 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
680 }
Thomas Graf7be99412012-06-13 02:54:55 +0000681 ret = nla_put_u8(skb, i, tc_pct);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800682 if (ret)
683 goto err_pg;
684 }
685
Thomas Graf7be99412012-06-13 02:54:55 +0000686 nla_nest_end(skb, pg_nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800687
688 return 0;
689
690err_param:
Thomas Graf7be99412012-06-13 02:54:55 +0000691 nla_nest_cancel(skb, param_nest);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800692err_pg:
Thomas Graf7be99412012-06-13 02:54:55 +0000693 nla_nest_cancel(skb, pg_nest);
Thomas Graf3d1f4862012-06-13 02:54:58 +0000694
695 return -EMSGSIZE;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800696}
697
Thomas Graf7be99412012-06-13 02:54:55 +0000698static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
699 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800700{
Thomas Graf7be99412012-06-13 02:54:55 +0000701 return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800702}
703
Thomas Graf7be99412012-06-13 02:54:55 +0000704static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
705 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800706{
Thomas Graf7be99412012-06-13 02:54:55 +0000707 return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 1);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800708}
709
Thomas Graf7be99412012-06-13 02:54:55 +0000710static int dcbnl_setstate(struct net_device *netdev, struct nlmsghdr *nlh,
711 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800712{
Alexander Duyck2f90b862008-11-20 20:52:10 -0800713 u8 value;
714
Thomas Graf3d1f4862012-06-13 02:54:58 +0000715 if (!tb[DCB_ATTR_STATE])
Thomas Graf7be99412012-06-13 02:54:55 +0000716 return -EINVAL;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800717
Thomas Graf3d1f4862012-06-13 02:54:58 +0000718 if (!netdev->dcbnl_ops->setstate)
719 return -EOPNOTSUPP;
720
Alexander Duyck2f90b862008-11-20 20:52:10 -0800721 value = nla_get_u8(tb[DCB_ATTR_STATE]);
722
Thomas Graf7be99412012-06-13 02:54:55 +0000723 return nla_put_u8(skb, DCB_ATTR_STATE,
724 netdev->dcbnl_ops->setstate(netdev, value));
Alexander Duyck2f90b862008-11-20 20:52:10 -0800725}
726
Thomas Graf7be99412012-06-13 02:54:55 +0000727static int dcbnl_setpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
728 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800729{
730 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
731 int i;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000732 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800733 u8 value;
734
Thomas Graf3d1f4862012-06-13 02:54:58 +0000735 if (!tb[DCB_ATTR_PFC_CFG])
736 return -EINVAL;
737
738 if (!netdev->dcbnl_ops->setpfccfg)
739 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800740
Johannes Berg8cb08172019-04-26 14:07:28 +0200741 ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
742 tb[DCB_ATTR_PFC_CFG],
743 dcbnl_pfc_up_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800744 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000745 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800746
747 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
748 if (data[i] == NULL)
749 continue;
750 value = nla_get_u8(data[i]);
751 netdev->dcbnl_ops->setpfccfg(netdev,
752 data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
753 }
754
Thomas Graf7be99412012-06-13 02:54:55 +0000755 return nla_put_u8(skb, DCB_ATTR_PFC_CFG, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800756}
757
Thomas Graf7be99412012-06-13 02:54:55 +0000758static int dcbnl_setall(struct net_device *netdev, struct nlmsghdr *nlh,
759 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800760{
Thomas Graf3d1f4862012-06-13 02:54:58 +0000761 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800762
Thomas Graf3d1f4862012-06-13 02:54:58 +0000763 if (!tb[DCB_ATTR_SET_ALL])
764 return -EINVAL;
765
766 if (!netdev->dcbnl_ops->setall)
767 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800768
Thomas Graf7be99412012-06-13 02:54:55 +0000769 ret = nla_put_u8(skb, DCB_ATTR_SET_ALL,
770 netdev->dcbnl_ops->setall(netdev));
John Fastabend08157982012-04-20 09:49:23 +0000771 dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SET_ALL, seq, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800772
773 return ret;
774}
775
Thomas Graf7be99412012-06-13 02:54:55 +0000776static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
777 u32 seq, struct nlattr **tb, struct sk_buff *skb,
778 int dir)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800779{
780 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
781 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
Thomas Graf3d1f4862012-06-13 02:54:58 +0000782 int ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800783 int i;
784 u8 pgid;
785 u8 up_map;
786 u8 prio;
787 u8 tc_pct;
788
Thomas Graf3d1f4862012-06-13 02:54:58 +0000789 if (!tb[DCB_ATTR_PG_CFG])
790 return -EINVAL;
791
792 if (!netdev->dcbnl_ops->setpgtccfgtx ||
Alexander Duyck2f90b862008-11-20 20:52:10 -0800793 !netdev->dcbnl_ops->setpgtccfgrx ||
794 !netdev->dcbnl_ops->setpgbwgcfgtx ||
795 !netdev->dcbnl_ops->setpgbwgcfgrx)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000796 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800797
Johannes Berg8cb08172019-04-26 14:07:28 +0200798 ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
799 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
800 NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800801 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000802 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800803
804 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
805 if (!pg_tb[i])
806 continue;
807
Johannes Berg8cb08172019-04-26 14:07:28 +0200808 ret = nla_parse_nested_deprecated(param_tb,
809 DCB_TC_ATTR_PARAM_MAX,
810 pg_tb[i],
811 dcbnl_tc_param_nest, NULL);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800812 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000813 return ret;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800814
815 pgid = DCB_ATTR_VALUE_UNDEFINED;
816 prio = DCB_ATTR_VALUE_UNDEFINED;
817 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
818 up_map = DCB_ATTR_VALUE_UNDEFINED;
819
820 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
821 prio =
822 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
823
824 if (param_tb[DCB_TC_ATTR_PARAM_PGID])
825 pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
826
827 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
828 tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
829
830 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
831 up_map =
832 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
833
834 /* dir: Tx = 0, Rx = 1 */
835 if (dir) {
836 /* Rx */
837 netdev->dcbnl_ops->setpgtccfgrx(netdev,
838 i - DCB_PG_ATTR_TC_0,
839 prio, pgid, tc_pct, up_map);
840 } else {
841 /* Tx */
842 netdev->dcbnl_ops->setpgtccfgtx(netdev,
843 i - DCB_PG_ATTR_TC_0,
844 prio, pgid, tc_pct, up_map);
845 }
846 }
847
848 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
849 if (!pg_tb[i])
850 continue;
851
852 tc_pct = nla_get_u8(pg_tb[i]);
853
854 /* dir: Tx = 0, Rx = 1 */
855 if (dir) {
856 /* Rx */
857 netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
858 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
859 } else {
860 /* Tx */
861 netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
862 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
863 }
864 }
865
John Fastabendbb1dfef2012-06-20 19:56:21 +0000866 return nla_put_u8(skb, DCB_ATTR_PG_CFG, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800867}
868
Thomas Graf7be99412012-06-13 02:54:55 +0000869static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
870 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800871{
Thomas Graf7be99412012-06-13 02:54:55 +0000872 return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800873}
874
Thomas Graf7be99412012-06-13 02:54:55 +0000875static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
876 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck2f90b862008-11-20 20:52:10 -0800877{
Thomas Graf7be99412012-06-13 02:54:55 +0000878 return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800879}
880
Thomas Graf7be99412012-06-13 02:54:55 +0000881static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
882 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800883{
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800884 struct nlattr *bcn_nest;
885 struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
886 u8 value_byte;
887 u32 value_integer;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000888 int ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800889 bool getall = false;
890 int i;
891
Thomas Graf3d1f4862012-06-13 02:54:58 +0000892 if (!tb[DCB_ATTR_BCN])
893 return -EINVAL;
894
895 if (!netdev->dcbnl_ops->getbcnrp ||
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800896 !netdev->dcbnl_ops->getbcncfg)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000897 return -EOPNOTSUPP;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800898
Johannes Berg8cb08172019-04-26 14:07:28 +0200899 ret = nla_parse_nested_deprecated(bcn_tb, DCB_BCN_ATTR_MAX,
900 tb[DCB_ATTR_BCN], dcbnl_bcn_nest,
901 NULL);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800902 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000903 return ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800904
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200905 bcn_nest = nla_nest_start_noflag(skb, DCB_ATTR_BCN);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800906 if (!bcn_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000907 return -EMSGSIZE;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800908
909 if (bcn_tb[DCB_BCN_ATTR_ALL])
910 getall = true;
911
912 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
913 if (!getall && !bcn_tb[i])
914 continue;
915
916 netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
917 &value_byte);
Thomas Graf7be99412012-06-13 02:54:55 +0000918 ret = nla_put_u8(skb, i, value_byte);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800919 if (ret)
920 goto err_bcn;
921 }
922
Don Skidmoref4314e82008-12-21 20:10:29 -0800923 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800924 if (!getall && !bcn_tb[i])
925 continue;
926
927 netdev->dcbnl_ops->getbcncfg(netdev, i,
928 &value_integer);
Thomas Graf7be99412012-06-13 02:54:55 +0000929 ret = nla_put_u32(skb, i, value_integer);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800930 if (ret)
931 goto err_bcn;
932 }
933
Thomas Graf7be99412012-06-13 02:54:55 +0000934 nla_nest_end(skb, bcn_nest);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800935
936 return 0;
937
938err_bcn:
Thomas Graf7be99412012-06-13 02:54:55 +0000939 nla_nest_cancel(skb, bcn_nest);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800940 return ret;
941}
942
Thomas Graf7be99412012-06-13 02:54:55 +0000943static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
944 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800945{
946 struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
947 int i;
Thomas Graf3d1f4862012-06-13 02:54:58 +0000948 int ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800949 u8 value_byte;
950 u32 value_int;
951
Thomas Graf3d1f4862012-06-13 02:54:58 +0000952 if (!tb[DCB_ATTR_BCN])
953 return -EINVAL;
954
955 if (!netdev->dcbnl_ops->setbcncfg ||
Joe Perchesf64f9e72009-11-29 16:55:45 -0800956 !netdev->dcbnl_ops->setbcnrp)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000957 return -EOPNOTSUPP;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800958
Johannes Berg8cb08172019-04-26 14:07:28 +0200959 ret = nla_parse_nested_deprecated(data, DCB_BCN_ATTR_MAX,
960 tb[DCB_ATTR_BCN], dcbnl_pfc_up_nest,
961 NULL);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800962 if (ret)
Thomas Graf3d1f4862012-06-13 02:54:58 +0000963 return ret;
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800964
965 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
966 if (data[i] == NULL)
967 continue;
968 value_byte = nla_get_u8(data[i]);
969 netdev->dcbnl_ops->setbcnrp(netdev,
970 data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
971 }
972
Don Skidmoref4314e82008-12-21 20:10:29 -0800973 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800974 if (data[i] == NULL)
975 continue;
976 value_int = nla_get_u32(data[i]);
977 netdev->dcbnl_ops->setbcncfg(netdev,
978 i, value_int);
979 }
980
Thomas Graf3d1f4862012-06-13 02:54:58 +0000981 return nla_put_u8(skb, DCB_ATTR_BCN, 0);
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800982}
983
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +0000984static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
985 int app_nested_type, int app_info_type,
986 int app_entry_type)
Shmulik Ravideed84712011-02-27 05:04:31 +0000987{
988 struct dcb_peer_app_info info;
989 struct dcb_app *table = NULL;
990 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
991 u16 app_count;
992 int err;
993
994
995 /**
996 * retrieve the peer app configuration form the driver. If the driver
997 * handlers fail exit without doing anything
998 */
999 err = ops->peer_getappinfo(netdev, &info, &app_count);
1000 if (!err && app_count) {
Kees Cook6da2ec52018-06-12 13:55:00 -07001001 table = kmalloc_array(app_count, sizeof(struct dcb_app),
1002 GFP_KERNEL);
Shmulik Ravideed84712011-02-27 05:04:31 +00001003 if (!table)
1004 return -ENOMEM;
1005
1006 err = ops->peer_getapptable(netdev, table);
1007 }
1008
1009 if (!err) {
1010 u16 i;
1011 struct nlattr *app;
1012
1013 /**
1014 * build the message, from here on the only possible failure
1015 * is due to the skb size
1016 */
1017 err = -EMSGSIZE;
1018
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001019 app = nla_nest_start_noflag(skb, app_nested_type);
Shmulik Ravideed84712011-02-27 05:04:31 +00001020 if (!app)
1021 goto nla_put_failure;
1022
David S. Miller1eb4c972012-04-01 20:03:01 -04001023 if (app_info_type &&
1024 nla_put(skb, app_info_type, sizeof(info), &info))
1025 goto nla_put_failure;
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001026
David S. Miller1eb4c972012-04-01 20:03:01 -04001027 for (i = 0; i < app_count; i++) {
1028 if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
1029 &table[i]))
1030 goto nla_put_failure;
1031 }
Shmulik Ravideed84712011-02-27 05:04:31 +00001032 nla_nest_end(skb, app);
1033 }
1034 err = 0;
1035
1036nla_put_failure:
1037 kfree(table);
1038 return err;
1039}
John Fastabend3e290272010-12-30 09:25:46 +00001040
Shani Michaelic93682472015-03-05 20:16:11 +02001041/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb GET commands. */
John Fastabend314b4772011-06-21 07:34:37 +00001042static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
John Fastabend3e290272010-12-30 09:25:46 +00001043{
John Fastabend9ab933a2010-12-30 09:26:31 +00001044 struct nlattr *ieee, *app;
1045 struct dcb_app_type *itr;
John Fastabend3e290272010-12-30 09:25:46 +00001046 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
John Fastabendc7797ba2011-06-21 07:34:31 +00001047 int dcbx;
Thomas Graf3d1f4862012-06-13 02:54:58 +00001048 int err;
John Fastabend3e290272010-12-30 09:25:46 +00001049
David S. Miller1eb4c972012-04-01 20:03:01 -04001050 if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001051 return -EMSGSIZE;
1052
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001053 ieee = nla_nest_start_noflag(skb, DCB_ATTR_IEEE);
John Fastabend3e290272010-12-30 09:25:46 +00001054 if (!ieee)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001055 return -EMSGSIZE;
John Fastabend3e290272010-12-30 09:25:46 +00001056
1057 if (ops->ieee_getets) {
1058 struct ieee_ets ets;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001059 memset(&ets, 0, sizeof(ets));
John Fastabend3e290272010-12-30 09:25:46 +00001060 err = ops->ieee_getets(netdev, &ets);
David S. Miller1eb4c972012-04-01 20:03:01 -04001061 if (!err &&
1062 nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001063 return -EMSGSIZE;
John Fastabend3e290272010-12-30 09:25:46 +00001064 }
1065
Amir Vadai08f10af2012-04-04 21:33:30 +00001066 if (ops->ieee_getmaxrate) {
1067 struct ieee_maxrate maxrate;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001068 memset(&maxrate, 0, sizeof(maxrate));
Amir Vadai08f10af2012-04-04 21:33:30 +00001069 err = ops->ieee_getmaxrate(netdev, &maxrate);
1070 if (!err) {
1071 err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
1072 sizeof(maxrate), &maxrate);
1073 if (err)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001074 return -EMSGSIZE;
Amir Vadai08f10af2012-04-04 21:33:30 +00001075 }
1076 }
1077
Shani Michaelic93682472015-03-05 20:16:11 +02001078 if (ops->ieee_getqcn) {
1079 struct ieee_qcn qcn;
1080
1081 memset(&qcn, 0, sizeof(qcn));
1082 err = ops->ieee_getqcn(netdev, &qcn);
1083 if (!err) {
1084 err = nla_put(skb, DCB_ATTR_IEEE_QCN,
1085 sizeof(qcn), &qcn);
1086 if (err)
1087 return -EMSGSIZE;
1088 }
1089 }
1090
1091 if (ops->ieee_getqcnstats) {
1092 struct ieee_qcn_stats qcn_stats;
1093
1094 memset(&qcn_stats, 0, sizeof(qcn_stats));
1095 err = ops->ieee_getqcnstats(netdev, &qcn_stats);
1096 if (!err) {
1097 err = nla_put(skb, DCB_ATTR_IEEE_QCN_STATS,
1098 sizeof(qcn_stats), &qcn_stats);
1099 if (err)
1100 return -EMSGSIZE;
1101 }
1102 }
1103
John Fastabend3e290272010-12-30 09:25:46 +00001104 if (ops->ieee_getpfc) {
1105 struct ieee_pfc pfc;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001106 memset(&pfc, 0, sizeof(pfc));
John Fastabend3e290272010-12-30 09:25:46 +00001107 err = ops->ieee_getpfc(netdev, &pfc);
David S. Miller1eb4c972012-04-01 20:03:01 -04001108 if (!err &&
1109 nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001110 return -EMSGSIZE;
John Fastabend3e290272010-12-30 09:25:46 +00001111 }
1112
Huy Nguyene549f6f2018-02-22 11:57:10 -06001113 if (ops->dcbnl_getbuffer) {
1114 struct dcbnl_buffer buffer;
1115
1116 memset(&buffer, 0, sizeof(buffer));
1117 err = ops->dcbnl_getbuffer(netdev, &buffer);
1118 if (!err &&
1119 nla_put(skb, DCB_ATTR_DCB_BUFFER, sizeof(buffer), &buffer))
1120 return -EMSGSIZE;
1121 }
1122
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001123 app = nla_nest_start_noflag(skb, DCB_ATTR_IEEE_APP_TABLE);
John Fastabend9ab933a2010-12-30 09:26:31 +00001124 if (!app)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001125 return -EMSGSIZE;
John Fastabend9ab933a2010-12-30 09:26:31 +00001126
Anish Bhatt52cff742014-11-14 16:38:31 -08001127 spin_lock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00001128 list_for_each_entry(itr, &dcb_app_list, list) {
Mark Rustade290ed82011-10-06 08:52:33 +00001129 if (itr->ifindex == netdev->ifindex) {
Dan Carpenter70bfa2d2011-01-04 21:03:12 +00001130 err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
1131 &itr->app);
1132 if (err) {
Anish Bhatt52cff742014-11-14 16:38:31 -08001133 spin_unlock_bh(&dcb_lock);
Thomas Graf3d1f4862012-06-13 02:54:58 +00001134 return -EMSGSIZE;
Dan Carpenter70bfa2d2011-01-04 21:03:12 +00001135 }
1136 }
John Fastabend9ab933a2010-12-30 09:26:31 +00001137 }
John Fastabendc7797ba2011-06-21 07:34:31 +00001138
1139 if (netdev->dcbnl_ops->getdcbx)
1140 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1141 else
1142 dcbx = -EOPNOTSUPP;
1143
Anish Bhatt52cff742014-11-14 16:38:31 -08001144 spin_unlock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00001145 nla_nest_end(skb, app);
1146
Shmulik Ravideed84712011-02-27 05:04:31 +00001147 /* get peer info if available */
1148 if (ops->ieee_peer_getets) {
1149 struct ieee_ets ets;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001150 memset(&ets, 0, sizeof(ets));
Shmulik Ravideed84712011-02-27 05:04:31 +00001151 err = ops->ieee_peer_getets(netdev, &ets);
David S. Miller1eb4c972012-04-01 20:03:01 -04001152 if (!err &&
1153 nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001154 return -EMSGSIZE;
Shmulik Ravideed84712011-02-27 05:04:31 +00001155 }
1156
1157 if (ops->ieee_peer_getpfc) {
1158 struct ieee_pfc pfc;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001159 memset(&pfc, 0, sizeof(pfc));
Shmulik Ravideed84712011-02-27 05:04:31 +00001160 err = ops->ieee_peer_getpfc(netdev, &pfc);
David S. Miller1eb4c972012-04-01 20:03:01 -04001161 if (!err &&
1162 nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001163 return -EMSGSIZE;
Shmulik Ravideed84712011-02-27 05:04:31 +00001164 }
1165
1166 if (ops->peer_getappinfo && ops->peer_getapptable) {
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001167 err = dcbnl_build_peer_app(netdev, skb,
1168 DCB_ATTR_IEEE_PEER_APP,
1169 DCB_ATTR_IEEE_APP_UNSPEC,
1170 DCB_ATTR_IEEE_APP);
Shmulik Ravideed84712011-02-27 05:04:31 +00001171 if (err)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001172 return -EMSGSIZE;
Shmulik Ravideed84712011-02-27 05:04:31 +00001173 }
1174
John Fastabend3e290272010-12-30 09:25:46 +00001175 nla_nest_end(skb, ieee);
John Fastabendc7797ba2011-06-21 07:34:31 +00001176 if (dcbx >= 0) {
1177 err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1178 if (err)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001179 return -EMSGSIZE;
John Fastabendc7797ba2011-06-21 07:34:31 +00001180 }
John Fastabend3e290272010-12-30 09:25:46 +00001181
John Fastabend314b4772011-06-21 07:34:37 +00001182 return 0;
John Fastabend3e290272010-12-30 09:25:46 +00001183}
1184
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001185static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
1186 int dir)
1187{
1188 u8 pgid, up_map, prio, tc_pct;
1189 const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1190 int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001191 struct nlattr *pg = nla_nest_start_noflag(skb, i);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001192
1193 if (!pg)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001194 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001195
1196 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001197 struct nlattr *tc_nest = nla_nest_start_noflag(skb, i);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001198
1199 if (!tc_nest)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001200 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001201
1202 pgid = DCB_ATTR_VALUE_UNDEFINED;
1203 prio = DCB_ATTR_VALUE_UNDEFINED;
1204 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1205 up_map = DCB_ATTR_VALUE_UNDEFINED;
1206
1207 if (!dir)
1208 ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
1209 &prio, &pgid, &tc_pct, &up_map);
1210 else
1211 ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
1212 &prio, &pgid, &tc_pct, &up_map);
1213
David S. Miller1eb4c972012-04-01 20:03:01 -04001214 if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
1215 nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
1216 nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
1217 nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001218 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001219 nla_nest_end(skb, tc_nest);
1220 }
1221
1222 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
1223 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1224
1225 if (!dir)
1226 ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
1227 &tc_pct);
1228 else
1229 ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
1230 &tc_pct);
David S. Miller1eb4c972012-04-01 20:03:01 -04001231 if (nla_put_u8(skb, i, tc_pct))
Thomas Graf3d1f4862012-06-13 02:54:58 +00001232 return -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001233 }
1234 nla_nest_end(skb, pg);
1235 return 0;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001236}
1237
1238static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
1239{
1240 struct nlattr *cee, *app;
1241 struct dcb_app_type *itr;
1242 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1243 int dcbx, i, err = -EMSGSIZE;
1244 u8 value;
1245
David S. Miller1eb4c972012-04-01 20:03:01 -04001246 if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
1247 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001248 cee = nla_nest_start_noflag(skb, DCB_ATTR_CEE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001249 if (!cee)
1250 goto nla_put_failure;
1251
1252 /* local pg */
1253 if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
1254 err = dcbnl_cee_pg_fill(skb, netdev, 1);
1255 if (err)
1256 goto nla_put_failure;
1257 }
1258
1259 if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
1260 err = dcbnl_cee_pg_fill(skb, netdev, 0);
1261 if (err)
1262 goto nla_put_failure;
1263 }
1264
1265 /* local pfc */
1266 if (ops->getpfccfg) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001267 struct nlattr *pfc_nest = nla_nest_start_noflag(skb,
1268 DCB_ATTR_CEE_PFC);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001269
1270 if (!pfc_nest)
1271 goto nla_put_failure;
1272
1273 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
1274 ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
David S. Miller1eb4c972012-04-01 20:03:01 -04001275 if (nla_put_u8(skb, i, value))
1276 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001277 }
1278 nla_nest_end(skb, pfc_nest);
1279 }
1280
1281 /* local app */
Anish Bhatt52cff742014-11-14 16:38:31 -08001282 spin_lock_bh(&dcb_lock);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001283 app = nla_nest_start_noflag(skb, DCB_ATTR_CEE_APP_TABLE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001284 if (!app)
Dan Carpenter40f5d722011-07-07 21:27:24 +00001285 goto dcb_unlock;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001286
1287 list_for_each_entry(itr, &dcb_app_list, list) {
Mark Rustade290ed82011-10-06 08:52:33 +00001288 if (itr->ifindex == netdev->ifindex) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001289 struct nlattr *app_nest = nla_nest_start_noflag(skb,
1290 DCB_ATTR_APP);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001291 if (!app_nest)
1292 goto dcb_unlock;
1293
1294 err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
1295 itr->app.selector);
1296 if (err)
1297 goto dcb_unlock;
1298
1299 err = nla_put_u16(skb, DCB_APP_ATTR_ID,
1300 itr->app.protocol);
1301 if (err)
1302 goto dcb_unlock;
1303
1304 err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
1305 itr->app.priority);
1306 if (err)
1307 goto dcb_unlock;
1308
1309 nla_nest_end(skb, app_nest);
1310 }
1311 }
1312 nla_nest_end(skb, app);
1313
1314 if (netdev->dcbnl_ops->getdcbx)
1315 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1316 else
1317 dcbx = -EOPNOTSUPP;
1318
Anish Bhatt52cff742014-11-14 16:38:31 -08001319 spin_unlock_bh(&dcb_lock);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001320
1321 /* features flags */
1322 if (ops->getfeatcfg) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001323 struct nlattr *feat = nla_nest_start_noflag(skb,
1324 DCB_ATTR_CEE_FEAT);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001325 if (!feat)
1326 goto nla_put_failure;
1327
1328 for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
1329 i++)
David S. Miller1eb4c972012-04-01 20:03:01 -04001330 if (!ops->getfeatcfg(netdev, i, &value) &&
1331 nla_put_u8(skb, i, value))
1332 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001333
1334 nla_nest_end(skb, feat);
1335 }
1336
1337 /* peer info if available */
1338 if (ops->cee_peer_getpg) {
1339 struct cee_pg pg;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001340 memset(&pg, 0, sizeof(pg));
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001341 err = ops->cee_peer_getpg(netdev, &pg);
David S. Miller1eb4c972012-04-01 20:03:01 -04001342 if (!err &&
1343 nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
1344 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001345 }
1346
1347 if (ops->cee_peer_getpfc) {
1348 struct cee_pfc pfc;
Mathias Krause29cd8ae2013-03-09 05:52:21 +00001349 memset(&pfc, 0, sizeof(pfc));
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001350 err = ops->cee_peer_getpfc(netdev, &pfc);
David S. Miller1eb4c972012-04-01 20:03:01 -04001351 if (!err &&
1352 nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
1353 goto nla_put_failure;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001354 }
1355
1356 if (ops->peer_getappinfo && ops->peer_getapptable) {
1357 err = dcbnl_build_peer_app(netdev, skb,
1358 DCB_ATTR_CEE_PEER_APP_TABLE,
1359 DCB_ATTR_CEE_PEER_APP_INFO,
1360 DCB_ATTR_CEE_PEER_APP);
1361 if (err)
1362 goto nla_put_failure;
1363 }
1364 nla_nest_end(skb, cee);
1365
1366 /* DCBX state */
1367 if (dcbx >= 0) {
1368 err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1369 if (err)
1370 goto nla_put_failure;
1371 }
1372 return 0;
1373
1374dcb_unlock:
Anish Bhatt52cff742014-11-14 16:38:31 -08001375 spin_unlock_bh(&dcb_lock);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001376nla_put_failure:
Pan Bianc66ebf22016-12-03 21:49:08 +08001377 err = -EMSGSIZE;
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001378 return err;
1379}
1380
1381static int dcbnl_notify(struct net_device *dev, int event, int cmd,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001382 u32 seq, u32 portid, int dcbx_ver)
John Fastabend314b4772011-06-21 07:34:37 +00001383{
1384 struct net *net = dev_net(dev);
1385 struct sk_buff *skb;
1386 struct nlmsghdr *nlh;
John Fastabend314b4772011-06-21 07:34:37 +00001387 const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1388 int err;
1389
1390 if (!ops)
1391 return -EOPNOTSUPP;
1392
Eric W. Biederman15e47302012-09-07 20:12:54 +00001393 skb = dcbnl_newmsg(event, cmd, portid, seq, 0, &nlh);
John Fastabend314b4772011-06-21 07:34:37 +00001394 if (!skb)
1395 return -ENOBUFS;
1396
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001397 if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
1398 err = dcbnl_ieee_fill(skb, dev);
1399 else
1400 err = dcbnl_cee_fill(skb, dev);
1401
John Fastabend314b4772011-06-21 07:34:37 +00001402 if (err < 0) {
1403 /* Report error to broadcast listeners */
Thomas Grafab6d4702012-06-13 02:54:57 +00001404 nlmsg_free(skb);
John Fastabend314b4772011-06-21 07:34:37 +00001405 rtnl_set_sk_err(net, RTNLGRP_DCB, err);
1406 } else {
1407 /* End nlmsg and notify broadcast listeners */
1408 nlmsg_end(skb, nlh);
1409 rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
1410 }
1411
1412 return err;
1413}
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001414
1415int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001416 u32 seq, u32 portid)
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001417{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001418 return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_IEEE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001419}
1420EXPORT_SYMBOL(dcbnl_ieee_notify);
1421
1422int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001423 u32 seq, u32 portid)
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001424{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001425 return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_CEE);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001426}
1427EXPORT_SYMBOL(dcbnl_cee_notify);
John Fastabend314b4772011-06-21 07:34:37 +00001428
Shani Michaelic93682472015-03-05 20:16:11 +02001429/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb SET commands.
1430 * If any requested operation can not be completed
1431 * the entire msg is aborted and error value is returned.
John Fastabend314b4772011-06-21 07:34:37 +00001432 * No attempt is made to reconcile the case where only part of the
1433 * cmd can be completed.
1434 */
Thomas Graf7be99412012-06-13 02:54:55 +00001435static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
1436 u32 seq, struct nlattr **tb, struct sk_buff *skb)
John Fastabend314b4772011-06-21 07:34:37 +00001437{
1438 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1439 struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
Thomas Graf3d1f4862012-06-13 02:54:58 +00001440 int err;
John Fastabend314b4772011-06-21 07:34:37 +00001441
1442 if (!ops)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001443 return -EOPNOTSUPP;
John Fastabend314b4772011-06-21 07:34:37 +00001444
John Fastabend4003b652011-06-21 07:35:04 +00001445 if (!tb[DCB_ATTR_IEEE])
1446 return -EINVAL;
1447
Johannes Berg8cb08172019-04-26 14:07:28 +02001448 err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
1449 tb[DCB_ATTR_IEEE],
1450 dcbnl_ieee_policy, NULL);
John Fastabend314b4772011-06-21 07:34:37 +00001451 if (err)
1452 return err;
1453
1454 if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
1455 struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
1456 err = ops->ieee_setets(netdev, ets);
1457 if (err)
1458 goto err;
1459 }
1460
Amir Vadai08f10af2012-04-04 21:33:30 +00001461 if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
1462 struct ieee_maxrate *maxrate =
1463 nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
1464 err = ops->ieee_setmaxrate(netdev, maxrate);
1465 if (err)
1466 goto err;
1467 }
1468
Shani Michaelic93682472015-03-05 20:16:11 +02001469 if (ieee[DCB_ATTR_IEEE_QCN] && ops->ieee_setqcn) {
1470 struct ieee_qcn *qcn =
1471 nla_data(ieee[DCB_ATTR_IEEE_QCN]);
1472
1473 err = ops->ieee_setqcn(netdev, qcn);
1474 if (err)
1475 goto err;
1476 }
1477
John Fastabend314b4772011-06-21 07:34:37 +00001478 if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
1479 struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
1480 err = ops->ieee_setpfc(netdev, pfc);
1481 if (err)
1482 goto err;
1483 }
1484
Huy Nguyene549f6f2018-02-22 11:57:10 -06001485 if (ieee[DCB_ATTR_DCB_BUFFER] && ops->dcbnl_setbuffer) {
1486 struct dcbnl_buffer *buffer =
1487 nla_data(ieee[DCB_ATTR_DCB_BUFFER]);
1488
1489 err = ops->dcbnl_setbuffer(netdev, buffer);
1490 if (err)
1491 goto err;
1492 }
1493
John Fastabend314b4772011-06-21 07:34:37 +00001494 if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1495 struct nlattr *attr;
1496 int rem;
1497
1498 nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
1499 struct dcb_app *app_data;
stephen hemminger332b4fc2017-05-19 09:55:48 -07001500
John Fastabend314b4772011-06-21 07:34:37 +00001501 if (nla_type(attr) != DCB_ATTR_IEEE_APP)
1502 continue;
stephen hemminger332b4fc2017-05-19 09:55:48 -07001503
1504 if (nla_len(attr) < sizeof(struct dcb_app)) {
1505 err = -ERANGE;
1506 goto err;
1507 }
1508
John Fastabend314b4772011-06-21 07:34:37 +00001509 app_data = nla_data(attr);
1510 if (ops->ieee_setapp)
1511 err = ops->ieee_setapp(netdev, app_data);
1512 else
John Fastabendb6db2172011-06-21 07:34:42 +00001513 err = dcb_ieee_setapp(netdev, app_data);
John Fastabend314b4772011-06-21 07:34:37 +00001514 if (err)
1515 goto err;
1516 }
1517 }
1518
1519err:
Thomas Graf7be99412012-06-13 02:54:55 +00001520 err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001521 dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
John Fastabend314b4772011-06-21 07:34:37 +00001522 return err;
1523}
1524
Thomas Graf7be99412012-06-13 02:54:55 +00001525static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1526 u32 seq, struct nlattr **tb, struct sk_buff *skb)
John Fastabend314b4772011-06-21 07:34:37 +00001527{
John Fastabend314b4772011-06-21 07:34:37 +00001528 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
John Fastabend314b4772011-06-21 07:34:37 +00001529
1530 if (!ops)
1531 return -EOPNOTSUPP;
1532
Thomas Graf7be99412012-06-13 02:54:55 +00001533 return dcbnl_ieee_fill(skb, netdev);
John Fastabend314b4772011-06-21 07:34:37 +00001534}
John Fastabendf9ae7e42011-06-21 07:34:48 +00001535
Thomas Graf7be99412012-06-13 02:54:55 +00001536static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
1537 u32 seq, struct nlattr **tb, struct sk_buff *skb)
John Fastabendf9ae7e42011-06-21 07:34:48 +00001538{
1539 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1540 struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
Thomas Graf3d1f4862012-06-13 02:54:58 +00001541 int err;
John Fastabendf9ae7e42011-06-21 07:34:48 +00001542
1543 if (!ops)
1544 return -EOPNOTSUPP;
1545
1546 if (!tb[DCB_ATTR_IEEE])
1547 return -EINVAL;
1548
Johannes Berg8cb08172019-04-26 14:07:28 +02001549 err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
1550 tb[DCB_ATTR_IEEE],
1551 dcbnl_ieee_policy, NULL);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001552 if (err)
1553 return err;
1554
1555 if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1556 struct nlattr *attr;
1557 int rem;
1558
1559 nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
1560 struct dcb_app *app_data;
1561
1562 if (nla_type(attr) != DCB_ATTR_IEEE_APP)
1563 continue;
1564 app_data = nla_data(attr);
1565 if (ops->ieee_delapp)
1566 err = ops->ieee_delapp(netdev, app_data);
1567 else
1568 err = dcb_ieee_delapp(netdev, app_data);
1569 if (err)
1570 goto err;
1571 }
1572 }
1573
1574err:
Thomas Graf7be99412012-06-13 02:54:55 +00001575 err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
Shmulik Ravid5b7f7622011-07-05 06:16:25 +00001576 dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001577 return err;
1578}
1579
1580
Shmulik Ravid6241b622010-12-30 06:26:48 +00001581/* DCBX configuration */
Thomas Graf7be99412012-06-13 02:54:55 +00001582static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1583 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravid6241b622010-12-30 06:26:48 +00001584{
Shmulik Ravid6241b622010-12-30 06:26:48 +00001585 if (!netdev->dcbnl_ops->getdcbx)
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001586 return -EOPNOTSUPP;
Shmulik Ravid6241b622010-12-30 06:26:48 +00001587
Thomas Graf7be99412012-06-13 02:54:55 +00001588 return nla_put_u8(skb, DCB_ATTR_DCBX,
1589 netdev->dcbnl_ops->getdcbx(netdev));
Shmulik Ravid6241b622010-12-30 06:26:48 +00001590}
1591
Thomas Graf7be99412012-06-13 02:54:55 +00001592static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1593 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravid6241b622010-12-30 06:26:48 +00001594{
Shmulik Ravid6241b622010-12-30 06:26:48 +00001595 u8 value;
1596
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001597 if (!netdev->dcbnl_ops->setdcbx)
1598 return -EOPNOTSUPP;
1599
1600 if (!tb[DCB_ATTR_DCBX])
1601 return -EINVAL;
Shmulik Ravid6241b622010-12-30 06:26:48 +00001602
1603 value = nla_get_u8(tb[DCB_ATTR_DCBX]);
1604
Thomas Graf7be99412012-06-13 02:54:55 +00001605 return nla_put_u8(skb, DCB_ATTR_DCBX,
1606 netdev->dcbnl_ops->setdcbx(netdev, value));
Shmulik Ravid6241b622010-12-30 06:26:48 +00001607}
1608
Thomas Graf7be99412012-06-13 02:54:55 +00001609static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1610 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001611{
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001612 struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
1613 u8 value;
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001614 int ret, i;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001615 int getall = 0;
1616
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001617 if (!netdev->dcbnl_ops->getfeatcfg)
1618 return -EOPNOTSUPP;
1619
1620 if (!tb[DCB_ATTR_FEATCFG])
1621 return -EINVAL;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001622
Johannes Berg8cb08172019-04-26 14:07:28 +02001623 ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
1624 tb[DCB_ATTR_FEATCFG],
1625 dcbnl_featcfg_nest, NULL);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001626 if (ret)
Thomas Graf7be99412012-06-13 02:54:55 +00001627 return ret;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001628
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001629 nest = nla_nest_start_noflag(skb, DCB_ATTR_FEATCFG);
Thomas Graf7be99412012-06-13 02:54:55 +00001630 if (!nest)
1631 return -EMSGSIZE;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001632
1633 if (data[DCB_FEATCFG_ATTR_ALL])
1634 getall = 1;
1635
1636 for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1637 if (!getall && !data[i])
1638 continue;
1639
1640 ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001641 if (!ret)
Thomas Graf7be99412012-06-13 02:54:55 +00001642 ret = nla_put_u8(skb, i, value);
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001643
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001644 if (ret) {
Thomas Graf7be99412012-06-13 02:54:55 +00001645 nla_nest_cancel(skb, nest);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001646 goto nla_put_failure;
1647 }
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001648 }
Thomas Graf7be99412012-06-13 02:54:55 +00001649 nla_nest_end(skb, nest);
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001650
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001651nla_put_failure:
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001652 return ret;
1653}
1654
Thomas Graf7be99412012-06-13 02:54:55 +00001655static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1656 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001657{
1658 struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001659 int ret, i;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001660 u8 value;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001661
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001662 if (!netdev->dcbnl_ops->setfeatcfg)
1663 return -ENOTSUPP;
1664
1665 if (!tb[DCB_ATTR_FEATCFG])
1666 return -EINVAL;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001667
Johannes Berg8cb08172019-04-26 14:07:28 +02001668 ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
1669 tb[DCB_ATTR_FEATCFG],
1670 dcbnl_featcfg_nest, NULL);
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001671
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001672 if (ret)
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001673 goto err;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001674
1675 for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1676 if (data[i] == NULL)
1677 continue;
1678
1679 value = nla_get_u8(data[i]);
1680
1681 ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
1682
1683 if (ret)
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001684 goto err;
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001685 }
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001686err:
Thomas Graf7be99412012-06-13 02:54:55 +00001687 ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
Shmulik Ravid7f891cf2011-01-03 08:04:59 +00001688
Shmulik Ravidea45fe42010-12-30 06:26:55 +00001689 return ret;
1690}
1691
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001692/* Handle CEE DCBX GET commands. */
Thomas Graf7be99412012-06-13 02:54:55 +00001693static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1694 u32 seq, struct nlattr **tb, struct sk_buff *skb)
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001695{
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001696 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001697
1698 if (!ops)
1699 return -EOPNOTSUPP;
1700
Thomas Graf7be99412012-06-13 02:54:55 +00001701 return dcbnl_cee_fill(skb, netdev);
Shmulik Raviddc6ed1d2011-02-27 05:04:38 +00001702}
1703
Thomas Graf33a03aa2012-06-13 02:54:54 +00001704struct reply_func {
1705 /* reply netlink message type */
1706 int type;
1707
1708 /* function to fill message contents */
1709 int (*cb)(struct net_device *, struct nlmsghdr *, u32,
1710 struct nlattr **, struct sk_buff *);
1711};
1712
1713static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
Thomas Graf7be99412012-06-13 02:54:55 +00001714 [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
1715 [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
1716 [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
1717 [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
1718 [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
1719 [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
1720 [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
1721 [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
1722 [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
1723 [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
1724 [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
1725 [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
1726 [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
1727 [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
1728 [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
1729 [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
1730 [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
1731 [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
1732 [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
1733 [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
1734 [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
1735 [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
1736 [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
1737 [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
1738 [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
1739 [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
1740 [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
Thomas Graf33a03aa2012-06-13 02:54:54 +00001741};
1742
David Ahernc21ef3e2017-04-16 09:48:24 -07001743static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1744 struct netlink_ext_ack *extack)
Alexander Duyck2f90b862008-11-20 20:52:10 -08001745{
1746 struct net *net = sock_net(skb->sk);
1747 struct net_device *netdev;
Thomas Graf7a282bc2012-06-13 02:55:01 +00001748 struct dcbmsg *dcb = nlmsg_data(nlh);
Alexander Duyck2f90b862008-11-20 20:52:10 -08001749 struct nlattr *tb[DCB_ATTR_MAX + 1];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001750 u32 portid = skb ? NETLINK_CB(skb).portid : 0;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001751 int ret = -EINVAL;
Thomas Graf33a03aa2012-06-13 02:54:54 +00001752 struct sk_buff *reply_skb;
Thomas Graf39912f92012-06-13 22:34:03 +00001753 struct nlmsghdr *reply_nlh = NULL;
Thomas Graf33a03aa2012-06-13 02:54:54 +00001754 const struct reply_func *fn;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001755
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001756 if ((nlh->nlmsg_type == RTM_SETDCB) && !netlink_capable(skb, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001757 return -EPERM;
1758
Johannes Berg8cb08172019-04-26 14:07:28 +02001759 ret = nlmsg_parse_deprecated(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
1760 dcbnl_rtnl_policy, extack);
Alexander Duyck2f90b862008-11-20 20:52:10 -08001761 if (ret < 0)
1762 return ret;
1763
Thomas Graf33a03aa2012-06-13 02:54:54 +00001764 if (dcb->cmd > DCB_CMD_MAX)
1765 return -EINVAL;
1766
1767 /* check if a reply function has been defined for the command */
1768 fn = &reply_funcs[dcb->cmd];
1769 if (!fn->cb)
1770 return -EOPNOTSUPP;
1771
Alexander Duyck2f90b862008-11-20 20:52:10 -08001772 if (!tb[DCB_ATTR_IFNAME])
1773 return -EINVAL;
1774
Ying Xued9ac62b2014-01-15 10:23:39 +08001775 netdev = __dev_get_by_name(net, nla_data(tb[DCB_ATTR_IFNAME]));
Alexander Duyck2f90b862008-11-20 20:52:10 -08001776 if (!netdev)
Thomas Graf3d1f4862012-06-13 02:54:58 +00001777 return -ENODEV;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001778
Ying Xued9ac62b2014-01-15 10:23:39 +08001779 if (!netdev->dcbnl_ops)
1780 return -EOPNOTSUPP;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001781
Eric W. Biederman15e47302012-09-07 20:12:54 +00001782 reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, portid, nlh->nlmsg_seq,
Thomas Graf33a03aa2012-06-13 02:54:54 +00001783 nlh->nlmsg_flags, &reply_nlh);
Ying Xued9ac62b2014-01-15 10:23:39 +08001784 if (!reply_skb)
1785 return -ENOBUFS;
Thomas Graf33a03aa2012-06-13 02:54:54 +00001786
1787 ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
1788 if (ret < 0) {
1789 nlmsg_free(reply_skb);
1790 goto out;
1791 }
1792
1793 nlmsg_end(reply_skb, reply_nlh);
1794
John Fastabend7c77ab22012-12-09 20:48:13 +00001795 ret = rtnl_unicast(reply_skb, net, portid);
Alexander Duyck2f90b862008-11-20 20:52:10 -08001796out:
Alexander Duyck2f90b862008-11-20 20:52:10 -08001797 return ret;
1798}
1799
Thomas Graf716b31a2012-06-13 02:54:59 +00001800static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
1801 int ifindex, int prio)
1802{
1803 struct dcb_app_type *itr;
1804
1805 list_for_each_entry(itr, &dcb_app_list, list) {
1806 if (itr->app.selector == app->selector &&
1807 itr->app.protocol == app->protocol &&
1808 itr->ifindex == ifindex &&
Petr Machata08193d12018-07-27 15:26:55 +03001809 ((prio == -1) || itr->app.priority == prio))
Thomas Graf716b31a2012-06-13 02:54:59 +00001810 return itr;
1811 }
1812
1813 return NULL;
1814}
1815
Thomas Graf4e4f2f62012-06-13 02:55:00 +00001816static int dcb_app_add(const struct dcb_app *app, int ifindex)
1817{
1818 struct dcb_app_type *entry;
1819
1820 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1821 if (!entry)
1822 return -ENOMEM;
1823
1824 memcpy(&entry->app, app, sizeof(*app));
1825 entry->ifindex = ifindex;
1826 list_add(&entry->list, &dcb_app_list);
1827
1828 return 0;
1829}
1830
John Fastabend9ab933a2010-12-30 09:26:31 +00001831/**
1832 * dcb_getapp - retrieve the DCBX application user priority
1833 *
1834 * On success returns a non-zero 802.1p user priority bitmap
1835 * otherwise returns 0 as the invalid user priority bitmap to
1836 * indicate an error.
1837 */
1838u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
1839{
1840 struct dcb_app_type *itr;
1841 u8 prio = 0;
1842
Anish Bhatt52cff742014-11-14 16:38:31 -08001843 spin_lock_bh(&dcb_lock);
Petr Machata08193d12018-07-27 15:26:55 +03001844 itr = dcb_app_lookup(app, dev->ifindex, -1);
1845 if (itr)
Thomas Graf716b31a2012-06-13 02:54:59 +00001846 prio = itr->app.priority;
Anish Bhatt52cff742014-11-14 16:38:31 -08001847 spin_unlock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00001848
1849 return prio;
1850}
1851EXPORT_SYMBOL(dcb_getapp);
1852
1853/**
John Fastabendb6db2172011-06-21 07:34:42 +00001854 * dcb_setapp - add CEE dcb application data to app list
John Fastabend9ab933a2010-12-30 09:26:31 +00001855 *
John Fastabendb6db2172011-06-21 07:34:42 +00001856 * Priority 0 is an invalid priority in CEE spec. This routine
1857 * removes applications from the app list if the priority is
Anish Bhatt16eecd92014-07-28 20:57:07 -07001858 * set to zero. Priority is expected to be 8-bit 802.1p user priority bitmap
John Fastabend9ab933a2010-12-30 09:26:31 +00001859 */
John Fastabendab6baf92011-06-21 07:34:58 +00001860int dcb_setapp(struct net_device *dev, struct dcb_app *new)
John Fastabend9ab933a2010-12-30 09:26:31 +00001861{
1862 struct dcb_app_type *itr;
John Fastabend7ec79272011-01-31 12:00:59 +00001863 struct dcb_app_type event;
Thomas Graf4e4f2f62012-06-13 02:55:00 +00001864 int err = 0;
John Fastabend7ec79272011-01-31 12:00:59 +00001865
Mark Rustade290ed82011-10-06 08:52:33 +00001866 event.ifindex = dev->ifindex;
John Fastabend7ec79272011-01-31 12:00:59 +00001867 memcpy(&event.app, new, sizeof(event.app));
John Fastabend6bd0e1c2011-10-06 08:52:38 +00001868 if (dev->dcbnl_ops->getdcbx)
1869 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
John Fastabend9ab933a2010-12-30 09:26:31 +00001870
Anish Bhatt52cff742014-11-14 16:38:31 -08001871 spin_lock_bh(&dcb_lock);
John Fastabend9ab933a2010-12-30 09:26:31 +00001872 /* Search for existing match and replace */
Petr Machata08193d12018-07-27 15:26:55 +03001873 itr = dcb_app_lookup(new, dev->ifindex, -1);
1874 if (itr) {
Thomas Graf716b31a2012-06-13 02:54:59 +00001875 if (new->priority)
1876 itr->app.priority = new->priority;
1877 else {
1878 list_del(&itr->list);
1879 kfree(itr);
John Fastabend9ab933a2010-12-30 09:26:31 +00001880 }
Thomas Graf716b31a2012-06-13 02:54:59 +00001881 goto out;
John Fastabend9ab933a2010-12-30 09:26:31 +00001882 }
1883 /* App type does not exist add new application type */
Thomas Graf4e4f2f62012-06-13 02:55:00 +00001884 if (new->priority)
1885 err = dcb_app_add(new, dev->ifindex);
John Fastabend9ab933a2010-12-30 09:26:31 +00001886out:
Anish Bhatt52cff742014-11-14 16:38:31 -08001887 spin_unlock_bh(&dcb_lock);
Thomas Graf4e4f2f62012-06-13 02:55:00 +00001888 if (!err)
1889 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
1890 return err;
John Fastabend9ab933a2010-12-30 09:26:31 +00001891}
1892EXPORT_SYMBOL(dcb_setapp);
1893
John Fastabendb6db2172011-06-21 07:34:42 +00001894/**
John Fastabenda364c8c2011-06-21 07:34:53 +00001895 * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
1896 *
1897 * Helper routine which on success returns a non-zero 802.1Qaz user
1898 * priority bitmap otherwise returns 0 to indicate the dcb_app was
1899 * not found in APP list.
1900 */
1901u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
1902{
1903 struct dcb_app_type *itr;
1904 u8 prio = 0;
1905
Anish Bhatt52cff742014-11-14 16:38:31 -08001906 spin_lock_bh(&dcb_lock);
Petr Machata08193d12018-07-27 15:26:55 +03001907 itr = dcb_app_lookup(app, dev->ifindex, -1);
1908 if (itr)
Thomas Graf716b31a2012-06-13 02:54:59 +00001909 prio |= 1 << itr->app.priority;
Anish Bhatt52cff742014-11-14 16:38:31 -08001910 spin_unlock_bh(&dcb_lock);
John Fastabenda364c8c2011-06-21 07:34:53 +00001911
1912 return prio;
1913}
1914EXPORT_SYMBOL(dcb_ieee_getapp_mask);
1915
1916/**
John Fastabendb6db2172011-06-21 07:34:42 +00001917 * dcb_ieee_setapp - add IEEE dcb application data to app list
1918 *
1919 * This adds Application data to the list. Multiple application
1920 * entries may exists for the same selector and protocol as long
Anish Bhatt16eecd92014-07-28 20:57:07 -07001921 * as the priorities are different. Priority is expected to be a
1922 * 3-bit unsigned integer
John Fastabendb6db2172011-06-21 07:34:42 +00001923 */
1924int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
1925{
John Fastabendb6db2172011-06-21 07:34:42 +00001926 struct dcb_app_type event;
1927 int err = 0;
1928
Mark Rustade290ed82011-10-06 08:52:33 +00001929 event.ifindex = dev->ifindex;
John Fastabendb6db2172011-06-21 07:34:42 +00001930 memcpy(&event.app, new, sizeof(event.app));
John Fastabend6bd0e1c2011-10-06 08:52:38 +00001931 if (dev->dcbnl_ops->getdcbx)
1932 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
John Fastabendb6db2172011-06-21 07:34:42 +00001933
Anish Bhatt52cff742014-11-14 16:38:31 -08001934 spin_lock_bh(&dcb_lock);
John Fastabendb6db2172011-06-21 07:34:42 +00001935 /* Search for existing match and abort if found */
Thomas Graf716b31a2012-06-13 02:54:59 +00001936 if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
1937 err = -EEXIST;
1938 goto out;
John Fastabendb6db2172011-06-21 07:34:42 +00001939 }
1940
Thomas Graf4e4f2f62012-06-13 02:55:00 +00001941 err = dcb_app_add(new, dev->ifindex);
John Fastabendb6db2172011-06-21 07:34:42 +00001942out:
Anish Bhatt52cff742014-11-14 16:38:31 -08001943 spin_unlock_bh(&dcb_lock);
John Fastabendb6db2172011-06-21 07:34:42 +00001944 if (!err)
1945 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
1946 return err;
1947}
1948EXPORT_SYMBOL(dcb_ieee_setapp);
1949
John Fastabendf9ae7e42011-06-21 07:34:48 +00001950/**
1951 * dcb_ieee_delapp - delete IEEE dcb application data from list
1952 *
1953 * This removes a matching APP data from the APP list
1954 */
1955int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
1956{
1957 struct dcb_app_type *itr;
1958 struct dcb_app_type event;
1959 int err = -ENOENT;
1960
Mark Rustade290ed82011-10-06 08:52:33 +00001961 event.ifindex = dev->ifindex;
John Fastabendf9ae7e42011-06-21 07:34:48 +00001962 memcpy(&event.app, del, sizeof(event.app));
John Fastabend6bd0e1c2011-10-06 08:52:38 +00001963 if (dev->dcbnl_ops->getdcbx)
1964 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001965
Anish Bhatt52cff742014-11-14 16:38:31 -08001966 spin_lock_bh(&dcb_lock);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001967 /* Search for existing match and remove it. */
Thomas Graf716b31a2012-06-13 02:54:59 +00001968 if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
1969 list_del(&itr->list);
1970 kfree(itr);
1971 err = 0;
John Fastabendf9ae7e42011-06-21 07:34:48 +00001972 }
1973
Anish Bhatt52cff742014-11-14 16:38:31 -08001974 spin_unlock_bh(&dcb_lock);
John Fastabendf9ae7e42011-06-21 07:34:48 +00001975 if (!err)
1976 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
1977 return err;
1978}
1979EXPORT_SYMBOL(dcb_ieee_delapp);
1980
Petr Machatab67c5402018-07-27 15:26:56 +03001981/**
1982 * dcb_ieee_getapp_prio_dscp_mask_map - For a given device, find mapping from
1983 * priorities to the DSCP values assigned to that priority. Initialize p_map
1984 * such that each map element holds a bit mask of DSCP values configured for
1985 * that priority by APP entries.
1986 */
1987void dcb_ieee_getapp_prio_dscp_mask_map(const struct net_device *dev,
1988 struct dcb_ieee_app_prio_map *p_map)
1989{
1990 int ifindex = dev->ifindex;
1991 struct dcb_app_type *itr;
1992 u8 prio;
1993
1994 memset(p_map->map, 0, sizeof(p_map->map));
1995
1996 spin_lock_bh(&dcb_lock);
1997 list_for_each_entry(itr, &dcb_app_list, list) {
1998 if (itr->ifindex == ifindex &&
1999 itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
2000 itr->app.protocol < 64 &&
2001 itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
2002 prio = itr->app.priority;
2003 p_map->map[prio] |= 1ULL << itr->app.protocol;
2004 }
2005 }
2006 spin_unlock_bh(&dcb_lock);
2007}
2008EXPORT_SYMBOL(dcb_ieee_getapp_prio_dscp_mask_map);
2009
2010/**
2011 * dcb_ieee_getapp_dscp_prio_mask_map - For a given device, find mapping from
2012 * DSCP values to the priorities assigned to that DSCP value. Initialize p_map
2013 * such that each map element holds a bit mask of priorities configured for a
2014 * given DSCP value by APP entries.
2015 */
2016void
2017dcb_ieee_getapp_dscp_prio_mask_map(const struct net_device *dev,
2018 struct dcb_ieee_app_dscp_map *p_map)
2019{
2020 int ifindex = dev->ifindex;
2021 struct dcb_app_type *itr;
2022
2023 memset(p_map->map, 0, sizeof(p_map->map));
2024
2025 spin_lock_bh(&dcb_lock);
2026 list_for_each_entry(itr, &dcb_app_list, list) {
2027 if (itr->ifindex == ifindex &&
2028 itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
2029 itr->app.protocol < 64 &&
2030 itr->app.priority < IEEE_8021QAZ_MAX_TCS)
2031 p_map->map[itr->app.protocol] |= 1 << itr->app.priority;
2032 }
2033 spin_unlock_bh(&dcb_lock);
2034}
2035EXPORT_SYMBOL(dcb_ieee_getapp_dscp_prio_mask_map);
2036
2037/**
2038 * Per 802.1Q-2014, the selector value of 1 is used for matching on Ethernet
2039 * type, with valid PID values >= 1536. A special meaning is then assigned to
2040 * protocol value of 0: "default priority. For use when priority is not
2041 * otherwise specified".
2042 *
2043 * dcb_ieee_getapp_default_prio_mask - For a given device, find all APP entries
2044 * of the form {$PRIO, ETHERTYPE, 0} and construct a bit mask of all default
2045 * priorities set by these entries.
2046 */
2047u8 dcb_ieee_getapp_default_prio_mask(const struct net_device *dev)
2048{
2049 int ifindex = dev->ifindex;
2050 struct dcb_app_type *itr;
2051 u8 mask = 0;
2052
2053 spin_lock_bh(&dcb_lock);
2054 list_for_each_entry(itr, &dcb_app_list, list) {
2055 if (itr->ifindex == ifindex &&
2056 itr->app.selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
2057 itr->app.protocol == 0 &&
2058 itr->app.priority < IEEE_8021QAZ_MAX_TCS)
2059 mask |= 1 << itr->app.priority;
2060 }
2061 spin_unlock_bh(&dcb_lock);
2062
2063 return mask;
2064}
2065EXPORT_SYMBOL(dcb_ieee_getapp_default_prio_mask);
2066
Alexander Duyck2f90b862008-11-20 20:52:10 -08002067static int __init dcbnl_init(void)
2068{
John Fastabend9ab933a2010-12-30 09:26:31 +00002069 INIT_LIST_HEAD(&dcb_app_list);
2070
Florian Westphalb97bac62017-08-09 20:41:48 +02002071 rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, 0);
2072 rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, 0);
Alexander Duyck2f90b862008-11-20 20:52:10 -08002073
2074 return 0;
2075}
Paul Gortmaker36b9ad802015-10-07 17:27:44 -04002076device_initcall(dcbnl_init);