dcbnl.c 43 KB

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