devlink.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390
  1. /*
  2. * net/core/devlink.c - Network physical/parent device Netlink interface
  3. *
  4. * Heavily inspired by net/wireless/
  5. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  6. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/gfp.h>
  18. #include <linux/device.h>
  19. #include <linux/list.h>
  20. #include <linux/netdevice.h>
  21. #include <rdma/ib_verbs.h>
  22. #include <net/netlink.h>
  23. #include <net/genetlink.h>
  24. #include <net/rtnetlink.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/devlink.h>
  28. #define CREATE_TRACE_POINTS
  29. #include <trace/events/devlink.h>
  30. static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
  31. {
  32. .name = "destination mac",
  33. .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
  34. .bitwidth = 48,
  35. },
  36. };
  37. struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
  38. .name = "ethernet",
  39. .id = DEVLINK_DPIPE_HEADER_ETHERNET,
  40. .fields = devlink_dpipe_fields_ethernet,
  41. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
  42. .global = true,
  43. };
  44. EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
  45. static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
  46. {
  47. .name = "destination ip",
  48. .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
  49. .bitwidth = 32,
  50. },
  51. };
  52. struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
  53. .name = "ipv4",
  54. .id = DEVLINK_DPIPE_HEADER_IPV4,
  55. .fields = devlink_dpipe_fields_ipv4,
  56. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
  57. .global = true,
  58. };
  59. EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
  60. static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
  61. {
  62. .name = "destination ip",
  63. .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
  64. .bitwidth = 128,
  65. },
  66. };
  67. struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
  68. .name = "ipv6",
  69. .id = DEVLINK_DPIPE_HEADER_IPV6,
  70. .fields = devlink_dpipe_fields_ipv6,
  71. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
  72. .global = true,
  73. };
  74. EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
  75. EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
  76. static LIST_HEAD(devlink_list);
  77. /* devlink_mutex
  78. *
  79. * An overall lock guarding every operation coming from userspace.
  80. * It also guards devlink devices list and it is taken when
  81. * driver registers/unregisters it.
  82. */
  83. static DEFINE_MUTEX(devlink_mutex);
  84. static struct net *devlink_net(const struct devlink *devlink)
  85. {
  86. return read_pnet(&devlink->_net);
  87. }
  88. static void devlink_net_set(struct devlink *devlink, struct net *net)
  89. {
  90. write_pnet(&devlink->_net, net);
  91. }
  92. static struct devlink *devlink_get_from_attrs(struct net *net,
  93. struct nlattr **attrs)
  94. {
  95. struct devlink *devlink;
  96. char *busname;
  97. char *devname;
  98. if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
  99. return ERR_PTR(-EINVAL);
  100. busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
  101. devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
  102. list_for_each_entry(devlink, &devlink_list, list) {
  103. if (strcmp(devlink->dev->bus->name, busname) == 0 &&
  104. strcmp(dev_name(devlink->dev), devname) == 0 &&
  105. net_eq(devlink_net(devlink), net))
  106. return devlink;
  107. }
  108. return ERR_PTR(-ENODEV);
  109. }
  110. static struct devlink *devlink_get_from_info(struct genl_info *info)
  111. {
  112. return devlink_get_from_attrs(genl_info_net(info), info->attrs);
  113. }
  114. static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
  115. int port_index)
  116. {
  117. struct devlink_port *devlink_port;
  118. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  119. if (devlink_port->index == port_index)
  120. return devlink_port;
  121. }
  122. return NULL;
  123. }
  124. static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
  125. {
  126. return devlink_port_get_by_index(devlink, port_index);
  127. }
  128. static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
  129. struct nlattr **attrs)
  130. {
  131. if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
  132. u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
  133. struct devlink_port *devlink_port;
  134. devlink_port = devlink_port_get_by_index(devlink, port_index);
  135. if (!devlink_port)
  136. return ERR_PTR(-ENODEV);
  137. return devlink_port;
  138. }
  139. return ERR_PTR(-EINVAL);
  140. }
  141. static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
  142. struct genl_info *info)
  143. {
  144. return devlink_port_get_from_attrs(devlink, info->attrs);
  145. }
  146. struct devlink_sb {
  147. struct list_head list;
  148. unsigned int index;
  149. u32 size;
  150. u16 ingress_pools_count;
  151. u16 egress_pools_count;
  152. u16 ingress_tc_count;
  153. u16 egress_tc_count;
  154. };
  155. static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
  156. {
  157. return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
  158. }
  159. static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
  160. unsigned int sb_index)
  161. {
  162. struct devlink_sb *devlink_sb;
  163. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  164. if (devlink_sb->index == sb_index)
  165. return devlink_sb;
  166. }
  167. return NULL;
  168. }
  169. static bool devlink_sb_index_exists(struct devlink *devlink,
  170. unsigned int sb_index)
  171. {
  172. return devlink_sb_get_by_index(devlink, sb_index);
  173. }
  174. static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
  175. struct nlattr **attrs)
  176. {
  177. if (attrs[DEVLINK_ATTR_SB_INDEX]) {
  178. u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
  179. struct devlink_sb *devlink_sb;
  180. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  181. if (!devlink_sb)
  182. return ERR_PTR(-ENODEV);
  183. return devlink_sb;
  184. }
  185. return ERR_PTR(-EINVAL);
  186. }
  187. static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
  188. struct genl_info *info)
  189. {
  190. return devlink_sb_get_from_attrs(devlink, info->attrs);
  191. }
  192. static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
  193. struct nlattr **attrs,
  194. u16 *p_pool_index)
  195. {
  196. u16 val;
  197. if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
  198. return -EINVAL;
  199. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
  200. if (val >= devlink_sb_pool_count(devlink_sb))
  201. return -EINVAL;
  202. *p_pool_index = val;
  203. return 0;
  204. }
  205. static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
  206. struct genl_info *info,
  207. u16 *p_pool_index)
  208. {
  209. return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
  210. p_pool_index);
  211. }
  212. static int
  213. devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
  214. enum devlink_sb_pool_type *p_pool_type)
  215. {
  216. u8 val;
  217. if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
  218. return -EINVAL;
  219. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
  220. if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
  221. val != DEVLINK_SB_POOL_TYPE_EGRESS)
  222. return -EINVAL;
  223. *p_pool_type = val;
  224. return 0;
  225. }
  226. static int
  227. devlink_sb_pool_type_get_from_info(struct genl_info *info,
  228. enum devlink_sb_pool_type *p_pool_type)
  229. {
  230. return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
  231. }
  232. static int
  233. devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
  234. enum devlink_sb_threshold_type *p_th_type)
  235. {
  236. u8 val;
  237. if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
  238. return -EINVAL;
  239. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
  240. if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
  241. val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
  242. return -EINVAL;
  243. *p_th_type = val;
  244. return 0;
  245. }
  246. static int
  247. devlink_sb_th_type_get_from_info(struct genl_info *info,
  248. enum devlink_sb_threshold_type *p_th_type)
  249. {
  250. return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
  251. }
  252. static int
  253. devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
  254. struct nlattr **attrs,
  255. enum devlink_sb_pool_type pool_type,
  256. u16 *p_tc_index)
  257. {
  258. u16 val;
  259. if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
  260. return -EINVAL;
  261. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
  262. if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
  263. val >= devlink_sb->ingress_tc_count)
  264. return -EINVAL;
  265. if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
  266. val >= devlink_sb->egress_tc_count)
  267. return -EINVAL;
  268. *p_tc_index = val;
  269. return 0;
  270. }
  271. static int
  272. devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
  273. struct genl_info *info,
  274. enum devlink_sb_pool_type pool_type,
  275. u16 *p_tc_index)
  276. {
  277. return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
  278. pool_type, p_tc_index);
  279. }
  280. #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
  281. #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
  282. #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
  283. /* The per devlink instance lock is taken by default in the pre-doit
  284. * operation, yet several commands do not require this. The global
  285. * devlink lock is taken and protects from disruption by user-calls.
  286. */
  287. #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
  288. static int devlink_nl_pre_doit(const struct genl_ops *ops,
  289. struct sk_buff *skb, struct genl_info *info)
  290. {
  291. struct devlink *devlink;
  292. int err;
  293. mutex_lock(&devlink_mutex);
  294. devlink = devlink_get_from_info(info);
  295. if (IS_ERR(devlink)) {
  296. mutex_unlock(&devlink_mutex);
  297. return PTR_ERR(devlink);
  298. }
  299. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  300. mutex_lock(&devlink->lock);
  301. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
  302. info->user_ptr[0] = devlink;
  303. } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
  304. struct devlink_port *devlink_port;
  305. devlink_port = devlink_port_get_from_info(devlink, info);
  306. if (IS_ERR(devlink_port)) {
  307. err = PTR_ERR(devlink_port);
  308. goto unlock;
  309. }
  310. info->user_ptr[0] = devlink_port;
  311. }
  312. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
  313. struct devlink_sb *devlink_sb;
  314. devlink_sb = devlink_sb_get_from_info(devlink, info);
  315. if (IS_ERR(devlink_sb)) {
  316. err = PTR_ERR(devlink_sb);
  317. goto unlock;
  318. }
  319. info->user_ptr[1] = devlink_sb;
  320. }
  321. return 0;
  322. unlock:
  323. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  324. mutex_unlock(&devlink->lock);
  325. mutex_unlock(&devlink_mutex);
  326. return err;
  327. }
  328. static void devlink_nl_post_doit(const struct genl_ops *ops,
  329. struct sk_buff *skb, struct genl_info *info)
  330. {
  331. struct devlink *devlink;
  332. devlink = devlink_get_from_info(info);
  333. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  334. mutex_unlock(&devlink->lock);
  335. mutex_unlock(&devlink_mutex);
  336. }
  337. static struct genl_family devlink_nl_family;
  338. enum devlink_multicast_groups {
  339. DEVLINK_MCGRP_CONFIG,
  340. };
  341. static const struct genl_multicast_group devlink_nl_mcgrps[] = {
  342. [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
  343. };
  344. static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  345. {
  346. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  347. return -EMSGSIZE;
  348. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  349. return -EMSGSIZE;
  350. return 0;
  351. }
  352. static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
  353. enum devlink_command cmd, u32 portid,
  354. u32 seq, int flags)
  355. {
  356. void *hdr;
  357. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  358. if (!hdr)
  359. return -EMSGSIZE;
  360. if (devlink_nl_put_handle(msg, devlink))
  361. goto nla_put_failure;
  362. genlmsg_end(msg, hdr);
  363. return 0;
  364. nla_put_failure:
  365. genlmsg_cancel(msg, hdr);
  366. return -EMSGSIZE;
  367. }
  368. static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
  369. {
  370. struct sk_buff *msg;
  371. int err;
  372. WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
  373. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  374. if (!msg)
  375. return;
  376. err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
  377. if (err) {
  378. nlmsg_free(msg);
  379. return;
  380. }
  381. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  382. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  383. }
  384. static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
  385. struct devlink_port *devlink_port,
  386. enum devlink_command cmd, u32 portid,
  387. u32 seq, int flags)
  388. {
  389. void *hdr;
  390. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  391. if (!hdr)
  392. return -EMSGSIZE;
  393. if (devlink_nl_put_handle(msg, devlink))
  394. goto nla_put_failure;
  395. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  396. goto nla_put_failure;
  397. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  398. goto nla_put_failure;
  399. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  400. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  401. devlink_port->desired_type))
  402. goto nla_put_failure;
  403. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  404. struct net_device *netdev = devlink_port->type_dev;
  405. if (netdev &&
  406. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  407. netdev->ifindex) ||
  408. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  409. netdev->name)))
  410. goto nla_put_failure;
  411. }
  412. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  413. struct ib_device *ibdev = devlink_port->type_dev;
  414. if (ibdev &&
  415. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  416. ibdev->name))
  417. goto nla_put_failure;
  418. }
  419. if (devlink_port->split &&
  420. nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
  421. devlink_port->split_group))
  422. goto nla_put_failure;
  423. genlmsg_end(msg, hdr);
  424. return 0;
  425. nla_put_failure:
  426. genlmsg_cancel(msg, hdr);
  427. return -EMSGSIZE;
  428. }
  429. static void devlink_port_notify(struct devlink_port *devlink_port,
  430. enum devlink_command cmd)
  431. {
  432. struct devlink *devlink = devlink_port->devlink;
  433. struct sk_buff *msg;
  434. int err;
  435. if (!devlink_port->registered)
  436. return;
  437. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  438. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  439. if (!msg)
  440. return;
  441. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  442. if (err) {
  443. nlmsg_free(msg);
  444. return;
  445. }
  446. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  447. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  448. }
  449. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  450. {
  451. struct devlink *devlink = info->user_ptr[0];
  452. struct sk_buff *msg;
  453. int err;
  454. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  455. if (!msg)
  456. return -ENOMEM;
  457. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  458. info->snd_portid, info->snd_seq, 0);
  459. if (err) {
  460. nlmsg_free(msg);
  461. return err;
  462. }
  463. return genlmsg_reply(msg, info);
  464. }
  465. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  466. struct netlink_callback *cb)
  467. {
  468. struct devlink *devlink;
  469. int start = cb->args[0];
  470. int idx = 0;
  471. int err;
  472. mutex_lock(&devlink_mutex);
  473. list_for_each_entry(devlink, &devlink_list, list) {
  474. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  475. continue;
  476. if (idx < start) {
  477. idx++;
  478. continue;
  479. }
  480. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  481. NETLINK_CB(cb->skb).portid,
  482. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  483. if (err)
  484. goto out;
  485. idx++;
  486. }
  487. out:
  488. mutex_unlock(&devlink_mutex);
  489. cb->args[0] = idx;
  490. return msg->len;
  491. }
  492. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  493. struct genl_info *info)
  494. {
  495. struct devlink_port *devlink_port = info->user_ptr[0];
  496. struct devlink *devlink = devlink_port->devlink;
  497. struct sk_buff *msg;
  498. int err;
  499. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  500. if (!msg)
  501. return -ENOMEM;
  502. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  503. DEVLINK_CMD_PORT_NEW,
  504. info->snd_portid, info->snd_seq, 0);
  505. if (err) {
  506. nlmsg_free(msg);
  507. return err;
  508. }
  509. return genlmsg_reply(msg, info);
  510. }
  511. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  512. struct netlink_callback *cb)
  513. {
  514. struct devlink *devlink;
  515. struct devlink_port *devlink_port;
  516. int start = cb->args[0];
  517. int idx = 0;
  518. int err;
  519. mutex_lock(&devlink_mutex);
  520. list_for_each_entry(devlink, &devlink_list, list) {
  521. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  522. continue;
  523. mutex_lock(&devlink->lock);
  524. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  525. if (idx < start) {
  526. idx++;
  527. continue;
  528. }
  529. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  530. DEVLINK_CMD_NEW,
  531. NETLINK_CB(cb->skb).portid,
  532. cb->nlh->nlmsg_seq,
  533. NLM_F_MULTI);
  534. if (err) {
  535. mutex_unlock(&devlink->lock);
  536. goto out;
  537. }
  538. idx++;
  539. }
  540. mutex_unlock(&devlink->lock);
  541. }
  542. out:
  543. mutex_unlock(&devlink_mutex);
  544. cb->args[0] = idx;
  545. return msg->len;
  546. }
  547. static int devlink_port_type_set(struct devlink *devlink,
  548. struct devlink_port *devlink_port,
  549. enum devlink_port_type port_type)
  550. {
  551. int err;
  552. if (devlink->ops && devlink->ops->port_type_set) {
  553. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  554. return -EINVAL;
  555. if (port_type == devlink_port->type)
  556. return 0;
  557. err = devlink->ops->port_type_set(devlink_port, port_type);
  558. if (err)
  559. return err;
  560. devlink_port->desired_type = port_type;
  561. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  562. return 0;
  563. }
  564. return -EOPNOTSUPP;
  565. }
  566. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  567. struct genl_info *info)
  568. {
  569. struct devlink_port *devlink_port = info->user_ptr[0];
  570. struct devlink *devlink = devlink_port->devlink;
  571. int err;
  572. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  573. enum devlink_port_type port_type;
  574. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  575. err = devlink_port_type_set(devlink, devlink_port, port_type);
  576. if (err)
  577. return err;
  578. }
  579. return 0;
  580. }
  581. static int devlink_port_split(struct devlink *devlink,
  582. u32 port_index, u32 count)
  583. {
  584. if (devlink->ops && devlink->ops->port_split)
  585. return devlink->ops->port_split(devlink, port_index, count);
  586. return -EOPNOTSUPP;
  587. }
  588. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  589. struct genl_info *info)
  590. {
  591. struct devlink *devlink = info->user_ptr[0];
  592. u32 port_index;
  593. u32 count;
  594. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  595. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  596. return -EINVAL;
  597. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  598. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  599. return devlink_port_split(devlink, port_index, count);
  600. }
  601. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index)
  602. {
  603. if (devlink->ops && devlink->ops->port_unsplit)
  604. return devlink->ops->port_unsplit(devlink, port_index);
  605. return -EOPNOTSUPP;
  606. }
  607. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  608. struct genl_info *info)
  609. {
  610. struct devlink *devlink = info->user_ptr[0];
  611. u32 port_index;
  612. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  613. return -EINVAL;
  614. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  615. return devlink_port_unsplit(devlink, port_index);
  616. }
  617. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  618. struct devlink_sb *devlink_sb,
  619. enum devlink_command cmd, u32 portid,
  620. u32 seq, int flags)
  621. {
  622. void *hdr;
  623. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  624. if (!hdr)
  625. return -EMSGSIZE;
  626. if (devlink_nl_put_handle(msg, devlink))
  627. goto nla_put_failure;
  628. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  629. goto nla_put_failure;
  630. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  631. goto nla_put_failure;
  632. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  633. devlink_sb->ingress_pools_count))
  634. goto nla_put_failure;
  635. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  636. devlink_sb->egress_pools_count))
  637. goto nla_put_failure;
  638. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  639. devlink_sb->ingress_tc_count))
  640. goto nla_put_failure;
  641. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  642. devlink_sb->egress_tc_count))
  643. goto nla_put_failure;
  644. genlmsg_end(msg, hdr);
  645. return 0;
  646. nla_put_failure:
  647. genlmsg_cancel(msg, hdr);
  648. return -EMSGSIZE;
  649. }
  650. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  651. struct genl_info *info)
  652. {
  653. struct devlink *devlink = info->user_ptr[0];
  654. struct devlink_sb *devlink_sb = info->user_ptr[1];
  655. struct sk_buff *msg;
  656. int err;
  657. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  658. if (!msg)
  659. return -ENOMEM;
  660. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  661. DEVLINK_CMD_SB_NEW,
  662. info->snd_portid, info->snd_seq, 0);
  663. if (err) {
  664. nlmsg_free(msg);
  665. return err;
  666. }
  667. return genlmsg_reply(msg, info);
  668. }
  669. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  670. struct netlink_callback *cb)
  671. {
  672. struct devlink *devlink;
  673. struct devlink_sb *devlink_sb;
  674. int start = cb->args[0];
  675. int idx = 0;
  676. int err;
  677. mutex_lock(&devlink_mutex);
  678. list_for_each_entry(devlink, &devlink_list, list) {
  679. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  680. continue;
  681. mutex_lock(&devlink->lock);
  682. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  683. if (idx < start) {
  684. idx++;
  685. continue;
  686. }
  687. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  688. DEVLINK_CMD_SB_NEW,
  689. NETLINK_CB(cb->skb).portid,
  690. cb->nlh->nlmsg_seq,
  691. NLM_F_MULTI);
  692. if (err) {
  693. mutex_unlock(&devlink->lock);
  694. goto out;
  695. }
  696. idx++;
  697. }
  698. mutex_unlock(&devlink->lock);
  699. }
  700. out:
  701. mutex_unlock(&devlink_mutex);
  702. cb->args[0] = idx;
  703. return msg->len;
  704. }
  705. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  706. struct devlink_sb *devlink_sb,
  707. u16 pool_index, enum devlink_command cmd,
  708. u32 portid, u32 seq, int flags)
  709. {
  710. struct devlink_sb_pool_info pool_info;
  711. void *hdr;
  712. int err;
  713. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  714. pool_index, &pool_info);
  715. if (err)
  716. return err;
  717. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  718. if (!hdr)
  719. return -EMSGSIZE;
  720. if (devlink_nl_put_handle(msg, devlink))
  721. goto nla_put_failure;
  722. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  723. goto nla_put_failure;
  724. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  725. goto nla_put_failure;
  726. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  727. goto nla_put_failure;
  728. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  729. goto nla_put_failure;
  730. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  731. pool_info.threshold_type))
  732. goto nla_put_failure;
  733. genlmsg_end(msg, hdr);
  734. return 0;
  735. nla_put_failure:
  736. genlmsg_cancel(msg, hdr);
  737. return -EMSGSIZE;
  738. }
  739. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  740. struct genl_info *info)
  741. {
  742. struct devlink *devlink = info->user_ptr[0];
  743. struct devlink_sb *devlink_sb = info->user_ptr[1];
  744. struct sk_buff *msg;
  745. u16 pool_index;
  746. int err;
  747. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  748. &pool_index);
  749. if (err)
  750. return err;
  751. if (!devlink->ops || !devlink->ops->sb_pool_get)
  752. return -EOPNOTSUPP;
  753. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  754. if (!msg)
  755. return -ENOMEM;
  756. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  757. DEVLINK_CMD_SB_POOL_NEW,
  758. info->snd_portid, info->snd_seq, 0);
  759. if (err) {
  760. nlmsg_free(msg);
  761. return err;
  762. }
  763. return genlmsg_reply(msg, info);
  764. }
  765. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  766. struct devlink *devlink,
  767. struct devlink_sb *devlink_sb,
  768. u32 portid, u32 seq)
  769. {
  770. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  771. u16 pool_index;
  772. int err;
  773. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  774. if (*p_idx < start) {
  775. (*p_idx)++;
  776. continue;
  777. }
  778. err = devlink_nl_sb_pool_fill(msg, devlink,
  779. devlink_sb,
  780. pool_index,
  781. DEVLINK_CMD_SB_POOL_NEW,
  782. portid, seq, NLM_F_MULTI);
  783. if (err)
  784. return err;
  785. (*p_idx)++;
  786. }
  787. return 0;
  788. }
  789. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  790. struct netlink_callback *cb)
  791. {
  792. struct devlink *devlink;
  793. struct devlink_sb *devlink_sb;
  794. int start = cb->args[0];
  795. int idx = 0;
  796. int err;
  797. mutex_lock(&devlink_mutex);
  798. list_for_each_entry(devlink, &devlink_list, list) {
  799. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  800. !devlink->ops || !devlink->ops->sb_pool_get)
  801. continue;
  802. mutex_lock(&devlink->lock);
  803. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  804. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  805. devlink_sb,
  806. NETLINK_CB(cb->skb).portid,
  807. cb->nlh->nlmsg_seq);
  808. if (err && err != -EOPNOTSUPP) {
  809. mutex_unlock(&devlink->lock);
  810. goto out;
  811. }
  812. }
  813. mutex_unlock(&devlink->lock);
  814. }
  815. out:
  816. mutex_unlock(&devlink_mutex);
  817. cb->args[0] = idx;
  818. return msg->len;
  819. }
  820. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  821. u16 pool_index, u32 size,
  822. enum devlink_sb_threshold_type threshold_type)
  823. {
  824. const struct devlink_ops *ops = devlink->ops;
  825. if (ops && ops->sb_pool_set)
  826. return ops->sb_pool_set(devlink, sb_index, pool_index,
  827. size, threshold_type);
  828. return -EOPNOTSUPP;
  829. }
  830. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  831. struct genl_info *info)
  832. {
  833. struct devlink *devlink = info->user_ptr[0];
  834. struct devlink_sb *devlink_sb = info->user_ptr[1];
  835. enum devlink_sb_threshold_type threshold_type;
  836. u16 pool_index;
  837. u32 size;
  838. int err;
  839. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  840. &pool_index);
  841. if (err)
  842. return err;
  843. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  844. if (err)
  845. return err;
  846. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  847. return -EINVAL;
  848. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  849. return devlink_sb_pool_set(devlink, devlink_sb->index,
  850. pool_index, size, threshold_type);
  851. }
  852. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  853. struct devlink *devlink,
  854. struct devlink_port *devlink_port,
  855. struct devlink_sb *devlink_sb,
  856. u16 pool_index,
  857. enum devlink_command cmd,
  858. u32 portid, u32 seq, int flags)
  859. {
  860. const struct devlink_ops *ops = devlink->ops;
  861. u32 threshold;
  862. void *hdr;
  863. int err;
  864. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  865. pool_index, &threshold);
  866. if (err)
  867. return err;
  868. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  869. if (!hdr)
  870. return -EMSGSIZE;
  871. if (devlink_nl_put_handle(msg, devlink))
  872. goto nla_put_failure;
  873. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  874. goto nla_put_failure;
  875. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  876. goto nla_put_failure;
  877. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  878. goto nla_put_failure;
  879. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  880. goto nla_put_failure;
  881. if (ops->sb_occ_port_pool_get) {
  882. u32 cur;
  883. u32 max;
  884. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  885. pool_index, &cur, &max);
  886. if (err && err != -EOPNOTSUPP)
  887. return err;
  888. if (!err) {
  889. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  890. goto nla_put_failure;
  891. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  892. goto nla_put_failure;
  893. }
  894. }
  895. genlmsg_end(msg, hdr);
  896. return 0;
  897. nla_put_failure:
  898. genlmsg_cancel(msg, hdr);
  899. return -EMSGSIZE;
  900. }
  901. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  902. struct genl_info *info)
  903. {
  904. struct devlink_port *devlink_port = info->user_ptr[0];
  905. struct devlink *devlink = devlink_port->devlink;
  906. struct devlink_sb *devlink_sb = info->user_ptr[1];
  907. struct sk_buff *msg;
  908. u16 pool_index;
  909. int err;
  910. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  911. &pool_index);
  912. if (err)
  913. return err;
  914. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  915. return -EOPNOTSUPP;
  916. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  917. if (!msg)
  918. return -ENOMEM;
  919. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  920. devlink_sb, pool_index,
  921. DEVLINK_CMD_SB_PORT_POOL_NEW,
  922. info->snd_portid, info->snd_seq, 0);
  923. if (err) {
  924. nlmsg_free(msg);
  925. return err;
  926. }
  927. return genlmsg_reply(msg, info);
  928. }
  929. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  930. struct devlink *devlink,
  931. struct devlink_sb *devlink_sb,
  932. u32 portid, u32 seq)
  933. {
  934. struct devlink_port *devlink_port;
  935. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  936. u16 pool_index;
  937. int err;
  938. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  939. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  940. if (*p_idx < start) {
  941. (*p_idx)++;
  942. continue;
  943. }
  944. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  945. devlink_port,
  946. devlink_sb,
  947. pool_index,
  948. DEVLINK_CMD_SB_PORT_POOL_NEW,
  949. portid, seq,
  950. NLM_F_MULTI);
  951. if (err)
  952. return err;
  953. (*p_idx)++;
  954. }
  955. }
  956. return 0;
  957. }
  958. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  959. struct netlink_callback *cb)
  960. {
  961. struct devlink *devlink;
  962. struct devlink_sb *devlink_sb;
  963. int start = cb->args[0];
  964. int idx = 0;
  965. int err;
  966. mutex_lock(&devlink_mutex);
  967. list_for_each_entry(devlink, &devlink_list, list) {
  968. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  969. !devlink->ops || !devlink->ops->sb_port_pool_get)
  970. continue;
  971. mutex_lock(&devlink->lock);
  972. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  973. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  974. devlink, devlink_sb,
  975. NETLINK_CB(cb->skb).portid,
  976. cb->nlh->nlmsg_seq);
  977. if (err && err != -EOPNOTSUPP) {
  978. mutex_unlock(&devlink->lock);
  979. goto out;
  980. }
  981. }
  982. mutex_unlock(&devlink->lock);
  983. }
  984. out:
  985. mutex_unlock(&devlink_mutex);
  986. cb->args[0] = idx;
  987. return msg->len;
  988. }
  989. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  990. unsigned int sb_index, u16 pool_index,
  991. u32 threshold)
  992. {
  993. const struct devlink_ops *ops = devlink_port->devlink->ops;
  994. if (ops && ops->sb_port_pool_set)
  995. return ops->sb_port_pool_set(devlink_port, sb_index,
  996. pool_index, threshold);
  997. return -EOPNOTSUPP;
  998. }
  999. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  1000. struct genl_info *info)
  1001. {
  1002. struct devlink_port *devlink_port = info->user_ptr[0];
  1003. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1004. u16 pool_index;
  1005. u32 threshold;
  1006. int err;
  1007. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1008. &pool_index);
  1009. if (err)
  1010. return err;
  1011. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1012. return -EINVAL;
  1013. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1014. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  1015. pool_index, threshold);
  1016. }
  1017. static int
  1018. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  1019. struct devlink_port *devlink_port,
  1020. struct devlink_sb *devlink_sb, u16 tc_index,
  1021. enum devlink_sb_pool_type pool_type,
  1022. enum devlink_command cmd,
  1023. u32 portid, u32 seq, int flags)
  1024. {
  1025. const struct devlink_ops *ops = devlink->ops;
  1026. u16 pool_index;
  1027. u32 threshold;
  1028. void *hdr;
  1029. int err;
  1030. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  1031. tc_index, pool_type,
  1032. &pool_index, &threshold);
  1033. if (err)
  1034. return err;
  1035. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1036. if (!hdr)
  1037. return -EMSGSIZE;
  1038. if (devlink_nl_put_handle(msg, devlink))
  1039. goto nla_put_failure;
  1040. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  1041. goto nla_put_failure;
  1042. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  1043. goto nla_put_failure;
  1044. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  1045. goto nla_put_failure;
  1046. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  1047. goto nla_put_failure;
  1048. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  1049. goto nla_put_failure;
  1050. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  1051. goto nla_put_failure;
  1052. if (ops->sb_occ_tc_port_bind_get) {
  1053. u32 cur;
  1054. u32 max;
  1055. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1056. devlink_sb->index,
  1057. tc_index, pool_type,
  1058. &cur, &max);
  1059. if (err && err != -EOPNOTSUPP)
  1060. return err;
  1061. if (!err) {
  1062. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1063. goto nla_put_failure;
  1064. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1065. goto nla_put_failure;
  1066. }
  1067. }
  1068. genlmsg_end(msg, hdr);
  1069. return 0;
  1070. nla_put_failure:
  1071. genlmsg_cancel(msg, hdr);
  1072. return -EMSGSIZE;
  1073. }
  1074. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1075. struct genl_info *info)
  1076. {
  1077. struct devlink_port *devlink_port = info->user_ptr[0];
  1078. struct devlink *devlink = devlink_port->devlink;
  1079. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1080. struct sk_buff *msg;
  1081. enum devlink_sb_pool_type pool_type;
  1082. u16 tc_index;
  1083. int err;
  1084. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1085. if (err)
  1086. return err;
  1087. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1088. pool_type, &tc_index);
  1089. if (err)
  1090. return err;
  1091. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1092. return -EOPNOTSUPP;
  1093. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1094. if (!msg)
  1095. return -ENOMEM;
  1096. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1097. devlink_sb, tc_index, pool_type,
  1098. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1099. info->snd_portid,
  1100. info->snd_seq, 0);
  1101. if (err) {
  1102. nlmsg_free(msg);
  1103. return err;
  1104. }
  1105. return genlmsg_reply(msg, info);
  1106. }
  1107. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1108. int start, int *p_idx,
  1109. struct devlink *devlink,
  1110. struct devlink_sb *devlink_sb,
  1111. u32 portid, u32 seq)
  1112. {
  1113. struct devlink_port *devlink_port;
  1114. u16 tc_index;
  1115. int err;
  1116. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1117. for (tc_index = 0;
  1118. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1119. if (*p_idx < start) {
  1120. (*p_idx)++;
  1121. continue;
  1122. }
  1123. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1124. devlink_port,
  1125. devlink_sb,
  1126. tc_index,
  1127. DEVLINK_SB_POOL_TYPE_INGRESS,
  1128. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1129. portid, seq,
  1130. NLM_F_MULTI);
  1131. if (err)
  1132. return err;
  1133. (*p_idx)++;
  1134. }
  1135. for (tc_index = 0;
  1136. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1137. if (*p_idx < start) {
  1138. (*p_idx)++;
  1139. continue;
  1140. }
  1141. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1142. devlink_port,
  1143. devlink_sb,
  1144. tc_index,
  1145. DEVLINK_SB_POOL_TYPE_EGRESS,
  1146. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1147. portid, seq,
  1148. NLM_F_MULTI);
  1149. if (err)
  1150. return err;
  1151. (*p_idx)++;
  1152. }
  1153. }
  1154. return 0;
  1155. }
  1156. static int
  1157. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1158. struct netlink_callback *cb)
  1159. {
  1160. struct devlink *devlink;
  1161. struct devlink_sb *devlink_sb;
  1162. int start = cb->args[0];
  1163. int idx = 0;
  1164. int err;
  1165. mutex_lock(&devlink_mutex);
  1166. list_for_each_entry(devlink, &devlink_list, list) {
  1167. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1168. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1169. continue;
  1170. mutex_lock(&devlink->lock);
  1171. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1172. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1173. devlink,
  1174. devlink_sb,
  1175. NETLINK_CB(cb->skb).portid,
  1176. cb->nlh->nlmsg_seq);
  1177. if (err && err != -EOPNOTSUPP) {
  1178. mutex_unlock(&devlink->lock);
  1179. goto out;
  1180. }
  1181. }
  1182. mutex_unlock(&devlink->lock);
  1183. }
  1184. out:
  1185. mutex_unlock(&devlink_mutex);
  1186. cb->args[0] = idx;
  1187. return msg->len;
  1188. }
  1189. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1190. unsigned int sb_index, u16 tc_index,
  1191. enum devlink_sb_pool_type pool_type,
  1192. u16 pool_index, u32 threshold)
  1193. {
  1194. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1195. if (ops && ops->sb_tc_pool_bind_set)
  1196. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1197. tc_index, pool_type,
  1198. pool_index, threshold);
  1199. return -EOPNOTSUPP;
  1200. }
  1201. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1202. struct genl_info *info)
  1203. {
  1204. struct devlink_port *devlink_port = info->user_ptr[0];
  1205. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1206. enum devlink_sb_pool_type pool_type;
  1207. u16 tc_index;
  1208. u16 pool_index;
  1209. u32 threshold;
  1210. int err;
  1211. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1212. if (err)
  1213. return err;
  1214. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1215. pool_type, &tc_index);
  1216. if (err)
  1217. return err;
  1218. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1219. &pool_index);
  1220. if (err)
  1221. return err;
  1222. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1223. return -EINVAL;
  1224. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1225. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1226. tc_index, pool_type,
  1227. pool_index, threshold);
  1228. }
  1229. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1230. struct genl_info *info)
  1231. {
  1232. struct devlink *devlink = info->user_ptr[0];
  1233. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1234. const struct devlink_ops *ops = devlink->ops;
  1235. if (ops && ops->sb_occ_snapshot)
  1236. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1237. return -EOPNOTSUPP;
  1238. }
  1239. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1240. struct genl_info *info)
  1241. {
  1242. struct devlink *devlink = info->user_ptr[0];
  1243. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1244. const struct devlink_ops *ops = devlink->ops;
  1245. if (ops && ops->sb_occ_max_clear)
  1246. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1247. return -EOPNOTSUPP;
  1248. }
  1249. static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
  1250. enum devlink_command cmd, u32 portid,
  1251. u32 seq, int flags)
  1252. {
  1253. const struct devlink_ops *ops = devlink->ops;
  1254. u8 inline_mode, encap_mode;
  1255. void *hdr;
  1256. int err = 0;
  1257. u16 mode;
  1258. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1259. if (!hdr)
  1260. return -EMSGSIZE;
  1261. err = devlink_nl_put_handle(msg, devlink);
  1262. if (err)
  1263. goto nla_put_failure;
  1264. if (ops->eswitch_mode_get) {
  1265. err = ops->eswitch_mode_get(devlink, &mode);
  1266. if (err)
  1267. goto nla_put_failure;
  1268. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1269. if (err)
  1270. goto nla_put_failure;
  1271. }
  1272. if (ops->eswitch_inline_mode_get) {
  1273. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1274. if (err)
  1275. goto nla_put_failure;
  1276. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1277. inline_mode);
  1278. if (err)
  1279. goto nla_put_failure;
  1280. }
  1281. if (ops->eswitch_encap_mode_get) {
  1282. err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
  1283. if (err)
  1284. goto nla_put_failure;
  1285. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
  1286. if (err)
  1287. goto nla_put_failure;
  1288. }
  1289. genlmsg_end(msg, hdr);
  1290. return 0;
  1291. nla_put_failure:
  1292. genlmsg_cancel(msg, hdr);
  1293. return err;
  1294. }
  1295. static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
  1296. struct genl_info *info)
  1297. {
  1298. struct devlink *devlink = info->user_ptr[0];
  1299. const struct devlink_ops *ops = devlink->ops;
  1300. struct sk_buff *msg;
  1301. int err;
  1302. if (!ops)
  1303. return -EOPNOTSUPP;
  1304. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1305. if (!msg)
  1306. return -ENOMEM;
  1307. err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
  1308. info->snd_portid, info->snd_seq, 0);
  1309. if (err) {
  1310. nlmsg_free(msg);
  1311. return err;
  1312. }
  1313. return genlmsg_reply(msg, info);
  1314. }
  1315. static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
  1316. struct genl_info *info)
  1317. {
  1318. struct devlink *devlink = info->user_ptr[0];
  1319. const struct devlink_ops *ops = devlink->ops;
  1320. u8 inline_mode, encap_mode;
  1321. int err = 0;
  1322. u16 mode;
  1323. if (!ops)
  1324. return -EOPNOTSUPP;
  1325. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1326. if (!ops->eswitch_mode_set)
  1327. return -EOPNOTSUPP;
  1328. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1329. err = ops->eswitch_mode_set(devlink, mode);
  1330. if (err)
  1331. return err;
  1332. }
  1333. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1334. if (!ops->eswitch_inline_mode_set)
  1335. return -EOPNOTSUPP;
  1336. inline_mode = nla_get_u8(
  1337. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1338. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1339. if (err)
  1340. return err;
  1341. }
  1342. if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
  1343. if (!ops->eswitch_encap_mode_set)
  1344. return -EOPNOTSUPP;
  1345. encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
  1346. err = ops->eswitch_encap_mode_set(devlink, encap_mode);
  1347. if (err)
  1348. return err;
  1349. }
  1350. return 0;
  1351. }
  1352. int devlink_dpipe_match_put(struct sk_buff *skb,
  1353. struct devlink_dpipe_match *match)
  1354. {
  1355. struct devlink_dpipe_header *header = match->header;
  1356. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  1357. struct nlattr *match_attr;
  1358. match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
  1359. if (!match_attr)
  1360. return -EMSGSIZE;
  1361. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  1362. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
  1363. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1364. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1365. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1366. goto nla_put_failure;
  1367. nla_nest_end(skb, match_attr);
  1368. return 0;
  1369. nla_put_failure:
  1370. nla_nest_cancel(skb, match_attr);
  1371. return -EMSGSIZE;
  1372. }
  1373. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  1374. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  1375. struct sk_buff *skb)
  1376. {
  1377. struct nlattr *matches_attr;
  1378. matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  1379. if (!matches_attr)
  1380. return -EMSGSIZE;
  1381. if (table->table_ops->matches_dump(table->priv, skb))
  1382. goto nla_put_failure;
  1383. nla_nest_end(skb, matches_attr);
  1384. return 0;
  1385. nla_put_failure:
  1386. nla_nest_cancel(skb, matches_attr);
  1387. return -EMSGSIZE;
  1388. }
  1389. int devlink_dpipe_action_put(struct sk_buff *skb,
  1390. struct devlink_dpipe_action *action)
  1391. {
  1392. struct devlink_dpipe_header *header = action->header;
  1393. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  1394. struct nlattr *action_attr;
  1395. action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
  1396. if (!action_attr)
  1397. return -EMSGSIZE;
  1398. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  1399. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  1400. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1401. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1402. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1403. goto nla_put_failure;
  1404. nla_nest_end(skb, action_attr);
  1405. return 0;
  1406. nla_put_failure:
  1407. nla_nest_cancel(skb, action_attr);
  1408. return -EMSGSIZE;
  1409. }
  1410. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  1411. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  1412. struct sk_buff *skb)
  1413. {
  1414. struct nlattr *actions_attr;
  1415. actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  1416. if (!actions_attr)
  1417. return -EMSGSIZE;
  1418. if (table->table_ops->actions_dump(table->priv, skb))
  1419. goto nla_put_failure;
  1420. nla_nest_end(skb, actions_attr);
  1421. return 0;
  1422. nla_put_failure:
  1423. nla_nest_cancel(skb, actions_attr);
  1424. return -EMSGSIZE;
  1425. }
  1426. static int devlink_dpipe_table_put(struct sk_buff *skb,
  1427. struct devlink_dpipe_table *table)
  1428. {
  1429. struct nlattr *table_attr;
  1430. u64 table_size;
  1431. table_size = table->table_ops->size_get(table->priv);
  1432. table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
  1433. if (!table_attr)
  1434. return -EMSGSIZE;
  1435. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  1436. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
  1437. DEVLINK_ATTR_PAD))
  1438. goto nla_put_failure;
  1439. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  1440. table->counters_enabled))
  1441. goto nla_put_failure;
  1442. if (table->resource_valid) {
  1443. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
  1444. table->resource_id, DEVLINK_ATTR_PAD) ||
  1445. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
  1446. table->resource_units, DEVLINK_ATTR_PAD))
  1447. goto nla_put_failure;
  1448. }
  1449. if (devlink_dpipe_matches_put(table, skb))
  1450. goto nla_put_failure;
  1451. if (devlink_dpipe_actions_put(table, skb))
  1452. goto nla_put_failure;
  1453. nla_nest_end(skb, table_attr);
  1454. return 0;
  1455. nla_put_failure:
  1456. nla_nest_cancel(skb, table_attr);
  1457. return -EMSGSIZE;
  1458. }
  1459. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  1460. struct genl_info *info)
  1461. {
  1462. int err;
  1463. if (*pskb) {
  1464. err = genlmsg_reply(*pskb, info);
  1465. if (err)
  1466. return err;
  1467. }
  1468. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1469. if (!*pskb)
  1470. return -ENOMEM;
  1471. return 0;
  1472. }
  1473. static int devlink_dpipe_tables_fill(struct genl_info *info,
  1474. enum devlink_command cmd, int flags,
  1475. struct list_head *dpipe_tables,
  1476. const char *table_name)
  1477. {
  1478. struct devlink *devlink = info->user_ptr[0];
  1479. struct devlink_dpipe_table *table;
  1480. struct nlattr *tables_attr;
  1481. struct sk_buff *skb = NULL;
  1482. struct nlmsghdr *nlh;
  1483. bool incomplete;
  1484. void *hdr;
  1485. int i;
  1486. int err;
  1487. table = list_first_entry(dpipe_tables,
  1488. struct devlink_dpipe_table, list);
  1489. start_again:
  1490. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1491. if (err)
  1492. return err;
  1493. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1494. &devlink_nl_family, NLM_F_MULTI, cmd);
  1495. if (!hdr) {
  1496. nlmsg_free(skb);
  1497. return -EMSGSIZE;
  1498. }
  1499. if (devlink_nl_put_handle(skb, devlink))
  1500. goto nla_put_failure;
  1501. tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
  1502. if (!tables_attr)
  1503. goto nla_put_failure;
  1504. i = 0;
  1505. incomplete = false;
  1506. list_for_each_entry_from(table, dpipe_tables, list) {
  1507. if (!table_name) {
  1508. err = devlink_dpipe_table_put(skb, table);
  1509. if (err) {
  1510. if (!i)
  1511. goto err_table_put;
  1512. incomplete = true;
  1513. break;
  1514. }
  1515. } else {
  1516. if (!strcmp(table->name, table_name)) {
  1517. err = devlink_dpipe_table_put(skb, table);
  1518. if (err)
  1519. break;
  1520. }
  1521. }
  1522. i++;
  1523. }
  1524. nla_nest_end(skb, tables_attr);
  1525. genlmsg_end(skb, hdr);
  1526. if (incomplete)
  1527. goto start_again;
  1528. send_done:
  1529. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1530. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1531. if (!nlh) {
  1532. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1533. if (err)
  1534. return err;
  1535. goto send_done;
  1536. }
  1537. return genlmsg_reply(skb, info);
  1538. nla_put_failure:
  1539. err = -EMSGSIZE;
  1540. err_table_put:
  1541. genlmsg_cancel(skb, hdr);
  1542. nlmsg_free(skb);
  1543. return err;
  1544. }
  1545. static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
  1546. struct genl_info *info)
  1547. {
  1548. struct devlink *devlink = info->user_ptr[0];
  1549. const char *table_name = NULL;
  1550. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1551. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1552. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  1553. &devlink->dpipe_table_list,
  1554. table_name);
  1555. }
  1556. static int devlink_dpipe_value_put(struct sk_buff *skb,
  1557. struct devlink_dpipe_value *value)
  1558. {
  1559. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  1560. value->value_size, value->value))
  1561. return -EMSGSIZE;
  1562. if (value->mask)
  1563. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  1564. value->value_size, value->mask))
  1565. return -EMSGSIZE;
  1566. if (value->mapping_valid)
  1567. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  1568. value->mapping_value))
  1569. return -EMSGSIZE;
  1570. return 0;
  1571. }
  1572. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  1573. struct devlink_dpipe_value *value)
  1574. {
  1575. if (!value->action)
  1576. return -EINVAL;
  1577. if (devlink_dpipe_action_put(skb, value->action))
  1578. return -EMSGSIZE;
  1579. if (devlink_dpipe_value_put(skb, value))
  1580. return -EMSGSIZE;
  1581. return 0;
  1582. }
  1583. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  1584. struct devlink_dpipe_value *values,
  1585. unsigned int values_count)
  1586. {
  1587. struct nlattr *action_attr;
  1588. int i;
  1589. int err;
  1590. for (i = 0; i < values_count; i++) {
  1591. action_attr = nla_nest_start(skb,
  1592. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  1593. if (!action_attr)
  1594. return -EMSGSIZE;
  1595. err = devlink_dpipe_action_value_put(skb, &values[i]);
  1596. if (err)
  1597. goto err_action_value_put;
  1598. nla_nest_end(skb, action_attr);
  1599. }
  1600. return 0;
  1601. err_action_value_put:
  1602. nla_nest_cancel(skb, action_attr);
  1603. return err;
  1604. }
  1605. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  1606. struct devlink_dpipe_value *value)
  1607. {
  1608. if (!value->match)
  1609. return -EINVAL;
  1610. if (devlink_dpipe_match_put(skb, value->match))
  1611. return -EMSGSIZE;
  1612. if (devlink_dpipe_value_put(skb, value))
  1613. return -EMSGSIZE;
  1614. return 0;
  1615. }
  1616. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  1617. struct devlink_dpipe_value *values,
  1618. unsigned int values_count)
  1619. {
  1620. struct nlattr *match_attr;
  1621. int i;
  1622. int err;
  1623. for (i = 0; i < values_count; i++) {
  1624. match_attr = nla_nest_start(skb,
  1625. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  1626. if (!match_attr)
  1627. return -EMSGSIZE;
  1628. err = devlink_dpipe_match_value_put(skb, &values[i]);
  1629. if (err)
  1630. goto err_match_value_put;
  1631. nla_nest_end(skb, match_attr);
  1632. }
  1633. return 0;
  1634. err_match_value_put:
  1635. nla_nest_cancel(skb, match_attr);
  1636. return err;
  1637. }
  1638. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  1639. struct devlink_dpipe_entry *entry)
  1640. {
  1641. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  1642. int err;
  1643. entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  1644. if (!entry_attr)
  1645. return -EMSGSIZE;
  1646. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  1647. DEVLINK_ATTR_PAD))
  1648. goto nla_put_failure;
  1649. if (entry->counter_valid)
  1650. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  1651. entry->counter, DEVLINK_ATTR_PAD))
  1652. goto nla_put_failure;
  1653. matches_attr = nla_nest_start(skb,
  1654. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  1655. if (!matches_attr)
  1656. goto nla_put_failure;
  1657. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  1658. entry->match_values_count);
  1659. if (err) {
  1660. nla_nest_cancel(skb, matches_attr);
  1661. goto err_match_values_put;
  1662. }
  1663. nla_nest_end(skb, matches_attr);
  1664. actions_attr = nla_nest_start(skb,
  1665. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  1666. if (!actions_attr)
  1667. goto nla_put_failure;
  1668. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  1669. entry->action_values_count);
  1670. if (err) {
  1671. nla_nest_cancel(skb, actions_attr);
  1672. goto err_action_values_put;
  1673. }
  1674. nla_nest_end(skb, actions_attr);
  1675. nla_nest_end(skb, entry_attr);
  1676. return 0;
  1677. nla_put_failure:
  1678. err = -EMSGSIZE;
  1679. err_match_values_put:
  1680. err_action_values_put:
  1681. nla_nest_cancel(skb, entry_attr);
  1682. return err;
  1683. }
  1684. static struct devlink_dpipe_table *
  1685. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  1686. const char *table_name)
  1687. {
  1688. struct devlink_dpipe_table *table;
  1689. list_for_each_entry_rcu(table, dpipe_tables, list) {
  1690. if (!strcmp(table->name, table_name))
  1691. return table;
  1692. }
  1693. return NULL;
  1694. }
  1695. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  1696. {
  1697. struct devlink *devlink;
  1698. int err;
  1699. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  1700. dump_ctx->info);
  1701. if (err)
  1702. return err;
  1703. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  1704. dump_ctx->info->snd_portid,
  1705. dump_ctx->info->snd_seq,
  1706. &devlink_nl_family, NLM_F_MULTI,
  1707. dump_ctx->cmd);
  1708. if (!dump_ctx->hdr)
  1709. goto nla_put_failure;
  1710. devlink = dump_ctx->info->user_ptr[0];
  1711. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  1712. goto nla_put_failure;
  1713. dump_ctx->nest = nla_nest_start(dump_ctx->skb,
  1714. DEVLINK_ATTR_DPIPE_ENTRIES);
  1715. if (!dump_ctx->nest)
  1716. goto nla_put_failure;
  1717. return 0;
  1718. nla_put_failure:
  1719. genlmsg_cancel(dump_ctx->skb, dump_ctx->hdr);
  1720. nlmsg_free(dump_ctx->skb);
  1721. return -EMSGSIZE;
  1722. }
  1723. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  1724. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  1725. struct devlink_dpipe_entry *entry)
  1726. {
  1727. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  1728. }
  1729. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  1730. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  1731. {
  1732. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  1733. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  1734. return 0;
  1735. }
  1736. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  1737. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  1738. {
  1739. unsigned int value_count, value_index;
  1740. struct devlink_dpipe_value *value;
  1741. value = entry->action_values;
  1742. value_count = entry->action_values_count;
  1743. for (value_index = 0; value_index < value_count; value_index++) {
  1744. kfree(value[value_index].value);
  1745. kfree(value[value_index].mask);
  1746. }
  1747. value = entry->match_values;
  1748. value_count = entry->match_values_count;
  1749. for (value_index = 0; value_index < value_count; value_index++) {
  1750. kfree(value[value_index].value);
  1751. kfree(value[value_index].mask);
  1752. }
  1753. }
  1754. EXPORT_SYMBOL(devlink_dpipe_entry_clear);
  1755. static int devlink_dpipe_entries_fill(struct genl_info *info,
  1756. enum devlink_command cmd, int flags,
  1757. struct devlink_dpipe_table *table)
  1758. {
  1759. struct devlink_dpipe_dump_ctx dump_ctx;
  1760. struct nlmsghdr *nlh;
  1761. int err;
  1762. dump_ctx.skb = NULL;
  1763. dump_ctx.cmd = cmd;
  1764. dump_ctx.info = info;
  1765. err = table->table_ops->entries_dump(table->priv,
  1766. table->counters_enabled,
  1767. &dump_ctx);
  1768. if (err)
  1769. return err;
  1770. send_done:
  1771. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  1772. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1773. if (!nlh) {
  1774. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  1775. if (err)
  1776. return err;
  1777. goto send_done;
  1778. }
  1779. return genlmsg_reply(dump_ctx.skb, info);
  1780. }
  1781. static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
  1782. struct genl_info *info)
  1783. {
  1784. struct devlink *devlink = info->user_ptr[0];
  1785. struct devlink_dpipe_table *table;
  1786. const char *table_name;
  1787. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1788. return -EINVAL;
  1789. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1790. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1791. table_name);
  1792. if (!table)
  1793. return -EINVAL;
  1794. if (!table->table_ops->entries_dump)
  1795. return -EINVAL;
  1796. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  1797. 0, table);
  1798. }
  1799. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  1800. const struct devlink_dpipe_header *header)
  1801. {
  1802. struct devlink_dpipe_field *field;
  1803. struct nlattr *field_attr;
  1804. int i;
  1805. for (i = 0; i < header->fields_count; i++) {
  1806. field = &header->fields[i];
  1807. field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
  1808. if (!field_attr)
  1809. return -EMSGSIZE;
  1810. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  1811. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1812. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  1813. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  1814. goto nla_put_failure;
  1815. nla_nest_end(skb, field_attr);
  1816. }
  1817. return 0;
  1818. nla_put_failure:
  1819. nla_nest_cancel(skb, field_attr);
  1820. return -EMSGSIZE;
  1821. }
  1822. static int devlink_dpipe_header_put(struct sk_buff *skb,
  1823. struct devlink_dpipe_header *header)
  1824. {
  1825. struct nlattr *fields_attr, *header_attr;
  1826. int err;
  1827. header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
  1828. if (!header_attr)
  1829. return -EMSGSIZE;
  1830. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  1831. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1832. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1833. goto nla_put_failure;
  1834. fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  1835. if (!fields_attr)
  1836. goto nla_put_failure;
  1837. err = devlink_dpipe_fields_put(skb, header);
  1838. if (err) {
  1839. nla_nest_cancel(skb, fields_attr);
  1840. goto nla_put_failure;
  1841. }
  1842. nla_nest_end(skb, fields_attr);
  1843. nla_nest_end(skb, header_attr);
  1844. return 0;
  1845. nla_put_failure:
  1846. err = -EMSGSIZE;
  1847. nla_nest_cancel(skb, header_attr);
  1848. return err;
  1849. }
  1850. static int devlink_dpipe_headers_fill(struct genl_info *info,
  1851. enum devlink_command cmd, int flags,
  1852. struct devlink_dpipe_headers *
  1853. dpipe_headers)
  1854. {
  1855. struct devlink *devlink = info->user_ptr[0];
  1856. struct nlattr *headers_attr;
  1857. struct sk_buff *skb = NULL;
  1858. struct nlmsghdr *nlh;
  1859. void *hdr;
  1860. int i, j;
  1861. int err;
  1862. i = 0;
  1863. start_again:
  1864. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1865. if (err)
  1866. return err;
  1867. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1868. &devlink_nl_family, NLM_F_MULTI, cmd);
  1869. if (!hdr) {
  1870. nlmsg_free(skb);
  1871. return -EMSGSIZE;
  1872. }
  1873. if (devlink_nl_put_handle(skb, devlink))
  1874. goto nla_put_failure;
  1875. headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  1876. if (!headers_attr)
  1877. goto nla_put_failure;
  1878. j = 0;
  1879. for (; i < dpipe_headers->headers_count; i++) {
  1880. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  1881. if (err) {
  1882. if (!j)
  1883. goto err_table_put;
  1884. break;
  1885. }
  1886. j++;
  1887. }
  1888. nla_nest_end(skb, headers_attr);
  1889. genlmsg_end(skb, hdr);
  1890. if (i != dpipe_headers->headers_count)
  1891. goto start_again;
  1892. send_done:
  1893. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1894. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1895. if (!nlh) {
  1896. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1897. if (err)
  1898. return err;
  1899. goto send_done;
  1900. }
  1901. return genlmsg_reply(skb, info);
  1902. nla_put_failure:
  1903. err = -EMSGSIZE;
  1904. err_table_put:
  1905. genlmsg_cancel(skb, hdr);
  1906. nlmsg_free(skb);
  1907. return err;
  1908. }
  1909. static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
  1910. struct genl_info *info)
  1911. {
  1912. struct devlink *devlink = info->user_ptr[0];
  1913. if (!devlink->dpipe_headers)
  1914. return -EOPNOTSUPP;
  1915. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  1916. 0, devlink->dpipe_headers);
  1917. }
  1918. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  1919. const char *table_name,
  1920. bool enable)
  1921. {
  1922. struct devlink_dpipe_table *table;
  1923. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1924. table_name);
  1925. if (!table)
  1926. return -EINVAL;
  1927. if (table->counter_control_extern)
  1928. return -EOPNOTSUPP;
  1929. if (!(table->counters_enabled ^ enable))
  1930. return 0;
  1931. table->counters_enabled = enable;
  1932. if (table->table_ops->counters_set_update)
  1933. table->table_ops->counters_set_update(table->priv, enable);
  1934. return 0;
  1935. }
  1936. static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
  1937. struct genl_info *info)
  1938. {
  1939. struct devlink *devlink = info->user_ptr[0];
  1940. const char *table_name;
  1941. bool counters_enable;
  1942. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
  1943. !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
  1944. return -EINVAL;
  1945. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1946. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  1947. return devlink_dpipe_table_counters_set(devlink, table_name,
  1948. counters_enable);
  1949. }
  1950. static struct devlink_resource *
  1951. devlink_resource_find(struct devlink *devlink,
  1952. struct devlink_resource *resource, u64 resource_id)
  1953. {
  1954. struct list_head *resource_list;
  1955. if (resource)
  1956. resource_list = &resource->resource_list;
  1957. else
  1958. resource_list = &devlink->resource_list;
  1959. list_for_each_entry(resource, resource_list, list) {
  1960. struct devlink_resource *child_resource;
  1961. if (resource->id == resource_id)
  1962. return resource;
  1963. child_resource = devlink_resource_find(devlink, resource,
  1964. resource_id);
  1965. if (child_resource)
  1966. return child_resource;
  1967. }
  1968. return NULL;
  1969. }
  1970. static void
  1971. devlink_resource_validate_children(struct devlink_resource *resource)
  1972. {
  1973. struct devlink_resource *child_resource;
  1974. bool size_valid = true;
  1975. u64 parts_size = 0;
  1976. if (list_empty(&resource->resource_list))
  1977. goto out;
  1978. list_for_each_entry(child_resource, &resource->resource_list, list)
  1979. parts_size += child_resource->size_new;
  1980. if (parts_size > resource->size_new)
  1981. size_valid = false;
  1982. out:
  1983. resource->size_valid = size_valid;
  1984. }
  1985. static int
  1986. devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
  1987. struct netlink_ext_ack *extack)
  1988. {
  1989. u64 reminder;
  1990. int err = 0;
  1991. if (size > resource->size_params.size_max) {
  1992. NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
  1993. err = -EINVAL;
  1994. }
  1995. if (size < resource->size_params.size_min) {
  1996. NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
  1997. err = -EINVAL;
  1998. }
  1999. div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
  2000. if (reminder) {
  2001. NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
  2002. err = -EINVAL;
  2003. }
  2004. return err;
  2005. }
  2006. static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
  2007. struct genl_info *info)
  2008. {
  2009. struct devlink *devlink = info->user_ptr[0];
  2010. struct devlink_resource *resource;
  2011. u64 resource_id;
  2012. u64 size;
  2013. int err;
  2014. if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
  2015. !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
  2016. return -EINVAL;
  2017. resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
  2018. resource = devlink_resource_find(devlink, NULL, resource_id);
  2019. if (!resource)
  2020. return -EINVAL;
  2021. size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
  2022. err = devlink_resource_validate_size(resource, size, info->extack);
  2023. if (err)
  2024. return err;
  2025. resource->size_new = size;
  2026. devlink_resource_validate_children(resource);
  2027. if (resource->parent)
  2028. devlink_resource_validate_children(resource->parent);
  2029. return 0;
  2030. }
  2031. static int
  2032. devlink_resource_size_params_put(struct devlink_resource *resource,
  2033. struct sk_buff *skb)
  2034. {
  2035. struct devlink_resource_size_params *size_params;
  2036. size_params = &resource->size_params;
  2037. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
  2038. size_params->size_granularity, DEVLINK_ATTR_PAD) ||
  2039. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
  2040. size_params->size_max, DEVLINK_ATTR_PAD) ||
  2041. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
  2042. size_params->size_min, DEVLINK_ATTR_PAD) ||
  2043. nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
  2044. return -EMSGSIZE;
  2045. return 0;
  2046. }
  2047. static int devlink_resource_occ_put(struct devlink_resource *resource,
  2048. struct sk_buff *skb)
  2049. {
  2050. if (!resource->occ_get)
  2051. return 0;
  2052. return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
  2053. resource->occ_get(resource->occ_get_priv),
  2054. DEVLINK_ATTR_PAD);
  2055. }
  2056. static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
  2057. struct devlink_resource *resource)
  2058. {
  2059. struct devlink_resource *child_resource;
  2060. struct nlattr *child_resource_attr;
  2061. struct nlattr *resource_attr;
  2062. resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
  2063. if (!resource_attr)
  2064. return -EMSGSIZE;
  2065. if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
  2066. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
  2067. DEVLINK_ATTR_PAD) ||
  2068. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
  2069. DEVLINK_ATTR_PAD))
  2070. goto nla_put_failure;
  2071. if (resource->size != resource->size_new)
  2072. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
  2073. resource->size_new, DEVLINK_ATTR_PAD);
  2074. if (devlink_resource_occ_put(resource, skb))
  2075. goto nla_put_failure;
  2076. if (devlink_resource_size_params_put(resource, skb))
  2077. goto nla_put_failure;
  2078. if (list_empty(&resource->resource_list))
  2079. goto out;
  2080. if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
  2081. resource->size_valid))
  2082. goto nla_put_failure;
  2083. child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2084. if (!child_resource_attr)
  2085. goto nla_put_failure;
  2086. list_for_each_entry(child_resource, &resource->resource_list, list) {
  2087. if (devlink_resource_put(devlink, skb, child_resource))
  2088. goto resource_put_failure;
  2089. }
  2090. nla_nest_end(skb, child_resource_attr);
  2091. out:
  2092. nla_nest_end(skb, resource_attr);
  2093. return 0;
  2094. resource_put_failure:
  2095. nla_nest_cancel(skb, child_resource_attr);
  2096. nla_put_failure:
  2097. nla_nest_cancel(skb, resource_attr);
  2098. return -EMSGSIZE;
  2099. }
  2100. static int devlink_resource_fill(struct genl_info *info,
  2101. enum devlink_command cmd, int flags)
  2102. {
  2103. struct devlink *devlink = info->user_ptr[0];
  2104. struct devlink_resource *resource;
  2105. struct nlattr *resources_attr;
  2106. struct sk_buff *skb = NULL;
  2107. struct nlmsghdr *nlh;
  2108. bool incomplete;
  2109. void *hdr;
  2110. int i;
  2111. int err;
  2112. resource = list_first_entry(&devlink->resource_list,
  2113. struct devlink_resource, list);
  2114. start_again:
  2115. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2116. if (err)
  2117. return err;
  2118. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  2119. &devlink_nl_family, NLM_F_MULTI, cmd);
  2120. if (!hdr) {
  2121. nlmsg_free(skb);
  2122. return -EMSGSIZE;
  2123. }
  2124. if (devlink_nl_put_handle(skb, devlink))
  2125. goto nla_put_failure;
  2126. resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2127. if (!resources_attr)
  2128. goto nla_put_failure;
  2129. incomplete = false;
  2130. i = 0;
  2131. list_for_each_entry_from(resource, &devlink->resource_list, list) {
  2132. err = devlink_resource_put(devlink, skb, resource);
  2133. if (err) {
  2134. if (!i)
  2135. goto err_resource_put;
  2136. incomplete = true;
  2137. break;
  2138. }
  2139. i++;
  2140. }
  2141. nla_nest_end(skb, resources_attr);
  2142. genlmsg_end(skb, hdr);
  2143. if (incomplete)
  2144. goto start_again;
  2145. send_done:
  2146. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  2147. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2148. if (!nlh) {
  2149. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2150. if (err)
  2151. goto err_skb_send_alloc;
  2152. goto send_done;
  2153. }
  2154. return genlmsg_reply(skb, info);
  2155. nla_put_failure:
  2156. err = -EMSGSIZE;
  2157. err_resource_put:
  2158. err_skb_send_alloc:
  2159. genlmsg_cancel(skb, hdr);
  2160. nlmsg_free(skb);
  2161. return err;
  2162. }
  2163. static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
  2164. struct genl_info *info)
  2165. {
  2166. struct devlink *devlink = info->user_ptr[0];
  2167. if (list_empty(&devlink->resource_list))
  2168. return -EOPNOTSUPP;
  2169. return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
  2170. }
  2171. static int
  2172. devlink_resources_validate(struct devlink *devlink,
  2173. struct devlink_resource *resource,
  2174. struct genl_info *info)
  2175. {
  2176. struct list_head *resource_list;
  2177. int err = 0;
  2178. if (resource)
  2179. resource_list = &resource->resource_list;
  2180. else
  2181. resource_list = &devlink->resource_list;
  2182. list_for_each_entry(resource, resource_list, list) {
  2183. if (!resource->size_valid)
  2184. return -EINVAL;
  2185. err = devlink_resources_validate(devlink, resource, info);
  2186. if (err)
  2187. return err;
  2188. }
  2189. return err;
  2190. }
  2191. static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
  2192. {
  2193. struct devlink *devlink = info->user_ptr[0];
  2194. int err;
  2195. if (!devlink->ops->reload)
  2196. return -EOPNOTSUPP;
  2197. err = devlink_resources_validate(devlink, NULL, info);
  2198. if (err) {
  2199. NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
  2200. return err;
  2201. }
  2202. return devlink->ops->reload(devlink);
  2203. }
  2204. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  2205. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  2206. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  2207. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  2208. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  2209. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  2210. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  2211. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  2212. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  2213. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  2214. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  2215. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  2216. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  2217. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  2218. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  2219. [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
  2220. [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
  2221. [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
  2222. [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
  2223. [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
  2224. };
  2225. static const struct genl_ops devlink_nl_ops[] = {
  2226. {
  2227. .cmd = DEVLINK_CMD_GET,
  2228. .doit = devlink_nl_cmd_get_doit,
  2229. .dumpit = devlink_nl_cmd_get_dumpit,
  2230. .policy = devlink_nl_policy,
  2231. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2232. /* can be retrieved by unprivileged users */
  2233. },
  2234. {
  2235. .cmd = DEVLINK_CMD_PORT_GET,
  2236. .doit = devlink_nl_cmd_port_get_doit,
  2237. .dumpit = devlink_nl_cmd_port_get_dumpit,
  2238. .policy = devlink_nl_policy,
  2239. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2240. /* can be retrieved by unprivileged users */
  2241. },
  2242. {
  2243. .cmd = DEVLINK_CMD_PORT_SET,
  2244. .doit = devlink_nl_cmd_port_set_doit,
  2245. .policy = devlink_nl_policy,
  2246. .flags = GENL_ADMIN_PERM,
  2247. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2248. },
  2249. {
  2250. .cmd = DEVLINK_CMD_PORT_SPLIT,
  2251. .doit = devlink_nl_cmd_port_split_doit,
  2252. .policy = devlink_nl_policy,
  2253. .flags = GENL_ADMIN_PERM,
  2254. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2255. DEVLINK_NL_FLAG_NO_LOCK,
  2256. },
  2257. {
  2258. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  2259. .doit = devlink_nl_cmd_port_unsplit_doit,
  2260. .policy = devlink_nl_policy,
  2261. .flags = GENL_ADMIN_PERM,
  2262. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2263. DEVLINK_NL_FLAG_NO_LOCK,
  2264. },
  2265. {
  2266. .cmd = DEVLINK_CMD_SB_GET,
  2267. .doit = devlink_nl_cmd_sb_get_doit,
  2268. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  2269. .policy = devlink_nl_policy,
  2270. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2271. DEVLINK_NL_FLAG_NEED_SB,
  2272. /* can be retrieved by unprivileged users */
  2273. },
  2274. {
  2275. .cmd = DEVLINK_CMD_SB_POOL_GET,
  2276. .doit = devlink_nl_cmd_sb_pool_get_doit,
  2277. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  2278. .policy = devlink_nl_policy,
  2279. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2280. DEVLINK_NL_FLAG_NEED_SB,
  2281. /* can be retrieved by unprivileged users */
  2282. },
  2283. {
  2284. .cmd = DEVLINK_CMD_SB_POOL_SET,
  2285. .doit = devlink_nl_cmd_sb_pool_set_doit,
  2286. .policy = devlink_nl_policy,
  2287. .flags = GENL_ADMIN_PERM,
  2288. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2289. DEVLINK_NL_FLAG_NEED_SB,
  2290. },
  2291. {
  2292. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  2293. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  2294. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  2295. .policy = devlink_nl_policy,
  2296. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2297. DEVLINK_NL_FLAG_NEED_SB,
  2298. /* can be retrieved by unprivileged users */
  2299. },
  2300. {
  2301. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  2302. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  2303. .policy = devlink_nl_policy,
  2304. .flags = GENL_ADMIN_PERM,
  2305. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2306. DEVLINK_NL_FLAG_NEED_SB,
  2307. },
  2308. {
  2309. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  2310. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  2311. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  2312. .policy = devlink_nl_policy,
  2313. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2314. DEVLINK_NL_FLAG_NEED_SB,
  2315. /* can be retrieved by unprivileged users */
  2316. },
  2317. {
  2318. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  2319. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  2320. .policy = devlink_nl_policy,
  2321. .flags = GENL_ADMIN_PERM,
  2322. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2323. DEVLINK_NL_FLAG_NEED_SB,
  2324. },
  2325. {
  2326. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  2327. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  2328. .policy = devlink_nl_policy,
  2329. .flags = GENL_ADMIN_PERM,
  2330. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2331. DEVLINK_NL_FLAG_NEED_SB,
  2332. },
  2333. {
  2334. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  2335. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  2336. .policy = devlink_nl_policy,
  2337. .flags = GENL_ADMIN_PERM,
  2338. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2339. DEVLINK_NL_FLAG_NEED_SB,
  2340. },
  2341. {
  2342. .cmd = DEVLINK_CMD_ESWITCH_GET,
  2343. .doit = devlink_nl_cmd_eswitch_get_doit,
  2344. .policy = devlink_nl_policy,
  2345. .flags = GENL_ADMIN_PERM,
  2346. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2347. },
  2348. {
  2349. .cmd = DEVLINK_CMD_ESWITCH_SET,
  2350. .doit = devlink_nl_cmd_eswitch_set_doit,
  2351. .policy = devlink_nl_policy,
  2352. .flags = GENL_ADMIN_PERM,
  2353. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2354. },
  2355. {
  2356. .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
  2357. .doit = devlink_nl_cmd_dpipe_table_get,
  2358. .policy = devlink_nl_policy,
  2359. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2360. /* can be retrieved by unprivileged users */
  2361. },
  2362. {
  2363. .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
  2364. .doit = devlink_nl_cmd_dpipe_entries_get,
  2365. .policy = devlink_nl_policy,
  2366. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2367. /* can be retrieved by unprivileged users */
  2368. },
  2369. {
  2370. .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
  2371. .doit = devlink_nl_cmd_dpipe_headers_get,
  2372. .policy = devlink_nl_policy,
  2373. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2374. /* can be retrieved by unprivileged users */
  2375. },
  2376. {
  2377. .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
  2378. .doit = devlink_nl_cmd_dpipe_table_counters_set,
  2379. .policy = devlink_nl_policy,
  2380. .flags = GENL_ADMIN_PERM,
  2381. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2382. },
  2383. {
  2384. .cmd = DEVLINK_CMD_RESOURCE_SET,
  2385. .doit = devlink_nl_cmd_resource_set,
  2386. .policy = devlink_nl_policy,
  2387. .flags = GENL_ADMIN_PERM,
  2388. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2389. },
  2390. {
  2391. .cmd = DEVLINK_CMD_RESOURCE_DUMP,
  2392. .doit = devlink_nl_cmd_resource_dump,
  2393. .policy = devlink_nl_policy,
  2394. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2395. /* can be retrieved by unprivileged users */
  2396. },
  2397. {
  2398. .cmd = DEVLINK_CMD_RELOAD,
  2399. .doit = devlink_nl_cmd_reload,
  2400. .policy = devlink_nl_policy,
  2401. .flags = GENL_ADMIN_PERM,
  2402. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2403. DEVLINK_NL_FLAG_NO_LOCK,
  2404. },
  2405. };
  2406. static struct genl_family devlink_nl_family __ro_after_init = {
  2407. .name = DEVLINK_GENL_NAME,
  2408. .version = DEVLINK_GENL_VERSION,
  2409. .maxattr = DEVLINK_ATTR_MAX,
  2410. .netnsok = true,
  2411. .pre_doit = devlink_nl_pre_doit,
  2412. .post_doit = devlink_nl_post_doit,
  2413. .module = THIS_MODULE,
  2414. .ops = devlink_nl_ops,
  2415. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  2416. .mcgrps = devlink_nl_mcgrps,
  2417. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  2418. };
  2419. /**
  2420. * devlink_alloc - Allocate new devlink instance resources
  2421. *
  2422. * @ops: ops
  2423. * @priv_size: size of user private data
  2424. *
  2425. * Allocate new devlink instance resources, including devlink index
  2426. * and name.
  2427. */
  2428. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  2429. {
  2430. struct devlink *devlink;
  2431. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  2432. if (!devlink)
  2433. return NULL;
  2434. devlink->ops = ops;
  2435. devlink_net_set(devlink, &init_net);
  2436. INIT_LIST_HEAD(&devlink->port_list);
  2437. INIT_LIST_HEAD(&devlink->sb_list);
  2438. INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
  2439. INIT_LIST_HEAD(&devlink->resource_list);
  2440. mutex_init(&devlink->lock);
  2441. return devlink;
  2442. }
  2443. EXPORT_SYMBOL_GPL(devlink_alloc);
  2444. /**
  2445. * devlink_register - Register devlink instance
  2446. *
  2447. * @devlink: devlink
  2448. */
  2449. int devlink_register(struct devlink *devlink, struct device *dev)
  2450. {
  2451. mutex_lock(&devlink_mutex);
  2452. devlink->dev = dev;
  2453. list_add_tail(&devlink->list, &devlink_list);
  2454. devlink_notify(devlink, DEVLINK_CMD_NEW);
  2455. mutex_unlock(&devlink_mutex);
  2456. return 0;
  2457. }
  2458. EXPORT_SYMBOL_GPL(devlink_register);
  2459. /**
  2460. * devlink_unregister - Unregister devlink instance
  2461. *
  2462. * @devlink: devlink
  2463. */
  2464. void devlink_unregister(struct devlink *devlink)
  2465. {
  2466. mutex_lock(&devlink_mutex);
  2467. devlink_notify(devlink, DEVLINK_CMD_DEL);
  2468. list_del(&devlink->list);
  2469. mutex_unlock(&devlink_mutex);
  2470. }
  2471. EXPORT_SYMBOL_GPL(devlink_unregister);
  2472. /**
  2473. * devlink_free - Free devlink instance resources
  2474. *
  2475. * @devlink: devlink
  2476. */
  2477. void devlink_free(struct devlink *devlink)
  2478. {
  2479. kfree(devlink);
  2480. }
  2481. EXPORT_SYMBOL_GPL(devlink_free);
  2482. /**
  2483. * devlink_port_register - Register devlink port
  2484. *
  2485. * @devlink: devlink
  2486. * @devlink_port: devlink port
  2487. * @port_index
  2488. *
  2489. * Register devlink port with provided port index. User can use
  2490. * any indexing, even hw-related one. devlink_port structure
  2491. * is convenient to be embedded inside user driver private structure.
  2492. * Note that the caller should take care of zeroing the devlink_port
  2493. * structure.
  2494. */
  2495. int devlink_port_register(struct devlink *devlink,
  2496. struct devlink_port *devlink_port,
  2497. unsigned int port_index)
  2498. {
  2499. mutex_lock(&devlink->lock);
  2500. if (devlink_port_index_exists(devlink, port_index)) {
  2501. mutex_unlock(&devlink->lock);
  2502. return -EEXIST;
  2503. }
  2504. devlink_port->devlink = devlink;
  2505. devlink_port->index = port_index;
  2506. devlink_port->registered = true;
  2507. list_add_tail(&devlink_port->list, &devlink->port_list);
  2508. mutex_unlock(&devlink->lock);
  2509. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2510. return 0;
  2511. }
  2512. EXPORT_SYMBOL_GPL(devlink_port_register);
  2513. /**
  2514. * devlink_port_unregister - Unregister devlink port
  2515. *
  2516. * @devlink_port: devlink port
  2517. */
  2518. void devlink_port_unregister(struct devlink_port *devlink_port)
  2519. {
  2520. struct devlink *devlink = devlink_port->devlink;
  2521. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  2522. mutex_lock(&devlink->lock);
  2523. list_del(&devlink_port->list);
  2524. mutex_unlock(&devlink->lock);
  2525. }
  2526. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  2527. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  2528. enum devlink_port_type type,
  2529. void *type_dev)
  2530. {
  2531. devlink_port->type = type;
  2532. devlink_port->type_dev = type_dev;
  2533. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2534. }
  2535. /**
  2536. * devlink_port_type_eth_set - Set port type to Ethernet
  2537. *
  2538. * @devlink_port: devlink port
  2539. * @netdev: related netdevice
  2540. */
  2541. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  2542. struct net_device *netdev)
  2543. {
  2544. return __devlink_port_type_set(devlink_port,
  2545. DEVLINK_PORT_TYPE_ETH, netdev);
  2546. }
  2547. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  2548. /**
  2549. * devlink_port_type_ib_set - Set port type to InfiniBand
  2550. *
  2551. * @devlink_port: devlink port
  2552. * @ibdev: related IB device
  2553. */
  2554. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  2555. struct ib_device *ibdev)
  2556. {
  2557. return __devlink_port_type_set(devlink_port,
  2558. DEVLINK_PORT_TYPE_IB, ibdev);
  2559. }
  2560. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  2561. /**
  2562. * devlink_port_type_clear - Clear port type
  2563. *
  2564. * @devlink_port: devlink port
  2565. */
  2566. void devlink_port_type_clear(struct devlink_port *devlink_port)
  2567. {
  2568. return __devlink_port_type_set(devlink_port,
  2569. DEVLINK_PORT_TYPE_NOTSET, NULL);
  2570. }
  2571. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  2572. /**
  2573. * devlink_port_split_set - Set port is split
  2574. *
  2575. * @devlink_port: devlink port
  2576. * @split_group: split group - identifies group split port is part of
  2577. */
  2578. void devlink_port_split_set(struct devlink_port *devlink_port,
  2579. u32 split_group)
  2580. {
  2581. devlink_port->split = true;
  2582. devlink_port->split_group = split_group;
  2583. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2584. }
  2585. EXPORT_SYMBOL_GPL(devlink_port_split_set);
  2586. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  2587. u32 size, u16 ingress_pools_count,
  2588. u16 egress_pools_count, u16 ingress_tc_count,
  2589. u16 egress_tc_count)
  2590. {
  2591. struct devlink_sb *devlink_sb;
  2592. int err = 0;
  2593. mutex_lock(&devlink->lock);
  2594. if (devlink_sb_index_exists(devlink, sb_index)) {
  2595. err = -EEXIST;
  2596. goto unlock;
  2597. }
  2598. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  2599. if (!devlink_sb) {
  2600. err = -ENOMEM;
  2601. goto unlock;
  2602. }
  2603. devlink_sb->index = sb_index;
  2604. devlink_sb->size = size;
  2605. devlink_sb->ingress_pools_count = ingress_pools_count;
  2606. devlink_sb->egress_pools_count = egress_pools_count;
  2607. devlink_sb->ingress_tc_count = ingress_tc_count;
  2608. devlink_sb->egress_tc_count = egress_tc_count;
  2609. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  2610. unlock:
  2611. mutex_unlock(&devlink->lock);
  2612. return err;
  2613. }
  2614. EXPORT_SYMBOL_GPL(devlink_sb_register);
  2615. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  2616. {
  2617. struct devlink_sb *devlink_sb;
  2618. mutex_lock(&devlink->lock);
  2619. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  2620. WARN_ON(!devlink_sb);
  2621. list_del(&devlink_sb->list);
  2622. mutex_unlock(&devlink->lock);
  2623. kfree(devlink_sb);
  2624. }
  2625. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  2626. /**
  2627. * devlink_dpipe_headers_register - register dpipe headers
  2628. *
  2629. * @devlink: devlink
  2630. * @dpipe_headers: dpipe header array
  2631. *
  2632. * Register the headers supported by hardware.
  2633. */
  2634. int devlink_dpipe_headers_register(struct devlink *devlink,
  2635. struct devlink_dpipe_headers *dpipe_headers)
  2636. {
  2637. mutex_lock(&devlink->lock);
  2638. devlink->dpipe_headers = dpipe_headers;
  2639. mutex_unlock(&devlink->lock);
  2640. return 0;
  2641. }
  2642. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
  2643. /**
  2644. * devlink_dpipe_headers_unregister - unregister dpipe headers
  2645. *
  2646. * @devlink: devlink
  2647. *
  2648. * Unregister the headers supported by hardware.
  2649. */
  2650. void devlink_dpipe_headers_unregister(struct devlink *devlink)
  2651. {
  2652. mutex_lock(&devlink->lock);
  2653. devlink->dpipe_headers = NULL;
  2654. mutex_unlock(&devlink->lock);
  2655. }
  2656. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
  2657. /**
  2658. * devlink_dpipe_table_counter_enabled - check if counter allocation
  2659. * required
  2660. * @devlink: devlink
  2661. * @table_name: tables name
  2662. *
  2663. * Used by driver to check if counter allocation is required.
  2664. * After counter allocation is turned on the table entries
  2665. * are updated to include counter statistics.
  2666. *
  2667. * After that point on the driver must respect the counter
  2668. * state so that each entry added to the table is added
  2669. * with a counter.
  2670. */
  2671. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  2672. const char *table_name)
  2673. {
  2674. struct devlink_dpipe_table *table;
  2675. bool enabled;
  2676. rcu_read_lock();
  2677. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2678. table_name);
  2679. enabled = false;
  2680. if (table)
  2681. enabled = table->counters_enabled;
  2682. rcu_read_unlock();
  2683. return enabled;
  2684. }
  2685. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  2686. /**
  2687. * devlink_dpipe_table_register - register dpipe table
  2688. *
  2689. * @devlink: devlink
  2690. * @table_name: table name
  2691. * @table_ops: table ops
  2692. * @priv: priv
  2693. * @counter_control_extern: external control for counters
  2694. */
  2695. int devlink_dpipe_table_register(struct devlink *devlink,
  2696. const char *table_name,
  2697. struct devlink_dpipe_table_ops *table_ops,
  2698. void *priv, bool counter_control_extern)
  2699. {
  2700. struct devlink_dpipe_table *table;
  2701. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
  2702. return -EEXIST;
  2703. if (WARN_ON(!table_ops->size_get))
  2704. return -EINVAL;
  2705. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2706. if (!table)
  2707. return -ENOMEM;
  2708. table->name = table_name;
  2709. table->table_ops = table_ops;
  2710. table->priv = priv;
  2711. table->counter_control_extern = counter_control_extern;
  2712. mutex_lock(&devlink->lock);
  2713. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  2714. mutex_unlock(&devlink->lock);
  2715. return 0;
  2716. }
  2717. EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
  2718. /**
  2719. * devlink_dpipe_table_unregister - unregister dpipe table
  2720. *
  2721. * @devlink: devlink
  2722. * @table_name: table name
  2723. */
  2724. void devlink_dpipe_table_unregister(struct devlink *devlink,
  2725. const char *table_name)
  2726. {
  2727. struct devlink_dpipe_table *table;
  2728. mutex_lock(&devlink->lock);
  2729. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2730. table_name);
  2731. if (!table)
  2732. goto unlock;
  2733. list_del_rcu(&table->list);
  2734. mutex_unlock(&devlink->lock);
  2735. kfree_rcu(table, rcu);
  2736. return;
  2737. unlock:
  2738. mutex_unlock(&devlink->lock);
  2739. }
  2740. EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
  2741. /**
  2742. * devlink_resource_register - devlink resource register
  2743. *
  2744. * @devlink: devlink
  2745. * @resource_name: resource's name
  2746. * @top_hierarchy: top hierarchy
  2747. * @reload_required: reload is required for new configuration to
  2748. * apply
  2749. * @resource_size: resource's size
  2750. * @resource_id: resource's id
  2751. * @parent_reosurce_id: resource's parent id
  2752. * @size params: size parameters
  2753. */
  2754. int devlink_resource_register(struct devlink *devlink,
  2755. const char *resource_name,
  2756. u64 resource_size,
  2757. u64 resource_id,
  2758. u64 parent_resource_id,
  2759. const struct devlink_resource_size_params *size_params)
  2760. {
  2761. struct devlink_resource *resource;
  2762. struct list_head *resource_list;
  2763. bool top_hierarchy;
  2764. int err = 0;
  2765. top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
  2766. mutex_lock(&devlink->lock);
  2767. resource = devlink_resource_find(devlink, NULL, resource_id);
  2768. if (resource) {
  2769. err = -EINVAL;
  2770. goto out;
  2771. }
  2772. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  2773. if (!resource) {
  2774. err = -ENOMEM;
  2775. goto out;
  2776. }
  2777. if (top_hierarchy) {
  2778. resource_list = &devlink->resource_list;
  2779. } else {
  2780. struct devlink_resource *parent_resource;
  2781. parent_resource = devlink_resource_find(devlink, NULL,
  2782. parent_resource_id);
  2783. if (parent_resource) {
  2784. resource_list = &parent_resource->resource_list;
  2785. resource->parent = parent_resource;
  2786. } else {
  2787. kfree(resource);
  2788. err = -EINVAL;
  2789. goto out;
  2790. }
  2791. }
  2792. resource->name = resource_name;
  2793. resource->size = resource_size;
  2794. resource->size_new = resource_size;
  2795. resource->id = resource_id;
  2796. resource->size_valid = true;
  2797. memcpy(&resource->size_params, size_params,
  2798. sizeof(resource->size_params));
  2799. INIT_LIST_HEAD(&resource->resource_list);
  2800. list_add_tail(&resource->list, resource_list);
  2801. out:
  2802. mutex_unlock(&devlink->lock);
  2803. return err;
  2804. }
  2805. EXPORT_SYMBOL_GPL(devlink_resource_register);
  2806. /**
  2807. * devlink_resources_unregister - free all resources
  2808. *
  2809. * @devlink: devlink
  2810. * @resource: resource
  2811. */
  2812. void devlink_resources_unregister(struct devlink *devlink,
  2813. struct devlink_resource *resource)
  2814. {
  2815. struct devlink_resource *tmp, *child_resource;
  2816. struct list_head *resource_list;
  2817. if (resource)
  2818. resource_list = &resource->resource_list;
  2819. else
  2820. resource_list = &devlink->resource_list;
  2821. if (!resource)
  2822. mutex_lock(&devlink->lock);
  2823. list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
  2824. devlink_resources_unregister(devlink, child_resource);
  2825. list_del(&child_resource->list);
  2826. kfree(child_resource);
  2827. }
  2828. if (!resource)
  2829. mutex_unlock(&devlink->lock);
  2830. }
  2831. EXPORT_SYMBOL_GPL(devlink_resources_unregister);
  2832. /**
  2833. * devlink_resource_size_get - get and update size
  2834. *
  2835. * @devlink: devlink
  2836. * @resource_id: the requested resource id
  2837. * @p_resource_size: ptr to update
  2838. */
  2839. int devlink_resource_size_get(struct devlink *devlink,
  2840. u64 resource_id,
  2841. u64 *p_resource_size)
  2842. {
  2843. struct devlink_resource *resource;
  2844. int err = 0;
  2845. mutex_lock(&devlink->lock);
  2846. resource = devlink_resource_find(devlink, NULL, resource_id);
  2847. if (!resource) {
  2848. err = -EINVAL;
  2849. goto out;
  2850. }
  2851. *p_resource_size = resource->size_new;
  2852. resource->size = resource->size_new;
  2853. out:
  2854. mutex_unlock(&devlink->lock);
  2855. return err;
  2856. }
  2857. EXPORT_SYMBOL_GPL(devlink_resource_size_get);
  2858. /**
  2859. * devlink_dpipe_table_resource_set - set the resource id
  2860. *
  2861. * @devlink: devlink
  2862. * @table_name: table name
  2863. * @resource_id: resource id
  2864. * @resource_units: number of resource's units consumed per table's entry
  2865. */
  2866. int devlink_dpipe_table_resource_set(struct devlink *devlink,
  2867. const char *table_name, u64 resource_id,
  2868. u64 resource_units)
  2869. {
  2870. struct devlink_dpipe_table *table;
  2871. int err = 0;
  2872. mutex_lock(&devlink->lock);
  2873. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  2874. table_name);
  2875. if (!table) {
  2876. err = -EINVAL;
  2877. goto out;
  2878. }
  2879. table->resource_id = resource_id;
  2880. table->resource_units = resource_units;
  2881. table->resource_valid = true;
  2882. out:
  2883. mutex_unlock(&devlink->lock);
  2884. return err;
  2885. }
  2886. EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
  2887. /**
  2888. * devlink_resource_occ_get_register - register occupancy getter
  2889. *
  2890. * @devlink: devlink
  2891. * @resource_id: resource id
  2892. * @occ_get: occupancy getter callback
  2893. * @occ_get_priv: occupancy getter callback priv
  2894. */
  2895. void devlink_resource_occ_get_register(struct devlink *devlink,
  2896. u64 resource_id,
  2897. devlink_resource_occ_get_t *occ_get,
  2898. void *occ_get_priv)
  2899. {
  2900. struct devlink_resource *resource;
  2901. mutex_lock(&devlink->lock);
  2902. resource = devlink_resource_find(devlink, NULL, resource_id);
  2903. if (WARN_ON(!resource))
  2904. goto out;
  2905. WARN_ON(resource->occ_get);
  2906. resource->occ_get = occ_get;
  2907. resource->occ_get_priv = occ_get_priv;
  2908. out:
  2909. mutex_unlock(&devlink->lock);
  2910. }
  2911. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
  2912. /**
  2913. * devlink_resource_occ_get_unregister - unregister occupancy getter
  2914. *
  2915. * @devlink: devlink
  2916. * @resource_id: resource id
  2917. */
  2918. void devlink_resource_occ_get_unregister(struct devlink *devlink,
  2919. u64 resource_id)
  2920. {
  2921. struct devlink_resource *resource;
  2922. mutex_lock(&devlink->lock);
  2923. resource = devlink_resource_find(devlink, NULL, resource_id);
  2924. if (WARN_ON(!resource))
  2925. goto out;
  2926. WARN_ON(!resource->occ_get);
  2927. resource->occ_get = NULL;
  2928. resource->occ_get_priv = NULL;
  2929. out:
  2930. mutex_unlock(&devlink->lock);
  2931. }
  2932. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
  2933. static int __init devlink_module_init(void)
  2934. {
  2935. return genl_register_family(&devlink_nl_family);
  2936. }
  2937. static void __exit devlink_module_exit(void)
  2938. {
  2939. genl_unregister_family(&devlink_nl_family);
  2940. }
  2941. module_init(devlink_module_init);
  2942. module_exit(devlink_module_exit);
  2943. MODULE_LICENSE("GPL v2");
  2944. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  2945. MODULE_DESCRIPTION("Network physical device Netlink interface");
  2946. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);