net: fix dcbnl_setnumtcs operation check
[linux-2.6.git] / 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         netdev->dcbnl_ops->setstate(netdev, value);
718
719         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
720                           pid, seq, flags);
721
722         return ret;
723 }
724
725 static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
726                            u32 pid, u32 seq, u16 flags)
727 {
728         struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
729         int i;
730         int ret = -EINVAL;
731         u8 value;
732
733         if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
734                 return ret;
735
736         ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
737                                tb[DCB_ATTR_PFC_CFG],
738                                dcbnl_pfc_up_nest);
739         if (ret)
740                 goto err;
741
742         for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
743                 if (data[i] == NULL)
744                         continue;
745                 value = nla_get_u8(data[i]);
746                 netdev->dcbnl_ops->setpfccfg(netdev,
747                         data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
748         }
749
750         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
751                           pid, seq, flags);
752 err:
753         return ret;
754 }
755
756 static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
757                         u32 pid, u32 seq, u16 flags)
758 {
759         int ret = -EINVAL;
760
761         if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
762                 return ret;
763
764         ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
765                           DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
766
767         return ret;
768 }
769
770 static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
771                              u32 pid, u32 seq, u16 flags, int dir)
772 {
773         struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
774         struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
775         int ret = -EINVAL;
776         int i;
777         u8 pgid;
778         u8 up_map;
779         u8 prio;
780         u8 tc_pct;
781
782         if (!tb[DCB_ATTR_PG_CFG] ||
783             !netdev->dcbnl_ops->setpgtccfgtx ||
784             !netdev->dcbnl_ops->setpgtccfgrx ||
785             !netdev->dcbnl_ops->setpgbwgcfgtx ||
786             !netdev->dcbnl_ops->setpgbwgcfgrx)
787                 return ret;
788
789         ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
790                                tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
791         if (ret)
792                 goto err;
793
794         for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
795                 if (!pg_tb[i])
796                         continue;
797
798                 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
799                                        pg_tb[i], dcbnl_tc_param_nest);
800                 if (ret)
801                         goto err;
802
803                 pgid = DCB_ATTR_VALUE_UNDEFINED;
804                 prio = DCB_ATTR_VALUE_UNDEFINED;
805                 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
806                 up_map = DCB_ATTR_VALUE_UNDEFINED;
807
808                 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
809                         prio =
810                             nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
811
812                 if (param_tb[DCB_TC_ATTR_PARAM_PGID])
813                         pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
814
815                 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
816                         tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
817
818                 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
819                         up_map =
820                              nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
821
822                 /* dir: Tx = 0, Rx = 1 */
823                 if (dir) {
824                         /* Rx */
825                         netdev->dcbnl_ops->setpgtccfgrx(netdev,
826                                 i - DCB_PG_ATTR_TC_0,
827                                 prio, pgid, tc_pct, up_map);
828                 } else {
829                         /* Tx */
830                         netdev->dcbnl_ops->setpgtccfgtx(netdev,
831                                 i - DCB_PG_ATTR_TC_0,
832                                 prio, pgid, tc_pct, up_map);
833                 }
834         }
835
836         for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
837                 if (!pg_tb[i])
838                         continue;
839
840                 tc_pct = nla_get_u8(pg_tb[i]);
841
842                 /* dir: Tx = 0, Rx = 1 */
843                 if (dir) {
844                         /* Rx */
845                         netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
846                                          i - DCB_PG_ATTR_BW_ID_0, tc_pct);
847                 } else {
848                         /* Tx */
849                         netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
850                                          i - DCB_PG_ATTR_BW_ID_0, tc_pct);
851                 }
852         }
853
854         ret = dcbnl_reply(0, RTM_SETDCB,
855                           (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
856                           DCB_ATTR_PG_CFG, pid, seq, flags);
857
858 err:
859         return ret;
860 }
861
862 static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
863                              u32 pid, u32 seq, u16 flags)
864 {
865         return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
866 }
867
868 static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
869                              u32 pid, u32 seq, u16 flags)
870 {
871         return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
872 }
873
874 static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlattr **tb,
875                             u32 pid, u32 seq, u16 flags)
876 {
877         struct sk_buff *dcbnl_skb;
878         struct nlmsghdr *nlh;
879         struct dcbmsg *dcb;
880         struct nlattr *bcn_nest;
881         struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
882         u8 value_byte;
883         u32 value_integer;
884         int ret  = -EINVAL;
885         bool getall = false;
886         int i;
887
888         if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
889             !netdev->dcbnl_ops->getbcncfg)
890                 return ret;
891
892         ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
893                                tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
894
895         if (ret)
896                 goto err_out;
897
898         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
899         if (!dcbnl_skb)
900                 goto err_out;
901
902         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
903
904         dcb = NLMSG_DATA(nlh);
905         dcb->dcb_family = AF_UNSPEC;
906         dcb->cmd = DCB_CMD_BCN_GCFG;
907
908         bcn_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_BCN);
909         if (!bcn_nest)
910                 goto err;
911
912         if (bcn_tb[DCB_BCN_ATTR_ALL])
913                 getall = true;
914
915         for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
916                 if (!getall && !bcn_tb[i])
917                         continue;
918
919                 netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
920                                             &value_byte);
921                 ret = nla_put_u8(dcbnl_skb, i, value_byte);
922                 if (ret)
923                         goto err_bcn;
924         }
925
926         for (i = DCB_BCN_ATTR_ALPHA; i <= DCB_BCN_ATTR_RI; i++) {
927                 if (!getall && !bcn_tb[i])
928                         continue;
929
930                 netdev->dcbnl_ops->getbcncfg(netdev, i,
931                                              &value_integer);
932                 ret = nla_put_u32(dcbnl_skb, i, value_integer);
933                 if (ret)
934                         goto err_bcn;
935         }
936
937         nla_nest_end(dcbnl_skb, bcn_nest);
938
939         nlmsg_end(dcbnl_skb, nlh);
940
941         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
942         if (ret)
943                 goto err;
944
945         return 0;
946
947 err_bcn:
948         nla_nest_cancel(dcbnl_skb, bcn_nest);
949 nlmsg_failure:
950 err:
951         kfree(dcbnl_skb);
952 err_out:
953         ret  = -EINVAL;
954         return ret;
955 }
956
957 static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlattr **tb,
958                             u32 pid, u32 seq, u16 flags)
959 {
960         struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
961         int i;
962         int ret = -EINVAL;
963         u8 value_byte;
964         u32 value_int;
965
966         if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg
967             || !netdev->dcbnl_ops->setbcnrp)
968                 return ret;
969
970         ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
971                                tb[DCB_ATTR_BCN],
972                                dcbnl_pfc_up_nest);
973         if (ret)
974                 goto err;
975
976         for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
977                 if (data[i] == NULL)
978                         continue;
979                 value_byte = nla_get_u8(data[i]);
980                 netdev->dcbnl_ops->setbcnrp(netdev,
981                         data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
982         }
983
984         for (i = DCB_BCN_ATTR_ALPHA; i <= DCB_BCN_ATTR_RI; i++) {
985                 if (data[i] == NULL)
986                         continue;
987                 value_int = nla_get_u32(data[i]);
988                 netdev->dcbnl_ops->setbcncfg(netdev,
989                                              i, value_int);
990         }
991
992         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_BCN_SCFG, DCB_ATTR_BCN,
993                           pid, seq, flags);
994 err:
995         return ret;
996 }
997
998 static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
999 {
1000         struct net *net = sock_net(skb->sk);
1001         struct net_device *netdev;
1002         struct dcbmsg  *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
1003         struct nlattr *tb[DCB_ATTR_MAX + 1];
1004         u32 pid = skb ? NETLINK_CB(skb).pid : 0;
1005         int ret = -EINVAL;
1006
1007         if (net != &init_net)
1008                 return -EINVAL;
1009
1010         ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
1011                           dcbnl_rtnl_policy);
1012         if (ret < 0)
1013                 return ret;
1014
1015         if (!tb[DCB_ATTR_IFNAME])
1016                 return -EINVAL;
1017
1018         netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
1019         if (!netdev)
1020                 return -EINVAL;
1021
1022         if (!netdev->dcbnl_ops)
1023                 goto errout;
1024
1025         switch (dcb->cmd) {
1026         case DCB_CMD_GSTATE:
1027                 ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
1028                                      nlh->nlmsg_flags);
1029                 goto out;
1030         case DCB_CMD_PFC_GCFG:
1031                 ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
1032                                       nlh->nlmsg_flags);
1033                 goto out;
1034         case DCB_CMD_GPERM_HWADDR:
1035                 ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
1036                                            nlh->nlmsg_flags);
1037                 goto out;
1038         case DCB_CMD_PGTX_GCFG:
1039                 ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1040                                         nlh->nlmsg_flags);
1041                 goto out;
1042         case DCB_CMD_PGRX_GCFG:
1043                 ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1044                                         nlh->nlmsg_flags);
1045                 goto out;
1046         case DCB_CMD_BCN_GCFG:
1047                 ret = dcbnl_bcn_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1048                                        nlh->nlmsg_flags);
1049                 goto out;
1050         case DCB_CMD_SSTATE:
1051                 ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
1052                                      nlh->nlmsg_flags);
1053                 goto out;
1054         case DCB_CMD_PFC_SCFG:
1055                 ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
1056                                       nlh->nlmsg_flags);
1057                 goto out;
1058
1059         case DCB_CMD_SET_ALL:
1060                 ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
1061                                    nlh->nlmsg_flags);
1062                 goto out;
1063         case DCB_CMD_PGTX_SCFG:
1064                 ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1065                                         nlh->nlmsg_flags);
1066                 goto out;
1067         case DCB_CMD_PGRX_SCFG:
1068                 ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1069                                         nlh->nlmsg_flags);
1070                 goto out;
1071         case DCB_CMD_GCAP:
1072                 ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
1073                                    nlh->nlmsg_flags);
1074                 goto out;
1075         case DCB_CMD_GNUMTCS:
1076                 ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
1077                                       nlh->nlmsg_flags);
1078                 goto out;
1079         case DCB_CMD_SNUMTCS:
1080                 ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
1081                                       nlh->nlmsg_flags);
1082                 goto out;
1083         case DCB_CMD_PFC_GSTATE:
1084                 ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
1085                                         nlh->nlmsg_flags);
1086                 goto out;
1087         case DCB_CMD_PFC_SSTATE:
1088                 ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
1089                                         nlh->nlmsg_flags);
1090                 goto out;
1091         case DCB_CMD_BCN_SCFG:
1092                 ret = dcbnl_bcn_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1093                                        nlh->nlmsg_flags);
1094                 goto out;
1095         default:
1096                 goto errout;
1097         }
1098 errout:
1099         ret = -EINVAL;
1100 out:
1101         dev_put(netdev);
1102         return ret;
1103 }
1104
1105 static int __init dcbnl_init(void)
1106 {
1107         rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL);
1108         rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL);
1109
1110         return 0;
1111 }
1112 module_init(dcbnl_init);
1113
1114 static void __exit dcbnl_exit(void)
1115 {
1116         rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
1117         rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
1118 }
1119 module_exit(dcbnl_exit);
1120
1121