team.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  1. /*
  2. * drivers/net/team/team.c - Network team device driver
  3. * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/errno.h>
  17. #include <linux/ctype.h>
  18. #include <linux/notifier.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/netpoll.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/socket.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/rtnetlink.h>
  26. #include <net/rtnetlink.h>
  27. #include <net/genetlink.h>
  28. #include <net/netlink.h>
  29. #include <net/sch_generic.h>
  30. #include <net/switchdev.h>
  31. #include <generated/utsrelease.h>
  32. #include <linux/if_team.h>
  33. #define DRV_NAME "team"
  34. /**********
  35. * Helpers
  36. **********/
  37. #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
  38. static struct team_port *team_port_get_rcu(const struct net_device *dev)
  39. {
  40. return rcu_dereference(dev->rx_handler_data);
  41. }
  42. static struct team_port *team_port_get_rtnl(const struct net_device *dev)
  43. {
  44. struct team_port *port = rtnl_dereference(dev->rx_handler_data);
  45. return team_port_exists(dev) ? port : NULL;
  46. }
  47. /*
  48. * Since the ability to change device address for open port device is tested in
  49. * team_port_add, this function can be called without control of return value
  50. */
  51. static int __set_port_dev_addr(struct net_device *port_dev,
  52. const unsigned char *dev_addr)
  53. {
  54. struct sockaddr addr;
  55. memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
  56. addr.sa_family = port_dev->type;
  57. return dev_set_mac_address(port_dev, &addr);
  58. }
  59. static int team_port_set_orig_dev_addr(struct team_port *port)
  60. {
  61. return __set_port_dev_addr(port->dev, port->orig.dev_addr);
  62. }
  63. static int team_port_set_team_dev_addr(struct team *team,
  64. struct team_port *port)
  65. {
  66. return __set_port_dev_addr(port->dev, team->dev->dev_addr);
  67. }
  68. int team_modeop_port_enter(struct team *team, struct team_port *port)
  69. {
  70. return team_port_set_team_dev_addr(team, port);
  71. }
  72. EXPORT_SYMBOL(team_modeop_port_enter);
  73. void team_modeop_port_change_dev_addr(struct team *team,
  74. struct team_port *port)
  75. {
  76. team_port_set_team_dev_addr(team, port);
  77. }
  78. EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
  79. static void team_lower_state_changed(struct team_port *port)
  80. {
  81. struct netdev_lag_lower_state_info info;
  82. info.link_up = port->linkup;
  83. info.tx_enabled = team_port_enabled(port);
  84. netdev_lower_state_changed(port->dev, &info);
  85. }
  86. static void team_refresh_port_linkup(struct team_port *port)
  87. {
  88. bool new_linkup = port->user.linkup_enabled ? port->user.linkup :
  89. port->state.linkup;
  90. if (port->linkup != new_linkup) {
  91. port->linkup = new_linkup;
  92. team_lower_state_changed(port);
  93. }
  94. }
  95. /*******************
  96. * Options handling
  97. *******************/
  98. struct team_option_inst { /* One for each option instance */
  99. struct list_head list;
  100. struct list_head tmp_list;
  101. struct team_option *option;
  102. struct team_option_inst_info info;
  103. bool changed;
  104. bool removed;
  105. };
  106. static struct team_option *__team_find_option(struct team *team,
  107. const char *opt_name)
  108. {
  109. struct team_option *option;
  110. list_for_each_entry(option, &team->option_list, list) {
  111. if (strcmp(option->name, opt_name) == 0)
  112. return option;
  113. }
  114. return NULL;
  115. }
  116. static void __team_option_inst_del(struct team_option_inst *opt_inst)
  117. {
  118. list_del(&opt_inst->list);
  119. kfree(opt_inst);
  120. }
  121. static void __team_option_inst_del_option(struct team *team,
  122. struct team_option *option)
  123. {
  124. struct team_option_inst *opt_inst, *tmp;
  125. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  126. if (opt_inst->option == option)
  127. __team_option_inst_del(opt_inst);
  128. }
  129. }
  130. static int __team_option_inst_add(struct team *team, struct team_option *option,
  131. struct team_port *port)
  132. {
  133. struct team_option_inst *opt_inst;
  134. unsigned int array_size;
  135. unsigned int i;
  136. int err;
  137. array_size = option->array_size;
  138. if (!array_size)
  139. array_size = 1; /* No array but still need one instance */
  140. for (i = 0; i < array_size; i++) {
  141. opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
  142. if (!opt_inst)
  143. return -ENOMEM;
  144. opt_inst->option = option;
  145. opt_inst->info.port = port;
  146. opt_inst->info.array_index = i;
  147. opt_inst->changed = true;
  148. opt_inst->removed = false;
  149. list_add_tail(&opt_inst->list, &team->option_inst_list);
  150. if (option->init) {
  151. err = option->init(team, &opt_inst->info);
  152. if (err)
  153. return err;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int __team_option_inst_add_option(struct team *team,
  159. struct team_option *option)
  160. {
  161. int err;
  162. if (!option->per_port) {
  163. err = __team_option_inst_add(team, option, NULL);
  164. if (err)
  165. goto inst_del_option;
  166. }
  167. return 0;
  168. inst_del_option:
  169. __team_option_inst_del_option(team, option);
  170. return err;
  171. }
  172. static void __team_option_inst_mark_removed_option(struct team *team,
  173. struct team_option *option)
  174. {
  175. struct team_option_inst *opt_inst;
  176. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  177. if (opt_inst->option == option) {
  178. opt_inst->changed = true;
  179. opt_inst->removed = true;
  180. }
  181. }
  182. }
  183. static void __team_option_inst_del_port(struct team *team,
  184. struct team_port *port)
  185. {
  186. struct team_option_inst *opt_inst, *tmp;
  187. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  188. if (opt_inst->option->per_port &&
  189. opt_inst->info.port == port)
  190. __team_option_inst_del(opt_inst);
  191. }
  192. }
  193. static int __team_option_inst_add_port(struct team *team,
  194. struct team_port *port)
  195. {
  196. struct team_option *option;
  197. int err;
  198. list_for_each_entry(option, &team->option_list, list) {
  199. if (!option->per_port)
  200. continue;
  201. err = __team_option_inst_add(team, option, port);
  202. if (err)
  203. goto inst_del_port;
  204. }
  205. return 0;
  206. inst_del_port:
  207. __team_option_inst_del_port(team, port);
  208. return err;
  209. }
  210. static void __team_option_inst_mark_removed_port(struct team *team,
  211. struct team_port *port)
  212. {
  213. struct team_option_inst *opt_inst;
  214. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  215. if (opt_inst->info.port == port) {
  216. opt_inst->changed = true;
  217. opt_inst->removed = true;
  218. }
  219. }
  220. }
  221. static bool __team_option_inst_tmp_find(const struct list_head *opts,
  222. const struct team_option_inst *needle)
  223. {
  224. struct team_option_inst *opt_inst;
  225. list_for_each_entry(opt_inst, opts, tmp_list)
  226. if (opt_inst == needle)
  227. return true;
  228. return false;
  229. }
  230. static int __team_options_register(struct team *team,
  231. const struct team_option *option,
  232. size_t option_count)
  233. {
  234. int i;
  235. struct team_option **dst_opts;
  236. int err;
  237. dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
  238. GFP_KERNEL);
  239. if (!dst_opts)
  240. return -ENOMEM;
  241. for (i = 0; i < option_count; i++, option++) {
  242. if (__team_find_option(team, option->name)) {
  243. err = -EEXIST;
  244. goto alloc_rollback;
  245. }
  246. dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
  247. if (!dst_opts[i]) {
  248. err = -ENOMEM;
  249. goto alloc_rollback;
  250. }
  251. }
  252. for (i = 0; i < option_count; i++) {
  253. err = __team_option_inst_add_option(team, dst_opts[i]);
  254. if (err)
  255. goto inst_rollback;
  256. list_add_tail(&dst_opts[i]->list, &team->option_list);
  257. }
  258. kfree(dst_opts);
  259. return 0;
  260. inst_rollback:
  261. for (i--; i >= 0; i--)
  262. __team_option_inst_del_option(team, dst_opts[i]);
  263. i = option_count - 1;
  264. alloc_rollback:
  265. for (i--; i >= 0; i--)
  266. kfree(dst_opts[i]);
  267. kfree(dst_opts);
  268. return err;
  269. }
  270. static void __team_options_mark_removed(struct team *team,
  271. const struct team_option *option,
  272. size_t option_count)
  273. {
  274. int i;
  275. for (i = 0; i < option_count; i++, option++) {
  276. struct team_option *del_opt;
  277. del_opt = __team_find_option(team, option->name);
  278. if (del_opt)
  279. __team_option_inst_mark_removed_option(team, del_opt);
  280. }
  281. }
  282. static void __team_options_unregister(struct team *team,
  283. const struct team_option *option,
  284. size_t option_count)
  285. {
  286. int i;
  287. for (i = 0; i < option_count; i++, option++) {
  288. struct team_option *del_opt;
  289. del_opt = __team_find_option(team, option->name);
  290. if (del_opt) {
  291. __team_option_inst_del_option(team, del_opt);
  292. list_del(&del_opt->list);
  293. kfree(del_opt);
  294. }
  295. }
  296. }
  297. static void __team_options_change_check(struct team *team);
  298. int team_options_register(struct team *team,
  299. const struct team_option *option,
  300. size_t option_count)
  301. {
  302. int err;
  303. err = __team_options_register(team, option, option_count);
  304. if (err)
  305. return err;
  306. __team_options_change_check(team);
  307. return 0;
  308. }
  309. EXPORT_SYMBOL(team_options_register);
  310. void team_options_unregister(struct team *team,
  311. const struct team_option *option,
  312. size_t option_count)
  313. {
  314. __team_options_mark_removed(team, option, option_count);
  315. __team_options_change_check(team);
  316. __team_options_unregister(team, option, option_count);
  317. }
  318. EXPORT_SYMBOL(team_options_unregister);
  319. static int team_option_get(struct team *team,
  320. struct team_option_inst *opt_inst,
  321. struct team_gsetter_ctx *ctx)
  322. {
  323. if (!opt_inst->option->getter)
  324. return -EOPNOTSUPP;
  325. return opt_inst->option->getter(team, ctx);
  326. }
  327. static int team_option_set(struct team *team,
  328. struct team_option_inst *opt_inst,
  329. struct team_gsetter_ctx *ctx)
  330. {
  331. if (!opt_inst->option->setter)
  332. return -EOPNOTSUPP;
  333. return opt_inst->option->setter(team, ctx);
  334. }
  335. void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
  336. {
  337. struct team_option_inst *opt_inst;
  338. opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
  339. opt_inst->changed = true;
  340. }
  341. EXPORT_SYMBOL(team_option_inst_set_change);
  342. void team_options_change_check(struct team *team)
  343. {
  344. __team_options_change_check(team);
  345. }
  346. EXPORT_SYMBOL(team_options_change_check);
  347. /****************
  348. * Mode handling
  349. ****************/
  350. static LIST_HEAD(mode_list);
  351. static DEFINE_SPINLOCK(mode_list_lock);
  352. struct team_mode_item {
  353. struct list_head list;
  354. const struct team_mode *mode;
  355. };
  356. static struct team_mode_item *__find_mode(const char *kind)
  357. {
  358. struct team_mode_item *mitem;
  359. list_for_each_entry(mitem, &mode_list, list) {
  360. if (strcmp(mitem->mode->kind, kind) == 0)
  361. return mitem;
  362. }
  363. return NULL;
  364. }
  365. static bool is_good_mode_name(const char *name)
  366. {
  367. while (*name != '\0') {
  368. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  369. return false;
  370. name++;
  371. }
  372. return true;
  373. }
  374. int team_mode_register(const struct team_mode *mode)
  375. {
  376. int err = 0;
  377. struct team_mode_item *mitem;
  378. if (!is_good_mode_name(mode->kind) ||
  379. mode->priv_size > TEAM_MODE_PRIV_SIZE)
  380. return -EINVAL;
  381. mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
  382. if (!mitem)
  383. return -ENOMEM;
  384. spin_lock(&mode_list_lock);
  385. if (__find_mode(mode->kind)) {
  386. err = -EEXIST;
  387. kfree(mitem);
  388. goto unlock;
  389. }
  390. mitem->mode = mode;
  391. list_add_tail(&mitem->list, &mode_list);
  392. unlock:
  393. spin_unlock(&mode_list_lock);
  394. return err;
  395. }
  396. EXPORT_SYMBOL(team_mode_register);
  397. void team_mode_unregister(const struct team_mode *mode)
  398. {
  399. struct team_mode_item *mitem;
  400. spin_lock(&mode_list_lock);
  401. mitem = __find_mode(mode->kind);
  402. if (mitem) {
  403. list_del_init(&mitem->list);
  404. kfree(mitem);
  405. }
  406. spin_unlock(&mode_list_lock);
  407. }
  408. EXPORT_SYMBOL(team_mode_unregister);
  409. static const struct team_mode *team_mode_get(const char *kind)
  410. {
  411. struct team_mode_item *mitem;
  412. const struct team_mode *mode = NULL;
  413. spin_lock(&mode_list_lock);
  414. mitem = __find_mode(kind);
  415. if (!mitem) {
  416. spin_unlock(&mode_list_lock);
  417. request_module("team-mode-%s", kind);
  418. spin_lock(&mode_list_lock);
  419. mitem = __find_mode(kind);
  420. }
  421. if (mitem) {
  422. mode = mitem->mode;
  423. if (!try_module_get(mode->owner))
  424. mode = NULL;
  425. }
  426. spin_unlock(&mode_list_lock);
  427. return mode;
  428. }
  429. static void team_mode_put(const struct team_mode *mode)
  430. {
  431. module_put(mode->owner);
  432. }
  433. static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
  434. {
  435. dev_kfree_skb_any(skb);
  436. return false;
  437. }
  438. static rx_handler_result_t team_dummy_receive(struct team *team,
  439. struct team_port *port,
  440. struct sk_buff *skb)
  441. {
  442. return RX_HANDLER_ANOTHER;
  443. }
  444. static const struct team_mode __team_no_mode = {
  445. .kind = "*NOMODE*",
  446. };
  447. static bool team_is_mode_set(struct team *team)
  448. {
  449. return team->mode != &__team_no_mode;
  450. }
  451. static void team_set_no_mode(struct team *team)
  452. {
  453. team->user_carrier_enabled = false;
  454. team->mode = &__team_no_mode;
  455. }
  456. static void team_adjust_ops(struct team *team)
  457. {
  458. /*
  459. * To avoid checks in rx/tx skb paths, ensure here that non-null and
  460. * correct ops are always set.
  461. */
  462. if (!team->en_port_count || !team_is_mode_set(team) ||
  463. !team->mode->ops->transmit)
  464. team->ops.transmit = team_dummy_transmit;
  465. else
  466. team->ops.transmit = team->mode->ops->transmit;
  467. if (!team->en_port_count || !team_is_mode_set(team) ||
  468. !team->mode->ops->receive)
  469. team->ops.receive = team_dummy_receive;
  470. else
  471. team->ops.receive = team->mode->ops->receive;
  472. }
  473. /*
  474. * We can benefit from the fact that it's ensured no port is present
  475. * at the time of mode change. Therefore no packets are in fly so there's no
  476. * need to set mode operations in any special way.
  477. */
  478. static int __team_change_mode(struct team *team,
  479. const struct team_mode *new_mode)
  480. {
  481. /* Check if mode was previously set and do cleanup if so */
  482. if (team_is_mode_set(team)) {
  483. void (*exit_op)(struct team *team) = team->ops.exit;
  484. /* Clear ops area so no callback is called any longer */
  485. memset(&team->ops, 0, sizeof(struct team_mode_ops));
  486. team_adjust_ops(team);
  487. if (exit_op)
  488. exit_op(team);
  489. team_mode_put(team->mode);
  490. team_set_no_mode(team);
  491. /* zero private data area */
  492. memset(&team->mode_priv, 0,
  493. sizeof(struct team) - offsetof(struct team, mode_priv));
  494. }
  495. if (!new_mode)
  496. return 0;
  497. if (new_mode->ops->init) {
  498. int err;
  499. err = new_mode->ops->init(team);
  500. if (err)
  501. return err;
  502. }
  503. team->mode = new_mode;
  504. memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
  505. team_adjust_ops(team);
  506. return 0;
  507. }
  508. static int team_change_mode(struct team *team, const char *kind)
  509. {
  510. const struct team_mode *new_mode;
  511. struct net_device *dev = team->dev;
  512. int err;
  513. if (!list_empty(&team->port_list)) {
  514. netdev_err(dev, "No ports can be present during mode change\n");
  515. return -EBUSY;
  516. }
  517. if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
  518. netdev_err(dev, "Unable to change to the same mode the team is in\n");
  519. return -EINVAL;
  520. }
  521. new_mode = team_mode_get(kind);
  522. if (!new_mode) {
  523. netdev_err(dev, "Mode \"%s\" not found\n", kind);
  524. return -EINVAL;
  525. }
  526. err = __team_change_mode(team, new_mode);
  527. if (err) {
  528. netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
  529. team_mode_put(new_mode);
  530. return err;
  531. }
  532. netdev_info(dev, "Mode changed to \"%s\"\n", kind);
  533. return 0;
  534. }
  535. /*********************
  536. * Peers notification
  537. *********************/
  538. static void team_notify_peers_work(struct work_struct *work)
  539. {
  540. struct team *team;
  541. int val;
  542. team = container_of(work, struct team, notify_peers.dw.work);
  543. if (!rtnl_trylock()) {
  544. schedule_delayed_work(&team->notify_peers.dw, 0);
  545. return;
  546. }
  547. val = atomic_dec_if_positive(&team->notify_peers.count_pending);
  548. if (val < 0) {
  549. rtnl_unlock();
  550. return;
  551. }
  552. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
  553. rtnl_unlock();
  554. if (val)
  555. schedule_delayed_work(&team->notify_peers.dw,
  556. msecs_to_jiffies(team->notify_peers.interval));
  557. }
  558. static void team_notify_peers(struct team *team)
  559. {
  560. if (!team->notify_peers.count || !netif_running(team->dev))
  561. return;
  562. atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
  563. schedule_delayed_work(&team->notify_peers.dw, 0);
  564. }
  565. static void team_notify_peers_init(struct team *team)
  566. {
  567. INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
  568. }
  569. static void team_notify_peers_fini(struct team *team)
  570. {
  571. cancel_delayed_work_sync(&team->notify_peers.dw);
  572. }
  573. /*******************************
  574. * Send multicast group rejoins
  575. *******************************/
  576. static void team_mcast_rejoin_work(struct work_struct *work)
  577. {
  578. struct team *team;
  579. int val;
  580. team = container_of(work, struct team, mcast_rejoin.dw.work);
  581. if (!rtnl_trylock()) {
  582. schedule_delayed_work(&team->mcast_rejoin.dw, 0);
  583. return;
  584. }
  585. val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
  586. if (val < 0) {
  587. rtnl_unlock();
  588. return;
  589. }
  590. call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
  591. rtnl_unlock();
  592. if (val)
  593. schedule_delayed_work(&team->mcast_rejoin.dw,
  594. msecs_to_jiffies(team->mcast_rejoin.interval));
  595. }
  596. static void team_mcast_rejoin(struct team *team)
  597. {
  598. if (!team->mcast_rejoin.count || !netif_running(team->dev))
  599. return;
  600. atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
  601. schedule_delayed_work(&team->mcast_rejoin.dw, 0);
  602. }
  603. static void team_mcast_rejoin_init(struct team *team)
  604. {
  605. INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
  606. }
  607. static void team_mcast_rejoin_fini(struct team *team)
  608. {
  609. cancel_delayed_work_sync(&team->mcast_rejoin.dw);
  610. }
  611. /************************
  612. * Rx path frame handler
  613. ************************/
  614. /* note: already called with rcu_read_lock */
  615. static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
  616. {
  617. struct sk_buff *skb = *pskb;
  618. struct team_port *port;
  619. struct team *team;
  620. rx_handler_result_t res;
  621. skb = skb_share_check(skb, GFP_ATOMIC);
  622. if (!skb)
  623. return RX_HANDLER_CONSUMED;
  624. *pskb = skb;
  625. port = team_port_get_rcu(skb->dev);
  626. team = port->team;
  627. if (!team_port_enabled(port)) {
  628. /* allow exact match delivery for disabled ports */
  629. res = RX_HANDLER_EXACT;
  630. } else {
  631. res = team->ops.receive(team, port, skb);
  632. }
  633. if (res == RX_HANDLER_ANOTHER) {
  634. struct team_pcpu_stats *pcpu_stats;
  635. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  636. u64_stats_update_begin(&pcpu_stats->syncp);
  637. pcpu_stats->rx_packets++;
  638. pcpu_stats->rx_bytes += skb->len;
  639. if (skb->pkt_type == PACKET_MULTICAST)
  640. pcpu_stats->rx_multicast++;
  641. u64_stats_update_end(&pcpu_stats->syncp);
  642. skb->dev = team->dev;
  643. } else if (res == RX_HANDLER_EXACT) {
  644. this_cpu_inc(team->pcpu_stats->rx_nohandler);
  645. } else {
  646. this_cpu_inc(team->pcpu_stats->rx_dropped);
  647. }
  648. return res;
  649. }
  650. /*************************************
  651. * Multiqueue Tx port select override
  652. *************************************/
  653. static int team_queue_override_init(struct team *team)
  654. {
  655. struct list_head *listarr;
  656. unsigned int queue_cnt = team->dev->num_tx_queues - 1;
  657. unsigned int i;
  658. if (!queue_cnt)
  659. return 0;
  660. listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
  661. if (!listarr)
  662. return -ENOMEM;
  663. team->qom_lists = listarr;
  664. for (i = 0; i < queue_cnt; i++)
  665. INIT_LIST_HEAD(listarr++);
  666. return 0;
  667. }
  668. static void team_queue_override_fini(struct team *team)
  669. {
  670. kfree(team->qom_lists);
  671. }
  672. static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
  673. {
  674. return &team->qom_lists[queue_id - 1];
  675. }
  676. /*
  677. * note: already called with rcu_read_lock
  678. */
  679. static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
  680. {
  681. struct list_head *qom_list;
  682. struct team_port *port;
  683. if (!team->queue_override_enabled || !skb->queue_mapping)
  684. return false;
  685. qom_list = __team_get_qom_list(team, skb->queue_mapping);
  686. list_for_each_entry_rcu(port, qom_list, qom_list) {
  687. if (!team_dev_queue_xmit(team, port, skb))
  688. return true;
  689. }
  690. return false;
  691. }
  692. static void __team_queue_override_port_del(struct team *team,
  693. struct team_port *port)
  694. {
  695. if (!port->queue_id)
  696. return;
  697. list_del_rcu(&port->qom_list);
  698. }
  699. static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
  700. struct team_port *cur)
  701. {
  702. if (port->priority < cur->priority)
  703. return true;
  704. if (port->priority > cur->priority)
  705. return false;
  706. if (port->index < cur->index)
  707. return true;
  708. return false;
  709. }
  710. static void __team_queue_override_port_add(struct team *team,
  711. struct team_port *port)
  712. {
  713. struct team_port *cur;
  714. struct list_head *qom_list;
  715. struct list_head *node;
  716. if (!port->queue_id)
  717. return;
  718. qom_list = __team_get_qom_list(team, port->queue_id);
  719. node = qom_list;
  720. list_for_each_entry(cur, qom_list, qom_list) {
  721. if (team_queue_override_port_has_gt_prio_than(port, cur))
  722. break;
  723. node = &cur->qom_list;
  724. }
  725. list_add_tail_rcu(&port->qom_list, node);
  726. }
  727. static void __team_queue_override_enabled_check(struct team *team)
  728. {
  729. struct team_port *port;
  730. bool enabled = false;
  731. list_for_each_entry(port, &team->port_list, list) {
  732. if (port->queue_id) {
  733. enabled = true;
  734. break;
  735. }
  736. }
  737. if (enabled == team->queue_override_enabled)
  738. return;
  739. netdev_dbg(team->dev, "%s queue override\n",
  740. enabled ? "Enabling" : "Disabling");
  741. team->queue_override_enabled = enabled;
  742. }
  743. static void team_queue_override_port_prio_changed(struct team *team,
  744. struct team_port *port)
  745. {
  746. if (!port->queue_id || team_port_enabled(port))
  747. return;
  748. __team_queue_override_port_del(team, port);
  749. __team_queue_override_port_add(team, port);
  750. __team_queue_override_enabled_check(team);
  751. }
  752. static void team_queue_override_port_change_queue_id(struct team *team,
  753. struct team_port *port,
  754. u16 new_queue_id)
  755. {
  756. if (team_port_enabled(port)) {
  757. __team_queue_override_port_del(team, port);
  758. port->queue_id = new_queue_id;
  759. __team_queue_override_port_add(team, port);
  760. __team_queue_override_enabled_check(team);
  761. } else {
  762. port->queue_id = new_queue_id;
  763. }
  764. }
  765. static void team_queue_override_port_add(struct team *team,
  766. struct team_port *port)
  767. {
  768. __team_queue_override_port_add(team, port);
  769. __team_queue_override_enabled_check(team);
  770. }
  771. static void team_queue_override_port_del(struct team *team,
  772. struct team_port *port)
  773. {
  774. __team_queue_override_port_del(team, port);
  775. __team_queue_override_enabled_check(team);
  776. }
  777. /****************
  778. * Port handling
  779. ****************/
  780. static bool team_port_find(const struct team *team,
  781. const struct team_port *port)
  782. {
  783. struct team_port *cur;
  784. list_for_each_entry(cur, &team->port_list, list)
  785. if (cur == port)
  786. return true;
  787. return false;
  788. }
  789. /*
  790. * Enable/disable port by adding to enabled port hashlist and setting
  791. * port->index (Might be racy so reader could see incorrect ifindex when
  792. * processing a flying packet, but that is not a problem). Write guarded
  793. * by team->lock.
  794. */
  795. static void team_port_enable(struct team *team,
  796. struct team_port *port)
  797. {
  798. if (team_port_enabled(port))
  799. return;
  800. port->index = team->en_port_count++;
  801. hlist_add_head_rcu(&port->hlist,
  802. team_port_index_hash(team, port->index));
  803. team_adjust_ops(team);
  804. team_queue_override_port_add(team, port);
  805. if (team->ops.port_enabled)
  806. team->ops.port_enabled(team, port);
  807. team_notify_peers(team);
  808. team_mcast_rejoin(team);
  809. team_lower_state_changed(port);
  810. }
  811. static void __reconstruct_port_hlist(struct team *team, int rm_index)
  812. {
  813. int i;
  814. struct team_port *port;
  815. for (i = rm_index + 1; i < team->en_port_count; i++) {
  816. port = team_get_port_by_index(team, i);
  817. hlist_del_rcu(&port->hlist);
  818. port->index--;
  819. hlist_add_head_rcu(&port->hlist,
  820. team_port_index_hash(team, port->index));
  821. }
  822. }
  823. static void team_port_disable(struct team *team,
  824. struct team_port *port)
  825. {
  826. if (!team_port_enabled(port))
  827. return;
  828. if (team->ops.port_disabled)
  829. team->ops.port_disabled(team, port);
  830. hlist_del_rcu(&port->hlist);
  831. __reconstruct_port_hlist(team, port->index);
  832. port->index = -1;
  833. team->en_port_count--;
  834. team_queue_override_port_del(team, port);
  835. team_adjust_ops(team);
  836. team_notify_peers(team);
  837. team_mcast_rejoin(team);
  838. team_lower_state_changed(port);
  839. }
  840. #define TEAM_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
  841. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  842. NETIF_F_HIGHDMA | NETIF_F_LRO)
  843. #define TEAM_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
  844. NETIF_F_RXCSUM | NETIF_F_ALL_TSO)
  845. static void ___team_compute_features(struct team *team)
  846. {
  847. struct team_port *port;
  848. netdev_features_t vlan_features = TEAM_VLAN_FEATURES &
  849. NETIF_F_ALL_FOR_ALL;
  850. netdev_features_t enc_features = TEAM_ENC_FEATURES;
  851. unsigned short max_hard_header_len = ETH_HLEN;
  852. unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
  853. IFF_XMIT_DST_RELEASE_PERM;
  854. list_for_each_entry(port, &team->port_list, list) {
  855. vlan_features = netdev_increment_features(vlan_features,
  856. port->dev->vlan_features,
  857. TEAM_VLAN_FEATURES);
  858. enc_features =
  859. netdev_increment_features(enc_features,
  860. port->dev->hw_enc_features,
  861. TEAM_ENC_FEATURES);
  862. dst_release_flag &= port->dev->priv_flags;
  863. if (port->dev->hard_header_len > max_hard_header_len)
  864. max_hard_header_len = port->dev->hard_header_len;
  865. }
  866. team->dev->vlan_features = vlan_features;
  867. team->dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL;
  868. team->dev->hard_header_len = max_hard_header_len;
  869. team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  870. if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
  871. team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
  872. }
  873. static void __team_compute_features(struct team *team)
  874. {
  875. ___team_compute_features(team);
  876. netdev_change_features(team->dev);
  877. }
  878. static void team_compute_features(struct team *team)
  879. {
  880. mutex_lock(&team->lock);
  881. ___team_compute_features(team);
  882. mutex_unlock(&team->lock);
  883. netdev_change_features(team->dev);
  884. }
  885. static int team_port_enter(struct team *team, struct team_port *port)
  886. {
  887. int err = 0;
  888. dev_hold(team->dev);
  889. if (team->ops.port_enter) {
  890. err = team->ops.port_enter(team, port);
  891. if (err) {
  892. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  893. port->dev->name);
  894. goto err_port_enter;
  895. }
  896. }
  897. return 0;
  898. err_port_enter:
  899. dev_put(team->dev);
  900. return err;
  901. }
  902. static void team_port_leave(struct team *team, struct team_port *port)
  903. {
  904. if (team->ops.port_leave)
  905. team->ops.port_leave(team, port);
  906. dev_put(team->dev);
  907. }
  908. #ifdef CONFIG_NET_POLL_CONTROLLER
  909. static int __team_port_enable_netpoll(struct team_port *port)
  910. {
  911. struct netpoll *np;
  912. int err;
  913. np = kzalloc(sizeof(*np), GFP_KERNEL);
  914. if (!np)
  915. return -ENOMEM;
  916. err = __netpoll_setup(np, port->dev);
  917. if (err) {
  918. kfree(np);
  919. return err;
  920. }
  921. port->np = np;
  922. return err;
  923. }
  924. static int team_port_enable_netpoll(struct team_port *port)
  925. {
  926. if (!port->team->dev->npinfo)
  927. return 0;
  928. return __team_port_enable_netpoll(port);
  929. }
  930. static void team_port_disable_netpoll(struct team_port *port)
  931. {
  932. struct netpoll *np = port->np;
  933. if (!np)
  934. return;
  935. port->np = NULL;
  936. /* Wait for transmitting packets to finish before freeing. */
  937. synchronize_rcu_bh();
  938. __netpoll_cleanup(np);
  939. kfree(np);
  940. }
  941. #else
  942. static int team_port_enable_netpoll(struct team_port *port)
  943. {
  944. return 0;
  945. }
  946. static void team_port_disable_netpoll(struct team_port *port)
  947. {
  948. }
  949. #endif
  950. static int team_upper_dev_link(struct team *team, struct team_port *port)
  951. {
  952. struct netdev_lag_upper_info lag_upper_info;
  953. int err;
  954. lag_upper_info.tx_type = team->mode->lag_tx_type;
  955. err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
  956. &lag_upper_info);
  957. if (err)
  958. return err;
  959. port->dev->priv_flags |= IFF_TEAM_PORT;
  960. return 0;
  961. }
  962. static void team_upper_dev_unlink(struct team *team, struct team_port *port)
  963. {
  964. netdev_upper_dev_unlink(port->dev, team->dev);
  965. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  966. }
  967. static void __team_port_change_port_added(struct team_port *port, bool linkup);
  968. static int team_dev_type_check_change(struct net_device *dev,
  969. struct net_device *port_dev);
  970. static int team_port_add(struct team *team, struct net_device *port_dev)
  971. {
  972. struct net_device *dev = team->dev;
  973. struct team_port *port;
  974. char *portname = port_dev->name;
  975. int err;
  976. if (port_dev->flags & IFF_LOOPBACK) {
  977. netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
  978. portname);
  979. return -EINVAL;
  980. }
  981. if (team_port_exists(port_dev)) {
  982. netdev_err(dev, "Device %s is already a port "
  983. "of a team device\n", portname);
  984. return -EBUSY;
  985. }
  986. if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
  987. vlan_uses_dev(dev)) {
  988. netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
  989. portname);
  990. return -EPERM;
  991. }
  992. err = team_dev_type_check_change(dev, port_dev);
  993. if (err)
  994. return err;
  995. if (port_dev->flags & IFF_UP) {
  996. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  997. portname);
  998. return -EBUSY;
  999. }
  1000. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  1001. GFP_KERNEL);
  1002. if (!port)
  1003. return -ENOMEM;
  1004. port->dev = port_dev;
  1005. port->team = team;
  1006. INIT_LIST_HEAD(&port->qom_list);
  1007. port->orig.mtu = port_dev->mtu;
  1008. err = dev_set_mtu(port_dev, dev->mtu);
  1009. if (err) {
  1010. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  1011. goto err_set_mtu;
  1012. }
  1013. memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
  1014. err = team_port_enter(team, port);
  1015. if (err) {
  1016. netdev_err(dev, "Device %s failed to enter team mode\n",
  1017. portname);
  1018. goto err_port_enter;
  1019. }
  1020. err = dev_open(port_dev);
  1021. if (err) {
  1022. netdev_dbg(dev, "Device %s opening failed\n",
  1023. portname);
  1024. goto err_dev_open;
  1025. }
  1026. err = vlan_vids_add_by_dev(port_dev, dev);
  1027. if (err) {
  1028. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  1029. portname);
  1030. goto err_vids_add;
  1031. }
  1032. err = team_port_enable_netpoll(port);
  1033. if (err) {
  1034. netdev_err(dev, "Failed to enable netpoll on device %s\n",
  1035. portname);
  1036. goto err_enable_netpoll;
  1037. }
  1038. if (!(dev->features & NETIF_F_LRO))
  1039. dev_disable_lro(port_dev);
  1040. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  1041. port);
  1042. if (err) {
  1043. netdev_err(dev, "Device %s failed to register rx_handler\n",
  1044. portname);
  1045. goto err_handler_register;
  1046. }
  1047. err = team_upper_dev_link(team, port);
  1048. if (err) {
  1049. netdev_err(dev, "Device %s failed to set upper link\n",
  1050. portname);
  1051. goto err_set_upper_link;
  1052. }
  1053. err = __team_option_inst_add_port(team, port);
  1054. if (err) {
  1055. netdev_err(dev, "Device %s failed to add per-port options\n",
  1056. portname);
  1057. goto err_option_port_add;
  1058. }
  1059. netif_addr_lock_bh(dev);
  1060. dev_uc_sync_multiple(port_dev, dev);
  1061. dev_mc_sync_multiple(port_dev, dev);
  1062. netif_addr_unlock_bh(dev);
  1063. port->index = -1;
  1064. list_add_tail_rcu(&port->list, &team->port_list);
  1065. team_port_enable(team, port);
  1066. __team_compute_features(team);
  1067. __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
  1068. __team_options_change_check(team);
  1069. netdev_info(dev, "Port device %s added\n", portname);
  1070. return 0;
  1071. err_option_port_add:
  1072. team_upper_dev_unlink(team, port);
  1073. err_set_upper_link:
  1074. netdev_rx_handler_unregister(port_dev);
  1075. err_handler_register:
  1076. team_port_disable_netpoll(port);
  1077. err_enable_netpoll:
  1078. vlan_vids_del_by_dev(port_dev, dev);
  1079. err_vids_add:
  1080. dev_close(port_dev);
  1081. err_dev_open:
  1082. team_port_leave(team, port);
  1083. team_port_set_orig_dev_addr(port);
  1084. err_port_enter:
  1085. dev_set_mtu(port_dev, port->orig.mtu);
  1086. err_set_mtu:
  1087. kfree(port);
  1088. return err;
  1089. }
  1090. static void __team_port_change_port_removed(struct team_port *port);
  1091. static int team_port_del(struct team *team, struct net_device *port_dev)
  1092. {
  1093. struct net_device *dev = team->dev;
  1094. struct team_port *port;
  1095. char *portname = port_dev->name;
  1096. port = team_port_get_rtnl(port_dev);
  1097. if (!port || !team_port_find(team, port)) {
  1098. netdev_err(dev, "Device %s does not act as a port of this team\n",
  1099. portname);
  1100. return -ENOENT;
  1101. }
  1102. team_port_disable(team, port);
  1103. list_del_rcu(&port->list);
  1104. team_upper_dev_unlink(team, port);
  1105. netdev_rx_handler_unregister(port_dev);
  1106. team_port_disable_netpoll(port);
  1107. vlan_vids_del_by_dev(port_dev, dev);
  1108. dev_uc_unsync(port_dev, dev);
  1109. dev_mc_unsync(port_dev, dev);
  1110. dev_close(port_dev);
  1111. team_port_leave(team, port);
  1112. __team_option_inst_mark_removed_port(team, port);
  1113. __team_options_change_check(team);
  1114. __team_option_inst_del_port(team, port);
  1115. __team_port_change_port_removed(port);
  1116. team_port_set_orig_dev_addr(port);
  1117. dev_set_mtu(port_dev, port->orig.mtu);
  1118. kfree_rcu(port, rcu);
  1119. netdev_info(dev, "Port device %s removed\n", portname);
  1120. __team_compute_features(team);
  1121. return 0;
  1122. }
  1123. /*****************
  1124. * Net device ops
  1125. *****************/
  1126. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  1127. {
  1128. ctx->data.str_val = team->mode->kind;
  1129. return 0;
  1130. }
  1131. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  1132. {
  1133. return team_change_mode(team, ctx->data.str_val);
  1134. }
  1135. static int team_notify_peers_count_get(struct team *team,
  1136. struct team_gsetter_ctx *ctx)
  1137. {
  1138. ctx->data.u32_val = team->notify_peers.count;
  1139. return 0;
  1140. }
  1141. static int team_notify_peers_count_set(struct team *team,
  1142. struct team_gsetter_ctx *ctx)
  1143. {
  1144. team->notify_peers.count = ctx->data.u32_val;
  1145. return 0;
  1146. }
  1147. static int team_notify_peers_interval_get(struct team *team,
  1148. struct team_gsetter_ctx *ctx)
  1149. {
  1150. ctx->data.u32_val = team->notify_peers.interval;
  1151. return 0;
  1152. }
  1153. static int team_notify_peers_interval_set(struct team *team,
  1154. struct team_gsetter_ctx *ctx)
  1155. {
  1156. team->notify_peers.interval = ctx->data.u32_val;
  1157. return 0;
  1158. }
  1159. static int team_mcast_rejoin_count_get(struct team *team,
  1160. struct team_gsetter_ctx *ctx)
  1161. {
  1162. ctx->data.u32_val = team->mcast_rejoin.count;
  1163. return 0;
  1164. }
  1165. static int team_mcast_rejoin_count_set(struct team *team,
  1166. struct team_gsetter_ctx *ctx)
  1167. {
  1168. team->mcast_rejoin.count = ctx->data.u32_val;
  1169. return 0;
  1170. }
  1171. static int team_mcast_rejoin_interval_get(struct team *team,
  1172. struct team_gsetter_ctx *ctx)
  1173. {
  1174. ctx->data.u32_val = team->mcast_rejoin.interval;
  1175. return 0;
  1176. }
  1177. static int team_mcast_rejoin_interval_set(struct team *team,
  1178. struct team_gsetter_ctx *ctx)
  1179. {
  1180. team->mcast_rejoin.interval = ctx->data.u32_val;
  1181. return 0;
  1182. }
  1183. static int team_port_en_option_get(struct team *team,
  1184. struct team_gsetter_ctx *ctx)
  1185. {
  1186. struct team_port *port = ctx->info->port;
  1187. ctx->data.bool_val = team_port_enabled(port);
  1188. return 0;
  1189. }
  1190. static int team_port_en_option_set(struct team *team,
  1191. struct team_gsetter_ctx *ctx)
  1192. {
  1193. struct team_port *port = ctx->info->port;
  1194. if (ctx->data.bool_val)
  1195. team_port_enable(team, port);
  1196. else
  1197. team_port_disable(team, port);
  1198. return 0;
  1199. }
  1200. static int team_user_linkup_option_get(struct team *team,
  1201. struct team_gsetter_ctx *ctx)
  1202. {
  1203. struct team_port *port = ctx->info->port;
  1204. ctx->data.bool_val = port->user.linkup;
  1205. return 0;
  1206. }
  1207. static void __team_carrier_check(struct team *team);
  1208. static int team_user_linkup_option_set(struct team *team,
  1209. struct team_gsetter_ctx *ctx)
  1210. {
  1211. struct team_port *port = ctx->info->port;
  1212. port->user.linkup = ctx->data.bool_val;
  1213. team_refresh_port_linkup(port);
  1214. __team_carrier_check(port->team);
  1215. return 0;
  1216. }
  1217. static int team_user_linkup_en_option_get(struct team *team,
  1218. struct team_gsetter_ctx *ctx)
  1219. {
  1220. struct team_port *port = ctx->info->port;
  1221. ctx->data.bool_val = port->user.linkup_enabled;
  1222. return 0;
  1223. }
  1224. static int team_user_linkup_en_option_set(struct team *team,
  1225. struct team_gsetter_ctx *ctx)
  1226. {
  1227. struct team_port *port = ctx->info->port;
  1228. port->user.linkup_enabled = ctx->data.bool_val;
  1229. team_refresh_port_linkup(port);
  1230. __team_carrier_check(port->team);
  1231. return 0;
  1232. }
  1233. static int team_priority_option_get(struct team *team,
  1234. struct team_gsetter_ctx *ctx)
  1235. {
  1236. struct team_port *port = ctx->info->port;
  1237. ctx->data.s32_val = port->priority;
  1238. return 0;
  1239. }
  1240. static int team_priority_option_set(struct team *team,
  1241. struct team_gsetter_ctx *ctx)
  1242. {
  1243. struct team_port *port = ctx->info->port;
  1244. s32 priority = ctx->data.s32_val;
  1245. if (port->priority == priority)
  1246. return 0;
  1247. port->priority = priority;
  1248. team_queue_override_port_prio_changed(team, port);
  1249. return 0;
  1250. }
  1251. static int team_queue_id_option_get(struct team *team,
  1252. struct team_gsetter_ctx *ctx)
  1253. {
  1254. struct team_port *port = ctx->info->port;
  1255. ctx->data.u32_val = port->queue_id;
  1256. return 0;
  1257. }
  1258. static int team_queue_id_option_set(struct team *team,
  1259. struct team_gsetter_ctx *ctx)
  1260. {
  1261. struct team_port *port = ctx->info->port;
  1262. u16 new_queue_id = ctx->data.u32_val;
  1263. if (port->queue_id == new_queue_id)
  1264. return 0;
  1265. if (new_queue_id >= team->dev->real_num_tx_queues)
  1266. return -EINVAL;
  1267. team_queue_override_port_change_queue_id(team, port, new_queue_id);
  1268. return 0;
  1269. }
  1270. static const struct team_option team_options[] = {
  1271. {
  1272. .name = "mode",
  1273. .type = TEAM_OPTION_TYPE_STRING,
  1274. .getter = team_mode_option_get,
  1275. .setter = team_mode_option_set,
  1276. },
  1277. {
  1278. .name = "notify_peers_count",
  1279. .type = TEAM_OPTION_TYPE_U32,
  1280. .getter = team_notify_peers_count_get,
  1281. .setter = team_notify_peers_count_set,
  1282. },
  1283. {
  1284. .name = "notify_peers_interval",
  1285. .type = TEAM_OPTION_TYPE_U32,
  1286. .getter = team_notify_peers_interval_get,
  1287. .setter = team_notify_peers_interval_set,
  1288. },
  1289. {
  1290. .name = "mcast_rejoin_count",
  1291. .type = TEAM_OPTION_TYPE_U32,
  1292. .getter = team_mcast_rejoin_count_get,
  1293. .setter = team_mcast_rejoin_count_set,
  1294. },
  1295. {
  1296. .name = "mcast_rejoin_interval",
  1297. .type = TEAM_OPTION_TYPE_U32,
  1298. .getter = team_mcast_rejoin_interval_get,
  1299. .setter = team_mcast_rejoin_interval_set,
  1300. },
  1301. {
  1302. .name = "enabled",
  1303. .type = TEAM_OPTION_TYPE_BOOL,
  1304. .per_port = true,
  1305. .getter = team_port_en_option_get,
  1306. .setter = team_port_en_option_set,
  1307. },
  1308. {
  1309. .name = "user_linkup",
  1310. .type = TEAM_OPTION_TYPE_BOOL,
  1311. .per_port = true,
  1312. .getter = team_user_linkup_option_get,
  1313. .setter = team_user_linkup_option_set,
  1314. },
  1315. {
  1316. .name = "user_linkup_enabled",
  1317. .type = TEAM_OPTION_TYPE_BOOL,
  1318. .per_port = true,
  1319. .getter = team_user_linkup_en_option_get,
  1320. .setter = team_user_linkup_en_option_set,
  1321. },
  1322. {
  1323. .name = "priority",
  1324. .type = TEAM_OPTION_TYPE_S32,
  1325. .per_port = true,
  1326. .getter = team_priority_option_get,
  1327. .setter = team_priority_option_set,
  1328. },
  1329. {
  1330. .name = "queue_id",
  1331. .type = TEAM_OPTION_TYPE_U32,
  1332. .per_port = true,
  1333. .getter = team_queue_id_option_get,
  1334. .setter = team_queue_id_option_set,
  1335. },
  1336. };
  1337. static int team_init(struct net_device *dev)
  1338. {
  1339. struct team *team = netdev_priv(dev);
  1340. int i;
  1341. int err;
  1342. team->dev = dev;
  1343. mutex_init(&team->lock);
  1344. team_set_no_mode(team);
  1345. team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
  1346. if (!team->pcpu_stats)
  1347. return -ENOMEM;
  1348. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1349. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1350. INIT_LIST_HEAD(&team->port_list);
  1351. err = team_queue_override_init(team);
  1352. if (err)
  1353. goto err_team_queue_override_init;
  1354. team_adjust_ops(team);
  1355. INIT_LIST_HEAD(&team->option_list);
  1356. INIT_LIST_HEAD(&team->option_inst_list);
  1357. team_notify_peers_init(team);
  1358. team_mcast_rejoin_init(team);
  1359. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1360. if (err)
  1361. goto err_options_register;
  1362. netif_carrier_off(dev);
  1363. netdev_lockdep_set_classes(dev);
  1364. return 0;
  1365. err_options_register:
  1366. team_mcast_rejoin_fini(team);
  1367. team_notify_peers_fini(team);
  1368. team_queue_override_fini(team);
  1369. err_team_queue_override_init:
  1370. free_percpu(team->pcpu_stats);
  1371. return err;
  1372. }
  1373. static void team_uninit(struct net_device *dev)
  1374. {
  1375. struct team *team = netdev_priv(dev);
  1376. struct team_port *port;
  1377. struct team_port *tmp;
  1378. mutex_lock(&team->lock);
  1379. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1380. team_port_del(team, port->dev);
  1381. __team_change_mode(team, NULL); /* cleanup */
  1382. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1383. team_mcast_rejoin_fini(team);
  1384. team_notify_peers_fini(team);
  1385. team_queue_override_fini(team);
  1386. mutex_unlock(&team->lock);
  1387. }
  1388. static void team_destructor(struct net_device *dev)
  1389. {
  1390. struct team *team = netdev_priv(dev);
  1391. free_percpu(team->pcpu_stats);
  1392. free_netdev(dev);
  1393. }
  1394. static int team_open(struct net_device *dev)
  1395. {
  1396. return 0;
  1397. }
  1398. static int team_close(struct net_device *dev)
  1399. {
  1400. return 0;
  1401. }
  1402. /*
  1403. * note: already called with rcu_read_lock
  1404. */
  1405. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1406. {
  1407. struct team *team = netdev_priv(dev);
  1408. bool tx_success;
  1409. unsigned int len = skb->len;
  1410. tx_success = team_queue_override_transmit(team, skb);
  1411. if (!tx_success)
  1412. tx_success = team->ops.transmit(team, skb);
  1413. if (tx_success) {
  1414. struct team_pcpu_stats *pcpu_stats;
  1415. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1416. u64_stats_update_begin(&pcpu_stats->syncp);
  1417. pcpu_stats->tx_packets++;
  1418. pcpu_stats->tx_bytes += len;
  1419. u64_stats_update_end(&pcpu_stats->syncp);
  1420. } else {
  1421. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1422. }
  1423. return NETDEV_TX_OK;
  1424. }
  1425. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
  1426. void *accel_priv, select_queue_fallback_t fallback)
  1427. {
  1428. /*
  1429. * This helper function exists to help dev_pick_tx get the correct
  1430. * destination queue. Using a helper function skips a call to
  1431. * skb_tx_hash and will put the skbs in the queue we expect on their
  1432. * way down to the team driver.
  1433. */
  1434. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1435. /*
  1436. * Save the original txq to restore before passing to the driver
  1437. */
  1438. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1439. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1440. do {
  1441. txq -= dev->real_num_tx_queues;
  1442. } while (txq >= dev->real_num_tx_queues);
  1443. }
  1444. return txq;
  1445. }
  1446. static void team_change_rx_flags(struct net_device *dev, int change)
  1447. {
  1448. struct team *team = netdev_priv(dev);
  1449. struct team_port *port;
  1450. int inc;
  1451. rcu_read_lock();
  1452. list_for_each_entry_rcu(port, &team->port_list, list) {
  1453. if (change & IFF_PROMISC) {
  1454. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1455. dev_set_promiscuity(port->dev, inc);
  1456. }
  1457. if (change & IFF_ALLMULTI) {
  1458. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1459. dev_set_allmulti(port->dev, inc);
  1460. }
  1461. }
  1462. rcu_read_unlock();
  1463. }
  1464. static void team_set_rx_mode(struct net_device *dev)
  1465. {
  1466. struct team *team = netdev_priv(dev);
  1467. struct team_port *port;
  1468. rcu_read_lock();
  1469. list_for_each_entry_rcu(port, &team->port_list, list) {
  1470. dev_uc_sync_multiple(port->dev, dev);
  1471. dev_mc_sync_multiple(port->dev, dev);
  1472. }
  1473. rcu_read_unlock();
  1474. }
  1475. static int team_set_mac_address(struct net_device *dev, void *p)
  1476. {
  1477. struct sockaddr *addr = p;
  1478. struct team *team = netdev_priv(dev);
  1479. struct team_port *port;
  1480. if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
  1481. return -EADDRNOTAVAIL;
  1482. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1483. mutex_lock(&team->lock);
  1484. list_for_each_entry(port, &team->port_list, list)
  1485. if (team->ops.port_change_dev_addr)
  1486. team->ops.port_change_dev_addr(team, port);
  1487. mutex_unlock(&team->lock);
  1488. return 0;
  1489. }
  1490. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1491. {
  1492. struct team *team = netdev_priv(dev);
  1493. struct team_port *port;
  1494. int err;
  1495. /*
  1496. * Alhough this is reader, it's guarded by team lock. It's not possible
  1497. * to traverse list in reverse under rcu_read_lock
  1498. */
  1499. mutex_lock(&team->lock);
  1500. team->port_mtu_change_allowed = true;
  1501. list_for_each_entry(port, &team->port_list, list) {
  1502. err = dev_set_mtu(port->dev, new_mtu);
  1503. if (err) {
  1504. netdev_err(dev, "Device %s failed to change mtu",
  1505. port->dev->name);
  1506. goto unwind;
  1507. }
  1508. }
  1509. team->port_mtu_change_allowed = false;
  1510. mutex_unlock(&team->lock);
  1511. dev->mtu = new_mtu;
  1512. return 0;
  1513. unwind:
  1514. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1515. dev_set_mtu(port->dev, dev->mtu);
  1516. team->port_mtu_change_allowed = false;
  1517. mutex_unlock(&team->lock);
  1518. return err;
  1519. }
  1520. static struct rtnl_link_stats64 *
  1521. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1522. {
  1523. struct team *team = netdev_priv(dev);
  1524. struct team_pcpu_stats *p;
  1525. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1526. u32 rx_dropped = 0, tx_dropped = 0, rx_nohandler = 0;
  1527. unsigned int start;
  1528. int i;
  1529. for_each_possible_cpu(i) {
  1530. p = per_cpu_ptr(team->pcpu_stats, i);
  1531. do {
  1532. start = u64_stats_fetch_begin_irq(&p->syncp);
  1533. rx_packets = p->rx_packets;
  1534. rx_bytes = p->rx_bytes;
  1535. rx_multicast = p->rx_multicast;
  1536. tx_packets = p->tx_packets;
  1537. tx_bytes = p->tx_bytes;
  1538. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  1539. stats->rx_packets += rx_packets;
  1540. stats->rx_bytes += rx_bytes;
  1541. stats->multicast += rx_multicast;
  1542. stats->tx_packets += tx_packets;
  1543. stats->tx_bytes += tx_bytes;
  1544. /*
  1545. * rx_dropped, tx_dropped & rx_nohandler are u32,
  1546. * updated without syncp protection.
  1547. */
  1548. rx_dropped += p->rx_dropped;
  1549. tx_dropped += p->tx_dropped;
  1550. rx_nohandler += p->rx_nohandler;
  1551. }
  1552. stats->rx_dropped = rx_dropped;
  1553. stats->tx_dropped = tx_dropped;
  1554. stats->rx_nohandler = rx_nohandler;
  1555. return stats;
  1556. }
  1557. static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  1558. {
  1559. struct team *team = netdev_priv(dev);
  1560. struct team_port *port;
  1561. int err;
  1562. /*
  1563. * Alhough this is reader, it's guarded by team lock. It's not possible
  1564. * to traverse list in reverse under rcu_read_lock
  1565. */
  1566. mutex_lock(&team->lock);
  1567. list_for_each_entry(port, &team->port_list, list) {
  1568. err = vlan_vid_add(port->dev, proto, vid);
  1569. if (err)
  1570. goto unwind;
  1571. }
  1572. mutex_unlock(&team->lock);
  1573. return 0;
  1574. unwind:
  1575. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1576. vlan_vid_del(port->dev, proto, vid);
  1577. mutex_unlock(&team->lock);
  1578. return err;
  1579. }
  1580. static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
  1581. {
  1582. struct team *team = netdev_priv(dev);
  1583. struct team_port *port;
  1584. mutex_lock(&team->lock);
  1585. list_for_each_entry(port, &team->port_list, list)
  1586. vlan_vid_del(port->dev, proto, vid);
  1587. mutex_unlock(&team->lock);
  1588. return 0;
  1589. }
  1590. #ifdef CONFIG_NET_POLL_CONTROLLER
  1591. static void team_poll_controller(struct net_device *dev)
  1592. {
  1593. }
  1594. static void __team_netpoll_cleanup(struct team *team)
  1595. {
  1596. struct team_port *port;
  1597. list_for_each_entry(port, &team->port_list, list)
  1598. team_port_disable_netpoll(port);
  1599. }
  1600. static void team_netpoll_cleanup(struct net_device *dev)
  1601. {
  1602. struct team *team = netdev_priv(dev);
  1603. mutex_lock(&team->lock);
  1604. __team_netpoll_cleanup(team);
  1605. mutex_unlock(&team->lock);
  1606. }
  1607. static int team_netpoll_setup(struct net_device *dev,
  1608. struct netpoll_info *npifo)
  1609. {
  1610. struct team *team = netdev_priv(dev);
  1611. struct team_port *port;
  1612. int err = 0;
  1613. mutex_lock(&team->lock);
  1614. list_for_each_entry(port, &team->port_list, list) {
  1615. err = __team_port_enable_netpoll(port);
  1616. if (err) {
  1617. __team_netpoll_cleanup(team);
  1618. break;
  1619. }
  1620. }
  1621. mutex_unlock(&team->lock);
  1622. return err;
  1623. }
  1624. #endif
  1625. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1626. {
  1627. struct team *team = netdev_priv(dev);
  1628. int err;
  1629. mutex_lock(&team->lock);
  1630. err = team_port_add(team, port_dev);
  1631. mutex_unlock(&team->lock);
  1632. return err;
  1633. }
  1634. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1635. {
  1636. struct team *team = netdev_priv(dev);
  1637. int err;
  1638. mutex_lock(&team->lock);
  1639. err = team_port_del(team, port_dev);
  1640. mutex_unlock(&team->lock);
  1641. return err;
  1642. }
  1643. static netdev_features_t team_fix_features(struct net_device *dev,
  1644. netdev_features_t features)
  1645. {
  1646. struct team_port *port;
  1647. struct team *team = netdev_priv(dev);
  1648. netdev_features_t mask;
  1649. mask = features;
  1650. features &= ~NETIF_F_ONE_FOR_ALL;
  1651. features |= NETIF_F_ALL_FOR_ALL;
  1652. rcu_read_lock();
  1653. list_for_each_entry_rcu(port, &team->port_list, list) {
  1654. features = netdev_increment_features(features,
  1655. port->dev->features,
  1656. mask);
  1657. }
  1658. rcu_read_unlock();
  1659. features = netdev_add_tso_features(features, mask);
  1660. return features;
  1661. }
  1662. static int team_change_carrier(struct net_device *dev, bool new_carrier)
  1663. {
  1664. struct team *team = netdev_priv(dev);
  1665. team->user_carrier_enabled = true;
  1666. if (new_carrier)
  1667. netif_carrier_on(dev);
  1668. else
  1669. netif_carrier_off(dev);
  1670. return 0;
  1671. }
  1672. static const struct net_device_ops team_netdev_ops = {
  1673. .ndo_init = team_init,
  1674. .ndo_uninit = team_uninit,
  1675. .ndo_open = team_open,
  1676. .ndo_stop = team_close,
  1677. .ndo_start_xmit = team_xmit,
  1678. .ndo_select_queue = team_select_queue,
  1679. .ndo_change_rx_flags = team_change_rx_flags,
  1680. .ndo_set_rx_mode = team_set_rx_mode,
  1681. .ndo_set_mac_address = team_set_mac_address,
  1682. .ndo_change_mtu = team_change_mtu,
  1683. .ndo_get_stats64 = team_get_stats64,
  1684. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1685. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1686. #ifdef CONFIG_NET_POLL_CONTROLLER
  1687. .ndo_poll_controller = team_poll_controller,
  1688. .ndo_netpoll_setup = team_netpoll_setup,
  1689. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1690. #endif
  1691. .ndo_add_slave = team_add_slave,
  1692. .ndo_del_slave = team_del_slave,
  1693. .ndo_fix_features = team_fix_features,
  1694. .ndo_neigh_construct = netdev_default_l2upper_neigh_construct,
  1695. .ndo_neigh_destroy = netdev_default_l2upper_neigh_destroy,
  1696. .ndo_change_carrier = team_change_carrier,
  1697. .ndo_bridge_setlink = switchdev_port_bridge_setlink,
  1698. .ndo_bridge_getlink = switchdev_port_bridge_getlink,
  1699. .ndo_bridge_dellink = switchdev_port_bridge_dellink,
  1700. .ndo_fdb_add = switchdev_port_fdb_add,
  1701. .ndo_fdb_del = switchdev_port_fdb_del,
  1702. .ndo_fdb_dump = switchdev_port_fdb_dump,
  1703. .ndo_features_check = passthru_features_check,
  1704. };
  1705. /***********************
  1706. * ethtool interface
  1707. ***********************/
  1708. static void team_ethtool_get_drvinfo(struct net_device *dev,
  1709. struct ethtool_drvinfo *drvinfo)
  1710. {
  1711. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  1712. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1713. }
  1714. static const struct ethtool_ops team_ethtool_ops = {
  1715. .get_drvinfo = team_ethtool_get_drvinfo,
  1716. .get_link = ethtool_op_get_link,
  1717. };
  1718. /***********************
  1719. * rt netlink interface
  1720. ***********************/
  1721. static void team_setup_by_port(struct net_device *dev,
  1722. struct net_device *port_dev)
  1723. {
  1724. dev->header_ops = port_dev->header_ops;
  1725. dev->type = port_dev->type;
  1726. dev->hard_header_len = port_dev->hard_header_len;
  1727. dev->addr_len = port_dev->addr_len;
  1728. dev->mtu = port_dev->mtu;
  1729. memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
  1730. eth_hw_addr_inherit(dev, port_dev);
  1731. }
  1732. static int team_dev_type_check_change(struct net_device *dev,
  1733. struct net_device *port_dev)
  1734. {
  1735. struct team *team = netdev_priv(dev);
  1736. char *portname = port_dev->name;
  1737. int err;
  1738. if (dev->type == port_dev->type)
  1739. return 0;
  1740. if (!list_empty(&team->port_list)) {
  1741. netdev_err(dev, "Device %s is of different type\n", portname);
  1742. return -EBUSY;
  1743. }
  1744. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  1745. err = notifier_to_errno(err);
  1746. if (err) {
  1747. netdev_err(dev, "Refused to change device type\n");
  1748. return err;
  1749. }
  1750. dev_uc_flush(dev);
  1751. dev_mc_flush(dev);
  1752. team_setup_by_port(dev, port_dev);
  1753. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  1754. return 0;
  1755. }
  1756. static void team_setup(struct net_device *dev)
  1757. {
  1758. ether_setup(dev);
  1759. dev->netdev_ops = &team_netdev_ops;
  1760. dev->ethtool_ops = &team_ethtool_ops;
  1761. dev->destructor = team_destructor;
  1762. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1763. dev->priv_flags |= IFF_NO_QUEUE;
  1764. dev->priv_flags |= IFF_TEAM;
  1765. /*
  1766. * Indicate we support unicast address filtering. That way core won't
  1767. * bring us to promisc mode in case a unicast addr is added.
  1768. * Let this up to underlay drivers.
  1769. */
  1770. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1771. dev->features |= NETIF_F_LLTX;
  1772. dev->features |= NETIF_F_GRO;
  1773. /* Don't allow team devices to change network namespaces. */
  1774. dev->features |= NETIF_F_NETNS_LOCAL;
  1775. dev->hw_features = TEAM_VLAN_FEATURES |
  1776. NETIF_F_HW_VLAN_CTAG_TX |
  1777. NETIF_F_HW_VLAN_CTAG_RX |
  1778. NETIF_F_HW_VLAN_CTAG_FILTER;
  1779. dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
  1780. dev->features |= dev->hw_features;
  1781. }
  1782. static int team_newlink(struct net *src_net, struct net_device *dev,
  1783. struct nlattr *tb[], struct nlattr *data[])
  1784. {
  1785. if (tb[IFLA_ADDRESS] == NULL)
  1786. eth_hw_addr_random(dev);
  1787. return register_netdevice(dev);
  1788. }
  1789. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1790. {
  1791. if (tb[IFLA_ADDRESS]) {
  1792. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1793. return -EINVAL;
  1794. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1795. return -EADDRNOTAVAIL;
  1796. }
  1797. return 0;
  1798. }
  1799. static unsigned int team_get_num_tx_queues(void)
  1800. {
  1801. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1802. }
  1803. static unsigned int team_get_num_rx_queues(void)
  1804. {
  1805. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1806. }
  1807. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1808. .kind = DRV_NAME,
  1809. .priv_size = sizeof(struct team),
  1810. .setup = team_setup,
  1811. .newlink = team_newlink,
  1812. .validate = team_validate,
  1813. .get_num_tx_queues = team_get_num_tx_queues,
  1814. .get_num_rx_queues = team_get_num_rx_queues,
  1815. };
  1816. /***********************************
  1817. * Generic netlink custom interface
  1818. ***********************************/
  1819. static struct genl_family team_nl_family = {
  1820. .id = GENL_ID_GENERATE,
  1821. .name = TEAM_GENL_NAME,
  1822. .version = TEAM_GENL_VERSION,
  1823. .maxattr = TEAM_ATTR_MAX,
  1824. .netnsok = true,
  1825. };
  1826. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1827. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1828. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1829. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1830. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1831. };
  1832. static const struct nla_policy
  1833. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1834. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1835. [TEAM_ATTR_OPTION_NAME] = {
  1836. .type = NLA_STRING,
  1837. .len = TEAM_STRING_MAX_LEN,
  1838. },
  1839. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1840. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1841. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1842. };
  1843. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1844. {
  1845. struct sk_buff *msg;
  1846. void *hdr;
  1847. int err;
  1848. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1849. if (!msg)
  1850. return -ENOMEM;
  1851. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1852. &team_nl_family, 0, TEAM_CMD_NOOP);
  1853. if (!hdr) {
  1854. err = -EMSGSIZE;
  1855. goto err_msg_put;
  1856. }
  1857. genlmsg_end(msg, hdr);
  1858. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  1859. err_msg_put:
  1860. nlmsg_free(msg);
  1861. return err;
  1862. }
  1863. /*
  1864. * Netlink cmd functions should be locked by following two functions.
  1865. * Since dev gets held here, that ensures dev won't disappear in between.
  1866. */
  1867. static struct team *team_nl_team_get(struct genl_info *info)
  1868. {
  1869. struct net *net = genl_info_net(info);
  1870. int ifindex;
  1871. struct net_device *dev;
  1872. struct team *team;
  1873. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1874. return NULL;
  1875. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1876. dev = dev_get_by_index(net, ifindex);
  1877. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1878. if (dev)
  1879. dev_put(dev);
  1880. return NULL;
  1881. }
  1882. team = netdev_priv(dev);
  1883. mutex_lock(&team->lock);
  1884. return team;
  1885. }
  1886. static void team_nl_team_put(struct team *team)
  1887. {
  1888. mutex_unlock(&team->lock);
  1889. dev_put(team->dev);
  1890. }
  1891. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1892. struct team *team, u32 portid);
  1893. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
  1894. {
  1895. return genlmsg_unicast(dev_net(team->dev), skb, portid);
  1896. }
  1897. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1898. struct team_option_inst *opt_inst)
  1899. {
  1900. struct nlattr *option_item;
  1901. struct team_option *option = opt_inst->option;
  1902. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1903. struct team_gsetter_ctx ctx;
  1904. int err;
  1905. ctx.info = opt_inst_info;
  1906. err = team_option_get(team, opt_inst, &ctx);
  1907. if (err)
  1908. return err;
  1909. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1910. if (!option_item)
  1911. return -EMSGSIZE;
  1912. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1913. goto nest_cancel;
  1914. if (opt_inst_info->port &&
  1915. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1916. opt_inst_info->port->dev->ifindex))
  1917. goto nest_cancel;
  1918. if (opt_inst->option->array_size &&
  1919. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1920. opt_inst_info->array_index))
  1921. goto nest_cancel;
  1922. switch (option->type) {
  1923. case TEAM_OPTION_TYPE_U32:
  1924. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1925. goto nest_cancel;
  1926. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1927. goto nest_cancel;
  1928. break;
  1929. case TEAM_OPTION_TYPE_STRING:
  1930. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1931. goto nest_cancel;
  1932. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1933. ctx.data.str_val))
  1934. goto nest_cancel;
  1935. break;
  1936. case TEAM_OPTION_TYPE_BINARY:
  1937. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1938. goto nest_cancel;
  1939. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1940. ctx.data.bin_val.ptr))
  1941. goto nest_cancel;
  1942. break;
  1943. case TEAM_OPTION_TYPE_BOOL:
  1944. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1945. goto nest_cancel;
  1946. if (ctx.data.bool_val &&
  1947. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1948. goto nest_cancel;
  1949. break;
  1950. case TEAM_OPTION_TYPE_S32:
  1951. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1952. goto nest_cancel;
  1953. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1954. goto nest_cancel;
  1955. break;
  1956. default:
  1957. BUG();
  1958. }
  1959. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1960. goto nest_cancel;
  1961. if (opt_inst->changed) {
  1962. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1963. goto nest_cancel;
  1964. opt_inst->changed = false;
  1965. }
  1966. nla_nest_end(skb, option_item);
  1967. return 0;
  1968. nest_cancel:
  1969. nla_nest_cancel(skb, option_item);
  1970. return -EMSGSIZE;
  1971. }
  1972. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1973. struct team *team, u32 portid,
  1974. team_nl_send_func_t *send_func)
  1975. {
  1976. int err;
  1977. if (*pskb) {
  1978. err = send_func(*pskb, team, portid);
  1979. if (err)
  1980. return err;
  1981. }
  1982. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1983. if (!*pskb)
  1984. return -ENOMEM;
  1985. return 0;
  1986. }
  1987. static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
  1988. int flags, team_nl_send_func_t *send_func,
  1989. struct list_head *sel_opt_inst_list)
  1990. {
  1991. struct nlattr *option_list;
  1992. struct nlmsghdr *nlh;
  1993. void *hdr;
  1994. struct team_option_inst *opt_inst;
  1995. int err;
  1996. struct sk_buff *skb = NULL;
  1997. bool incomplete;
  1998. int i;
  1999. opt_inst = list_first_entry(sel_opt_inst_list,
  2000. struct team_option_inst, tmp_list);
  2001. start_again:
  2002. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2003. if (err)
  2004. return err;
  2005. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2006. TEAM_CMD_OPTIONS_GET);
  2007. if (!hdr) {
  2008. nlmsg_free(skb);
  2009. return -EMSGSIZE;
  2010. }
  2011. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2012. goto nla_put_failure;
  2013. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  2014. if (!option_list)
  2015. goto nla_put_failure;
  2016. i = 0;
  2017. incomplete = false;
  2018. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  2019. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  2020. if (err) {
  2021. if (err == -EMSGSIZE) {
  2022. if (!i)
  2023. goto errout;
  2024. incomplete = true;
  2025. break;
  2026. }
  2027. goto errout;
  2028. }
  2029. i++;
  2030. }
  2031. nla_nest_end(skb, option_list);
  2032. genlmsg_end(skb, hdr);
  2033. if (incomplete)
  2034. goto start_again;
  2035. send_done:
  2036. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2037. if (!nlh) {
  2038. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2039. if (err)
  2040. return err;
  2041. goto send_done;
  2042. }
  2043. return send_func(skb, team, portid);
  2044. nla_put_failure:
  2045. err = -EMSGSIZE;
  2046. errout:
  2047. genlmsg_cancel(skb, hdr);
  2048. nlmsg_free(skb);
  2049. return err;
  2050. }
  2051. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  2052. {
  2053. struct team *team;
  2054. struct team_option_inst *opt_inst;
  2055. int err;
  2056. LIST_HEAD(sel_opt_inst_list);
  2057. team = team_nl_team_get(info);
  2058. if (!team)
  2059. return -EINVAL;
  2060. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  2061. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2062. err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
  2063. NLM_F_ACK, team_nl_send_unicast,
  2064. &sel_opt_inst_list);
  2065. team_nl_team_put(team);
  2066. return err;
  2067. }
  2068. static int team_nl_send_event_options_get(struct team *team,
  2069. struct list_head *sel_opt_inst_list);
  2070. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  2071. {
  2072. struct team *team;
  2073. int err = 0;
  2074. int i;
  2075. struct nlattr *nl_option;
  2076. LIST_HEAD(opt_inst_list);
  2077. rtnl_lock();
  2078. team = team_nl_team_get(info);
  2079. if (!team) {
  2080. err = -EINVAL;
  2081. goto rtnl_unlock;
  2082. }
  2083. err = -EINVAL;
  2084. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  2085. err = -EINVAL;
  2086. goto team_put;
  2087. }
  2088. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  2089. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  2090. struct nlattr *attr;
  2091. struct nlattr *attr_data;
  2092. enum team_option_type opt_type;
  2093. int opt_port_ifindex = 0; /* != 0 for per-port options */
  2094. u32 opt_array_index = 0;
  2095. bool opt_is_array = false;
  2096. struct team_option_inst *opt_inst;
  2097. char *opt_name;
  2098. bool opt_found = false;
  2099. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  2100. err = -EINVAL;
  2101. goto team_put;
  2102. }
  2103. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  2104. nl_option, team_nl_option_policy);
  2105. if (err)
  2106. goto team_put;
  2107. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  2108. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  2109. err = -EINVAL;
  2110. goto team_put;
  2111. }
  2112. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  2113. case NLA_U32:
  2114. opt_type = TEAM_OPTION_TYPE_U32;
  2115. break;
  2116. case NLA_STRING:
  2117. opt_type = TEAM_OPTION_TYPE_STRING;
  2118. break;
  2119. case NLA_BINARY:
  2120. opt_type = TEAM_OPTION_TYPE_BINARY;
  2121. break;
  2122. case NLA_FLAG:
  2123. opt_type = TEAM_OPTION_TYPE_BOOL;
  2124. break;
  2125. case NLA_S32:
  2126. opt_type = TEAM_OPTION_TYPE_S32;
  2127. break;
  2128. default:
  2129. goto team_put;
  2130. }
  2131. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  2132. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  2133. err = -EINVAL;
  2134. goto team_put;
  2135. }
  2136. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  2137. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  2138. if (attr)
  2139. opt_port_ifindex = nla_get_u32(attr);
  2140. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  2141. if (attr) {
  2142. opt_is_array = true;
  2143. opt_array_index = nla_get_u32(attr);
  2144. }
  2145. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2146. struct team_option *option = opt_inst->option;
  2147. struct team_gsetter_ctx ctx;
  2148. struct team_option_inst_info *opt_inst_info;
  2149. int tmp_ifindex;
  2150. opt_inst_info = &opt_inst->info;
  2151. tmp_ifindex = opt_inst_info->port ?
  2152. opt_inst_info->port->dev->ifindex : 0;
  2153. if (option->type != opt_type ||
  2154. strcmp(option->name, opt_name) ||
  2155. tmp_ifindex != opt_port_ifindex ||
  2156. (option->array_size && !opt_is_array) ||
  2157. opt_inst_info->array_index != opt_array_index)
  2158. continue;
  2159. opt_found = true;
  2160. ctx.info = opt_inst_info;
  2161. switch (opt_type) {
  2162. case TEAM_OPTION_TYPE_U32:
  2163. ctx.data.u32_val = nla_get_u32(attr_data);
  2164. break;
  2165. case TEAM_OPTION_TYPE_STRING:
  2166. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  2167. err = -EINVAL;
  2168. goto team_put;
  2169. }
  2170. ctx.data.str_val = nla_data(attr_data);
  2171. break;
  2172. case TEAM_OPTION_TYPE_BINARY:
  2173. ctx.data.bin_val.len = nla_len(attr_data);
  2174. ctx.data.bin_val.ptr = nla_data(attr_data);
  2175. break;
  2176. case TEAM_OPTION_TYPE_BOOL:
  2177. ctx.data.bool_val = attr_data ? true : false;
  2178. break;
  2179. case TEAM_OPTION_TYPE_S32:
  2180. ctx.data.s32_val = nla_get_s32(attr_data);
  2181. break;
  2182. default:
  2183. BUG();
  2184. }
  2185. err = team_option_set(team, opt_inst, &ctx);
  2186. if (err)
  2187. goto team_put;
  2188. opt_inst->changed = true;
  2189. /* dumb/evil user-space can send us duplicate opt,
  2190. * keep only the last one
  2191. */
  2192. if (__team_option_inst_tmp_find(&opt_inst_list,
  2193. opt_inst))
  2194. continue;
  2195. list_add(&opt_inst->tmp_list, &opt_inst_list);
  2196. }
  2197. if (!opt_found) {
  2198. err = -ENOENT;
  2199. goto team_put;
  2200. }
  2201. }
  2202. err = team_nl_send_event_options_get(team, &opt_inst_list);
  2203. team_put:
  2204. team_nl_team_put(team);
  2205. rtnl_unlock:
  2206. rtnl_unlock();
  2207. return err;
  2208. }
  2209. static int team_nl_fill_one_port_get(struct sk_buff *skb,
  2210. struct team_port *port)
  2211. {
  2212. struct nlattr *port_item;
  2213. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  2214. if (!port_item)
  2215. goto nest_cancel;
  2216. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  2217. goto nest_cancel;
  2218. if (port->changed) {
  2219. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  2220. goto nest_cancel;
  2221. port->changed = false;
  2222. }
  2223. if ((port->removed &&
  2224. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  2225. (port->state.linkup &&
  2226. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  2227. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  2228. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  2229. goto nest_cancel;
  2230. nla_nest_end(skb, port_item);
  2231. return 0;
  2232. nest_cancel:
  2233. nla_nest_cancel(skb, port_item);
  2234. return -EMSGSIZE;
  2235. }
  2236. static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
  2237. int flags, team_nl_send_func_t *send_func,
  2238. struct team_port *one_port)
  2239. {
  2240. struct nlattr *port_list;
  2241. struct nlmsghdr *nlh;
  2242. void *hdr;
  2243. struct team_port *port;
  2244. int err;
  2245. struct sk_buff *skb = NULL;
  2246. bool incomplete;
  2247. int i;
  2248. port = list_first_entry_or_null(&team->port_list,
  2249. struct team_port, list);
  2250. start_again:
  2251. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2252. if (err)
  2253. return err;
  2254. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2255. TEAM_CMD_PORT_LIST_GET);
  2256. if (!hdr) {
  2257. nlmsg_free(skb);
  2258. return -EMSGSIZE;
  2259. }
  2260. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2261. goto nla_put_failure;
  2262. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  2263. if (!port_list)
  2264. goto nla_put_failure;
  2265. i = 0;
  2266. incomplete = false;
  2267. /* If one port is selected, called wants to send port list containing
  2268. * only this port. Otherwise go through all listed ports and send all
  2269. */
  2270. if (one_port) {
  2271. err = team_nl_fill_one_port_get(skb, one_port);
  2272. if (err)
  2273. goto errout;
  2274. } else if (port) {
  2275. list_for_each_entry_from(port, &team->port_list, list) {
  2276. err = team_nl_fill_one_port_get(skb, port);
  2277. if (err) {
  2278. if (err == -EMSGSIZE) {
  2279. if (!i)
  2280. goto errout;
  2281. incomplete = true;
  2282. break;
  2283. }
  2284. goto errout;
  2285. }
  2286. i++;
  2287. }
  2288. }
  2289. nla_nest_end(skb, port_list);
  2290. genlmsg_end(skb, hdr);
  2291. if (incomplete)
  2292. goto start_again;
  2293. send_done:
  2294. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2295. if (!nlh) {
  2296. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2297. if (err)
  2298. return err;
  2299. goto send_done;
  2300. }
  2301. return send_func(skb, team, portid);
  2302. nla_put_failure:
  2303. err = -EMSGSIZE;
  2304. errout:
  2305. genlmsg_cancel(skb, hdr);
  2306. nlmsg_free(skb);
  2307. return err;
  2308. }
  2309. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  2310. struct genl_info *info)
  2311. {
  2312. struct team *team;
  2313. int err;
  2314. team = team_nl_team_get(info);
  2315. if (!team)
  2316. return -EINVAL;
  2317. err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
  2318. NLM_F_ACK, team_nl_send_unicast, NULL);
  2319. team_nl_team_put(team);
  2320. return err;
  2321. }
  2322. static const struct genl_ops team_nl_ops[] = {
  2323. {
  2324. .cmd = TEAM_CMD_NOOP,
  2325. .doit = team_nl_cmd_noop,
  2326. .policy = team_nl_policy,
  2327. },
  2328. {
  2329. .cmd = TEAM_CMD_OPTIONS_SET,
  2330. .doit = team_nl_cmd_options_set,
  2331. .policy = team_nl_policy,
  2332. .flags = GENL_ADMIN_PERM,
  2333. },
  2334. {
  2335. .cmd = TEAM_CMD_OPTIONS_GET,
  2336. .doit = team_nl_cmd_options_get,
  2337. .policy = team_nl_policy,
  2338. .flags = GENL_ADMIN_PERM,
  2339. },
  2340. {
  2341. .cmd = TEAM_CMD_PORT_LIST_GET,
  2342. .doit = team_nl_cmd_port_list_get,
  2343. .policy = team_nl_policy,
  2344. .flags = GENL_ADMIN_PERM,
  2345. },
  2346. };
  2347. static const struct genl_multicast_group team_nl_mcgrps[] = {
  2348. { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
  2349. };
  2350. static int team_nl_send_multicast(struct sk_buff *skb,
  2351. struct team *team, u32 portid)
  2352. {
  2353. return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
  2354. skb, 0, 0, GFP_KERNEL);
  2355. }
  2356. static int team_nl_send_event_options_get(struct team *team,
  2357. struct list_head *sel_opt_inst_list)
  2358. {
  2359. return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
  2360. sel_opt_inst_list);
  2361. }
  2362. static int team_nl_send_event_port_get(struct team *team,
  2363. struct team_port *port)
  2364. {
  2365. return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
  2366. port);
  2367. }
  2368. static int team_nl_init(void)
  2369. {
  2370. return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
  2371. team_nl_mcgrps);
  2372. }
  2373. static void team_nl_fini(void)
  2374. {
  2375. genl_unregister_family(&team_nl_family);
  2376. }
  2377. /******************
  2378. * Change checkers
  2379. ******************/
  2380. static void __team_options_change_check(struct team *team)
  2381. {
  2382. int err;
  2383. struct team_option_inst *opt_inst;
  2384. LIST_HEAD(sel_opt_inst_list);
  2385. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2386. if (opt_inst->changed)
  2387. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2388. }
  2389. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2390. if (err && err != -ESRCH)
  2391. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2392. err);
  2393. }
  2394. /* rtnl lock is held */
  2395. static void __team_port_change_send(struct team_port *port, bool linkup)
  2396. {
  2397. int err;
  2398. port->changed = true;
  2399. port->state.linkup = linkup;
  2400. team_refresh_port_linkup(port);
  2401. if (linkup) {
  2402. struct ethtool_link_ksettings ecmd;
  2403. err = __ethtool_get_link_ksettings(port->dev, &ecmd);
  2404. if (!err) {
  2405. port->state.speed = ecmd.base.speed;
  2406. port->state.duplex = ecmd.base.duplex;
  2407. goto send_event;
  2408. }
  2409. }
  2410. port->state.speed = 0;
  2411. port->state.duplex = 0;
  2412. send_event:
  2413. err = team_nl_send_event_port_get(port->team, port);
  2414. if (err && err != -ESRCH)
  2415. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
  2416. port->dev->name, err);
  2417. }
  2418. static void __team_carrier_check(struct team *team)
  2419. {
  2420. struct team_port *port;
  2421. bool team_linkup;
  2422. if (team->user_carrier_enabled)
  2423. return;
  2424. team_linkup = false;
  2425. list_for_each_entry(port, &team->port_list, list) {
  2426. if (port->linkup) {
  2427. team_linkup = true;
  2428. break;
  2429. }
  2430. }
  2431. if (team_linkup)
  2432. netif_carrier_on(team->dev);
  2433. else
  2434. netif_carrier_off(team->dev);
  2435. }
  2436. static void __team_port_change_check(struct team_port *port, bool linkup)
  2437. {
  2438. if (port->state.linkup != linkup)
  2439. __team_port_change_send(port, linkup);
  2440. __team_carrier_check(port->team);
  2441. }
  2442. static void __team_port_change_port_added(struct team_port *port, bool linkup)
  2443. {
  2444. __team_port_change_send(port, linkup);
  2445. __team_carrier_check(port->team);
  2446. }
  2447. static void __team_port_change_port_removed(struct team_port *port)
  2448. {
  2449. port->removed = true;
  2450. __team_port_change_send(port, false);
  2451. __team_carrier_check(port->team);
  2452. }
  2453. static void team_port_change_check(struct team_port *port, bool linkup)
  2454. {
  2455. struct team *team = port->team;
  2456. mutex_lock(&team->lock);
  2457. __team_port_change_check(port, linkup);
  2458. mutex_unlock(&team->lock);
  2459. }
  2460. /************************************
  2461. * Net device notifier event handler
  2462. ************************************/
  2463. static int team_device_event(struct notifier_block *unused,
  2464. unsigned long event, void *ptr)
  2465. {
  2466. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2467. struct team_port *port;
  2468. port = team_port_get_rtnl(dev);
  2469. if (!port)
  2470. return NOTIFY_DONE;
  2471. switch (event) {
  2472. case NETDEV_UP:
  2473. if (netif_carrier_ok(dev))
  2474. team_port_change_check(port, true);
  2475. break;
  2476. case NETDEV_DOWN:
  2477. team_port_change_check(port, false);
  2478. break;
  2479. case NETDEV_CHANGE:
  2480. if (netif_running(port->dev))
  2481. team_port_change_check(port,
  2482. !!netif_carrier_ok(port->dev));
  2483. break;
  2484. case NETDEV_UNREGISTER:
  2485. team_del_slave(port->team->dev, dev);
  2486. break;
  2487. case NETDEV_FEAT_CHANGE:
  2488. team_compute_features(port->team);
  2489. break;
  2490. case NETDEV_PRECHANGEMTU:
  2491. /* Forbid to change mtu of underlaying device */
  2492. if (!port->team->port_mtu_change_allowed)
  2493. return NOTIFY_BAD;
  2494. break;
  2495. case NETDEV_PRE_TYPE_CHANGE:
  2496. /* Forbid to change type of underlaying device */
  2497. return NOTIFY_BAD;
  2498. case NETDEV_RESEND_IGMP:
  2499. /* Propagate to master device */
  2500. call_netdevice_notifiers(event, port->team->dev);
  2501. break;
  2502. }
  2503. return NOTIFY_DONE;
  2504. }
  2505. static struct notifier_block team_notifier_block __read_mostly = {
  2506. .notifier_call = team_device_event,
  2507. };
  2508. /***********************
  2509. * Module init and exit
  2510. ***********************/
  2511. static int __init team_module_init(void)
  2512. {
  2513. int err;
  2514. register_netdevice_notifier(&team_notifier_block);
  2515. err = rtnl_link_register(&team_link_ops);
  2516. if (err)
  2517. goto err_rtnl_reg;
  2518. err = team_nl_init();
  2519. if (err)
  2520. goto err_nl_init;
  2521. return 0;
  2522. err_nl_init:
  2523. rtnl_link_unregister(&team_link_ops);
  2524. err_rtnl_reg:
  2525. unregister_netdevice_notifier(&team_notifier_block);
  2526. return err;
  2527. }
  2528. static void __exit team_module_exit(void)
  2529. {
  2530. team_nl_fini();
  2531. rtnl_link_unregister(&team_link_ops);
  2532. unregister_netdevice_notifier(&team_notifier_block);
  2533. }
  2534. module_init(team_module_init);
  2535. module_exit(team_module_exit);
  2536. MODULE_LICENSE("GPL v2");
  2537. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2538. MODULE_DESCRIPTION("Ethernet team device driver");
  2539. MODULE_ALIAS_RTNL_LINK(DRV_NAME);