dcbnl.c 52 KB

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