devlink.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233
  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. struct devlink_region {
  281. struct devlink *devlink;
  282. struct list_head list;
  283. const char *name;
  284. struct list_head snapshot_list;
  285. u32 max_snapshots;
  286. u32 cur_snapshots;
  287. u64 size;
  288. };
  289. static struct devlink_region *
  290. devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
  291. {
  292. struct devlink_region *region;
  293. list_for_each_entry(region, &devlink->region_list, list)
  294. if (!strcmp(region->name, region_name))
  295. return region;
  296. return NULL;
  297. }
  298. #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
  299. #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
  300. #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
  301. /* The per devlink instance lock is taken by default in the pre-doit
  302. * operation, yet several commands do not require this. The global
  303. * devlink lock is taken and protects from disruption by user-calls.
  304. */
  305. #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
  306. static int devlink_nl_pre_doit(const struct genl_ops *ops,
  307. struct sk_buff *skb, struct genl_info *info)
  308. {
  309. struct devlink *devlink;
  310. int err;
  311. mutex_lock(&devlink_mutex);
  312. devlink = devlink_get_from_info(info);
  313. if (IS_ERR(devlink)) {
  314. mutex_unlock(&devlink_mutex);
  315. return PTR_ERR(devlink);
  316. }
  317. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  318. mutex_lock(&devlink->lock);
  319. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
  320. info->user_ptr[0] = devlink;
  321. } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
  322. struct devlink_port *devlink_port;
  323. devlink_port = devlink_port_get_from_info(devlink, info);
  324. if (IS_ERR(devlink_port)) {
  325. err = PTR_ERR(devlink_port);
  326. goto unlock;
  327. }
  328. info->user_ptr[0] = devlink_port;
  329. }
  330. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
  331. struct devlink_sb *devlink_sb;
  332. devlink_sb = devlink_sb_get_from_info(devlink, info);
  333. if (IS_ERR(devlink_sb)) {
  334. err = PTR_ERR(devlink_sb);
  335. goto unlock;
  336. }
  337. info->user_ptr[1] = devlink_sb;
  338. }
  339. return 0;
  340. unlock:
  341. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  342. mutex_unlock(&devlink->lock);
  343. mutex_unlock(&devlink_mutex);
  344. return err;
  345. }
  346. static void devlink_nl_post_doit(const struct genl_ops *ops,
  347. struct sk_buff *skb, struct genl_info *info)
  348. {
  349. struct devlink *devlink;
  350. devlink = devlink_get_from_info(info);
  351. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  352. mutex_unlock(&devlink->lock);
  353. mutex_unlock(&devlink_mutex);
  354. }
  355. static struct genl_family devlink_nl_family;
  356. enum devlink_multicast_groups {
  357. DEVLINK_MCGRP_CONFIG,
  358. };
  359. static const struct genl_multicast_group devlink_nl_mcgrps[] = {
  360. [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
  361. };
  362. static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  363. {
  364. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  365. return -EMSGSIZE;
  366. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  367. return -EMSGSIZE;
  368. return 0;
  369. }
  370. static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
  371. enum devlink_command cmd, u32 portid,
  372. u32 seq, int flags)
  373. {
  374. void *hdr;
  375. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  376. if (!hdr)
  377. return -EMSGSIZE;
  378. if (devlink_nl_put_handle(msg, devlink))
  379. goto nla_put_failure;
  380. genlmsg_end(msg, hdr);
  381. return 0;
  382. nla_put_failure:
  383. genlmsg_cancel(msg, hdr);
  384. return -EMSGSIZE;
  385. }
  386. static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
  387. {
  388. struct sk_buff *msg;
  389. int err;
  390. WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
  391. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  392. if (!msg)
  393. return;
  394. err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
  395. if (err) {
  396. nlmsg_free(msg);
  397. return;
  398. }
  399. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  400. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  401. }
  402. static int devlink_nl_port_attrs_put(struct sk_buff *msg,
  403. struct devlink_port *devlink_port)
  404. {
  405. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  406. if (!attrs->set)
  407. return 0;
  408. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
  409. return -EMSGSIZE;
  410. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
  411. return -EMSGSIZE;
  412. if (!attrs->split)
  413. return 0;
  414. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
  415. return -EMSGSIZE;
  416. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
  417. attrs->split_subport_number))
  418. return -EMSGSIZE;
  419. return 0;
  420. }
  421. static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
  422. struct devlink_port *devlink_port,
  423. enum devlink_command cmd, u32 portid,
  424. u32 seq, int flags)
  425. {
  426. void *hdr;
  427. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  428. if (!hdr)
  429. return -EMSGSIZE;
  430. if (devlink_nl_put_handle(msg, devlink))
  431. goto nla_put_failure;
  432. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  433. goto nla_put_failure;
  434. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  435. goto nla_put_failure;
  436. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  437. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  438. devlink_port->desired_type))
  439. goto nla_put_failure;
  440. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  441. struct net_device *netdev = devlink_port->type_dev;
  442. if (netdev &&
  443. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  444. netdev->ifindex) ||
  445. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  446. netdev->name)))
  447. goto nla_put_failure;
  448. }
  449. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  450. struct ib_device *ibdev = devlink_port->type_dev;
  451. if (ibdev &&
  452. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  453. ibdev->name))
  454. goto nla_put_failure;
  455. }
  456. if (devlink_nl_port_attrs_put(msg, devlink_port))
  457. goto nla_put_failure;
  458. genlmsg_end(msg, hdr);
  459. return 0;
  460. nla_put_failure:
  461. genlmsg_cancel(msg, hdr);
  462. return -EMSGSIZE;
  463. }
  464. static void devlink_port_notify(struct devlink_port *devlink_port,
  465. enum devlink_command cmd)
  466. {
  467. struct devlink *devlink = devlink_port->devlink;
  468. struct sk_buff *msg;
  469. int err;
  470. if (!devlink_port->registered)
  471. return;
  472. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  473. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  474. if (!msg)
  475. return;
  476. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  477. if (err) {
  478. nlmsg_free(msg);
  479. return;
  480. }
  481. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  482. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  483. }
  484. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  485. {
  486. struct devlink *devlink = info->user_ptr[0];
  487. struct sk_buff *msg;
  488. int err;
  489. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  490. if (!msg)
  491. return -ENOMEM;
  492. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  493. info->snd_portid, info->snd_seq, 0);
  494. if (err) {
  495. nlmsg_free(msg);
  496. return err;
  497. }
  498. return genlmsg_reply(msg, info);
  499. }
  500. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  501. struct netlink_callback *cb)
  502. {
  503. struct devlink *devlink;
  504. int start = cb->args[0];
  505. int idx = 0;
  506. int err;
  507. mutex_lock(&devlink_mutex);
  508. list_for_each_entry(devlink, &devlink_list, list) {
  509. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  510. continue;
  511. if (idx < start) {
  512. idx++;
  513. continue;
  514. }
  515. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  516. NETLINK_CB(cb->skb).portid,
  517. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  518. if (err)
  519. goto out;
  520. idx++;
  521. }
  522. out:
  523. mutex_unlock(&devlink_mutex);
  524. cb->args[0] = idx;
  525. return msg->len;
  526. }
  527. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  528. struct genl_info *info)
  529. {
  530. struct devlink_port *devlink_port = info->user_ptr[0];
  531. struct devlink *devlink = devlink_port->devlink;
  532. struct sk_buff *msg;
  533. int err;
  534. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  535. if (!msg)
  536. return -ENOMEM;
  537. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  538. DEVLINK_CMD_PORT_NEW,
  539. info->snd_portid, info->snd_seq, 0);
  540. if (err) {
  541. nlmsg_free(msg);
  542. return err;
  543. }
  544. return genlmsg_reply(msg, info);
  545. }
  546. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  547. struct netlink_callback *cb)
  548. {
  549. struct devlink *devlink;
  550. struct devlink_port *devlink_port;
  551. int start = cb->args[0];
  552. int idx = 0;
  553. int err;
  554. mutex_lock(&devlink_mutex);
  555. list_for_each_entry(devlink, &devlink_list, list) {
  556. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  557. continue;
  558. mutex_lock(&devlink->lock);
  559. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  560. if (idx < start) {
  561. idx++;
  562. continue;
  563. }
  564. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  565. DEVLINK_CMD_NEW,
  566. NETLINK_CB(cb->skb).portid,
  567. cb->nlh->nlmsg_seq,
  568. NLM_F_MULTI);
  569. if (err) {
  570. mutex_unlock(&devlink->lock);
  571. goto out;
  572. }
  573. idx++;
  574. }
  575. mutex_unlock(&devlink->lock);
  576. }
  577. out:
  578. mutex_unlock(&devlink_mutex);
  579. cb->args[0] = idx;
  580. return msg->len;
  581. }
  582. static int devlink_port_type_set(struct devlink *devlink,
  583. struct devlink_port *devlink_port,
  584. enum devlink_port_type port_type)
  585. {
  586. int err;
  587. if (devlink->ops && devlink->ops->port_type_set) {
  588. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  589. return -EINVAL;
  590. if (port_type == devlink_port->type)
  591. return 0;
  592. err = devlink->ops->port_type_set(devlink_port, port_type);
  593. if (err)
  594. return err;
  595. devlink_port->desired_type = port_type;
  596. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  597. return 0;
  598. }
  599. return -EOPNOTSUPP;
  600. }
  601. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  602. struct genl_info *info)
  603. {
  604. struct devlink_port *devlink_port = info->user_ptr[0];
  605. struct devlink *devlink = devlink_port->devlink;
  606. int err;
  607. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  608. enum devlink_port_type port_type;
  609. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  610. err = devlink_port_type_set(devlink, devlink_port, port_type);
  611. if (err)
  612. return err;
  613. }
  614. return 0;
  615. }
  616. static int devlink_port_split(struct devlink *devlink, u32 port_index,
  617. u32 count, struct netlink_ext_ack *extack)
  618. {
  619. if (devlink->ops && devlink->ops->port_split)
  620. return devlink->ops->port_split(devlink, port_index, count,
  621. extack);
  622. return -EOPNOTSUPP;
  623. }
  624. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  625. struct genl_info *info)
  626. {
  627. struct devlink *devlink = info->user_ptr[0];
  628. u32 port_index;
  629. u32 count;
  630. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  631. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  632. return -EINVAL;
  633. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  634. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  635. return devlink_port_split(devlink, port_index, count, info->extack);
  636. }
  637. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
  638. struct netlink_ext_ack *extack)
  639. {
  640. if (devlink->ops && devlink->ops->port_unsplit)
  641. return devlink->ops->port_unsplit(devlink, port_index, extack);
  642. return -EOPNOTSUPP;
  643. }
  644. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  645. struct genl_info *info)
  646. {
  647. struct devlink *devlink = info->user_ptr[0];
  648. u32 port_index;
  649. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  650. return -EINVAL;
  651. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  652. return devlink_port_unsplit(devlink, port_index, info->extack);
  653. }
  654. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  655. struct devlink_sb *devlink_sb,
  656. enum devlink_command cmd, u32 portid,
  657. u32 seq, int flags)
  658. {
  659. void *hdr;
  660. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  661. if (!hdr)
  662. return -EMSGSIZE;
  663. if (devlink_nl_put_handle(msg, devlink))
  664. goto nla_put_failure;
  665. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  666. goto nla_put_failure;
  667. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  668. goto nla_put_failure;
  669. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  670. devlink_sb->ingress_pools_count))
  671. goto nla_put_failure;
  672. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  673. devlink_sb->egress_pools_count))
  674. goto nla_put_failure;
  675. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  676. devlink_sb->ingress_tc_count))
  677. goto nla_put_failure;
  678. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  679. devlink_sb->egress_tc_count))
  680. goto nla_put_failure;
  681. genlmsg_end(msg, hdr);
  682. return 0;
  683. nla_put_failure:
  684. genlmsg_cancel(msg, hdr);
  685. return -EMSGSIZE;
  686. }
  687. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  688. struct genl_info *info)
  689. {
  690. struct devlink *devlink = info->user_ptr[0];
  691. struct devlink_sb *devlink_sb = info->user_ptr[1];
  692. struct sk_buff *msg;
  693. int err;
  694. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  695. if (!msg)
  696. return -ENOMEM;
  697. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  698. DEVLINK_CMD_SB_NEW,
  699. info->snd_portid, info->snd_seq, 0);
  700. if (err) {
  701. nlmsg_free(msg);
  702. return err;
  703. }
  704. return genlmsg_reply(msg, info);
  705. }
  706. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  707. struct netlink_callback *cb)
  708. {
  709. struct devlink *devlink;
  710. struct devlink_sb *devlink_sb;
  711. int start = cb->args[0];
  712. int idx = 0;
  713. int err;
  714. mutex_lock(&devlink_mutex);
  715. list_for_each_entry(devlink, &devlink_list, list) {
  716. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  717. continue;
  718. mutex_lock(&devlink->lock);
  719. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  720. if (idx < start) {
  721. idx++;
  722. continue;
  723. }
  724. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  725. DEVLINK_CMD_SB_NEW,
  726. NETLINK_CB(cb->skb).portid,
  727. cb->nlh->nlmsg_seq,
  728. NLM_F_MULTI);
  729. if (err) {
  730. mutex_unlock(&devlink->lock);
  731. goto out;
  732. }
  733. idx++;
  734. }
  735. mutex_unlock(&devlink->lock);
  736. }
  737. out:
  738. mutex_unlock(&devlink_mutex);
  739. cb->args[0] = idx;
  740. return msg->len;
  741. }
  742. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  743. struct devlink_sb *devlink_sb,
  744. u16 pool_index, enum devlink_command cmd,
  745. u32 portid, u32 seq, int flags)
  746. {
  747. struct devlink_sb_pool_info pool_info;
  748. void *hdr;
  749. int err;
  750. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  751. pool_index, &pool_info);
  752. if (err)
  753. return err;
  754. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  755. if (!hdr)
  756. return -EMSGSIZE;
  757. if (devlink_nl_put_handle(msg, devlink))
  758. goto nla_put_failure;
  759. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  760. goto nla_put_failure;
  761. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  762. goto nla_put_failure;
  763. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  764. goto nla_put_failure;
  765. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  766. goto nla_put_failure;
  767. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  768. pool_info.threshold_type))
  769. goto nla_put_failure;
  770. genlmsg_end(msg, hdr);
  771. return 0;
  772. nla_put_failure:
  773. genlmsg_cancel(msg, hdr);
  774. return -EMSGSIZE;
  775. }
  776. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  777. struct genl_info *info)
  778. {
  779. struct devlink *devlink = info->user_ptr[0];
  780. struct devlink_sb *devlink_sb = info->user_ptr[1];
  781. struct sk_buff *msg;
  782. u16 pool_index;
  783. int err;
  784. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  785. &pool_index);
  786. if (err)
  787. return err;
  788. if (!devlink->ops || !devlink->ops->sb_pool_get)
  789. return -EOPNOTSUPP;
  790. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  791. if (!msg)
  792. return -ENOMEM;
  793. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  794. DEVLINK_CMD_SB_POOL_NEW,
  795. info->snd_portid, info->snd_seq, 0);
  796. if (err) {
  797. nlmsg_free(msg);
  798. return err;
  799. }
  800. return genlmsg_reply(msg, info);
  801. }
  802. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  803. struct devlink *devlink,
  804. struct devlink_sb *devlink_sb,
  805. u32 portid, u32 seq)
  806. {
  807. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  808. u16 pool_index;
  809. int err;
  810. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  811. if (*p_idx < start) {
  812. (*p_idx)++;
  813. continue;
  814. }
  815. err = devlink_nl_sb_pool_fill(msg, devlink,
  816. devlink_sb,
  817. pool_index,
  818. DEVLINK_CMD_SB_POOL_NEW,
  819. portid, seq, NLM_F_MULTI);
  820. if (err)
  821. return err;
  822. (*p_idx)++;
  823. }
  824. return 0;
  825. }
  826. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  827. struct netlink_callback *cb)
  828. {
  829. struct devlink *devlink;
  830. struct devlink_sb *devlink_sb;
  831. int start = cb->args[0];
  832. int idx = 0;
  833. int err;
  834. mutex_lock(&devlink_mutex);
  835. list_for_each_entry(devlink, &devlink_list, list) {
  836. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  837. !devlink->ops || !devlink->ops->sb_pool_get)
  838. continue;
  839. mutex_lock(&devlink->lock);
  840. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  841. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  842. devlink_sb,
  843. NETLINK_CB(cb->skb).portid,
  844. cb->nlh->nlmsg_seq);
  845. if (err && err != -EOPNOTSUPP) {
  846. mutex_unlock(&devlink->lock);
  847. goto out;
  848. }
  849. }
  850. mutex_unlock(&devlink->lock);
  851. }
  852. out:
  853. mutex_unlock(&devlink_mutex);
  854. cb->args[0] = idx;
  855. return msg->len;
  856. }
  857. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  858. u16 pool_index, u32 size,
  859. enum devlink_sb_threshold_type threshold_type)
  860. {
  861. const struct devlink_ops *ops = devlink->ops;
  862. if (ops && ops->sb_pool_set)
  863. return ops->sb_pool_set(devlink, sb_index, pool_index,
  864. size, threshold_type);
  865. return -EOPNOTSUPP;
  866. }
  867. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  868. struct genl_info *info)
  869. {
  870. struct devlink *devlink = info->user_ptr[0];
  871. struct devlink_sb *devlink_sb = info->user_ptr[1];
  872. enum devlink_sb_threshold_type threshold_type;
  873. u16 pool_index;
  874. u32 size;
  875. int err;
  876. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  877. &pool_index);
  878. if (err)
  879. return err;
  880. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  881. if (err)
  882. return err;
  883. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  884. return -EINVAL;
  885. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  886. return devlink_sb_pool_set(devlink, devlink_sb->index,
  887. pool_index, size, threshold_type);
  888. }
  889. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  890. struct devlink *devlink,
  891. struct devlink_port *devlink_port,
  892. struct devlink_sb *devlink_sb,
  893. u16 pool_index,
  894. enum devlink_command cmd,
  895. u32 portid, u32 seq, int flags)
  896. {
  897. const struct devlink_ops *ops = devlink->ops;
  898. u32 threshold;
  899. void *hdr;
  900. int err;
  901. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  902. pool_index, &threshold);
  903. if (err)
  904. return err;
  905. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  906. if (!hdr)
  907. return -EMSGSIZE;
  908. if (devlink_nl_put_handle(msg, devlink))
  909. goto nla_put_failure;
  910. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  911. goto nla_put_failure;
  912. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  913. goto nla_put_failure;
  914. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  915. goto nla_put_failure;
  916. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  917. goto nla_put_failure;
  918. if (ops->sb_occ_port_pool_get) {
  919. u32 cur;
  920. u32 max;
  921. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  922. pool_index, &cur, &max);
  923. if (err && err != -EOPNOTSUPP)
  924. return err;
  925. if (!err) {
  926. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  927. goto nla_put_failure;
  928. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  929. goto nla_put_failure;
  930. }
  931. }
  932. genlmsg_end(msg, hdr);
  933. return 0;
  934. nla_put_failure:
  935. genlmsg_cancel(msg, hdr);
  936. return -EMSGSIZE;
  937. }
  938. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  939. struct genl_info *info)
  940. {
  941. struct devlink_port *devlink_port = info->user_ptr[0];
  942. struct devlink *devlink = devlink_port->devlink;
  943. struct devlink_sb *devlink_sb = info->user_ptr[1];
  944. struct sk_buff *msg;
  945. u16 pool_index;
  946. int err;
  947. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  948. &pool_index);
  949. if (err)
  950. return err;
  951. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  952. return -EOPNOTSUPP;
  953. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  954. if (!msg)
  955. return -ENOMEM;
  956. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  957. devlink_sb, pool_index,
  958. DEVLINK_CMD_SB_PORT_POOL_NEW,
  959. info->snd_portid, info->snd_seq, 0);
  960. if (err) {
  961. nlmsg_free(msg);
  962. return err;
  963. }
  964. return genlmsg_reply(msg, info);
  965. }
  966. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  967. struct devlink *devlink,
  968. struct devlink_sb *devlink_sb,
  969. u32 portid, u32 seq)
  970. {
  971. struct devlink_port *devlink_port;
  972. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  973. u16 pool_index;
  974. int err;
  975. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  976. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  977. if (*p_idx < start) {
  978. (*p_idx)++;
  979. continue;
  980. }
  981. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  982. devlink_port,
  983. devlink_sb,
  984. pool_index,
  985. DEVLINK_CMD_SB_PORT_POOL_NEW,
  986. portid, seq,
  987. NLM_F_MULTI);
  988. if (err)
  989. return err;
  990. (*p_idx)++;
  991. }
  992. }
  993. return 0;
  994. }
  995. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  996. struct netlink_callback *cb)
  997. {
  998. struct devlink *devlink;
  999. struct devlink_sb *devlink_sb;
  1000. int start = cb->args[0];
  1001. int idx = 0;
  1002. int err;
  1003. mutex_lock(&devlink_mutex);
  1004. list_for_each_entry(devlink, &devlink_list, list) {
  1005. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1006. !devlink->ops || !devlink->ops->sb_port_pool_get)
  1007. continue;
  1008. mutex_lock(&devlink->lock);
  1009. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1010. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  1011. devlink, devlink_sb,
  1012. NETLINK_CB(cb->skb).portid,
  1013. cb->nlh->nlmsg_seq);
  1014. if (err && err != -EOPNOTSUPP) {
  1015. mutex_unlock(&devlink->lock);
  1016. goto out;
  1017. }
  1018. }
  1019. mutex_unlock(&devlink->lock);
  1020. }
  1021. out:
  1022. mutex_unlock(&devlink_mutex);
  1023. cb->args[0] = idx;
  1024. return msg->len;
  1025. }
  1026. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  1027. unsigned int sb_index, u16 pool_index,
  1028. u32 threshold)
  1029. {
  1030. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1031. if (ops && ops->sb_port_pool_set)
  1032. return ops->sb_port_pool_set(devlink_port, sb_index,
  1033. pool_index, threshold);
  1034. return -EOPNOTSUPP;
  1035. }
  1036. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  1037. struct genl_info *info)
  1038. {
  1039. struct devlink_port *devlink_port = info->user_ptr[0];
  1040. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1041. u16 pool_index;
  1042. u32 threshold;
  1043. int err;
  1044. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1045. &pool_index);
  1046. if (err)
  1047. return err;
  1048. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1049. return -EINVAL;
  1050. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1051. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  1052. pool_index, threshold);
  1053. }
  1054. static int
  1055. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  1056. struct devlink_port *devlink_port,
  1057. struct devlink_sb *devlink_sb, u16 tc_index,
  1058. enum devlink_sb_pool_type pool_type,
  1059. enum devlink_command cmd,
  1060. u32 portid, u32 seq, int flags)
  1061. {
  1062. const struct devlink_ops *ops = devlink->ops;
  1063. u16 pool_index;
  1064. u32 threshold;
  1065. void *hdr;
  1066. int err;
  1067. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  1068. tc_index, pool_type,
  1069. &pool_index, &threshold);
  1070. if (err)
  1071. return err;
  1072. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1073. if (!hdr)
  1074. return -EMSGSIZE;
  1075. if (devlink_nl_put_handle(msg, devlink))
  1076. goto nla_put_failure;
  1077. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  1078. goto nla_put_failure;
  1079. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  1080. goto nla_put_failure;
  1081. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  1082. goto nla_put_failure;
  1083. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  1084. goto nla_put_failure;
  1085. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  1086. goto nla_put_failure;
  1087. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  1088. goto nla_put_failure;
  1089. if (ops->sb_occ_tc_port_bind_get) {
  1090. u32 cur;
  1091. u32 max;
  1092. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1093. devlink_sb->index,
  1094. tc_index, pool_type,
  1095. &cur, &max);
  1096. if (err && err != -EOPNOTSUPP)
  1097. return err;
  1098. if (!err) {
  1099. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1100. goto nla_put_failure;
  1101. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1102. goto nla_put_failure;
  1103. }
  1104. }
  1105. genlmsg_end(msg, hdr);
  1106. return 0;
  1107. nla_put_failure:
  1108. genlmsg_cancel(msg, hdr);
  1109. return -EMSGSIZE;
  1110. }
  1111. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1112. struct genl_info *info)
  1113. {
  1114. struct devlink_port *devlink_port = info->user_ptr[0];
  1115. struct devlink *devlink = devlink_port->devlink;
  1116. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1117. struct sk_buff *msg;
  1118. enum devlink_sb_pool_type pool_type;
  1119. u16 tc_index;
  1120. int err;
  1121. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1122. if (err)
  1123. return err;
  1124. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1125. pool_type, &tc_index);
  1126. if (err)
  1127. return err;
  1128. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1129. return -EOPNOTSUPP;
  1130. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1131. if (!msg)
  1132. return -ENOMEM;
  1133. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1134. devlink_sb, tc_index, pool_type,
  1135. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1136. info->snd_portid,
  1137. info->snd_seq, 0);
  1138. if (err) {
  1139. nlmsg_free(msg);
  1140. return err;
  1141. }
  1142. return genlmsg_reply(msg, info);
  1143. }
  1144. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1145. int start, int *p_idx,
  1146. struct devlink *devlink,
  1147. struct devlink_sb *devlink_sb,
  1148. u32 portid, u32 seq)
  1149. {
  1150. struct devlink_port *devlink_port;
  1151. u16 tc_index;
  1152. int err;
  1153. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1154. for (tc_index = 0;
  1155. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1156. if (*p_idx < start) {
  1157. (*p_idx)++;
  1158. continue;
  1159. }
  1160. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1161. devlink_port,
  1162. devlink_sb,
  1163. tc_index,
  1164. DEVLINK_SB_POOL_TYPE_INGRESS,
  1165. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1166. portid, seq,
  1167. NLM_F_MULTI);
  1168. if (err)
  1169. return err;
  1170. (*p_idx)++;
  1171. }
  1172. for (tc_index = 0;
  1173. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1174. if (*p_idx < start) {
  1175. (*p_idx)++;
  1176. continue;
  1177. }
  1178. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1179. devlink_port,
  1180. devlink_sb,
  1181. tc_index,
  1182. DEVLINK_SB_POOL_TYPE_EGRESS,
  1183. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1184. portid, seq,
  1185. NLM_F_MULTI);
  1186. if (err)
  1187. return err;
  1188. (*p_idx)++;
  1189. }
  1190. }
  1191. return 0;
  1192. }
  1193. static int
  1194. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1195. struct netlink_callback *cb)
  1196. {
  1197. struct devlink *devlink;
  1198. struct devlink_sb *devlink_sb;
  1199. int start = cb->args[0];
  1200. int idx = 0;
  1201. int err;
  1202. mutex_lock(&devlink_mutex);
  1203. list_for_each_entry(devlink, &devlink_list, list) {
  1204. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1205. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1206. continue;
  1207. mutex_lock(&devlink->lock);
  1208. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1209. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1210. devlink,
  1211. devlink_sb,
  1212. NETLINK_CB(cb->skb).portid,
  1213. cb->nlh->nlmsg_seq);
  1214. if (err && err != -EOPNOTSUPP) {
  1215. mutex_unlock(&devlink->lock);
  1216. goto out;
  1217. }
  1218. }
  1219. mutex_unlock(&devlink->lock);
  1220. }
  1221. out:
  1222. mutex_unlock(&devlink_mutex);
  1223. cb->args[0] = idx;
  1224. return msg->len;
  1225. }
  1226. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1227. unsigned int sb_index, u16 tc_index,
  1228. enum devlink_sb_pool_type pool_type,
  1229. u16 pool_index, u32 threshold)
  1230. {
  1231. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1232. if (ops && ops->sb_tc_pool_bind_set)
  1233. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1234. tc_index, pool_type,
  1235. pool_index, threshold);
  1236. return -EOPNOTSUPP;
  1237. }
  1238. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1239. struct genl_info *info)
  1240. {
  1241. struct devlink_port *devlink_port = info->user_ptr[0];
  1242. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1243. enum devlink_sb_pool_type pool_type;
  1244. u16 tc_index;
  1245. u16 pool_index;
  1246. u32 threshold;
  1247. int err;
  1248. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1249. if (err)
  1250. return err;
  1251. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1252. pool_type, &tc_index);
  1253. if (err)
  1254. return err;
  1255. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1256. &pool_index);
  1257. if (err)
  1258. return err;
  1259. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1260. return -EINVAL;
  1261. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1262. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1263. tc_index, pool_type,
  1264. pool_index, threshold);
  1265. }
  1266. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1267. struct genl_info *info)
  1268. {
  1269. struct devlink *devlink = info->user_ptr[0];
  1270. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1271. const struct devlink_ops *ops = devlink->ops;
  1272. if (ops && ops->sb_occ_snapshot)
  1273. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1274. return -EOPNOTSUPP;
  1275. }
  1276. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1277. struct genl_info *info)
  1278. {
  1279. struct devlink *devlink = info->user_ptr[0];
  1280. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1281. const struct devlink_ops *ops = devlink->ops;
  1282. if (ops && ops->sb_occ_max_clear)
  1283. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1284. return -EOPNOTSUPP;
  1285. }
  1286. static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
  1287. enum devlink_command cmd, u32 portid,
  1288. u32 seq, int flags)
  1289. {
  1290. const struct devlink_ops *ops = devlink->ops;
  1291. u8 inline_mode, encap_mode;
  1292. void *hdr;
  1293. int err = 0;
  1294. u16 mode;
  1295. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1296. if (!hdr)
  1297. return -EMSGSIZE;
  1298. err = devlink_nl_put_handle(msg, devlink);
  1299. if (err)
  1300. goto nla_put_failure;
  1301. if (ops->eswitch_mode_get) {
  1302. err = ops->eswitch_mode_get(devlink, &mode);
  1303. if (err)
  1304. goto nla_put_failure;
  1305. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1306. if (err)
  1307. goto nla_put_failure;
  1308. }
  1309. if (ops->eswitch_inline_mode_get) {
  1310. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1311. if (err)
  1312. goto nla_put_failure;
  1313. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1314. inline_mode);
  1315. if (err)
  1316. goto nla_put_failure;
  1317. }
  1318. if (ops->eswitch_encap_mode_get) {
  1319. err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
  1320. if (err)
  1321. goto nla_put_failure;
  1322. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
  1323. if (err)
  1324. goto nla_put_failure;
  1325. }
  1326. genlmsg_end(msg, hdr);
  1327. return 0;
  1328. nla_put_failure:
  1329. genlmsg_cancel(msg, hdr);
  1330. return err;
  1331. }
  1332. static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
  1333. struct genl_info *info)
  1334. {
  1335. struct devlink *devlink = info->user_ptr[0];
  1336. const struct devlink_ops *ops = devlink->ops;
  1337. struct sk_buff *msg;
  1338. int err;
  1339. if (!ops)
  1340. return -EOPNOTSUPP;
  1341. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1342. if (!msg)
  1343. return -ENOMEM;
  1344. err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
  1345. info->snd_portid, info->snd_seq, 0);
  1346. if (err) {
  1347. nlmsg_free(msg);
  1348. return err;
  1349. }
  1350. return genlmsg_reply(msg, info);
  1351. }
  1352. static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
  1353. struct genl_info *info)
  1354. {
  1355. struct devlink *devlink = info->user_ptr[0];
  1356. const struct devlink_ops *ops = devlink->ops;
  1357. u8 inline_mode, encap_mode;
  1358. int err = 0;
  1359. u16 mode;
  1360. if (!ops)
  1361. return -EOPNOTSUPP;
  1362. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1363. if (!ops->eswitch_mode_set)
  1364. return -EOPNOTSUPP;
  1365. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1366. err = ops->eswitch_mode_set(devlink, mode);
  1367. if (err)
  1368. return err;
  1369. }
  1370. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1371. if (!ops->eswitch_inline_mode_set)
  1372. return -EOPNOTSUPP;
  1373. inline_mode = nla_get_u8(
  1374. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1375. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1376. if (err)
  1377. return err;
  1378. }
  1379. if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
  1380. if (!ops->eswitch_encap_mode_set)
  1381. return -EOPNOTSUPP;
  1382. encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
  1383. err = ops->eswitch_encap_mode_set(devlink, encap_mode);
  1384. if (err)
  1385. return err;
  1386. }
  1387. return 0;
  1388. }
  1389. int devlink_dpipe_match_put(struct sk_buff *skb,
  1390. struct devlink_dpipe_match *match)
  1391. {
  1392. struct devlink_dpipe_header *header = match->header;
  1393. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  1394. struct nlattr *match_attr;
  1395. match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
  1396. if (!match_attr)
  1397. return -EMSGSIZE;
  1398. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  1399. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->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, match_attr);
  1405. return 0;
  1406. nla_put_failure:
  1407. nla_nest_cancel(skb, match_attr);
  1408. return -EMSGSIZE;
  1409. }
  1410. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  1411. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  1412. struct sk_buff *skb)
  1413. {
  1414. struct nlattr *matches_attr;
  1415. matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  1416. if (!matches_attr)
  1417. return -EMSGSIZE;
  1418. if (table->table_ops->matches_dump(table->priv, skb))
  1419. goto nla_put_failure;
  1420. nla_nest_end(skb, matches_attr);
  1421. return 0;
  1422. nla_put_failure:
  1423. nla_nest_cancel(skb, matches_attr);
  1424. return -EMSGSIZE;
  1425. }
  1426. int devlink_dpipe_action_put(struct sk_buff *skb,
  1427. struct devlink_dpipe_action *action)
  1428. {
  1429. struct devlink_dpipe_header *header = action->header;
  1430. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  1431. struct nlattr *action_attr;
  1432. action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
  1433. if (!action_attr)
  1434. return -EMSGSIZE;
  1435. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  1436. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  1437. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1438. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1439. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1440. goto nla_put_failure;
  1441. nla_nest_end(skb, action_attr);
  1442. return 0;
  1443. nla_put_failure:
  1444. nla_nest_cancel(skb, action_attr);
  1445. return -EMSGSIZE;
  1446. }
  1447. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  1448. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  1449. struct sk_buff *skb)
  1450. {
  1451. struct nlattr *actions_attr;
  1452. actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  1453. if (!actions_attr)
  1454. return -EMSGSIZE;
  1455. if (table->table_ops->actions_dump(table->priv, skb))
  1456. goto nla_put_failure;
  1457. nla_nest_end(skb, actions_attr);
  1458. return 0;
  1459. nla_put_failure:
  1460. nla_nest_cancel(skb, actions_attr);
  1461. return -EMSGSIZE;
  1462. }
  1463. static int devlink_dpipe_table_put(struct sk_buff *skb,
  1464. struct devlink_dpipe_table *table)
  1465. {
  1466. struct nlattr *table_attr;
  1467. u64 table_size;
  1468. table_size = table->table_ops->size_get(table->priv);
  1469. table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
  1470. if (!table_attr)
  1471. return -EMSGSIZE;
  1472. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  1473. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
  1474. DEVLINK_ATTR_PAD))
  1475. goto nla_put_failure;
  1476. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  1477. table->counters_enabled))
  1478. goto nla_put_failure;
  1479. if (table->resource_valid) {
  1480. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
  1481. table->resource_id, DEVLINK_ATTR_PAD) ||
  1482. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
  1483. table->resource_units, DEVLINK_ATTR_PAD))
  1484. goto nla_put_failure;
  1485. }
  1486. if (devlink_dpipe_matches_put(table, skb))
  1487. goto nla_put_failure;
  1488. if (devlink_dpipe_actions_put(table, skb))
  1489. goto nla_put_failure;
  1490. nla_nest_end(skb, table_attr);
  1491. return 0;
  1492. nla_put_failure:
  1493. nla_nest_cancel(skb, table_attr);
  1494. return -EMSGSIZE;
  1495. }
  1496. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  1497. struct genl_info *info)
  1498. {
  1499. int err;
  1500. if (*pskb) {
  1501. err = genlmsg_reply(*pskb, info);
  1502. if (err)
  1503. return err;
  1504. }
  1505. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1506. if (!*pskb)
  1507. return -ENOMEM;
  1508. return 0;
  1509. }
  1510. static int devlink_dpipe_tables_fill(struct genl_info *info,
  1511. enum devlink_command cmd, int flags,
  1512. struct list_head *dpipe_tables,
  1513. const char *table_name)
  1514. {
  1515. struct devlink *devlink = info->user_ptr[0];
  1516. struct devlink_dpipe_table *table;
  1517. struct nlattr *tables_attr;
  1518. struct sk_buff *skb = NULL;
  1519. struct nlmsghdr *nlh;
  1520. bool incomplete;
  1521. void *hdr;
  1522. int i;
  1523. int err;
  1524. table = list_first_entry(dpipe_tables,
  1525. struct devlink_dpipe_table, list);
  1526. start_again:
  1527. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1528. if (err)
  1529. return err;
  1530. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1531. &devlink_nl_family, NLM_F_MULTI, cmd);
  1532. if (!hdr) {
  1533. nlmsg_free(skb);
  1534. return -EMSGSIZE;
  1535. }
  1536. if (devlink_nl_put_handle(skb, devlink))
  1537. goto nla_put_failure;
  1538. tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
  1539. if (!tables_attr)
  1540. goto nla_put_failure;
  1541. i = 0;
  1542. incomplete = false;
  1543. list_for_each_entry_from(table, dpipe_tables, list) {
  1544. if (!table_name) {
  1545. err = devlink_dpipe_table_put(skb, table);
  1546. if (err) {
  1547. if (!i)
  1548. goto err_table_put;
  1549. incomplete = true;
  1550. break;
  1551. }
  1552. } else {
  1553. if (!strcmp(table->name, table_name)) {
  1554. err = devlink_dpipe_table_put(skb, table);
  1555. if (err)
  1556. break;
  1557. }
  1558. }
  1559. i++;
  1560. }
  1561. nla_nest_end(skb, tables_attr);
  1562. genlmsg_end(skb, hdr);
  1563. if (incomplete)
  1564. goto start_again;
  1565. send_done:
  1566. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1567. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1568. if (!nlh) {
  1569. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1570. if (err)
  1571. return err;
  1572. goto send_done;
  1573. }
  1574. return genlmsg_reply(skb, info);
  1575. nla_put_failure:
  1576. err = -EMSGSIZE;
  1577. err_table_put:
  1578. nlmsg_free(skb);
  1579. return err;
  1580. }
  1581. static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
  1582. struct genl_info *info)
  1583. {
  1584. struct devlink *devlink = info->user_ptr[0];
  1585. const char *table_name = NULL;
  1586. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1587. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1588. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  1589. &devlink->dpipe_table_list,
  1590. table_name);
  1591. }
  1592. static int devlink_dpipe_value_put(struct sk_buff *skb,
  1593. struct devlink_dpipe_value *value)
  1594. {
  1595. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  1596. value->value_size, value->value))
  1597. return -EMSGSIZE;
  1598. if (value->mask)
  1599. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  1600. value->value_size, value->mask))
  1601. return -EMSGSIZE;
  1602. if (value->mapping_valid)
  1603. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  1604. value->mapping_value))
  1605. return -EMSGSIZE;
  1606. return 0;
  1607. }
  1608. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  1609. struct devlink_dpipe_value *value)
  1610. {
  1611. if (!value->action)
  1612. return -EINVAL;
  1613. if (devlink_dpipe_action_put(skb, value->action))
  1614. return -EMSGSIZE;
  1615. if (devlink_dpipe_value_put(skb, value))
  1616. return -EMSGSIZE;
  1617. return 0;
  1618. }
  1619. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  1620. struct devlink_dpipe_value *values,
  1621. unsigned int values_count)
  1622. {
  1623. struct nlattr *action_attr;
  1624. int i;
  1625. int err;
  1626. for (i = 0; i < values_count; i++) {
  1627. action_attr = nla_nest_start(skb,
  1628. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  1629. if (!action_attr)
  1630. return -EMSGSIZE;
  1631. err = devlink_dpipe_action_value_put(skb, &values[i]);
  1632. if (err)
  1633. goto err_action_value_put;
  1634. nla_nest_end(skb, action_attr);
  1635. }
  1636. return 0;
  1637. err_action_value_put:
  1638. nla_nest_cancel(skb, action_attr);
  1639. return err;
  1640. }
  1641. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  1642. struct devlink_dpipe_value *value)
  1643. {
  1644. if (!value->match)
  1645. return -EINVAL;
  1646. if (devlink_dpipe_match_put(skb, value->match))
  1647. return -EMSGSIZE;
  1648. if (devlink_dpipe_value_put(skb, value))
  1649. return -EMSGSIZE;
  1650. return 0;
  1651. }
  1652. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  1653. struct devlink_dpipe_value *values,
  1654. unsigned int values_count)
  1655. {
  1656. struct nlattr *match_attr;
  1657. int i;
  1658. int err;
  1659. for (i = 0; i < values_count; i++) {
  1660. match_attr = nla_nest_start(skb,
  1661. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  1662. if (!match_attr)
  1663. return -EMSGSIZE;
  1664. err = devlink_dpipe_match_value_put(skb, &values[i]);
  1665. if (err)
  1666. goto err_match_value_put;
  1667. nla_nest_end(skb, match_attr);
  1668. }
  1669. return 0;
  1670. err_match_value_put:
  1671. nla_nest_cancel(skb, match_attr);
  1672. return err;
  1673. }
  1674. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  1675. struct devlink_dpipe_entry *entry)
  1676. {
  1677. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  1678. int err;
  1679. entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  1680. if (!entry_attr)
  1681. return -EMSGSIZE;
  1682. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  1683. DEVLINK_ATTR_PAD))
  1684. goto nla_put_failure;
  1685. if (entry->counter_valid)
  1686. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  1687. entry->counter, DEVLINK_ATTR_PAD))
  1688. goto nla_put_failure;
  1689. matches_attr = nla_nest_start(skb,
  1690. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  1691. if (!matches_attr)
  1692. goto nla_put_failure;
  1693. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  1694. entry->match_values_count);
  1695. if (err) {
  1696. nla_nest_cancel(skb, matches_attr);
  1697. goto err_match_values_put;
  1698. }
  1699. nla_nest_end(skb, matches_attr);
  1700. actions_attr = nla_nest_start(skb,
  1701. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  1702. if (!actions_attr)
  1703. goto nla_put_failure;
  1704. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  1705. entry->action_values_count);
  1706. if (err) {
  1707. nla_nest_cancel(skb, actions_attr);
  1708. goto err_action_values_put;
  1709. }
  1710. nla_nest_end(skb, actions_attr);
  1711. nla_nest_end(skb, entry_attr);
  1712. return 0;
  1713. nla_put_failure:
  1714. err = -EMSGSIZE;
  1715. err_match_values_put:
  1716. err_action_values_put:
  1717. nla_nest_cancel(skb, entry_attr);
  1718. return err;
  1719. }
  1720. static struct devlink_dpipe_table *
  1721. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  1722. const char *table_name)
  1723. {
  1724. struct devlink_dpipe_table *table;
  1725. list_for_each_entry_rcu(table, dpipe_tables, list) {
  1726. if (!strcmp(table->name, table_name))
  1727. return table;
  1728. }
  1729. return NULL;
  1730. }
  1731. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  1732. {
  1733. struct devlink *devlink;
  1734. int err;
  1735. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  1736. dump_ctx->info);
  1737. if (err)
  1738. return err;
  1739. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  1740. dump_ctx->info->snd_portid,
  1741. dump_ctx->info->snd_seq,
  1742. &devlink_nl_family, NLM_F_MULTI,
  1743. dump_ctx->cmd);
  1744. if (!dump_ctx->hdr)
  1745. goto nla_put_failure;
  1746. devlink = dump_ctx->info->user_ptr[0];
  1747. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  1748. goto nla_put_failure;
  1749. dump_ctx->nest = nla_nest_start(dump_ctx->skb,
  1750. DEVLINK_ATTR_DPIPE_ENTRIES);
  1751. if (!dump_ctx->nest)
  1752. goto nla_put_failure;
  1753. return 0;
  1754. nla_put_failure:
  1755. nlmsg_free(dump_ctx->skb);
  1756. return -EMSGSIZE;
  1757. }
  1758. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  1759. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  1760. struct devlink_dpipe_entry *entry)
  1761. {
  1762. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  1763. }
  1764. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  1765. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  1766. {
  1767. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  1768. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  1769. return 0;
  1770. }
  1771. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  1772. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  1773. {
  1774. unsigned int value_count, value_index;
  1775. struct devlink_dpipe_value *value;
  1776. value = entry->action_values;
  1777. value_count = entry->action_values_count;
  1778. for (value_index = 0; value_index < value_count; value_index++) {
  1779. kfree(value[value_index].value);
  1780. kfree(value[value_index].mask);
  1781. }
  1782. value = entry->match_values;
  1783. value_count = entry->match_values_count;
  1784. for (value_index = 0; value_index < value_count; value_index++) {
  1785. kfree(value[value_index].value);
  1786. kfree(value[value_index].mask);
  1787. }
  1788. }
  1789. EXPORT_SYMBOL(devlink_dpipe_entry_clear);
  1790. static int devlink_dpipe_entries_fill(struct genl_info *info,
  1791. enum devlink_command cmd, int flags,
  1792. struct devlink_dpipe_table *table)
  1793. {
  1794. struct devlink_dpipe_dump_ctx dump_ctx;
  1795. struct nlmsghdr *nlh;
  1796. int err;
  1797. dump_ctx.skb = NULL;
  1798. dump_ctx.cmd = cmd;
  1799. dump_ctx.info = info;
  1800. err = table->table_ops->entries_dump(table->priv,
  1801. table->counters_enabled,
  1802. &dump_ctx);
  1803. if (err)
  1804. return err;
  1805. send_done:
  1806. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  1807. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1808. if (!nlh) {
  1809. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  1810. if (err)
  1811. return err;
  1812. goto send_done;
  1813. }
  1814. return genlmsg_reply(dump_ctx.skb, info);
  1815. }
  1816. static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
  1817. struct genl_info *info)
  1818. {
  1819. struct devlink *devlink = info->user_ptr[0];
  1820. struct devlink_dpipe_table *table;
  1821. const char *table_name;
  1822. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1823. return -EINVAL;
  1824. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1825. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1826. table_name);
  1827. if (!table)
  1828. return -EINVAL;
  1829. if (!table->table_ops->entries_dump)
  1830. return -EINVAL;
  1831. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  1832. 0, table);
  1833. }
  1834. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  1835. const struct devlink_dpipe_header *header)
  1836. {
  1837. struct devlink_dpipe_field *field;
  1838. struct nlattr *field_attr;
  1839. int i;
  1840. for (i = 0; i < header->fields_count; i++) {
  1841. field = &header->fields[i];
  1842. field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
  1843. if (!field_attr)
  1844. return -EMSGSIZE;
  1845. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  1846. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1847. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  1848. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  1849. goto nla_put_failure;
  1850. nla_nest_end(skb, field_attr);
  1851. }
  1852. return 0;
  1853. nla_put_failure:
  1854. nla_nest_cancel(skb, field_attr);
  1855. return -EMSGSIZE;
  1856. }
  1857. static int devlink_dpipe_header_put(struct sk_buff *skb,
  1858. struct devlink_dpipe_header *header)
  1859. {
  1860. struct nlattr *fields_attr, *header_attr;
  1861. int err;
  1862. header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
  1863. if (!header_attr)
  1864. return -EMSGSIZE;
  1865. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  1866. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1867. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1868. goto nla_put_failure;
  1869. fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  1870. if (!fields_attr)
  1871. goto nla_put_failure;
  1872. err = devlink_dpipe_fields_put(skb, header);
  1873. if (err) {
  1874. nla_nest_cancel(skb, fields_attr);
  1875. goto nla_put_failure;
  1876. }
  1877. nla_nest_end(skb, fields_attr);
  1878. nla_nest_end(skb, header_attr);
  1879. return 0;
  1880. nla_put_failure:
  1881. err = -EMSGSIZE;
  1882. nla_nest_cancel(skb, header_attr);
  1883. return err;
  1884. }
  1885. static int devlink_dpipe_headers_fill(struct genl_info *info,
  1886. enum devlink_command cmd, int flags,
  1887. struct devlink_dpipe_headers *
  1888. dpipe_headers)
  1889. {
  1890. struct devlink *devlink = info->user_ptr[0];
  1891. struct nlattr *headers_attr;
  1892. struct sk_buff *skb = NULL;
  1893. struct nlmsghdr *nlh;
  1894. void *hdr;
  1895. int i, j;
  1896. int err;
  1897. i = 0;
  1898. start_again:
  1899. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1900. if (err)
  1901. return err;
  1902. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1903. &devlink_nl_family, NLM_F_MULTI, cmd);
  1904. if (!hdr) {
  1905. nlmsg_free(skb);
  1906. return -EMSGSIZE;
  1907. }
  1908. if (devlink_nl_put_handle(skb, devlink))
  1909. goto nla_put_failure;
  1910. headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  1911. if (!headers_attr)
  1912. goto nla_put_failure;
  1913. j = 0;
  1914. for (; i < dpipe_headers->headers_count; i++) {
  1915. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  1916. if (err) {
  1917. if (!j)
  1918. goto err_table_put;
  1919. break;
  1920. }
  1921. j++;
  1922. }
  1923. nla_nest_end(skb, headers_attr);
  1924. genlmsg_end(skb, hdr);
  1925. if (i != dpipe_headers->headers_count)
  1926. goto start_again;
  1927. send_done:
  1928. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1929. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1930. if (!nlh) {
  1931. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1932. if (err)
  1933. return err;
  1934. goto send_done;
  1935. }
  1936. return genlmsg_reply(skb, info);
  1937. nla_put_failure:
  1938. err = -EMSGSIZE;
  1939. err_table_put:
  1940. nlmsg_free(skb);
  1941. return err;
  1942. }
  1943. static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
  1944. struct genl_info *info)
  1945. {
  1946. struct devlink *devlink = info->user_ptr[0];
  1947. if (!devlink->dpipe_headers)
  1948. return -EOPNOTSUPP;
  1949. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  1950. 0, devlink->dpipe_headers);
  1951. }
  1952. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  1953. const char *table_name,
  1954. bool enable)
  1955. {
  1956. struct devlink_dpipe_table *table;
  1957. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1958. table_name);
  1959. if (!table)
  1960. return -EINVAL;
  1961. if (table->counter_control_extern)
  1962. return -EOPNOTSUPP;
  1963. if (!(table->counters_enabled ^ enable))
  1964. return 0;
  1965. table->counters_enabled = enable;
  1966. if (table->table_ops->counters_set_update)
  1967. table->table_ops->counters_set_update(table->priv, enable);
  1968. return 0;
  1969. }
  1970. static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
  1971. struct genl_info *info)
  1972. {
  1973. struct devlink *devlink = info->user_ptr[0];
  1974. const char *table_name;
  1975. bool counters_enable;
  1976. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
  1977. !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
  1978. return -EINVAL;
  1979. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1980. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  1981. return devlink_dpipe_table_counters_set(devlink, table_name,
  1982. counters_enable);
  1983. }
  1984. static struct devlink_resource *
  1985. devlink_resource_find(struct devlink *devlink,
  1986. struct devlink_resource *resource, u64 resource_id)
  1987. {
  1988. struct list_head *resource_list;
  1989. if (resource)
  1990. resource_list = &resource->resource_list;
  1991. else
  1992. resource_list = &devlink->resource_list;
  1993. list_for_each_entry(resource, resource_list, list) {
  1994. struct devlink_resource *child_resource;
  1995. if (resource->id == resource_id)
  1996. return resource;
  1997. child_resource = devlink_resource_find(devlink, resource,
  1998. resource_id);
  1999. if (child_resource)
  2000. return child_resource;
  2001. }
  2002. return NULL;
  2003. }
  2004. static void
  2005. devlink_resource_validate_children(struct devlink_resource *resource)
  2006. {
  2007. struct devlink_resource *child_resource;
  2008. bool size_valid = true;
  2009. u64 parts_size = 0;
  2010. if (list_empty(&resource->resource_list))
  2011. goto out;
  2012. list_for_each_entry(child_resource, &resource->resource_list, list)
  2013. parts_size += child_resource->size_new;
  2014. if (parts_size > resource->size_new)
  2015. size_valid = false;
  2016. out:
  2017. resource->size_valid = size_valid;
  2018. }
  2019. static int
  2020. devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
  2021. struct netlink_ext_ack *extack)
  2022. {
  2023. u64 reminder;
  2024. int err = 0;
  2025. if (size > resource->size_params.size_max) {
  2026. NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
  2027. err = -EINVAL;
  2028. }
  2029. if (size < resource->size_params.size_min) {
  2030. NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
  2031. err = -EINVAL;
  2032. }
  2033. div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
  2034. if (reminder) {
  2035. NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
  2036. err = -EINVAL;
  2037. }
  2038. return err;
  2039. }
  2040. static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
  2041. struct genl_info *info)
  2042. {
  2043. struct devlink *devlink = info->user_ptr[0];
  2044. struct devlink_resource *resource;
  2045. u64 resource_id;
  2046. u64 size;
  2047. int err;
  2048. if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
  2049. !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
  2050. return -EINVAL;
  2051. resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
  2052. resource = devlink_resource_find(devlink, NULL, resource_id);
  2053. if (!resource)
  2054. return -EINVAL;
  2055. size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
  2056. err = devlink_resource_validate_size(resource, size, info->extack);
  2057. if (err)
  2058. return err;
  2059. resource->size_new = size;
  2060. devlink_resource_validate_children(resource);
  2061. if (resource->parent)
  2062. devlink_resource_validate_children(resource->parent);
  2063. return 0;
  2064. }
  2065. static int
  2066. devlink_resource_size_params_put(struct devlink_resource *resource,
  2067. struct sk_buff *skb)
  2068. {
  2069. struct devlink_resource_size_params *size_params;
  2070. size_params = &resource->size_params;
  2071. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
  2072. size_params->size_granularity, DEVLINK_ATTR_PAD) ||
  2073. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
  2074. size_params->size_max, DEVLINK_ATTR_PAD) ||
  2075. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
  2076. size_params->size_min, DEVLINK_ATTR_PAD) ||
  2077. nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
  2078. return -EMSGSIZE;
  2079. return 0;
  2080. }
  2081. static int devlink_resource_occ_put(struct devlink_resource *resource,
  2082. struct sk_buff *skb)
  2083. {
  2084. if (!resource->occ_get)
  2085. return 0;
  2086. return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
  2087. resource->occ_get(resource->occ_get_priv),
  2088. DEVLINK_ATTR_PAD);
  2089. }
  2090. static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
  2091. struct devlink_resource *resource)
  2092. {
  2093. struct devlink_resource *child_resource;
  2094. struct nlattr *child_resource_attr;
  2095. struct nlattr *resource_attr;
  2096. resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
  2097. if (!resource_attr)
  2098. return -EMSGSIZE;
  2099. if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
  2100. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
  2101. DEVLINK_ATTR_PAD) ||
  2102. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
  2103. DEVLINK_ATTR_PAD))
  2104. goto nla_put_failure;
  2105. if (resource->size != resource->size_new)
  2106. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
  2107. resource->size_new, DEVLINK_ATTR_PAD);
  2108. if (devlink_resource_occ_put(resource, skb))
  2109. goto nla_put_failure;
  2110. if (devlink_resource_size_params_put(resource, skb))
  2111. goto nla_put_failure;
  2112. if (list_empty(&resource->resource_list))
  2113. goto out;
  2114. if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
  2115. resource->size_valid))
  2116. goto nla_put_failure;
  2117. child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2118. if (!child_resource_attr)
  2119. goto nla_put_failure;
  2120. list_for_each_entry(child_resource, &resource->resource_list, list) {
  2121. if (devlink_resource_put(devlink, skb, child_resource))
  2122. goto resource_put_failure;
  2123. }
  2124. nla_nest_end(skb, child_resource_attr);
  2125. out:
  2126. nla_nest_end(skb, resource_attr);
  2127. return 0;
  2128. resource_put_failure:
  2129. nla_nest_cancel(skb, child_resource_attr);
  2130. nla_put_failure:
  2131. nla_nest_cancel(skb, resource_attr);
  2132. return -EMSGSIZE;
  2133. }
  2134. static int devlink_resource_fill(struct genl_info *info,
  2135. enum devlink_command cmd, int flags)
  2136. {
  2137. struct devlink *devlink = info->user_ptr[0];
  2138. struct devlink_resource *resource;
  2139. struct nlattr *resources_attr;
  2140. struct sk_buff *skb = NULL;
  2141. struct nlmsghdr *nlh;
  2142. bool incomplete;
  2143. void *hdr;
  2144. int i;
  2145. int err;
  2146. resource = list_first_entry(&devlink->resource_list,
  2147. struct devlink_resource, list);
  2148. start_again:
  2149. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2150. if (err)
  2151. return err;
  2152. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  2153. &devlink_nl_family, NLM_F_MULTI, cmd);
  2154. if (!hdr) {
  2155. nlmsg_free(skb);
  2156. return -EMSGSIZE;
  2157. }
  2158. if (devlink_nl_put_handle(skb, devlink))
  2159. goto nla_put_failure;
  2160. resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2161. if (!resources_attr)
  2162. goto nla_put_failure;
  2163. incomplete = false;
  2164. i = 0;
  2165. list_for_each_entry_from(resource, &devlink->resource_list, list) {
  2166. err = devlink_resource_put(devlink, skb, resource);
  2167. if (err) {
  2168. if (!i)
  2169. goto err_resource_put;
  2170. incomplete = true;
  2171. break;
  2172. }
  2173. i++;
  2174. }
  2175. nla_nest_end(skb, resources_attr);
  2176. genlmsg_end(skb, hdr);
  2177. if (incomplete)
  2178. goto start_again;
  2179. send_done:
  2180. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  2181. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2182. if (!nlh) {
  2183. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2184. if (err)
  2185. goto err_skb_send_alloc;
  2186. goto send_done;
  2187. }
  2188. return genlmsg_reply(skb, info);
  2189. nla_put_failure:
  2190. err = -EMSGSIZE;
  2191. err_resource_put:
  2192. err_skb_send_alloc:
  2193. nlmsg_free(skb);
  2194. return err;
  2195. }
  2196. static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
  2197. struct genl_info *info)
  2198. {
  2199. struct devlink *devlink = info->user_ptr[0];
  2200. if (list_empty(&devlink->resource_list))
  2201. return -EOPNOTSUPP;
  2202. return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
  2203. }
  2204. static int
  2205. devlink_resources_validate(struct devlink *devlink,
  2206. struct devlink_resource *resource,
  2207. struct genl_info *info)
  2208. {
  2209. struct list_head *resource_list;
  2210. int err = 0;
  2211. if (resource)
  2212. resource_list = &resource->resource_list;
  2213. else
  2214. resource_list = &devlink->resource_list;
  2215. list_for_each_entry(resource, resource_list, list) {
  2216. if (!resource->size_valid)
  2217. return -EINVAL;
  2218. err = devlink_resources_validate(devlink, resource, info);
  2219. if (err)
  2220. return err;
  2221. }
  2222. return err;
  2223. }
  2224. static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
  2225. {
  2226. struct devlink *devlink = info->user_ptr[0];
  2227. int err;
  2228. if (!devlink->ops->reload)
  2229. return -EOPNOTSUPP;
  2230. err = devlink_resources_validate(devlink, NULL, info);
  2231. if (err) {
  2232. NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
  2233. return err;
  2234. }
  2235. return devlink->ops->reload(devlink, info->extack);
  2236. }
  2237. static const struct devlink_param devlink_param_generic[] = {
  2238. {
  2239. .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
  2240. .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
  2241. .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
  2242. },
  2243. {
  2244. .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
  2245. .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
  2246. .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
  2247. },
  2248. {
  2249. .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
  2250. .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
  2251. .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
  2252. },
  2253. };
  2254. static int devlink_param_generic_verify(const struct devlink_param *param)
  2255. {
  2256. /* verify it match generic parameter by id and name */
  2257. if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
  2258. return -EINVAL;
  2259. if (strcmp(param->name, devlink_param_generic[param->id].name))
  2260. return -ENOENT;
  2261. WARN_ON(param->type != devlink_param_generic[param->id].type);
  2262. return 0;
  2263. }
  2264. static int devlink_param_driver_verify(const struct devlink_param *param)
  2265. {
  2266. int i;
  2267. if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
  2268. return -EINVAL;
  2269. /* verify no such name in generic params */
  2270. for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
  2271. if (!strcmp(param->name, devlink_param_generic[i].name))
  2272. return -EEXIST;
  2273. return 0;
  2274. }
  2275. static struct devlink_param_item *
  2276. devlink_param_find_by_name(struct list_head *param_list,
  2277. const char *param_name)
  2278. {
  2279. struct devlink_param_item *param_item;
  2280. list_for_each_entry(param_item, param_list, list)
  2281. if (!strcmp(param_item->param->name, param_name))
  2282. return param_item;
  2283. return NULL;
  2284. }
  2285. static struct devlink_param_item *
  2286. devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
  2287. {
  2288. struct devlink_param_item *param_item;
  2289. list_for_each_entry(param_item, param_list, list)
  2290. if (param_item->param->id == param_id)
  2291. return param_item;
  2292. return NULL;
  2293. }
  2294. static bool
  2295. devlink_param_cmode_is_supported(const struct devlink_param *param,
  2296. enum devlink_param_cmode cmode)
  2297. {
  2298. return test_bit(cmode, &param->supported_cmodes);
  2299. }
  2300. static int devlink_param_get(struct devlink *devlink,
  2301. const struct devlink_param *param,
  2302. struct devlink_param_gset_ctx *ctx)
  2303. {
  2304. if (!param->get)
  2305. return -EOPNOTSUPP;
  2306. return param->get(devlink, param->id, ctx);
  2307. }
  2308. static int devlink_param_set(struct devlink *devlink,
  2309. const struct devlink_param *param,
  2310. struct devlink_param_gset_ctx *ctx)
  2311. {
  2312. if (!param->set)
  2313. return -EOPNOTSUPP;
  2314. return param->set(devlink, param->id, ctx);
  2315. }
  2316. static int
  2317. devlink_param_type_to_nla_type(enum devlink_param_type param_type)
  2318. {
  2319. switch (param_type) {
  2320. case DEVLINK_PARAM_TYPE_U8:
  2321. return NLA_U8;
  2322. case DEVLINK_PARAM_TYPE_U16:
  2323. return NLA_U16;
  2324. case DEVLINK_PARAM_TYPE_U32:
  2325. return NLA_U32;
  2326. case DEVLINK_PARAM_TYPE_STRING:
  2327. return NLA_STRING;
  2328. case DEVLINK_PARAM_TYPE_BOOL:
  2329. return NLA_FLAG;
  2330. default:
  2331. return -EINVAL;
  2332. }
  2333. }
  2334. static int
  2335. devlink_nl_param_value_fill_one(struct sk_buff *msg,
  2336. enum devlink_param_type type,
  2337. enum devlink_param_cmode cmode,
  2338. union devlink_param_value val)
  2339. {
  2340. struct nlattr *param_value_attr;
  2341. param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
  2342. if (!param_value_attr)
  2343. goto nla_put_failure;
  2344. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
  2345. goto value_nest_cancel;
  2346. switch (type) {
  2347. case DEVLINK_PARAM_TYPE_U8:
  2348. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
  2349. goto value_nest_cancel;
  2350. break;
  2351. case DEVLINK_PARAM_TYPE_U16:
  2352. if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
  2353. goto value_nest_cancel;
  2354. break;
  2355. case DEVLINK_PARAM_TYPE_U32:
  2356. if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
  2357. goto value_nest_cancel;
  2358. break;
  2359. case DEVLINK_PARAM_TYPE_STRING:
  2360. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
  2361. val.vstr))
  2362. goto value_nest_cancel;
  2363. break;
  2364. case DEVLINK_PARAM_TYPE_BOOL:
  2365. if (val.vbool &&
  2366. nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
  2367. goto value_nest_cancel;
  2368. break;
  2369. }
  2370. nla_nest_end(msg, param_value_attr);
  2371. return 0;
  2372. value_nest_cancel:
  2373. nla_nest_cancel(msg, param_value_attr);
  2374. nla_put_failure:
  2375. return -EMSGSIZE;
  2376. }
  2377. static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
  2378. struct devlink_param_item *param_item,
  2379. enum devlink_command cmd,
  2380. u32 portid, u32 seq, int flags)
  2381. {
  2382. union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
  2383. const struct devlink_param *param = param_item->param;
  2384. struct devlink_param_gset_ctx ctx;
  2385. struct nlattr *param_values_list;
  2386. struct nlattr *param_attr;
  2387. int nla_type;
  2388. void *hdr;
  2389. int err;
  2390. int i;
  2391. /* Get value from driver part to driverinit configuration mode */
  2392. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2393. if (!devlink_param_cmode_is_supported(param, i))
  2394. continue;
  2395. if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2396. if (!param_item->driverinit_value_valid)
  2397. return -EOPNOTSUPP;
  2398. param_value[i] = param_item->driverinit_value;
  2399. } else {
  2400. ctx.cmode = i;
  2401. err = devlink_param_get(devlink, param, &ctx);
  2402. if (err)
  2403. return err;
  2404. param_value[i] = ctx.val;
  2405. }
  2406. }
  2407. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  2408. if (!hdr)
  2409. return -EMSGSIZE;
  2410. if (devlink_nl_put_handle(msg, devlink))
  2411. goto genlmsg_cancel;
  2412. param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
  2413. if (!param_attr)
  2414. goto genlmsg_cancel;
  2415. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
  2416. goto param_nest_cancel;
  2417. if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
  2418. goto param_nest_cancel;
  2419. nla_type = devlink_param_type_to_nla_type(param->type);
  2420. if (nla_type < 0)
  2421. goto param_nest_cancel;
  2422. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
  2423. goto param_nest_cancel;
  2424. param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
  2425. if (!param_values_list)
  2426. goto param_nest_cancel;
  2427. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2428. if (!devlink_param_cmode_is_supported(param, i))
  2429. continue;
  2430. err = devlink_nl_param_value_fill_one(msg, param->type,
  2431. i, param_value[i]);
  2432. if (err)
  2433. goto values_list_nest_cancel;
  2434. }
  2435. nla_nest_end(msg, param_values_list);
  2436. nla_nest_end(msg, param_attr);
  2437. genlmsg_end(msg, hdr);
  2438. return 0;
  2439. values_list_nest_cancel:
  2440. nla_nest_end(msg, param_values_list);
  2441. param_nest_cancel:
  2442. nla_nest_cancel(msg, param_attr);
  2443. genlmsg_cancel:
  2444. genlmsg_cancel(msg, hdr);
  2445. return -EMSGSIZE;
  2446. }
  2447. static void devlink_param_notify(struct devlink *devlink,
  2448. struct devlink_param_item *param_item,
  2449. enum devlink_command cmd)
  2450. {
  2451. struct sk_buff *msg;
  2452. int err;
  2453. WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL);
  2454. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2455. if (!msg)
  2456. return;
  2457. err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0);
  2458. if (err) {
  2459. nlmsg_free(msg);
  2460. return;
  2461. }
  2462. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  2463. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  2464. }
  2465. static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
  2466. struct netlink_callback *cb)
  2467. {
  2468. struct devlink_param_item *param_item;
  2469. struct devlink *devlink;
  2470. int start = cb->args[0];
  2471. int idx = 0;
  2472. int err;
  2473. mutex_lock(&devlink_mutex);
  2474. list_for_each_entry(devlink, &devlink_list, list) {
  2475. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  2476. continue;
  2477. mutex_lock(&devlink->lock);
  2478. list_for_each_entry(param_item, &devlink->param_list, list) {
  2479. if (idx < start) {
  2480. idx++;
  2481. continue;
  2482. }
  2483. err = devlink_nl_param_fill(msg, devlink, param_item,
  2484. DEVLINK_CMD_PARAM_GET,
  2485. NETLINK_CB(cb->skb).portid,
  2486. cb->nlh->nlmsg_seq,
  2487. NLM_F_MULTI);
  2488. if (err) {
  2489. mutex_unlock(&devlink->lock);
  2490. goto out;
  2491. }
  2492. idx++;
  2493. }
  2494. mutex_unlock(&devlink->lock);
  2495. }
  2496. out:
  2497. mutex_unlock(&devlink_mutex);
  2498. cb->args[0] = idx;
  2499. return msg->len;
  2500. }
  2501. static int
  2502. devlink_param_type_get_from_info(struct genl_info *info,
  2503. enum devlink_param_type *param_type)
  2504. {
  2505. if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
  2506. return -EINVAL;
  2507. switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
  2508. case NLA_U8:
  2509. *param_type = DEVLINK_PARAM_TYPE_U8;
  2510. break;
  2511. case NLA_U16:
  2512. *param_type = DEVLINK_PARAM_TYPE_U16;
  2513. break;
  2514. case NLA_U32:
  2515. *param_type = DEVLINK_PARAM_TYPE_U32;
  2516. break;
  2517. case NLA_STRING:
  2518. *param_type = DEVLINK_PARAM_TYPE_STRING;
  2519. break;
  2520. case NLA_FLAG:
  2521. *param_type = DEVLINK_PARAM_TYPE_BOOL;
  2522. break;
  2523. default:
  2524. return -EINVAL;
  2525. }
  2526. return 0;
  2527. }
  2528. static int
  2529. devlink_param_value_get_from_info(const struct devlink_param *param,
  2530. struct genl_info *info,
  2531. union devlink_param_value *value)
  2532. {
  2533. if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
  2534. !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
  2535. return -EINVAL;
  2536. switch (param->type) {
  2537. case DEVLINK_PARAM_TYPE_U8:
  2538. value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2539. break;
  2540. case DEVLINK_PARAM_TYPE_U16:
  2541. value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2542. break;
  2543. case DEVLINK_PARAM_TYPE_U32:
  2544. value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2545. break;
  2546. case DEVLINK_PARAM_TYPE_STRING:
  2547. if (nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) >
  2548. DEVLINK_PARAM_MAX_STRING_VALUE)
  2549. return -EINVAL;
  2550. value->vstr = nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
  2551. break;
  2552. case DEVLINK_PARAM_TYPE_BOOL:
  2553. value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
  2554. true : false;
  2555. break;
  2556. }
  2557. return 0;
  2558. }
  2559. static struct devlink_param_item *
  2560. devlink_param_get_from_info(struct devlink *devlink,
  2561. struct genl_info *info)
  2562. {
  2563. char *param_name;
  2564. if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
  2565. return NULL;
  2566. param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
  2567. return devlink_param_find_by_name(&devlink->param_list, param_name);
  2568. }
  2569. static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
  2570. struct genl_info *info)
  2571. {
  2572. struct devlink *devlink = info->user_ptr[0];
  2573. struct devlink_param_item *param_item;
  2574. struct sk_buff *msg;
  2575. int err;
  2576. param_item = devlink_param_get_from_info(devlink, info);
  2577. if (!param_item)
  2578. return -EINVAL;
  2579. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2580. if (!msg)
  2581. return -ENOMEM;
  2582. err = devlink_nl_param_fill(msg, devlink, param_item,
  2583. DEVLINK_CMD_PARAM_GET,
  2584. info->snd_portid, info->snd_seq, 0);
  2585. if (err) {
  2586. nlmsg_free(msg);
  2587. return err;
  2588. }
  2589. return genlmsg_reply(msg, info);
  2590. }
  2591. static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
  2592. struct genl_info *info)
  2593. {
  2594. struct devlink *devlink = info->user_ptr[0];
  2595. enum devlink_param_type param_type;
  2596. struct devlink_param_gset_ctx ctx;
  2597. enum devlink_param_cmode cmode;
  2598. struct devlink_param_item *param_item;
  2599. const struct devlink_param *param;
  2600. union devlink_param_value value;
  2601. int err = 0;
  2602. param_item = devlink_param_get_from_info(devlink, info);
  2603. if (!param_item)
  2604. return -EINVAL;
  2605. param = param_item->param;
  2606. err = devlink_param_type_get_from_info(info, &param_type);
  2607. if (err)
  2608. return err;
  2609. if (param_type != param->type)
  2610. return -EINVAL;
  2611. err = devlink_param_value_get_from_info(param, info, &value);
  2612. if (err)
  2613. return err;
  2614. if (param->validate) {
  2615. err = param->validate(devlink, param->id, value, info->extack);
  2616. if (err)
  2617. return err;
  2618. }
  2619. if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
  2620. return -EINVAL;
  2621. cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
  2622. if (!devlink_param_cmode_is_supported(param, cmode))
  2623. return -EOPNOTSUPP;
  2624. if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2625. param_item->driverinit_value = value;
  2626. param_item->driverinit_value_valid = true;
  2627. } else {
  2628. if (!param->set)
  2629. return -EOPNOTSUPP;
  2630. ctx.val = value;
  2631. ctx.cmode = cmode;
  2632. err = devlink_param_set(devlink, param, &ctx);
  2633. if (err)
  2634. return err;
  2635. }
  2636. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  2637. return 0;
  2638. }
  2639. static int devlink_param_register_one(struct devlink *devlink,
  2640. const struct devlink_param *param)
  2641. {
  2642. struct devlink_param_item *param_item;
  2643. if (devlink_param_find_by_name(&devlink->param_list,
  2644. param->name))
  2645. return -EEXIST;
  2646. if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
  2647. WARN_ON(param->get || param->set);
  2648. else
  2649. WARN_ON(!param->get || !param->set);
  2650. param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
  2651. if (!param_item)
  2652. return -ENOMEM;
  2653. param_item->param = param;
  2654. list_add_tail(&param_item->list, &devlink->param_list);
  2655. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  2656. return 0;
  2657. }
  2658. static void devlink_param_unregister_one(struct devlink *devlink,
  2659. const struct devlink_param *param)
  2660. {
  2661. struct devlink_param_item *param_item;
  2662. param_item = devlink_param_find_by_name(&devlink->param_list,
  2663. param->name);
  2664. WARN_ON(!param_item);
  2665. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL);
  2666. list_del(&param_item->list);
  2667. kfree(param_item);
  2668. }
  2669. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  2670. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  2671. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  2672. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  2673. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  2674. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  2675. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  2676. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  2677. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  2678. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  2679. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  2680. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  2681. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  2682. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  2683. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  2684. [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
  2685. [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
  2686. [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
  2687. [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
  2688. [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
  2689. [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
  2690. [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
  2691. [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
  2692. };
  2693. static const struct genl_ops devlink_nl_ops[] = {
  2694. {
  2695. .cmd = DEVLINK_CMD_GET,
  2696. .doit = devlink_nl_cmd_get_doit,
  2697. .dumpit = devlink_nl_cmd_get_dumpit,
  2698. .policy = devlink_nl_policy,
  2699. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2700. /* can be retrieved by unprivileged users */
  2701. },
  2702. {
  2703. .cmd = DEVLINK_CMD_PORT_GET,
  2704. .doit = devlink_nl_cmd_port_get_doit,
  2705. .dumpit = devlink_nl_cmd_port_get_dumpit,
  2706. .policy = devlink_nl_policy,
  2707. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2708. /* can be retrieved by unprivileged users */
  2709. },
  2710. {
  2711. .cmd = DEVLINK_CMD_PORT_SET,
  2712. .doit = devlink_nl_cmd_port_set_doit,
  2713. .policy = devlink_nl_policy,
  2714. .flags = GENL_ADMIN_PERM,
  2715. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  2716. },
  2717. {
  2718. .cmd = DEVLINK_CMD_PORT_SPLIT,
  2719. .doit = devlink_nl_cmd_port_split_doit,
  2720. .policy = devlink_nl_policy,
  2721. .flags = GENL_ADMIN_PERM,
  2722. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2723. DEVLINK_NL_FLAG_NO_LOCK,
  2724. },
  2725. {
  2726. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  2727. .doit = devlink_nl_cmd_port_unsplit_doit,
  2728. .policy = devlink_nl_policy,
  2729. .flags = GENL_ADMIN_PERM,
  2730. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2731. DEVLINK_NL_FLAG_NO_LOCK,
  2732. },
  2733. {
  2734. .cmd = DEVLINK_CMD_SB_GET,
  2735. .doit = devlink_nl_cmd_sb_get_doit,
  2736. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  2737. .policy = devlink_nl_policy,
  2738. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2739. DEVLINK_NL_FLAG_NEED_SB,
  2740. /* can be retrieved by unprivileged users */
  2741. },
  2742. {
  2743. .cmd = DEVLINK_CMD_SB_POOL_GET,
  2744. .doit = devlink_nl_cmd_sb_pool_get_doit,
  2745. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  2746. .policy = devlink_nl_policy,
  2747. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2748. DEVLINK_NL_FLAG_NEED_SB,
  2749. /* can be retrieved by unprivileged users */
  2750. },
  2751. {
  2752. .cmd = DEVLINK_CMD_SB_POOL_SET,
  2753. .doit = devlink_nl_cmd_sb_pool_set_doit,
  2754. .policy = devlink_nl_policy,
  2755. .flags = GENL_ADMIN_PERM,
  2756. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2757. DEVLINK_NL_FLAG_NEED_SB,
  2758. },
  2759. {
  2760. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  2761. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  2762. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  2763. .policy = devlink_nl_policy,
  2764. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2765. DEVLINK_NL_FLAG_NEED_SB,
  2766. /* can be retrieved by unprivileged users */
  2767. },
  2768. {
  2769. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  2770. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  2771. .policy = devlink_nl_policy,
  2772. .flags = GENL_ADMIN_PERM,
  2773. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2774. DEVLINK_NL_FLAG_NEED_SB,
  2775. },
  2776. {
  2777. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  2778. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  2779. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  2780. .policy = devlink_nl_policy,
  2781. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2782. DEVLINK_NL_FLAG_NEED_SB,
  2783. /* can be retrieved by unprivileged users */
  2784. },
  2785. {
  2786. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  2787. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  2788. .policy = devlink_nl_policy,
  2789. .flags = GENL_ADMIN_PERM,
  2790. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  2791. DEVLINK_NL_FLAG_NEED_SB,
  2792. },
  2793. {
  2794. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  2795. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  2796. .policy = devlink_nl_policy,
  2797. .flags = GENL_ADMIN_PERM,
  2798. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2799. DEVLINK_NL_FLAG_NEED_SB,
  2800. },
  2801. {
  2802. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  2803. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  2804. .policy = devlink_nl_policy,
  2805. .flags = GENL_ADMIN_PERM,
  2806. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2807. DEVLINK_NL_FLAG_NEED_SB,
  2808. },
  2809. {
  2810. .cmd = DEVLINK_CMD_ESWITCH_GET,
  2811. .doit = devlink_nl_cmd_eswitch_get_doit,
  2812. .policy = devlink_nl_policy,
  2813. .flags = GENL_ADMIN_PERM,
  2814. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2815. },
  2816. {
  2817. .cmd = DEVLINK_CMD_ESWITCH_SET,
  2818. .doit = devlink_nl_cmd_eswitch_set_doit,
  2819. .policy = devlink_nl_policy,
  2820. .flags = GENL_ADMIN_PERM,
  2821. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2822. DEVLINK_NL_FLAG_NO_LOCK,
  2823. },
  2824. {
  2825. .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
  2826. .doit = devlink_nl_cmd_dpipe_table_get,
  2827. .policy = devlink_nl_policy,
  2828. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2829. /* can be retrieved by unprivileged users */
  2830. },
  2831. {
  2832. .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
  2833. .doit = devlink_nl_cmd_dpipe_entries_get,
  2834. .policy = devlink_nl_policy,
  2835. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2836. /* can be retrieved by unprivileged users */
  2837. },
  2838. {
  2839. .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
  2840. .doit = devlink_nl_cmd_dpipe_headers_get,
  2841. .policy = devlink_nl_policy,
  2842. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2843. /* can be retrieved by unprivileged users */
  2844. },
  2845. {
  2846. .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
  2847. .doit = devlink_nl_cmd_dpipe_table_counters_set,
  2848. .policy = devlink_nl_policy,
  2849. .flags = GENL_ADMIN_PERM,
  2850. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2851. },
  2852. {
  2853. .cmd = DEVLINK_CMD_RESOURCE_SET,
  2854. .doit = devlink_nl_cmd_resource_set,
  2855. .policy = devlink_nl_policy,
  2856. .flags = GENL_ADMIN_PERM,
  2857. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2858. },
  2859. {
  2860. .cmd = DEVLINK_CMD_RESOURCE_DUMP,
  2861. .doit = devlink_nl_cmd_resource_dump,
  2862. .policy = devlink_nl_policy,
  2863. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2864. /* can be retrieved by unprivileged users */
  2865. },
  2866. {
  2867. .cmd = DEVLINK_CMD_RELOAD,
  2868. .doit = devlink_nl_cmd_reload,
  2869. .policy = devlink_nl_policy,
  2870. .flags = GENL_ADMIN_PERM,
  2871. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  2872. DEVLINK_NL_FLAG_NO_LOCK,
  2873. },
  2874. {
  2875. .cmd = DEVLINK_CMD_PARAM_GET,
  2876. .doit = devlink_nl_cmd_param_get_doit,
  2877. .dumpit = devlink_nl_cmd_param_get_dumpit,
  2878. .policy = devlink_nl_policy,
  2879. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2880. /* can be retrieved by unprivileged users */
  2881. },
  2882. {
  2883. .cmd = DEVLINK_CMD_PARAM_SET,
  2884. .doit = devlink_nl_cmd_param_set_doit,
  2885. .policy = devlink_nl_policy,
  2886. .flags = GENL_ADMIN_PERM,
  2887. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  2888. },
  2889. };
  2890. static struct genl_family devlink_nl_family __ro_after_init = {
  2891. .name = DEVLINK_GENL_NAME,
  2892. .version = DEVLINK_GENL_VERSION,
  2893. .maxattr = DEVLINK_ATTR_MAX,
  2894. .netnsok = true,
  2895. .pre_doit = devlink_nl_pre_doit,
  2896. .post_doit = devlink_nl_post_doit,
  2897. .module = THIS_MODULE,
  2898. .ops = devlink_nl_ops,
  2899. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  2900. .mcgrps = devlink_nl_mcgrps,
  2901. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  2902. };
  2903. /**
  2904. * devlink_alloc - Allocate new devlink instance resources
  2905. *
  2906. * @ops: ops
  2907. * @priv_size: size of user private data
  2908. *
  2909. * Allocate new devlink instance resources, including devlink index
  2910. * and name.
  2911. */
  2912. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  2913. {
  2914. struct devlink *devlink;
  2915. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  2916. if (!devlink)
  2917. return NULL;
  2918. devlink->ops = ops;
  2919. devlink_net_set(devlink, &init_net);
  2920. INIT_LIST_HEAD(&devlink->port_list);
  2921. INIT_LIST_HEAD(&devlink->sb_list);
  2922. INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
  2923. INIT_LIST_HEAD(&devlink->resource_list);
  2924. INIT_LIST_HEAD(&devlink->param_list);
  2925. INIT_LIST_HEAD(&devlink->region_list);
  2926. mutex_init(&devlink->lock);
  2927. return devlink;
  2928. }
  2929. EXPORT_SYMBOL_GPL(devlink_alloc);
  2930. /**
  2931. * devlink_register - Register devlink instance
  2932. *
  2933. * @devlink: devlink
  2934. */
  2935. int devlink_register(struct devlink *devlink, struct device *dev)
  2936. {
  2937. mutex_lock(&devlink_mutex);
  2938. devlink->dev = dev;
  2939. list_add_tail(&devlink->list, &devlink_list);
  2940. devlink_notify(devlink, DEVLINK_CMD_NEW);
  2941. mutex_unlock(&devlink_mutex);
  2942. return 0;
  2943. }
  2944. EXPORT_SYMBOL_GPL(devlink_register);
  2945. /**
  2946. * devlink_unregister - Unregister devlink instance
  2947. *
  2948. * @devlink: devlink
  2949. */
  2950. void devlink_unregister(struct devlink *devlink)
  2951. {
  2952. mutex_lock(&devlink_mutex);
  2953. devlink_notify(devlink, DEVLINK_CMD_DEL);
  2954. list_del(&devlink->list);
  2955. mutex_unlock(&devlink_mutex);
  2956. }
  2957. EXPORT_SYMBOL_GPL(devlink_unregister);
  2958. /**
  2959. * devlink_free - Free devlink instance resources
  2960. *
  2961. * @devlink: devlink
  2962. */
  2963. void devlink_free(struct devlink *devlink)
  2964. {
  2965. kfree(devlink);
  2966. }
  2967. EXPORT_SYMBOL_GPL(devlink_free);
  2968. /**
  2969. * devlink_port_register - Register devlink port
  2970. *
  2971. * @devlink: devlink
  2972. * @devlink_port: devlink port
  2973. * @port_index
  2974. *
  2975. * Register devlink port with provided port index. User can use
  2976. * any indexing, even hw-related one. devlink_port structure
  2977. * is convenient to be embedded inside user driver private structure.
  2978. * Note that the caller should take care of zeroing the devlink_port
  2979. * structure.
  2980. */
  2981. int devlink_port_register(struct devlink *devlink,
  2982. struct devlink_port *devlink_port,
  2983. unsigned int port_index)
  2984. {
  2985. mutex_lock(&devlink->lock);
  2986. if (devlink_port_index_exists(devlink, port_index)) {
  2987. mutex_unlock(&devlink->lock);
  2988. return -EEXIST;
  2989. }
  2990. devlink_port->devlink = devlink;
  2991. devlink_port->index = port_index;
  2992. devlink_port->registered = true;
  2993. list_add_tail(&devlink_port->list, &devlink->port_list);
  2994. mutex_unlock(&devlink->lock);
  2995. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  2996. return 0;
  2997. }
  2998. EXPORT_SYMBOL_GPL(devlink_port_register);
  2999. /**
  3000. * devlink_port_unregister - Unregister devlink port
  3001. *
  3002. * @devlink_port: devlink port
  3003. */
  3004. void devlink_port_unregister(struct devlink_port *devlink_port)
  3005. {
  3006. struct devlink *devlink = devlink_port->devlink;
  3007. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  3008. mutex_lock(&devlink->lock);
  3009. list_del(&devlink_port->list);
  3010. mutex_unlock(&devlink->lock);
  3011. }
  3012. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  3013. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  3014. enum devlink_port_type type,
  3015. void *type_dev)
  3016. {
  3017. devlink_port->type = type;
  3018. devlink_port->type_dev = type_dev;
  3019. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3020. }
  3021. /**
  3022. * devlink_port_type_eth_set - Set port type to Ethernet
  3023. *
  3024. * @devlink_port: devlink port
  3025. * @netdev: related netdevice
  3026. */
  3027. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  3028. struct net_device *netdev)
  3029. {
  3030. return __devlink_port_type_set(devlink_port,
  3031. DEVLINK_PORT_TYPE_ETH, netdev);
  3032. }
  3033. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  3034. /**
  3035. * devlink_port_type_ib_set - Set port type to InfiniBand
  3036. *
  3037. * @devlink_port: devlink port
  3038. * @ibdev: related IB device
  3039. */
  3040. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  3041. struct ib_device *ibdev)
  3042. {
  3043. return __devlink_port_type_set(devlink_port,
  3044. DEVLINK_PORT_TYPE_IB, ibdev);
  3045. }
  3046. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  3047. /**
  3048. * devlink_port_type_clear - Clear port type
  3049. *
  3050. * @devlink_port: devlink port
  3051. */
  3052. void devlink_port_type_clear(struct devlink_port *devlink_port)
  3053. {
  3054. return __devlink_port_type_set(devlink_port,
  3055. DEVLINK_PORT_TYPE_NOTSET, NULL);
  3056. }
  3057. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  3058. /**
  3059. * devlink_port_attrs_set - Set port attributes
  3060. *
  3061. * @devlink_port: devlink port
  3062. * @flavour: flavour of the port
  3063. * @port_number: number of the port that is facing user, for example
  3064. * the front panel port number
  3065. * @split: indicates if this is split port
  3066. * @split_subport_number: if the port is split, this is the number
  3067. * of subport.
  3068. */
  3069. void devlink_port_attrs_set(struct devlink_port *devlink_port,
  3070. enum devlink_port_flavour flavour,
  3071. u32 port_number, bool split,
  3072. u32 split_subport_number)
  3073. {
  3074. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3075. attrs->set = true;
  3076. attrs->flavour = flavour;
  3077. attrs->port_number = port_number;
  3078. attrs->split = split;
  3079. attrs->split_subport_number = split_subport_number;
  3080. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3081. }
  3082. EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
  3083. int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
  3084. char *name, size_t len)
  3085. {
  3086. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3087. int n = 0;
  3088. if (!attrs->set)
  3089. return -EOPNOTSUPP;
  3090. switch (attrs->flavour) {
  3091. case DEVLINK_PORT_FLAVOUR_PHYSICAL:
  3092. if (!attrs->split)
  3093. n = snprintf(name, len, "p%u", attrs->port_number);
  3094. else
  3095. n = snprintf(name, len, "p%us%u", attrs->port_number,
  3096. attrs->split_subport_number);
  3097. break;
  3098. case DEVLINK_PORT_FLAVOUR_CPU:
  3099. case DEVLINK_PORT_FLAVOUR_DSA:
  3100. /* As CPU and DSA ports do not have a netdevice associated
  3101. * case should not ever happen.
  3102. */
  3103. WARN_ON(1);
  3104. return -EINVAL;
  3105. }
  3106. if (n >= len)
  3107. return -EINVAL;
  3108. return 0;
  3109. }
  3110. EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
  3111. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  3112. u32 size, u16 ingress_pools_count,
  3113. u16 egress_pools_count, u16 ingress_tc_count,
  3114. u16 egress_tc_count)
  3115. {
  3116. struct devlink_sb *devlink_sb;
  3117. int err = 0;
  3118. mutex_lock(&devlink->lock);
  3119. if (devlink_sb_index_exists(devlink, sb_index)) {
  3120. err = -EEXIST;
  3121. goto unlock;
  3122. }
  3123. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  3124. if (!devlink_sb) {
  3125. err = -ENOMEM;
  3126. goto unlock;
  3127. }
  3128. devlink_sb->index = sb_index;
  3129. devlink_sb->size = size;
  3130. devlink_sb->ingress_pools_count = ingress_pools_count;
  3131. devlink_sb->egress_pools_count = egress_pools_count;
  3132. devlink_sb->ingress_tc_count = ingress_tc_count;
  3133. devlink_sb->egress_tc_count = egress_tc_count;
  3134. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  3135. unlock:
  3136. mutex_unlock(&devlink->lock);
  3137. return err;
  3138. }
  3139. EXPORT_SYMBOL_GPL(devlink_sb_register);
  3140. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  3141. {
  3142. struct devlink_sb *devlink_sb;
  3143. mutex_lock(&devlink->lock);
  3144. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  3145. WARN_ON(!devlink_sb);
  3146. list_del(&devlink_sb->list);
  3147. mutex_unlock(&devlink->lock);
  3148. kfree(devlink_sb);
  3149. }
  3150. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  3151. /**
  3152. * devlink_dpipe_headers_register - register dpipe headers
  3153. *
  3154. * @devlink: devlink
  3155. * @dpipe_headers: dpipe header array
  3156. *
  3157. * Register the headers supported by hardware.
  3158. */
  3159. int devlink_dpipe_headers_register(struct devlink *devlink,
  3160. struct devlink_dpipe_headers *dpipe_headers)
  3161. {
  3162. mutex_lock(&devlink->lock);
  3163. devlink->dpipe_headers = dpipe_headers;
  3164. mutex_unlock(&devlink->lock);
  3165. return 0;
  3166. }
  3167. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
  3168. /**
  3169. * devlink_dpipe_headers_unregister - unregister dpipe headers
  3170. *
  3171. * @devlink: devlink
  3172. *
  3173. * Unregister the headers supported by hardware.
  3174. */
  3175. void devlink_dpipe_headers_unregister(struct devlink *devlink)
  3176. {
  3177. mutex_lock(&devlink->lock);
  3178. devlink->dpipe_headers = NULL;
  3179. mutex_unlock(&devlink->lock);
  3180. }
  3181. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
  3182. /**
  3183. * devlink_dpipe_table_counter_enabled - check if counter allocation
  3184. * required
  3185. * @devlink: devlink
  3186. * @table_name: tables name
  3187. *
  3188. * Used by driver to check if counter allocation is required.
  3189. * After counter allocation is turned on the table entries
  3190. * are updated to include counter statistics.
  3191. *
  3192. * After that point on the driver must respect the counter
  3193. * state so that each entry added to the table is added
  3194. * with a counter.
  3195. */
  3196. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  3197. const char *table_name)
  3198. {
  3199. struct devlink_dpipe_table *table;
  3200. bool enabled;
  3201. rcu_read_lock();
  3202. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3203. table_name);
  3204. enabled = false;
  3205. if (table)
  3206. enabled = table->counters_enabled;
  3207. rcu_read_unlock();
  3208. return enabled;
  3209. }
  3210. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  3211. /**
  3212. * devlink_dpipe_table_register - register dpipe table
  3213. *
  3214. * @devlink: devlink
  3215. * @table_name: table name
  3216. * @table_ops: table ops
  3217. * @priv: priv
  3218. * @counter_control_extern: external control for counters
  3219. */
  3220. int devlink_dpipe_table_register(struct devlink *devlink,
  3221. const char *table_name,
  3222. struct devlink_dpipe_table_ops *table_ops,
  3223. void *priv, bool counter_control_extern)
  3224. {
  3225. struct devlink_dpipe_table *table;
  3226. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
  3227. return -EEXIST;
  3228. if (WARN_ON(!table_ops->size_get))
  3229. return -EINVAL;
  3230. table = kzalloc(sizeof(*table), GFP_KERNEL);
  3231. if (!table)
  3232. return -ENOMEM;
  3233. table->name = table_name;
  3234. table->table_ops = table_ops;
  3235. table->priv = priv;
  3236. table->counter_control_extern = counter_control_extern;
  3237. mutex_lock(&devlink->lock);
  3238. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  3239. mutex_unlock(&devlink->lock);
  3240. return 0;
  3241. }
  3242. EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
  3243. /**
  3244. * devlink_dpipe_table_unregister - unregister dpipe table
  3245. *
  3246. * @devlink: devlink
  3247. * @table_name: table name
  3248. */
  3249. void devlink_dpipe_table_unregister(struct devlink *devlink,
  3250. const char *table_name)
  3251. {
  3252. struct devlink_dpipe_table *table;
  3253. mutex_lock(&devlink->lock);
  3254. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3255. table_name);
  3256. if (!table)
  3257. goto unlock;
  3258. list_del_rcu(&table->list);
  3259. mutex_unlock(&devlink->lock);
  3260. kfree_rcu(table, rcu);
  3261. return;
  3262. unlock:
  3263. mutex_unlock(&devlink->lock);
  3264. }
  3265. EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
  3266. /**
  3267. * devlink_resource_register - devlink resource register
  3268. *
  3269. * @devlink: devlink
  3270. * @resource_name: resource's name
  3271. * @top_hierarchy: top hierarchy
  3272. * @reload_required: reload is required for new configuration to
  3273. * apply
  3274. * @resource_size: resource's size
  3275. * @resource_id: resource's id
  3276. * @parent_reosurce_id: resource's parent id
  3277. * @size params: size parameters
  3278. */
  3279. int devlink_resource_register(struct devlink *devlink,
  3280. const char *resource_name,
  3281. u64 resource_size,
  3282. u64 resource_id,
  3283. u64 parent_resource_id,
  3284. const struct devlink_resource_size_params *size_params)
  3285. {
  3286. struct devlink_resource *resource;
  3287. struct list_head *resource_list;
  3288. bool top_hierarchy;
  3289. int err = 0;
  3290. top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
  3291. mutex_lock(&devlink->lock);
  3292. resource = devlink_resource_find(devlink, NULL, resource_id);
  3293. if (resource) {
  3294. err = -EINVAL;
  3295. goto out;
  3296. }
  3297. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  3298. if (!resource) {
  3299. err = -ENOMEM;
  3300. goto out;
  3301. }
  3302. if (top_hierarchy) {
  3303. resource_list = &devlink->resource_list;
  3304. } else {
  3305. struct devlink_resource *parent_resource;
  3306. parent_resource = devlink_resource_find(devlink, NULL,
  3307. parent_resource_id);
  3308. if (parent_resource) {
  3309. resource_list = &parent_resource->resource_list;
  3310. resource->parent = parent_resource;
  3311. } else {
  3312. kfree(resource);
  3313. err = -EINVAL;
  3314. goto out;
  3315. }
  3316. }
  3317. resource->name = resource_name;
  3318. resource->size = resource_size;
  3319. resource->size_new = resource_size;
  3320. resource->id = resource_id;
  3321. resource->size_valid = true;
  3322. memcpy(&resource->size_params, size_params,
  3323. sizeof(resource->size_params));
  3324. INIT_LIST_HEAD(&resource->resource_list);
  3325. list_add_tail(&resource->list, resource_list);
  3326. out:
  3327. mutex_unlock(&devlink->lock);
  3328. return err;
  3329. }
  3330. EXPORT_SYMBOL_GPL(devlink_resource_register);
  3331. /**
  3332. * devlink_resources_unregister - free all resources
  3333. *
  3334. * @devlink: devlink
  3335. * @resource: resource
  3336. */
  3337. void devlink_resources_unregister(struct devlink *devlink,
  3338. struct devlink_resource *resource)
  3339. {
  3340. struct devlink_resource *tmp, *child_resource;
  3341. struct list_head *resource_list;
  3342. if (resource)
  3343. resource_list = &resource->resource_list;
  3344. else
  3345. resource_list = &devlink->resource_list;
  3346. if (!resource)
  3347. mutex_lock(&devlink->lock);
  3348. list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
  3349. devlink_resources_unregister(devlink, child_resource);
  3350. list_del(&child_resource->list);
  3351. kfree(child_resource);
  3352. }
  3353. if (!resource)
  3354. mutex_unlock(&devlink->lock);
  3355. }
  3356. EXPORT_SYMBOL_GPL(devlink_resources_unregister);
  3357. /**
  3358. * devlink_resource_size_get - get and update size
  3359. *
  3360. * @devlink: devlink
  3361. * @resource_id: the requested resource id
  3362. * @p_resource_size: ptr to update
  3363. */
  3364. int devlink_resource_size_get(struct devlink *devlink,
  3365. u64 resource_id,
  3366. u64 *p_resource_size)
  3367. {
  3368. struct devlink_resource *resource;
  3369. int err = 0;
  3370. mutex_lock(&devlink->lock);
  3371. resource = devlink_resource_find(devlink, NULL, resource_id);
  3372. if (!resource) {
  3373. err = -EINVAL;
  3374. goto out;
  3375. }
  3376. *p_resource_size = resource->size_new;
  3377. resource->size = resource->size_new;
  3378. out:
  3379. mutex_unlock(&devlink->lock);
  3380. return err;
  3381. }
  3382. EXPORT_SYMBOL_GPL(devlink_resource_size_get);
  3383. /**
  3384. * devlink_dpipe_table_resource_set - set the resource id
  3385. *
  3386. * @devlink: devlink
  3387. * @table_name: table name
  3388. * @resource_id: resource id
  3389. * @resource_units: number of resource's units consumed per table's entry
  3390. */
  3391. int devlink_dpipe_table_resource_set(struct devlink *devlink,
  3392. const char *table_name, u64 resource_id,
  3393. u64 resource_units)
  3394. {
  3395. struct devlink_dpipe_table *table;
  3396. int err = 0;
  3397. mutex_lock(&devlink->lock);
  3398. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3399. table_name);
  3400. if (!table) {
  3401. err = -EINVAL;
  3402. goto out;
  3403. }
  3404. table->resource_id = resource_id;
  3405. table->resource_units = resource_units;
  3406. table->resource_valid = true;
  3407. out:
  3408. mutex_unlock(&devlink->lock);
  3409. return err;
  3410. }
  3411. EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
  3412. /**
  3413. * devlink_resource_occ_get_register - register occupancy getter
  3414. *
  3415. * @devlink: devlink
  3416. * @resource_id: resource id
  3417. * @occ_get: occupancy getter callback
  3418. * @occ_get_priv: occupancy getter callback priv
  3419. */
  3420. void devlink_resource_occ_get_register(struct devlink *devlink,
  3421. u64 resource_id,
  3422. devlink_resource_occ_get_t *occ_get,
  3423. void *occ_get_priv)
  3424. {
  3425. struct devlink_resource *resource;
  3426. mutex_lock(&devlink->lock);
  3427. resource = devlink_resource_find(devlink, NULL, resource_id);
  3428. if (WARN_ON(!resource))
  3429. goto out;
  3430. WARN_ON(resource->occ_get);
  3431. resource->occ_get = occ_get;
  3432. resource->occ_get_priv = occ_get_priv;
  3433. out:
  3434. mutex_unlock(&devlink->lock);
  3435. }
  3436. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
  3437. /**
  3438. * devlink_resource_occ_get_unregister - unregister occupancy getter
  3439. *
  3440. * @devlink: devlink
  3441. * @resource_id: resource id
  3442. */
  3443. void devlink_resource_occ_get_unregister(struct devlink *devlink,
  3444. u64 resource_id)
  3445. {
  3446. struct devlink_resource *resource;
  3447. mutex_lock(&devlink->lock);
  3448. resource = devlink_resource_find(devlink, NULL, resource_id);
  3449. if (WARN_ON(!resource))
  3450. goto out;
  3451. WARN_ON(!resource->occ_get);
  3452. resource->occ_get = NULL;
  3453. resource->occ_get_priv = NULL;
  3454. out:
  3455. mutex_unlock(&devlink->lock);
  3456. }
  3457. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
  3458. /**
  3459. * devlink_params_register - register configuration parameters
  3460. *
  3461. * @devlink: devlink
  3462. * @params: configuration parameters array
  3463. * @params_count: number of parameters provided
  3464. *
  3465. * Register the configuration parameters supported by the driver.
  3466. */
  3467. int devlink_params_register(struct devlink *devlink,
  3468. const struct devlink_param *params,
  3469. size_t params_count)
  3470. {
  3471. const struct devlink_param *param = params;
  3472. int i;
  3473. int err;
  3474. mutex_lock(&devlink->lock);
  3475. for (i = 0; i < params_count; i++, param++) {
  3476. if (!param || !param->name || !param->supported_cmodes) {
  3477. err = -EINVAL;
  3478. goto rollback;
  3479. }
  3480. if (param->generic) {
  3481. err = devlink_param_generic_verify(param);
  3482. if (err)
  3483. goto rollback;
  3484. } else {
  3485. err = devlink_param_driver_verify(param);
  3486. if (err)
  3487. goto rollback;
  3488. }
  3489. err = devlink_param_register_one(devlink, param);
  3490. if (err)
  3491. goto rollback;
  3492. }
  3493. mutex_unlock(&devlink->lock);
  3494. return 0;
  3495. rollback:
  3496. if (!i)
  3497. goto unlock;
  3498. for (param--; i > 0; i--, param--)
  3499. devlink_param_unregister_one(devlink, param);
  3500. unlock:
  3501. mutex_unlock(&devlink->lock);
  3502. return err;
  3503. }
  3504. EXPORT_SYMBOL_GPL(devlink_params_register);
  3505. /**
  3506. * devlink_params_unregister - unregister configuration parameters
  3507. * @devlink: devlink
  3508. * @params: configuration parameters to unregister
  3509. * @params_count: number of parameters provided
  3510. */
  3511. void devlink_params_unregister(struct devlink *devlink,
  3512. const struct devlink_param *params,
  3513. size_t params_count)
  3514. {
  3515. const struct devlink_param *param = params;
  3516. int i;
  3517. mutex_lock(&devlink->lock);
  3518. for (i = 0; i < params_count; i++, param++)
  3519. devlink_param_unregister_one(devlink, param);
  3520. mutex_unlock(&devlink->lock);
  3521. }
  3522. EXPORT_SYMBOL_GPL(devlink_params_unregister);
  3523. /**
  3524. * devlink_param_driverinit_value_get - get configuration parameter
  3525. * value for driver initializing
  3526. *
  3527. * @devlink: devlink
  3528. * @param_id: parameter ID
  3529. * @init_val: value of parameter in driverinit configuration mode
  3530. *
  3531. * This function should be used by the driver to get driverinit
  3532. * configuration for initialization after reload command.
  3533. */
  3534. int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
  3535. union devlink_param_value *init_val)
  3536. {
  3537. struct devlink_param_item *param_item;
  3538. if (!devlink->ops || !devlink->ops->reload)
  3539. return -EOPNOTSUPP;
  3540. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  3541. if (!param_item)
  3542. return -EINVAL;
  3543. if (!param_item->driverinit_value_valid ||
  3544. !devlink_param_cmode_is_supported(param_item->param,
  3545. DEVLINK_PARAM_CMODE_DRIVERINIT))
  3546. return -EOPNOTSUPP;
  3547. *init_val = param_item->driverinit_value;
  3548. return 0;
  3549. }
  3550. EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
  3551. /**
  3552. * devlink_param_driverinit_value_set - set value of configuration
  3553. * parameter for driverinit
  3554. * configuration mode
  3555. *
  3556. * @devlink: devlink
  3557. * @param_id: parameter ID
  3558. * @init_val: value of parameter to set for driverinit configuration mode
  3559. *
  3560. * This function should be used by the driver to set driverinit
  3561. * configuration mode default value.
  3562. */
  3563. int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
  3564. union devlink_param_value init_val)
  3565. {
  3566. struct devlink_param_item *param_item;
  3567. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  3568. if (!param_item)
  3569. return -EINVAL;
  3570. if (!devlink_param_cmode_is_supported(param_item->param,
  3571. DEVLINK_PARAM_CMODE_DRIVERINIT))
  3572. return -EOPNOTSUPP;
  3573. param_item->driverinit_value = init_val;
  3574. param_item->driverinit_value_valid = true;
  3575. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  3576. return 0;
  3577. }
  3578. EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
  3579. /**
  3580. * devlink_param_value_changed - notify devlink on a parameter's value
  3581. * change. Should be called by the driver
  3582. * right after the change.
  3583. *
  3584. * @devlink: devlink
  3585. * @param_id: parameter ID
  3586. *
  3587. * This function should be used by the driver to notify devlink on value
  3588. * change, excluding driverinit configuration mode.
  3589. * For driverinit configuration mode driver should use the function
  3590. * devlink_param_driverinit_value_set() instead.
  3591. */
  3592. void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
  3593. {
  3594. struct devlink_param_item *param_item;
  3595. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  3596. WARN_ON(!param_item);
  3597. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  3598. }
  3599. EXPORT_SYMBOL_GPL(devlink_param_value_changed);
  3600. /**
  3601. * devlink_region_create - create a new address region
  3602. *
  3603. * @devlink: devlink
  3604. * @region_name: region name
  3605. * @region_max_snapshots: Maximum supported number of snapshots for region
  3606. * @region_size: size of region
  3607. */
  3608. struct devlink_region *devlink_region_create(struct devlink *devlink,
  3609. const char *region_name,
  3610. u32 region_max_snapshots,
  3611. u64 region_size)
  3612. {
  3613. struct devlink_region *region;
  3614. int err = 0;
  3615. mutex_lock(&devlink->lock);
  3616. if (devlink_region_get_by_name(devlink, region_name)) {
  3617. err = -EEXIST;
  3618. goto unlock;
  3619. }
  3620. region = kzalloc(sizeof(*region), GFP_KERNEL);
  3621. if (!region) {
  3622. err = -ENOMEM;
  3623. goto unlock;
  3624. }
  3625. region->devlink = devlink;
  3626. region->max_snapshots = region_max_snapshots;
  3627. region->name = region_name;
  3628. region->size = region_size;
  3629. INIT_LIST_HEAD(&region->snapshot_list);
  3630. list_add_tail(&region->list, &devlink->region_list);
  3631. mutex_unlock(&devlink->lock);
  3632. return region;
  3633. unlock:
  3634. mutex_unlock(&devlink->lock);
  3635. return ERR_PTR(err);
  3636. }
  3637. EXPORT_SYMBOL_GPL(devlink_region_create);
  3638. /**
  3639. * devlink_region_destroy - destroy address region
  3640. *
  3641. * @region: devlink region to destroy
  3642. */
  3643. void devlink_region_destroy(struct devlink_region *region)
  3644. {
  3645. struct devlink *devlink = region->devlink;
  3646. mutex_lock(&devlink->lock);
  3647. list_del(&region->list);
  3648. mutex_unlock(&devlink->lock);
  3649. kfree(region);
  3650. }
  3651. EXPORT_SYMBOL_GPL(devlink_region_destroy);
  3652. /**
  3653. * devlink_region_shapshot_id_get - get snapshot ID
  3654. *
  3655. * This callback should be called when adding a new snapshot,
  3656. * Driver should use the same id for multiple snapshots taken
  3657. * on multiple regions at the same time/by the same trigger.
  3658. *
  3659. * @devlink: devlink
  3660. */
  3661. u32 devlink_region_shapshot_id_get(struct devlink *devlink)
  3662. {
  3663. u32 id;
  3664. mutex_lock(&devlink->lock);
  3665. id = ++devlink->snapshot_id;
  3666. mutex_unlock(&devlink->lock);
  3667. return id;
  3668. }
  3669. EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
  3670. static int __init devlink_module_init(void)
  3671. {
  3672. return genl_register_family(&devlink_nl_family);
  3673. }
  3674. static void __exit devlink_module_exit(void)
  3675. {
  3676. genl_unregister_family(&devlink_nl_family);
  3677. }
  3678. module_init(devlink_module_init);
  3679. module_exit(devlink_module_exit);
  3680. MODULE_LICENSE("GPL v2");
  3681. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  3682. MODULE_DESCRIPTION("Network physical device Netlink interface");
  3683. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);