team.c 71 KB

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