team.c 69 KB

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