dcbnl.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. /*
  2. * Copyright (c) 2008, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Author: Lucy Liu <lucy.liu@intel.com>
  18. */
  19. #include <linux/netdevice.h>
  20. #include <linux/netlink.h>
  21. #include <linux/slab.h>
  22. #include <net/netlink.h>
  23. #include <net/rtnetlink.h>
  24. #include <linux/dcbnl.h>
  25. #include <net/dcbevent.h>
  26. #include <linux/rtnetlink.h>
  27. #include <net/sock.h>
  28. /**
  29. * Data Center Bridging (DCB) is a collection of Ethernet enhancements
  30. * intended to allow network traffic with differing requirements
  31. * (highly reliable, no drops vs. best effort vs. low latency) to operate
  32. * and co-exist on Ethernet. Current DCB features are:
  33. *
  34. * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
  35. * framework for assigning bandwidth guarantees to traffic classes.
  36. *
  37. * Priority-based Flow Control (PFC) - provides a flow control mechanism which
  38. * can work independently for each 802.1p priority.
  39. *
  40. * Congestion Notification - provides a mechanism for end-to-end congestion
  41. * control for protocols which do not have built-in congestion management.
  42. *
  43. * More information about the emerging standards for these Ethernet features
  44. * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
  45. *
  46. * This file implements an rtnetlink interface to allow configuration of DCB
  47. * features for capable devices.
  48. */
  49. MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
  50. MODULE_DESCRIPTION("Data Center Bridging netlink interface");
  51. MODULE_LICENSE("GPL");
  52. /**************** DCB attribute policies *************************************/
  53. /* DCB netlink attributes policy */
  54. static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
  55. [DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
  56. [DCB_ATTR_STATE] = {.type = NLA_U8},
  57. [DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},
  58. [DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},
  59. [DCB_ATTR_SET_ALL] = {.type = NLA_U8},
  60. [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
  61. [DCB_ATTR_CAP] = {.type = NLA_NESTED},
  62. [DCB_ATTR_PFC_STATE] = {.type = NLA_U8},
  63. [DCB_ATTR_BCN] = {.type = NLA_NESTED},
  64. [DCB_ATTR_APP] = {.type = NLA_NESTED},
  65. [DCB_ATTR_IEEE] = {.type = NLA_NESTED},
  66. [DCB_ATTR_DCBX] = {.type = NLA_U8},
  67. [DCB_ATTR_FEATCFG] = {.type = NLA_NESTED},
  68. };
  69. /* DCB priority flow control to User Priority nested attributes */
  70. static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
  71. [DCB_PFC_UP_ATTR_0] = {.type = NLA_U8},
  72. [DCB_PFC_UP_ATTR_1] = {.type = NLA_U8},
  73. [DCB_PFC_UP_ATTR_2] = {.type = NLA_U8},
  74. [DCB_PFC_UP_ATTR_3] = {.type = NLA_U8},
  75. [DCB_PFC_UP_ATTR_4] = {.type = NLA_U8},
  76. [DCB_PFC_UP_ATTR_5] = {.type = NLA_U8},
  77. [DCB_PFC_UP_ATTR_6] = {.type = NLA_U8},
  78. [DCB_PFC_UP_ATTR_7] = {.type = NLA_U8},
  79. [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
  80. };
  81. /* DCB priority grouping nested attributes */
  82. static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
  83. [DCB_PG_ATTR_TC_0] = {.type = NLA_NESTED},
  84. [DCB_PG_ATTR_TC_1] = {.type = NLA_NESTED},
  85. [DCB_PG_ATTR_TC_2] = {.type = NLA_NESTED},
  86. [DCB_PG_ATTR_TC_3] = {.type = NLA_NESTED},
  87. [DCB_PG_ATTR_TC_4] = {.type = NLA_NESTED},
  88. [DCB_PG_ATTR_TC_5] = {.type = NLA_NESTED},
  89. [DCB_PG_ATTR_TC_6] = {.type = NLA_NESTED},
  90. [DCB_PG_ATTR_TC_7] = {.type = NLA_NESTED},
  91. [DCB_PG_ATTR_TC_ALL] = {.type = NLA_NESTED},
  92. [DCB_PG_ATTR_BW_ID_0] = {.type = NLA_U8},
  93. [DCB_PG_ATTR_BW_ID_1] = {.type = NLA_U8},
  94. [DCB_PG_ATTR_BW_ID_2] = {.type = NLA_U8},
  95. [DCB_PG_ATTR_BW_ID_3] = {.type = NLA_U8},
  96. [DCB_PG_ATTR_BW_ID_4] = {.type = NLA_U8},
  97. [DCB_PG_ATTR_BW_ID_5] = {.type = NLA_U8},
  98. [DCB_PG_ATTR_BW_ID_6] = {.type = NLA_U8},
  99. [DCB_PG_ATTR_BW_ID_7] = {.type = NLA_U8},
  100. [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
  101. };
  102. /* DCB traffic class nested attributes. */
  103. static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
  104. [DCB_TC_ATTR_PARAM_PGID] = {.type = NLA_U8},
  105. [DCB_TC_ATTR_PARAM_UP_MAPPING] = {.type = NLA_U8},
  106. [DCB_TC_ATTR_PARAM_STRICT_PRIO] = {.type = NLA_U8},
  107. [DCB_TC_ATTR_PARAM_BW_PCT] = {.type = NLA_U8},
  108. [DCB_TC_ATTR_PARAM_ALL] = {.type = NLA_FLAG},
  109. };
  110. /* DCB capabilities nested attributes. */
  111. static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
  112. [DCB_CAP_ATTR_ALL] = {.type = NLA_FLAG},
  113. [DCB_CAP_ATTR_PG] = {.type = NLA_U8},
  114. [DCB_CAP_ATTR_PFC] = {.type = NLA_U8},
  115. [DCB_CAP_ATTR_UP2TC] = {.type = NLA_U8},
  116. [DCB_CAP_ATTR_PG_TCS] = {.type = NLA_U8},
  117. [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
  118. [DCB_CAP_ATTR_GSP] = {.type = NLA_U8},
  119. [DCB_CAP_ATTR_BCN] = {.type = NLA_U8},
  120. [DCB_CAP_ATTR_DCBX] = {.type = NLA_U8},
  121. };
  122. /* DCB capabilities nested attributes. */
  123. static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
  124. [DCB_NUMTCS_ATTR_ALL] = {.type = NLA_FLAG},
  125. [DCB_NUMTCS_ATTR_PG] = {.type = NLA_U8},
  126. [DCB_NUMTCS_ATTR_PFC] = {.type = NLA_U8},
  127. };
  128. /* DCB BCN nested attributes. */
  129. static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
  130. [DCB_BCN_ATTR_RP_0] = {.type = NLA_U8},
  131. [DCB_BCN_ATTR_RP_1] = {.type = NLA_U8},
  132. [DCB_BCN_ATTR_RP_2] = {.type = NLA_U8},
  133. [DCB_BCN_ATTR_RP_3] = {.type = NLA_U8},
  134. [DCB_BCN_ATTR_RP_4] = {.type = NLA_U8},
  135. [DCB_BCN_ATTR_RP_5] = {.type = NLA_U8},
  136. [DCB_BCN_ATTR_RP_6] = {.type = NLA_U8},
  137. [DCB_BCN_ATTR_RP_7] = {.type = NLA_U8},
  138. [DCB_BCN_ATTR_RP_ALL] = {.type = NLA_FLAG},
  139. [DCB_BCN_ATTR_BCNA_0] = {.type = NLA_U32},
  140. [DCB_BCN_ATTR_BCNA_1] = {.type = NLA_U32},
  141. [DCB_BCN_ATTR_ALPHA] = {.type = NLA_U32},
  142. [DCB_BCN_ATTR_BETA] = {.type = NLA_U32},
  143. [DCB_BCN_ATTR_GD] = {.type = NLA_U32},
  144. [DCB_BCN_ATTR_GI] = {.type = NLA_U32},
  145. [DCB_BCN_ATTR_TMAX] = {.type = NLA_U32},
  146. [DCB_BCN_ATTR_TD] = {.type = NLA_U32},
  147. [DCB_BCN_ATTR_RMIN] = {.type = NLA_U32},
  148. [DCB_BCN_ATTR_W] = {.type = NLA_U32},
  149. [DCB_BCN_ATTR_RD] = {.type = NLA_U32},
  150. [DCB_BCN_ATTR_RU] = {.type = NLA_U32},
  151. [DCB_BCN_ATTR_WRTT] = {.type = NLA_U32},
  152. [DCB_BCN_ATTR_RI] = {.type = NLA_U32},
  153. [DCB_BCN_ATTR_C] = {.type = NLA_U32},
  154. [DCB_BCN_ATTR_ALL] = {.type = NLA_FLAG},
  155. };
  156. /* DCB APP nested attributes. */
  157. static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
  158. [DCB_APP_ATTR_IDTYPE] = {.type = NLA_U8},
  159. [DCB_APP_ATTR_ID] = {.type = NLA_U16},
  160. [DCB_APP_ATTR_PRIORITY] = {.type = NLA_U8},
  161. };
  162. /* IEEE 802.1Qaz nested attributes. */
  163. static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
  164. [DCB_ATTR_IEEE_ETS] = {.len = sizeof(struct ieee_ets)},
  165. [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
  166. [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
  167. };
  168. static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
  169. [DCB_ATTR_IEEE_APP] = {.len = sizeof(struct dcb_app)},
  170. };
  171. /* DCB number of traffic classes nested attributes. */
  172. static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
  173. [DCB_FEATCFG_ATTR_ALL] = {.type = NLA_FLAG},
  174. [DCB_FEATCFG_ATTR_PG] = {.type = NLA_U8},
  175. [DCB_FEATCFG_ATTR_PFC] = {.type = NLA_U8},
  176. [DCB_FEATCFG_ATTR_APP] = {.type = NLA_U8},
  177. };
  178. static LIST_HEAD(dcb_app_list);
  179. static DEFINE_SPINLOCK(dcb_lock);
  180. /* standard netlink reply call */
  181. static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,
  182. u32 seq, u16 flags)
  183. {
  184. struct sk_buff *dcbnl_skb;
  185. struct dcbmsg *dcb;
  186. struct nlmsghdr *nlh;
  187. int ret = -EINVAL;
  188. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  189. if (!dcbnl_skb)
  190. return ret;
  191. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);
  192. dcb = NLMSG_DATA(nlh);
  193. dcb->dcb_family = AF_UNSPEC;
  194. dcb->cmd = cmd;
  195. dcb->dcb_pad = 0;
  196. ret = nla_put_u8(dcbnl_skb, attr, value);
  197. if (ret)
  198. goto err;
  199. /* end the message, assign the nlmsg_len. */
  200. nlmsg_end(dcbnl_skb, nlh);
  201. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  202. if (ret)
  203. return -EINVAL;
  204. return 0;
  205. nlmsg_failure:
  206. err:
  207. kfree_skb(dcbnl_skb);
  208. return ret;
  209. }
  210. static int dcbnl_getstate(struct net_device *netdev, struct nlattr **tb,
  211. u32 pid, u32 seq, u16 flags)
  212. {
  213. int ret = -EINVAL;
  214. /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
  215. if (!netdev->dcbnl_ops->getstate)
  216. return ret;
  217. ret = dcbnl_reply(netdev->dcbnl_ops->getstate(netdev), RTM_GETDCB,
  218. DCB_CMD_GSTATE, DCB_ATTR_STATE, pid, seq, flags);
  219. return ret;
  220. }
  221. static int dcbnl_getpfccfg(struct net_device *netdev, struct nlattr **tb,
  222. u32 pid, u32 seq, u16 flags)
  223. {
  224. struct sk_buff *dcbnl_skb;
  225. struct nlmsghdr *nlh;
  226. struct dcbmsg *dcb;
  227. struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
  228. u8 value;
  229. int ret = -EINVAL;
  230. int i;
  231. int getall = 0;
  232. if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
  233. return ret;
  234. ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  235. tb[DCB_ATTR_PFC_CFG],
  236. dcbnl_pfc_up_nest);
  237. if (ret)
  238. goto err_out;
  239. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  240. if (!dcbnl_skb)
  241. goto err_out;
  242. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  243. dcb = NLMSG_DATA(nlh);
  244. dcb->dcb_family = AF_UNSPEC;
  245. dcb->cmd = DCB_CMD_PFC_GCFG;
  246. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PFC_CFG);
  247. if (!nest)
  248. goto err;
  249. if (data[DCB_PFC_UP_ATTR_ALL])
  250. getall = 1;
  251. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  252. if (!getall && !data[i])
  253. continue;
  254. netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
  255. &value);
  256. ret = nla_put_u8(dcbnl_skb, i, value);
  257. if (ret) {
  258. nla_nest_cancel(dcbnl_skb, nest);
  259. goto err;
  260. }
  261. }
  262. nla_nest_end(dcbnl_skb, nest);
  263. nlmsg_end(dcbnl_skb, nlh);
  264. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  265. if (ret)
  266. goto err_out;
  267. return 0;
  268. nlmsg_failure:
  269. err:
  270. kfree_skb(dcbnl_skb);
  271. err_out:
  272. return -EINVAL;
  273. }
  274. static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlattr **tb,
  275. u32 pid, u32 seq, u16 flags)
  276. {
  277. struct sk_buff *dcbnl_skb;
  278. struct nlmsghdr *nlh;
  279. struct dcbmsg *dcb;
  280. u8 perm_addr[MAX_ADDR_LEN];
  281. int ret = -EINVAL;
  282. if (!netdev->dcbnl_ops->getpermhwaddr)
  283. return ret;
  284. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  285. if (!dcbnl_skb)
  286. goto err_out;
  287. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  288. dcb = NLMSG_DATA(nlh);
  289. dcb->dcb_family = AF_UNSPEC;
  290. dcb->cmd = DCB_CMD_GPERM_HWADDR;
  291. netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
  292. ret = nla_put(dcbnl_skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr),
  293. perm_addr);
  294. nlmsg_end(dcbnl_skb, nlh);
  295. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  296. if (ret)
  297. goto err_out;
  298. return 0;
  299. nlmsg_failure:
  300. kfree_skb(dcbnl_skb);
  301. err_out:
  302. return -EINVAL;
  303. }
  304. static int dcbnl_getcap(struct net_device *netdev, struct nlattr **tb,
  305. u32 pid, u32 seq, u16 flags)
  306. {
  307. struct sk_buff *dcbnl_skb;
  308. struct nlmsghdr *nlh;
  309. struct dcbmsg *dcb;
  310. struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
  311. u8 value;
  312. int ret = -EINVAL;
  313. int i;
  314. int getall = 0;
  315. if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
  316. return ret;
  317. ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
  318. dcbnl_cap_nest);
  319. if (ret)
  320. goto err_out;
  321. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  322. if (!dcbnl_skb)
  323. goto err_out;
  324. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  325. dcb = NLMSG_DATA(nlh);
  326. dcb->dcb_family = AF_UNSPEC;
  327. dcb->cmd = DCB_CMD_GCAP;
  328. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_CAP);
  329. if (!nest)
  330. goto err;
  331. if (data[DCB_CAP_ATTR_ALL])
  332. getall = 1;
  333. for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
  334. if (!getall && !data[i])
  335. continue;
  336. if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
  337. ret = nla_put_u8(dcbnl_skb, i, value);
  338. if (ret) {
  339. nla_nest_cancel(dcbnl_skb, nest);
  340. goto err;
  341. }
  342. }
  343. }
  344. nla_nest_end(dcbnl_skb, nest);
  345. nlmsg_end(dcbnl_skb, nlh);
  346. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  347. if (ret)
  348. goto err_out;
  349. return 0;
  350. nlmsg_failure:
  351. err:
  352. kfree_skb(dcbnl_skb);
  353. err_out:
  354. return -EINVAL;
  355. }
  356. static int dcbnl_getnumtcs(struct net_device *netdev, struct nlattr **tb,
  357. u32 pid, u32 seq, u16 flags)
  358. {
  359. struct sk_buff *dcbnl_skb;
  360. struct nlmsghdr *nlh;
  361. struct dcbmsg *dcb;
  362. struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
  363. u8 value;
  364. int ret = -EINVAL;
  365. int i;
  366. int getall = 0;
  367. if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
  368. return ret;
  369. ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  370. dcbnl_numtcs_nest);
  371. if (ret) {
  372. ret = -EINVAL;
  373. goto err_out;
  374. }
  375. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  376. if (!dcbnl_skb) {
  377. ret = -EINVAL;
  378. goto err_out;
  379. }
  380. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  381. dcb = NLMSG_DATA(nlh);
  382. dcb->dcb_family = AF_UNSPEC;
  383. dcb->cmd = DCB_CMD_GNUMTCS;
  384. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_NUMTCS);
  385. if (!nest) {
  386. ret = -EINVAL;
  387. goto err;
  388. }
  389. if (data[DCB_NUMTCS_ATTR_ALL])
  390. getall = 1;
  391. for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  392. if (!getall && !data[i])
  393. continue;
  394. ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
  395. if (!ret) {
  396. ret = nla_put_u8(dcbnl_skb, i, value);
  397. if (ret) {
  398. nla_nest_cancel(dcbnl_skb, nest);
  399. ret = -EINVAL;
  400. goto err;
  401. }
  402. } else {
  403. goto err;
  404. }
  405. }
  406. nla_nest_end(dcbnl_skb, nest);
  407. nlmsg_end(dcbnl_skb, nlh);
  408. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  409. if (ret) {
  410. ret = -EINVAL;
  411. goto err_out;
  412. }
  413. return 0;
  414. nlmsg_failure:
  415. err:
  416. kfree_skb(dcbnl_skb);
  417. err_out:
  418. return ret;
  419. }
  420. static int dcbnl_setnumtcs(struct net_device *netdev, struct nlattr **tb,
  421. u32 pid, u32 seq, u16 flags)
  422. {
  423. struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
  424. int ret = -EINVAL;
  425. u8 value;
  426. int i;
  427. if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
  428. return ret;
  429. ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  430. dcbnl_numtcs_nest);
  431. if (ret) {
  432. ret = -EINVAL;
  433. goto err;
  434. }
  435. for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  436. if (data[i] == NULL)
  437. continue;
  438. value = nla_get_u8(data[i]);
  439. ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
  440. if (ret)
  441. goto operr;
  442. }
  443. operr:
  444. ret = dcbnl_reply(!!ret, RTM_SETDCB, DCB_CMD_SNUMTCS,
  445. DCB_ATTR_NUMTCS, pid, seq, flags);
  446. err:
  447. return ret;
  448. }
  449. static int dcbnl_getpfcstate(struct net_device *netdev, struct nlattr **tb,
  450. u32 pid, u32 seq, u16 flags)
  451. {
  452. int ret = -EINVAL;
  453. if (!netdev->dcbnl_ops->getpfcstate)
  454. return ret;
  455. ret = dcbnl_reply(netdev->dcbnl_ops->getpfcstate(netdev), RTM_GETDCB,
  456. DCB_CMD_PFC_GSTATE, DCB_ATTR_PFC_STATE,
  457. pid, seq, flags);
  458. return ret;
  459. }
  460. static int dcbnl_setpfcstate(struct net_device *netdev, struct nlattr **tb,
  461. u32 pid, u32 seq, u16 flags)
  462. {
  463. int ret = -EINVAL;
  464. u8 value;
  465. if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
  466. return ret;
  467. value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
  468. netdev->dcbnl_ops->setpfcstate(netdev, value);
  469. ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE,
  470. pid, seq, flags);
  471. return ret;
  472. }
  473. static int dcbnl_getapp(struct net_device *netdev, struct nlattr **tb,
  474. u32 pid, u32 seq, u16 flags)
  475. {
  476. struct sk_buff *dcbnl_skb;
  477. struct nlmsghdr *nlh;
  478. struct dcbmsg *dcb;
  479. struct nlattr *app_nest;
  480. struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
  481. u16 id;
  482. u8 up, idtype;
  483. int ret = -EINVAL;
  484. if (!tb[DCB_ATTR_APP] || !netdev->dcbnl_ops->getapp)
  485. goto out;
  486. ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  487. dcbnl_app_nest);
  488. if (ret)
  489. goto out;
  490. ret = -EINVAL;
  491. /* all must be non-null */
  492. if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  493. (!app_tb[DCB_APP_ATTR_ID]))
  494. goto out;
  495. /* either by eth type or by socket number */
  496. idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  497. if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  498. (idtype != DCB_APP_IDTYPE_PORTNUM))
  499. goto out;
  500. id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
  501. up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
  502. /* send this back */
  503. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  504. if (!dcbnl_skb)
  505. goto out;
  506. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  507. dcb = NLMSG_DATA(nlh);
  508. dcb->dcb_family = AF_UNSPEC;
  509. dcb->cmd = DCB_CMD_GAPP;
  510. app_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_APP);
  511. ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_IDTYPE, idtype);
  512. if (ret)
  513. goto out_cancel;
  514. ret = nla_put_u16(dcbnl_skb, DCB_APP_ATTR_ID, id);
  515. if (ret)
  516. goto out_cancel;
  517. ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_PRIORITY, up);
  518. if (ret)
  519. goto out_cancel;
  520. nla_nest_end(dcbnl_skb, app_nest);
  521. nlmsg_end(dcbnl_skb, nlh);
  522. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  523. if (ret)
  524. goto nlmsg_failure;
  525. goto out;
  526. out_cancel:
  527. nla_nest_cancel(dcbnl_skb, app_nest);
  528. nlmsg_failure:
  529. kfree_skb(dcbnl_skb);
  530. out:
  531. return ret;
  532. }
  533. static int dcbnl_setapp(struct net_device *netdev, struct nlattr **tb,
  534. u32 pid, u32 seq, u16 flags)
  535. {
  536. int err, ret = -EINVAL;
  537. u16 id;
  538. u8 up, idtype;
  539. struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
  540. if (!tb[DCB_ATTR_APP])
  541. goto out;
  542. ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  543. dcbnl_app_nest);
  544. if (ret)
  545. goto out;
  546. ret = -EINVAL;
  547. /* all must be non-null */
  548. if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  549. (!app_tb[DCB_APP_ATTR_ID]) ||
  550. (!app_tb[DCB_APP_ATTR_PRIORITY]))
  551. goto out;
  552. /* either by eth type or by socket number */
  553. idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  554. if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  555. (idtype != DCB_APP_IDTYPE_PORTNUM))
  556. goto out;
  557. id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
  558. up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
  559. if (netdev->dcbnl_ops->setapp) {
  560. err = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
  561. } else {
  562. struct dcb_app app;
  563. app.selector = idtype;
  564. app.protocol = id;
  565. app.priority = up;
  566. err = dcb_setapp(netdev, &app);
  567. }
  568. ret = dcbnl_reply(err, RTM_SETDCB, DCB_CMD_SAPP, DCB_ATTR_APP,
  569. pid, seq, flags);
  570. out:
  571. return ret;
  572. }
  573. static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlattr **tb,
  574. u32 pid, u32 seq, u16 flags, int dir)
  575. {
  576. struct sk_buff *dcbnl_skb;
  577. struct nlmsghdr *nlh;
  578. struct dcbmsg *dcb;
  579. struct nlattr *pg_nest, *param_nest, *data;
  580. struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  581. struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  582. u8 prio, pgid, tc_pct, up_map;
  583. int ret = -EINVAL;
  584. int getall = 0;
  585. int i;
  586. if (!tb[DCB_ATTR_PG_CFG] ||
  587. !netdev->dcbnl_ops->getpgtccfgtx ||
  588. !netdev->dcbnl_ops->getpgtccfgrx ||
  589. !netdev->dcbnl_ops->getpgbwgcfgtx ||
  590. !netdev->dcbnl_ops->getpgbwgcfgrx)
  591. return ret;
  592. ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  593. tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  594. if (ret)
  595. goto err_out;
  596. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  597. if (!dcbnl_skb)
  598. goto err_out;
  599. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  600. dcb = NLMSG_DATA(nlh);
  601. dcb->dcb_family = AF_UNSPEC;
  602. dcb->cmd = (dir) ? DCB_CMD_PGRX_GCFG : DCB_CMD_PGTX_GCFG;
  603. pg_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PG_CFG);
  604. if (!pg_nest)
  605. goto err;
  606. if (pg_tb[DCB_PG_ATTR_TC_ALL])
  607. getall = 1;
  608. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  609. if (!getall && !pg_tb[i])
  610. continue;
  611. if (pg_tb[DCB_PG_ATTR_TC_ALL])
  612. data = pg_tb[DCB_PG_ATTR_TC_ALL];
  613. else
  614. data = pg_tb[i];
  615. ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  616. data, dcbnl_tc_param_nest);
  617. if (ret)
  618. goto err_pg;
  619. param_nest = nla_nest_start(dcbnl_skb, i);
  620. if (!param_nest)
  621. goto err_pg;
  622. pgid = DCB_ATTR_VALUE_UNDEFINED;
  623. prio = DCB_ATTR_VALUE_UNDEFINED;
  624. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  625. up_map = DCB_ATTR_VALUE_UNDEFINED;
  626. if (dir) {
  627. /* Rx */
  628. netdev->dcbnl_ops->getpgtccfgrx(netdev,
  629. i - DCB_PG_ATTR_TC_0, &prio,
  630. &pgid, &tc_pct, &up_map);
  631. } else {
  632. /* Tx */
  633. netdev->dcbnl_ops->getpgtccfgtx(netdev,
  634. i - DCB_PG_ATTR_TC_0, &prio,
  635. &pgid, &tc_pct, &up_map);
  636. }
  637. if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
  638. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  639. ret = nla_put_u8(dcbnl_skb,
  640. DCB_TC_ATTR_PARAM_PGID, pgid);
  641. if (ret)
  642. goto err_param;
  643. }
  644. if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
  645. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  646. ret = nla_put_u8(dcbnl_skb,
  647. DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
  648. if (ret)
  649. goto err_param;
  650. }
  651. if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
  652. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  653. ret = nla_put_u8(dcbnl_skb,
  654. DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
  655. if (ret)
  656. goto err_param;
  657. }
  658. if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
  659. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  660. ret = nla_put_u8(dcbnl_skb, DCB_TC_ATTR_PARAM_BW_PCT,
  661. tc_pct);
  662. if (ret)
  663. goto err_param;
  664. }
  665. nla_nest_end(dcbnl_skb, param_nest);
  666. }
  667. if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
  668. getall = 1;
  669. else
  670. getall = 0;
  671. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  672. if (!getall && !pg_tb[i])
  673. continue;
  674. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  675. if (dir) {
  676. /* Rx */
  677. netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
  678. i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  679. } else {
  680. /* Tx */
  681. netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
  682. i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  683. }
  684. ret = nla_put_u8(dcbnl_skb, i, tc_pct);
  685. if (ret)
  686. goto err_pg;
  687. }
  688. nla_nest_end(dcbnl_skb, pg_nest);
  689. nlmsg_end(dcbnl_skb, nlh);
  690. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  691. if (ret)
  692. goto err_out;
  693. return 0;
  694. err_param:
  695. nla_nest_cancel(dcbnl_skb, param_nest);
  696. err_pg:
  697. nla_nest_cancel(dcbnl_skb, pg_nest);
  698. nlmsg_failure:
  699. err:
  700. kfree_skb(dcbnl_skb);
  701. err_out:
  702. ret = -EINVAL;
  703. return ret;
  704. }
  705. static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlattr **tb,
  706. u32 pid, u32 seq, u16 flags)
  707. {
  708. return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 0);
  709. }
  710. static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlattr **tb,
  711. u32 pid, u32 seq, u16 flags)
  712. {
  713. return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 1);
  714. }
  715. static int dcbnl_setstate(struct net_device *netdev, struct nlattr **tb,
  716. u32 pid, u32 seq, u16 flags)
  717. {
  718. int ret = -EINVAL;
  719. u8 value;
  720. if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
  721. return ret;
  722. value = nla_get_u8(tb[DCB_ATTR_STATE]);
  723. ret = dcbnl_reply(netdev->dcbnl_ops->setstate(netdev, value),
  724. RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
  725. pid, seq, flags);
  726. return ret;
  727. }
  728. static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
  729. u32 pid, u32 seq, u16 flags)
  730. {
  731. struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
  732. int i;
  733. int ret = -EINVAL;
  734. u8 value;
  735. if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
  736. return ret;
  737. ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  738. tb[DCB_ATTR_PFC_CFG],
  739. dcbnl_pfc_up_nest);
  740. if (ret)
  741. goto err;
  742. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  743. if (data[i] == NULL)
  744. continue;
  745. value = nla_get_u8(data[i]);
  746. netdev->dcbnl_ops->setpfccfg(netdev,
  747. data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
  748. }
  749. ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
  750. pid, seq, flags);
  751. err:
  752. return ret;
  753. }
  754. static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
  755. u32 pid, u32 seq, u16 flags)
  756. {
  757. int ret = -EINVAL;
  758. if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
  759. return ret;
  760. ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
  761. DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
  762. return ret;
  763. }
  764. static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
  765. u32 pid, u32 seq, u16 flags, int dir)
  766. {
  767. struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  768. struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  769. int ret = -EINVAL;
  770. int i;
  771. u8 pgid;
  772. u8 up_map;
  773. u8 prio;
  774. u8 tc_pct;
  775. if (!tb[DCB_ATTR_PG_CFG] ||
  776. !netdev->dcbnl_ops->setpgtccfgtx ||
  777. !netdev->dcbnl_ops->setpgtccfgrx ||
  778. !netdev->dcbnl_ops->setpgbwgcfgtx ||
  779. !netdev->dcbnl_ops->setpgbwgcfgrx)
  780. return ret;
  781. ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  782. tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  783. if (ret)
  784. goto err;
  785. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  786. if (!pg_tb[i])
  787. continue;
  788. ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  789. pg_tb[i], dcbnl_tc_param_nest);
  790. if (ret)
  791. goto err;
  792. pgid = DCB_ATTR_VALUE_UNDEFINED;
  793. prio = DCB_ATTR_VALUE_UNDEFINED;
  794. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  795. up_map = DCB_ATTR_VALUE_UNDEFINED;
  796. if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
  797. prio =
  798. nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
  799. if (param_tb[DCB_TC_ATTR_PARAM_PGID])
  800. pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
  801. if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
  802. tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
  803. if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
  804. up_map =
  805. nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
  806. /* dir: Tx = 0, Rx = 1 */
  807. if (dir) {
  808. /* Rx */
  809. netdev->dcbnl_ops->setpgtccfgrx(netdev,
  810. i - DCB_PG_ATTR_TC_0,
  811. prio, pgid, tc_pct, up_map);
  812. } else {
  813. /* Tx */
  814. netdev->dcbnl_ops->setpgtccfgtx(netdev,
  815. i - DCB_PG_ATTR_TC_0,
  816. prio, pgid, tc_pct, up_map);
  817. }
  818. }
  819. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  820. if (!pg_tb[i])
  821. continue;
  822. tc_pct = nla_get_u8(pg_tb[i]);
  823. /* dir: Tx = 0, Rx = 1 */
  824. if (dir) {
  825. /* Rx */
  826. netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
  827. i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  828. } else {
  829. /* Tx */
  830. netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
  831. i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  832. }
  833. }
  834. ret = dcbnl_reply(0, RTM_SETDCB,
  835. (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
  836. DCB_ATTR_PG_CFG, pid, seq, flags);
  837. err:
  838. return ret;
  839. }
  840. static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
  841. u32 pid, u32 seq, u16 flags)
  842. {
  843. return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
  844. }
  845. static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
  846. u32 pid, u32 seq, u16 flags)
  847. {
  848. return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
  849. }
  850. static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlattr **tb,
  851. u32 pid, u32 seq, u16 flags)
  852. {
  853. struct sk_buff *dcbnl_skb;
  854. struct nlmsghdr *nlh;
  855. struct dcbmsg *dcb;
  856. struct nlattr *bcn_nest;
  857. struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
  858. u8 value_byte;
  859. u32 value_integer;
  860. int ret = -EINVAL;
  861. bool getall = false;
  862. int i;
  863. if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
  864. !netdev->dcbnl_ops->getbcncfg)
  865. return ret;
  866. ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
  867. tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
  868. if (ret)
  869. goto err_out;
  870. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  871. if (!dcbnl_skb)
  872. goto err_out;
  873. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  874. dcb = NLMSG_DATA(nlh);
  875. dcb->dcb_family = AF_UNSPEC;
  876. dcb->cmd = DCB_CMD_BCN_GCFG;
  877. bcn_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_BCN);
  878. if (!bcn_nest)
  879. goto err;
  880. if (bcn_tb[DCB_BCN_ATTR_ALL])
  881. getall = true;
  882. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  883. if (!getall && !bcn_tb[i])
  884. continue;
  885. netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
  886. &value_byte);
  887. ret = nla_put_u8(dcbnl_skb, i, value_byte);
  888. if (ret)
  889. goto err_bcn;
  890. }
  891. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  892. if (!getall && !bcn_tb[i])
  893. continue;
  894. netdev->dcbnl_ops->getbcncfg(netdev, i,
  895. &value_integer);
  896. ret = nla_put_u32(dcbnl_skb, i, value_integer);
  897. if (ret)
  898. goto err_bcn;
  899. }
  900. nla_nest_end(dcbnl_skb, bcn_nest);
  901. nlmsg_end(dcbnl_skb, nlh);
  902. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  903. if (ret)
  904. goto err_out;
  905. return 0;
  906. err_bcn:
  907. nla_nest_cancel(dcbnl_skb, bcn_nest);
  908. nlmsg_failure:
  909. err:
  910. kfree_skb(dcbnl_skb);
  911. err_out:
  912. ret = -EINVAL;
  913. return ret;
  914. }
  915. static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlattr **tb,
  916. u32 pid, u32 seq, u16 flags)
  917. {
  918. struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
  919. int i;
  920. int ret = -EINVAL;
  921. u8 value_byte;
  922. u32 value_int;
  923. if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg ||
  924. !netdev->dcbnl_ops->setbcnrp)
  925. return ret;
  926. ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
  927. tb[DCB_ATTR_BCN],
  928. dcbnl_pfc_up_nest);
  929. if (ret)
  930. goto err;
  931. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  932. if (data[i] == NULL)
  933. continue;
  934. value_byte = nla_get_u8(data[i]);
  935. netdev->dcbnl_ops->setbcnrp(netdev,
  936. data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
  937. }
  938. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  939. if (data[i] == NULL)
  940. continue;
  941. value_int = nla_get_u32(data[i]);
  942. netdev->dcbnl_ops->setbcncfg(netdev,
  943. i, value_int);
  944. }
  945. ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_BCN_SCFG, DCB_ATTR_BCN,
  946. pid, seq, flags);
  947. err:
  948. return ret;
  949. }
  950. /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
  951. * be completed the entire msg is aborted and error value is returned.
  952. * No attempt is made to reconcile the case where only part of the
  953. * cmd can be completed.
  954. */
  955. static int dcbnl_ieee_set(struct net_device *netdev, struct nlattr **tb,
  956. u32 pid, u32 seq, u16 flags)
  957. {
  958. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  959. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  960. int err = -EOPNOTSUPP;
  961. if (!ops)
  962. goto err;
  963. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  964. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  965. if (err)
  966. goto err;
  967. if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  968. struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  969. err = ops->ieee_setets(netdev, ets);
  970. if (err)
  971. goto err;
  972. }
  973. if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setets) {
  974. struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  975. err = ops->ieee_setpfc(netdev, pfc);
  976. if (err)
  977. goto err;
  978. }
  979. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  980. struct nlattr *attr;
  981. int rem;
  982. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  983. struct dcb_app *app_data;
  984. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  985. continue;
  986. app_data = nla_data(attr);
  987. if (ops->ieee_setapp)
  988. err = ops->ieee_setapp(netdev, app_data);
  989. else
  990. err = dcb_setapp(netdev, app_data);
  991. if (err)
  992. goto err;
  993. }
  994. }
  995. err:
  996. dcbnl_reply(err, RTM_SETDCB, DCB_CMD_IEEE_SET, DCB_ATTR_IEEE,
  997. pid, seq, flags);
  998. return err;
  999. }
  1000. /* Handle IEEE 802.1Qaz GET commands. */
  1001. static int dcbnl_ieee_get(struct net_device *netdev, struct nlattr **tb,
  1002. u32 pid, u32 seq, u16 flags)
  1003. {
  1004. struct sk_buff *skb;
  1005. struct nlmsghdr *nlh;
  1006. struct dcbmsg *dcb;
  1007. struct nlattr *ieee, *app;
  1008. struct dcb_app_type *itr;
  1009. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1010. int err;
  1011. if (!ops)
  1012. return -EOPNOTSUPP;
  1013. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1014. if (!skb)
  1015. return -ENOBUFS;
  1016. nlh = NLMSG_NEW(skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  1017. dcb = NLMSG_DATA(nlh);
  1018. dcb->dcb_family = AF_UNSPEC;
  1019. dcb->cmd = DCB_CMD_IEEE_GET;
  1020. NLA_PUT_STRING(skb, DCB_ATTR_IFNAME, netdev->name);
  1021. ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
  1022. if (!ieee)
  1023. goto nla_put_failure;
  1024. if (ops->ieee_getets) {
  1025. struct ieee_ets ets;
  1026. err = ops->ieee_getets(netdev, &ets);
  1027. if (!err)
  1028. NLA_PUT(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets);
  1029. }
  1030. if (ops->ieee_getpfc) {
  1031. struct ieee_pfc pfc;
  1032. err = ops->ieee_getpfc(netdev, &pfc);
  1033. if (!err)
  1034. NLA_PUT(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc);
  1035. }
  1036. app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
  1037. if (!app)
  1038. goto nla_put_failure;
  1039. spin_lock(&dcb_lock);
  1040. list_for_each_entry(itr, &dcb_app_list, list) {
  1041. if (strncmp(itr->name, netdev->name, IFNAMSIZ) == 0) {
  1042. err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
  1043. &itr->app);
  1044. if (err) {
  1045. spin_unlock(&dcb_lock);
  1046. goto nla_put_failure;
  1047. }
  1048. }
  1049. }
  1050. spin_unlock(&dcb_lock);
  1051. nla_nest_end(skb, app);
  1052. nla_nest_end(skb, ieee);
  1053. nlmsg_end(skb, nlh);
  1054. return rtnl_unicast(skb, &init_net, pid);
  1055. nla_put_failure:
  1056. nlmsg_cancel(skb, nlh);
  1057. nlmsg_failure:
  1058. kfree_skb(skb);
  1059. return -1;
  1060. }
  1061. /* DCBX configuration */
  1062. static int dcbnl_getdcbx(struct net_device *netdev, struct nlattr **tb,
  1063. u32 pid, u32 seq, u16 flags)
  1064. {
  1065. int ret;
  1066. if (!netdev->dcbnl_ops->getdcbx)
  1067. return -EOPNOTSUPP;
  1068. ret = dcbnl_reply(netdev->dcbnl_ops->getdcbx(netdev), RTM_GETDCB,
  1069. DCB_CMD_GDCBX, DCB_ATTR_DCBX, pid, seq, flags);
  1070. return ret;
  1071. }
  1072. static int dcbnl_setdcbx(struct net_device *netdev, struct nlattr **tb,
  1073. u32 pid, u32 seq, u16 flags)
  1074. {
  1075. int ret;
  1076. u8 value;
  1077. if (!netdev->dcbnl_ops->setdcbx)
  1078. return -EOPNOTSUPP;
  1079. if (!tb[DCB_ATTR_DCBX])
  1080. return -EINVAL;
  1081. value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  1082. ret = dcbnl_reply(netdev->dcbnl_ops->setdcbx(netdev, value),
  1083. RTM_SETDCB, DCB_CMD_SDCBX, DCB_ATTR_DCBX,
  1084. pid, seq, flags);
  1085. return ret;
  1086. }
  1087. static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlattr **tb,
  1088. u32 pid, u32 seq, u16 flags)
  1089. {
  1090. struct sk_buff *dcbnl_skb;
  1091. struct nlmsghdr *nlh;
  1092. struct dcbmsg *dcb;
  1093. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  1094. u8 value;
  1095. int ret, i;
  1096. int getall = 0;
  1097. if (!netdev->dcbnl_ops->getfeatcfg)
  1098. return -EOPNOTSUPP;
  1099. if (!tb[DCB_ATTR_FEATCFG])
  1100. return -EINVAL;
  1101. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1102. dcbnl_featcfg_nest);
  1103. if (ret)
  1104. goto err_out;
  1105. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1106. if (!dcbnl_skb) {
  1107. ret = -ENOBUFS;
  1108. goto err_out;
  1109. }
  1110. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  1111. dcb = NLMSG_DATA(nlh);
  1112. dcb->dcb_family = AF_UNSPEC;
  1113. dcb->cmd = DCB_CMD_GFEATCFG;
  1114. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_FEATCFG);
  1115. if (!nest) {
  1116. ret = -EMSGSIZE;
  1117. goto nla_put_failure;
  1118. }
  1119. if (data[DCB_FEATCFG_ATTR_ALL])
  1120. getall = 1;
  1121. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1122. if (!getall && !data[i])
  1123. continue;
  1124. ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
  1125. if (!ret)
  1126. ret = nla_put_u8(dcbnl_skb, i, value);
  1127. if (ret) {
  1128. nla_nest_cancel(dcbnl_skb, nest);
  1129. goto nla_put_failure;
  1130. }
  1131. }
  1132. nla_nest_end(dcbnl_skb, nest);
  1133. nlmsg_end(dcbnl_skb, nlh);
  1134. return rtnl_unicast(dcbnl_skb, &init_net, pid);
  1135. nla_put_failure:
  1136. nlmsg_cancel(dcbnl_skb, nlh);
  1137. nlmsg_failure:
  1138. kfree_skb(dcbnl_skb);
  1139. err_out:
  1140. return ret;
  1141. }
  1142. static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlattr **tb,
  1143. u32 pid, u32 seq, u16 flags)
  1144. {
  1145. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
  1146. int ret, i;
  1147. u8 value;
  1148. if (!netdev->dcbnl_ops->setfeatcfg)
  1149. return -ENOTSUPP;
  1150. if (!tb[DCB_ATTR_FEATCFG])
  1151. return -EINVAL;
  1152. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1153. dcbnl_featcfg_nest);
  1154. if (ret)
  1155. goto err;
  1156. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1157. if (data[i] == NULL)
  1158. continue;
  1159. value = nla_get_u8(data[i]);
  1160. ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  1161. if (ret)
  1162. goto err;
  1163. }
  1164. err:
  1165. dcbnl_reply(ret, RTM_SETDCB, DCB_CMD_SFEATCFG, DCB_ATTR_FEATCFG,
  1166. pid, seq, flags);
  1167. return ret;
  1168. }
  1169. static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1170. {
  1171. struct net *net = sock_net(skb->sk);
  1172. struct net_device *netdev;
  1173. struct dcbmsg *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
  1174. struct nlattr *tb[DCB_ATTR_MAX + 1];
  1175. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  1176. int ret = -EINVAL;
  1177. if (!net_eq(net, &init_net))
  1178. return -EINVAL;
  1179. ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  1180. dcbnl_rtnl_policy);
  1181. if (ret < 0)
  1182. return ret;
  1183. if (!tb[DCB_ATTR_IFNAME])
  1184. return -EINVAL;
  1185. netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
  1186. if (!netdev)
  1187. return -EINVAL;
  1188. if (!netdev->dcbnl_ops)
  1189. goto errout;
  1190. switch (dcb->cmd) {
  1191. case DCB_CMD_GSTATE:
  1192. ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
  1193. nlh->nlmsg_flags);
  1194. goto out;
  1195. case DCB_CMD_PFC_GCFG:
  1196. ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
  1197. nlh->nlmsg_flags);
  1198. goto out;
  1199. case DCB_CMD_GPERM_HWADDR:
  1200. ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
  1201. nlh->nlmsg_flags);
  1202. goto out;
  1203. case DCB_CMD_PGTX_GCFG:
  1204. ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1205. nlh->nlmsg_flags);
  1206. goto out;
  1207. case DCB_CMD_PGRX_GCFG:
  1208. ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1209. nlh->nlmsg_flags);
  1210. goto out;
  1211. case DCB_CMD_BCN_GCFG:
  1212. ret = dcbnl_bcn_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1213. nlh->nlmsg_flags);
  1214. goto out;
  1215. case DCB_CMD_SSTATE:
  1216. ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
  1217. nlh->nlmsg_flags);
  1218. goto out;
  1219. case DCB_CMD_PFC_SCFG:
  1220. ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
  1221. nlh->nlmsg_flags);
  1222. goto out;
  1223. case DCB_CMD_SET_ALL:
  1224. ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
  1225. nlh->nlmsg_flags);
  1226. goto out;
  1227. case DCB_CMD_PGTX_SCFG:
  1228. ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1229. nlh->nlmsg_flags);
  1230. goto out;
  1231. case DCB_CMD_PGRX_SCFG:
  1232. ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1233. nlh->nlmsg_flags);
  1234. goto out;
  1235. case DCB_CMD_GCAP:
  1236. ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
  1237. nlh->nlmsg_flags);
  1238. goto out;
  1239. case DCB_CMD_GNUMTCS:
  1240. ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
  1241. nlh->nlmsg_flags);
  1242. goto out;
  1243. case DCB_CMD_SNUMTCS:
  1244. ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
  1245. nlh->nlmsg_flags);
  1246. goto out;
  1247. case DCB_CMD_PFC_GSTATE:
  1248. ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
  1249. nlh->nlmsg_flags);
  1250. goto out;
  1251. case DCB_CMD_PFC_SSTATE:
  1252. ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
  1253. nlh->nlmsg_flags);
  1254. goto out;
  1255. case DCB_CMD_BCN_SCFG:
  1256. ret = dcbnl_bcn_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1257. nlh->nlmsg_flags);
  1258. goto out;
  1259. case DCB_CMD_GAPP:
  1260. ret = dcbnl_getapp(netdev, tb, pid, nlh->nlmsg_seq,
  1261. nlh->nlmsg_flags);
  1262. goto out;
  1263. case DCB_CMD_SAPP:
  1264. ret = dcbnl_setapp(netdev, tb, pid, nlh->nlmsg_seq,
  1265. nlh->nlmsg_flags);
  1266. goto out;
  1267. case DCB_CMD_IEEE_SET:
  1268. ret = dcbnl_ieee_set(netdev, tb, pid, nlh->nlmsg_seq,
  1269. nlh->nlmsg_flags);
  1270. goto out;
  1271. case DCB_CMD_IEEE_GET:
  1272. ret = dcbnl_ieee_get(netdev, tb, pid, nlh->nlmsg_seq,
  1273. nlh->nlmsg_flags);
  1274. goto out;
  1275. case DCB_CMD_GDCBX:
  1276. ret = dcbnl_getdcbx(netdev, tb, pid, nlh->nlmsg_seq,
  1277. nlh->nlmsg_flags);
  1278. goto out;
  1279. case DCB_CMD_SDCBX:
  1280. ret = dcbnl_setdcbx(netdev, tb, pid, nlh->nlmsg_seq,
  1281. nlh->nlmsg_flags);
  1282. goto out;
  1283. case DCB_CMD_GFEATCFG:
  1284. ret = dcbnl_getfeatcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1285. nlh->nlmsg_flags);
  1286. goto out;
  1287. case DCB_CMD_SFEATCFG:
  1288. ret = dcbnl_setfeatcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1289. nlh->nlmsg_flags);
  1290. goto out;
  1291. default:
  1292. goto errout;
  1293. }
  1294. errout:
  1295. ret = -EINVAL;
  1296. out:
  1297. dev_put(netdev);
  1298. return ret;
  1299. }
  1300. /**
  1301. * dcb_getapp - retrieve the DCBX application user priority
  1302. *
  1303. * On success returns a non-zero 802.1p user priority bitmap
  1304. * otherwise returns 0 as the invalid user priority bitmap to
  1305. * indicate an error.
  1306. */
  1307. u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  1308. {
  1309. struct dcb_app_type *itr;
  1310. u8 prio = 0;
  1311. spin_lock(&dcb_lock);
  1312. list_for_each_entry(itr, &dcb_app_list, list) {
  1313. if (itr->app.selector == app->selector &&
  1314. itr->app.protocol == app->protocol &&
  1315. (strncmp(itr->name, dev->name, IFNAMSIZ) == 0)) {
  1316. prio = itr->app.priority;
  1317. break;
  1318. }
  1319. }
  1320. spin_unlock(&dcb_lock);
  1321. return prio;
  1322. }
  1323. EXPORT_SYMBOL(dcb_getapp);
  1324. /**
  1325. * ixgbe_dcbnl_setapp - add dcb application data to app list
  1326. *
  1327. * Priority 0 is the default priority this removes applications
  1328. * from the app list if the priority is set to zero.
  1329. */
  1330. u8 dcb_setapp(struct net_device *dev, struct dcb_app *new)
  1331. {
  1332. struct dcb_app_type *itr;
  1333. spin_lock(&dcb_lock);
  1334. /* Search for existing match and replace */
  1335. list_for_each_entry(itr, &dcb_app_list, list) {
  1336. if (itr->app.selector == new->selector &&
  1337. itr->app.protocol == new->protocol &&
  1338. (strncmp(itr->name, dev->name, IFNAMSIZ) == 0)) {
  1339. if (new->priority)
  1340. itr->app.priority = new->priority;
  1341. else {
  1342. list_del(&itr->list);
  1343. kfree(itr);
  1344. }
  1345. goto out;
  1346. }
  1347. }
  1348. /* App type does not exist add new application type */
  1349. if (new->priority) {
  1350. struct dcb_app_type *entry;
  1351. entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
  1352. if (!entry) {
  1353. spin_unlock(&dcb_lock);
  1354. return -ENOMEM;
  1355. }
  1356. memcpy(&entry->app, new, sizeof(*new));
  1357. strncpy(entry->name, dev->name, IFNAMSIZ);
  1358. list_add(&entry->list, &dcb_app_list);
  1359. }
  1360. out:
  1361. spin_unlock(&dcb_lock);
  1362. call_dcbevent_notifiers(DCB_APP_EVENT, new);
  1363. return 0;
  1364. }
  1365. EXPORT_SYMBOL(dcb_setapp);
  1366. static void dcb_flushapp(void)
  1367. {
  1368. struct dcb_app_type *app;
  1369. struct dcb_app_type *tmp;
  1370. spin_lock(&dcb_lock);
  1371. list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
  1372. list_del(&app->list);
  1373. kfree(app);
  1374. }
  1375. spin_unlock(&dcb_lock);
  1376. }
  1377. static int __init dcbnl_init(void)
  1378. {
  1379. INIT_LIST_HEAD(&dcb_app_list);
  1380. rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL);
  1381. rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL);
  1382. return 0;
  1383. }
  1384. module_init(dcbnl_init);
  1385. static void __exit dcbnl_exit(void)
  1386. {
  1387. rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
  1388. rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
  1389. dcb_flushapp();
  1390. }
  1391. module_exit(dcbnl_exit);