xref: /openbmc/linux/net/dcb/dcbnl.c (revision 96b99684)
12f90b865SAlexander Duyck /*
22f90b865SAlexander Duyck  * Copyright (c) 2008, Intel Corporation.
32f90b865SAlexander Duyck  *
42f90b865SAlexander Duyck  * This program is free software; you can redistribute it and/or modify it
52f90b865SAlexander Duyck  * under the terms and conditions of the GNU General Public License,
62f90b865SAlexander Duyck  * version 2, as published by the Free Software Foundation.
72f90b865SAlexander Duyck  *
82f90b865SAlexander Duyck  * This program is distributed in the hope it will be useful, but WITHOUT
92f90b865SAlexander Duyck  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
102f90b865SAlexander Duyck  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
112f90b865SAlexander Duyck  * more details.
122f90b865SAlexander Duyck  *
132f90b865SAlexander Duyck  * You should have received a copy of the GNU General Public License along with
142f90b865SAlexander Duyck  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
152f90b865SAlexander Duyck  * Place - Suite 330, Boston, MA 02111-1307 USA.
162f90b865SAlexander Duyck  *
172f90b865SAlexander Duyck  * Author: Lucy Liu <lucy.liu@intel.com>
182f90b865SAlexander Duyck  */
192f90b865SAlexander Duyck 
202f90b865SAlexander Duyck #include <linux/netdevice.h>
212f90b865SAlexander Duyck #include <linux/netlink.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
232f90b865SAlexander Duyck #include <net/netlink.h>
242f90b865SAlexander Duyck #include <net/rtnetlink.h>
252f90b865SAlexander Duyck #include <linux/dcbnl.h>
2696b99684SJohn Fastabend #include <net/dcbevent.h>
272f90b865SAlexander Duyck #include <linux/rtnetlink.h>
282f90b865SAlexander Duyck #include <net/sock.h>
292f90b865SAlexander Duyck 
302f90b865SAlexander Duyck /**
312f90b865SAlexander Duyck  * Data Center Bridging (DCB) is a collection of Ethernet enhancements
322f90b865SAlexander Duyck  * intended to allow network traffic with differing requirements
332f90b865SAlexander Duyck  * (highly reliable, no drops vs. best effort vs. low latency) to operate
342f90b865SAlexander Duyck  * and co-exist on Ethernet.  Current DCB features are:
352f90b865SAlexander Duyck  *
362f90b865SAlexander Duyck  * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
372f90b865SAlexander Duyck  *   framework for assigning bandwidth guarantees to traffic classes.
382f90b865SAlexander Duyck  *
392f90b865SAlexander Duyck  * Priority-based Flow Control (PFC) - provides a flow control mechanism which
402f90b865SAlexander Duyck  *   can work independently for each 802.1p priority.
412f90b865SAlexander Duyck  *
422f90b865SAlexander Duyck  * Congestion Notification - provides a mechanism for end-to-end congestion
432f90b865SAlexander Duyck  *   control for protocols which do not have built-in congestion management.
442f90b865SAlexander Duyck  *
452f90b865SAlexander Duyck  * More information about the emerging standards for these Ethernet features
462f90b865SAlexander Duyck  * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
472f90b865SAlexander Duyck  *
482f90b865SAlexander Duyck  * This file implements an rtnetlink interface to allow configuration of DCB
492f90b865SAlexander Duyck  * features for capable devices.
502f90b865SAlexander Duyck  */
512f90b865SAlexander Duyck 
522f90b865SAlexander Duyck MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
537a6b6f51SJeff Kirsher MODULE_DESCRIPTION("Data Center Bridging netlink interface");
542f90b865SAlexander Duyck MODULE_LICENSE("GPL");
552f90b865SAlexander Duyck 
562f90b865SAlexander Duyck /**************** DCB attribute policies *************************************/
572f90b865SAlexander Duyck 
582f90b865SAlexander Duyck /* DCB netlink attributes policy */
59b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
60859ee3c4SAlexander Duyck 	[DCB_ATTR_IFNAME]      = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
612f90b865SAlexander Duyck 	[DCB_ATTR_STATE]       = {.type = NLA_U8},
622f90b865SAlexander Duyck 	[DCB_ATTR_PFC_CFG]     = {.type = NLA_NESTED},
632f90b865SAlexander Duyck 	[DCB_ATTR_PG_CFG]      = {.type = NLA_NESTED},
642f90b865SAlexander Duyck 	[DCB_ATTR_SET_ALL]     = {.type = NLA_U8},
652f90b865SAlexander Duyck 	[DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
6646132188SAlexander Duyck 	[DCB_ATTR_CAP]         = {.type = NLA_NESTED},
670eb3aa9bSAlexander Duyck 	[DCB_ATTR_PFC_STATE]   = {.type = NLA_U8},
68859ee3c4SAlexander Duyck 	[DCB_ATTR_BCN]         = {.type = NLA_NESTED},
696fa382afSYi Zou 	[DCB_ATTR_APP]         = {.type = NLA_NESTED},
703e29027aSJohn Fastabend 	[DCB_ATTR_IEEE]	       = {.type = NLA_NESTED},
712f90b865SAlexander Duyck };
722f90b865SAlexander Duyck 
732f90b865SAlexander Duyck /* DCB priority flow control to User Priority nested attributes */
74b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
752f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_0]   = {.type = NLA_U8},
762f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_1]   = {.type = NLA_U8},
772f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_2]   = {.type = NLA_U8},
782f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_3]   = {.type = NLA_U8},
792f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_4]   = {.type = NLA_U8},
802f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_5]   = {.type = NLA_U8},
812f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_6]   = {.type = NLA_U8},
822f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_7]   = {.type = NLA_U8},
832f90b865SAlexander Duyck 	[DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
842f90b865SAlexander Duyck };
852f90b865SAlexander Duyck 
862f90b865SAlexander Duyck /* DCB priority grouping nested attributes */
87b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
882f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_0]      = {.type = NLA_NESTED},
892f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_1]      = {.type = NLA_NESTED},
902f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_2]      = {.type = NLA_NESTED},
912f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_3]      = {.type = NLA_NESTED},
922f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_4]      = {.type = NLA_NESTED},
932f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_5]      = {.type = NLA_NESTED},
942f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_6]      = {.type = NLA_NESTED},
952f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_7]      = {.type = NLA_NESTED},
962f90b865SAlexander Duyck 	[DCB_PG_ATTR_TC_ALL]    = {.type = NLA_NESTED},
972f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_0]   = {.type = NLA_U8},
982f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_1]   = {.type = NLA_U8},
992f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_2]   = {.type = NLA_U8},
1002f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_3]   = {.type = NLA_U8},
1012f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_4]   = {.type = NLA_U8},
1022f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_5]   = {.type = NLA_U8},
1032f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_6]   = {.type = NLA_U8},
1042f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_7]   = {.type = NLA_U8},
1052f90b865SAlexander Duyck 	[DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
1062f90b865SAlexander Duyck };
1072f90b865SAlexander Duyck 
1082f90b865SAlexander Duyck /* DCB traffic class nested attributes. */
109b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
1102f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_PGID]            = {.type = NLA_U8},
1112f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_UP_MAPPING]      = {.type = NLA_U8},
1122f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_STRICT_PRIO]     = {.type = NLA_U8},
1132f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_BW_PCT]          = {.type = NLA_U8},
1142f90b865SAlexander Duyck 	[DCB_TC_ATTR_PARAM_ALL]             = {.type = NLA_FLAG},
1152f90b865SAlexander Duyck };
1162f90b865SAlexander Duyck 
11746132188SAlexander Duyck /* DCB capabilities nested attributes. */
118b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
11946132188SAlexander Duyck 	[DCB_CAP_ATTR_ALL]     = {.type = NLA_FLAG},
12046132188SAlexander Duyck 	[DCB_CAP_ATTR_PG]      = {.type = NLA_U8},
12146132188SAlexander Duyck 	[DCB_CAP_ATTR_PFC]     = {.type = NLA_U8},
12246132188SAlexander Duyck 	[DCB_CAP_ATTR_UP2TC]   = {.type = NLA_U8},
12346132188SAlexander Duyck 	[DCB_CAP_ATTR_PG_TCS]  = {.type = NLA_U8},
12446132188SAlexander Duyck 	[DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
12546132188SAlexander Duyck 	[DCB_CAP_ATTR_GSP]     = {.type = NLA_U8},
12646132188SAlexander Duyck 	[DCB_CAP_ATTR_BCN]     = {.type = NLA_U8},
12746132188SAlexander Duyck };
1282f90b865SAlexander Duyck 
12933dbabc4SAlexander Duyck /* DCB capabilities nested attributes. */
130b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
13133dbabc4SAlexander Duyck 	[DCB_NUMTCS_ATTR_ALL]     = {.type = NLA_FLAG},
13233dbabc4SAlexander Duyck 	[DCB_NUMTCS_ATTR_PG]      = {.type = NLA_U8},
13333dbabc4SAlexander Duyck 	[DCB_NUMTCS_ATTR_PFC]     = {.type = NLA_U8},
13433dbabc4SAlexander Duyck };
13533dbabc4SAlexander Duyck 
136859ee3c4SAlexander Duyck /* DCB BCN nested attributes. */
137b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
138859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_0]         = {.type = NLA_U8},
139859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_1]         = {.type = NLA_U8},
140859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_2]         = {.type = NLA_U8},
141859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_3]         = {.type = NLA_U8},
142859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_4]         = {.type = NLA_U8},
143859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_5]         = {.type = NLA_U8},
144859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_6]         = {.type = NLA_U8},
145859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_7]         = {.type = NLA_U8},
146859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RP_ALL]       = {.type = NLA_FLAG},
147f4314e81SDon Skidmore 	[DCB_BCN_ATTR_BCNA_0]       = {.type = NLA_U32},
148f4314e81SDon Skidmore 	[DCB_BCN_ATTR_BCNA_1]       = {.type = NLA_U32},
149859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_ALPHA]        = {.type = NLA_U32},
150859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_BETA]         = {.type = NLA_U32},
151859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_GD]           = {.type = NLA_U32},
152859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_GI]           = {.type = NLA_U32},
153859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_TMAX]         = {.type = NLA_U32},
154859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_TD]           = {.type = NLA_U32},
155859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RMIN]         = {.type = NLA_U32},
156859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_W]            = {.type = NLA_U32},
157859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RD]           = {.type = NLA_U32},
158859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RU]           = {.type = NLA_U32},
159859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_WRTT]         = {.type = NLA_U32},
160859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_RI]           = {.type = NLA_U32},
161859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_C]            = {.type = NLA_U32},
162859ee3c4SAlexander Duyck 	[DCB_BCN_ATTR_ALL]          = {.type = NLA_FLAG},
163859ee3c4SAlexander Duyck };
164859ee3c4SAlexander Duyck 
1656fa382afSYi Zou /* DCB APP nested attributes. */
166b54452b0SAlexey Dobriyan static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
1676fa382afSYi Zou 	[DCB_APP_ATTR_IDTYPE]       = {.type = NLA_U8},
1686fa382afSYi Zou 	[DCB_APP_ATTR_ID]           = {.type = NLA_U16},
1696fa382afSYi Zou 	[DCB_APP_ATTR_PRIORITY]     = {.type = NLA_U8},
1706fa382afSYi Zou };
1716fa382afSYi Zou 
1723e29027aSJohn Fastabend /* IEEE 802.1Qaz nested attributes. */
1733e29027aSJohn Fastabend static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
1743e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_ETS]	    = {.len = sizeof(struct ieee_ets)},
1753e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_PFC]	    = {.len = sizeof(struct ieee_pfc)},
1763e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_APP_TABLE]   = {.type = NLA_NESTED},
1773e29027aSJohn Fastabend };
1783e29027aSJohn Fastabend 
1793e29027aSJohn Fastabend static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
1803e29027aSJohn Fastabend 	[DCB_ATTR_IEEE_APP]	    = {.len = sizeof(struct dcb_app)},
1813e29027aSJohn Fastabend };
1823e29027aSJohn Fastabend 
1839ab933abSJohn Fastabend static LIST_HEAD(dcb_app_list);
1849ab933abSJohn Fastabend static DEFINE_SPINLOCK(dcb_lock);
1859ab933abSJohn Fastabend 
1862f90b865SAlexander Duyck /* standard netlink reply call */
1872f90b865SAlexander Duyck static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,
1882f90b865SAlexander Duyck                        u32 seq, u16 flags)
1892f90b865SAlexander Duyck {
1902f90b865SAlexander Duyck 	struct sk_buff *dcbnl_skb;
1912f90b865SAlexander Duyck 	struct dcbmsg *dcb;
1922f90b865SAlexander Duyck 	struct nlmsghdr *nlh;
1932f90b865SAlexander Duyck 	int ret = -EINVAL;
1942f90b865SAlexander Duyck 
1952f90b865SAlexander Duyck 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1962f90b865SAlexander Duyck 	if (!dcbnl_skb)
1972f90b865SAlexander Duyck 		return ret;
1982f90b865SAlexander Duyck 
1992f90b865SAlexander Duyck 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);
2002f90b865SAlexander Duyck 
2012f90b865SAlexander Duyck 	dcb = NLMSG_DATA(nlh);
2022f90b865SAlexander Duyck 	dcb->dcb_family = AF_UNSPEC;
2032f90b865SAlexander Duyck 	dcb->cmd = cmd;
2042f90b865SAlexander Duyck 	dcb->dcb_pad = 0;
2052f90b865SAlexander Duyck 
2062f90b865SAlexander Duyck 	ret = nla_put_u8(dcbnl_skb, attr, value);
2072f90b865SAlexander Duyck 	if (ret)
2082f90b865SAlexander Duyck 		goto err;
2092f90b865SAlexander Duyck 
2102f90b865SAlexander Duyck 	/* end the message, assign the nlmsg_len. */
2112f90b865SAlexander Duyck 	nlmsg_end(dcbnl_skb, nlh);
2122f90b865SAlexander Duyck 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
2132f90b865SAlexander Duyck 	if (ret)
2147eaf5077SJohn Fastabend 		return -EINVAL;
2152f90b865SAlexander Duyck 
2162f90b865SAlexander Duyck 	return 0;
2172f90b865SAlexander Duyck nlmsg_failure:
2182f90b865SAlexander Duyck err:
219858eb711SRoel Kluin 	kfree_skb(dcbnl_skb);
2202f90b865SAlexander Duyck 	return ret;
2212f90b865SAlexander Duyck }
2222f90b865SAlexander Duyck 
2232f90b865SAlexander Duyck static int dcbnl_getstate(struct net_device *netdev, struct nlattr **tb,
2242f90b865SAlexander Duyck                           u32 pid, u32 seq, u16 flags)
2252f90b865SAlexander Duyck {
2262f90b865SAlexander Duyck 	int ret = -EINVAL;
2272f90b865SAlexander Duyck 
2282f90b865SAlexander Duyck 	/* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
2292f90b865SAlexander Duyck 	if (!netdev->dcbnl_ops->getstate)
2302f90b865SAlexander Duyck 		return ret;
2312f90b865SAlexander Duyck 
2322f90b865SAlexander Duyck 	ret = dcbnl_reply(netdev->dcbnl_ops->getstate(netdev), RTM_GETDCB,
2332f90b865SAlexander Duyck 	                  DCB_CMD_GSTATE, DCB_ATTR_STATE, pid, seq, flags);
2342f90b865SAlexander Duyck 
2352f90b865SAlexander Duyck 	return ret;
2362f90b865SAlexander Duyck }
2372f90b865SAlexander Duyck 
2382f90b865SAlexander Duyck static int dcbnl_getpfccfg(struct net_device *netdev, struct nlattr **tb,
2392f90b865SAlexander Duyck                            u32 pid, u32 seq, u16 flags)
2402f90b865SAlexander Duyck {
2412f90b865SAlexander Duyck 	struct sk_buff *dcbnl_skb;
2422f90b865SAlexander Duyck 	struct nlmsghdr *nlh;
2432f90b865SAlexander Duyck 	struct dcbmsg *dcb;
2442f90b865SAlexander Duyck 	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
2452f90b865SAlexander Duyck 	u8 value;
2462f90b865SAlexander Duyck 	int ret = -EINVAL;
2472f90b865SAlexander Duyck 	int i;
2482f90b865SAlexander Duyck 	int getall = 0;
2492f90b865SAlexander Duyck 
2502f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
2512f90b865SAlexander Duyck 		return ret;
2522f90b865SAlexander Duyck 
2532f90b865SAlexander Duyck 	ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
2542f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PFC_CFG],
2552f90b865SAlexander Duyck 	                       dcbnl_pfc_up_nest);
2562f90b865SAlexander Duyck 	if (ret)
2572f90b865SAlexander Duyck 		goto err_out;
2582f90b865SAlexander Duyck 
2592f90b865SAlexander Duyck 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2602f90b865SAlexander Duyck 	if (!dcbnl_skb)
2612f90b865SAlexander Duyck 		goto err_out;
2622f90b865SAlexander Duyck 
2632f90b865SAlexander Duyck 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
2642f90b865SAlexander Duyck 
2652f90b865SAlexander Duyck 	dcb = NLMSG_DATA(nlh);
2662f90b865SAlexander Duyck 	dcb->dcb_family = AF_UNSPEC;
2672f90b865SAlexander Duyck 	dcb->cmd = DCB_CMD_PFC_GCFG;
2682f90b865SAlexander Duyck 
2692f90b865SAlexander Duyck 	nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PFC_CFG);
2702f90b865SAlexander Duyck 	if (!nest)
2712f90b865SAlexander Duyck 		goto err;
2722f90b865SAlexander Duyck 
2732f90b865SAlexander Duyck 	if (data[DCB_PFC_UP_ATTR_ALL])
2742f90b865SAlexander Duyck 		getall = 1;
2752f90b865SAlexander Duyck 
2762f90b865SAlexander Duyck 	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
2772f90b865SAlexander Duyck 		if (!getall && !data[i])
2782f90b865SAlexander Duyck 			continue;
2792f90b865SAlexander Duyck 
2802f90b865SAlexander Duyck 		netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
2812f90b865SAlexander Duyck 		                             &value);
2822f90b865SAlexander Duyck 		ret = nla_put_u8(dcbnl_skb, i, value);
2832f90b865SAlexander Duyck 
2842f90b865SAlexander Duyck 		if (ret) {
2852f90b865SAlexander Duyck 			nla_nest_cancel(dcbnl_skb, nest);
2862f90b865SAlexander Duyck 			goto err;
2872f90b865SAlexander Duyck 		}
2882f90b865SAlexander Duyck 	}
2892f90b865SAlexander Duyck 	nla_nest_end(dcbnl_skb, nest);
2902f90b865SAlexander Duyck 
2912f90b865SAlexander Duyck 	nlmsg_end(dcbnl_skb, nlh);
2922f90b865SAlexander Duyck 
2932f90b865SAlexander Duyck 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
2942f90b865SAlexander Duyck 	if (ret)
2957eaf5077SJohn Fastabend 		goto err_out;
2962f90b865SAlexander Duyck 
2972f90b865SAlexander Duyck 	return 0;
2982f90b865SAlexander Duyck nlmsg_failure:
2992f90b865SAlexander Duyck err:
300858eb711SRoel Kluin 	kfree_skb(dcbnl_skb);
3012f90b865SAlexander Duyck err_out:
3022f90b865SAlexander Duyck 	return -EINVAL;
3032f90b865SAlexander Duyck }
3042f90b865SAlexander Duyck 
3052f90b865SAlexander Duyck static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlattr **tb,
3062f90b865SAlexander Duyck                                 u32 pid, u32 seq, u16 flags)
3072f90b865SAlexander Duyck {
3082f90b865SAlexander Duyck 	struct sk_buff *dcbnl_skb;
3092f90b865SAlexander Duyck 	struct nlmsghdr *nlh;
3102f90b865SAlexander Duyck 	struct dcbmsg *dcb;
3112f90b865SAlexander Duyck 	u8 perm_addr[MAX_ADDR_LEN];
3122f90b865SAlexander Duyck 	int ret = -EINVAL;
3132f90b865SAlexander Duyck 
3142f90b865SAlexander Duyck 	if (!netdev->dcbnl_ops->getpermhwaddr)
3152f90b865SAlexander Duyck 		return ret;
3162f90b865SAlexander Duyck 
3172f90b865SAlexander Duyck 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3182f90b865SAlexander Duyck 	if (!dcbnl_skb)
3192f90b865SAlexander Duyck 		goto err_out;
3202f90b865SAlexander Duyck 
3212f90b865SAlexander Duyck 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
3222f90b865SAlexander Duyck 
3232f90b865SAlexander Duyck 	dcb = NLMSG_DATA(nlh);
3242f90b865SAlexander Duyck 	dcb->dcb_family = AF_UNSPEC;
3252f90b865SAlexander Duyck 	dcb->cmd = DCB_CMD_GPERM_HWADDR;
3262f90b865SAlexander Duyck 
3272f90b865SAlexander Duyck 	netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
3282f90b865SAlexander Duyck 
3292f90b865SAlexander Duyck 	ret = nla_put(dcbnl_skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr),
3302f90b865SAlexander Duyck 	              perm_addr);
3312f90b865SAlexander Duyck 
3322f90b865SAlexander Duyck 	nlmsg_end(dcbnl_skb, nlh);
3332f90b865SAlexander Duyck 
3342f90b865SAlexander Duyck 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
3352f90b865SAlexander Duyck 	if (ret)
3367eaf5077SJohn Fastabend 		goto err_out;
3372f90b865SAlexander Duyck 
3382f90b865SAlexander Duyck 	return 0;
3392f90b865SAlexander Duyck 
3402f90b865SAlexander Duyck nlmsg_failure:
341858eb711SRoel Kluin 	kfree_skb(dcbnl_skb);
3422f90b865SAlexander Duyck err_out:
3432f90b865SAlexander Duyck 	return -EINVAL;
3442f90b865SAlexander Duyck }
3452f90b865SAlexander Duyck 
34646132188SAlexander Duyck static int dcbnl_getcap(struct net_device *netdev, struct nlattr **tb,
34746132188SAlexander Duyck                         u32 pid, u32 seq, u16 flags)
34846132188SAlexander Duyck {
34946132188SAlexander Duyck 	struct sk_buff *dcbnl_skb;
35046132188SAlexander Duyck 	struct nlmsghdr *nlh;
35146132188SAlexander Duyck 	struct dcbmsg *dcb;
35246132188SAlexander Duyck 	struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
35346132188SAlexander Duyck 	u8 value;
35446132188SAlexander Duyck 	int ret = -EINVAL;
35546132188SAlexander Duyck 	int i;
35646132188SAlexander Duyck 	int getall = 0;
35746132188SAlexander Duyck 
35846132188SAlexander Duyck 	if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
35946132188SAlexander Duyck 		return ret;
36046132188SAlexander Duyck 
36146132188SAlexander Duyck 	ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
36246132188SAlexander Duyck 	                       dcbnl_cap_nest);
36346132188SAlexander Duyck 	if (ret)
36446132188SAlexander Duyck 		goto err_out;
36546132188SAlexander Duyck 
36646132188SAlexander Duyck 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
36746132188SAlexander Duyck 	if (!dcbnl_skb)
36846132188SAlexander Duyck 		goto err_out;
36946132188SAlexander Duyck 
37046132188SAlexander Duyck 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
37146132188SAlexander Duyck 
37246132188SAlexander Duyck 	dcb = NLMSG_DATA(nlh);
37346132188SAlexander Duyck 	dcb->dcb_family = AF_UNSPEC;
37446132188SAlexander Duyck 	dcb->cmd = DCB_CMD_GCAP;
37546132188SAlexander Duyck 
37646132188SAlexander Duyck 	nest = nla_nest_start(dcbnl_skb, DCB_ATTR_CAP);
37746132188SAlexander Duyck 	if (!nest)
37846132188SAlexander Duyck 		goto err;
37946132188SAlexander Duyck 
38046132188SAlexander Duyck 	if (data[DCB_CAP_ATTR_ALL])
38146132188SAlexander Duyck 		getall = 1;
38246132188SAlexander Duyck 
38346132188SAlexander Duyck 	for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
38446132188SAlexander Duyck 		if (!getall && !data[i])
38546132188SAlexander Duyck 			continue;
38646132188SAlexander Duyck 
38746132188SAlexander Duyck 		if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
38846132188SAlexander Duyck 			ret = nla_put_u8(dcbnl_skb, i, value);
38946132188SAlexander Duyck 
39046132188SAlexander Duyck 			if (ret) {
39146132188SAlexander Duyck 				nla_nest_cancel(dcbnl_skb, nest);
39246132188SAlexander Duyck 				goto err;
39346132188SAlexander Duyck 			}
39446132188SAlexander Duyck 		}
39546132188SAlexander Duyck 	}
39646132188SAlexander Duyck 	nla_nest_end(dcbnl_skb, nest);
39746132188SAlexander Duyck 
39846132188SAlexander Duyck 	nlmsg_end(dcbnl_skb, nlh);
39946132188SAlexander Duyck 
40046132188SAlexander Duyck 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
40146132188SAlexander Duyck 	if (ret)
4027eaf5077SJohn Fastabend 		goto err_out;
40346132188SAlexander Duyck 
40446132188SAlexander Duyck 	return 0;
40546132188SAlexander Duyck nlmsg_failure:
40646132188SAlexander Duyck err:
407858eb711SRoel Kluin 	kfree_skb(dcbnl_skb);
40846132188SAlexander Duyck err_out:
40946132188SAlexander Duyck 	return -EINVAL;
41046132188SAlexander Duyck }
41146132188SAlexander Duyck 
41233dbabc4SAlexander Duyck static int dcbnl_getnumtcs(struct net_device *netdev, struct nlattr **tb,
41333dbabc4SAlexander Duyck                            u32 pid, u32 seq, u16 flags)
41433dbabc4SAlexander Duyck {
41533dbabc4SAlexander Duyck 	struct sk_buff *dcbnl_skb;
41633dbabc4SAlexander Duyck 	struct nlmsghdr *nlh;
41733dbabc4SAlexander Duyck 	struct dcbmsg *dcb;
41833dbabc4SAlexander Duyck 	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
41933dbabc4SAlexander Duyck 	u8 value;
42033dbabc4SAlexander Duyck 	int ret = -EINVAL;
42133dbabc4SAlexander Duyck 	int i;
42233dbabc4SAlexander Duyck 	int getall = 0;
42333dbabc4SAlexander Duyck 
42433dbabc4SAlexander Duyck 	if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
42533dbabc4SAlexander Duyck 		return ret;
42633dbabc4SAlexander Duyck 
42733dbabc4SAlexander Duyck 	ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
42833dbabc4SAlexander Duyck 	                       dcbnl_numtcs_nest);
42933dbabc4SAlexander Duyck 	if (ret) {
43033dbabc4SAlexander Duyck 		ret = -EINVAL;
43133dbabc4SAlexander Duyck 		goto err_out;
43233dbabc4SAlexander Duyck 	}
43333dbabc4SAlexander Duyck 
43433dbabc4SAlexander Duyck 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
43533dbabc4SAlexander Duyck 	if (!dcbnl_skb) {
43633dbabc4SAlexander Duyck 		ret = -EINVAL;
43733dbabc4SAlexander Duyck 		goto err_out;
43833dbabc4SAlexander Duyck 	}
43933dbabc4SAlexander Duyck 
44033dbabc4SAlexander Duyck 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
44133dbabc4SAlexander Duyck 
44233dbabc4SAlexander Duyck 	dcb = NLMSG_DATA(nlh);
44333dbabc4SAlexander Duyck 	dcb->dcb_family = AF_UNSPEC;
44433dbabc4SAlexander Duyck 	dcb->cmd = DCB_CMD_GNUMTCS;
44533dbabc4SAlexander Duyck 
44633dbabc4SAlexander Duyck 	nest = nla_nest_start(dcbnl_skb, DCB_ATTR_NUMTCS);
44733dbabc4SAlexander Duyck 	if (!nest) {
44833dbabc4SAlexander Duyck 		ret = -EINVAL;
44933dbabc4SAlexander Duyck 		goto err;
45033dbabc4SAlexander Duyck 	}
45133dbabc4SAlexander Duyck 
45233dbabc4SAlexander Duyck 	if (data[DCB_NUMTCS_ATTR_ALL])
45333dbabc4SAlexander Duyck 		getall = 1;
45433dbabc4SAlexander Duyck 
45533dbabc4SAlexander Duyck 	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
45633dbabc4SAlexander Duyck 		if (!getall && !data[i])
45733dbabc4SAlexander Duyck 			continue;
45833dbabc4SAlexander Duyck 
45933dbabc4SAlexander Duyck 		ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
46033dbabc4SAlexander Duyck 		if (!ret) {
46133dbabc4SAlexander Duyck 			ret = nla_put_u8(dcbnl_skb, i, value);
46233dbabc4SAlexander Duyck 
46333dbabc4SAlexander Duyck 			if (ret) {
46433dbabc4SAlexander Duyck 				nla_nest_cancel(dcbnl_skb, nest);
46533dbabc4SAlexander Duyck 				ret = -EINVAL;
46633dbabc4SAlexander Duyck 				goto err;
46733dbabc4SAlexander Duyck 			}
46833dbabc4SAlexander Duyck 		} else {
46933dbabc4SAlexander Duyck 			goto err;
47033dbabc4SAlexander Duyck 		}
47133dbabc4SAlexander Duyck 	}
47233dbabc4SAlexander Duyck 	nla_nest_end(dcbnl_skb, nest);
47333dbabc4SAlexander Duyck 
47433dbabc4SAlexander Duyck 	nlmsg_end(dcbnl_skb, nlh);
47533dbabc4SAlexander Duyck 
47633dbabc4SAlexander Duyck 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
47733dbabc4SAlexander Duyck 	if (ret) {
47833dbabc4SAlexander Duyck 		ret = -EINVAL;
4797eaf5077SJohn Fastabend 		goto err_out;
48033dbabc4SAlexander Duyck 	}
48133dbabc4SAlexander Duyck 
48233dbabc4SAlexander Duyck 	return 0;
48333dbabc4SAlexander Duyck nlmsg_failure:
48433dbabc4SAlexander Duyck err:
485858eb711SRoel Kluin 	kfree_skb(dcbnl_skb);
48633dbabc4SAlexander Duyck err_out:
48733dbabc4SAlexander Duyck 	return ret;
48833dbabc4SAlexander Duyck }
48933dbabc4SAlexander Duyck 
49033dbabc4SAlexander Duyck static int dcbnl_setnumtcs(struct net_device *netdev, struct nlattr **tb,
49133dbabc4SAlexander Duyck                            u32 pid, u32 seq, u16 flags)
49233dbabc4SAlexander Duyck {
49333dbabc4SAlexander Duyck 	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
49433dbabc4SAlexander Duyck 	int ret = -EINVAL;
49533dbabc4SAlexander Duyck 	u8 value;
49633dbabc4SAlexander Duyck 	int i;
49733dbabc4SAlexander Duyck 
4988b124a8eSDon Skidmore 	if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
49933dbabc4SAlexander Duyck 		return ret;
50033dbabc4SAlexander Duyck 
50133dbabc4SAlexander Duyck 	ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
50233dbabc4SAlexander Duyck 	                       dcbnl_numtcs_nest);
50333dbabc4SAlexander Duyck 
50433dbabc4SAlexander Duyck 	if (ret) {
50533dbabc4SAlexander Duyck 		ret = -EINVAL;
50633dbabc4SAlexander Duyck 		goto err;
50733dbabc4SAlexander Duyck 	}
50833dbabc4SAlexander Duyck 
50933dbabc4SAlexander Duyck 	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
51033dbabc4SAlexander Duyck 		if (data[i] == NULL)
51133dbabc4SAlexander Duyck 			continue;
51233dbabc4SAlexander Duyck 
51333dbabc4SAlexander Duyck 		value = nla_get_u8(data[i]);
51433dbabc4SAlexander Duyck 
51533dbabc4SAlexander Duyck 		ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
51633dbabc4SAlexander Duyck 
51733dbabc4SAlexander Duyck 		if (ret)
51833dbabc4SAlexander Duyck 			goto operr;
51933dbabc4SAlexander Duyck 	}
52033dbabc4SAlexander Duyck 
52133dbabc4SAlexander Duyck operr:
52233dbabc4SAlexander Duyck 	ret = dcbnl_reply(!!ret, RTM_SETDCB, DCB_CMD_SNUMTCS,
52333dbabc4SAlexander Duyck 	                  DCB_ATTR_NUMTCS, pid, seq, flags);
52433dbabc4SAlexander Duyck 
52533dbabc4SAlexander Duyck err:
52633dbabc4SAlexander Duyck 	return ret;
52733dbabc4SAlexander Duyck }
52833dbabc4SAlexander Duyck 
5290eb3aa9bSAlexander Duyck static int dcbnl_getpfcstate(struct net_device *netdev, struct nlattr **tb,
5300eb3aa9bSAlexander Duyck                              u32 pid, u32 seq, u16 flags)
5310eb3aa9bSAlexander Duyck {
5320eb3aa9bSAlexander Duyck 	int ret = -EINVAL;
5330eb3aa9bSAlexander Duyck 
5340eb3aa9bSAlexander Duyck 	if (!netdev->dcbnl_ops->getpfcstate)
5350eb3aa9bSAlexander Duyck 		return ret;
5360eb3aa9bSAlexander Duyck 
5370eb3aa9bSAlexander Duyck 	ret = dcbnl_reply(netdev->dcbnl_ops->getpfcstate(netdev), RTM_GETDCB,
5380eb3aa9bSAlexander Duyck 	                  DCB_CMD_PFC_GSTATE, DCB_ATTR_PFC_STATE,
5390eb3aa9bSAlexander Duyck 	                  pid, seq, flags);
5400eb3aa9bSAlexander Duyck 
5410eb3aa9bSAlexander Duyck 	return ret;
5420eb3aa9bSAlexander Duyck }
5430eb3aa9bSAlexander Duyck 
5440eb3aa9bSAlexander Duyck static int dcbnl_setpfcstate(struct net_device *netdev, struct nlattr **tb,
5450eb3aa9bSAlexander Duyck                              u32 pid, u32 seq, u16 flags)
5460eb3aa9bSAlexander Duyck {
5470eb3aa9bSAlexander Duyck 	int ret = -EINVAL;
5480eb3aa9bSAlexander Duyck 	u8 value;
5490eb3aa9bSAlexander Duyck 
5500eb3aa9bSAlexander Duyck 	if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
5510eb3aa9bSAlexander Duyck 		return ret;
5520eb3aa9bSAlexander Duyck 
5530eb3aa9bSAlexander Duyck 	value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
5540eb3aa9bSAlexander Duyck 
5550eb3aa9bSAlexander Duyck 	netdev->dcbnl_ops->setpfcstate(netdev, value);
5560eb3aa9bSAlexander Duyck 
5570eb3aa9bSAlexander Duyck 	ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE,
5580eb3aa9bSAlexander Duyck 	                  pid, seq, flags);
5590eb3aa9bSAlexander Duyck 
5600eb3aa9bSAlexander Duyck 	return ret;
5610eb3aa9bSAlexander Duyck }
5620eb3aa9bSAlexander Duyck 
56357949686SYi Zou static int dcbnl_getapp(struct net_device *netdev, struct nlattr **tb,
56457949686SYi Zou                         u32 pid, u32 seq, u16 flags)
56557949686SYi Zou {
56657949686SYi Zou 	struct sk_buff *dcbnl_skb;
56757949686SYi Zou 	struct nlmsghdr *nlh;
56857949686SYi Zou 	struct dcbmsg *dcb;
56957949686SYi Zou 	struct nlattr *app_nest;
57057949686SYi Zou 	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
57157949686SYi Zou 	u16 id;
57257949686SYi Zou 	u8 up, idtype;
57357949686SYi Zou 	int ret = -EINVAL;
57457949686SYi Zou 
57557949686SYi Zou 	if (!tb[DCB_ATTR_APP] || !netdev->dcbnl_ops->getapp)
57657949686SYi Zou 		goto out;
57757949686SYi Zou 
57857949686SYi Zou 	ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
57957949686SYi Zou 	                       dcbnl_app_nest);
58057949686SYi Zou 	if (ret)
58157949686SYi Zou 		goto out;
58257949686SYi Zou 
58357949686SYi Zou 	ret = -EINVAL;
58457949686SYi Zou 	/* all must be non-null */
58557949686SYi Zou 	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
58657949686SYi Zou 	    (!app_tb[DCB_APP_ATTR_ID]))
58757949686SYi Zou 		goto out;
58857949686SYi Zou 
58957949686SYi Zou 	/* either by eth type or by socket number */
59057949686SYi Zou 	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
59157949686SYi Zou 	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
59257949686SYi Zou 	    (idtype != DCB_APP_IDTYPE_PORTNUM))
59357949686SYi Zou 		goto out;
59457949686SYi Zou 
59557949686SYi Zou 	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
59657949686SYi Zou 	up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
59757949686SYi Zou 
59857949686SYi Zou 	/* send this back */
59957949686SYi Zou 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
60057949686SYi Zou 	if (!dcbnl_skb)
60157949686SYi Zou 		goto out;
60257949686SYi Zou 
60357949686SYi Zou 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
60457949686SYi Zou 	dcb = NLMSG_DATA(nlh);
60557949686SYi Zou 	dcb->dcb_family = AF_UNSPEC;
60657949686SYi Zou 	dcb->cmd = DCB_CMD_GAPP;
60757949686SYi Zou 
60857949686SYi Zou 	app_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_APP);
60957949686SYi Zou 	ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_IDTYPE, idtype);
61057949686SYi Zou 	if (ret)
61157949686SYi Zou 		goto out_cancel;
61257949686SYi Zou 
61357949686SYi Zou 	ret = nla_put_u16(dcbnl_skb, DCB_APP_ATTR_ID, id);
61457949686SYi Zou 	if (ret)
61557949686SYi Zou 		goto out_cancel;
61657949686SYi Zou 
61757949686SYi Zou 	ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_PRIORITY, up);
61857949686SYi Zou 	if (ret)
61957949686SYi Zou 		goto out_cancel;
62057949686SYi Zou 
62157949686SYi Zou 	nla_nest_end(dcbnl_skb, app_nest);
62257949686SYi Zou 	nlmsg_end(dcbnl_skb, nlh);
62357949686SYi Zou 
62457949686SYi Zou 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
62557949686SYi Zou 	if (ret)
62657949686SYi Zou 		goto nlmsg_failure;
62757949686SYi Zou 
62857949686SYi Zou 	goto out;
62957949686SYi Zou 
63057949686SYi Zou out_cancel:
63157949686SYi Zou 	nla_nest_cancel(dcbnl_skb, app_nest);
63257949686SYi Zou nlmsg_failure:
63357949686SYi Zou 	kfree_skb(dcbnl_skb);
63457949686SYi Zou out:
63557949686SYi Zou 	return ret;
63657949686SYi Zou }
63757949686SYi Zou 
63857949686SYi Zou static int dcbnl_setapp(struct net_device *netdev, struct nlattr **tb,
63957949686SYi Zou                         u32 pid, u32 seq, u16 flags)
64057949686SYi Zou {
6419ab933abSJohn Fastabend 	int err, ret = -EINVAL;
64257949686SYi Zou 	u16 id;
64357949686SYi Zou 	u8 up, idtype;
64457949686SYi Zou 	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
64557949686SYi Zou 
6469ab933abSJohn Fastabend 	if (!tb[DCB_ATTR_APP])
64757949686SYi Zou 		goto out;
64857949686SYi Zou 
64957949686SYi Zou 	ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
65057949686SYi Zou 	                       dcbnl_app_nest);
65157949686SYi Zou 	if (ret)
65257949686SYi Zou 		goto out;
65357949686SYi Zou 
65457949686SYi Zou 	ret = -EINVAL;
65557949686SYi Zou 	/* all must be non-null */
65657949686SYi Zou 	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
65757949686SYi Zou 	    (!app_tb[DCB_APP_ATTR_ID]) ||
65857949686SYi Zou 	    (!app_tb[DCB_APP_ATTR_PRIORITY]))
65957949686SYi Zou 		goto out;
66057949686SYi Zou 
66157949686SYi Zou 	/* either by eth type or by socket number */
66257949686SYi Zou 	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
66357949686SYi Zou 	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
66457949686SYi Zou 	    (idtype != DCB_APP_IDTYPE_PORTNUM))
66557949686SYi Zou 		goto out;
66657949686SYi Zou 
66757949686SYi Zou 	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
66857949686SYi Zou 	up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
66957949686SYi Zou 
6709ab933abSJohn Fastabend 	if (netdev->dcbnl_ops->setapp) {
6719ab933abSJohn Fastabend 		err = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
6729ab933abSJohn Fastabend 	} else {
6739ab933abSJohn Fastabend 		struct dcb_app app;
6749ab933abSJohn Fastabend 		app.selector = idtype;
6759ab933abSJohn Fastabend 		app.protocol = id;
6769ab933abSJohn Fastabend 		app.priority = up;
6779ab933abSJohn Fastabend 		err = dcb_setapp(netdev, &app);
6789ab933abSJohn Fastabend 	}
6799ab933abSJohn Fastabend 
6809ab933abSJohn Fastabend 	ret = dcbnl_reply(err, RTM_SETDCB, DCB_CMD_SAPP, DCB_ATTR_APP,
68157949686SYi Zou 			  pid, seq, flags);
68257949686SYi Zou out:
68357949686SYi Zou 	return ret;
68457949686SYi Zou }
68557949686SYi Zou 
6862f90b865SAlexander Duyck static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlattr **tb,
6872f90b865SAlexander Duyck                              u32 pid, u32 seq, u16 flags, int dir)
6882f90b865SAlexander Duyck {
6892f90b865SAlexander Duyck 	struct sk_buff *dcbnl_skb;
6902f90b865SAlexander Duyck 	struct nlmsghdr *nlh;
6912f90b865SAlexander Duyck 	struct dcbmsg *dcb;
6922f90b865SAlexander Duyck 	struct nlattr *pg_nest, *param_nest, *data;
6932f90b865SAlexander Duyck 	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
6942f90b865SAlexander Duyck 	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
6952f90b865SAlexander Duyck 	u8 prio, pgid, tc_pct, up_map;
6962f90b865SAlexander Duyck 	int ret  = -EINVAL;
6972f90b865SAlexander Duyck 	int getall = 0;
6982f90b865SAlexander Duyck 	int i;
6992f90b865SAlexander Duyck 
7002f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_PG_CFG] ||
7012f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->getpgtccfgtx ||
7022f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->getpgtccfgrx ||
7032f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->getpgbwgcfgtx ||
7042f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->getpgbwgcfgrx)
7052f90b865SAlexander Duyck 		return ret;
7062f90b865SAlexander Duyck 
7072f90b865SAlexander Duyck 	ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
7082f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
7092f90b865SAlexander Duyck 
7102f90b865SAlexander Duyck 	if (ret)
7112f90b865SAlexander Duyck 		goto err_out;
7122f90b865SAlexander Duyck 
7132f90b865SAlexander Duyck 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7142f90b865SAlexander Duyck 	if (!dcbnl_skb)
7152f90b865SAlexander Duyck 		goto err_out;
7162f90b865SAlexander Duyck 
7172f90b865SAlexander Duyck 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
7182f90b865SAlexander Duyck 
7192f90b865SAlexander Duyck 	dcb = NLMSG_DATA(nlh);
7202f90b865SAlexander Duyck 	dcb->dcb_family = AF_UNSPEC;
7212f90b865SAlexander Duyck 	dcb->cmd = (dir) ? DCB_CMD_PGRX_GCFG : DCB_CMD_PGTX_GCFG;
7222f90b865SAlexander Duyck 
7232f90b865SAlexander Duyck 	pg_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PG_CFG);
7242f90b865SAlexander Duyck 	if (!pg_nest)
7252f90b865SAlexander Duyck 		goto err;
7262f90b865SAlexander Duyck 
7272f90b865SAlexander Duyck 	if (pg_tb[DCB_PG_ATTR_TC_ALL])
7282f90b865SAlexander Duyck 		getall = 1;
7292f90b865SAlexander Duyck 
7302f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
7312f90b865SAlexander Duyck 		if (!getall && !pg_tb[i])
7322f90b865SAlexander Duyck 			continue;
7332f90b865SAlexander Duyck 
7342f90b865SAlexander Duyck 		if (pg_tb[DCB_PG_ATTR_TC_ALL])
7352f90b865SAlexander Duyck 			data = pg_tb[DCB_PG_ATTR_TC_ALL];
7362f90b865SAlexander Duyck 		else
7372f90b865SAlexander Duyck 			data = pg_tb[i];
7382f90b865SAlexander Duyck 		ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
7392f90b865SAlexander Duyck 				       data, dcbnl_tc_param_nest);
7402f90b865SAlexander Duyck 		if (ret)
7412f90b865SAlexander Duyck 			goto err_pg;
7422f90b865SAlexander Duyck 
7432f90b865SAlexander Duyck 		param_nest = nla_nest_start(dcbnl_skb, i);
7442f90b865SAlexander Duyck 		if (!param_nest)
7452f90b865SAlexander Duyck 			goto err_pg;
7462f90b865SAlexander Duyck 
7472f90b865SAlexander Duyck 		pgid = DCB_ATTR_VALUE_UNDEFINED;
7482f90b865SAlexander Duyck 		prio = DCB_ATTR_VALUE_UNDEFINED;
7492f90b865SAlexander Duyck 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
7502f90b865SAlexander Duyck 		up_map = DCB_ATTR_VALUE_UNDEFINED;
7512f90b865SAlexander Duyck 
7522f90b865SAlexander Duyck 		if (dir) {
7532f90b865SAlexander Duyck 			/* Rx */
7542f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgtccfgrx(netdev,
7552f90b865SAlexander Duyck 						i - DCB_PG_ATTR_TC_0, &prio,
7562f90b865SAlexander Duyck 						&pgid, &tc_pct, &up_map);
7572f90b865SAlexander Duyck 		} else {
7582f90b865SAlexander Duyck 			/* Tx */
7592f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgtccfgtx(netdev,
7602f90b865SAlexander Duyck 						i - DCB_PG_ATTR_TC_0, &prio,
7612f90b865SAlexander Duyck 						&pgid, &tc_pct, &up_map);
7622f90b865SAlexander Duyck 		}
7632f90b865SAlexander Duyck 
7642f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
7652f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7662f90b865SAlexander Duyck 			ret = nla_put_u8(dcbnl_skb,
7672f90b865SAlexander Duyck 			                 DCB_TC_ATTR_PARAM_PGID, pgid);
7682f90b865SAlexander Duyck 			if (ret)
7692f90b865SAlexander Duyck 				goto err_param;
7702f90b865SAlexander Duyck 		}
7712f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
7722f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7732f90b865SAlexander Duyck 			ret = nla_put_u8(dcbnl_skb,
7742f90b865SAlexander Duyck 			                 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
7752f90b865SAlexander Duyck 			if (ret)
7762f90b865SAlexander Duyck 				goto err_param;
7772f90b865SAlexander Duyck 		}
7782f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
7792f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7802f90b865SAlexander Duyck 			ret = nla_put_u8(dcbnl_skb,
7812f90b865SAlexander Duyck 			                 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
7822f90b865SAlexander Duyck 			if (ret)
7832f90b865SAlexander Duyck 				goto err_param;
7842f90b865SAlexander Duyck 		}
7852f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
7862f90b865SAlexander Duyck 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
7872f90b865SAlexander Duyck 			ret = nla_put_u8(dcbnl_skb, DCB_TC_ATTR_PARAM_BW_PCT,
7882f90b865SAlexander Duyck 			                 tc_pct);
7892f90b865SAlexander Duyck 			if (ret)
7902f90b865SAlexander Duyck 				goto err_param;
7912f90b865SAlexander Duyck 		}
7922f90b865SAlexander Duyck 		nla_nest_end(dcbnl_skb, param_nest);
7932f90b865SAlexander Duyck 	}
7942f90b865SAlexander Duyck 
7952f90b865SAlexander Duyck 	if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
7962f90b865SAlexander Duyck 		getall = 1;
7972f90b865SAlexander Duyck 	else
7982f90b865SAlexander Duyck 		getall = 0;
7992f90b865SAlexander Duyck 
8002f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
8012f90b865SAlexander Duyck 		if (!getall && !pg_tb[i])
8022f90b865SAlexander Duyck 			continue;
8032f90b865SAlexander Duyck 
8042f90b865SAlexander Duyck 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
8052f90b865SAlexander Duyck 
8062f90b865SAlexander Duyck 		if (dir) {
8072f90b865SAlexander Duyck 			/* Rx */
8082f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
8092f90b865SAlexander Duyck 					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
8102f90b865SAlexander Duyck 		} else {
8112f90b865SAlexander Duyck 			/* Tx */
8122f90b865SAlexander Duyck 			netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
8132f90b865SAlexander Duyck 					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
8142f90b865SAlexander Duyck 		}
8152f90b865SAlexander Duyck 		ret = nla_put_u8(dcbnl_skb, i, tc_pct);
8162f90b865SAlexander Duyck 
8172f90b865SAlexander Duyck 		if (ret)
8182f90b865SAlexander Duyck 			goto err_pg;
8192f90b865SAlexander Duyck 	}
8202f90b865SAlexander Duyck 
8212f90b865SAlexander Duyck 	nla_nest_end(dcbnl_skb, pg_nest);
8222f90b865SAlexander Duyck 
8232f90b865SAlexander Duyck 	nlmsg_end(dcbnl_skb, nlh);
8242f90b865SAlexander Duyck 
8252f90b865SAlexander Duyck 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
8262f90b865SAlexander Duyck 	if (ret)
8277eaf5077SJohn Fastabend 		goto err_out;
8282f90b865SAlexander Duyck 
8292f90b865SAlexander Duyck 	return 0;
8302f90b865SAlexander Duyck 
8312f90b865SAlexander Duyck err_param:
8322f90b865SAlexander Duyck 	nla_nest_cancel(dcbnl_skb, param_nest);
8332f90b865SAlexander Duyck err_pg:
8342f90b865SAlexander Duyck 	nla_nest_cancel(dcbnl_skb, pg_nest);
8352f90b865SAlexander Duyck nlmsg_failure:
8362f90b865SAlexander Duyck err:
837858eb711SRoel Kluin 	kfree_skb(dcbnl_skb);
8382f90b865SAlexander Duyck err_out:
8392f90b865SAlexander Duyck 	ret  = -EINVAL;
8402f90b865SAlexander Duyck 	return ret;
8412f90b865SAlexander Duyck }
8422f90b865SAlexander Duyck 
8432f90b865SAlexander Duyck static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlattr **tb,
8442f90b865SAlexander Duyck                              u32 pid, u32 seq, u16 flags)
8452f90b865SAlexander Duyck {
8462f90b865SAlexander Duyck 	return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 0);
8472f90b865SAlexander Duyck }
8482f90b865SAlexander Duyck 
8492f90b865SAlexander Duyck static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlattr **tb,
8502f90b865SAlexander Duyck                              u32 pid, u32 seq, u16 flags)
8512f90b865SAlexander Duyck {
8522f90b865SAlexander Duyck 	return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 1);
8532f90b865SAlexander Duyck }
8542f90b865SAlexander Duyck 
8552f90b865SAlexander Duyck static int dcbnl_setstate(struct net_device *netdev, struct nlattr **tb,
8562f90b865SAlexander Duyck                           u32 pid, u32 seq, u16 flags)
8572f90b865SAlexander Duyck {
8582f90b865SAlexander Duyck 	int ret = -EINVAL;
8592f90b865SAlexander Duyck 	u8 value;
8602f90b865SAlexander Duyck 
8612f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
8622f90b865SAlexander Duyck 		return ret;
8632f90b865SAlexander Duyck 
8642f90b865SAlexander Duyck 	value = nla_get_u8(tb[DCB_ATTR_STATE]);
8652f90b865SAlexander Duyck 
8661486a61eSDon Skidmore 	ret = dcbnl_reply(netdev->dcbnl_ops->setstate(netdev, value),
8671486a61eSDon Skidmore 	                  RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
8682f90b865SAlexander Duyck 	                  pid, seq, flags);
8692f90b865SAlexander Duyck 
8702f90b865SAlexander Duyck 	return ret;
8712f90b865SAlexander Duyck }
8722f90b865SAlexander Duyck 
8732f90b865SAlexander Duyck static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
8742f90b865SAlexander Duyck                            u32 pid, u32 seq, u16 flags)
8752f90b865SAlexander Duyck {
8762f90b865SAlexander Duyck 	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
8772f90b865SAlexander Duyck 	int i;
8782f90b865SAlexander Duyck 	int ret = -EINVAL;
8792f90b865SAlexander Duyck 	u8 value;
8802f90b865SAlexander Duyck 
8812f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
8822f90b865SAlexander Duyck 		return ret;
8832f90b865SAlexander Duyck 
8842f90b865SAlexander Duyck 	ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
8852f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PFC_CFG],
8862f90b865SAlexander Duyck 	                       dcbnl_pfc_up_nest);
8872f90b865SAlexander Duyck 	if (ret)
8882f90b865SAlexander Duyck 		goto err;
8892f90b865SAlexander Duyck 
8902f90b865SAlexander Duyck 	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
8912f90b865SAlexander Duyck 		if (data[i] == NULL)
8922f90b865SAlexander Duyck 			continue;
8932f90b865SAlexander Duyck 		value = nla_get_u8(data[i]);
8942f90b865SAlexander Duyck 		netdev->dcbnl_ops->setpfccfg(netdev,
8952f90b865SAlexander Duyck 			data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
8962f90b865SAlexander Duyck 	}
8972f90b865SAlexander Duyck 
8982f90b865SAlexander Duyck 	ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
8992f90b865SAlexander Duyck 	                  pid, seq, flags);
9002f90b865SAlexander Duyck err:
9012f90b865SAlexander Duyck 	return ret;
9022f90b865SAlexander Duyck }
9032f90b865SAlexander Duyck 
9042f90b865SAlexander Duyck static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
9052f90b865SAlexander Duyck                         u32 pid, u32 seq, u16 flags)
9062f90b865SAlexander Duyck {
9072f90b865SAlexander Duyck 	int ret = -EINVAL;
9082f90b865SAlexander Duyck 
9092f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
9102f90b865SAlexander Duyck 		return ret;
9112f90b865SAlexander Duyck 
9122f90b865SAlexander Duyck 	ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
9132f90b865SAlexander Duyck 	                  DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
9142f90b865SAlexander Duyck 
9152f90b865SAlexander Duyck 	return ret;
9162f90b865SAlexander Duyck }
9172f90b865SAlexander Duyck 
9182f90b865SAlexander Duyck static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
9192f90b865SAlexander Duyck                              u32 pid, u32 seq, u16 flags, int dir)
9202f90b865SAlexander Duyck {
9212f90b865SAlexander Duyck 	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
9222f90b865SAlexander Duyck 	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
9232f90b865SAlexander Duyck 	int ret = -EINVAL;
9242f90b865SAlexander Duyck 	int i;
9252f90b865SAlexander Duyck 	u8 pgid;
9262f90b865SAlexander Duyck 	u8 up_map;
9272f90b865SAlexander Duyck 	u8 prio;
9282f90b865SAlexander Duyck 	u8 tc_pct;
9292f90b865SAlexander Duyck 
9302f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_PG_CFG] ||
9312f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->setpgtccfgtx ||
9322f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->setpgtccfgrx ||
9332f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->setpgbwgcfgtx ||
9342f90b865SAlexander Duyck 	    !netdev->dcbnl_ops->setpgbwgcfgrx)
9352f90b865SAlexander Duyck 		return ret;
9362f90b865SAlexander Duyck 
9372f90b865SAlexander Duyck 	ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
9382f90b865SAlexander Duyck 	                       tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
9392f90b865SAlexander Duyck 	if (ret)
9402f90b865SAlexander Duyck 		goto err;
9412f90b865SAlexander Duyck 
9422f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
9432f90b865SAlexander Duyck 		if (!pg_tb[i])
9442f90b865SAlexander Duyck 			continue;
9452f90b865SAlexander Duyck 
9462f90b865SAlexander Duyck 		ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
9472f90b865SAlexander Duyck 		                       pg_tb[i], dcbnl_tc_param_nest);
9482f90b865SAlexander Duyck 		if (ret)
9492f90b865SAlexander Duyck 			goto err;
9502f90b865SAlexander Duyck 
9512f90b865SAlexander Duyck 		pgid = DCB_ATTR_VALUE_UNDEFINED;
9522f90b865SAlexander Duyck 		prio = DCB_ATTR_VALUE_UNDEFINED;
9532f90b865SAlexander Duyck 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
9542f90b865SAlexander Duyck 		up_map = DCB_ATTR_VALUE_UNDEFINED;
9552f90b865SAlexander Duyck 
9562f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
9572f90b865SAlexander Duyck 			prio =
9582f90b865SAlexander Duyck 			    nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
9592f90b865SAlexander Duyck 
9602f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_PGID])
9612f90b865SAlexander Duyck 			pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
9622f90b865SAlexander Duyck 
9632f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
9642f90b865SAlexander Duyck 			tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
9652f90b865SAlexander Duyck 
9662f90b865SAlexander Duyck 		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
9672f90b865SAlexander Duyck 			up_map =
9682f90b865SAlexander Duyck 			     nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
9692f90b865SAlexander Duyck 
9702f90b865SAlexander Duyck 		/* dir: Tx = 0, Rx = 1 */
9712f90b865SAlexander Duyck 		if (dir) {
9722f90b865SAlexander Duyck 			/* Rx */
9732f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgtccfgrx(netdev,
9742f90b865SAlexander Duyck 				i - DCB_PG_ATTR_TC_0,
9752f90b865SAlexander Duyck 				prio, pgid, tc_pct, up_map);
9762f90b865SAlexander Duyck 		} else {
9772f90b865SAlexander Duyck 			/* Tx */
9782f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgtccfgtx(netdev,
9792f90b865SAlexander Duyck 				i - DCB_PG_ATTR_TC_0,
9802f90b865SAlexander Duyck 				prio, pgid, tc_pct, up_map);
9812f90b865SAlexander Duyck 		}
9822f90b865SAlexander Duyck 	}
9832f90b865SAlexander Duyck 
9842f90b865SAlexander Duyck 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
9852f90b865SAlexander Duyck 		if (!pg_tb[i])
9862f90b865SAlexander Duyck 			continue;
9872f90b865SAlexander Duyck 
9882f90b865SAlexander Duyck 		tc_pct = nla_get_u8(pg_tb[i]);
9892f90b865SAlexander Duyck 
9902f90b865SAlexander Duyck 		/* dir: Tx = 0, Rx = 1 */
9912f90b865SAlexander Duyck 		if (dir) {
9922f90b865SAlexander Duyck 			/* Rx */
9932f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
9942f90b865SAlexander Duyck 					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
9952f90b865SAlexander Duyck 		} else {
9962f90b865SAlexander Duyck 			/* Tx */
9972f90b865SAlexander Duyck 			netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
9982f90b865SAlexander Duyck 					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
9992f90b865SAlexander Duyck 		}
10002f90b865SAlexander Duyck 	}
10012f90b865SAlexander Duyck 
10022f90b865SAlexander Duyck 	ret = dcbnl_reply(0, RTM_SETDCB,
10032f90b865SAlexander Duyck 			  (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
10042f90b865SAlexander Duyck 			  DCB_ATTR_PG_CFG, pid, seq, flags);
10052f90b865SAlexander Duyck 
10062f90b865SAlexander Duyck err:
10072f90b865SAlexander Duyck 	return ret;
10082f90b865SAlexander Duyck }
10092f90b865SAlexander Duyck 
10102f90b865SAlexander Duyck static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
10112f90b865SAlexander Duyck                              u32 pid, u32 seq, u16 flags)
10122f90b865SAlexander Duyck {
10132f90b865SAlexander Duyck 	return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
10142f90b865SAlexander Duyck }
10152f90b865SAlexander Duyck 
10162f90b865SAlexander Duyck static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
10172f90b865SAlexander Duyck                              u32 pid, u32 seq, u16 flags)
10182f90b865SAlexander Duyck {
10192f90b865SAlexander Duyck 	return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
10202f90b865SAlexander Duyck }
10212f90b865SAlexander Duyck 
1022859ee3c4SAlexander Duyck static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlattr **tb,
1023859ee3c4SAlexander Duyck                             u32 pid, u32 seq, u16 flags)
1024859ee3c4SAlexander Duyck {
1025859ee3c4SAlexander Duyck 	struct sk_buff *dcbnl_skb;
1026859ee3c4SAlexander Duyck 	struct nlmsghdr *nlh;
1027859ee3c4SAlexander Duyck 	struct dcbmsg *dcb;
1028859ee3c4SAlexander Duyck 	struct nlattr *bcn_nest;
1029859ee3c4SAlexander Duyck 	struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
1030859ee3c4SAlexander Duyck 	u8 value_byte;
1031859ee3c4SAlexander Duyck 	u32 value_integer;
1032859ee3c4SAlexander Duyck 	int ret  = -EINVAL;
1033859ee3c4SAlexander Duyck 	bool getall = false;
1034859ee3c4SAlexander Duyck 	int i;
1035859ee3c4SAlexander Duyck 
1036859ee3c4SAlexander Duyck 	if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
1037859ee3c4SAlexander Duyck 	    !netdev->dcbnl_ops->getbcncfg)
1038859ee3c4SAlexander Duyck 		return ret;
1039859ee3c4SAlexander Duyck 
1040859ee3c4SAlexander Duyck 	ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
1041859ee3c4SAlexander Duyck 	                       tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
1042859ee3c4SAlexander Duyck 
1043859ee3c4SAlexander Duyck 	if (ret)
1044859ee3c4SAlexander Duyck 		goto err_out;
1045859ee3c4SAlexander Duyck 
1046859ee3c4SAlexander Duyck 	dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1047859ee3c4SAlexander Duyck 	if (!dcbnl_skb)
1048859ee3c4SAlexander Duyck 		goto err_out;
1049859ee3c4SAlexander Duyck 
1050859ee3c4SAlexander Duyck 	nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
1051859ee3c4SAlexander Duyck 
1052859ee3c4SAlexander Duyck 	dcb = NLMSG_DATA(nlh);
1053859ee3c4SAlexander Duyck 	dcb->dcb_family = AF_UNSPEC;
1054859ee3c4SAlexander Duyck 	dcb->cmd = DCB_CMD_BCN_GCFG;
1055859ee3c4SAlexander Duyck 
1056859ee3c4SAlexander Duyck 	bcn_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_BCN);
1057859ee3c4SAlexander Duyck 	if (!bcn_nest)
1058859ee3c4SAlexander Duyck 		goto err;
1059859ee3c4SAlexander Duyck 
1060859ee3c4SAlexander Duyck 	if (bcn_tb[DCB_BCN_ATTR_ALL])
1061859ee3c4SAlexander Duyck 		getall = true;
1062859ee3c4SAlexander Duyck 
1063859ee3c4SAlexander Duyck 	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
1064859ee3c4SAlexander Duyck 		if (!getall && !bcn_tb[i])
1065859ee3c4SAlexander Duyck 			continue;
1066859ee3c4SAlexander Duyck 
1067859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
1068859ee3c4SAlexander Duyck 		                            &value_byte);
1069859ee3c4SAlexander Duyck 		ret = nla_put_u8(dcbnl_skb, i, value_byte);
1070859ee3c4SAlexander Duyck 		if (ret)
1071859ee3c4SAlexander Duyck 			goto err_bcn;
1072859ee3c4SAlexander Duyck 	}
1073859ee3c4SAlexander Duyck 
1074f4314e81SDon Skidmore 	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
1075859ee3c4SAlexander Duyck 		if (!getall && !bcn_tb[i])
1076859ee3c4SAlexander Duyck 			continue;
1077859ee3c4SAlexander Duyck 
1078859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->getbcncfg(netdev, i,
1079859ee3c4SAlexander Duyck 		                             &value_integer);
1080859ee3c4SAlexander Duyck 		ret = nla_put_u32(dcbnl_skb, i, value_integer);
1081859ee3c4SAlexander Duyck 		if (ret)
1082859ee3c4SAlexander Duyck 			goto err_bcn;
1083859ee3c4SAlexander Duyck 	}
1084859ee3c4SAlexander Duyck 
1085859ee3c4SAlexander Duyck 	nla_nest_end(dcbnl_skb, bcn_nest);
1086859ee3c4SAlexander Duyck 
1087859ee3c4SAlexander Duyck 	nlmsg_end(dcbnl_skb, nlh);
1088859ee3c4SAlexander Duyck 
1089859ee3c4SAlexander Duyck 	ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
1090859ee3c4SAlexander Duyck 	if (ret)
10917eaf5077SJohn Fastabend 		goto err_out;
1092859ee3c4SAlexander Duyck 
1093859ee3c4SAlexander Duyck 	return 0;
1094859ee3c4SAlexander Duyck 
1095859ee3c4SAlexander Duyck err_bcn:
1096859ee3c4SAlexander Duyck 	nla_nest_cancel(dcbnl_skb, bcn_nest);
1097859ee3c4SAlexander Duyck nlmsg_failure:
1098859ee3c4SAlexander Duyck err:
1099858eb711SRoel Kluin 	kfree_skb(dcbnl_skb);
1100859ee3c4SAlexander Duyck err_out:
1101859ee3c4SAlexander Duyck 	ret  = -EINVAL;
1102859ee3c4SAlexander Duyck 	return ret;
1103859ee3c4SAlexander Duyck }
1104859ee3c4SAlexander Duyck 
1105859ee3c4SAlexander Duyck static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlattr **tb,
1106859ee3c4SAlexander Duyck                             u32 pid, u32 seq, u16 flags)
1107859ee3c4SAlexander Duyck {
1108859ee3c4SAlexander Duyck 	struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
1109859ee3c4SAlexander Duyck 	int i;
1110859ee3c4SAlexander Duyck 	int ret = -EINVAL;
1111859ee3c4SAlexander Duyck 	u8 value_byte;
1112859ee3c4SAlexander Duyck 	u32 value_int;
1113859ee3c4SAlexander Duyck 
1114f64f9e71SJoe Perches 	if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg ||
1115f64f9e71SJoe Perches 	    !netdev->dcbnl_ops->setbcnrp)
1116859ee3c4SAlexander Duyck 		return ret;
1117859ee3c4SAlexander Duyck 
1118859ee3c4SAlexander Duyck 	ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
1119859ee3c4SAlexander Duyck 	                       tb[DCB_ATTR_BCN],
1120859ee3c4SAlexander Duyck 	                       dcbnl_pfc_up_nest);
1121859ee3c4SAlexander Duyck 	if (ret)
1122859ee3c4SAlexander Duyck 		goto err;
1123859ee3c4SAlexander Duyck 
1124859ee3c4SAlexander Duyck 	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
1125859ee3c4SAlexander Duyck 		if (data[i] == NULL)
1126859ee3c4SAlexander Duyck 			continue;
1127859ee3c4SAlexander Duyck 		value_byte = nla_get_u8(data[i]);
1128859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->setbcnrp(netdev,
1129859ee3c4SAlexander Duyck 			data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
1130859ee3c4SAlexander Duyck 	}
1131859ee3c4SAlexander Duyck 
1132f4314e81SDon Skidmore 	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
1133859ee3c4SAlexander Duyck 		if (data[i] == NULL)
1134859ee3c4SAlexander Duyck 			continue;
1135859ee3c4SAlexander Duyck 		value_int = nla_get_u32(data[i]);
1136859ee3c4SAlexander Duyck 		netdev->dcbnl_ops->setbcncfg(netdev,
1137859ee3c4SAlexander Duyck 	                                     i, value_int);
1138859ee3c4SAlexander Duyck 	}
1139859ee3c4SAlexander Duyck 
1140859ee3c4SAlexander Duyck 	ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_BCN_SCFG, DCB_ATTR_BCN,
1141859ee3c4SAlexander Duyck 	                  pid, seq, flags);
1142859ee3c4SAlexander Duyck err:
1143859ee3c4SAlexander Duyck 	return ret;
1144859ee3c4SAlexander Duyck }
1145859ee3c4SAlexander Duyck 
11463e29027aSJohn Fastabend /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
11473e29027aSJohn Fastabend  * be completed the entire msg is aborted and error value is returned.
11483e29027aSJohn Fastabend  * No attempt is made to reconcile the case where only part of the
11493e29027aSJohn Fastabend  * cmd can be completed.
11503e29027aSJohn Fastabend  */
11513e29027aSJohn Fastabend static int dcbnl_ieee_set(struct net_device *netdev, struct nlattr **tb,
11523e29027aSJohn Fastabend 			  u32 pid, u32 seq, u16 flags)
11533e29027aSJohn Fastabend {
11543e29027aSJohn Fastabend 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
11553e29027aSJohn Fastabend 	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
11563e29027aSJohn Fastabend 	int err = -EOPNOTSUPP;
11573e29027aSJohn Fastabend 
11583e29027aSJohn Fastabend 	if (!ops)
11593e29027aSJohn Fastabend 		goto err;
11603e29027aSJohn Fastabend 
11613e29027aSJohn Fastabend 	err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
11623e29027aSJohn Fastabend 			       tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
11633e29027aSJohn Fastabend 	if (err)
11643e29027aSJohn Fastabend 		goto err;
11653e29027aSJohn Fastabend 
11663e29027aSJohn Fastabend 	if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
11673e29027aSJohn Fastabend 		struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
11683e29027aSJohn Fastabend 		err = ops->ieee_setets(netdev, ets);
11693e29027aSJohn Fastabend 		if (err)
11703e29027aSJohn Fastabend 			goto err;
11713e29027aSJohn Fastabend 	}
11723e29027aSJohn Fastabend 
11733e29027aSJohn Fastabend 	if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setets) {
11743e29027aSJohn Fastabend 		struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
11753e29027aSJohn Fastabend 		err = ops->ieee_setpfc(netdev, pfc);
11763e29027aSJohn Fastabend 		if (err)
11773e29027aSJohn Fastabend 			goto err;
11783e29027aSJohn Fastabend 	}
11793e29027aSJohn Fastabend 
11809ab933abSJohn Fastabend 	if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
11813e29027aSJohn Fastabend 		struct nlattr *attr;
11823e29027aSJohn Fastabend 		int rem;
11833e29027aSJohn Fastabend 
11843e29027aSJohn Fastabend 		nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
11853e29027aSJohn Fastabend 			struct dcb_app *app_data;
11863e29027aSJohn Fastabend 			if (nla_type(attr) != DCB_ATTR_IEEE_APP)
11873e29027aSJohn Fastabend 				continue;
11883e29027aSJohn Fastabend 			app_data = nla_data(attr);
11899ab933abSJohn Fastabend 			if (ops->ieee_setapp)
11903e29027aSJohn Fastabend 				err = ops->ieee_setapp(netdev, app_data);
11919ab933abSJohn Fastabend 			else
11929ab933abSJohn Fastabend 				err = dcb_setapp(netdev, app_data);
11933e29027aSJohn Fastabend 			if (err)
11943e29027aSJohn Fastabend 				goto err;
11953e29027aSJohn Fastabend 		}
11963e29027aSJohn Fastabend 	}
11973e29027aSJohn Fastabend 
11983e29027aSJohn Fastabend err:
11993e29027aSJohn Fastabend 	dcbnl_reply(err, RTM_SETDCB, DCB_CMD_IEEE_SET, DCB_ATTR_IEEE,
12003e29027aSJohn Fastabend 		    pid, seq, flags);
12013e29027aSJohn Fastabend 	return err;
12023e29027aSJohn Fastabend }
12033e29027aSJohn Fastabend 
12043e29027aSJohn Fastabend 
12053e29027aSJohn Fastabend /* Handle IEEE 802.1Qaz GET commands. */
12063e29027aSJohn Fastabend static int dcbnl_ieee_get(struct net_device *netdev, struct nlattr **tb,
12073e29027aSJohn Fastabend 			  u32 pid, u32 seq, u16 flags)
12083e29027aSJohn Fastabend {
12093e29027aSJohn Fastabend 	struct sk_buff *skb;
12103e29027aSJohn Fastabend 	struct nlmsghdr *nlh;
12113e29027aSJohn Fastabend 	struct dcbmsg *dcb;
12129ab933abSJohn Fastabend 	struct nlattr *ieee, *app;
12139ab933abSJohn Fastabend 	struct dcb_app_type *itr;
12143e29027aSJohn Fastabend 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
12153e29027aSJohn Fastabend 	int err;
12163e29027aSJohn Fastabend 
12173e29027aSJohn Fastabend 	if (!ops)
12183e29027aSJohn Fastabend 		return -EOPNOTSUPP;
12193e29027aSJohn Fastabend 
12203e29027aSJohn Fastabend 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12213e29027aSJohn Fastabend 	if (!skb)
12223e29027aSJohn Fastabend 		return -ENOBUFS;
12233e29027aSJohn Fastabend 
12243e29027aSJohn Fastabend 	nlh = NLMSG_NEW(skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
12253e29027aSJohn Fastabend 
12263e29027aSJohn Fastabend 	dcb = NLMSG_DATA(nlh);
12273e29027aSJohn Fastabend 	dcb->dcb_family = AF_UNSPEC;
12283e29027aSJohn Fastabend 	dcb->cmd = DCB_CMD_IEEE_GET;
12293e29027aSJohn Fastabend 
12303e29027aSJohn Fastabend 	NLA_PUT_STRING(skb, DCB_ATTR_IFNAME, netdev->name);
12313e29027aSJohn Fastabend 
12323e29027aSJohn Fastabend 	ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
12333e29027aSJohn Fastabend 	if (!ieee)
12343e29027aSJohn Fastabend 		goto nla_put_failure;
12353e29027aSJohn Fastabend 
12363e29027aSJohn Fastabend 	if (ops->ieee_getets) {
12373e29027aSJohn Fastabend 		struct ieee_ets ets;
12383e29027aSJohn Fastabend 		err = ops->ieee_getets(netdev, &ets);
12393e29027aSJohn Fastabend 		if (!err)
12403e29027aSJohn Fastabend 			NLA_PUT(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets);
12413e29027aSJohn Fastabend 	}
12423e29027aSJohn Fastabend 
12433e29027aSJohn Fastabend 	if (ops->ieee_getpfc) {
12443e29027aSJohn Fastabend 		struct ieee_pfc pfc;
12453e29027aSJohn Fastabend 		err = ops->ieee_getpfc(netdev, &pfc);
12463e29027aSJohn Fastabend 		if (!err)
12473e29027aSJohn Fastabend 			NLA_PUT(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc);
12483e29027aSJohn Fastabend 	}
12493e29027aSJohn Fastabend 
12509ab933abSJohn Fastabend 	app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
12519ab933abSJohn Fastabend 	if (!app)
12529ab933abSJohn Fastabend 		goto nla_put_failure;
12539ab933abSJohn Fastabend 
12549ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
12559ab933abSJohn Fastabend 	list_for_each_entry(itr, &dcb_app_list, list) {
12569ab933abSJohn Fastabend 		if (strncmp(itr->name, netdev->name, IFNAMSIZ) == 0)
12579ab933abSJohn Fastabend 			NLA_PUT(skb, DCB_ATTR_IEEE_APP,
12589ab933abSJohn Fastabend 				sizeof(itr->app), &itr->app);
12599ab933abSJohn Fastabend 	}
12609ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
12619ab933abSJohn Fastabend 	nla_nest_end(skb, app);
12629ab933abSJohn Fastabend 
12633e29027aSJohn Fastabend 	nla_nest_end(skb, ieee);
12643e29027aSJohn Fastabend 	nlmsg_end(skb, nlh);
12653e29027aSJohn Fastabend 
12663e29027aSJohn Fastabend 	return rtnl_unicast(skb, &init_net, pid);
12673e29027aSJohn Fastabend nla_put_failure:
12683e29027aSJohn Fastabend 	nlmsg_cancel(skb, nlh);
12693e29027aSJohn Fastabend nlmsg_failure:
12703e29027aSJohn Fastabend 	kfree_skb(skb);
12713e29027aSJohn Fastabend 	return -1;
12723e29027aSJohn Fastabend }
12733e29027aSJohn Fastabend 
12742f90b865SAlexander Duyck static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
12752f90b865SAlexander Duyck {
12762f90b865SAlexander Duyck 	struct net *net = sock_net(skb->sk);
12772f90b865SAlexander Duyck 	struct net_device *netdev;
12782f90b865SAlexander Duyck 	struct dcbmsg  *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
12792f90b865SAlexander Duyck 	struct nlattr *tb[DCB_ATTR_MAX + 1];
12802f90b865SAlexander Duyck 	u32 pid = skb ? NETLINK_CB(skb).pid : 0;
12812f90b865SAlexander Duyck 	int ret = -EINVAL;
12822f90b865SAlexander Duyck 
128309ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
12842f90b865SAlexander Duyck 		return -EINVAL;
12852f90b865SAlexander Duyck 
12862f90b865SAlexander Duyck 	ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
12872f90b865SAlexander Duyck 			  dcbnl_rtnl_policy);
12882f90b865SAlexander Duyck 	if (ret < 0)
12892f90b865SAlexander Duyck 		return ret;
12902f90b865SAlexander Duyck 
12912f90b865SAlexander Duyck 	if (!tb[DCB_ATTR_IFNAME])
12922f90b865SAlexander Duyck 		return -EINVAL;
12932f90b865SAlexander Duyck 
12942f90b865SAlexander Duyck 	netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
12952f90b865SAlexander Duyck 	if (!netdev)
12962f90b865SAlexander Duyck 		return -EINVAL;
12972f90b865SAlexander Duyck 
12982f90b865SAlexander Duyck 	if (!netdev->dcbnl_ops)
12992f90b865SAlexander Duyck 		goto errout;
13002f90b865SAlexander Duyck 
13012f90b865SAlexander Duyck 	switch (dcb->cmd) {
13022f90b865SAlexander Duyck 	case DCB_CMD_GSTATE:
13032f90b865SAlexander Duyck 		ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
13042f90b865SAlexander Duyck 		                     nlh->nlmsg_flags);
13052f90b865SAlexander Duyck 		goto out;
13062f90b865SAlexander Duyck 	case DCB_CMD_PFC_GCFG:
13072f90b865SAlexander Duyck 		ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
13082f90b865SAlexander Duyck 		                      nlh->nlmsg_flags);
13092f90b865SAlexander Duyck 		goto out;
13102f90b865SAlexander Duyck 	case DCB_CMD_GPERM_HWADDR:
13112f90b865SAlexander Duyck 		ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
13122f90b865SAlexander Duyck 		                           nlh->nlmsg_flags);
13132f90b865SAlexander Duyck 		goto out;
13142f90b865SAlexander Duyck 	case DCB_CMD_PGTX_GCFG:
13152f90b865SAlexander Duyck 		ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
13162f90b865SAlexander Duyck 		                        nlh->nlmsg_flags);
13172f90b865SAlexander Duyck 		goto out;
13182f90b865SAlexander Duyck 	case DCB_CMD_PGRX_GCFG:
13192f90b865SAlexander Duyck 		ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
13202f90b865SAlexander Duyck 		                        nlh->nlmsg_flags);
13212f90b865SAlexander Duyck 		goto out;
1322859ee3c4SAlexander Duyck 	case DCB_CMD_BCN_GCFG:
1323859ee3c4SAlexander Duyck 		ret = dcbnl_bcn_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
1324859ee3c4SAlexander Duyck 		                       nlh->nlmsg_flags);
1325859ee3c4SAlexander Duyck 		goto out;
13262f90b865SAlexander Duyck 	case DCB_CMD_SSTATE:
13272f90b865SAlexander Duyck 		ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
13282f90b865SAlexander Duyck 		                     nlh->nlmsg_flags);
13292f90b865SAlexander Duyck 		goto out;
13302f90b865SAlexander Duyck 	case DCB_CMD_PFC_SCFG:
13312f90b865SAlexander Duyck 		ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
13322f90b865SAlexander Duyck 		                      nlh->nlmsg_flags);
13332f90b865SAlexander Duyck 		goto out;
13342f90b865SAlexander Duyck 
13352f90b865SAlexander Duyck 	case DCB_CMD_SET_ALL:
13362f90b865SAlexander Duyck 		ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
13372f90b865SAlexander Duyck 		                   nlh->nlmsg_flags);
13382f90b865SAlexander Duyck 		goto out;
13392f90b865SAlexander Duyck 	case DCB_CMD_PGTX_SCFG:
13402f90b865SAlexander Duyck 		ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
13412f90b865SAlexander Duyck 		                        nlh->nlmsg_flags);
13422f90b865SAlexander Duyck 		goto out;
13432f90b865SAlexander Duyck 	case DCB_CMD_PGRX_SCFG:
13442f90b865SAlexander Duyck 		ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
13452f90b865SAlexander Duyck 		                        nlh->nlmsg_flags);
13462f90b865SAlexander Duyck 		goto out;
134746132188SAlexander Duyck 	case DCB_CMD_GCAP:
134846132188SAlexander Duyck 		ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
134946132188SAlexander Duyck 		                   nlh->nlmsg_flags);
135046132188SAlexander Duyck 		goto out;
135133dbabc4SAlexander Duyck 	case DCB_CMD_GNUMTCS:
135233dbabc4SAlexander Duyck 		ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
135333dbabc4SAlexander Duyck 		                      nlh->nlmsg_flags);
135433dbabc4SAlexander Duyck 		goto out;
135533dbabc4SAlexander Duyck 	case DCB_CMD_SNUMTCS:
135633dbabc4SAlexander Duyck 		ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
135733dbabc4SAlexander Duyck 		                      nlh->nlmsg_flags);
135833dbabc4SAlexander Duyck 		goto out;
13590eb3aa9bSAlexander Duyck 	case DCB_CMD_PFC_GSTATE:
13600eb3aa9bSAlexander Duyck 		ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
13610eb3aa9bSAlexander Duyck 		                        nlh->nlmsg_flags);
13620eb3aa9bSAlexander Duyck 		goto out;
13630eb3aa9bSAlexander Duyck 	case DCB_CMD_PFC_SSTATE:
13640eb3aa9bSAlexander Duyck 		ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
13650eb3aa9bSAlexander Duyck 		                        nlh->nlmsg_flags);
13660eb3aa9bSAlexander Duyck 		goto out;
1367859ee3c4SAlexander Duyck 	case DCB_CMD_BCN_SCFG:
1368859ee3c4SAlexander Duyck 		ret = dcbnl_bcn_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
1369859ee3c4SAlexander Duyck 		                       nlh->nlmsg_flags);
1370859ee3c4SAlexander Duyck 		goto out;
137157949686SYi Zou 	case DCB_CMD_GAPP:
137257949686SYi Zou 		ret = dcbnl_getapp(netdev, tb, pid, nlh->nlmsg_seq,
137357949686SYi Zou 		                   nlh->nlmsg_flags);
137457949686SYi Zou 		goto out;
137557949686SYi Zou 	case DCB_CMD_SAPP:
137657949686SYi Zou 		ret = dcbnl_setapp(netdev, tb, pid, nlh->nlmsg_seq,
137757949686SYi Zou 		                   nlh->nlmsg_flags);
137857949686SYi Zou 		goto out;
13793e29027aSJohn Fastabend 	case DCB_CMD_IEEE_SET:
13803e29027aSJohn Fastabend 		ret = dcbnl_ieee_set(netdev, tb, pid, nlh->nlmsg_seq,
13813e29027aSJohn Fastabend 				 nlh->nlmsg_flags);
13823e29027aSJohn Fastabend 		goto out;
13833e29027aSJohn Fastabend 	case DCB_CMD_IEEE_GET:
13843e29027aSJohn Fastabend 		ret = dcbnl_ieee_get(netdev, tb, pid, nlh->nlmsg_seq,
13853e29027aSJohn Fastabend 				 nlh->nlmsg_flags);
13863e29027aSJohn Fastabend 		goto out;
13872f90b865SAlexander Duyck 	default:
13882f90b865SAlexander Duyck 		goto errout;
13892f90b865SAlexander Duyck 	}
13902f90b865SAlexander Duyck errout:
13912f90b865SAlexander Duyck 	ret = -EINVAL;
13922f90b865SAlexander Duyck out:
13932f90b865SAlexander Duyck 	dev_put(netdev);
13942f90b865SAlexander Duyck 	return ret;
13952f90b865SAlexander Duyck }
13962f90b865SAlexander Duyck 
13979ab933abSJohn Fastabend /**
13989ab933abSJohn Fastabend  * dcb_getapp - retrieve the DCBX application user priority
13999ab933abSJohn Fastabend  *
14009ab933abSJohn Fastabend  * On success returns a non-zero 802.1p user priority bitmap
14019ab933abSJohn Fastabend  * otherwise returns 0 as the invalid user priority bitmap to
14029ab933abSJohn Fastabend  * indicate an error.
14039ab933abSJohn Fastabend  */
14049ab933abSJohn Fastabend u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
14059ab933abSJohn Fastabend {
14069ab933abSJohn Fastabend 	struct dcb_app_type *itr;
14079ab933abSJohn Fastabend 	u8 prio = 0;
14089ab933abSJohn Fastabend 
14099ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
14109ab933abSJohn Fastabend 	list_for_each_entry(itr, &dcb_app_list, list) {
14119ab933abSJohn Fastabend 		if (itr->app.selector == app->selector &&
14129ab933abSJohn Fastabend 		    itr->app.protocol == app->protocol &&
14139ab933abSJohn Fastabend 		    (strncmp(itr->name, dev->name, IFNAMSIZ) == 0)) {
14149ab933abSJohn Fastabend 			prio = itr->app.priority;
14159ab933abSJohn Fastabend 			break;
14169ab933abSJohn Fastabend 		}
14179ab933abSJohn Fastabend 	}
14189ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
14199ab933abSJohn Fastabend 
14209ab933abSJohn Fastabend 	return prio;
14219ab933abSJohn Fastabend }
14229ab933abSJohn Fastabend EXPORT_SYMBOL(dcb_getapp);
14239ab933abSJohn Fastabend 
14249ab933abSJohn Fastabend /**
14259ab933abSJohn Fastabend  * ixgbe_dcbnl_setapp - add dcb application data to app list
14269ab933abSJohn Fastabend  *
14279ab933abSJohn Fastabend  * Priority 0 is the default priority this removes applications
14289ab933abSJohn Fastabend  * from the app list if the priority is set to zero.
14299ab933abSJohn Fastabend  */
14309ab933abSJohn Fastabend u8 dcb_setapp(struct net_device *dev, struct dcb_app *new)
14319ab933abSJohn Fastabend {
14329ab933abSJohn Fastabend 	struct dcb_app_type *itr;
14339ab933abSJohn Fastabend 
14349ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
14359ab933abSJohn Fastabend 	/* Search for existing match and replace */
14369ab933abSJohn Fastabend 	list_for_each_entry(itr, &dcb_app_list, list) {
14379ab933abSJohn Fastabend 		if (itr->app.selector == new->selector &&
14389ab933abSJohn Fastabend 		    itr->app.protocol == new->protocol &&
14399ab933abSJohn Fastabend 		    (strncmp(itr->name, dev->name, IFNAMSIZ) == 0)) {
14409ab933abSJohn Fastabend 			if (new->priority)
14419ab933abSJohn Fastabend 				itr->app.priority = new->priority;
14429ab933abSJohn Fastabend 			else {
14439ab933abSJohn Fastabend 				list_del(&itr->list);
14449ab933abSJohn Fastabend 				kfree(itr);
14459ab933abSJohn Fastabend 			}
14469ab933abSJohn Fastabend 			goto out;
14479ab933abSJohn Fastabend 		}
14489ab933abSJohn Fastabend 	}
14499ab933abSJohn Fastabend 	/* App type does not exist add new application type */
14509ab933abSJohn Fastabend 	if (new->priority) {
14519ab933abSJohn Fastabend 		struct dcb_app_type *entry;
14529ab933abSJohn Fastabend 		entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
14539ab933abSJohn Fastabend 		if (!entry) {
14549ab933abSJohn Fastabend 			spin_unlock(&dcb_lock);
14559ab933abSJohn Fastabend 			return -ENOMEM;
14569ab933abSJohn Fastabend 		}
14579ab933abSJohn Fastabend 
14589ab933abSJohn Fastabend 		memcpy(&entry->app, new, sizeof(*new));
14599ab933abSJohn Fastabend 		strncpy(entry->name, dev->name, IFNAMSIZ);
14609ab933abSJohn Fastabend 		list_add(&entry->list, &dcb_app_list);
14619ab933abSJohn Fastabend 	}
14629ab933abSJohn Fastabend out:
14639ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
146496b99684SJohn Fastabend 	call_dcbevent_notifiers(DCB_APP_EVENT, new);
14659ab933abSJohn Fastabend 	return 0;
14669ab933abSJohn Fastabend }
14679ab933abSJohn Fastabend EXPORT_SYMBOL(dcb_setapp);
14689ab933abSJohn Fastabend 
14699ab933abSJohn Fastabend void dcb_flushapp(void)
14709ab933abSJohn Fastabend {
14719ab933abSJohn Fastabend 	struct dcb_app_type *app;
14729ab933abSJohn Fastabend 
14739ab933abSJohn Fastabend 	spin_lock(&dcb_lock);
14749ab933abSJohn Fastabend 	list_for_each_entry(app, &dcb_app_list, list) {
14759ab933abSJohn Fastabend 		list_del(&app->list);
14769ab933abSJohn Fastabend 		kfree(app);
14779ab933abSJohn Fastabend 	}
14789ab933abSJohn Fastabend 	spin_unlock(&dcb_lock);
14799ab933abSJohn Fastabend }
14809ab933abSJohn Fastabend 
14812f90b865SAlexander Duyck static int __init dcbnl_init(void)
14822f90b865SAlexander Duyck {
14839ab933abSJohn Fastabend 	INIT_LIST_HEAD(&dcb_app_list);
14849ab933abSJohn Fastabend 
14852f90b865SAlexander Duyck 	rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL);
14862f90b865SAlexander Duyck 	rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL);
14872f90b865SAlexander Duyck 
14882f90b865SAlexander Duyck 	return 0;
14892f90b865SAlexander Duyck }
14902f90b865SAlexander Duyck module_init(dcbnl_init);
14912f90b865SAlexander Duyck 
14922f90b865SAlexander Duyck static void __exit dcbnl_exit(void)
14932f90b865SAlexander Duyck {
14942f90b865SAlexander Duyck 	rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
14952f90b865SAlexander Duyck 	rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
14969ab933abSJohn Fastabend 	dcb_flushapp();
14972f90b865SAlexander Duyck }
14982f90b865SAlexander Duyck module_exit(dcbnl_exit);
1499