dcbnl.c 49 KB

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