team.c 69 KB

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