dcbnl.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  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_array(app_count, sizeof(struct dcb_app),
  851. GFP_KERNEL);
  852. if (!table)
  853. return -ENOMEM;
  854. err = ops->peer_getapptable(netdev, table);
  855. }
  856. if (!err) {
  857. u16 i;
  858. struct nlattr *app;
  859. /**
  860. * build the message, from here on the only possible failure
  861. * is due to the skb size
  862. */
  863. err = -EMSGSIZE;
  864. app = nla_nest_start_noflag(skb, app_nested_type);
  865. if (!app)
  866. goto nla_put_failure;
  867. if (app_info_type &&
  868. nla_put(skb, app_info_type, sizeof(info), &info))
  869. goto nla_put_failure;
  870. for (i = 0; i < app_count; i++) {
  871. if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
  872. &table[i]))
  873. goto nla_put_failure;
  874. }
  875. nla_nest_end(skb, app);
  876. }
  877. err = 0;
  878. nla_put_failure:
  879. kfree(table);
  880. return err;
  881. }
  882. static int dcbnl_getapptrust(struct net_device *netdev, struct sk_buff *skb)
  883. {
  884. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  885. enum ieee_attrs_app type;
  886. struct nlattr *apptrust;
  887. int nselectors, err, i;
  888. u8 *selectors;
  889. selectors = kzalloc(IEEE_8021QAZ_APP_SEL_MAX + 1, GFP_KERNEL);
  890. if (!selectors)
  891. return -ENOMEM;
  892. err = ops->dcbnl_getapptrust(netdev, selectors, &nselectors);
  893. if (err) {
  894. err = 0;
  895. goto out;
  896. }
  897. apptrust = nla_nest_start(skb, DCB_ATTR_DCB_APP_TRUST_TABLE);
  898. if (!apptrust) {
  899. err = -EMSGSIZE;
  900. goto out;
  901. }
  902. for (i = 0; i < nselectors; i++) {
  903. type = dcbnl_app_attr_type_get(selectors[i]);
  904. err = nla_put_u8(skb, type, selectors[i]);
  905. if (err) {
  906. nla_nest_cancel(skb, apptrust);
  907. goto out;
  908. }
  909. }
  910. nla_nest_end(skb, apptrust);
  911. out:
  912. kfree(selectors);
  913. return err;
  914. }
  915. /* Set or delete APP table or rewrite table entries. The APP struct is validated
  916. * and the appropriate callback function is called.
  917. */
  918. static int dcbnl_app_table_setdel(struct nlattr *attr,
  919. struct net_device *netdev,
  920. int (*setdel)(struct net_device *dev,
  921. struct dcb_app *app))
  922. {
  923. struct dcb_app *app_data;
  924. enum ieee_attrs_app type;
  925. struct nlattr *attr_itr;
  926. int rem, err;
  927. nla_for_each_nested(attr_itr, attr, rem) {
  928. type = nla_type(attr_itr);
  929. if (!dcbnl_app_attr_type_validate(type))
  930. continue;
  931. if (nla_len(attr_itr) < sizeof(struct dcb_app))
  932. return -ERANGE;
  933. app_data = nla_data(attr_itr);
  934. if (!dcbnl_app_selector_validate(type, app_data->selector))
  935. return -EINVAL;
  936. err = setdel(netdev, app_data);
  937. if (err)
  938. return err;
  939. }
  940. return 0;
  941. }
  942. /* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb GET commands. */
  943. static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
  944. {
  945. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  946. struct nlattr *ieee, *app, *rewr;
  947. struct dcb_app_type *itr;
  948. int dcbx;
  949. int err;
  950. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  951. return -EMSGSIZE;
  952. ieee = nla_nest_start_noflag(skb, DCB_ATTR_IEEE);
  953. if (!ieee)
  954. return -EMSGSIZE;
  955. if (ops->ieee_getets) {
  956. struct ieee_ets ets;
  957. memset(&ets, 0, sizeof(ets));
  958. err = ops->ieee_getets(netdev, &ets);
  959. if (!err &&
  960. nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
  961. return -EMSGSIZE;
  962. }
  963. if (ops->ieee_getmaxrate) {
  964. struct ieee_maxrate maxrate;
  965. memset(&maxrate, 0, sizeof(maxrate));
  966. err = ops->ieee_getmaxrate(netdev, &maxrate);
  967. if (!err) {
  968. err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
  969. sizeof(maxrate), &maxrate);
  970. if (err)
  971. return -EMSGSIZE;
  972. }
  973. }
  974. if (ops->ieee_getqcn) {
  975. struct ieee_qcn qcn;
  976. memset(&qcn, 0, sizeof(qcn));
  977. err = ops->ieee_getqcn(netdev, &qcn);
  978. if (!err) {
  979. err = nla_put(skb, DCB_ATTR_IEEE_QCN,
  980. sizeof(qcn), &qcn);
  981. if (err)
  982. return -EMSGSIZE;
  983. }
  984. }
  985. if (ops->ieee_getqcnstats) {
  986. struct ieee_qcn_stats qcn_stats;
  987. memset(&qcn_stats, 0, sizeof(qcn_stats));
  988. err = ops->ieee_getqcnstats(netdev, &qcn_stats);
  989. if (!err) {
  990. err = nla_put(skb, DCB_ATTR_IEEE_QCN_STATS,
  991. sizeof(qcn_stats), &qcn_stats);
  992. if (err)
  993. return -EMSGSIZE;
  994. }
  995. }
  996. if (ops->ieee_getpfc) {
  997. struct ieee_pfc pfc;
  998. memset(&pfc, 0, sizeof(pfc));
  999. err = ops->ieee_getpfc(netdev, &pfc);
  1000. if (!err &&
  1001. nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
  1002. return -EMSGSIZE;
  1003. }
  1004. if (ops->dcbnl_getbuffer) {
  1005. struct dcbnl_buffer buffer;
  1006. memset(&buffer, 0, sizeof(buffer));
  1007. err = ops->dcbnl_getbuffer(netdev, &buffer);
  1008. if (!err &&
  1009. nla_put(skb, DCB_ATTR_DCB_BUFFER, sizeof(buffer), &buffer))
  1010. return -EMSGSIZE;
  1011. }
  1012. app = nla_nest_start_noflag(skb, DCB_ATTR_IEEE_APP_TABLE);
  1013. if (!app)
  1014. return -EMSGSIZE;
  1015. spin_lock_bh(&dcb_lock);
  1016. list_for_each_entry(itr, &dcb_app_list, list) {
  1017. if (itr->ifindex == netdev->ifindex) {
  1018. enum ieee_attrs_app type =
  1019. dcbnl_app_attr_type_get(itr->app.selector);
  1020. err = nla_put(skb, type, sizeof(itr->app), &itr->app);
  1021. if (err) {
  1022. spin_unlock_bh(&dcb_lock);
  1023. return -EMSGSIZE;
  1024. }
  1025. }
  1026. }
  1027. if (netdev->dcbnl_ops->getdcbx)
  1028. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1029. else
  1030. dcbx = -EOPNOTSUPP;
  1031. spin_unlock_bh(&dcb_lock);
  1032. nla_nest_end(skb, app);
  1033. rewr = nla_nest_start(skb, DCB_ATTR_DCB_REWR_TABLE);
  1034. if (!rewr)
  1035. return -EMSGSIZE;
  1036. spin_lock_bh(&dcb_lock);
  1037. list_for_each_entry(itr, &dcb_rewr_list, list) {
  1038. if (itr->ifindex == netdev->ifindex) {
  1039. enum ieee_attrs_app type =
  1040. dcbnl_app_attr_type_get(itr->app.selector);
  1041. err = nla_put(skb, type, sizeof(itr->app), &itr->app);
  1042. if (err) {
  1043. spin_unlock_bh(&dcb_lock);
  1044. nla_nest_cancel(skb, rewr);
  1045. return -EMSGSIZE;
  1046. }
  1047. }
  1048. }
  1049. spin_unlock_bh(&dcb_lock);
  1050. nla_nest_end(skb, rewr);
  1051. if (ops->dcbnl_getapptrust) {
  1052. err = dcbnl_getapptrust(netdev, skb);
  1053. if (err)
  1054. return err;
  1055. }
  1056. /* get peer info if available */
  1057. if (ops->ieee_peer_getets) {
  1058. struct ieee_ets ets;
  1059. memset(&ets, 0, sizeof(ets));
  1060. err = ops->ieee_peer_getets(netdev, &ets);
  1061. if (!err &&
  1062. nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
  1063. return -EMSGSIZE;
  1064. }
  1065. if (ops->ieee_peer_getpfc) {
  1066. struct ieee_pfc pfc;
  1067. memset(&pfc, 0, sizeof(pfc));
  1068. err = ops->ieee_peer_getpfc(netdev, &pfc);
  1069. if (!err &&
  1070. nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
  1071. return -EMSGSIZE;
  1072. }
  1073. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1074. err = dcbnl_build_peer_app(netdev, skb,
  1075. DCB_ATTR_IEEE_PEER_APP,
  1076. DCB_ATTR_IEEE_APP_UNSPEC,
  1077. DCB_ATTR_IEEE_APP);
  1078. if (err)
  1079. return -EMSGSIZE;
  1080. }
  1081. nla_nest_end(skb, ieee);
  1082. if (dcbx >= 0) {
  1083. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1084. if (err)
  1085. return -EMSGSIZE;
  1086. }
  1087. return 0;
  1088. }
  1089. static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
  1090. int dir)
  1091. {
  1092. u8 pgid, up_map, prio, tc_pct;
  1093. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1094. int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
  1095. struct nlattr *pg = nla_nest_start_noflag(skb, i);
  1096. if (!pg)
  1097. return -EMSGSIZE;
  1098. for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
  1099. struct nlattr *tc_nest = nla_nest_start_noflag(skb, i);
  1100. if (!tc_nest)
  1101. return -EMSGSIZE;
  1102. pgid = DCB_ATTR_VALUE_UNDEFINED;
  1103. prio = DCB_ATTR_VALUE_UNDEFINED;
  1104. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  1105. up_map = DCB_ATTR_VALUE_UNDEFINED;
  1106. if (!dir)
  1107. ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
  1108. &prio, &pgid, &tc_pct, &up_map);
  1109. else
  1110. ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
  1111. &prio, &pgid, &tc_pct, &up_map);
  1112. if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
  1113. nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
  1114. nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
  1115. nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
  1116. return -EMSGSIZE;
  1117. nla_nest_end(skb, tc_nest);
  1118. }
  1119. for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
  1120. tc_pct = DCB_ATTR_VALUE_UNDEFINED;
  1121. if (!dir)
  1122. ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
  1123. &tc_pct);
  1124. else
  1125. ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
  1126. &tc_pct);
  1127. if (nla_put_u8(skb, i, tc_pct))
  1128. return -EMSGSIZE;
  1129. }
  1130. nla_nest_end(skb, pg);
  1131. return 0;
  1132. }
  1133. static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
  1134. {
  1135. struct nlattr *cee, *app;
  1136. struct dcb_app_type *itr;
  1137. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1138. int dcbx, i, err = -EMSGSIZE;
  1139. u8 value;
  1140. if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
  1141. goto nla_put_failure;
  1142. cee = nla_nest_start_noflag(skb, DCB_ATTR_CEE);
  1143. if (!cee)
  1144. goto nla_put_failure;
  1145. /* local pg */
  1146. if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
  1147. err = dcbnl_cee_pg_fill(skb, netdev, 1);
  1148. if (err)
  1149. goto nla_put_failure;
  1150. }
  1151. if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
  1152. err = dcbnl_cee_pg_fill(skb, netdev, 0);
  1153. if (err)
  1154. goto nla_put_failure;
  1155. }
  1156. /* local pfc */
  1157. if (ops->getpfccfg) {
  1158. struct nlattr *pfc_nest = nla_nest_start_noflag(skb,
  1159. DCB_ATTR_CEE_PFC);
  1160. if (!pfc_nest)
  1161. goto nla_put_failure;
  1162. for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
  1163. ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
  1164. if (nla_put_u8(skb, i, value))
  1165. goto nla_put_failure;
  1166. }
  1167. nla_nest_end(skb, pfc_nest);
  1168. }
  1169. /* local app */
  1170. spin_lock_bh(&dcb_lock);
  1171. app = nla_nest_start_noflag(skb, DCB_ATTR_CEE_APP_TABLE);
  1172. if (!app)
  1173. goto dcb_unlock;
  1174. list_for_each_entry(itr, &dcb_app_list, list) {
  1175. if (itr->ifindex == netdev->ifindex) {
  1176. struct nlattr *app_nest = nla_nest_start_noflag(skb,
  1177. DCB_ATTR_APP);
  1178. if (!app_nest)
  1179. goto dcb_unlock;
  1180. err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
  1181. itr->app.selector);
  1182. if (err)
  1183. goto dcb_unlock;
  1184. err = nla_put_u16(skb, DCB_APP_ATTR_ID,
  1185. itr->app.protocol);
  1186. if (err)
  1187. goto dcb_unlock;
  1188. err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
  1189. itr->app.priority);
  1190. if (err)
  1191. goto dcb_unlock;
  1192. nla_nest_end(skb, app_nest);
  1193. }
  1194. }
  1195. nla_nest_end(skb, app);
  1196. if (netdev->dcbnl_ops->getdcbx)
  1197. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1198. else
  1199. dcbx = -EOPNOTSUPP;
  1200. spin_unlock_bh(&dcb_lock);
  1201. /* features flags */
  1202. if (ops->getfeatcfg) {
  1203. struct nlattr *feat = nla_nest_start_noflag(skb,
  1204. DCB_ATTR_CEE_FEAT);
  1205. if (!feat)
  1206. goto nla_put_failure;
  1207. for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
  1208. i++)
  1209. if (!ops->getfeatcfg(netdev, i, &value) &&
  1210. nla_put_u8(skb, i, value))
  1211. goto nla_put_failure;
  1212. nla_nest_end(skb, feat);
  1213. }
  1214. /* peer info if available */
  1215. if (ops->cee_peer_getpg) {
  1216. struct cee_pg pg;
  1217. memset(&pg, 0, sizeof(pg));
  1218. err = ops->cee_peer_getpg(netdev, &pg);
  1219. if (!err &&
  1220. nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
  1221. goto nla_put_failure;
  1222. }
  1223. if (ops->cee_peer_getpfc) {
  1224. struct cee_pfc pfc;
  1225. memset(&pfc, 0, sizeof(pfc));
  1226. err = ops->cee_peer_getpfc(netdev, &pfc);
  1227. if (!err &&
  1228. nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
  1229. goto nla_put_failure;
  1230. }
  1231. if (ops->peer_getappinfo && ops->peer_getapptable) {
  1232. err = dcbnl_build_peer_app(netdev, skb,
  1233. DCB_ATTR_CEE_PEER_APP_TABLE,
  1234. DCB_ATTR_CEE_PEER_APP_INFO,
  1235. DCB_ATTR_CEE_PEER_APP);
  1236. if (err)
  1237. goto nla_put_failure;
  1238. }
  1239. nla_nest_end(skb, cee);
  1240. /* DCBX state */
  1241. if (dcbx >= 0) {
  1242. err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
  1243. if (err)
  1244. goto nla_put_failure;
  1245. }
  1246. return 0;
  1247. dcb_unlock:
  1248. spin_unlock_bh(&dcb_lock);
  1249. nla_put_failure:
  1250. err = -EMSGSIZE;
  1251. return err;
  1252. }
  1253. static int dcbnl_notify(struct net_device *dev, int event, int cmd,
  1254. u32 seq, u32 portid, int dcbx_ver)
  1255. {
  1256. struct net *net = dev_net(dev);
  1257. struct sk_buff *skb;
  1258. struct nlmsghdr *nlh;
  1259. const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
  1260. int err;
  1261. if (!ops)
  1262. return -EOPNOTSUPP;
  1263. skb = dcbnl_newmsg(event, cmd, portid, seq, 0, &nlh);
  1264. if (!skb)
  1265. return -ENOMEM;
  1266. if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
  1267. err = dcbnl_ieee_fill(skb, dev);
  1268. else
  1269. err = dcbnl_cee_fill(skb, dev);
  1270. if (err < 0) {
  1271. /* Report error to broadcast listeners */
  1272. nlmsg_free(skb);
  1273. rtnl_set_sk_err(net, RTNLGRP_DCB, err);
  1274. } else {
  1275. /* End nlmsg and notify broadcast listeners */
  1276. nlmsg_end(skb, nlh);
  1277. rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
  1278. }
  1279. return err;
  1280. }
  1281. int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
  1282. u32 seq, u32 portid)
  1283. {
  1284. return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_IEEE);
  1285. }
  1286. EXPORT_SYMBOL(dcbnl_ieee_notify);
  1287. int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
  1288. u32 seq, u32 portid)
  1289. {
  1290. return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_CEE);
  1291. }
  1292. EXPORT_SYMBOL(dcbnl_cee_notify);
  1293. /* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb SET commands.
  1294. * If any requested operation can not be completed
  1295. * the entire msg is aborted and error value is returned.
  1296. * No attempt is made to reconcile the case where only part of the
  1297. * cmd can be completed.
  1298. */
  1299. static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
  1300. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1301. {
  1302. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1303. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1304. int prio;
  1305. int err;
  1306. if (!ops)
  1307. return -EOPNOTSUPP;
  1308. if (!tb[DCB_ATTR_IEEE])
  1309. return -EINVAL;
  1310. err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
  1311. tb[DCB_ATTR_IEEE],
  1312. dcbnl_ieee_policy, NULL);
  1313. if (err)
  1314. return err;
  1315. if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
  1316. struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
  1317. err = ops->ieee_setets(netdev, ets);
  1318. if (err)
  1319. goto err;
  1320. }
  1321. if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
  1322. struct ieee_maxrate *maxrate =
  1323. nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
  1324. err = ops->ieee_setmaxrate(netdev, maxrate);
  1325. if (err)
  1326. goto err;
  1327. }
  1328. if (ieee[DCB_ATTR_IEEE_QCN] && ops->ieee_setqcn) {
  1329. struct ieee_qcn *qcn =
  1330. nla_data(ieee[DCB_ATTR_IEEE_QCN]);
  1331. err = ops->ieee_setqcn(netdev, qcn);
  1332. if (err)
  1333. goto err;
  1334. }
  1335. if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
  1336. struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
  1337. err = ops->ieee_setpfc(netdev, pfc);
  1338. if (err)
  1339. goto err;
  1340. }
  1341. if (ieee[DCB_ATTR_DCB_BUFFER] && ops->dcbnl_setbuffer) {
  1342. struct dcbnl_buffer *buffer =
  1343. nla_data(ieee[DCB_ATTR_DCB_BUFFER]);
  1344. for (prio = 0; prio < ARRAY_SIZE(buffer->prio2buffer); prio++) {
  1345. if (buffer->prio2buffer[prio] >= DCBX_MAX_BUFFERS) {
  1346. err = -EINVAL;
  1347. goto err;
  1348. }
  1349. }
  1350. err = ops->dcbnl_setbuffer(netdev, buffer);
  1351. if (err)
  1352. goto err;
  1353. }
  1354. if (ieee[DCB_ATTR_DCB_REWR_TABLE]) {
  1355. err = dcbnl_app_table_setdel(ieee[DCB_ATTR_DCB_REWR_TABLE],
  1356. netdev,
  1357. ops->dcbnl_setrewr ?: dcb_setrewr);
  1358. if (err)
  1359. goto err;
  1360. }
  1361. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1362. err = dcbnl_app_table_setdel(ieee[DCB_ATTR_IEEE_APP_TABLE],
  1363. netdev, ops->ieee_setapp ?:
  1364. dcb_ieee_setapp);
  1365. if (err)
  1366. goto err;
  1367. }
  1368. if (ieee[DCB_ATTR_DCB_APP_TRUST_TABLE]) {
  1369. u8 selectors[IEEE_8021QAZ_APP_SEL_MAX + 1] = {0};
  1370. struct nlattr *attr;
  1371. int nselectors = 0;
  1372. int rem;
  1373. if (!ops->dcbnl_setapptrust) {
  1374. err = -EOPNOTSUPP;
  1375. goto err;
  1376. }
  1377. nla_for_each_nested(attr, ieee[DCB_ATTR_DCB_APP_TRUST_TABLE],
  1378. rem) {
  1379. enum ieee_attrs_app type = nla_type(attr);
  1380. u8 selector;
  1381. int i;
  1382. if (!dcbnl_app_attr_type_validate(type) ||
  1383. nla_len(attr) != 1 ||
  1384. nselectors >= sizeof(selectors)) {
  1385. err = -EINVAL;
  1386. goto err;
  1387. }
  1388. selector = nla_get_u8(attr);
  1389. if (!dcbnl_app_selector_validate(type, selector)) {
  1390. err = -EINVAL;
  1391. goto err;
  1392. }
  1393. /* Duplicate selector ? */
  1394. for (i = 0; i < nselectors; i++) {
  1395. if (selectors[i] == selector) {
  1396. err = -EINVAL;
  1397. goto err;
  1398. }
  1399. }
  1400. selectors[nselectors++] = selector;
  1401. }
  1402. err = ops->dcbnl_setapptrust(netdev, selectors, nselectors);
  1403. if (err)
  1404. goto err;
  1405. }
  1406. err:
  1407. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1408. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
  1409. return err;
  1410. }
  1411. static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1412. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1413. {
  1414. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1415. if (!ops)
  1416. return -EOPNOTSUPP;
  1417. return dcbnl_ieee_fill(skb, netdev);
  1418. }
  1419. static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
  1420. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1421. {
  1422. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1423. struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
  1424. int err;
  1425. if (!ops)
  1426. return -EOPNOTSUPP;
  1427. if (!tb[DCB_ATTR_IEEE])
  1428. return -EINVAL;
  1429. err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
  1430. tb[DCB_ATTR_IEEE],
  1431. dcbnl_ieee_policy, NULL);
  1432. if (err)
  1433. return err;
  1434. if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
  1435. err = dcbnl_app_table_setdel(ieee[DCB_ATTR_IEEE_APP_TABLE],
  1436. netdev, ops->ieee_delapp ?:
  1437. dcb_ieee_delapp);
  1438. if (err)
  1439. goto err;
  1440. }
  1441. if (ieee[DCB_ATTR_DCB_REWR_TABLE]) {
  1442. err = dcbnl_app_table_setdel(ieee[DCB_ATTR_DCB_REWR_TABLE],
  1443. netdev,
  1444. ops->dcbnl_delrewr ?: dcb_delrewr);
  1445. if (err)
  1446. goto err;
  1447. }
  1448. err:
  1449. err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
  1450. dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
  1451. return err;
  1452. }
  1453. /* DCBX configuration */
  1454. static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1455. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1456. {
  1457. if (!netdev->dcbnl_ops->getdcbx)
  1458. return -EOPNOTSUPP;
  1459. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1460. netdev->dcbnl_ops->getdcbx(netdev));
  1461. }
  1462. static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
  1463. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1464. {
  1465. u8 value;
  1466. if (!netdev->dcbnl_ops->setdcbx)
  1467. return -EOPNOTSUPP;
  1468. if (!tb[DCB_ATTR_DCBX])
  1469. return -EINVAL;
  1470. value = nla_get_u8(tb[DCB_ATTR_DCBX]);
  1471. return nla_put_u8(skb, DCB_ATTR_DCBX,
  1472. netdev->dcbnl_ops->setdcbx(netdev, value));
  1473. }
  1474. static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1475. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1476. {
  1477. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
  1478. u8 value;
  1479. int ret, i;
  1480. int getall = 0;
  1481. if (!netdev->dcbnl_ops->getfeatcfg)
  1482. return -EOPNOTSUPP;
  1483. if (!tb[DCB_ATTR_FEATCFG])
  1484. return -EINVAL;
  1485. ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
  1486. tb[DCB_ATTR_FEATCFG],
  1487. dcbnl_featcfg_nest, NULL);
  1488. if (ret)
  1489. return ret;
  1490. nest = nla_nest_start_noflag(skb, DCB_ATTR_FEATCFG);
  1491. if (!nest)
  1492. return -EMSGSIZE;
  1493. if (data[DCB_FEATCFG_ATTR_ALL])
  1494. getall = 1;
  1495. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1496. if (!getall && !data[i])
  1497. continue;
  1498. ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
  1499. if (!ret)
  1500. ret = nla_put_u8(skb, i, value);
  1501. if (ret) {
  1502. nla_nest_cancel(skb, nest);
  1503. goto nla_put_failure;
  1504. }
  1505. }
  1506. nla_nest_end(skb, nest);
  1507. nla_put_failure:
  1508. return ret;
  1509. }
  1510. static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
  1511. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1512. {
  1513. struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
  1514. int ret, i;
  1515. u8 value;
  1516. if (!netdev->dcbnl_ops->setfeatcfg)
  1517. return -ENOTSUPP;
  1518. if (!tb[DCB_ATTR_FEATCFG])
  1519. return -EINVAL;
  1520. ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
  1521. tb[DCB_ATTR_FEATCFG],
  1522. dcbnl_featcfg_nest, NULL);
  1523. if (ret)
  1524. goto err;
  1525. for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
  1526. if (data[i] == NULL)
  1527. continue;
  1528. value = nla_get_u8(data[i]);
  1529. ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
  1530. if (ret)
  1531. goto err;
  1532. }
  1533. err:
  1534. ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
  1535. return ret;
  1536. }
  1537. /* Handle CEE DCBX GET commands. */
  1538. static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
  1539. u32 seq, struct nlattr **tb, struct sk_buff *skb)
  1540. {
  1541. const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
  1542. if (!ops)
  1543. return -EOPNOTSUPP;
  1544. return dcbnl_cee_fill(skb, netdev);
  1545. }
  1546. struct reply_func {
  1547. /* reply netlink message type */
  1548. int type;
  1549. /* function to fill message contents */
  1550. int (*cb)(struct net_device *, struct nlmsghdr *, u32,
  1551. struct nlattr **, struct sk_buff *);
  1552. };
  1553. static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
  1554. [DCB_CMD_GSTATE] = { RTM_GETDCB, dcbnl_getstate },
  1555. [DCB_CMD_SSTATE] = { RTM_SETDCB, dcbnl_setstate },
  1556. [DCB_CMD_PFC_GCFG] = { RTM_GETDCB, dcbnl_getpfccfg },
  1557. [DCB_CMD_PFC_SCFG] = { RTM_SETDCB, dcbnl_setpfccfg },
  1558. [DCB_CMD_GPERM_HWADDR] = { RTM_GETDCB, dcbnl_getperm_hwaddr },
  1559. [DCB_CMD_GCAP] = { RTM_GETDCB, dcbnl_getcap },
  1560. [DCB_CMD_GNUMTCS] = { RTM_GETDCB, dcbnl_getnumtcs },
  1561. [DCB_CMD_SNUMTCS] = { RTM_SETDCB, dcbnl_setnumtcs },
  1562. [DCB_CMD_PFC_GSTATE] = { RTM_GETDCB, dcbnl_getpfcstate },
  1563. [DCB_CMD_PFC_SSTATE] = { RTM_SETDCB, dcbnl_setpfcstate },
  1564. [DCB_CMD_GAPP] = { RTM_GETDCB, dcbnl_getapp },
  1565. [DCB_CMD_SAPP] = { RTM_SETDCB, dcbnl_setapp },
  1566. [DCB_CMD_PGTX_GCFG] = { RTM_GETDCB, dcbnl_pgtx_getcfg },
  1567. [DCB_CMD_PGTX_SCFG] = { RTM_SETDCB, dcbnl_pgtx_setcfg },
  1568. [DCB_CMD_PGRX_GCFG] = { RTM_GETDCB, dcbnl_pgrx_getcfg },
  1569. [DCB_CMD_PGRX_SCFG] = { RTM_SETDCB, dcbnl_pgrx_setcfg },
  1570. [DCB_CMD_SET_ALL] = { RTM_SETDCB, dcbnl_setall },
  1571. [DCB_CMD_BCN_GCFG] = { RTM_GETDCB, dcbnl_bcn_getcfg },
  1572. [DCB_CMD_BCN_SCFG] = { RTM_SETDCB, dcbnl_bcn_setcfg },
  1573. [DCB_CMD_IEEE_GET] = { RTM_GETDCB, dcbnl_ieee_get },
  1574. [DCB_CMD_IEEE_SET] = { RTM_SETDCB, dcbnl_ieee_set },
  1575. [DCB_CMD_IEEE_DEL] = { RTM_SETDCB, dcbnl_ieee_del },
  1576. [DCB_CMD_GDCBX] = { RTM_GETDCB, dcbnl_getdcbx },
  1577. [DCB_CMD_SDCBX] = { RTM_SETDCB, dcbnl_setdcbx },
  1578. [DCB_CMD_GFEATCFG] = { RTM_GETDCB, dcbnl_getfeatcfg },
  1579. [DCB_CMD_SFEATCFG] = { RTM_SETDCB, dcbnl_setfeatcfg },
  1580. [DCB_CMD_CEE_GET] = { RTM_GETDCB, dcbnl_cee_get },
  1581. };
  1582. static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
  1583. struct netlink_ext_ack *extack)
  1584. {
  1585. struct net *net = sock_net(skb->sk);
  1586. struct net_device *netdev;
  1587. struct dcbmsg *dcb = nlmsg_data(nlh);
  1588. struct nlattr *tb[DCB_ATTR_MAX + 1];
  1589. u32 portid = NETLINK_CB(skb).portid;
  1590. int ret = -EINVAL;
  1591. struct sk_buff *reply_skb;
  1592. struct nlmsghdr *reply_nlh = NULL;
  1593. const struct reply_func *fn;
  1594. if ((nlh->nlmsg_type == RTM_SETDCB) && !netlink_capable(skb, CAP_NET_ADMIN))
  1595. return -EPERM;
  1596. ret = nlmsg_parse_deprecated(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
  1597. dcbnl_rtnl_policy, extack);
  1598. if (ret < 0)
  1599. return ret;
  1600. if (dcb->cmd > DCB_CMD_MAX)
  1601. return -EINVAL;
  1602. /* check if a reply function has been defined for the command */
  1603. fn = &reply_funcs[dcb->cmd];
  1604. if (!fn->cb)
  1605. return -EOPNOTSUPP;
  1606. if (fn->type == RTM_SETDCB && !netlink_capable(skb, CAP_NET_ADMIN))
  1607. return -EPERM;
  1608. if (!tb[DCB_ATTR_IFNAME])
  1609. return -EINVAL;
  1610. netdev = __dev_get_by_name(net, nla_data(tb[DCB_ATTR_IFNAME]));
  1611. if (!netdev)
  1612. return -ENODEV;
  1613. if (!netdev->dcbnl_ops)
  1614. return -EOPNOTSUPP;
  1615. reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, portid, nlh->nlmsg_seq,
  1616. nlh->nlmsg_flags, &reply_nlh);
  1617. if (!reply_skb)
  1618. return -ENOMEM;
  1619. ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
  1620. if (ret < 0) {
  1621. nlmsg_free(reply_skb);
  1622. goto out;
  1623. }
  1624. nlmsg_end(reply_skb, reply_nlh);
  1625. ret = rtnl_unicast(reply_skb, net, portid);
  1626. out:
  1627. return ret;
  1628. }
  1629. static struct dcb_app_type *dcb_rewr_lookup(const struct dcb_app *app,
  1630. int ifindex, int proto)
  1631. {
  1632. struct dcb_app_type *itr;
  1633. list_for_each_entry(itr, &dcb_rewr_list, list) {
  1634. if (itr->app.selector == app->selector &&
  1635. itr->app.priority == app->priority &&
  1636. itr->ifindex == ifindex &&
  1637. ((proto == -1) || itr->app.protocol == proto))
  1638. return itr;
  1639. }
  1640. return NULL;
  1641. }
  1642. static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
  1643. int ifindex, int prio)
  1644. {
  1645. struct dcb_app_type *itr;
  1646. list_for_each_entry(itr, &dcb_app_list, list) {
  1647. if (itr->app.selector == app->selector &&
  1648. itr->app.protocol == app->protocol &&
  1649. itr->ifindex == ifindex &&
  1650. ((prio == -1) || itr->app.priority == prio))
  1651. return itr;
  1652. }
  1653. return NULL;
  1654. }
  1655. static int dcb_app_add(struct list_head *list, const struct dcb_app *app,
  1656. int ifindex)
  1657. {
  1658. struct dcb_app_type *entry;
  1659. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1660. if (!entry)
  1661. return -ENOMEM;
  1662. memcpy(&entry->app, app, sizeof(*app));
  1663. entry->ifindex = ifindex;
  1664. list_add(&entry->list, list);
  1665. return 0;
  1666. }
  1667. /**
  1668. * dcb_getapp - retrieve the DCBX application user priority
  1669. * @dev: network interface
  1670. * @app: application to get user priority of
  1671. *
  1672. * On success returns a non-zero 802.1p user priority bitmap
  1673. * otherwise returns 0 as the invalid user priority bitmap to
  1674. * indicate an error.
  1675. */
  1676. u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
  1677. {
  1678. struct dcb_app_type *itr;
  1679. u8 prio = 0;
  1680. spin_lock_bh(&dcb_lock);
  1681. itr = dcb_app_lookup(app, dev->ifindex, -1);
  1682. if (itr)
  1683. prio = itr->app.priority;
  1684. spin_unlock_bh(&dcb_lock);
  1685. return prio;
  1686. }
  1687. EXPORT_SYMBOL(dcb_getapp);
  1688. /**
  1689. * dcb_setapp - add CEE dcb application data to app list
  1690. * @dev: network interface
  1691. * @new: application data to add
  1692. *
  1693. * Priority 0 is an invalid priority in CEE spec. This routine
  1694. * removes applications from the app list if the priority is
  1695. * set to zero. Priority is expected to be 8-bit 802.1p user priority bitmap
  1696. */
  1697. int dcb_setapp(struct net_device *dev, struct dcb_app *new)
  1698. {
  1699. struct dcb_app_type *itr;
  1700. struct dcb_app_type event;
  1701. int err = 0;
  1702. event.ifindex = dev->ifindex;
  1703. memcpy(&event.app, new, sizeof(event.app));
  1704. if (dev->dcbnl_ops->getdcbx)
  1705. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1706. spin_lock_bh(&dcb_lock);
  1707. /* Search for existing match and replace */
  1708. itr = dcb_app_lookup(new, dev->ifindex, -1);
  1709. if (itr) {
  1710. if (new->priority)
  1711. itr->app.priority = new->priority;
  1712. else {
  1713. list_del(&itr->list);
  1714. kfree(itr);
  1715. }
  1716. goto out;
  1717. }
  1718. /* App type does not exist add new application type */
  1719. if (new->priority)
  1720. err = dcb_app_add(&dcb_app_list, new, dev->ifindex);
  1721. out:
  1722. spin_unlock_bh(&dcb_lock);
  1723. if (!err)
  1724. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1725. return err;
  1726. }
  1727. EXPORT_SYMBOL(dcb_setapp);
  1728. /**
  1729. * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
  1730. * @dev: network interface
  1731. * @app: where to store the retrieve application data
  1732. *
  1733. * Helper routine which on success returns a non-zero 802.1Qaz user
  1734. * priority bitmap otherwise returns 0 to indicate the dcb_app was
  1735. * not found in APP list.
  1736. */
  1737. u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
  1738. {
  1739. struct dcb_app_type *itr;
  1740. u8 prio = 0;
  1741. spin_lock_bh(&dcb_lock);
  1742. itr = dcb_app_lookup(app, dev->ifindex, -1);
  1743. if (itr)
  1744. prio |= 1 << itr->app.priority;
  1745. spin_unlock_bh(&dcb_lock);
  1746. return prio;
  1747. }
  1748. EXPORT_SYMBOL(dcb_ieee_getapp_mask);
  1749. /* Get protocol value from rewrite entry. */
  1750. u16 dcb_getrewr(struct net_device *dev, struct dcb_app *app)
  1751. {
  1752. struct dcb_app_type *itr;
  1753. u16 proto = 0;
  1754. spin_lock_bh(&dcb_lock);
  1755. itr = dcb_rewr_lookup(app, dev->ifindex, -1);
  1756. if (itr)
  1757. proto = itr->app.protocol;
  1758. spin_unlock_bh(&dcb_lock);
  1759. return proto;
  1760. }
  1761. EXPORT_SYMBOL(dcb_getrewr);
  1762. /* Add rewrite entry to the rewrite list. */
  1763. int dcb_setrewr(struct net_device *dev, struct dcb_app *new)
  1764. {
  1765. int err;
  1766. spin_lock_bh(&dcb_lock);
  1767. /* Search for existing match and abort if found. */
  1768. if (dcb_rewr_lookup(new, dev->ifindex, new->protocol)) {
  1769. err = -EEXIST;
  1770. goto out;
  1771. }
  1772. err = dcb_app_add(&dcb_rewr_list, new, dev->ifindex);
  1773. out:
  1774. spin_unlock_bh(&dcb_lock);
  1775. return err;
  1776. }
  1777. EXPORT_SYMBOL(dcb_setrewr);
  1778. /* Delete rewrite entry from the rewrite list. */
  1779. int dcb_delrewr(struct net_device *dev, struct dcb_app *del)
  1780. {
  1781. struct dcb_app_type *itr;
  1782. int err = -ENOENT;
  1783. spin_lock_bh(&dcb_lock);
  1784. /* Search for existing match and remove it. */
  1785. itr = dcb_rewr_lookup(del, dev->ifindex, del->protocol);
  1786. if (itr) {
  1787. list_del(&itr->list);
  1788. kfree(itr);
  1789. err = 0;
  1790. }
  1791. spin_unlock_bh(&dcb_lock);
  1792. return err;
  1793. }
  1794. EXPORT_SYMBOL(dcb_delrewr);
  1795. /**
  1796. * dcb_ieee_setapp - add IEEE dcb application data to app list
  1797. * @dev: network interface
  1798. * @new: application data to add
  1799. *
  1800. * This adds Application data to the list. Multiple application
  1801. * entries may exists for the same selector and protocol as long
  1802. * as the priorities are different. Priority is expected to be a
  1803. * 3-bit unsigned integer
  1804. */
  1805. int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
  1806. {
  1807. struct dcb_app_type event;
  1808. int err = 0;
  1809. event.ifindex = dev->ifindex;
  1810. memcpy(&event.app, new, sizeof(event.app));
  1811. if (dev->dcbnl_ops->getdcbx)
  1812. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1813. spin_lock_bh(&dcb_lock);
  1814. /* Search for existing match and abort if found */
  1815. if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
  1816. err = -EEXIST;
  1817. goto out;
  1818. }
  1819. err = dcb_app_add(&dcb_app_list, new, dev->ifindex);
  1820. out:
  1821. spin_unlock_bh(&dcb_lock);
  1822. if (!err)
  1823. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1824. return err;
  1825. }
  1826. EXPORT_SYMBOL(dcb_ieee_setapp);
  1827. /**
  1828. * dcb_ieee_delapp - delete IEEE dcb application data from list
  1829. * @dev: network interface
  1830. * @del: application data to delete
  1831. *
  1832. * This removes a matching APP data from the APP list
  1833. */
  1834. int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
  1835. {
  1836. struct dcb_app_type *itr;
  1837. struct dcb_app_type event;
  1838. int err = -ENOENT;
  1839. event.ifindex = dev->ifindex;
  1840. memcpy(&event.app, del, sizeof(event.app));
  1841. if (dev->dcbnl_ops->getdcbx)
  1842. event.dcbx = dev->dcbnl_ops->getdcbx(dev);
  1843. spin_lock_bh(&dcb_lock);
  1844. /* Search for existing match and remove it. */
  1845. if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
  1846. list_del(&itr->list);
  1847. kfree(itr);
  1848. err = 0;
  1849. }
  1850. spin_unlock_bh(&dcb_lock);
  1851. if (!err)
  1852. call_dcbevent_notifiers(DCB_APP_EVENT, &event);
  1853. return err;
  1854. }
  1855. EXPORT_SYMBOL(dcb_ieee_delapp);
  1856. /* dcb_getrewr_prio_pcp_mask_map - For a given device, find mapping from
  1857. * priorities to the PCP and DEI values assigned to that priority.
  1858. */
  1859. void dcb_getrewr_prio_pcp_mask_map(const struct net_device *dev,
  1860. struct dcb_rewr_prio_pcp_map *p_map)
  1861. {
  1862. int ifindex = dev->ifindex;
  1863. struct dcb_app_type *itr;
  1864. u8 prio;
  1865. memset(p_map->map, 0, sizeof(p_map->map));
  1866. spin_lock_bh(&dcb_lock);
  1867. list_for_each_entry(itr, &dcb_rewr_list, list) {
  1868. if (itr->ifindex == ifindex &&
  1869. itr->app.selector == DCB_APP_SEL_PCP &&
  1870. itr->app.protocol < 16 &&
  1871. itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
  1872. prio = itr->app.priority;
  1873. p_map->map[prio] |= 1 << itr->app.protocol;
  1874. }
  1875. }
  1876. spin_unlock_bh(&dcb_lock);
  1877. }
  1878. EXPORT_SYMBOL(dcb_getrewr_prio_pcp_mask_map);
  1879. /* dcb_getrewr_prio_dscp_mask_map - For a given device, find mapping from
  1880. * priorities to the DSCP values assigned to that priority.
  1881. */
  1882. void dcb_getrewr_prio_dscp_mask_map(const struct net_device *dev,
  1883. struct dcb_ieee_app_prio_map *p_map)
  1884. {
  1885. int ifindex = dev->ifindex;
  1886. struct dcb_app_type *itr;
  1887. u8 prio;
  1888. memset(p_map->map, 0, sizeof(p_map->map));
  1889. spin_lock_bh(&dcb_lock);
  1890. list_for_each_entry(itr, &dcb_rewr_list, list) {
  1891. if (itr->ifindex == ifindex &&
  1892. itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
  1893. itr->app.protocol < 64 &&
  1894. itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
  1895. prio = itr->app.priority;
  1896. p_map->map[prio] |= 1ULL << itr->app.protocol;
  1897. }
  1898. }
  1899. spin_unlock_bh(&dcb_lock);
  1900. }
  1901. EXPORT_SYMBOL(dcb_getrewr_prio_dscp_mask_map);
  1902. /*
  1903. * dcb_ieee_getapp_prio_dscp_mask_map - For a given device, find mapping from
  1904. * priorities to the DSCP values assigned to that priority. Initialize p_map
  1905. * such that each map element holds a bit mask of DSCP values configured for
  1906. * that priority by APP entries.
  1907. */
  1908. void dcb_ieee_getapp_prio_dscp_mask_map(const struct net_device *dev,
  1909. struct dcb_ieee_app_prio_map *p_map)
  1910. {
  1911. int ifindex = dev->ifindex;
  1912. struct dcb_app_type *itr;
  1913. u8 prio;
  1914. memset(p_map->map, 0, sizeof(p_map->map));
  1915. spin_lock_bh(&dcb_lock);
  1916. list_for_each_entry(itr, &dcb_app_list, list) {
  1917. if (itr->ifindex == ifindex &&
  1918. itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
  1919. itr->app.protocol < 64 &&
  1920. itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
  1921. prio = itr->app.priority;
  1922. p_map->map[prio] |= 1ULL << itr->app.protocol;
  1923. }
  1924. }
  1925. spin_unlock_bh(&dcb_lock);
  1926. }
  1927. EXPORT_SYMBOL(dcb_ieee_getapp_prio_dscp_mask_map);
  1928. /*
  1929. * dcb_ieee_getapp_dscp_prio_mask_map - For a given device, find mapping from
  1930. * DSCP values to the priorities assigned to that DSCP value. Initialize p_map
  1931. * such that each map element holds a bit mask of priorities configured for a
  1932. * given DSCP value by APP entries.
  1933. */
  1934. void
  1935. dcb_ieee_getapp_dscp_prio_mask_map(const struct net_device *dev,
  1936. struct dcb_ieee_app_dscp_map *p_map)
  1937. {
  1938. int ifindex = dev->ifindex;
  1939. struct dcb_app_type *itr;
  1940. memset(p_map->map, 0, sizeof(p_map->map));
  1941. spin_lock_bh(&dcb_lock);
  1942. list_for_each_entry(itr, &dcb_app_list, list) {
  1943. if (itr->ifindex == ifindex &&
  1944. itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
  1945. itr->app.protocol < 64 &&
  1946. itr->app.priority < IEEE_8021QAZ_MAX_TCS)
  1947. p_map->map[itr->app.protocol] |= 1 << itr->app.priority;
  1948. }
  1949. spin_unlock_bh(&dcb_lock);
  1950. }
  1951. EXPORT_SYMBOL(dcb_ieee_getapp_dscp_prio_mask_map);
  1952. /*
  1953. * Per 802.1Q-2014, the selector value of 1 is used for matching on Ethernet
  1954. * type, with valid PID values >= 1536. A special meaning is then assigned to
  1955. * protocol value of 0: "default priority. For use when priority is not
  1956. * otherwise specified".
  1957. *
  1958. * dcb_ieee_getapp_default_prio_mask - For a given device, find all APP entries
  1959. * of the form {$PRIO, ETHERTYPE, 0} and construct a bit mask of all default
  1960. * priorities set by these entries.
  1961. */
  1962. u8 dcb_ieee_getapp_default_prio_mask(const struct net_device *dev)
  1963. {
  1964. int ifindex = dev->ifindex;
  1965. struct dcb_app_type *itr;
  1966. u8 mask = 0;
  1967. spin_lock_bh(&dcb_lock);
  1968. list_for_each_entry(itr, &dcb_app_list, list) {
  1969. if (itr->ifindex == ifindex &&
  1970. itr->app.selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
  1971. itr->app.protocol == 0 &&
  1972. itr->app.priority < IEEE_8021QAZ_MAX_TCS)
  1973. mask |= 1 << itr->app.priority;
  1974. }
  1975. spin_unlock_bh(&dcb_lock);
  1976. return mask;
  1977. }
  1978. EXPORT_SYMBOL(dcb_ieee_getapp_default_prio_mask);
  1979. static void dcbnl_flush_dev(struct net_device *dev)
  1980. {
  1981. struct dcb_app_type *itr, *tmp;
  1982. spin_lock_bh(&dcb_lock);
  1983. list_for_each_entry_safe(itr, tmp, &dcb_app_list, list) {
  1984. if (itr->ifindex == dev->ifindex) {
  1985. list_del(&itr->list);
  1986. kfree(itr);
  1987. }
  1988. }
  1989. spin_unlock_bh(&dcb_lock);
  1990. }
  1991. static int dcbnl_netdevice_event(struct notifier_block *nb,
  1992. unsigned long event, void *ptr)
  1993. {
  1994. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1995. switch (event) {
  1996. case NETDEV_UNREGISTER:
  1997. if (!dev->dcbnl_ops)
  1998. return NOTIFY_DONE;
  1999. dcbnl_flush_dev(dev);
  2000. return NOTIFY_OK;
  2001. default:
  2002. return NOTIFY_DONE;
  2003. }
  2004. }
  2005. static struct notifier_block dcbnl_nb __read_mostly = {
  2006. .notifier_call = dcbnl_netdevice_event,
  2007. };
  2008. static int __init dcbnl_init(void)
  2009. {
  2010. int err;
  2011. err = register_netdevice_notifier(&dcbnl_nb);
  2012. if (err)
  2013. return err;
  2014. rtnl_register(PF_UNSPEC, RTM_GETDCB, dcb_doit, NULL, 0);
  2015. rtnl_register(PF_UNSPEC, RTM_SETDCB, dcb_doit, NULL, 0);
  2016. return 0;
  2017. }
  2018. device_initcall(dcbnl_init);