Blame view

net/dcb/dcbnl.c 55.5 KB
2f90b8657   Alexander Duyck   ixgbe: this patch...
1
  /*
698e1d23c   Mark Rustad   net: dcbnl: Updat...
2
   * Copyright (c) 2008-2011, Intel Corporation.
2f90b8657   Alexander Duyck   ixgbe: this patch...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms and conditions of the GNU General Public License,
   * version 2, as published by the Free Software Foundation.
   *
   * This program is distributed in the hope it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   *
   * You should have received a copy of the GNU General Public License along with
   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
   * Place - Suite 330, Boston, MA 02111-1307 USA.
   *
   * Author: Lucy Liu <lucy.liu@intel.com>
   */
  
  #include <linux/netdevice.h>
  #include <linux/netlink.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/slab.h>
2f90b8657   Alexander Duyck   ixgbe: this patch...
23
24
25
  #include <net/netlink.h>
  #include <net/rtnetlink.h>
  #include <linux/dcbnl.h>
96b99684e   John Fastabend   net_dcb: add appl...
26
  #include <net/dcbevent.h>
2f90b8657   Alexander Duyck   ixgbe: this patch...
27
  #include <linux/rtnetlink.h>
3a9a231d9   Paul Gortmaker   net: Fix files ex...
28
  #include <linux/module.h>
2f90b8657   Alexander Duyck   ixgbe: this patch...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include <net/sock.h>
  
  /**
   * Data Center Bridging (DCB) is a collection of Ethernet enhancements
   * intended to allow network traffic with differing requirements
   * (highly reliable, no drops vs. best effort vs. low latency) to operate
   * and co-exist on Ethernet.  Current DCB features are:
   *
   * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
   *   framework for assigning bandwidth guarantees to traffic classes.
   *
   * Priority-based Flow Control (PFC) - provides a flow control mechanism which
   *   can work independently for each 802.1p priority.
   *
   * Congestion Notification - provides a mechanism for end-to-end congestion
   *   control for protocols which do not have built-in congestion management.
   *
   * More information about the emerging standards for these Ethernet features
   * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
   *
   * This file implements an rtnetlink interface to allow configuration of DCB
   * features for capable devices.
   */
  
  MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
7a6b6f515   Jeff Kirsher   DCB: fix kconfig ...
54
  MODULE_DESCRIPTION("Data Center Bridging netlink interface");
2f90b8657   Alexander Duyck   ixgbe: this patch...
55
56
57
58
59
  MODULE_LICENSE("GPL");
  
  /**************** DCB attribute policies *************************************/
  
  /* DCB netlink attributes policy */
b54452b07   Alexey Dobriyan   const: struct nla...
60
  static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
859ee3c43   Alexander Duyck   DCB: Add support ...
61
62
63
64
65
  	[DCB_ATTR_IFNAME]      = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
  	[DCB_ATTR_STATE]       = {.type = NLA_U8},
  	[DCB_ATTR_PFC_CFG]     = {.type = NLA_NESTED},
  	[DCB_ATTR_PG_CFG]      = {.type = NLA_NESTED},
  	[DCB_ATTR_SET_ALL]     = {.type = NLA_U8},
2f90b8657   Alexander Duyck   ixgbe: this patch...
66
  	[DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
859ee3c43   Alexander Duyck   DCB: Add support ...
67
68
69
  	[DCB_ATTR_CAP]         = {.type = NLA_NESTED},
  	[DCB_ATTR_PFC_STATE]   = {.type = NLA_U8},
  	[DCB_ATTR_BCN]         = {.type = NLA_NESTED},
6fa382af6   Yi Zou   dcbnl: Add netlin...
70
  	[DCB_ATTR_APP]         = {.type = NLA_NESTED},
3e29027af   John Fastabend   dcbnl: add suppor...
71
  	[DCB_ATTR_IEEE]	       = {.type = NLA_NESTED},
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
72
  	[DCB_ATTR_DCBX]        = {.type = NLA_U8},
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
73
  	[DCB_ATTR_FEATCFG]     = {.type = NLA_NESTED},
2f90b8657   Alexander Duyck   ixgbe: this patch...
74
75
76
  };
  
  /* DCB priority flow control to User Priority nested attributes */
b54452b07   Alexey Dobriyan   const: struct nla...
77
  static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
2f90b8657   Alexander Duyck   ixgbe: this patch...
78
79
80
81
82
83
84
85
86
87
88
89
  	[DCB_PFC_UP_ATTR_0]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_1]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_2]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_3]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_4]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_5]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_6]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_7]   = {.type = NLA_U8},
  	[DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
  };
  
  /* DCB priority grouping nested attributes */
b54452b07   Alexey Dobriyan   const: struct nla...
90
  static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
2f90b8657   Alexander Duyck   ixgbe: this patch...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  	[DCB_PG_ATTR_TC_0]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_1]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_2]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_3]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_4]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_5]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_6]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_7]      = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_TC_ALL]    = {.type = NLA_NESTED},
  	[DCB_PG_ATTR_BW_ID_0]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_1]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_2]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_3]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_4]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_5]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_6]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_7]   = {.type = NLA_U8},
  	[DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
  };
  
  /* DCB traffic class nested attributes. */
b54452b07   Alexey Dobriyan   const: struct nla...
112
  static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
2f90b8657   Alexander Duyck   ixgbe: this patch...
113
114
115
116
117
118
  	[DCB_TC_ATTR_PARAM_PGID]            = {.type = NLA_U8},
  	[DCB_TC_ATTR_PARAM_UP_MAPPING]      = {.type = NLA_U8},
  	[DCB_TC_ATTR_PARAM_STRICT_PRIO]     = {.type = NLA_U8},
  	[DCB_TC_ATTR_PARAM_BW_PCT]          = {.type = NLA_U8},
  	[DCB_TC_ATTR_PARAM_ALL]             = {.type = NLA_FLAG},
  };
46132188b   Alexander Duyck   DCB: Add interfac...
119
  /* DCB capabilities nested attributes. */
b54452b07   Alexey Dobriyan   const: struct nla...
120
  static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
46132188b   Alexander Duyck   DCB: Add interfac...
121
122
123
124
125
126
127
128
  	[DCB_CAP_ATTR_ALL]     = {.type = NLA_FLAG},
  	[DCB_CAP_ATTR_PG]      = {.type = NLA_U8},
  	[DCB_CAP_ATTR_PFC]     = {.type = NLA_U8},
  	[DCB_CAP_ATTR_UP2TC]   = {.type = NLA_U8},
  	[DCB_CAP_ATTR_PG_TCS]  = {.type = NLA_U8},
  	[DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
  	[DCB_CAP_ATTR_GSP]     = {.type = NLA_U8},
  	[DCB_CAP_ATTR_BCN]     = {.type = NLA_U8},
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
129
  	[DCB_CAP_ATTR_DCBX]    = {.type = NLA_U8},
46132188b   Alexander Duyck   DCB: Add interfac...
130
  };
2f90b8657   Alexander Duyck   ixgbe: this patch...
131

33dbabc4a   Alexander Duyck   DCB: Add interfac...
132
  /* DCB capabilities nested attributes. */
b54452b07   Alexey Dobriyan   const: struct nla...
133
  static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
33dbabc4a   Alexander Duyck   DCB: Add interfac...
134
135
136
137
  	[DCB_NUMTCS_ATTR_ALL]     = {.type = NLA_FLAG},
  	[DCB_NUMTCS_ATTR_PG]      = {.type = NLA_U8},
  	[DCB_NUMTCS_ATTR_PFC]     = {.type = NLA_U8},
  };
859ee3c43   Alexander Duyck   DCB: Add support ...
138
  /* DCB BCN nested attributes. */
b54452b07   Alexey Dobriyan   const: struct nla...
139
  static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
859ee3c43   Alexander Duyck   DCB: Add support ...
140
141
142
143
144
145
146
147
148
  	[DCB_BCN_ATTR_RP_0]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_1]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_2]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_3]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_4]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_5]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_6]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_7]         = {.type = NLA_U8},
  	[DCB_BCN_ATTR_RP_ALL]       = {.type = NLA_FLAG},
f4314e815   Don Skidmore   net: add DCNA att...
149
150
  	[DCB_BCN_ATTR_BCNA_0]       = {.type = NLA_U32},
  	[DCB_BCN_ATTR_BCNA_1]       = {.type = NLA_U32},
859ee3c43   Alexander Duyck   DCB: Add support ...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  	[DCB_BCN_ATTR_ALPHA]        = {.type = NLA_U32},
  	[DCB_BCN_ATTR_BETA]         = {.type = NLA_U32},
  	[DCB_BCN_ATTR_GD]           = {.type = NLA_U32},
  	[DCB_BCN_ATTR_GI]           = {.type = NLA_U32},
  	[DCB_BCN_ATTR_TMAX]         = {.type = NLA_U32},
  	[DCB_BCN_ATTR_TD]           = {.type = NLA_U32},
  	[DCB_BCN_ATTR_RMIN]         = {.type = NLA_U32},
  	[DCB_BCN_ATTR_W]            = {.type = NLA_U32},
  	[DCB_BCN_ATTR_RD]           = {.type = NLA_U32},
  	[DCB_BCN_ATTR_RU]           = {.type = NLA_U32},
  	[DCB_BCN_ATTR_WRTT]         = {.type = NLA_U32},
  	[DCB_BCN_ATTR_RI]           = {.type = NLA_U32},
  	[DCB_BCN_ATTR_C]            = {.type = NLA_U32},
  	[DCB_BCN_ATTR_ALL]          = {.type = NLA_FLAG},
  };
