team_core.c 73 KB

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