team.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984
  1. /*
  2. * drivers/net/team/team.c - Network team device driver
  3. * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/errno.h>
  17. #include <linux/ctype.h>
  18. #include <linux/notifier.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/netpoll.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/socket.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/rtnetlink.h>
  26. #include <net/rtnetlink.h>
  27. #include <net/genetlink.h>
  28. #include <net/netlink.h>
  29. #include <net/sch_generic.h>
  30. #include <net/switchdev.h>
  31. #include <generated/utsrelease.h>
  32. #include <linux/if_team.h>
  33. #define DRV_NAME "team"
  34. /**********
  35. * Helpers
  36. **********/
  37. #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
  38. static struct team_port *team_port_get_rcu(const struct net_device *dev)
  39. {
  40. return rcu_dereference(dev->rx_handler_data);
  41. }
  42. static struct team_port *team_port_get_rtnl(const struct net_device *dev)
  43. {
  44. struct team_port *port = rtnl_dereference(dev->rx_handler_data);
  45. return team_port_exists(dev) ? port : NULL;
  46. }
  47. /*
  48. * Since the ability to change device address for open port device is tested in
  49. * team_port_add, this function can be called without control of return value
  50. */
  51. static int __set_port_dev_addr(struct net_device *port_dev,
  52. const unsigned char *dev_addr)
  53. {
  54. struct sockaddr addr;
  55. memcpy(addr.sa_data, dev_addr, port_dev->addr_len);
  56. addr.sa_family = port_dev->type;
  57. return dev_set_mac_address(port_dev, &addr);
  58. }
  59. static int team_port_set_orig_dev_addr(struct team_port *port)
  60. {
  61. return __set_port_dev_addr(port->dev, port->orig.dev_addr);
  62. }
  63. static int team_port_set_team_dev_addr(struct team *team,
  64. struct team_port *port)
  65. {
  66. return __set_port_dev_addr(port->dev, team->dev->dev_addr);
  67. }
  68. int team_modeop_port_enter(struct team *team, struct team_port *port)
  69. {
  70. return team_port_set_team_dev_addr(team, port);
  71. }
  72. EXPORT_SYMBOL(team_modeop_port_enter);
  73. void team_modeop_port_change_dev_addr(struct team *team,
  74. struct team_port *port)
  75. {
  76. team_port_set_team_dev_addr(team, port);
  77. }
  78. EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
  79. static void team_lower_state_changed(struct team_port *port)
  80. {
  81. struct netdev_lag_lower_state_info info;
  82. info.link_up = port->linkup;
  83. info.tx_enabled = team_port_enabled(port);
  84. netdev_lower_state_changed(port->dev, &info);
  85. }
  86. static void team_refresh_port_linkup(struct team_port *port)
  87. {
  88. bool new_linkup = port->user.linkup_enabled ? port->user.linkup :
  89. port->state.linkup;
  90. if (port->linkup != new_linkup) {
  91. port->linkup = new_linkup;
  92. team_lower_state_changed(port);
  93. }
  94. }
  95. /*******************
  96. * Options handling
  97. *******************/
  98. struct team_option_inst { /* One for each option instance */
  99. struct list_head list;
  100. struct list_head tmp_list;
  101. struct team_option *option;
  102. struct team_option_inst_info info;
  103. bool changed;
  104. bool removed;
  105. };
  106. static struct team_option *__team_find_option(struct team *team,
  107. const char *opt_name)
  108. {
  109. struct team_option *option;
  110. list_for_each_entry(option, &team->option_list, list) {
  111. if (strcmp(option->name, opt_name) == 0)
  112. return option;
  113. }
  114. return NULL;
  115. }
  116. static void __team_option_inst_del(struct team_option_inst *opt_inst)
  117. {
  118. list_del(&opt_inst->list);
  119. kfree(opt_inst);
  120. }
  121. static void __team_option_inst_del_option(struct team *team,
  122. struct team_option *option)
  123. {
  124. struct team_option_inst *opt_inst, *tmp;
  125. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  126. if (opt_inst->option == option)
  127. __team_option_inst_del(opt_inst);
  128. }
  129. }
  130. static int __team_option_inst_add(struct team *team, struct team_option *option,
  131. struct team_port *port)
  132. {
  133. struct team_option_inst *opt_inst;
  134. unsigned int array_size;
  135. unsigned int i;
  136. int err;
  137. array_size = option->array_size;
  138. if (!array_size)
  139. array_size = 1; /* No array but still need one instance */
  140. for (i = 0; i < array_size; i++) {
  141. opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
  142. if (!opt_inst)
  143. return -ENOMEM;
  144. opt_inst->option = option;
  145. opt_inst->info.port = port;
  146. opt_inst->info.array_index = i;
  147. opt_inst->changed = true;
  148. opt_inst->removed = false;
  149. list_add_tail(&opt_inst->list, &team->option_inst_list);
  150. if (option->init) {
  151. err = option->init(team, &opt_inst->info);
  152. if (err)
  153. return err;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int __team_option_inst_add_option(struct team *team,
  159. struct team_option *option)
  160. {
  161. int err;
  162. if (!option->per_port) {
  163. err = __team_option_inst_add(team, option, NULL);
  164. if (err)
  165. goto inst_del_option;
  166. }
  167. return 0;
  168. inst_del_option:
  169. __team_option_inst_del_option(team, option);
  170. return err;
  171. }
  172. static void __team_option_inst_mark_removed_option(struct team *team,
  173. struct team_option *option)
  174. {
  175. struct team_option_inst *opt_inst;
  176. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  177. if (opt_inst->option == option) {
  178. opt_inst->changed = true;
  179. opt_inst->removed = true;
  180. }
  181. }
  182. }
  183. static void __team_option_inst_del_port(struct team *team,
  184. struct team_port *port)
  185. {
  186. struct team_option_inst *opt_inst, *tmp;
  187. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  188. if (opt_inst->option->per_port &&
  189. opt_inst->info.port == port)
  190. __team_option_inst_del(opt_inst);
  191. }
  192. }
  193. static int __team_option_inst_add_port(struct team *team,
  194. struct team_port *port)
  195. {
  196. struct team_option *option;
  197. int err;
  198. list_for_each_entry(option, &team->option_list, list) {
  199. if (!option->per_port)
  200. continue;
  201. err = __team_option_inst_add(team, option, port);
  202. if (err)
  203. goto inst_del_port;
  204. }
  205. return 0;
  206. inst_del_port:
  207. __team_option_inst_del_port(team, port);
  208. return err;
  209. }
  210. static void __team_option_inst_mark_removed_port(struct team *team,
  211. struct team_port *port)
  212. {
  213. struct team_option_inst *opt_inst;
  214. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  215. if (opt_inst->info.port == port) {
  216. opt_inst->changed = true;
  217. opt_inst->removed = true;
  218. }
  219. }
  220. }
  221. static 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. ___team_compute_features(team);
  866. netdev_change_features(team->dev);
  867. }
  868. static void team_compute_features(struct team *team)
  869. {
  870. mutex_lock(&team->lock);
  871. ___team_compute_features(team);
  872. mutex_unlock(&team->lock);
  873. netdev_change_features(team->dev);
  874. }
  875. static int team_port_enter(struct team *team, struct team_port *port)
  876. {
  877. int err = 0;
  878. dev_hold(team->dev);
  879. if (team->ops.port_enter) {
  880. err = team->ops.port_enter(team, port);
  881. if (err) {
  882. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  883. port->dev->name);
  884. goto err_port_enter;
  885. }
  886. }
  887. return 0;
  888. err_port_enter:
  889. dev_put(team->dev);
  890. return err;
  891. }
  892. static void team_port_leave(struct team *team, struct team_port *port)
  893. {
  894. if (team->ops.port_leave)
  895. team->ops.port_leave(team, port);
  896. dev_put(team->dev);
  897. }
  898. #ifdef CONFIG_NET_POLL_CONTROLLER
  899. static int team_port_enable_netpoll(struct team *team, struct team_port *port)
  900. {
  901. struct netpoll *np;
  902. int err;
  903. if (!team->dev->npinfo)
  904. return 0;
  905. np = kzalloc(sizeof(*np), GFP_KERNEL);
  906. if (!np)
  907. return -ENOMEM;
  908. err = __netpoll_setup(np, port->dev);
  909. if (err) {
  910. kfree(np);
  911. return err;
  912. }
  913. port->np = np;
  914. return err;
  915. }
  916. static void team_port_disable_netpoll(struct team_port *port)
  917. {
  918. struct netpoll *np = port->np;
  919. if (!np)
  920. return;
  921. port->np = NULL;
  922. /* Wait for transmitting packets to finish before freeing. */
  923. synchronize_rcu_bh();
  924. __netpoll_cleanup(np);
  925. kfree(np);
  926. }
  927. #else
  928. static int team_port_enable_netpoll(struct team *team, struct team_port *port)
  929. {
  930. return 0;
  931. }
  932. static void team_port_disable_netpoll(struct team_port *port)
  933. {
  934. }
  935. #endif
  936. static int team_upper_dev_link(struct team *team, struct team_port *port)
  937. {
  938. struct netdev_lag_upper_info lag_upper_info;
  939. int err;
  940. lag_upper_info.tx_type = team->mode->lag_tx_type;
  941. err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
  942. &lag_upper_info);
  943. if (err)
  944. return err;
  945. port->dev->priv_flags |= IFF_TEAM_PORT;
  946. return 0;
  947. }
  948. static void team_upper_dev_unlink(struct team *team, struct team_port *port)
  949. {
  950. netdev_upper_dev_unlink(port->dev, team->dev);
  951. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  952. }
  953. static void __team_port_change_port_added(struct team_port *port, bool linkup);
  954. static int team_dev_type_check_change(struct net_device *dev,
  955. struct net_device *port_dev);
  956. static int team_port_add(struct team *team, struct net_device *port_dev)
  957. {
  958. struct net_device *dev = team->dev;
  959. struct team_port *port;
  960. char *portname = port_dev->name;
  961. int err;
  962. if (port_dev->flags & IFF_LOOPBACK) {
  963. netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
  964. portname);
  965. return -EINVAL;
  966. }
  967. if (team_port_exists(port_dev)) {
  968. netdev_err(dev, "Device %s is already a port "
  969. "of a team device\n", portname);
  970. return -EBUSY;
  971. }
  972. if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
  973. vlan_uses_dev(dev)) {
  974. netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
  975. portname);
  976. return -EPERM;
  977. }
  978. err = team_dev_type_check_change(dev, port_dev);
  979. if (err)
  980. return err;
  981. if (port_dev->flags & IFF_UP) {
  982. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  983. portname);
  984. return -EBUSY;
  985. }
  986. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  987. GFP_KERNEL);
  988. if (!port)
  989. return -ENOMEM;
  990. port->dev = port_dev;
  991. port->team = team;
  992. INIT_LIST_HEAD(&port->qom_list);
  993. port->orig.mtu = port_dev->mtu;
  994. err = dev_set_mtu(port_dev, dev->mtu);
  995. if (err) {
  996. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  997. goto err_set_mtu;
  998. }
  999. memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
  1000. err = team_port_enter(team, port);
  1001. if (err) {
  1002. netdev_err(dev, "Device %s failed to enter team mode\n",
  1003. portname);
  1004. goto err_port_enter;
  1005. }
  1006. err = dev_open(port_dev);
  1007. if (err) {
  1008. netdev_dbg(dev, "Device %s opening failed\n",
  1009. portname);
  1010. goto err_dev_open;
  1011. }
  1012. netif_addr_lock_bh(dev);
  1013. dev_uc_sync_multiple(port_dev, dev);
  1014. dev_mc_sync_multiple(port_dev, dev);
  1015. netif_addr_unlock_bh(dev);
  1016. err = vlan_vids_add_by_dev(port_dev, dev);
  1017. if (err) {
  1018. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  1019. portname);
  1020. goto err_vids_add;
  1021. }
  1022. err = team_port_enable_netpoll(team, port);
  1023. if (err) {
  1024. netdev_err(dev, "Failed to enable netpoll on device %s\n",
  1025. portname);
  1026. goto err_enable_netpoll;
  1027. }
  1028. if (!(dev->features & NETIF_F_LRO))
  1029. dev_disable_lro(port_dev);
  1030. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  1031. port);
  1032. if (err) {
  1033. netdev_err(dev, "Device %s failed to register rx_handler\n",
  1034. portname);
  1035. goto err_handler_register;
  1036. }
  1037. err = team_upper_dev_link(team, port);
  1038. if (err) {
  1039. netdev_err(dev, "Device %s failed to set upper link\n",
  1040. portname);
  1041. goto err_set_upper_link;
  1042. }
  1043. err = __team_option_inst_add_port(team, port);
  1044. if (err) {
  1045. netdev_err(dev, "Device %s failed to add per-port options\n",
  1046. portname);
  1047. goto err_option_port_add;
  1048. }
  1049. port->index = -1;
  1050. list_add_tail_rcu(&port->list, &team->port_list);
  1051. team_port_enable(team, port);
  1052. __team_compute_features(team);
  1053. __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
  1054. __team_options_change_check(team);
  1055. netdev_info(dev, "Port device %s added\n", portname);
  1056. return 0;
  1057. err_option_port_add:
  1058. team_upper_dev_unlink(team, port);
  1059. err_set_upper_link:
  1060. netdev_rx_handler_unregister(port_dev);
  1061. err_handler_register:
  1062. team_port_disable_netpoll(port);
  1063. err_enable_netpoll:
  1064. vlan_vids_del_by_dev(port_dev, dev);
  1065. err_vids_add:
  1066. dev_uc_unsync(port_dev, dev);
  1067. dev_mc_unsync(port_dev, dev);
  1068. dev_close(port_dev);
  1069. err_dev_open:
  1070. team_port_leave(team, port);
  1071. team_port_set_orig_dev_addr(port);
  1072. err_port_enter:
  1073. dev_set_mtu(port_dev, port->orig.mtu);
  1074. err_set_mtu:
  1075. kfree(port);
  1076. return err;
  1077. }
  1078. static void __team_port_change_port_removed(struct team_port *port);
  1079. static int team_port_del(struct team *team, struct net_device *port_dev)
  1080. {
  1081. struct net_device *dev = team->dev;
  1082. struct team_port *port;
  1083. char *portname = port_dev->name;
  1084. port = team_port_get_rtnl(port_dev);
  1085. if (!port || !team_port_find(team, port)) {
  1086. netdev_err(dev, "Device %s does not act as a port of this team\n",
  1087. portname);
  1088. return -ENOENT;
  1089. }
  1090. team_port_disable(team, port);
  1091. list_del_rcu(&port->list);
  1092. team_upper_dev_unlink(team, port);
  1093. netdev_rx_handler_unregister(port_dev);
  1094. team_port_disable_netpoll(port);
  1095. vlan_vids_del_by_dev(port_dev, dev);
  1096. dev_uc_unsync(port_dev, dev);
  1097. dev_mc_unsync(port_dev, dev);
  1098. dev_close(port_dev);
  1099. team_port_leave(team, port);
  1100. __team_option_inst_mark_removed_port(team, port);
  1101. __team_options_change_check(team);
  1102. __team_option_inst_del_port(team, port);
  1103. __team_port_change_port_removed(port);
  1104. team_port_set_orig_dev_addr(port);
  1105. dev_set_mtu(port_dev, port->orig.mtu);
  1106. kfree_rcu(port, rcu);
  1107. netdev_info(dev, "Port device %s removed\n", portname);
  1108. __team_compute_features(team);
  1109. return 0;
  1110. }
  1111. /*****************
  1112. * Net device ops
  1113. *****************/
  1114. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  1115. {
  1116. ctx->data.str_val = team->mode->kind;
  1117. return 0;
  1118. }
  1119. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  1120. {
  1121. return team_change_mode(team, ctx->data.str_val);
  1122. }
  1123. static int team_notify_peers_count_get(struct team *team,
  1124. struct team_gsetter_ctx *ctx)
  1125. {
  1126. ctx->data.u32_val = team->notify_peers.count;
  1127. return 0;
  1128. }
  1129. static int team_notify_peers_count_set(struct team *team,
  1130. struct team_gsetter_ctx *ctx)
  1131. {
  1132. team->notify_peers.count = ctx->data.u32_val;
  1133. return 0;
  1134. }
  1135. static int team_notify_peers_interval_get(struct team *team,
  1136. struct team_gsetter_ctx *ctx)
  1137. {
  1138. ctx->data.u32_val = team->notify_peers.interval;
  1139. return 0;
  1140. }
  1141. static int team_notify_peers_interval_set(struct team *team,
  1142. struct team_gsetter_ctx *ctx)
  1143. {
  1144. team->notify_peers.interval = ctx->data.u32_val;
  1145. return 0;
  1146. }
  1147. static int team_mcast_rejoin_count_get(struct team *team,
  1148. struct team_gsetter_ctx *ctx)
  1149. {
  1150. ctx->data.u32_val = team->mcast_rejoin.count;
  1151. return 0;
  1152. }
  1153. static int team_mcast_rejoin_count_set(struct team *team,
  1154. struct team_gsetter_ctx *ctx)
  1155. {
  1156. team->mcast_rejoin.count = ctx->data.u32_val;
  1157. return 0;
  1158. }
  1159. static int team_mcast_rejoin_interval_get(struct team *team,
  1160. struct team_gsetter_ctx *ctx)
  1161. {
  1162. ctx->data.u32_val = team->mcast_rejoin.interval;
  1163. return 0;
  1164. }
  1165. static int team_mcast_rejoin_interval_set(struct team *team,
  1166. struct team_gsetter_ctx *ctx)
  1167. {
  1168. team->mcast_rejoin.interval = ctx->data.u32_val;
  1169. return 0;
  1170. }
  1171. static int team_port_en_option_get(struct team *team,
  1172. struct team_gsetter_ctx *ctx)
  1173. {
  1174. struct team_port *port = ctx->info->port;
  1175. ctx->data.bool_val = team_port_enabled(port);
  1176. return 0;
  1177. }
  1178. static int team_port_en_option_set(struct team *team,
  1179. struct team_gsetter_ctx *ctx)
  1180. {
  1181. struct team_port *port = ctx->info->port;
  1182. if (ctx->data.bool_val)
  1183. team_port_enable(team, port);
  1184. else
  1185. team_port_disable(team, port);
  1186. return 0;
  1187. }
  1188. static int team_user_linkup_option_get(struct team *team,
  1189. struct team_gsetter_ctx *ctx)
  1190. {
  1191. struct team_port *port = ctx->info->port;
  1192. ctx->data.bool_val = port->user.linkup;
  1193. return 0;
  1194. }
  1195. static void __team_carrier_check(struct team *team);
  1196. static int team_user_linkup_option_set(struct team *team,
  1197. struct team_gsetter_ctx *ctx)
  1198. {
  1199. struct team_port *port = ctx->info->port;
  1200. port->user.linkup = ctx->data.bool_val;
  1201. team_refresh_port_linkup(port);
  1202. __team_carrier_check(port->team);
  1203. return 0;
  1204. }
  1205. static int team_user_linkup_en_option_get(struct team *team,
  1206. struct team_gsetter_ctx *ctx)
  1207. {
  1208. struct team_port *port = ctx->info->port;
  1209. ctx->data.bool_val = port->user.linkup_enabled;
  1210. return 0;
  1211. }
  1212. static int team_user_linkup_en_option_set(struct team *team,
  1213. struct team_gsetter_ctx *ctx)
  1214. {
  1215. struct team_port *port = ctx->info->port;
  1216. port->user.linkup_enabled = ctx->data.bool_val;
  1217. team_refresh_port_linkup(port);
  1218. __team_carrier_check(port->team);
  1219. return 0;
  1220. }
  1221. static int team_priority_option_get(struct team *team,
  1222. struct team_gsetter_ctx *ctx)
  1223. {
  1224. struct team_port *port = ctx->info->port;
  1225. ctx->data.s32_val = port->priority;
  1226. return 0;
  1227. }
  1228. static int team_priority_option_set(struct team *team,
  1229. struct team_gsetter_ctx *ctx)
  1230. {
  1231. struct team_port *port = ctx->info->port;
  1232. s32 priority = ctx->data.s32_val;
  1233. if (port->priority == priority)
  1234. return 0;
  1235. port->priority = priority;
  1236. team_queue_override_port_prio_changed(team, port);
  1237. return 0;
  1238. }
  1239. static int team_queue_id_option_get(struct team *team,
  1240. struct team_gsetter_ctx *ctx)
  1241. {
  1242. struct team_port *port = ctx->info->port;
  1243. ctx->data.u32_val = port->queue_id;
  1244. return 0;
  1245. }
  1246. static int team_queue_id_option_set(struct team *team,
  1247. struct team_gsetter_ctx *ctx)
  1248. {
  1249. struct team_port *port = ctx->info->port;
  1250. u16 new_queue_id = ctx->data.u32_val;
  1251. if (port->queue_id == new_queue_id)
  1252. return 0;
  1253. if (new_queue_id >= team->dev->real_num_tx_queues)
  1254. return -EINVAL;
  1255. team_queue_override_port_change_queue_id(team, port, new_queue_id);
  1256. return 0;
  1257. }
  1258. static const struct team_option team_options[] = {
  1259. {
  1260. .name = "mode",
  1261. .type = TEAM_OPTION_TYPE_STRING,
  1262. .getter = team_mode_option_get,
  1263. .setter = team_mode_option_set,
  1264. },
  1265. {
  1266. .name = "notify_peers_count",
  1267. .type = TEAM_OPTION_TYPE_U32,
  1268. .getter = team_notify_peers_count_get,
  1269. .setter = team_notify_peers_count_set,
  1270. },
  1271. {
  1272. .name = "notify_peers_interval",
  1273. .type = TEAM_OPTION_TYPE_U32,
  1274. .getter = team_notify_peers_interval_get,
  1275. .setter = team_notify_peers_interval_set,
  1276. },
  1277. {
  1278. .name = "mcast_rejoin_count",
  1279. .type = TEAM_OPTION_TYPE_U32,
  1280. .getter = team_mcast_rejoin_count_get,
  1281. .setter = team_mcast_rejoin_count_set,
  1282. },
  1283. {
  1284. .name = "mcast_rejoin_interval",
  1285. .type = TEAM_OPTION_TYPE_U32,
  1286. .getter = team_mcast_rejoin_interval_get,
  1287. .setter = team_mcast_rejoin_interval_set,
  1288. },
  1289. {
  1290. .name = "enabled",
  1291. .type = TEAM_OPTION_TYPE_BOOL,
  1292. .per_port = true,
  1293. .getter = team_port_en_option_get,
  1294. .setter = team_port_en_option_set,
  1295. },
  1296. {
  1297. .name = "user_linkup",
  1298. .type = TEAM_OPTION_TYPE_BOOL,
  1299. .per_port = true,
  1300. .getter = team_user_linkup_option_get,
  1301. .setter = team_user_linkup_option_set,
  1302. },
  1303. {
  1304. .name = "user_linkup_enabled",
  1305. .type = TEAM_OPTION_TYPE_BOOL,
  1306. .per_port = true,
  1307. .getter = team_user_linkup_en_option_get,
  1308. .setter = team_user_linkup_en_option_set,
  1309. },
  1310. {
  1311. .name = "priority",
  1312. .type = TEAM_OPTION_TYPE_S32,
  1313. .per_port = true,
  1314. .getter = team_priority_option_get,
  1315. .setter = team_priority_option_set,
  1316. },
  1317. {
  1318. .name = "queue_id",
  1319. .type = TEAM_OPTION_TYPE_U32,
  1320. .per_port = true,
  1321. .getter = team_queue_id_option_get,
  1322. .setter = team_queue_id_option_set,
  1323. },
  1324. };
  1325. static int team_init(struct net_device *dev)
  1326. {
  1327. struct team *team = netdev_priv(dev);
  1328. int i;
  1329. int err;
  1330. team->dev = dev;
  1331. mutex_init(&team->lock);
  1332. team_set_no_mode(team);
  1333. team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
  1334. if (!team->pcpu_stats)
  1335. return -ENOMEM;
  1336. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1337. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1338. INIT_LIST_HEAD(&team->port_list);
  1339. err = team_queue_override_init(team);
  1340. if (err)
  1341. goto err_team_queue_override_init;
  1342. team_adjust_ops(team);
  1343. INIT_LIST_HEAD(&team->option_list);
  1344. INIT_LIST_HEAD(&team->option_inst_list);
  1345. team_notify_peers_init(team);
  1346. team_mcast_rejoin_init(team);
  1347. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1348. if (err)
  1349. goto err_options_register;
  1350. netif_carrier_off(dev);
  1351. netdev_lockdep_set_classes(dev);
  1352. return 0;
  1353. err_options_register:
  1354. team_mcast_rejoin_fini(team);
  1355. team_notify_peers_fini(team);
  1356. team_queue_override_fini(team);
  1357. err_team_queue_override_init:
  1358. free_percpu(team->pcpu_stats);
  1359. return err;
  1360. }
  1361. static void team_uninit(struct net_device *dev)
  1362. {
  1363. struct team *team = netdev_priv(dev);
  1364. struct team_port *port;
  1365. struct team_port *tmp;
  1366. mutex_lock(&team->lock);
  1367. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1368. team_port_del(team, port->dev);
  1369. __team_change_mode(team, NULL); /* cleanup */
  1370. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1371. team_mcast_rejoin_fini(team);
  1372. team_notify_peers_fini(team);
  1373. team_queue_override_fini(team);
  1374. mutex_unlock(&team->lock);
  1375. }
  1376. static void team_destructor(struct net_device *dev)
  1377. {
  1378. struct team *team = netdev_priv(dev);
  1379. free_percpu(team->pcpu_stats);
  1380. free_netdev(dev);
  1381. }
  1382. static int team_open(struct net_device *dev)
  1383. {
  1384. return 0;
  1385. }
  1386. static int team_close(struct net_device *dev)
  1387. {
  1388. return 0;
  1389. }
  1390. /*
  1391. * note: already called with rcu_read_lock
  1392. */
  1393. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1394. {
  1395. struct team *team = netdev_priv(dev);
  1396. bool tx_success;
  1397. unsigned int len = skb->len;
  1398. tx_success = team_queue_override_transmit(team, skb);
  1399. if (!tx_success)
  1400. tx_success = team->ops.transmit(team, skb);
  1401. if (tx_success) {
  1402. struct team_pcpu_stats *pcpu_stats;
  1403. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1404. u64_stats_update_begin(&pcpu_stats->syncp);
  1405. pcpu_stats->tx_packets++;
  1406. pcpu_stats->tx_bytes += len;
  1407. u64_stats_update_end(&pcpu_stats->syncp);
  1408. } else {
  1409. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1410. }
  1411. return NETDEV_TX_OK;
  1412. }
  1413. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
  1414. void *accel_priv, select_queue_fallback_t fallback)
  1415. {
  1416. /*
  1417. * This helper function exists to help dev_pick_tx get the correct
  1418. * destination queue. Using a helper function skips a call to
  1419. * skb_tx_hash and will put the skbs in the queue we expect on their
  1420. * way down to the team driver.
  1421. */
  1422. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1423. /*
  1424. * Save the original txq to restore before passing to the driver
  1425. */
  1426. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1427. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1428. do {
  1429. txq -= dev->real_num_tx_queues;
  1430. } while (txq >= dev->real_num_tx_queues);
  1431. }
  1432. return txq;
  1433. }
  1434. static void team_change_rx_flags(struct net_device *dev, int change)
  1435. {
  1436. struct team *team = netdev_priv(dev);
  1437. struct team_port *port;
  1438. int inc;
  1439. rcu_read_lock();
  1440. list_for_each_entry_rcu(port, &team->port_list, list) {
  1441. if (change & IFF_PROMISC) {
  1442. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1443. dev_set_promiscuity(port->dev, inc);
  1444. }
  1445. if (change & IFF_ALLMULTI) {
  1446. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1447. dev_set_allmulti(port->dev, inc);
  1448. }
  1449. }
  1450. rcu_read_unlock();
  1451. }
  1452. static void team_set_rx_mode(struct net_device *dev)
  1453. {
  1454. struct team *team = netdev_priv(dev);
  1455. struct team_port *port;
  1456. rcu_read_lock();
  1457. list_for_each_entry_rcu(port, &team->port_list, list) {
  1458. dev_uc_sync_multiple(port->dev, dev);
  1459. dev_mc_sync_multiple(port->dev, dev);
  1460. }
  1461. rcu_read_unlock();
  1462. }
  1463. static int team_set_mac_address(struct net_device *dev, void *p)
  1464. {
  1465. struct sockaddr *addr = p;
  1466. struct team *team = netdev_priv(dev);
  1467. struct team_port *port;
  1468. if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
  1469. return -EADDRNOTAVAIL;
  1470. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1471. mutex_lock(&team->lock);
  1472. list_for_each_entry(port, &team->port_list, list)
  1473. if (team->ops.port_change_dev_addr)
  1474. team->ops.port_change_dev_addr(team, port);
  1475. mutex_unlock(&team->lock);
  1476. return 0;
  1477. }
  1478. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1479. {
  1480. struct team *team = netdev_priv(dev);
  1481. struct team_port *port;
  1482. int err;
  1483. /*
  1484. * Alhough this is reader, it's guarded by team lock. It's not possible
  1485. * to traverse list in reverse under rcu_read_lock
  1486. */
  1487. mutex_lock(&team->lock);
  1488. team->port_mtu_change_allowed = true;
  1489. list_for_each_entry(port, &team->port_list, list) {
  1490. err = dev_set_mtu(port->dev, new_mtu);
  1491. if (err) {
  1492. netdev_err(dev, "Device %s failed to change mtu",
  1493. port->dev->name);
  1494. goto unwind;
  1495. }
  1496. }
  1497. team->port_mtu_change_allowed = false;
  1498. mutex_unlock(&team->lock);
  1499. dev->mtu = new_mtu;
  1500. return 0;
  1501. unwind:
  1502. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1503. dev_set_mtu(port->dev, dev->mtu);
  1504. team->port_mtu_change_allowed = false;
  1505. mutex_unlock(&team->lock);
  1506. return err;
  1507. }
  1508. static struct rtnl_link_stats64 *
  1509. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1510. {
  1511. struct team *team = netdev_priv(dev);
  1512. struct team_pcpu_stats *p;
  1513. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1514. u32 rx_dropped = 0, tx_dropped = 0, rx_nohandler = 0;
  1515. unsigned int start;
  1516. int i;
  1517. for_each_possible_cpu(i) {
  1518. p = per_cpu_ptr(team->pcpu_stats, i);
  1519. do {
  1520. start = u64_stats_fetch_begin_irq(&p->syncp);
  1521. rx_packets = p->rx_packets;
  1522. rx_bytes = p->rx_bytes;
  1523. rx_multicast = p->rx_multicast;
  1524. tx_packets = p->tx_packets;
  1525. tx_bytes = p->tx_bytes;
  1526. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  1527. stats->rx_packets += rx_packets;
  1528. stats->rx_bytes += rx_bytes;
  1529. stats->multicast += rx_multicast;
  1530. stats->tx_packets += tx_packets;
  1531. stats->tx_bytes += tx_bytes;
  1532. /*
  1533. * rx_dropped, tx_dropped & rx_nohandler are u32,
  1534. * updated without syncp protection.
  1535. */
  1536. rx_dropped += p->rx_dropped;
  1537. tx_dropped += p->tx_dropped;
  1538. rx_nohandler += p->rx_nohandler;
  1539. }
  1540. stats->rx_dropped = rx_dropped;
  1541. stats->tx_dropped = tx_dropped;
  1542. stats->rx_nohandler = rx_nohandler;
  1543. return stats;
  1544. }
  1545. static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  1546. {
  1547. struct team *team = netdev_priv(dev);
  1548. struct team_port *port;
  1549. int err;
  1550. /*
  1551. * Alhough this is reader, it's guarded by team lock. It's not possible
  1552. * to traverse list in reverse under rcu_read_lock
  1553. */
  1554. mutex_lock(&team->lock);
  1555. list_for_each_entry(port, &team->port_list, list) {
  1556. err = vlan_vid_add(port->dev, proto, vid);
  1557. if (err)
  1558. goto unwind;
  1559. }
  1560. mutex_unlock(&team->lock);
  1561. return 0;
  1562. unwind:
  1563. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1564. vlan_vid_del(port->dev, proto, vid);
  1565. mutex_unlock(&team->lock);
  1566. return err;
  1567. }
  1568. static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
  1569. {
  1570. struct team *team = netdev_priv(dev);
  1571. struct team_port *port;
  1572. mutex_lock(&team->lock);
  1573. list_for_each_entry(port, &team->port_list, list)
  1574. vlan_vid_del(port->dev, proto, vid);
  1575. mutex_unlock(&team->lock);
  1576. return 0;
  1577. }
  1578. #ifdef CONFIG_NET_POLL_CONTROLLER
  1579. static void team_poll_controller(struct net_device *dev)
  1580. {
  1581. }
  1582. static void __team_netpoll_cleanup(struct team *team)
  1583. {
  1584. struct team_port *port;
  1585. list_for_each_entry(port, &team->port_list, list)
  1586. team_port_disable_netpoll(port);
  1587. }
  1588. static void team_netpoll_cleanup(struct net_device *dev)
  1589. {
  1590. struct team *team = netdev_priv(dev);
  1591. mutex_lock(&team->lock);
  1592. __team_netpoll_cleanup(team);
  1593. mutex_unlock(&team->lock);
  1594. }
  1595. static int team_netpoll_setup(struct net_device *dev,
  1596. struct netpoll_info *npifo)
  1597. {
  1598. struct team *team = netdev_priv(dev);
  1599. struct team_port *port;
  1600. int err = 0;
  1601. mutex_lock(&team->lock);
  1602. list_for_each_entry(port, &team->port_list, list) {
  1603. err = team_port_enable_netpoll(team, port);
  1604. if (err) {
  1605. __team_netpoll_cleanup(team);
  1606. break;
  1607. }
  1608. }
  1609. mutex_unlock(&team->lock);
  1610. return err;
  1611. }
  1612. #endif
  1613. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1614. {
  1615. struct team *team = netdev_priv(dev);
  1616. int err;
  1617. mutex_lock(&team->lock);
  1618. err = team_port_add(team, port_dev);
  1619. mutex_unlock(&team->lock);
  1620. return err;
  1621. }
  1622. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1623. {
  1624. struct team *team = netdev_priv(dev);
  1625. int err;
  1626. mutex_lock(&team->lock);
  1627. err = team_port_del(team, port_dev);
  1628. mutex_unlock(&team->lock);
  1629. return err;
  1630. }
  1631. static netdev_features_t team_fix_features(struct net_device *dev,
  1632. netdev_features_t features)
  1633. {
  1634. struct team_port *port;
  1635. struct team *team = netdev_priv(dev);
  1636. netdev_features_t mask;
  1637. mask = features;
  1638. features &= ~NETIF_F_ONE_FOR_ALL;
  1639. features |= NETIF_F_ALL_FOR_ALL;
  1640. rcu_read_lock();
  1641. list_for_each_entry_rcu(port, &team->port_list, list) {
  1642. features = netdev_increment_features(features,
  1643. port->dev->features,
  1644. mask);
  1645. }
  1646. rcu_read_unlock();
  1647. features = netdev_add_tso_features(features, mask);
  1648. return features;
  1649. }
  1650. static int team_change_carrier(struct net_device *dev, bool new_carrier)
  1651. {
  1652. struct team *team = netdev_priv(dev);
  1653. team->user_carrier_enabled = true;
  1654. if (new_carrier)
  1655. netif_carrier_on(dev);
  1656. else
  1657. netif_carrier_off(dev);
  1658. return 0;
  1659. }
  1660. static const struct net_device_ops team_netdev_ops = {
  1661. .ndo_init = team_init,
  1662. .ndo_uninit = team_uninit,
  1663. .ndo_open = team_open,
  1664. .ndo_stop = team_close,
  1665. .ndo_start_xmit = team_xmit,
  1666. .ndo_select_queue = team_select_queue,
  1667. .ndo_change_rx_flags = team_change_rx_flags,
  1668. .ndo_set_rx_mode = team_set_rx_mode,
  1669. .ndo_set_mac_address = team_set_mac_address,
  1670. .ndo_change_mtu = team_change_mtu,
  1671. .ndo_get_stats64 = team_get_stats64,
  1672. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1673. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1674. #ifdef CONFIG_NET_POLL_CONTROLLER
  1675. .ndo_poll_controller = team_poll_controller,
  1676. .ndo_netpoll_setup = team_netpoll_setup,
  1677. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1678. #endif
  1679. .ndo_add_slave = team_add_slave,
  1680. .ndo_del_slave = team_del_slave,
  1681. .ndo_fix_features = team_fix_features,
  1682. .ndo_neigh_construct = netdev_default_l2upper_neigh_construct,
  1683. .ndo_neigh_destroy = netdev_default_l2upper_neigh_destroy,
  1684. .ndo_change_carrier = team_change_carrier,
  1685. .ndo_bridge_setlink = switchdev_port_bridge_setlink,
  1686. .ndo_bridge_getlink = switchdev_port_bridge_getlink,
  1687. .ndo_bridge_dellink = switchdev_port_bridge_dellink,
  1688. .ndo_fdb_add = switchdev_port_fdb_add,
  1689. .ndo_fdb_del = switchdev_port_fdb_del,
  1690. .ndo_fdb_dump = switchdev_port_fdb_dump,
  1691. .ndo_features_check = passthru_features_check,
  1692. };
  1693. /***********************
  1694. * ethtool interface
  1695. ***********************/
  1696. static void team_ethtool_get_drvinfo(struct net_device *dev,
  1697. struct ethtool_drvinfo *drvinfo)
  1698. {
  1699. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  1700. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1701. }
  1702. static const struct ethtool_ops team_ethtool_ops = {
  1703. .get_drvinfo = team_ethtool_get_drvinfo,
  1704. .get_link = ethtool_op_get_link,
  1705. };
  1706. /***********************
  1707. * rt netlink interface
  1708. ***********************/
  1709. static void team_setup_by_port(struct net_device *dev,
  1710. struct net_device *port_dev)
  1711. {
  1712. dev->header_ops = port_dev->header_ops;
  1713. dev->type = port_dev->type;
  1714. dev->hard_header_len = port_dev->hard_header_len;
  1715. dev->addr_len = port_dev->addr_len;
  1716. dev->mtu = port_dev->mtu;
  1717. memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
  1718. eth_hw_addr_inherit(dev, port_dev);
  1719. }
  1720. static int team_dev_type_check_change(struct net_device *dev,
  1721. struct net_device *port_dev)
  1722. {
  1723. struct team *team = netdev_priv(dev);
  1724. char *portname = port_dev->name;
  1725. int err;
  1726. if (dev->type == port_dev->type)
  1727. return 0;
  1728. if (!list_empty(&team->port_list)) {
  1729. netdev_err(dev, "Device %s is of different type\n", portname);
  1730. return -EBUSY;
  1731. }
  1732. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  1733. err = notifier_to_errno(err);
  1734. if (err) {
  1735. netdev_err(dev, "Refused to change device type\n");
  1736. return err;
  1737. }
  1738. dev_uc_flush(dev);
  1739. dev_mc_flush(dev);
  1740. team_setup_by_port(dev, port_dev);
  1741. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  1742. return 0;
  1743. }
  1744. static void team_setup(struct net_device *dev)
  1745. {
  1746. ether_setup(dev);
  1747. dev->netdev_ops = &team_netdev_ops;
  1748. dev->ethtool_ops = &team_ethtool_ops;
  1749. dev->destructor = team_destructor;
  1750. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1751. dev->priv_flags |= IFF_NO_QUEUE;
  1752. dev->priv_flags |= IFF_TEAM;
  1753. /*
  1754. * Indicate we support unicast address filtering. That way core won't
  1755. * bring us to promisc mode in case a unicast addr is added.
  1756. * Let this up to underlay drivers.
  1757. */
  1758. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1759. dev->features |= NETIF_F_LLTX;
  1760. dev->features |= NETIF_F_GRO;
  1761. /* Don't allow team devices to change network namespaces. */
  1762. dev->features |= NETIF_F_NETNS_LOCAL;
  1763. dev->hw_features = TEAM_VLAN_FEATURES |
  1764. NETIF_F_HW_VLAN_CTAG_TX |
  1765. NETIF_F_HW_VLAN_CTAG_RX |
  1766. NETIF_F_HW_VLAN_CTAG_FILTER;
  1767. dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
  1768. dev->features |= dev->hw_features;
  1769. }
  1770. static int team_newlink(struct net *src_net, struct net_device *dev,
  1771. struct nlattr *tb[], struct nlattr *data[])
  1772. {
  1773. if (tb[IFLA_ADDRESS] == NULL)
  1774. eth_hw_addr_random(dev);
  1775. return register_netdevice(dev);
  1776. }
  1777. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1778. {
  1779. if (tb[IFLA_ADDRESS]) {
  1780. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1781. return -EINVAL;
  1782. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1783. return -EADDRNOTAVAIL;
  1784. }
  1785. return 0;
  1786. }
  1787. static unsigned int team_get_num_tx_queues(void)
  1788. {
  1789. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1790. }
  1791. static unsigned int team_get_num_rx_queues(void)
  1792. {
  1793. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1794. }
  1795. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1796. .kind = DRV_NAME,
  1797. .priv_size = sizeof(struct team),
  1798. .setup = team_setup,
  1799. .newlink = team_newlink,
  1800. .validate = team_validate,
  1801. .get_num_tx_queues = team_get_num_tx_queues,
  1802. .get_num_rx_queues = team_get_num_rx_queues,
  1803. };
  1804. /***********************************
  1805. * Generic netlink custom interface
  1806. ***********************************/
  1807. static struct genl_family team_nl_family = {
  1808. .id = GENL_ID_GENERATE,
  1809. .name = TEAM_GENL_NAME,
  1810. .version = TEAM_GENL_VERSION,
  1811. .maxattr = TEAM_ATTR_MAX,
  1812. .netnsok = true,
  1813. };
  1814. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1815. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1816. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1817. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1818. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1819. };
  1820. static const struct nla_policy
  1821. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1822. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1823. [TEAM_ATTR_OPTION_NAME] = {
  1824. .type = NLA_STRING,
  1825. .len = TEAM_STRING_MAX_LEN,
  1826. },
  1827. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1828. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1829. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1830. };
  1831. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1832. {
  1833. struct sk_buff *msg;
  1834. void *hdr;
  1835. int err;
  1836. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1837. if (!msg)
  1838. return -ENOMEM;
  1839. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1840. &team_nl_family, 0, TEAM_CMD_NOOP);
  1841. if (!hdr) {
  1842. err = -EMSGSIZE;
  1843. goto err_msg_put;
  1844. }
  1845. genlmsg_end(msg, hdr);
  1846. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  1847. err_msg_put:
  1848. nlmsg_free(msg);
  1849. return err;
  1850. }
  1851. /*
  1852. * Netlink cmd functions should be locked by following two functions.
  1853. * Since dev gets held here, that ensures dev won't disappear in between.
  1854. */
  1855. static struct team *team_nl_team_get(struct genl_info *info)
  1856. {
  1857. struct net *net = genl_info_net(info);
  1858. int ifindex;
  1859. struct net_device *dev;
  1860. struct team *team;
  1861. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1862. return NULL;
  1863. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1864. dev = dev_get_by_index(net, ifindex);
  1865. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1866. if (dev)
  1867. dev_put(dev);
  1868. return NULL;
  1869. }
  1870. team = netdev_priv(dev);
  1871. mutex_lock(&team->lock);
  1872. return team;
  1873. }
  1874. static void team_nl_team_put(struct team *team)
  1875. {
  1876. mutex_unlock(&team->lock);
  1877. dev_put(team->dev);
  1878. }
  1879. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1880. struct team *team, u32 portid);
  1881. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
  1882. {
  1883. return genlmsg_unicast(dev_net(team->dev), skb, portid);
  1884. }
  1885. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1886. struct team_option_inst *opt_inst)
  1887. {
  1888. struct nlattr *option_item;
  1889. struct team_option *option = opt_inst->option;
  1890. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1891. struct team_gsetter_ctx ctx;
  1892. int err;
  1893. ctx.info = opt_inst_info;
  1894. err = team_option_get(team, opt_inst, &ctx);
  1895. if (err)
  1896. return err;
  1897. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1898. if (!option_item)
  1899. return -EMSGSIZE;
  1900. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1901. goto nest_cancel;
  1902. if (opt_inst_info->port &&
  1903. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1904. opt_inst_info->port->dev->ifindex))
  1905. goto nest_cancel;
  1906. if (opt_inst->option->array_size &&
  1907. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1908. opt_inst_info->array_index))
  1909. goto nest_cancel;
  1910. switch (option->type) {
  1911. case TEAM_OPTION_TYPE_U32:
  1912. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1913. goto nest_cancel;
  1914. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1915. goto nest_cancel;
  1916. break;
  1917. case TEAM_OPTION_TYPE_STRING:
  1918. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1919. goto nest_cancel;
  1920. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1921. ctx.data.str_val))
  1922. goto nest_cancel;
  1923. break;
  1924. case TEAM_OPTION_TYPE_BINARY:
  1925. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1926. goto nest_cancel;
  1927. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1928. ctx.data.bin_val.ptr))
  1929. goto nest_cancel;
  1930. break;
  1931. case TEAM_OPTION_TYPE_BOOL:
  1932. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1933. goto nest_cancel;
  1934. if (ctx.data.bool_val &&
  1935. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1936. goto nest_cancel;
  1937. break;
  1938. case TEAM_OPTION_TYPE_S32:
  1939. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1940. goto nest_cancel;
  1941. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1942. goto nest_cancel;
  1943. break;
  1944. default:
  1945. BUG();
  1946. }
  1947. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1948. goto nest_cancel;
  1949. if (opt_inst->changed) {
  1950. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1951. goto nest_cancel;
  1952. opt_inst->changed = false;
  1953. }
  1954. nla_nest_end(skb, option_item);
  1955. return 0;
  1956. nest_cancel:
  1957. nla_nest_cancel(skb, option_item);
  1958. return -EMSGSIZE;
  1959. }
  1960. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1961. struct team *team, u32 portid,
  1962. team_nl_send_func_t *send_func)
  1963. {
  1964. int err;
  1965. if (*pskb) {
  1966. err = send_func(*pskb, team, portid);
  1967. if (err)
  1968. return err;
  1969. }
  1970. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1971. if (!*pskb)
  1972. return -ENOMEM;
  1973. return 0;
  1974. }
  1975. static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
  1976. int flags, team_nl_send_func_t *send_func,
  1977. struct list_head *sel_opt_inst_list)
  1978. {
  1979. struct nlattr *option_list;
  1980. struct nlmsghdr *nlh;
  1981. void *hdr;
  1982. struct team_option_inst *opt_inst;
  1983. int err;
  1984. struct sk_buff *skb = NULL;
  1985. bool incomplete;
  1986. int i;
  1987. opt_inst = list_first_entry(sel_opt_inst_list,
  1988. struct team_option_inst, tmp_list);
  1989. start_again:
  1990. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1991. if (err)
  1992. return err;
  1993. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  1994. TEAM_CMD_OPTIONS_GET);
  1995. if (!hdr) {
  1996. nlmsg_free(skb);
  1997. return -EMSGSIZE;
  1998. }
  1999. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2000. goto nla_put_failure;
  2001. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  2002. if (!option_list)
  2003. goto nla_put_failure;
  2004. i = 0;
  2005. incomplete = false;
  2006. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  2007. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  2008. if (err) {
  2009. if (err == -EMSGSIZE) {
  2010. if (!i)
  2011. goto errout;
  2012. incomplete = true;
  2013. break;
  2014. }
  2015. goto errout;
  2016. }
  2017. i++;
  2018. }
  2019. nla_nest_end(skb, option_list);
  2020. genlmsg_end(skb, hdr);
  2021. if (incomplete)
  2022. goto start_again;
  2023. send_done:
  2024. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2025. if (!nlh) {
  2026. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2027. if (err)
  2028. goto errout;
  2029. goto send_done;
  2030. }
  2031. return send_func(skb, team, portid);
  2032. nla_put_failure:
  2033. err = -EMSGSIZE;
  2034. errout:
  2035. genlmsg_cancel(skb, hdr);
  2036. nlmsg_free(skb);
  2037. return err;
  2038. }
  2039. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  2040. {
  2041. struct team *team;
  2042. struct team_option_inst *opt_inst;
  2043. int err;
  2044. LIST_HEAD(sel_opt_inst_list);
  2045. team = team_nl_team_get(info);
  2046. if (!team)
  2047. return -EINVAL;
  2048. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  2049. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2050. err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
  2051. NLM_F_ACK, team_nl_send_unicast,
  2052. &sel_opt_inst_list);
  2053. team_nl_team_put(team);
  2054. return err;
  2055. }
  2056. static int team_nl_send_event_options_get(struct team *team,
  2057. struct list_head *sel_opt_inst_list);
  2058. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  2059. {
  2060. struct team *team;
  2061. int err = 0;
  2062. int i;
  2063. struct nlattr *nl_option;
  2064. LIST_HEAD(opt_inst_list);
  2065. rtnl_lock();
  2066. team = team_nl_team_get(info);
  2067. if (!team) {
  2068. err = -EINVAL;
  2069. goto rtnl_unlock;
  2070. }
  2071. err = -EINVAL;
  2072. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  2073. err = -EINVAL;
  2074. goto team_put;
  2075. }
  2076. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  2077. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  2078. struct nlattr *attr;
  2079. struct nlattr *attr_data;
  2080. enum team_option_type opt_type;
  2081. int opt_port_ifindex = 0; /* != 0 for per-port options */
  2082. u32 opt_array_index = 0;
  2083. bool opt_is_array = false;
  2084. struct team_option_inst *opt_inst;
  2085. char *opt_name;
  2086. bool opt_found = false;
  2087. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  2088. err = -EINVAL;
  2089. goto team_put;
  2090. }
  2091. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  2092. nl_option, team_nl_option_policy);
  2093. if (err)
  2094. goto team_put;
  2095. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  2096. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  2097. err = -EINVAL;
  2098. goto team_put;
  2099. }
  2100. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  2101. case NLA_U32:
  2102. opt_type = TEAM_OPTION_TYPE_U32;
  2103. break;
  2104. case NLA_STRING:
  2105. opt_type = TEAM_OPTION_TYPE_STRING;
  2106. break;
  2107. case NLA_BINARY:
  2108. opt_type = TEAM_OPTION_TYPE_BINARY;
  2109. break;
  2110. case NLA_FLAG:
  2111. opt_type = TEAM_OPTION_TYPE_BOOL;
  2112. break;
  2113. case NLA_S32:
  2114. opt_type = TEAM_OPTION_TYPE_S32;
  2115. break;
  2116. default:
  2117. goto team_put;
  2118. }
  2119. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  2120. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  2121. err = -EINVAL;
  2122. goto team_put;
  2123. }
  2124. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  2125. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  2126. if (attr)
  2127. opt_port_ifindex = nla_get_u32(attr);
  2128. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  2129. if (attr) {
  2130. opt_is_array = true;
  2131. opt_array_index = nla_get_u32(attr);
  2132. }
  2133. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2134. struct team_option *option = opt_inst->option;
  2135. struct team_gsetter_ctx ctx;
  2136. struct team_option_inst_info *opt_inst_info;
  2137. int tmp_ifindex;
  2138. opt_inst_info = &opt_inst->info;
  2139. tmp_ifindex = opt_inst_info->port ?
  2140. opt_inst_info->port->dev->ifindex : 0;
  2141. if (option->type != opt_type ||
  2142. strcmp(option->name, opt_name) ||
  2143. tmp_ifindex != opt_port_ifindex ||
  2144. (option->array_size && !opt_is_array) ||
  2145. opt_inst_info->array_index != opt_array_index)
  2146. continue;
  2147. opt_found = true;
  2148. ctx.info = opt_inst_info;
  2149. switch (opt_type) {
  2150. case TEAM_OPTION_TYPE_U32:
  2151. ctx.data.u32_val = nla_get_u32(attr_data);
  2152. break;
  2153. case TEAM_OPTION_TYPE_STRING:
  2154. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  2155. err = -EINVAL;
  2156. goto team_put;
  2157. }
  2158. ctx.data.str_val = nla_data(attr_data);
  2159. break;
  2160. case TEAM_OPTION_TYPE_BINARY:
  2161. ctx.data.bin_val.len = nla_len(attr_data);
  2162. ctx.data.bin_val.ptr = nla_data(attr_data);
  2163. break;
  2164. case TEAM_OPTION_TYPE_BOOL:
  2165. ctx.data.bool_val = attr_data ? true : false;
  2166. break;
  2167. case TEAM_OPTION_TYPE_S32:
  2168. ctx.data.s32_val = nla_get_s32(attr_data);
  2169. break;
  2170. default:
  2171. BUG();
  2172. }
  2173. err = team_option_set(team, opt_inst, &ctx);
  2174. if (err)
  2175. goto team_put;
  2176. opt_inst->changed = true;
  2177. list_add(&opt_inst->tmp_list, &opt_inst_list);
  2178. }
  2179. if (!opt_found) {
  2180. err = -ENOENT;
  2181. goto team_put;
  2182. }
  2183. }
  2184. err = team_nl_send_event_options_get(team, &opt_inst_list);
  2185. team_put:
  2186. team_nl_team_put(team);
  2187. rtnl_unlock:
  2188. rtnl_unlock();
  2189. return err;
  2190. }
  2191. static int team_nl_fill_one_port_get(struct sk_buff *skb,
  2192. struct team_port *port)
  2193. {
  2194. struct nlattr *port_item;
  2195. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  2196. if (!port_item)
  2197. goto nest_cancel;
  2198. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  2199. goto nest_cancel;
  2200. if (port->changed) {
  2201. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  2202. goto nest_cancel;
  2203. port->changed = false;
  2204. }
  2205. if ((port->removed &&
  2206. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  2207. (port->state.linkup &&
  2208. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  2209. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  2210. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  2211. goto nest_cancel;
  2212. nla_nest_end(skb, port_item);
  2213. return 0;
  2214. nest_cancel:
  2215. nla_nest_cancel(skb, port_item);
  2216. return -EMSGSIZE;
  2217. }
  2218. static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
  2219. int flags, team_nl_send_func_t *send_func,
  2220. struct team_port *one_port)
  2221. {
  2222. struct nlattr *port_list;
  2223. struct nlmsghdr *nlh;
  2224. void *hdr;
  2225. struct team_port *port;
  2226. int err;
  2227. struct sk_buff *skb = NULL;
  2228. bool incomplete;
  2229. int i;
  2230. port = list_first_entry_or_null(&team->port_list,
  2231. struct team_port, list);
  2232. start_again:
  2233. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2234. if (err)
  2235. return err;
  2236. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2237. TEAM_CMD_PORT_LIST_GET);
  2238. if (!hdr) {
  2239. nlmsg_free(skb);
  2240. return -EMSGSIZE;
  2241. }
  2242. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2243. goto nla_put_failure;
  2244. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  2245. if (!port_list)
  2246. goto nla_put_failure;
  2247. i = 0;
  2248. incomplete = false;
  2249. /* If one port is selected, called wants to send port list containing
  2250. * only this port. Otherwise go through all listed ports and send all
  2251. */
  2252. if (one_port) {
  2253. err = team_nl_fill_one_port_get(skb, one_port);
  2254. if (err)
  2255. goto errout;
  2256. } else if (port) {
  2257. list_for_each_entry_from(port, &team->port_list, list) {
  2258. err = team_nl_fill_one_port_get(skb, port);
  2259. if (err) {
  2260. if (err == -EMSGSIZE) {
  2261. if (!i)
  2262. goto errout;
  2263. incomplete = true;
  2264. break;
  2265. }
  2266. goto errout;
  2267. }
  2268. i++;
  2269. }
  2270. }
  2271. nla_nest_end(skb, port_list);
  2272. genlmsg_end(skb, hdr);
  2273. if (incomplete)
  2274. goto start_again;
  2275. send_done:
  2276. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2277. if (!nlh) {
  2278. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2279. if (err)
  2280. goto errout;
  2281. goto send_done;
  2282. }
  2283. return send_func(skb, team, portid);
  2284. nla_put_failure:
  2285. err = -EMSGSIZE;
  2286. errout:
  2287. genlmsg_cancel(skb, hdr);
  2288. nlmsg_free(skb);
  2289. return err;
  2290. }
  2291. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  2292. struct genl_info *info)
  2293. {
  2294. struct team *team;
  2295. int err;
  2296. team = team_nl_team_get(info);
  2297. if (!team)
  2298. return -EINVAL;
  2299. err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
  2300. NLM_F_ACK, team_nl_send_unicast, NULL);
  2301. team_nl_team_put(team);
  2302. return err;
  2303. }
  2304. static const struct genl_ops team_nl_ops[] = {
  2305. {
  2306. .cmd = TEAM_CMD_NOOP,
  2307. .doit = team_nl_cmd_noop,
  2308. .policy = team_nl_policy,
  2309. },
  2310. {
  2311. .cmd = TEAM_CMD_OPTIONS_SET,
  2312. .doit = team_nl_cmd_options_set,
  2313. .policy = team_nl_policy,
  2314. .flags = GENL_ADMIN_PERM,
  2315. },
  2316. {
  2317. .cmd = TEAM_CMD_OPTIONS_GET,
  2318. .doit = team_nl_cmd_options_get,
  2319. .policy = team_nl_policy,
  2320. .flags = GENL_ADMIN_PERM,
  2321. },
  2322. {
  2323. .cmd = TEAM_CMD_PORT_LIST_GET,
  2324. .doit = team_nl_cmd_port_list_get,
  2325. .policy = team_nl_policy,
  2326. .flags = GENL_ADMIN_PERM,
  2327. },
  2328. };
  2329. static const struct genl_multicast_group team_nl_mcgrps[] = {
  2330. { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
  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 team_nl_init(void)
  2351. {
  2352. return genl_register_family_with_ops_groups(&team_nl_family, team_nl_ops,
  2353. team_nl_mcgrps);
  2354. }
  2355. static void team_nl_fini(void)
  2356. {
  2357. genl_unregister_family(&team_nl_family);
  2358. }
  2359. /******************
  2360. * Change checkers
  2361. ******************/
  2362. static void __team_options_change_check(struct team *team)
  2363. {
  2364. int err;
  2365. struct team_option_inst *opt_inst;
  2366. LIST_HEAD(sel_opt_inst_list);
  2367. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2368. if (opt_inst->changed)
  2369. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2370. }
  2371. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2372. if (err && err != -ESRCH)
  2373. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2374. err);
  2375. }
  2376. /* rtnl lock is held */
  2377. static void __team_port_change_send(struct team_port *port, bool linkup)
  2378. {
  2379. int err;
  2380. port->changed = true;
  2381. port->state.linkup = linkup;
  2382. team_refresh_port_linkup(port);
  2383. if (linkup) {
  2384. struct ethtool_link_ksettings ecmd;
  2385. err = __ethtool_get_link_ksettings(port->dev, &ecmd);
  2386. if (!err) {
  2387. port->state.speed = ecmd.base.speed;
  2388. port->state.duplex = ecmd.base.duplex;
  2389. goto send_event;
  2390. }
  2391. }
  2392. port->state.speed = 0;
  2393. port->state.duplex = 0;
  2394. send_event:
  2395. err = team_nl_send_event_port_get(port->team, port);
  2396. if (err && err != -ESRCH)
  2397. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
  2398. port->dev->name, err);
  2399. }
  2400. static void __team_carrier_check(struct team *team)
  2401. {
  2402. struct team_port *port;
  2403. bool team_linkup;
  2404. if (team->user_carrier_enabled)
  2405. return;
  2406. team_linkup = false;
  2407. list_for_each_entry(port, &team->port_list, list) {
  2408. if (port->linkup) {
  2409. team_linkup = true;
  2410. break;
  2411. }
  2412. }
  2413. if (team_linkup)
  2414. netif_carrier_on(team->dev);
  2415. else
  2416. netif_carrier_off(team->dev);
  2417. }
  2418. static void __team_port_change_check(struct team_port *port, bool linkup)
  2419. {
  2420. if (port->state.linkup != linkup)
  2421. __team_port_change_send(port, linkup);
  2422. __team_carrier_check(port->team);
  2423. }
  2424. static void __team_port_change_port_added(struct team_port *port, bool linkup)
  2425. {
  2426. __team_port_change_send(port, linkup);
  2427. __team_carrier_check(port->team);
  2428. }
  2429. static void __team_port_change_port_removed(struct team_port *port)
  2430. {
  2431. port->removed = true;
  2432. __team_port_change_send(port, false);
  2433. __team_carrier_check(port->team);
  2434. }
  2435. static void team_port_change_check(struct team_port *port, bool linkup)
  2436. {
  2437. struct team *team = port->team;
  2438. mutex_lock(&team->lock);
  2439. __team_port_change_check(port, linkup);
  2440. mutex_unlock(&team->lock);
  2441. }
  2442. /************************************
  2443. * Net device notifier event handler
  2444. ************************************/
  2445. static int team_device_event(struct notifier_block *unused,
  2446. unsigned long event, void *ptr)
  2447. {
  2448. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2449. struct team_port *port;
  2450. port = team_port_get_rtnl(dev);
  2451. if (!port)
  2452. return NOTIFY_DONE;
  2453. switch (event) {
  2454. case NETDEV_UP:
  2455. if (netif_carrier_ok(dev))
  2456. team_port_change_check(port, true);
  2457. break;
  2458. case NETDEV_DOWN:
  2459. team_port_change_check(port, false);
  2460. break;
  2461. case NETDEV_CHANGE:
  2462. if (netif_running(port->dev))
  2463. team_port_change_check(port,
  2464. !!netif_carrier_ok(port->dev));
  2465. break;
  2466. case NETDEV_UNREGISTER:
  2467. team_del_slave(port->team->dev, dev);
  2468. break;
  2469. case NETDEV_FEAT_CHANGE:
  2470. team_compute_features(port->team);
  2471. break;
  2472. case NETDEV_PRECHANGEMTU:
  2473. /* Forbid to change mtu of underlaying device */
  2474. if (!port->team->port_mtu_change_allowed)
  2475. return NOTIFY_BAD;
  2476. break;
  2477. case NETDEV_PRE_TYPE_CHANGE:
  2478. /* Forbid to change type of underlaying device */
  2479. return NOTIFY_BAD;
  2480. case NETDEV_RESEND_IGMP:
  2481. /* Propagate to master device */
  2482. call_netdevice_notifiers(event, port->team->dev);
  2483. break;
  2484. }
  2485. return NOTIFY_DONE;
  2486. }
  2487. static struct notifier_block team_notifier_block __read_mostly = {
  2488. .notifier_call = team_device_event,
  2489. };
  2490. /***********************
  2491. * Module init and exit
  2492. ***********************/
  2493. static int __init team_module_init(void)
  2494. {
  2495. int err;
  2496. register_netdevice_notifier(&team_notifier_block);
  2497. err = rtnl_link_register(&team_link_ops);
  2498. if (err)
  2499. goto err_rtnl_reg;
  2500. err = team_nl_init();
  2501. if (err)
  2502. goto err_nl_init;
  2503. return 0;
  2504. err_nl_init:
  2505. rtnl_link_unregister(&team_link_ops);
  2506. err_rtnl_reg:
  2507. unregister_netdevice_notifier(&team_notifier_block);
  2508. return err;
  2509. }
  2510. static void __exit team_module_exit(void)
  2511. {
  2512. team_nl_fini();
  2513. rtnl_link_unregister(&team_link_ops);
  2514. unregister_netdevice_notifier(&team_notifier_block);
  2515. }
  2516. module_init(team_module_init);
  2517. module_exit(team_module_exit);
  2518. MODULE_LICENSE("GPL v2");
  2519. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2520. MODULE_DESCRIPTION("Ethernet team device driver");
  2521. MODULE_ALIAS_RTNL_LINK(DRV_NAME);