dcbnl.c 52.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
M
Mark Rustad 已提交
3
 * Copyright (c) 2008-2011, Intel Corporation.
4
 *
5
 * Description: Data Center Bridging netlink interface
6 7 8 9 10
 * Author: Lucy Liu <lucy.liu@intel.com>
 */

#include <linux/netdevice.h>
#include <linux/netlink.h>
11
#include <linux/slab.h>
12 13 14
#include <net/netlink.h>
#include <net/rtnetlink.h>
#include <linux/dcbnl.h>
15
#include <net/dcbevent.h>
16
#include <linux/rtnetlink.h>
17
#include <linux/init.h>
18 19
#include <net/sock.h>

20
/* Data Center Bridging (DCB) is a collection of Ethernet enhancements
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * 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.
 */

/**************** DCB attribute policies *************************************/

/* DCB netlink attributes policy */
A
Alexey Dobriyan 已提交
44
static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
A
Alexander Duyck 已提交
45 46 47 48 49
	[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},
50
	[DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
A
Alexander Duyck 已提交
51 52 53
	[DCB_ATTR_CAP]         = {.type = NLA_NESTED},
	[DCB_ATTR_PFC_STATE]   = {.type = NLA_U8},
	[DCB_ATTR_BCN]         = {.type = NLA_NESTED},
54
	[DCB_ATTR_APP]         = {.type = NLA_NESTED},
55
	[DCB_ATTR_IEEE]	       = {.type = NLA_NESTED},
56
	[DCB_ATTR_DCBX]        = {.type = NLA_U8},
57
	[DCB_ATTR_FEATCFG]     = {.type = NLA_NESTED},
58 59 60
};

/* DCB priority flow control to User Priority nested attributes */
A
Alexey Dobriyan 已提交
61
static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
62 63 64 65 66 67 68 69 70 71 72 73
	[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 */
A
Alexey Dobriyan 已提交
74
static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	[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. */
A
Alexey Dobriyan 已提交
96
static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
97 98 99 100 101 102 103
	[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},
};

104
/* DCB capabilities nested attributes. */
A
Alexey Dobriyan 已提交
105
static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
106 107 108 109 110 111 112 113
	[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},
114
	[DCB_CAP_ATTR_DCBX]    = {.type = NLA_U8},
115
};
116

117
/* DCB capabilities nested attributes. */
A
Alexey Dobriyan 已提交
118
static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
119 120 121 122 123
	[DCB_NUMTCS_ATTR_ALL]     = {.type = NLA_FLAG},
	[DCB_NUMTCS_ATTR_PG]      = {.type = NLA_U8},
	[DCB_NUMTCS_ATTR_PFC]     = {.type = NLA_U8},
};

A
Alexander Duyck 已提交
124
/* DCB BCN nested attributes. */
A
Alexey Dobriyan 已提交
125
static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
A
Alexander Duyck 已提交
126 127 128 129 130 131 132 133 134
	[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},
135 136
	[DCB_BCN_ATTR_BCNA_0]       = {.type = NLA_U32},
	[DCB_BCN_ATTR_BCNA_1]       = {.type = NLA_U32},
A
Alexander Duyck 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	[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},
};

153
/* DCB APP nested attributes. */
A
Alexey Dobriyan 已提交
154
static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
155 156 157 158 159
	[DCB_APP_ATTR_IDTYPE]       = {.type = NLA_U8},
	[DCB_APP_ATTR_ID]           = {.type = NLA_U16},
	[DCB_APP_ATTR_PRIORITY]     = {.type = NLA_U8},
};

