devlink.c 84 KB

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