dcbnl.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. /* Data Center Bridging (DCB) is a collection of Ethernet enhancements
  30. * intended to allow network traffic with differing requirements
  31. * (highly reliable, no drops vs. best effort vs. low latency) to operate
  32. * and co-exist on Ethernet. Current DCB features are:
  33. *
  34. * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
  35. * framework for assigning bandwidth guarantees to traffic classes.
  36. *
  37. * Priority-based Flow Control (PFC) - provides a flow control mechanism which
  38. * can work independently for each 802.1p priority.
  39. *
  40. * Congestion Notification - provides a mechanism for end-to-end congestion
  41. * control for protocols which do not have built-in congestion management.
  42. *
  43. * More information about the emerging standards for these Ethernet features
  44. * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
  45. *
  46. * This file implements an rtnetlink interface to allow configuration of DCB
  47. * features for capable devices.
  48. */
  49. MODULE_AUTHOR("Lucy Liu, <lucy.liu@intel.com>");
  50. MODULE_DESCRIPTION("Data Center Bridging netlink interface");
  51. MODULE_LICENSE("GPL");
  52. /**************** DCB attribute policies *************************************/
  53. /* DCB netlink attributes policy */
  54. static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
  55. [DCB_ATTR_IFNAME] = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
  56. [DCB_ATTR_STATE] = {.type = NLA_U8},
  57. [DCB_ATTR_PFC_CFG] = {.type = NLA_NESTED},
  58. [DCB_ATTR_PG_CFG] = {.type = NLA_NESTED},
  59. [DCB_ATTR_SET_ALL] = {.type = NLA_U8},
  60. [DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
  61. [DCB_ATTR_CAP] = {.type = NLA_NESTED},
  62. [DCB_ATTR_PFC_STATE] = {.type = NLA_U8},
  63. [DCB_ATTR_BCN] = {.type = NLA_NESTED},
  64. [DCB_ATTR_APP] = {.type = NLA_NESTED},
  65. [DCB_ATTR_IEEE] = {.type = NLA_NESTED},
  66. [DCB_ATTR_DCBX] = {.type = NLA_U8},
  67. [DCB_ATTR_FEATCFG] = {.type = NLA_NESTED},
  68. };
  69. /* DCB priority flow control to User Priority nested attributes */
  70. static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
  71. [DCB_PFC_UP_ATTR_0] = {.type = NLA_U8},
  72. [DCB_PFC_UP_ATTR_1] = {.type = NLA_U8},
  73. [DCB_PFC_UP_ATTR_2] = {.type = NLA_U8},
  74. [DCB_PFC_UP_ATTR_3] = {.type = NLA_U8},
  75. [DCB_PFC_UP_ATTR_4] = {.type = NLA_U8},
  76. [DCB_PFC_UP_ATTR_5] = {.type = NLA_U8},
  77. [DCB_PFC_UP_ATTR_6] = {.type = NLA_U8},
  78. [DCB_PFC_UP_ATTR_7] = {.type = NLA_U8},
  79. [DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
  80. };
  81. /* DCB priority grouping nested attributes */
  82. static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
  83. [DCB_PG_ATTR_TC_0] = {.type = NLA_NESTED},
  84. [DCB_PG_ATTR_TC_1] = {.type = NLA_NESTED},
  85. [DCB_PG_ATTR_TC_2] = {.type = NLA_NESTED},
  86. [DCB_PG_ATTR_TC_3] = {.type = NLA_NESTED},
  87. [DCB_PG_ATTR_TC_4] = {.type = NLA_NESTED},
  88. [DCB_PG_ATTR_TC_5] = {.type = NLA_NESTED},
  89. [DCB_PG_ATTR_TC_6] = {.type = NLA_NESTED},
  90. [DCB_PG_ATTR_TC_7] = {.type = NLA_NESTED},
  91. [DCB_PG_ATTR_TC_ALL] = {.type = NLA_NESTED},
  92. [DCB_PG_ATTR_BW_ID_0] = {.type = NLA_U8},
  93. [DCB_PG_ATTR_BW_ID_1] = {.type = NLA_U8},
  94. [DCB_PG_ATTR_BW_ID_2] = {.type = NLA_U8},
  95. [DCB_PG_ATTR_BW_ID_3] = {.type = NLA_U8},
  96. [DCB_PG_ATTR_BW_ID_4] = {.type = NLA_U8},
  97. [DCB_PG_ATTR_BW_ID_5] = {.type = NLA_U8},
  98. [DCB_PG_ATTR_BW_ID_6] = {.type = NLA_U8},
  99. [DCB_PG_ATTR_BW_ID_7] = {.type = NLA_U8},
  100. [DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
  101. };
  102. /* DCB traffic class nested attributes. */
  103. static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
  104. [DCB_TC_ATTR_PARAM_PGID] = {.type = NLA_U8},
  105. [DCB_TC_ATTR_PARAM_UP_MAPPING] = {.type = NLA_U8},
  106. [DCB_TC_ATTR_PARAM_STRICT_PRIO] = {.type = NLA_U8},
  107. [DCB_TC_ATTR_PARAM_BW_PCT] = {.type = NLA_U8},
  108. [DCB_TC_ATTR_PARAM_ALL] = {.type = NLA_FLAG},
  109. };
  110. /* DCB capabilities nested attributes. */
  111. static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
  112. [DCB_CAP_ATTR_ALL] = {.type = NLA_FLAG},
  113. [DCB_CAP_ATTR_PG] = {.type = NLA_U8},
  114. [DCB_CAP_ATTR_PFC] = {.type = NLA_U8},
  115. [DCB_CAP_ATTR_UP2TC] = {.type = NLA_U8},
  116. [DCB_CAP_ATTR_PG_TCS] = {.type = NLA_U8},
  117. [DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
  118. [DCB_CAP_ATTR_GSP] = {.type = NLA_U8},
  119. [DCB_CAP_ATTR_BCN] = {.type = NLA_U8},
  120. [DCB_CAP_ATTR_DCBX] = {.type = NLA_U8},
  121. };
  122. /* DCB capabilities nested attributes. */
  123. static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
  124. [DCB_NUMTCS_ATTR_ALL] = {.type = NLA_FLAG},
  125. [DCB_NUMTCS_ATTR_PG] = {.type = NLA_U8},
  126. [DCB_NUMTCS_ATTR_PFC] = {.type = NLA_U8},
  127. };
  128. /* DCB BCN nested attributes. */
  129. static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
  130. [DCB_BCN_ATTR_RP_0] = {.type = NLA_U8},
  131. [DCB_BCN_ATTR_RP_1] = {.type = NLA_U8},
  132. [DCB_BCN_ATTR_RP_2] = {.type = NLA_U8},
  133. [DCB_BCN_ATTR_RP_3] = {.type = NLA_U8},
  134. [DCB_BCN_ATTR_RP_4] = {.type = NLA_U8},
  135. [DCB_BCN_ATTR_RP_5] = {.type = NLA_U8},
  136. [DCB_BCN_ATTR_RP_6] = {.type = NLA_U8},
  137. [DCB_BCN_ATTR_RP_7] = {.type = NLA_U8},
  138. [DCB_BCN_ATTR_RP_ALL] = {.type = NLA_FLAG},
  139. [DCB_BCN_ATTR_BCNA_0] = {.type = NLA_U32},
  140. [DCB_BCN_ATTR_BCNA_1] = {.type = NLA_U32},
  141. [DCB_BCN_ATTR_ALPHA] = {.type = NLA_U32},
  142. [DCB_BCN_ATTR_BETA] = {.type = NLA_U32},
  143. [DCB_BCN_ATTR_GD] = {.type = NLA_U32},
  144. [DCB_BCN_ATTR_GI] = {.type = NLA_U32},
  145. [DCB_BCN_ATTR_TMAX] = {.type = NLA_U32},
  146. [DCB_BCN_ATTR_TD] = {.type = NLA_U32},
  147. [DCB_BCN_ATTR_RMIN] = {.type = NLA_U32},
  148. [DCB_BCN_ATTR_W] = {.type = NLA_U32},
  149. [DCB_BCN_ATTR_RD] = {.type = NLA_U32},
  150. [DCB_BCN_ATTR_RU] = {.type = NLA_U32},
  151. [DCB_BCN_ATTR_WRTT] = {.type = NLA_U32},
  152. [DCB_BCN_ATTR_RI] = {.type = NLA_U32},
  153. [DCB_BCN_ATTR_C] = {.type = NLA_U32},
  154. [DCB_BCN_ATTR_ALL] = {.type = NLA_FLAG},
  155. };
  156. /* DCB APP nested attributes. */
  157. static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
  158. [DCB_APP_ATTR_IDTYPE] = {.type = NLA_U8},
  159. [DCB_APP_ATTR_ID] = {.type = NLA_U16},
  160. [DCB_APP_ATTR_PRIORITY] = {.type = NLA_U8},
  161. };
  162. /* IEEE 802.1Qaz nested attributes. */
  163. static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
  164. [DCB_ATTR_IEEE_ETS] = {.len = sizeof(struct ieee_ets)},
  165. [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
  166. [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
  167. [DCB_ATTR_IEEE_MAXRATE] = {.len = sizeof(struct ieee_maxrate)},
  168. };
  169. static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
  170. [DCB_ATTR_IEEE_APP] = {.len = sizeof(struct dcb_app)},
  171. };
  172. /* DCB number of traffic classes nested attributes. */
  173. static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
  174. [DCB_FEATCFG_ATTR_ALL] = {.type = NLA_FLAG},
  175. [DCB_FEATCFG_ATTR_PG] = {.type = NLA_U8},
  176. [DCB_FEATCFG_ATTR_PFC] = {.type = NLA_U8},
  177. [DCB_FEATCFG_ATTR_APP] = {.type = NLA_U8},
  178. };
  179. static LIST_HEAD(dcb_app_list);
  180. static DEFINE_SPINLOCK(dcb_lock);
  181. static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
  182. u32 flags, struct nlmsghdr **nlhp)
  183. {
  184. struct sk_buff *skb;
  185. struct dcbmsg *dcb;
  186. struct nlmsghdr *nlh;
  187. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  188. if (!skb)
  189. return NULL;
  190. nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
  191. BUG_ON(!nlh);
  192. dcb = nlmsg_data(nlh);
  193. dcb->dcb_family = AF_UNSPEC;
  194. dcb->cmd = cmd;
  195. dcb->dcb_pad = 0;
  196. if (nlhp)
  197. *nlhp = nlh;
  198. return skb;
  199. }
  200. static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
  201. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  202. {
  203. /* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
  204. if (!netdev->dcbnl_ops->getstate)
  205. return -EOPNOTSUPP;
  206. return nla_put_u8(skb, DCB_ATTR_STATE,
  207. netdev->dcbnl_ops->getstate(netdev));
  208. }
  209. static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
  210. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  211. {
  212. struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
  213. u8 value;
  214. int ret;
  215. int i;
  216. int getall = 0;
  217. if (!tb[DCB_ATTR_PFC_CFG])
  218. return -EINVAL;
  219. if (!netdev->dcbnl_ops->getpfccfg)
  220. return -EOPNOTSUPP;
  221. ret = nla_parse_nested(data, DCB_PFC_UP_ATTR_MAX,
  222. tb[DCB_ATTR_PFC_CFG],
  223. dcbnl_pfc_up_nest);
  224. if (ret)
  225. return ret;
  226. nest = nla_nest_start(skb, DCB_ATTR_PFC_CFG);
  227. if (!nest)
  228. return -EMSGSIZE;
  229. if (data[DCB_PFC_UP_ATTR_ALL])
  230. getall = 1;
  231. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  232. if (!getall && !data[i])
  233. continue;
  234. netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
  235. &value);
  236. ret = nla_put_u8(skb, i, value);
  237. if (ret) {
  238. nla_nest_cancel(skb, nest);
  239. return ret;
  240. }
  241. }
  242. nla_nest_end(skb, nest);
  243. return 0;
  244. }
  245. static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
  246. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  247. {
  248. u8 perm_addr[MAX_ADDR_LEN];
  249. if (!netdev->dcbnl_ops->getpermhwaddr)
  250. return -EOPNOTSUPP;
  251. memset(perm_addr, 0, sizeof(perm_addr));
  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. memset(&ets, 0, sizeof(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. memset(&maxrate, 0, sizeof(maxrate));
  871. err = ops->ieee_getmaxrate(netdev, &maxrate);
  872. if (!err) {
  873. err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
  874. sizeof(maxrate), &maxrate);
  875. if (err)
  876. return -EMSGSIZE;
  877. }
  878. }
  879. if (ops->ieee_getpfc) {
  880. struct ieee_pfc pfc;
  881. memset(&pfc, 0, sizeof(pfc));
  882. err = ops->ieee_getpfc(netdev, &pfc);
  883. if (!err &&
  884. nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
  885. return -EMSGSIZE;
  886. }
  887. app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
  888. if (!app)
  889. return -EMSGSIZE;
  890. spin_lock(&dcb_lock);
  891. list_for_each_entry(itr, &dcb_app_list, list) {
  892. if (itr->ifindex == netdev->ifindex) {
  893. err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
  894. &itr->app);
  895. if (err) {
  896. spin_unlock(&dcb_lock);
  897. return -EMSGSIZE;
  898. }
  899. }
  900. }
  901. if (netdev->dcbnl_ops->getdcbx)
  902. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  903. else
  904. dcbx = -EOPNOTSUPP;
  905. spin_unlock(&dcb_lock);
  906. nla_nest_end(skb, app);
  907. /* get peer info if available */
  908. if (ops->ieee_peer_getets) {
  909. struct ieee_ets ets;
  910. memset(&ets, 0, sizeof(ets));
  911. err = ops->ieee_peer_getets(netdev, &ets);
  912. if (!err &&
  913. nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
  914. return -EMSGSIZE;
  915. }
  916. if (ops->ieee_peer_getpfc) {
  917. struct ieee_pfc pfc;
  918. memset(&pfc, 0, sizeof(pfc));
  919. err = ops->ieee_peer_getpfc(netdev, &pfc);
  920. if (!err &&
  921. nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
  922. return -EMSGSIZE;
  923. }
  924. if (ops->peer_getappinfo && ops->peer_getapptable) {
  925. err = dcbnl_build_peer_app(netdev, skb,
  926. DCB_ATTR_IEEE_PEER_APP,
  927. DCB_ATTR_IEEE_APP_UNSPEC,
  928. DCB_ATTR_IEEE_APP);
  929. if (err)
  930. return -EMSGSIZE;
  931. }
  932. nla_nest_end(skb, ieee);
  933. if (dcbx >= 0) {
  934. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  935. if (err)
  936. return -EMSGSIZE;
  937. }
  938. return 0;
  939. }
  940. static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
  941. int dir)
  942. {
  943. u8 pgid, up_map, prio, tc_pct;
  944. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  945. int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
  946. struct nlattr *pg = nla_nest_start(skb, i);
  947. if (!pg)
  948. return -EMSGSIZE;
  949. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  950. struct nlattr *tc_nest = nla_nest_start(skb, i);
  951. if (!tc_nest)
  952. return -EMSGSIZE;
  953. pgid = DCB_ATTR_VALUE_UNDEFINED;
  954. prio = DCB_ATTR_VALUE_UNDEFINED;
  955. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  956. up_map = DCB_ATTR_VALUE_UNDEFINED;
  957. if (!dir)
  958. ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
  959. &prio, &pgid, &tc_pct, &up_map);
  960. else
  961. ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
  962. &prio, &pgid, &tc_pct, &up_map);
  963. if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
  964. nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
  965. nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
  966. nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
  967. return -EMSGSIZE;
  968. nla_nest_end(skb, tc_nest);
  969. }
  970. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  971. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  972. if (!dir)
  973. ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
  974. &tc_pct);
  975. else
  976. ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
  977. &tc_pct);
  978. if (nla_put_u8(skb, i, tc_pct))
  979. return -EMSGSIZE;
  980. }
  981. nla_nest_end(skb, pg);
  982. return 0;
  983. }
  984. static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
  985. {
  986. struct nlattr *cee, *app;
  987. struct dcb_app_type *itr;
  988. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  989. int dcbx, i, err = -EMSGSIZE;
  990. u8 value;
  991. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  992. goto nla_put_failure;
  993. cee = nla_nest_start(skb, DCB_ATTR_CEE);
  994. if (!cee)
  995. goto nla_put_failure;
  996. /* local pg */
  997. if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
  998. err = dcbnl_cee_pg_fill(skb, netdev, 1);
  999. if (err)
  1000. goto nla_put_failure;
  1001. }
  1002. if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
  1003. err = dcbnl_cee_pg_fill(skb, netdev, 0);
  1004. if (err)
  1005. goto nla_put_failure;
  1006. }
  1007. /* local pfc */
  1008. if (ops->getpfccfg) {
  1009. struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
  1010. if (!pfc_nest)
  1011. goto nla_put_failure;
  1012. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  1013. ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
  1014. if (nla_put_u8(skb, i, value))
  1015. goto nla_put_failure;
  1016. }
  1017. nla_nest_end(skb, pfc_nest);
  1018. }
  1019. /* local app */
  1020. spin_lock(&dcb_lock);
  1021. app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
  1022. if (!app)
  1023. goto dcb_unlock;
  1024. list_for_each_entry(itr, &dcb_app_list, list) {
  1025. if (itr->ifindex == netdev->ifindex) {
  1026. struct nlattr *app_nest = nla_nest_start(skb,
  1027. DCB_ATTR_APP);
  1028. if (!app_nest)
  1029. goto dcb_unlock;
  1030. err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
  1031. itr->app.selector);
  1032. if (err)
  1033. goto dcb_unlock;
  1034. err = nla_put_u16(skb, DCB_APP_ATTR_ID,
  1035. itr->app.protocol);
  1036. if (err)
  1037. goto dcb_unlock;
  1038. err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
  1039. itr->app.priority);
  1040. if (err)
  1041. goto dcb_unlock;
  1042. nla_nest_end(skb, app_nest);
  1043. }
  1044. }
  1045. nla_nest_end(skb, app);
  1046. if (netdev->dcbnl_ops->getdcbx)
  1047. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1048. else
  1049. dcbx = -EOPNOTSUPP;
  1050. spin_unlock(&dcb_lock);
  1051. /* features flags */
  1052. if (ops->getfeatcfg) {
  1053. struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
  1054. if (!feat)
  1055. goto nla_put_failure;
  1056. for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
  1057. i++)
  1058. if (!ops->getfeatcfg(netdev, i, &value) &&
  1059. nla_put_u8(skb, i, value))
  1060. goto nla_put_failure;
  1061. nla_nest_end(skb, feat);
  1062. }
  1063. /* peer info if available */
  1064. if (ops->cee_peer_getpg) {
  1065. struct cee_pg pg;
  1066. memset(&pg, 0, sizeof(pg));
  1067. err = ops->cee_peer_getpg(netdev, &pg);
  1068. if (!err &&
  1069. nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
  1070. goto nla_put_failure;
  1071. }
  1072. if (ops->cee_peer_getpfc) {
  1073. struct cee_pfc pfc;
  1074. memset(&pfc, 0, sizeof(pfc));
  1075. err = ops->cee_peer_getpfc(netdev, &pfc);
  1076. if (!err &&
  1077. nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
  1078. goto nla_put_failure;
  1079. }
  1080. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1081. err = dcbnl_build_peer_app(netdev, skb,
  1082. DCB_ATTR_CEE_PEER_APP_TABLE,
  1083. DCB_ATTR_CEE_PEER_APP_INFO,
  1084. DCB_ATTR_CEE_PEER_APP);
  1085. if (err)
  1086. goto nla_put_failure;
  1087. }
  1088. nla_nest_end(skb, cee);
  1089. /* DCBX state */
  1090. if (dcbx >= 0) {
  1091. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1092. if (err)
  1093. goto nla_put_failure;
  1094. }
  1095. return 0;
  1096. dcb_unlock:
  1097. spin_unlock(&dcb_lock);
  1098. nla_put_failure:
  1099. return err;
  1100. }
  1101. static int dcbnl_notify(struct net_device *dev, int event, int cmd,
  1102. u32 seq, u32 portid, int dcbx_ver)
  1103. {
  1104. struct net *net = dev_net(dev);
  1105. struct sk_buff *skb;
  1106. struct nlmsghdr *nlh;
  1107. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1108. int err;
  1109. if (!ops)
  1110. return -EOPNOTSUPP;
  1111. skb = dcbnl_newmsg(event, cmd, portid, seq, 0, &nlh);
  1112. if (!skb)
  1113. return -ENOBUFS;
  1114. if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
  1115. err = dcbnl_ieee_fill(skb, dev);
  1116. else
  1117. err = dcbnl_cee_fill(skb, dev);
  1118. if (err < 0) {
  1119. /* Report error to broadcast listeners */
  1120. nlmsg_free(skb);
  1121. rtnl_set_sk_err(net, RTNLGRP_DCB, err);
  1122. } else {
  1123. /* End nlmsg and notify broadcast listeners */
  1124. nlmsg_end(skb, nlh);
  1125. rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
  1126. }
  1127. return err;
  1128. }
  1129. int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
  1130. u32 seq, u32 portid)
  1131. {
  1132. return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_IEEE);
  1133. }
  1134. EXPORT_SYMBOL(dcbnl_ieee_notify);
  1135. int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
  1136. u32 seq, u32 portid)
  1137. {
  1138. return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_CEE);
  1139. }
  1140. EXPORT_SYMBOL(dcbnl_cee_notify);
  1141. /* Handle IEEE 802.1Qaz SET commands. If any requested operation can not
  1142. * be completed the entire msg is aborted and error value is returned.
  1143. * No attempt is made to reconcile the case where only part of the
  1144. * cmd can be completed.
  1145. */
  1146. static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
  1147. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1148. {
  1149. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1150. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1151. int err;
  1152. if (!ops)
  1153. return -EOPNOTSUPP;
  1154. if (!tb[DCB_ATTR_IEEE])
  1155. return -EINVAL;
  1156. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1157. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1158. if (err)
  1159. return err;
  1160. if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  1161. struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  1162. err = ops->ieee_setets(netdev, ets);
  1163. if (err)
  1164. goto err;
  1165. }
  1166. if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
  1167. struct ieee_maxrate *maxrate =
  1168. nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
  1169. err = ops->ieee_setmaxrate(netdev, maxrate);
  1170. if (err)
  1171. goto err;
  1172. }
  1173. if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
  1174. struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  1175. err = ops->ieee_setpfc(netdev, pfc);
  1176. if (err)
  1177. goto err;
  1178. }
  1179. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1180. struct nlattr *attr;
  1181. int rem;
  1182. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1183. struct dcb_app *app_data;
  1184. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1185. continue;
  1186. app_data = nla_data(attr);
  1187. if (ops->ieee_setapp)
  1188. err = ops->ieee_setapp(netdev, app_data);
  1189. else
  1190. err = dcb_ieee_setapp(netdev, app_data);
  1191. if (err)
  1192. goto err;
  1193. }
  1194. }
  1195. err:
  1196. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1197. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
  1198. return err;
  1199. }
  1200. static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1201. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1202. {
  1203. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1204. if (!ops)
  1205. return -EOPNOTSUPP;
  1206. return dcbnl_ieee_fill(skb, netdev);
  1207. }
  1208. static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
  1209. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1210. {
  1211. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1212. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1213. int err;
  1214. if (!ops)
  1215. return -EOPNOTSUPP;
  1216. if (!tb[DCB_ATTR_IEEE])
  1217. return -EINVAL;
  1218. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX,
  1219. tb[DCB_ATTR_IEEE], dcbnl_ieee_policy);
  1220. if (err)
  1221. return err;
  1222. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1223. struct nlattr *attr;
  1224. int rem;
  1225. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1226. struct dcb_app *app_data;
  1227. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1228. continue;
  1229. app_data = nla_data(attr);
  1230. if (ops->ieee_delapp)
  1231. err = ops->ieee_delapp(netdev, app_data);
  1232. else
  1233. err = dcb_ieee_delapp(netdev, app_data);
  1234. if (err)
  1235. goto err;
  1236. }
  1237. }
  1238. err:
  1239. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1240. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
  1241. return err;
  1242. }
  1243. /* DCBX configuration */
  1244. static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1245. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1246. {
  1247. if (!netdev->dcbnl_ops->getdcbx)
  1248. return -EOPNOTSUPP;
  1249. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1250. netdev->dcbnl_ops->getdcbx(netdev));
  1251. }
  1252. static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1253. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1254. {
  1255. u8 value;
  1256. if (!netdev->dcbnl_ops->setdcbx)
  1257. return -EOPNOTSUPP;
  1258. if (!tb[DCB_ATTR_DCBX])
  1259. return -EINVAL;
  1260. value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  1261. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1262. netdev->dcbnl_ops->setdcbx(netdev, value));
  1263. }
  1264. static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1265. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1266. {
  1267. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  1268. u8 value;
  1269. int ret, i;
  1270. int getall = 0;
  1271. if (!netdev->dcbnl_ops->getfeatcfg)
  1272. return -EOPNOTSUPP;
  1273. if (!tb[DCB_ATTR_FEATCFG])
  1274. return -EINVAL;
  1275. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1276. dcbnl_featcfg_nest);
  1277. if (ret)
  1278. return ret;
  1279. nest = nla_nest_start(skb, DCB_ATTR_FEATCFG);
  1280. if (!nest)
  1281. return -EMSGSIZE;
  1282. if (data[DCB_FEATCFG_ATTR_ALL])
  1283. getall = 1;
  1284. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1285. if (!getall && !data[i])
  1286. continue;
  1287. ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
  1288. if (!ret)
  1289. ret = nla_put_u8(skb, i, value);
  1290. if (ret) {
  1291. nla_nest_cancel(skb, nest);
  1292. goto nla_put_failure;
  1293. }
  1294. }
  1295. nla_nest_end(skb, nest);
  1296. nla_put_failure:
  1297. return ret;
  1298. }
  1299. static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1300. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1301. {
  1302. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
  1303. int ret, i;
  1304. u8 value;
  1305. if (!netdev->dcbnl_ops->setfeatcfg)
  1306. return -ENOTSUPP;
  1307. if (!tb[DCB_ATTR_FEATCFG])
  1308. return -EINVAL;
  1309. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX, tb[DCB_ATTR_FEATCFG],
  1310. dcbnl_featcfg_nest);
  1311. if (ret)
  1312. goto err;
  1313. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1314. if (data[i] == NULL)
  1315. continue;
  1316. value = nla_get_u8(data[i]);
  1317. ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  1318. if (ret)
  1319. goto err;
  1320. }
  1321. err:
  1322. ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
  1323. return ret;
  1324. }
  1325. /* Handle CEE DCBX GET commands. */
  1326. static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1327. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1328. {
  1329. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1330. if (!ops)
  1331. return -EOPNOTSUPP;
  1332. return dcbnl_cee_fill(skb, netdev);
  1333. }
  1334. struct reply_func {
  1335. /* reply netlink message type */
  1336. int type;
  1337. /* function to fill message contents */
  1338. int (*cb)(struct net_device *, struct nlmsghdr *, u32,
  1339. struct nlattr **, struct sk_buff *);
  1340. };
  1341. static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
  1342. [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
  1343. [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
  1344. [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
  1345. [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
  1346. [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
  1347. [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
  1348. [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
  1349. [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
  1350. [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
  1351. [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
  1352. [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
  1353. [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
  1354. [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
  1355. [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
  1356. [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
  1357. [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
  1358. [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
  1359. [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
  1360. [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
  1361. [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
  1362. [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
  1363. [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
  1364. [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
  1365. [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
  1366. [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
  1367. [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
  1368. [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
  1369. };
  1370. static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  1371. {
  1372. struct net *net = sock_net(skb->sk);
  1373. struct net_device *netdev;
  1374. struct dcbmsg *dcb = nlmsg_data(nlh);
  1375. struct nlattr *tb[DCB_ATTR_MAX + 1];
  1376. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  1377. int ret = -EINVAL;
  1378. struct sk_buff *reply_skb;
  1379. struct nlmsghdr *reply_nlh = NULL;
  1380. const struct reply_func *fn;
  1381. if ((nlh->nlmsg_type == RTM_SETDCB) && !capable(CAP_NET_ADMIN))
  1382. return -EPERM;
  1383. ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  1384. dcbnl_rtnl_policy);
  1385. if (ret < 0)
  1386. return ret;
  1387. if (dcb->cmd > DCB_CMD_MAX)
  1388. return -EINVAL;
  1389. /* check if a reply function has been defined for the command */
  1390. fn = &reply_funcs[dcb->cmd];
  1391. if (!fn->cb)
  1392. return -EOPNOTSUPP;
  1393. if (!tb[DCB_ATTR_IFNAME])
  1394. return -EINVAL;
  1395. netdev = dev_get_by_name(net, nla_data(tb[DCB_ATTR_IFNAME]));
  1396. if (!netdev)
  1397. return -ENODEV;
  1398. if (!netdev->dcbnl_ops) {
  1399. ret = -EOPNOTSUPP;
  1400. goto out;
  1401. }
  1402. reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, portid, nlh->nlmsg_seq,
  1403. nlh->nlmsg_flags, &reply_nlh);
  1404. if (!reply_skb) {
  1405. ret = -ENOBUFS;
  1406. goto out;
  1407. }
  1408. ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
  1409. if (ret < 0) {
  1410. nlmsg_free(reply_skb);
  1411. goto out;
  1412. }
  1413. nlmsg_end(reply_skb, reply_nlh);
  1414. ret = rtnl_unicast(reply_skb, net, portid);
  1415. out:
  1416. dev_put(netdev);
  1417. return ret;
  1418. }
  1419. static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
  1420. int ifindex, int prio)
  1421. {
  1422. struct dcb_app_type *itr;
  1423. list_for_each_entry(itr, &dcb_app_list, list) {
  1424. if (itr->app.selector == app->selector &&
  1425. itr->app.protocol == app->protocol &&
  1426. itr->ifindex == ifindex &&
  1427. (!prio || itr->app.priority == prio))
  1428. return itr;
  1429. }
  1430. return NULL;
  1431. }
  1432. static int dcb_app_add(const struct dcb_app *app, int ifindex)
  1433. {
  1434. struct dcb_app_type *entry;
  1435. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1436. if (!entry)
  1437. return -ENOMEM;
  1438. memcpy(&entry->app, app, sizeof(*app));
  1439. entry->ifindex = ifindex;
  1440. list_add(&entry->list, &dcb_app_list);
  1441. return 0;
  1442. }
  1443. /**
  1444. * dcb_getapp - retrieve the DCBX application user priority
  1445. *
  1446. * On success returns a non-zero 802.1p user priority bitmap
  1447. * otherwise returns 0 as the invalid user priority bitmap to
  1448. * indicate an error.
  1449. */
  1450. u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  1451. {
  1452. struct dcb_app_type *itr;
  1453. u8 prio = 0;
  1454. spin_lock(&dcb_lock);
  1455. if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
  1456. prio = itr->app.priority;
  1457. spin_unlock(&dcb_lock);
  1458. return prio;
  1459. }
  1460. EXPORT_SYMBOL(dcb_getapp);
  1461. /**
  1462. * dcb_setapp - add CEE dcb application data to app list
  1463. *
  1464. * Priority 0 is an invalid priority in CEE spec. This routine
  1465. * removes applications from the app list if the priority is
  1466. * set to zero.
  1467. */
  1468. int dcb_setapp(struct net_device *dev, struct dcb_app *new)
  1469. {
  1470. struct dcb_app_type *itr;
  1471. struct dcb_app_type event;
  1472. int err = 0;
  1473. event.ifindex = dev->ifindex;
  1474. memcpy(&event.app, new, sizeof(event.app));
  1475. if (dev->dcbnl_ops->getdcbx)
  1476. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1477. spin_lock(&dcb_lock);
  1478. /* Search for existing match and replace */
  1479. if ((itr = dcb_app_lookup(new, dev->ifindex, 0))) {
  1480. if (new->priority)
  1481. itr->app.priority = new->priority;
  1482. else {
  1483. list_del(&itr->list);
  1484. kfree(itr);
  1485. }
  1486. goto out;
  1487. }
  1488. /* App type does not exist add new application type */
  1489. if (new->priority)
  1490. err = dcb_app_add(new, dev->ifindex);
  1491. out:
  1492. spin_unlock(&dcb_lock);
  1493. if (!err)
  1494. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1495. return err;
  1496. }
  1497. EXPORT_SYMBOL(dcb_setapp);
  1498. /**
  1499. * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
  1500. *
  1501. * Helper routine which on success returns a non-zero 802.1Qaz user
  1502. * priority bitmap otherwise returns 0 to indicate the dcb_app was
  1503. * not found in APP list.
  1504. */
  1505. u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
  1506. {
  1507. struct dcb_app_type *itr;
  1508. u8 prio = 0;
  1509. spin_lock(&dcb_lock);
  1510. if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
  1511. prio |= 1 << itr->app.priority;
  1512. spin_unlock(&dcb_lock);
  1513. return prio;
  1514. }
  1515. EXPORT_SYMBOL(dcb_ieee_getapp_mask);
  1516. /**
  1517. * dcb_ieee_setapp - add IEEE dcb application data to app list
  1518. *
  1519. * This adds Application data to the list. Multiple application
  1520. * entries may exists for the same selector and protocol as long
  1521. * as the priorities are different.
  1522. */
  1523. int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
  1524. {
  1525. struct dcb_app_type event;
  1526. int err = 0;
  1527. event.ifindex = dev->ifindex;
  1528. memcpy(&event.app, new, sizeof(event.app));
  1529. if (dev->dcbnl_ops->getdcbx)
  1530. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1531. spin_lock(&dcb_lock);
  1532. /* Search for existing match and abort if found */
  1533. if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
  1534. err = -EEXIST;
  1535. goto out;
  1536. }
  1537. err = dcb_app_add(new, dev->ifindex);
  1538. out:
  1539. spin_unlock(&dcb_lock);
  1540. if (!err)
  1541. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1542. return err;
  1543. }
  1544. EXPORT_SYMBOL(dcb_ieee_setapp);
  1545. /**
  1546. * dcb_ieee_delapp - delete IEEE dcb application data from list
  1547. *
  1548. * This removes a matching APP data from the APP list
  1549. */
  1550. int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
  1551. {
  1552. struct dcb_app_type *itr;
  1553. struct dcb_app_type event;
  1554. int err = -ENOENT;
  1555. event.ifindex = dev->ifindex;
  1556. memcpy(&event.app, del, sizeof(event.app));
  1557. if (dev->dcbnl_ops->getdcbx)
  1558. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1559. spin_lock(&dcb_lock);
  1560. /* Search for existing match and remove it. */
  1561. if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
  1562. list_del(&itr->list);
  1563. kfree(itr);
  1564. err = 0;
  1565. }
  1566. spin_unlock(&dcb_lock);
  1567. if (!err)
  1568. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1569. return err;
  1570. }
  1571. EXPORT_SYMBOL(dcb_ieee_delapp);
  1572. static void dcb_flushapp(void)
  1573. {
  1574. struct dcb_app_type *app;
  1575. struct dcb_app_type *tmp;
  1576. spin_lock(&dcb_lock);
  1577. list_for_each_entry_safe(app, tmp, &dcb_app_list, list) {
  1578. list_del(&app->list);
  1579. kfree(app);
  1580. }
  1581. spin_unlock(&dcb_lock);
  1582. }
  1583. static int __init dcbnl_init(void)
  1584. {
  1585. INIT_LIST_HEAD(&dcb_app_list);
  1586. rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, NULL);
  1587. rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, NULL);
  1588. return 0;
  1589. }
  1590. module_init(dcbnl_init);
  1591. static void __exit dcbnl_exit(void)
  1592. {
  1593. rtnl_unregister(PF_UNSPEC, RTM_GETDCB);
  1594. rtnl_unregister(PF_UNSPEC, RTM_SETDCB);
  1595. dcb_flushapp();
  1596. }
  1597. module_exit(dcbnl_exit);