160 161 162 163 164
/* 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},
165
	[DCB_ATTR_IEEE_MAXRATE]   = {.len = sizeof(struct ieee_maxrate)},
S
Shani Michaeli 已提交
166 167
	[DCB_ATTR_IEEE_QCN]         = {.len = sizeof(struct ieee_qcn)},
	[DCB_ATTR_IEEE_QCN_STATS]   = {.len = sizeof(struct ieee_qcn_stats)},
H
Huy Nguyen 已提交
168
	[DCB_ATTR_DCB_BUFFER]       = {.len = sizeof(struct dcbnl_buffer)},
169 170
};

171 172 173 174 175 176 177 178
/* 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},
};

179 180 181
static LIST_HEAD(dcb_app_list);
static DEFINE_SPINLOCK(dcb_lock);

182 183 184 185 186 187 188 189 190 191 192 193
static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
				    u32 flags, struct nlmsghdr **nlhp)
{
	struct sk_buff *skb;
	struct dcbmsg *dcb;
	struct nlmsghdr *nlh;

	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!skb)
		return NULL;

	nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
194
	BUG_ON(!nlh);
195 196 197 198 199 200 201 202 203 204 205 206

	dcb = nlmsg_data(nlh);
	dcb->dcb_family = AF_UNSPEC;
	dcb->cmd = cmd;
	dcb->dcb_pad = 0;

	if (nlhp)
		*nlhp = nlh;

	return skb;
}

207 208
static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
209 210 211
{
	/* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
	if (!netdev->dcbnl_ops->getstate)
212
		return -EOPNOTSUPP;
213

214 215
	return nla_put_u8(skb, DCB_ATTR_STATE,
			  netdev->dcbnl_ops->getstate(netdev));
216 217
}

218 219
static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
220 221 222
{
	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
	u8 value;
223
	int ret;
224 225 226
	int i;
	int getall = 0;

227 228 229 230 231
	if (!tb[DCB_ATTR_PFC_CFG])
		return -EINVAL;

	if (!netdev->dcbnl_ops->getpfccfg)
		return -EOPNOTSUPP;
232

233 234 235
	ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
					  tb[DCB_ATTR_PFC_CFG],
					  dcbnl_pfc_up_nest, NULL);
236
	if (ret)
237
		return ret;
238

239
	nest = nla_nest_start_noflag(skb, DCB_ATTR_PFC_CFG);
240
	if (!nest)
241
		return -EMSGSIZE;
242 243 244 245 246 247 248 249 250 251

	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);
252
		ret = nla_put_u8(skb, i, value);
253
		if (ret) {
254
			nla_nest_cancel(skb, nest);
255
			return ret;
256 257
		}
	}
258
	nla_nest_end(skb, nest);
259 260 261 262

	return 0;
}

263 264
static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
				u32 seq, struct nlattr **tb, struct sk_buff *skb)
265 266 267 268
{
	u8 perm_addr[MAX_ADDR_LEN];

	if (!netdev->dcbnl_ops->getpermhwaddr)
269
		return -EOPNOTSUPP;
270

271
	memset(perm_addr, 0, sizeof(perm_addr));
272 273
	netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);

274
	return nla_put(skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr), perm_addr);
275 276
}

277 278
static int dcbnl_getcap(struct net_device *netdev, struct nlmsghdr *nlh,
			u32 seq, struct nlattr **tb, struct sk_buff *skb)
279 280 281
{
	struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
	u8 value;
282
	int ret;
283 284 285
	int i;
	int getall = 0;

286 287 288 289 290
	if (!tb[DCB_ATTR_CAP])
		return -EINVAL;

	if (!netdev->dcbnl_ops->getcap)
		return -EOPNOTSUPP;
291

292 293 294
	ret = nla_parse_nested_deprecated(data, DCB_CAP_ATTR_MAX,
					  tb[DCB_ATTR_CAP], dcbnl_cap_nest,
					  NULL);
295
	if (ret)
296
		return ret;
297

298
	nest = nla_nest_start_noflag(skb, DCB_ATTR_CAP);
299
	if (!nest)
300
		return -EMSGSIZE;
301 302 303 304 305 306 307 308 309

	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)) {
310
			ret = nla_put_u8(skb, i, value);
311
			if (ret) {
312
				nla_nest_cancel(skb, nest);
313
				return ret;
314 315 316
			}
		}
	}
317
	nla_nest_end(skb, nest);
318 319 320 321

	return 0;
}

322 323
static int dcbnl_getnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
324 325 326
{
	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
	u8 value;
327
	int ret;
328 329 330
	int i;
	int getall = 0;

331 332 333 334 335
	if (!tb[DCB_ATTR_NUMTCS])
		return -EINVAL;

	if (!netdev->dcbnl_ops->getnumtcs)
		return -EOPNOTSUPP;
336

337 338 339
	ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
					  tb[DCB_ATTR_NUMTCS],
					  dcbnl_numtcs_nest, NULL);
340 341
	if (ret)
		return ret;
342

343
	nest = nla_nest_start_noflag(skb, DCB_ATTR_NUMTCS);
344 345
	if (!nest)
		return -EMSGSIZE;
346 347 348 349 350 351 352 353 354 355

	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) {
356
			ret = nla_put_u8(skb, i, value);
357
			if (ret) {
358
				nla_nest_cancel(skb, nest);
359
				return ret;
360
			}
361 362
		} else
			return -EINVAL;
363
	}
364
	nla_nest_end(skb, nest);
365 366 367 368

	return 0;
}

369 370
static int dcbnl_setnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
371 372
{
	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
373
	int ret;
374 375 376
	u8 value;
	int i;

377 378 379 380 381
	if (!tb[DCB_ATTR_NUMTCS])
		return -EINVAL;

	if (!netdev->dcbnl_ops->setnumtcs)
		return -EOPNOTSUPP;
382

383 384 385
	ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
					  tb[DCB_ATTR_NUMTCS],
					  dcbnl_numtcs_nest, NULL);
386
	if (ret)
387
		return ret;
388 389 390 391 392 393 394 395 396

	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)
397
			break;
398 399
	}

400
	return nla_put_u8(skb, DCB_ATTR_NUMTCS, !!ret);
401 402
}

403 404
static int dcbnl_getpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
405 406
{
	if (!netdev->dcbnl_ops->getpfcstate)
407
		return -EOPNOTSUPP;
408

409 410
	return nla_put_u8(skb, DCB_ATTR_PFC_STATE,
			  netdev->dcbnl_ops->getpfcstate(netdev));
411 412
}

413 414
static int dcbnl_setpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
415 416 417
{
	u8 value;

418
	if (!tb[DCB_ATTR_PFC_STATE])
419
		return -EINVAL;
420

421 422 423
	if (!netdev->dcbnl_ops->setpfcstate)
		return -EOPNOTSUPP;

424 425 426 427
	value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);

	netdev->dcbnl_ops->setpfcstate(netdev, value);

428
	return nla_put_u8(skb, DCB_ATTR_PFC_STATE, 0);
429 430
}

431 432
static int dcbnl_getapp(struct net_device *netdev, struct nlmsghdr *nlh,
			u32 seq, struct nlattr **tb, struct sk_buff *skb)
433 434 435 436 437
{
	struct nlattr *app_nest;
	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
	u16 id;
	u8 up, idtype;
438
	int ret;
439

440
	if (!tb[DCB_ATTR_APP])
441
		return -EINVAL;
442

443 444 445
	ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
					  tb[DCB_ATTR_APP], dcbnl_app_nest,
					  NULL);
446
	if (ret)
447
		return ret;
448 449 450 451

	/* all must be non-null */
	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
	    (!app_tb[DCB_APP_ATTR_ID]))
452
		return -EINVAL;
453 454 455 456 457

	/* 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))
458
		return -EINVAL;
459 460

	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
461 462

	if (netdev->dcbnl_ops->getapp) {
463 464 465 466 467
		ret = netdev->dcbnl_ops->getapp(netdev, idtype, id);
		if (ret < 0)
			return ret;
		else
			up = ret;
468 469 470 471 472 473 474
	} else {
		struct dcb_app app = {
					.selector = idtype,
					.protocol = id,
				     };
		up = dcb_getapp(netdev, &app);
	}
475

476
	app_nest = nla_nest_start_noflag(skb, DCB_ATTR_APP);
477
	if (!app_nest)
478
		return -EMSGSIZE;
479

480
	ret = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE, idtype);
481 482 483
	if (ret)
		goto out_cancel;

484
	ret = nla_put_u16(skb, DCB_APP_ATTR_ID, id);
485 486 487
	if (ret)
		goto out_cancel;

488
	ret = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY, up);
489 490 491
	if (ret)
		goto out_cancel;

492
	nla_nest_end(skb, app_nest);
493

494
	return 0;
495 496

out_cancel:
497
	nla_nest_cancel(skb, app_nest);
498 499 500
	return ret;
}

501 502
static int dcbnl_setapp(struct net_device *netdev, struct nlmsghdr *nlh,
			u32 seq, struct nlattr **tb, struct sk_buff *skb)
503
{
504
	int ret;
505 506 507 508
	u16 id;
	u8 up, idtype;
	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];

509
	if (!tb[DCB_ATTR_APP])
510
		return -EINVAL;
511

512 513 514
	ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
					  tb[DCB_ATTR_APP], dcbnl_app_nest,
					  NULL);
515
	if (ret)
516
		return ret;
517 518 519 520 521

	/* all must be non-null */
	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
	    (!app_tb[DCB_APP_ATTR_ID]) ||
	    (!app_tb[DCB_APP_ATTR_PRIORITY]))
522
		return -EINVAL;
