team.c 66 KB

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