team.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925
  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. if (!team->dev->npinfo)
  872. return 0;
  873. np = kzalloc(sizeof(*np), gfp);
  874. if (!np)
  875. return -ENOMEM;
  876. err = __netpoll_setup(np, port->dev, gfp);
  877. if (err) {
  878. kfree(np);
  879. return err;
  880. }
  881. port->np = np;
  882. return err;
  883. }
  884. static void team_port_disable_netpoll(struct team_port *port)
  885. {
  886. struct netpoll *np = port->np;
  887. if (!np)
  888. return;
  889. port->np = NULL;
  890. /* Wait for transmitting packets to finish before freeing. */
  891. synchronize_rcu_bh();
  892. __netpoll_cleanup(np);
  893. kfree(np);
  894. }
  895. #else
  896. static int team_port_enable_netpoll(struct team *team, struct team_port *port,
  897. gfp_t gfp)
  898. {
  899. return 0;
  900. }
  901. static void team_port_disable_netpoll(struct team_port *port)
  902. {
  903. }
  904. #endif
  905. static void __team_port_change_port_added(struct team_port *port, bool linkup);
  906. static int team_dev_type_check_change(struct net_device *dev,
  907. struct net_device *port_dev);
  908. static int team_port_add(struct team *team, struct net_device *port_dev)
  909. {
  910. struct net_device *dev = team->dev;
  911. struct team_port *port;
  912. char *portname = port_dev->name;
  913. int err;
  914. if (port_dev->flags & IFF_LOOPBACK) {
  915. netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
  916. portname);
  917. return -EINVAL;
  918. }
  919. if (team_port_exists(port_dev)) {
  920. netdev_err(dev, "Device %s is already a port "
  921. "of a team device\n", portname);
  922. return -EBUSY;
  923. }
  924. if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
  925. vlan_uses_dev(dev)) {
  926. netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
  927. portname);
  928. return -EPERM;
  929. }
  930. err = team_dev_type_check_change(dev, port_dev);
  931. if (err)
  932. return err;
  933. if (port_dev->flags & IFF_UP) {
  934. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  935. portname);
  936. return -EBUSY;
  937. }
  938. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  939. GFP_KERNEL);
  940. if (!port)
  941. return -ENOMEM;
  942. port->dev = port_dev;
  943. port->team = team;
  944. INIT_LIST_HEAD(&port->qom_list);
  945. port->orig.mtu = port_dev->mtu;
  946. err = dev_set_mtu(port_dev, dev->mtu);
  947. if (err) {
  948. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  949. goto err_set_mtu;
  950. }
  951. memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
  952. err = team_port_enter(team, port);
  953. if (err) {
  954. netdev_err(dev, "Device %s failed to enter team mode\n",
  955. portname);
  956. goto err_port_enter;
  957. }
  958. err = dev_open(port_dev);
  959. if (err) {
  960. netdev_dbg(dev, "Device %s opening failed\n",
  961. portname);
  962. goto err_dev_open;
  963. }
  964. err = vlan_vids_add_by_dev(port_dev, dev);
  965. if (err) {
  966. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  967. portname);
  968. goto err_vids_add;
  969. }
  970. err = team_port_enable_netpoll(team, port, GFP_KERNEL);
  971. if (err) {
  972. netdev_err(dev, "Failed to enable netpoll on device %s\n",
  973. portname);
  974. goto err_enable_netpoll;
  975. }
  976. err = netdev_master_upper_dev_link(port_dev, dev);
  977. if (err) {
  978. netdev_err(dev, "Device %s failed to set upper link\n",
  979. portname);
  980. goto err_set_upper_link;
  981. }
  982. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  983. port);
  984. if (err) {
  985. netdev_err(dev, "Device %s failed to register rx_handler\n",
  986. portname);
  987. goto err_handler_register;
  988. }
  989. err = __team_option_inst_add_port(team, port);
  990. if (err) {
  991. netdev_err(dev, "Device %s failed to add per-port options\n",
  992. portname);
  993. goto err_option_port_add;
  994. }
  995. port->index = -1;
  996. list_add_tail_rcu(&port->list, &team->port_list);
  997. team_port_enable(team, port);
  998. __team_compute_features(team);
  999. __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
  1000. __team_options_change_check(team);
  1001. netdev_info(dev, "Port device %s added\n", portname);
  1002. return 0;
  1003. err_option_port_add:
  1004. netdev_rx_handler_unregister(port_dev);
  1005. err_handler_register:
  1006. netdev_upper_dev_unlink(port_dev, dev);
  1007. err_set_upper_link:
  1008. team_port_disable_netpoll(port);
  1009. err_enable_netpoll:
  1010. vlan_vids_del_by_dev(port_dev, dev);
  1011. err_vids_add:
  1012. dev_close(port_dev);
  1013. err_dev_open:
  1014. team_port_leave(team, port);
  1015. team_port_set_orig_dev_addr(port);
  1016. err_port_enter:
  1017. dev_set_mtu(port_dev, port->orig.mtu);
  1018. err_set_mtu:
  1019. kfree(port);
  1020. return err;
  1021. }
  1022. static void __team_port_change_port_removed(struct team_port *port);
  1023. static int team_port_del(struct team *team, struct net_device *port_dev)
  1024. {
  1025. struct net_device *dev = team->dev;
  1026. struct team_port *port;
  1027. char *portname = port_dev->name;
  1028. port = team_port_get_rtnl(port_dev);
  1029. if (!port || !team_port_find(team, port)) {
  1030. netdev_err(dev, "Device %s does not act as a port of this team\n",
  1031. portname);
  1032. return -ENOENT;
  1033. }
  1034. team_port_disable(team, port);
  1035. list_del_rcu(&port->list);
  1036. netdev_rx_handler_unregister(port_dev);
  1037. netdev_upper_dev_unlink(port_dev, dev);
  1038. team_port_disable_netpoll(port);
  1039. vlan_vids_del_by_dev(port_dev, dev);
  1040. dev_uc_unsync(port_dev, dev);
  1041. dev_mc_unsync(port_dev, dev);
  1042. dev_close(port_dev);
  1043. team_port_leave(team, port);
  1044. __team_option_inst_mark_removed_port(team, port);
  1045. __team_options_change_check(team);
  1046. __team_option_inst_del_port(team, port);
  1047. __team_port_change_port_removed(port);
  1048. team_port_set_orig_dev_addr(port);
  1049. dev_set_mtu(port_dev, port->orig.mtu);
  1050. kfree_rcu(port, rcu);
  1051. netdev_info(dev, "Port device %s removed\n", portname);
  1052. __team_compute_features(team);
  1053. return 0;
  1054. }
  1055. /*****************
  1056. * Net device ops
  1057. *****************/
  1058. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  1059. {
  1060. ctx->data.str_val = team->mode->kind;
  1061. return 0;
  1062. }
  1063. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  1064. {
  1065. return team_change_mode(team, ctx->data.str_val);
  1066. }
  1067. static int team_notify_peers_count_get(struct team *team,
  1068. struct team_gsetter_ctx *ctx)
  1069. {
  1070. ctx->data.u32_val = team->notify_peers.count;
  1071. return 0;
  1072. }
  1073. static int team_notify_peers_count_set(struct team *team,
  1074. struct team_gsetter_ctx *ctx)
  1075. {
  1076. team->notify_peers.count = ctx->data.u32_val;
  1077. return 0;
  1078. }
  1079. static int team_notify_peers_interval_get(struct team *team,
  1080. struct team_gsetter_ctx *ctx)
  1081. {
  1082. ctx->data.u32_val = team->notify_peers.interval;
  1083. return 0;
  1084. }
  1085. static int team_notify_peers_interval_set(struct team *team,
  1086. struct team_gsetter_ctx *ctx)
  1087. {
  1088. team->notify_peers.interval = ctx->data.u32_val;
  1089. return 0;
  1090. }
  1091. static int team_mcast_rejoin_count_get(struct team *team,
  1092. struct team_gsetter_ctx *ctx)
  1093. {
  1094. ctx->data.u32_val = team->mcast_rejoin.count;
  1095. return 0;
  1096. }
  1097. static int team_mcast_rejoin_count_set(struct team *team,
  1098. struct team_gsetter_ctx *ctx)
  1099. {
  1100. team->mcast_rejoin.count = ctx->data.u32_val;
  1101. return 0;
  1102. }
  1103. static int team_mcast_rejoin_interval_get(struct team *team,
  1104. struct team_gsetter_ctx *ctx)
  1105. {
  1106. ctx->data.u32_val = team->mcast_rejoin.interval;
  1107. return 0;
  1108. }
  1109. static int team_mcast_rejoin_interval_set(struct team *team,
  1110. struct team_gsetter_ctx *ctx)
  1111. {
  1112. team->mcast_rejoin.interval = ctx->data.u32_val;
  1113. return 0;
  1114. }
  1115. static int team_port_en_option_get(struct team *team,
  1116. struct team_gsetter_ctx *ctx)
  1117. {
  1118. struct team_port *port = ctx->info->port;
  1119. ctx->data.bool_val = team_port_enabled(port);
  1120. return 0;
  1121. }
  1122. static int team_port_en_option_set(struct team *team,
  1123. struct team_gsetter_ctx *ctx)
  1124. {
  1125. struct team_port *port = ctx->info->port;
  1126. if (ctx->data.bool_val)
  1127. team_port_enable(team, port);
  1128. else
  1129. team_port_disable(team, port);
  1130. return 0;
  1131. }
  1132. static int team_user_linkup_option_get(struct team *team,
  1133. struct team_gsetter_ctx *ctx)
  1134. {
  1135. struct team_port *port = ctx->info->port;
  1136. ctx->data.bool_val = port->user.linkup;
  1137. return 0;
  1138. }
  1139. static int team_user_linkup_option_set(struct team *team,
  1140. struct team_gsetter_ctx *ctx)
  1141. {
  1142. struct team_port *port = ctx->info->port;
  1143. port->user.linkup = ctx->data.bool_val;
  1144. team_refresh_port_linkup(port);
  1145. return 0;
  1146. }
  1147. static int team_user_linkup_en_option_get(struct team *team,
  1148. struct team_gsetter_ctx *ctx)
  1149. {
  1150. struct team_port *port = ctx->info->port;
  1151. ctx->data.bool_val = port->user.linkup_enabled;
  1152. return 0;
  1153. }
  1154. static int team_user_linkup_en_option_set(struct team *team,
  1155. struct team_gsetter_ctx *ctx)
  1156. {
  1157. struct team_port *port = ctx->info->port;
  1158. port->user.linkup_enabled = ctx->data.bool_val;
  1159. team_refresh_port_linkup(port);
  1160. return 0;
  1161. }
  1162. static int team_priority_option_get(struct team *team,
  1163. struct team_gsetter_ctx *ctx)
  1164. {
  1165. struct team_port *port = ctx->info->port;
  1166. ctx->data.s32_val = port->priority;
  1167. return 0;
  1168. }
  1169. static int team_priority_option_set(struct team *team,
  1170. struct team_gsetter_ctx *ctx)
  1171. {
  1172. struct team_port *port = ctx->info->port;
  1173. s32 priority = ctx->data.s32_val;
  1174. if (port->priority == priority)
  1175. return 0;
  1176. port->priority = priority;
  1177. team_queue_override_port_prio_changed(team, port);
  1178. return 0;
  1179. }
  1180. static int team_queue_id_option_get(struct team *team,
  1181. struct team_gsetter_ctx *ctx)
  1182. {
  1183. struct team_port *port = ctx->info->port;
  1184. ctx->data.u32_val = port->queue_id;
  1185. return 0;
  1186. }
  1187. static int team_queue_id_option_set(struct team *team,
  1188. struct team_gsetter_ctx *ctx)
  1189. {
  1190. struct team_port *port = ctx->info->port;
  1191. u16 new_queue_id = ctx->data.u32_val;
  1192. if (port->queue_id == new_queue_id)
  1193. return 0;
  1194. if (new_queue_id >= team->dev->real_num_tx_queues)
  1195. return -EINVAL;
  1196. team_queue_override_port_change_queue_id(team, port, new_queue_id);
  1197. return 0;
  1198. }
  1199. static const struct team_option team_options[] = {
  1200. {
  1201. .name = "mode",
  1202. .type = TEAM_OPTION_TYPE_STRING,
  1203. .getter = team_mode_option_get,
  1204. .setter = team_mode_option_set,
  1205. },
  1206. {
  1207. .name = "notify_peers_count",
  1208. .type = TEAM_OPTION_TYPE_U32,
  1209. .getter = team_notify_peers_count_get,
  1210. .setter = team_notify_peers_count_set,
  1211. },
  1212. {
  1213. .name = "notify_peers_interval",
  1214. .type = TEAM_OPTION_TYPE_U32,
  1215. .getter = team_notify_peers_interval_get,
  1216. .setter = team_notify_peers_interval_set,
  1217. },
  1218. {
  1219. .name = "mcast_rejoin_count",
  1220. .type = TEAM_OPTION_TYPE_U32,
  1221. .getter = team_mcast_rejoin_count_get,
  1222. .setter = team_mcast_rejoin_count_set,
  1223. },
  1224. {
  1225. .name = "mcast_rejoin_interval",
  1226. .type = TEAM_OPTION_TYPE_U32,
  1227. .getter = team_mcast_rejoin_interval_get,
  1228. .setter = team_mcast_rejoin_interval_set,
  1229. },
  1230. {
  1231. .name = "enabled",
  1232. .type = TEAM_OPTION_TYPE_BOOL,
  1233. .per_port = true,
  1234. .getter = team_port_en_option_get,
  1235. .setter = team_port_en_option_set,
  1236. },
  1237. {
  1238. .name = "user_linkup",
  1239. .type = TEAM_OPTION_TYPE_BOOL,
  1240. .per_port = true,
  1241. .getter = team_user_linkup_option_get,
  1242. .setter = team_user_linkup_option_set,
  1243. },
  1244. {
  1245. .name = "user_linkup_enabled",
  1246. .type = TEAM_OPTION_TYPE_BOOL,
  1247. .per_port = true,
  1248. .getter = team_user_linkup_en_option_get,
  1249. .setter = team_user_linkup_en_option_set,
  1250. },
  1251. {
  1252. .name = "priority",
  1253. .type = TEAM_OPTION_TYPE_S32,
  1254. .per_port = true,
  1255. .getter = team_priority_option_get,
  1256. .setter = team_priority_option_set,
  1257. },
  1258. {
  1259. .name = "queue_id",
  1260. .type = TEAM_OPTION_TYPE_U32,
  1261. .per_port = true,
  1262. .getter = team_queue_id_option_get,
  1263. .setter = team_queue_id_option_set,
  1264. },
  1265. };
  1266. static struct lock_class_key team_netdev_xmit_lock_key;
  1267. static struct lock_class_key team_netdev_addr_lock_key;
  1268. static struct lock_class_key team_tx_busylock_key;
  1269. static void team_set_lockdep_class_one(struct net_device *dev,
  1270. struct netdev_queue *txq,
  1271. void *unused)
  1272. {
  1273. lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
  1274. }
  1275. static void team_set_lockdep_class(struct net_device *dev)
  1276. {
  1277. lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
  1278. netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
  1279. dev->qdisc_tx_busylock = &team_tx_busylock_key;
  1280. }
  1281. static int team_init(struct net_device *dev)
  1282. {
  1283. struct team *team = netdev_priv(dev);
  1284. int i;
  1285. int err;
  1286. team->dev = dev;
  1287. mutex_init(&team->lock);
  1288. team_set_no_mode(team);
  1289. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  1290. if (!team->pcpu_stats)
  1291. return -ENOMEM;
  1292. for_each_possible_cpu(i) {
  1293. struct team_pcpu_stats *team_stats;
  1294. team_stats = per_cpu_ptr(team->pcpu_stats, i);
  1295. u64_stats_init(&team_stats->syncp);
  1296. }
  1297. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1298. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1299. INIT_LIST_HEAD(&team->port_list);
  1300. err = team_queue_override_init(team);
  1301. if (err)
  1302. goto err_team_queue_override_init;
  1303. team_adjust_ops(team);
  1304. INIT_LIST_HEAD(&team->option_list);
  1305. INIT_LIST_HEAD(&team->option_inst_list);
  1306. team_notify_peers_init(team);
  1307. team_mcast_rejoin_init(team);
  1308. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1309. if (err)
  1310. goto err_options_register;
  1311. netif_carrier_off(dev);
  1312. team_set_lockdep_class(dev);
  1313. return 0;
  1314. err_options_register:
  1315. team_mcast_rejoin_fini(team);
  1316. team_notify_peers_fini(team);
  1317. team_queue_override_fini(team);
  1318. err_team_queue_override_init:
  1319. free_percpu(team->pcpu_stats);
  1320. return err;
  1321. }
  1322. static void team_uninit(struct net_device *dev)
  1323. {
  1324. struct team *team = netdev_priv(dev);
  1325. struct team_port *port;
  1326. struct team_port *tmp;
  1327. mutex_lock(&team->lock);
  1328. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1329. team_port_del(team, port->dev);
  1330. __team_change_mode(team, NULL); /* cleanup */
  1331. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1332. team_mcast_rejoin_fini(team);
  1333. team_notify_peers_fini(team);
  1334. team_queue_override_fini(team);
  1335. mutex_unlock(&team->lock);
  1336. }
  1337. static void team_destructor(struct net_device *dev)
  1338. {
  1339. struct team *team = netdev_priv(dev);
  1340. free_percpu(team->pcpu_stats);
  1341. free_netdev(dev);
  1342. }
  1343. static int team_open(struct net_device *dev)
  1344. {
  1345. return 0;
  1346. }
  1347. static int team_close(struct net_device *dev)
  1348. {
  1349. return 0;
  1350. }
  1351. /*
  1352. * note: already called with rcu_read_lock
  1353. */
  1354. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1355. {
  1356. struct team *team = netdev_priv(dev);
  1357. bool tx_success;
  1358. unsigned int len = skb->len;
  1359. tx_success = team_queue_override_transmit(team, skb);
  1360. if (!tx_success)
  1361. tx_success = team->ops.transmit(team, skb);
  1362. if (tx_success) {
  1363. struct team_pcpu_stats *pcpu_stats;
  1364. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1365. u64_stats_update_begin(&pcpu_stats->syncp);
  1366. pcpu_stats->tx_packets++;
  1367. pcpu_stats->tx_bytes += len;
  1368. u64_stats_update_end(&pcpu_stats->syncp);
  1369. } else {
  1370. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1371. }
  1372. return NETDEV_TX_OK;
  1373. }
  1374. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
  1375. {
  1376. /*
  1377. * This helper function exists to help dev_pick_tx get the correct
  1378. * destination queue. Using a helper function skips a call to
  1379. * skb_tx_hash and will put the skbs in the queue we expect on their
  1380. * way down to the team driver.
  1381. */
  1382. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1383. /*
  1384. * Save the original txq to restore before passing to the driver
  1385. */
  1386. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1387. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1388. do {
  1389. txq -= dev->real_num_tx_queues;
  1390. } while (txq >= dev->real_num_tx_queues);
  1391. }
  1392. return txq;
  1393. }
  1394. static void team_change_rx_flags(struct net_device *dev, int change)
  1395. {
  1396. struct team *team = netdev_priv(dev);
  1397. struct team_port *port;
  1398. int inc;
  1399. rcu_read_lock();
  1400. list_for_each_entry_rcu(port, &team->port_list, list) {
  1401. if (change & IFF_PROMISC) {
  1402. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1403. dev_set_promiscuity(port->dev, inc);
  1404. }
  1405. if (change & IFF_ALLMULTI) {
  1406. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1407. dev_set_allmulti(port->dev, inc);
  1408. }
  1409. }
  1410. rcu_read_unlock();
  1411. }
  1412. static void team_set_rx_mode(struct net_device *dev)
  1413. {
  1414. struct team *team = netdev_priv(dev);
  1415. struct team_port *port;
  1416. rcu_read_lock();
  1417. list_for_each_entry_rcu(port, &team->port_list, list) {
  1418. dev_uc_sync_multiple(port->dev, dev);
  1419. dev_mc_sync_multiple(port->dev, dev);
  1420. }
  1421. rcu_read_unlock();
  1422. }
  1423. static int team_set_mac_address(struct net_device *dev, void *p)
  1424. {
  1425. struct sockaddr *addr = p;
  1426. struct team *team = netdev_priv(dev);
  1427. struct team_port *port;
  1428. if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
  1429. return -EADDRNOTAVAIL;
  1430. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1431. rcu_read_lock();
  1432. list_for_each_entry_rcu(port, &team->port_list, list)
  1433. if (team->ops.port_change_dev_addr)
  1434. team->ops.port_change_dev_addr(team, port);
  1435. rcu_read_unlock();
  1436. return 0;
  1437. }
  1438. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1439. {
  1440. struct team *team = netdev_priv(dev);
  1441. struct team_port *port;
  1442. int err;
  1443. /*
  1444. * Alhough this is reader, it's guarded by team lock. It's not possible
  1445. * to traverse list in reverse under rcu_read_lock
  1446. */
  1447. mutex_lock(&team->lock);
  1448. list_for_each_entry(port, &team->port_list, list) {
  1449. err = dev_set_mtu(port->dev, new_mtu);
  1450. if (err) {
  1451. netdev_err(dev, "Device %s failed to change mtu",
  1452. port->dev->name);
  1453. goto unwind;
  1454. }
  1455. }
  1456. mutex_unlock(&team->lock);
  1457. dev->mtu = new_mtu;
  1458. return 0;
  1459. unwind:
  1460. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1461. dev_set_mtu(port->dev, dev->mtu);
  1462. mutex_unlock(&team->lock);
  1463. return err;
  1464. }
  1465. static struct rtnl_link_stats64 *
  1466. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1467. {
  1468. struct team *team = netdev_priv(dev);
  1469. struct team_pcpu_stats *p;
  1470. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1471. u32 rx_dropped = 0, tx_dropped = 0;
  1472. unsigned int start;
  1473. int i;
  1474. for_each_possible_cpu(i) {
  1475. p = per_cpu_ptr(team->pcpu_stats, i);
  1476. do {
  1477. start = u64_stats_fetch_begin_bh(&p->syncp);
  1478. rx_packets = p->rx_packets;
  1479. rx_bytes = p->rx_bytes;
  1480. rx_multicast = p->rx_multicast;
  1481. tx_packets = p->tx_packets;
  1482. tx_bytes = p->tx_bytes;
  1483. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  1484. stats->rx_packets += rx_packets;
  1485. stats->rx_bytes += rx_bytes;
  1486. stats->multicast += rx_multicast;
  1487. stats->tx_packets += tx_packets;
  1488. stats->tx_bytes += tx_bytes;
  1489. /*
  1490. * rx_dropped & tx_dropped are u32, updated
  1491. * without syncp protection.
  1492. */
  1493. rx_dropped += p->rx_dropped;
  1494. tx_dropped += p->tx_dropped;
  1495. }
  1496. stats->rx_dropped = rx_dropped;
  1497. stats->tx_dropped = tx_dropped;
  1498. return stats;
  1499. }
  1500. static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  1501. {
  1502. struct team *team = netdev_priv(dev);
  1503. struct team_port *port;
  1504. int err;
  1505. /*
  1506. * Alhough this is reader, it's guarded by team lock. It's not possible
  1507. * to traverse list in reverse under rcu_read_lock
  1508. */
  1509. mutex_lock(&team->lock);
  1510. list_for_each_entry(port, &team->port_list, list) {
  1511. err = vlan_vid_add(port->dev, proto, vid);
  1512. if (err)
  1513. goto unwind;
  1514. }
  1515. mutex_unlock(&team->lock);
  1516. return 0;
  1517. unwind:
  1518. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1519. vlan_vid_del(port->dev, proto, vid);
  1520. mutex_unlock(&team->lock);
  1521. return err;
  1522. }
  1523. static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
  1524. {
  1525. struct team *team = netdev_priv(dev);
  1526. struct team_port *port;
  1527. rcu_read_lock();
  1528. list_for_each_entry_rcu(port, &team->port_list, list)
  1529. vlan_vid_del(port->dev, proto, vid);
  1530. rcu_read_unlock();
  1531. return 0;
  1532. }
  1533. #ifdef CONFIG_NET_POLL_CONTROLLER
  1534. static void team_poll_controller(struct net_device *dev)
  1535. {
  1536. }
  1537. static void __team_netpoll_cleanup(struct team *team)
  1538. {
  1539. struct team_port *port;
  1540. list_for_each_entry(port, &team->port_list, list)
  1541. team_port_disable_netpoll(port);
  1542. }
  1543. static void team_netpoll_cleanup(struct net_device *dev)
  1544. {
  1545. struct team *team = netdev_priv(dev);
  1546. mutex_lock(&team->lock);
  1547. __team_netpoll_cleanup(team);
  1548. mutex_unlock(&team->lock);
  1549. }
  1550. static int team_netpoll_setup(struct net_device *dev,
  1551. struct netpoll_info *npifo, gfp_t gfp)
  1552. {
  1553. struct team *team = netdev_priv(dev);
  1554. struct team_port *port;
  1555. int err = 0;
  1556. mutex_lock(&team->lock);
  1557. list_for_each_entry(port, &team->port_list, list) {
  1558. err = team_port_enable_netpoll(team, port, gfp);
  1559. if (err) {
  1560. __team_netpoll_cleanup(team);
  1561. break;
  1562. }
  1563. }
  1564. mutex_unlock(&team->lock);
  1565. return err;
  1566. }
  1567. #endif
  1568. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1569. {
  1570. struct team *team = netdev_priv(dev);
  1571. int err;
  1572. mutex_lock(&team->lock);
  1573. err = team_port_add(team, port_dev);
  1574. mutex_unlock(&team->lock);
  1575. return err;
  1576. }
  1577. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1578. {
  1579. struct team *team = netdev_priv(dev);
  1580. int err;
  1581. mutex_lock(&team->lock);
  1582. err = team_port_del(team, port_dev);
  1583. mutex_unlock(&team->lock);
  1584. return err;
  1585. }
  1586. static netdev_features_t team_fix_features(struct net_device *dev,
  1587. netdev_features_t features)
  1588. {
  1589. struct team_port *port;
  1590. struct team *team = netdev_priv(dev);
  1591. netdev_features_t mask;
  1592. mask = features;
  1593. features &= ~NETIF_F_ONE_FOR_ALL;
  1594. features |= NETIF_F_ALL_FOR_ALL;
  1595. rcu_read_lock();
  1596. list_for_each_entry_rcu(port, &team->port_list, list) {
  1597. features = netdev_increment_features(features,
  1598. port->dev->features,
  1599. mask);
  1600. }
  1601. rcu_read_unlock();
  1602. return features;
  1603. }
  1604. static int team_change_carrier(struct net_device *dev, bool new_carrier)
  1605. {
  1606. struct team *team = netdev_priv(dev);
  1607. team->user_carrier_enabled = true;
  1608. if (new_carrier)
  1609. netif_carrier_on(dev);
  1610. else
  1611. netif_carrier_off(dev);
  1612. return 0;
  1613. }
  1614. static const struct net_device_ops team_netdev_ops = {
  1615. .ndo_init = team_init,
  1616. .ndo_uninit = team_uninit,
  1617. .ndo_open = team_open,
  1618. .ndo_stop = team_close,
  1619. .ndo_start_xmit = team_xmit,
  1620. .ndo_select_queue = team_select_queue,
  1621. .ndo_change_rx_flags = team_change_rx_flags,
  1622. .ndo_set_rx_mode = team_set_rx_mode,
  1623. .ndo_set_mac_address = team_set_mac_address,
  1624. .ndo_change_mtu = team_change_mtu,
  1625. .ndo_get_stats64 = team_get_stats64,
  1626. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1627. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1628. #ifdef CONFIG_NET_POLL_CONTROLLER
  1629. .ndo_poll_controller = team_poll_controller,
  1630. .ndo_netpoll_setup = team_netpoll_setup,
  1631. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1632. #endif
  1633. .ndo_add_slave = team_add_slave,
  1634. .ndo_del_slave = team_del_slave,
  1635. .ndo_fix_features = team_fix_features,
  1636. .ndo_change_carrier = team_change_carrier,
  1637. };
  1638. /***********************
  1639. * ethtool interface
  1640. ***********************/
  1641. static void team_ethtool_get_drvinfo(struct net_device *dev,
  1642. struct ethtool_drvinfo *drvinfo)
  1643. {
  1644. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  1645. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1646. }
  1647. static const struct ethtool_ops team_ethtool_ops = {
  1648. .get_drvinfo = team_ethtool_get_drvinfo,
  1649. .get_link = ethtool_op_get_link,
  1650. };
  1651. /***********************
  1652. * rt netlink interface
  1653. ***********************/
  1654. static void team_setup_by_port(struct net_device *dev,
  1655. struct net_device *port_dev)
  1656. {
  1657. dev->header_ops = port_dev->header_ops;
  1658. dev->type = port_dev->type;
  1659. dev->hard_header_len = port_dev->hard_header_len;
  1660. dev->addr_len = port_dev->addr_len;
  1661. dev->mtu = port_dev->mtu;
  1662. memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
  1663. eth_hw_addr_inherit(dev, port_dev);
  1664. }
  1665. static int team_dev_type_check_change(struct net_device *dev,
  1666. struct net_device *port_dev)
  1667. {
  1668. struct team *team = netdev_priv(dev);
  1669. char *portname = port_dev->name;
  1670. int err;
  1671. if (dev->type == port_dev->type)
  1672. return 0;
  1673. if (!list_empty(&team->port_list)) {
  1674. netdev_err(dev, "Device %s is of different type\n", portname);
  1675. return -EBUSY;
  1676. }
  1677. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  1678. err = notifier_to_errno(err);
  1679. if (err) {
  1680. netdev_err(dev, "Refused to change device type\n");
  1681. return err;
  1682. }
  1683. dev_uc_flush(dev);
  1684. dev_mc_flush(dev);
  1685. team_setup_by_port(dev, port_dev);
  1686. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  1687. return 0;
  1688. }
  1689. static void team_setup(struct net_device *dev)
  1690. {
  1691. ether_setup(dev);
  1692. dev->netdev_ops = &team_netdev_ops;
  1693. dev->ethtool_ops = &team_ethtool_ops;
  1694. dev->destructor = team_destructor;
  1695. dev->tx_queue_len = 0;
  1696. dev->flags |= IFF_MULTICAST;
  1697. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1698. /*
  1699. * Indicate we support unicast address filtering. That way core won't
  1700. * bring us to promisc mode in case a unicast addr is added.
  1701. * Let this up to underlay drivers.
  1702. */
  1703. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1704. dev->features |= NETIF_F_LLTX;
  1705. dev->features |= NETIF_F_GRO;
  1706. dev->hw_features = TEAM_VLAN_FEATURES |
  1707. NETIF_F_HW_VLAN_CTAG_TX |
  1708. NETIF_F_HW_VLAN_CTAG_RX |
  1709. NETIF_F_HW_VLAN_CTAG_FILTER;
  1710. dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
  1711. dev->features |= dev->hw_features;
  1712. }
  1713. static int team_newlink(struct net *src_net, struct net_device *dev,
  1714. struct nlattr *tb[], struct nlattr *data[])
  1715. {
  1716. int err;
  1717. if (tb[IFLA_ADDRESS] == NULL)
  1718. eth_hw_addr_random(dev);
  1719. err = register_netdevice(dev);
  1720. if (err)
  1721. return err;
  1722. return 0;
  1723. }
  1724. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1725. {
  1726. if (tb[IFLA_ADDRESS]) {
  1727. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1728. return -EINVAL;
  1729. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1730. return -EADDRNOTAVAIL;
  1731. }
  1732. return 0;
  1733. }
  1734. static unsigned int team_get_num_tx_queues(void)
  1735. {
  1736. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1737. }
  1738. static unsigned int team_get_num_rx_queues(void)
  1739. {
  1740. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1741. }
  1742. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1743. .kind = DRV_NAME,
  1744. .priv_size = sizeof(struct team),
  1745. .setup = team_setup,
  1746. .newlink = team_newlink,
  1747. .validate = team_validate,
  1748. .get_num_tx_queues = team_get_num_tx_queues,
  1749. .get_num_rx_queues = team_get_num_rx_queues,
  1750. };
  1751. /***********************************
  1752. * Generic netlink custom interface
  1753. ***********************************/
  1754. static struct genl_family team_nl_family = {
  1755. .id = GENL_ID_GENERATE,
  1756. .name = TEAM_GENL_NAME,
  1757. .version = TEAM_GENL_VERSION,
  1758. .maxattr = TEAM_ATTR_MAX,
  1759. .netnsok = true,
  1760. };
  1761. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1762. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1763. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1764. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1765. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1766. };
  1767. static const struct nla_policy
  1768. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1769. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1770. [TEAM_ATTR_OPTION_NAME] = {
  1771. .type = NLA_STRING,
  1772. .len = TEAM_STRING_MAX_LEN,
  1773. },
  1774. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1775. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1776. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1777. };
  1778. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1779. {
  1780. struct sk_buff *msg;
  1781. void *hdr;
  1782. int err;
  1783. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1784. if (!msg)
  1785. return -ENOMEM;
  1786. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1787. &team_nl_family, 0, TEAM_CMD_NOOP);
  1788. if (!hdr) {
  1789. err = -EMSGSIZE;
  1790. goto err_msg_put;
  1791. }
  1792. genlmsg_end(msg, hdr);
  1793. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  1794. err_msg_put:
  1795. nlmsg_free(msg);
  1796. return err;
  1797. }
  1798. /*
  1799. * Netlink cmd functions should be locked by following two functions.
  1800. * Since dev gets held here, that ensures dev won't disappear in between.
  1801. */
  1802. static struct team *team_nl_team_get(struct genl_info *info)
  1803. {
  1804. struct net *net = genl_info_net(info);
  1805. int ifindex;
  1806. struct net_device *dev;
  1807. struct team *team;
  1808. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1809. return NULL;
  1810. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1811. dev = dev_get_by_index(net, ifindex);
  1812. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1813. if (dev)
  1814. dev_put(dev);
  1815. return NULL;
  1816. }
  1817. team = netdev_priv(dev);
  1818. mutex_lock(&team->lock);
  1819. return team;
  1820. }
  1821. static void team_nl_team_put(struct team *team)
  1822. {
  1823. mutex_unlock(&team->lock);
  1824. dev_put(team->dev);
  1825. }
  1826. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1827. struct team *team, u32 portid);
  1828. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
  1829. {
  1830. return genlmsg_unicast(dev_net(team->dev), skb, portid);
  1831. }
  1832. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1833. struct team_option_inst *opt_inst)
  1834. {
  1835. struct nlattr *option_item;
  1836. struct team_option *option = opt_inst->option;
  1837. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1838. struct team_gsetter_ctx ctx;
  1839. int err;
  1840. ctx.info = opt_inst_info;
  1841. err = team_option_get(team, opt_inst, &ctx);
  1842. if (err)
  1843. return err;
  1844. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1845. if (!option_item)
  1846. return -EMSGSIZE;
  1847. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1848. goto nest_cancel;
  1849. if (opt_inst_info->port &&
  1850. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1851. opt_inst_info->port->dev->ifindex))
  1852. goto nest_cancel;
  1853. if (opt_inst->option->array_size &&
  1854. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1855. opt_inst_info->array_index))
  1856. goto nest_cancel;
  1857. switch (option->type) {
  1858. case TEAM_OPTION_TYPE_U32:
  1859. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1860. goto nest_cancel;
  1861. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1862. goto nest_cancel;
  1863. break;
  1864. case TEAM_OPTION_TYPE_STRING:
  1865. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1866. goto nest_cancel;
  1867. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1868. ctx.data.str_val))
  1869. goto nest_cancel;
  1870. break;
  1871. case TEAM_OPTION_TYPE_BINARY:
  1872. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1873. goto nest_cancel;
  1874. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1875. ctx.data.bin_val.ptr))
  1876. goto nest_cancel;
  1877. break;
  1878. case TEAM_OPTION_TYPE_BOOL:
  1879. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1880. goto nest_cancel;
  1881. if (ctx.data.bool_val &&
  1882. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1883. goto nest_cancel;
  1884. break;
  1885. case TEAM_OPTION_TYPE_S32:
  1886. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1887. goto nest_cancel;
  1888. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1889. goto nest_cancel;
  1890. break;
  1891. default:
  1892. BUG();
  1893. }
  1894. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1895. goto nest_cancel;
  1896. if (opt_inst->changed) {
  1897. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1898. goto nest_cancel;
  1899. opt_inst->changed = false;
  1900. }
  1901. nla_nest_end(skb, option_item);
  1902. return 0;
  1903. nest_cancel:
  1904. nla_nest_cancel(skb, option_item);
  1905. return -EMSGSIZE;
  1906. }
  1907. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1908. struct team *team, u32 portid,
  1909. team_nl_send_func_t *send_func)
  1910. {
  1911. int err;
  1912. if (*pskb) {
  1913. err = send_func(*pskb, team, portid);
  1914. if (err)
  1915. return err;
  1916. }
  1917. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1918. if (!*pskb)
  1919. return -ENOMEM;
  1920. return 0;
  1921. }
  1922. static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
  1923. int flags, team_nl_send_func_t *send_func,
  1924. struct list_head *sel_opt_inst_list)
  1925. {
  1926. struct nlattr *option_list;
  1927. struct nlmsghdr *nlh;
  1928. void *hdr;
  1929. struct team_option_inst *opt_inst;
  1930. int err;
  1931. struct sk_buff *skb = NULL;
  1932. bool incomplete;
  1933. int i;
  1934. opt_inst = list_first_entry(sel_opt_inst_list,
  1935. struct team_option_inst, tmp_list);
  1936. start_again:
  1937. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1938. if (err)
  1939. return err;
  1940. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  1941. TEAM_CMD_OPTIONS_GET);
  1942. if (!hdr)
  1943. return -EMSGSIZE;
  1944. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1945. goto nla_put_failure;
  1946. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1947. if (!option_list)
  1948. goto nla_put_failure;
  1949. i = 0;
  1950. incomplete = false;
  1951. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  1952. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  1953. if (err) {
  1954. if (err == -EMSGSIZE) {
  1955. if (!i)
  1956. goto errout;
  1957. incomplete = true;
  1958. break;
  1959. }
  1960. goto errout;
  1961. }
  1962. i++;
  1963. }
  1964. nla_nest_end(skb, option_list);
  1965. genlmsg_end(skb, hdr);
  1966. if (incomplete)
  1967. goto start_again;
  1968. send_done:
  1969. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1970. if (!nlh) {
  1971. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1972. if (err)
  1973. goto errout;
  1974. goto send_done;
  1975. }
  1976. return send_func(skb, team, portid);
  1977. nla_put_failure:
  1978. err = -EMSGSIZE;
  1979. errout:
  1980. genlmsg_cancel(skb, hdr);
  1981. nlmsg_free(skb);
  1982. return err;
  1983. }
  1984. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1985. {
  1986. struct team *team;
  1987. struct team_option_inst *opt_inst;
  1988. int err;
  1989. LIST_HEAD(sel_opt_inst_list);
  1990. team = team_nl_team_get(info);
  1991. if (!team)
  1992. return -EINVAL;
  1993. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  1994. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  1995. err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
  1996. NLM_F_ACK, team_nl_send_unicast,
  1997. &sel_opt_inst_list);
  1998. team_nl_team_put(team);
  1999. return err;
  2000. }
  2001. static int team_nl_send_event_options_get(struct team *team,
  2002. struct list_head *sel_opt_inst_list);
  2003. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  2004. {
  2005. struct team *team;
  2006. int err = 0;
  2007. int i;
  2008. struct nlattr *nl_option;
  2009. LIST_HEAD(opt_inst_list);
  2010. team = team_nl_team_get(info);
  2011. if (!team)
  2012. return -EINVAL;
  2013. err = -EINVAL;
  2014. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  2015. err = -EINVAL;
  2016. goto team_put;
  2017. }
  2018. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  2019. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  2020. struct nlattr *attr;
  2021. struct nlattr *attr_data;
  2022. enum team_option_type opt_type;
  2023. int opt_port_ifindex = 0; /* != 0 for per-port options */
  2024. u32 opt_array_index = 0;
  2025. bool opt_is_array = false;
  2026. struct team_option_inst *opt_inst;
  2027. char *opt_name;
  2028. bool opt_found = false;
  2029. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  2030. err = -EINVAL;
  2031. goto team_put;
  2032. }
  2033. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  2034. nl_option, team_nl_option_policy);
  2035. if (err)
  2036. goto team_put;
  2037. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  2038. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  2039. err = -EINVAL;
  2040. goto team_put;
  2041. }
  2042. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  2043. case NLA_U32:
  2044. opt_type = TEAM_OPTION_TYPE_U32;
  2045. break;
  2046. case NLA_STRING:
  2047. opt_type = TEAM_OPTION_TYPE_STRING;
  2048. break;
  2049. case NLA_BINARY:
  2050. opt_type = TEAM_OPTION_TYPE_BINARY;
  2051. break;
  2052. case NLA_FLAG:
  2053. opt_type = TEAM_OPTION_TYPE_BOOL;
  2054. break;
  2055. case NLA_S32:
  2056. opt_type = TEAM_OPTION_TYPE_S32;
  2057. break;
  2058. default:
  2059. goto team_put;
  2060. }
  2061. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  2062. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  2063. err = -EINVAL;
  2064. goto team_put;
  2065. }
  2066. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  2067. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  2068. if (attr)
  2069. opt_port_ifindex = nla_get_u32(attr);
  2070. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  2071. if (attr) {
  2072. opt_is_array = true;
  2073. opt_array_index = nla_get_u32(attr);
  2074. }
  2075. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2076. struct team_option *option = opt_inst->option;
  2077. struct team_gsetter_ctx ctx;
  2078. struct team_option_inst_info *opt_inst_info;
  2079. int tmp_ifindex;
  2080. opt_inst_info = &opt_inst->info;
  2081. tmp_ifindex = opt_inst_info->port ?
  2082. opt_inst_info->port->dev->ifindex : 0;
  2083. if (option->type != opt_type ||
  2084. strcmp(option->name, opt_name) ||
  2085. tmp_ifindex != opt_port_ifindex ||
  2086. (option->array_size && !opt_is_array) ||
  2087. opt_inst_info->array_index != opt_array_index)
  2088. continue;
  2089. opt_found = true;
  2090. ctx.info = opt_inst_info;
  2091. switch (opt_type) {
  2092. case TEAM_OPTION_TYPE_U32:
  2093. ctx.data.u32_val = nla_get_u32(attr_data);
  2094. break;
  2095. case TEAM_OPTION_TYPE_STRING:
  2096. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  2097. err = -EINVAL;
  2098. goto team_put;
  2099. }
  2100. ctx.data.str_val = nla_data(attr_data);
  2101. break;
  2102. case TEAM_OPTION_TYPE_BINARY:
  2103. ctx.data.bin_val.len = nla_len(attr_data);
  2104. ctx.data.bin_val.ptr = nla_data(attr_data);
  2105. break;
  2106. case TEAM_OPTION_TYPE_BOOL:
  2107. ctx.data.bool_val = attr_data ? true : false;
  2108. break;
  2109. case TEAM_OPTION_TYPE_S32:
  2110. ctx.data.s32_val = nla_get_s32(attr_data);
  2111. break;
  2112. default:
  2113. BUG();
  2114. }
  2115. err = team_option_set(team, opt_inst, &ctx);
  2116. if (err)
  2117. goto team_put;
  2118. opt_inst->changed = true;
  2119. list_add(&opt_inst->tmp_list, &opt_inst_list);
  2120. }
  2121. if (!opt_found) {
  2122. err = -ENOENT;
  2123. goto team_put;
  2124. }
  2125. }
  2126. err = team_nl_send_event_options_get(team, &opt_inst_list);
  2127. team_put:
  2128. team_nl_team_put(team);
  2129. return err;
  2130. }
  2131. static int team_nl_fill_one_port_get(struct sk_buff *skb,
  2132. struct team_port *port)
  2133. {
  2134. struct nlattr *port_item;
  2135. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  2136. if (!port_item)
  2137. goto nest_cancel;
  2138. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  2139. goto nest_cancel;
  2140. if (port->changed) {
  2141. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  2142. goto nest_cancel;
  2143. port->changed = false;
  2144. }
  2145. if ((port->removed &&
  2146. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  2147. (port->state.linkup &&
  2148. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  2149. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  2150. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  2151. goto nest_cancel;
  2152. nla_nest_end(skb, port_item);
  2153. return 0;
  2154. nest_cancel:
  2155. nla_nest_cancel(skb, port_item);
  2156. return -EMSGSIZE;
  2157. }
  2158. static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
  2159. int flags, team_nl_send_func_t *send_func,
  2160. struct team_port *one_port)
  2161. {
  2162. struct nlattr *port_list;
  2163. struct nlmsghdr *nlh;
  2164. void *hdr;
  2165. struct team_port *port;
  2166. int err;
  2167. struct sk_buff *skb = NULL;
  2168. bool incomplete;
  2169. int i;
  2170. port = list_first_entry_or_null(&team->port_list,
  2171. struct team_port, list);
  2172. start_again:
  2173. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2174. if (err)
  2175. return err;
  2176. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2177. TEAM_CMD_PORT_LIST_GET);
  2178. if (!hdr)
  2179. return -EMSGSIZE;
  2180. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2181. goto nla_put_failure;
  2182. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  2183. if (!port_list)
  2184. goto nla_put_failure;
  2185. i = 0;
  2186. incomplete = false;
  2187. /* If one port is selected, called wants to send port list containing
  2188. * only this port. Otherwise go through all listed ports and send all
  2189. */
  2190. if (one_port) {
  2191. err = team_nl_fill_one_port_get(skb, one_port);
  2192. if (err)
  2193. goto errout;
  2194. } else if (port) {
  2195. list_for_each_entry_from(port, &team->port_list, list) {
  2196. err = team_nl_fill_one_port_get(skb, port);
  2197. if (err) {
  2198. if (err == -EMSGSIZE) {
  2199. if (!i)
  2200. goto errout;
  2201. incomplete = true;
  2202. break;
  2203. }
  2204. goto errout;
  2205. }
  2206. i++;
  2207. }
  2208. }
  2209. nla_nest_end(skb, port_list);
  2210. genlmsg_end(skb, hdr);
  2211. if (incomplete)
  2212. goto start_again;
  2213. send_done:
  2214. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2215. if (!nlh) {
  2216. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2217. if (err)
  2218. goto errout;
  2219. goto send_done;
  2220. }
  2221. return send_func(skb, team, portid);
  2222. nla_put_failure:
  2223. err = -EMSGSIZE;
  2224. errout:
  2225. genlmsg_cancel(skb, hdr);
  2226. nlmsg_free(skb);
  2227. return err;
  2228. }
  2229. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  2230. struct genl_info *info)
  2231. {
  2232. struct team *team;
  2233. int err;
  2234. team = team_nl_team_get(info);
  2235. if (!team)
  2236. return -EINVAL;
  2237. err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
  2238. NLM_F_ACK, team_nl_send_unicast, NULL);
  2239. team_nl_team_put(team);
  2240. return err;
  2241. }
  2242. static struct genl_ops team_nl_ops[] = {
  2243. {
  2244. .cmd = TEAM_CMD_NOOP,
  2245. .doit = team_nl_cmd_noop,
  2246. .policy = team_nl_policy,
  2247. },
  2248. {
  2249. .cmd = TEAM_CMD_OPTIONS_SET,
  2250. .doit = team_nl_cmd_options_set,
  2251. .policy = team_nl_policy,
  2252. .flags = GENL_ADMIN_PERM,
  2253. },
  2254. {
  2255. .cmd = TEAM_CMD_OPTIONS_GET,
  2256. .doit = team_nl_cmd_options_get,
  2257. .policy = team_nl_policy,
  2258. .flags = GENL_ADMIN_PERM,
  2259. },
  2260. {
  2261. .cmd = TEAM_CMD_PORT_LIST_GET,
  2262. .doit = team_nl_cmd_port_list_get,
  2263. .policy = team_nl_policy,
  2264. .flags = GENL_ADMIN_PERM,
  2265. },
  2266. };
  2267. static struct genl_multicast_group team_change_event_mcgrp = {
  2268. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  2269. };
  2270. static int team_nl_send_multicast(struct sk_buff *skb,
  2271. struct team *team, u32 portid)
  2272. {
  2273. return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
  2274. team_change_event_mcgrp.id, GFP_KERNEL);
  2275. }
  2276. static int team_nl_send_event_options_get(struct team *team,
  2277. struct list_head *sel_opt_inst_list)
  2278. {
  2279. return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
  2280. sel_opt_inst_list);
  2281. }
  2282. static int team_nl_send_event_port_get(struct team *team,
  2283. struct team_port *port)
  2284. {
  2285. return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
  2286. port);
  2287. }
  2288. static int team_nl_init(void)
  2289. {
  2290. int err;
  2291. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  2292. ARRAY_SIZE(team_nl_ops));
  2293. if (err)
  2294. return err;
  2295. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  2296. if (err)
  2297. goto err_change_event_grp_reg;
  2298. return 0;
  2299. err_change_event_grp_reg:
  2300. genl_unregister_family(&team_nl_family);
  2301. return err;
  2302. }
  2303. static void team_nl_fini(void)
  2304. {
  2305. genl_unregister_family(&team_nl_family);
  2306. }
  2307. /******************
  2308. * Change checkers
  2309. ******************/
  2310. static void __team_options_change_check(struct team *team)
  2311. {
  2312. int err;
  2313. struct team_option_inst *opt_inst;
  2314. LIST_HEAD(sel_opt_inst_list);
  2315. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2316. if (opt_inst->changed)
  2317. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2318. }
  2319. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2320. if (err && err != -ESRCH)
  2321. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2322. err);
  2323. }
  2324. /* rtnl lock is held */
  2325. static void __team_port_change_send(struct team_port *port, bool linkup)
  2326. {
  2327. int err;
  2328. port->changed = true;
  2329. port->state.linkup = linkup;
  2330. team_refresh_port_linkup(port);
  2331. if (linkup) {
  2332. struct ethtool_cmd ecmd;
  2333. err = __ethtool_get_settings(port->dev, &ecmd);
  2334. if (!err) {
  2335. port->state.speed = ethtool_cmd_speed(&ecmd);
  2336. port->state.duplex = ecmd.duplex;
  2337. goto send_event;
  2338. }
  2339. }
  2340. port->state.speed = 0;
  2341. port->state.duplex = 0;
  2342. send_event:
  2343. err = team_nl_send_event_port_get(port->team, port);
  2344. if (err && err != -ESRCH)
  2345. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
  2346. port->dev->name, err);
  2347. }
  2348. static void __team_carrier_check(struct team *team)
  2349. {
  2350. struct team_port *port;
  2351. bool team_linkup;
  2352. if (team->user_carrier_enabled)
  2353. return;
  2354. team_linkup = false;
  2355. list_for_each_entry(port, &team->port_list, list) {
  2356. if (port->linkup) {
  2357. team_linkup = true;
  2358. break;
  2359. }
  2360. }
  2361. if (team_linkup)
  2362. netif_carrier_on(team->dev);
  2363. else
  2364. netif_carrier_off(team->dev);
  2365. }
  2366. static void __team_port_change_check(struct team_port *port, bool linkup)
  2367. {
  2368. if (port->state.linkup != linkup)
  2369. __team_port_change_send(port, linkup);
  2370. __team_carrier_check(port->team);
  2371. }
  2372. static void __team_port_change_port_added(struct team_port *port, bool linkup)
  2373. {
  2374. __team_port_change_send(port, linkup);
  2375. __team_carrier_check(port->team);
  2376. }
  2377. static void __team_port_change_port_removed(struct team_port *port)
  2378. {
  2379. port->removed = true;
  2380. __team_port_change_send(port, false);
  2381. __team_carrier_check(port->team);
  2382. }
  2383. static void team_port_change_check(struct team_port *port, bool linkup)
  2384. {
  2385. struct team *team = port->team;
  2386. mutex_lock(&team->lock);
  2387. __team_port_change_check(port, linkup);
  2388. mutex_unlock(&team->lock);
  2389. }
  2390. /************************************
  2391. * Net device notifier event handler
  2392. ************************************/
  2393. static int team_device_event(struct notifier_block *unused,
  2394. unsigned long event, void *ptr)
  2395. {
  2396. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2397. struct team_port *port;
  2398. port = team_port_get_rtnl(dev);
  2399. if (!port)
  2400. return NOTIFY_DONE;
  2401. switch (event) {
  2402. case NETDEV_UP:
  2403. if (netif_carrier_ok(dev))
  2404. team_port_change_check(port, true);
  2405. case NETDEV_DOWN:
  2406. team_port_change_check(port, false);
  2407. case NETDEV_CHANGE:
  2408. if (netif_running(port->dev))
  2409. team_port_change_check(port,
  2410. !!netif_carrier_ok(port->dev));
  2411. break;
  2412. case NETDEV_UNREGISTER:
  2413. team_del_slave(port->team->dev, dev);
  2414. break;
  2415. case NETDEV_FEAT_CHANGE:
  2416. team_compute_features(port->team);
  2417. break;
  2418. case NETDEV_CHANGEMTU:
  2419. /* Forbid to change mtu of underlaying device */
  2420. return NOTIFY_BAD;
  2421. case NETDEV_PRE_TYPE_CHANGE:
  2422. /* Forbid to change type of underlaying device */
  2423. return NOTIFY_BAD;
  2424. case NETDEV_RESEND_IGMP:
  2425. /* Propagate to master device */
  2426. call_netdevice_notifiers(event, port->team->dev);
  2427. break;
  2428. }
  2429. return NOTIFY_DONE;
  2430. }
  2431. static struct notifier_block team_notifier_block __read_mostly = {
  2432. .notifier_call = team_device_event,
  2433. };
  2434. /***********************
  2435. * Module init and exit
  2436. ***********************/
  2437. static int __init team_module_init(void)
  2438. {
  2439. int err;
  2440. register_netdevice_notifier(&team_notifier_block);
  2441. err = rtnl_link_register(&team_link_ops);
  2442. if (err)
  2443. goto err_rtnl_reg;
  2444. err = team_nl_init();
  2445. if (err)
  2446. goto err_nl_init;
  2447. return 0;
  2448. err_nl_init:
  2449. rtnl_link_unregister(&team_link_ops);
  2450. err_rtnl_reg:
  2451. unregister_netdevice_notifier(&team_notifier_block);
  2452. return err;
  2453. }
  2454. static void __exit team_module_exit(void)
  2455. {
  2456. team_nl_fini();
  2457. rtnl_link_unregister(&team_link_ops);
  2458. unregister_netdevice_notifier(&team_notifier_block);
  2459. }
  2460. module_init(team_module_init);
  2461. module_exit(team_module_exit);
  2462. MODULE_LICENSE("GPL v2");
  2463. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2464. MODULE_DESCRIPTION("Ethernet team device driver");
  2465. MODULE_ALIAS_RTNL_LINK(DRV_NAME);