team.c 69 KB

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