dcbnl.c 49 KB

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