dcbnl.c 49 KB

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