team.c 63 KB

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