team.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919
  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 (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1293. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1294. INIT_LIST_HEAD(&team->port_list);
  1295. err = team_queue_override_init(team);
  1296. if (err)
  1297. goto err_team_queue_override_init;
  1298. team_adjust_ops(team);
  1299. INIT_LIST_HEAD(&team->option_list);
  1300. INIT_LIST_HEAD(&team->option_inst_list);
  1301. team_notify_peers_init(team);
  1302. team_mcast_rejoin_init(team);
  1303. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1304. if (err)
  1305. goto err_options_register;
  1306. netif_carrier_off(dev);
  1307. team_set_lockdep_class(dev);
  1308. return 0;
  1309. err_options_register:
  1310. team_mcast_rejoin_fini(team);
  1311. team_notify_peers_fini(team);
  1312. team_queue_override_fini(team);
  1313. err_team_queue_override_init:
  1314. free_percpu(team->pcpu_stats);
  1315. return err;
  1316. }
  1317. static void team_uninit(struct net_device *dev)
  1318. {
  1319. struct team *team = netdev_priv(dev);
  1320. struct team_port *port;
  1321. struct team_port *tmp;
  1322. mutex_lock(&team->lock);
  1323. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1324. team_port_del(team, port->dev);
  1325. __team_change_mode(team, NULL); /* cleanup */
  1326. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1327. team_mcast_rejoin_fini(team);
  1328. team_notify_peers_fini(team);
  1329. team_queue_override_fini(team);
  1330. mutex_unlock(&team->lock);
  1331. }
  1332. static void team_destructor(struct net_device *dev)
  1333. {
  1334. struct team *team = netdev_priv(dev);
  1335. free_percpu(team->pcpu_stats);
  1336. free_netdev(dev);
  1337. }
  1338. static int team_open(struct net_device *dev)
  1339. {
  1340. return 0;
  1341. }
  1342. static int team_close(struct net_device *dev)
  1343. {
  1344. return 0;
  1345. }
  1346. /*
  1347. * note: already called with rcu_read_lock
  1348. */
  1349. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1350. {
  1351. struct team *team = netdev_priv(dev);
  1352. bool tx_success;
  1353. unsigned int len = skb->len;
  1354. tx_success = team_queue_override_transmit(team, skb);
  1355. if (!tx_success)
  1356. tx_success = team->ops.transmit(team, skb);
  1357. if (tx_success) {
  1358. struct team_pcpu_stats *pcpu_stats;
  1359. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1360. u64_stats_update_begin(&pcpu_stats->syncp);
  1361. pcpu_stats->tx_packets++;
  1362. pcpu_stats->tx_bytes += len;
  1363. u64_stats_update_end(&pcpu_stats->syncp);
  1364. } else {
  1365. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1366. }
  1367. return NETDEV_TX_OK;
  1368. }
  1369. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
  1370. {
  1371. /*
  1372. * This helper function exists to help dev_pick_tx get the correct
  1373. * destination queue. Using a helper function skips a call to
  1374. * skb_tx_hash and will put the skbs in the queue we expect on their
  1375. * way down to the team driver.
  1376. */
  1377. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1378. /*
  1379. * Save the original txq to restore before passing to the driver
  1380. */
  1381. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1382. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1383. do {
  1384. txq -= dev->real_num_tx_queues;
  1385. } while (txq >= dev->real_num_tx_queues);
  1386. }
  1387. return txq;
  1388. }
  1389. static void team_change_rx_flags(struct net_device *dev, int change)
  1390. {
  1391. struct team *team = netdev_priv(dev);
  1392. struct team_port *port;
  1393. int inc;
  1394. rcu_read_lock();
  1395. list_for_each_entry_rcu(port, &team->port_list, list) {
  1396. if (change & IFF_PROMISC) {
  1397. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1398. dev_set_promiscuity(port->dev, inc);
  1399. }
  1400. if (change & IFF_ALLMULTI) {
  1401. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1402. dev_set_allmulti(port->dev, inc);
  1403. }
  1404. }
  1405. rcu_read_unlock();
  1406. }
  1407. static void team_set_rx_mode(struct net_device *dev)
  1408. {
  1409. struct team *team = netdev_priv(dev);
  1410. struct team_port *port;
  1411. rcu_read_lock();
  1412. list_for_each_entry_rcu(port, &team->port_list, list) {
  1413. dev_uc_sync_multiple(port->dev, dev);
  1414. dev_mc_sync_multiple(port->dev, dev);
  1415. }
  1416. rcu_read_unlock();
  1417. }
  1418. static int team_set_mac_address(struct net_device *dev, void *p)
  1419. {
  1420. struct sockaddr *addr = p;
  1421. struct team *team = netdev_priv(dev);
  1422. struct team_port *port;
  1423. if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
  1424. return -EADDRNOTAVAIL;
  1425. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1426. rcu_read_lock();
  1427. list_for_each_entry_rcu(port, &team->port_list, list)
  1428. if (team->ops.port_change_dev_addr)
  1429. team->ops.port_change_dev_addr(team, port);
  1430. rcu_read_unlock();
  1431. return 0;
  1432. }
  1433. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1434. {
  1435. struct team *team = netdev_priv(dev);
  1436. struct team_port *port;
  1437. int err;
  1438. /*
  1439. * Alhough this is reader, it's guarded by team lock. It's not possible
  1440. * to traverse list in reverse under rcu_read_lock
  1441. */
  1442. mutex_lock(&team->lock);
  1443. list_for_each_entry(port, &team->port_list, list) {
  1444. err = dev_set_mtu(port->dev, new_mtu);
  1445. if (err) {
  1446. netdev_err(dev, "Device %s failed to change mtu",
  1447. port->dev->name);
  1448. goto unwind;
  1449. }
  1450. }
  1451. mutex_unlock(&team->lock);
  1452. dev->mtu = new_mtu;
  1453. return 0;
  1454. unwind:
  1455. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1456. dev_set_mtu(port->dev, dev->mtu);
  1457. mutex_unlock(&team->lock);
  1458. return err;
  1459. }
  1460. static struct rtnl_link_stats64 *
  1461. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1462. {
  1463. struct team *team = netdev_priv(dev);
  1464. struct team_pcpu_stats *p;
  1465. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1466. u32 rx_dropped = 0, tx_dropped = 0;
  1467. unsigned int start;
  1468. int i;
  1469. for_each_possible_cpu(i) {
  1470. p = per_cpu_ptr(team->pcpu_stats, i);
  1471. do {
  1472. start = u64_stats_fetch_begin_bh(&p->syncp);
  1473. rx_packets = p->rx_packets;
  1474. rx_bytes = p->rx_bytes;
  1475. rx_multicast = p->rx_multicast;
  1476. tx_packets = p->tx_packets;
  1477. tx_bytes = p->tx_bytes;
  1478. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  1479. stats->rx_packets += rx_packets;
  1480. stats->rx_bytes += rx_bytes;
  1481. stats->multicast += rx_multicast;
  1482. stats->tx_packets += tx_packets;
  1483. stats->tx_bytes += tx_bytes;
  1484. /*
  1485. * rx_dropped & tx_dropped are u32, updated
  1486. * without syncp protection.
  1487. */
  1488. rx_dropped += p->rx_dropped;
  1489. tx_dropped += p->tx_dropped;
  1490. }
  1491. stats->rx_dropped = rx_dropped;
  1492. stats->tx_dropped = tx_dropped;
  1493. return stats;
  1494. }
  1495. static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  1496. {
  1497. struct team *team = netdev_priv(dev);
  1498. struct team_port *port;
  1499. int err;
  1500. /*
  1501. * Alhough this is reader, it's guarded by team lock. It's not possible
  1502. * to traverse list in reverse under rcu_read_lock
  1503. */
  1504. mutex_lock(&team->lock);
  1505. list_for_each_entry(port, &team->port_list, list) {
  1506. err = vlan_vid_add(port->dev, proto, vid);
  1507. if (err)
  1508. goto unwind;
  1509. }
  1510. mutex_unlock(&team->lock);
  1511. return 0;
  1512. unwind:
  1513. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1514. vlan_vid_del(port->dev, proto, vid);
  1515. mutex_unlock(&team->lock);
  1516. return err;
  1517. }
  1518. static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
  1519. {
  1520. struct team *team = netdev_priv(dev);
  1521. struct team_port *port;
  1522. rcu_read_lock();
  1523. list_for_each_entry_rcu(port, &team->port_list, list)
  1524. vlan_vid_del(port->dev, proto, vid);
  1525. rcu_read_unlock();
  1526. return 0;
  1527. }
  1528. #ifdef CONFIG_NET_POLL_CONTROLLER
  1529. static void team_poll_controller(struct net_device *dev)
  1530. {
  1531. }
  1532. static void __team_netpoll_cleanup(struct team *team)
  1533. {
  1534. struct team_port *port;
  1535. list_for_each_entry(port, &team->port_list, list)
  1536. team_port_disable_netpoll(port);
  1537. }
  1538. static void team_netpoll_cleanup(struct net_device *dev)
  1539. {
  1540. struct team *team = netdev_priv(dev);
  1541. mutex_lock(&team->lock);
  1542. __team_netpoll_cleanup(team);
  1543. mutex_unlock(&team->lock);
  1544. }
  1545. static int team_netpoll_setup(struct net_device *dev,
  1546. struct netpoll_info *npifo, gfp_t gfp)
  1547. {
  1548. struct team *team = netdev_priv(dev);
  1549. struct team_port *port;
  1550. int err = 0;
  1551. mutex_lock(&team->lock);
  1552. list_for_each_entry(port, &team->port_list, list) {
  1553. err = team_port_enable_netpoll(team, port, gfp);
  1554. if (err) {
  1555. __team_netpoll_cleanup(team);
  1556. break;
  1557. }
  1558. }
  1559. mutex_unlock(&team->lock);
  1560. return err;
  1561. }
  1562. #endif
  1563. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1564. {
  1565. struct team *team = netdev_priv(dev);
  1566. int err;
  1567. mutex_lock(&team->lock);
  1568. err = team_port_add(team, port_dev);
  1569. mutex_unlock(&team->lock);
  1570. return err;
  1571. }
  1572. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1573. {
  1574. struct team *team = netdev_priv(dev);
  1575. int err;
  1576. mutex_lock(&team->lock);
  1577. err = team_port_del(team, port_dev);
  1578. mutex_unlock(&team->lock);
  1579. return err;
  1580. }
  1581. static netdev_features_t team_fix_features(struct net_device *dev,
  1582. netdev_features_t features)
  1583. {
  1584. struct team_port *port;
  1585. struct team *team = netdev_priv(dev);
  1586. netdev_features_t mask;
  1587. mask = features;
  1588. features &= ~NETIF_F_ONE_FOR_ALL;
  1589. features |= NETIF_F_ALL_FOR_ALL;
  1590. rcu_read_lock();
  1591. list_for_each_entry_rcu(port, &team->port_list, list) {
  1592. features = netdev_increment_features(features,
  1593. port->dev->features,
  1594. mask);
  1595. }
  1596. rcu_read_unlock();
  1597. return features;
  1598. }
  1599. static int team_change_carrier(struct net_device *dev, bool new_carrier)
  1600. {
  1601. struct team *team = netdev_priv(dev);
  1602. team->user_carrier_enabled = true;
  1603. if (new_carrier)
  1604. netif_carrier_on(dev);
  1605. else
  1606. netif_carrier_off(dev);
  1607. return 0;
  1608. }
  1609. static const struct net_device_ops team_netdev_ops = {
  1610. .ndo_init = team_init,
  1611. .ndo_uninit = team_uninit,
  1612. .ndo_open = team_open,
  1613. .ndo_stop = team_close,
  1614. .ndo_start_xmit = team_xmit,
  1615. .ndo_select_queue = team_select_queue,
  1616. .ndo_change_rx_flags = team_change_rx_flags,
  1617. .ndo_set_rx_mode = team_set_rx_mode,
  1618. .ndo_set_mac_address = team_set_mac_address,
  1619. .ndo_change_mtu = team_change_mtu,
  1620. .ndo_get_stats64 = team_get_stats64,
  1621. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1622. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1623. #ifdef CONFIG_NET_POLL_CONTROLLER
  1624. .ndo_poll_controller = team_poll_controller,
  1625. .ndo_netpoll_setup = team_netpoll_setup,
  1626. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1627. #endif
  1628. .ndo_add_slave = team_add_slave,
  1629. .ndo_del_slave = team_del_slave,
  1630. .ndo_fix_features = team_fix_features,
  1631. .ndo_change_carrier = team_change_carrier,
  1632. };
  1633. /***********************
  1634. * ethtool interface
  1635. ***********************/
  1636. static void team_ethtool_get_drvinfo(struct net_device *dev,
  1637. struct ethtool_drvinfo *drvinfo)
  1638. {
  1639. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  1640. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1641. }
  1642. static const struct ethtool_ops team_ethtool_ops = {
  1643. .get_drvinfo = team_ethtool_get_drvinfo,
  1644. .get_link = ethtool_op_get_link,
  1645. };
  1646. /***********************
  1647. * rt netlink interface
  1648. ***********************/
  1649. static void team_setup_by_port(struct net_device *dev,
  1650. struct net_device *port_dev)
  1651. {
  1652. dev->header_ops = port_dev->header_ops;
  1653. dev->type = port_dev->type;
  1654. dev->hard_header_len = port_dev->hard_header_len;
  1655. dev->addr_len = port_dev->addr_len;
  1656. dev->mtu = port_dev->mtu;
  1657. memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
  1658. eth_hw_addr_inherit(dev, port_dev);
  1659. }
  1660. static int team_dev_type_check_change(struct net_device *dev,
  1661. struct net_device *port_dev)
  1662. {
  1663. struct team *team = netdev_priv(dev);
  1664. char *portname = port_dev->name;
  1665. int err;
  1666. if (dev->type == port_dev->type)
  1667. return 0;
  1668. if (!list_empty(&team->port_list)) {
  1669. netdev_err(dev, "Device %s is of different type\n", portname);
  1670. return -EBUSY;
  1671. }
  1672. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  1673. err = notifier_to_errno(err);
  1674. if (err) {
  1675. netdev_err(dev, "Refused to change device type\n");
  1676. return err;
  1677. }
  1678. dev_uc_flush(dev);
  1679. dev_mc_flush(dev);
  1680. team_setup_by_port(dev, port_dev);
  1681. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  1682. return 0;
  1683. }
  1684. static void team_setup(struct net_device *dev)
  1685. {
  1686. ether_setup(dev);
  1687. dev->netdev_ops = &team_netdev_ops;
  1688. dev->ethtool_ops = &team_ethtool_ops;
  1689. dev->destructor = team_destructor;
  1690. dev->tx_queue_len = 0;
  1691. dev->flags |= IFF_MULTICAST;
  1692. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1693. /*
  1694. * Indicate we support unicast address filtering. That way core won't
  1695. * bring us to promisc mode in case a unicast addr is added.
  1696. * Let this up to underlay drivers.
  1697. */
  1698. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1699. dev->features |= NETIF_F_LLTX;
  1700. dev->features |= NETIF_F_GRO;
  1701. dev->hw_features = TEAM_VLAN_FEATURES |
  1702. NETIF_F_HW_VLAN_CTAG_TX |
  1703. NETIF_F_HW_VLAN_CTAG_RX |
  1704. NETIF_F_HW_VLAN_CTAG_FILTER;
  1705. dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
  1706. dev->features |= dev->hw_features;
  1707. }
  1708. static int team_newlink(struct net *src_net, struct net_device *dev,
  1709. struct nlattr *tb[], struct nlattr *data[])
  1710. {
  1711. int err;
  1712. if (tb[IFLA_ADDRESS] == NULL)
  1713. eth_hw_addr_random(dev);
  1714. err = register_netdevice(dev);
  1715. if (err)
  1716. return err;
  1717. return 0;
  1718. }
  1719. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1720. {
  1721. if (tb[IFLA_ADDRESS]) {
  1722. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1723. return -EINVAL;
  1724. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1725. return -EADDRNOTAVAIL;
  1726. }
  1727. return 0;
  1728. }
  1729. static unsigned int team_get_num_tx_queues(void)
  1730. {
  1731. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1732. }
  1733. static unsigned int team_get_num_rx_queues(void)
  1734. {
  1735. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1736. }
  1737. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1738. .kind = DRV_NAME,
  1739. .priv_size = sizeof(struct team),
  1740. .setup = team_setup,
  1741. .newlink = team_newlink,
  1742. .validate = team_validate,
  1743. .get_num_tx_queues = team_get_num_tx_queues,
  1744. .get_num_rx_queues = team_get_num_rx_queues,
  1745. };
  1746. /***********************************
  1747. * Generic netlink custom interface
  1748. ***********************************/
  1749. static struct genl_family team_nl_family = {
  1750. .id = GENL_ID_GENERATE,
  1751. .name = TEAM_GENL_NAME,
  1752. .version = TEAM_GENL_VERSION,
  1753. .maxattr = TEAM_ATTR_MAX,
  1754. .netnsok = true,
  1755. };
  1756. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1757. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1758. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1759. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1760. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1761. };
  1762. static const struct nla_policy
  1763. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1764. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1765. [TEAM_ATTR_OPTION_NAME] = {
  1766. .type = NLA_STRING,
  1767. .len = TEAM_STRING_MAX_LEN,
  1768. },
  1769. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1770. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1771. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1772. };
  1773. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1774. {
  1775. struct sk_buff *msg;
  1776. void *hdr;
  1777. int err;
  1778. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1779. if (!msg)
  1780. return -ENOMEM;
  1781. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1782. &team_nl_family, 0, TEAM_CMD_NOOP);
  1783. if (!hdr) {
  1784. err = -EMSGSIZE;
  1785. goto err_msg_put;
  1786. }
  1787. genlmsg_end(msg, hdr);
  1788. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  1789. err_msg_put:
  1790. nlmsg_free(msg);
  1791. return err;
  1792. }
  1793. /*
  1794. * Netlink cmd functions should be locked by following two functions.
  1795. * Since dev gets held here, that ensures dev won't disappear in between.
  1796. */
  1797. static struct team *team_nl_team_get(struct genl_info *info)
  1798. {
  1799. struct net *net = genl_info_net(info);
  1800. int ifindex;
  1801. struct net_device *dev;
  1802. struct team *team;
  1803. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1804. return NULL;
  1805. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1806. dev = dev_get_by_index(net, ifindex);
  1807. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1808. if (dev)
  1809. dev_put(dev);
  1810. return NULL;
  1811. }
  1812. team = netdev_priv(dev);
  1813. mutex_lock(&team->lock);
  1814. return team;
  1815. }
  1816. static void team_nl_team_put(struct team *team)
  1817. {
  1818. mutex_unlock(&team->lock);
  1819. dev_put(team->dev);
  1820. }
  1821. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1822. struct team *team, u32 portid);
  1823. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
  1824. {
  1825. return genlmsg_unicast(dev_net(team->dev), skb, portid);
  1826. }
  1827. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1828. struct team_option_inst *opt_inst)
  1829. {
  1830. struct nlattr *option_item;
  1831. struct team_option *option = opt_inst->option;
  1832. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1833. struct team_gsetter_ctx ctx;
  1834. int err;
  1835. ctx.info = opt_inst_info;
  1836. err = team_option_get(team, opt_inst, &ctx);
  1837. if (err)
  1838. return err;
  1839. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1840. if (!option_item)
  1841. return -EMSGSIZE;
  1842. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1843. goto nest_cancel;
  1844. if (opt_inst_info->port &&
  1845. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1846. opt_inst_info->port->dev->ifindex))
  1847. goto nest_cancel;
  1848. if (opt_inst->option->array_size &&
  1849. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1850. opt_inst_info->array_index))
  1851. goto nest_cancel;
  1852. switch (option->type) {
  1853. case TEAM_OPTION_TYPE_U32:
  1854. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1855. goto nest_cancel;
  1856. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1857. goto nest_cancel;
  1858. break;
  1859. case TEAM_OPTION_TYPE_STRING:
  1860. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1861. goto nest_cancel;
  1862. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1863. ctx.data.str_val))
  1864. goto nest_cancel;
  1865. break;
  1866. case TEAM_OPTION_TYPE_BINARY:
  1867. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1868. goto nest_cancel;
  1869. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1870. ctx.data.bin_val.ptr))
  1871. goto nest_cancel;
  1872. break;
  1873. case TEAM_OPTION_TYPE_BOOL:
  1874. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1875. goto nest_cancel;
  1876. if (ctx.data.bool_val &&
  1877. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1878. goto nest_cancel;
  1879. break;
  1880. case TEAM_OPTION_TYPE_S32:
  1881. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1882. goto nest_cancel;
  1883. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1884. goto nest_cancel;
  1885. break;
  1886. default:
  1887. BUG();
  1888. }
  1889. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1890. goto nest_cancel;
  1891. if (opt_inst->changed) {
  1892. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1893. goto nest_cancel;
  1894. opt_inst->changed = false;
  1895. }
  1896. nla_nest_end(skb, option_item);
  1897. return 0;
  1898. nest_cancel:
  1899. nla_nest_cancel(skb, option_item);
  1900. return -EMSGSIZE;
  1901. }
  1902. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1903. struct team *team, u32 portid,
  1904. team_nl_send_func_t *send_func)
  1905. {
  1906. int err;
  1907. if (*pskb) {
  1908. err = send_func(*pskb, team, portid);
  1909. if (err)
  1910. return err;
  1911. }
  1912. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1913. if (!*pskb)
  1914. return -ENOMEM;
  1915. return 0;
  1916. }
  1917. static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
  1918. int flags, team_nl_send_func_t *send_func,
  1919. struct list_head *sel_opt_inst_list)
  1920. {
  1921. struct nlattr *option_list;
  1922. struct nlmsghdr *nlh;
  1923. void *hdr;
  1924. struct team_option_inst *opt_inst;
  1925. int err;
  1926. struct sk_buff *skb = NULL;
  1927. bool incomplete;
  1928. int i;
  1929. opt_inst = list_first_entry(sel_opt_inst_list,
  1930. struct team_option_inst, tmp_list);
  1931. start_again:
  1932. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1933. if (err)
  1934. return err;
  1935. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  1936. TEAM_CMD_OPTIONS_GET);
  1937. if (!hdr)
  1938. return -EMSGSIZE;
  1939. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1940. goto nla_put_failure;
  1941. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1942. if (!option_list)
  1943. goto nla_put_failure;
  1944. i = 0;
  1945. incomplete = false;
  1946. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  1947. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  1948. if (err) {
  1949. if (err == -EMSGSIZE) {
  1950. if (!i)
  1951. goto errout;
  1952. incomplete = true;
  1953. break;
  1954. }
  1955. goto errout;
  1956. }
  1957. i++;
  1958. }
  1959. nla_nest_end(skb, option_list);
  1960. genlmsg_end(skb, hdr);
  1961. if (incomplete)
  1962. goto start_again;
  1963. send_done:
  1964. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1965. if (!nlh) {
  1966. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1967. if (err)
  1968. goto errout;
  1969. goto send_done;
  1970. }
  1971. return send_func(skb, team, portid);
  1972. nla_put_failure:
  1973. err = -EMSGSIZE;
  1974. errout:
  1975. genlmsg_cancel(skb, hdr);
  1976. nlmsg_free(skb);
  1977. return err;
  1978. }
  1979. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1980. {
  1981. struct team *team;
  1982. struct team_option_inst *opt_inst;
  1983. int err;
  1984. LIST_HEAD(sel_opt_inst_list);
  1985. team = team_nl_team_get(info);
  1986. if (!team)
  1987. return -EINVAL;
  1988. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  1989. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  1990. err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
  1991. NLM_F_ACK, team_nl_send_unicast,
  1992. &sel_opt_inst_list);
  1993. team_nl_team_put(team);
  1994. return err;
  1995. }
  1996. static int team_nl_send_event_options_get(struct team *team,
  1997. struct list_head *sel_opt_inst_list);
  1998. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1999. {
  2000. struct team *team;
  2001. int err = 0;
  2002. int i;
  2003. struct nlattr *nl_option;
  2004. LIST_HEAD(opt_inst_list);
  2005. team = team_nl_team_get(info);
  2006. if (!team)
  2007. return -EINVAL;
  2008. err = -EINVAL;
  2009. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  2010. err = -EINVAL;
  2011. goto team_put;
  2012. }
  2013. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  2014. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  2015. struct nlattr *attr;
  2016. struct nlattr *attr_data;
  2017. enum team_option_type opt_type;
  2018. int opt_port_ifindex = 0; /* != 0 for per-port options */
  2019. u32 opt_array_index = 0;
  2020. bool opt_is_array = false;
  2021. struct team_option_inst *opt_inst;
  2022. char *opt_name;
  2023. bool opt_found = false;
  2024. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  2025. err = -EINVAL;
  2026. goto team_put;
  2027. }
  2028. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  2029. nl_option, team_nl_option_policy);
  2030. if (err)
  2031. goto team_put;
  2032. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  2033. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  2034. err = -EINVAL;
  2035. goto team_put;
  2036. }
  2037. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  2038. case NLA_U32:
  2039. opt_type = TEAM_OPTION_TYPE_U32;
  2040. break;
  2041. case NLA_STRING:
  2042. opt_type = TEAM_OPTION_TYPE_STRING;
  2043. break;
  2044. case NLA_BINARY:
  2045. opt_type = TEAM_OPTION_TYPE_BINARY;
  2046. break;
  2047. case NLA_FLAG:
  2048. opt_type = TEAM_OPTION_TYPE_BOOL;
  2049. break;
  2050. case NLA_S32:
  2051. opt_type = TEAM_OPTION_TYPE_S32;
  2052. break;
  2053. default:
  2054. goto team_put;
  2055. }
  2056. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  2057. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  2058. err = -EINVAL;
  2059. goto team_put;
  2060. }
  2061. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  2062. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  2063. if (attr)
  2064. opt_port_ifindex = nla_get_u32(attr);
  2065. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  2066. if (attr) {
  2067. opt_is_array = true;
  2068. opt_array_index = nla_get_u32(attr);
  2069. }
  2070. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2071. struct team_option *option = opt_inst->option;
  2072. struct team_gsetter_ctx ctx;
  2073. struct team_option_inst_info *opt_inst_info;
  2074. int tmp_ifindex;
  2075. opt_inst_info = &opt_inst->info;
  2076. tmp_ifindex = opt_inst_info->port ?
  2077. opt_inst_info->port->dev->ifindex : 0;
  2078. if (option->type != opt_type ||
  2079. strcmp(option->name, opt_name) ||
  2080. tmp_ifindex != opt_port_ifindex ||
  2081. (option->array_size && !opt_is_array) ||
  2082. opt_inst_info->array_index != opt_array_index)
  2083. continue;
  2084. opt_found = true;
  2085. ctx.info = opt_inst_info;
  2086. switch (opt_type) {
  2087. case TEAM_OPTION_TYPE_U32:
  2088. ctx.data.u32_val = nla_get_u32(attr_data);
  2089. break;
  2090. case TEAM_OPTION_TYPE_STRING:
  2091. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  2092. err = -EINVAL;
  2093. goto team_put;
  2094. }
  2095. ctx.data.str_val = nla_data(attr_data);
  2096. break;
  2097. case TEAM_OPTION_TYPE_BINARY:
  2098. ctx.data.bin_val.len = nla_len(attr_data);
  2099. ctx.data.bin_val.ptr = nla_data(attr_data);
  2100. break;
  2101. case TEAM_OPTION_TYPE_BOOL:
  2102. ctx.data.bool_val = attr_data ? true : false;
  2103. break;
  2104. case TEAM_OPTION_TYPE_S32:
  2105. ctx.data.s32_val = nla_get_s32(attr_data);
  2106. break;
  2107. default:
  2108. BUG();
  2109. }
  2110. err = team_option_set(team, opt_inst, &ctx);
  2111. if (err)
  2112. goto team_put;
  2113. opt_inst->changed = true;
  2114. list_add(&opt_inst->tmp_list, &opt_inst_list);
  2115. }
  2116. if (!opt_found) {
  2117. err = -ENOENT;
  2118. goto team_put;
  2119. }
  2120. }
  2121. err = team_nl_send_event_options_get(team, &opt_inst_list);
  2122. team_put:
  2123. team_nl_team_put(team);
  2124. return err;
  2125. }
  2126. static int team_nl_fill_one_port_get(struct sk_buff *skb,
  2127. struct team_port *port)
  2128. {
  2129. struct nlattr *port_item;
  2130. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  2131. if (!port_item)
  2132. goto nest_cancel;
  2133. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  2134. goto nest_cancel;
  2135. if (port->changed) {
  2136. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  2137. goto nest_cancel;
  2138. port->changed = false;
  2139. }
  2140. if ((port->removed &&
  2141. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  2142. (port->state.linkup &&
  2143. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  2144. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  2145. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  2146. goto nest_cancel;
  2147. nla_nest_end(skb, port_item);
  2148. return 0;
  2149. nest_cancel:
  2150. nla_nest_cancel(skb, port_item);
  2151. return -EMSGSIZE;
  2152. }
  2153. static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
  2154. int flags, team_nl_send_func_t *send_func,
  2155. struct team_port *one_port)
  2156. {
  2157. struct nlattr *port_list;
  2158. struct nlmsghdr *nlh;
  2159. void *hdr;
  2160. struct team_port *port;
  2161. int err;
  2162. struct sk_buff *skb = NULL;
  2163. bool incomplete;
  2164. int i;
  2165. port = list_first_entry_or_null(&team->port_list,
  2166. struct team_port, list);
  2167. start_again:
  2168. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2169. if (err)
  2170. return err;
  2171. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2172. TEAM_CMD_PORT_LIST_GET);
  2173. if (!hdr)
  2174. return -EMSGSIZE;
  2175. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2176. goto nla_put_failure;
  2177. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  2178. if (!port_list)
  2179. goto nla_put_failure;
  2180. i = 0;
  2181. incomplete = false;
  2182. /* If one port is selected, called wants to send port list containing
  2183. * only this port. Otherwise go through all listed ports and send all
  2184. */
  2185. if (one_port) {
  2186. err = team_nl_fill_one_port_get(skb, one_port);
  2187. if (err)
  2188. goto errout;
  2189. } else if (port) {
  2190. list_for_each_entry_from(port, &team->port_list, list) {
  2191. err = team_nl_fill_one_port_get(skb, port);
  2192. if (err) {
  2193. if (err == -EMSGSIZE) {
  2194. if (!i)
  2195. goto errout;
  2196. incomplete = true;
  2197. break;
  2198. }
  2199. goto errout;
  2200. }
  2201. i++;
  2202. }
  2203. }
  2204. nla_nest_end(skb, port_list);
  2205. genlmsg_end(skb, hdr);
  2206. if (incomplete)
  2207. goto start_again;
  2208. send_done:
  2209. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2210. if (!nlh) {
  2211. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2212. if (err)
  2213. goto errout;
  2214. goto send_done;
  2215. }
  2216. return send_func(skb, team, portid);
  2217. nla_put_failure:
  2218. err = -EMSGSIZE;
  2219. errout:
  2220. genlmsg_cancel(skb, hdr);
  2221. nlmsg_free(skb);
  2222. return err;
  2223. }
  2224. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  2225. struct genl_info *info)
  2226. {
  2227. struct team *team;
  2228. int err;
  2229. team = team_nl_team_get(info);
  2230. if (!team)
  2231. return -EINVAL;
  2232. err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
  2233. NLM_F_ACK, team_nl_send_unicast, NULL);
  2234. team_nl_team_put(team);
  2235. return err;
  2236. }
  2237. static struct genl_ops team_nl_ops[] = {
  2238. {
  2239. .cmd = TEAM_CMD_NOOP,
  2240. .doit = team_nl_cmd_noop,
  2241. .policy = team_nl_policy,
  2242. },
  2243. {
  2244. .cmd = TEAM_CMD_OPTIONS_SET,
  2245. .doit = team_nl_cmd_options_set,
  2246. .policy = team_nl_policy,
  2247. .flags = GENL_ADMIN_PERM,
  2248. },
  2249. {
  2250. .cmd = TEAM_CMD_OPTIONS_GET,
  2251. .doit = team_nl_cmd_options_get,
  2252. .policy = team_nl_policy,
  2253. .flags = GENL_ADMIN_PERM,
  2254. },
  2255. {
  2256. .cmd = TEAM_CMD_PORT_LIST_GET,
  2257. .doit = team_nl_cmd_port_list_get,
  2258. .policy = team_nl_policy,
  2259. .flags = GENL_ADMIN_PERM,
  2260. },
  2261. };
  2262. static struct genl_multicast_group team_change_event_mcgrp = {
  2263. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  2264. };
  2265. static int team_nl_send_multicast(struct sk_buff *skb,
  2266. struct team *team, u32 portid)
  2267. {
  2268. return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
  2269. team_change_event_mcgrp.id, GFP_KERNEL);
  2270. }
  2271. static int team_nl_send_event_options_get(struct team *team,
  2272. struct list_head *sel_opt_inst_list)
  2273. {
  2274. return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
  2275. sel_opt_inst_list);
  2276. }
  2277. static int team_nl_send_event_port_get(struct team *team,
  2278. struct team_port *port)
  2279. {
  2280. return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
  2281. port);
  2282. }
  2283. static int team_nl_init(void)
  2284. {
  2285. int err;
  2286. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  2287. ARRAY_SIZE(team_nl_ops));
  2288. if (err)
  2289. return err;
  2290. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  2291. if (err)
  2292. goto err_change_event_grp_reg;
  2293. return 0;
  2294. err_change_event_grp_reg:
  2295. genl_unregister_family(&team_nl_family);
  2296. return err;
  2297. }
  2298. static void team_nl_fini(void)
  2299. {
  2300. genl_unregister_family(&team_nl_family);
  2301. }
  2302. /******************
  2303. * Change checkers
  2304. ******************/
  2305. static void __team_options_change_check(struct team *team)
  2306. {
  2307. int err;
  2308. struct team_option_inst *opt_inst;
  2309. LIST_HEAD(sel_opt_inst_list);
  2310. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2311. if (opt_inst->changed)
  2312. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2313. }
  2314. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2315. if (err && err != -ESRCH)
  2316. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2317. err);
  2318. }
  2319. /* rtnl lock is held */
  2320. static void __team_port_change_send(struct team_port *port, bool linkup)
  2321. {
  2322. int err;
  2323. port->changed = true;
  2324. port->state.linkup = linkup;
  2325. team_refresh_port_linkup(port);
  2326. if (linkup) {
  2327. struct ethtool_cmd ecmd;
  2328. err = __ethtool_get_settings(port->dev, &ecmd);
  2329. if (!err) {
  2330. port->state.speed = ethtool_cmd_speed(&ecmd);
  2331. port->state.duplex = ecmd.duplex;
  2332. goto send_event;
  2333. }
  2334. }
  2335. port->state.speed = 0;
  2336. port->state.duplex = 0;
  2337. send_event:
  2338. err = team_nl_send_event_port_get(port->team, port);
  2339. if (err && err != -ESRCH)
  2340. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
  2341. port->dev->name, err);
  2342. }
  2343. static void __team_carrier_check(struct team *team)
  2344. {
  2345. struct team_port *port;
  2346. bool team_linkup;
  2347. if (team->user_carrier_enabled)
  2348. return;
  2349. team_linkup = false;
  2350. list_for_each_entry(port, &team->port_list, list) {
  2351. if (port->linkup) {
  2352. team_linkup = true;
  2353. break;
  2354. }
  2355. }
  2356. if (team_linkup)
  2357. netif_carrier_on(team->dev);
  2358. else
  2359. netif_carrier_off(team->dev);
  2360. }
  2361. static void __team_port_change_check(struct team_port *port, bool linkup)
  2362. {
  2363. if (port->state.linkup != linkup)
  2364. __team_port_change_send(port, linkup);
  2365. __team_carrier_check(port->team);
  2366. }
  2367. static void __team_port_change_port_added(struct team_port *port, bool linkup)
  2368. {
  2369. __team_port_change_send(port, linkup);
  2370. __team_carrier_check(port->team);
  2371. }
  2372. static void __team_port_change_port_removed(struct team_port *port)
  2373. {
  2374. port->removed = true;
  2375. __team_port_change_send(port, false);
  2376. __team_carrier_check(port->team);
  2377. }
  2378. static void team_port_change_check(struct team_port *port, bool linkup)
  2379. {
  2380. struct team *team = port->team;
  2381. mutex_lock(&team->lock);
  2382. __team_port_change_check(port, linkup);
  2383. mutex_unlock(&team->lock);
  2384. }
  2385. /************************************
  2386. * Net device notifier event handler
  2387. ************************************/
  2388. static int team_device_event(struct notifier_block *unused,
  2389. unsigned long event, void *ptr)
  2390. {
  2391. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2392. struct team_port *port;
  2393. port = team_port_get_rtnl(dev);
  2394. if (!port)
  2395. return NOTIFY_DONE;
  2396. switch (event) {
  2397. case NETDEV_UP:
  2398. if (netif_carrier_ok(dev))
  2399. team_port_change_check(port, true);
  2400. case NETDEV_DOWN:
  2401. team_port_change_check(port, false);
  2402. case NETDEV_CHANGE:
  2403. if (netif_running(port->dev))
  2404. team_port_change_check(port,
  2405. !!netif_carrier_ok(port->dev));
  2406. break;
  2407. case NETDEV_UNREGISTER:
  2408. team_del_slave(port->team->dev, dev);
  2409. break;
  2410. case NETDEV_FEAT_CHANGE:
  2411. team_compute_features(port->team);
  2412. break;
  2413. case NETDEV_CHANGEMTU:
  2414. /* Forbid to change mtu of underlaying device */
  2415. return NOTIFY_BAD;
  2416. case NETDEV_PRE_TYPE_CHANGE:
  2417. /* Forbid to change type of underlaying device */
  2418. return NOTIFY_BAD;
  2419. case NETDEV_RESEND_IGMP:
  2420. /* Propagate to master device */
  2421. call_netdevice_notifiers(event, port->team->dev);
  2422. break;
  2423. }
  2424. return NOTIFY_DONE;
  2425. }
  2426. static struct notifier_block team_notifier_block __read_mostly = {
  2427. .notifier_call = team_device_event,
  2428. };
  2429. /***********************
  2430. * Module init and exit
  2431. ***********************/
  2432. static int __init team_module_init(void)
  2433. {
  2434. int err;
  2435. register_netdevice_notifier(&team_notifier_block);
  2436. err = rtnl_link_register(&team_link_ops);
  2437. if (err)
  2438. goto err_rtnl_reg;
  2439. err = team_nl_init();
  2440. if (err)
  2441. goto err_nl_init;
  2442. return 0;
  2443. err_nl_init:
  2444. rtnl_link_unregister(&team_link_ops);
  2445. err_rtnl_reg:
  2446. unregister_netdevice_notifier(&team_notifier_block);
  2447. return err;
  2448. }
  2449. static void __exit team_module_exit(void)
  2450. {
  2451. team_nl_fini();
  2452. rtnl_link_unregister(&team_link_ops);
  2453. unregister_netdevice_notifier(&team_notifier_block);
  2454. }
  2455. module_init(team_module_init);
  2456. module_exit(team_module_exit);
  2457. MODULE_LICENSE("GPL v2");
  2458. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2459. MODULE_DESCRIPTION("Ethernet team device driver");
  2460. MODULE_ALIAS_RTNL_LINK(DRV_NAME);