dcbnl.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. /*
  2. * Copyright (c) 2008-2011, 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 <linux/module.h>
  28. #include <net/sock.h>
  29. /**
  30. * Data Center Bridging (DCB) is a collection of Ethernet enhancements
  31. * intended to allow network traffic with differing requirements
  32. * (highly reliable, no drops vs. best effort vs. low latency) to operate
  33. * and co-exist on Ethernet. Current DCB features are:
  34. *
  35. * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
  36. * framework for assigning bandwidth guarantees to traffic classes.
  37. *
  38. * Priority-based Flow Control (PFC) - provides a flow control mechanism which
  39. * can work independently for each 802.1p priority.
  40. *
  41. * Congestion Notification - provides a mechanism for end-to-end congestion
  42. * control for protocols which do not have built-in congestion management.
  43. *
  44. * More information about the emerging standards for these Ethernet features
  45. * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
  46. *
  47. * This file implements an rtnetlink interface to allow configuration of DCB
  48. * features for capable devices.
  49. */
  50. MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
  51. MODULE_DESCRIPTION("Data Center Bridging netlink interface");
  52. MODULE_LICENSE("GPL");
  53. /**************** DCB attribute policies *************************************/
  54. /* DCB netlink attributes policy */
  55. static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
  56. [DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
  57. [DCB_ATTR_STATE] = {.type = NLA_U8},
  58. [DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},
  59. [DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},
  60. [DCB_ATTR_SET_ALL] = {.type = NLA_U8},
  61. [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
  62. [DCB_ATTR_CAP] = {.type = NLA_NESTED},
  63. [DCB_ATTR_PFC_STATE] = {.type = NLA_U8},
  64. [DCB_ATTR_BCN] = {.type = NLA_NESTED},
  65. [DCB_ATTR_APP] = {.type = NLA_NESTED},
  66. [DCB_ATTR_IEEE] = {.type = NLA_NESTED},
  67. [DCB_ATTR_DCBX] = {.type = NLA_U8},
  68. [DCB_ATTR_FEATCFG] = {.type = NLA_NESTED},
  69. };
  70. /* DCB priority flow control to User Priority nested attributes */
  71. static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
  72. [DCB_PFC_UP_ATTR_0] = {.type = NLA_U8},
  73. [DCB_PFC_UP_ATTR_1] = {.type = NLA_U8},
  74. [DCB_PFC_UP_ATTR_2] = {.type = NLA_U8},
  75. [DCB_PFC_UP_ATTR_3] = {.type = NLA_U8},
  76. [DCB_PFC_UP_ATTR_4] = {.type = NLA_U8},
  77. [DCB_PFC_UP_ATTR_5] = {.type = NLA_U8},
  78. [DCB_PFC_UP_ATTR_6] = {.type = NLA_U8},
  79. [DCB_PFC_UP_ATTR_7] = {.type = NLA_U8},
  80. [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
  81. };
  82. /* DCB priority grouping nested attributes */
  83. static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
  84. [DCB_PG_ATTR_TC_0] = {.type = NLA_NESTED},
  85. [DCB_PG_ATTR_TC_1] = {.type = NLA_NESTED},
  86. [DCB_PG_ATTR_TC_2] = {.type = NLA_NESTED},
  87. [DCB_PG_ATTR_TC_3] = {.type = NLA_NESTED},
  88. [DCB_PG_ATTR_TC_4] = {.type = NLA_NESTED},
  89. [DCB_PG_ATTR_TC_5] = {.type = NLA_NESTED},
  90. [DCB_PG_ATTR_TC_6] = {.type = NLA_NESTED},
  91. [DCB_PG_ATTR_TC_7] = {.type = NLA_NESTED},
  92. [DCB_PG_ATTR_TC_ALL] = {.type = NLA_NESTED},
  93. [DCB_PG_ATTR_BW_ID_0] = {.type = NLA_U8},
  94. [DCB_PG_ATTR_BW_ID_1] = {.type = NLA_U8},
  95. [DCB_PG_ATTR_BW_ID_2] = {.type = NLA_U8},
  96. [DCB_PG_ATTR_BW_ID_3] = {.type = NLA_U8},
  97. [DCB_PG_ATTR_BW_ID_4] = {.type = NLA_U8},
  98. [DCB_PG_ATTR_BW_ID_5] = {.type = NLA_U8},
  99. [DCB_PG_ATTR_BW_ID_6] = {.type = NLA_U8},
  100. [DCB_PG_ATTR_BW_ID_7] = {.type = NLA_U8},
  101. [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
  102. };
  103. /* DCB traffic class nested attributes. */
  104. static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
  105. [DCB_TC_ATTR_PARAM_PGID] = {.type = NLA_U8},
  106. [DCB_TC_ATTR_PARAM_UP_MAPPING] = {.type = NLA_U8},
  107. [DCB_TC_ATTR_PARAM_STRICT_PRIO] = {.type = NLA_U8},
  108. [DCB_TC_ATTR_PARAM_BW_PCT] = {.type = NLA_U8},
  109. [DCB_TC_ATTR_PARAM_ALL] = {.type = NLA_FLAG},
  110. };
  111. /* DCB capabilities nested attributes. */
  112. static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
  113. [DCB_CAP_ATTR_ALL] = {.type = NLA_FLAG},
  114. [DCB_CAP_ATTR_PG] = {.type = NLA_U8},
  115. [DCB_CAP_ATTR_PFC] = {.type = NLA_U8},
  116. [DCB_CAP_ATTR_UP2TC] = {.type = NLA_U8},
  117. [DCB_CAP_ATTR_PG_TCS] = {.type = NLA_U8},
  118. [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
  119. [DCB_CAP_ATTR_GSP] = {.type = NLA_U8},
  120. [DCB_CAP_ATTR_BCN] = {.type = NLA_U8},
  121. [DCB_CAP_ATTR_DCBX] = {.type = NLA_U8},
  122. };
  123. /* DCB capabilities nested attributes. */
  124. static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
  125. [DCB_NUMTCS_ATTR_ALL] = {.type = NLA_FLAG},
  126. [DCB_NUMTCS_ATTR_PG] = {.type = NLA_U8},
  127. [DCB_NUMTCS_ATTR_PFC] = {.type = NLA_U8},
  128. };
  129. /* DCB BCN nested attributes. */
  130. static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
  131. [DCB_BCN_ATTR_RP_0] = {.type = NLA_U8},
  132. [DCB_BCN_ATTR_RP_1] = {.type = NLA_U8},
  133. [DCB_BCN_ATTR_RP_2] = {.type = NLA_U8},
  134. [DCB_BCN_ATTR_RP_3] = {.type = NLA_U8},
  135. [DCB_BCN_ATTR_RP_4] = {.type = NLA_U8},
  136. [DCB_BCN_ATTR_RP_5] = {.type = NLA_U8},
  137. [DCB_BCN_ATTR_RP_6] = {.type = NLA_U8},
  138. [DCB_BCN_ATTR_RP_7] = {.type = NLA_U8},
  139. [DCB_BCN_ATTR_RP_ALL] = {.type = NLA_FLAG},
  140. [DCB_BCN_ATTR_BCNA_0] = {.type = NLA_U32},
  141. [DCB_BCN_ATTR_BCNA_1] = {.type = NLA_U32},
  142. [DCB_BCN_ATTR_ALPHA] = {.type = NLA_U32},
  143. [DCB_BCN_ATTR_BETA] = {.type = NLA_U32},
  144. [DCB_BCN_ATTR_GD] = {.type = NLA_U32},
  145. [DCB_BCN_ATTR_GI] = {.type = NLA_U32},
  146. [DCB_BCN_ATTR_TMAX] = {.type = NLA_U32},
  147. [DCB_BCN_ATTR_TD] = {.type = NLA_U32},
  148. [DCB_BCN_ATTR_RMIN] = {.type = NLA_U32},
  149. [DCB_BCN_ATTR_W] = {.type = NLA_U32},
  150. [DCB_BCN_ATTR_RD] = {.type = NLA_U32},
  151. [DCB_BCN_ATTR_RU] = {.type = NLA_U32},
  152. [DCB_BCN_ATTR_WRTT] = {.type = NLA_U32},
  153. [DCB_BCN_ATTR_RI] = {.type = NLA_U32},
  154. [DCB_BCN_ATTR_C] = {.type = NLA_U32},
  155. [DCB_BCN_ATTR_ALL] = {.type = NLA_FLAG},
  156. };
  157. /* DCB APP nested attributes. */
  158. static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
  159. [DCB_APP_ATTR_IDTYPE] = {.type = NLA_U8},
  160. [DCB_APP_ATTR_ID] = {.type = NLA_U16},
  161. [DCB_APP_ATTR_PRIORITY] = {.type = NLA_U8},
  162. };
  163. /* IEEE 802.1Qaz nested attributes. */
  164. static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
  165. [DCB_ATTR_IEEE_ETS] = {.len = sizeof(struct ieee_ets)},
  166. [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
  167. [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
  168. [DCB_ATTR_IEEE_MAXRATE] = {.len = sizeof(struct ieee_maxrate)},
  169. };
  170. static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
  171. [DCB_ATTR_IEEE_APP] = {.len = sizeof(struct dcb_app)},
  172. };
  173. /* DCB number of traffic classes nested attributes. */
  174. static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
  175. [DCB_FEATCFG_ATTR_ALL] = {.type = NLA_FLAG},
  176. [DCB_FEATCFG_ATTR_PG] = {.type = NLA_U8},
  177. [DCB_FEATCFG_ATTR_PFC] = {.type = NLA_U8},
  178. [DCB_FEATCFG_ATTR_APP] = {.type = NLA_U8},
  179. };
  180. static LIST_HEAD(dcb_app_list);
  181. static DEFINE_SPINLOCK(dcb_lock);
  182. /* standard netlink reply call */
  183. static int dcbnl_reply(u8 value, u8 event, u8 cmd, u8 attr, u32 pid,
  184. u32 seq, u16 flags)
  185. {
  186. struct sk_buff *dcbnl_skb;
  187. struct dcbmsg *dcb;
  188. struct nlmsghdr *nlh;
  189. int ret = -EINVAL;
  190. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  191. if (!dcbnl_skb)
  192. return ret;
  193. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, event, sizeof(*dcb), flags);
  194. dcb = NLMSG_DATA(nlh);
  195. dcb->dcb_family = AF_UNSPEC;
  196. dcb->cmd = cmd;
  197. dcb->dcb_pad = 0;
  198. ret = nla_put_u8(dcbnl_skb, attr, value);
  199. if (ret)
  200. goto err;
  201. /* end the message, assign the nlmsg_len. */
  202. nlmsg_end(dcbnl_skb, nlh);
  203. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  204. if (ret)
  205. return -EINVAL;
  206. return 0;
  207. nlmsg_failure:
  208. err:
  209. kfree_skb(dcbnl_skb);
  210. return ret;
  211. }
  212. static int dcbnl_getstate(struct net_device *netdev, struct nlattr **tb,
  213. u32 pid, u32 seq, u16 flags)
  214. {
  215. int ret = -EINVAL;
  216. /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
  217. if (!netdev->dcbnl_ops->getstate)
  218. return ret;
  219. ret = dcbnl_reply(netdev->dcbnl_ops->getstate(netdev), RTM_GETDCB,
  220. DCB_CMD_GSTATE, DCB_ATTR_STATE, pid, seq, flags);
  221. return ret;
  222. }
  223. static int dcbnl_getpfccfg(struct net_device *netdev, struct nlattr **tb,
  224. u32 pid, u32 seq, u16 flags)
  225. {
  226. struct sk_buff *dcbnl_skb;
  227. struct nlmsghdr *nlh;
  228. struct dcbmsg *dcb;
  229. struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
  230. u8 value;
  231. int ret = -EINVAL;
  232. int i;
  233. int getall = 0;
  234. if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
  235. return ret;
  236. ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  237. tb[DCB_ATTR_PFC_CFG],
  238. dcbnl_pfc_up_nest);
  239. if (ret)
  240. goto err_out;
  241. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  242. if (!dcbnl_skb)
  243. goto err_out;
  244. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  245. dcb = NLMSG_DATA(nlh);
  246. dcb->dcb_family = AF_UNSPEC;
  247. dcb->cmd = DCB_CMD_PFC_GCFG;
  248. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PFC_CFG);
  249. if (!nest)
  250. goto err;
  251. if (data[DCB_PFC_UP_ATTR_ALL])
  252. getall = 1;
  253. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  254. if (!getall && !data[i])
  255. continue;
  256. netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
  257. &value);
  258. ret = nla_put_u8(dcbnl_skb, i, value);
  259. if (ret) {
  260. nla_nest_cancel(dcbnl_skb, nest);
  261. goto err;
  262. }
  263. }
  264. nla_nest_end(dcbnl_skb, nest);
  265. nlmsg_end(dcbnl_skb, nlh);
  266. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  267. if (ret)
  268. goto err_out;
  269. return 0;
  270. nlmsg_failure:
  271. err:
  272. kfree_skb(dcbnl_skb);
  273. err_out:
  274. return -EINVAL;
  275. }
  276. static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlattr **tb,
  277. u32 pid, u32 seq, u16 flags)
  278. {
  279. struct sk_buff *dcbnl_skb;
  280. struct nlmsghdr *nlh;
  281. struct dcbmsg *dcb;
  282. u8 perm_addr[MAX_ADDR_LEN];
  283. int ret = -EINVAL;
  284. if (!netdev->dcbnl_ops->getpermhwaddr)
  285. return ret;
  286. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  287. if (!dcbnl_skb)
  288. goto err_out;
  289. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  290. dcb = NLMSG_DATA(nlh);
  291. dcb->dcb_family = AF_UNSPEC;
  292. dcb->cmd = DCB_CMD_GPERM_HWADDR;
  293. netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
  294. ret = nla_put(dcbnl_skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr),
  295. perm_addr);
  296. nlmsg_end(dcbnl_skb, nlh);
  297. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  298. if (ret)
  299. goto err_out;
  300. return 0;
  301. nlmsg_failure:
  302. kfree_skb(dcbnl_skb);
  303. err_out:
  304. return -EINVAL;
  305. }
  306. static int dcbnl_getcap(struct net_device *netdev, struct nlattr **tb,
  307. u32 pid, u32 seq, u16 flags)
  308. {
  309. struct sk_buff *dcbnl_skb;
  310. struct nlmsghdr *nlh;
  311. struct dcbmsg *dcb;
  312. struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
  313. u8 value;
  314. int ret = -EINVAL;
  315. int i;
  316. int getall = 0;
  317. if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
  318. return ret;
  319. ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
  320. dcbnl_cap_nest);
  321. if (ret)
  322. goto err_out;
  323. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  324. if (!dcbnl_skb)
  325. goto err_out;
  326. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  327. dcb = NLMSG_DATA(nlh);
  328. dcb->dcb_family = AF_UNSPEC;
  329. dcb->cmd = DCB_CMD_GCAP;
  330. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_CAP);
  331. if (!nest)
  332. goto err;
  333. if (data[DCB_CAP_ATTR_ALL])
  334. getall = 1;
  335. for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
  336. if (!getall && !data[i])
  337. continue;
  338. if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
  339. ret = nla_put_u8(dcbnl_skb, i, value);
  340. if (ret) {
  341. nla_nest_cancel(dcbnl_skb, nest);
  342. goto err;
  343. }
  344. }
  345. }
  346. nla_nest_end(dcbnl_skb, nest);
  347. nlmsg_end(dcbnl_skb, nlh);
  348. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  349. if (ret)
  350. goto err_out;
  351. return 0;
  352. nlmsg_failure:
  353. err:
  354. kfree_skb(dcbnl_skb);
  355. err_out:
  356. return -EINVAL;
  357. }
  358. static int dcbnl_getnumtcs(struct net_device *netdev, struct nlattr **tb,
  359. u32 pid, u32 seq, u16 flags)
  360. {
  361. struct sk_buff *dcbnl_skb;
  362. struct nlmsghdr *nlh;
  363. struct dcbmsg *dcb;
  364. struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
  365. u8 value;
  366. int ret = -EINVAL;
  367. int i;
  368. int getall = 0;
  369. if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
  370. return ret;
  371. ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  372. dcbnl_numtcs_nest);
  373. if (ret) {
  374. ret = -EINVAL;
  375. goto err_out;
  376. }
  377. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  378. if (!dcbnl_skb) {
  379. ret = -EINVAL;
  380. goto err_out;
  381. }
  382. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  383. dcb = NLMSG_DATA(nlh);
  384. dcb->dcb_family = AF_UNSPEC;
  385. dcb->cmd = DCB_CMD_GNUMTCS;
  386. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_NUMTCS);
  387. if (!nest) {
  388. ret = -EINVAL;
  389. goto err;
  390. }
  391. if (data[DCB_NUMTCS_ATTR_ALL])
  392. getall = 1;
  393. for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  394. if (!getall && !data[i])
  395. continue;
  396. ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
  397. if (!ret) {
  398. ret = nla_put_u8(dcbnl_skb, i, value);
  399. if (ret) {
  400. nla_nest_cancel(dcbnl_skb, nest);
  401. ret = -EINVAL;
  402. goto err;
  403. }
  404. } else {
  405. goto err;
  406. }
  407. }
  408. nla_nest_end(dcbnl_skb, nest);
  409. nlmsg_end(dcbnl_skb, nlh);
  410. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  411. if (ret) {
  412. ret = -EINVAL;
  413. goto err_out;
  414. }
  415. return 0;
  416. nlmsg_failure:
  417. err:
  418. kfree_skb(dcbnl_skb);
  419. err_out:
  420. return ret;
  421. }
  422. static int dcbnl_setnumtcs(struct net_device *netdev, struct nlattr **tb,
  423. u32 pid, u32 seq, u16 flags)
  424. {
  425. struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
  426. int ret = -EINVAL;
  427. u8 value;
  428. int i;
  429. if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
  430. return ret;
  431. ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  432. dcbnl_numtcs_nest);
  433. if (ret) {
  434. ret = -EINVAL;
  435. goto err;
  436. }
  437. for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  438. if (data[i] == NULL)
  439. continue;
  440. value = nla_get_u8(data[i]);
  441. ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
  442. if (ret)
  443. goto operr;
  444. }
  445. operr:
  446. ret = dcbnl_reply(!!ret, RTM_SETDCB, DCB_CMD_SNUMTCS,
  447. DCB_ATTR_NUMTCS, pid, seq, flags);
  448. err:
  449. return ret;
  450. }
  451. static int dcbnl_getpfcstate(struct net_device *netdev, struct nlattr **tb,
  452. u32 pid, u32 seq, u16 flags)
  453. {
  454. int ret = -EINVAL;
  455. if (!netdev->dcbnl_ops->getpfcstate)
  456. return ret;
  457. ret = dcbnl_reply(netdev->dcbnl_ops->getpfcstate(netdev), RTM_GETDCB,
  458. DCB_CMD_PFC_GSTATE, DCB_ATTR_PFC_STATE,
  459. pid, seq, flags);
  460. return ret;
  461. }
  462. static int dcbnl_setpfcstate(struct net_device *netdev, struct nlattr **tb,
  463. u32 pid, u32 seq, u16 flags)
  464. {
  465. int ret = -EINVAL;
  466. u8 value;
  467. if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
  468. return ret;
  469. value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
  470. netdev->dcbnl_ops->setpfcstate(netdev, value);
  471. ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SSTATE, DCB_ATTR_PFC_STATE,
  472. pid, seq, flags);
  473. return ret;
  474. }
  475. static int dcbnl_getapp(struct net_device *netdev, struct nlattr **tb,
  476. u32 pid, u32 seq, u16 flags)
  477. {
  478. struct sk_buff *dcbnl_skb;
  479. struct nlmsghdr *nlh;
  480. struct dcbmsg *dcb;
  481. struct nlattr *app_nest;
  482. struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
  483. u16 id;
  484. u8 up, idtype;
  485. int ret = -EINVAL;
  486. if (!tb[DCB_ATTR_APP])
  487. goto out;
  488. ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  489. dcbnl_app_nest);
  490. if (ret)
  491. goto out;
  492. ret = -EINVAL;
  493. /* all must be non-null */
  494. if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  495. (!app_tb[DCB_APP_ATTR_ID]))
  496. goto out;
  497. /* either by eth type or by socket number */
  498. idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  499. if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  500. (idtype != DCB_APP_IDTYPE_PORTNUM))
  501. goto out;
  502. id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
  503. if (netdev->dcbnl_ops->getapp) {
  504. up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
  505. } else {
  506. struct dcb_app app = {
  507. .selector = idtype,
  508. .protocol = id,
  509. };
  510. up = dcb_getapp(netdev, &app);
  511. }
  512. /* send this back */
  513. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  514. if (!dcbnl_skb)
  515. goto out;
  516. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  517. dcb = NLMSG_DATA(nlh);
  518. dcb->dcb_family = AF_UNSPEC;
  519. dcb->cmd = DCB_CMD_GAPP;
  520. app_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_APP);
  521. if (!app_nest)
  522. goto out_cancel;
  523. ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_IDTYPE, idtype);
  524. if (ret)
  525. goto out_cancel;
  526. ret = nla_put_u16(dcbnl_skb, DCB_APP_ATTR_ID, id);
  527. if (ret)
  528. goto out_cancel;
  529. ret = nla_put_u8(dcbnl_skb, DCB_APP_ATTR_PRIORITY, up);
  530. if (ret)
  531. goto out_cancel;
  532. nla_nest_end(dcbnl_skb, app_nest);
  533. nlmsg_end(dcbnl_skb, nlh);
  534. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  535. if (ret)
  536. goto nlmsg_failure;
  537. goto out;
  538. out_cancel:
  539. nla_nest_cancel(dcbnl_skb, app_nest);
  540. nlmsg_failure:
  541. kfree_skb(dcbnl_skb);
  542. out:
  543. return ret;
  544. }
  545. static int dcbnl_setapp(struct net_device *netdev, struct nlattr **tb,
  546. u32 pid, u32 seq, u16 flags)
  547. {
  548. int err, ret = -EINVAL;
  549. u16 id;
  550. u8 up, idtype;
  551. struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
  552. if (!tb[DCB_ATTR_APP])
  553. goto out;
  554. ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  555. dcbnl_app_nest);
  556. if (ret)
  557. goto out;
  558. ret = -EINVAL;
  559. /* all must be non-null */
  560. if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  561. (!app_tb[DCB_APP_ATTR_ID]) ||
  562. (!app_tb[DCB_APP_ATTR_PRIORITY]))
  563. goto out;
  564. /* either by eth type or by socket number */
  565. idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  566. if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  567. (idtype != DCB_APP_IDTYPE_PORTNUM))
  568. goto out;
  569. id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
  570. up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
  571. if (netdev->dcbnl_ops->setapp) {
  572. err = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
  573. } else {
  574. struct dcb_app app;
  575. app.selector = idtype;
  576. app.protocol = id;
  577. app.priority = up;
  578. err = dcb_setapp(netdev, &app);
  579. }
  580. ret = dcbnl_reply(err, RTM_SETDCB, DCB_CMD_SAPP, DCB_ATTR_APP,
  581. pid, seq, flags);
  582. out:
  583. return ret;
  584. }
  585. static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlattr **tb,
  586. u32 pid, u32 seq, u16 flags, int dir)
  587. {
  588. struct sk_buff *dcbnl_skb;
  589. struct nlmsghdr *nlh;
  590. struct dcbmsg *dcb;
  591. struct nlattr *pg_nest, *param_nest, *data;
  592. struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  593. struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  594. u8 prio, pgid, tc_pct, up_map;
  595. int ret = -EINVAL;
  596. int getall = 0;
  597. int i;
  598. if (!tb[DCB_ATTR_PG_CFG] ||
  599. !netdev->dcbnl_ops->getpgtccfgtx ||
  600. !netdev->dcbnl_ops->getpgtccfgrx ||
  601. !netdev->dcbnl_ops->getpgbwgcfgtx ||
  602. !netdev->dcbnl_ops->getpgbwgcfgrx)
  603. return ret;
  604. ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  605. tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  606. if (ret)
  607. goto err_out;
  608. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  609. if (!dcbnl_skb)
  610. goto err_out;
  611. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  612. dcb = NLMSG_DATA(nlh);
  613. dcb->dcb_family = AF_UNSPEC;
  614. dcb->cmd = (dir) ? DCB_CMD_PGRX_GCFG : DCB_CMD_PGTX_GCFG;
  615. pg_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_PG_CFG);
  616. if (!pg_nest)
  617. goto err;
  618. if (pg_tb[DCB_PG_ATTR_TC_ALL])
  619. getall = 1;
  620. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  621. if (!getall && !pg_tb[i])
  622. continue;
  623. if (pg_tb[DCB_PG_ATTR_TC_ALL])
  624. data = pg_tb[DCB_PG_ATTR_TC_ALL];
  625. else
  626. data = pg_tb[i];
  627. ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  628. data, dcbnl_tc_param_nest);
  629. if (ret)
  630. goto err_pg;
  631. param_nest = nla_nest_start(dcbnl_skb, i);
  632. if (!param_nest)
  633. goto err_pg;
  634. pgid = DCB_ATTR_VALUE_UNDEFINED;
  635. prio = DCB_ATTR_VALUE_UNDEFINED;
  636. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  637. up_map = DCB_ATTR_VALUE_UNDEFINED;
  638. if (dir) {
  639. /* Rx */
  640. netdev->dcbnl_ops->getpgtccfgrx(netdev,
  641. i - DCB_PG_ATTR_TC_0, &prio,
  642. &pgid, &tc_pct, &up_map);
  643. } else {
  644. /* Tx */
  645. netdev->dcbnl_ops->getpgtccfgtx(netdev,
  646. i - DCB_PG_ATTR_TC_0, &prio,
  647. &pgid, &tc_pct, &up_map);
  648. }
  649. if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
  650. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  651. ret = nla_put_u8(dcbnl_skb,
  652. DCB_TC_ATTR_PARAM_PGID, pgid);
  653. if (ret)
  654. goto err_param;
  655. }
  656. if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
  657. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  658. ret = nla_put_u8(dcbnl_skb,
  659. DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
  660. if (ret)
  661. goto err_param;
  662. }
  663. if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
  664. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  665. ret = nla_put_u8(dcbnl_skb,
  666. DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
  667. if (ret)
  668. goto err_param;
  669. }
  670. if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
  671. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  672. ret = nla_put_u8(dcbnl_skb, DCB_TC_ATTR_PARAM_BW_PCT,
  673. tc_pct);
  674. if (ret)
  675. goto err_param;
  676. }
  677. nla_nest_end(dcbnl_skb, param_nest);
  678. }
  679. if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
  680. getall = 1;
  681. else
  682. getall = 0;
  683. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  684. if (!getall && !pg_tb[i])
  685. continue;
  686. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  687. if (dir) {
  688. /* Rx */
  689. netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
  690. i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  691. } else {
  692. /* Tx */
  693. netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
  694. i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  695. }
  696. ret = nla_put_u8(dcbnl_skb, i, tc_pct);
  697. if (ret)
  698. goto err_pg;
  699. }
  700. nla_nest_end(dcbnl_skb, pg_nest);
  701. nlmsg_end(dcbnl_skb, nlh);
  702. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  703. if (ret)
  704. goto err_out;
  705. return 0;
  706. err_param:
  707. nla_nest_cancel(dcbnl_skb, param_nest);
  708. err_pg:
  709. nla_nest_cancel(dcbnl_skb, pg_nest);
  710. nlmsg_failure:
  711. err:
  712. kfree_skb(dcbnl_skb);
  713. err_out:
  714. ret = -EINVAL;
  715. return ret;
  716. }
  717. static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlattr **tb,
  718. u32 pid, u32 seq, u16 flags)
  719. {
  720. return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 0);
  721. }
  722. static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlattr **tb,
  723. u32 pid, u32 seq, u16 flags)
  724. {
  725. return __dcbnl_pg_getcfg(netdev, tb, pid, seq, flags, 1);
  726. }
  727. static int dcbnl_setstate(struct net_device *netdev, struct nlattr **tb,
  728. u32 pid, u32 seq, u16 flags)
  729. {
  730. int ret = -EINVAL;
  731. u8 value;
  732. if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
  733. return ret;
  734. value = nla_get_u8(tb[DCB_ATTR_STATE]);
  735. ret = dcbnl_reply(netdev->dcbnl_ops->setstate(netdev, value),
  736. RTM_SETDCB, DCB_CMD_SSTATE, DCB_ATTR_STATE,
  737. pid, seq, flags);
  738. return ret;
  739. }
  740. static int dcbnl_setpfccfg(struct net_device *netdev, struct nlattr **tb,
  741. u32 pid, u32 seq, u16 flags)
  742. {
  743. struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
  744. int i;
  745. int ret = -EINVAL;
  746. u8 value;
  747. if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
  748. return ret;
  749. ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  750. tb[DCB_ATTR_PFC_CFG],
  751. dcbnl_pfc_up_nest);
  752. if (ret)
  753. goto err;
  754. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  755. if (data[i] == NULL)
  756. continue;
  757. value = nla_get_u8(data[i]);
  758. netdev->dcbnl_ops->setpfccfg(netdev,
  759. data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
  760. }
  761. ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_PFC_SCFG, DCB_ATTR_PFC_CFG,
  762. pid, seq, flags);
  763. err:
  764. return ret;
  765. }
  766. static int dcbnl_setall(struct net_device *netdev, struct nlattr **tb,
  767. u32 pid, u32 seq, u16 flags)
  768. {
  769. int ret = -EINVAL;
  770. if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
  771. return ret;
  772. ret = dcbnl_reply(netdev->dcbnl_ops->setall(netdev), RTM_SETDCB,
  773. DCB_CMD_SET_ALL, DCB_ATTR_SET_ALL, pid, seq, flags);
  774. return ret;
  775. }
  776. static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlattr **tb,
  777. u32 pid, u32 seq, u16 flags, int dir)
  778. {
  779. struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  780. struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  781. int ret = -EINVAL;
  782. int i;
  783. u8 pgid;
  784. u8 up_map;
  785. u8 prio;
  786. u8 tc_pct;
  787. if (!tb[DCB_ATTR_PG_CFG] ||
  788. !netdev->dcbnl_ops->setpgtccfgtx ||
  789. !netdev->dcbnl_ops->setpgtccfgrx ||
  790. !netdev->dcbnl_ops->setpgbwgcfgtx ||
  791. !netdev->dcbnl_ops->setpgbwgcfgrx)
  792. return ret;
  793. ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  794. tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  795. if (ret)
  796. goto err;
  797. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  798. if (!pg_tb[i])
  799. continue;
  800. ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  801. pg_tb[i], dcbnl_tc_param_nest);
  802. if (ret)
  803. goto err;
  804. pgid = DCB_ATTR_VALUE_UNDEFINED;
  805. prio = DCB_ATTR_VALUE_UNDEFINED;
  806. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  807. up_map = DCB_ATTR_VALUE_UNDEFINED;
  808. if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
  809. prio =
  810. nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
  811. if (param_tb[DCB_TC_ATTR_PARAM_PGID])
  812. pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
  813. if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
  814. tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
  815. if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
  816. up_map =
  817. nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
  818. /* dir: Tx = 0, Rx = 1 */
  819. if (dir) {
  820. /* Rx */
  821. netdev->dcbnl_ops->setpgtccfgrx(netdev,
  822. i - DCB_PG_ATTR_TC_0,
  823. prio, pgid, tc_pct, up_map);
  824. } else {
  825. /* Tx */
  826. netdev->dcbnl_ops->setpgtccfgtx(netdev,
  827. i - DCB_PG_ATTR_TC_0,
  828. prio, pgid, tc_pct, up_map);
  829. }
  830. }
  831. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  832. if (!pg_tb[i])
  833. continue;
  834. tc_pct = nla_get_u8(pg_tb[i]);
  835. /* dir: Tx = 0, Rx = 1 */
  836. if (dir) {
  837. /* Rx */
  838. netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
  839. i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  840. } else {
  841. /* Tx */
  842. netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
  843. i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  844. }
  845. }
  846. ret = dcbnl_reply(0, RTM_SETDCB,
  847. (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG),
  848. DCB_ATTR_PG_CFG, pid, seq, flags);
  849. err:
  850. return ret;
  851. }
  852. static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlattr **tb,
  853. u32 pid, u32 seq, u16 flags)
  854. {
  855. return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 0);
  856. }
  857. static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlattr **tb,
  858. u32 pid, u32 seq, u16 flags)
  859. {
  860. return __dcbnl_pg_setcfg(netdev, tb, pid, seq, flags, 1);
  861. }
  862. static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlattr **tb,
  863. u32 pid, u32 seq, u16 flags)
  864. {
  865. struct sk_buff *dcbnl_skb;
  866. struct nlmsghdr *nlh;
  867. struct dcbmsg *dcb;
  868. struct nlattr *bcn_nest;
  869. struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
  870. u8 value_byte;
  871. u32 value_integer;
  872. int ret = -EINVAL;
  873. bool getall = false;
  874. int i;
  875. if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
  876. !netdev->dcbnl_ops->getbcncfg)
  877. return ret;
  878. ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
  879. tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
  880. if (ret)
  881. goto err_out;
  882. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  883. if (!dcbnl_skb)
  884. goto err_out;
  885. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  886. dcb = NLMSG_DATA(nlh);
  887. dcb->dcb_family = AF_UNSPEC;
  888. dcb->cmd = DCB_CMD_BCN_GCFG;
  889. bcn_nest = nla_nest_start(dcbnl_skb, DCB_ATTR_BCN);
  890. if (!bcn_nest)
  891. goto err;
  892. if (bcn_tb[DCB_BCN_ATTR_ALL])
  893. getall = true;
  894. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  895. if (!getall && !bcn_tb[i])
  896. continue;
  897. netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
  898. &value_byte);
  899. ret = nla_put_u8(dcbnl_skb, i, value_byte);
  900. if (ret)
  901. goto err_bcn;
  902. }
  903. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  904. if (!getall && !bcn_tb[i])
  905. continue;
  906. netdev->dcbnl_ops->getbcncfg(netdev, i,
  907. &value_integer);
  908. ret = nla_put_u32(dcbnl_skb, i, value_integer);
  909. if (ret)
  910. goto err_bcn;
  911. }
  912. nla_nest_end(dcbnl_skb, bcn_nest);
  913. nlmsg_end(dcbnl_skb, nlh);
  914. ret = rtnl_unicast(dcbnl_skb, &init_net, pid);
  915. if (ret)
  916. goto err_out;
  917. return 0;
  918. err_bcn:
  919. nla_nest_cancel(dcbnl_skb, bcn_nest);
  920. nlmsg_failure:
  921. err:
  922. kfree_skb(dcbnl_skb);
  923. err_out:
  924. ret = -EINVAL;
  925. return ret;
  926. }
  927. static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlattr **tb,
  928. u32 pid, u32 seq, u16 flags)
  929. {
  930. struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
  931. int i;
  932. int ret = -EINVAL;
  933. u8 value_byte;
  934. u32 value_int;
  935. if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg ||
  936. !netdev->dcbnl_ops->setbcnrp)
  937. return ret;
  938. ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
  939. tb[DCB_ATTR_BCN],
  940. dcbnl_pfc_up_nest);
  941. if (ret)
  942. goto err;
  943. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  944. if (data[i] == NULL)
  945. continue;
  946. value_byte = nla_get_u8(data[i]);
  947. netdev->dcbnl_ops->setbcnrp(netdev,
  948. data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
  949. }
  950. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  951. if (data[i] == NULL)
  952. continue;
  953. value_int = nla_get_u32(data[i]);
  954. netdev->dcbnl_ops->setbcncfg(netdev,
  955. i, value_int);
  956. }
  957. ret = dcbnl_reply(0, RTM_SETDCB, DCB_CMD_BCN_SCFG, DCB_ATTR_BCN,
  958. pid, seq, flags);
  959. err:
  960. return ret;
  961. }
  962. static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
  963. int app_nested_type, int app_info_type,
  964. int app_entry_type)
  965. {
  966. struct dcb_peer_app_info info;
  967. struct dcb_app *table = NULL;
  968. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  969. u16 app_count;
  970. int err;
  971. /**
  972. * retrieve the peer app configuration form the driver. If the driver
  973. * handlers fail exit without doing anything
  974. */
  975. err = ops->peer_getappinfo(netdev, &info, &app_count);
  976. if (!err && app_count) {
  977. table = kmalloc(sizeof(struct dcb_app) * app_count, GFP_KERNEL);
  978. if (!table)
  979. return -ENOMEM;
  980. err = ops->peer_getapptable(netdev, table);
  981. }
  982. if (!err) {
  983. u16 i;
  984. struct nlattr *app;
  985. /**
  986. * build the message, from here on the only possible failure
  987. * is due to the skb size
  988. */
  989. err = -EMSGSIZE;
  990. app = nla_nest_start(skb, app_nested_type);
  991. if (!app)
  992. goto nla_put_failure;
  993. if (app_info_type &&
  994. nla_put(skb, app_info_type, sizeof(info), &info))
  995. goto nla_put_failure;
  996. for (i = 0; i < app_count; i++) {
  997. if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
  998. &table[i]))
  999. goto nla_put_failure;
  1000. }
  1001. nla_nest_end(skb, app);
  1002. }
  1003. err = 0;
  1004. nla_put_failure:
  1005. kfree(table);
  1006. return err;
  1007. }
  1008. /* Handle IEEE 802.1Qaz GET commands. */
  1009. static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
  1010. {
  1011. struct nlattr *ieee, *app;
  1012. struct dcb_app_type *itr;
  1013. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1014. int dcbx;
  1015. int err = -EMSGSIZE;
  1016. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  1017. goto nla_put_failure;
  1018. ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
  1019. if (!ieee)
  1020. goto nla_put_failure;
  1021. if (ops->ieee_getets) {
  1022. struct ieee_ets ets;
  1023. err = ops->ieee_getets(netdev, &ets);
  1024. if (!err &&
  1025. nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
  1026. goto nla_put_failure;
  1027. }
  1028. if (ops->ieee_getmaxrate) {
  1029. struct ieee_maxrate maxrate;
  1030. err = ops->ieee_getmaxrate(netdev, &maxrate);
  1031. if (!err) {
  1032. err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
  1033. sizeof(maxrate), &maxrate);
  1034. if (err)
  1035. goto nla_put_failure;
  1036. }
  1037. }
  1038. if (ops->ieee_getpfc) {
  1039. struct ieee_pfc pfc;
  1040. err = ops->ieee_getpfc(netdev, &pfc);
  1041. if (!err &&
  1042. nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
  1043. goto nla_put_failure;
  1044. }
  1045. app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
  1046. if (!app)
  1047. goto nla_put_failure;
  1048. spin_lock(&dcb_lock);
  1049. list_for_each_entry(itr, &dcb_app_list, list) {
  1050. if (itr->ifindex == netdev->ifindex) {
  1051. err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
  1052. &itr->app);
  1053. if (err) {
  1054. spin_unlock(&dcb_lock);
  1055. goto nla_put_failure;
  1056. }
  1057. }
  1058. }
  1059. if (netdev->dcbnl_ops->getdcbx)
  1060. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1061. else
  1062. dcbx = -EOPNOTSUPP;
  1063. spin_unlock(&dcb_lock);
  1064. nla_nest_end(skb, app);
  1065. /* get peer info if available */
  1066. if (ops->ieee_peer_getets) {
  1067. struct ieee_ets ets;
  1068. err = ops->ieee_peer_getets(netdev, &ets);
  1069. if (!err &&
  1070. nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
  1071. goto nla_put_failure;
  1072. }
  1073. if (ops->ieee_peer_getpfc) {
  1074. struct ieee_pfc pfc;
  1075. err = ops->ieee_peer_getpfc(netdev, &pfc);
  1076. if (!err &&
  1077. nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
  1078. goto nla_put_failure;
  1079. }
  1080. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1081. err = dcbnl_build_peer_app(netdev, skb,
  1082. DCB_ATTR_IEEE_PEER_APP,
  1083. DCB_ATTR_IEEE_APP_UNSPEC,
  1084. DCB_ATTR_IEEE_APP);
  1085. if (err)
  1086. goto nla_put_failure;
  1087. }
  1088. nla_nest_end(skb, ieee);
  1089. if (dcbx >= 0) {
  1090. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1091. if (err)
  1092. goto nla_put_failure;
  1093. }
  1094. return 0;
  1095. nla_put_failure:
  1096. return err;
  1097. }
  1098. static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
  1099. int dir)
  1100. {
  1101. u8 pgid, up_map, prio, tc_pct;
  1102. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1103. int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
  1104. struct nlattr *pg = nla_nest_start(skb, i);
  1105. if (!pg)
  1106. goto nla_put_failure;
  1107. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  1108. struct nlattr *tc_nest = nla_nest_start(skb, i);
  1109. if (!tc_nest)
  1110. goto nla_put_failure;
  1111. pgid = DCB_ATTR_VALUE_UNDEFINED;
  1112. prio = DCB_ATTR_VALUE_UNDEFINED;
  1113. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  1114. up_map = DCB_ATTR_VALUE_UNDEFINED;
  1115. if (!dir)
  1116. ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
  1117. &prio, &pgid, &tc_pct, &up_map);
  1118. else
  1119. ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
  1120. &prio, &pgid, &tc_pct, &up_map);
  1121. if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
  1122. nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
  1123. nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
  1124. nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
  1125. goto nla_put_failure;
  1126. nla_nest_end(skb, tc_nest);
  1127. }
  1128. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  1129. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  1130. if (!dir)
  1131. ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
  1132. &tc_pct);
  1133. else
  1134. ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
  1135. &tc_pct);
  1136. if (nla_put_u8(skb, i, tc_pct))
  1137. goto nla_put_failure;
  1138. }
  1139. nla_nest_end(skb, pg);
  1140. return 0;
  1141. nla_put_failure:
  1142. return -EMSGSIZE;
  1143. }
  1144. static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
  1145. {
  1146. struct nlattr *cee, *app;
  1147. struct dcb_app_type *itr;
  1148. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1149. int dcbx, i, err = -EMSGSIZE;
  1150. u8 value;
  1151. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  1152. goto nla_put_failure;
  1153. cee = nla_nest_start(skb, DCB_ATTR_CEE);
  1154. if (!cee)
  1155. goto nla_put_failure;
  1156. /* local pg */
  1157. if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
  1158. err = dcbnl_cee_pg_fill(skb, netdev, 1);
  1159. if (err)
  1160. goto nla_put_failure;
  1161. }
  1162. if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
  1163. err = dcbnl_cee_pg_fill(skb, netdev, 0);
  1164. if (err)
  1165. goto nla_put_failure;
  1166. }
  1167. /* local pfc */
  1168. if (ops->getpfccfg) {
  1169. struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
  1170. if (!pfc_nest)
  1171. goto nla_put_failure;
  1172. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  1173. ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
  1174. if (nla_put_u8(skb, i, value))
  1175. goto nla_put_failure;
  1176. }
  1177. nla_nest_end(skb, pfc_nest);
  1178. }
  1179. /* local app */
  1180. spin_lock(&dcb_lock);
  1181. app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
  1182. if (!app)
  1183. goto dcb_unlock;
  1184. list_for_each_entry(itr, &dcb_app_list, list) {
  1185. if (itr->ifindex == netdev->ifindex) {
  1186. struct nlattr *app_nest = nla_nest_start(skb,
  1187. DCB_ATTR_APP);
  1188. if (!app_nest)
  1189. goto dcb_unlock;
  1190. err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
  1191. itr->app.selector);
  1192. if (err)
  1193. goto dcb_unlock;
  1194. err = nla_put_u16(skb, DCB_APP_ATTR_ID,
  1195. itr->app.protocol);
  1196. if (err)
  1197. goto dcb_unlock;
  1198. err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
  1199. itr->app.priority);
  1200. if (err)
  1201. goto dcb_unlock;
  1202. nla_nest_end(skb, app_nest);
  1203. }
  1204. }
  1205. nla_nest_end(skb, app);
  1206. if (netdev->dcbnl_ops->getdcbx)
  1207. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1208. else
  1209. dcbx = -EOPNOTSUPP;
  1210. spin_unlock(&dcb_lock);
  1211. /* features flags */
  1212. if (ops->getfeatcfg) {
  1213. struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
  1214. if (!feat)
  1215. goto nla_put_failure;
  1216. for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
  1217. i++)
  1218. if (!ops->getfeatcfg(netdev, i, &value) &&
  1219. nla_put_u8(skb, i, value))
  1220. goto nla_put_failure;
  1221. nla_nest_end(skb, feat);
  1222. }
  1223. /* peer info if available */
  1224. if (ops->cee_peer_getpg) {
  1225. struct cee_pg pg;
  1226. err = ops->cee_peer_getpg(netdev, &pg);
  1227. if (!err &&
  1228. nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
  1229. goto nla_put_failure;
  1230. }
  1231. if (ops->cee_peer_getpfc) {
  1232. struct cee_pfc pfc;
  1233. err = ops->cee_peer_getpfc(netdev, &pfc);
  1234. if (!err &&
  1235. nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
  1236. goto nla_put_failure;
  1237. }
  1238. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1239. err = dcbnl_build_peer_app(netdev, skb,
  1240. DCB_ATTR_CEE_PEER_APP_TABLE,
  1241. DCB_ATTR_CEE_PEER_APP_INFO,
  1242. DCB_ATTR_CEE_PEER_APP);
  1243. if (err)
  1244. goto nla_put_failure;
  1245. }
  1246. nla_nest_end(skb, cee);
  1247. /* DCBX state */
  1248. if (dcbx >= 0) {
  1249. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1250. if (err)
  1251. goto nla_put_failure;
  1252. }
  1253. return 0;
  1254. dcb_unlock:
  1255. spin_unlock(&dcb_lock);
  1256. nla_put_failure:
  1257. return err;
  1258. }
  1259. static int dcbnl_notify(struct net_device *dev, int event, int cmd,
  1260. u32 seq, u32 pid, int dcbx_ver)
  1261. {
  1262. struct net *net = dev_net(dev);
  1263. struct sk_buff *skb;
  1264. struct nlmsghdr *nlh;
  1265. struct dcbmsg *dcb;
  1266. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1267. int err;
  1268. if (!ops)
  1269. return -EOPNOTSUPP;
  1270. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1271. if (!skb)
  1272. return -ENOBUFS;
  1273. nlh = nlmsg_put(skb, pid, 0, event, sizeof(*dcb), 0);
  1274. if (nlh == NULL) {
  1275. nlmsg_free(skb);
  1276. return -EMSGSIZE;
  1277. }
  1278. dcb = NLMSG_DATA(nlh);
  1279. dcb->dcb_family = AF_UNSPEC;
  1280. dcb->cmd = cmd;
  1281. if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
  1282. err = dcbnl_ieee_fill(skb, dev);
  1283. else
  1284. err = dcbnl_cee_fill(skb, dev);
  1285. if (err < 0) {
  1286. /* Report error to broadcast listeners */
  1287. nlmsg_cancel(skb, nlh);
  1288. kfree_skb(skb);
  1289. rtnl_set_sk_err(net, RTNLGRP_DCB, err);
  1290. } else {
  1291. /* End nlmsg and notify broadcast listeners */
  1292. nlmsg_end(skb, nlh);
  1293. rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
  1294. }
  1295. return err;
  1296. }
  1297. int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
  1298. u32 seq, u32 pid)
  1299. {
  1300. return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_IEEE);
  1301. }
  1302. EXPORT_SYMBOL(dcbnl_ieee_notify);
  1303. int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
  1304. u32 seq, u32 pid)
  1305. {
  1306. return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_CEE);
  1307. }
  1308. EXPORT_SYMBOL(dcbnl_cee_notify);
  1309. /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
  1310. * be completed the entire msg is aborted and error value is returned.
  1311. * No attempt is made to reconcile the case where only part of the
  1312. * cmd can be completed.
  1313. */
  1314. static int dcbnl_ieee_set(struct net_device *netdev, struct nlattr **tb,
  1315. u32 pid, u32 seq, u16 flags)
  1316. {
  1317. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1318. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1319. int err = -EOPNOTSUPP;
  1320. if (!ops)
  1321. return err;
  1322. if (!tb[DCB_ATTR_IEEE])
  1323. return -EINVAL;
  1324. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1325. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1326. if (err)
  1327. return err;
  1328. if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  1329. struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  1330. err = ops->ieee_setets(netdev, ets);
  1331. if (err)
  1332. goto err;
  1333. }
  1334. if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
  1335. struct ieee_maxrate *maxrate =
  1336. nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
  1337. err = ops->ieee_setmaxrate(netdev, maxrate);
  1338. if (err)
  1339. goto err;
  1340. }
  1341. if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
  1342. struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  1343. err = ops->ieee_setpfc(netdev, pfc);
  1344. if (err)
  1345. goto err;
  1346. }
  1347. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1348. struct nlattr *attr;
  1349. int rem;
  1350. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1351. struct dcb_app *app_data;
  1352. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1353. continue;
  1354. app_data = nla_data(attr);
  1355. if (ops->ieee_setapp)
  1356. err = ops->ieee_setapp(netdev, app_data);
  1357. else
  1358. err = dcb_ieee_setapp(netdev, app_data);
  1359. if (err)
  1360. goto err;
  1361. }
  1362. }
  1363. err:
  1364. dcbnl_reply(err, RTM_SETDCB, DCB_CMD_IEEE_SET, DCB_ATTR_IEEE,
  1365. pid, seq, flags);
  1366. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
  1367. return err;
  1368. }
  1369. static int dcbnl_ieee_get(struct net_device *netdev, struct nlattr **tb,
  1370. u32 pid, u32 seq, u16 flags)
  1371. {
  1372. struct net *net = dev_net(netdev);
  1373. struct sk_buff *skb;
  1374. struct nlmsghdr *nlh;
  1375. struct dcbmsg *dcb;
  1376. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1377. int err;
  1378. if (!ops)
  1379. return -EOPNOTSUPP;
  1380. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1381. if (!skb)
  1382. return -ENOBUFS;
  1383. nlh = nlmsg_put(skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  1384. if (nlh == NULL) {
  1385. nlmsg_free(skb);
  1386. return -EMSGSIZE;
  1387. }
  1388. dcb = NLMSG_DATA(nlh);
  1389. dcb->dcb_family = AF_UNSPEC;
  1390. dcb->cmd = DCB_CMD_IEEE_GET;
  1391. err = dcbnl_ieee_fill(skb, netdev);
  1392. if (err < 0) {
  1393. nlmsg_cancel(skb, nlh);
  1394. kfree_skb(skb);
  1395. } else {
  1396. nlmsg_end(skb, nlh);
  1397. err = rtnl_unicast(skb, net, pid);
  1398. }
  1399. return err;
  1400. }
  1401. static int dcbnl_ieee_del(struct net_device *netdev, struct nlattr **tb,
  1402. u32 pid, u32 seq, u16 flags)
  1403. {
  1404. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1405. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1406. int err = -EOPNOTSUPP;
  1407. if (!ops)
  1408. return -EOPNOTSUPP;
  1409. if (!tb[DCB_ATTR_IEEE])
  1410. return -EINVAL;
  1411. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1412. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1413. if (err)
  1414. return err;
  1415. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1416. struct nlattr *attr;
  1417. int rem;
  1418. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1419. struct dcb_app *app_data;
  1420. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1421. continue;
  1422. app_data = nla_data(attr);
  1423. if (ops->ieee_delapp)
  1424. err = ops->ieee_delapp(netdev, app_data);
  1425. else
  1426. err = dcb_ieee_delapp(netdev, app_data);
  1427. if (err)
  1428. goto err;
  1429. }
  1430. }
  1431. err:
  1432. dcbnl_reply(err, RTM_SETDCB, DCB_CMD_IEEE_DEL, DCB_ATTR_IEEE,
  1433. pid, seq, flags);
  1434. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
  1435. return err;
  1436. }
  1437. /* DCBX configuration */
  1438. static int dcbnl_getdcbx(struct net_device *netdev, struct nlattr **tb,
  1439. u32 pid, u32 seq, u16 flags)
  1440. {
  1441. int ret;
  1442. if (!netdev->dcbnl_ops->getdcbx)
  1443. return -EOPNOTSUPP;
  1444. ret = dcbnl_reply(netdev->dcbnl_ops->getdcbx(netdev), RTM_GETDCB,
  1445. DCB_CMD_GDCBX, DCB_ATTR_DCBX, pid, seq, flags);
  1446. return ret;
  1447. }
  1448. static int dcbnl_setdcbx(struct net_device *netdev, struct nlattr **tb,
  1449. u32 pid, u32 seq, u16 flags)
  1450. {
  1451. int ret;
  1452. u8 value;
  1453. if (!netdev->dcbnl_ops->setdcbx)
  1454. return -EOPNOTSUPP;
  1455. if (!tb[DCB_ATTR_DCBX])
  1456. return -EINVAL;
  1457. value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  1458. ret = dcbnl_reply(netdev->dcbnl_ops->setdcbx(netdev, value),
  1459. RTM_SETDCB, DCB_CMD_SDCBX, DCB_ATTR_DCBX,
  1460. pid, seq, flags);
  1461. return ret;
  1462. }
  1463. static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlattr **tb,
  1464. u32 pid, u32 seq, u16 flags)
  1465. {
  1466. struct sk_buff *dcbnl_skb;
  1467. struct nlmsghdr *nlh;
  1468. struct dcbmsg *dcb;
  1469. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  1470. u8 value;
  1471. int ret, i;
  1472. int getall = 0;
  1473. if (!netdev->dcbnl_ops->getfeatcfg)
  1474. return -EOPNOTSUPP;
  1475. if (!tb[DCB_ATTR_FEATCFG])
  1476. return -EINVAL;
  1477. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1478. dcbnl_featcfg_nest);
  1479. if (ret)
  1480. goto err_out;
  1481. dcbnl_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1482. if (!dcbnl_skb) {
  1483. ret = -ENOBUFS;
  1484. goto err_out;
  1485. }
  1486. nlh = NLMSG_NEW(dcbnl_skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  1487. dcb = NLMSG_DATA(nlh);
  1488. dcb->dcb_family = AF_UNSPEC;
  1489. dcb->cmd = DCB_CMD_GFEATCFG;
  1490. nest = nla_nest_start(dcbnl_skb, DCB_ATTR_FEATCFG);
  1491. if (!nest) {
  1492. ret = -EMSGSIZE;
  1493. goto nla_put_failure;
  1494. }
  1495. if (data[DCB_FEATCFG_ATTR_ALL])
  1496. getall = 1;
  1497. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1498. if (!getall && !data[i])
  1499. continue;
  1500. ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
  1501. if (!ret)
  1502. ret = nla_put_u8(dcbnl_skb, i, value);
  1503. if (ret) {
  1504. nla_nest_cancel(dcbnl_skb, nest);
  1505. goto nla_put_failure;
  1506. }
  1507. }
  1508. nla_nest_end(dcbnl_skb, nest);
  1509. nlmsg_end(dcbnl_skb, nlh);
  1510. return rtnl_unicast(dcbnl_skb, &init_net, pid);
  1511. nla_put_failure:
  1512. nlmsg_cancel(dcbnl_skb, nlh);
  1513. nlmsg_failure:
  1514. kfree_skb(dcbnl_skb);
  1515. err_out:
  1516. return ret;
  1517. }
  1518. static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlattr **tb,
  1519. u32 pid, u32 seq, u16 flags)
  1520. {
  1521. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
  1522. int ret, i;
  1523. u8 value;
  1524. if (!netdev->dcbnl_ops->setfeatcfg)
  1525. return -ENOTSUPP;
  1526. if (!tb[DCB_ATTR_FEATCFG])
  1527. return -EINVAL;
  1528. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1529. dcbnl_featcfg_nest);
  1530. if (ret)
  1531. goto err;
  1532. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1533. if (data[i] == NULL)
  1534. continue;
  1535. value = nla_get_u8(data[i]);
  1536. ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  1537. if (ret)
  1538. goto err;
  1539. }
  1540. err:
  1541. dcbnl_reply(ret, RTM_SETDCB, DCB_CMD_SFEATCFG, DCB_ATTR_FEATCFG,
  1542. pid, seq, flags);
  1543. return ret;
  1544. }
  1545. /* Handle CEE DCBX GET commands. */
  1546. static int dcbnl_cee_get(struct net_device *netdev, struct nlattr **tb,
  1547. u32 pid, u32 seq, u16 flags)
  1548. {
  1549. struct net *net = dev_net(netdev);
  1550. struct sk_buff *skb;
  1551. struct nlmsghdr *nlh;
  1552. struct dcbmsg *dcb;
  1553. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1554. int err;
  1555. if (!ops)
  1556. return -EOPNOTSUPP;
  1557. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1558. if (!skb)
  1559. return -ENOBUFS;
  1560. nlh = nlmsg_put(skb, pid, seq, RTM_GETDCB, sizeof(*dcb), flags);
  1561. if (nlh == NULL) {
  1562. nlmsg_free(skb);
  1563. return -EMSGSIZE;
  1564. }
  1565. dcb = NLMSG_DATA(nlh);
  1566. dcb->dcb_family = AF_UNSPEC;
  1567. dcb->cmd = DCB_CMD_CEE_GET;
  1568. err = dcbnl_cee_fill(skb, netdev);
  1569. if (err < 0) {
  1570. nlmsg_cancel(skb, nlh);
  1571. nlmsg_free(skb);
  1572. } else {
  1573. nlmsg_end(skb, nlh);
  1574. err = rtnl_unicast(skb, net, pid);
  1575. }
  1576. return err;
  1577. }
  1578. static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1579. {
  1580. struct net *net = sock_net(skb->sk);
  1581. struct net_device *netdev;
  1582. struct dcbmsg *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
  1583. struct nlattr *tb[DCB_ATTR_MAX + 1];
  1584. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  1585. int ret = -EINVAL;
  1586. if (!net_eq(net, &init_net))
  1587. return -EINVAL;
  1588. ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  1589. dcbnl_rtnl_policy);
  1590. if (ret < 0)
  1591. return ret;
  1592. if (!tb[DCB_ATTR_IFNAME])
  1593. return -EINVAL;
  1594. netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
  1595. if (!netdev)
  1596. return -EINVAL;
  1597. if (!netdev->dcbnl_ops)
  1598. goto errout;
  1599. switch (dcb->cmd) {
  1600. case DCB_CMD_GSTATE:
  1601. ret = dcbnl_getstate(netdev, tb, pid, nlh->nlmsg_seq,
  1602. nlh->nlmsg_flags);
  1603. goto out;
  1604. case DCB_CMD_PFC_GCFG:
  1605. ret = dcbnl_getpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
  1606. nlh->nlmsg_flags);
  1607. goto out;
  1608. case DCB_CMD_GPERM_HWADDR:
  1609. ret = dcbnl_getperm_hwaddr(netdev, tb, pid, nlh->nlmsg_seq,
  1610. nlh->nlmsg_flags);
  1611. goto out;
  1612. case DCB_CMD_PGTX_GCFG:
  1613. ret = dcbnl_pgtx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1614. nlh->nlmsg_flags);
  1615. goto out;
  1616. case DCB_CMD_PGRX_GCFG:
  1617. ret = dcbnl_pgrx_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1618. nlh->nlmsg_flags);
  1619. goto out;
  1620. case DCB_CMD_BCN_GCFG:
  1621. ret = dcbnl_bcn_getcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1622. nlh->nlmsg_flags);
  1623. goto out;
  1624. case DCB_CMD_SSTATE:
  1625. ret = dcbnl_setstate(netdev, tb, pid, nlh->nlmsg_seq,
  1626. nlh->nlmsg_flags);
  1627. goto out;
  1628. case DCB_CMD_PFC_SCFG:
  1629. ret = dcbnl_setpfccfg(netdev, tb, pid, nlh->nlmsg_seq,
  1630. nlh->nlmsg_flags);
  1631. goto out;
  1632. case DCB_CMD_SET_ALL:
  1633. ret = dcbnl_setall(netdev, tb, pid, nlh->nlmsg_seq,
  1634. nlh->nlmsg_flags);
  1635. goto out;
  1636. case DCB_CMD_PGTX_SCFG:
  1637. ret = dcbnl_pgtx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1638. nlh->nlmsg_flags);
  1639. goto out;
  1640. case DCB_CMD_PGRX_SCFG:
  1641. ret = dcbnl_pgrx_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1642. nlh->nlmsg_flags);
  1643. goto out;
  1644. case DCB_CMD_GCAP:
  1645. ret = dcbnl_getcap(netdev, tb, pid, nlh->nlmsg_seq,
  1646. nlh->nlmsg_flags);
  1647. goto out;
  1648. case DCB_CMD_GNUMTCS:
  1649. ret = dcbnl_getnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
  1650. nlh->nlmsg_flags);
  1651. goto out;
  1652. case DCB_CMD_SNUMTCS:
  1653. ret = dcbnl_setnumtcs(netdev, tb, pid, nlh->nlmsg_seq,
  1654. nlh->nlmsg_flags);
  1655. goto out;
  1656. case DCB_CMD_PFC_GSTATE:
  1657. ret = dcbnl_getpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
  1658. nlh->nlmsg_flags);
  1659. goto out;
  1660. case DCB_CMD_PFC_SSTATE:
  1661. ret = dcbnl_setpfcstate(netdev, tb, pid, nlh->nlmsg_seq,
  1662. nlh->nlmsg_flags);
  1663. goto out;
  1664. case DCB_CMD_BCN_SCFG:
  1665. ret = dcbnl_bcn_setcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1666. nlh->nlmsg_flags);
  1667. goto out;
  1668. case DCB_CMD_GAPP:
  1669. ret = dcbnl_getapp(netdev, tb, pid, nlh->nlmsg_seq,
  1670. nlh->nlmsg_flags);
  1671. goto out;
  1672. case DCB_CMD_SAPP:
  1673. ret = dcbnl_setapp(netdev, tb, pid, nlh->nlmsg_seq,
  1674. nlh->nlmsg_flags);
  1675. goto out;
  1676. case DCB_CMD_IEEE_SET:
  1677. ret = dcbnl_ieee_set(netdev, tb, pid, nlh->nlmsg_seq,
  1678. nlh->nlmsg_flags);
  1679. goto out;
  1680. case DCB_CMD_IEEE_GET:
  1681. ret = dcbnl_ieee_get(netdev, tb, pid, nlh->nlmsg_seq,
  1682. nlh->nlmsg_flags);
  1683. goto out;
  1684. case DCB_CMD_IEEE_DEL:
  1685. ret = dcbnl_ieee_del(netdev, tb, pid, nlh->nlmsg_seq,
  1686. nlh->nlmsg_flags);
  1687. goto out;
  1688. case DCB_CMD_GDCBX:
  1689. ret = dcbnl_getdcbx(netdev, tb, pid, nlh->nlmsg_seq,
  1690. nlh->nlmsg_flags);
  1691. goto out;
  1692. case DCB_CMD_SDCBX:
  1693. ret = dcbnl_setdcbx(netdev, tb, pid, nlh->nlmsg_seq,
  1694. nlh->nlmsg_flags);
  1695. goto out;
  1696. case DCB_CMD_GFEATCFG:
  1697. ret = dcbnl_getfeatcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1698. nlh->nlmsg_flags);
  1699. goto out;
  1700. case DCB_CMD_SFEATCFG:
  1701. ret = dcbnl_setfeatcfg(netdev, tb, pid, nlh->nlmsg_seq,
  1702. nlh->nlmsg_flags);
  1703. goto out;
  1704. case DCB_CMD_CEE_GET:
  1705. ret = dcbnl_cee_get(netdev, tb, pid, nlh->nlmsg_seq,
  1706. nlh->nlmsg_flags);
  1707. goto out;
  1708. default:
  1709. goto errout;
  1710. }
  1711. errout:
  1712. ret = -EINVAL;
  1713. out:
  1714. dev_put(netdev);
  1715. return ret;
  1716. }
  1717. /**
  1718. * dcb_getapp - retrieve the DCBX application user priority
  1719. *
  1720. * On success returns a non-zero 802.1p user priority bitmap
  1721. * otherwise returns 0 as the invalid user priority bitmap to
  1722. * indicate an error.
  1723. */
  1724. u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  1725. {
  1726. struct dcb_app_type *itr;
  1727. u8 prio = 0;
  1728. spin_lock(&dcb_lock);
  1729. list_for_each_entry(itr, &dcb_app_list, list) {
  1730. if (itr->app.selector == app->selector &&
  1731. itr->app.protocol == app->protocol &&
  1732. itr->ifindex == dev->ifindex) {
  1733. prio = itr->app.priority;
  1734. break;
  1735. }
  1736. }
  1737. spin_unlock(&dcb_lock);
  1738. return prio;
  1739. }
  1740. EXPORT_SYMBOL(dcb_getapp);
  1741. /**
  1742. * dcb_setapp - add CEE dcb application data to app list
  1743. *
  1744. * Priority 0 is an invalid priority in CEE spec. This routine
  1745. * removes applications from the app list if the priority is
  1746. * set to zero.
  1747. */
  1748. int dcb_setapp(struct net_device *dev, struct dcb_app *new)
  1749. {
  1750. struct dcb_app_type *itr;
  1751. struct dcb_app_type event;
  1752. event.ifindex = dev->ifindex;
  1753. memcpy(&event.app, new, sizeof(event.app));
  1754. if (dev->dcbnl_ops->getdcbx)
  1755. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1756. spin_lock(&dcb_lock);
  1757. /* Search for existing match and replace */
  1758. list_for_each_entry(itr, &dcb_app_list, list) {
  1759. if (itr->app.selector == new->selector &&
  1760. itr->app.protocol == new->protocol &&
  1761. itr->ifindex == dev->ifindex) {
  1762. if (new->priority)
  1763. itr->app.priority = new->priority;
  1764. else {
  1765. list_del(&itr->list);
  1766. kfree(itr);
  1767. }
  1768. goto out;
  1769. }
  1770. }
  1771. /* App type does not exist add new application type */
  1772. if (new->priority) {
  1773. struct dcb_app_type *entry;
  1774. entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
  1775. if (!entry) {
  1776. spin_unlock(&dcb_lock);
  1777. return -ENOMEM;
  1778. }
  1779. memcpy(&entry->app, new, sizeof(*new));
  1780. entry->ifindex = dev->ifindex;
  1781. list_add(&entry->list, &dcb_app_list);
  1782. }
  1783. out:
  1784. spin_unlock(&dcb_lock);
  1785. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1786. return 0;
  1787. }
  1788. EXPORT_SYMBOL(dcb_setapp);
  1789. /**
  1790. * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
  1791. *
  1792. * Helper routine which on success returns a non-zero 802.1Qaz user
  1793. * priority bitmap otherwise returns 0 to indicate the dcb_app was
  1794. * not found in APP list.
  1795. */
  1796. u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
  1797. {
  1798. struct dcb_app_type *itr;
  1799. u8 prio = 0;
  1800. spin_lock(&dcb_lock);
  1801. list_for_each_entry(itr, &dcb_app_list, list) {
  1802. if (itr->app.selector == app->selector &&
  1803. itr->app.protocol == app->protocol &&
  1804. itr->ifindex == dev->ifindex) {
  1805. prio |= 1 << itr->app.priority;
  1806. }
  1807. }
  1808. spin_unlock(&dcb_lock);
  1809. return prio;
  1810. }
  1811. EXPORT_SYMBOL(dcb_ieee_getapp_mask);
  1812. /**
  1813. * dcb_ieee_setapp - add IEEE dcb application data to app list
  1814. *
  1815. * This adds Application data to the list. Multiple application
  1816. * entries may exists for the same selector and protocol as long
  1817. * as the priorities are different.
  1818. */
  1819. int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
  1820. {
  1821. struct dcb_app_type *itr, *entry;
  1822. struct dcb_app_type event;
  1823. int err = 0;
  1824. event.ifindex = dev->ifindex;
  1825. memcpy(&event.app, new, sizeof(event.app));
  1826. if (dev->dcbnl_ops->getdcbx)
  1827. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1828. spin_lock(&dcb_lock);
  1829. /* Search for existing match and abort if found */
  1830. list_for_each_entry(itr, &dcb_app_list, list) {
  1831. if (itr->app.selector == new->selector &&
  1832. itr->app.protocol == new->protocol &&
  1833. itr->app.priority == new->priority &&
  1834. itr->ifindex == dev->ifindex) {
  1835. err = -EEXIST;
  1836. goto out;
  1837. }
  1838. }
  1839. /* App entry does not exist add new entry */
  1840. entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
  1841. if (!entry) {
  1842. err = -ENOMEM;
  1843. goto out;
  1844. }
  1845. memcpy(&entry->app, new, sizeof(*new));
  1846. entry->ifindex = dev->ifindex;
  1847. list_add(&entry->list, &dcb_app_list);
  1848. out:
  1849. spin_unlock(&dcb_lock);
  1850. if (!err)
  1851. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1852. return err;
  1853. }
  1854. EXPORT_SYMBOL(dcb_ieee_setapp);
  1855. /**
  1856. * dcb_ieee_delapp - delete IEEE dcb application data from list
  1857. *
  1858. * This removes a matching APP data from the APP list
  1859. */
  1860. int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
  1861. {
  1862. struct dcb_app_type *itr;
  1863. struct dcb_app_type event;
  1864. int err = -ENOENT;
  1865. event.ifindex = dev->ifindex;
  1866. memcpy(&event.app, del, sizeof(event.app));
  1867. if (dev->dcbnl_ops->getdcbx)
  1868. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1869. spin_lock(&dcb_lock);
  1870. /* Search for existing match and remove it. */
  1871. list_for_each_entry(itr, &dcb_app_list, list) {
  1872. if (itr->app.selector == del->selector &&
  1873. itr->app.protocol == del->protocol &&
  1874. itr->app.priority == del->priority &&
  1875. itr->ifindex == dev->ifindex) {
  1876. list_del(&itr->list);
  1877. kfree(itr);
  1878. err = 0;
  1879. goto out;
  1880. }
  1881. }
  1882. out:
  1883. spin_unlock(&dcb_lock);
  1884. if (!err)
  1885. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1886. return err;
  1887. }
  1888. EXPORT_SYMBOL(dcb_ieee_delapp);
  1889. static void dcb_flushapp(void)
  1890. {
  1891. struct dcb_app_type *app;
  1892. struct dcb_app_type *tmp;
  1893. spin_lock(&dcb_lock);
  1894. list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
  1895. list_del(&app->list);
  1896. kfree(app);
  1897. }
  1898. spin_unlock(&dcb_lock);
  1899. }
  1900. static int __init dcbnl_init(void)
  1901. {
  1902. INIT_LIST_HEAD(&dcb_app_list);
  1903. rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, NULL);
  1904. rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, NULL);
  1905. return 0;
  1906. }
  1907. module_init(dcbnl_init);
  1908. static void __exit dcbnl_exit(void)
  1909. {
  1910. rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
  1911. rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
  1912. dcb_flushapp();
  1913. }
  1914. module_exit(dcbnl_exit);