6fa382af6   Yi Zou   dcbnl: Add netlin...
166
  /* DCB APP nested attributes. */
b54452b07   Alexey Dobriyan   const: struct nla...
167
  static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
6fa382af6   Yi Zou   dcbnl: Add netlin...
168
169
170
171
  	[DCB_APP_ATTR_IDTYPE]       = {.type = NLA_U8},
  	[DCB_APP_ATTR_ID]           = {.type = NLA_U16},
  	[DCB_APP_ATTR_PRIORITY]     = {.type = NLA_U8},
  };
3e29027af   John Fastabend   dcbnl: add suppor...
172
173
174
175
176
177
178
179
180
181
  /* IEEE 802.1Qaz nested attributes. */
  static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
  	[DCB_ATTR_IEEE_ETS]	    = {.len = sizeof(struct ieee_ets)},
  	[DCB_ATTR_IEEE_PFC]	    = {.len = sizeof(struct ieee_pfc)},
  	[DCB_ATTR_IEEE_APP_TABLE]   = {.type = NLA_NESTED},
  };
  
  static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
  	[DCB_ATTR_IEEE_APP]	    = {.len = sizeof(struct dcb_app)},
  };
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
182
183
184
185
186
187
188
  /* DCB number of traffic classes nested attributes. */
  static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
  	[DCB_FEATCFG_ATTR_ALL]      = {.type = NLA_FLAG},
  	[DCB_FEATCFG_ATTR_PG]       = {.type = NLA_U8},
  	[DCB_FEATCFG_ATTR_PFC]      = {.type = NLA_U8},
  	[DCB_FEATCFG_ATTR_APP]      = {.type = NLA_U8},
  };
9ab933ab2   John Fastabend   dcbnl: add applic...
189
190
  static LIST_HEAD(dcb_app_list);
  static DEFINE_SPINLOCK(dcb_lock);
2f90b8657   Alexander Duyck   ixgbe: this patch...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  /* standard netlink reply call */
  static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,
                         u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct dcbmsg *dcb;
  	struct nlmsghdr *nlh;
  	int ret = -EINVAL;
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb)
  		return ret;
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = cmd;
  	dcb->dcb_pad = 0;
  
  	ret = nla_put_u8(dcbnl_skb, attr, value);
  	if (ret)
  		goto err;
  
  	/* end the message, assign the nlmsg_len. */
  	nlmsg_end(dcbnl_skb, nlh);
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret)
7eaf5077b   John Fastabend   net: fix double s...
219
  		return -EINVAL;
2f90b8657   Alexander Duyck   ixgbe: this patch...
220
221
222
223
  
  	return 0;
  nlmsg_failure:
  err:
858eb711b   Roel Kluin   DCB: fix kfree(skb)
224
  	kfree_skb(dcbnl_skb);
2f90b8657   Alexander Duyck   ixgbe: this patch...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  	return ret;
  }
  
  static int dcbnl_getstate(struct net_device *netdev, struct nlattr **tb,
                            u32 pid, u32 seq, u16 flags)
  {
  	int ret = -EINVAL;
  
  	/* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
  	if (!netdev->dcbnl_ops->getstate)
  		return ret;
  
  	ret = dcbnl_reply(netdev->dcbnl_ops->getstate(netdev), RTM_GETDCB,
  	                  DCB_CMD_GSTATE, DCB_ATTR_STATE, pid, seq, flags);
  
  	return ret;
  }
  
  static int dcbnl_getpfccfg(struct net_device *netdev, struct nlattr **tb,
                             u32 pid, u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
  	u8 value;
  	int ret = -EINVAL;
  	int i;
  	int getall = 0;
  
  	if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
  		return ret;
  
  	ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  	                       tb[DCB_ATTR_PFC_CFG],
  	                       dcbnl_pfc_up_nest);
  	if (ret)
  		goto err_out;
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb)
  		goto err_out;
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_PFC_GCFG;
  
  	nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PFC_CFG);
  	if (!nest)
  		goto err;
  
  	if (data[DCB_PFC_UP_ATTR_ALL])
  		getall = 1;
  
  	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  		if (!getall && !data[i])
  			continue;
  
  		netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
  		                             &value);
  		ret = nla_put_u8(dcbnl_skb, i, value);
  
  		if (ret) {
  			nla_nest_cancel(dcbnl_skb, nest);
  			goto err;
  		}
  	}
  	nla_nest_end(dcbnl_skb, nest);
  
  	nlmsg_end(dcbnl_skb, nlh);
  
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret)
7eaf5077b   John Fastabend   net: fix double s...
300
  		goto err_out;
2f90b8657   Alexander Duyck   ixgbe: this patch...
301
302
303
304
  
  	return 0;
  nlmsg_failure:
  err:
858eb711b   Roel Kluin   DCB: fix kfree(skb)
305
  	kfree_skb(dcbnl_skb);
2f90b8657   Alexander Duyck   ixgbe: this patch...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  err_out:
  	return -EINVAL;
  }
  
  static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlattr **tb,
                                  u32 pid, u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	u8 perm_addr[MAX_ADDR_LEN];
  	int ret = -EINVAL;
  
  	if (!netdev->dcbnl_ops->getpermhwaddr)
  		return ret;
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb)
  		goto err_out;
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_GPERM_HWADDR;
  
  	netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
  
  	ret = nla_put(dcbnl_skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr),
  	              perm_addr);
  
  	nlmsg_end(dcbnl_skb, nlh);
  
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret)
7eaf5077b   John Fastabend   net: fix double s...
341
  		goto err_out;
2f90b8657   Alexander Duyck   ixgbe: this patch...
342
343
344
345
  
  	return 0;
  
  nlmsg_failure:
858eb711b   Roel Kluin   DCB: fix kfree(skb)
346
  	kfree_skb(dcbnl_skb);
2f90b8657   Alexander Duyck   ixgbe: this patch...
347
348
349
  err_out:
  	return -EINVAL;
  }
46132188b   Alexander Duyck   DCB: Add interfac...
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  static int dcbnl_getcap(struct net_device *netdev, struct nlattr **tb,
                          u32 pid, u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
  	u8 value;
  	int ret = -EINVAL;
  	int i;
  	int getall = 0;
  
  	if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
  		return ret;
  
  	ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
  	                       dcbnl_cap_nest);
  	if (ret)
  		goto err_out;
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb)
  		goto err_out;
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_GCAP;
  
  	nest = nla_nest_start(dcbnl_skb, DCB_ATTR_CAP);
  	if (!nest)
  		goto err;
  
  	if (data[DCB_CAP_ATTR_ALL])
  		getall = 1;
  
  	for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
  		if (!getall && !data[i])
  			continue;
  
  		if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
  			ret = nla_put_u8(dcbnl_skb, i, value);
  
  			if (ret) {
  				nla_nest_cancel(dcbnl_skb, nest);
  				goto err;
  			}
  		}
  	}
  	nla_nest_end(dcbnl_skb, nest);
  
  	nlmsg_end(dcbnl_skb, nlh);
  
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret)
7eaf5077b   John Fastabend   net: fix double s...
406
  		goto err_out;
46132188b   Alexander Duyck   DCB: Add interfac...
407
408
409
410
  
  	return 0;
  nlmsg_failure:
  err:
858eb711b   Roel Kluin   DCB: fix kfree(skb)
411
  	kfree_skb(dcbnl_skb);
46132188b   Alexander Duyck   DCB: Add interfac...
412
413
414
  err_out:
  	return -EINVAL;
  }
33dbabc4a   Alexander Duyck   DCB: Add interfac...
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  static int dcbnl_getnumtcs(struct net_device *netdev, struct nlattr **tb,
                             u32 pid, u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
  	u8 value;
  	int ret = -EINVAL;
  	int i;
  	int getall = 0;
  
  	if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
  		return ret;
  
  	ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  	                       dcbnl_numtcs_nest);
  	if (ret) {
  		ret = -EINVAL;
  		goto err_out;
  	}
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb) {
  		ret = -EINVAL;
  		goto err_out;
  	}
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_GNUMTCS;
  
  	nest = nla_nest_start(dcbnl_skb, DCB_ATTR_NUMTCS);
  	if (!nest) {
  		ret = -EINVAL;
  		goto err;
  	}
  
  	if (data[DCB_NUMTCS_ATTR_ALL])
  		getall = 1;
  
  	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  		if (!getall && !data[i])
  			continue;
  
  		ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
  		if (!ret) {
  			ret = nla_put_u8(dcbnl_skb, i, value);
  
  			if (ret) {
  				nla_nest_cancel(dcbnl_skb, nest);
  				ret = -EINVAL;
  				goto err;
  			}
  		} else {
  			goto err;
  		}
  	}
  	nla_nest_end(dcbnl_skb, nest);
  
  	nlmsg_end(dcbnl_skb, nlh);
  
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret) {
  		ret = -EINVAL;
7eaf5077b   John Fastabend   net: fix double s...
482
  		goto err_out;
33dbabc4a   Alexander Duyck   DCB: Add interfac...
483
484
485
486
487
  	}
  
  	return 0;
  nlmsg_failure:
  err:
858eb711b   Roel Kluin   DCB: fix kfree(skb)
488
  	kfree_skb(dcbnl_skb);
33dbabc4a   Alexander Duyck   DCB: Add interfac...
489
490
491
492
493
494
495
496
497
498
499
  err_out:
  	return ret;
  }
  
  static int dcbnl_setnumtcs(struct net_device *netdev, struct nlattr **tb,
                             u32 pid, u32 seq, u16 flags)
  {
  	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
  	int ret = -EINVAL;
  	u8 value;
  	int i;
8b124a8e1   Don Skidmore   net: fix dcbnl_se...
500
  	if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
33dbabc4a   Alexander Duyck   DCB: Add interfac...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
  		return ret;
  
  	ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  	                       dcbnl_numtcs_nest);
  
  	if (ret) {
  		ret = -EINVAL;
  		goto err;
  	}
  
  	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  		if (data[i] == NULL)
  			continue;
  
  		value = nla_get_u8(data[i]);
  
  		ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
  
  		if (ret)
  			goto operr;
  	}
  
  operr:
  	ret = dcbnl_reply(!!ret, RTM_SETDCB, DCB_CMD_SNUMTCS,
  	                  DCB_ATTR_NUMTCS, pid, seq, flags);
  
  err:
  	return ret;
  }
0eb3aa9ba   Alexander Duyck   DCB: Add interfac...
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
  static int dcbnl_getpfcstate(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags)
  {
  	int ret = -EINVAL;
  
  	if (!netdev->dcbnl_ops->getpfcstate)
  		return ret;
  
  	ret = dcbnl_reply(netdev->dcbnl_ops->getpfcstate(netdev), RTM_GETDCB,
  	                  DCB_CMD_PFC_GSTATE, DCB_ATTR_PFC_STATE,
  	                  pid, seq, flags);
  
  	return ret;
  }
  
  static int dcbnl_setpfcstate(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags)
  {
  	int ret = -EINVAL;
  	u8 value;
  
  	if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
  		return ret;
  
  	value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
  
  	netdev->dcbnl_ops->setpfcstate(netdev, value);
  
  	ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE,
  	                  pid, seq, flags);
  
  	return ret;
  }
579496865   Yi Zou   dcbnl: Add implem...
563
564
565
566
567
568
569
570
571
572
573
  static int dcbnl_getapp(struct net_device *netdev, struct nlattr **tb,
                          u32 pid, u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	struct nlattr *app_nest;
  	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
  	u16 id;
  	u8 up, idtype;
  	int ret = -EINVAL;
3dce38a02   John Fastabend   dcbnl: make get_a...
574
  	if (!tb[DCB_ATTR_APP])
579496865   Yi Zou   dcbnl: Add implem...
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  		goto out;
  
  	ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  	                       dcbnl_app_nest);
  	if (ret)
  		goto out;
  
  	ret = -EINVAL;
  	/* all must be non-null */
  	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  	    (!app_tb[DCB_APP_ATTR_ID]))
  		goto out;
  
  	/* either by eth type or by socket number */
  	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  	    (idtype != DCB_APP_IDTYPE_PORTNUM))
  		goto out;
  
  	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
