team.c 64 KB

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