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