3dce38a02   John Fastabend   dcbnl: make get_a...
595
596
597
598
599
600
601
602
603
604
  
  	if (netdev->dcbnl_ops->getapp) {
  		up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
  	} else {
  		struct dcb_app app = {
  					.selector = idtype,
  					.protocol = id,
  				     };
  		up = dcb_getapp(netdev, &app);
  	}
579496865   Yi Zou   dcbnl: Add implem...
605
606
607
608
609
610
611
612
613
614
615
616
  
  	/* send this back */
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb)
  		goto out;
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_GAPP;
  
  	app_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_APP);
d3337de52   Jesper Juhl   Don't potentially...
617
618
  	if (!app_nest)
  		goto out_cancel;
579496865   Yi Zou   dcbnl: Add implem...
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  	ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_IDTYPE, idtype);
  	if (ret)
  		goto out_cancel;
  
  	ret = nla_put_u16(dcbnl_skb, DCB_APP_ATTR_ID, id);
  	if (ret)
  		goto out_cancel;
  
  	ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_PRIORITY, up);
  	if (ret)
  		goto out_cancel;
  
  	nla_nest_end(dcbnl_skb, app_nest);
  	nlmsg_end(dcbnl_skb, nlh);
  
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret)
  		goto nlmsg_failure;
  
  	goto out;
  
  out_cancel:
  	nla_nest_cancel(dcbnl_skb, app_nest);
  nlmsg_failure:
  	kfree_skb(dcbnl_skb);
  out:
  	return ret;
  }
  
  static int dcbnl_setapp(struct net_device *netdev, struct nlattr **tb,
                          u32 pid, u32 seq, u16 flags)
  {
9ab933ab2   John Fastabend   dcbnl: add applic...
651
  	int err, ret = -EINVAL;
579496865   Yi Zou   dcbnl: Add implem...
652
653
654
  	u16 id;
  	u8 up, idtype;
  	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
9ab933ab2   John Fastabend   dcbnl: add applic...
655
  	if (!tb[DCB_ATTR_APP])
579496865   Yi Zou   dcbnl: Add implem...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
  		goto out;
  
  	ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  	                       dcbnl_app_nest);
  	if (ret)
  		goto out;
  
  	ret = -EINVAL;
  	/* all must be non-null */
  	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  	    (!app_tb[DCB_APP_ATTR_ID]) ||
  	    (!app_tb[DCB_APP_ATTR_PRIORITY]))
  		goto out;
  
  	/* either by eth type or by socket number */
  	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  	    (idtype != DCB_APP_IDTYPE_PORTNUM))
  		goto out;
  
  	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
  	up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
9ab933ab2   John Fastabend   dcbnl: add applic...
678
679
680
681
682
683
684
685
686
687
688
689
  	if (netdev->dcbnl_ops->setapp) {
  		err = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
  	} else {
  		struct dcb_app app;
  		app.selector = idtype;
  		app.protocol = id;
  		app.priority = up;
  		err = dcb_setapp(netdev, &app);
  	}
  
  	ret = dcbnl_reply(err, RTM_SETDCB, DCB_CMD_SAPP, DCB_ATTR_APP,
  			  pid, seq, flags);
579496865   Yi Zou   dcbnl: Add implem...
690
691
692
  out:
  	return ret;
  }
2f90b8657   Alexander Duyck   ixgbe: this patch...
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
  static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags, int dir)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	struct nlattr *pg_nest, *param_nest, *data;
  	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  	u8 prio, pgid, tc_pct, up_map;
  	int ret  = -EINVAL;
  	int getall = 0;
  	int i;
  
  	if (!tb[DCB_ATTR_PG_CFG] ||
  	    !netdev->dcbnl_ops->getpgtccfgtx ||
  	    !netdev->dcbnl_ops->getpgtccfgrx ||
  	    !netdev->dcbnl_ops->getpgbwgcfgtx ||
  	    !netdev->dcbnl_ops->getpgbwgcfgrx)
  		return ret;
  
  	ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  	                       tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  
  	if (ret)
  		goto err_out;
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb)
  		goto err_out;
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = (dir) ? DCB_CMD_PGRX_GCFG : DCB_CMD_PGTX_GCFG;
  
  	pg_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PG_CFG);
  	if (!pg_nest)
  		goto err;
  
  	if (pg_tb[DCB_PG_ATTR_TC_ALL])
  		getall = 1;
  
  	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  		if (!getall && !pg_tb[i])
  			continue;
  
  		if (pg_tb[DCB_PG_ATTR_TC_ALL])
  			data = pg_tb[DCB_PG_ATTR_TC_ALL];
  		else
  			data = pg_tb[i];
  		ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  				       data, dcbnl_tc_param_nest);
  		if (ret)
  			goto err_pg;
  
  		param_nest = nla_nest_start(dcbnl_skb, i);
  		if (!param_nest)
  			goto err_pg;
  
  		pgid = DCB_ATTR_VALUE_UNDEFINED;
  		prio = DCB_ATTR_VALUE_UNDEFINED;
  		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  		up_map = DCB_ATTR_VALUE_UNDEFINED;
  
  		if (dir) {
  			/* Rx */
  			netdev->dcbnl_ops->getpgtccfgrx(netdev,
  						i - DCB_PG_ATTR_TC_0, &prio,
  						&pgid, &tc_pct, &up_map);
  		} else {
  			/* Tx */
  			netdev->dcbnl_ops->getpgtccfgtx(netdev,
  						i - DCB_PG_ATTR_TC_0, &prio,
  						&pgid, &tc_pct, &up_map);
  		}
  
  		if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
  		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  			ret = nla_put_u8(dcbnl_skb,
  			                 DCB_TC_ATTR_PARAM_PGID, pgid);
  			if (ret)
  				goto err_param;
  		}
  		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
  		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  			ret = nla_put_u8(dcbnl_skb,
  			                 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
  			if (ret)
  				goto err_param;
  		}
  		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
  		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  			ret = nla_put_u8(dcbnl_skb,
  			                 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
  			if (ret)
  				goto err_param;
  		}
  		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
  		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  			ret = nla_put_u8(dcbnl_skb, DCB_TC_ATTR_PARAM_BW_PCT,
  			                 tc_pct);
  			if (ret)
  				goto err_param;
  		}
  		nla_nest_end(dcbnl_skb, param_nest);
  	}
  
  	if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
  		getall = 1;
  	else
  		getall = 0;
  
  	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  		if (!getall && !pg_tb[i])
  			continue;
  
  		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  
  		if (dir) {
  			/* Rx */
  			netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
  					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  		} else {
  			/* Tx */
  			netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
  					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  		}
  		ret = nla_put_u8(dcbnl_skb, i, tc_pct);
  
  		if (ret)
  			goto err_pg;
  	}
  
  	nla_nest_end(dcbnl_skb, pg_nest);
  
  	nlmsg_end(dcbnl_skb, nlh);
  
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret)
7eaf5077b   John Fastabend   net: fix double s...
834
  		goto err_out;
