dcbnl.c 60 KB

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