523 524 525 526 527

	/* 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))
528
		return -EINVAL;
529 530 531 532

	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
	up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);

533
	if (netdev->dcbnl_ops->setapp) {
534
		ret = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
535 536
		if (ret < 0)
			return ret;
537 538 539 540 541
	} else {
		struct dcb_app app;
		app.selector = idtype;
		app.protocol = id;
		app.priority = up;
542
		ret = dcb_setapp(netdev, &app);
543 544
	}

545
	ret = nla_put_u8(skb, DCB_ATTR_APP, ret);
J
John Fastabend 已提交
546
	dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SAPP, seq, 0);
547

548 549 550
	return ret;
}

551 552
static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			     struct nlattr **tb, struct sk_buff *skb, int dir)
553 554 555 556 557
{
	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;
558
	int ret;
559 560 561
	int getall = 0;
	int i;

562 563 564 565
	if (!tb[DCB_ATTR_PG_CFG])
		return -EINVAL;

	if (!netdev->dcbnl_ops->getpgtccfgtx ||
566 567 568
	    !netdev->dcbnl_ops->getpgtccfgrx ||
	    !netdev->dcbnl_ops->getpgbwgcfgtx ||
	    !netdev->dcbnl_ops->getpgbwgcfgrx)
569
		return -EOPNOTSUPP;
570

571 572 573
	ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
					  tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
					  NULL);
574
	if (ret)
575
		return ret;
576

577
	pg_nest = nla_nest_start_noflag(skb, DCB_ATTR_PG_CFG);
578
	if (!pg_nest)
579
		return -EMSGSIZE;
580 581 582 583 584 585 586 587 588 589 590 591

	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];
592 593 594
		ret = nla_parse_nested_deprecated(param_tb,
						  DCB_TC_ATTR_PARAM_MAX, data,
						  dcbnl_tc_param_nest, NULL);
595 596 597
		if (ret)
			goto err_pg;

598
		param_nest = nla_nest_start_noflag(skb, i);
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
		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]) {
621
			ret = nla_put_u8(skb,
622 623 624 625 626 627
			                 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]) {
628
			ret = nla_put_u8(skb,
629 630 631 632 633 634
			                 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]) {
635
			ret = nla_put_u8(skb,
636 637 638 639 640 641
			                 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]) {
642
			ret = nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT,
643 644 645 646
			                 tc_pct);
			if (ret)
				goto err_param;
		}
647
		nla_nest_end(skb, param_nest);
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	}

	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);
		}
670
		ret = nla_put_u8(skb, i, tc_pct);
671 672 673 674
		if (ret)
			goto err_pg;
	}

675
	nla_nest_end(skb, pg_nest);
676 677 678 679

	return 0;

err_param:
680
	nla_nest_cancel(skb, param_nest);
681
err_pg:
682
	nla_nest_cancel(skb, pg_nest);
683 684

	return -EMSGSIZE;
685 686
}

687 688
static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
689
{
690
	return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 0);
691 692
}

693 694
static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
695
{
696
	return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 1);
697 698
}

699 700
static int dcbnl_setstate(struct net_device *netdev, struct nlmsghdr *nlh,
			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
701 702 703
{
	u8 value;

704
	if (!tb[DCB_ATTR_STATE])
705
		return -EINVAL;
706

707 708 709
	if (!netdev->dcbnl_ops->setstate)
		return -EOPNOTSUPP;

710 711
	value = nla_get_u8(tb[DCB_ATTR_STATE]);

712 713
	return nla_put_u8(skb, DCB_ATTR_STATE,
			  netdev->dcbnl_ops->setstate(netdev, value));
714 715
}

716 717
static int dcbnl_setpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
718 719 720
{
	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
	int i;
721
	int ret;
722 723
	u8 value;

724 725 726 727 728
	if (!tb[DCB_ATTR_PFC_CFG])
		return -EINVAL;

	if (!netdev->dcbnl_ops->setpfccfg)
		return -EOPNOTSUPP;
729

730 731 732
	ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
					  tb[DCB_ATTR_PFC_CFG],
					  dcbnl_pfc_up_nest, NULL);
733
	if (ret)
734
		return ret;
735 736 737 738 739 740 741 742 743

	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);
	}

744
	return nla_put_u8(skb, DCB_ATTR_PFC_CFG, 0);
745 746
}

747 748
static int dcbnl_setall(struct net_device *netdev, struct nlmsghdr *nlh,
			u32 seq, struct nlattr **tb, struct sk_buff *skb)
749
{
750
	int ret;
751

752 753 754 755 756
	if (!tb[DCB_ATTR_SET_ALL])
		return -EINVAL;

	if (!netdev->dcbnl_ops->setall)
		return -EOPNOTSUPP;
757

758 759
	ret = nla_put_u8(skb, DCB_ATTR_SET_ALL,
			 netdev->dcbnl_ops->setall(netdev));
J
John Fastabend 已提交
760
	dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SET_ALL, seq, 0);
761 762 763 764

	return ret;
}

765 766 767
static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			     u32 seq, struct nlattr **tb, struct sk_buff *skb,
			     int dir)
768 769 770
{
	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
771
	int ret;
772 773 774 775 776 777
	int i;
	u8 pgid;
	u8 up_map;
	u8 prio;
	u8 tc_pct;

778 779 780 781
	if (!tb[DCB_ATTR_PG_CFG])
		return -EINVAL;

	if (!netdev->dcbnl_ops->setpgtccfgtx ||
782 783 784
	    !netdev->dcbnl_ops->setpgtccfgrx ||
	    !netdev->dcbnl_ops->setpgbwgcfgtx ||
	    !netdev->dcbnl_ops->setpgbwgcfgrx)
785
		return -EOPNOTSUPP;
786

787 788 789
	ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
					  tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
					  NULL);
790
	if (ret)
791
		return ret;
792 793 794 795 796

	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
		if (!pg_tb[i])
			continue;

797 798 799 800
		ret = nla_parse_nested_deprecated(param_tb,
						  DCB_TC_ATTR_PARAM_MAX,
						  pg_tb[i],
						  dcbnl_tc_param_nest, NULL);
801
		if (ret)
802
			return ret;
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 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

		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);
		}
	}

855
	return nla_put_u8(skb, DCB_ATTR_PG_CFG, 0);
856 857
}

858 859
static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
860
{
861
	return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
862 863
}

864 865
static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
866
{
867
	return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
868 869
}

870 871
static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
A
Alexander Duyck 已提交
872 873 874 875 876
{
	struct nlattr *bcn_nest;
	struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
	u8 value_byte;
	u32 value_integer;
877
	int ret;
A
Alexander Duyck 已提交
878 879 880
	bool getall = false;
	int i;

881 882 883 884
	if (!tb[DCB_ATTR_BCN])
		return -EINVAL;

	if (!netdev->dcbnl_ops->getbcnrp ||
A
Alexander Duyck 已提交
885
	    !netdev->dcbnl_ops->getbcncfg)
886
		return -EOPNOTSUPP;
A
Alexander Duyck 已提交
887

888 889 890
	ret = nla_parse_nested_deprecated(bcn_tb, DCB_BCN_ATTR_MAX,
					  tb[DCB_ATTR_BCN], dcbnl_bcn_nest,
					  NULL);
A
Alexander Duyck 已提交
891
	if (ret)
892
		return ret;
A
Alexander Duyck 已提交
893

894
	bcn_nest = nla_nest_start_noflag(skb, DCB_ATTR_BCN);
A
Alexander Duyck 已提交
895
	if (!bcn_nest)
896
		return -EMSGSIZE;
A
Alexander Duyck 已提交
897 898 899 900 901 902 903 904 905 906

	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);
907
		ret = nla_put_u8(skb, i, value_byte);
A
Alexander Duyck 已提交
908 909 910 911
		if (ret)
			goto err_bcn;
	}

912
	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
A
Alexander Duyck 已提交
913 914 915 916 917
		if (!getall && !bcn_tb[i])
			continue;

		netdev->dcbnl_ops->getbcncfg(netdev, i,
		                             &value_integer);
918
		ret = nla_put_u32(skb, i, value_integer);
A
Alexander Duyck 已提交
919 920 921 922
		if (ret)
			goto err_bcn;
	}

923
	nla_nest_end(skb, bcn_nest);
A
Alexander Duyck 已提交
924 925 926 927

	return 0;

err_bcn:
928
	nla_nest_cancel(skb, bcn_nest);
A
Alexander Duyck 已提交
929 930 931
	return ret;
}

932 933
static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
A
Alexander Duyck 已提交
934 935 936
{
	struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
	int i;
937
	int ret;
A
Alexander Duyck 已提交
938 939 940
	u8 value_byte;
	u32 value_int;

941 942 943 944
	if (!tb[DCB_ATTR_BCN])
		return -EINVAL;

	if (!netdev->dcbnl_ops->setbcncfg ||
945
	    !netdev->dcbnl_ops->setbcnrp)
946
		return -EOPNOTSUPP;
A
Alexander Duyck 已提交
947

948 949 950
	ret = nla_parse_nested_deprecated(data, DCB_BCN_ATTR_MAX,
					  tb[DCB_ATTR_BCN], dcbnl_pfc_up_nest,
					  NULL);
A
Alexander Duyck 已提交
951
	if (ret)
952
		return ret;
A
Alexander Duyck 已提交
953 954 955 956 957 958 959 960 961

	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);
	}

962
	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
A
Alexander Duyck 已提交
963 964 965 966 967 968 969
		if (data[i] == NULL)
			continue;
		value_int = nla_get_u32(data[i]);
		netdev->dcbnl_ops->setbcncfg(netdev,
	                                     i, value_int);
	}

970
	return nla_put_u8(skb, DCB_ATTR_BCN, 0);
A
Alexander Duyck 已提交
971 972
}

973 974 975
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)
976 977 978 979 980 981 982 983 984 985 986 987 988 989
{
	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) {
990 991
		table = kmalloc_array(app_count, sizeof(struct dcb_app),
				      GFP_KERNEL);
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
		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;

1008
		app = nla_nest_start_noflag(skb, app_nested_type);
1009 1010 1011
		if (!app)
			goto nla_put_failure;

D
David S. Miller 已提交
1012 1013 1014
		if (app_info_type &&
		    nla_put(skb, app_info_type, sizeof(info), &info))
			goto nla_put_failure;
1015

D
David S. Miller 已提交
1016 1017 1018 1019 1020
		for (i = 0; i < app_count; i++) {
			if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
				    &table[i]))
				goto nla_put_failure;
		}
1021 1022 1023 1024 1025 1026 1027 1028
		nla_nest_end(skb, app);
	}
	err = 0;

nla_put_failure:
	kfree(table);
	return err;
}
1029

S
Shani Michaeli 已提交
1030
/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb GET commands. */
1031
static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
1032
{
1033 1034
	struct nlattr *ieee, *app;
	struct dcb_app_type *itr;
1035
	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1036
	int dcbx;
1037
	int err;
1038

D
David S. Miller 已提交
1039
	if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
1040 1041
		return -EMSGSIZE;

1042
	ieee = nla_nest_start_noflag(skb, DCB_ATTR_IEEE);
1043
	if (!ieee)
1044
		return -EMSGSIZE;
1045 1046 1047

	if (ops->ieee_getets) {
		struct ieee_ets ets;
1048
		memset(&ets, 0, sizeof(ets));
1049
		err = ops->ieee_getets(netdev, &ets);
D
David S. Miller 已提交
1050 1051
		if (!err &&
		    nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
1052
			return -EMSGSIZE;
1053 1054
	}

1055 1056
	if (ops->ieee_getmaxrate) {
		struct ieee_maxrate maxrate;
1057
		memset(&maxrate, 0, sizeof(maxrate));
1058 1059 1060 1061 1062
		err = ops->ieee_getmaxrate(netdev, &maxrate);
		if (!err) {
			err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
				      sizeof(maxrate), &maxrate);
			if (err)
1063
				return -EMSGSIZE;
1064 1065 1066
		}
	}

S
Shani Michaeli 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
	if (ops->ieee_getqcn) {
		struct ieee_qcn qcn;

		memset(&qcn, 0, sizeof(qcn));
		err = ops->ieee_getqcn(netdev, &qcn);
		if (!err) {
			err = nla_put(skb, DCB_ATTR_IEEE_QCN,
				      sizeof(qcn), &qcn);
			if (err)
				return -EMSGSIZE;
		}
	}

	if (ops->ieee_getqcnstats) {
		struct ieee_qcn_stats qcn_stats;

		memset(&qcn_stats, 0, sizeof(qcn_stats));
		err = ops->ieee_getqcnstats(netdev, &qcn_stats);
		if (!err) {
			err = nla_put(skb, DCB_ATTR_IEEE_QCN_STATS,
				      sizeof(qcn_stats), &qcn_stats);
			if (err)
				return -EMSGSIZE;
		}
	}

1093 1094
	if (ops->ieee_getpfc) {
		struct ieee_pfc pfc;
1095
		memset(&pfc, 0, sizeof(pfc));
1096
		err = ops->ieee_getpfc(netdev, &pfc);
D
David S. Miller 已提交
1097 1098
		if (!err &&
		    nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
1099
			return -EMSGSIZE;
1100 1101
	}

H
Huy Nguyen 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	if (ops->dcbnl_getbuffer) {
		struct dcbnl_buffer buffer;

		memset(&buffer, 0, sizeof(buffer));
		err = ops->dcbnl_getbuffer(netdev, &buffer);
		if (!err &&
		    nla_put(skb, DCB_ATTR_DCB_BUFFER, sizeof(buffer), &buffer))
			return -EMSGSIZE;
	}

1112
	app = nla_nest_start_noflag(skb, DCB_ATTR_IEEE_APP_TABLE);
1113
	if (!app)
1114
		return -EMSGSIZE;
1115

1116
	spin_lock_bh(&dcb_lock);
1117
	list_for_each_entry(itr, &dcb_app_list, list) {
M
Mark Rustad 已提交
1118
		if (itr->ifindex == netdev->ifindex) {
1119 1120 1121
			err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
					 &itr->app);
			if (err) {
1122
				spin_unlock_bh(&dcb_lock);
1123
				return -EMSGSIZE;
1124 1125
			}
		}
1126
	}
1127 1128 1129 1130 1131 1132

	if (netdev->dcbnl_ops->getdcbx)
		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
	else
		dcbx = -EOPNOTSUPP;

1133
	spin_unlock_bh(&dcb_lock);
1134 1135
	nla_nest_end(skb, app);

1136 1137 1138
	/* get peer info if available */
	if (ops->ieee_peer_getets) {
		struct ieee_ets ets;
1139
		memset(&ets, 0, sizeof(ets));
1140
		err = ops->ieee_peer_getets(netdev, &ets);
D
David S. Miller 已提交
1141 1142
		if (!err &&
		    nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
1143
			return -EMSGSIZE;
1144 1145 1146 1147
	}

	if (ops->ieee_peer_getpfc) {
		struct ieee_pfc pfc;
1148
		memset(&pfc, 0, sizeof(pfc));
1149
		err = ops->ieee_peer_getpfc(netdev, &pfc);
D
David S. Miller 已提交
1150 1151
		if (!err &&
		    nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
1152
			return -EMSGSIZE;
1153 1154 1155
	}

	if (ops->peer_getappinfo && ops->peer_getapptable) {
1156 1157 1158 1159
		err = dcbnl_build_peer_app(netdev, skb,
					   DCB_ATTR_IEEE_PEER_APP,
					   DCB_ATTR_IEEE_APP_UNSPEC,
					   DCB_ATTR_IEEE_APP);
1160
		if (err)
1161
			return -EMSGSIZE;
1162 1163
	}

1164
	nla_nest_end(skb, ieee);
1165 1166 1167
	if (dcbx >= 0) {
		err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
		if (err)
1168
			return -EMSGSIZE;
1169
	}
1170

1171
	return 0;
1172 1173
}

S
Shmulik Ravid 已提交
1174 1175 1176 1177 1178 1179
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;
1180
	struct nlattr *pg = nla_nest_start_noflag(skb, i);
S
Shmulik Ravid 已提交
1181 1182

	if (!pg)
1183
		return -EMSGSIZE;
S
Shmulik Ravid 已提交
1184 1185

	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
1186
		struct nlattr *tc_nest = nla_nest_start_noflag(skb, i);
S
Shmulik Ravid 已提交
1187 1188

		if (!tc_nest)
1189
			return -EMSGSIZE;
S
Shmulik Ravid 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

		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);

D
David S. Miller 已提交
1203 1204 1205 1206
		if (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))