2f90b8657   Alexander Duyck   ixgbe: this patch...
835
836
837
838
839
840
841
842
843
  
  	return 0;
  
  err_param:
  	nla_nest_cancel(dcbnl_skb, param_nest);
  err_pg:
  	nla_nest_cancel(dcbnl_skb, pg_nest);
  nlmsg_failure:
  err:
858eb711b   Roel Kluin   DCB: fix kfree(skb)
844
  	kfree_skb(dcbnl_skb);
2f90b8657   Alexander Duyck   ixgbe: this patch...
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
  err_out:
  	ret  = -EINVAL;
  	return ret;
  }
  
  static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags)
  {
  	return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 0);
  }
  
  static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags)
  {
  	return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 1);
  }
  
  static int dcbnl_setstate(struct net_device *netdev, struct nlattr **tb,
                            u32 pid, u32 seq, u16 flags)
  {
  	int ret = -EINVAL;
  	u8 value;
  
  	if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
  		return ret;
  
  	value = nla_get_u8(tb[DCB_ATTR_STATE]);
1486a61eb   Don Skidmore   net: fix DCB sets...
872
873
  	ret = dcbnl_reply(netdev->dcbnl_ops->setstate(netdev, value),
  	                  RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
2f90b8657   Alexander Duyck   ixgbe: this patch...
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
  	                  pid, seq, flags);
  
  	return ret;
  }
  
  static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
                             u32 pid, u32 seq, u16 flags)
  {
  	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
  	int i;
  	int ret = -EINVAL;
  	u8 value;
  
  	if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
  		return ret;
  
  	ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  	                       tb[DCB_ATTR_PFC_CFG],
  	                       dcbnl_pfc_up_nest);
  	if (ret)
  		goto err;
  
  	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  		if (data[i] == NULL)
  			continue;
  		value = nla_get_u8(data[i]);
  		netdev->dcbnl_ops->setpfccfg(netdev,
  			data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
  	}
  
  	ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
  	                  pid, seq, flags);
  err:
  	return ret;
  }
  
  static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
                          u32 pid, u32 seq, u16 flags)
  {
  	int ret = -EINVAL;
  
  	if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
  		return ret;
  
  	ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
  	                  DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
  
  	return ret;
  }
  
  static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags, int dir)
  {
  	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  	int ret = -EINVAL;
  	int i;
  	u8 pgid;
  	u8 up_map;
  	u8 prio;
  	u8 tc_pct;
  
  	if (!tb[DCB_ATTR_PG_CFG] ||
  	    !netdev->dcbnl_ops->setpgtccfgtx ||
  	    !netdev->dcbnl_ops->setpgtccfgrx ||
  	    !netdev->dcbnl_ops->setpgbwgcfgtx ||
  	    !netdev->dcbnl_ops->setpgbwgcfgrx)
  		return ret;
  
  	ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  	                       tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  	if (ret)
  		goto err;
  
  	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  		if (!pg_tb[i])
  			continue;
  
  		ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  		                       pg_tb[i], dcbnl_tc_param_nest);
  		if (ret)
  			goto err;
  
  		pgid = DCB_ATTR_VALUE_UNDEFINED;
  		prio = DCB_ATTR_VALUE_UNDEFINED;
  		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  		up_map = DCB_ATTR_VALUE_UNDEFINED;
  
  		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
  			prio =
  			    nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
  
  		if (param_tb[DCB_TC_ATTR_PARAM_PGID])
  			pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
  
  		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
  			tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
  
  		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
  			up_map =
  			     nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
  
  		/* dir: Tx = 0, Rx = 1 */
  		if (dir) {
  			/* Rx */
  			netdev->dcbnl_ops->setpgtccfgrx(netdev,
  				i - DCB_PG_ATTR_TC_0,
  				prio, pgid, tc_pct, up_map);
  		} else {
  			/* Tx */
  			netdev->dcbnl_ops->setpgtccfgtx(netdev,
  				i - DCB_PG_ATTR_TC_0,
  				prio, pgid, tc_pct, up_map);
  		}
  	}
  
  	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  		if (!pg_tb[i])
  			continue;
  
  		tc_pct = nla_get_u8(pg_tb[i]);
  
  		/* dir: Tx = 0, Rx = 1 */
  		if (dir) {
  			/* Rx */
  			netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
  					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  		} else {
  			/* Tx */
  			netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
  					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  		}
  	}
  
  	ret = dcbnl_reply(0, RTM_SETDCB,
  			  (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
  			  DCB_ATTR_PG_CFG, pid, seq, flags);
  
  err:
  	return ret;
  }
  
  static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags)
  {
  	return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
  }
  
  static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
                               u32 pid, u32 seq, u16 flags)
  {
  	return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
  }
859ee3c43   Alexander Duyck   DCB: Add support ...
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
  static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlattr **tb,
                              u32 pid, u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	struct nlattr *bcn_nest;
  	struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
  	u8 value_byte;
  	u32 value_integer;
  	int ret  = -EINVAL;
  	bool getall = false;
  	int i;
  
  	if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
  	    !netdev->dcbnl_ops->getbcncfg)
  		return ret;
  
  	ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
  	                       tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
  
  	if (ret)
  		goto err_out;
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb)
  		goto err_out;
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_BCN_GCFG;
  
  	bcn_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_BCN);
  	if (!bcn_nest)
  		goto err;
  
  	if (bcn_tb[DCB_BCN_ATTR_ALL])
  		getall = true;
  
  	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  		if (!getall && !bcn_tb[i])
  			continue;
  
  		netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
  		                            &value_byte);
  		ret = nla_put_u8(dcbnl_skb, i, value_byte);
  		if (ret)
  			goto err_bcn;
  	}
f4314e815   Don Skidmore   net: add DCNA att...
1078
  	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
859ee3c43   Alexander Duyck   DCB: Add support ...
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
  		if (!getall && !bcn_tb[i])
  			continue;
  
  		netdev->dcbnl_ops->getbcncfg(netdev, i,
  		                             &value_integer);
  		ret = nla_put_u32(dcbnl_skb, i, value_integer);
  		if (ret)
  			goto err_bcn;
  	}
  
  	nla_nest_end(dcbnl_skb, bcn_nest);
  
  	nlmsg_end(dcbnl_skb, nlh);
  
  	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  	if (ret)
7eaf5077b   John Fastabend   net: fix double s...
1095
  		goto err_out;
859ee3c43   Alexander Duyck   DCB: Add support ...
1096
1097
1098
1099
1100
1101
1102
  
  	return 0;
  
  err_bcn:
  	nla_nest_cancel(dcbnl_skb, bcn_nest);
  nlmsg_failure:
  err:
858eb711b   Roel Kluin   DCB: fix kfree(skb)
1103
  	kfree_skb(dcbnl_skb);
859ee3c43   Alexander Duyck   DCB: Add support ...
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
  err_out:
  	ret  = -EINVAL;
  	return ret;
  }
  
  static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlattr **tb,
                              u32 pid, u32 seq, u16 flags)
  {
  	struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
  	int i;
  	int ret = -EINVAL;
  	u8 value_byte;
  	u32 value_int;
f64f9e719   Joe Perches   net: Move && and ...
1117
1118
  	if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg ||
  	    !netdev->dcbnl_ops->setbcnrp)
859ee3c43   Alexander Duyck   DCB: Add support ...
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
  		return ret;
  
  	ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
  	                       tb[DCB_ATTR_BCN],
  	                       dcbnl_pfc_up_nest);
  	if (ret)
  		goto err;
  
  	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  		if (data[i] == NULL)
  			continue;
  		value_byte = nla_get_u8(data[i]);
  		netdev->dcbnl_ops->setbcnrp(netdev,
  			data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
  	}
f4314e815   Don Skidmore   net: add DCNA att...
1134
  	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
859ee3c43   Alexander Duyck   DCB: Add support ...
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
  		if (data[i] == NULL)
  			continue;
  		value_int = nla_get_u32(data[i]);
  		netdev->dcbnl_ops->setbcncfg(netdev,
  	                                     i, value_int);
  	}
  
  	ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_BCN_SCFG, DCB_ATTR_BCN,
  	                  pid, seq, flags);
  err:
  	return ret;
  }
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1147
1148
1149
  static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
  				int app_nested_type, int app_info_type,
  				int app_entry_type)
eed84713b   Shmulik Ravid   dcbnl: add suppor...
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
  {
  	struct dcb_peer_app_info info;
  	struct dcb_app *table = NULL;
  	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  	u16 app_count;
  	int err;
  
  
  	/**
  	 * retrieve the peer app configuration form the driver. If the driver
  	 * handlers fail exit without doing anything
  	 */
  	err = ops->peer_getappinfo(netdev, &info, &app_count);
  	if (!err && app_count) {
  		table = kmalloc(sizeof(struct dcb_app) * app_count, GFP_KERNEL);
  		if (!table)
  			return -ENOMEM;
  
  		err = ops->peer_getapptable(netdev, table);
  	}
  
  	if (!err) {
  		u16 i;
  		struct nlattr *app;
  
  		/**
  		 * build the message, from here on the only possible failure
  		 * is due to the skb size
  		 */
  		err = -EMSGSIZE;
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1180
  		app = nla_nest_start(skb, app_nested_type);
eed84713b   Shmulik Ravid   dcbnl: add suppor...
1181
1182
  		if (!app)
  			goto nla_put_failure;
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1183
1184
  		if (app_info_type)
  			NLA_PUT(skb, app_info_type, sizeof(info), &info);
eed84713b   Shmulik Ravid   dcbnl: add suppor...
1185
  		for (i = 0; i < app_count; i++)
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1186
  			NLA_PUT(skb, app_entry_type, sizeof(struct dcb_app),
eed84713b   Shmulik Ravid   dcbnl: add suppor...
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
  				&table[i]);
  
  		nla_nest_end(skb, app);
  	}
  	err = 0;
  
  nla_put_failure:
  	kfree(table);
  	return err;
  }
