dcbnl.c 48 KB

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