team.c 69 KB

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