3e29027af   John Fastabend   dcbnl: add suppor...
1197
1198
  
  /* Handle IEEE 802.1Qaz GET commands. */
314b4778e   John Fastabend   net: dcbnl, add m...
1199
  static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
3e29027af   John Fastabend   dcbnl: add suppor...
1200
  {
9ab933ab2   John Fastabend   dcbnl: add applic...
1201
1202
  	struct nlattr *ieee, *app;
  	struct dcb_app_type *itr;
3e29027af   John Fastabend   dcbnl: add suppor...
1203
  	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
c7797baf9   John Fastabend   dcb: Add DCBX cap...
1204
  	int dcbx;
314b4778e   John Fastabend   net: dcbnl, add m...
1205
  	int err = -EMSGSIZE;
3e29027af   John Fastabend   dcbnl: add suppor...
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
  
  	NLA_PUT_STRING(skb, DCB_ATTR_IFNAME, netdev->name);
  
  	ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
  	if (!ieee)
  		goto nla_put_failure;
  
  	if (ops->ieee_getets) {
  		struct ieee_ets ets;
  		err = ops->ieee_getets(netdev, &ets);
  		if (!err)
  			NLA_PUT(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets);
  	}
  
  	if (ops->ieee_getpfc) {
  		struct ieee_pfc pfc;
  		err = ops->ieee_getpfc(netdev, &pfc);
  		if (!err)
  			NLA_PUT(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc);
  	}
9ab933ab2   John Fastabend   dcbnl: add applic...
1226
1227
1228
1229
1230
1231
  	app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
  	if (!app)
  		goto nla_put_failure;
  
  	spin_lock(&dcb_lock);
  	list_for_each_entry(itr, &dcb_app_list, list) {
e290ed813   Mark Rustad   dcb: Use ifindex ...
1232
  		if (itr->ifindex == netdev->ifindex) {
70bfa2d2e   Dan Carpenter   dcb: unlock on er...
1233
1234
1235
1236
1237
1238
1239
  			err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
  					 &itr->app);
  			if (err) {
  				spin_unlock(&dcb_lock);
  				goto nla_put_failure;
  			}
  		}
9ab933ab2   John Fastabend   dcbnl: add applic...
1240
  	}
c7797baf9   John Fastabend   dcb: Add DCBX cap...
1241
1242
1243
1244
1245
  
  	if (netdev->dcbnl_ops->getdcbx)
  		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  	else
  		dcbx = -EOPNOTSUPP;
9ab933ab2   John Fastabend   dcbnl: add applic...
1246
1247
  	spin_unlock(&dcb_lock);
  	nla_nest_end(skb, app);
eed84713b   Shmulik Ravid   dcbnl: add suppor...
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
  	/* get peer info if available */
  	if (ops->ieee_peer_getets) {
  		struct ieee_ets ets;
  		err = ops->ieee_peer_getets(netdev, &ets);
  		if (!err)
  			NLA_PUT(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets);
  	}
  
  	if (ops->ieee_peer_getpfc) {
  		struct ieee_pfc pfc;
  		err = ops->ieee_peer_getpfc(netdev, &pfc);
  		if (!err)
  			NLA_PUT(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc);
  	}
  
  	if (ops->peer_getappinfo && ops->peer_getapptable) {
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1264
1265
1266
1267
  		err = dcbnl_build_peer_app(netdev, skb,
  					   DCB_ATTR_IEEE_PEER_APP,
  					   DCB_ATTR_IEEE_APP_UNSPEC,
  					   DCB_ATTR_IEEE_APP);
eed84713b   Shmulik Ravid   dcbnl: add suppor...
1268
1269
1270
  		if (err)
  			goto nla_put_failure;
  	}
3e29027af   John Fastabend   dcbnl: add suppor...
1271
  	nla_nest_end(skb, ieee);
c7797baf9   John Fastabend   dcb: Add DCBX cap...
1272
1273
1274
1275
1276
  	if (dcbx >= 0) {
  		err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  		if (err)
  			goto nla_put_failure;
  	}
3e29027af   John Fastabend   dcbnl: add suppor...
1277

314b4778e   John Fastabend   net: dcbnl, add m...
1278
  	return 0;
3e29027af   John Fastabend   dcbnl: add suppor...
1279
  nla_put_failure:
314b4778e   John Fastabend   net: dcbnl, add m...
1280
  	return err;
3e29027af   John Fastabend   dcbnl: add suppor...
1281
  }
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
  static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
  			     int dir)
  {
  	u8 pgid, up_map, prio, tc_pct;
  	const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  	int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
  	struct nlattr *pg = nla_nest_start(skb, i);
  
  	if (!pg)
  		goto nla_put_failure;
  
  	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  		struct nlattr *tc_nest = nla_nest_start(skb, i);
  
  		if (!tc_nest)
  			goto nla_put_failure;
  
  		pgid = DCB_ATTR_VALUE_UNDEFINED;
  		prio = DCB_ATTR_VALUE_UNDEFINED;
  		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  		up_map = DCB_ATTR_VALUE_UNDEFINED;
  
  		if (!dir)
  			ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
  					  &prio, &pgid, &tc_pct, &up_map);
  		else
  			ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
  					  &prio, &pgid, &tc_pct, &up_map);
  
  		NLA_PUT_U8(skb, DCB_TC_ATTR_PARAM_PGID, pgid);
  		NLA_PUT_U8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
  		NLA_PUT_U8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
  		NLA_PUT_U8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct);
  		nla_nest_end(skb, tc_nest);
  	}
  
  	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  
  		if (!dir)
  			ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
  					   &tc_pct);
  		else
  			ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
  					   &tc_pct);
  		NLA_PUT_U8(skb, i, tc_pct);
  	}
  	nla_nest_end(skb, pg);
  	return 0;
  
  nla_put_failure:
  	return -EMSGSIZE;
  }
  
  static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
  {
  	struct nlattr *cee, *app;
  	struct dcb_app_type *itr;
  	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  	int dcbx, i, err = -EMSGSIZE;
  	u8 value;
  
  	NLA_PUT_STRING(skb, DCB_ATTR_IFNAME, netdev->name);
  
  	cee = nla_nest_start(skb, DCB_ATTR_CEE);
  	if (!cee)
  		goto nla_put_failure;
  
  	/* local pg */
  	if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
  		err = dcbnl_cee_pg_fill(skb, netdev, 1);
  		if (err)
  			goto nla_put_failure;
  	}
  
  	if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
  		err = dcbnl_cee_pg_fill(skb, netdev, 0);
  		if (err)
  			goto nla_put_failure;
  	}
  
  	/* local pfc */
  	if (ops->getpfccfg) {
  		struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
  
  		if (!pfc_nest)
  			goto nla_put_failure;
  
  		for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  			ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
  			NLA_PUT_U8(skb, i, value);
  		}
  		nla_nest_end(skb, pfc_nest);
  	}
  
  	/* local app */
  	spin_lock(&dcb_lock);
  	app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
  	if (!app)
40f5d72a4   Dan Carpenter   dcbnl: unlock on ...
1381
  		goto dcb_unlock;
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1382
1383
  
  	list_for_each_entry(itr, &dcb_app_list, list) {
e290ed813   Mark Rustad   dcb: Use ifindex ...
1384
  		if (itr->ifindex == netdev->ifindex) {
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
  			struct nlattr *app_nest = nla_nest_start(skb,
  								 DCB_ATTR_APP);
  			if (!app_nest)
  				goto dcb_unlock;
  
  			err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
  					 itr->app.selector);
  			if (err)
  				goto dcb_unlock;
  
  			err = nla_put_u16(skb, DCB_APP_ATTR_ID,
  					  itr->app.protocol);
  			if (err)
  				goto dcb_unlock;
  
  			err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
  					 itr->app.priority);
  			if (err)
  				goto dcb_unlock;
  
  			nla_nest_end(skb, app_nest);
  		}
  	}
  	nla_nest_end(skb, app);
  
  	if (netdev->dcbnl_ops->getdcbx)
  		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  	else
  		dcbx = -EOPNOTSUPP;
  
  	spin_unlock(&dcb_lock);
  
  	/* features flags */
  	if (ops->getfeatcfg) {
  		struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
  		if (!feat)
  			goto nla_put_failure;
  
  		for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
  		     i++)
  			if (!ops->getfeatcfg(netdev, i, &value))
  				NLA_PUT_U8(skb, i, value);
  
  		nla_nest_end(skb, feat);
  	}
  
  	/* peer info if available */
  	if (ops->cee_peer_getpg) {
  		struct cee_pg pg;
  		err = ops->cee_peer_getpg(netdev, &pg);
  		if (!err)
  			NLA_PUT(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg);
  	}
  
  	if (ops->cee_peer_getpfc) {
  		struct cee_pfc pfc;
  		err = ops->cee_peer_getpfc(netdev, &pfc);
  		if (!err)
  			NLA_PUT(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc);
  	}
  
  	if (ops->peer_getappinfo && ops->peer_getapptable) {
  		err = dcbnl_build_peer_app(netdev, skb,
  					   DCB_ATTR_CEE_PEER_APP_TABLE,
  					   DCB_ATTR_CEE_PEER_APP_INFO,
  					   DCB_ATTR_CEE_PEER_APP);
  		if (err)
  			goto nla_put_failure;
  	}
  	nla_nest_end(skb, cee);
  
  	/* DCBX state */
  	if (dcbx >= 0) {
  		err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  		if (err)
  			goto nla_put_failure;
  	}
  	return 0;
  
  dcb_unlock:
  	spin_unlock(&dcb_lock);
  nla_put_failure:
  	return err;
  }
  
  static int dcbnl_notify(struct net_device *dev, int event, int cmd,
  			u32 seq, u32 pid, int dcbx_ver)
