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