1207
			return -EMSGSIZE;
S
Shmulik Ravid 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		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);
D
David S. Miller 已提交
1220
		if (nla_put_u8(skb, i, tc_pct))
1221
			return -EMSGSIZE;
S
Shmulik Ravid 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	}
	nla_nest_end(skb, pg);
	return 0;
}

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;

D
David S. Miller 已提交
1235 1236
	if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
		goto nla_put_failure;
1237
	cee = nla_nest_start_noflag(skb, DCB_ATTR_CEE);
S
Shmulik Ravid 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
	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) {
1256 1257
		struct nlattr *pfc_nest = nla_nest_start_noflag(skb,
								DCB_ATTR_CEE_PFC);
S
Shmulik Ravid 已提交
1258 1259 1260 1261 1262 1263

		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);
D
David S. Miller 已提交
1264 1265
			if (nla_put_u8(skb, i, value))
				goto nla_put_failure;
S
Shmulik Ravid 已提交
1266 1267 1268 1269 1270
		}
		nla_nest_end(skb, pfc_nest);
	}

	/* local app */
1271
	spin_lock_bh(&dcb_lock);
1272
	app = nla_nest_start_noflag(skb, DCB_ATTR_CEE_APP_TABLE);
S
Shmulik Ravid 已提交
1273
	if (!app)
