team.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  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_master_upper_dev_link(port_dev, dev);
  887. if (err) {
  888. netdev_err(dev, "Device %s failed to set upper link\n",
  889. portname);
  890. goto err_set_upper_link;
  891. }
  892. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  893. port);
  894. if (err) {
  895. netdev_err(dev, "Device %s failed to register rx_handler\n",
  896. portname);
  897. goto err_handler_register;
  898. }
  899. err = __team_option_inst_add_port(team, port);
  900. if (err) {
  901. netdev_err(dev, "Device %s failed to add per-port options\n",
  902. portname);
  903. goto err_option_port_add;
  904. }
  905. port->index = -1;
  906. team_port_enable(team, port);
  907. list_add_tail_rcu(&port->list, &team->port_list);
  908. __team_compute_features(team);
  909. __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
  910. __team_options_change_check(team);
  911. netdev_info(dev, "Port device %s added\n", portname);
  912. return 0;
  913. err_option_port_add:
  914. netdev_rx_handler_unregister(port_dev);
  915. err_handler_register:
  916. netdev_upper_dev_unlink(port_dev, dev);
  917. err_set_upper_link:
  918. team_port_disable_netpoll(port);
  919. err_enable_netpoll:
  920. vlan_vids_del_by_dev(port_dev, dev);
  921. err_vids_add:
  922. dev_close(port_dev);
  923. err_dev_open:
  924. team_port_leave(team, port);
  925. team_port_set_orig_dev_addr(port);
  926. err_port_enter:
  927. dev_set_mtu(port_dev, port->orig.mtu);
  928. err_set_mtu:
  929. kfree(port);
  930. return err;
  931. }
  932. static void __team_port_change_port_removed(struct team_port *port);
  933. static int team_port_del(struct team *team, struct net_device *port_dev)
  934. {
  935. struct net_device *dev = team->dev;
  936. struct team_port *port;
  937. char *portname = port_dev->name;
  938. port = team_port_get_rtnl(port_dev);
  939. if (!port || !team_port_find(team, port)) {
  940. netdev_err(dev, "Device %s does not act as a port of this team\n",
  941. portname);
  942. return -ENOENT;
  943. }
  944. team_port_disable(team, port);
  945. list_del_rcu(&port->list);
  946. netdev_rx_handler_unregister(port_dev);
  947. netdev_upper_dev_unlink(port_dev, dev);
  948. team_port_disable_netpoll(port);
  949. vlan_vids_del_by_dev(port_dev, dev);
  950. dev_close(port_dev);
  951. team_port_leave(team, port);
  952. __team_option_inst_mark_removed_port(team, port);
  953. __team_options_change_check(team);
  954. __team_option_inst_del_port(team, port);
  955. __team_port_change_port_removed(port);
  956. team_port_set_orig_dev_addr(port);
  957. dev_set_mtu(port_dev, port->orig.mtu);
  958. synchronize_rcu();
  959. kfree(port);
  960. netdev_info(dev, "Port device %s removed\n", portname);
  961. __team_compute_features(team);
  962. return 0;
  963. }
  964. /*****************
  965. * Net device ops
  966. *****************/
  967. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  968. {
  969. ctx->data.str_val = team->mode->kind;
  970. return 0;
  971. }
  972. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  973. {
  974. return team_change_mode(team, ctx->data.str_val);
  975. }
  976. static int team_port_en_option_get(struct team *team,
  977. struct team_gsetter_ctx *ctx)
  978. {
  979. struct team_port *port = ctx->info->port;
  980. ctx->data.bool_val = team_port_enabled(port);
  981. return 0;
  982. }
  983. static int team_port_en_option_set(struct team *team,
  984. struct team_gsetter_ctx *ctx)
  985. {
  986. struct team_port *port = ctx->info->port;
  987. if (ctx->data.bool_val)
  988. team_port_enable(team, port);
  989. else
  990. team_port_disable(team, port);
  991. return 0;
  992. }
  993. static int team_user_linkup_option_get(struct team *team,
  994. struct team_gsetter_ctx *ctx)
  995. {
  996. struct team_port *port = ctx->info->port;
  997. ctx->data.bool_val = port->user.linkup;
  998. return 0;
  999. }
  1000. static int team_user_linkup_option_set(struct team *team,
  1001. struct team_gsetter_ctx *ctx)
  1002. {
  1003. struct team_port *port = ctx->info->port;
  1004. port->user.linkup = ctx->data.bool_val;
  1005. team_refresh_port_linkup(port);
  1006. return 0;
  1007. }
  1008. static int team_user_linkup_en_option_get(struct team *team,
  1009. struct team_gsetter_ctx *ctx)
  1010. {
  1011. struct team_port *port = ctx->info->port;
  1012. ctx->data.bool_val = port->user.linkup_enabled;
  1013. return 0;
  1014. }
  1015. static int team_user_linkup_en_option_set(struct team *team,
  1016. struct team_gsetter_ctx *ctx)
  1017. {
  1018. struct team_port *port = ctx->info->port;
  1019. port->user.linkup_enabled = ctx->data.bool_val;
  1020. team_refresh_port_linkup(port);
  1021. return 0;
  1022. }
  1023. static int team_priority_option_get(struct team *team,
  1024. struct team_gsetter_ctx *ctx)
  1025. {
  1026. struct team_port *port = ctx->info->port;
  1027. ctx->data.s32_val = port->priority;
  1028. return 0;
  1029. }
  1030. static int team_priority_option_set(struct team *team,
  1031. struct team_gsetter_ctx *ctx)
  1032. {
  1033. struct team_port *port = ctx->info->port;
  1034. port->priority = ctx->data.s32_val;
  1035. team_queue_override_port_refresh(team, port);
  1036. return 0;
  1037. }
  1038. static int team_queue_id_option_get(struct team *team,
  1039. struct team_gsetter_ctx *ctx)
  1040. {
  1041. struct team_port *port = ctx->info->port;
  1042. ctx->data.u32_val = port->queue_id;
  1043. return 0;
  1044. }
  1045. static int team_queue_id_option_set(struct team *team,
  1046. struct team_gsetter_ctx *ctx)
  1047. {
  1048. struct team_port *port = ctx->info->port;
  1049. if (port->queue_id == ctx->data.u32_val)
  1050. return 0;
  1051. if (ctx->data.u32_val >= team->dev->real_num_tx_queues)
  1052. return -EINVAL;
  1053. port->queue_id = ctx->data.u32_val;
  1054. team_queue_override_port_refresh(team, port);
  1055. return 0;
  1056. }
  1057. static const struct team_option team_options[] = {
  1058. {
  1059. .name = "mode",
  1060. .type = TEAM_OPTION_TYPE_STRING,
  1061. .getter = team_mode_option_get,
  1062. .setter = team_mode_option_set,
  1063. },
  1064. {
  1065. .name = "enabled",
  1066. .type = TEAM_OPTION_TYPE_BOOL,
  1067. .per_port = true,
  1068. .getter = team_port_en_option_get,
  1069. .setter = team_port_en_option_set,
  1070. },
  1071. {
  1072. .name = "user_linkup",
  1073. .type = TEAM_OPTION_TYPE_BOOL,
  1074. .per_port = true,
  1075. .getter = team_user_linkup_option_get,
  1076. .setter = team_user_linkup_option_set,
  1077. },
  1078. {
  1079. .name = "user_linkup_enabled",
  1080. .type = TEAM_OPTION_TYPE_BOOL,
  1081. .per_port = true,
  1082. .getter = team_user_linkup_en_option_get,
  1083. .setter = team_user_linkup_en_option_set,
  1084. },
  1085. {
  1086. .name = "priority",
  1087. .type = TEAM_OPTION_TYPE_S32,
  1088. .per_port = true,
  1089. .getter = team_priority_option_get,
  1090. .setter = team_priority_option_set,
  1091. },
  1092. {
  1093. .name = "queue_id",
  1094. .type = TEAM_OPTION_TYPE_U32,
  1095. .per_port = true,
  1096. .getter = team_queue_id_option_get,
  1097. .setter = team_queue_id_option_set,
  1098. },
  1099. };
  1100. static struct lock_class_key team_netdev_xmit_lock_key;
  1101. static struct lock_class_key team_netdev_addr_lock_key;
  1102. static struct lock_class_key team_tx_busylock_key;
  1103. static void team_set_lockdep_class_one(struct net_device *dev,
  1104. struct netdev_queue *txq,
  1105. void *unused)
  1106. {
  1107. lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
  1108. }
  1109. static void team_set_lockdep_class(struct net_device *dev)
  1110. {
  1111. lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
  1112. netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
  1113. dev->qdisc_tx_busylock = &team_tx_busylock_key;
  1114. }
  1115. static int team_init(struct net_device *dev)
  1116. {
  1117. struct team *team = netdev_priv(dev);
  1118. int i;
  1119. int err;
  1120. team->dev = dev;
  1121. mutex_init(&team->lock);
  1122. team_set_no_mode(team);
  1123. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  1124. if (!team->pcpu_stats)
  1125. return -ENOMEM;
  1126. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1127. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1128. INIT_LIST_HEAD(&team->port_list);
  1129. err = team_queue_override_init(team);
  1130. if (err)
  1131. goto err_team_queue_override_init;
  1132. team_adjust_ops(team);
  1133. INIT_LIST_HEAD(&team->option_list);
  1134. INIT_LIST_HEAD(&team->option_inst_list);
  1135. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1136. if (err)
  1137. goto err_options_register;
  1138. netif_carrier_off(dev);
  1139. team_set_lockdep_class(dev);
  1140. return 0;
  1141. err_options_register:
  1142. team_queue_override_fini(team);
  1143. err_team_queue_override_init:
  1144. free_percpu(team->pcpu_stats);
  1145. return err;
  1146. }
  1147. static void team_uninit(struct net_device *dev)
  1148. {
  1149. struct team *team = netdev_priv(dev);
  1150. struct team_port *port;
  1151. struct team_port *tmp;
  1152. mutex_lock(&team->lock);
  1153. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1154. team_port_del(team, port->dev);
  1155. __team_change_mode(team, NULL); /* cleanup */
  1156. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1157. team_queue_override_fini(team);
  1158. mutex_unlock(&team->lock);
  1159. }
  1160. static void team_destructor(struct net_device *dev)
  1161. {
  1162. struct team *team = netdev_priv(dev);
  1163. free_percpu(team->pcpu_stats);
  1164. free_netdev(dev);
  1165. }
  1166. static int team_open(struct net_device *dev)
  1167. {
  1168. return 0;
  1169. }
  1170. static int team_close(struct net_device *dev)
  1171. {
  1172. return 0;
  1173. }
  1174. /*
  1175. * note: already called with rcu_read_lock
  1176. */
  1177. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1178. {
  1179. struct team *team = netdev_priv(dev);
  1180. bool tx_success;
  1181. unsigned int len = skb->len;
  1182. tx_success = team_queue_override_transmit(team, skb);
  1183. if (!tx_success)
  1184. tx_success = team->ops.transmit(team, skb);
  1185. if (tx_success) {
  1186. struct team_pcpu_stats *pcpu_stats;
  1187. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1188. u64_stats_update_begin(&pcpu_stats->syncp);
  1189. pcpu_stats->tx_packets++;
  1190. pcpu_stats->tx_bytes += len;
  1191. u64_stats_update_end(&pcpu_stats->syncp);
  1192. } else {
  1193. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1194. }
  1195. return NETDEV_TX_OK;
  1196. }
  1197. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
  1198. {
  1199. /*
  1200. * This helper function exists to help dev_pick_tx get the correct
  1201. * destination queue. Using a helper function skips a call to
  1202. * skb_tx_hash and will put the skbs in the queue we expect on their
  1203. * way down to the team driver.
  1204. */
  1205. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1206. /*
  1207. * Save the original txq to restore before passing to the driver
  1208. */
  1209. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1210. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1211. do {
  1212. txq -= dev->real_num_tx_queues;
  1213. } while (txq >= dev->real_num_tx_queues);
  1214. }
  1215. return txq;
  1216. }
  1217. static void team_change_rx_flags(struct net_device *dev, int change)
  1218. {
  1219. struct team *team = netdev_priv(dev);
  1220. struct team_port *port;
  1221. int inc;
  1222. rcu_read_lock();
  1223. list_for_each_entry_rcu(port, &team->port_list, list) {
  1224. if (change & IFF_PROMISC) {
  1225. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1226. dev_set_promiscuity(port->dev, inc);
  1227. }
  1228. if (change & IFF_ALLMULTI) {
  1229. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1230. dev_set_allmulti(port->dev, inc);
  1231. }
  1232. }
  1233. rcu_read_unlock();
  1234. }
  1235. static void team_set_rx_mode(struct net_device *dev)
  1236. {
  1237. struct team *team = netdev_priv(dev);
  1238. struct team_port *port;
  1239. rcu_read_lock();
  1240. list_for_each_entry_rcu(port, &team->port_list, list) {
  1241. dev_uc_sync(port->dev, dev);
  1242. dev_mc_sync(port->dev, dev);
  1243. }
  1244. rcu_read_unlock();
  1245. }
  1246. static int team_set_mac_address(struct net_device *dev, void *p)
  1247. {
  1248. struct sockaddr *addr = p;
  1249. struct team *team = netdev_priv(dev);
  1250. struct team_port *port;
  1251. if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
  1252. return -EADDRNOTAVAIL;
  1253. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1254. rcu_read_lock();
  1255. list_for_each_entry_rcu(port, &team->port_list, list)
  1256. if (team->ops.port_change_dev_addr)
  1257. team->ops.port_change_dev_addr(team, port);
  1258. rcu_read_unlock();
  1259. return 0;
  1260. }
  1261. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1262. {
  1263. struct team *team = netdev_priv(dev);
  1264. struct team_port *port;
  1265. int err;
  1266. /*
  1267. * Alhough this is reader, it's guarded by team lock. It's not possible
  1268. * to traverse list in reverse under rcu_read_lock
  1269. */
  1270. mutex_lock(&team->lock);
  1271. list_for_each_entry(port, &team->port_list, list) {
  1272. err = dev_set_mtu(port->dev, new_mtu);
  1273. if (err) {
  1274. netdev_err(dev, "Device %s failed to change mtu",
  1275. port->dev->name);
  1276. goto unwind;
  1277. }
  1278. }
  1279. mutex_unlock(&team->lock);
  1280. dev->mtu = new_mtu;
  1281. return 0;
  1282. unwind:
  1283. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1284. dev_set_mtu(port->dev, dev->mtu);
  1285. mutex_unlock(&team->lock);
  1286. return err;
  1287. }
  1288. static struct rtnl_link_stats64 *
  1289. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1290. {
  1291. struct team *team = netdev_priv(dev);
  1292. struct team_pcpu_stats *p;
  1293. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1294. u32 rx_dropped = 0, tx_dropped = 0;
  1295. unsigned int start;
  1296. int i;
  1297. for_each_possible_cpu(i) {
  1298. p = per_cpu_ptr(team->pcpu_stats, i);
  1299. do {
  1300. start = u64_stats_fetch_begin_bh(&p->syncp);
  1301. rx_packets = p->rx_packets;
  1302. rx_bytes = p->rx_bytes;
  1303. rx_multicast = p->rx_multicast;
  1304. tx_packets = p->tx_packets;
  1305. tx_bytes = p->tx_bytes;
  1306. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  1307. stats->rx_packets += rx_packets;
  1308. stats->rx_bytes += rx_bytes;
  1309. stats->multicast += rx_multicast;
  1310. stats->tx_packets += tx_packets;
  1311. stats->tx_bytes += tx_bytes;
  1312. /*
  1313. * rx_dropped & tx_dropped are u32, updated
  1314. * without syncp protection.
  1315. */
  1316. rx_dropped += p->rx_dropped;
  1317. tx_dropped += p->tx_dropped;
  1318. }
  1319. stats->rx_dropped = rx_dropped;
  1320. stats->tx_dropped = tx_dropped;
  1321. return stats;
  1322. }
  1323. static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
  1324. {
  1325. struct team *team = netdev_priv(dev);
  1326. struct team_port *port;
  1327. int err;
  1328. /*
  1329. * Alhough this is reader, it's guarded by team lock. It's not possible
  1330. * to traverse list in reverse under rcu_read_lock
  1331. */
  1332. mutex_lock(&team->lock);
  1333. list_for_each_entry(port, &team->port_list, list) {
  1334. err = vlan_vid_add(port->dev, vid);
  1335. if (err)
  1336. goto unwind;
  1337. }
  1338. mutex_unlock(&team->lock);
  1339. return 0;
  1340. unwind:
  1341. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1342. vlan_vid_del(port->dev, vid);
  1343. mutex_unlock(&team->lock);
  1344. return err;
  1345. }
  1346. static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
  1347. {
  1348. struct team *team = netdev_priv(dev);
  1349. struct team_port *port;
  1350. rcu_read_lock();
  1351. list_for_each_entry_rcu(port, &team->port_list, list)
  1352. vlan_vid_del(port->dev, vid);
  1353. rcu_read_unlock();
  1354. return 0;
  1355. }
  1356. #ifdef CONFIG_NET_POLL_CONTROLLER
  1357. static void team_poll_controller(struct net_device *dev)
  1358. {
  1359. }
  1360. static void __team_netpoll_cleanup(struct team *team)
  1361. {
  1362. struct team_port *port;
  1363. list_for_each_entry(port, &team->port_list, list)
  1364. team_port_disable_netpoll(port);
  1365. }
  1366. static void team_netpoll_cleanup(struct net_device *dev)
  1367. {
  1368. struct team *team = netdev_priv(dev);
  1369. mutex_lock(&team->lock);
  1370. __team_netpoll_cleanup(team);
  1371. mutex_unlock(&team->lock);
  1372. }
  1373. static int team_netpoll_setup(struct net_device *dev,
  1374. struct netpoll_info *npifo, gfp_t gfp)
  1375. {
  1376. struct team *team = netdev_priv(dev);
  1377. struct team_port *port;
  1378. int err = 0;
  1379. mutex_lock(&team->lock);
  1380. list_for_each_entry(port, &team->port_list, list) {
  1381. err = team_port_enable_netpoll(team, port, gfp);
  1382. if (err) {
  1383. __team_netpoll_cleanup(team);
  1384. break;
  1385. }
  1386. }
  1387. mutex_unlock(&team->lock);
  1388. return err;
  1389. }
  1390. #endif
  1391. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1392. {
  1393. struct team *team = netdev_priv(dev);
  1394. int err;
  1395. mutex_lock(&team->lock);
  1396. err = team_port_add(team, port_dev);
  1397. mutex_unlock(&team->lock);
  1398. return err;
  1399. }
  1400. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1401. {
  1402. struct team *team = netdev_priv(dev);
  1403. int err;
  1404. mutex_lock(&team->lock);
  1405. err = team_port_del(team, port_dev);
  1406. mutex_unlock(&team->lock);
  1407. return err;
  1408. }
  1409. static netdev_features_t team_fix_features(struct net_device *dev,
  1410. netdev_features_t features)
  1411. {
  1412. struct team_port *port;
  1413. struct team *team = netdev_priv(dev);
  1414. netdev_features_t mask;
  1415. mask = features;
  1416. features &= ~NETIF_F_ONE_FOR_ALL;
  1417. features |= NETIF_F_ALL_FOR_ALL;
  1418. rcu_read_lock();
  1419. list_for_each_entry_rcu(port, &team->port_list, list) {
  1420. features = netdev_increment_features(features,
  1421. port->dev->features,
  1422. mask);
  1423. }
  1424. rcu_read_unlock();
  1425. return features;
  1426. }
  1427. static int team_change_carrier(struct net_device *dev, bool new_carrier)
  1428. {
  1429. if (new_carrier)
  1430. netif_carrier_on(dev);
  1431. else
  1432. netif_carrier_off(dev);
  1433. return 0;
  1434. }
  1435. static const struct net_device_ops team_netdev_ops = {
  1436. .ndo_init = team_init,
  1437. .ndo_uninit = team_uninit,
  1438. .ndo_open = team_open,
  1439. .ndo_stop = team_close,
  1440. .ndo_start_xmit = team_xmit,
  1441. .ndo_select_queue = team_select_queue,
  1442. .ndo_change_rx_flags = team_change_rx_flags,
  1443. .ndo_set_rx_mode = team_set_rx_mode,
  1444. .ndo_set_mac_address = team_set_mac_address,
  1445. .ndo_change_mtu = team_change_mtu,
  1446. .ndo_get_stats64 = team_get_stats64,
  1447. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1448. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1449. #ifdef CONFIG_NET_POLL_CONTROLLER
  1450. .ndo_poll_controller = team_poll_controller,
  1451. .ndo_netpoll_setup = team_netpoll_setup,
  1452. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1453. #endif
  1454. .ndo_add_slave = team_add_slave,
  1455. .ndo_del_slave = team_del_slave,
  1456. .ndo_fix_features = team_fix_features,
  1457. .ndo_change_carrier = team_change_carrier,
  1458. };
  1459. /***********************
  1460. * ethtool interface
  1461. ***********************/
  1462. static void team_ethtool_get_drvinfo(struct net_device *dev,
  1463. struct ethtool_drvinfo *drvinfo)
  1464. {
  1465. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  1466. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1467. }
  1468. static const struct ethtool_ops team_ethtool_ops = {
  1469. .get_drvinfo = team_ethtool_get_drvinfo,
  1470. .get_link = ethtool_op_get_link,
  1471. };
  1472. /***********************
  1473. * rt netlink interface
  1474. ***********************/
  1475. static void team_setup_by_port(struct net_device *dev,
  1476. struct net_device *port_dev)
  1477. {
  1478. dev->header_ops = port_dev->header_ops;
  1479. dev->type = port_dev->type;
  1480. dev->hard_header_len = port_dev->hard_header_len;
  1481. dev->addr_len = port_dev->addr_len;
  1482. dev->mtu = port_dev->mtu;
  1483. memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
  1484. memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len);
  1485. }
  1486. static int team_dev_type_check_change(struct net_device *dev,
  1487. struct net_device *port_dev)
  1488. {
  1489. struct team *team = netdev_priv(dev);
  1490. char *portname = port_dev->name;
  1491. int err;
  1492. if (dev->type == port_dev->type)
  1493. return 0;
  1494. if (!list_empty(&team->port_list)) {
  1495. netdev_err(dev, "Device %s is of different type\n", portname);
  1496. return -EBUSY;
  1497. }
  1498. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  1499. err = notifier_to_errno(err);
  1500. if (err) {
  1501. netdev_err(dev, "Refused to change device type\n");
  1502. return err;
  1503. }
  1504. dev_uc_flush(dev);
  1505. dev_mc_flush(dev);
  1506. team_setup_by_port(dev, port_dev);
  1507. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  1508. return 0;
  1509. }
  1510. static void team_setup(struct net_device *dev)
  1511. {
  1512. ether_setup(dev);
  1513. dev->netdev_ops = &team_netdev_ops;
  1514. dev->ethtool_ops = &team_ethtool_ops;
  1515. dev->destructor = team_destructor;
  1516. dev->tx_queue_len = 0;
  1517. dev->flags |= IFF_MULTICAST;
  1518. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1519. /*
  1520. * Indicate we support unicast address filtering. That way core won't
  1521. * bring us to promisc mode in case a unicast addr is added.
  1522. * Let this up to underlay drivers.
  1523. */
  1524. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1525. dev->features |= NETIF_F_LLTX;
  1526. dev->features |= NETIF_F_GRO;
  1527. dev->hw_features = TEAM_VLAN_FEATURES |
  1528. NETIF_F_HW_VLAN_TX |
  1529. NETIF_F_HW_VLAN_RX |
  1530. NETIF_F_HW_VLAN_FILTER;
  1531. dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
  1532. dev->features |= dev->hw_features;
  1533. }
  1534. static int team_newlink(struct net *src_net, struct net_device *dev,
  1535. struct nlattr *tb[], struct nlattr *data[])
  1536. {
  1537. int err;
  1538. if (tb[IFLA_ADDRESS] == NULL)
  1539. eth_hw_addr_random(dev);
  1540. err = register_netdevice(dev);
  1541. if (err)
  1542. return err;
  1543. return 0;
  1544. }
  1545. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1546. {
  1547. if (tb[IFLA_ADDRESS]) {
  1548. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1549. return -EINVAL;
  1550. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1551. return -EADDRNOTAVAIL;
  1552. }
  1553. return 0;
  1554. }
  1555. static unsigned int team_get_num_tx_queues(void)
  1556. {
  1557. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1558. }
  1559. static unsigned int team_get_num_rx_queues(void)
  1560. {
  1561. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1562. }
  1563. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1564. .kind = DRV_NAME,
  1565. .priv_size = sizeof(struct team),
  1566. .setup = team_setup,
  1567. .newlink = team_newlink,
  1568. .validate = team_validate,
  1569. .get_num_tx_queues = team_get_num_tx_queues,
  1570. .get_num_rx_queues = team_get_num_rx_queues,
  1571. };
  1572. /***********************************
  1573. * Generic netlink custom interface
  1574. ***********************************/
  1575. static struct genl_family team_nl_family = {
  1576. .id = GENL_ID_GENERATE,
  1577. .name = TEAM_GENL_NAME,
  1578. .version = TEAM_GENL_VERSION,
  1579. .maxattr = TEAM_ATTR_MAX,
  1580. .netnsok = true,
  1581. };
  1582. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1583. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1584. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1585. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1586. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1587. };
  1588. static const struct nla_policy
  1589. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1590. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1591. [TEAM_ATTR_OPTION_NAME] = {
  1592. .type = NLA_STRING,
  1593. .len = TEAM_STRING_MAX_LEN,
  1594. },
  1595. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1596. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1597. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1598. };
  1599. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1600. {
  1601. struct sk_buff *msg;
  1602. void *hdr;
  1603. int err;
  1604. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1605. if (!msg)
  1606. return -ENOMEM;
  1607. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1608. &team_nl_family, 0, TEAM_CMD_NOOP);
  1609. if (!hdr) {
  1610. err = -EMSGSIZE;
  1611. goto err_msg_put;
  1612. }
  1613. genlmsg_end(msg, hdr);
  1614. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  1615. err_msg_put:
  1616. nlmsg_free(msg);
  1617. return err;
  1618. }
  1619. /*
  1620. * Netlink cmd functions should be locked by following two functions.
  1621. * Since dev gets held here, that ensures dev won't disappear in between.
  1622. */
  1623. static struct team *team_nl_team_get(struct genl_info *info)
  1624. {
  1625. struct net *net = genl_info_net(info);
  1626. int ifindex;
  1627. struct net_device *dev;
  1628. struct team *team;
  1629. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1630. return NULL;
  1631. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1632. dev = dev_get_by_index(net, ifindex);
  1633. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1634. if (dev)
  1635. dev_put(dev);
  1636. return NULL;
  1637. }
  1638. team = netdev_priv(dev);
  1639. mutex_lock(&team->lock);
  1640. return team;
  1641. }
  1642. static void team_nl_team_put(struct team *team)
  1643. {
  1644. mutex_unlock(&team->lock);
  1645. dev_put(team->dev);
  1646. }
  1647. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1648. struct team *team, u32 portid);
  1649. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
  1650. {
  1651. return genlmsg_unicast(dev_net(team->dev), skb, portid);
  1652. }
  1653. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1654. struct team_option_inst *opt_inst)
  1655. {
  1656. struct nlattr *option_item;
  1657. struct team_option *option = opt_inst->option;
  1658. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1659. struct team_gsetter_ctx ctx;
  1660. int err;
  1661. ctx.info = opt_inst_info;
  1662. err = team_option_get(team, opt_inst, &ctx);
  1663. if (err)
  1664. return err;
  1665. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1666. if (!option_item)
  1667. return -EMSGSIZE;
  1668. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1669. goto nest_cancel;
  1670. if (opt_inst_info->port &&
  1671. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1672. opt_inst_info->port->dev->ifindex))
  1673. goto nest_cancel;
  1674. if (opt_inst->option->array_size &&
  1675. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1676. opt_inst_info->array_index))
  1677. goto nest_cancel;
  1678. switch (option->type) {
  1679. case TEAM_OPTION_TYPE_U32:
  1680. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1681. goto nest_cancel;
  1682. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1683. goto nest_cancel;
  1684. break;
  1685. case TEAM_OPTION_TYPE_STRING:
  1686. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1687. goto nest_cancel;
  1688. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1689. ctx.data.str_val))
  1690. goto nest_cancel;
  1691. break;
  1692. case TEAM_OPTION_TYPE_BINARY:
  1693. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1694. goto nest_cancel;
  1695. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1696. ctx.data.bin_val.ptr))
  1697. goto nest_cancel;
  1698. break;
  1699. case TEAM_OPTION_TYPE_BOOL:
  1700. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1701. goto nest_cancel;
  1702. if (ctx.data.bool_val &&
  1703. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1704. goto nest_cancel;
  1705. break;
  1706. case TEAM_OPTION_TYPE_S32:
  1707. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1708. goto nest_cancel;
  1709. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1710. goto nest_cancel;
  1711. break;
  1712. default:
  1713. BUG();
  1714. }
  1715. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1716. goto nest_cancel;
  1717. if (opt_inst->changed) {
  1718. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1719. goto nest_cancel;
  1720. opt_inst->changed = false;
  1721. }
  1722. nla_nest_end(skb, option_item);
  1723. return 0;
  1724. nest_cancel:
  1725. nla_nest_cancel(skb, option_item);
  1726. return -EMSGSIZE;
  1727. }
  1728. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1729. struct team *team, u32 portid,
  1730. team_nl_send_func_t *send_func)
  1731. {
  1732. int err;
  1733. if (*pskb) {
  1734. err = send_func(*pskb, team, portid);
  1735. if (err)
  1736. return err;
  1737. }
  1738. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1739. if (!*pskb)
  1740. return -ENOMEM;
  1741. return 0;
  1742. }
  1743. static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
  1744. int flags, team_nl_send_func_t *send_func,
  1745. struct list_head *sel_opt_inst_list)
  1746. {
  1747. struct nlattr *option_list;
  1748. struct nlmsghdr *nlh;
  1749. void *hdr;
  1750. struct team_option_inst *opt_inst;
  1751. int err;
  1752. struct sk_buff *skb = NULL;
  1753. bool incomplete;
  1754. int i;
  1755. opt_inst = list_first_entry(sel_opt_inst_list,
  1756. struct team_option_inst, tmp_list);
  1757. start_again:
  1758. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1759. if (err)
  1760. return err;
  1761. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  1762. TEAM_CMD_OPTIONS_GET);
  1763. if (!hdr)
  1764. return -EMSGSIZE;
  1765. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1766. goto nla_put_failure;
  1767. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1768. if (!option_list)
  1769. goto nla_put_failure;
  1770. i = 0;
  1771. incomplete = false;
  1772. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  1773. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  1774. if (err) {
  1775. if (err == -EMSGSIZE) {
  1776. if (!i)
  1777. goto errout;
  1778. incomplete = true;
  1779. break;
  1780. }
  1781. goto errout;
  1782. }
  1783. i++;
  1784. }
  1785. nla_nest_end(skb, option_list);
  1786. genlmsg_end(skb, hdr);
  1787. if (incomplete)
  1788. goto start_again;
  1789. send_done:
  1790. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1791. if (!nlh) {
  1792. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1793. if (err)
  1794. goto errout;
  1795. goto send_done;
  1796. }
  1797. return send_func(skb, team, portid);
  1798. nla_put_failure:
  1799. err = -EMSGSIZE;
  1800. errout:
  1801. genlmsg_cancel(skb, hdr);
  1802. nlmsg_free(skb);
  1803. return err;
  1804. }
  1805. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1806. {
  1807. struct team *team;
  1808. struct team_option_inst *opt_inst;
  1809. int err;
  1810. LIST_HEAD(sel_opt_inst_list);
  1811. team = team_nl_team_get(info);
  1812. if (!team)
  1813. return -EINVAL;
  1814. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  1815. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  1816. err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
  1817. NLM_F_ACK, team_nl_send_unicast,
  1818. &sel_opt_inst_list);
  1819. team_nl_team_put(team);
  1820. return err;
  1821. }
  1822. static int team_nl_send_event_options_get(struct team *team,
  1823. struct list_head *sel_opt_inst_list);
  1824. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1825. {
  1826. struct team *team;
  1827. int err = 0;
  1828. int i;
  1829. struct nlattr *nl_option;
  1830. LIST_HEAD(opt_inst_list);
  1831. team = team_nl_team_get(info);
  1832. if (!team)
  1833. return -EINVAL;
  1834. err = -EINVAL;
  1835. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  1836. err = -EINVAL;
  1837. goto team_put;
  1838. }
  1839. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  1840. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  1841. struct nlattr *attr;
  1842. struct nlattr *attr_data;
  1843. enum team_option_type opt_type;
  1844. int opt_port_ifindex = 0; /* != 0 for per-port options */
  1845. u32 opt_array_index = 0;
  1846. bool opt_is_array = false;
  1847. struct team_option_inst *opt_inst;
  1848. char *opt_name;
  1849. bool opt_found = false;
  1850. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  1851. err = -EINVAL;
  1852. goto team_put;
  1853. }
  1854. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  1855. nl_option, team_nl_option_policy);
  1856. if (err)
  1857. goto team_put;
  1858. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  1859. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  1860. err = -EINVAL;
  1861. goto team_put;
  1862. }
  1863. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  1864. case NLA_U32:
  1865. opt_type = TEAM_OPTION_TYPE_U32;
  1866. break;
  1867. case NLA_STRING:
  1868. opt_type = TEAM_OPTION_TYPE_STRING;
  1869. break;
  1870. case NLA_BINARY:
  1871. opt_type = TEAM_OPTION_TYPE_BINARY;
  1872. break;
  1873. case NLA_FLAG:
  1874. opt_type = TEAM_OPTION_TYPE_BOOL;
  1875. break;
  1876. case NLA_S32:
  1877. opt_type = TEAM_OPTION_TYPE_S32;
  1878. break;
  1879. default:
  1880. goto team_put;
  1881. }
  1882. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  1883. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  1884. err = -EINVAL;
  1885. goto team_put;
  1886. }
  1887. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  1888. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  1889. if (attr)
  1890. opt_port_ifindex = nla_get_u32(attr);
  1891. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  1892. if (attr) {
  1893. opt_is_array = true;
  1894. opt_array_index = nla_get_u32(attr);
  1895. }
  1896. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  1897. struct team_option *option = opt_inst->option;
  1898. struct team_gsetter_ctx ctx;
  1899. struct team_option_inst_info *opt_inst_info;
  1900. int tmp_ifindex;
  1901. opt_inst_info = &opt_inst->info;
  1902. tmp_ifindex = opt_inst_info->port ?
  1903. opt_inst_info->port->dev->ifindex : 0;
  1904. if (option->type != opt_type ||
  1905. strcmp(option->name, opt_name) ||
  1906. tmp_ifindex != opt_port_ifindex ||
  1907. (option->array_size && !opt_is_array) ||
  1908. opt_inst_info->array_index != opt_array_index)
  1909. continue;
  1910. opt_found = true;
  1911. ctx.info = opt_inst_info;
  1912. switch (opt_type) {
  1913. case TEAM_OPTION_TYPE_U32:
  1914. ctx.data.u32_val = nla_get_u32(attr_data);
  1915. break;
  1916. case TEAM_OPTION_TYPE_STRING:
  1917. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  1918. err = -EINVAL;
  1919. goto team_put;
  1920. }
  1921. ctx.data.str_val = nla_data(attr_data);
  1922. break;
  1923. case TEAM_OPTION_TYPE_BINARY:
  1924. ctx.data.bin_val.len = nla_len(attr_data);
  1925. ctx.data.bin_val.ptr = nla_data(attr_data);
  1926. break;
  1927. case TEAM_OPTION_TYPE_BOOL:
  1928. ctx.data.bool_val = attr_data ? true : false;
  1929. break;
  1930. case TEAM_OPTION_TYPE_S32:
  1931. ctx.data.s32_val = nla_get_s32(attr_data);
  1932. break;
  1933. default:
  1934. BUG();
  1935. }
  1936. err = team_option_set(team, opt_inst, &ctx);
  1937. if (err)
  1938. goto team_put;
  1939. opt_inst->changed = true;
  1940. list_add(&opt_inst->tmp_list, &opt_inst_list);
  1941. }
  1942. if (!opt_found) {
  1943. err = -ENOENT;
  1944. goto team_put;
  1945. }
  1946. }
  1947. err = team_nl_send_event_options_get(team, &opt_inst_list);
  1948. team_put:
  1949. team_nl_team_put(team);
  1950. return err;
  1951. }
  1952. static int team_nl_fill_one_port_get(struct sk_buff *skb,
  1953. struct team_port *port)
  1954. {
  1955. struct nlattr *port_item;
  1956. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  1957. if (!port_item)
  1958. goto nest_cancel;
  1959. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  1960. goto nest_cancel;
  1961. if (port->changed) {
  1962. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  1963. goto nest_cancel;
  1964. port->changed = false;
  1965. }
  1966. if ((port->removed &&
  1967. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  1968. (port->state.linkup &&
  1969. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  1970. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  1971. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  1972. goto nest_cancel;
  1973. nla_nest_end(skb, port_item);
  1974. return 0;
  1975. nest_cancel:
  1976. nla_nest_cancel(skb, port_item);
  1977. return -EMSGSIZE;
  1978. }
  1979. static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
  1980. int flags, team_nl_send_func_t *send_func,
  1981. struct team_port *one_port)
  1982. {
  1983. struct nlattr *port_list;
  1984. struct nlmsghdr *nlh;
  1985. void *hdr;
  1986. struct team_port *port;
  1987. int err;
  1988. struct sk_buff *skb = NULL;
  1989. bool incomplete;
  1990. int i;
  1991. port = list_first_entry(&team->port_list, struct team_port, list);
  1992. start_again:
  1993. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1994. if (err)
  1995. return err;
  1996. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  1997. TEAM_CMD_PORT_LIST_GET);
  1998. if (!hdr)
  1999. return -EMSGSIZE;
  2000. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2001. goto nla_put_failure;
  2002. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  2003. if (!port_list)
  2004. goto nla_put_failure;
  2005. i = 0;
  2006. incomplete = false;
  2007. /* If one port is selected, called wants to send port list containing
  2008. * only this port. Otherwise go through all listed ports and send all
  2009. */
  2010. if (one_port) {
  2011. err = team_nl_fill_one_port_get(skb, one_port);
  2012. if (err)
  2013. goto errout;
  2014. } else {
  2015. list_for_each_entry(port, &team->port_list, list) {
  2016. err = team_nl_fill_one_port_get(skb, port);
  2017. if (err) {
  2018. if (err == -EMSGSIZE) {
  2019. if (!i)
  2020. goto errout;
  2021. incomplete = true;
  2022. break;
  2023. }
  2024. goto errout;
  2025. }
  2026. i++;
  2027. }
  2028. }
  2029. nla_nest_end(skb, port_list);
  2030. genlmsg_end(skb, hdr);
  2031. if (incomplete)
  2032. goto start_again;
  2033. send_done:
  2034. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2035. if (!nlh) {
  2036. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2037. if (err)
  2038. goto errout;
  2039. goto send_done;
  2040. }
  2041. return send_func(skb, team, portid);
  2042. nla_put_failure:
  2043. err = -EMSGSIZE;
  2044. errout:
  2045. genlmsg_cancel(skb, hdr);
  2046. nlmsg_free(skb);
  2047. return err;
  2048. }
  2049. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  2050. struct genl_info *info)
  2051. {
  2052. struct team *team;
  2053. int err;
  2054. team = team_nl_team_get(info);
  2055. if (!team)
  2056. return -EINVAL;
  2057. err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
  2058. NLM_F_ACK, team_nl_send_unicast, NULL);
  2059. team_nl_team_put(team);
  2060. return err;
  2061. }
  2062. static struct genl_ops team_nl_ops[] = {
  2063. {
  2064. .cmd = TEAM_CMD_NOOP,
  2065. .doit = team_nl_cmd_noop,
  2066. .policy = team_nl_policy,
  2067. },
  2068. {
  2069. .cmd = TEAM_CMD_OPTIONS_SET,
  2070. .doit = team_nl_cmd_options_set,
  2071. .policy = team_nl_policy,
  2072. .flags = GENL_ADMIN_PERM,
  2073. },
  2074. {
  2075. .cmd = TEAM_CMD_OPTIONS_GET,
  2076. .doit = team_nl_cmd_options_get,
  2077. .policy = team_nl_policy,
  2078. .flags = GENL_ADMIN_PERM,
  2079. },
  2080. {
  2081. .cmd = TEAM_CMD_PORT_LIST_GET,
  2082. .doit = team_nl_cmd_port_list_get,
  2083. .policy = team_nl_policy,
  2084. .flags = GENL_ADMIN_PERM,
  2085. },
  2086. };
  2087. static struct genl_multicast_group team_change_event_mcgrp = {
  2088. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  2089. };
  2090. static int team_nl_send_multicast(struct sk_buff *skb,
  2091. struct team *team, u32 portid)
  2092. {
  2093. return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
  2094. team_change_event_mcgrp.id, GFP_KERNEL);
  2095. }
  2096. static int team_nl_send_event_options_get(struct team *team,
  2097. struct list_head *sel_opt_inst_list)
  2098. {
  2099. return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
  2100. sel_opt_inst_list);
  2101. }
  2102. static int team_nl_send_event_port_get(struct team *team,
  2103. struct team_port *port)
  2104. {
  2105. return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
  2106. port);
  2107. }
  2108. static int team_nl_init(void)
  2109. {
  2110. int err;
  2111. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  2112. ARRAY_SIZE(team_nl_ops));
  2113. if (err)
  2114. return err;
  2115. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  2116. if (err)
  2117. goto err_change_event_grp_reg;
  2118. return 0;
  2119. err_change_event_grp_reg:
  2120. genl_unregister_family(&team_nl_family);
  2121. return err;
  2122. }
  2123. static void team_nl_fini(void)
  2124. {
  2125. genl_unregister_family(&team_nl_family);
  2126. }
  2127. /******************
  2128. * Change checkers
  2129. ******************/
  2130. static void __team_options_change_check(struct team *team)
  2131. {
  2132. int err;
  2133. struct team_option_inst *opt_inst;
  2134. LIST_HEAD(sel_opt_inst_list);
  2135. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2136. if (opt_inst->changed)
  2137. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2138. }
  2139. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2140. if (err && err != -ESRCH)
  2141. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2142. err);
  2143. }
  2144. /* rtnl lock is held */
  2145. static void __team_port_change_send(struct team_port *port, bool linkup)
  2146. {
  2147. int err;
  2148. port->changed = true;
  2149. port->state.linkup = linkup;
  2150. team_refresh_port_linkup(port);
  2151. if (linkup) {
  2152. struct ethtool_cmd ecmd;
  2153. err = __ethtool_get_settings(port->dev, &ecmd);
  2154. if (!err) {
  2155. port->state.speed = ethtool_cmd_speed(&ecmd);
  2156. port->state.duplex = ecmd.duplex;
  2157. goto send_event;
  2158. }
  2159. }
  2160. port->state.speed = 0;
  2161. port->state.duplex = 0;
  2162. send_event:
  2163. err = team_nl_send_event_port_get(port->team, port);
  2164. if (err && err != -ESRCH)
  2165. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
  2166. port->dev->name, err);
  2167. }
  2168. static void __team_carrier_check(struct team *team)
  2169. {
  2170. struct team_port *port;
  2171. bool team_linkup;
  2172. team_linkup = false;
  2173. list_for_each_entry(port, &team->port_list, list) {
  2174. if (port->linkup) {
  2175. team_linkup = true;
  2176. break;
  2177. }
  2178. }
  2179. if (team_linkup)
  2180. netif_carrier_on(team->dev);
  2181. else
  2182. netif_carrier_off(team->dev);
  2183. }
  2184. static void __team_port_change_check(struct team_port *port, bool linkup)
  2185. {
  2186. if (port->state.linkup != linkup)
  2187. __team_port_change_send(port, linkup);
  2188. __team_carrier_check(port->team);
  2189. }
  2190. static void __team_port_change_port_added(struct team_port *port, bool linkup)
  2191. {
  2192. __team_port_change_send(port, linkup);
  2193. __team_carrier_check(port->team);
  2194. }
  2195. static void __team_port_change_port_removed(struct team_port *port)
  2196. {
  2197. port->removed = true;
  2198. __team_port_change_send(port, false);
  2199. __team_carrier_check(port->team);
  2200. }
  2201. static void team_port_change_check(struct team_port *port, bool linkup)
  2202. {
  2203. struct team *team = port->team;
  2204. mutex_lock(&team->lock);
  2205. __team_port_change_check(port, linkup);
  2206. mutex_unlock(&team->lock);
  2207. }
  2208. /************************************
  2209. * Net device notifier event handler
  2210. ************************************/
  2211. static int team_device_event(struct notifier_block *unused,
  2212. unsigned long event, void *ptr)
  2213. {
  2214. struct net_device *dev = (struct net_device *) ptr;
  2215. struct team_port *port;
  2216. port = team_port_get_rtnl(dev);
  2217. if (!port)
  2218. return NOTIFY_DONE;
  2219. switch (event) {
  2220. case NETDEV_UP:
  2221. if (netif_carrier_ok(dev))
  2222. team_port_change_check(port, true);
  2223. case NETDEV_DOWN:
  2224. team_port_change_check(port, false);
  2225. case NETDEV_CHANGE:
  2226. if (netif_running(port->dev))
  2227. team_port_change_check(port,
  2228. !!netif_carrier_ok(port->dev));
  2229. break;
  2230. case NETDEV_UNREGISTER:
  2231. team_del_slave(port->team->dev, dev);
  2232. break;
  2233. case NETDEV_FEAT_CHANGE:
  2234. team_compute_features(port->team);
  2235. break;
  2236. case NETDEV_CHANGEMTU:
  2237. /* Forbid to change mtu of underlaying device */
  2238. return NOTIFY_BAD;
  2239. case NETDEV_PRE_TYPE_CHANGE:
  2240. /* Forbid to change type of underlaying device */
  2241. return NOTIFY_BAD;
  2242. }
  2243. return NOTIFY_DONE;
  2244. }
  2245. static struct notifier_block team_notifier_block __read_mostly = {
  2246. .notifier_call = team_device_event,
  2247. };
  2248. /***********************
  2249. * Module init and exit
  2250. ***********************/
  2251. static int __init team_module_init(void)
  2252. {
  2253. int err;
  2254. register_netdevice_notifier(&team_notifier_block);
  2255. err = rtnl_link_register(&team_link_ops);
  2256. if (err)
  2257. goto err_rtnl_reg;
  2258. err = team_nl_init();
  2259. if (err)
  2260. goto err_nl_init;
  2261. return 0;
  2262. err_nl_init:
  2263. rtnl_link_unregister(&team_link_ops);
  2264. err_rtnl_reg:
  2265. unregister_netdevice_notifier(&team_notifier_block);
  2266. return err;
  2267. }
  2268. static void __exit team_module_exit(void)
  2269. {
  2270. team_nl_fini();
  2271. rtnl_link_unregister(&team_link_ops);
  2272. unregister_netdevice_notifier(&team_notifier_block);
  2273. }
  2274. module_init(team_module_init);
  2275. module_exit(team_module_exit);
  2276. MODULE_LICENSE("GPL v2");
  2277. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2278. MODULE_DESCRIPTION("Ethernet team device driver");
  2279. MODULE_ALIAS_RTNL_LINK(DRV_NAME);