team.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  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/if_vlan.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/socket.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/rtnetlink.h>
  25. #include <net/rtnetlink.h>
  26. #include <net/genetlink.h>
  27. #include <net/netlink.h>
  28. #include <linux/if_team.h>
  29. #define DRV_NAME "team"
  30. /**********
  31. * Helpers
  32. **********/
  33. #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
  34. static struct team_port *team_port_get_rcu(const struct net_device *dev)
  35. {
  36. struct team_port *port = rcu_dereference(dev->rx_handler_data);
  37. return team_port_exists(dev) ? port : NULL;
  38. }
  39. static struct team_port *team_port_get_rtnl(const struct net_device *dev)
  40. {
  41. struct team_port *port = rtnl_dereference(dev->rx_handler_data);
  42. return team_port_exists(dev) ? port : NULL;
  43. }
  44. /*
  45. * Since the ability to change mac address for open port device is tested in
  46. * team_port_add, this function can be called without control of return value
  47. */
  48. static int __set_port_mac(struct net_device *port_dev,
  49. const unsigned char *dev_addr)
  50. {
  51. struct sockaddr addr;
  52. memcpy(addr.sa_data, dev_addr, ETH_ALEN);
  53. addr.sa_family = ARPHRD_ETHER;
  54. return dev_set_mac_address(port_dev, &addr);
  55. }
  56. static int team_port_set_orig_mac(struct team_port *port)
  57. {
  58. return __set_port_mac(port->dev, port->orig.dev_addr);
  59. }
  60. int team_port_set_team_mac(struct team_port *port)
  61. {
  62. return __set_port_mac(port->dev, port->team->dev->dev_addr);
  63. }
  64. EXPORT_SYMBOL(team_port_set_team_mac);
  65. static void team_refresh_port_linkup(struct team_port *port)
  66. {
  67. port->linkup = port->user.linkup_enabled ? port->user.linkup :
  68. port->state.linkup;
  69. }
  70. /*******************
  71. * Options handling
  72. *******************/
  73. struct team_option_inst { /* One for each option instance */
  74. struct list_head list;
  75. struct list_head tmp_list;
  76. struct team_option *option;
  77. struct team_option_inst_info info;
  78. bool changed;
  79. bool removed;
  80. };
  81. static struct team_option *__team_find_option(struct team *team,
  82. const char *opt_name)
  83. {
  84. struct team_option *option;
  85. list_for_each_entry(option, &team->option_list, list) {
  86. if (strcmp(option->name, opt_name) == 0)
  87. return option;
  88. }
  89. return NULL;
  90. }
  91. static void __team_option_inst_del(struct team_option_inst *opt_inst)
  92. {
  93. list_del(&opt_inst->list);
  94. kfree(opt_inst);
  95. }
  96. static void __team_option_inst_del_option(struct team *team,
  97. struct team_option *option)
  98. {
  99. struct team_option_inst *opt_inst, *tmp;
  100. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  101. if (opt_inst->option == option)
  102. __team_option_inst_del(opt_inst);
  103. }
  104. }
  105. static int __team_option_inst_add(struct team *team, struct team_option *option,
  106. struct team_port *port)
  107. {
  108. struct team_option_inst *opt_inst;
  109. unsigned int array_size;
  110. unsigned int i;
  111. int err;
  112. array_size = option->array_size;
  113. if (!array_size)
  114. array_size = 1; /* No array but still need one instance */
  115. for (i = 0; i < array_size; i++) {
  116. opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
  117. if (!opt_inst)
  118. return -ENOMEM;
  119. opt_inst->option = option;
  120. opt_inst->info.port = port;
  121. opt_inst->info.array_index = i;
  122. opt_inst->changed = true;
  123. opt_inst->removed = false;
  124. list_add_tail(&opt_inst->list, &team->option_inst_list);
  125. if (option->init) {
  126. err = option->init(team, &opt_inst->info);
  127. if (err)
  128. return err;
  129. }
  130. }
  131. return 0;
  132. }
  133. static int __team_option_inst_add_option(struct team *team,
  134. struct team_option *option)
  135. {
  136. struct team_port *port;
  137. int err;
  138. if (!option->per_port) {
  139. err = __team_option_inst_add(team, option, 0);
  140. if (err)
  141. goto inst_del_option;
  142. }
  143. list_for_each_entry(port, &team->port_list, list) {
  144. err = __team_option_inst_add(team, option, port);
  145. if (err)
  146. goto inst_del_option;
  147. }
  148. return 0;
  149. inst_del_option:
  150. __team_option_inst_del_option(team, option);
  151. return err;
  152. }
  153. static void __team_option_inst_mark_removed_option(struct team *team,
  154. struct team_option *option)
  155. {
  156. struct team_option_inst *opt_inst;
  157. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  158. if (opt_inst->option == option) {
  159. opt_inst->changed = true;
  160. opt_inst->removed = true;
  161. }
  162. }
  163. }
  164. static void __team_option_inst_del_port(struct team *team,
  165. struct team_port *port)
  166. {
  167. struct team_option_inst *opt_inst, *tmp;
  168. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  169. if (opt_inst->option->per_port &&
  170. opt_inst->info.port == port)
  171. __team_option_inst_del(opt_inst);
  172. }
  173. }
  174. static int __team_option_inst_add_port(struct team *team,
  175. struct team_port *port)
  176. {
  177. struct team_option *option;
  178. int err;
  179. list_for_each_entry(option, &team->option_list, list) {
  180. if (!option->per_port)
  181. continue;
  182. err = __team_option_inst_add(team, option, port);
  183. if (err)
  184. goto inst_del_port;
  185. }
  186. return 0;
  187. inst_del_port:
  188. __team_option_inst_del_port(team, port);
  189. return err;
  190. }
  191. static void __team_option_inst_mark_removed_port(struct team *team,
  192. struct team_port *port)
  193. {
  194. struct team_option_inst *opt_inst;
  195. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  196. if (opt_inst->info.port == port) {
  197. opt_inst->changed = true;
  198. opt_inst->removed = true;
  199. }
  200. }
  201. }
  202. static int __team_options_register(struct team *team,
  203. const struct team_option *option,
  204. size_t option_count)
  205. {
  206. int i;
  207. struct team_option **dst_opts;
  208. int err;
  209. dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
  210. GFP_KERNEL);
  211. if (!dst_opts)
  212. return -ENOMEM;
  213. for (i = 0; i < option_count; i++, option++) {
  214. if (__team_find_option(team, option->name)) {
  215. err = -EEXIST;
  216. goto alloc_rollback;
  217. }
  218. dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
  219. if (!dst_opts[i]) {
  220. err = -ENOMEM;
  221. goto alloc_rollback;
  222. }
  223. }
  224. for (i = 0; i < option_count; i++) {
  225. err = __team_option_inst_add_option(team, dst_opts[i]);
  226. if (err)
  227. goto inst_rollback;
  228. list_add_tail(&dst_opts[i]->list, &team->option_list);
  229. }
  230. kfree(dst_opts);
  231. return 0;
  232. inst_rollback:
  233. for (i--; i >= 0; i--)
  234. __team_option_inst_del_option(team, dst_opts[i]);
  235. i = option_count - 1;
  236. alloc_rollback:
  237. for (i--; i >= 0; i--)
  238. kfree(dst_opts[i]);
  239. kfree(dst_opts);
  240. return err;
  241. }
  242. static void __team_options_mark_removed(struct team *team,
  243. const struct team_option *option,
  244. size_t option_count)
  245. {
  246. int i;
  247. for (i = 0; i < option_count; i++, option++) {
  248. struct team_option *del_opt;
  249. del_opt = __team_find_option(team, option->name);
  250. if (del_opt)
  251. __team_option_inst_mark_removed_option(team, del_opt);
  252. }
  253. }
  254. static void __team_options_unregister(struct team *team,
  255. const struct team_option *option,
  256. size_t option_count)
  257. {
  258. int i;
  259. for (i = 0; i < option_count; i++, option++) {
  260. struct team_option *del_opt;
  261. del_opt = __team_find_option(team, option->name);
  262. if (del_opt) {
  263. __team_option_inst_del_option(team, del_opt);
  264. list_del(&del_opt->list);
  265. kfree(del_opt);
  266. }
  267. }
  268. }
  269. static void __team_options_change_check(struct team *team);
  270. static void __team_option_inst_change(struct team *team,
  271. struct team_option_inst *opt_inst);
  272. int team_options_register(struct team *team,
  273. const struct team_option *option,
  274. size_t option_count)
  275. {
  276. int err;
  277. err = __team_options_register(team, option, option_count);
  278. if (err)
  279. return err;
  280. __team_options_change_check(team);
  281. return 0;
  282. }
  283. EXPORT_SYMBOL(team_options_register);
  284. void team_options_unregister(struct team *team,
  285. const struct team_option *option,
  286. size_t option_count)
  287. {
  288. __team_options_mark_removed(team, option, option_count);
  289. __team_options_change_check(team);
  290. __team_options_unregister(team, option, option_count);
  291. }
  292. EXPORT_SYMBOL(team_options_unregister);
  293. static int team_option_port_add(struct team *team, struct team_port *port)
  294. {
  295. int err;
  296. err = __team_option_inst_add_port(team, port);
  297. if (err)
  298. return err;
  299. __team_options_change_check(team);
  300. return 0;
  301. }
  302. static void team_option_port_del(struct team *team, struct team_port *port)
  303. {
  304. __team_option_inst_mark_removed_port(team, port);
  305. __team_options_change_check(team);
  306. __team_option_inst_del_port(team, port);
  307. }
  308. static int team_option_get(struct team *team,
  309. struct team_option_inst *opt_inst,
  310. struct team_gsetter_ctx *ctx)
  311. {
  312. if (!opt_inst->option->getter)
  313. return -EOPNOTSUPP;
  314. return opt_inst->option->getter(team, ctx);
  315. }
  316. static int team_option_set(struct team *team,
  317. struct team_option_inst *opt_inst,
  318. struct team_gsetter_ctx *ctx)
  319. {
  320. int err;
  321. if (!opt_inst->option->setter)
  322. return -EOPNOTSUPP;
  323. err = opt_inst->option->setter(team, ctx);
  324. if (err)
  325. return err;
  326. __team_option_inst_change(team, opt_inst);
  327. return err;
  328. }
  329. void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
  330. {
  331. struct team_option_inst *opt_inst;
  332. opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
  333. opt_inst->changed = true;
  334. }
  335. EXPORT_SYMBOL(team_option_inst_set_change);
  336. void team_options_change_check(struct team *team)
  337. {
  338. __team_options_change_check(team);
  339. }
  340. EXPORT_SYMBOL(team_options_change_check);
  341. /****************
  342. * Mode handling
  343. ****************/
  344. static LIST_HEAD(mode_list);
  345. static DEFINE_SPINLOCK(mode_list_lock);
  346. struct team_mode_item {
  347. struct list_head list;
  348. const struct team_mode *mode;
  349. };
  350. static struct team_mode_item *__find_mode(const char *kind)
  351. {
  352. struct team_mode_item *mitem;
  353. list_for_each_entry(mitem, &mode_list, list) {
  354. if (strcmp(mitem->mode->kind, kind) == 0)
  355. return mitem;
  356. }
  357. return NULL;
  358. }
  359. static bool is_good_mode_name(const char *name)
  360. {
  361. while (*name != '\0') {
  362. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  363. return false;
  364. name++;
  365. }
  366. return true;
  367. }
  368. int team_mode_register(const struct team_mode *mode)
  369. {
  370. int err = 0;
  371. struct team_mode_item *mitem;
  372. if (!is_good_mode_name(mode->kind) ||
  373. mode->priv_size > TEAM_MODE_PRIV_SIZE)
  374. return -EINVAL;
  375. mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
  376. if (!mitem)
  377. return -ENOMEM;
  378. spin_lock(&mode_list_lock);
  379. if (__find_mode(mode->kind)) {
  380. err = -EEXIST;
  381. kfree(mitem);
  382. goto unlock;
  383. }
  384. mitem->mode = mode;
  385. list_add_tail(&mitem->list, &mode_list);
  386. unlock:
  387. spin_unlock(&mode_list_lock);
  388. return err;
  389. }
  390. EXPORT_SYMBOL(team_mode_register);
  391. void team_mode_unregister(const struct team_mode *mode)
  392. {
  393. struct team_mode_item *mitem;
  394. spin_lock(&mode_list_lock);
  395. mitem = __find_mode(mode->kind);
  396. if (mitem) {
  397. list_del_init(&mitem->list);
  398. kfree(mitem);
  399. }
  400. spin_unlock(&mode_list_lock);
  401. }
  402. EXPORT_SYMBOL(team_mode_unregister);
  403. static const struct team_mode *team_mode_get(const char *kind)
  404. {
  405. struct team_mode_item *mitem;
  406. const struct team_mode *mode = NULL;
  407. spin_lock(&mode_list_lock);
  408. mitem = __find_mode(kind);
  409. if (!mitem) {
  410. spin_unlock(&mode_list_lock);
  411. request_module("team-mode-%s", kind);
  412. spin_lock(&mode_list_lock);
  413. mitem = __find_mode(kind);
  414. }
  415. if (mitem) {
  416. mode = mitem->mode;
  417. if (!try_module_get(mode->owner))
  418. mode = NULL;
  419. }
  420. spin_unlock(&mode_list_lock);
  421. return mode;
  422. }
  423. static void team_mode_put(const struct team_mode *mode)
  424. {
  425. module_put(mode->owner);
  426. }
  427. static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
  428. {
  429. dev_kfree_skb_any(skb);
  430. return false;
  431. }
  432. rx_handler_result_t team_dummy_receive(struct team *team,
  433. struct team_port *port,
  434. struct sk_buff *skb)
  435. {
  436. return RX_HANDLER_ANOTHER;
  437. }
  438. static const struct team_mode __team_no_mode = {
  439. .kind = "*NOMODE*",
  440. };
  441. static bool team_is_mode_set(struct team *team)
  442. {
  443. return team->mode != &__team_no_mode;
  444. }
  445. static void team_set_no_mode(struct team *team)
  446. {
  447. team->mode = &__team_no_mode;
  448. }
  449. static void team_adjust_ops(struct team *team)
  450. {
  451. /*
  452. * To avoid checks in rx/tx skb paths, ensure here that non-null and
  453. * correct ops are always set.
  454. */
  455. if (list_empty(&team->port_list) ||
  456. !team_is_mode_set(team) || !team->mode->ops->transmit)
  457. team->ops.transmit = team_dummy_transmit;
  458. else
  459. team->ops.transmit = team->mode->ops->transmit;
  460. if (list_empty(&team->port_list) ||
  461. !team_is_mode_set(team) || !team->mode->ops->receive)
  462. team->ops.receive = team_dummy_receive;
  463. else
  464. team->ops.receive = team->mode->ops->receive;
  465. }
  466. /*
  467. * We can benefit from the fact that it's ensured no port is present
  468. * at the time of mode change. Therefore no packets are in fly so there's no
  469. * need to set mode operations in any special way.
  470. */
  471. static int __team_change_mode(struct team *team,
  472. const struct team_mode *new_mode)
  473. {
  474. /* Check if mode was previously set and do cleanup if so */
  475. if (team_is_mode_set(team)) {
  476. void (*exit_op)(struct team *team) = team->ops.exit;
  477. /* Clear ops area so no callback is called any longer */
  478. memset(&team->ops, 0, sizeof(struct team_mode_ops));
  479. team_adjust_ops(team);
  480. if (exit_op)
  481. exit_op(team);
  482. team_mode_put(team->mode);
  483. team_set_no_mode(team);
  484. /* zero private data area */
  485. memset(&team->mode_priv, 0,
  486. sizeof(struct team) - offsetof(struct team, mode_priv));
  487. }
  488. if (!new_mode)
  489. return 0;
  490. if (new_mode->ops->init) {
  491. int err;
  492. err = new_mode->ops->init(team);
  493. if (err)
  494. return err;
  495. }
  496. team->mode = new_mode;
  497. memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
  498. team_adjust_ops(team);
  499. return 0;
  500. }
  501. static int team_change_mode(struct team *team, const char *kind)
  502. {
  503. const struct team_mode *new_mode;
  504. struct net_device *dev = team->dev;
  505. int err;
  506. if (!list_empty(&team->port_list)) {
  507. netdev_err(dev, "No ports can be present during mode change\n");
  508. return -EBUSY;
  509. }
  510. if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
  511. netdev_err(dev, "Unable to change to the same mode the team is in\n");
  512. return -EINVAL;
  513. }
  514. new_mode = team_mode_get(kind);
  515. if (!new_mode) {
  516. netdev_err(dev, "Mode \"%s\" not found\n", kind);
  517. return -EINVAL;
  518. }
  519. err = __team_change_mode(team, new_mode);
  520. if (err) {
  521. netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
  522. team_mode_put(new_mode);
  523. return err;
  524. }
  525. netdev_info(dev, "Mode changed to \"%s\"\n", kind);
  526. return 0;
  527. }
  528. /************************
  529. * Rx path frame handler
  530. ************************/
  531. static bool team_port_enabled(struct team_port *port);
  532. /* note: already called with rcu_read_lock */
  533. static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
  534. {
  535. struct sk_buff *skb = *pskb;
  536. struct team_port *port;
  537. struct team *team;
  538. rx_handler_result_t res;
  539. skb = skb_share_check(skb, GFP_ATOMIC);
  540. if (!skb)
  541. return RX_HANDLER_CONSUMED;
  542. *pskb = skb;
  543. port = team_port_get_rcu(skb->dev);
  544. team = port->team;
  545. if (!team_port_enabled(port)) {
  546. /* allow exact match delivery for disabled ports */
  547. res = RX_HANDLER_EXACT;
  548. } else {
  549. res = team->ops.receive(team, port, skb);
  550. }
  551. if (res == RX_HANDLER_ANOTHER) {
  552. struct team_pcpu_stats *pcpu_stats;
  553. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  554. u64_stats_update_begin(&pcpu_stats->syncp);
  555. pcpu_stats->rx_packets++;
  556. pcpu_stats->rx_bytes += skb->len;
  557. if (skb->pkt_type == PACKET_MULTICAST)
  558. pcpu_stats->rx_multicast++;
  559. u64_stats_update_end(&pcpu_stats->syncp);
  560. skb->dev = team->dev;
  561. } else {
  562. this_cpu_inc(team->pcpu_stats->rx_dropped);
  563. }
  564. return res;
  565. }
  566. /****************
  567. * Port handling
  568. ****************/
  569. static bool team_port_find(const struct team *team,
  570. const struct team_port *port)
  571. {
  572. struct team_port *cur;
  573. list_for_each_entry(cur, &team->port_list, list)
  574. if (cur == port)
  575. return true;
  576. return false;
  577. }
  578. static bool team_port_enabled(struct team_port *port)
  579. {
  580. return port->index != -1;
  581. }
  582. /*
  583. * Enable/disable port by adding to enabled port hashlist and setting
  584. * port->index (Might be racy so reader could see incorrect ifindex when
  585. * processing a flying packet, but that is not a problem). Write guarded
  586. * by team->lock.
  587. */
  588. static void team_port_enable(struct team *team,
  589. struct team_port *port)
  590. {
  591. if (team_port_enabled(port))
  592. return;
  593. port->index = team->en_port_count++;
  594. hlist_add_head_rcu(&port->hlist,
  595. team_port_index_hash(team, port->index));
  596. }
  597. static void __reconstruct_port_hlist(struct team *team, int rm_index)
  598. {
  599. int i;
  600. struct team_port *port;
  601. for (i = rm_index + 1; i < team->en_port_count; i++) {
  602. port = team_get_port_by_index(team, i);
  603. hlist_del_rcu(&port->hlist);
  604. port->index--;
  605. hlist_add_head_rcu(&port->hlist,
  606. team_port_index_hash(team, port->index));
  607. }
  608. }
  609. static void team_port_disable(struct team *team,
  610. struct team_port *port)
  611. {
  612. int rm_index = port->index;
  613. if (!team_port_enabled(port))
  614. return;
  615. hlist_del_rcu(&port->hlist);
  616. __reconstruct_port_hlist(team, rm_index);
  617. team->en_port_count--;
  618. port->index = -1;
  619. }
  620. #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
  621. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  622. NETIF_F_HIGHDMA | NETIF_F_LRO)
  623. static void __team_compute_features(struct team *team)
  624. {
  625. struct team_port *port;
  626. u32 vlan_features = TEAM_VLAN_FEATURES;
  627. unsigned short max_hard_header_len = ETH_HLEN;
  628. list_for_each_entry(port, &team->port_list, list) {
  629. vlan_features = netdev_increment_features(vlan_features,
  630. port->dev->vlan_features,
  631. TEAM_VLAN_FEATURES);
  632. if (port->dev->hard_header_len > max_hard_header_len)
  633. max_hard_header_len = port->dev->hard_header_len;
  634. }
  635. team->dev->vlan_features = vlan_features;
  636. team->dev->hard_header_len = max_hard_header_len;
  637. netdev_change_features(team->dev);
  638. }
  639. static void team_compute_features(struct team *team)
  640. {
  641. mutex_lock(&team->lock);
  642. __team_compute_features(team);
  643. mutex_unlock(&team->lock);
  644. }
  645. static int team_port_enter(struct team *team, struct team_port *port)
  646. {
  647. int err = 0;
  648. dev_hold(team->dev);
  649. port->dev->priv_flags |= IFF_TEAM_PORT;
  650. if (team->ops.port_enter) {
  651. err = team->ops.port_enter(team, port);
  652. if (err) {
  653. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  654. port->dev->name);
  655. goto err_port_enter;
  656. }
  657. }
  658. return 0;
  659. err_port_enter:
  660. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  661. dev_put(team->dev);
  662. return err;
  663. }
  664. static void team_port_leave(struct team *team, struct team_port *port)
  665. {
  666. if (team->ops.port_leave)
  667. team->ops.port_leave(team, port);
  668. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  669. dev_put(team->dev);
  670. }
  671. static void __team_port_change_check(struct team_port *port, bool linkup);
  672. static int team_port_add(struct team *team, struct net_device *port_dev)
  673. {
  674. struct net_device *dev = team->dev;
  675. struct team_port *port;
  676. char *portname = port_dev->name;
  677. int err;
  678. if (port_dev->flags & IFF_LOOPBACK ||
  679. port_dev->type != ARPHRD_ETHER) {
  680. netdev_err(dev, "Device %s is of an unsupported type\n",
  681. portname);
  682. return -EINVAL;
  683. }
  684. if (team_port_exists(port_dev)) {
  685. netdev_err(dev, "Device %s is already a port "
  686. "of a team device\n", portname);
  687. return -EBUSY;
  688. }
  689. if (port_dev->flags & IFF_UP) {
  690. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  691. portname);
  692. return -EBUSY;
  693. }
  694. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  695. GFP_KERNEL);
  696. if (!port)
  697. return -ENOMEM;
  698. port->dev = port_dev;
  699. port->team = team;
  700. port->orig.mtu = port_dev->mtu;
  701. err = dev_set_mtu(port_dev, dev->mtu);
  702. if (err) {
  703. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  704. goto err_set_mtu;
  705. }
  706. memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
  707. err = team_port_enter(team, port);
  708. if (err) {
  709. netdev_err(dev, "Device %s failed to enter team mode\n",
  710. portname);
  711. goto err_port_enter;
  712. }
  713. err = dev_open(port_dev);
  714. if (err) {
  715. netdev_dbg(dev, "Device %s opening failed\n",
  716. portname);
  717. goto err_dev_open;
  718. }
  719. err = vlan_vids_add_by_dev(port_dev, dev);
  720. if (err) {
  721. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  722. portname);
  723. goto err_vids_add;
  724. }
  725. err = netdev_set_master(port_dev, dev);
  726. if (err) {
  727. netdev_err(dev, "Device %s failed to set master\n", portname);
  728. goto err_set_master;
  729. }
  730. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  731. port);
  732. if (err) {
  733. netdev_err(dev, "Device %s failed to register rx_handler\n",
  734. portname);
  735. goto err_handler_register;
  736. }
  737. err = team_option_port_add(team, port);
  738. if (err) {
  739. netdev_err(dev, "Device %s failed to add per-port options\n",
  740. portname);
  741. goto err_option_port_add;
  742. }
  743. port->index = -1;
  744. team_port_enable(team, port);
  745. list_add_tail_rcu(&port->list, &team->port_list);
  746. team_adjust_ops(team);
  747. __team_compute_features(team);
  748. __team_port_change_check(port, !!netif_carrier_ok(port_dev));
  749. netdev_info(dev, "Port device %s added\n", portname);
  750. return 0;
  751. err_option_port_add:
  752. netdev_rx_handler_unregister(port_dev);
  753. err_handler_register:
  754. netdev_set_master(port_dev, NULL);
  755. err_set_master:
  756. vlan_vids_del_by_dev(port_dev, dev);
  757. err_vids_add:
  758. dev_close(port_dev);
  759. err_dev_open:
  760. team_port_leave(team, port);
  761. team_port_set_orig_mac(port);
  762. err_port_enter:
  763. dev_set_mtu(port_dev, port->orig.mtu);
  764. err_set_mtu:
  765. kfree(port);
  766. return err;
  767. }
  768. static int team_port_del(struct team *team, struct net_device *port_dev)
  769. {
  770. struct net_device *dev = team->dev;
  771. struct team_port *port;
  772. char *portname = port_dev->name;
  773. port = team_port_get_rtnl(port_dev);
  774. if (!port || !team_port_find(team, port)) {
  775. netdev_err(dev, "Device %s does not act as a port of this team\n",
  776. portname);
  777. return -ENOENT;
  778. }
  779. port->removed = true;
  780. __team_port_change_check(port, false);
  781. team_port_disable(team, port);
  782. list_del_rcu(&port->list);
  783. team_adjust_ops(team);
  784. team_option_port_del(team, port);
  785. netdev_rx_handler_unregister(port_dev);
  786. netdev_set_master(port_dev, NULL);
  787. vlan_vids_del_by_dev(port_dev, dev);
  788. dev_close(port_dev);
  789. team_port_leave(team, port);
  790. team_port_set_orig_mac(port);
  791. dev_set_mtu(port_dev, port->orig.mtu);
  792. synchronize_rcu();
  793. kfree(port);
  794. netdev_info(dev, "Port device %s removed\n", portname);
  795. __team_compute_features(team);
  796. return 0;
  797. }
  798. /*****************
  799. * Net device ops
  800. *****************/
  801. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  802. {
  803. ctx->data.str_val = team->mode->kind;
  804. return 0;
  805. }
  806. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  807. {
  808. return team_change_mode(team, ctx->data.str_val);
  809. }
  810. static int team_port_en_option_get(struct team *team,
  811. struct team_gsetter_ctx *ctx)
  812. {
  813. struct team_port *port = ctx->info->port;
  814. ctx->data.bool_val = team_port_enabled(port);
  815. return 0;
  816. }
  817. static int team_port_en_option_set(struct team *team,
  818. struct team_gsetter_ctx *ctx)
  819. {
  820. struct team_port *port = ctx->info->port;
  821. if (ctx->data.bool_val)
  822. team_port_enable(team, port);
  823. else
  824. team_port_disable(team, port);
  825. return 0;
  826. }
  827. static int team_user_linkup_option_get(struct team *team,
  828. struct team_gsetter_ctx *ctx)
  829. {
  830. struct team_port *port = ctx->info->port;
  831. ctx->data.bool_val = port->user.linkup;
  832. return 0;
  833. }
  834. static int team_user_linkup_option_set(struct team *team,
  835. struct team_gsetter_ctx *ctx)
  836. {
  837. struct team_port *port = ctx->info->port;
  838. port->user.linkup = ctx->data.bool_val;
  839. team_refresh_port_linkup(port);
  840. return 0;
  841. }
  842. static int team_user_linkup_en_option_get(struct team *team,
  843. struct team_gsetter_ctx *ctx)
  844. {
  845. struct team_port *port = ctx->info->port;
  846. ctx->data.bool_val = port->user.linkup_enabled;
  847. return 0;
  848. }
  849. static int team_user_linkup_en_option_set(struct team *team,
  850. struct team_gsetter_ctx *ctx)
  851. {
  852. struct team_port *port = ctx->info->port;
  853. port->user.linkup_enabled = ctx->data.bool_val;
  854. team_refresh_port_linkup(port);
  855. return 0;
  856. }
  857. static const struct team_option team_options[] = {
  858. {
  859. .name = "mode",
  860. .type = TEAM_OPTION_TYPE_STRING,
  861. .getter = team_mode_option_get,
  862. .setter = team_mode_option_set,
  863. },
  864. {
  865. .name = "enabled",
  866. .type = TEAM_OPTION_TYPE_BOOL,
  867. .per_port = true,
  868. .getter = team_port_en_option_get,
  869. .setter = team_port_en_option_set,
  870. },
  871. {
  872. .name = "user_linkup",
  873. .type = TEAM_OPTION_TYPE_BOOL,
  874. .per_port = true,
  875. .getter = team_user_linkup_option_get,
  876. .setter = team_user_linkup_option_set,
  877. },
  878. {
  879. .name = "user_linkup_enabled",
  880. .type = TEAM_OPTION_TYPE_BOOL,
  881. .per_port = true,
  882. .getter = team_user_linkup_en_option_get,
  883. .setter = team_user_linkup_en_option_set,
  884. },
  885. };
  886. static int team_init(struct net_device *dev)
  887. {
  888. struct team *team = netdev_priv(dev);
  889. int i;
  890. int err;
  891. team->dev = dev;
  892. mutex_init(&team->lock);
  893. team_set_no_mode(team);
  894. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  895. if (!team->pcpu_stats)
  896. return -ENOMEM;
  897. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  898. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  899. INIT_LIST_HEAD(&team->port_list);
  900. team_adjust_ops(team);
  901. INIT_LIST_HEAD(&team->option_list);
  902. INIT_LIST_HEAD(&team->option_inst_list);
  903. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  904. if (err)
  905. goto err_options_register;
  906. netif_carrier_off(dev);
  907. return 0;
  908. err_options_register:
  909. free_percpu(team->pcpu_stats);
  910. return err;
  911. }
  912. static void team_uninit(struct net_device *dev)
  913. {
  914. struct team *team = netdev_priv(dev);
  915. struct team_port *port;
  916. struct team_port *tmp;
  917. mutex_lock(&team->lock);
  918. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  919. team_port_del(team, port->dev);
  920. __team_change_mode(team, NULL); /* cleanup */
  921. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  922. mutex_unlock(&team->lock);
  923. }
  924. static void team_destructor(struct net_device *dev)
  925. {
  926. struct team *team = netdev_priv(dev);
  927. free_percpu(team->pcpu_stats);
  928. free_netdev(dev);
  929. }
  930. static int team_open(struct net_device *dev)
  931. {
  932. netif_carrier_on(dev);
  933. return 0;
  934. }
  935. static int team_close(struct net_device *dev)
  936. {
  937. netif_carrier_off(dev);
  938. return 0;
  939. }
  940. /*
  941. * note: already called with rcu_read_lock
  942. */
  943. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  944. {
  945. struct team *team = netdev_priv(dev);
  946. bool tx_success = false;
  947. unsigned int len = skb->len;
  948. tx_success = team->ops.transmit(team, skb);
  949. if (tx_success) {
  950. struct team_pcpu_stats *pcpu_stats;
  951. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  952. u64_stats_update_begin(&pcpu_stats->syncp);
  953. pcpu_stats->tx_packets++;
  954. pcpu_stats->tx_bytes += len;
  955. u64_stats_update_end(&pcpu_stats->syncp);
  956. } else {
  957. this_cpu_inc(team->pcpu_stats->tx_dropped);
  958. }
  959. return NETDEV_TX_OK;
  960. }
  961. static void team_change_rx_flags(struct net_device *dev, int change)
  962. {
  963. struct team *team = netdev_priv(dev);
  964. struct team_port *port;
  965. int inc;
  966. rcu_read_lock();
  967. list_for_each_entry_rcu(port, &team->port_list, list) {
  968. if (change & IFF_PROMISC) {
  969. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  970. dev_set_promiscuity(port->dev, inc);
  971. }
  972. if (change & IFF_ALLMULTI) {
  973. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  974. dev_set_allmulti(port->dev, inc);
  975. }
  976. }
  977. rcu_read_unlock();
  978. }
  979. static void team_set_rx_mode(struct net_device *dev)
  980. {
  981. struct team *team = netdev_priv(dev);
  982. struct team_port *port;
  983. rcu_read_lock();
  984. list_for_each_entry_rcu(port, &team->port_list, list) {
  985. dev_uc_sync(port->dev, dev);
  986. dev_mc_sync(port->dev, dev);
  987. }
  988. rcu_read_unlock();
  989. }
  990. static int team_set_mac_address(struct net_device *dev, void *p)
  991. {
  992. struct team *team = netdev_priv(dev);
  993. struct team_port *port;
  994. struct sockaddr *addr = p;
  995. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  996. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  997. rcu_read_lock();
  998. list_for_each_entry_rcu(port, &team->port_list, list)
  999. if (team->ops.port_change_mac)
  1000. team->ops.port_change_mac(team, port);
  1001. rcu_read_unlock();
  1002. return 0;
  1003. }
  1004. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1005. {
  1006. struct team *team = netdev_priv(dev);
  1007. struct team_port *port;
  1008. int err;
  1009. /*
  1010. * Alhough this is reader, it's guarded by team lock. It's not possible
  1011. * to traverse list in reverse under rcu_read_lock
  1012. */
  1013. mutex_lock(&team->lock);
  1014. list_for_each_entry(port, &team->port_list, list) {
  1015. err = dev_set_mtu(port->dev, new_mtu);
  1016. if (err) {
  1017. netdev_err(dev, "Device %s failed to change mtu",
  1018. port->dev->name);
  1019. goto unwind;
  1020. }
  1021. }
  1022. mutex_unlock(&team->lock);
  1023. dev->mtu = new_mtu;
  1024. return 0;
  1025. unwind:
  1026. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1027. dev_set_mtu(port->dev, dev->mtu);
  1028. mutex_unlock(&team->lock);
  1029. return err;
  1030. }
  1031. static struct rtnl_link_stats64 *
  1032. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1033. {
  1034. struct team *team = netdev_priv(dev);
  1035. struct team_pcpu_stats *p;
  1036. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1037. u32 rx_dropped = 0, tx_dropped = 0;
  1038. unsigned int start;
  1039. int i;
  1040. for_each_possible_cpu(i) {
  1041. p = per_cpu_ptr(team->pcpu_stats, i);
  1042. do {
  1043. start = u64_stats_fetch_begin_bh(&p->syncp);
  1044. rx_packets = p->rx_packets;
  1045. rx_bytes = p->rx_bytes;
  1046. rx_multicast = p->rx_multicast;
  1047. tx_packets = p->tx_packets;
  1048. tx_bytes = p->tx_bytes;
  1049. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  1050. stats->rx_packets += rx_packets;
  1051. stats->rx_bytes += rx_bytes;
  1052. stats->multicast += rx_multicast;
  1053. stats->tx_packets += tx_packets;
  1054. stats->tx_bytes += tx_bytes;
  1055. /*
  1056. * rx_dropped & tx_dropped are u32, updated
  1057. * without syncp protection.
  1058. */
  1059. rx_dropped += p->rx_dropped;
  1060. tx_dropped += p->tx_dropped;
  1061. }
  1062. stats->rx_dropped = rx_dropped;
  1063. stats->tx_dropped = tx_dropped;
  1064. return stats;
  1065. }
  1066. static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
  1067. {
  1068. struct team *team = netdev_priv(dev);
  1069. struct team_port *port;
  1070. int err;
  1071. /*
  1072. * Alhough this is reader, it's guarded by team lock. It's not possible
  1073. * to traverse list in reverse under rcu_read_lock
  1074. */
  1075. mutex_lock(&team->lock);
  1076. list_for_each_entry(port, &team->port_list, list) {
  1077. err = vlan_vid_add(port->dev, vid);
  1078. if (err)
  1079. goto unwind;
  1080. }
  1081. mutex_unlock(&team->lock);
  1082. return 0;
  1083. unwind:
  1084. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1085. vlan_vid_del(port->dev, vid);
  1086. mutex_unlock(&team->lock);
  1087. return err;
  1088. }
  1089. static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
  1090. {
  1091. struct team *team = netdev_priv(dev);
  1092. struct team_port *port;
  1093. rcu_read_lock();
  1094. list_for_each_entry_rcu(port, &team->port_list, list)
  1095. vlan_vid_del(port->dev, vid);
  1096. rcu_read_unlock();
  1097. return 0;
  1098. }
  1099. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1100. {
  1101. struct team *team = netdev_priv(dev);
  1102. int err;
  1103. mutex_lock(&team->lock);
  1104. err = team_port_add(team, port_dev);
  1105. mutex_unlock(&team->lock);
  1106. return err;
  1107. }
  1108. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1109. {
  1110. struct team *team = netdev_priv(dev);
  1111. int err;
  1112. mutex_lock(&team->lock);
  1113. err = team_port_del(team, port_dev);
  1114. mutex_unlock(&team->lock);
  1115. return err;
  1116. }
  1117. static netdev_features_t team_fix_features(struct net_device *dev,
  1118. netdev_features_t features)
  1119. {
  1120. struct team_port *port;
  1121. struct team *team = netdev_priv(dev);
  1122. netdev_features_t mask;
  1123. mask = features;
  1124. features &= ~NETIF_F_ONE_FOR_ALL;
  1125. features |= NETIF_F_ALL_FOR_ALL;
  1126. rcu_read_lock();
  1127. list_for_each_entry_rcu(port, &team->port_list, list) {
  1128. features = netdev_increment_features(features,
  1129. port->dev->features,
  1130. mask);
  1131. }
  1132. rcu_read_unlock();
  1133. return features;
  1134. }
  1135. static const struct net_device_ops team_netdev_ops = {
  1136. .ndo_init = team_init,
  1137. .ndo_uninit = team_uninit,
  1138. .ndo_open = team_open,
  1139. .ndo_stop = team_close,
  1140. .ndo_start_xmit = team_xmit,
  1141. .ndo_change_rx_flags = team_change_rx_flags,
  1142. .ndo_set_rx_mode = team_set_rx_mode,
  1143. .ndo_set_mac_address = team_set_mac_address,
  1144. .ndo_change_mtu = team_change_mtu,
  1145. .ndo_get_stats64 = team_get_stats64,
  1146. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1147. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1148. .ndo_add_slave = team_add_slave,
  1149. .ndo_del_slave = team_del_slave,
  1150. .ndo_fix_features = team_fix_features,
  1151. };
  1152. /***********************
  1153. * rt netlink interface
  1154. ***********************/
  1155. static void team_setup(struct net_device *dev)
  1156. {
  1157. ether_setup(dev);
  1158. dev->netdev_ops = &team_netdev_ops;
  1159. dev->destructor = team_destructor;
  1160. dev->tx_queue_len = 0;
  1161. dev->flags |= IFF_MULTICAST;
  1162. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1163. /*
  1164. * Indicate we support unicast address filtering. That way core won't
  1165. * bring us to promisc mode in case a unicast addr is added.
  1166. * Let this up to underlay drivers.
  1167. */
  1168. dev->priv_flags |= IFF_UNICAST_FLT;
  1169. dev->features |= NETIF_F_LLTX;
  1170. dev->features |= NETIF_F_GRO;
  1171. dev->hw_features = NETIF_F_HW_VLAN_TX |
  1172. NETIF_F_HW_VLAN_RX |
  1173. NETIF_F_HW_VLAN_FILTER;
  1174. dev->features |= dev->hw_features;
  1175. }
  1176. static int team_newlink(struct net *src_net, struct net_device *dev,
  1177. struct nlattr *tb[], struct nlattr *data[])
  1178. {
  1179. int err;
  1180. if (tb[IFLA_ADDRESS] == NULL)
  1181. eth_hw_addr_random(dev);
  1182. err = register_netdevice(dev);
  1183. if (err)
  1184. return err;
  1185. return 0;
  1186. }
  1187. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1188. {
  1189. if (tb[IFLA_ADDRESS]) {
  1190. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1191. return -EINVAL;
  1192. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1193. return -EADDRNOTAVAIL;
  1194. }
  1195. return 0;
  1196. }
  1197. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1198. .kind = DRV_NAME,
  1199. .priv_size = sizeof(struct team),
  1200. .setup = team_setup,
  1201. .newlink = team_newlink,
  1202. .validate = team_validate,
  1203. };
  1204. /***********************************
  1205. * Generic netlink custom interface
  1206. ***********************************/
  1207. static struct genl_family team_nl_family = {
  1208. .id = GENL_ID_GENERATE,
  1209. .name = TEAM_GENL_NAME,
  1210. .version = TEAM_GENL_VERSION,
  1211. .maxattr = TEAM_ATTR_MAX,
  1212. .netnsok = true,
  1213. };
  1214. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1215. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1216. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1217. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1218. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1219. };
  1220. static const struct nla_policy
  1221. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1222. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1223. [TEAM_ATTR_OPTION_NAME] = {
  1224. .type = NLA_STRING,
  1225. .len = TEAM_STRING_MAX_LEN,
  1226. },
  1227. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1228. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1229. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1230. };
  1231. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1232. {
  1233. struct sk_buff *msg;
  1234. void *hdr;
  1235. int err;
  1236. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1237. if (!msg)
  1238. return -ENOMEM;
  1239. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  1240. &team_nl_family, 0, TEAM_CMD_NOOP);
  1241. if (IS_ERR(hdr)) {
  1242. err = PTR_ERR(hdr);
  1243. goto err_msg_put;
  1244. }
  1245. genlmsg_end(msg, hdr);
  1246. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  1247. err_msg_put:
  1248. nlmsg_free(msg);
  1249. return err;
  1250. }
  1251. /*
  1252. * Netlink cmd functions should be locked by following two functions.
  1253. * Since dev gets held here, that ensures dev won't disappear in between.
  1254. */
  1255. static struct team *team_nl_team_get(struct genl_info *info)
  1256. {
  1257. struct net *net = genl_info_net(info);
  1258. int ifindex;
  1259. struct net_device *dev;
  1260. struct team *team;
  1261. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1262. return NULL;
  1263. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1264. dev = dev_get_by_index(net, ifindex);
  1265. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1266. if (dev)
  1267. dev_put(dev);
  1268. return NULL;
  1269. }
  1270. team = netdev_priv(dev);
  1271. mutex_lock(&team->lock);
  1272. return team;
  1273. }
  1274. static void team_nl_team_put(struct team *team)
  1275. {
  1276. mutex_unlock(&team->lock);
  1277. dev_put(team->dev);
  1278. }
  1279. static int team_nl_send_generic(struct genl_info *info, struct team *team,
  1280. int (*fill_func)(struct sk_buff *skb,
  1281. struct genl_info *info,
  1282. int flags, struct team *team))
  1283. {
  1284. struct sk_buff *skb;
  1285. int err;
  1286. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1287. if (!skb)
  1288. return -ENOMEM;
  1289. err = fill_func(skb, info, NLM_F_ACK, team);
  1290. if (err < 0)
  1291. goto err_fill;
  1292. err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
  1293. return err;
  1294. err_fill:
  1295. nlmsg_free(skb);
  1296. return err;
  1297. }
  1298. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1299. struct team_option_inst *opt_inst)
  1300. {
  1301. struct nlattr *option_item;
  1302. struct team_option *option = opt_inst->option;
  1303. struct team_option_inst_info *opt_inst_info;
  1304. struct team_gsetter_ctx ctx;
  1305. int err;
  1306. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1307. if (!option_item)
  1308. goto nla_put_failure;
  1309. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1310. goto nla_put_failure;
  1311. if (opt_inst->changed) {
  1312. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1313. goto nla_put_failure;
  1314. opt_inst->changed = false;
  1315. }
  1316. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1317. goto nla_put_failure;
  1318. opt_inst_info = &opt_inst->info;
  1319. if (opt_inst_info->port &&
  1320. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1321. opt_inst_info->port->dev->ifindex))
  1322. goto nla_put_failure;
  1323. if (opt_inst->option->array_size &&
  1324. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1325. opt_inst_info->array_index))
  1326. goto nla_put_failure;
  1327. ctx.info = opt_inst_info;
  1328. switch (option->type) {
  1329. case TEAM_OPTION_TYPE_U32:
  1330. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1331. goto nla_put_failure;
  1332. err = team_option_get(team, opt_inst, &ctx);
  1333. if (err)
  1334. goto errout;
  1335. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1336. goto nla_put_failure;
  1337. break;
  1338. case TEAM_OPTION_TYPE_STRING:
  1339. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1340. goto nla_put_failure;
  1341. err = team_option_get(team, opt_inst, &ctx);
  1342. if (err)
  1343. goto errout;
  1344. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1345. ctx.data.str_val))
  1346. goto nla_put_failure;
  1347. break;
  1348. case TEAM_OPTION_TYPE_BINARY:
  1349. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1350. goto nla_put_failure;
  1351. err = team_option_get(team, opt_inst, &ctx);
  1352. if (err)
  1353. goto errout;
  1354. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1355. ctx.data.bin_val.ptr))
  1356. goto nla_put_failure;
  1357. break;
  1358. case TEAM_OPTION_TYPE_BOOL:
  1359. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1360. goto nla_put_failure;
  1361. err = team_option_get(team, opt_inst, &ctx);
  1362. if (err)
  1363. goto errout;
  1364. if (ctx.data.bool_val &&
  1365. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1366. goto nla_put_failure;
  1367. break;
  1368. default:
  1369. BUG();
  1370. }
  1371. nla_nest_end(skb, option_item);
  1372. return 0;
  1373. nla_put_failure:
  1374. err = -EMSGSIZE;
  1375. errout:
  1376. return err;
  1377. }
  1378. static int team_nl_fill_options_get(struct sk_buff *skb,
  1379. u32 pid, u32 seq, int flags,
  1380. struct team *team,
  1381. struct list_head *sel_opt_inst_list)
  1382. {
  1383. struct nlattr *option_list;
  1384. void *hdr;
  1385. struct team_option_inst *opt_inst;
  1386. int err;
  1387. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1388. TEAM_CMD_OPTIONS_GET);
  1389. if (IS_ERR(hdr))
  1390. return PTR_ERR(hdr);
  1391. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1392. goto nla_put_failure;
  1393. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1394. if (!option_list)
  1395. goto nla_put_failure;
  1396. list_for_each_entry(opt_inst, sel_opt_inst_list, tmp_list) {
  1397. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  1398. if (err)
  1399. goto errout;
  1400. }
  1401. nla_nest_end(skb, option_list);
  1402. return genlmsg_end(skb, hdr);
  1403. nla_put_failure:
  1404. err = -EMSGSIZE;
  1405. errout:
  1406. genlmsg_cancel(skb, hdr);
  1407. return err;
  1408. }
  1409. static int team_nl_fill_options_get_all(struct sk_buff *skb,
  1410. struct genl_info *info, int flags,
  1411. struct team *team)
  1412. {
  1413. struct team_option_inst *opt_inst;
  1414. LIST_HEAD(sel_opt_inst_list);
  1415. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  1416. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  1417. return team_nl_fill_options_get(skb, info->snd_pid,
  1418. info->snd_seq, NLM_F_ACK,
  1419. team, &sel_opt_inst_list);
  1420. }
  1421. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1422. {
  1423. struct team *team;
  1424. int err;
  1425. team = team_nl_team_get(info);
  1426. if (!team)
  1427. return -EINVAL;
  1428. err = team_nl_send_generic(info, team, team_nl_fill_options_get_all);
  1429. team_nl_team_put(team);
  1430. return err;
  1431. }
  1432. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1433. {
  1434. struct team *team;
  1435. int err = 0;
  1436. int i;
  1437. struct nlattr *nl_option;
  1438. team = team_nl_team_get(info);
  1439. if (!team)
  1440. return -EINVAL;
  1441. err = -EINVAL;
  1442. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  1443. err = -EINVAL;
  1444. goto team_put;
  1445. }
  1446. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  1447. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  1448. struct nlattr *attr;
  1449. struct nlattr *attr_data;
  1450. enum team_option_type opt_type;
  1451. int opt_port_ifindex = 0; /* != 0 for per-port options */
  1452. u32 opt_array_index = 0;
  1453. bool opt_is_array = false;
  1454. struct team_option_inst *opt_inst;
  1455. char *opt_name;
  1456. bool opt_found = false;
  1457. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  1458. err = -EINVAL;
  1459. goto team_put;
  1460. }
  1461. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  1462. nl_option, team_nl_option_policy);
  1463. if (err)
  1464. goto team_put;
  1465. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  1466. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  1467. err = -EINVAL;
  1468. goto team_put;
  1469. }
  1470. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  1471. case NLA_U32:
  1472. opt_type = TEAM_OPTION_TYPE_U32;
  1473. break;
  1474. case NLA_STRING:
  1475. opt_type = TEAM_OPTION_TYPE_STRING;
  1476. break;
  1477. case NLA_BINARY:
  1478. opt_type = TEAM_OPTION_TYPE_BINARY;
  1479. break;
  1480. case NLA_FLAG:
  1481. opt_type = TEAM_OPTION_TYPE_BOOL;
  1482. break;
  1483. default:
  1484. goto team_put;
  1485. }
  1486. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  1487. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  1488. err = -EINVAL;
  1489. goto team_put;
  1490. }
  1491. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  1492. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  1493. if (attr)
  1494. opt_port_ifindex = nla_get_u32(attr);
  1495. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  1496. if (attr) {
  1497. opt_is_array = true;
  1498. opt_array_index = nla_get_u32(attr);
  1499. }
  1500. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  1501. struct team_option *option = opt_inst->option;
  1502. struct team_gsetter_ctx ctx;
  1503. struct team_option_inst_info *opt_inst_info;
  1504. int tmp_ifindex;
  1505. opt_inst_info = &opt_inst->info;
  1506. tmp_ifindex = opt_inst_info->port ?
  1507. opt_inst_info->port->dev->ifindex : 0;
  1508. if (option->type != opt_type ||
  1509. strcmp(option->name, opt_name) ||
  1510. tmp_ifindex != opt_port_ifindex ||
  1511. (option->array_size && !opt_is_array) ||
  1512. opt_inst_info->array_index != opt_array_index)
  1513. continue;
  1514. opt_found = true;
  1515. ctx.info = opt_inst_info;
  1516. switch (opt_type) {
  1517. case TEAM_OPTION_TYPE_U32:
  1518. ctx.data.u32_val = nla_get_u32(attr_data);
  1519. break;
  1520. case TEAM_OPTION_TYPE_STRING:
  1521. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  1522. err = -EINVAL;
  1523. goto team_put;
  1524. }
  1525. ctx.data.str_val = nla_data(attr_data);
  1526. break;
  1527. case TEAM_OPTION_TYPE_BINARY:
  1528. ctx.data.bin_val.len = nla_len(attr_data);
  1529. ctx.data.bin_val.ptr = nla_data(attr_data);
  1530. break;
  1531. case TEAM_OPTION_TYPE_BOOL:
  1532. ctx.data.bool_val = attr_data ? true : false;
  1533. break;
  1534. default:
  1535. BUG();
  1536. }
  1537. err = team_option_set(team, opt_inst, &ctx);
  1538. if (err)
  1539. goto team_put;
  1540. }
  1541. if (!opt_found) {
  1542. err = -ENOENT;
  1543. goto team_put;
  1544. }
  1545. }
  1546. team_put:
  1547. team_nl_team_put(team);
  1548. return err;
  1549. }
  1550. static int team_nl_fill_port_list_get(struct sk_buff *skb,
  1551. u32 pid, u32 seq, int flags,
  1552. struct team *team,
  1553. bool fillall)
  1554. {
  1555. struct nlattr *port_list;
  1556. void *hdr;
  1557. struct team_port *port;
  1558. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1559. TEAM_CMD_PORT_LIST_GET);
  1560. if (IS_ERR(hdr))
  1561. return PTR_ERR(hdr);
  1562. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1563. goto nla_put_failure;
  1564. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  1565. if (!port_list)
  1566. goto nla_put_failure;
  1567. list_for_each_entry(port, &team->port_list, list) {
  1568. struct nlattr *port_item;
  1569. /* Include only changed ports if fill all mode is not on */
  1570. if (!fillall && !port->changed)
  1571. continue;
  1572. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  1573. if (!port_item)
  1574. goto nla_put_failure;
  1575. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  1576. goto nla_put_failure;
  1577. if (port->changed) {
  1578. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  1579. goto nla_put_failure;
  1580. port->changed = false;
  1581. }
  1582. if ((port->removed &&
  1583. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  1584. (port->state.linkup &&
  1585. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  1586. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  1587. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  1588. goto nla_put_failure;
  1589. nla_nest_end(skb, port_item);
  1590. }
  1591. nla_nest_end(skb, port_list);
  1592. return genlmsg_end(skb, hdr);
  1593. nla_put_failure:
  1594. genlmsg_cancel(skb, hdr);
  1595. return -EMSGSIZE;
  1596. }
  1597. static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
  1598. struct genl_info *info, int flags,
  1599. struct team *team)
  1600. {
  1601. return team_nl_fill_port_list_get(skb, info->snd_pid,
  1602. info->snd_seq, NLM_F_ACK,
  1603. team, true);
  1604. }
  1605. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  1606. struct genl_info *info)
  1607. {
  1608. struct team *team;
  1609. int err;
  1610. team = team_nl_team_get(info);
  1611. if (!team)
  1612. return -EINVAL;
  1613. err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
  1614. team_nl_team_put(team);
  1615. return err;
  1616. }
  1617. static struct genl_ops team_nl_ops[] = {
  1618. {
  1619. .cmd = TEAM_CMD_NOOP,
  1620. .doit = team_nl_cmd_noop,
  1621. .policy = team_nl_policy,
  1622. },
  1623. {
  1624. .cmd = TEAM_CMD_OPTIONS_SET,
  1625. .doit = team_nl_cmd_options_set,
  1626. .policy = team_nl_policy,
  1627. .flags = GENL_ADMIN_PERM,
  1628. },
  1629. {
  1630. .cmd = TEAM_CMD_OPTIONS_GET,
  1631. .doit = team_nl_cmd_options_get,
  1632. .policy = team_nl_policy,
  1633. .flags = GENL_ADMIN_PERM,
  1634. },
  1635. {
  1636. .cmd = TEAM_CMD_PORT_LIST_GET,
  1637. .doit = team_nl_cmd_port_list_get,
  1638. .policy = team_nl_policy,
  1639. .flags = GENL_ADMIN_PERM,
  1640. },
  1641. };
  1642. static struct genl_multicast_group team_change_event_mcgrp = {
  1643. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  1644. };
  1645. static int team_nl_send_event_options_get(struct team *team,
  1646. struct list_head *sel_opt_inst_list)
  1647. {
  1648. struct sk_buff *skb;
  1649. int err;
  1650. struct net *net = dev_net(team->dev);
  1651. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1652. if (!skb)
  1653. return -ENOMEM;
  1654. err = team_nl_fill_options_get(skb, 0, 0, 0, team, sel_opt_inst_list);
  1655. if (err < 0)
  1656. goto err_fill;
  1657. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1658. GFP_KERNEL);
  1659. return err;
  1660. err_fill:
  1661. nlmsg_free(skb);
  1662. return err;
  1663. }
  1664. static int team_nl_send_event_port_list_get(struct team *team)
  1665. {
  1666. struct sk_buff *skb;
  1667. int err;
  1668. struct net *net = dev_net(team->dev);
  1669. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1670. if (!skb)
  1671. return -ENOMEM;
  1672. err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
  1673. if (err < 0)
  1674. goto err_fill;
  1675. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1676. GFP_KERNEL);
  1677. return err;
  1678. err_fill:
  1679. nlmsg_free(skb);
  1680. return err;
  1681. }
  1682. static int team_nl_init(void)
  1683. {
  1684. int err;
  1685. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  1686. ARRAY_SIZE(team_nl_ops));
  1687. if (err)
  1688. return err;
  1689. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  1690. if (err)
  1691. goto err_change_event_grp_reg;
  1692. return 0;
  1693. err_change_event_grp_reg:
  1694. genl_unregister_family(&team_nl_family);
  1695. return err;
  1696. }
  1697. static void team_nl_fini(void)
  1698. {
  1699. genl_unregister_family(&team_nl_family);
  1700. }
  1701. /******************
  1702. * Change checkers
  1703. ******************/
  1704. static void __team_options_change_check(struct team *team)
  1705. {
  1706. int err;
  1707. struct team_option_inst *opt_inst;
  1708. LIST_HEAD(sel_opt_inst_list);
  1709. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  1710. if (opt_inst->changed)
  1711. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  1712. }
  1713. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  1714. if (err)
  1715. netdev_warn(team->dev, "Failed to send options change via netlink\n");
  1716. }
  1717. static void __team_option_inst_change(struct team *team,
  1718. struct team_option_inst *sel_opt_inst)
  1719. {
  1720. int err;
  1721. LIST_HEAD(sel_opt_inst_list);
  1722. sel_opt_inst->changed = true;
  1723. list_add(&sel_opt_inst->tmp_list, &sel_opt_inst_list);
  1724. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  1725. if (err)
  1726. netdev_warn(team->dev, "Failed to send option change via netlink\n");
  1727. }
  1728. /* rtnl lock is held */
  1729. static void __team_port_change_check(struct team_port *port, bool linkup)
  1730. {
  1731. int err;
  1732. if (!port->removed && port->state.linkup == linkup)
  1733. return;
  1734. port->changed = true;
  1735. port->state.linkup = linkup;
  1736. team_refresh_port_linkup(port);
  1737. if (linkup) {
  1738. struct ethtool_cmd ecmd;
  1739. err = __ethtool_get_settings(port->dev, &ecmd);
  1740. if (!err) {
  1741. port->state.speed = ethtool_cmd_speed(&ecmd);
  1742. port->state.duplex = ecmd.duplex;
  1743. goto send_event;
  1744. }
  1745. }
  1746. port->state.speed = 0;
  1747. port->state.duplex = 0;
  1748. send_event:
  1749. err = team_nl_send_event_port_list_get(port->team);
  1750. if (err)
  1751. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
  1752. port->dev->name);
  1753. }
  1754. static void team_port_change_check(struct team_port *port, bool linkup)
  1755. {
  1756. struct team *team = port->team;
  1757. mutex_lock(&team->lock);
  1758. __team_port_change_check(port, linkup);
  1759. mutex_unlock(&team->lock);
  1760. }
  1761. /************************************
  1762. * Net device notifier event handler
  1763. ************************************/
  1764. static int team_device_event(struct notifier_block *unused,
  1765. unsigned long event, void *ptr)
  1766. {
  1767. struct net_device *dev = (struct net_device *) ptr;
  1768. struct team_port *port;
  1769. port = team_port_get_rtnl(dev);
  1770. if (!port)
  1771. return NOTIFY_DONE;
  1772. switch (event) {
  1773. case NETDEV_UP:
  1774. if (netif_carrier_ok(dev))
  1775. team_port_change_check(port, true);
  1776. case NETDEV_DOWN:
  1777. team_port_change_check(port, false);
  1778. case NETDEV_CHANGE:
  1779. if (netif_running(port->dev))
  1780. team_port_change_check(port,
  1781. !!netif_carrier_ok(port->dev));
  1782. break;
  1783. case NETDEV_UNREGISTER:
  1784. team_del_slave(port->team->dev, dev);
  1785. break;
  1786. case NETDEV_FEAT_CHANGE:
  1787. team_compute_features(port->team);
  1788. break;
  1789. case NETDEV_CHANGEMTU:
  1790. /* Forbid to change mtu of underlaying device */
  1791. return NOTIFY_BAD;
  1792. case NETDEV_PRE_TYPE_CHANGE:
  1793. /* Forbid to change type of underlaying device */
  1794. return NOTIFY_BAD;
  1795. }
  1796. return NOTIFY_DONE;
  1797. }
  1798. static struct notifier_block team_notifier_block __read_mostly = {
  1799. .notifier_call = team_device_event,
  1800. };
  1801. /***********************
  1802. * Module init and exit
  1803. ***********************/
  1804. static int __init team_module_init(void)
  1805. {
  1806. int err;
  1807. register_netdevice_notifier(&team_notifier_block);
  1808. err = rtnl_link_register(&team_link_ops);
  1809. if (err)
  1810. goto err_rtnl_reg;
  1811. err = team_nl_init();
  1812. if (err)
  1813. goto err_nl_init;
  1814. return 0;
  1815. err_nl_init:
  1816. rtnl_link_unregister(&team_link_ops);
  1817. err_rtnl_reg:
  1818. unregister_netdevice_notifier(&team_notifier_block);
  1819. return err;
  1820. }
  1821. static void __exit team_module_exit(void)
  1822. {
  1823. team_nl_fini();
  1824. rtnl_link_unregister(&team_link_ops);
  1825. unregister_netdevice_notifier(&team_notifier_block);
  1826. }
  1827. module_init(team_module_init);
  1828. module_exit(team_module_exit);
  1829. MODULE_LICENSE("GPL v2");
  1830. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  1831. MODULE_DESCRIPTION("Ethernet team device driver");
  1832. MODULE_ALIAS_RTNL_LINK(DRV_NAME);