dcbnl.c 52 KB

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