team.c 51 KB

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