team.c 70 KB

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