net: fix DCB setstate to return success/failure
[safe/jmp/linux-2.6] / net / dcb / dcbnl.c
1 /*
2  * Copyright (c) 2008, Intel Corporation.
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>
22 #include <net/netlink.h>
23 #include <net/rtnetlink.h>
24 #include <linux/dcbnl.h>
25 #include <linux/rtnetlink.h>
26 #include <net/sock.h>
27
28 /**
29  * Data Center Bridging (DCB) is a collection of Ethernet enhancements
30  * intended to allow network traffic with differing requirements
31  * (highly reliable, no drops vs. best effort vs. low latency) to operate
32  * and co-exist on Ethernet.  Current DCB features are:
33  *
34  * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
35  *   framework for assigning bandwidth guarantees to traffic classes.
36  *
37  * Priority-based Flow Control (PFC) - provides a flow control mechanism which
38  *   can work independently for each 802.1p priority.
39  *
40  * Congestion Notification - provides a mechanism for end-to-end congestion
41  *   control for protocols which do not have built-in congestion management.
42  *
43  * More information about the emerging standards for these Ethernet features
44  * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
45  *
46  * This file implements an rtnetlink interface to allow configuration of DCB
47  * features for capable devices.
48  */
49
50 MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
51 MODULE_DESCRIPTION("Data Center Bridging netlink interface");
52 MODULE_LICENSE("GPL");
53
54 /**************** DCB attribute policies *************************************/
55
56 /* DCB netlink attributes policy */
57 static struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
58         [DCB_ATTR_IFNAME]      = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
59         [DCB_ATTR_STATE]       = {.type = NLA_U8},
60         [DCB_ATTR_PFC_CFG]     = {.type = NLA_NESTED},
61         [DCB_ATTR_PG_CFG]      = {.type = NLA_NESTED},
62         [DCB_ATTR_SET_ALL]     = {.type = NLA_U8},
63         [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
64         [DCB_ATTR_CAP]         = {.type = NLA_NESTED},
65         [DCB_ATTR_PFC_STATE]   = {.type = NLA_U8},
66         [DCB_ATTR_BCN]         = {.type = NLA_NESTED},
67 };
68
69 /* DCB priority flow control to User Priority nested attributes */
70 static struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
71         [DCB_PFC_UP_ATTR_0]   = {.type = NLA_U8},
72         [DCB_PFC_UP_ATTR_1]   = {.type = NLA_U8},
73         [DCB_PFC_UP_ATTR_2]   = {.type = NLA_U8},
74         [DCB_PFC_UP_ATTR_3]   = {.type = NLA_U8},
75         [DCB_PFC_UP_ATTR_4]   = {.type = NLA_U8},
76         [DCB_PFC_UP_ATTR_5]   = {.type = NLA_U8},
77         [DCB_PFC_UP_ATTR_6]   = {.type = NLA_U8},
78         [DCB_PFC_UP_ATTR_7]   = {.type = NLA_U8},
79         [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
80 };
81
82 /* DCB priority grouping nested attributes */
83 static struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
84         [DCB_PG_ATTR_TC_0]      = {.type = NLA_NESTED},
85         [DCB_PG_ATTR_TC_1]      = {.type = NLA_NESTED},
86         [DCB_PG_ATTR_TC_2]      = {.type = NLA_NESTED},
87         [DCB_PG_ATTR_TC_3]      = {.type = NLA_NESTED},
88         [DCB_PG_ATTR_TC_4]      = {.type = NLA_NESTED},
89         [DCB_PG_ATTR_TC_5]      = {.type = NLA_NESTED},
90         [DCB_PG_ATTR_TC_6]      = {.type = NLA_NESTED},
91         [DCB_PG_ATTR_TC_7]      = {.type = NLA_NESTED},
92         [DCB_PG_ATTR_TC_ALL]    = {.type = NLA_NESTED},
93         [DCB_PG_ATTR_BW_ID_0]   = {.type = NLA_U8},
94         [DCB_PG_ATTR_BW_ID_1]   = {.type = NLA_U8},
95         [DCB_PG_ATTR_BW_ID_2]   = {.type = NLA_U8},
96         [DCB_PG_ATTR_BW_ID_3]   = {.type = NLA_U8},
97         [DCB_PG_ATTR_BW_ID_4]   = {.type = NLA_U8},
98         [DCB_PG_ATTR_BW_ID_5]   = {.type = NLA_U8},
99         [DCB_PG_ATTR_BW_ID_6]   = {.type = NLA_U8},
100         [DCB_PG_ATTR_BW_ID_7]   = {.type = NLA_U8},
101         [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
102 };
103
104 /* DCB traffic class nested attributes. */
105 static struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
106         [DCB_TC_ATTR_PARAM_PGID]            = {.type = NLA_U8},
107         [DCB_TC_ATTR_PARAM_UP_MAPPING]      = {.type = NLA_U8},
108         [DCB_TC_ATTR_PARAM_STRICT_PRIO]     = {.type = NLA_U8},
109         [DCB_TC_ATTR_PARAM_BW_PCT]          = {.type = NLA_U8},
110         [DCB_TC_ATTR_PARAM_ALL]             = {.type = NLA_FLAG},
111 };
112
113 /* DCB capabilities nested attributes. */
114 static struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
115         [DCB_CAP_ATTR_ALL]     = {.type = NLA_FLAG},
116         [DCB_CAP_ATTR_PG]      = {.type = NLA_U8},
117         [DCB_CAP_ATTR_PFC]     = {.type = NLA_U8},
118         [DCB_CAP_ATTR_UP2TC]   = {.type = NLA_U8},
119         [DCB_CAP_ATTR_PG_TCS]  = {.type = NLA_U8},
120         [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
121         [DCB_CAP_ATTR_GSP]     = {.type = NLA_U8},
122         [DCB_CAP_ATTR_BCN]     = {.type = NLA_U8},
123 };
124
125 /* DCB capabilities nested attributes. */
126 static struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
127         [DCB_NUMTCS_ATTR_ALL]     = {.type = NLA_FLAG},
128         [DCB_NUMTCS_ATTR_PG]      = {.type = NLA_U8},
129         [DCB_NUMTCS_ATTR_PFC]     = {.type = NLA_U8},
130 };
131
132 /* DCB BCN nested attributes. */
133 static struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
134         [DCB_BCN_ATTR_RP_0]         = {.type = NLA_U8},
135         [DCB_BCN_ATTR_RP_1]         = {.type = NLA_U8},
136         [DCB_BCN_ATTR_RP_2]         = {.type = NLA_U8},
137         [DCB_BCN_ATTR_RP_3]         = {.type = NLA_U8},
138         [DCB_BCN_ATTR_RP_4]         = {.type = NLA_U8},
139         [DCB_BCN_ATTR_RP_5]         = {.type = NLA_U8},
140         [DCB_BCN_ATTR_RP_6]         = {.type = NLA_U8},
141         [DCB_BCN_ATTR_RP_7]         = {.type = NLA_U8},
142         [DCB_BCN_ATTR_RP_ALL]       = {.type = NLA_FLAG},
143         [DCB_BCN_ATTR_ALPHA]        = {.type = NLA_U32},
144         [DCB_BCN_ATTR_BETA]         = {.type = NLA_U32},
145         [DCB_BCN_ATTR_GD]           = {.type = NLA_U32},
146         [DCB_BCN_ATTR_GI]           = {.type = NLA_U32},
147         [DCB_BCN_ATTR_TMAX]         = {.type = NLA_U32},
148         [DCB_BCN_ATTR_TD]           = {.type = NLA_U32},
149         [DCB_BCN_ATTR_RMIN]         = {.type = NLA_U32},
150         [DCB_BCN_ATTR_W]            = {.type = NLA_U32},
151         [DCB_BCN_ATTR_RD]           = {.type = NLA_U32},
152         [DCB_BCN_ATTR_RU]           = {.type = NLA_U32},
153         [DCB_BCN_ATTR_WRTT]         = {.type = NLA_U32},
154         [DCB_BCN_ATTR_RI]           = {.type = NLA_U32},
155         [DCB_BCN_ATTR_C]            = {.type = NLA_U32},
156         [DCB_BCN_ATTR_ALL]          = {.type = NLA_FLAG},
157 };
158
159 /* standard netlink reply call */
160 static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,
161                        u32 seq, u16 flags)
162 {
163         struct sk_buff *dcbnl_skb;
164         struct dcbmsg *dcb;
165         struct nlmsghdr *nlh;
166         int ret = -EINVAL;
167
168         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
169         if (!dcbnl_skb)
170                 return ret;
171
172         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);
173
174         dcb = NLMSG_DATA(nlh);
175         dcb->dcb_family = AF_UNSPEC;
176         dcb->cmd = cmd;
177         dcb->dcb_pad = 0;
178
179         ret = nla_put_u8(dcbnl_skb, attr, value);
180         if (ret)
181                 goto err;
182
183         /* end the message, assign the nlmsg_len. */
184         nlmsg_end(dcbnl_skb, nlh);
185         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
186         if (ret)
187                 goto err;
188
189         return 0;
190 nlmsg_failure:
191 err:
192         kfree(dcbnl_skb);
193         return ret;
194 }
195
196 static int dcbnl_getstate(struct net_device *netdev, struct nlattr **tb,
197                           u32 pid, u32 seq, u16 flags)
198 {
199         int ret = -EINVAL;
200
201         /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
202         if (!netdev->dcbnl_ops->getstate)
203                 return ret;
204
205         ret = dcbnl_reply(netdev->dcbnl_ops->getstate(netdev), RTM_GETDCB,
206                           DCB_CMD_GSTATE, DCB_ATTR_STATE, pid, seq, flags);
207
208         return ret;
209 }
210
211 static int dcbnl_getpfccfg(struct net_device *netdev, struct nlattr **tb,
212                            u32 pid, u32 seq, u16 flags)
213 {
214         struct sk_buff *dcbnl_skb;
215         struct nlmsghdr *nlh;
216         struct dcbmsg *dcb;
217         struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
218         u8 value;
219         int ret = -EINVAL;
220         int i;
221         int getall = 0;
222
223         if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
224                 return ret;
225
226         ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
227                                tb[DCB_ATTR_PFC_CFG],
228                                dcbnl_pfc_up_nest);
229         if (ret)
230                 goto err_out;
231
232         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
233         if (!dcbnl_skb)
234                 goto err_out;
235
236         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
237
238         dcb = NLMSG_DATA(nlh);
239         dcb->dcb_family = AF_UNSPEC;
240         dcb->cmd = DCB_CMD_PFC_GCFG;
241
242         nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PFC_CFG);
243         if (!nest)
244                 goto err;
245
246         if (data[DCB_PFC_UP_ATTR_ALL])
247                 getall = 1;
248
249         for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
250                 if (!getall && !data[i])
251                         continue;
252
253                 netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
254                                              &value);
255                 ret = nla_put_u8(dcbnl_skb, i, value);
256
257                 if (ret) {
258                         nla_nest_cancel(dcbnl_skb, nest);
259                         goto err;
260                 }
261         }
262         nla_nest_end(dcbnl_skb, nest);
263
264         nlmsg_end(dcbnl_skb, nlh);
265
266         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
267         if (ret)
268                 goto err;
269
270         return 0;
271 nlmsg_failure:
272 err:
273         kfree(dcbnl_skb);
274 err_out:
275         return -EINVAL;
276 }
277
278 static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlattr **tb,
279                                 u32 pid, u32 seq, u16 flags)
280 {
281         struct sk_buff *dcbnl_skb;
282         struct nlmsghdr *nlh;
283         struct dcbmsg *dcb;
284         u8 perm_addr[MAX_ADDR_LEN];
285         int ret = -EINVAL;
286
287         if (!netdev->dcbnl_ops->getpermhwaddr)
288                 return ret;
289
290         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
291         if (!dcbnl_skb)
292                 goto err_out;
293
294         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
295
296         dcb = NLMSG_DATA(nlh);
297         dcb->dcb_family = AF_UNSPEC;
298         dcb->cmd = DCB_CMD_GPERM_HWADDR;
299
300         netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
301
302         ret = nla_put(dcbnl_skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr),
303                       perm_addr);
304
305         nlmsg_end(dcbnl_skb, nlh);
306
307         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
308         if (ret)
309                 goto err;
310
311         return 0;
312
313 nlmsg_failure:
314 err:
315         kfree(dcbnl_skb);
316 err_out:
317         return -EINVAL;
318 }
319
320 static int dcbnl_getcap(struct net_device *netdev, struct nlattr **tb,
321                         u32 pid, u32 seq, u16 flags)
322 {
323         struct sk_buff *dcbnl_skb;
324         struct nlmsghdr *nlh;
325         struct dcbmsg *dcb;
326         struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
327         u8 value;
328         int ret = -EINVAL;
329         int i;
330         int getall = 0;
331
332         if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
333                 return ret;
334
335         ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
336                                dcbnl_cap_nest);
337         if (ret)
338                 goto err_out;
339
340         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
341         if (!dcbnl_skb)
342                 goto err_out;
343
344         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
345
346         dcb = NLMSG_DATA(nlh);
347         dcb->dcb_family = AF_UNSPEC;
348         dcb->cmd = DCB_CMD_GCAP;
349
350         nest = nla_nest_start(dcbnl_skb, DCB_ATTR_CAP);
351         if (!nest)
352                 goto err;
353
354         if (data[DCB_CAP_ATTR_ALL])
355                 getall = 1;
356
357         for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
358                 if (!getall && !data[i])
359                         continue;
360
361                 if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
362                         ret = nla_put_u8(dcbnl_skb, i, value);
363
364                         if (ret) {
365                                 nla_nest_cancel(dcbnl_skb, nest);
366                                 goto err;
367                         }
368                 }
369         }
370         nla_nest_end(dcbnl_skb, nest);
371
372         nlmsg_end(dcbnl_skb, nlh);
373
374         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
375         if (ret)
376                 goto err;
377
378         return 0;
379 nlmsg_failure:
380 err:
381         kfree(dcbnl_skb);
382 err_out:
383         return -EINVAL;
384 }
385
386 static int dcbnl_getnumtcs(struct net_device *netdev, struct nlattr **tb,
387                            u32 pid, u32 seq, u16 flags)
388 {
389         struct sk_buff *dcbnl_skb;
390         struct nlmsghdr *nlh;
391         struct dcbmsg *dcb;
392         struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
393         u8 value;
394         int ret = -EINVAL;
395         int i;
396         int getall = 0;
397
398         if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
399                 return ret;
400
401         ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
402                                dcbnl_numtcs_nest);
403         if (ret) {
404                 ret = -EINVAL;
405                 goto err_out;
406         }
407
408         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
409         if (!dcbnl_skb) {
410                 ret = -EINVAL;
411                 goto err_out;
412         }
413
414         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
415
416         dcb = NLMSG_DATA(nlh);
417         dcb->dcb_family = AF_UNSPEC;
418         dcb->cmd = DCB_CMD_GNUMTCS;
419
420         nest = nla_nest_start(dcbnl_skb, DCB_ATTR_NUMTCS);
421         if (!nest) {
422                 ret = -EINVAL;
423                 goto err;
424         }
425
426         if (data[DCB_NUMTCS_ATTR_ALL])
427                 getall = 1;
428
429         for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
430                 if (!getall && !data[i])
431                         continue;
432
433                 ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
434                 if (!ret) {
435                         ret = nla_put_u8(dcbnl_skb, i, value);
436
437                         if (ret) {
438                                 nla_nest_cancel(dcbnl_skb, nest);
439                                 ret = -EINVAL;
440                                 goto err;
441                         }
442                 } else {
443                         goto err;
444                 }
445         }
446         nla_nest_end(dcbnl_skb, nest);
447
448         nlmsg_end(dcbnl_skb, nlh);
449
450         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
451         if (ret) {
452                 ret = -EINVAL;
453                 goto err;
454         }
455
456         return 0;
457 nlmsg_failure:
458 err:
459         kfree(dcbnl_skb);
460 err_out:
461         return ret;
462 }
463
464 static int dcbnl_setnumtcs(struct net_device *netdev, struct nlattr **tb,
465                            u32 pid, u32 seq, u16 flags)
466 {
467         struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
468         int ret = -EINVAL;
469         u8 value;
470         int i;
471
472         if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
473                 return ret;
474
475         ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
476                                dcbnl_numtcs_nest);
477
478         if (ret) {
479                 ret = -EINVAL;
480                 goto err;
481         }
482
483         for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
484                 if (data[i] == NULL)
485                         continue;
486
487                 value = nla_get_u8(data[i]);
488
489                 ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
490
491                 if (ret)
492                         goto operr;
493         }
494
495 operr:
496         ret = dcbnl_reply(!!ret, RTM_SETDCB, DCB_CMD_SNUMTCS,
497                           DCB_ATTR_NUMTCS, pid, seq, flags);
498
499 err:
500         return ret;
501 }
502
503 static int dcbnl_getpfcstate(struct net_device *netdev, struct nlattr **tb,
504                              u32 pid, u32 seq, u16 flags)
505 {
506         int ret = -EINVAL;
507
508         if (!netdev->dcbnl_ops->getpfcstate)
509                 return ret;
510
511         ret = dcbnl_reply(netdev->dcbnl_ops->getpfcstate(netdev), RTM_GETDCB,
512                           DCB_CMD_PFC_GSTATE, DCB_ATTR_PFC_STATE,
513                           pid, seq, flags);
514
515         return ret;
516 }
517
518 static int dcbnl_setpfcstate(struct net_device *netdev, struct nlattr **tb,
519                              u32 pid, u32 seq, u16 flags)
520 {
521         int ret = -EINVAL;
522         u8 value;
523
524         if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
525                 return ret;
526
527         value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
528
529         netdev->dcbnl_ops->setpfcstate(netdev, value);
530
531         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE,
532                           pid, seq, flags);
533
534         return ret;
535 }
536
537 static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlattr **tb,
538                              u32 pid, u32 seq, u16 flags, int dir)
539 {
540         struct sk_buff *dcbnl_skb;
541         struct nlmsghdr *nlh;
542         struct dcbmsg *dcb;
543         struct nlattr *pg_nest, *param_nest, *data;
544         struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
545         struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
546         u8 prio, pgid, tc_pct, up_map;
547         int ret  = -EINVAL;
548         int getall = 0;
549         int i;
550
551         if (!tb[DCB_ATTR_PG_CFG] ||
552             !netdev->dcbnl_ops->getpgtccfgtx ||
553             !netdev->dcbnl_ops->getpgtccfgrx ||
554             !netdev->dcbnl_ops->getpgbwgcfgtx ||
555             !netdev->dcbnl_ops->getpgbwgcfgrx)
556                 return ret;
557
558         ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
559                                tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
560
561         if (ret)
562                 goto err_out;
563
564         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
565         if (!dcbnl_skb)
566                 goto err_out;
567
568         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
569
570         dcb = NLMSG_DATA(nlh);
571         dcb->dcb_family = AF_UNSPEC;
572         dcb->cmd = (dir) ? DCB_CMD_PGRX_GCFG : DCB_CMD_PGTX_GCFG;
573
574         pg_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PG_CFG);
575         if (!pg_nest)
576                 goto err;
577
578         if (pg_tb[DCB_PG_ATTR_TC_ALL])
579                 getall = 1;
580
581         for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
582                 if (!getall && !pg_tb[i])
583                         continue;
584
585                 if (pg_tb[DCB_PG_ATTR_TC_ALL])
586                         data = pg_tb[DCB_PG_ATTR_TC_ALL];
587                 else
588                         data = pg_tb[i];
589                 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
590                                        data, dcbnl_tc_param_nest);
591                 if (ret)
592                         goto err_pg;
593
594                 param_nest = nla_nest_start(dcbnl_skb, i);
595                 if (!param_nest)
596                         goto err_pg;
597
598                 pgid = DCB_ATTR_VALUE_UNDEFINED;
599                 prio = DCB_ATTR_VALUE_UNDEFINED;
600                 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
601                 up_map = DCB_ATTR_VALUE_UNDEFINED;
602
603                 if (dir) {
604                         /* Rx */
605                         netdev->dcbnl_ops->getpgtccfgrx(netdev,
606                                                 i - DCB_PG_ATTR_TC_0, &prio,
607                                                 &pgid, &tc_pct, &up_map);
608                 } else {
609                         /* Tx */
610                         netdev->dcbnl_ops->getpgtccfgtx(netdev,
611                                                 i - DCB_PG_ATTR_TC_0, &prio,
612                                                 &pgid, &tc_pct, &up_map);
613                 }
614
615                 if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
616                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
617                         ret = nla_put_u8(dcbnl_skb,
618                                          DCB_TC_ATTR_PARAM_PGID, pgid);
619                         if (ret)
620                                 goto err_param;
621                 }
622                 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
623                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
624                         ret = nla_put_u8(dcbnl_skb,
625                                          DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
626                         if (ret)
627                                 goto err_param;
628                 }
629                 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
630                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
631                         ret = nla_put_u8(dcbnl_skb,
632                                          DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
633                         if (ret)
634                                 goto err_param;
635                 }
636                 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
637                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
638                         ret = nla_put_u8(dcbnl_skb, DCB_TC_ATTR_PARAM_BW_PCT,
639                                          tc_pct);
640                         if (ret)
641                                 goto err_param;
642                 }
643                 nla_nest_end(dcbnl_skb, param_nest);
644         }
645
646         if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
647                 getall = 1;
648         else
649                 getall = 0;
650
651         for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
652                 if (!getall && !pg_tb[i])
653                         continue;
654
655                 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
656
657                 if (dir) {
658                         /* Rx */
659                         netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
660                                         i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
661                 } else {
662                         /* Tx */
663                         netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
664                                         i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
665                 }
666                 ret = nla_put_u8(dcbnl_skb, i, tc_pct);
667
668                 if (ret)
669                         goto err_pg;
670         }
671
672         nla_nest_end(dcbnl_skb, pg_nest);
673
674         nlmsg_end(dcbnl_skb, nlh);
675
676         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
677         if (ret)
678                 goto err;
679
680         return 0;
681
682 err_param:
683         nla_nest_cancel(dcbnl_skb, param_nest);
684 err_pg:
685         nla_nest_cancel(dcbnl_skb, pg_nest);
686 nlmsg_failure:
687 err:
688         kfree(dcbnl_skb);
689 err_out:
690         ret  = -EINVAL;
691         return ret;
692 }
693
694 static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlattr **tb,
695                              u32 pid, u32 seq, u16 flags)
696 {
697         return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 0);
698 }
699
700 static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlattr **tb,
701                              u32 pid, u32 seq, u16 flags)
702 {
703         return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 1);
704 }
705
706 static int dcbnl_setstate(struct net_device *netdev, struct nlattr **tb,
707                           u32 pid, u32 seq, u16 flags)
708 {
709         int ret = -EINVAL;
710         u8 value;
711
712         if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
713                 return ret;
714
715         value = nla_get_u8(tb[DCB_ATTR_STATE]);
716
717         ret = dcbnl_reply(netdev->dcbnl_ops->setstate(netdev, value),
718                           RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
719                           pid, seq, flags);
720
721         return ret;
722 }
723
724 static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
725                            u32 pid, u32 seq, u16 flags)
726 {
727         struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
728         int i;
729         int ret = -EINVAL;
730         u8 value;
731
732         if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
733                 return ret;
734
735         ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
736                                tb[DCB_ATTR_PFC_CFG],
737                                dcbnl_pfc_up_nest);
738         if (ret)
739                 goto err;
740
741         for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
742                 if (data[i] == NULL)
743                         continue;
744                 value = nla_get_u8(data[i]);
745                 netdev->dcbnl_ops->setpfccfg(netdev,
746                         data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
747         }
748
749         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
750                           pid, seq, flags);
751 err:
752         return ret;
753 }
754
755 static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
756                         u32 pid, u32 seq, u16 flags)
757 {
758         int ret = -EINVAL;
759
760         if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
761                 return ret;
762
763         ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
764                           DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
765
766         return ret;
767 }
768
769 static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
770                              u32 pid, u32 seq, u16 flags, int dir)
771 {
772         struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
773         struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
774         int ret = -EINVAL;
775         int i;
776         u8 pgid;
777         u8 up_map;
778         u8 prio;
779         u8 tc_pct;
780
781         if (!tb[DCB_ATTR_PG_CFG] ||
782             !netdev->dcbnl_ops->setpgtccfgtx ||
783             !netdev->dcbnl_ops->setpgtccfgrx ||
784             !netdev->dcbnl_ops->setpgbwgcfgtx ||
785             !netdev->dcbnl_ops->setpgbwgcfgrx)
786                 return ret;
787
788         ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
789                                tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
790         if (ret)
791                 goto err;
792
793         for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
794                 if (!pg_tb[i])
795                         continue;
796
797                 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
798                                        pg_tb[i], dcbnl_tc_param_nest);
799                 if (ret)
800                         goto err;
801
802                 pgid = DCB_ATTR_VALUE_UNDEFINED;
803                 prio = DCB_ATTR_VALUE_UNDEFINED;
804                 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
805                 up_map = DCB_ATTR_VALUE_UNDEFINED;
806
807                 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
808                         prio =
809                             nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
810
811                 if (param_tb[DCB_TC_ATTR_PARAM_PGID])
812                         pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
813
814                 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
815                         tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
816
817                 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
818                         up_map =
819                              nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
820
821                 /* dir: Tx = 0, Rx = 1 */
822                 if (dir) {
823                         /* Rx */
824                         netdev->dcbnl_ops->setpgtccfgrx(netdev,
825                                 i - DCB_PG_ATTR_TC_0,
826                                 prio, pgid, tc_pct, up_map);
827                 } else {
828                         /* Tx */
829                         netdev->dcbnl_ops->setpgtccfgtx(netdev,
830                                 i - DCB_PG_ATTR_TC_0,
831                                 prio, pgid, tc_pct, up_map);
832                 }
833         }
834
835         for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
836                 if (!pg_tb[i])
837                         continue;
838
839                 tc_pct = nla_get_u8(pg_tb[i]);
840
841                 /* dir: Tx = 0, Rx = 1 */
842                 if (dir) {
843                         /* Rx */
844                         netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
845                                          i - DCB_PG_ATTR_BW_ID_0, tc_pct);
846                 } else {
847                         /* Tx */
848                         netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
849                                          i - DCB_PG_ATTR_BW_ID_0, tc_pct);
850                 }
851         }
852
853         ret = dcbnl_reply(0, RTM_SETDCB,
854                           (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
855                           DCB_ATTR_PG_CFG, pid, seq, flags);
856
857 err:
858         return ret;
859 }
860
861 static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
862                              u32 pid, u32 seq, u16 flags)
863 {
864         return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
865 }
866
867 static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
868                              u32 pid, u32 seq, u16 flags)
869 {
870         return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
871 }
872
873 static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlattr **tb,
874                             u32 pid, u32 seq, u16 flags)
875 {
876         struct sk_buff *dcbnl_skb;
877         struct nlmsghdr *nlh;
878         struct dcbmsg *dcb;
879         struct nlattr *bcn_nest;
880         struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
881         u8 value_byte;
882         u32 value_integer;
883         int ret  = -EINVAL;
884         bool getall = false;
885         int i;
886
887         if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
888             !netdev->dcbnl_ops->getbcncfg)
889                 return ret;
890
891         ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
892                                tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
893
894         if (ret)
895                 goto err_out;
896
897         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
898         if (!dcbnl_skb)
899                 goto err_out;
900
901         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
902
903         dcb = NLMSG_DATA(nlh);
904         dcb->dcb_family = AF_UNSPEC;
905         dcb->cmd = DCB_CMD_BCN_GCFG;
906
907         bcn_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_BCN);
908         if (!bcn_nest)
909                 goto err;
910
911         if (bcn_tb[DCB_BCN_ATTR_ALL])
912                 getall = true;
913
914         for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
915                 if (!getall && !bcn_tb[i])
916                         continue;
917
918                 netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
919                                             &value_byte);
920                 ret = nla_put_u8(dcbnl_skb, i, value_byte);
921                 if (ret)
922                         goto err_bcn;
923         }
924
925         for (i = DCB_BCN_ATTR_ALPHA; i <= DCB_BCN_ATTR_RI; i++) {
926                 if (!getall && !bcn_tb[i])
927                         continue;
928
929                 netdev->dcbnl_ops->getbcncfg(netdev, i,
930                                              &value_integer);
931                 ret = nla_put_u32(dcbnl_skb, i, value_integer);
932                 if (ret)
933                         goto err_bcn;
934         }
935
936         nla_nest_end(dcbnl_skb, bcn_nest);
937
938         nlmsg_end(dcbnl_skb, nlh);
939
940         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
941         if (ret)
942                 goto err;
943
944         return 0;
945
946 err_bcn:
947         nla_nest_cancel(dcbnl_skb, bcn_nest);
948 nlmsg_failure:
949 err:
950         kfree(dcbnl_skb);
951 err_out:
952         ret  = -EINVAL;
953         return ret;
954 }
955
956 static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlattr **tb,
957                             u32 pid, u32 seq, u16 flags)
958 {
959         struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
960         int i;
961         int ret = -EINVAL;
962         u8 value_byte;
963         u32 value_int;
964
965         if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg
966             || !netdev->dcbnl_ops->setbcnrp)
967                 return ret;
968
969         ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
970                                tb[DCB_ATTR_BCN],
971                                dcbnl_pfc_up_nest);
972         if (ret)
973                 goto err;
974
975         for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
976                 if (data[i] == NULL)
977                         continue;
978                 value_byte = nla_get_u8(data[i]);
979                 netdev->dcbnl_ops->setbcnrp(netdev,
980                         data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
981         }
982
983         for (i = DCB_BCN_ATTR_ALPHA; i <= DCB_BCN_ATTR_RI; i++) {
984                 if (data[i] == NULL)
985                         continue;
986                 value_int = nla_get_u32(data[i]);
987                 netdev->dcbnl_ops->setbcncfg(netdev,
988                                              i, value_int);
989         }
990
991         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_BCN_SCFG, DCB_ATTR_BCN,
992                           pid, seq, flags);
993 err:
994         return ret;
995 }
996
997 static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
998 {
999         struct net *net = sock_net(skb->sk);
1000         struct net_device *netdev;
1001         struct dcbmsg  *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
1002         struct nlattr *tb[DCB_ATTR_MAX + 1];
1003         u32 pid = skb ? NETLINK_CB(skb).pid : 0;
1004         int ret = -EINVAL;
1005
1006         if (net != &init_net)
1007                 return -EINVAL;
1008
1009         ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
1010                           dcbnl_rtnl_policy);
1011         if (ret < 0)
1012                 return ret;
1013
1014         if (!tb[DCB_ATTR_IFNAME])
1015                 return -EINVAL;
1016
1017         netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
1018         if (!netdev)
1019                 return -EINVAL;
1020
1021         if (!netdev->dcbnl_ops)
1022                 goto errout;
1023
1024         switch (dcb->cmd) {
1025         case DCB_CMD_GSTATE:
1026                 ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
1027                                      nlh->nlmsg_flags);
1028                 goto out;
1029         case DCB_CMD_PFC_GCFG:
1030                 ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
1031                                       nlh->nlmsg_flags);
1032                 goto out;
1033         case DCB_CMD_GPERM_HWADDR:
1034                 ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
1035                                            nlh->nlmsg_flags);
1036                 goto out;
1037         case DCB_CMD_PGTX_GCFG:
1038                 ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1039                                         nlh->nlmsg_flags);
1040                 goto out;
1041         case DCB_CMD_PGRX_GCFG:
1042                 ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1043                                         nlh->nlmsg_flags);
1044                 goto out;
1045         case DCB_CMD_BCN_GCFG:
1046                 ret = dcbnl_bcn_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1047                                        nlh->nlmsg_flags);
1048                 goto out;
1049         case DCB_CMD_SSTATE:
1050                 ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
1051                                      nlh->nlmsg_flags);
1052                 goto out;
1053         case DCB_CMD_PFC_SCFG:
1054                 ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
1055                                       nlh->nlmsg_flags);
1056                 goto out;
1057
1058         case DCB_CMD_SET_ALL:
1059                 ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
1060                                    nlh->nlmsg_flags);
1061                 goto out;
1062         case DCB_CMD_PGTX_SCFG:
1063                 ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1064                                         nlh->nlmsg_flags);
1065                 goto out;
1066         case DCB_CMD_PGRX_SCFG:
1067                 ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1068                                         nlh->nlmsg_flags);
1069                 goto out;
1070         case DCB_CMD_GCAP:
1071                 ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
1072                                    nlh->nlmsg_flags);
1073                 goto out;
1074         case DCB_CMD_GNUMTCS:
1075                 ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
1076                                       nlh->nlmsg_flags);
1077                 goto out;
1078         case DCB_CMD_SNUMTCS:
1079                 ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
1080                                       nlh->nlmsg_flags);
1081                 goto out;
1082         case DCB_CMD_PFC_GSTATE:
1083                 ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
1084                                         nlh->nlmsg_flags);
1085                 goto out;
1086         case DCB_CMD_PFC_SSTATE:
1087                 ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
1088                                         nlh->nlmsg_flags);
1089                 goto out;
1090         case DCB_CMD_BCN_SCFG:
1091                 ret = dcbnl_bcn_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1092                                        nlh->nlmsg_flags);
1093                 goto out;
1094         default:
1095                 goto errout;
1096         }
1097 errout:
1098         ret = -EINVAL;
1099 out:
1100         dev_put(netdev);
1101         return ret;
1102 }
1103
1104 static int __init dcbnl_init(void)
1105 {
1106         rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL);
1107         rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL);
1108
1109         return 0;
1110 }
1111 module_init(dcbnl_init);
1112
1113 static void __exit dcbnl_exit(void)
1114 {
1115         rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
1116         rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
1117 }
1118 module_exit(dcbnl_exit);
1119
1120