dcbnl.c 48 KB

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