blob: 8f83ad859d9b26fd232d5f9ddb19c48ee3f9758c [file] [log] [blame]
Alexander Duyck2f90b862008-11-20 20:52:10 -08001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080023#include <net/netlink.h>
24#include <net/rtnetlink.h>
25#include <linux/dcbnl.h>
John Fastabend96b99682010-12-30 09:26:37 +000026#include <net/dcbevent.h>
Alexander Duyck2f90b862008-11-20 20:52:10 -080027#include <linux/rtnetlink.h>
28#include <net/sock.h>
29
30/**
31 * Data Center Bridging (DCB) is a collection of Ethernet enhancements
32 * intended to allow network traffic with differing requirements
33 * (highly reliable, no drops vs. best effort vs. low latency) to operate
34 * and co-exist on Ethernet. Current DCB features are:
35 *
36 * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
37 * framework for assigning bandwidth guarantees to traffic classes.
38 *
39 * Priority-based Flow Control (PFC) - provides a flow control mechanism which
40 * can work independently for each 802.1p priority.
41 *
42 * Congestion Notification - provides a mechanism for end-to-end congestion
43 * control for protocols which do not have built-in congestion management.
44 *
45 * More information about the emerging standards for these Ethernet features
46 * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
47 *
48 * This file implements an rtnetlink interface to allow configuration of DCB
49 * features for capable devices.
50 */
51
52MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
Jeff Kirsher7a6b6f52008-11-25 01:02:08 -080053MODULE_DESCRIPTION("Data Center Bridging netlink interface");
Alexander Duyck2f90b862008-11-20 20:52:10 -080054MODULE_LICENSE("GPL");
55
56/**************** DCB attribute policies *************************************/
57
58/* DCB netlink attributes policy */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000059static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
Alexander Duyck859ee3c2008-11-20 21:10:23 -080060 [DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
61 [DCB_ATTR_STATE] = {.type = NLA_U8},
62 [DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},
63 [DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},
64 [DCB_ATTR_SET_ALL] = {.type = NLA_U8},
Alexander Duyck2f90b862008-11-20 20:52:10 -080065 [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
Alexander Duyck859ee3c2008-11-20 21:10:23 -080066 [DCB_ATTR_CAP] = {.type = NLA_NESTED},
67 [DCB_ATTR_PFC_STATE] = {.type = NLA_U8},
68 [DCB_ATTR_BCN] = {.type = NLA_NESTED},
Yi Zou6fa382a2009-08-31 12:33:20 +000069 [DCB_ATTR_APP] = {.type = NLA_NESTED},
John Fastabend3e290272010-12-30 09:25:46 +000070 [DCB_ATTR_IEEE] = {.type = NLA_NESTED},
Shmulik Ravid6241b622010-12-30 06:26:48 +000071 [DCB_ATTR_DCBX] = {.type = NLA_U8},
Alexander Duyck2f90b862008-11-20 20:52:10 -080072};
73
74/* DCB priority flow control to User Priority nested attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000075static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -080076 [DCB_PFC_UP_ATTR_0] = {.type = NLA_U8},
77 [DCB_PFC_UP_ATTR_1] = {.type = NLA_U8},
78 [DCB_PFC_UP_ATTR_2] = {.type = NLA_U8},
79 [DCB_PFC_UP_ATTR_3] = {.type = NLA_U8},
80 [DCB_PFC_UP_ATTR_4] = {.type = NLA_U8},
81 [DCB_PFC_UP_ATTR_5] = {.type = NLA_U8},
82 [DCB_PFC_UP_ATTR_6] = {.type = NLA_U8},
83 [DCB_PFC_UP_ATTR_7] = {.type = NLA_U8},
84 [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
85};
86
87/* DCB priority grouping nested attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000088static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -080089 [DCB_PG_ATTR_TC_0] = {.type = NLA_NESTED},
90 [DCB_PG_ATTR_TC_1] = {.type = NLA_NESTED},
91 [DCB_PG_ATTR_TC_2] = {.type = NLA_NESTED},
92 [DCB_PG_ATTR_TC_3] = {.type = NLA_NESTED},
93 [DCB_PG_ATTR_TC_4] = {.type = NLA_NESTED},
94 [DCB_PG_ATTR_TC_5] = {.type = NLA_NESTED},
95 [DCB_PG_ATTR_TC_6] = {.type = NLA_NESTED},
96 [DCB_PG_ATTR_TC_7] = {.type = NLA_NESTED},
97 [DCB_PG_ATTR_TC_ALL] = {.type = NLA_NESTED},
98 [DCB_PG_ATTR_BW_ID_0] = {.type = NLA_U8},
99 [DCB_PG_ATTR_BW_ID_1] = {.type = NLA_U8},
100 [DCB_PG_ATTR_BW_ID_2] = {.type = NLA_U8},
101 [DCB_PG_ATTR_BW_ID_3] = {.type = NLA_U8},
102 [DCB_PG_ATTR_BW_ID_4] = {.type = NLA_U8},
103 [DCB_PG_ATTR_BW_ID_5] = {.type = NLA_U8},
104 [DCB_PG_ATTR_BW_ID_6] = {.type = NLA_U8},
105 [DCB_PG_ATTR_BW_ID_7] = {.type = NLA_U8},
106 [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
107};
108
109/* DCB traffic class nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000110static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
Alexander Duyck2f90b862008-11-20 20:52:10 -0800111 [DCB_TC_ATTR_PARAM_PGID] = {.type = NLA_U8},
112 [DCB_TC_ATTR_PARAM_UP_MAPPING] = {.type = NLA_U8},
113 [DCB_TC_ATTR_PARAM_STRICT_PRIO] = {.type = NLA_U8},
114 [DCB_TC_ATTR_PARAM_BW_PCT] = {.type = NLA_U8},
115 [DCB_TC_ATTR_PARAM_ALL] = {.type = NLA_FLAG},
116};
117
Alexander Duyck46132182008-11-20 21:05:08 -0800118/* DCB capabilities nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000119static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
Alexander Duyck46132182008-11-20 21:05:08 -0800120 [DCB_CAP_ATTR_ALL] = {.type = NLA_FLAG},
121 [DCB_CAP_ATTR_PG] = {.type = NLA_U8},
122 [DCB_CAP_ATTR_PFC] = {.type = NLA_U8},
123 [DCB_CAP_ATTR_UP2TC] = {.type = NLA_U8},
124 [DCB_CAP_ATTR_PG_TCS] = {.type = NLA_U8},
125 [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
126 [DCB_CAP_ATTR_GSP] = {.type = NLA_U8},
127 [DCB_CAP_ATTR_BCN] = {.type = NLA_U8},
Shmulik Ravid6241b622010-12-30 06:26:48 +0000128 [DCB_CAP_ATTR_DCBX] = {.type = NLA_U8},
Alexander Duyck46132182008-11-20 21:05:08 -0800129};
Alexander Duyck2f90b862008-11-20 20:52:10 -0800130
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800131/* DCB capabilities nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000132static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800133 [DCB_NUMTCS_ATTR_ALL] = {.type = NLA_FLAG},
134 [DCB_NUMTCS_ATTR_PG] = {.type = NLA_U8},
135 [DCB_NUMTCS_ATTR_PFC] = {.type = NLA_U8},
136};
137
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800138/* DCB BCN nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000139static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800140 [DCB_BCN_ATTR_RP_0] = {.type = NLA_U8},
141 [DCB_BCN_ATTR_RP_1] = {.type = NLA_U8},
142 [DCB_BCN_ATTR_RP_2] = {.type = NLA_U8},
143 [DCB_BCN_ATTR_RP_3] = {.type = NLA_U8},
144 [DCB_BCN_ATTR_RP_4] = {.type = NLA_U8},
145 [DCB_BCN_ATTR_RP_5] = {.type = NLA_U8},
146 [DCB_BCN_ATTR_RP_6] = {.type = NLA_U8},
147 [DCB_BCN_ATTR_RP_7] = {.type = NLA_U8},
148 [DCB_BCN_ATTR_RP_ALL] = {.type = NLA_FLAG},
Don Skidmoref4314e82008-12-21 20:10:29 -0800149 [DCB_BCN_ATTR_BCNA_0] = {.type = NLA_U32},
150 [DCB_BCN_ATTR_BCNA_1] = {.type = NLA_U32},
Alexander Duyck859ee3c2008-11-20 21:10:23 -0800151 [DCB_BCN_ATTR_ALPHA] = {.type = NLA_U32},
152 [DCB_BCN_ATTR_BETA] = {.type = NLA_U32},
153 [DCB_BCN_ATTR_GD] = {.type = NLA_U32},
154 [DCB_BCN_ATTR_GI] = {.type = NLA_U32},
155 [DCB_BCN_ATTR_TMAX] = {.type = NLA_U32},
156 [DCB_BCN_ATTR_TD] = {.type = NLA_U32},
157 [DCB_BCN_ATTR_RMIN] = {.type = NLA_U32},
158 [DCB_BCN_ATTR_W] = {.type = NLA_U32},
159 [DCB_BCN_ATTR_RD] = {.type = NLA_U32},
160 [DCB_BCN_ATTR_RU] = {.type = NLA_U32},
161 [DCB_BCN_ATTR_WRTT] = {.type = NLA_U32},
162 [DCB_BCN_ATTR_RI] = {.type = NLA_U32},
163 [DCB_BCN_ATTR_C] = {.type = NLA_U32},
164 [DCB_BCN_ATTR_ALL] = {.type = NLA_FLAG},
165};
166
Yi Zou6fa382a2009-08-31 12:33:20 +0000167/* DCB APP nested attributes. */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000168static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
Yi Zou6fa382a2009-08-31 12:33:20 +0000169 [DCB_APP_ATTR_IDTYPE] = {.type = NLA_U8},
170 [DCB_APP_ATTR_ID] = {.type = NLA_U16},
171 [DCB_APP_ATTR_PRIORITY] = {.type = NLA_U8},
172};
173
John Fastabend3e290272010-12-30 09:25:46 +0000174/* IEEE 802.1Qaz nested attributes. */
175static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
176 [DCB_ATTR_IEEE_ETS] = {.len = sizeof(struct ieee_ets)},
177 [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
178 [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
179};
180
181static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
182 [DCB_ATTR_IEEE_APP] = {.len = sizeof(struct dcb_app)},
183};
184
John Fastabend9ab933a2010-12-30 09:26:31 +0000185static LIST_HEAD(dcb_app_list);
186static DEFINE_SPINLOCK(dcb_lock);
187
Alexander Duyck2f90b862008-11-20 20:52:10 -0800188/* standard netlink reply call */
189static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,
190 u32 seq, u16 flags)
191{
192 struct sk_buff *dcbnl_skb;
193 struct dcbmsg *dcb;
194 struct nlmsghdr *nlh;
195 int ret = -EINVAL;
196
197 dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
198 if (!dcbnl_skb)
199 return ret;
200
201 nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);
202
203 dcb = NLMSG_DATA(nlh);
204 dcb->dcb_family = AF_UNSPEC;
205 dcb->cmd = cmd;
206 dcb->dcb_pad = 0;
207
208 ret = nla_put_u8(dcbnl_skb, attr, value);
209 if (ret)
210 goto err;
211
212 /* end the message, assign the nlmsg_len. */
213 nlmsg_end(dcbnl_skb, nlh);
214 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
215 if (ret)
John Fastabend7eaf5072009-09-25 13:12:03 +0000216 return -EINVAL;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800217
218 return 0;
219nlmsg_failure:
220err:
Roel Kluin858eb712009-01-04 17:29:21 -0800221 kfree_skb(dcbnl_skb);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800222 return ret;
223}
224
225static int dcbnl_getstate(struct net_device *netdev, struct nlattr **tb,
226 u32 pid, u32 seq, u16 flags)
227{
228 int ret = -EINVAL;
229
230 /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
231 if (!netdev->dcbnl_ops->getstate)
232 return ret;
233
234 ret = dcbnl_reply(netdev->dcbnl_ops->getstate(netdev), RTM_GETDCB,
235 DCB_CMD_GSTATE, DCB_ATTR_STATE, pid, seq, flags);
236
237 return ret;
238}
239
240static int dcbnl_getpfccfg(struct net_device *netdev, struct nlattr **tb,
241 u32 pid, u32 seq, u16 flags)
242{
243 struct sk_buff *dcbnl_skb;
244 struct nlmsghdr *nlh;
245 struct dcbmsg *dcb;
246 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
247 u8 value;
248 int ret = -EINVAL;
249 int i;
250 int getall = 0;
251
252 if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
253 return ret;
254
255 ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
256 tb[DCB_ATTR_PFC_CFG],
257 dcbnl_pfc_up_nest);
258 if (ret)
259 goto err_out;
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_PFC_GCFG;
270
271 nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PFC_CFG);
272 if (!nest)
273 goto err;
274
275 if (data[DCB_PFC_UP_ATTR_ALL])
276 getall = 1;
277
278 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
279 if (!getall && !data[i])
280 continue;
281
282 netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
283 &value);
284 ret = nla_put_u8(dcbnl_skb, i, value);
285
286 if (ret) {
287 nla_nest_cancel(dcbnl_skb, nest);
288 goto err;
289 }
290 }
291 nla_nest_end(dcbnl_skb, nest);
292
293 nlmsg_end(dcbnl_skb, nlh);
294
295 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
296 if (ret)
John Fastabend7eaf5072009-09-25 13:12:03 +0000297 goto err_out;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800298
299 return 0;
300nlmsg_failure:
301err:
Roel Kluin858eb712009-01-04 17:29:21 -0800302 kfree_skb(dcbnl_skb);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800303err_out:
304 return -EINVAL;
305}
306
307static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlattr **tb,
308 u32 pid, u32 seq, u16 flags)
309{
310 struct sk_buff *dcbnl_skb;
311 struct nlmsghdr *nlh;
312 struct dcbmsg *dcb;
313 u8 perm_addr[MAX_ADDR_LEN];
314 int ret = -EINVAL;
315
316 if (!netdev->dcbnl_ops->getpermhwaddr)
317 return ret;
318
319 dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
320 if (!dcbnl_skb)
321 goto err_out;
322
323 nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
324
325 dcb = NLMSG_DATA(nlh);
326 dcb->dcb_family = AF_UNSPEC;
327 dcb->cmd = DCB_CMD_GPERM_HWADDR;
328
329 netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
330
331 ret = nla_put(dcbnl_skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr),
332 perm_addr);
333
334 nlmsg_end(dcbnl_skb, nlh);
335
336 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
337 if (ret)
John Fastabend7eaf5072009-09-25 13:12:03 +0000338 goto err_out;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800339
340 return 0;
341
342nlmsg_failure:
Roel Kluin858eb712009-01-04 17:29:21 -0800343 kfree_skb(dcbnl_skb);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800344err_out:
345 return -EINVAL;
346}
347
Alexander Duyck46132182008-11-20 21:05:08 -0800348static int dcbnl_getcap(struct net_device *netdev, struct nlattr **tb,
349 u32 pid, u32 seq, u16 flags)
350{
351 struct sk_buff *dcbnl_skb;
352 struct nlmsghdr *nlh;
353 struct dcbmsg *dcb;
354 struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
355 u8 value;
356 int ret = -EINVAL;
357 int i;
358 int getall = 0;
359
360 if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
361 return ret;
362
363 ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
364 dcbnl_cap_nest);
365 if (ret)
366 goto err_out;
367
368 dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
369 if (!dcbnl_skb)
370 goto err_out;
371
372 nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
373
374 dcb = NLMSG_DATA(nlh);
375 dcb->dcb_family = AF_UNSPEC;
376 dcb->cmd = DCB_CMD_GCAP;
377
378 nest = nla_nest_start(dcbnl_skb, DCB_ATTR_CAP);
379 if (!nest)
380 goto err;
381
382 if (data[DCB_CAP_ATTR_ALL])
383 getall = 1;
384
385 for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
386 if (!getall && !data[i])
387 continue;
388
389 if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
390 ret = nla_put_u8(dcbnl_skb, i, value);
391
392 if (ret) {
393 nla_nest_cancel(dcbnl_skb, nest);
394 goto err;
395 }
396 }
397 }
398 nla_nest_end(dcbnl_skb, nest);
399
400 nlmsg_end(dcbnl_skb, nlh);
401
402 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
403 if (ret)
John Fastabend7eaf5072009-09-25 13:12:03 +0000404 goto err_out;
Alexander Duyck46132182008-11-20 21:05:08 -0800405
406 return 0;
407nlmsg_failure:
408err:
Roel Kluin858eb712009-01-04 17:29:21 -0800409 kfree_skb(dcbnl_skb);
Alexander Duyck46132182008-11-20 21:05:08 -0800410err_out:
411 return -EINVAL;
412}
413
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800414static int dcbnl_getnumtcs(struct net_device *netdev, struct nlattr **tb,
415 u32 pid, u32 seq, u16 flags)
416{
417 struct sk_buff *dcbnl_skb;
418 struct nlmsghdr *nlh;
419 struct dcbmsg *dcb;
420 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
421 u8 value;
422 int ret = -EINVAL;
423 int i;
424 int getall = 0;
425
426 if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
427 return ret;
428
429 ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
430 dcbnl_numtcs_nest);
431 if (ret) {
432 ret = -EINVAL;
433 goto err_out;
434 }
435
436 dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
437 if (!dcbnl_skb) {
438 ret = -EINVAL;
439 goto err_out;
440 }
441
442 nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
443
444 dcb = NLMSG_DATA(nlh);
445 dcb->dcb_family = AF_UNSPEC;
446 dcb->cmd = DCB_CMD_GNUMTCS;
447
448 nest = nla_nest_start(dcbnl_skb, DCB_ATTR_NUMTCS);
449 if (!nest) {
450 ret = -EINVAL;
451 goto err;
452 }
453
454 if (data[DCB_NUMTCS_ATTR_ALL])
455 getall = 1;
456
457 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
458 if (!getall && !data[i])
459 continue;
460
461 ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
462 if (!ret) {
463 ret = nla_put_u8(dcbnl_skb, i, value);
464
465 if (ret) {
466 nla_nest_cancel(dcbnl_skb, nest);
467 ret = -EINVAL;
468 goto err;
469 }
470 } else {
471 goto err;
472 }
473 }
474 nla_nest_end(dcbnl_skb, nest);
475
476 nlmsg_end(dcbnl_skb, nlh);
477
478 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
479 if (ret) {
480 ret = -EINVAL;
John Fastabend7eaf5072009-09-25 13:12:03 +0000481 goto err_out;
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800482 }
483
484 return 0;
485nlmsg_failure:
486err:
Roel Kluin858eb712009-01-04 17:29:21 -0800487 kfree_skb(dcbnl_skb);
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800488err_out:
489 return ret;
490}
491
492static int dcbnl_setnumtcs(struct net_device *netdev, struct nlattr **tb,
493 u32 pid, u32 seq, u16 flags)
494{
495 struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
496 int ret = -EINVAL;
497 u8 value;
498 int i;
499
Don Skidmore8b124a82008-12-15 01:06:23 -0800500 if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
Alexander Duyck33dbabc2008-11-20 21:08:19 -0800501 return ret;
502
503 ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
504 dcbnl_numtcs_nest);
505
506 if (ret) {
507 ret = -EINVAL;
508 goto err;
509 }
510
511 for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
512 if (data[i] == NULL)
513 continue;
514
515 value = nla_get_u8(data[i]);
516
517 ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
518
519 if (ret)
520 goto operr;
521 }
522
523operr:
524 ret = dcbnl_reply(!!ret, RTM_SETDCB, DCB_CMD_SNUMTCS,
525 DCB_ATTR_NUMTCS, pid, seq, flags);
526
527err:
528 return ret;
529}
530
Alexander Duyck0eb3aa92008-11-20 21:09:23 -0800531static int dcbnl_getpfcstate(struct net_device *netdev, struct nlattr **tb,
532 u32 pid, u32 seq, u16 flags)
533{
534 int ret = -EINVAL;
535
536 if (!netdev->dcbnl_ops->getpfcstate)
537 return ret;
538
539 ret = dcbnl_reply(netdev->dcbnl_ops->getpfcstate(netdev), RTM_GETDCB,
540 DCB_CMD_PFC_GSTATE, DCB_ATTR_PFC_STATE,
541 pid, seq, flags);
542
543 return ret;
544}
545
546static int dcbnl_setpfcstate(struct net_device *netdev, struct nlattr **tb,
547 u32 pid, u32 seq, u16 flags)
548{
549 int ret = -EINVAL;
550 u8 value;
551
552 if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
553 return ret;
554
555 value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
556
557 netdev->dcbnl_ops->setpfcstate(netdev, value);
558
559 ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE,
560 pid, seq, flags);
561
562 return ret;
563}
564
Yi Zou57949682009-08-31 12:33:40 +0000565static int dcbnl_getapp(struct net_device *netdev, struct nlattr **tb,
566 u32 pid, u32 seq, u16 flags)
567{
568 struct sk_buff *dcbnl_skb;
569 struct nlmsghdr *nlh;
570 struct dcbmsg *dcb;
571 struct nlattr *app_nest;
572 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
573 u16 id;
574 u8 up, idtype;
575 int ret = -EINVAL;
576
577 if (!tb[DCB_ATTR_APP] || !netdev->dcbnl_ops->getapp)
578 goto out;
579
580 ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
581 dcbnl_app_nest);
582 if (ret)
583 goto out;
584
585 ret = -EINVAL;
586 /* all must be non-null */
587 if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
588 (!app_tb[DCB_APP_ATTR_ID]))
589 goto out;
590
591 /* either by eth type or by socket number */
592 idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
593 if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
594 (idtype != DCB_APP_IDTYPE_PORTNUM))
595 goto out;
596
597 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
598 up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
599
600 /* send this back */
601 dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
602 if (!dcbnl_skb)
603 goto out;
604
605 nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
606 dcb = NLMSG_DATA(nlh);
607 dcb->dcb_family = AF_UNSPEC;
608 dcb->cmd = DCB_CMD_GAPP;
609
610 app_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_APP);
611 ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_IDTYPE, idtype);
612 if (ret)
613 goto out_cancel;
614
615 ret = nla_put_u16(dcbnl_skb, DCB_APP_ATTR_ID, id);
616 if (ret)
617 goto out_cancel;
618
619 ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_PRIORITY, up);
620 if (ret)
621 goto out_cancel;
622
623 nla_nest_end(dcbnl_skb, app_nest);
624 nlmsg_end(dcbnl_skb, nlh);
625
626 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
627 if (ret)
628 goto nlmsg_failure;
629
630 goto out;
631
632out_cancel:
633 nla_nest_cancel(dcbnl_skb, app_nest);
634nlmsg_failure:
635 kfree_skb(dcbnl_skb);
636out:
637 return ret;
638}
639
640static int dcbnl_setapp(struct net_device *netdev, struct nlattr **tb,
641 u32 pid, u32 seq, u16 flags)
642{
John Fastabend9ab933a2010-12-30 09:26:31 +0000643 int err, ret = -EINVAL;
Yi Zou57949682009-08-31 12:33:40 +0000644 u16 id;
645 u8 up, idtype;
646 struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
647
John Fastabend9ab933a2010-12-30 09:26:31 +0000648 if (!tb[DCB_ATTR_APP])
Yi Zou57949682009-08-31 12:33:40 +0000649 goto out;
650
651 ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
652 dcbnl_app_nest);
653 if (ret)
654 goto out;
655
656 ret = -EINVAL;
657 /* all must be non-null */
658 if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
659 (!app_tb[DCB_APP_ATTR_ID]) ||
660 (!app_tb[DCB_APP_ATTR_PRIORITY]))
661 goto out;
662
663 /* either by eth type or by socket number */
664 idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
665 if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
666 (idtype != DCB_APP_IDTYPE_PORTNUM))
667 goto out;
668
669 id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
670 up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
671
John Fastabend9ab933a2010-12-30 09:26:31 +0000672 if (netdev->dcbnl_ops->setapp) {
673 err = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
674 } else {
675 struct dcb_app app;
676 app.selector = idtype;
677 app.protocol = id;
678 app.priority = up;
679 err = dcb_setapp(netdev, &app);
680 }
681
682 ret = dcbnl_reply(err, RTM_SETDCB, DCB_CMD_SAPP, DCB_ATTR_APP,
683 pid, seq, flags);
Yi Zou57949682009-08-31 12:33:40 +0000684out:
685 return ret;
686}
687
Alexander Duyck2f90b862008-11-20 20:52:10 -0800688static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlattr **tb,
689 u32 pid, u32 seq, u16 flags, int dir)
690{
691 struct sk_buff *dcbnl_skb;
692 struct nlmsghdr *nlh;
693 struct dcbmsg *dcb;
694 struct nlattr *pg_nest, *param_nest, *data;
695 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
696 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
697 u8 prio, pgid, tc_pct, up_map;
698 int ret = -EINVAL;
699 int getall = 0;
700 int i;
701
702 if (!tb[DCB_ATTR_PG_CFG] ||
703 !netdev->dcbnl_ops->getpgtccfgtx ||
704 !netdev->dcbnl_ops->getpgtccfgrx ||
705 !netdev->dcbnl_ops->getpgbwgcfgtx ||
706 !netdev->dcbnl_ops->getpgbwgcfgrx)
707 return ret;
708
709 ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
710 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
711
712 if (ret)
713 goto err_out;
714
715 dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
716 if (!dcbnl_skb)
717 goto err_out;
718
719 nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
720
721 dcb = NLMSG_DATA(nlh);
722 dcb->dcb_family = AF_UNSPEC;
723 dcb->cmd = (dir) ? DCB_CMD_PGRX_GCFG : DCB_CMD_PGTX_GCFG;
724
725 pg_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PG_CFG);
726 if (!pg_nest)
727 goto err;
728
729 if (pg_tb[DCB_PG_ATTR_TC_ALL])
730 getall = 1;
731
732 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
733 if (!getall && !pg_tb[i])
734 continue;
735
736 if (pg_tb[DCB_PG_ATTR_TC_ALL])
737 data = pg_tb[DCB_PG_ATTR_TC_ALL];
738 else
739 data = pg_tb[i];
740 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
741 data, dcbnl_tc_param_nest);
742 if (ret)
743 goto err_pg;
744
745 param_nest = nla_nest_start(dcbnl_skb, i);
746 if (!param_nest)
747 goto err_pg;
748
749 pgid = DCB_ATTR_VALUE_UNDEFINED;
750 prio = DCB_ATTR_VALUE_UNDEFINED;
751 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
752 up_map = DCB_ATTR_VALUE_UNDEFINED;
753
754 if (dir) {
755 /* Rx */
756 netdev->dcbnl_ops->getpgtccfgrx(netdev,
757 i - DCB_PG_ATTR_TC_0, &prio,
758 &pgid, &tc_pct, &up_map);
759 } else {
760 /* Tx */
761 netdev->dcbnl_ops->getpgtccfgtx(netdev,
762 i - DCB_PG_ATTR_TC_0, &prio,
763 &pgid, &tc_pct, &up_map);
764 }
765
766 if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
767 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
768 ret = nla_put_u8(dcbnl_skb,
769 DCB_TC_ATTR_PARAM_PGID, pgid);
770 if (ret)
771 goto err_param;
772 }
773 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
774 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
775 ret = nla_put_u8(dcbnl_skb,
776 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
777 if (ret)
778 goto err_param;
779 }
780 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
781 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
782 ret = nla_put_u8(dcbnl_skb,
783 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
784 if (ret)
785 goto err_param;
786 }
787 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
788 param_tb[DCB_TC_ATTR_PARAM_ALL]) {
789 ret = nla_put_u8(dcbnl_skb, DCB_TC_ATTR_PARAM_BW_PCT,
790 tc_pct);
791 if (ret)
792 goto err_param;
793 }
794 nla_nest_end(dcbnl_skb, param_nest);
795 }
796
797 if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
798 getall = 1;
799 else
800 getall = 0;
801
802 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
803 if (!getall && !pg_tb[i])
804 continue;
805
806 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
807
808 if (dir) {
809 /* Rx */
810 netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
811 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
812 } else {
813 /* Tx */
814 netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
815 i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
816 }
817 ret = nla_put_u8(dcbnl_skb, i, tc_pct);
818
819 if (ret)
820 goto err_pg;
821 }
822
823 nla_nest_end(dcbnl_skb, pg_nest);
824
825 nlmsg_end(dcbnl_skb, nlh);
826
827 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
828 if (ret)
John Fastabend7eaf5072009-09-25 13:12:03 +0000829 goto err_out;
Alexander Duyck2f90b862008-11-20 20:52:10 -0800830
831 return 0;
832
833err_param:
834 nla_nest_cancel(dcbnl_skb, param_nest);
835err_pg:
836 nla_nest_cancel(dcbnl_skb, pg_nest);
837nlmsg_failure:
838err:
Roel Kluin858eb712009-01-04 17:29:21 -0800839 kfree_skb(dcbnl_skb);
Alexander Duyck2f90b862008-11-20 20:52:10 -0800840err_out:
841 ret = -EINVAL;
842 return ret;
843}
844
845static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlattr **tb,
846 u32 pid, u32 seq, u16 flags)
847{
848 return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 0);
849}
850
851static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlattr **tb,
852 u32 pid, u32 seq, u16 flags)
853{
854 return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 1);
855}
856
857static int dcbnl_setstate(struct net_device *netdev, struct nlattr **tb,
858 u32 pid, u32 seq, u16 flags)
859{
860 int ret = -EINVAL;
861 u8 value;
862
863 if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
864 return ret;
865
866 value = nla_get_u8(tb[DCB_ATTR_STATE]);
867
Don Skidmore1486a612008-12-21 20:09:50 -0800868 ret = dcbnl_reply(netdev->dcbnl_ops->setstate(netdev, value),
869 RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
Alexander Duyck2f90b862008-11-20 20:52:10 -0800870 pid, seq, flags);
871
872 return ret;
873}
874
875static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
876 u32 pid, u32 seq, u16 flags)
877{
878 struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
879 int i;
880 int ret = -EINVAL;
881 u8 value;
882
883 if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
884 return ret;
885
886 ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
887 tb[DCB_ATTR_PFC_CFG],
888 dcbnl_pfc_up_nest);
889 if (ret)
890 goto err;
891
892 for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
893 if (data[i] == NULL)
894 continue;
895 value = nla_get_u8(data[i]);
896 netdev->dcbnl_ops->setpfccfg(netdev,
897 data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
898 }
899
900 ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
901 pid, seq, flags);
902err:
903 return ret;
904}
905
906static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
907 u32 pid, u32 seq, u16 flags)
908{
909 int ret = -EINVAL;
910
911 if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
912 return ret;
913
914 ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
915 DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
916
917 return ret;
918}
919
920static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
921 u32 pid, u32 seq, u16 flags, int dir)
922{
923 struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
924 struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
925 int ret = -EINVAL;
926 int i;
927 u8 pgid;
928 u8 up_map;
929 u8 prio;
930 u8 tc_pct;
931
932 if (!tb[DCB_ATTR_PG_CFG] ||
933 !netdev->dcbnl_ops->setpgtccfgtx ||
934 !netdev->dcbnl_ops->setpgtccfgrx ||
935 !netdev->dcbnl_ops->setpgbwgcfgtx ||
936 !netdev->dcbnl_ops->setpgbwgcfgrx)
937 return ret;
938
939 ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
940 tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
941 if (ret)
942 goto err;
943
944 for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
945 if (!pg_tb[i])
946 continue;
947
948 ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
949 pg_tb[i], dcbnl_tc_param_nest);
950 if (ret)
951 goto err;
952
953 pgid = DCB_ATTR_VALUE_UNDEFINED;
954 prio = DCB_ATTR_VALUE_UNDEFINED;
955 tc_pct = DCB_ATTR_VALUE_UNDEFINED;
956 up_map = DCB_ATTR_VALUE_UNDEFINED;
957
958 if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
959 prio =
960 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
961
962 if (param_tb[DCB_TC_ATTR_PARAM_PGID])
963 pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
964
965 if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
966 tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
967
968 if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
969 up_map =
970 nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
971
972 /* dir: Tx = 0, Rx = 1 */
973 if (dir) {
974 /* Rx */
975 netdev->dcbnl_ops->setpgtccfgrx(netdev,
976 i - DCB_PG_ATTR_TC_0,
977 prio, pgid, tc_pct, up_map);
978 } else {
979 /* Tx */
980 netdev->dcbnl_ops->setpgtccfgtx(netdev,
981 i - DCB_PG_ATTR_TC_0,
982 prio, pgid, tc_pct, up_map);
983 }
984 }
985
986 for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
987 if (!pg_tb[i])
988 continue;
989
990 tc_pct = nla_get_u8(pg_tb[i]);
991
992 /* dir: Tx = 0, Rx = 1 */
993 if (dir) {
994 /* Rx */
995 netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
996 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
997 } else {
998 /* Tx */
999 netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
1000 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
1001 }
1002 }
1003
1004 ret = dcbnl_reply(0, RTM_SETDCB,
1005 (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
1006 DCB_ATTR_PG_CFG, pid, seq, flags);
1007
1008err:
1009 return ret;
1010}
1011
1012static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
1013 u32 pid, u32 seq, u16 flags)
1014{
1015 return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
1016}
1017
1018static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
1019 u32 pid, u32 seq, u16 flags)
1020{
1021 return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
1022}
1023
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001024static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlattr **tb,
1025 u32 pid, u32 seq, u16 flags)
1026{
1027 struct sk_buff *dcbnl_skb;
1028 struct nlmsghdr *nlh;
1029 struct dcbmsg *dcb;
1030 struct nlattr *bcn_nest;
1031 struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
1032 u8 value_byte;
1033 u32 value_integer;
1034 int ret = -EINVAL;
1035 bool getall = false;
1036 int i;
1037
1038 if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
1039 !netdev->dcbnl_ops->getbcncfg)
1040 return ret;
1041
1042 ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
1043 tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
1044
1045 if (ret)
1046 goto err_out;
1047
1048 dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1049 if (!dcbnl_skb)
1050 goto err_out;
1051
1052 nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
1053
1054 dcb = NLMSG_DATA(nlh);
1055 dcb->dcb_family = AF_UNSPEC;
1056 dcb->cmd = DCB_CMD_BCN_GCFG;
1057
1058 bcn_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_BCN);
1059 if (!bcn_nest)
1060 goto err;
1061
1062 if (bcn_tb[DCB_BCN_ATTR_ALL])
1063 getall = true;
1064
1065 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
1066 if (!getall && !bcn_tb[i])
1067 continue;
1068
1069 netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
1070 &value_byte);
1071 ret = nla_put_u8(dcbnl_skb, i, value_byte);
1072 if (ret)
1073 goto err_bcn;
1074 }
1075
Don Skidmoref4314e82008-12-21 20:10:29 -08001076 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001077 if (!getall && !bcn_tb[i])
1078 continue;
1079
1080 netdev->dcbnl_ops->getbcncfg(netdev, i,
1081 &value_integer);
1082 ret = nla_put_u32(dcbnl_skb, i, value_integer);
1083 if (ret)
1084 goto err_bcn;
1085 }
1086
1087 nla_nest_end(dcbnl_skb, bcn_nest);
1088
1089 nlmsg_end(dcbnl_skb, nlh);
1090
1091 ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
1092 if (ret)
John Fastabend7eaf5072009-09-25 13:12:03 +00001093 goto err_out;
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001094
1095 return 0;
1096
1097err_bcn:
1098 nla_nest_cancel(dcbnl_skb, bcn_nest);
1099nlmsg_failure:
1100err:
Roel Kluin858eb712009-01-04 17:29:21 -08001101 kfree_skb(dcbnl_skb);
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001102err_out:
1103 ret = -EINVAL;
1104 return ret;
1105}
1106
1107static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlattr **tb,
1108 u32 pid, u32 seq, u16 flags)
1109{
1110 struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
1111 int i;
1112 int ret = -EINVAL;
1113 u8 value_byte;
1114 u32 value_int;
1115
Joe Perchesf64f9e72009-11-29 16:55:45 -08001116 if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg ||
1117 !netdev->dcbnl_ops->setbcnrp)
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001118 return ret;
1119
1120 ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
1121 tb[DCB_ATTR_BCN],
1122 dcbnl_pfc_up_nest);
1123 if (ret)
1124 goto err;
1125
1126 for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
1127 if (data[i] == NULL)
1128 continue;
1129 value_byte = nla_get_u8(data[i]);
1130 netdev->dcbnl_ops->setbcnrp(netdev,
1131 data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
1132 }
1133
Don Skidmoref4314e82008-12-21 20:10:29 -08001134 for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001135 if (data[i] == NULL)
1136 continue;
1137 value_int = nla_get_u32(data[i]);
1138 netdev->dcbnl_ops->setbcncfg(netdev,
1139 i, value_int);
1140 }
1141
1142 ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_BCN_SCFG, DCB_ATTR_BCN,
1143 pid, seq, flags);
1144err:
1145 return ret;
1146}
1147
John Fastabend3e290272010-12-30 09:25:46 +00001148/* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
1149 * be completed the entire msg is aborted and error value is returned.
1150 * No attempt is made to reconcile the case where only part of the
1151 * cmd can be completed.
1152 */
1153static int dcbnl_ieee_set(struct net_device *netdev, struct nlattr **tb,
1154 u32 pid, u32 seq, u16 flags)
1155{
1156 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1157 struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
1158 int err = -EOPNOTSUPP;
1159
1160 if (!ops)
1161 goto err;
1162
1163 err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
1164 tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
1165 if (err)
1166 goto err;
1167
1168 if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
1169 struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
1170 err = ops->ieee_setets(netdev, ets);
1171 if (err)
1172 goto err;
1173 }
1174
1175 if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setets) {
1176 struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
1177 err = ops->ieee_setpfc(netdev, pfc);
1178 if (err)
1179 goto err;
1180 }
1181
John Fastabend9ab933a2010-12-30 09:26:31 +00001182 if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
John Fastabend3e290272010-12-30 09:25:46 +00001183 struct nlattr *attr;
1184 int rem;
1185
1186 nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
1187 struct dcb_app *app_data;
1188 if (nla_type(attr) != DCB_ATTR_IEEE_APP)
1189 continue;
1190 app_data = nla_data(attr);
John Fastabend9ab933a2010-12-30 09:26:31 +00001191 if (ops->ieee_setapp)
1192 err = ops->ieee_setapp(netdev, app_data);
1193 else
1194 err = dcb_setapp(netdev, app_data);
John Fastabend3e290272010-12-30 09:25:46 +00001195 if (err)
1196 goto err;
1197 }
1198 }
1199
1200err:
1201 dcbnl_reply(err, RTM_SETDCB, DCB_CMD_IEEE_SET, DCB_ATTR_IEEE,
1202 pid, seq, flags);
1203 return err;
1204}
1205
1206
1207/* Handle IEEE 802.1Qaz GET commands. */
1208static int dcbnl_ieee_get(struct net_device *netdev, struct nlattr **tb,
1209 u32 pid, u32 seq, u16 flags)
1210{
1211 struct sk_buff *skb;
1212 struct nlmsghdr *nlh;
1213 struct dcbmsg *dcb;
John Fastabend9ab933a2010-12-30 09:26:31 +00001214 struct nlattr *ieee, *app;
1215 struct dcb_app_type *itr;
John Fastabend3e290272010-12-30 09:25:46 +00001216 const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1217 int err;
1218
1219 if (!ops)
1220 return -EOPNOTSUPP;
1221
1222 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1223 if (!skb)
1224 return -ENOBUFS;
1225
1226 nlh = NLMSG_NEW(skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
1227
1228 dcb = NLMSG_DATA(nlh);
1229 dcb->dcb_family = AF_UNSPEC;
1230 dcb->cmd = DCB_CMD_IEEE_GET;
1231
1232 NLA_PUT_STRING(skb, DCB_ATTR_IFNAME, netdev->name);
1233
1234 ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
1235 if (!ieee)
1236 goto nla_put_failure;
1237
1238 if (ops->ieee_getets) {
1239 struct ieee_ets ets;
1240 err = ops->ieee_getets(netdev, &ets);
1241 if (!err)
1242 NLA_PUT(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets);
1243 }
1244
1245 if (ops->ieee_getpfc) {
1246 struct ieee_pfc pfc;
1247 err = ops->ieee_getpfc(netdev, &pfc);
1248 if (!err)
1249 NLA_PUT(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc);
1250 }
1251
John Fastabend9ab933a2010-12-30 09:26:31 +00001252 app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
1253 if (!app)
1254 goto nla_put_failure;
1255
1256 spin_lock(&dcb_lock);
1257 list_for_each_entry(itr, &dcb_app_list, list) {
1258 if (strncmp(itr->name, netdev->name, IFNAMSIZ) == 0)
1259 NLA_PUT(skb, DCB_ATTR_IEEE_APP,
1260 sizeof(itr->app), &itr->app);
1261 }
1262 spin_unlock(&dcb_lock);
1263 nla_nest_end(skb, app);
1264
John Fastabend3e290272010-12-30 09:25:46 +00001265 nla_nest_end(skb, ieee);
1266 nlmsg_end(skb, nlh);
1267
1268 return rtnl_unicast(skb, &init_net, pid);
1269nla_put_failure:
1270 nlmsg_cancel(skb, nlh);
1271nlmsg_failure:
1272 kfree_skb(skb);
1273 return -1;
1274}
1275
Shmulik Ravid6241b622010-12-30 06:26:48 +00001276/* DCBX configuration */
1277static int dcbnl_getdcbx(struct net_device *netdev, struct nlattr **tb,
1278 u32 pid, u32 seq, u16 flags)
1279{
1280 int ret = -EINVAL;
1281
1282 if (!netdev->dcbnl_ops->getdcbx)
1283 return ret;
1284
1285 ret = dcbnl_reply(netdev->dcbnl_ops->getdcbx(netdev), RTM_GETDCB,
1286 DCB_CMD_GDCBX, DCB_ATTR_DCBX, pid, seq, flags);
1287
1288 return ret;
1289}
1290
1291static int dcbnl_setdcbx(struct net_device *netdev, struct nlattr **tb,
1292 u32 pid, u32 seq, u16 flags)
1293{
1294 int ret = -EINVAL;
1295 u8 value;
1296
1297 if (!tb[DCB_ATTR_DCBX] || !netdev->dcbnl_ops->setdcbx)
1298 return ret;
1299
1300 value = nla_get_u8(tb[DCB_ATTR_DCBX]);
1301
1302 ret = dcbnl_reply(netdev->dcbnl_ops->setdcbx(netdev, value),
1303 RTM_SETDCB, DCB_CMD_SDCBX, DCB_ATTR_DCBX,
1304 pid, seq, flags);
1305
1306 return ret;
1307}
1308
Alexander Duyck2f90b862008-11-20 20:52:10 -08001309static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1310{
1311 struct net *net = sock_net(skb->sk);
1312 struct net_device *netdev;
1313 struct dcbmsg *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
1314 struct nlattr *tb[DCB_ATTR_MAX + 1];
1315 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
1316 int ret = -EINVAL;
1317
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001318 if (!net_eq(net, &init_net))
Alexander Duyck2f90b862008-11-20 20:52:10 -08001319 return -EINVAL;
1320
1321 ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
1322 dcbnl_rtnl_policy);
1323 if (ret < 0)
1324 return ret;
1325
1326 if (!tb[DCB_ATTR_IFNAME])
1327 return -EINVAL;
1328
1329 netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
1330 if (!netdev)
1331 return -EINVAL;
1332
1333 if (!netdev->dcbnl_ops)
1334 goto errout;
1335
1336 switch (dcb->cmd) {
1337 case DCB_CMD_GSTATE:
1338 ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
1339 nlh->nlmsg_flags);
1340 goto out;
1341 case DCB_CMD_PFC_GCFG:
1342 ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
1343 nlh->nlmsg_flags);
1344 goto out;
1345 case DCB_CMD_GPERM_HWADDR:
1346 ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
1347 nlh->nlmsg_flags);
1348 goto out;
1349 case DCB_CMD_PGTX_GCFG:
1350 ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1351 nlh->nlmsg_flags);
1352 goto out;
1353 case DCB_CMD_PGRX_GCFG:
1354 ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1355 nlh->nlmsg_flags);
1356 goto out;
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001357 case DCB_CMD_BCN_GCFG:
1358 ret = dcbnl_bcn_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1359 nlh->nlmsg_flags);
1360 goto out;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001361 case DCB_CMD_SSTATE:
1362 ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
1363 nlh->nlmsg_flags);
1364 goto out;
1365 case DCB_CMD_PFC_SCFG:
1366 ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
1367 nlh->nlmsg_flags);
1368 goto out;
1369
1370 case DCB_CMD_SET_ALL:
1371 ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
1372 nlh->nlmsg_flags);
1373 goto out;
1374 case DCB_CMD_PGTX_SCFG:
1375 ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1376 nlh->nlmsg_flags);
1377 goto out;
1378 case DCB_CMD_PGRX_SCFG:
1379 ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1380 nlh->nlmsg_flags);
1381 goto out;
Alexander Duyck46132182008-11-20 21:05:08 -08001382 case DCB_CMD_GCAP:
1383 ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
1384 nlh->nlmsg_flags);
1385 goto out;
Alexander Duyck33dbabc2008-11-20 21:08:19 -08001386 case DCB_CMD_GNUMTCS:
1387 ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
1388 nlh->nlmsg_flags);
1389 goto out;
1390 case DCB_CMD_SNUMTCS:
1391 ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
1392 nlh->nlmsg_flags);
1393 goto out;
Alexander Duyck0eb3aa92008-11-20 21:09:23 -08001394 case DCB_CMD_PFC_GSTATE:
1395 ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
1396 nlh->nlmsg_flags);
1397 goto out;
1398 case DCB_CMD_PFC_SSTATE:
1399 ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
1400 nlh->nlmsg_flags);
1401 goto out;
Alexander Duyck859ee3c2008-11-20 21:10:23 -08001402 case DCB_CMD_BCN_SCFG:
1403 ret = dcbnl_bcn_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1404 nlh->nlmsg_flags);
1405 goto out;
Yi Zou57949682009-08-31 12:33:40 +00001406 case DCB_CMD_GAPP:
1407 ret = dcbnl_getapp(netdev, tb, pid, nlh->nlmsg_seq,
1408 nlh->nlmsg_flags);
1409 goto out;
1410 case DCB_CMD_SAPP:
1411 ret = dcbnl_setapp(netdev, tb, pid, nlh->nlmsg_seq,
1412 nlh->nlmsg_flags);
1413 goto out;
John Fastabend3e290272010-12-30 09:25:46 +00001414 case DCB_CMD_IEEE_SET:
1415 ret = dcbnl_ieee_set(netdev, tb, pid, nlh->nlmsg_seq,
1416 nlh->nlmsg_flags);
1417 goto out;
1418 case DCB_CMD_IEEE_GET:
1419 ret = dcbnl_ieee_get(netdev, tb, pid, nlh->nlmsg_seq,
1420 nlh->nlmsg_flags);
1421 goto out;
Shmulik Ravid6241b622010-12-30 06:26:48 +00001422 case DCB_CMD_GDCBX:
1423 ret = dcbnl_getdcbx(netdev, tb, pid, nlh->nlmsg_seq,
1424 nlh->nlmsg_flags);
1425 goto out;
1426 case DCB_CMD_SDCBX:
1427 ret = dcbnl_setdcbx(netdev, tb, pid, nlh->nlmsg_seq,
1428 nlh->nlmsg_flags);
1429 goto out;
Alexander Duyck2f90b862008-11-20 20:52:10 -08001430 default:
1431 goto errout;
1432 }
1433errout:
1434 ret = -EINVAL;
1435out:
1436 dev_put(netdev);
1437 return ret;
1438}
1439
John Fastabend9ab933a2010-12-30 09:26:31 +00001440/**
1441 * dcb_getapp - retrieve the DCBX application user priority
1442 *
1443 * On success returns a non-zero 802.1p user priority bitmap
1444 * otherwise returns 0 as the invalid user priority bitmap to
1445 * indicate an error.
1446 */
1447u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
1448{
1449 struct dcb_app_type *itr;
1450 u8 prio = 0;
1451
1452 spin_lock(&dcb_lock);
1453 list_for_each_entry(itr, &dcb_app_list, list) {
1454 if (itr->app.selector == app->selector &&
1455 itr->app.protocol == app->protocol &&
1456 (strncmp(itr->name, dev->name, IFNAMSIZ) == 0)) {
1457 prio = itr->app.priority;
1458 break;
1459 }
1460 }
1461 spin_unlock(&dcb_lock);
1462
1463 return prio;
1464}
1465EXPORT_SYMBOL(dcb_getapp);
1466
1467/**
1468 * ixgbe_dcbnl_setapp - add dcb application data to app list
1469 *
1470 * Priority 0 is the default priority this removes applications
1471 * from the app list if the priority is set to zero.
1472 */
1473u8 dcb_setapp(struct net_device *dev, struct dcb_app *new)
1474{
1475 struct dcb_app_type *itr;
1476
1477 spin_lock(&dcb_lock);
1478 /* Search for existing match and replace */
1479 list_for_each_entry(itr, &dcb_app_list, list) {
1480 if (itr->app.selector == new->selector &&
1481 itr->app.protocol == new->protocol &&
1482 (strncmp(itr->name, dev->name, IFNAMSIZ) == 0)) {
1483 if (new->priority)
1484 itr->app.priority = new->priority;
1485 else {
1486 list_del(&itr->list);
1487 kfree(itr);
1488 }
1489 goto out;
1490 }
1491 }
1492 /* App type does not exist add new application type */
1493 if (new->priority) {
1494 struct dcb_app_type *entry;
1495 entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
1496 if (!entry) {
1497 spin_unlock(&dcb_lock);
1498 return -ENOMEM;
1499 }
1500
1501 memcpy(&entry->app, new, sizeof(*new));
1502 strncpy(entry->name, dev->name, IFNAMSIZ);
1503 list_add(&entry->list, &dcb_app_list);
1504 }
1505out:
1506 spin_unlock(&dcb_lock);
John Fastabend96b99682010-12-30 09:26:37 +00001507 call_dcbevent_notifiers(DCB_APP_EVENT, new);
John Fastabend9ab933a2010-12-30 09:26:31 +00001508 return 0;
1509}
1510EXPORT_SYMBOL(dcb_setapp);
1511
1512void dcb_flushapp(void)
1513{
1514 struct dcb_app_type *app;
1515
1516 spin_lock(&dcb_lock);
1517 list_for_each_entry(app, &dcb_app_list, list) {
1518 list_del(&app->list);
1519 kfree(app);
1520 }
1521 spin_unlock(&dcb_lock);
1522}
1523
Alexander Duyck2f90b862008-11-20 20:52:10 -08001524static int __init dcbnl_init(void)
1525{
John Fastabend9ab933a2010-12-30 09:26:31 +00001526 INIT_LIST_HEAD(&dcb_app_list);
1527
Alexander Duyck2f90b862008-11-20 20:52:10 -08001528 rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL);
1529 rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL);
1530
1531 return 0;
1532}
1533module_init(dcbnl_init);
1534
1535static void __exit dcbnl_exit(void)
1536{
1537 rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
1538 rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
John Fastabend9ab933a2010-12-30 09:26:31 +00001539 dcb_flushapp();
Alexander Duyck2f90b862008-11-20 20:52:10 -08001540}
1541module_exit(dcbnl_exit);