team.c 72 KB

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