1274
		goto dcb_unlock;
S
Shmulik Ravid 已提交
1275 1276

	list_for_each_entry(itr, &dcb_app_list, list) {
M
Mark Rustad 已提交
1277
		if (itr->ifindex == netdev->ifindex) {
1278 1279
			struct nlattr *app_nest = nla_nest_start_noflag(skb,
									DCB_ATTR_APP);
S
Shmulik Ravid 已提交
1280 1281 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
			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;

1308
	spin_unlock_bh(&dcb_lock);
S
Shmulik Ravid 已提交
1309 1310 1311

	/* features flags */
	if (ops->getfeatcfg) {
1312 1313
		struct nlattr *feat = nla_nest_start_noflag(skb,
							    DCB_ATTR_CEE_FEAT);
S
Shmulik Ravid 已提交
1314 1315 1316 1317 1318
		if (!feat)
			goto nla_put_failure;

		for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
		     i++)
D
David S. Miller 已提交
1319 1320 1321
			if (!ops->getfeatcfg(netdev, i, &value) &&
			    nla_put_u8(skb, i, value))
				goto nla_put_failure;
S
Shmulik Ravid 已提交
1322 1323 1324 1325 1326 1327 1328

		nla_nest_end(skb, feat);
	}

	/* peer info if available */
	if (ops->cee_peer_getpg) {
		struct cee_pg pg;
1329
		memset(&pg, 0, sizeof(pg));
S
Shmulik Ravid 已提交
1330
		err = ops->cee_peer_getpg(netdev, &pg);
D
David S. Miller 已提交
1331 1332 1333
		if (!err &&
		    nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
			goto nla_put_failure;
S
Shmulik Ravid 已提交
1334 1335 1336 1337
	}

	if (ops->cee_peer_getpfc) {
		struct cee_pfc pfc;
1338
		memset(&pfc, 0, sizeof(pfc));
S
Shmulik Ravid 已提交
1339
		err = ops->cee_peer_getpfc(netdev, &pfc);
D
David S. Miller 已提交
1340 1341 1342
		if (!err &&
		    nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
			goto nla_put_failure;
S
Shmulik Ravid 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
	}

	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:
1364
	spin_unlock_bh(&dcb_lock);
S
Shmulik Ravid 已提交
1365
nla_put_failure:
P
Pan Bian 已提交
1366
	err = -EMSGSIZE;
S
Shmulik Ravid 已提交
1367 1368 1369 1370
	return err;
}

static int dcbnl_notify(struct net_device *dev, int event, int cmd,
1371
			u32 seq, u32 portid, int dcbx_ver)
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
{
	struct net *net = dev_net(dev);
	struct sk_buff *skb;
	struct nlmsghdr *nlh;
	const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
	int err;

	if (!ops)
		return -EOPNOTSUPP;

1382
	skb = dcbnl_newmsg(event, cmd, portid, seq, 0, &nlh);
1383 1384 1385
	if (!skb)
		return -ENOBUFS;

S
Shmulik Ravid 已提交
1386 1387 1388 1389 1390
	if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
		err = dcbnl_ieee_fill(skb, dev);
	else
		err = dcbnl_cee_fill(skb, dev);

1391 1392
	if (err < 0) {
		/* Report error to broadcast listeners */
1393
		nlmsg_free(skb);
1394 1395 1396 1397 1398 1399 1400 1401 1402
		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;
}
S
Shmulik Ravid 已提交
1403 1404

int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
1405
		      u32 seq, u32 portid)
S
Shmulik Ravid 已提交
1406
{
1407
	return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_IEEE);
S
Shmulik Ravid 已提交
1408 1409 1410 1411
}
EXPORT_SYMBOL(dcbnl_ieee_notify);

int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
1412
		     u32 seq, u32 portid)
S
Shmulik Ravid 已提交
1413
{
1414
	return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_CEE);
S
Shmulik Ravid 已提交
1415 1416
}
EXPORT_SYMBOL(dcbnl_cee_notify);
1417

S
Shani Michaeli 已提交
1418 1419 1420
/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb SET commands.
 * If any requested operation can not be completed
 * the entire msg is aborted and error value is returned.
1421 1422 1423
 * No attempt is made to reconcile the case where only part of the
 * cmd can be completed.
 */
