dcbnl.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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. static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
  183. u32 flags, struct nlmsghdr **nlhp)
  184. {
  185. struct sk_buff *skb;
  186. struct dcbmsg *dcb;
  187. struct nlmsghdr *nlh;
  188. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  189. if (!skb)
  190. return NULL;
  191. nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
  192. if (!nlh) {
  193. /* header should always fit, allocation must be buggy */
  194. BUG();
  195. }
  196. dcb = nlmsg_data(nlh);
  197. dcb->dcb_family = AF_UNSPEC;
  198. dcb->cmd = cmd;
  199. dcb->dcb_pad = 0;
  200. if (nlhp)
  201. *nlhp = nlh;
  202. return skb;
  203. }
  204. static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
  205. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  206. {
  207. /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
  208. if (!netdev->dcbnl_ops->getstate)
  209. return -EINVAL;
  210. return nla_put_u8(skb, DCB_ATTR_STATE,
  211. netdev->dcbnl_ops->getstate(netdev));
  212. }
  213. static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
  214. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  215. {
  216. struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
  217. u8 value;
  218. int ret = -EINVAL;
  219. int i;
  220. int getall = 0;
  221. if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->getpfccfg)
  222. return ret;
  223. ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  224. tb[DCB_ATTR_PFC_CFG],
  225. dcbnl_pfc_up_nest);
  226. if (ret)
  227. goto err;
  228. nest = nla_nest_start(skb, DCB_ATTR_PFC_CFG);
  229. if (!nest)
  230. goto err;
  231. if (data[DCB_PFC_UP_ATTR_ALL])
  232. getall = 1;
  233. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  234. if (!getall && !data[i])
  235. continue;
  236. netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
  237. &value);
  238. ret = nla_put_u8(skb, i, value);
  239. if (ret) {
  240. nla_nest_cancel(skb, nest);
  241. goto err;
  242. }
  243. }
  244. nla_nest_end(skb, nest);
  245. return 0;
  246. err:
  247. return -EINVAL;
  248. }
  249. static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
  250. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  251. {
  252. u8 perm_addr[MAX_ADDR_LEN];
  253. if (!netdev->dcbnl_ops->getpermhwaddr)
  254. return -EINVAL;
  255. netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
  256. return nla_put(skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr), perm_addr);
  257. }
  258. static int dcbnl_getcap(struct net_device *netdev, struct nlmsghdr *nlh,
  259. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  260. {
  261. struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
  262. u8 value;
  263. int ret = -EINVAL;
  264. int i;
  265. int getall = 0;
  266. if (!tb[DCB_ATTR_CAP] || !netdev->dcbnl_ops->getcap)
  267. return ret;
  268. ret = nla_parse_nested(data, DCB_CAP_ATTR_MAX, tb[DCB_ATTR_CAP],
  269. dcbnl_cap_nest);
  270. if (ret)
  271. goto err_out;
  272. nest = nla_nest_start(skb, DCB_ATTR_CAP);
  273. if (!nest)
  274. goto err_out;
  275. if (data[DCB_CAP_ATTR_ALL])
  276. getall = 1;
  277. for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
  278. if (!getall && !data[i])
  279. continue;
  280. if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
  281. ret = nla_put_u8(skb, i, value);
  282. if (ret) {
  283. nla_nest_cancel(skb, nest);
  284. goto err_out;
  285. }
  286. }
  287. }
  288. nla_nest_end(skb, nest);
  289. return 0;
  290. err_out:
  291. return -EINVAL;
  292. }
  293. static int dcbnl_getnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
  294. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  295. {
  296. struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
  297. u8 value;
  298. int ret = -EINVAL;
  299. int i;
  300. int getall = 0;
  301. if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->getnumtcs)
  302. return ret;
  303. ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  304. dcbnl_numtcs_nest);
  305. if (ret) {
  306. ret = -EINVAL;
  307. goto err_out;
  308. }
  309. nest = nla_nest_start(skb, DCB_ATTR_NUMTCS);
  310. if (!nest) {
  311. ret = -EINVAL;
  312. goto err_out;
  313. }
  314. if (data[DCB_NUMTCS_ATTR_ALL])
  315. getall = 1;
  316. for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  317. if (!getall && !data[i])
  318. continue;
  319. ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
  320. if (!ret) {
  321. ret = nla_put_u8(skb, i, value);
  322. if (ret) {
  323. nla_nest_cancel(skb, nest);
  324. ret = -EINVAL;
  325. goto err_out;
  326. }
  327. } else {
  328. goto err_out;
  329. }
  330. }
  331. nla_nest_end(skb, nest);
  332. return 0;
  333. err_out:
  334. return ret;
  335. }
  336. static int dcbnl_setnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
  337. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  338. {
  339. struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
  340. int ret = -EINVAL;
  341. u8 value;
  342. int i;
  343. if (!tb[DCB_ATTR_NUMTCS] || !netdev->dcbnl_ops->setnumtcs)
  344. return ret;
  345. ret = nla_parse_nested(data, DCB_NUMTCS_ATTR_MAX, tb[DCB_ATTR_NUMTCS],
  346. dcbnl_numtcs_nest);
  347. if (ret)
  348. return -EINVAL;
  349. for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
  350. if (data[i] == NULL)
  351. continue;
  352. value = nla_get_u8(data[i]);
  353. ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
  354. if (ret)
  355. break;
  356. }
  357. return nla_put_u8(skb, DCB_ATTR_NUMTCS, !!ret);
  358. }
  359. static int dcbnl_getpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
  360. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  361. {
  362. if (!netdev->dcbnl_ops->getpfcstate)
  363. return -EINVAL;
  364. return nla_put_u8(skb, DCB_ATTR_PFC_STATE,
  365. netdev->dcbnl_ops->getpfcstate(netdev));
  366. }
  367. static int dcbnl_setpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
  368. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  369. {
  370. u8 value;
  371. if (!tb[DCB_ATTR_PFC_STATE] || !netdev->dcbnl_ops->setpfcstate)
  372. return -EINVAL;
  373. value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
  374. netdev->dcbnl_ops->setpfcstate(netdev, value);
  375. return nla_put_u8(skb, DCB_ATTR_PFC_STATE, 0);
  376. }
  377. static int dcbnl_getapp(struct net_device *netdev, struct nlmsghdr *nlh,
  378. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  379. {
  380. struct nlattr *app_nest;
  381. struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
  382. u16 id;
  383. u8 up, idtype;
  384. int ret = -EINVAL;
  385. if (!tb[DCB_ATTR_APP])
  386. goto out;
  387. ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  388. dcbnl_app_nest);
  389. if (ret)
  390. goto out;
  391. ret = -EINVAL;
  392. /* all must be non-null */
  393. if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  394. (!app_tb[DCB_APP_ATTR_ID]))
  395. goto out;
  396. /* either by eth type or by socket number */
  397. idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  398. if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  399. (idtype != DCB_APP_IDTYPE_PORTNUM))
  400. goto out;
  401. id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
  402. if (netdev->dcbnl_ops->getapp) {
  403. up = netdev->dcbnl_ops->getapp(netdev, idtype, id);
  404. } else {
  405. struct dcb_app app = {
  406. .selector = idtype,
  407. .protocol = id,
  408. };
  409. up = dcb_getapp(netdev, &app);
  410. }
  411. app_nest = nla_nest_start(skb, DCB_ATTR_APP);
  412. if (!app_nest)
  413. goto out;
  414. ret = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE, idtype);
  415. if (ret)
  416. goto out_cancel;
  417. ret = nla_put_u16(skb, DCB_APP_ATTR_ID, id);
  418. if (ret)
  419. goto out_cancel;
  420. ret = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY, up);
  421. if (ret)
  422. goto out_cancel;
  423. nla_nest_end(skb, app_nest);
  424. goto out;
  425. out_cancel:
  426. nla_nest_cancel(skb, app_nest);
  427. out:
  428. return ret;
  429. }
  430. static int dcbnl_setapp(struct net_device *netdev, struct nlmsghdr *nlh,
  431. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  432. {
  433. int err, ret = -EINVAL;
  434. u16 id;
  435. u8 up, idtype;
  436. struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
  437. if (!tb[DCB_ATTR_APP])
  438. goto out;
  439. ret = nla_parse_nested(app_tb, DCB_APP_ATTR_MAX, tb[DCB_ATTR_APP],
  440. dcbnl_app_nest);
  441. if (ret)
  442. goto out;
  443. ret = -EINVAL;
  444. /* all must be non-null */
  445. if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
  446. (!app_tb[DCB_APP_ATTR_ID]) ||
  447. (!app_tb[DCB_APP_ATTR_PRIORITY]))
  448. goto out;
  449. /* either by eth type or by socket number */
  450. idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
  451. if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
  452. (idtype != DCB_APP_IDTYPE_PORTNUM))
  453. goto out;
  454. id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
  455. up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
  456. if (netdev->dcbnl_ops->setapp) {
  457. err = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
  458. } else {
  459. struct dcb_app app;
  460. app.selector = idtype;
  461. app.protocol = id;
  462. app.priority = up;
  463. err = dcb_setapp(netdev, &app);
  464. }
  465. ret = nla_put_u8(skb, DCB_ATTR_APP, ret);
  466. dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SAPP, seq, 0);
  467. out:
  468. return ret;
  469. }
  470. static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  471. struct nlattr **tb, struct sk_buff *skb, int dir)
  472. {
  473. struct nlattr *pg_nest, *param_nest, *data;
  474. struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  475. struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  476. u8 prio, pgid, tc_pct, up_map;
  477. int ret = -EINVAL;
  478. int getall = 0;
  479. int i;
  480. if (!tb[DCB_ATTR_PG_CFG] ||
  481. !netdev->dcbnl_ops->getpgtccfgtx ||
  482. !netdev->dcbnl_ops->getpgtccfgrx ||
  483. !netdev->dcbnl_ops->getpgbwgcfgtx ||
  484. !netdev->dcbnl_ops->getpgbwgcfgrx)
  485. return ret;
  486. ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  487. tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  488. if (ret)
  489. goto err_out;
  490. pg_nest = nla_nest_start(skb, DCB_ATTR_PG_CFG);
  491. if (!pg_nest)
  492. goto err_out;
  493. if (pg_tb[DCB_PG_ATTR_TC_ALL])
  494. getall = 1;
  495. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  496. if (!getall && !pg_tb[i])
  497. continue;
  498. if (pg_tb[DCB_PG_ATTR_TC_ALL])
  499. data = pg_tb[DCB_PG_ATTR_TC_ALL];
  500. else
  501. data = pg_tb[i];
  502. ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  503. data, dcbnl_tc_param_nest);
  504. if (ret)
  505. goto err_pg;
  506. param_nest = nla_nest_start(skb, i);
  507. if (!param_nest)
  508. goto err_pg;
  509. pgid = DCB_ATTR_VALUE_UNDEFINED;
  510. prio = DCB_ATTR_VALUE_UNDEFINED;
  511. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  512. up_map = DCB_ATTR_VALUE_UNDEFINED;
  513. if (dir) {
  514. /* Rx */
  515. netdev->dcbnl_ops->getpgtccfgrx(netdev,
  516. i - DCB_PG_ATTR_TC_0, &prio,
  517. &pgid, &tc_pct, &up_map);
  518. } else {
  519. /* Tx */
  520. netdev->dcbnl_ops->getpgtccfgtx(netdev,
  521. i - DCB_PG_ATTR_TC_0, &prio,
  522. &pgid, &tc_pct, &up_map);
  523. }
  524. if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
  525. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  526. ret = nla_put_u8(skb,
  527. DCB_TC_ATTR_PARAM_PGID, pgid);
  528. if (ret)
  529. goto err_param;
  530. }
  531. if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
  532. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  533. ret = nla_put_u8(skb,
  534. DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
  535. if (ret)
  536. goto err_param;
  537. }
  538. if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
  539. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  540. ret = nla_put_u8(skb,
  541. DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
  542. if (ret)
  543. goto err_param;
  544. }
  545. if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
  546. param_tb[DCB_TC_ATTR_PARAM_ALL]) {
  547. ret = nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT,
  548. tc_pct);
  549. if (ret)
  550. goto err_param;
  551. }
  552. nla_nest_end(skb, param_nest);
  553. }
  554. if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
  555. getall = 1;
  556. else
  557. getall = 0;
  558. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  559. if (!getall && !pg_tb[i])
  560. continue;
  561. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  562. if (dir) {
  563. /* Rx */
  564. netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
  565. i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  566. } else {
  567. /* Tx */
  568. netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
  569. i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
  570. }
  571. ret = nla_put_u8(skb, i, tc_pct);
  572. if (ret)
  573. goto err_pg;
  574. }
  575. nla_nest_end(skb, pg_nest);
  576. return 0;
  577. err_param:
  578. nla_nest_cancel(skb, param_nest);
  579. err_pg:
  580. nla_nest_cancel(skb, pg_nest);
  581. err_out:
  582. ret = -EINVAL;
  583. return ret;
  584. }
  585. static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  586. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  587. {
  588. return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 0);
  589. }
  590. static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  591. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  592. {
  593. return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 1);
  594. }
  595. static int dcbnl_setstate(struct net_device *netdev, struct nlmsghdr *nlh,
  596. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  597. {
  598. u8 value;
  599. if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->setstate)
  600. return -EINVAL;
  601. value = nla_get_u8(tb[DCB_ATTR_STATE]);
  602. return nla_put_u8(skb, DCB_ATTR_STATE,
  603. netdev->dcbnl_ops->setstate(netdev, value));
  604. }
  605. static int dcbnl_setpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
  606. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  607. {
  608. struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
  609. int i;
  610. int ret = -EINVAL;
  611. u8 value;
  612. if (!tb[DCB_ATTR_PFC_CFG] || !netdev->dcbnl_ops->setpfccfg)
  613. return ret;
  614. ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  615. tb[DCB_ATTR_PFC_CFG],
  616. dcbnl_pfc_up_nest);
  617. if (ret)
  618. goto err;
  619. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  620. if (data[i] == NULL)
  621. continue;
  622. value = nla_get_u8(data[i]);
  623. netdev->dcbnl_ops->setpfccfg(netdev,
  624. data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
  625. }
  626. return nla_put_u8(skb, DCB_ATTR_PFC_CFG, 0);
  627. err:
  628. return ret;
  629. }
  630. static int dcbnl_setall(struct net_device *netdev, struct nlmsghdr *nlh,
  631. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  632. {
  633. int ret = -EINVAL;
  634. if (!tb[DCB_ATTR_SET_ALL] || !netdev->dcbnl_ops->setall)
  635. return ret;
  636. ret = nla_put_u8(skb, DCB_ATTR_SET_ALL,
  637. netdev->dcbnl_ops->setall(netdev));
  638. dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SET_ALL, seq, 0);
  639. return ret;
  640. }
  641. static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  642. u32 seq, struct nlattr **tb, struct sk_buff *skb,
  643. int dir)
  644. {
  645. struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
  646. struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
  647. int ret = -EINVAL;
  648. int i;
  649. u8 pgid;
  650. u8 up_map;
  651. u8 prio;
  652. u8 tc_pct;
  653. if (!tb[DCB_ATTR_PG_CFG] ||
  654. !netdev->dcbnl_ops->setpgtccfgtx ||
  655. !netdev->dcbnl_ops->setpgtccfgrx ||
  656. !netdev->dcbnl_ops->setpgbwgcfgtx ||
  657. !netdev->dcbnl_ops->setpgbwgcfgrx)
  658. return ret;
  659. ret = nla_parse_nested(pg_tb, DCB_PG_ATTR_MAX,
  660. tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest);
  661. if (ret)
  662. goto err;
  663. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  664. if (!pg_tb[i])
  665. continue;
  666. ret = nla_parse_nested(param_tb, DCB_TC_ATTR_PARAM_MAX,
  667. pg_tb[i], dcbnl_tc_param_nest);
  668. if (ret)
  669. goto err;
  670. pgid = DCB_ATTR_VALUE_UNDEFINED;
  671. prio = DCB_ATTR_VALUE_UNDEFINED;
  672. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  673. up_map = DCB_ATTR_VALUE_UNDEFINED;
  674. if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
  675. prio =
  676. nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
  677. if (param_tb[DCB_TC_ATTR_PARAM_PGID])
  678. pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
  679. if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
  680. tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
  681. if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
  682. up_map =
  683. nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
  684. /* dir: Tx = 0, Rx = 1 */
  685. if (dir) {
  686. /* Rx */
  687. netdev->dcbnl_ops->setpgtccfgrx(netdev,
  688. i - DCB_PG_ATTR_TC_0,
  689. prio, pgid, tc_pct, up_map);
  690. } else {
  691. /* Tx */
  692. netdev->dcbnl_ops->setpgtccfgtx(netdev,
  693. i - DCB_PG_ATTR_TC_0,
  694. prio, pgid, tc_pct, up_map);
  695. }
  696. }
  697. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  698. if (!pg_tb[i])
  699. continue;
  700. tc_pct = nla_get_u8(pg_tb[i]);
  701. /* dir: Tx = 0, Rx = 1 */
  702. if (dir) {
  703. /* Rx */
  704. netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
  705. i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  706. } else {
  707. /* Tx */
  708. netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
  709. i - DCB_PG_ATTR_BW_ID_0, tc_pct);
  710. }
  711. }
  712. ret = nla_put_u8(skb, (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG), 0);
  713. err:
  714. return ret;
  715. }
  716. static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  717. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  718. {
  719. return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
  720. }
  721. static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  722. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  723. {
  724. return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
  725. }
  726. static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  727. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  728. {
  729. struct nlattr *bcn_nest;
  730. struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
  731. u8 value_byte;
  732. u32 value_integer;
  733. int ret = -EINVAL;
  734. bool getall = false;
  735. int i;
  736. if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->getbcnrp ||
  737. !netdev->dcbnl_ops->getbcncfg)
  738. return ret;
  739. ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
  740. tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
  741. if (ret)
  742. goto err_out;
  743. bcn_nest = nla_nest_start(skb, DCB_ATTR_BCN);
  744. if (!bcn_nest)
  745. goto err_out;
  746. if (bcn_tb[DCB_BCN_ATTR_ALL])
  747. getall = true;
  748. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  749. if (!getall && !bcn_tb[i])
  750. continue;
  751. netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
  752. &value_byte);
  753. ret = nla_put_u8(skb, i, value_byte);
  754. if (ret)
  755. goto err_bcn;
  756. }
  757. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  758. if (!getall && !bcn_tb[i])
  759. continue;
  760. netdev->dcbnl_ops->getbcncfg(netdev, i,
  761. &value_integer);
  762. ret = nla_put_u32(skb, i, value_integer);
  763. if (ret)
  764. goto err_bcn;
  765. }
  766. nla_nest_end(skb, bcn_nest);
  767. return 0;
  768. err_bcn:
  769. nla_nest_cancel(skb, bcn_nest);
  770. err_out:
  771. ret = -EINVAL;
  772. return ret;
  773. }
  774. static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  775. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  776. {
  777. struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
  778. int i;
  779. int ret = -EINVAL;
  780. u8 value_byte;
  781. u32 value_int;
  782. if (!tb[DCB_ATTR_BCN] || !netdev->dcbnl_ops->setbcncfg ||
  783. !netdev->dcbnl_ops->setbcnrp)
  784. return ret;
  785. ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
  786. tb[DCB_ATTR_BCN],
  787. dcbnl_pfc_up_nest);
  788. if (ret)
  789. goto err;
  790. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  791. if (data[i] == NULL)
  792. continue;
  793. value_byte = nla_get_u8(data[i]);
  794. netdev->dcbnl_ops->setbcnrp(netdev,
  795. data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
  796. }
  797. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  798. if (data[i] == NULL)
  799. continue;
  800. value_int = nla_get_u32(data[i]);
  801. netdev->dcbnl_ops->setbcncfg(netdev,
  802. i, value_int);
  803. }
  804. ret = nla_put_u8(skb, DCB_ATTR_BCN, 0);
  805. err:
  806. return ret;
  807. }
  808. static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
  809. int app_nested_type, int app_info_type,
  810. int app_entry_type)
  811. {
  812. struct dcb_peer_app_info info;
  813. struct dcb_app *table = NULL;
  814. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  815. u16 app_count;
  816. int err;
  817. /**
  818. * retrieve the peer app configuration form the driver. If the driver
  819. * handlers fail exit without doing anything
  820. */
  821. err = ops->peer_getappinfo(netdev, &info, &app_count);
  822. if (!err && app_count) {
  823. table = kmalloc(sizeof(struct dcb_app) * app_count, GFP_KERNEL);
  824. if (!table)
  825. return -ENOMEM;
  826. err = ops->peer_getapptable(netdev, table);
  827. }
  828. if (!err) {
  829. u16 i;
  830. struct nlattr *app;
  831. /**
  832. * build the message, from here on the only possible failure
  833. * is due to the skb size
  834. */
  835. err = -EMSGSIZE;
  836. app = nla_nest_start(skb, app_nested_type);
  837. if (!app)
  838. goto nla_put_failure;
  839. if (app_info_type &&
  840. nla_put(skb, app_info_type, sizeof(info), &info))
  841. goto nla_put_failure;
  842. for (i = 0; i < app_count; i++) {
  843. if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
  844. &table[i]))
  845. goto nla_put_failure;
  846. }
  847. nla_nest_end(skb, app);
  848. }
  849. err = 0;
  850. nla_put_failure:
  851. kfree(table);
  852. return err;
  853. }
  854. /* Handle IEEE 802.1Qaz GET commands. */
  855. static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
  856. {
  857. struct nlattr *ieee, *app;
  858. struct dcb_app_type *itr;
  859. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  860. int dcbx;
  861. int err = -EMSGSIZE;
  862. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  863. goto nla_put_failure;
  864. ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
  865. if (!ieee)
  866. goto nla_put_failure;
  867. if (ops->ieee_getets) {
  868. struct ieee_ets ets;
  869. err = ops->ieee_getets(netdev, &ets);
  870. if (!err &&
  871. nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
  872. goto nla_put_failure;
  873. }
  874. if (ops->ieee_getmaxrate) {
  875. struct ieee_maxrate maxrate;
  876. err = ops->ieee_getmaxrate(netdev, &maxrate);
  877. if (!err) {
  878. err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
  879. sizeof(maxrate), &maxrate);
  880. if (err)
  881. goto nla_put_failure;
  882. }
  883. }
  884. if (ops->ieee_getpfc) {
  885. struct ieee_pfc pfc;
  886. err = ops->ieee_getpfc(netdev, &pfc);
  887. if (!err &&
  888. nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
  889. goto nla_put_failure;
  890. }
  891. app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
  892. if (!app)
  893. goto nla_put_failure;
  894. spin_lock(&dcb_lock);
  895. list_for_each_entry(itr, &dcb_app_list, list) {
  896. if (itr->ifindex == netdev->ifindex) {
  897. err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
  898. &itr->app);
  899. if (err) {
  900. spin_unlock(&dcb_lock);
  901. goto nla_put_failure;
  902. }
  903. }
  904. }
  905. if (netdev->dcbnl_ops->getdcbx)
  906. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  907. else
  908. dcbx = -EOPNOTSUPP;
  909. spin_unlock(&dcb_lock);
  910. nla_nest_end(skb, app);
  911. /* get peer info if available */
  912. if (ops->ieee_peer_getets) {
  913. struct ieee_ets ets;
  914. err = ops->ieee_peer_getets(netdev, &ets);
  915. if (!err &&
  916. nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
  917. goto nla_put_failure;
  918. }
  919. if (ops->ieee_peer_getpfc) {
  920. struct ieee_pfc pfc;
  921. err = ops->ieee_peer_getpfc(netdev, &pfc);
  922. if (!err &&
  923. nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
  924. goto nla_put_failure;
  925. }
  926. if (ops->peer_getappinfo && ops->peer_getapptable) {
  927. err = dcbnl_build_peer_app(netdev, skb,
  928. DCB_ATTR_IEEE_PEER_APP,
  929. DCB_ATTR_IEEE_APP_UNSPEC,
  930. DCB_ATTR_IEEE_APP);
  931. if (err)
  932. goto nla_put_failure;
  933. }
  934. nla_nest_end(skb, ieee);
  935. if (dcbx >= 0) {
  936. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  937. if (err)
  938. goto nla_put_failure;
  939. }
  940. return 0;
  941. nla_put_failure:
  942. return err;
  943. }
  944. static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
  945. int dir)
  946. {
  947. u8 pgid, up_map, prio, tc_pct;
  948. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  949. int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
  950. struct nlattr *pg = nla_nest_start(skb, i);
  951. if (!pg)
  952. goto nla_put_failure;
  953. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  954. struct nlattr *tc_nest = nla_nest_start(skb, i);
  955. if (!tc_nest)
  956. goto nla_put_failure;
  957. pgid = DCB_ATTR_VALUE_UNDEFINED;
  958. prio = DCB_ATTR_VALUE_UNDEFINED;
  959. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  960. up_map = DCB_ATTR_VALUE_UNDEFINED;
  961. if (!dir)
  962. ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
  963. &prio, &pgid, &tc_pct, &up_map);
  964. else
  965. ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
  966. &prio, &pgid, &tc_pct, &up_map);
  967. if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
  968. nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
  969. nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
  970. nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
  971. goto nla_put_failure;
  972. nla_nest_end(skb, tc_nest);
  973. }
  974. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  975. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  976. if (!dir)
  977. ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
  978. &tc_pct);
  979. else
  980. ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
  981. &tc_pct);
  982. if (nla_put_u8(skb, i, tc_pct))
  983. goto nla_put_failure;
  984. }
  985. nla_nest_end(skb, pg);
  986. return 0;
  987. nla_put_failure:
  988. return -EMSGSIZE;
  989. }
  990. static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
  991. {
  992. struct nlattr *cee, *app;
  993. struct dcb_app_type *itr;
  994. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  995. int dcbx, i, err = -EMSGSIZE;
  996. u8 value;
  997. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  998. goto nla_put_failure;
  999. cee = nla_nest_start(skb, DCB_ATTR_CEE);
  1000. if (!cee)
  1001. goto nla_put_failure;
  1002. /* local pg */
  1003. if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
  1004. err = dcbnl_cee_pg_fill(skb, netdev, 1);
  1005. if (err)
  1006. goto nla_put_failure;
  1007. }
  1008. if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
  1009. err = dcbnl_cee_pg_fill(skb, netdev, 0);
  1010. if (err)
  1011. goto nla_put_failure;
  1012. }
  1013. /* local pfc */
  1014. if (ops->getpfccfg) {
  1015. struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
  1016. if (!pfc_nest)
  1017. goto nla_put_failure;
  1018. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  1019. ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
  1020. if (nla_put_u8(skb, i, value))
  1021. goto nla_put_failure;
  1022. }
  1023. nla_nest_end(skb, pfc_nest);
  1024. }
  1025. /* local app */
  1026. spin_lock(&dcb_lock);
  1027. app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
  1028. if (!app)
  1029. goto dcb_unlock;
  1030. list_for_each_entry(itr, &dcb_app_list, list) {
  1031. if (itr->ifindex == netdev->ifindex) {
  1032. struct nlattr *app_nest = nla_nest_start(skb,
  1033. DCB_ATTR_APP);
  1034. if (!app_nest)
  1035. goto dcb_unlock;
  1036. err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
  1037. itr->app.selector);
  1038. if (err)
  1039. goto dcb_unlock;
  1040. err = nla_put_u16(skb, DCB_APP_ATTR_ID,
  1041. itr->app.protocol);
  1042. if (err)
  1043. goto dcb_unlock;
  1044. err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
  1045. itr->app.priority);
  1046. if (err)
  1047. goto dcb_unlock;
  1048. nla_nest_end(skb, app_nest);
  1049. }
  1050. }
  1051. nla_nest_end(skb, app);
  1052. if (netdev->dcbnl_ops->getdcbx)
  1053. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1054. else
  1055. dcbx = -EOPNOTSUPP;
  1056. spin_unlock(&dcb_lock);
  1057. /* features flags */
  1058. if (ops->getfeatcfg) {
  1059. struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
  1060. if (!feat)
  1061. goto nla_put_failure;
  1062. for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
  1063. i++)
  1064. if (!ops->getfeatcfg(netdev, i, &value) &&
  1065. nla_put_u8(skb, i, value))
  1066. goto nla_put_failure;
  1067. nla_nest_end(skb, feat);
  1068. }
  1069. /* peer info if available */
  1070. if (ops->cee_peer_getpg) {
  1071. struct cee_pg pg;
  1072. err = ops->cee_peer_getpg(netdev, &pg);
  1073. if (!err &&
  1074. nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
  1075. goto nla_put_failure;
  1076. }
  1077. if (ops->cee_peer_getpfc) {
  1078. struct cee_pfc pfc;
  1079. err = ops->cee_peer_getpfc(netdev, &pfc);
  1080. if (!err &&
  1081. nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
  1082. goto nla_put_failure;
  1083. }
  1084. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1085. err = dcbnl_build_peer_app(netdev, skb,
  1086. DCB_ATTR_CEE_PEER_APP_TABLE,
  1087. DCB_ATTR_CEE_PEER_APP_INFO,
  1088. DCB_ATTR_CEE_PEER_APP);
  1089. if (err)
  1090. goto nla_put_failure;
  1091. }
  1092. nla_nest_end(skb, cee);
  1093. /* DCBX state */
  1094. if (dcbx >= 0) {
  1095. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1096. if (err)
  1097. goto nla_put_failure;
  1098. }
  1099. return 0;
  1100. dcb_unlock:
  1101. spin_unlock(&dcb_lock);
  1102. nla_put_failure:
  1103. return err;
  1104. }
  1105. static int dcbnl_notify(struct net_device *dev, int event, int cmd,
  1106. u32 seq, u32 pid, int dcbx_ver)
  1107. {
  1108. struct net *net = dev_net(dev);
  1109. struct sk_buff *skb;
  1110. struct nlmsghdr *nlh;
  1111. struct dcbmsg *dcb;
  1112. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1113. int err;
  1114. if (!ops)
  1115. return -EOPNOTSUPP;
  1116. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1117. if (!skb)
  1118. return -ENOBUFS;
  1119. nlh = nlmsg_put(skb, pid, 0, event, sizeof(*dcb), 0);
  1120. if (nlh == NULL) {
  1121. nlmsg_free(skb);
  1122. return -EMSGSIZE;
  1123. }
  1124. dcb = NLMSG_DATA(nlh);
  1125. dcb->dcb_family = AF_UNSPEC;
  1126. dcb->cmd = cmd;
  1127. if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
  1128. err = dcbnl_ieee_fill(skb, dev);
  1129. else
  1130. err = dcbnl_cee_fill(skb, dev);
  1131. if (err < 0) {
  1132. /* Report error to broadcast listeners */
  1133. nlmsg_cancel(skb, nlh);
  1134. kfree_skb(skb);
  1135. rtnl_set_sk_err(net, RTNLGRP_DCB, err);
  1136. } else {
  1137. /* End nlmsg and notify broadcast listeners */
  1138. nlmsg_end(skb, nlh);
  1139. rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
  1140. }
  1141. return err;
  1142. }
  1143. int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
  1144. u32 seq, u32 pid)
  1145. {
  1146. return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_IEEE);
  1147. }
  1148. EXPORT_SYMBOL(dcbnl_ieee_notify);
  1149. int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
  1150. u32 seq, u32 pid)
  1151. {
  1152. return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_CEE);
  1153. }
  1154. EXPORT_SYMBOL(dcbnl_cee_notify);
  1155. /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
  1156. * be completed the entire msg is aborted and error value is returned.
  1157. * No attempt is made to reconcile the case where only part of the
  1158. * cmd can be completed.
  1159. */
  1160. static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
  1161. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1162. {
  1163. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1164. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1165. int err = -EOPNOTSUPP;
  1166. if (!ops)
  1167. return err;
  1168. if (!tb[DCB_ATTR_IEEE])
  1169. return -EINVAL;
  1170. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1171. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1172. if (err)
  1173. return err;
  1174. if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  1175. struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  1176. err = ops->ieee_setets(netdev, ets);
  1177. if (err)
  1178. goto err;
  1179. }
  1180. if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
  1181. struct ieee_maxrate *maxrate =
  1182. nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
  1183. err = ops->ieee_setmaxrate(netdev, maxrate);
  1184. if (err)
  1185. goto err;
  1186. }
  1187. if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
  1188. struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  1189. err = ops->ieee_setpfc(netdev, pfc);
  1190. if (err)
  1191. goto err;
  1192. }
  1193. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1194. struct nlattr *attr;
  1195. int rem;
  1196. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1197. struct dcb_app *app_data;
  1198. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1199. continue;
  1200. app_data = nla_data(attr);
  1201. if (ops->ieee_setapp)
  1202. err = ops->ieee_setapp(netdev, app_data);
  1203. else
  1204. err = dcb_ieee_setapp(netdev, app_data);
  1205. if (err)
  1206. goto err;
  1207. }
  1208. }
  1209. err:
  1210. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1211. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
  1212. return err;
  1213. }
  1214. static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1215. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1216. {
  1217. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1218. if (!ops)
  1219. return -EOPNOTSUPP;
  1220. return dcbnl_ieee_fill(skb, netdev);
  1221. }
  1222. static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
  1223. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1224. {
  1225. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1226. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1227. int err = -EOPNOTSUPP;
  1228. if (!ops)
  1229. return -EOPNOTSUPP;
  1230. if (!tb[DCB_ATTR_IEEE])
  1231. return -EINVAL;
  1232. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1233. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1234. if (err)
  1235. return err;
  1236. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1237. struct nlattr *attr;
  1238. int rem;
  1239. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1240. struct dcb_app *app_data;
  1241. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1242. continue;
  1243. app_data = nla_data(attr);
  1244. if (ops->ieee_delapp)
  1245. err = ops->ieee_delapp(netdev, app_data);
  1246. else
  1247. err = dcb_ieee_delapp(netdev, app_data);
  1248. if (err)
  1249. goto err;
  1250. }
  1251. }
  1252. err:
  1253. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1254. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
  1255. return err;
  1256. }
  1257. /* DCBX configuration */
  1258. static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1259. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1260. {
  1261. if (!netdev->dcbnl_ops->getdcbx)
  1262. return -EOPNOTSUPP;
  1263. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1264. netdev->dcbnl_ops->getdcbx(netdev));
  1265. }
  1266. static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1267. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1268. {
  1269. u8 value;
  1270. if (!netdev->dcbnl_ops->setdcbx)
  1271. return -EOPNOTSUPP;
  1272. if (!tb[DCB_ATTR_DCBX])
  1273. return -EINVAL;
  1274. value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  1275. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1276. netdev->dcbnl_ops->setdcbx(netdev, value));
  1277. }
  1278. static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1279. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1280. {
  1281. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  1282. u8 value;
  1283. int ret, i;
  1284. int getall = 0;
  1285. if (!netdev->dcbnl_ops->getfeatcfg)
  1286. return -EOPNOTSUPP;
  1287. if (!tb[DCB_ATTR_FEATCFG])
  1288. return -EINVAL;
  1289. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1290. dcbnl_featcfg_nest);
  1291. if (ret)
  1292. return ret;
  1293. nest = nla_nest_start(skb, DCB_ATTR_FEATCFG);
  1294. if (!nest)
  1295. return -EMSGSIZE;
  1296. if (data[DCB_FEATCFG_ATTR_ALL])
  1297. getall = 1;
  1298. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1299. if (!getall && !data[i])
  1300. continue;
  1301. ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
  1302. if (!ret)
  1303. ret = nla_put_u8(skb, i, value);
  1304. if (ret) {
  1305. nla_nest_cancel(skb, nest);
  1306. goto nla_put_failure;
  1307. }
  1308. }
  1309. nla_nest_end(skb, nest);
  1310. nla_put_failure:
  1311. return ret;
  1312. }
  1313. static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1314. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1315. {
  1316. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
  1317. int ret, i;
  1318. u8 value;
  1319. if (!netdev->dcbnl_ops->setfeatcfg)
  1320. return -ENOTSUPP;
  1321. if (!tb[DCB_ATTR_FEATCFG])
  1322. return -EINVAL;
  1323. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1324. dcbnl_featcfg_nest);
  1325. if (ret)
  1326. goto err;
  1327. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1328. if (data[i] == NULL)
  1329. continue;
  1330. value = nla_get_u8(data[i]);
  1331. ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  1332. if (ret)
  1333. goto err;
  1334. }
  1335. err:
  1336. ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
  1337. return ret;
  1338. }
  1339. /* Handle CEE DCBX GET commands. */
  1340. static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1341. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1342. {
  1343. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1344. if (!ops)
  1345. return -EOPNOTSUPP;
  1346. return dcbnl_cee_fill(skb, netdev);
  1347. }
  1348. struct reply_func {
  1349. /* reply netlink message type */
  1350. int type;
  1351. /* function to fill message contents */
  1352. int (*cb)(struct net_device *, struct nlmsghdr *, u32,
  1353. struct nlattr **, struct sk_buff *);
  1354. };
  1355. static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
  1356. [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
  1357. [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
  1358. [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
  1359. [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
  1360. [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
  1361. [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
  1362. [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
  1363. [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
  1364. [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
  1365. [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
  1366. [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
  1367. [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
  1368. [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
  1369. [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
  1370. [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
  1371. [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
  1372. [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
  1373. [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
  1374. [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
  1375. [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
  1376. [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
  1377. [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
  1378. [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
  1379. [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
  1380. [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
  1381. [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
  1382. [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
  1383. };
  1384. static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1385. {
  1386. struct net *net = sock_net(skb->sk);
  1387. struct net_device *netdev;
  1388. struct dcbmsg *dcb = (struct dcbmsg *)NLMSG_DATA(nlh);
  1389. struct nlattr *tb[DCB_ATTR_MAX + 1];
  1390. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  1391. int ret = -EINVAL;
  1392. struct sk_buff *reply_skb;
  1393. struct nlmsghdr *reply_nlh;
  1394. const struct reply_func *fn;
  1395. if (!net_eq(net, &init_net))
  1396. return -EINVAL;
  1397. ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  1398. dcbnl_rtnl_policy);
  1399. if (ret < 0)
  1400. return ret;
  1401. if (dcb->cmd > DCB_CMD_MAX)
  1402. return -EINVAL;
  1403. /* check if a reply function has been defined for the command */
  1404. fn = &reply_funcs[dcb->cmd];
  1405. if (!fn->cb)
  1406. return -EOPNOTSUPP;
  1407. if (!tb[DCB_ATTR_IFNAME])
  1408. return -EINVAL;
  1409. netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
  1410. if (!netdev)
  1411. return -EINVAL;
  1412. if (!netdev->dcbnl_ops)
  1413. goto errout;
  1414. reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, pid, nlh->nlmsg_seq,
  1415. nlh->nlmsg_flags, &reply_nlh);
  1416. if (!reply_skb) {
  1417. ret = -ENOBUFS;
  1418. goto out;
  1419. }
  1420. ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
  1421. if (ret < 0) {
  1422. nlmsg_free(reply_skb);
  1423. goto out;
  1424. }
  1425. nlmsg_end(reply_skb, reply_nlh);
  1426. ret = rtnl_unicast(reply_skb, &init_net, pid);
  1427. out:
  1428. dev_put(netdev);
  1429. return ret;
  1430. }
  1431. /**
  1432. * dcb_getapp - retrieve the DCBX application user priority
  1433. *
  1434. * On success returns a non-zero 802.1p user priority bitmap
  1435. * otherwise returns 0 as the invalid user priority bitmap to
  1436. * indicate an error.
  1437. */
  1438. u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  1439. {
  1440. struct dcb_app_type *itr;
  1441. u8 prio = 0;
  1442. spin_lock(&dcb_lock);
  1443. list_for_each_entry(itr, &dcb_app_list, list) {
  1444. if (itr->app.selector == app->selector &&
  1445. itr->app.protocol == app->protocol &&
  1446. itr->ifindex == dev->ifindex) {
  1447. prio = itr->app.priority;
  1448. break;
  1449. }
  1450. }
  1451. spin_unlock(&dcb_lock);
  1452. return prio;
  1453. }
  1454. EXPORT_SYMBOL(dcb_getapp);
  1455. /**
  1456. * dcb_setapp - add CEE dcb application data to app list
  1457. *
  1458. * Priority 0 is an invalid priority in CEE spec. This routine
  1459. * removes applications from the app list if the priority is
  1460. * set to zero.
  1461. */
  1462. int dcb_setapp(struct net_device *dev, struct dcb_app *new)
  1463. {
  1464. struct dcb_app_type *itr;
  1465. struct dcb_app_type event;
  1466. event.ifindex = dev->ifindex;
  1467. memcpy(&event.app, new, sizeof(event.app));
  1468. if (dev->dcbnl_ops->getdcbx)
  1469. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1470. spin_lock(&dcb_lock);
  1471. /* Search for existing match and replace */
  1472. list_for_each_entry(itr, &dcb_app_list, list) {
  1473. if (itr->app.selector == new->selector &&
  1474. itr->app.protocol == new->protocol &&
  1475. itr->ifindex == dev->ifindex) {
  1476. if (new->priority)
  1477. itr->app.priority = new->priority;
  1478. else {
  1479. list_del(&itr->list);
  1480. kfree(itr);
  1481. }
  1482. goto out;
  1483. }
  1484. }
  1485. /* App type does not exist add new application type */
  1486. if (new->priority) {
  1487. struct dcb_app_type *entry;
  1488. entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
  1489. if (!entry) {
  1490. spin_unlock(&dcb_lock);
  1491. return -ENOMEM;
  1492. }
  1493. memcpy(&entry->app, new, sizeof(*new));
  1494. entry->ifindex = dev->ifindex;
  1495. list_add(&entry->list, &dcb_app_list);
  1496. }
  1497. out:
  1498. spin_unlock(&dcb_lock);
  1499. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1500. return 0;
  1501. }
  1502. EXPORT_SYMBOL(dcb_setapp);
  1503. /**
  1504. * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
  1505. *
  1506. * Helper routine which on success returns a non-zero 802.1Qaz user
  1507. * priority bitmap otherwise returns 0 to indicate the dcb_app was
  1508. * not found in APP list.
  1509. */
  1510. u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
  1511. {
  1512. struct dcb_app_type *itr;
  1513. u8 prio = 0;
  1514. spin_lock(&dcb_lock);
  1515. list_for_each_entry(itr, &dcb_app_list, list) {
  1516. if (itr->app.selector == app->selector &&
  1517. itr->app.protocol == app->protocol &&
  1518. itr->ifindex == dev->ifindex) {
  1519. prio |= 1 << itr->app.priority;
  1520. }
  1521. }
  1522. spin_unlock(&dcb_lock);
  1523. return prio;
  1524. }
  1525. EXPORT_SYMBOL(dcb_ieee_getapp_mask);
  1526. /**
  1527. * dcb_ieee_setapp - add IEEE dcb application data to app list
  1528. *
  1529. * This adds Application data to the list. Multiple application
  1530. * entries may exists for the same selector and protocol as long
  1531. * as the priorities are different.
  1532. */
  1533. int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
  1534. {
  1535. struct dcb_app_type *itr, *entry;
  1536. struct dcb_app_type event;
  1537. int err = 0;
  1538. event.ifindex = dev->ifindex;
  1539. memcpy(&event.app, new, sizeof(event.app));
  1540. if (dev->dcbnl_ops->getdcbx)
  1541. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1542. spin_lock(&dcb_lock);
  1543. /* Search for existing match and abort if found */
  1544. list_for_each_entry(itr, &dcb_app_list, list) {
  1545. if (itr->app.selector == new->selector &&
  1546. itr->app.protocol == new->protocol &&
  1547. itr->app.priority == new->priority &&
  1548. itr->ifindex == dev->ifindex) {
  1549. err = -EEXIST;
  1550. goto out;
  1551. }
  1552. }
  1553. /* App entry does not exist add new entry */
  1554. entry = kmalloc(sizeof(struct dcb_app_type), GFP_ATOMIC);
  1555. if (!entry) {
  1556. err = -ENOMEM;
  1557. goto out;
  1558. }
  1559. memcpy(&entry->app, new, sizeof(*new));
  1560. entry->ifindex = dev->ifindex;
  1561. list_add(&entry->list, &dcb_app_list);
  1562. out:
  1563. spin_unlock(&dcb_lock);
  1564. if (!err)
  1565. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1566. return err;
  1567. }
  1568. EXPORT_SYMBOL(dcb_ieee_setapp);
  1569. /**
  1570. * dcb_ieee_delapp - delete IEEE dcb application data from list
  1571. *
  1572. * This removes a matching APP data from the APP list
  1573. */
  1574. int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
  1575. {
  1576. struct dcb_app_type *itr;
  1577. struct dcb_app_type event;
  1578. int err = -ENOENT;
  1579. event.ifindex = dev->ifindex;
  1580. memcpy(&event.app, del, sizeof(event.app));
  1581. if (dev->dcbnl_ops->getdcbx)
  1582. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1583. spin_lock(&dcb_lock);
  1584. /* Search for existing match and remove it. */
  1585. list_for_each_entry(itr, &dcb_app_list, list) {
  1586. if (itr->app.selector == del->selector &&
  1587. itr->app.protocol == del->protocol &&
  1588. itr->app.priority == del->priority &&
  1589. itr->ifindex == dev->ifindex) {
  1590. list_del(&itr->list);
  1591. kfree(itr);
  1592. err = 0;
  1593. goto out;
  1594. }
  1595. }
  1596. out:
  1597. spin_unlock(&dcb_lock);
  1598. if (!err)
  1599. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1600. return err;
  1601. }
  1602. EXPORT_SYMBOL(dcb_ieee_delapp);
  1603. static void dcb_flushapp(void)
  1604. {
  1605. struct dcb_app_type *app;
  1606. struct dcb_app_type *tmp;
  1607. spin_lock(&dcb_lock);
  1608. list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
  1609. list_del(&app->list);
  1610. kfree(app);
  1611. }
  1612. spin_unlock(&dcb_lock);
  1613. }
  1614. static int __init dcbnl_init(void)
  1615. {
  1616. INIT_LIST_HEAD(&dcb_app_list);
  1617. rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, NULL);
  1618. rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, NULL);
  1619. return 0;
  1620. }
  1621. module_init(dcbnl_init);
  1622. static void __exit dcbnl_exit(void)
  1623. {
  1624. rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
  1625. rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
  1626. dcb_flushapp();
  1627. }
  1628. module_exit(dcbnl_exit);