devlink.c 72 KB

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