1424 1425
static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1426 1427 1428
{
	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
1429
	int prio;
1430
	int err;
1431 1432

	if (!ops)
1433
		return -EOPNOTSUPP;
1434

1435 1436 1437
	if (!tb[DCB_ATTR_IEEE])
		return -EINVAL;

1438 1439 1440
	err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
					  tb[DCB_ATTR_IEEE],
					  dcbnl_ieee_policy, NULL);
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
	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;
	}

1451 1452 1453 1454 1455 1456 1457 1458
	if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
		struct ieee_maxrate *maxrate =
			nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
		err = ops->ieee_setmaxrate(netdev, maxrate);
		if (err)
			goto err;
	}

S
Shani Michaeli 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467
	if (ieee[DCB_ATTR_IEEE_QCN] && ops->ieee_setqcn) {
		struct ieee_qcn *qcn =
			nla_data(ieee[DCB_ATTR_IEEE_QCN]);

		err = ops->ieee_setqcn(netdev, qcn);
		if (err)
			goto err;
	}

1468 1469 1470 1471 1472 1473 1474
	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;
	}

H
Huy Nguyen 已提交
1475 1476 1477 1478
	if (ieee[DCB_ATTR_DCB_BUFFER] && ops->dcbnl_setbuffer) {
		struct dcbnl_buffer *buffer =
			nla_data(ieee[DCB_ATTR_DCB_BUFFER]);

1479 1480 1481 1482 1483 1484 1485
		for (prio = 0; prio < ARRAY_SIZE(buffer->prio2buffer); prio++) {
			if (buffer->prio2buffer[prio] >= DCBX_MAX_BUFFERS) {
				err = -EINVAL;
				goto err;
			}
		}

H
Huy Nguyen 已提交
1486 1487 1488 1489 1490
		err = ops->dcbnl_setbuffer(netdev, buffer);
		if (err)
			goto err;
	}

1491 1492 1493 1494 1495 1496
	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;
1497

1498 1499
			if (nla_type(attr) != DCB_ATTR_IEEE_APP)
				continue;
1500 1501 1502 1503 1504 1505

			if (nla_len(attr) < sizeof(struct dcb_app)) {
				err = -ERANGE;
				goto err;
			}

1506 1507 1508 1509
			app_data = nla_data(attr);
			if (ops->ieee_setapp)
				err = ops->ieee_setapp(netdev, app_data);
			else
1510
				err = dcb_ieee_setapp(netdev, app_data);
1511 1512 1513 1514 1515 1516
			if (err)
				goto err;
		}
	}

err:
1517
	err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
S
Shmulik Ravid 已提交
1518
	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
1519 1520 1521
	return err;
}

1522 1523
static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1524 1525 1526 1527 1528 1529
{
	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;

	if (!ops)
		return -EOPNOTSUPP;

1530
	return dcbnl_ieee_fill(skb, netdev);
1531
}
1532

1533 1534
static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1535 1536 1537
{
	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
1538
	int err;
1539 1540 1541 1542 1543 1544 1545

	if (!ops)
		return -EOPNOTSUPP;

	if (!tb[DCB_ATTR_IEEE])
		return -EINVAL;

1546 1547 1548
	err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
					  tb[DCB_ATTR_IEEE],
					  dcbnl_ieee_policy, NULL);
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
	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:
1572
	err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
S
Shmulik Ravid 已提交
1573
	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
1574 1575 1576 1577
	return err;
}


1578
/* DCBX configuration */
1579 1580
static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
1581 1582
{
	if (!netdev->dcbnl_ops->getdcbx)
1583
		return -EOPNOTSUPP;
1584

1585 1586
	return nla_put_u8(skb, DCB_ATTR_DCBX,
			  netdev->dcbnl_ops->getdcbx(netdev));
1587 1588
}

1589 1590
static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
1591 1592 1593
{
	u8 value;

1594 1595 1596 1597 1598
	if (!netdev->dcbnl_ops->setdcbx)
		return -EOPNOTSUPP;

	if (!tb[DCB_ATTR_DCBX])
		return -EINVAL;
1599 1600 1601

	value = nla_get_u8(tb[DCB_ATTR_DCBX]);

1602 1603
	return nla_put_u8(skb, DCB_ATTR_DCBX,
			  netdev->dcbnl_ops->setdcbx(netdev, value));
1604 1605
}

1606 1607
static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
1608 1609 1610
{
	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
	u8 value;
1611
	int ret, i;
1612 1613
	int getall = 0;

1614 1615 1616 1617 1618
	if (!netdev->dcbnl_ops->getfeatcfg)
		return -EOPNOTSUPP;

	if (!tb[DCB_ATTR_FEATCFG])
		return -EINVAL;
1619

1620 1621 1622
	ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
					  tb[DCB_ATTR_FEATCFG],
					  dcbnl_featcfg_nest, NULL);
1623
	if (ret)
1624
		return ret;
1625

1626
	nest = nla_nest_start_noflag(skb, DCB_ATTR_FEATCFG);
1627 1628
	if (!nest)
		return -EMSGSIZE;
1629 1630 1631 1632 1633 1634 1635 1636 1637

	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);
1638
		if (!ret)
1639
			ret = nla_put_u8(skb, i, value);
1640

1641
		if (ret) {
1642
			nla_nest_cancel(skb, nest);
1643 1644
			goto nla_put_failure;
		}
1645
	}
1646
	nla_nest_end(skb, nest);
1647

1648
nla_put_failure:
1649 1650 1651
	return ret;
}

1652 1653
static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
1654 1655
{
	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
1656
	int ret, i;
1657 1658
	u8 value;

1659 1660 1661 1662 1663
	if (!netdev->dcbnl_ops->setfeatcfg)
		return -ENOTSUPP;

	if (!tb[DCB_ATTR_FEATCFG])
		return -EINVAL;
1664

1665 1666 1667
	ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
					  tb[DCB_ATTR_FEATCFG],
					  dcbnl_featcfg_nest, NULL);
1668

1669
	if (ret)
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
		goto err;

	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)
1681
			goto err;
1682 1683
	}
err:
1684
	ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
1685

1686 1687 1688
	return ret;
}

1689
/* Handle CEE DCBX GET commands. */
1690 1691
static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
1692 1693 1694 1695 1696 1697
{
	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;

	if (!ops)
		return -EOPNOTSUPP;

1698
	return dcbnl_cee_fill(skb, netdev);
1699 1700
}

1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
struct reply_func {
	/* reply netlink message type */
	int	type;

	/* function to fill message contents */
	int   (*cb)(struct net_device *, struct nlmsghdr *, u32,
		    struct nlattr **, struct sk_buff *);
};

