team.c 64 KB

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