dcbnl.c 42 KB

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