team.c 63 KB

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