dcbnl.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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(sizeof(struct dcb_app) * app_count, GFP_KERNEL);
  817. if (!table)
  818. return -ENOMEM;
  819. err = ops->peer_getapptable(netdev, table);
  820. }
  821. if (!err) {
  822. u16 i;
  823. struct nlattr *app;
  824. /**
  825. * build the message, from here on the only possible failure
  826. * is due to the skb size
  827. */
  828. err = -EMSGSIZE;
  829. app = nla_nest_start(skb, app_nested_type);
  830. if (!app)
  831. goto nla_put_failure;
  832. if (app_info_type &&
  833. nla_put(skb, app_info_type, sizeof(info), &info))
  834. goto nla_put_failure;
  835. for (i = 0; i < app_count; i++) {
  836. if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
  837. &table[i]))
  838. goto nla_put_failure;
  839. }
  840. nla_nest_end(skb, app);
  841. }
  842. err = 0;
  843. nla_put_failure:
  844. kfree(table);
  845. return err;
  846. }
  847. /* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb GET commands. */
  848. static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
  849. {
  850. struct nlattr *ieee, *app;
  851. struct dcb_app_type *itr;
  852. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  853. int dcbx;
  854. int err;
  855. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  856. return -EMSGSIZE;
  857. ieee = nla_nest_start(skb, DCB_ATTR_IEEE);
  858. if (!ieee)
  859. return -EMSGSIZE;
  860. if (ops->ieee_getets) {
  861. struct ieee_ets ets;
  862. memset(&ets, 0, sizeof(ets));
  863. err = ops->ieee_getets(netdev, &ets);
  864. if (!err &&
  865. nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
  866. return -EMSGSIZE;
  867. }
  868. if (ops->ieee_getmaxrate) {
  869. struct ieee_maxrate maxrate;
  870. memset(&maxrate, 0, sizeof(maxrate));
  871. err = ops->ieee_getmaxrate(netdev, &maxrate);
  872. if (!err) {
  873. err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
  874. sizeof(maxrate), &maxrate);
  875. if (err)
  876. return -EMSGSIZE;
  877. }
  878. }
  879. if (ops->ieee_getqcn) {
  880. struct ieee_qcn qcn;
  881. memset(&qcn, 0, sizeof(qcn));
  882. err = ops->ieee_getqcn(netdev, &qcn);
  883. if (!err) {
  884. err = nla_put(skb, DCB_ATTR_IEEE_QCN,
  885. sizeof(qcn), &qcn);
  886. if (err)
  887. return -EMSGSIZE;
  888. }
  889. }
  890. if (ops->ieee_getqcnstats) {
  891. struct ieee_qcn_stats qcn_stats;
  892. memset(&qcn_stats, 0, sizeof(qcn_stats));
  893. err = ops->ieee_getqcnstats(netdev, &qcn_stats);
  894. if (!err) {
  895. err = nla_put(skb, DCB_ATTR_IEEE_QCN_STATS,
  896. sizeof(qcn_stats), &qcn_stats);
  897. if (err)
  898. return -EMSGSIZE;
  899. }
  900. }
  901. if (ops->ieee_getpfc) {
  902. struct ieee_pfc pfc;
  903. memset(&pfc, 0, sizeof(pfc));
  904. err = ops->ieee_getpfc(netdev, &pfc);
  905. if (!err &&
  906. nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
  907. return -EMSGSIZE;
  908. }
  909. if (ops->dcbnl_getbuffer) {
  910. struct dcbnl_buffer buffer;
  911. memset(&buffer, 0, sizeof(buffer));
  912. err = ops->dcbnl_getbuffer(netdev, &buffer);
  913. if (!err &&
  914. nla_put(skb, DCB_ATTR_DCB_BUFFER, sizeof(buffer), &buffer))
  915. return -EMSGSIZE;
  916. }
  917. app = nla_nest_start(skb, DCB_ATTR_IEEE_APP_TABLE);
  918. if (!app)
  919. return -EMSGSIZE;
  920. spin_lock_bh(&dcb_lock);
  921. list_for_each_entry(itr, &dcb_app_list, list) {
  922. if (itr->ifindex == netdev->ifindex) {
  923. err = nla_put(skb, DCB_ATTR_IEEE_APP, sizeof(itr->app),
  924. &itr->app);
  925. if (err) {
  926. spin_unlock_bh(&dcb_lock);
  927. return -EMSGSIZE;
  928. }
  929. }
  930. }
  931. if (netdev->dcbnl_ops->getdcbx)
  932. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  933. else
  934. dcbx = -EOPNOTSUPP;
  935. spin_unlock_bh(&dcb_lock);
  936. nla_nest_end(skb, app);
  937. /* get peer info if available */
  938. if (ops->ieee_peer_getets) {
  939. struct ieee_ets ets;
  940. memset(&ets, 0, sizeof(ets));
  941. err = ops->ieee_peer_getets(netdev, &ets);
  942. if (!err &&
  943. nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
  944. return -EMSGSIZE;
  945. }
  946. if (ops->ieee_peer_getpfc) {
  947. struct ieee_pfc pfc;
  948. memset(&pfc, 0, sizeof(pfc));
  949. err = ops->ieee_peer_getpfc(netdev, &pfc);
  950. if (!err &&
  951. nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
  952. return -EMSGSIZE;
  953. }
  954. if (ops->peer_getappinfo && ops->peer_getapptable) {
  955. err = dcbnl_build_peer_app(netdev, skb,
  956. DCB_ATTR_IEEE_PEER_APP,
  957. DCB_ATTR_IEEE_APP_UNSPEC,
  958. DCB_ATTR_IEEE_APP);
  959. if (err)
  960. return -EMSGSIZE;
  961. }
  962. nla_nest_end(skb, ieee);
  963. if (dcbx >= 0) {
  964. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  965. if (err)
  966. return -EMSGSIZE;
  967. }
  968. return 0;
  969. }
  970. static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
  971. int dir)
  972. {
  973. u8 pgid, up_map, prio, tc_pct;
  974. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  975. int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
  976. struct nlattr *pg = nla_nest_start(skb, i);
  977. if (!pg)
  978. return -EMSGSIZE;
  979. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  980. struct nlattr *tc_nest = nla_nest_start(skb, i);
  981. if (!tc_nest)
  982. return -EMSGSIZE;
  983. pgid = DCB_ATTR_VALUE_UNDEFINED;
  984. prio = DCB_ATTR_VALUE_UNDEFINED;
  985. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  986. up_map = DCB_ATTR_VALUE_UNDEFINED;
  987. if (!dir)
  988. ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
  989. &prio, &pgid, &tc_pct, &up_map);
  990. else
  991. ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
  992. &prio, &pgid, &tc_pct, &up_map);
  993. if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
  994. nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
  995. nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
  996. nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
  997. return -EMSGSIZE;
  998. nla_nest_end(skb, tc_nest);
  999. }
  1000. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  1001. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  1002. if (!dir)
  1003. ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
  1004. &tc_pct);
  1005. else
  1006. ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
  1007. &tc_pct);
  1008. if (nla_put_u8(skb, i, tc_pct))
  1009. return -EMSGSIZE;
  1010. }
  1011. nla_nest_end(skb, pg);
  1012. return 0;
  1013. }
  1014. static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
  1015. {
  1016. struct nlattr *cee, *app;
  1017. struct dcb_app_type *itr;
  1018. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1019. int dcbx, i, err = -EMSGSIZE;
  1020. u8 value;
  1021. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  1022. goto nla_put_failure;
  1023. cee = nla_nest_start(skb, DCB_ATTR_CEE);
  1024. if (!cee)
  1025. goto nla_put_failure;
  1026. /* local pg */
  1027. if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
  1028. err = dcbnl_cee_pg_fill(skb, netdev, 1);
  1029. if (err)
  1030. goto nla_put_failure;
  1031. }
  1032. if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
  1033. err = dcbnl_cee_pg_fill(skb, netdev, 0);
  1034. if (err)
  1035. goto nla_put_failure;
  1036. }
  1037. /* local pfc */
  1038. if (ops->getpfccfg) {
  1039. struct nlattr *pfc_nest = nla_nest_start(skb, DCB_ATTR_CEE_PFC);
  1040. if (!pfc_nest)
  1041. goto nla_put_failure;
  1042. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  1043. ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
  1044. if (nla_put_u8(skb, i, value))
  1045. goto nla_put_failure;
  1046. }
  1047. nla_nest_end(skb, pfc_nest);
  1048. }
  1049. /* local app */
  1050. spin_lock_bh(&dcb_lock);
  1051. app = nla_nest_start(skb, DCB_ATTR_CEE_APP_TABLE);
  1052. if (!app)
  1053. goto dcb_unlock;
  1054. list_for_each_entry(itr, &dcb_app_list, list) {
  1055. if (itr->ifindex == netdev->ifindex) {
  1056. struct nlattr *app_nest = nla_nest_start(skb,
  1057. DCB_ATTR_APP);
  1058. if (!app_nest)
  1059. goto dcb_unlock;
  1060. err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
  1061. itr->app.selector);
  1062. if (err)
  1063. goto dcb_unlock;
  1064. err = nla_put_u16(skb, DCB_APP_ATTR_ID,
  1065. itr->app.protocol);
  1066. if (err)
  1067. goto dcb_unlock;
  1068. err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
  1069. itr->app.priority);
  1070. if (err)
  1071. goto dcb_unlock;
  1072. nla_nest_end(skb, app_nest);
  1073. }
  1074. }
  1075. nla_nest_end(skb, app);
  1076. if (netdev->dcbnl_ops->getdcbx)
  1077. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1078. else
  1079. dcbx = -EOPNOTSUPP;
  1080. spin_unlock_bh(&dcb_lock);
  1081. /* features flags */
  1082. if (ops->getfeatcfg) {
  1083. struct nlattr *feat = nla_nest_start(skb, DCB_ATTR_CEE_FEAT);
  1084. if (!feat)
  1085. goto nla_put_failure;
  1086. for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
  1087. i++)
  1088. if (!ops->getfeatcfg(netdev, i, &value) &&
  1089. nla_put_u8(skb, i, value))
  1090. goto nla_put_failure;
  1091. nla_nest_end(skb, feat);
  1092. }
  1093. /* peer info if available */
  1094. if (ops->cee_peer_getpg) {
  1095. struct cee_pg pg;
  1096. memset(&pg, 0, sizeof(pg));
  1097. err = ops->cee_peer_getpg(netdev, &pg);
  1098. if (!err &&
  1099. nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
  1100. goto nla_put_failure;
  1101. }
  1102. if (ops->cee_peer_getpfc) {
  1103. struct cee_pfc pfc;
  1104. memset(&pfc, 0, sizeof(pfc));
  1105. err = ops->cee_peer_getpfc(netdev, &pfc);
  1106. if (!err &&
  1107. nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
  1108. goto nla_put_failure;
  1109. }
  1110. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1111. err = dcbnl_build_peer_app(netdev, skb,
  1112. DCB_ATTR_CEE_PEER_APP_TABLE,
  1113. DCB_ATTR_CEE_PEER_APP_INFO,
  1114. DCB_ATTR_CEE_PEER_APP);
  1115. if (err)
  1116. goto nla_put_failure;
  1117. }
  1118. nla_nest_end(skb, cee);
  1119. /* DCBX state */
  1120. if (dcbx >= 0) {
  1121. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1122. if (err)
  1123. goto nla_put_failure;
  1124. }
  1125. return 0;
  1126. dcb_unlock:
  1127. spin_unlock_bh(&dcb_lock);
  1128. nla_put_failure:
  1129. err = -EMSGSIZE;
  1130. return err;
  1131. }
  1132. static int dcbnl_notify(struct net_device *dev, int event, int cmd,
  1133. u32 seq, u32 portid, int dcbx_ver)
  1134. {
  1135. struct net *net = dev_net(dev);
  1136. struct sk_buff *skb;
  1137. struct nlmsghdr *nlh;
  1138. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1139. int err;
  1140. if (!ops)
  1141. return -EOPNOTSUPP;
  1142. skb = dcbnl_newmsg(event, cmd, portid, seq, 0, &nlh);
  1143. if (!skb)
  1144. return -ENOBUFS;
  1145. if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
  1146. err = dcbnl_ieee_fill(skb, dev);
  1147. else
  1148. err = dcbnl_cee_fill(skb, dev);
  1149. if (err < 0) {
  1150. /* Report error to broadcast listeners */
  1151. nlmsg_free(skb);
  1152. rtnl_set_sk_err(net, RTNLGRP_DCB, err);
  1153. } else {
  1154. /* End nlmsg and notify broadcast listeners */
  1155. nlmsg_end(skb, nlh);
  1156. rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
  1157. }
  1158. return err;
  1159. }
  1160. int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
  1161. u32 seq, u32 portid)
  1162. {
  1163. return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_IEEE);
  1164. }
  1165. EXPORT_SYMBOL(dcbnl_ieee_notify);
  1166. int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
  1167. u32 seq, u32 portid)
  1168. {
  1169. return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_CEE);
  1170. }
  1171. EXPORT_SYMBOL(dcbnl_cee_notify);
  1172. /* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb SET commands.
  1173. * If any requested operation can not be completed
  1174. * the entire msg is aborted and error value is returned.
  1175. * No attempt is made to reconcile the case where only part of the
  1176. * cmd can be completed.
  1177. */
  1178. static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
  1179. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1180. {
  1181. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1182. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1183. int err;
  1184. if (!ops)
  1185. return -EOPNOTSUPP;
  1186. if (!tb[DCB_ATTR_IEEE])
  1187. return -EINVAL;
  1188. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX, tb[DCB_ATTR_IEEE],
  1189. dcbnl_ieee_policy, NULL);
  1190. if (err)
  1191. return err;
  1192. if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  1193. struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  1194. err = ops->ieee_setets(netdev, ets);
  1195. if (err)
  1196. goto err;
  1197. }
  1198. if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
  1199. struct ieee_maxrate *maxrate =
  1200. nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
  1201. err = ops->ieee_setmaxrate(netdev, maxrate);
  1202. if (err)
  1203. goto err;
  1204. }
  1205. if (ieee[DCB_ATTR_IEEE_QCN] && ops->ieee_setqcn) {
  1206. struct ieee_qcn *qcn =
  1207. nla_data(ieee[DCB_ATTR_IEEE_QCN]);
  1208. err = ops->ieee_setqcn(netdev, qcn);
  1209. if (err)
  1210. goto err;
  1211. }
  1212. if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
  1213. struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  1214. err = ops->ieee_setpfc(netdev, pfc);
  1215. if (err)
  1216. goto err;
  1217. }
  1218. if (ieee[DCB_ATTR_DCB_BUFFER] && ops->dcbnl_setbuffer) {
  1219. struct dcbnl_buffer *buffer =
  1220. nla_data(ieee[DCB_ATTR_DCB_BUFFER]);
  1221. err = ops->dcbnl_setbuffer(netdev, buffer);
  1222. if (err)
  1223. goto err;
  1224. }
  1225. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1226. struct nlattr *attr;
  1227. int rem;
  1228. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1229. struct dcb_app *app_data;
  1230. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1231. continue;
  1232. if (nla_len(attr) < sizeof(struct dcb_app)) {
  1233. err = -ERANGE;
  1234. goto err;
  1235. }
  1236. app_data = nla_data(attr);
  1237. if (ops->ieee_setapp)
  1238. err = ops->ieee_setapp(netdev, app_data);
  1239. else
  1240. err = dcb_ieee_setapp(netdev, app_data);
  1241. if (err)
  1242. goto err;
  1243. }
  1244. }
  1245. err:
  1246. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1247. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
  1248. return err;
  1249. }
  1250. static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1251. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1252. {
  1253. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1254. if (!ops)
  1255. return -EOPNOTSUPP;
  1256. return dcbnl_ieee_fill(skb, netdev);
  1257. }
  1258. static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
  1259. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1260. {
  1261. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1262. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1263. int err;
  1264. if (!ops)
  1265. return -EOPNOTSUPP;
  1266. if (!tb[DCB_ATTR_IEEE])
  1267. return -EINVAL;
  1268. err = nla_parse_nested(ieee, DCB_ATTR_IEEE_MAX, tb[DCB_ATTR_IEEE],
  1269. dcbnl_ieee_policy, NULL);
  1270. if (err)
  1271. return err;
  1272. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1273. struct nlattr *attr;
  1274. int rem;
  1275. nla_for_each_nested(attr, ieee[DCB_ATTR_IEEE_APP_TABLE], rem) {
  1276. struct dcb_app *app_data;
  1277. if (nla_type(attr) != DCB_ATTR_IEEE_APP)
  1278. continue;
  1279. app_data = nla_data(attr);
  1280. if (ops->ieee_delapp)
  1281. err = ops->ieee_delapp(netdev, app_data);
  1282. else
  1283. err = dcb_ieee_delapp(netdev, app_data);
  1284. if (err)
  1285. goto err;
  1286. }
  1287. }
  1288. err:
  1289. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1290. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
  1291. return err;
  1292. }
  1293. /* DCBX configuration */
  1294. static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1295. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1296. {
  1297. if (!netdev->dcbnl_ops->getdcbx)
  1298. return -EOPNOTSUPP;
  1299. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1300. netdev->dcbnl_ops->getdcbx(netdev));
  1301. }
  1302. static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1303. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1304. {
  1305. u8 value;
  1306. if (!netdev->dcbnl_ops->setdcbx)
  1307. return -EOPNOTSUPP;
  1308. if (!tb[DCB_ATTR_DCBX])
  1309. return -EINVAL;
  1310. value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  1311. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1312. netdev->dcbnl_ops->setdcbx(netdev, value));
  1313. }
  1314. static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1315. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1316. {
  1317. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  1318. u8 value;
  1319. int ret, i;
  1320. int getall = 0;
  1321. if (!netdev->dcbnl_ops->getfeatcfg)
  1322. return -EOPNOTSUPP;
  1323. if (!tb[DCB_ATTR_FEATCFG])
  1324. return -EINVAL;
  1325. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX,
  1326. tb[DCB_ATTR_FEATCFG], dcbnl_featcfg_nest, NULL);
  1327. if (ret)
  1328. return ret;
  1329. nest = nla_nest_start(skb, DCB_ATTR_FEATCFG);
  1330. if (!nest)
  1331. return -EMSGSIZE;
  1332. if (data[DCB_FEATCFG_ATTR_ALL])
  1333. getall = 1;
  1334. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1335. if (!getall && !data[i])
  1336. continue;
  1337. ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
  1338. if (!ret)
  1339. ret = nla_put_u8(skb, i, value);
  1340. if (ret) {
  1341. nla_nest_cancel(skb, nest);
  1342. goto nla_put_failure;
  1343. }
  1344. }
  1345. nla_nest_end(skb, nest);
  1346. nla_put_failure:
  1347. return ret;
  1348. }
  1349. static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1350. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1351. {
  1352. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
  1353. int ret, i;
  1354. u8 value;
  1355. if (!netdev->dcbnl_ops->setfeatcfg)
  1356. return -ENOTSUPP;
  1357. if (!tb[DCB_ATTR_FEATCFG])
  1358. return -EINVAL;
  1359. ret = nla_parse_nested(data, DCB_FEATCFG_ATTR_MAX,
  1360. tb[DCB_ATTR_FEATCFG], dcbnl_featcfg_nest, NULL);
  1361. if (ret)
  1362. goto err;
  1363. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1364. if (data[i] == NULL)
  1365. continue;
  1366. value = nla_get_u8(data[i]);
  1367. ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  1368. if (ret)
  1369. goto err;
  1370. }
  1371. err:
  1372. ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
  1373. return ret;
  1374. }
  1375. /* Handle CEE DCBX GET commands. */
  1376. static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1377. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1378. {
  1379. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1380. if (!ops)
  1381. return -EOPNOTSUPP;
  1382. return dcbnl_cee_fill(skb, netdev);
  1383. }
  1384. struct reply_func {
  1385. /* reply netlink message type */
  1386. int type;
  1387. /* function to fill message contents */
  1388. int (*cb)(struct net_device *, struct nlmsghdr *, u32,
  1389. struct nlattr **, struct sk_buff *);
  1390. };
  1391. static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
  1392. [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
  1393. [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
  1394. [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
  1395. [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
  1396. [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
  1397. [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
  1398. [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
  1399. [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
  1400. [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
  1401. [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
  1402. [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
  1403. [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
  1404. [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
  1405. [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
  1406. [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
  1407. [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
  1408. [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
  1409. [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
  1410. [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
  1411. [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
  1412. [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
  1413. [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
  1414. [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
  1415. [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
  1416. [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
  1417. [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
  1418. [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
  1419. };
  1420. static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
  1421. struct netlink_ext_ack *extack)
  1422. {
  1423. struct net *net = sock_net(skb->sk);
  1424. struct net_device *netdev;
  1425. struct dcbmsg *dcb = nlmsg_data(nlh);
  1426. struct nlattr *tb[DCB_ATTR_MAX + 1];
  1427. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  1428. int ret = -EINVAL;
  1429. struct sk_buff *reply_skb;
  1430. struct nlmsghdr *reply_nlh = NULL;
  1431. const struct reply_func *fn;
  1432. if ((nlh->nlmsg_type == RTM_SETDCB) && !netlink_capable(skb, CAP_NET_ADMIN))
  1433. return -EPERM;
  1434. ret = nlmsg_parse(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  1435. dcbnl_rtnl_policy, extack);
  1436. if (ret < 0)
  1437. return ret;
  1438. if (dcb->cmd > DCB_CMD_MAX)
  1439. return -EINVAL;
  1440. /* check if a reply function has been defined for the command */
  1441. fn = &reply_funcs[dcb->cmd];
  1442. if (!fn->cb)
  1443. return -EOPNOTSUPP;
  1444. if (!tb[DCB_ATTR_IFNAME])
  1445. return -EINVAL;
  1446. netdev = __dev_get_by_name(net, nla_data(tb[DCB_ATTR_IFNAME]));
  1447. if (!netdev)
  1448. return -ENODEV;
  1449. if (!netdev->dcbnl_ops)
  1450. return -EOPNOTSUPP;
  1451. reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, portid, nlh->nlmsg_seq,
  1452. nlh->nlmsg_flags, &reply_nlh);
  1453. if (!reply_skb)
  1454. return -ENOBUFS;
  1455. ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
  1456. if (ret < 0) {
  1457. nlmsg_free(reply_skb);
  1458. goto out;
  1459. }
  1460. nlmsg_end(reply_skb, reply_nlh);
  1461. ret = rtnl_unicast(reply_skb, net, portid);
  1462. out:
  1463. return ret;
  1464. }
  1465. static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
  1466. int ifindex, int prio)
  1467. {
  1468. struct dcb_app_type *itr;
  1469. list_for_each_entry(itr, &dcb_app_list, list) {
  1470. if (itr->app.selector == app->selector &&
  1471. itr->app.protocol == app->protocol &&
  1472. itr->ifindex == ifindex &&
  1473. (!prio || itr->app.priority == prio))
  1474. return itr;
  1475. }
  1476. return NULL;
  1477. }
  1478. static int dcb_app_add(const struct dcb_app *app, int ifindex)
  1479. {
  1480. struct dcb_app_type *entry;
  1481. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1482. if (!entry)
  1483. return -ENOMEM;
  1484. memcpy(&entry->app, app, sizeof(*app));
  1485. entry->ifindex = ifindex;
  1486. list_add(&entry->list, &dcb_app_list);
  1487. return 0;
  1488. }
  1489. /**
  1490. * dcb_getapp - retrieve the DCBX application user priority
  1491. *
  1492. * On success returns a non-zero 802.1p user priority bitmap
  1493. * otherwise returns 0 as the invalid user priority bitmap to
  1494. * indicate an error.
  1495. */
  1496. u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  1497. {
  1498. struct dcb_app_type *itr;
  1499. u8 prio = 0;
  1500. spin_lock_bh(&dcb_lock);
  1501. if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
  1502. prio = itr->app.priority;
  1503. spin_unlock_bh(&dcb_lock);
  1504. return prio;
  1505. }
  1506. EXPORT_SYMBOL(dcb_getapp);
  1507. /**
  1508. * dcb_setapp - add CEE dcb application data to app list
  1509. *
  1510. * Priority 0 is an invalid priority in CEE spec. This routine
  1511. * removes applications from the app list if the priority is
  1512. * set to zero. Priority is expected to be 8-bit 802.1p user priority bitmap
  1513. */
  1514. int dcb_setapp(struct net_device *dev, struct dcb_app *new)
  1515. {
  1516. struct dcb_app_type *itr;
  1517. struct dcb_app_type event;
  1518. int err = 0;
  1519. event.ifindex = dev->ifindex;
  1520. memcpy(&event.app, new, sizeof(event.app));
  1521. if (dev->dcbnl_ops->getdcbx)
  1522. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1523. spin_lock_bh(&dcb_lock);
  1524. /* Search for existing match and replace */
  1525. if ((itr = dcb_app_lookup(new, dev->ifindex, 0))) {
  1526. if (new->priority)
  1527. itr->app.priority = new->priority;
  1528. else {
  1529. list_del(&itr->list);
  1530. kfree(itr);
  1531. }
  1532. goto out;
  1533. }
  1534. /* App type does not exist add new application type */
  1535. if (new->priority)
  1536. err = dcb_app_add(new, dev->ifindex);
  1537. out:
  1538. spin_unlock_bh(&dcb_lock);
  1539. if (!err)
  1540. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1541. return err;
  1542. }
  1543. EXPORT_SYMBOL(dcb_setapp);
  1544. /**
  1545. * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
  1546. *
  1547. * Helper routine which on success returns a non-zero 802.1Qaz user
  1548. * priority bitmap otherwise returns 0 to indicate the dcb_app was
  1549. * not found in APP list.
  1550. */
  1551. u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
  1552. {
  1553. struct dcb_app_type *itr;
  1554. u8 prio = 0;
  1555. spin_lock_bh(&dcb_lock);
  1556. if ((itr = dcb_app_lookup(app, dev->ifindex, 0)))
  1557. prio |= 1 << itr->app.priority;
  1558. spin_unlock_bh(&dcb_lock);
  1559. return prio;
  1560. }
  1561. EXPORT_SYMBOL(dcb_ieee_getapp_mask);
  1562. /**
  1563. * dcb_ieee_setapp - add IEEE dcb application data to app list
  1564. *
  1565. * This adds Application data to the list. Multiple application
  1566. * entries may exists for the same selector and protocol as long
  1567. * as the priorities are different. Priority is expected to be a
  1568. * 3-bit unsigned integer
  1569. */
  1570. int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
  1571. {
  1572. struct dcb_app_type event;
  1573. int err = 0;
  1574. event.ifindex = dev->ifindex;
  1575. memcpy(&event.app, new, sizeof(event.app));
  1576. if (dev->dcbnl_ops->getdcbx)
  1577. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1578. spin_lock_bh(&dcb_lock);
  1579. /* Search for existing match and abort if found */
  1580. if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
  1581. err = -EEXIST;
  1582. goto out;
  1583. }
  1584. err = dcb_app_add(new, dev->ifindex);
  1585. out:
  1586. spin_unlock_bh(&dcb_lock);
  1587. if (!err)
  1588. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1589. return err;
  1590. }
  1591. EXPORT_SYMBOL(dcb_ieee_setapp);
  1592. /**
  1593. * dcb_ieee_delapp - delete IEEE dcb application data from list
  1594. *
  1595. * This removes a matching APP data from the APP list
  1596. */
  1597. int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
  1598. {
  1599. struct dcb_app_type *itr;
  1600. struct dcb_app_type event;
  1601. int err = -ENOENT;
  1602. event.ifindex = dev->ifindex;
  1603. memcpy(&event.app, del, sizeof(event.app));
  1604. if (dev->dcbnl_ops->getdcbx)
  1605. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1606. spin_lock_bh(&dcb_lock);
  1607. /* Search for existing match and remove it. */
  1608. if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
  1609. list_del(&itr->list);
  1610. kfree(itr);
  1611. err = 0;
  1612. }
  1613. spin_unlock_bh(&dcb_lock);
  1614. if (!err)
  1615. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1616. return err;
  1617. }
  1618. EXPORT_SYMBOL(dcb_ieee_delapp);
  1619. static int __init dcbnl_init(void)
  1620. {
  1621. INIT_LIST_HEAD(&dcb_app_list);
  1622. rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, 0);
  1623. rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, 0);
  1624. return 0;
  1625. }
  1626. device_initcall(dcbnl_init);