static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
	[DCB_CMD_GSTATE]	= { RTM_GETDCB, dcbnl_getstate },
	[DCB_CMD_SSTATE]	= { RTM_SETDCB, dcbnl_setstate },
	[DCB_CMD_PFC_GCFG]	= { RTM_GETDCB, dcbnl_getpfccfg },
	[DCB_CMD_PFC_SCFG]	= { RTM_SETDCB, dcbnl_setpfccfg },
	[DCB_CMD_GPERM_HWADDR]	= { RTM_GETDCB, dcbnl_getperm_hwaddr },
	[DCB_CMD_GCAP]		= { RTM_GETDCB, dcbnl_getcap },
	[DCB_CMD_GNUMTCS]	= { RTM_GETDCB, dcbnl_getnumtcs },
	[DCB_CMD_SNUMTCS]	= { RTM_SETDCB, dcbnl_setnumtcs },
	[DCB_CMD_PFC_GSTATE]	= { RTM_GETDCB, dcbnl_getpfcstate },
	[DCB_CMD_PFC_SSTATE]	= { RTM_SETDCB, dcbnl_setpfcstate },
	[DCB_CMD_GAPP]		= { RTM_GETDCB, dcbnl_getapp },
	[DCB_CMD_SAPP]		= { RTM_SETDCB, dcbnl_setapp },
	[DCB_CMD_PGTX_GCFG]	= { RTM_GETDCB, dcbnl_pgtx_getcfg },
	[DCB_CMD_PGTX_SCFG]	= { RTM_SETDCB, dcbnl_pgtx_setcfg },
	[DCB_CMD_PGRX_GCFG]	= { RTM_GETDCB, dcbnl_pgrx_getcfg },
	[DCB_CMD_PGRX_SCFG]	= { RTM_SETDCB, dcbnl_pgrx_setcfg },
	[DCB_CMD_SET_ALL]	= { RTM_SETDCB, dcbnl_setall },
	[DCB_CMD_BCN_GCFG]	= { RTM_GETDCB, dcbnl_bcn_getcfg },
	[DCB_CMD_BCN_SCFG]	= { RTM_SETDCB, dcbnl_bcn_setcfg },
	[DCB_CMD_IEEE_GET]	= { RTM_GETDCB, dcbnl_ieee_get },
	[DCB_CMD_IEEE_SET]	= { RTM_SETDCB, dcbnl_ieee_set },
	[DCB_CMD_IEEE_DEL]	= { RTM_SETDCB, dcbnl_ieee_del },
	[DCB_CMD_GDCBX]		= { RTM_GETDCB, dcbnl_getdcbx },
	[DCB_CMD_SDCBX]		= { RTM_SETDCB, dcbnl_setdcbx },
	[DCB_CMD_GFEATCFG]	= { RTM_GETDCB, dcbnl_getfeatcfg },
	[DCB_CMD_SFEATCFG]	= { RTM_SETDCB, dcbnl_setfeatcfg },
	[DCB_CMD_CEE_GET]	= { RTM_GETDCB, dcbnl_cee_get },
1738 1739
};

1740 1741
static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
		    struct netlink_ext_ack *extack)
1742 1743 1744
{
	struct net *net = sock_net(skb->sk);
	struct net_device *netdev;
T
Thomas Graf 已提交
1745
	struct dcbmsg *dcb = nlmsg_data(nlh);
1746
	struct nlattr *tb[DCB_ATTR_MAX + 1];
1747
	u32 portid = NETLINK_CB(skb).portid;
1748
	int ret = -EINVAL;
1749
	struct sk_buff *reply_skb;
1750
	struct nlmsghdr *reply_nlh = NULL;
1751
	const struct reply_func *fn;
1752

1753
	if ((nlh->nlmsg_type == RTM_SETDCB) && !netlink_capable(skb, CAP_NET_ADMIN))
1754 1755
		return -EPERM;

1756 1757
	ret = nlmsg_parse_deprecated(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
				     dcbnl_rtnl_policy, extack);
1758 1759 1760
	if (ret < 0)
		return ret;

1761 1762 1763 1764 1765 1766 1767
	if (dcb->cmd > DCB_CMD_MAX)
		return -EINVAL;

	/* check if a reply function has been defined for the command */
	fn = &reply_funcs[dcb->cmd];
	if (!fn->cb)
		return -EOPNOTSUPP;
1768
	if (fn->type == RTM_SETDCB && !netlink_capable(skb, CAP_NET_ADMIN))
1769
		return -EPERM;
1770

1771 1772 1773
	if (!tb[DCB_ATTR_IFNAME])
		return -EINVAL;

1774
	netdev = __dev_get_by_name(net, nla_data(tb[DCB_ATTR_IFNAME]));
1775
	if (!netdev)
1776
		return -ENODEV;
1777

1778 1779
	if (!netdev->dcbnl_ops)
		return -EOPNOTSUPP;
1780

1781
	reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, portid, nlh->nlmsg_seq,
1782
				 nlh->nlmsg_flags, &reply_nlh);
1783 1784
	if (!reply_skb)
		return -ENOBUFS;
1785 1786 1787 1788 1789 1790 1791 1792 1793

	ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
	if (ret < 0) {
		nlmsg_free(reply_skb);
		goto out;
	}

	nlmsg_end(reply_skb, reply_nlh);

1794
	ret = rtnl_unicast(reply_skb, net, portid);
1795 1796 1797 1798
out:
	return ret;
}

1799 1800 1801 1802 1803 1804 1805 1806 1807
static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
					   int ifindex, int prio)
{
	struct dcb_app_type *itr;

	list_for_each_entry(itr, &dcb_app_list, list) {
		if (itr->app.selector == app->selector &&
		    itr->app.protocol == app->protocol &&
		    itr->ifindex == ifindex &&
1808
		    ((prio == -1) || itr->app.priority == prio))
1809 1810 1811 1812 1813 1814
			return itr;
	}

	return NULL;
}

1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
static int dcb_app_add(const struct dcb_app *app, int ifindex)
{
	struct dcb_app_type *entry;

	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
	if (!entry)
		return -ENOMEM;

	memcpy(&entry->app, app, sizeof(*app));
	entry->ifindex = ifindex;
	list_add(&entry->list, &dcb_app_list);

	return 0;
}

1830 1831
/**
 * dcb_getapp - retrieve the DCBX application user priority
A
Andrew Lunn 已提交
1832 1833
 * @dev: network interface
 * @app: application to get user priority of
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
 *
 * 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;

1844
	spin_lock_bh(&dcb_lock);
1845 1846
	itr = dcb_app_lookup(app, dev->ifindex, -1);
	if (itr)
1847
		prio = itr->app.priority;
1848
	spin_unlock_bh(&dcb_lock);
1849 1850 1851 1852 1853 1854

	return prio;
}
EXPORT_SYMBOL(dcb_getapp);

/**
1855
 * dcb_setapp - add CEE dcb application data to app list
A
Andrew Lunn 已提交
1856 1857
 * @dev: network interface
 * @new: application data to add
1858
 *
1859 1860
 * Priority 0 is an invalid priority in CEE spec. This routine
 * removes applications from the app list if the priority is
1861
 * set to zero. Priority is expected to be 8-bit 802.1p user priority bitmap
1862
 */
