team.c 53 KB

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