314b4778e   John Fastabend   net: dcbnl, add m...
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
  {
  	struct net *net = dev_net(dev);
  	struct sk_buff *skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  	int err;
  
  	if (!ops)
  		return -EOPNOTSUPP;
  
  	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!skb)
  		return -ENOBUFS;
  
  	nlh = nlmsg_put(skb, pid, 0, event, sizeof(*dcb), 0);
  	if (nlh == NULL) {
4d054f2f1   Dan Carpenter   dcb: use nlmsg_fr...
1489
  		nlmsg_free(skb);
314b4778e   John Fastabend   net: dcbnl, add m...
1490
1491
1492
1493
1494
1495
  		return -EMSGSIZE;
  	}
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = cmd;
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1496
1497
1498
1499
  	if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
  		err = dcbnl_ieee_fill(skb, dev);
  	else
  		err = dcbnl_cee_fill(skb, dev);
314b4778e   John Fastabend   net: dcbnl, add m...
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
  	if (err < 0) {
  		/* Report error to broadcast listeners */
  		nlmsg_cancel(skb, nlh);
  		kfree_skb(skb);
  		rtnl_set_sk_err(net, RTNLGRP_DCB, err);
  	} else {
  		/* End nlmsg and notify broadcast listeners */
  		nlmsg_end(skb, nlh);
  		rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
  	}
  
  	return err;
  }
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
  
  int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
  		      u32 seq, u32 pid)
  {
  	return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_IEEE);
  }
  EXPORT_SYMBOL(dcbnl_ieee_notify);
  
  int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
  		     u32 seq, u32 pid)
  {
  	return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_CEE);
  }
  EXPORT_SYMBOL(dcbnl_cee_notify);
314b4778e   John Fastabend   net: dcbnl, add m...
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
  
  /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
   * be completed the entire msg is aborted and error value is returned.
   * No attempt is made to reconcile the case where only part of the
   * cmd can be completed.
   */
  static int dcbnl_ieee_set(struct net_device *netdev, struct nlattr **tb,
  			  u32 pid, u32 seq, u16 flags)
  {
  	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  	int err = -EOPNOTSUPP;
  
  	if (!ops)
  		return err;
4003b6587   John Fastabend   dcb: Add missing ...
1542
1543
  	if (!tb[DCB_ATTR_IEEE])
  		return -EINVAL;
314b4778e   John Fastabend   net: dcbnl, add m...
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
  	err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  			       tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  	if (err)
  		return err;
  
  	if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  		struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  		err = ops->ieee_setets(netdev, ets);
  		if (err)
  			goto err;
  	}
  
  	if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
  		struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  		err = ops->ieee_setpfc(netdev, pfc);
  		if (err)
  			goto err;
  	}
  
  	if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  		struct nlattr *attr;
  		int rem;
  
  		nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  			struct dcb_app *app_data;
  			if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  				continue;
  			app_data = nla_data(attr);
  			if (ops->ieee_setapp)
  				err = ops->ieee_setapp(netdev, app_data);
  			else
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
1575
  				err = dcb_ieee_setapp(netdev, app_data);
314b4778e   John Fastabend   net: dcbnl, add m...
1576
1577
1578
1579
1580
1581
1582
1583
  			if (err)
  				goto err;
  		}
  	}
  
  err:
  	dcbnl_reply(err, RTM_SETDCB, DCB_CMD_IEEE_SET, DCB_ATTR_IEEE,
  		    pid, seq, flags);
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1584
  	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
314b4778e   John Fastabend   net: dcbnl, add m...
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
  	return err;
  }
  
  static int dcbnl_ieee_get(struct net_device *netdev, struct nlattr **tb,
  			  u32 pid, u32 seq, u16 flags)
  {
  	struct net *net = dev_net(netdev);
  	struct sk_buff *skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  	int err;
  
  	if (!ops)
  		return -EOPNOTSUPP;
  
  	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!skb)
  		return -ENOBUFS;
  
  	nlh = nlmsg_put(skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  	if (nlh == NULL) {
4d054f2f1   Dan Carpenter   dcb: use nlmsg_fr...
1607
  		nlmsg_free(skb);
314b4778e   John Fastabend   net: dcbnl, add m...
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
  		return -EMSGSIZE;
  	}
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_IEEE_GET;
  
  	err = dcbnl_ieee_fill(skb, netdev);
  
  	if (err < 0) {
  		nlmsg_cancel(skb, nlh);
  		kfree_skb(skb);
  	} else {
  		nlmsg_end(skb, nlh);
  		err = rtnl_unicast(skb, net, pid);
  	}
  
  	return err;
  }
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
  
  static int dcbnl_ieee_del(struct net_device *netdev, struct nlattr **tb,
  			  u32 pid, u32 seq, u16 flags)
  {
  	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  	int err = -EOPNOTSUPP;
  
  	if (!ops)
  		return -EOPNOTSUPP;
  
  	if (!tb[DCB_ATTR_IEEE])
  		return -EINVAL;
  
  	err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  			       tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  	if (err)
  		return err;
  
  	if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  		struct nlattr *attr;
  		int rem;
  
  		nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  			struct dcb_app *app_data;
  
  			if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  				continue;
  			app_data = nla_data(attr);
  			if (ops->ieee_delapp)
  				err = ops->ieee_delapp(netdev, app_data);
  			else
  				err = dcb_ieee_delapp(netdev, app_data);
  			if (err)
  				goto err;
  		}
  	}
  
  err:
  	dcbnl_reply(err, RTM_SETDCB, DCB_CMD_IEEE_DEL, DCB_ATTR_IEEE,
  		    pid, seq, flags);
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1668
  	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
1669
1670
  	return err;
  }
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
1671
1672
1673
1674
  /* DCBX configuration */
  static int dcbnl_getdcbx(struct net_device *netdev, struct nlattr **tb,
  			 u32 pid, u32 seq, u16 flags)
  {
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1675
  	int ret;
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
1676
1677
  
  	if (!netdev->dcbnl_ops->getdcbx)
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1678
  		return -EOPNOTSUPP;
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
  
  	ret = dcbnl_reply(netdev->dcbnl_ops->getdcbx(netdev), RTM_GETDCB,
  			  DCB_CMD_GDCBX, DCB_ATTR_DCBX, pid, seq, flags);
  
  	return ret;
  }
  
  static int dcbnl_setdcbx(struct net_device *netdev, struct nlattr **tb,
  			 u32 pid, u32 seq, u16 flags)
  {
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1689
  	int ret;
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
1690
  	u8 value;
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1691
1692
1693
1694
1695
  	if (!netdev->dcbnl_ops->setdcbx)
  		return -EOPNOTSUPP;
  
  	if (!tb[DCB_ATTR_DCBX])
  		return -EINVAL;
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
1696
1697
1698
1699
1700
1701
1702
1703
1704
  
  	value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  
  	ret = dcbnl_reply(netdev->dcbnl_ops->setdcbx(netdev, value),
  			  RTM_SETDCB, DCB_CMD_SDCBX, DCB_ATTR_DCBX,
  			  pid, seq, flags);
  
  	return ret;
  }
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1705
1706
1707
1708
1709
1710
1711
1712
  static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlattr **tb,
  			    u32 pid, u32 seq, u16 flags)
  {
  	struct sk_buff *dcbnl_skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
  	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  	u8 value;
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1713
  	int ret, i;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1714
  	int getall = 0;
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1715
1716
1717
1718
1719
  	if (!netdev->dcbnl_ops->getfeatcfg)
  		return -EOPNOTSUPP;
  
  	if (!tb[DCB_ATTR_FEATCFG])
  		return -EINVAL;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1720
1721
1722
  
  	ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  			       dcbnl_featcfg_nest);
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1723
  	if (ret)
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1724
  		goto err_out;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1725