1863
int dcb_setapp(struct net_device *dev, struct dcb_app *new)
1864 1865
{
	struct dcb_app_type *itr;
1866
	struct dcb_app_type event;
1867
	int err = 0;
1868

M
Mark Rustad 已提交
1869
	event.ifindex = dev->ifindex;
1870
	memcpy(&event.app, new, sizeof(event.app));
1871 1872
	if (dev->dcbnl_ops->getdcbx)
		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
1873

1874
	spin_lock_bh(&dcb_lock);
1875
	/* Search for existing match and replace */
1876 1877
	itr = dcb_app_lookup(new, dev->ifindex, -1);
	if (itr) {
1878 1879 1880 1881 1882
		if (new->priority)
			itr->app.priority = new->priority;
		else {
			list_del(&itr->list);
			kfree(itr);
1883
		}
1884
		goto out;
1885 1886
	}
	/* App type does not exist add new application type */
1887 1888
	if (new->priority)
		err = dcb_app_add(new, dev->ifindex);
1889
out:
1890
	spin_unlock_bh(&dcb_lock);
1891 1892 1893
	if (!err)
		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
	return err;
1894 1895 1896
}
EXPORT_SYMBOL(dcb_setapp);

1897 1898
/**
 * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
A
Andrew Lunn 已提交
1899 1900
 * @dev: network interface
 * @app: where to store the retrieve application data
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
 *
 * 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;

1911
	spin_lock_bh(&dcb_lock);
1912 1913
	itr = dcb_app_lookup(app, dev->ifindex, -1);
	if (itr)
1914
		prio |= 1 << itr->app.priority;
1915
	spin_unlock_bh(&dcb_lock);
1916 1917 1918 1919 1920

	return prio;
}
EXPORT_SYMBOL(dcb_ieee_getapp_mask);

1921 1922
/**
 * dcb_ieee_setapp - add IEEE dcb application data to app list
A
Andrew Lunn 已提交
1923 1924
 * @dev: network interface
 * @new: application data to add
1925 1926 1927
 *
 * This adds Application data to the list. Multiple application
 * entries may exists for the same selector and protocol as long
1928 1929
 * as the priorities are different. Priority is expected to be a
 * 3-bit unsigned integer
1930 1931 1932 1933 1934 1935
 */
int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
{
	struct dcb_app_type event;
	int err = 0;

M
Mark Rustad 已提交
1936
	event.ifindex = dev->ifindex;
1937
	memcpy(&event.app, new, sizeof(event.app));
1938 1939
	if (dev->dcbnl_ops->getdcbx)
		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
1940

1941
	spin_lock_bh(&dcb_lock);
1942
	/* Search for existing match and abort if found */
1943 1944 1945
	if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
		err = -EEXIST;
		goto out;
1946 1947
	}

1948
	err = dcb_app_add(new, dev->ifindex);
1949
out:
1950
	spin_unlock_bh(&dcb_lock);
1951 1952 1953 1954 1955 1956
	if (!err)
		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
	return err;
}
EXPORT_SYMBOL(dcb_ieee_setapp);

1957 1958
/**
 * dcb_ieee_delapp - delete IEEE dcb application data from list
A
Andrew Lunn 已提交
1959 1960
 * @dev: network interface
 * @del: application data to delete
1961 1962 1963 1964 1965 1966 1967 1968 1969
 *
 * 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;

M
Mark Rustad 已提交
1970
	event.ifindex = dev->ifindex;
1971
	memcpy(&event.app, del, sizeof(event.app));
1972 1973
	if (dev->dcbnl_ops->getdcbx)
		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
1974

1975
	spin_lock_bh(&dcb_lock);
1976
	/* Search for existing match and remove it. */
1977 1978 1979 1980
	if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
		list_del(&itr->list);
		kfree(itr);
		err = 0;
1981 1982
	}

1983
	spin_unlock_bh(&dcb_lock);
1984 1985 1986 1987 1988 1989
	if (!err)
		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
	return err;
}
EXPORT_SYMBOL(dcb_ieee_delapp);

A
Andrew Lunn 已提交
1990
/*
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
 * dcb_ieee_getapp_prio_dscp_mask_map - For a given device, find mapping from
 * priorities to the DSCP values assigned to that priority. Initialize p_map
 * such that each map element holds a bit mask of DSCP values configured for
 * that priority by APP entries.
 */
void dcb_ieee_getapp_prio_dscp_mask_map(const struct net_device *dev,
					struct dcb_ieee_app_prio_map *p_map)
{
	int ifindex = dev->ifindex;
	struct dcb_app_type *itr;
	u8 prio;

	memset(p_map->map, 0, sizeof(p_map->map));

	spin_lock_bh(&dcb_lock);
	list_for_each_entry(itr, &dcb_app_list, list) {
		if (itr->ifindex == ifindex &&
		    itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
		    itr->app.protocol < 64 &&
		    itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
			prio = itr->app.priority;
			p_map->map[prio] |= 1ULL << itr->app.protocol;
		}
	}
	spin_unlock_bh(&dcb_lock);
}
EXPORT_SYMBOL(dcb_ieee_getapp_prio_dscp_mask_map);

A
Andrew Lunn 已提交
2019
/*
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
 * dcb_ieee_getapp_dscp_prio_mask_map - For a given device, find mapping from
 * DSCP values to the priorities assigned to that DSCP value. Initialize p_map
 * such that each map element holds a bit mask of priorities configured for a
 * given DSCP value by APP entries.
 */
void
dcb_ieee_getapp_dscp_prio_mask_map(const struct net_device *dev,
				   struct dcb_ieee_app_dscp_map *p_map)
{
	int ifindex = dev->ifindex;
	struct dcb_app_type *itr;

	memset(p_map->map, 0, sizeof(p_map->map));

	spin_lock_bh(&dcb_lock);
	list_for_each_entry(itr, &dcb_app_list, list) {
		if (itr->ifindex == ifindex &&
		    itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
		    itr->app.protocol < 64 &&
		    itr->app.priority < IEEE_8021QAZ_MAX_TCS)
			p_map->map[itr->app.protocol] |= 1 << itr->app.priority;
	}
	spin_unlock_bh(&dcb_lock);
}
EXPORT_SYMBOL(dcb_ieee_getapp_dscp_prio_mask_map);

A
Andrew Lunn 已提交
2046
/*
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
 * Per 802.1Q-2014, the selector value of 1 is used for matching on Ethernet
 * type, with valid PID values >= 1536. A special meaning is then assigned to
 * protocol value of 0: "default priority. For use when priority is not
 * otherwise specified".
 *
 * dcb_ieee_getapp_default_prio_mask - For a given device, find all APP entries
 * of the form {$PRIO, ETHERTYPE, 0} and construct a bit mask of all default
 * priorities set by these entries.
 */
u8 dcb_ieee_getapp_default_prio_mask(const struct net_device *dev)
{
	int ifindex = dev->ifindex;
	struct dcb_app_type *itr;
	u8 mask = 0;

	spin_lock_bh(&dcb_lock);
	list_for_each_entry(itr, &dcb_app_list, list) {
		if (itr->ifindex == ifindex &&
		    itr->app.selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
		    itr->app.protocol == 0 &&
		    itr->app.priority < IEEE_8021QAZ_MAX_TCS)
			mask |= 1 << itr->app.priority;
	}
	spin_unlock_bh(&dcb_lock);

	return mask;
}
EXPORT_SYMBOL(dcb_ieee_getapp_default_prio_mask);

2076 2077
static int __init dcbnl_init(void)
{
2078 2079
	rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, 0);
	rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, 0);
2080 2081 2082

	return 0;
}
2083
device_initcall(dcbnl_init);