dcbnl: Remove now unused dcbnl_reply()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / dcb / dcbnl.c
CommitLineData
2f90b865 1/*
698e1d23 2 * Copyright (c) 2008-2011, Intel Corporation.
2f90b865
AD
3 *
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
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Author: Lucy Liu <lucy.liu@intel.com>
18 */
19
20#include <linux/netdevice.h>
21#include <linux/netlink.h>
5a0e3ad6 22#include <linux/slab.h>
2f90b865
AD
23#include <net/netlink.h>
24#include <net/rtnetlink.h>
25#include <linux/dcbnl.h>
96b99684 26#include <net/dcbevent.h>
2f90b865 27#include <linux/rtnetlink.h>
3a9a231d 28#include <linux/module.h>
2f90b865
AD
29#include <net/sock.h>
30
31/**
32 * Data Center Bridging (DCB) is a collection of Ethernet enhancements
33 * intended to allow network traffic with differing requirements
34 * (highly reliable, no drops vs. best effort vs. low latency) to operate
35 * and co-exist on Ethernet. Current DCB features are:
36 *
37 * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
38 * framework for assigning bandwidth guarantees to traffic classes.
39 *
40 * Priority-based Flow Control (PFC) - provides a flow control mechanism which
41 * can work independently for each 802.1p priority.
42 *
43 * Congestion Notification - provides a mechanism for end-to-end congestion
44 * control for protocols which do not have built-in congestion management.
45 *
46 * More information about the emerging standards for these Ethernet features
47 * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
48 *
49 * This file implements an rtnetlink interface to allow configuration of DCB
50 * features for capable devices.
51 */
52
53MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
7a6b6f51 54MODULE_DESCRIPTION("Data Center Bridging netlink interface");
2f90b865
AD
55MODULE_LICENSE("GPL");
56
57/**************** DCB attribute policies *************************************/
58
59/* DCB netlink attributes policy */
b54452b0 60static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
859ee3c4
AD
61 [DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
62 [DCB_ATTR_STATE] = {.type = NLA_U8},
63 [DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},
64 [DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},
65 [DCB_ATTR_SET_ALL] = {.type = NLA_U8},
2f90b865 66 [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
859ee3c4
AD
67 [DCB_ATTR_CAP] = {.type = NLA_NESTED},
68 [DCB_ATTR_PFC_STATE] = {.type = NLA_U8},
69 [DCB_ATTR_BCN] = {.type = NLA_NESTED},
6fa382af 70 [DCB_ATTR_APP] = {.type = NLA_NESTED},
3e29027a 71 [DCB_ATTR_IEEE] = {.type = NLA_NESTED},
6241b625 72 [DCB_ATTR_DCBX] = {.type = NLA_U8},
ea45fe4e 73 [DCB_ATTR_FEATCFG] = {.type = NLA_NESTED},
2f90b865
AD
74};
75
76/* DCB priority flow control to User Priority nested attributes */
b54452b0 77static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
2f90b865
AD
78 [DCB_PFC_UP_ATTR_0] = {.type = NLA_U8},
79 [DCB_PFC_UP_ATTR_1] = {.type = NLA_U8},
80 [DCB_PFC_UP_ATTR_2] = {.type = NLA_U8},
81 [DCB_PFC_UP_ATTR_3] = {.type = NLA_U8},
82 [DCB_PFC_UP_ATTR_4] = {.type = NLA_U8},
83 [DCB_PFC_UP_ATTR_5] = {.type = NLA_U8},
84 [DCB_PFC_UP_ATTR_6] = {.type = NLA_U8},
85 [DCB_PFC_UP_ATTR_7] = {.type = NLA_U8},
86 [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
87};
88
89/* DCB priority grouping nested attributes */
b54452b0 90static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
2f90b865
AD
91 [DCB_PG_ATTR_TC_0] = {.type = NLA_NESTED},
92 [DCB_PG_ATTR_TC_1] = {.type = NLA_NESTED},
93 [DCB_PG_ATTR_TC_2] = {.type = NLA_NESTED},
94 [DCB_PG_ATTR_TC_3] = {.type = NLA_NESTED},
95 [DCB_PG_ATTR_TC_4] = {.type = NLA_NESTED},
96 [DCB_PG_ATTR_TC_5] = {.type = NLA_NESTED},
97 [DCB_PG_ATTR_TC_6] = {.type = NLA_NESTED},
98 [DCB_PG_ATTR_TC_7] = {.type = NLA_NESTED},
99 [DCB_PG_ATTR_TC_ALL] = {.type = NLA_NESTED},
100 [DCB_PG_ATTR_BW_ID_0] = {.type = NLA_U8},
101 [DCB_PG_ATTR_BW_ID_1] = {.type = NLA_U8},
102 [DCB_PG_ATTR_BW_ID_2] = {.type = NLA_U8},
103 [DCB_PG_ATTR_BW_ID_3] = {.type = NLA_U8},
104 [DCB_PG_ATTR_BW_ID_4] = {.type = NLA_U8},
105 [DCB_PG_ATTR_BW_ID_5] = {.type = NLA_U8},
106 [DCB_PG_ATTR_BW_ID_6] = {.type = NLA_U8},
107 [DCB_PG_ATTR_BW_ID_7] = {.type = NLA_U8},
108 [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
109};
110
111/* DCB traffic class nested attributes. */
b54452b0 112static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
2f90b865
AD
113 [DCB_TC_ATTR_PARAM_PGID] = {.type = NLA_U8},
114 [DCB_TC_ATTR_PARAM_UP_MAPPING] = {.type = NLA_U8},
115 [DCB_TC_ATTR_PARAM_STRICT_PRIO] = {.type = NLA_U8},
116 [DCB_TC_ATTR_PARAM_BW_PCT] = {.type = NLA_U8},
117 [DCB_TC_ATTR_PARAM_ALL] = {.type = NLA_FLAG},
118};
119
46132188 120/* DCB capabilities nested attributes. */
b54452b0 121static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
46132188
AD
122 [DCB_CAP_ATTR_ALL] = {.type = NLA_FLAG},
123 [DCB_CAP_ATTR_PG] = {.type = NLA_U8},
124 [DCB_CAP_ATTR_PFC] = {.type = NLA_U8},
125 [DCB_CAP_ATTR_UP2TC] = {.type = NLA_U8},
126 [DCB_CAP_ATTR_PG_TCS] = {.type = NLA_U8},
127 [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
128 [DCB_CAP_ATTR_GSP] = {.type = NLA_U8},
129 [DCB_CAP_ATTR_BCN] = {.type = NLA_U8},
6241b625 130 [DCB_CAP_ATTR_DCBX] = {.type = NLA_U8},
46132188 131};
2f90b865 132
33dbabc4 133/* DCB capabilities nested attributes. */
b54452b0 134static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
33dbabc4
AD
135 [DCB_NUMTCS_ATTR_ALL] = {.type = NLA_FLAG},
136 [DCB_NUMTCS_ATTR_PG] = {.type = NLA_U8},
137 [DCB_NUMTCS_ATTR_PFC] = {.type = NLA_U8},
138};
139
859ee3c4 140/* DCB BCN nested attributes. */
b54452b0 141static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
859ee3c4
AD
142 [DCB_BCN_ATTR_RP_0] = {.type = NLA_U8},
143 [DCB_BCN_ATTR_RP_1] = {.type = NLA_U8},
144 [DCB_BCN_ATTR_RP_2] = {.type = NLA_U8},
145 [DCB_BCN_ATTR_RP_3] = {.type = NLA_U8},
146 [DCB_BCN_ATTR_RP_4] = {.type = NLA_U8},
147 [DCB_BCN_ATTR_RP_5] = {.type = NLA_U8},
148 [DCB_BCN_ATTR_RP_6] = {.type = NLA_U8},
149 [DCB_BCN_ATTR_RP_7] = {.type = NLA_U8},
150 [DCB_BCN_ATTR_RP_ALL] = {.type = NLA_FLAG},
f4314e81
DS
151 [DCB_BCN_ATTR_BCNA_0] = {.type = NLA_U32},
152 [DCB_BCN_ATTR_BCNA_1] = {.type = NLA_U32},
859ee3c4
AD
153 [DCB_BCN_ATTR_ALPHA] = {.type = NLA_U32},
154 [DCB_BCN_ATTR_BETA] = {.type = NLA_U32},
155 [DCB_BCN_ATTR_GD] = {.type = NLA_U32},
156 [DCB_BCN_ATTR_GI] = {.type = NLA_U32},
157 [DCB_BCN_ATTR_TMAX] = {.type = NLA_U32},
158 [DCB_BCN_ATTR_TD] = {.type = NLA_U32},
159 [DCB_BCN_ATTR_RMIN] = {.type = NLA_U32},
160 [DCB_BCN_ATTR_W] = {.type = NLA_U32},
161 [DCB_BCN_ATTR_RD] = {.type = NLA_U32},
162 [DCB_BCN_ATTR_RU] = {.type = NLA_U32},
163 [DCB_BCN_ATTR_WRTT] = {.type = NLA_U32},
164 [DCB_BCN_ATTR_RI] = {.type = NLA_U32},
165 [DCB_BCN_ATTR_C] = {.type = NLA_U32},
166 [DCB_BCN_ATTR_ALL] = {.type = NLA_FLAG},
167};
168
6fa382af 169/* DCB APP nested attributes. */
b54452b0 170static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
6fa382af
YZ
171 [DCB_APP_ATTR_IDTYPE] = {.type = NLA_U8},
172 [DCB_APP_ATTR_ID] = {.type = NLA_U16},
173 [DCB_APP_ATTR_PRIORITY] = {.type = NLA_U8},
174};
175
3e29027a
JF
176/* IEEE 802.1Qaz nested attributes. */
177static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
178 [DCB_ATTR_IEEE_ETS] = {.len = sizeof(struct ieee_ets)},
179 [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
180 [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
08f10aff 181 [DCB_ATTR_IEEE_MAXRATE] = {.len = sizeof(struct ieee_maxrate)},
3e29027a
JF
182};
183
184static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
185 [DCB_ATTR_IEEE_APP] = {.len = sizeof(struct dcb_app)},
186};
187
ea45fe4e
SR
188/* DCB number of traffic classes nested attributes. */
189static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
190 [DCB_FEATCFG_ATTR_ALL] = {.type = NLA_FLAG},
191 [DCB_FEATCFG_ATTR_PG] = {.type = NLA_U8},
192 [DCB_FEATCFG_ATTR_PFC] = {.type = NLA_U8},
193 [DCB_FEATCFG_ATTR_APP] = {.type = NLA_U8},
194};
195
9ab933ab
JF
196static LIST_HEAD(dcb_app_list);
197static DEFINE_SPINLOCK(dcb_lock);
198
33a03aad
TG
199static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
200 u32 flags, struct nlmsghdr **nlhp)
201{
202 struct sk_buff *skb;
203 struct dcbmsg *dcb;
204 struct nlmsghdr *nlh;
205
206 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
207 if (!skb)
208 return NULL;
209
210 nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
211 if (!nlh) {
212 /* header should always fit, allocation must be buggy */
213 BUG();
214 }
215
216 dcb = nlmsg_data(nlh);
217 dcb->dcb_family = AF_UNSPEC;
218 dcb->cmd = cmd;
219 dcb->dcb_pad = 0;
220
221 if (nlhp)
222 *nlhp = nlh;
223
224 return skb;
225}
226
7be99413
TG
227static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
228 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 229{
2f90b865
AD
230 /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
231 if (!netdev->dcbnl_ops->getstate)
7be99413 232 return -EINVAL;
2f90b865 233
7be99413
TG
234 return nla_put_u8(skb, DCB_ATTR_STATE,
235 netdev->dcbnl_ops->getstate(netdev));
2f90b865
AD
236}
237
7be99413
TG
238static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
239 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 240{
2f90b865
AD
241 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
242 u8 value;
243 int ret = -EINVAL;
244 int i;
245 int getall = 0;
246
247 if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
248 return ret;
249
250 ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
251 tb[DCB_ATTR_PFC_CFG],
252 dcbnl_pfc_up_nest);
253 if (ret)
7be99413 254 goto err;
2f90b865 255
7be99413 256 nest = nla_nest_start(skb, DCB_ATTR_PFC_CFG);
2f90b865
AD
257 if (!nest)
258 goto err;
259
260 if (data[DCB_PFC_UP_ATTR_ALL])
261 getall = 1;
262
263 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
264 if (!getall && !data[i])
265 continue;
266
267 netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
268 &value);
7be99413 269 ret = nla_put_u8(skb, i, value);
2f90b865 270 if (ret) {
7be99413 271 nla_nest_cancel(skb, nest);
2f90b865
AD
272 goto err;
273 }
274 }
7be99413 275 nla_nest_end(skb, nest);
2f90b865
AD
276
277 return 0;
2f90b865 278err:
2f90b865
AD
279 return -EINVAL;
280}
281
7be99413
TG
282static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
283 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 284{
2f90b865 285 u8 perm_addr[MAX_ADDR_LEN];
2f90b865
AD
286
287 if (!netdev->dcbnl_ops->getpermhwaddr)
7be99413 288 return -EINVAL;
2f90b865
AD
289
290 netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
291
7be99413 292 return nla_put(skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr), perm_addr);
2f90b865
AD
293}
294
7be99413
TG
295static int dcbnl_getcap(struct net_device *netdev, struct nlmsghdr *nlh,
296 u32 seq, struct nlattr **tb, struct sk_buff *skb)
46132188 297{
46132188
AD
298 struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
299 u8 value;
300 int ret = -EINVAL;
301 int i;
302 int getall = 0;
303
304 if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
305 return ret;
306
307 ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
308 dcbnl_cap_nest);
309 if (ret)
310 goto err_out;
311
7be99413 312 nest = nla_nest_start(skb, DCB_ATTR_CAP);
46132188 313 if (!nest)
7be99413 314 goto err_out;
46132188
AD
315
316 if (data[DCB_CAP_ATTR_ALL])
317 getall = 1;
318
319 for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
320 if (!getall && !data[i])
321 continue;
322
323 if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
7be99413 324 ret = nla_put_u8(skb, i, value);
46132188 325 if (ret) {
7be99413
TG
326 nla_nest_cancel(skb, nest);
327 goto err_out;
46132188
AD
328 }
329 }
330 }
7be99413 331 nla_nest_end(skb, nest);
46132188
AD
332
333 return 0;
46132188
AD
334err_out:
335 return -EINVAL;
336}
337
7be99413
TG
338static int dcbnl_getnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
339 u32 seq, struct nlattr **tb, struct sk_buff *skb)
33dbabc4 340{
33dbabc4
AD
341 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
342 u8 value;
343 int ret = -EINVAL;
344 int i;
345 int getall = 0;
346
347 if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
348 return ret;
349
350 ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
351 dcbnl_numtcs_nest);
352 if (ret) {
353 ret = -EINVAL;
354 goto err_out;
355 }
356
7be99413 357 nest = nla_nest_start(skb, DCB_ATTR_NUMTCS);
33dbabc4
AD
358 if (!nest) {
359 ret = -EINVAL;
7be99413 360 goto err_out;
33dbabc4
AD
361 }
362
363 if (data[DCB_NUMTCS_ATTR_ALL])
364 getall = 1;
365
366 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
367 if (!getall && !data[i])
368 continue;
369
370 ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
371 if (!ret) {
7be99413 372 ret = nla_put_u8(skb, i, value);
33dbabc4 373 if (ret) {
7be99413 374 nla_nest_cancel(skb, nest);
33dbabc4 375 ret = -EINVAL;
7be99413 376 goto err_out;
33dbabc4
AD
377 }
378 } else {
7be99413 379 goto err_out;
33dbabc4
AD
380 }
381 }
7be99413 382 nla_nest_end(skb, nest);
33dbabc4
AD
383
384 return 0;
33dbabc4
AD
385err_out:
386 return ret;
387}
388
7be99413
TG
389static int dcbnl_setnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
390 u32 seq, struct nlattr **tb, struct sk_buff *skb)
33dbabc4
AD
391{
392 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
393 int ret = -EINVAL;
394 u8 value;
395 int i;
396
8b124a8e 397 if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
33dbabc4
AD
398 return ret;
399
400 ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
401 dcbnl_numtcs_nest);
402
7be99413
TG
403 if (ret)
404 return -EINVAL;
33dbabc4
AD
405
406 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
407 if (data[i] == NULL)
408 continue;
409
410 value = nla_get_u8(data[i]);
411
412 ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
33dbabc4 413 if (ret)
7be99413 414 break;
33dbabc4
AD
415 }
416
7be99413 417 return nla_put_u8(skb, DCB_ATTR_NUMTCS, !!ret);
33dbabc4
AD
418}
419
7be99413
TG
420static int dcbnl_getpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
421 u32 seq, struct nlattr **tb, struct sk_buff *skb)
0eb3aa9b 422{
0eb3aa9b 423 if (!netdev->dcbnl_ops->getpfcstate)
7be99413 424 return -EINVAL;
0eb3aa9b 425
7be99413
TG
426 return nla_put_u8(skb, DCB_ATTR_PFC_STATE,
427 netdev->dcbnl_ops->getpfcstate(netdev));
0eb3aa9b
AD
428}
429
7be99413
TG
430static int dcbnl_setpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
431 u32 seq, struct nlattr **tb, struct sk_buff *skb)
0eb3aa9b 432{
0eb3aa9b
AD
433 u8 value;
434
435 if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
7be99413 436 return -EINVAL;
0eb3aa9b
AD
437
438 value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
439
440 netdev->dcbnl_ops->setpfcstate(netdev, value);
441
7be99413 442 return nla_put_u8(skb, DCB_ATTR_PFC_STATE, 0);
0eb3aa9b
AD
443}
444
7be99413
TG
445static int dcbnl_getapp(struct net_device *netdev, struct nlmsghdr *nlh,
446 u32 seq, struct nlattr **tb, struct sk_buff *skb)
57949686 447{
57949686
YZ
448 struct nlattr *app_nest;
449 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
450 u16 id;
451 u8 up, idtype;
452 int ret = -EINVAL;
453
3dce38a0 454 if (!tb[DCB_ATTR_APP])
57949686
YZ
455 goto out;
456
457 ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
458 dcbnl_app_nest);
459 if (ret)
460 goto out;
461
462 ret = -EINVAL;
463 /* all must be non-null */
464 if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
465 (!app_tb[DCB_APP_ATTR_ID]))
466 goto out;
467
468 /* either by eth type or by socket number */
469 idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
470 if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
471 (idtype != DCB_APP_IDTYPE_PORTNUM))
472 goto out;
473
474 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
3dce38a0
JF
475
476 if (netdev->dcbnl_ops->getapp) {
477 up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
478 } else {
479 struct dcb_app app = {
480 .selector = idtype,
481 .protocol = id,
482 };
483 up = dcb_getapp(netdev, &app);
484 }
57949686 485
7be99413 486 app_nest = nla_nest_start(skb, DCB_ATTR_APP);
d3337de5 487 if (!app_nest)
7be99413 488 goto out;
d3337de5 489
7be99413 490 ret = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE, idtype);
57949686
YZ
491 if (ret)
492 goto out_cancel;
493
7be99413 494 ret = nla_put_u16(skb, DCB_APP_ATTR_ID, id);
57949686
YZ
495 if (ret)
496 goto out_cancel;
497
7be99413 498 ret = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY, up);
57949686
YZ
499 if (ret)
500 goto out_cancel;
501
7be99413 502 nla_nest_end(skb, app_nest);
57949686
YZ
503
504 goto out;
505
506out_cancel:
7be99413 507 nla_nest_cancel(skb, app_nest);
57949686
YZ
508out:
509 return ret;
510}
511
7be99413
TG
512static int dcbnl_setapp(struct net_device *netdev, struct nlmsghdr *nlh,
513 u32 seq, struct nlattr **tb, struct sk_buff *skb)
57949686 514{
9ab933ab 515 int err, ret = -EINVAL;
57949686
YZ
516 u16 id;
517 u8 up, idtype;
518 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
519
9ab933ab 520 if (!tb[DCB_ATTR_APP])
57949686
YZ
521 goto out;
522
523 ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
524 dcbnl_app_nest);
525 if (ret)
526 goto out;
527
528 ret = -EINVAL;
529 /* 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]))
533 goto out;
534
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))
539 goto out;
540
541 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
542 up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
543
9ab933ab
JF
544 if (netdev->dcbnl_ops->setapp) {
545 err = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
546 } else {
547 struct dcb_app app;
548 app.selector = idtype;
549 app.protocol = id;
550 app.priority = up;
551 err = dcb_setapp(netdev, &app);
552 }
553
7be99413 554 ret = nla_put_u8(skb, DCB_ATTR_APP, ret);
08157984 555 dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SAPP, seq, 0);
57949686
YZ
556out:
557 return ret;
558}
559
7be99413
TG
560static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
561 struct nlattr **tb, struct sk_buff *skb, int dir)
2f90b865 562{
2f90b865
AD
563 struct nlattr *pg_nest, *param_nest, *data;
564 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
565 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
566 u8 prio, pgid, tc_pct, up_map;
567 int ret = -EINVAL;
568 int getall = 0;
569 int i;
570
571 if (!tb[DCB_ATTR_PG_CFG] ||
572 !netdev->dcbnl_ops->getpgtccfgtx ||
573 !netdev->dcbnl_ops->getpgtccfgrx ||
574 !netdev->dcbnl_ops->getpgbwgcfgtx ||
575 !netdev->dcbnl_ops->getpgbwgcfgrx)
576 return ret;
577
578 ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
579 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
580
581 if (ret)
582 goto err_out;
583
7be99413 584 pg_nest = nla_nest_start(skb, DCB_ATTR_PG_CFG);
2f90b865 585 if (!pg_nest)
7be99413 586 goto err_out;
2f90b865
AD
587
588 if (pg_tb[DCB_PG_ATTR_TC_ALL])
589 getall = 1;
590
591 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
592 if (!getall && !pg_tb[i])
593 continue;
594
595 if (pg_tb[DCB_PG_ATTR_TC_ALL])
596 data = pg_tb[DCB_PG_ATTR_TC_ALL];
597 else
598 data = pg_tb[i];
599 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
600 data, dcbnl_tc_param_nest);
601 if (ret)
602 goto err_pg;
603
7be99413 604 param_nest = nla_nest_start(skb, i);
2f90b865
AD
605 if (!param_nest)
606 goto err_pg;
607
608 pgid = DCB_ATTR_VALUE_UNDEFINED;
609 prio = DCB_ATTR_VALUE_UNDEFINED;
610 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
611 up_map = DCB_ATTR_VALUE_UNDEFINED;
612
613 if (dir) {
614 /* Rx */
615 netdev->dcbnl_ops->getpgtccfgrx(netdev,
616 i - DCB_PG_ATTR_TC_0, &prio,
617 &pgid, &tc_pct, &up_map);
618 } else {
619 /* Tx */
620 netdev->dcbnl_ops->getpgtccfgtx(netdev,
621 i - DCB_PG_ATTR_TC_0, &prio,
622 &pgid, &tc_pct, &up_map);
623 }
624
625 if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
626 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7be99413 627 ret = nla_put_u8(skb,
2f90b865
AD
628 DCB_TC_ATTR_PARAM_PGID, pgid);
629 if (ret)
630 goto err_param;
631 }
632 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
633 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7be99413 634 ret = nla_put_u8(skb,
2f90b865
AD
635 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
636 if (ret)
637 goto err_param;
638 }
639 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
640 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7be99413 641 ret = nla_put_u8(skb,
2f90b865
AD
642 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
643 if (ret)
644 goto err_param;
645 }
646 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
647 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7be99413 648 ret = nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT,
2f90b865
AD
649 tc_pct);
650 if (ret)
651 goto err_param;
652 }
7be99413 653 nla_nest_end(skb, param_nest);
2f90b865
AD
654 }
655
656 if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
657 getall = 1;
658 else
659 getall = 0;
660
661 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
662 if (!getall && !pg_tb[i])
663 continue;
664
665 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
666
667 if (dir) {
668 /* Rx */
669 netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
670 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
671 } else {
672 /* Tx */
673 netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
674 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
675 }
7be99413 676 ret = nla_put_u8(skb, i, tc_pct);
2f90b865
AD
677
678 if (ret)
679 goto err_pg;
680 }
681
7be99413 682 nla_nest_end(skb, pg_nest);
2f90b865
AD
683
684 return 0;
685
686err_param:
7be99413 687 nla_nest_cancel(skb, param_nest);
2f90b865 688err_pg:
7be99413 689 nla_nest_cancel(skb, pg_nest);
2f90b865
AD
690err_out:
691 ret = -EINVAL;
692 return ret;
693}
694
7be99413
TG
695static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
696 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 697{
7be99413 698 return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 0);
2f90b865
AD
699}
700
7be99413
TG
701static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
702 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 703{
7be99413 704 return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 1);
2f90b865
AD
705}
706
7be99413
TG
707static int dcbnl_setstate(struct net_device *netdev, struct nlmsghdr *nlh,
708 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 709{
2f90b865
AD
710 u8 value;
711
712 if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
7be99413 713 return -EINVAL;
2f90b865
AD
714
715 value = nla_get_u8(tb[DCB_ATTR_STATE]);
716
7be99413
TG
717 return nla_put_u8(skb, DCB_ATTR_STATE,
718 netdev->dcbnl_ops->setstate(netdev, value));
2f90b865
AD
719}
720
7be99413
TG
721static int dcbnl_setpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
722 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865
AD
723{
724 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
725 int i;
726 int ret = -EINVAL;
727 u8 value;
728
729 if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
730 return ret;
731
732 ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
733 tb[DCB_ATTR_PFC_CFG],
734 dcbnl_pfc_up_nest);
735 if (ret)
736 goto err;
737
738 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
739 if (data[i] == NULL)
740 continue;
741 value = nla_get_u8(data[i]);
742 netdev->dcbnl_ops->setpfccfg(netdev,
743 data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
744 }
745
7be99413 746 return nla_put_u8(skb, DCB_ATTR_PFC_CFG, 0);
2f90b865
AD
747err:
748 return ret;
749}
750
7be99413
TG
751static int dcbnl_setall(struct net_device *netdev, struct nlmsghdr *nlh,
752 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865
AD
753{
754 int ret = -EINVAL;
755
756 if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
757 return ret;
758
7be99413
TG
759 ret = nla_put_u8(skb, DCB_ATTR_SET_ALL,
760 netdev->dcbnl_ops->setall(netdev));
08157984 761 dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SET_ALL, seq, 0);
2f90b865
AD
762
763 return ret;
764}
765
7be99413
TG
766static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
767 u32 seq, struct nlattr **tb, struct sk_buff *skb,
768 int dir)
2f90b865
AD
769{
770 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
771 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
772 int ret = -EINVAL;
773 int i;
774 u8 pgid;
775 u8 up_map;
776 u8 prio;
777 u8 tc_pct;
778
779 if (!tb[DCB_ATTR_PG_CFG] ||
780 !netdev->dcbnl_ops->setpgtccfgtx ||
781 !netdev->dcbnl_ops->setpgtccfgrx ||
782 !netdev->dcbnl_ops->setpgbwgcfgtx ||
783 !netdev->dcbnl_ops->setpgbwgcfgrx)
784 return ret;
785
786 ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
787 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
788 if (ret)
789 goto err;
790
791 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
792 if (!pg_tb[i])
793 continue;
794
795 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
796 pg_tb[i], dcbnl_tc_param_nest);
797 if (ret)
798 goto err;
799
800 pgid = DCB_ATTR_VALUE_UNDEFINED;
801 prio = DCB_ATTR_VALUE_UNDEFINED;
802 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
803 up_map = DCB_ATTR_VALUE_UNDEFINED;
804
805 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
806 prio =
807 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
808
809 if (param_tb[DCB_TC_ATTR_PARAM_PGID])
810 pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
811
812 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
813 tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
814
815 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
816 up_map =
817 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
818
819 /* dir: Tx = 0, Rx = 1 */
820 if (dir) {
821 /* Rx */
822 netdev->dcbnl_ops->setpgtccfgrx(netdev,
823 i - DCB_PG_ATTR_TC_0,
824 prio, pgid, tc_pct, up_map);
825 } else {
826 /* Tx */
827 netdev->dcbnl_ops->setpgtccfgtx(netdev,
828 i - DCB_PG_ATTR_TC_0,
829 prio, pgid, tc_pct, up_map);
830 }
831 }
832
833 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
834 if (!pg_tb[i])
835 continue;
836
837 tc_pct = nla_get_u8(pg_tb[i]);
838
839 /* dir: Tx = 0, Rx = 1 */
840 if (dir) {
841 /* Rx */
842 netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
843 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
844 } else {
845 /* Tx */
846 netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
847 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
848 }
849 }
850
7be99413 851 ret = nla_put_u8(skb, (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG), 0);
2f90b865
AD
852
853err:
854 return ret;
855}
856
7be99413
TG
857static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
858 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 859{
7be99413 860 return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
2f90b865
AD
861}
862
7be99413
TG
863static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
864 u32 seq, struct nlattr **tb, struct sk_buff *skb)
2f90b865 865{
7be99413 866 return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
2f90b865
AD
867}
868
7be99413
TG
869static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
870 u32 seq, struct nlattr **tb, struct sk_buff *skb)
859ee3c4 871{
859ee3c4
AD
872 struct nlattr *bcn_nest;
873 struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
874 u8 value_byte;
875 u32 value_integer;
876 int ret = -EINVAL;
877 bool getall = false;
878 int i;
879
880 if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
881 !netdev->dcbnl_ops->getbcncfg)
882 return ret;
883
884 ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
885 tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
886
887 if (ret)
888 goto err_out;
889
7be99413 890 bcn_nest = nla_nest_start(skb, DCB_ATTR_BCN);
859ee3c4 891 if (!bcn_nest)
7be99413 892 goto err_out;
859ee3c4
AD
893
894 if (bcn_tb[DCB_BCN_ATTR_ALL])
895 getall = true;
896
897 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
898 if (!getall && !bcn_tb[i])
899 continue;
900
901 netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
902 &value_byte);
7be99413 903 ret = nla_put_u8(skb, i, value_byte);
859ee3c4
AD
904 if (ret)
905 goto err_bcn;
906 }
907
f4314e81 908 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
859ee3c4
AD
909 if (!getall && !bcn_tb[i])
910 continue;
911
912 netdev->dcbnl_ops->getbcncfg(netdev, i,
913 &value_integer);
7be99413 914 ret = nla_put_u32(skb, i, value_integer);
859ee3c4
AD
915 if (ret)
916 goto err_bcn;
917 }
918
7be99413 919 nla_nest_end(skb, bcn_nest);
859ee3c4
AD
920
921 return 0;
922
923err_bcn:
7be99413 924 nla_nest_cancel(skb, bcn_nest);
859ee3c4
AD
925err_out:
926 ret = -EINVAL;
927 return ret;
928}
929
7be99413
TG
930static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
931 u32 seq, struct nlattr **tb, struct sk_buff *skb)
859ee3c4
AD
932{
933 struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
934 int i;
935 int ret = -EINVAL;
936 u8 value_byte;
937 u32 value_int;
938
f64f9e71
JP
939 if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg ||
940 !netdev->dcbnl_ops->setbcnrp)
859ee3c4
AD
941 return ret;
942
943 ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
944 tb[DCB_ATTR_BCN],
945 dcbnl_pfc_up_nest);
946 if (ret)
947 goto err;
948
949 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
950 if (data[i] == NULL)
951 continue;
952 value_byte = nla_get_u8(data[i]);
953 netdev->dcbnl_ops->setbcnrp(netdev,
954 data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
955 }
956
f4314e81 957 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
859ee3c4
AD
958 if (data[i] == NULL)
959 continue;
960 value_int = nla_get_u32(data[i]);
961 netdev->dcbnl_ops->setbcncfg(netdev,
962 i, value_int);
963 }
964
7be99413 965 ret = nla_put_u8(skb, DCB_ATTR_BCN, 0);
859ee3c4
AD
966err:
967 return ret;
968}
969
dc6ed1df
SR
970static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
971 int app_nested_type, int app_info_type,
972 int app_entry_type)
eed84713
SR
973{
974 struct dcb_peer_app_info info;
975 struct dcb_app *table = NULL;
976 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
977 u16 app_count;
978 int err;
979
980
981 /**
982 * retrieve the peer app configuration form the driver. If the driver
983 * handlers fail exit without doing anything
984 */
985 err = ops->peer_getappinfo(netdev, &info, &app_count);
986 if (!err && app_count) {
987 table = kmalloc(sizeof(struct dcb_app) * app_count, GFP_KERNEL);
988 if (!table)
989 return -ENOMEM;
990
991 err = ops->peer_getapptable(netdev, table);
992 }
993
994 if (!err) {
995 u16 i;
996 struct nlattr *app;
997
998 /**
999 * build the message, from here on the only possible failure
1000 * is due to the skb size
1001 */
1002 err = -EMSGSIZE;
1003
dc6ed1df 1004 app = nla_nest_start(skb, app_nested_type);
eed84713
SR
1005 if (!app)
1006 goto nla_put_failure;
1007
1eb4c977
DM
1008 if (app_info_type &&
1009 nla_put(skb, app_info_type, sizeof(info), &info))
1010 goto nla_put_failure;
eed84713 1011
1eb4c977
DM
1012 for (i = 0; i < app_count; i++) {
1013 if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
1014 &table[i]))
1015 goto nla_put_failure;
1016 }
eed84713
SR
1017 nla_nest_end(skb, app);
1018 }
1019 err = 0;
1020
1021nla_put_failure:
1022 kfree(table);
1023 return err;
1024}
3e29027a
JF
1025
1026/* Handle IEEE 802.1Qaz GET commands. */
314b4778 1027static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
3e29027a 1028{
9ab933ab
JF
1029 struct nlattr *ieee, *app;
1030 struct dcb_app_type *itr;
3e29027a 1031 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
c7797baf 1032 int dcbx;
314b4778 1033 int err = -EMSGSIZE;
3e29027a 1034
1eb4c977
DM
1035 if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
1036 goto nla_put_failure;
3e29027a
JF
1037 ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
1038 if (!ieee)
1039 goto nla_put_failure;
1040
1041 if (ops->ieee_getets) {
1042 struct ieee_ets ets;
1043 err = ops->ieee_getets(netdev, &ets);
1eb4c977
DM
1044 if (!err &&
1045 nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
1046 goto nla_put_failure;
3e29027a
JF
1047 }
1048
08f10aff
AV
1049 if (ops->ieee_getmaxrate) {
1050 struct ieee_maxrate maxrate;
1051 err = ops->ieee_getmaxrate(netdev, &maxrate);
1052 if (!err) {
1053 err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
1054 sizeof(maxrate), &maxrate);
1055 if (err)
1056 goto nla_put_failure;
1057 }
1058 }
1059
3e29027a
JF
1060 if (ops->ieee_getpfc) {
1061 struct ieee_pfc pfc;
1062 err = ops->ieee_getpfc(netdev, &pfc);
1eb4c977
DM
1063 if (!err &&
1064 nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
1065 goto nla_put_failure;
3e29027a
JF
1066 }
1067
9ab933ab
JF
1068 app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
1069 if (!app)
1070 goto nla_put_failure;
1071
1072 spin_lock(&dcb_lock);
1073 list_for_each_entry(itr, &dcb_app_list, list) {
e290ed81 1074 if (itr->ifindex == netdev->ifindex) {
70bfa2d2
DC
1075 err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
1076 &itr->app);
1077 if (err) {
1078 spin_unlock(&dcb_lock);
1079 goto nla_put_failure;
1080 }
1081 }
9ab933ab 1082 }
c7797baf
JF
1083
1084 if (netdev->dcbnl_ops->getdcbx)
1085 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1086 else
1087 dcbx = -EOPNOTSUPP;
1088
9ab933ab
JF
1089 spin_unlock(&dcb_lock);
1090 nla_nest_end(skb, app);
1091
eed84713
SR
1092 /* get peer info if available */
1093 if (ops->ieee_peer_getets) {
1094 struct ieee_ets ets;
1095 err = ops->ieee_peer_getets(netdev, &ets);
1eb4c977
DM
1096 if (!err &&
1097 nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
1098 goto nla_put_failure;
eed84713
SR
1099 }
1100
1101 if (ops->ieee_peer_getpfc) {
1102 struct ieee_pfc pfc;
1103 err = ops->ieee_peer_getpfc(netdev, &pfc);
1eb4c977
DM
1104 if (!err &&
1105 nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
1106 goto nla_put_failure;
eed84713
SR
1107 }
1108
1109 if (ops->peer_getappinfo && ops->peer_getapptable) {
dc6ed1df
SR
1110 err = dcbnl_build_peer_app(netdev, skb,
1111 DCB_ATTR_IEEE_PEER_APP,
1112 DCB_ATTR_IEEE_APP_UNSPEC,
1113 DCB_ATTR_IEEE_APP);
eed84713
SR
1114 if (err)
1115 goto nla_put_failure;
1116 }
1117
3e29027a 1118 nla_nest_end(skb, ieee);
c7797baf
JF
1119 if (dcbx >= 0) {
1120 err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1121 if (err)
1122 goto nla_put_failure;
1123 }
3e29027a 1124
314b4778
JF
1125 return 0;
1126
3e29027a 1127nla_put_failure:
314b4778 1128 return err;
3e29027a
JF
1129}
1130
5b7f7626
SR
1131static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
1132 int dir)
1133{
1134 u8 pgid, up_map, prio, tc_pct;
1135 const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1136 int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
1137 struct nlattr *pg = nla_nest_start(skb, i);
1138
1139 if (!pg)
1140 goto nla_put_failure;
1141
1142 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
1143 struct nlattr *tc_nest = nla_nest_start(skb, i);
1144
1145 if (!tc_nest)
1146 goto nla_put_failure;
1147
1148 pgid = DCB_ATTR_VALUE_UNDEFINED;
1149 prio = DCB_ATTR_VALUE_UNDEFINED;
1150 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1151 up_map = DCB_ATTR_VALUE_UNDEFINED;
1152
1153 if (!dir)
1154 ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
1155 &prio, &pgid, &tc_pct, &up_map);
1156 else
1157 ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
1158 &prio, &pgid, &tc_pct, &up_map);
1159
1eb4c977
DM
1160 if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
1161 nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
1162 nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
1163 nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
1164 goto nla_put_failure;
5b7f7626
SR
1165 nla_nest_end(skb, tc_nest);
1166 }
1167
1168 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
1169 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1170
1171 if (!dir)
1172 ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
1173 &tc_pct);
1174 else
1175 ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
1176 &tc_pct);
1eb4c977
DM
1177 if (nla_put_u8(skb, i, tc_pct))
1178 goto nla_put_failure;
5b7f7626
SR
1179 }
1180 nla_nest_end(skb, pg);
1181 return 0;
1182
1183nla_put_failure:
1184 return -EMSGSIZE;
1185}
1186
1187static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
1188{
1189 struct nlattr *cee, *app;
1190 struct dcb_app_type *itr;
1191 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1192 int dcbx, i, err = -EMSGSIZE;
1193 u8 value;
1194
1eb4c977
DM
1195 if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
1196 goto nla_put_failure;
5b7f7626
SR
1197 cee = nla_nest_start(skb, DCB_ATTR_CEE);
1198 if (!cee)
1199 goto nla_put_failure;
1200
1201 /* local pg */
1202 if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
1203 err = dcbnl_cee_pg_fill(skb, netdev, 1);
1204 if (err)
1205 goto nla_put_failure;
1206 }
1207
1208 if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
1209 err = dcbnl_cee_pg_fill(skb, netdev, 0);
1210 if (err)
1211 goto nla_put_failure;
1212 }
1213
1214 /* local pfc */
1215 if (ops->getpfccfg) {
1216 struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
1217
1218 if (!pfc_nest)
1219 goto nla_put_failure;
1220
1221 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
1222 ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
1eb4c977
DM
1223 if (nla_put_u8(skb, i, value))
1224 goto nla_put_failure;
5b7f7626
SR
1225 }
1226 nla_nest_end(skb, pfc_nest);
1227 }
1228
1229 /* local app */
1230 spin_lock(&dcb_lock);
1231 app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
1232 if (!app)
40f5d72a 1233 goto dcb_unlock;
5b7f7626
SR
1234
1235 list_for_each_entry(itr, &dcb_app_list, list) {
e290ed81 1236 if (itr->ifindex == netdev->ifindex) {
5b7f7626
SR
1237 struct nlattr *app_nest = nla_nest_start(skb,
1238 DCB_ATTR_APP);
1239 if (!app_nest)
1240 goto dcb_unlock;
1241
1242 err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
1243 itr->app.selector);
1244 if (err)
1245 goto dcb_unlock;
1246
1247 err = nla_put_u16(skb, DCB_APP_ATTR_ID,
1248 itr->app.protocol);
1249 if (err)
1250 goto dcb_unlock;
1251
1252 err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
1253 itr->app.priority);
1254 if (err)
1255 goto dcb_unlock;
1256
1257 nla_nest_end(skb, app_nest);
1258 }
1259 }
1260 nla_nest_end(skb, app);
1261
1262 if (netdev->dcbnl_ops->getdcbx)
1263 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1264 else
1265 dcbx = -EOPNOTSUPP;
1266
1267 spin_unlock(&dcb_lock);
1268
1269 /* features flags */
1270 if (ops->getfeatcfg) {
1271 struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
1272 if (!feat)
1273 goto nla_put_failure;
1274
1275 for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
1276 i++)
1eb4c977
DM
1277 if (!ops->getfeatcfg(netdev, i, &value) &&
1278 nla_put_u8(skb, i, value))
1279 goto nla_put_failure;
5b7f7626
SR
1280
1281 nla_nest_end(skb, feat);
1282 }
1283
1284 /* peer info if available */
1285 if (ops->cee_peer_getpg) {
1286 struct cee_pg pg;
1287 err = ops->cee_peer_getpg(netdev, &pg);
1eb4c977
DM
1288 if (!err &&
1289 nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
1290 goto nla_put_failure;
5b7f7626
SR
1291 }
1292
1293 if (ops->cee_peer_getpfc) {
1294 struct cee_pfc pfc;
1295 err = ops->cee_peer_getpfc(netdev, &pfc);
1eb4c977
DM
1296 if (!err &&
1297 nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
1298 goto nla_put_failure;
5b7f7626
SR
1299 }
1300
1301 if (ops->peer_getappinfo && ops->peer_getapptable) {
1302 err = dcbnl_build_peer_app(netdev, skb,
1303 DCB_ATTR_CEE_PEER_APP_TABLE,
1304 DCB_ATTR_CEE_PEER_APP_INFO,
1305 DCB_ATTR_CEE_PEER_APP);
1306 if (err)
1307 goto nla_put_failure;
1308 }
1309 nla_nest_end(skb, cee);
1310
1311 /* DCBX state */
1312 if (dcbx >= 0) {
1313 err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1314 if (err)
1315 goto nla_put_failure;
1316 }
1317 return 0;
1318
1319dcb_unlock:
1320 spin_unlock(&dcb_lock);
1321nla_put_failure:
1322 return err;
1323}
1324
1325static int dcbnl_notify(struct net_device *dev, int event, int cmd,
1326 u32 seq, u32 pid, int dcbx_ver)
314b4778
JF
1327{
1328 struct net *net = dev_net(dev);
1329 struct sk_buff *skb;
1330 struct nlmsghdr *nlh;
1331 struct dcbmsg *dcb;
1332 const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1333 int err;
1334
1335 if (!ops)
1336 return -EOPNOTSUPP;
1337
1338 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1339 if (!skb)
1340 return -ENOBUFS;
1341
1342 nlh = nlmsg_put(skb, pid, 0, event, sizeof(*dcb), 0);
1343 if (nlh == NULL) {
4d054f2f 1344 nlmsg_free(skb);
314b4778
JF
1345 return -EMSGSIZE;
1346 }
1347
1348 dcb = NLMSG_DATA(nlh);
1349 dcb->dcb_family = AF_UNSPEC;
1350 dcb->cmd = cmd;
1351
5b7f7626
SR
1352 if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
1353 err = dcbnl_ieee_fill(skb, dev);
1354 else
1355 err = dcbnl_cee_fill(skb, dev);
1356
314b4778
JF
1357 if (err < 0) {
1358 /* Report error to broadcast listeners */
1359 nlmsg_cancel(skb, nlh);
1360 kfree_skb(skb);
1361 rtnl_set_sk_err(net, RTNLGRP_DCB, err);
1362 } else {
1363 /* End nlmsg and notify broadcast listeners */
1364 nlmsg_end(skb, nlh);
1365 rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
1366 }
1367
1368 return err;
1369}
5b7f7626
SR
1370
1371int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
1372 u32 seq, u32 pid)
1373{
1374 return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_IEEE);
1375}
1376EXPORT_SYMBOL(dcbnl_ieee_notify);
1377
1378int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
1379 u32 seq, u32 pid)
1380{
1381 return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_CEE);
1382}
1383EXPORT_SYMBOL(dcbnl_cee_notify);
314b4778
JF
1384
1385/* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
1386 * be completed the entire msg is aborted and error value is returned.
1387 * No attempt is made to reconcile the case where only part of the
1388 * cmd can be completed.
1389 */
7be99413
TG
1390static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
1391 u32 seq, struct nlattr **tb, struct sk_buff *skb)
314b4778
JF
1392{
1393 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1394 struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
1395 int err = -EOPNOTSUPP;
1396
1397 if (!ops)
1398 return err;
1399
4003b658
JF
1400 if (!tb[DCB_ATTR_IEEE])
1401 return -EINVAL;
1402
314b4778
JF
1403 err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
1404 tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
1405 if (err)
1406 return err;
1407
1408 if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
1409 struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
1410 err = ops->ieee_setets(netdev, ets);
1411 if (err)
1412 goto err;
1413 }
1414
08f10aff
AV
1415 if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
1416 struct ieee_maxrate *maxrate =
1417 nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
1418 err = ops->ieee_setmaxrate(netdev, maxrate);
1419 if (err)
1420 goto err;
1421 }
1422
314b4778
JF
1423 if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
1424 struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
1425 err = ops->ieee_setpfc(netdev, pfc);
1426 if (err)
1427 goto err;
1428 }
1429
1430 if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1431 struct nlattr *attr;
1432 int rem;
1433
1434 nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
1435 struct dcb_app *app_data;
1436 if (nla_type(attr) != DCB_ATTR_IEEE_APP)
1437 continue;
1438 app_data = nla_data(attr);
1439 if (ops->ieee_setapp)
1440 err = ops->ieee_setapp(netdev, app_data);
1441 else
b6db2174 1442 err = dcb_ieee_setapp(netdev, app_data);
314b4778
JF
1443 if (err)
1444 goto err;
1445 }
1446 }
1447
1448err:
7be99413 1449 err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
5b7f7626 1450 dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
314b4778
JF
1451 return err;
1452}
1453
7be99413
TG
1454static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1455 u32 seq, struct nlattr **tb, struct sk_buff *skb)
314b4778 1456{
314b4778 1457 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
314b4778
JF
1458
1459 if (!ops)
1460 return -EOPNOTSUPP;
1461
7be99413 1462 return dcbnl_ieee_fill(skb, netdev);
314b4778 1463}
f9ae7e4b 1464
7be99413
TG
1465static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
1466 u32 seq, struct nlattr **tb, struct sk_buff *skb)
f9ae7e4b
JF
1467{
1468 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1469 struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
1470 int err = -EOPNOTSUPP;
1471
1472 if (!ops)
1473 return -EOPNOTSUPP;
1474
1475 if (!tb[DCB_ATTR_IEEE])
1476 return -EINVAL;
1477
1478 err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
1479 tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
1480 if (err)
1481 return err;
1482
1483 if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1484 struct nlattr *attr;
1485 int rem;
1486
1487 nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
1488 struct dcb_app *app_data;
1489
1490 if (nla_type(attr) != DCB_ATTR_IEEE_APP)
1491 continue;
1492 app_data = nla_data(attr);
1493 if (ops->ieee_delapp)
1494 err = ops->ieee_delapp(netdev, app_data);
1495 else
1496 err = dcb_ieee_delapp(netdev, app_data);
1497 if (err)
1498 goto err;
1499 }
1500 }
1501
1502err:
7be99413 1503 err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
5b7f7626 1504 dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
f9ae7e4b
JF
1505 return err;
1506}
1507
1508
6241b625 1509/* DCBX configuration */
7be99413
TG
1510static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1511 u32 seq, struct nlattr **tb, struct sk_buff *skb)
6241b625 1512{
6241b625 1513 if (!netdev->dcbnl_ops->getdcbx)
7f891cf1 1514 return -EOPNOTSUPP;
6241b625 1515
7be99413
TG
1516 return nla_put_u8(skb, DCB_ATTR_DCBX,
1517 netdev->dcbnl_ops->getdcbx(netdev));
6241b625
SR
1518}
1519
7be99413
TG
1520static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1521 u32 seq, struct nlattr **tb, struct sk_buff *skb)
6241b625 1522{
6241b625
SR
1523 u8 value;
1524
7f891cf1
SR
1525 if (!netdev->dcbnl_ops->setdcbx)
1526 return -EOPNOTSUPP;
1527
1528 if (!tb[DCB_ATTR_DCBX])
1529 return -EINVAL;
6241b625
SR
1530
1531 value = nla_get_u8(tb[DCB_ATTR_DCBX]);
1532
7be99413
TG
1533 return nla_put_u8(skb, DCB_ATTR_DCBX,
1534 netdev->dcbnl_ops->setdcbx(netdev, value));
6241b625
SR
1535}
1536
7be99413
TG
1537static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1538 u32 seq, struct nlattr **tb, struct sk_buff *skb)
ea45fe4e 1539{
ea45fe4e
SR
1540 struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
1541 u8 value;
7f891cf1 1542 int ret, i;
ea45fe4e
SR
1543 int getall = 0;
1544
7f891cf1
SR
1545 if (!netdev->dcbnl_ops->getfeatcfg)
1546 return -EOPNOTSUPP;
1547
1548 if (!tb[DCB_ATTR_FEATCFG])
1549 return -EINVAL;
ea45fe4e
SR
1550
1551 ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
1552 dcbnl_featcfg_nest);
7f891cf1 1553 if (ret)
7be99413 1554 return ret;
ea45fe4e 1555
7be99413
TG
1556 nest = nla_nest_start(skb, DCB_ATTR_FEATCFG);
1557 if (!nest)
1558 return -EMSGSIZE;
ea45fe4e
SR
1559
1560 if (data[DCB_FEATCFG_ATTR_ALL])
1561 getall = 1;
1562
1563 for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1564 if (!getall && !data[i])
1565 continue;
1566
1567 ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
7f891cf1 1568 if (!ret)
7be99413 1569 ret = nla_put_u8(skb, i, value);
ea45fe4e 1570
7f891cf1 1571 if (ret) {
7be99413 1572 nla_nest_cancel(skb, nest);
7f891cf1
SR
1573 goto nla_put_failure;
1574 }
ea45fe4e 1575 }
7be99413 1576 nla_nest_end(skb, nest);
ea45fe4e 1577
7f891cf1 1578nla_put_failure:
ea45fe4e
SR
1579 return ret;
1580}
1581
7be99413
TG
1582static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1583 u32 seq, struct nlattr **tb, struct sk_buff *skb)
ea45fe4e
SR
1584{
1585 struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
7f891cf1 1586 int ret, i;
ea45fe4e 1587 u8 value;
ea45fe4e 1588
7f891cf1
SR
1589 if (!netdev->dcbnl_ops->setfeatcfg)
1590 return -ENOTSUPP;
1591
1592 if (!tb[DCB_ATTR_FEATCFG])
1593 return -EINVAL;
ea45fe4e
SR
1594
1595 ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
1596 dcbnl_featcfg_nest);
1597
7f891cf1 1598 if (ret)
ea45fe4e 1599 goto err;
ea45fe4e
SR
1600
1601 for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1602 if (data[i] == NULL)
1603 continue;
1604
1605 value = nla_get_u8(data[i]);
1606
1607 ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
1608
1609 if (ret)
7f891cf1 1610 goto err;
ea45fe4e 1611 }
ea45fe4e 1612err:
7be99413 1613 ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
7f891cf1 1614
ea45fe4e
SR
1615 return ret;
1616}
1617
dc6ed1df 1618/* Handle CEE DCBX GET commands. */
7be99413
TG
1619static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1620 u32 seq, struct nlattr **tb, struct sk_buff *skb)
dc6ed1df 1621{
dc6ed1df 1622 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
dc6ed1df
SR
1623
1624 if (!ops)
1625 return -EOPNOTSUPP;
1626
7be99413 1627 return dcbnl_cee_fill(skb, netdev);
dc6ed1df
SR
1628}
1629
33a03aad
TG
1630struct reply_func {
1631 /* reply netlink message type */
1632 int type;
1633
1634 /* function to fill message contents */
1635 int (*cb)(struct net_device *, struct nlmsghdr *, u32,
1636 struct nlattr **, struct sk_buff *);
1637};
1638
1639static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
7be99413
TG
1640 [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
1641 [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
1642 [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
1643 [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
1644 [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
1645 [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
1646 [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
1647 [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
1648 [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
1649 [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
1650 [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
1651 [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
1652 [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
1653 [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
1654 [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
1655 [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
1656 [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
1657 [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
1658 [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
1659 [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
1660 [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
1661 [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
1662 [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
1663 [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
1664 [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
1665 [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
1666 [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
33a03aad
TG
1667};
1668
2f90b865
AD
1669static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1670{
1671 struct net *net = sock_net(skb->sk);
1672 struct net_device *netdev;
1673 struct dcbmsg *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
1674 struct nlattr *tb[DCB_ATTR_MAX + 1];
1675 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
1676 int ret = -EINVAL;
33a03aad
TG
1677 struct sk_buff *reply_skb;
1678 struct nlmsghdr *reply_nlh;
1679 const struct reply_func *fn;
2f90b865 1680
09ad9bc7 1681 if (!net_eq(net, &init_net))
2f90b865
AD
1682 return -EINVAL;
1683
1684 ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
1685 dcbnl_rtnl_policy);
1686 if (ret < 0)
1687 return ret;
1688
33a03aad
TG
1689 if (dcb->cmd > DCB_CMD_MAX)
1690 return -EINVAL;
1691
1692 /* check if a reply function has been defined for the command */
1693 fn = &reply_funcs[dcb->cmd];
1694 if (!fn->cb)
1695 return -EOPNOTSUPP;
1696
2f90b865
AD
1697 if (!tb[DCB_ATTR_IFNAME])
1698 return -EINVAL;
1699
1700 netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
1701 if (!netdev)
1702 return -EINVAL;
1703
1704 if (!netdev->dcbnl_ops)
1705 goto errout;
1706
33a03aad
TG
1707 reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, pid, nlh->nlmsg_seq,
1708 nlh->nlmsg_flags, &reply_nlh);
1709 if (!reply_skb) {
1710 ret = -ENOBUFS;
1711 goto out;
1712 }
1713
1714 ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
1715 if (ret < 0) {
1716 nlmsg_free(reply_skb);
1717 goto out;
1718 }
1719
1720 nlmsg_end(reply_skb, reply_nlh);
1721
1722 ret = rtnl_unicast(reply_skb, &init_net, pid);
2f90b865
AD
1723out:
1724 dev_put(netdev);
1725 return ret;
1726}
1727
9ab933ab
JF
1728/**
1729 * dcb_getapp - retrieve the DCBX application user priority
1730 *
1731 * On success returns a non-zero 802.1p user priority bitmap
1732 * otherwise returns 0 as the invalid user priority bitmap to
1733 * indicate an error.
1734 */
1735u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
1736{
1737 struct dcb_app_type *itr;
1738 u8 prio = 0;
1739
1740 spin_lock(&dcb_lock);
1741 list_for_each_entry(itr, &dcb_app_list, list) {
1742 if (itr->app.selector == app->selector &&
1743 itr->app.protocol == app->protocol &&
e290ed81 1744 itr->ifindex == dev->ifindex) {
9ab933ab
JF
1745 prio = itr->app.priority;
1746 break;
1747 }
1748 }
1749 spin_unlock(&dcb_lock);
1750
1751 return prio;
1752}
1753EXPORT_SYMBOL(dcb_getapp);
1754
1755/**
b6db2174 1756 * dcb_setapp - add CEE dcb application data to app list
9ab933ab 1757 *
b6db2174
JF
1758 * Priority 0 is an invalid priority in CEE spec. This routine
1759 * removes applications from the app list if the priority is
1760 * set to zero.
9ab933ab 1761 */
ab6baf98 1762int dcb_setapp(struct net_device *dev, struct dcb_app *new)
9ab933ab
JF
1763{
1764 struct dcb_app_type *itr;
7ec79270
JF
1765 struct dcb_app_type event;
1766
e290ed81 1767 event.ifindex = dev->ifindex;
7ec79270 1768 memcpy(&event.app, new, sizeof(event.app));
6bd0e1cb
JF
1769 if (dev->dcbnl_ops->getdcbx)
1770 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
9ab933ab
JF
1771
1772 spin_lock(&dcb_lock);
1773 /* Search for existing match and replace */
1774 list_for_each_entry(itr, &dcb_app_list, list) {
1775 if (itr->app.selector == new->selector &&
1776 itr->app.protocol == new->protocol &&
e290ed81 1777 itr->ifindex == dev->ifindex) {
9ab933ab
JF
1778 if (new->priority)
1779 itr->app.priority = new->priority;
1780 else {
1781 list_del(&itr->list);
1782 kfree(itr);
1783 }
1784 goto out;
1785 }
1786 }
1787 /* App type does not exist add new application type */
1788 if (new->priority) {
1789 struct dcb_app_type *entry;
1790 entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
1791 if (!entry) {
1792 spin_unlock(&dcb_lock);
1793 return -ENOMEM;
1794 }
1795
1796 memcpy(&entry->app, new, sizeof(*new));
e290ed81 1797 entry->ifindex = dev->ifindex;
9ab933ab
JF
1798 list_add(&entry->list, &dcb_app_list);
1799 }
1800out:
1801 spin_unlock(&dcb_lock);
7ec79270 1802 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
9ab933ab
JF
1803 return 0;
1804}
1805EXPORT_SYMBOL(dcb_setapp);
1806
a364c8cf
JF
1807/**
1808 * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
1809 *
1810 * Helper routine which on success returns a non-zero 802.1Qaz user
1811 * priority bitmap otherwise returns 0 to indicate the dcb_app was
1812 * not found in APP list.
1813 */
1814u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
1815{
1816 struct dcb_app_type *itr;
1817 u8 prio = 0;
1818
1819 spin_lock(&dcb_lock);
1820 list_for_each_entry(itr, &dcb_app_list, list) {
1821 if (itr->app.selector == app->selector &&
1822 itr->app.protocol == app->protocol &&
e290ed81 1823 itr->ifindex == dev->ifindex) {
a364c8cf
JF
1824 prio |= 1 << itr->app.priority;
1825 }
1826 }
1827 spin_unlock(&dcb_lock);
1828
1829 return prio;
1830}
1831EXPORT_SYMBOL(dcb_ieee_getapp_mask);
1832
b6db2174
JF
1833/**
1834 * dcb_ieee_setapp - add IEEE dcb application data to app list
1835 *
1836 * This adds Application data to the list. Multiple application
1837 * entries may exists for the same selector and protocol as long
1838 * as the priorities are different.
1839 */
1840int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
1841{
1842 struct dcb_app_type *itr, *entry;
1843 struct dcb_app_type event;
1844 int err = 0;
1845
e290ed81 1846 event.ifindex = dev->ifindex;
b6db2174 1847 memcpy(&event.app, new, sizeof(event.app));
6bd0e1cb
JF
1848 if (dev->dcbnl_ops->getdcbx)
1849 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
b6db2174
JF
1850
1851 spin_lock(&dcb_lock);
1852 /* Search for existing match and abort if found */
1853 list_for_each_entry(itr, &dcb_app_list, list) {
1854 if (itr->app.selector == new->selector &&
1855 itr->app.protocol == new->protocol &&
1856 itr->app.priority == new->priority &&
e290ed81 1857 itr->ifindex == dev->ifindex) {
b6db2174
JF
1858 err = -EEXIST;
1859 goto out;
1860 }
1861 }
1862
1863 /* App entry does not exist add new entry */
1864 entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
1865 if (!entry) {
1866 err = -ENOMEM;
1867 goto out;
1868 }
1869
1870 memcpy(&entry->app, new, sizeof(*new));
e290ed81 1871 entry->ifindex = dev->ifindex;
b6db2174
JF
1872 list_add(&entry->list, &dcb_app_list);
1873out:
1874 spin_unlock(&dcb_lock);
1875 if (!err)
1876 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
1877 return err;
1878}
1879EXPORT_SYMBOL(dcb_ieee_setapp);
1880
f9ae7e4b
JF
1881/**
1882 * dcb_ieee_delapp - delete IEEE dcb application data from list
1883 *
1884 * This removes a matching APP data from the APP list
1885 */
1886int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
1887{
1888 struct dcb_app_type *itr;
1889 struct dcb_app_type event;
1890 int err = -ENOENT;
1891
e290ed81 1892 event.ifindex = dev->ifindex;
f9ae7e4b 1893 memcpy(&event.app, del, sizeof(event.app));
6bd0e1cb
JF
1894 if (dev->dcbnl_ops->getdcbx)
1895 event.dcbx = dev->dcbnl_ops->getdcbx(dev);
f9ae7e4b
JF
1896
1897 spin_lock(&dcb_lock);
1898 /* Search for existing match and remove it. */
1899 list_for_each_entry(itr, &dcb_app_list, list) {
1900 if (itr->app.selector == del->selector &&
1901 itr->app.protocol == del->protocol &&
1902 itr->app.priority == del->priority &&
e290ed81 1903 itr->ifindex == dev->ifindex) {
f9ae7e4b
JF
1904 list_del(&itr->list);
1905 kfree(itr);
1906 err = 0;
1907 goto out;
1908 }
1909 }
1910
1911out:
1912 spin_unlock(&dcb_lock);
1913 if (!err)
1914 call_dcbevent_notifiers(DCB_APP_EVENT, &event);
1915 return err;
1916}
1917EXPORT_SYMBOL(dcb_ieee_delapp);
1918
7c14c3f1 1919static void dcb_flushapp(void)
9ab933ab
JF
1920{
1921 struct dcb_app_type *app;
2a8fe003 1922 struct dcb_app_type *tmp;
9ab933ab
JF
1923
1924 spin_lock(&dcb_lock);
2a8fe003 1925 list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
9ab933ab
JF
1926 list_del(&app->list);
1927 kfree(app);
1928 }
1929 spin_unlock(&dcb_lock);
1930}
1931
2f90b865
AD
1932static int __init dcbnl_init(void)
1933{
9ab933ab
JF
1934 INIT_LIST_HEAD(&dcb_app_list);
1935
c7ac8679
GR
1936 rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, NULL);
1937 rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, NULL);
2f90b865
AD
1938
1939 return 0;
1940}
1941module_init(dcbnl_init);
1942
1943static void __exit dcbnl_exit(void)
1944{
1945 rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
1946 rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
9ab933ab 1947 dcb_flushapp();
2f90b865
AD
1948}
1949module_exit(dcbnl_exit);