dcbnl.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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,
  708. (dir ? DCB_CMD_PGRX_SCFG : DCB_CMD_PGTX_SCFG), 0);
  709. }
  710. static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  711. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  712. {
  713. return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
  714. }
  715. static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  716. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  717. {
  718. return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
  719. }
  720. static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  721. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  722. {
  723. struct nlattr *bcn_nest;
  724. struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
  725. u8 value_byte;
  726. u32 value_integer;
  727. int ret;
  728. bool getall = false;
  729. int i;
  730. if (!tb[DCB_ATTR_BCN])
  731. return -EINVAL;
  732. if (!netdev->dcbnl_ops->getbcnrp ||
  733. !netdev->dcbnl_ops->getbcncfg)
  734. return -EOPNOTSUPP;
  735. ret = nla_parse_nested(bcn_tb, DCB_BCN_ATTR_MAX,
  736. tb[DCB_ATTR_BCN], dcbnl_bcn_nest);
  737. if (ret)
  738. return ret;
  739. bcn_nest = nla_nest_start(skb, DCB_ATTR_BCN);
  740. if (!bcn_nest)
  741. return -EMSGSIZE;
  742. if (bcn_tb[DCB_BCN_ATTR_ALL])
  743. getall = true;
  744. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  745. if (!getall && !bcn_tb[i])
  746. continue;
  747. netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
  748. &value_byte);
  749. ret = nla_put_u8(skb, i, value_byte);
  750. if (ret)
  751. goto err_bcn;
  752. }
  753. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  754. if (!getall && !bcn_tb[i])
  755. continue;
  756. netdev->dcbnl_ops->getbcncfg(netdev, i,
  757. &value_integer);
  758. ret = nla_put_u32(skb, i, value_integer);
  759. if (ret)
  760. goto err_bcn;
  761. }
  762. nla_nest_end(skb, bcn_nest);
  763. return 0;
  764. err_bcn:
  765. nla_nest_cancel(skb, bcn_nest);
  766. return ret;
  767. }
  768. static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  769. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  770. {
  771. struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
  772. int i;
  773. int ret;
  774. u8 value_byte;
  775. u32 value_int;
  776. if (!tb[DCB_ATTR_BCN])
  777. return -EINVAL;
  778. if (!netdev->dcbnl_ops->setbcncfg ||
  779. !netdev->dcbnl_ops->setbcnrp)
  780. return -EOPNOTSUPP;
  781. ret = nla_parse_nested(data, DCB_BCN_ATTR_MAX,
  782. tb[DCB_ATTR_BCN],
  783. dcbnl_pfc_up_nest);
  784. if (ret)
  785. return ret;
  786. for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
  787. if (data[i] == NULL)
  788. continue;
  789. value_byte = nla_get_u8(data[i]);
  790. netdev->dcbnl_ops->setbcnrp(netdev,
  791. data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
  792. }
  793. for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
  794. if (data[i] == NULL)
  795. continue;
  796. value_int = nla_get_u32(data[i]);
  797. netdev->dcbnl_ops->setbcncfg(netdev,
  798. i, value_int);
  799. }
  800. return nla_put_u8(skb, DCB_ATTR_BCN, 0);
  801. }
  802. static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
  803. int app_nested_type, int app_info_type,
  804. int app_entry_type)
  805. {
  806. struct dcb_peer_app_info info;
  807. struct dcb_app *table = NULL;
  808. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  809. u16 app_count;
  810. int err;
  811. /**
  812. * retrieve the peer app configuration form the driver. If the driver
  813. * handlers fail exit without doing anything
  814. */
  815. err = ops->peer_getappinfo(netdev, &info, &app_count);
  816. if (!err && app_count) {
  817. table = kmalloc(sizeof(struct dcb_app) * app_count, GFP_KERNEL);
  818. if (!table)
  819. return -ENOMEM;
  820. err = ops->peer_getapptable(netdev, table);
  821. }
  822. if (!err) {
  823. u16 i;
  824. struct nlattr *app;
  825. /**
  826. * build the message, from here on the only possible failure
  827. * is due to the skb size
  828. */
  829. err = -EMSGSIZE;
  830. app = nla_nest_start(skb, app_nested_type);
  831. if (!app)
  832. goto nla_put_failure;
  833. if (app_info_type &&
  834. nla_put(skb, app_info_type, sizeof(info), &info))
  835. goto nla_put_failure;
  836. for (i = 0; i < app_count; i++) {
  837. if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
  838. &table[i]))
  839. goto nla_put_failure;
  840. }
  841. nla_nest_end(skb, app);
  842. }
  843. err = 0;
  844. nla_put_failure:
  845. kfree(table);
  846. return err;
  847. }
  848. /* Handle IEEE 802.1Qaz GET commands. */
  849. static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
  850. {
  851. struct nlattr *ieee, *app;
  852. struct dcb_app_type *itr;
  853. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  854. int dcbx;
  855. int err;
  856. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  857. return -EMSGSIZE;
  858. ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
  859. if (!ieee)
  860. return -EMSGSIZE;
  861. if (ops->ieee_getets) {
  862. struct ieee_ets ets;
  863. err = ops->ieee_getets(netdev, &ets);
  864. if (!err &&
  865. nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
  866. return -EMSGSIZE;
  867. }
  868. if (ops->ieee_getmaxrate) {
  869. struct ieee_maxrate 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. err = ops->ieee_getpfc(netdev, &pfc);
  881. if (!err &&
  882. nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
  883. return -EMSGSIZE;
  884. }
  885. app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
  886. if (!app)
  887. return -EMSGSIZE;
  888. spin_lock(&dcb_lock);
  889. list_for_each_entry(itr, &dcb_app_list, list) {
  890. if (itr->ifindex == netdev->ifindex) {
  891. err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
  892. &itr->app);
  893. if (err) {
  894. spin_unlock(&dcb_lock);
  895. return -EMSGSIZE;
  896. }
  897. }
  898. }
  899. if (netdev->dcbnl_ops->getdcbx)
  900. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  901. else
  902. dcbx = -EOPNOTSUPP;
  903. spin_unlock(&dcb_lock);
  904. nla_nest_end(skb, app);
  905. /* get peer info if available */
  906. if (ops->ieee_peer_getets) {
  907. struct ieee_ets ets;
  908. err = ops->ieee_peer_getets(netdev, &ets);
  909. if (!err &&
  910. nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
  911. return -EMSGSIZE;
  912. }
  913. if (ops->ieee_peer_getpfc) {
  914. struct ieee_pfc pfc;
  915. err = ops->ieee_peer_getpfc(netdev, &pfc);
  916. if (!err &&
  917. nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
  918. return -EMSGSIZE;
  919. }
  920. if (ops->peer_getappinfo && ops->peer_getapptable) {
  921. err = dcbnl_build_peer_app(netdev, skb,
  922. DCB_ATTR_IEEE_PEER_APP,
  923. DCB_ATTR_IEEE_APP_UNSPEC,
  924. DCB_ATTR_IEEE_APP);
  925. if (err)
  926. return -EMSGSIZE;
  927. }
  928. nla_nest_end(skb, ieee);
  929. if (dcbx >= 0) {
  930. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  931. if (err)
  932. return -EMSGSIZE;
  933. }
  934. return 0;
  935. }
  936. static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
  937. int dir)
  938. {
  939. u8 pgid, up_map, prio, tc_pct;
  940. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  941. int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
  942. struct nlattr *pg = nla_nest_start(skb, i);
  943. if (!pg)
  944. return -EMSGSIZE;
  945. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  946. struct nlattr *tc_nest = nla_nest_start(skb, i);
  947. if (!tc_nest)
  948. return -EMSGSIZE;
  949. pgid = DCB_ATTR_VALUE_UNDEFINED;
  950. prio = DCB_ATTR_VALUE_UNDEFINED;
  951. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  952. up_map = DCB_ATTR_VALUE_UNDEFINED;
  953. if (!dir)
  954. ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
  955. &prio, &pgid, &tc_pct, &up_map);
  956. else
  957. ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
  958. &prio, &pgid, &tc_pct, &up_map);
  959. if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
  960. nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
  961. nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
  962. nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
  963. return -EMSGSIZE;
  964. nla_nest_end(skb, tc_nest);
  965. }
  966. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  967. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  968. if (!dir)
  969. ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
  970. &tc_pct);
  971. else
  972. ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
  973. &tc_pct);
  974. if (nla_put_u8(skb, i, tc_pct))
  975. return -EMSGSIZE;
  976. }
  977. nla_nest_end(skb, pg);
  978. return 0;
  979. }
  980. static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
  981. {
  982. struct nlattr *cee, *app;
  983. struct dcb_app_type *itr;
  984. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  985. int dcbx, i, err = -EMSGSIZE;
  986. u8 value;
  987. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  988. goto nla_put_failure;
  989. cee = nla_nest_start(skb, DCB_ATTR_CEE);
  990. if (!cee)
  991. goto nla_put_failure;
  992. /* local pg */
  993. if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
  994. err = dcbnl_cee_pg_fill(skb, netdev, 1);
  995. if (err)
  996. goto nla_put_failure;
  997. }
  998. if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
  999. err = dcbnl_cee_pg_fill(skb, netdev, 0);
  1000. if (err)
  1001. goto nla_put_failure;
  1002. }
  1003. /* local pfc */
  1004. if (ops->getpfccfg) {
  1005. struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
  1006. if (!pfc_nest)
  1007. goto nla_put_failure;
  1008. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  1009. ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
  1010. if (nla_put_u8(skb, i, value))
  1011. goto nla_put_failure;
  1012. }
  1013. nla_nest_end(skb, pfc_nest);
  1014. }
  1015. /* local app */
  1016. spin_lock(&dcb_lock);
  1017. app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
  1018. if (!app)
  1019. goto dcb_unlock;
  1020. list_for_each_entry(itr, &dcb_app_list, list) {
  1021. if (itr->ifindex == netdev->ifindex) {
  1022. struct nlattr *app_nest = nla_nest_start(skb,
  1023. DCB_ATTR_APP);
  1024. if (!app_nest)
  1025. goto dcb_unlock;
  1026. err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
  1027. itr->app.selector);
  1028. if (err)
  1029. goto dcb_unlock;
  1030. err = nla_put_u16(skb, DCB_APP_ATTR_ID,
  1031. itr->app.protocol);
  1032. if (err)
  1033. goto dcb_unlock;
  1034. err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
  1035. itr->app.priority);
  1036. if (err)
  1037. goto dcb_unlock;
  1038. nla_nest_end(skb, app_nest);
  1039. }
  1040. }
  1041. nla_nest_end(skb, app);
  1042. if (netdev->dcbnl_ops->getdcbx)
  1043. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1044. else
  1045. dcbx = -EOPNOTSUPP;
  1046. spin_unlock(&dcb_lock);
  1047. /* features flags */
  1048. if (ops->getfeatcfg) {
  1049. struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
  1050. if (!feat)
  1051. goto nla_put_failure;
  1052. for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
  1053. i++)
  1054. if (!ops->getfeatcfg(netdev, i, &value) &&
  1055. nla_put_u8(skb, i, value))
  1056. goto nla_put_failure;
  1057. nla_nest_end(skb, feat);
  1058. }
  1059. /* peer info if available */
  1060. if (ops->cee_peer_getpg) {
  1061. struct cee_pg pg;
  1062. err = ops->cee_peer_getpg(netdev, &pg);
  1063. if (!err &&
  1064. nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
  1065. goto nla_put_failure;
  1066. }
  1067. if (ops->cee_peer_getpfc) {
  1068. struct cee_pfc pfc;
  1069. err = ops->cee_peer_getpfc(netdev, &pfc);
  1070. if (!err &&
  1071. nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
  1072. goto nla_put_failure;
  1073. }
  1074. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1075. err = dcbnl_build_peer_app(netdev, skb,
  1076. DCB_ATTR_CEE_PEER_APP_TABLE,
  1077. DCB_ATTR_CEE_PEER_APP_INFO,
  1078. DCB_ATTR_CEE_PEER_APP);
  1079. if (err)
  1080. goto nla_put_failure;
  1081. }
  1082. nla_nest_end(skb, cee);
  1083. /* DCBX state */
  1084. if (dcbx >= 0) {
  1085. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1086. if (err)
  1087. goto nla_put_failure;
  1088. }
  1089. return 0;
  1090. dcb_unlock:
  1091. spin_unlock(&dcb_lock);
  1092. nla_put_failure:
  1093. return err;
  1094. }
  1095. static int dcbnl_notify(struct net_device *dev, int event, int cmd,
  1096. u32 seq, u32 pid, int dcbx_ver)
  1097. {
  1098. struct net *net = dev_net(dev);
  1099. struct sk_buff *skb;
  1100. struct nlmsghdr *nlh;
  1101. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1102. int err;
  1103. if (!ops)
  1104. return -EOPNOTSUPP;
  1105. skb = dcbnl_newmsg(event, cmd, pid, seq, 0, &nlh);
  1106. if (!skb)
  1107. return -ENOBUFS;
  1108. if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
  1109. err = dcbnl_ieee_fill(skb, dev);
  1110. else
  1111. err = dcbnl_cee_fill(skb, dev);
  1112. if (err < 0) {
  1113. /* Report error to broadcast listeners */
  1114. nlmsg_free(skb);
  1115. rtnl_set_sk_err(net, RTNLGRP_DCB, err);
  1116. } else {
  1117. /* End nlmsg and notify broadcast listeners */
  1118. nlmsg_end(skb, nlh);
  1119. rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
  1120. }
  1121. return err;
  1122. }
  1123. int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
  1124. u32 seq, u32 pid)
  1125. {
  1126. return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_IEEE);
  1127. }
  1128. EXPORT_SYMBOL(dcbnl_ieee_notify);
  1129. int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
  1130. u32 seq, u32 pid)
  1131. {
  1132. return dcbnl_notify(dev, event, cmd, seq, pid, DCB_CAP_DCBX_VER_CEE);
  1133. }
  1134. EXPORT_SYMBOL(dcbnl_cee_notify);
  1135. /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
  1136. * be completed the entire msg is aborted and error value is returned.
  1137. * No attempt is made to reconcile the case where only part of the
  1138. * cmd can be completed.
  1139. */
  1140. static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
  1141. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1142. {
  1143. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1144. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1145. int err;
  1146. if (!ops)
  1147. return -EOPNOTSUPP;
  1148. if (!tb[DCB_ATTR_IEEE])
  1149. return -EINVAL;
  1150. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1151. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1152. if (err)
  1153. return err;
  1154. if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  1155. struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  1156. err = ops->ieee_setets(netdev, ets);
  1157. if (err)
  1158. goto err;
  1159. }
  1160. if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
  1161. struct ieee_maxrate *maxrate =
  1162. nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
  1163. err = ops->ieee_setmaxrate(netdev, maxrate);
  1164. if (err)
  1165. goto err;
  1166. }
  1167. if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
  1168. struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  1169. err = ops->ieee_setpfc(netdev, pfc);
  1170. if (err)
  1171. goto err;
  1172. }
  1173. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1174. struct nlattr *attr;
  1175. int rem;
  1176. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1177. struct dcb_app *app_data;
  1178. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1179. continue;
  1180. app_data = nla_data(attr);
  1181. if (ops->ieee_setapp)
  1182. err = ops->ieee_setapp(netdev, app_data);
  1183. else
  1184. err = dcb_ieee_setapp(netdev, app_data);
  1185. if (err)
  1186. goto err;
  1187. }
  1188. }
  1189. err:
  1190. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1191. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
  1192. return err;
  1193. }
  1194. static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1195. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1196. {
  1197. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1198. if (!ops)
  1199. return -EOPNOTSUPP;
  1200. return dcbnl_ieee_fill(skb, netdev);
  1201. }
  1202. static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
  1203. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1204. {
  1205. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1206. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1207. int err;
  1208. if (!ops)
  1209. return -EOPNOTSUPP;
  1210. if (!tb[DCB_ATTR_IEEE])
  1211. return -EINVAL;
  1212. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1213. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1214. if (err)
  1215. return err;
  1216. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1217. struct nlattr *attr;
  1218. int rem;
  1219. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1220. struct dcb_app *app_data;
  1221. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1222. continue;
  1223. app_data = nla_data(attr);
  1224. if (ops->ieee_delapp)
  1225. err = ops->ieee_delapp(netdev, app_data);
  1226. else
  1227. err = dcb_ieee_delapp(netdev, app_data);
  1228. if (err)
  1229. goto err;
  1230. }
  1231. }
  1232. err:
  1233. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1234. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
  1235. return err;
  1236. }
  1237. /* DCBX configuration */
  1238. static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1239. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1240. {
  1241. if (!netdev->dcbnl_ops->getdcbx)
  1242. return -EOPNOTSUPP;
  1243. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1244. netdev->dcbnl_ops->getdcbx(netdev));
  1245. }
  1246. static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1247. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1248. {
  1249. u8 value;
  1250. if (!netdev->dcbnl_ops->setdcbx)
  1251. return -EOPNOTSUPP;
  1252. if (!tb[DCB_ATTR_DCBX])
  1253. return -EINVAL;
  1254. value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  1255. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1256. netdev->dcbnl_ops->setdcbx(netdev, value));
  1257. }
  1258. static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1259. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1260. {
  1261. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  1262. u8 value;
  1263. int ret, i;
  1264. int getall = 0;
  1265. if (!netdev->dcbnl_ops->getfeatcfg)
  1266. return -EOPNOTSUPP;
  1267. if (!tb[DCB_ATTR_FEATCFG])
  1268. return -EINVAL;
  1269. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1270. dcbnl_featcfg_nest);
  1271. if (ret)
  1272. return ret;
  1273. nest = nla_nest_start(skb, DCB_ATTR_FEATCFG);
  1274. if (!nest)
  1275. return -EMSGSIZE;
  1276. if (data[DCB_FEATCFG_ATTR_ALL])
  1277. getall = 1;
  1278. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1279. if (!getall && !data[i])
  1280. continue;
  1281. ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
  1282. if (!ret)
  1283. ret = nla_put_u8(skb, i, value);
  1284. if (ret) {
  1285. nla_nest_cancel(skb, nest);
  1286. goto nla_put_failure;
  1287. }
  1288. }
  1289. nla_nest_end(skb, nest);
  1290. nla_put_failure:
  1291. return ret;
  1292. }
  1293. static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1294. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1295. {
  1296. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
  1297. int ret, i;
  1298. u8 value;
  1299. if (!netdev->dcbnl_ops->setfeatcfg)
  1300. return -ENOTSUPP;
  1301. if (!tb[DCB_ATTR_FEATCFG])
  1302. return -EINVAL;
  1303. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1304. dcbnl_featcfg_nest);
  1305. if (ret)
  1306. goto err;
  1307. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1308. if (data[i] == NULL)
  1309. continue;
  1310. value = nla_get_u8(data[i]);
  1311. ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  1312. if (ret)
  1313. goto err;
  1314. }
  1315. err:
  1316. ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
  1317. return ret;
  1318. }
  1319. /* Handle CEE DCBX GET commands. */
  1320. static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1321. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1322. {
  1323. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1324. if (!ops)
  1325. return -EOPNOTSUPP;
  1326. return dcbnl_cee_fill(skb, netdev);
  1327. }
  1328. struct reply_func {
  1329. /* reply netlink message type */
  1330. int type;
  1331. /* function to fill message contents */
  1332. int (*cb)(struct net_device *, struct nlmsghdr *, u32,
  1333. struct nlattr **, struct sk_buff *);
  1334. };
  1335. static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
  1336. [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
  1337. [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
  1338. [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
  1339. [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
  1340. [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
  1341. [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
  1342. [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
  1343. [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
  1344. [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
  1345. [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
  1346. [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
  1347. [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
  1348. [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
  1349. [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
  1350. [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
  1351. [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
  1352. [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
  1353. [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
  1354. [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
  1355. [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
  1356. [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
  1357. [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
  1358. [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
  1359. [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
  1360. [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
  1361. [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
  1362. [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
  1363. };
  1364. static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1365. {
  1366. struct net *net = sock_net(skb->sk);
  1367. struct net_device *netdev;
  1368. struct dcbmsg *dcb = nlmsg_data(nlh);
  1369. struct nlattr *tb[DCB_ATTR_MAX + 1];
  1370. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  1371. int ret = -EINVAL;
  1372. struct sk_buff *reply_skb;
  1373. struct nlmsghdr *reply_nlh = NULL;
  1374. const struct reply_func *fn;
  1375. if (!net_eq(net, &init_net))
  1376. return -EINVAL;
  1377. ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  1378. dcbnl_rtnl_policy);
  1379. if (ret < 0)
  1380. return ret;
  1381. if (dcb->cmd > DCB_CMD_MAX)
  1382. return -EINVAL;
  1383. /* check if a reply function has been defined for the command */
  1384. fn = &reply_funcs[dcb->cmd];
  1385. if (!fn->cb)
  1386. return -EOPNOTSUPP;
  1387. if (!tb[DCB_ATTR_IFNAME])
  1388. return -EINVAL;
  1389. netdev = dev_get_by_name(&init_net, nla_data(tb[DCB_ATTR_IFNAME]));
  1390. if (!netdev)
  1391. return -ENODEV;
  1392. if (!netdev->dcbnl_ops) {
  1393. ret = -EOPNOTSUPP;
  1394. goto out;
  1395. }
  1396. reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, pid, nlh->nlmsg_seq,
  1397. nlh->nlmsg_flags, &reply_nlh);
  1398. if (!reply_skb) {
  1399. ret = -ENOBUFS;
  1400. goto out;
  1401. }
  1402. ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
  1403. if (ret < 0) {
  1404. nlmsg_free(reply_skb);
  1405. goto out;
  1406. }
  1407. nlmsg_end(reply_skb, reply_nlh);
  1408. ret = rtnl_unicast(reply_skb, &init_net, pid);
  1409. out:
  1410. dev_put(netdev);
  1411. return ret;
  1412. }
  1413. static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
  1414. int ifindex, int prio)
  1415. {
  1416. struct dcb_app_type *itr;
  1417. list_for_each_entry(itr, &dcb_app_list, list) {
  1418. if (itr->app.selector == app->selector &&
  1419. itr->app.protocol == app->protocol &&
  1420. itr->ifindex == ifindex &&
  1421. (!prio || itr->app.priority == prio))
  1422. return itr;
  1423. }
  1424. return NULL;
  1425. }
  1426. static int dcb_app_add(const struct dcb_app *app, int ifindex)
  1427. {
  1428. struct dcb_app_type *entry;
  1429. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1430. if (!entry)
  1431. return -ENOMEM;
  1432. memcpy(&entry->app, app, sizeof(*app));
  1433. entry->ifindex = ifindex;
  1434. list_add(&entry->list, &dcb_app_list);
  1435. return 0;
  1436. }
  1437. /**
  1438. * dcb_getapp - retrieve the DCBX application user priority
  1439. *
  1440. * On success returns a non-zero 802.1p user priority bitmap
  1441. * otherwise returns 0 as the invalid user priority bitmap to
  1442. * indicate an error.
  1443. */
  1444. u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  1445. {
  1446. struct dcb_app_type *itr;
  1447. u8 prio = 0;
  1448. spin_lock(&dcb_lock);
  1449. if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
  1450. prio = itr->app.priority;
  1451. spin_unlock(&dcb_lock);
  1452. return prio;
  1453. }
  1454. EXPORT_SYMBOL(dcb_getapp);
  1455. /**
  1456. * dcb_setapp - add CEE dcb application data to app list
  1457. *
  1458. * Priority 0 is an invalid priority in CEE spec. This routine
  1459. * removes applications from the app list if the priority is
  1460. * set to zero.
  1461. */
  1462. int dcb_setapp(struct net_device *dev, struct dcb_app *new)
  1463. {
  1464. struct dcb_app_type *itr;
  1465. struct dcb_app_type event;
  1466. int err = 0;
  1467. event.ifindex = dev->ifindex;
  1468. memcpy(&event.app, new, sizeof(event.app));
  1469. if (dev->dcbnl_ops->getdcbx)
  1470. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1471. spin_lock(&dcb_lock);
  1472. /* Search for existing match and replace */
  1473. if ((itr = dcb_app_lookup(new, dev->ifindex, 0))) {
  1474. if (new->priority)
  1475. itr->app.priority = new->priority;
  1476. else {
  1477. list_del(&itr->list);
  1478. kfree(itr);
  1479. }
  1480. goto out;
  1481. }
  1482. /* App type does not exist add new application type */
  1483. if (new->priority)
  1484. err = dcb_app_add(new, dev->ifindex);
  1485. out:
  1486. spin_unlock(&dcb_lock);
  1487. if (!err)
  1488. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1489. return err;
  1490. }
  1491. EXPORT_SYMBOL(dcb_setapp);
  1492. /**
  1493. * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
  1494. *
  1495. * Helper routine which on success returns a non-zero 802.1Qaz user
  1496. * priority bitmap otherwise returns 0 to indicate the dcb_app was
  1497. * not found in APP list.
  1498. */
  1499. u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
  1500. {
  1501. struct dcb_app_type *itr;
  1502. u8 prio = 0;
  1503. spin_lock(&dcb_lock);
  1504. if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
  1505. prio |= 1 << itr->app.priority;
  1506. spin_unlock(&dcb_lock);
  1507. return prio;
  1508. }
  1509. EXPORT_SYMBOL(dcb_ieee_getapp_mask);
  1510. /**
  1511. * dcb_ieee_setapp - add IEEE dcb application data to app list
  1512. *
  1513. * This adds Application data to the list. Multiple application
  1514. * entries may exists for the same selector and protocol as long
  1515. * as the priorities are different.
  1516. */
  1517. int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
  1518. {
  1519. struct dcb_app_type event;
  1520. int err = 0;
  1521. event.ifindex = dev->ifindex;
  1522. memcpy(&event.app, new, sizeof(event.app));
  1523. if (dev->dcbnl_ops->getdcbx)
  1524. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1525. spin_lock(&dcb_lock);
  1526. /* Search for existing match and abort if found */
  1527. if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
  1528. err = -EEXIST;
  1529. goto out;
  1530. }
  1531. err = dcb_app_add(new, dev->ifindex);
  1532. out:
  1533. spin_unlock(&dcb_lock);
  1534. if (!err)
  1535. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1536. return err;
  1537. }
  1538. EXPORT_SYMBOL(dcb_ieee_setapp);
  1539. /**
  1540. * dcb_ieee_delapp - delete IEEE dcb application data from list
  1541. *
  1542. * This removes a matching APP data from the APP list
  1543. */
  1544. int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
  1545. {
  1546. struct dcb_app_type *itr;
  1547. struct dcb_app_type event;
  1548. int err = -ENOENT;
  1549. event.ifindex = dev->ifindex;
  1550. memcpy(&event.app, del, sizeof(event.app));
  1551. if (dev->dcbnl_ops->getdcbx)
  1552. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1553. spin_lock(&dcb_lock);
  1554. /* Search for existing match and remove it. */
  1555. if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
  1556. list_del(&itr->list);
  1557. kfree(itr);
  1558. err = 0;
  1559. }
  1560. spin_unlock(&dcb_lock);
  1561. if (!err)
  1562. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1563. return err;
  1564. }
  1565. EXPORT_SYMBOL(dcb_ieee_delapp);
  1566. static void dcb_flushapp(void)
  1567. {
  1568. struct dcb_app_type *app;
  1569. struct dcb_app_type *tmp;
  1570. spin_lock(&dcb_lock);
  1571. list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
  1572. list_del(&app->list);
  1573. kfree(app);
  1574. }
  1575. spin_unlock(&dcb_lock);
  1576. }
  1577. static int __init dcbnl_init(void)
  1578. {
  1579. INIT_LIST_HEAD(&dcb_app_list);
  1580. rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, NULL);
  1581. rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, NULL);
  1582. return 0;
  1583. }
  1584. module_init(dcbnl_init);
  1585. static void __exit dcbnl_exit(void)
  1586. {
  1587. rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
  1588. rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
  1589. dcb_flushapp();
  1590. }
  1591. module_exit(dcbnl_exit);