dcbnl.c 48 KB

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