1726
1727
  
  	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!dcbnl_skb) {
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1728
  		ret = -ENOBUFS;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
  		goto err_out;
  	}
  
  	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_GFEATCFG;
  
  	nest = nla_nest_start(dcbnl_skb, DCB_ATTR_FEATCFG);
  	if (!nest) {
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1740
1741
  		ret = -EMSGSIZE;
  		goto nla_put_failure;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
  	}
  
  	if (data[DCB_FEATCFG_ATTR_ALL])
  		getall = 1;
  
  	for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  		if (!getall && !data[i])
  			continue;
  
  		ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1752
  		if (!ret)
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1753
  			ret = nla_put_u8(dcbnl_skb, i, value);
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1754
1755
1756
1757
  		if (ret) {
  			nla_nest_cancel(dcbnl_skb, nest);
  			goto nla_put_failure;
  		}
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1758
1759
1760
1761
  	}
  	nla_nest_end(dcbnl_skb, nest);
  
  	nlmsg_end(dcbnl_skb, nlh);
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1762
1763
1764
  	return rtnl_unicast(dcbnl_skb, &init_net, pid);
  nla_put_failure:
  	nlmsg_cancel(dcbnl_skb, nlh);
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1765
  nlmsg_failure:
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1766
1767
1768
1769
1770
1771
1772
1773
1774
  	kfree_skb(dcbnl_skb);
  err_out:
  	return ret;
  }
  
  static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlattr **tb,
  			    u32 pid, u32 seq, u16 flags)
  {
  	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1775
  	int ret, i;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1776
  	u8 value;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1777

7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1778
1779
1780
1781
1782
  	if (!netdev->dcbnl_ops->setfeatcfg)
  		return -ENOTSUPP;
  
  	if (!tb[DCB_ATTR_FEATCFG])
  		return -EINVAL;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1783
1784
1785
  
  	ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  			       dcbnl_featcfg_nest);
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1786
  	if (ret)
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1787
  		goto err;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
  
  	for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  		if (data[i] == NULL)
  			continue;
  
  		value = nla_get_u8(data[i]);
  
  		ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  
  		if (ret)
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1798
  			goto err;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1799
  	}
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1800
  err:
7f891cf1f   Shmulik Ravid   dcbnl: more infor...
1801
1802
  	dcbnl_reply(ret, RTM_SETDCB, DCB_CMD_SFEATCFG, DCB_ATTR_FEATCFG,
  		    pid, seq, flags);
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1803
1804
  	return ret;
  }
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1805
1806
1807
1808
  /* Handle CEE DCBX GET commands. */
  static int dcbnl_cee_get(struct net_device *netdev, struct nlattr **tb,
  			 u32 pid, u32 seq, u16 flags)
  {
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1809
  	struct net *net = dev_net(netdev);
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1810
1811
1812
  	struct sk_buff *skb;
  	struct nlmsghdr *nlh;
  	struct dcbmsg *dcb;
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1813
  	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1814
  	int err;
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1815
1816
1817
1818
1819
1820
1821
  
  	if (!ops)
  		return -EOPNOTSUPP;
  
  	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  	if (!skb)
  		return -ENOBUFS;
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1822
1823
1824
1825
1826
  	nlh = nlmsg_put(skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  	if (nlh == NULL) {
  		nlmsg_free(skb);
  		return -EMSGSIZE;
  	}
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1827
1828
1829
1830
  
  	dcb = NLMSG_DATA(nlh);
  	dcb->dcb_family = AF_UNSPEC;
  	dcb->cmd = DCB_CMD_CEE_GET;
5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1831
  	err = dcbnl_cee_fill(skb, netdev);
37cf4d1a9   Shmulik Ravid   dcbnl: Aggregated...
1832

5b7f76267   Shmulik Ravid   dcbnl: Add CEE no...
1833
1834
1835
1836
1837
1838
  	if (err < 0) {
  		nlmsg_cancel(skb, nlh);
  		nlmsg_free(skb);
  	} else {
  		nlmsg_end(skb, nlh);
  		err = rtnl_unicast(skb, net, pid);
37cf4d1a9   Shmulik Ravid   dcbnl: Aggregated...
1839
  	}
37cf4d1a9   Shmulik Ravid   dcbnl: Aggregated...
1840
  	return err;
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1841
  }
2f90b8657   Alexander Duyck   ixgbe: this patch...
1842
1843
1844
1845
1846
1847
1848
1849
  static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  {
  	struct net *net = sock_net(skb->sk);
  	struct net_device *netdev;
  	struct dcbmsg  *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
  	struct nlattr *tb[DCB_ATTR_MAX + 1];
  	u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  	int ret = -EINVAL;
09ad9bc75   Octavian Purdila   net: use net_eq t...
1850
  	if (!net_eq(net, &init_net))
2f90b8657   Alexander Duyck   ixgbe: this patch...
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
  		return -EINVAL;
  
  	ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  			  dcbnl_rtnl_policy);
  	if (ret < 0)
  		return ret;
  
  	if (!tb[DCB_ATTR_IFNAME])
  		return -EINVAL;
  
  	netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
  	if (!netdev)
  		return -EINVAL;
  
  	if (!netdev->dcbnl_ops)
  		goto errout;
  
  	switch (dcb->cmd) {
  	case DCB_CMD_GSTATE:
  		ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
  		                     nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_PFC_GCFG:
  		ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                      nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_GPERM_HWADDR:
  		ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
  		                           nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_PGTX_GCFG:
  		ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                        nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_PGRX_GCFG:
  		ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                        nlh->nlmsg_flags);
  		goto out;
859ee3c43   Alexander Duyck   DCB: Add support ...
1889
1890
1891
1892
  	case DCB_CMD_BCN_GCFG:
  		ret = dcbnl_bcn_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                       nlh->nlmsg_flags);
  		goto out;
2f90b8657   Alexander Duyck   ixgbe: this patch...
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
  	case DCB_CMD_SSTATE:
  		ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
  		                     nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_PFC_SCFG:
  		ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                      nlh->nlmsg_flags);
  		goto out;
  
  	case DCB_CMD_SET_ALL:
  		ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
  		                   nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_PGTX_SCFG:
  		ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                        nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_PGRX_SCFG:
  		ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                        nlh->nlmsg_flags);
  		goto out;
46132188b   Alexander Duyck   DCB: Add interfac...
1914
1915
1916
1917
  	case DCB_CMD_GCAP:
  		ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
  		                   nlh->nlmsg_flags);
  		goto out;
33dbabc4a   Alexander Duyck   DCB: Add interfac...
1918
1919
1920
1921
1922
1923
1924
1925
  	case DCB_CMD_GNUMTCS:
  		ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
  		                      nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_SNUMTCS:
  		ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
  		                      nlh->nlmsg_flags);
  		goto out;
0eb3aa9ba   Alexander Duyck   DCB: Add interfac...
1926
1927
1928
1929
1930
1931
1932
1933
  	case DCB_CMD_PFC_GSTATE:
  		ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
  		                        nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_PFC_SSTATE:
  		ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
  		                        nlh->nlmsg_flags);
  		goto out;
859ee3c43   Alexander Duyck   DCB: Add support ...
1934
1935
1936
1937
  	case DCB_CMD_BCN_SCFG:
  		ret = dcbnl_bcn_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  		                       nlh->nlmsg_flags);
  		goto out;
