dcbnl.c 49 KB

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