devlink.c 70 KB

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