team.c 69 KB

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