team.c 64 KB

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