DCB: Add interface to query the state of PFC feature.
[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 generic 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_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 };
67
68 /* DCB priority flow control to User Priority nested attributes */
69 static struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
70         [DCB_PFC_UP_ATTR_0]   = {.type = NLA_U8},
71         [DCB_PFC_UP_ATTR_1]   = {.type = NLA_U8},
72         [DCB_PFC_UP_ATTR_2]   = {.type = NLA_U8},
73         [DCB_PFC_UP_ATTR_3]   = {.type = NLA_U8},
74         [DCB_PFC_UP_ATTR_4]   = {.type = NLA_U8},
75         [DCB_PFC_UP_ATTR_5]   = {.type = NLA_U8},
76         [DCB_PFC_UP_ATTR_6]   = {.type = NLA_U8},
77         [DCB_PFC_UP_ATTR_7]   = {.type = NLA_U8},
78         [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
79 };
80
81 /* DCB priority grouping nested attributes */
82 static struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
83         [DCB_PG_ATTR_TC_0]      = {.type = NLA_NESTED},
84         [DCB_PG_ATTR_TC_1]      = {.type = NLA_NESTED},
85         [DCB_PG_ATTR_TC_2]      = {.type = NLA_NESTED},
86         [DCB_PG_ATTR_TC_3]      = {.type = NLA_NESTED},
87         [DCB_PG_ATTR_TC_4]      = {.type = NLA_NESTED},
88         [DCB_PG_ATTR_TC_5]      = {.type = NLA_NESTED},
89         [DCB_PG_ATTR_TC_6]      = {.type = NLA_NESTED},
90         [DCB_PG_ATTR_TC_7]      = {.type = NLA_NESTED},
91         [DCB_PG_ATTR_TC_ALL]    = {.type = NLA_NESTED},
92         [DCB_PG_ATTR_BW_ID_0]   = {.type = NLA_U8},
93         [DCB_PG_ATTR_BW_ID_1]   = {.type = NLA_U8},
94         [DCB_PG_ATTR_BW_ID_2]   = {.type = NLA_U8},
95         [DCB_PG_ATTR_BW_ID_3]   = {.type = NLA_U8},
96         [DCB_PG_ATTR_BW_ID_4]   = {.type = NLA_U8},
97         [DCB_PG_ATTR_BW_ID_5]   = {.type = NLA_U8},
98         [DCB_PG_ATTR_BW_ID_6]   = {.type = NLA_U8},
99         [DCB_PG_ATTR_BW_ID_7]   = {.type = NLA_U8},
100         [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
101 };
102
103 /* DCB traffic class nested attributes. */
104 static struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
105         [DCB_TC_ATTR_PARAM_PGID]            = {.type = NLA_U8},
106         [DCB_TC_ATTR_PARAM_UP_MAPPING]      = {.type = NLA_U8},
107         [DCB_TC_ATTR_PARAM_STRICT_PRIO]     = {.type = NLA_U8},
108         [DCB_TC_ATTR_PARAM_BW_PCT]          = {.type = NLA_U8},
109         [DCB_TC_ATTR_PARAM_ALL]             = {.type = NLA_FLAG},
110 };
111
112 /* DCB capabilities nested attributes. */
113 static struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
114         [DCB_CAP_ATTR_ALL]     = {.type = NLA_FLAG},
115         [DCB_CAP_ATTR_PG]      = {.type = NLA_U8},
116         [DCB_CAP_ATTR_PFC]     = {.type = NLA_U8},
117         [DCB_CAP_ATTR_UP2TC]   = {.type = NLA_U8},
118         [DCB_CAP_ATTR_PG_TCS]  = {.type = NLA_U8},
119         [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
120         [DCB_CAP_ATTR_GSP]     = {.type = NLA_U8},
121         [DCB_CAP_ATTR_BCN]     = {.type = NLA_U8},
122 };
123
124 /* DCB capabilities nested attributes. */
125 static struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
126         [DCB_NUMTCS_ATTR_ALL]     = {.type = NLA_FLAG},
127         [DCB_NUMTCS_ATTR_PG]      = {.type = NLA_U8},
128         [DCB_NUMTCS_ATTR_PFC]     = {.type = NLA_U8},
129 };
130
131 /* standard netlink reply call */
132 static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,
133                        u32 seq, u16 flags)
134 {
135         struct sk_buff *dcbnl_skb;
136         struct dcbmsg *dcb;
137         struct nlmsghdr *nlh;
138         int ret = -EINVAL;
139
140         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
141         if (!dcbnl_skb)
142                 return ret;
143
144         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);
145
146         dcb = NLMSG_DATA(nlh);
147         dcb->dcb_family = AF_UNSPEC;
148         dcb->cmd = cmd;
149         dcb->dcb_pad = 0;
150
151         ret = nla_put_u8(dcbnl_skb, attr, value);
152         if (ret)
153                 goto err;
154
155         /* end the message, assign the nlmsg_len. */
156         nlmsg_end(dcbnl_skb, nlh);
157         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
158         if (ret)
159                 goto err;
160
161         return 0;
162 nlmsg_failure:
163 err:
164         kfree(dcbnl_skb);
165         return ret;
166 }
167
168 static int dcbnl_getstate(struct net_device *netdev, struct nlattr **tb,
169                           u32 pid, u32 seq, u16 flags)
170 {
171         int ret = -EINVAL;
172
173         /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
174         if (!netdev->dcbnl_ops->getstate)
175                 return ret;
176
177         ret = dcbnl_reply(netdev->dcbnl_ops->getstate(netdev), RTM_GETDCB,
178                           DCB_CMD_GSTATE, DCB_ATTR_STATE, pid, seq, flags);
179
180         return ret;
181 }
182
183 static int dcbnl_getpfccfg(struct net_device *netdev, struct nlattr **tb,
184                            u32 pid, u32 seq, u16 flags)
185 {
186         struct sk_buff *dcbnl_skb;
187         struct nlmsghdr *nlh;
188         struct dcbmsg *dcb;
189         struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
190         u8 value;
191         int ret = -EINVAL;
192         int i;
193         int getall = 0;
194
195         if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
196                 return ret;
197
198         ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
199                                tb[DCB_ATTR_PFC_CFG],
200                                dcbnl_pfc_up_nest);
201         if (ret)
202                 goto err_out;
203
204         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
205         if (!dcbnl_skb)
206                 goto err_out;
207
208         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
209
210         dcb = NLMSG_DATA(nlh);
211         dcb->dcb_family = AF_UNSPEC;
212         dcb->cmd = DCB_CMD_PFC_GCFG;
213
214         nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PFC_CFG);
215         if (!nest)
216                 goto err;
217
218         if (data[DCB_PFC_UP_ATTR_ALL])
219                 getall = 1;
220
221         for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
222                 if (!getall && !data[i])
223                         continue;
224
225                 netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
226                                              &value);
227                 ret = nla_put_u8(dcbnl_skb, i, value);
228
229                 if (ret) {
230                         nla_nest_cancel(dcbnl_skb, nest);
231                         goto err;
232                 }
233         }
234         nla_nest_end(dcbnl_skb, nest);
235
236         nlmsg_end(dcbnl_skb, nlh);
237
238         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
239         if (ret)
240                 goto err;
241
242         return 0;
243 nlmsg_failure:
244 err:
245         kfree(dcbnl_skb);
246 err_out:
247         return -EINVAL;
248 }
249
250 static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlattr **tb,
251                                 u32 pid, u32 seq, u16 flags)
252 {
253         struct sk_buff *dcbnl_skb;
254         struct nlmsghdr *nlh;
255         struct dcbmsg *dcb;
256         u8 perm_addr[MAX_ADDR_LEN];
257         int ret = -EINVAL;
258
259         if (!netdev->dcbnl_ops->getpermhwaddr)
260                 return ret;
261
262         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
263         if (!dcbnl_skb)
264                 goto err_out;
265
266         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
267
268         dcb = NLMSG_DATA(nlh);
269         dcb->dcb_family = AF_UNSPEC;
270         dcb->cmd = DCB_CMD_GPERM_HWADDR;
271
272         netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
273
274         ret = nla_put(dcbnl_skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr),
275                       perm_addr);
276
277         nlmsg_end(dcbnl_skb, nlh);
278
279         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
280         if (ret)
281                 goto err;
282
283         return 0;
284
285 nlmsg_failure:
286 err:
287         kfree(dcbnl_skb);
288 err_out:
289         return -EINVAL;
290 }
291
292 static int dcbnl_getcap(struct net_device *netdev, struct nlattr **tb,
293                         u32 pid, u32 seq, u16 flags)
294 {
295         struct sk_buff *dcbnl_skb;
296         struct nlmsghdr *nlh;
297         struct dcbmsg *dcb;
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
312         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
313         if (!dcbnl_skb)
314                 goto err_out;
315
316         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
317
318         dcb = NLMSG_DATA(nlh);
319         dcb->dcb_family = AF_UNSPEC;
320         dcb->cmd = DCB_CMD_GCAP;
321
322         nest = nla_nest_start(dcbnl_skb, DCB_ATTR_CAP);
323         if (!nest)
324                 goto err;
325
326         if (data[DCB_CAP_ATTR_ALL])
327                 getall = 1;
328
329         for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
330                 if (!getall && !data[i])
331                         continue;
332
333                 if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
334                         ret = nla_put_u8(dcbnl_skb, i, value);
335
336                         if (ret) {
337                                 nla_nest_cancel(dcbnl_skb, nest);
338                                 goto err;
339                         }
340                 }
341         }
342         nla_nest_end(dcbnl_skb, nest);
343
344         nlmsg_end(dcbnl_skb, nlh);
345
346         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
347         if (ret)
348                 goto err;
349
350         return 0;
351 nlmsg_failure:
352 err:
353         kfree(dcbnl_skb);
354 err_out:
355         return -EINVAL;
356 }
357
358 static int dcbnl_getnumtcs(struct net_device *netdev, struct nlattr **tb,
359                            u32 pid, u32 seq, u16 flags)
360 {
361         struct sk_buff *dcbnl_skb;
362         struct nlmsghdr *nlh;
363         struct dcbmsg *dcb;
364         struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
365         u8 value;
366         int ret = -EINVAL;
367         int i;
368         int getall = 0;
369
370         if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
371                 return ret;
372
373         ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
374                                dcbnl_numtcs_nest);
375         if (ret) {
376                 ret = -EINVAL;
377                 goto err_out;
378         }
379
380         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
381         if (!dcbnl_skb) {
382                 ret = -EINVAL;
383                 goto err_out;
384         }
385
386         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
387
388         dcb = NLMSG_DATA(nlh);
389         dcb->dcb_family = AF_UNSPEC;
390         dcb->cmd = DCB_CMD_GNUMTCS;
391
392         nest = nla_nest_start(dcbnl_skb, DCB_ATTR_NUMTCS);
393         if (!nest) {
394                 ret = -EINVAL;
395                 goto err;
396         }
397
398         if (data[DCB_NUMTCS_ATTR_ALL])
399                 getall = 1;
400
401         for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
402                 if (!getall && !data[i])
403                         continue;
404
405                 ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
406                 if (!ret) {
407                         ret = nla_put_u8(dcbnl_skb, i, value);
408
409                         if (ret) {
410                                 nla_nest_cancel(dcbnl_skb, nest);
411                                 ret = -EINVAL;
412                                 goto err;
413                         }
414                 } else {
415                         goto err;
416                 }
417         }
418         nla_nest_end(dcbnl_skb, nest);
419
420         nlmsg_end(dcbnl_skb, nlh);
421
422         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
423         if (ret) {
424                 ret = -EINVAL;
425                 goto err;
426         }
427
428         return 0;
429 nlmsg_failure:
430 err:
431         kfree(dcbnl_skb);
432 err_out:
433         return ret;
434 }
435
436 static int dcbnl_setnumtcs(struct net_device *netdev, struct nlattr **tb,
437                            u32 pid, u32 seq, u16 flags)
438 {
439         struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
440         int ret = -EINVAL;
441         u8 value;
442         int i;
443
444         if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setstate)
445                 return ret;
446
447         ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
448                                dcbnl_numtcs_nest);
449
450         if (ret) {
451                 ret = -EINVAL;
452                 goto err;
453         }
454
455         for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
456                 if (data[i] == NULL)
457                         continue;
458
459                 value = nla_get_u8(data[i]);
460
461                 ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
462
463                 if (ret)
464                         goto operr;
465         }
466
467 operr:
468         ret = dcbnl_reply(!!ret, RTM_SETDCB, DCB_CMD_SNUMTCS,
469                           DCB_ATTR_NUMTCS, pid, seq, flags);
470
471 err:
472         return ret;
473 }
474
475 static int dcbnl_getpfcstate(struct net_device *netdev, struct nlattr **tb,
476                              u32 pid, u32 seq, u16 flags)
477 {
478         int ret = -EINVAL;
479
480         if (!netdev->dcbnl_ops->getpfcstate)
481                 return ret;
482
483         ret = dcbnl_reply(netdev->dcbnl_ops->getpfcstate(netdev), RTM_GETDCB,
484                           DCB_CMD_PFC_GSTATE, DCB_ATTR_PFC_STATE,
485                           pid, seq, flags);
486
487         return ret;
488 }
489
490 static int dcbnl_setpfcstate(struct net_device *netdev, struct nlattr **tb,
491                              u32 pid, u32 seq, u16 flags)
492 {
493         int ret = -EINVAL;
494         u8 value;
495
496         if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
497                 return ret;
498
499         value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
500
501         netdev->dcbnl_ops->setpfcstate(netdev, value);
502
503         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE,
504                           pid, seq, flags);
505
506         return ret;
507 }
508
509 static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlattr **tb,
510                              u32 pid, u32 seq, u16 flags, int dir)
511 {
512         struct sk_buff *dcbnl_skb;
513         struct nlmsghdr *nlh;
514         struct dcbmsg *dcb;
515         struct nlattr *pg_nest, *param_nest, *data;
516         struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
517         struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
518         u8 prio, pgid, tc_pct, up_map;
519         int ret  = -EINVAL;
520         int getall = 0;
521         int i;
522
523         if (!tb[DCB_ATTR_PG_CFG] ||
524             !netdev->dcbnl_ops->getpgtccfgtx ||
525             !netdev->dcbnl_ops->getpgtccfgrx ||
526             !netdev->dcbnl_ops->getpgbwgcfgtx ||
527             !netdev->dcbnl_ops->getpgbwgcfgrx)
528                 return ret;
529
530         ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
531                                tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
532
533         if (ret)
534                 goto err_out;
535
536         dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
537         if (!dcbnl_skb)
538                 goto err_out;
539
540         nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
541
542         dcb = NLMSG_DATA(nlh);
543         dcb->dcb_family = AF_UNSPEC;
544         dcb->cmd = (dir) ? DCB_CMD_PGRX_GCFG : DCB_CMD_PGTX_GCFG;
545
546         pg_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PG_CFG);
547         if (!pg_nest)
548                 goto err;
549
550         if (pg_tb[DCB_PG_ATTR_TC_ALL])
551                 getall = 1;
552
553         for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
554                 if (!getall && !pg_tb[i])
555                         continue;
556
557                 if (pg_tb[DCB_PG_ATTR_TC_ALL])
558                         data = pg_tb[DCB_PG_ATTR_TC_ALL];
559                 else
560                         data = pg_tb[i];
561                 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
562                                        data, dcbnl_tc_param_nest);
563                 if (ret)
564                         goto err_pg;
565
566                 param_nest = nla_nest_start(dcbnl_skb, i);
567                 if (!param_nest)
568                         goto err_pg;
569
570                 pgid = DCB_ATTR_VALUE_UNDEFINED;
571                 prio = DCB_ATTR_VALUE_UNDEFINED;
572                 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
573                 up_map = DCB_ATTR_VALUE_UNDEFINED;
574
575                 if (dir) {
576                         /* Rx */
577                         netdev->dcbnl_ops->getpgtccfgrx(netdev,
578                                                 i - DCB_PG_ATTR_TC_0, &prio,
579                                                 &pgid, &tc_pct, &up_map);
580                 } else {
581                         /* Tx */
582                         netdev->dcbnl_ops->getpgtccfgtx(netdev,
583                                                 i - DCB_PG_ATTR_TC_0, &prio,
584                                                 &pgid, &tc_pct, &up_map);
585                 }
586
587                 if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
588                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
589                         ret = nla_put_u8(dcbnl_skb,
590                                          DCB_TC_ATTR_PARAM_PGID, pgid);
591                         if (ret)
592                                 goto err_param;
593                 }
594                 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
595                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
596                         ret = nla_put_u8(dcbnl_skb,
597                                          DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
598                         if (ret)
599                                 goto err_param;
600                 }
601                 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
602                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
603                         ret = nla_put_u8(dcbnl_skb,
604                                          DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
605                         if (ret)
606                                 goto err_param;
607                 }
608                 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
609                     param_tb[DCB_TC_ATTR_PARAM_ALL]) {
610                         ret = nla_put_u8(dcbnl_skb, DCB_TC_ATTR_PARAM_BW_PCT,
611                                          tc_pct);
612                         if (ret)
613                                 goto err_param;
614                 }
615                 nla_nest_end(dcbnl_skb, param_nest);
616         }
617
618         if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
619                 getall = 1;
620         else
621                 getall = 0;
622
623         for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
624                 if (!getall && !pg_tb[i])
625                         continue;
626
627                 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
628
629                 if (dir) {
630                         /* Rx */
631                         netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
632                                         i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
633                 } else {
634                         /* Tx */
635                         netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
636                                         i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
637                 }
638                 ret = nla_put_u8(dcbnl_skb, i, tc_pct);
639
640                 if (ret)
641                         goto err_pg;
642         }
643
644         nla_nest_end(dcbnl_skb, pg_nest);
645
646         nlmsg_end(dcbnl_skb, nlh);
647
648         ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
649         if (ret)
650                 goto err;
651
652         return 0;
653
654 err_param:
655         nla_nest_cancel(dcbnl_skb, param_nest);
656 err_pg:
657         nla_nest_cancel(dcbnl_skb, pg_nest);
658 nlmsg_failure:
659 err:
660         kfree(dcbnl_skb);
661 err_out:
662         ret  = -EINVAL;
663         return ret;
664 }
665
666 static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlattr **tb,
667                              u32 pid, u32 seq, u16 flags)
668 {
669         return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 0);
670 }
671
672 static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlattr **tb,
673                              u32 pid, u32 seq, u16 flags)
674 {
675         return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 1);
676 }
677
678 static int dcbnl_setstate(struct net_device *netdev, struct nlattr **tb,
679                           u32 pid, u32 seq, u16 flags)
680 {
681         int ret = -EINVAL;
682         u8 value;
683
684         if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
685                 return ret;
686
687         value = nla_get_u8(tb[DCB_ATTR_STATE]);
688
689         netdev->dcbnl_ops->setstate(netdev, value);
690
691         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
692                           pid, seq, flags);
693
694         return ret;
695 }
696
697 static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
698                            u32 pid, u32 seq, u16 flags)
699 {
700         struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
701         int i;
702         int ret = -EINVAL;
703         u8 value;
704
705         if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
706                 return ret;
707
708         ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
709                                tb[DCB_ATTR_PFC_CFG],
710                                dcbnl_pfc_up_nest);
711         if (ret)
712                 goto err;
713
714         for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
715                 if (data[i] == NULL)
716                         continue;
717                 value = nla_get_u8(data[i]);
718                 netdev->dcbnl_ops->setpfccfg(netdev,
719                         data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
720         }
721
722         ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
723                           pid, seq, flags);
724 err:
725         return ret;
726 }
727
728 static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
729                         u32 pid, u32 seq, u16 flags)
730 {
731         int ret = -EINVAL;
732
733         if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
734                 return ret;
735
736         ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
737                           DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
738
739         return ret;
740 }
741
742 static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
743                              u32 pid, u32 seq, u16 flags, int dir)
744 {
745         struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
746         struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
747         int ret = -EINVAL;
748         int i;
749         u8 pgid;
750         u8 up_map;
751         u8 prio;
752         u8 tc_pct;
753
754         if (!tb[DCB_ATTR_PG_CFG] ||
755             !netdev->dcbnl_ops->setpgtccfgtx ||
756             !netdev->dcbnl_ops->setpgtccfgrx ||
757             !netdev->dcbnl_ops->setpgbwgcfgtx ||
758             !netdev->dcbnl_ops->setpgbwgcfgrx)
759                 return ret;
760
761         ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
762                                tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
763         if (ret)
764                 goto err;
765
766         for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
767                 if (!pg_tb[i])
768                         continue;
769
770                 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
771                                        pg_tb[i], dcbnl_tc_param_nest);
772                 if (ret)
773                         goto err;
774
775                 pgid = DCB_ATTR_VALUE_UNDEFINED;
776                 prio = DCB_ATTR_VALUE_UNDEFINED;
777                 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
778                 up_map = DCB_ATTR_VALUE_UNDEFINED;
779
780                 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
781                         prio =
782                             nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
783
784                 if (param_tb[DCB_TC_ATTR_PARAM_PGID])
785                         pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
786
787                 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
788                         tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
789
790                 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
791                         up_map =
792                              nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
793
794                 /* dir: Tx = 0, Rx = 1 */
795                 if (dir) {
796                         /* Rx */
797                         netdev->dcbnl_ops->setpgtccfgrx(netdev,
798                                 i - DCB_PG_ATTR_TC_0,
799                                 prio, pgid, tc_pct, up_map);
800                 } else {
801                         /* Tx */
802                         netdev->dcbnl_ops->setpgtccfgtx(netdev,
803                                 i - DCB_PG_ATTR_TC_0,
804                                 prio, pgid, tc_pct, up_map);
805                 }
806         }
807
808         for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
809                 if (!pg_tb[i])
810                         continue;
811
812                 tc_pct = nla_get_u8(pg_tb[i]);
813
814                 /* dir: Tx = 0, Rx = 1 */
815                 if (dir) {
816                         /* Rx */
817                         netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
818                                          i - DCB_PG_ATTR_BW_ID_0, tc_pct);
819                 } else {
820                         /* Tx */
821                         netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
822                                          i - DCB_PG_ATTR_BW_ID_0, tc_pct);
823                 }
824         }
825
826         ret = dcbnl_reply(0, RTM_SETDCB,
827                           (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
828                           DCB_ATTR_PG_CFG, pid, seq, flags);
829
830 err:
831         return ret;
832 }
833
834 static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
835                              u32 pid, u32 seq, u16 flags)
836 {
837         return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
838 }
839
840 static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
841                              u32 pid, u32 seq, u16 flags)
842 {
843         return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
844 }
845
846 static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
847 {
848         struct net *net = sock_net(skb->sk);
849         struct net_device *netdev;
850         struct dcbmsg  *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
851         struct nlattr *tb[DCB_ATTR_MAX + 1];
852         u32 pid = skb ? NETLINK_CB(skb).pid : 0;
853         int ret = -EINVAL;
854
855         if (net != &init_net)
856                 return -EINVAL;
857
858         ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
859                           dcbnl_rtnl_policy);
860         if (ret < 0)
861                 return ret;
862
863         if (!tb[DCB_ATTR_IFNAME])
864                 return -EINVAL;
865
866         netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
867         if (!netdev)
868                 return -EINVAL;
869
870         if (!netdev->dcbnl_ops)
871                 goto errout;
872
873         switch (dcb->cmd) {
874         case DCB_CMD_GSTATE:
875                 ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
876                                      nlh->nlmsg_flags);
877                 goto out;
878         case DCB_CMD_PFC_GCFG:
879                 ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
880                                       nlh->nlmsg_flags);
881                 goto out;
882         case DCB_CMD_GPERM_HWADDR:
883                 ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
884                                            nlh->nlmsg_flags);
885                 goto out;
886         case DCB_CMD_PGTX_GCFG:
887                 ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
888                                         nlh->nlmsg_flags);
889                 goto out;
890         case DCB_CMD_PGRX_GCFG:
891                 ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
892                                         nlh->nlmsg_flags);
893                 goto out;
894         case DCB_CMD_SSTATE:
895                 ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
896                                      nlh->nlmsg_flags);
897                 goto out;
898         case DCB_CMD_PFC_SCFG:
899                 ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
900                                       nlh->nlmsg_flags);
901                 goto out;
902
903         case DCB_CMD_SET_ALL:
904                 ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
905                                    nlh->nlmsg_flags);
906                 goto out;
907         case DCB_CMD_PGTX_SCFG:
908                 ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
909                                         nlh->nlmsg_flags);
910                 goto out;
911         case DCB_CMD_PGRX_SCFG:
912                 ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
913                                         nlh->nlmsg_flags);
914                 goto out;
915         case DCB_CMD_GCAP:
916                 ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
917                                    nlh->nlmsg_flags);
918                 goto out;
919         case DCB_CMD_GNUMTCS:
920                 ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
921                                       nlh->nlmsg_flags);
922                 goto out;
923         case DCB_CMD_SNUMTCS:
924                 ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
925                                       nlh->nlmsg_flags);
926                 goto out;
927         case DCB_CMD_PFC_GSTATE:
928                 ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
929                                         nlh->nlmsg_flags);
930                 goto out;
931         case DCB_CMD_PFC_SSTATE:
932                 ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
933                                         nlh->nlmsg_flags);
934                 goto out;
935         default:
936                 goto errout;
937         }
938 errout:
939         ret = -EINVAL;
940 out:
941         dev_put(netdev);
942         return ret;
943 }
944
945 static int __init dcbnl_init(void)
946 {
947         rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL);
948         rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL);
949
950         return 0;
951 }
952 module_init(dcbnl_init);
953
954 static void __exit dcbnl_exit(void)
955 {
956         rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
957         rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
958 }
959 module_exit(dcbnl_exit);
960
961