579496865   Yi Zou   dcbnl: Add implem...
1938
1939
1940
1941
1942
1943
1944
1945
  	case DCB_CMD_GAPP:
  		ret = dcbnl_getapp(netdev, tb, pid, nlh->nlmsg_seq,
  		                   nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_SAPP:
  		ret = dcbnl_setapp(netdev, tb, pid, nlh->nlmsg_seq,
  		                   nlh->nlmsg_flags);
  		goto out;
3e29027af   John Fastabend   dcbnl: add suppor...
1946
1947
  	case DCB_CMD_IEEE_SET:
  		ret = dcbnl_ieee_set(netdev, tb, pid, nlh->nlmsg_seq,
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
1948
  				     nlh->nlmsg_flags);
3e29027af   John Fastabend   dcbnl: add suppor...
1949
1950
1951
  		goto out;
  	case DCB_CMD_IEEE_GET:
  		ret = dcbnl_ieee_get(netdev, tb, pid, nlh->nlmsg_seq,
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
1952
1953
1954
1955
1956
  				     nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_IEEE_DEL:
  		ret = dcbnl_ieee_del(netdev, tb, pid, nlh->nlmsg_seq,
  				     nlh->nlmsg_flags);
3e29027af   John Fastabend   dcbnl: add suppor...
1957
  		goto out;
6241b6259   Shmulik Ravid   dcbnl: adding DCB...
1958
1959
1960
1961
1962
1963
1964
1965
  	case DCB_CMD_GDCBX:
  		ret = dcbnl_getdcbx(netdev, tb, pid, nlh->nlmsg_seq,
  				    nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_SDCBX:
  		ret = dcbnl_setdcbx(netdev, tb, pid, nlh->nlmsg_seq,
  				    nlh->nlmsg_flags);
  		goto out;
ea45fe4e1   Shmulik Ravid   dcbnl: adding DCB...
1966
1967
1968
1969
1970
1971
1972
1973
  	case DCB_CMD_GFEATCFG:
  		ret = dcbnl_getfeatcfg(netdev, tb, pid, nlh->nlmsg_seq,
  				       nlh->nlmsg_flags);
  		goto out;
  	case DCB_CMD_SFEATCFG:
  		ret = dcbnl_setfeatcfg(netdev, tb, pid, nlh->nlmsg_seq,
  				       nlh->nlmsg_flags);
  		goto out;
dc6ed1df5   Shmulik Ravid   dcbnl: add suppor...
1974
1975
1976
1977
  	case DCB_CMD_CEE_GET:
  		ret = dcbnl_cee_get(netdev, tb, pid, nlh->nlmsg_seq,
  				    nlh->nlmsg_flags);
  		goto out;
2f90b8657   Alexander Duyck   ixgbe: this patch...
1978
1979
1980
1981
1982
1983
1984
1985
1986
  	default:
  		goto errout;
  	}
  errout:
  	ret = -EINVAL;
  out:
  	dev_put(netdev);
  	return ret;
  }
9ab933ab2   John Fastabend   dcbnl: add applic...
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
  /**
   * dcb_getapp - retrieve the DCBX application user priority
   *
   * On success returns a non-zero 802.1p user priority bitmap
   * otherwise returns 0 as the invalid user priority bitmap to
   * indicate an error.
   */
  u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  {
  	struct dcb_app_type *itr;
  	u8 prio = 0;
  
  	spin_lock(&dcb_lock);
  	list_for_each_entry(itr, &dcb_app_list, list) {
  		if (itr->app.selector == app->selector &&
  		    itr->app.protocol == app->protocol &&
e290ed813   Mark Rustad   dcb: Use ifindex ...
2003
  		    itr->ifindex == dev->ifindex) {
9ab933ab2   John Fastabend   dcbnl: add applic...
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
  			prio = itr->app.priority;
  			break;
  		}
  	}
  	spin_unlock(&dcb_lock);
  
  	return prio;
  }
  EXPORT_SYMBOL(dcb_getapp);
  
  /**
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2015
   * dcb_setapp - add CEE dcb application data to app list
9ab933ab2   John Fastabend   dcbnl: add applic...
2016
   *
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2017
2018
2019
   * Priority 0 is an invalid priority in CEE spec. This routine
   * removes applications from the app list if the priority is
   * set to zero.
9ab933ab2   John Fastabend   dcbnl: add applic...
2020
   */
ab6baf980   John Fastabend   dcb: fix return t...
2021
  int dcb_setapp(struct net_device *dev, struct dcb_app *new)
9ab933ab2   John Fastabend   dcbnl: add applic...
2022
2023
  {
  	struct dcb_app_type *itr;
7ec79270d   John Fastabend   net: dcb: applica...
2024
  	struct dcb_app_type event;
e290ed813   Mark Rustad   dcb: Use ifindex ...
2025
  	event.ifindex = dev->ifindex;
7ec79270d   John Fastabend   net: dcb: applica...
2026
  	memcpy(&event.app, new, sizeof(event.app));
6bd0e1cb1   John Fastabend   dcb: add DCBX mod...
2027
2028
  	if (dev->dcbnl_ops->getdcbx)
  		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
9ab933ab2   John Fastabend   dcbnl: add applic...
2029
2030
2031
2032
2033
2034
  
  	spin_lock(&dcb_lock);
  	/* Search for existing match and replace */
  	list_for_each_entry(itr, &dcb_app_list, list) {
  		if (itr->app.selector == new->selector &&
  		    itr->app.protocol == new->protocol &&
e290ed813   Mark Rustad   dcb: Use ifindex ...
2035
  		    itr->ifindex == dev->ifindex) {
9ab933ab2   John Fastabend   dcbnl: add applic...
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
  			if (new->priority)
  				itr->app.priority = new->priority;
  			else {
  				list_del(&itr->list);
  				kfree(itr);
  			}
  			goto out;
  		}
  	}
  	/* App type does not exist add new application type */
  	if (new->priority) {
  		struct dcb_app_type *entry;
  		entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
  		if (!entry) {
  			spin_unlock(&dcb_lock);
  			return -ENOMEM;
  		}
  
  		memcpy(&entry->app, new, sizeof(*new));
e290ed813   Mark Rustad   dcb: Use ifindex ...
2055
  		entry->ifindex = dev->ifindex;
9ab933ab2   John Fastabend   dcbnl: add applic...
2056
2057
2058
2059
  		list_add(&entry->list, &dcb_app_list);
  	}
  out:
  	spin_unlock(&dcb_lock);
7ec79270d   John Fastabend   net: dcb: applica...
2060
  	call_dcbevent_notifiers(DCB_APP_EVENT, &event);
9ab933ab2   John Fastabend   dcbnl: add applic...
2061
2062
2063
  	return 0;
  }
  EXPORT_SYMBOL(dcb_setapp);
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2064
  /**
a364c8cf8   John Fastabend   dcb: Add dcb_ieee...
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
   * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
   *
   * Helper routine which on success returns a non-zero 802.1Qaz user
   * priority bitmap otherwise returns 0 to indicate the dcb_app was
   * not found in APP list.
   */
  u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
  {
  	struct dcb_app_type *itr;
  	u8 prio = 0;
  
  	spin_lock(&dcb_lock);
  	list_for_each_entry(itr, &dcb_app_list, list) {
  		if (itr->app.selector == app->selector &&
  		    itr->app.protocol == app->protocol &&
e290ed813   Mark Rustad   dcb: Use ifindex ...
2080
  		    itr->ifindex == dev->ifindex) {
a364c8cf8   John Fastabend   dcb: Add dcb_ieee...
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
  			prio |= 1 << itr->app.priority;
  		}
  	}
  	spin_unlock(&dcb_lock);
  
  	return prio;
  }
  EXPORT_SYMBOL(dcb_ieee_getapp_mask);
  
  /**
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
   * dcb_ieee_setapp - add IEEE dcb application data to app list
   *
   * This adds Application data to the list. Multiple application
   * entries may exists for the same selector and protocol as long
   * as the priorities are different.
   */
  int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
  {
  	struct dcb_app_type *itr, *entry;
  	struct dcb_app_type event;
  	int err = 0;
e290ed813   Mark Rustad   dcb: Use ifindex ...
2102
  	event.ifindex = dev->ifindex;
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2103
  	memcpy(&event.app, new, sizeof(event.app));
6bd0e1cb1   John Fastabend   dcb: add DCBX mod...
2104
2105
  	if (dev->dcbnl_ops->getdcbx)
  		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2106
2107
2108
2109
2110
2111
2112
  
  	spin_lock(&dcb_lock);
  	/* Search for existing match and abort if found */
  	list_for_each_entry(itr, &dcb_app_list, list) {
  		if (itr->app.selector == new->selector &&
  		    itr->app.protocol == new->protocol &&
  		    itr->app.priority == new->priority &&
e290ed813   Mark Rustad   dcb: Use ifindex ...
2113
  		    itr->ifindex == dev->ifindex) {
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
  			err = -EEXIST;
  			goto out;
  		}
  	}
  
  	/* App entry does not exist add new entry */
  	entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
  	if (!entry) {
  		err = -ENOMEM;
  		goto out;
  	}
  
  	memcpy(&entry->app, new, sizeof(*new));
e290ed813   Mark Rustad   dcb: Use ifindex ...
2127
  	entry->ifindex = dev->ifindex;
b6db2174c   John Fastabend   dcb: Add ieee_dcb...
2128
2129
2130
2131
2132
2133
2134
2135
  	list_add(&entry->list, &dcb_app_list);
  out:
  	spin_unlock(&dcb_lock);
  	if (!err)
  		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  	return err;
  }
  EXPORT_SYMBOL(dcb_ieee_setapp);
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
  /**
   * dcb_ieee_delapp - delete IEEE dcb application data from list
   *
   * This removes a matching APP data from the APP list
   */
  int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
  {
  	struct dcb_app_type *itr;
  	struct dcb_app_type event;
  	int err = -ENOENT;
e290ed813   Mark Rustad   dcb: Use ifindex ...
2146
  	event.ifindex = dev->ifindex;
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
2147
  	memcpy(&event.app, del, sizeof(event.app));
6bd0e1cb1   John Fastabend   dcb: add DCBX mod...
2148
2149
  	if (dev->dcbnl_ops->getdcbx)
  		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
2150
2151
2152
2153
2154
2155
2156
  
  	spin_lock(&dcb_lock);
  	/* Search for existing match and remove it. */
  	list_for_each_entry(itr, &dcb_app_list, list) {
  		if (itr->app.selector == del->selector &&
  		    itr->app.protocol == del->protocol &&
  		    itr->app.priority == del->priority &&
e290ed813   Mark Rustad   dcb: Use ifindex ...
2157
  		    itr->ifindex == dev->ifindex) {
f9ae7e4b5   John Fastabend   dcb: Add ieee_dcb...
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
  			list_del(&itr->list);
  			kfree(itr);
  			err = 0;
  			goto out;
  		}
  	}
  
  out:
  	spin_unlock(&dcb_lock);
  	if (!err)
  		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  	return err;
  }
  EXPORT_SYMBOL(dcb_ieee_delapp);
7c14c3f10   Shmulik Ravid   dcbnl: cleanup
2172
  static void dcb_flushapp(void)
9ab933ab2   John Fastabend   dcbnl: add applic...
2173
2174
  {
  	struct dcb_app_type *app;
2a8fe0037   Dan Carpenter   dcb: use after fr...
2175
  	struct dcb_app_type *tmp;
9ab933ab2   John Fastabend   dcbnl: add applic...
2176
2177
  
  	spin_lock(&dcb_lock);
2a8fe0037   Dan Carpenter   dcb: use after fr...
2178
  	list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
9ab933ab2   John Fastabend   dcbnl: add applic...
2179
2180
2181
2182
2183
  		list_del(&app->list);
  		kfree(app);
  	}
  	spin_unlock(&dcb_lock);
  }
2f90b8657   Alexander Duyck   ixgbe: this patch...
2184
2185
  static int __init dcbnl_init(void)
  {
9ab933ab2   John Fastabend   dcbnl: add applic...
2186
  	INIT_LIST_HEAD(&dcb_app_list);
c7ac8679b   Greg Rose   rtnetlink: Comput...
2187
2188
  	rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, NULL);
  	rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, NULL);
2f90b8657   Alexander Duyck   ixgbe: this patch...
2189
2190
2191
2192
2193
2194
2195
2196
2197
  
  	return 0;
  }
  module_init(dcbnl_init);
  
  static void __exit dcbnl_exit(void)
  {
  	rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
  	rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
9ab933ab2   John Fastabend   dcbnl: add applic...
2198
  	dcb_flushapp();
2f90b8657   Alexander Duyck   ixgbe: this patch...
2199
2200
  }
  module_exit(dcbnl_exit);