team.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  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, int en_port_count)
  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 (!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 (!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. static void team_adjust_ops(struct team *team)
  460. {
  461. __team_adjust_ops(team, team->en_port_count);
  462. }
  463. /*
  464. * We can benefit from the fact that it's ensured no port is present
  465. * at the time of mode change. Therefore no packets are in fly so there's no
  466. * need to set mode operations in any special way.
  467. */
  468. static int __team_change_mode(struct team *team,
  469. const struct team_mode *new_mode)
  470. {
  471. /* Check if mode was previously set and do cleanup if so */
  472. if (team_is_mode_set(team)) {
  473. void (*exit_op)(struct team *team) = team->ops.exit;
  474. /* Clear ops area so no callback is called any longer */
  475. memset(&team->ops, 0, sizeof(struct team_mode_ops));
  476. team_adjust_ops(team);
  477. if (exit_op)
  478. exit_op(team);
  479. team_mode_put(team->mode);
  480. team_set_no_mode(team);
  481. /* zero private data area */
  482. memset(&team->mode_priv, 0,
  483. sizeof(struct team) - offsetof(struct team, mode_priv));
  484. }
  485. if (!new_mode)
  486. return 0;
  487. if (new_mode->ops->init) {
  488. int err;
  489. err = new_mode->ops->init(team);
  490. if (err)
  491. return err;
  492. }
  493. team->mode = new_mode;
  494. memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
  495. team_adjust_ops(team);
  496. return 0;
  497. }
  498. static int team_change_mode(struct team *team, const char *kind)
  499. {
  500. const struct team_mode *new_mode;
  501. struct net_device *dev = team->dev;
  502. int err;
  503. if (!list_empty(&team->port_list)) {
  504. netdev_err(dev, "No ports can be present during mode change\n");
  505. return -EBUSY;
  506. }
  507. if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
  508. netdev_err(dev, "Unable to change to the same mode the team is in\n");
  509. return -EINVAL;
  510. }
  511. new_mode = team_mode_get(kind);
  512. if (!new_mode) {
  513. netdev_err(dev, "Mode \"%s\" not found\n", kind);
  514. return -EINVAL;
  515. }
  516. err = __team_change_mode(team, new_mode);
  517. if (err) {
  518. netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
  519. team_mode_put(new_mode);
  520. return err;
  521. }
  522. netdev_info(dev, "Mode changed to \"%s\"\n", kind);
  523. return 0;
  524. }
  525. /************************
  526. * Rx path frame handler
  527. ************************/
  528. /* note: already called with rcu_read_lock */
  529. static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
  530. {
  531. struct sk_buff *skb = *pskb;
  532. struct team_port *port;
  533. struct team *team;
  534. rx_handler_result_t res;
  535. skb = skb_share_check(skb, GFP_ATOMIC);
  536. if (!skb)
  537. return RX_HANDLER_CONSUMED;
  538. *pskb = skb;
  539. port = team_port_get_rcu(skb->dev);
  540. team = port->team;
  541. if (!team_port_enabled(port)) {
  542. /* allow exact match delivery for disabled ports */
  543. res = RX_HANDLER_EXACT;
  544. } else {
  545. res = team->ops.receive(team, port, skb);
  546. }
  547. if (res == RX_HANDLER_ANOTHER) {
  548. struct team_pcpu_stats *pcpu_stats;
  549. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  550. u64_stats_update_begin(&pcpu_stats->syncp);
  551. pcpu_stats->rx_packets++;
  552. pcpu_stats->rx_bytes += skb->len;
  553. if (skb->pkt_type == PACKET_MULTICAST)
  554. pcpu_stats->rx_multicast++;
  555. u64_stats_update_end(&pcpu_stats->syncp);
  556. skb->dev = team->dev;
  557. } else {
  558. this_cpu_inc(team->pcpu_stats->rx_dropped);
  559. }
  560. return res;
  561. }
  562. /*************************************
  563. * Multiqueue Tx port select override
  564. *************************************/
  565. static int team_queue_override_init(struct team *team)
  566. {
  567. struct list_head *listarr;
  568. unsigned int queue_cnt = team->dev->num_tx_queues - 1;
  569. unsigned int i;
  570. if (!queue_cnt)
  571. return 0;
  572. listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
  573. if (!listarr)
  574. return -ENOMEM;
  575. team->qom_lists = listarr;
  576. for (i = 0; i < queue_cnt; i++)
  577. INIT_LIST_HEAD(listarr++);
  578. return 0;
  579. }
  580. static void team_queue_override_fini(struct team *team)
  581. {
  582. kfree(team->qom_lists);
  583. }
  584. static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
  585. {
  586. return &team->qom_lists[queue_id - 1];
  587. }
  588. /*
  589. * note: already called with rcu_read_lock
  590. */
  591. static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
  592. {
  593. struct list_head *qom_list;
  594. struct team_port *port;
  595. if (!team->queue_override_enabled || !skb->queue_mapping)
  596. return false;
  597. qom_list = __team_get_qom_list(team, skb->queue_mapping);
  598. list_for_each_entry_rcu(port, qom_list, qom_list) {
  599. if (!team_dev_queue_xmit(team, port, skb))
  600. return true;
  601. }
  602. return false;
  603. }
  604. static void __team_queue_override_port_del(struct team *team,
  605. struct team_port *port)
  606. {
  607. if (!port->queue_id)
  608. return;
  609. list_del_rcu(&port->qom_list);
  610. }
  611. static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
  612. struct team_port *cur)
  613. {
  614. if (port->priority < cur->priority)
  615. return true;
  616. if (port->priority > cur->priority)
  617. return false;
  618. if (port->index < cur->index)
  619. return true;
  620. return false;
  621. }
  622. static void __team_queue_override_port_add(struct team *team,
  623. struct team_port *port)
  624. {
  625. struct team_port *cur;
  626. struct list_head *qom_list;
  627. struct list_head *node;
  628. if (!port->queue_id)
  629. return;
  630. qom_list = __team_get_qom_list(team, port->queue_id);
  631. node = qom_list;
  632. list_for_each_entry(cur, qom_list, qom_list) {
  633. if (team_queue_override_port_has_gt_prio_than(port, cur))
  634. break;
  635. node = &cur->qom_list;
  636. }
  637. list_add_tail_rcu(&port->qom_list, node);
  638. }
  639. static void __team_queue_override_enabled_check(struct team *team)
  640. {
  641. struct team_port *port;
  642. bool enabled = false;
  643. list_for_each_entry(port, &team->port_list, list) {
  644. if (port->queue_id) {
  645. enabled = true;
  646. break;
  647. }
  648. }
  649. if (enabled == team->queue_override_enabled)
  650. return;
  651. netdev_dbg(team->dev, "%s queue override\n",
  652. enabled ? "Enabling" : "Disabling");
  653. team->queue_override_enabled = enabled;
  654. }
  655. static void team_queue_override_port_prio_changed(struct team *team,
  656. struct team_port *port)
  657. {
  658. if (!port->queue_id || team_port_enabled(port))
  659. return;
  660. __team_queue_override_port_del(team, port);
  661. __team_queue_override_port_add(team, port);
  662. __team_queue_override_enabled_check(team);
  663. }
  664. static void team_queue_override_port_change_queue_id(struct team *team,
  665. struct team_port *port,
  666. u16 new_queue_id)
  667. {
  668. if (team_port_enabled(port)) {
  669. __team_queue_override_port_del(team, port);
  670. port->queue_id = new_queue_id;
  671. __team_queue_override_port_add(team, port);
  672. __team_queue_override_enabled_check(team);
  673. } else {
  674. port->queue_id = new_queue_id;
  675. }
  676. }
  677. static void team_queue_override_port_add(struct team *team,
  678. struct team_port *port)
  679. {
  680. __team_queue_override_port_add(team, port);
  681. __team_queue_override_enabled_check(team);
  682. }
  683. static void team_queue_override_port_del(struct team *team,
  684. struct team_port *port)
  685. {
  686. __team_queue_override_port_del(team, port);
  687. __team_queue_override_enabled_check(team);
  688. }
  689. /****************
  690. * Port handling
  691. ****************/
  692. static bool team_port_find(const struct team *team,
  693. const struct team_port *port)
  694. {
  695. struct team_port *cur;
  696. list_for_each_entry(cur, &team->port_list, list)
  697. if (cur == port)
  698. return true;
  699. return false;
  700. }
  701. /*
  702. * Enable/disable port by adding to enabled port hashlist and setting
  703. * port->index (Might be racy so reader could see incorrect ifindex when
  704. * processing a flying packet, but that is not a problem). Write guarded
  705. * by team->lock.
  706. */
  707. static void team_port_enable(struct team *team,
  708. struct team_port *port)
  709. {
  710. if (team_port_enabled(port))
  711. return;
  712. port->index = team->en_port_count++;
  713. hlist_add_head_rcu(&port->hlist,
  714. team_port_index_hash(team, port->index));
  715. team_adjust_ops(team);
  716. team_queue_override_port_add(team, port);
  717. if (team->ops.port_enabled)
  718. team->ops.port_enabled(team, port);
  719. }
  720. static void __reconstruct_port_hlist(struct team *team, int rm_index)
  721. {
  722. int i;
  723. struct team_port *port;
  724. for (i = rm_index + 1; i < team->en_port_count; i++) {
  725. port = team_get_port_by_index(team, i);
  726. hlist_del_rcu(&port->hlist);
  727. port->index--;
  728. hlist_add_head_rcu(&port->hlist,
  729. team_port_index_hash(team, port->index));
  730. }
  731. }
  732. static void team_port_disable(struct team *team,
  733. struct team_port *port)
  734. {
  735. if (!team_port_enabled(port))
  736. return;
  737. if (team->ops.port_disabled)
  738. team->ops.port_disabled(team, port);
  739. hlist_del_rcu(&port->hlist);
  740. __reconstruct_port_hlist(team, port->index);
  741. port->index = -1;
  742. team_queue_override_port_del(team, port);
  743. __team_adjust_ops(team, team->en_port_count - 1);
  744. /*
  745. * Wait until readers see adjusted ops. This ensures that
  746. * readers never see team->en_port_count == 0
  747. */
  748. synchronize_rcu();
  749. team->en_port_count--;
  750. }
  751. #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
  752. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  753. NETIF_F_HIGHDMA | NETIF_F_LRO)
  754. static void __team_compute_features(struct team *team)
  755. {
  756. struct team_port *port;
  757. u32 vlan_features = TEAM_VLAN_FEATURES;
  758. unsigned short max_hard_header_len = ETH_HLEN;
  759. unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
  760. list_for_each_entry(port, &team->port_list, list) {
  761. vlan_features = netdev_increment_features(vlan_features,
  762. port->dev->vlan_features,
  763. TEAM_VLAN_FEATURES);
  764. dst_release_flag &= port->dev->priv_flags;
  765. if (port->dev->hard_header_len > max_hard_header_len)
  766. max_hard_header_len = port->dev->hard_header_len;
  767. }
  768. team->dev->vlan_features = vlan_features;
  769. team->dev->hard_header_len = max_hard_header_len;
  770. flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
  771. team->dev->priv_flags = flags | dst_release_flag;
  772. netdev_change_features(team->dev);
  773. }
  774. static void team_compute_features(struct team *team)
  775. {
  776. mutex_lock(&team->lock);
  777. __team_compute_features(team);
  778. mutex_unlock(&team->lock);
  779. }
  780. static int team_port_enter(struct team *team, struct team_port *port)
  781. {
  782. int err = 0;
  783. dev_hold(team->dev);
  784. port->dev->priv_flags |= IFF_TEAM_PORT;
  785. if (team->ops.port_enter) {
  786. err = team->ops.port_enter(team, port);
  787. if (err) {
  788. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  789. port->dev->name);
  790. goto err_port_enter;
  791. }
  792. }
  793. return 0;
  794. err_port_enter:
  795. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  796. dev_put(team->dev);
  797. return err;
  798. }
  799. static void team_port_leave(struct team *team, struct team_port *port)
  800. {
  801. if (team->ops.port_leave)
  802. team->ops.port_leave(team, port);
  803. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  804. dev_put(team->dev);
  805. }
  806. #ifdef CONFIG_NET_POLL_CONTROLLER
  807. static int team_port_enable_netpoll(struct team *team, struct team_port *port,
  808. gfp_t gfp)
  809. {
  810. struct netpoll *np;
  811. int err;
  812. np = kzalloc(sizeof(*np), gfp);
  813. if (!np)
  814. return -ENOMEM;
  815. err = __netpoll_setup(np, port->dev, gfp);
  816. if (err) {
  817. kfree(np);
  818. return err;
  819. }
  820. port->np = np;
  821. return err;
  822. }
  823. static void team_port_disable_netpoll(struct team_port *port)
  824. {
  825. struct netpoll *np = port->np;
  826. if (!np)
  827. return;
  828. port->np = NULL;
  829. /* Wait for transmitting packets to finish before freeing. */
  830. synchronize_rcu_bh();
  831. __netpoll_cleanup(np);
  832. kfree(np);
  833. }
  834. static struct netpoll_info *team_netpoll_info(struct team *team)
  835. {
  836. return team->dev->npinfo;
  837. }
  838. #else
  839. static int team_port_enable_netpoll(struct team *team, struct team_port *port,
  840. gfp_t gfp)
  841. {
  842. return 0;
  843. }
  844. static void team_port_disable_netpoll(struct team_port *port)
  845. {
  846. }
  847. static struct netpoll_info *team_netpoll_info(struct team *team)
  848. {
  849. return NULL;
  850. }
  851. #endif
  852. static void __team_port_change_port_added(struct team_port *port, bool linkup);
  853. static int team_dev_type_check_change(struct net_device *dev,
  854. struct net_device *port_dev);
  855. static int team_port_add(struct team *team, struct net_device *port_dev)
  856. {
  857. struct net_device *dev = team->dev;
  858. struct team_port *port;
  859. char *portname = port_dev->name;
  860. int err;
  861. if (port_dev->flags & IFF_LOOPBACK) {
  862. netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
  863. portname);
  864. return -EINVAL;
  865. }
  866. if (team_port_exists(port_dev)) {
  867. netdev_err(dev, "Device %s is already a port "
  868. "of a team device\n", portname);
  869. return -EBUSY;
  870. }
  871. if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
  872. vlan_uses_dev(dev)) {
  873. netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
  874. portname);
  875. return -EPERM;
  876. }
  877. err = team_dev_type_check_change(dev, port_dev);
  878. if (err)
  879. return err;
  880. if (port_dev->flags & IFF_UP) {
  881. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  882. portname);
  883. return -EBUSY;
  884. }
  885. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  886. GFP_KERNEL);
  887. if (!port)
  888. return -ENOMEM;
  889. port->dev = port_dev;
  890. port->team = team;
  891. INIT_LIST_HEAD(&port->qom_list);
  892. port->orig.mtu = port_dev->mtu;
  893. err = dev_set_mtu(port_dev, dev->mtu);
  894. if (err) {
  895. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  896. goto err_set_mtu;
  897. }
  898. memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
  899. err = team_port_enter(team, port);
  900. if (err) {
  901. netdev_err(dev, "Device %s failed to enter team mode\n",
  902. portname);
  903. goto err_port_enter;
  904. }
  905. err = dev_open(port_dev);
  906. if (err) {
  907. netdev_dbg(dev, "Device %s opening failed\n",
  908. portname);
  909. goto err_dev_open;
  910. }
  911. err = vlan_vids_add_by_dev(port_dev, dev);
  912. if (err) {
  913. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  914. portname);
  915. goto err_vids_add;
  916. }
  917. if (team_netpoll_info(team)) {
  918. err = team_port_enable_netpoll(team, port, GFP_KERNEL);
  919. if (err) {
  920. netdev_err(dev, "Failed to enable netpoll on device %s\n",
  921. portname);
  922. goto err_enable_netpoll;
  923. }
  924. }
  925. err = netdev_master_upper_dev_link(port_dev, dev);
  926. if (err) {
  927. netdev_err(dev, "Device %s failed to set upper link\n",
  928. portname);
  929. goto err_set_upper_link;
  930. }
  931. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  932. port);
  933. if (err) {
  934. netdev_err(dev, "Device %s failed to register rx_handler\n",
  935. portname);
  936. goto err_handler_register;
  937. }
  938. err = __team_option_inst_add_port(team, port);
  939. if (err) {
  940. netdev_err(dev, "Device %s failed to add per-port options\n",
  941. portname);
  942. goto err_option_port_add;
  943. }
  944. port->index = -1;
  945. team_port_enable(team, port);
  946. list_add_tail_rcu(&port->list, &team->port_list);
  947. __team_compute_features(team);
  948. __team_port_change_port_added(port, !!netif_carrier_ok(port_dev));
  949. __team_options_change_check(team);
  950. netdev_info(dev, "Port device %s added\n", portname);
  951. return 0;
  952. err_option_port_add:
  953. netdev_rx_handler_unregister(port_dev);
  954. err_handler_register:
  955. netdev_upper_dev_unlink(port_dev, dev);
  956. err_set_upper_link:
  957. team_port_disable_netpoll(port);
  958. err_enable_netpoll:
  959. vlan_vids_del_by_dev(port_dev, dev);
  960. err_vids_add:
  961. dev_close(port_dev);
  962. err_dev_open:
  963. team_port_leave(team, port);
  964. team_port_set_orig_dev_addr(port);
  965. err_port_enter:
  966. dev_set_mtu(port_dev, port->orig.mtu);
  967. err_set_mtu:
  968. kfree(port);
  969. return err;
  970. }
  971. static void __team_port_change_port_removed(struct team_port *port);
  972. static int team_port_del(struct team *team, struct net_device *port_dev)
  973. {
  974. struct net_device *dev = team->dev;
  975. struct team_port *port;
  976. char *portname = port_dev->name;
  977. port = team_port_get_rtnl(port_dev);
  978. if (!port || !team_port_find(team, port)) {
  979. netdev_err(dev, "Device %s does not act as a port of this team\n",
  980. portname);
  981. return -ENOENT;
  982. }
  983. team_port_disable(team, port);
  984. list_del_rcu(&port->list);
  985. netdev_rx_handler_unregister(port_dev);
  986. netdev_upper_dev_unlink(port_dev, dev);
  987. team_port_disable_netpoll(port);
  988. vlan_vids_del_by_dev(port_dev, dev);
  989. dev_uc_unsync(port_dev, dev);
  990. dev_mc_unsync(port_dev, dev);
  991. dev_close(port_dev);
  992. team_port_leave(team, port);
  993. __team_option_inst_mark_removed_port(team, port);
  994. __team_options_change_check(team);
  995. __team_option_inst_del_port(team, port);
  996. __team_port_change_port_removed(port);
  997. team_port_set_orig_dev_addr(port);
  998. dev_set_mtu(port_dev, port->orig.mtu);
  999. synchronize_rcu();
  1000. kfree(port);
  1001. netdev_info(dev, "Port device %s removed\n", portname);
  1002. __team_compute_features(team);
  1003. return 0;
  1004. }
  1005. /*****************
  1006. * Net device ops
  1007. *****************/
  1008. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  1009. {
  1010. ctx->data.str_val = team->mode->kind;
  1011. return 0;
  1012. }
  1013. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  1014. {
  1015. return team_change_mode(team, ctx->data.str_val);
  1016. }
  1017. static int team_port_en_option_get(struct team *team,
  1018. struct team_gsetter_ctx *ctx)
  1019. {
  1020. struct team_port *port = ctx->info->port;
  1021. ctx->data.bool_val = team_port_enabled(port);
  1022. return 0;
  1023. }
  1024. static int team_port_en_option_set(struct team *team,
  1025. struct team_gsetter_ctx *ctx)
  1026. {
  1027. struct team_port *port = ctx->info->port;
  1028. if (ctx->data.bool_val)
  1029. team_port_enable(team, port);
  1030. else
  1031. team_port_disable(team, port);
  1032. return 0;
  1033. }
  1034. static int team_user_linkup_option_get(struct team *team,
  1035. struct team_gsetter_ctx *ctx)
  1036. {
  1037. struct team_port *port = ctx->info->port;
  1038. ctx->data.bool_val = port->user.linkup;
  1039. return 0;
  1040. }
  1041. static int team_user_linkup_option_set(struct team *team,
  1042. struct team_gsetter_ctx *ctx)
  1043. {
  1044. struct team_port *port = ctx->info->port;
  1045. port->user.linkup = ctx->data.bool_val;
  1046. team_refresh_port_linkup(port);
  1047. return 0;
  1048. }
  1049. static int team_user_linkup_en_option_get(struct team *team,
  1050. struct team_gsetter_ctx *ctx)
  1051. {
  1052. struct team_port *port = ctx->info->port;
  1053. ctx->data.bool_val = port->user.linkup_enabled;
  1054. return 0;
  1055. }
  1056. static int team_user_linkup_en_option_set(struct team *team,
  1057. struct team_gsetter_ctx *ctx)
  1058. {
  1059. struct team_port *port = ctx->info->port;
  1060. port->user.linkup_enabled = ctx->data.bool_val;
  1061. team_refresh_port_linkup(port);
  1062. return 0;
  1063. }
  1064. static int team_priority_option_get(struct team *team,
  1065. struct team_gsetter_ctx *ctx)
  1066. {
  1067. struct team_port *port = ctx->info->port;
  1068. ctx->data.s32_val = port->priority;
  1069. return 0;
  1070. }
  1071. static int team_priority_option_set(struct team *team,
  1072. struct team_gsetter_ctx *ctx)
  1073. {
  1074. struct team_port *port = ctx->info->port;
  1075. s32 priority = ctx->data.s32_val;
  1076. if (port->priority == priority)
  1077. return 0;
  1078. port->priority = priority;
  1079. team_queue_override_port_prio_changed(team, port);
  1080. return 0;
  1081. }
  1082. static int team_queue_id_option_get(struct team *team,
  1083. struct team_gsetter_ctx *ctx)
  1084. {
  1085. struct team_port *port = ctx->info->port;
  1086. ctx->data.u32_val = port->queue_id;
  1087. return 0;
  1088. }
  1089. static int team_queue_id_option_set(struct team *team,
  1090. struct team_gsetter_ctx *ctx)
  1091. {
  1092. struct team_port *port = ctx->info->port;
  1093. u16 new_queue_id = ctx->data.u32_val;
  1094. if (port->queue_id == new_queue_id)
  1095. return 0;
  1096. if (new_queue_id >= team->dev->real_num_tx_queues)
  1097. return -EINVAL;
  1098. team_queue_override_port_change_queue_id(team, port, new_queue_id);
  1099. return 0;
  1100. }
  1101. static const struct team_option team_options[] = {
  1102. {
  1103. .name = "mode",
  1104. .type = TEAM_OPTION_TYPE_STRING,
  1105. .getter = team_mode_option_get,
  1106. .setter = team_mode_option_set,
  1107. },
  1108. {
  1109. .name = "enabled",
  1110. .type = TEAM_OPTION_TYPE_BOOL,
  1111. .per_port = true,
  1112. .getter = team_port_en_option_get,
  1113. .setter = team_port_en_option_set,
  1114. },
  1115. {
  1116. .name = "user_linkup",
  1117. .type = TEAM_OPTION_TYPE_BOOL,
  1118. .per_port = true,
  1119. .getter = team_user_linkup_option_get,
  1120. .setter = team_user_linkup_option_set,
  1121. },
  1122. {
  1123. .name = "user_linkup_enabled",
  1124. .type = TEAM_OPTION_TYPE_BOOL,
  1125. .per_port = true,
  1126. .getter = team_user_linkup_en_option_get,
  1127. .setter = team_user_linkup_en_option_set,
  1128. },
  1129. {
  1130. .name = "priority",
  1131. .type = TEAM_OPTION_TYPE_S32,
  1132. .per_port = true,
  1133. .getter = team_priority_option_get,
  1134. .setter = team_priority_option_set,
  1135. },
  1136. {
  1137. .name = "queue_id",
  1138. .type = TEAM_OPTION_TYPE_U32,
  1139. .per_port = true,
  1140. .getter = team_queue_id_option_get,
  1141. .setter = team_queue_id_option_set,
  1142. },
  1143. };
  1144. static struct lock_class_key team_netdev_xmit_lock_key;
  1145. static struct lock_class_key team_netdev_addr_lock_key;
  1146. static struct lock_class_key team_tx_busylock_key;
  1147. static void team_set_lockdep_class_one(struct net_device *dev,
  1148. struct netdev_queue *txq,
  1149. void *unused)
  1150. {
  1151. lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
  1152. }
  1153. static void team_set_lockdep_class(struct net_device *dev)
  1154. {
  1155. lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
  1156. netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
  1157. dev->qdisc_tx_busylock = &team_tx_busylock_key;
  1158. }
  1159. static int team_init(struct net_device *dev)
  1160. {
  1161. struct team *team = netdev_priv(dev);
  1162. int i;
  1163. int err;
  1164. team->dev = dev;
  1165. mutex_init(&team->lock);
  1166. team_set_no_mode(team);
  1167. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  1168. if (!team->pcpu_stats)
  1169. return -ENOMEM;
  1170. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1171. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1172. INIT_LIST_HEAD(&team->port_list);
  1173. err = team_queue_override_init(team);
  1174. if (err)
  1175. goto err_team_queue_override_init;
  1176. team_adjust_ops(team);
  1177. INIT_LIST_HEAD(&team->option_list);
  1178. INIT_LIST_HEAD(&team->option_inst_list);
  1179. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1180. if (err)
  1181. goto err_options_register;
  1182. netif_carrier_off(dev);
  1183. team_set_lockdep_class(dev);
  1184. return 0;
  1185. err_options_register:
  1186. team_queue_override_fini(team);
  1187. err_team_queue_override_init:
  1188. free_percpu(team->pcpu_stats);
  1189. return err;
  1190. }
  1191. static void team_uninit(struct net_device *dev)
  1192. {
  1193. struct team *team = netdev_priv(dev);
  1194. struct team_port *port;
  1195. struct team_port *tmp;
  1196. mutex_lock(&team->lock);
  1197. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1198. team_port_del(team, port->dev);
  1199. __team_change_mode(team, NULL); /* cleanup */
  1200. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1201. team_queue_override_fini(team);
  1202. mutex_unlock(&team->lock);
  1203. }
  1204. static void team_destructor(struct net_device *dev)
  1205. {
  1206. struct team *team = netdev_priv(dev);
  1207. free_percpu(team->pcpu_stats);
  1208. free_netdev(dev);
  1209. }
  1210. static int team_open(struct net_device *dev)
  1211. {
  1212. return 0;
  1213. }
  1214. static int team_close(struct net_device *dev)
  1215. {
  1216. return 0;
  1217. }
  1218. /*
  1219. * note: already called with rcu_read_lock
  1220. */
  1221. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1222. {
  1223. struct team *team = netdev_priv(dev);
  1224. bool tx_success;
  1225. unsigned int len = skb->len;
  1226. tx_success = team_queue_override_transmit(team, skb);
  1227. if (!tx_success)
  1228. tx_success = team->ops.transmit(team, skb);
  1229. if (tx_success) {
  1230. struct team_pcpu_stats *pcpu_stats;
  1231. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1232. u64_stats_update_begin(&pcpu_stats->syncp);
  1233. pcpu_stats->tx_packets++;
  1234. pcpu_stats->tx_bytes += len;
  1235. u64_stats_update_end(&pcpu_stats->syncp);
  1236. } else {
  1237. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1238. }
  1239. return NETDEV_TX_OK;
  1240. }
  1241. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
  1242. {
  1243. /*
  1244. * This helper function exists to help dev_pick_tx get the correct
  1245. * destination queue. Using a helper function skips a call to
  1246. * skb_tx_hash and will put the skbs in the queue we expect on their
  1247. * way down to the team driver.
  1248. */
  1249. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1250. /*
  1251. * Save the original txq to restore before passing to the driver
  1252. */
  1253. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1254. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1255. do {
  1256. txq -= dev->real_num_tx_queues;
  1257. } while (txq >= dev->real_num_tx_queues);
  1258. }
  1259. return txq;
  1260. }
  1261. static void team_change_rx_flags(struct net_device *dev, int change)
  1262. {
  1263. struct team *team = netdev_priv(dev);
  1264. struct team_port *port;
  1265. int inc;
  1266. rcu_read_lock();
  1267. list_for_each_entry_rcu(port, &team->port_list, list) {
  1268. if (change & IFF_PROMISC) {
  1269. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1270. dev_set_promiscuity(port->dev, inc);
  1271. }
  1272. if (change & IFF_ALLMULTI) {
  1273. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1274. dev_set_allmulti(port->dev, inc);
  1275. }
  1276. }
  1277. rcu_read_unlock();
  1278. }
  1279. static void team_set_rx_mode(struct net_device *dev)
  1280. {
  1281. struct team *team = netdev_priv(dev);
  1282. struct team_port *port;
  1283. rcu_read_lock();
  1284. list_for_each_entry_rcu(port, &team->port_list, list) {
  1285. dev_uc_sync_multiple(port->dev, dev);
  1286. dev_mc_sync_multiple(port->dev, dev);
  1287. }
  1288. rcu_read_unlock();
  1289. }
  1290. static int team_set_mac_address(struct net_device *dev, void *p)
  1291. {
  1292. struct sockaddr *addr = p;
  1293. struct team *team = netdev_priv(dev);
  1294. struct team_port *port;
  1295. if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
  1296. return -EADDRNOTAVAIL;
  1297. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1298. rcu_read_lock();
  1299. list_for_each_entry_rcu(port, &team->port_list, list)
  1300. if (team->ops.port_change_dev_addr)
  1301. team->ops.port_change_dev_addr(team, port);
  1302. rcu_read_unlock();
  1303. return 0;
  1304. }
  1305. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1306. {
  1307. struct team *team = netdev_priv(dev);
  1308. struct team_port *port;
  1309. int err;
  1310. /*
  1311. * Alhough this is reader, it's guarded by team lock. It's not possible
  1312. * to traverse list in reverse under rcu_read_lock
  1313. */
  1314. mutex_lock(&team->lock);
  1315. list_for_each_entry(port, &team->port_list, list) {
  1316. err = dev_set_mtu(port->dev, new_mtu);
  1317. if (err) {
  1318. netdev_err(dev, "Device %s failed to change mtu",
  1319. port->dev->name);
  1320. goto unwind;
  1321. }
  1322. }
  1323. mutex_unlock(&team->lock);
  1324. dev->mtu = new_mtu;
  1325. return 0;
  1326. unwind:
  1327. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1328. dev_set_mtu(port->dev, dev->mtu);
  1329. mutex_unlock(&team->lock);
  1330. return err;
  1331. }
  1332. static struct rtnl_link_stats64 *
  1333. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1334. {
  1335. struct team *team = netdev_priv(dev);
  1336. struct team_pcpu_stats *p;
  1337. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1338. u32 rx_dropped = 0, tx_dropped = 0;
  1339. unsigned int start;
  1340. int i;
  1341. for_each_possible_cpu(i) {
  1342. p = per_cpu_ptr(team->pcpu_stats, i);
  1343. do {
  1344. start = u64_stats_fetch_begin_bh(&p->syncp);
  1345. rx_packets = p->rx_packets;
  1346. rx_bytes = p->rx_bytes;
  1347. rx_multicast = p->rx_multicast;
  1348. tx_packets = p->tx_packets;
  1349. tx_bytes = p->tx_bytes;
  1350. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  1351. stats->rx_packets += rx_packets;
  1352. stats->rx_bytes += rx_bytes;
  1353. stats->multicast += rx_multicast;
  1354. stats->tx_packets += tx_packets;
  1355. stats->tx_bytes += tx_bytes;
  1356. /*
  1357. * rx_dropped & tx_dropped are u32, updated
  1358. * without syncp protection.
  1359. */
  1360. rx_dropped += p->rx_dropped;
  1361. tx_dropped += p->tx_dropped;
  1362. }
  1363. stats->rx_dropped = rx_dropped;
  1364. stats->tx_dropped = tx_dropped;
  1365. return stats;
  1366. }
  1367. static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  1368. {
  1369. struct team *team = netdev_priv(dev);
  1370. struct team_port *port;
  1371. int err;
  1372. /*
  1373. * Alhough this is reader, it's guarded by team lock. It's not possible
  1374. * to traverse list in reverse under rcu_read_lock
  1375. */
  1376. mutex_lock(&team->lock);
  1377. list_for_each_entry(port, &team->port_list, list) {
  1378. err = vlan_vid_add(port->dev, proto, vid);
  1379. if (err)
  1380. goto unwind;
  1381. }
  1382. mutex_unlock(&team->lock);
  1383. return 0;
  1384. unwind:
  1385. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1386. vlan_vid_del(port->dev, proto, vid);
  1387. mutex_unlock(&team->lock);
  1388. return err;
  1389. }
  1390. static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
  1391. {
  1392. struct team *team = netdev_priv(dev);
  1393. struct team_port *port;
  1394. rcu_read_lock();
  1395. list_for_each_entry_rcu(port, &team->port_list, list)
  1396. vlan_vid_del(port->dev, proto, vid);
  1397. rcu_read_unlock();
  1398. return 0;
  1399. }
  1400. #ifdef CONFIG_NET_POLL_CONTROLLER
  1401. static void team_poll_controller(struct net_device *dev)
  1402. {
  1403. }
  1404. static void __team_netpoll_cleanup(struct team *team)
  1405. {
  1406. struct team_port *port;
  1407. list_for_each_entry(port, &team->port_list, list)
  1408. team_port_disable_netpoll(port);
  1409. }
  1410. static void team_netpoll_cleanup(struct net_device *dev)
  1411. {
  1412. struct team *team = netdev_priv(dev);
  1413. mutex_lock(&team->lock);
  1414. __team_netpoll_cleanup(team);
  1415. mutex_unlock(&team->lock);
  1416. }
  1417. static int team_netpoll_setup(struct net_device *dev,
  1418. struct netpoll_info *npifo, gfp_t gfp)
  1419. {
  1420. struct team *team = netdev_priv(dev);
  1421. struct team_port *port;
  1422. int err = 0;
  1423. mutex_lock(&team->lock);
  1424. list_for_each_entry(port, &team->port_list, list) {
  1425. err = team_port_enable_netpoll(team, port, gfp);
  1426. if (err) {
  1427. __team_netpoll_cleanup(team);
  1428. break;
  1429. }
  1430. }
  1431. mutex_unlock(&team->lock);
  1432. return err;
  1433. }
  1434. #endif
  1435. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1436. {
  1437. struct team *team = netdev_priv(dev);
  1438. int err;
  1439. mutex_lock(&team->lock);
  1440. err = team_port_add(team, port_dev);
  1441. mutex_unlock(&team->lock);
  1442. return err;
  1443. }
  1444. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1445. {
  1446. struct team *team = netdev_priv(dev);
  1447. int err;
  1448. mutex_lock(&team->lock);
  1449. err = team_port_del(team, port_dev);
  1450. mutex_unlock(&team->lock);
  1451. return err;
  1452. }
  1453. static netdev_features_t team_fix_features(struct net_device *dev,
  1454. netdev_features_t features)
  1455. {
  1456. struct team_port *port;
  1457. struct team *team = netdev_priv(dev);
  1458. netdev_features_t mask;
  1459. mask = features;
  1460. features &= ~NETIF_F_ONE_FOR_ALL;
  1461. features |= NETIF_F_ALL_FOR_ALL;
  1462. rcu_read_lock();
  1463. list_for_each_entry_rcu(port, &team->port_list, list) {
  1464. features = netdev_increment_features(features,
  1465. port->dev->features,
  1466. mask);
  1467. }
  1468. rcu_read_unlock();
  1469. return features;
  1470. }
  1471. static int team_change_carrier(struct net_device *dev, bool new_carrier)
  1472. {
  1473. struct team *team = netdev_priv(dev);
  1474. team->user_carrier_enabled = true;
  1475. if (new_carrier)
  1476. netif_carrier_on(dev);
  1477. else
  1478. netif_carrier_off(dev);
  1479. return 0;
  1480. }
  1481. static const struct net_device_ops team_netdev_ops = {
  1482. .ndo_init = team_init,
  1483. .ndo_uninit = team_uninit,
  1484. .ndo_open = team_open,
  1485. .ndo_stop = team_close,
  1486. .ndo_start_xmit = team_xmit,
  1487. .ndo_select_queue = team_select_queue,
  1488. .ndo_change_rx_flags = team_change_rx_flags,
  1489. .ndo_set_rx_mode = team_set_rx_mode,
  1490. .ndo_set_mac_address = team_set_mac_address,
  1491. .ndo_change_mtu = team_change_mtu,
  1492. .ndo_get_stats64 = team_get_stats64,
  1493. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1494. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1495. #ifdef CONFIG_NET_POLL_CONTROLLER
  1496. .ndo_poll_controller = team_poll_controller,
  1497. .ndo_netpoll_setup = team_netpoll_setup,
  1498. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1499. #endif
  1500. .ndo_add_slave = team_add_slave,
  1501. .ndo_del_slave = team_del_slave,
  1502. .ndo_fix_features = team_fix_features,
  1503. .ndo_change_carrier = team_change_carrier,
  1504. };
  1505. /***********************
  1506. * ethtool interface
  1507. ***********************/
  1508. static void team_ethtool_get_drvinfo(struct net_device *dev,
  1509. struct ethtool_drvinfo *drvinfo)
  1510. {
  1511. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  1512. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1513. }
  1514. static const struct ethtool_ops team_ethtool_ops = {
  1515. .get_drvinfo = team_ethtool_get_drvinfo,
  1516. .get_link = ethtool_op_get_link,
  1517. };
  1518. /***********************
  1519. * rt netlink interface
  1520. ***********************/
  1521. static void team_setup_by_port(struct net_device *dev,
  1522. struct net_device *port_dev)
  1523. {
  1524. dev->header_ops = port_dev->header_ops;
  1525. dev->type = port_dev->type;
  1526. dev->hard_header_len = port_dev->hard_header_len;
  1527. dev->addr_len = port_dev->addr_len;
  1528. dev->mtu = port_dev->mtu;
  1529. memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
  1530. memcpy(dev->dev_addr, port_dev->dev_addr, port_dev->addr_len);
  1531. }
  1532. static int team_dev_type_check_change(struct net_device *dev,
  1533. struct net_device *port_dev)
  1534. {
  1535. struct team *team = netdev_priv(dev);
  1536. char *portname = port_dev->name;
  1537. int err;
  1538. if (dev->type == port_dev->type)
  1539. return 0;
  1540. if (!list_empty(&team->port_list)) {
  1541. netdev_err(dev, "Device %s is of different type\n", portname);
  1542. return -EBUSY;
  1543. }
  1544. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  1545. err = notifier_to_errno(err);
  1546. if (err) {
  1547. netdev_err(dev, "Refused to change device type\n");
  1548. return err;
  1549. }
  1550. dev_uc_flush(dev);
  1551. dev_mc_flush(dev);
  1552. team_setup_by_port(dev, port_dev);
  1553. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  1554. return 0;
  1555. }
  1556. static void team_setup(struct net_device *dev)
  1557. {
  1558. ether_setup(dev);
  1559. dev->netdev_ops = &team_netdev_ops;
  1560. dev->ethtool_ops = &team_ethtool_ops;
  1561. dev->destructor = team_destructor;
  1562. dev->tx_queue_len = 0;
  1563. dev->flags |= IFF_MULTICAST;
  1564. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1565. /*
  1566. * Indicate we support unicast address filtering. That way core won't
  1567. * bring us to promisc mode in case a unicast addr is added.
  1568. * Let this up to underlay drivers.
  1569. */
  1570. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1571. dev->features |= NETIF_F_LLTX;
  1572. dev->features |= NETIF_F_GRO;
  1573. dev->hw_features = TEAM_VLAN_FEATURES |
  1574. NETIF_F_HW_VLAN_CTAG_TX |
  1575. NETIF_F_HW_VLAN_CTAG_RX |
  1576. NETIF_F_HW_VLAN_CTAG_FILTER;
  1577. dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
  1578. dev->features |= dev->hw_features;
  1579. }
  1580. static int team_newlink(struct net *src_net, struct net_device *dev,
  1581. struct nlattr *tb[], struct nlattr *data[])
  1582. {
  1583. int err;
  1584. if (tb[IFLA_ADDRESS] == NULL)
  1585. eth_hw_addr_random(dev);
  1586. err = register_netdevice(dev);
  1587. if (err)
  1588. return err;
  1589. return 0;
  1590. }
  1591. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1592. {
  1593. if (tb[IFLA_ADDRESS]) {
  1594. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1595. return -EINVAL;
  1596. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1597. return -EADDRNOTAVAIL;
  1598. }
  1599. return 0;
  1600. }
  1601. static unsigned int team_get_num_tx_queues(void)
  1602. {
  1603. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1604. }
  1605. static unsigned int team_get_num_rx_queues(void)
  1606. {
  1607. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1608. }
  1609. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1610. .kind = DRV_NAME,
  1611. .priv_size = sizeof(struct team),
  1612. .setup = team_setup,
  1613. .newlink = team_newlink,
  1614. .validate = team_validate,
  1615. .get_num_tx_queues = team_get_num_tx_queues,
  1616. .get_num_rx_queues = team_get_num_rx_queues,
  1617. };
  1618. /***********************************
  1619. * Generic netlink custom interface
  1620. ***********************************/
  1621. static struct genl_family team_nl_family = {
  1622. .id = GENL_ID_GENERATE,
  1623. .name = TEAM_GENL_NAME,
  1624. .version = TEAM_GENL_VERSION,
  1625. .maxattr = TEAM_ATTR_MAX,
  1626. .netnsok = true,
  1627. };
  1628. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1629. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1630. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1631. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1632. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1633. };
  1634. static const struct nla_policy
  1635. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1636. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1637. [TEAM_ATTR_OPTION_NAME] = {
  1638. .type = NLA_STRING,
  1639. .len = TEAM_STRING_MAX_LEN,
  1640. },
  1641. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1642. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1643. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1644. };
  1645. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1646. {
  1647. struct sk_buff *msg;
  1648. void *hdr;
  1649. int err;
  1650. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1651. if (!msg)
  1652. return -ENOMEM;
  1653. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1654. &team_nl_family, 0, TEAM_CMD_NOOP);
  1655. if (!hdr) {
  1656. err = -EMSGSIZE;
  1657. goto err_msg_put;
  1658. }
  1659. genlmsg_end(msg, hdr);
  1660. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  1661. err_msg_put:
  1662. nlmsg_free(msg);
  1663. return err;
  1664. }
  1665. /*
  1666. * Netlink cmd functions should be locked by following two functions.
  1667. * Since dev gets held here, that ensures dev won't disappear in between.
  1668. */
  1669. static struct team *team_nl_team_get(struct genl_info *info)
  1670. {
  1671. struct net *net = genl_info_net(info);
  1672. int ifindex;
  1673. struct net_device *dev;
  1674. struct team *team;
  1675. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1676. return NULL;
  1677. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1678. dev = dev_get_by_index(net, ifindex);
  1679. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1680. if (dev)
  1681. dev_put(dev);
  1682. return NULL;
  1683. }
  1684. team = netdev_priv(dev);
  1685. mutex_lock(&team->lock);
  1686. return team;
  1687. }
  1688. static void team_nl_team_put(struct team *team)
  1689. {
  1690. mutex_unlock(&team->lock);
  1691. dev_put(team->dev);
  1692. }
  1693. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1694. struct team *team, u32 portid);
  1695. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
  1696. {
  1697. return genlmsg_unicast(dev_net(team->dev), skb, portid);
  1698. }
  1699. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1700. struct team_option_inst *opt_inst)
  1701. {
  1702. struct nlattr *option_item;
  1703. struct team_option *option = opt_inst->option;
  1704. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1705. struct team_gsetter_ctx ctx;
  1706. int err;
  1707. ctx.info = opt_inst_info;
  1708. err = team_option_get(team, opt_inst, &ctx);
  1709. if (err)
  1710. return err;
  1711. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1712. if (!option_item)
  1713. return -EMSGSIZE;
  1714. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1715. goto nest_cancel;
  1716. if (opt_inst_info->port &&
  1717. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1718. opt_inst_info->port->dev->ifindex))
  1719. goto nest_cancel;
  1720. if (opt_inst->option->array_size &&
  1721. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1722. opt_inst_info->array_index))
  1723. goto nest_cancel;
  1724. switch (option->type) {
  1725. case TEAM_OPTION_TYPE_U32:
  1726. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1727. goto nest_cancel;
  1728. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1729. goto nest_cancel;
  1730. break;
  1731. case TEAM_OPTION_TYPE_STRING:
  1732. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1733. goto nest_cancel;
  1734. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1735. ctx.data.str_val))
  1736. goto nest_cancel;
  1737. break;
  1738. case TEAM_OPTION_TYPE_BINARY:
  1739. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1740. goto nest_cancel;
  1741. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1742. ctx.data.bin_val.ptr))
  1743. goto nest_cancel;
  1744. break;
  1745. case TEAM_OPTION_TYPE_BOOL:
  1746. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1747. goto nest_cancel;
  1748. if (ctx.data.bool_val &&
  1749. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1750. goto nest_cancel;
  1751. break;
  1752. case TEAM_OPTION_TYPE_S32:
  1753. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1754. goto nest_cancel;
  1755. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1756. goto nest_cancel;
  1757. break;
  1758. default:
  1759. BUG();
  1760. }
  1761. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1762. goto nest_cancel;
  1763. if (opt_inst->changed) {
  1764. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1765. goto nest_cancel;
  1766. opt_inst->changed = false;
  1767. }
  1768. nla_nest_end(skb, option_item);
  1769. return 0;
  1770. nest_cancel:
  1771. nla_nest_cancel(skb, option_item);
  1772. return -EMSGSIZE;
  1773. }
  1774. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1775. struct team *team, u32 portid,
  1776. team_nl_send_func_t *send_func)
  1777. {
  1778. int err;
  1779. if (*pskb) {
  1780. err = send_func(*pskb, team, portid);
  1781. if (err)
  1782. return err;
  1783. }
  1784. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1785. if (!*pskb)
  1786. return -ENOMEM;
  1787. return 0;
  1788. }
  1789. static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
  1790. int flags, team_nl_send_func_t *send_func,
  1791. struct list_head *sel_opt_inst_list)
  1792. {
  1793. struct nlattr *option_list;
  1794. struct nlmsghdr *nlh;
  1795. void *hdr;
  1796. struct team_option_inst *opt_inst;
  1797. int err;
  1798. struct sk_buff *skb = NULL;
  1799. bool incomplete;
  1800. int i;
  1801. opt_inst = list_first_entry(sel_opt_inst_list,
  1802. struct team_option_inst, tmp_list);
  1803. start_again:
  1804. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1805. if (err)
  1806. return err;
  1807. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  1808. TEAM_CMD_OPTIONS_GET);
  1809. if (!hdr)
  1810. return -EMSGSIZE;
  1811. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1812. goto nla_put_failure;
  1813. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1814. if (!option_list)
  1815. goto nla_put_failure;
  1816. i = 0;
  1817. incomplete = false;
  1818. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  1819. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  1820. if (err) {
  1821. if (err == -EMSGSIZE) {
  1822. if (!i)
  1823. goto errout;
  1824. incomplete = true;
  1825. break;
  1826. }
  1827. goto errout;
  1828. }
  1829. i++;
  1830. }
  1831. nla_nest_end(skb, option_list);
  1832. genlmsg_end(skb, hdr);
  1833. if (incomplete)
  1834. goto start_again;
  1835. send_done:
  1836. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1837. if (!nlh) {
  1838. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  1839. if (err)
  1840. goto errout;
  1841. goto send_done;
  1842. }
  1843. return send_func(skb, team, portid);
  1844. nla_put_failure:
  1845. err = -EMSGSIZE;
  1846. errout:
  1847. genlmsg_cancel(skb, hdr);
  1848. nlmsg_free(skb);
  1849. return err;
  1850. }
  1851. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1852. {
  1853. struct team *team;
  1854. struct team_option_inst *opt_inst;
  1855. int err;
  1856. LIST_HEAD(sel_opt_inst_list);
  1857. team = team_nl_team_get(info);
  1858. if (!team)
  1859. return -EINVAL;
  1860. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  1861. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  1862. err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
  1863. NLM_F_ACK, team_nl_send_unicast,
  1864. &sel_opt_inst_list);
  1865. team_nl_team_put(team);
  1866. return err;
  1867. }
  1868. static int team_nl_send_event_options_get(struct team *team,
  1869. struct list_head *sel_opt_inst_list);
  1870. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1871. {
  1872. struct team *team;
  1873. int err = 0;
  1874. int i;
  1875. struct nlattr *nl_option;
  1876. LIST_HEAD(opt_inst_list);
  1877. team = team_nl_team_get(info);
  1878. if (!team)
  1879. return -EINVAL;
  1880. err = -EINVAL;
  1881. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  1882. err = -EINVAL;
  1883. goto team_put;
  1884. }
  1885. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  1886. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  1887. struct nlattr *attr;
  1888. struct nlattr *attr_data;
  1889. enum team_option_type opt_type;
  1890. int opt_port_ifindex = 0; /* != 0 for per-port options */
  1891. u32 opt_array_index = 0;
  1892. bool opt_is_array = false;
  1893. struct team_option_inst *opt_inst;
  1894. char *opt_name;
  1895. bool opt_found = false;
  1896. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  1897. err = -EINVAL;
  1898. goto team_put;
  1899. }
  1900. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  1901. nl_option, team_nl_option_policy);
  1902. if (err)
  1903. goto team_put;
  1904. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  1905. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  1906. err = -EINVAL;
  1907. goto team_put;
  1908. }
  1909. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  1910. case NLA_U32:
  1911. opt_type = TEAM_OPTION_TYPE_U32;
  1912. break;
  1913. case NLA_STRING:
  1914. opt_type = TEAM_OPTION_TYPE_STRING;
  1915. break;
  1916. case NLA_BINARY:
  1917. opt_type = TEAM_OPTION_TYPE_BINARY;
  1918. break;
  1919. case NLA_FLAG:
  1920. opt_type = TEAM_OPTION_TYPE_BOOL;
  1921. break;
  1922. case NLA_S32:
  1923. opt_type = TEAM_OPTION_TYPE_S32;
  1924. break;
  1925. default:
  1926. goto team_put;
  1927. }
  1928. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  1929. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  1930. err = -EINVAL;
  1931. goto team_put;
  1932. }
  1933. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  1934. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  1935. if (attr)
  1936. opt_port_ifindex = nla_get_u32(attr);
  1937. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  1938. if (attr) {
  1939. opt_is_array = true;
  1940. opt_array_index = nla_get_u32(attr);
  1941. }
  1942. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  1943. struct team_option *option = opt_inst->option;
  1944. struct team_gsetter_ctx ctx;
  1945. struct team_option_inst_info *opt_inst_info;
  1946. int tmp_ifindex;
  1947. opt_inst_info = &opt_inst->info;
  1948. tmp_ifindex = opt_inst_info->port ?
  1949. opt_inst_info->port->dev->ifindex : 0;
  1950. if (option->type != opt_type ||
  1951. strcmp(option->name, opt_name) ||
  1952. tmp_ifindex != opt_port_ifindex ||
  1953. (option->array_size && !opt_is_array) ||
  1954. opt_inst_info->array_index != opt_array_index)
  1955. continue;
  1956. opt_found = true;
  1957. ctx.info = opt_inst_info;
  1958. switch (opt_type) {
  1959. case TEAM_OPTION_TYPE_U32:
  1960. ctx.data.u32_val = nla_get_u32(attr_data);
  1961. break;
  1962. case TEAM_OPTION_TYPE_STRING:
  1963. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  1964. err = -EINVAL;
  1965. goto team_put;
  1966. }
  1967. ctx.data.str_val = nla_data(attr_data);
  1968. break;
  1969. case TEAM_OPTION_TYPE_BINARY:
  1970. ctx.data.bin_val.len = nla_len(attr_data);
  1971. ctx.data.bin_val.ptr = nla_data(attr_data);
  1972. break;
  1973. case TEAM_OPTION_TYPE_BOOL:
  1974. ctx.data.bool_val = attr_data ? true : false;
  1975. break;
  1976. case TEAM_OPTION_TYPE_S32:
  1977. ctx.data.s32_val = nla_get_s32(attr_data);
  1978. break;
  1979. default:
  1980. BUG();
  1981. }
  1982. err = team_option_set(team, opt_inst, &ctx);
  1983. if (err)
  1984. goto team_put;
  1985. opt_inst->changed = true;
  1986. list_add(&opt_inst->tmp_list, &opt_inst_list);
  1987. }
  1988. if (!opt_found) {
  1989. err = -ENOENT;
  1990. goto team_put;
  1991. }
  1992. }
  1993. err = team_nl_send_event_options_get(team, &opt_inst_list);
  1994. team_put:
  1995. team_nl_team_put(team);
  1996. return err;
  1997. }
  1998. static int team_nl_fill_one_port_get(struct sk_buff *skb,
  1999. struct team_port *port)
  2000. {
  2001. struct nlattr *port_item;
  2002. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  2003. if (!port_item)
  2004. goto nest_cancel;
  2005. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  2006. goto nest_cancel;
  2007. if (port->changed) {
  2008. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  2009. goto nest_cancel;
  2010. port->changed = false;
  2011. }
  2012. if ((port->removed &&
  2013. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  2014. (port->state.linkup &&
  2015. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  2016. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  2017. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  2018. goto nest_cancel;
  2019. nla_nest_end(skb, port_item);
  2020. return 0;
  2021. nest_cancel:
  2022. nla_nest_cancel(skb, port_item);
  2023. return -EMSGSIZE;
  2024. }
  2025. static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
  2026. int flags, team_nl_send_func_t *send_func,
  2027. struct team_port *one_port)
  2028. {
  2029. struct nlattr *port_list;
  2030. struct nlmsghdr *nlh;
  2031. void *hdr;
  2032. struct team_port *port;
  2033. int err;
  2034. struct sk_buff *skb = NULL;
  2035. bool incomplete;
  2036. int i;
  2037. port = list_first_entry_or_null(&team->port_list,
  2038. struct team_port, list);
  2039. start_again:
  2040. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2041. if (err)
  2042. return err;
  2043. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2044. TEAM_CMD_PORT_LIST_GET);
  2045. if (!hdr)
  2046. return -EMSGSIZE;
  2047. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2048. goto nla_put_failure;
  2049. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  2050. if (!port_list)
  2051. goto nla_put_failure;
  2052. i = 0;
  2053. incomplete = false;
  2054. /* If one port is selected, called wants to send port list containing
  2055. * only this port. Otherwise go through all listed ports and send all
  2056. */
  2057. if (one_port) {
  2058. err = team_nl_fill_one_port_get(skb, one_port);
  2059. if (err)
  2060. goto errout;
  2061. } else if (port) {
  2062. list_for_each_entry_from(port, &team->port_list, list) {
  2063. err = team_nl_fill_one_port_get(skb, port);
  2064. if (err) {
  2065. if (err == -EMSGSIZE) {
  2066. if (!i)
  2067. goto errout;
  2068. incomplete = true;
  2069. break;
  2070. }
  2071. goto errout;
  2072. }
  2073. i++;
  2074. }
  2075. }
  2076. nla_nest_end(skb, port_list);
  2077. genlmsg_end(skb, hdr);
  2078. if (incomplete)
  2079. goto start_again;
  2080. send_done:
  2081. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2082. if (!nlh) {
  2083. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2084. if (err)
  2085. goto errout;
  2086. goto send_done;
  2087. }
  2088. return send_func(skb, team, portid);
  2089. nla_put_failure:
  2090. err = -EMSGSIZE;
  2091. errout:
  2092. genlmsg_cancel(skb, hdr);
  2093. nlmsg_free(skb);
  2094. return err;
  2095. }
  2096. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  2097. struct genl_info *info)
  2098. {
  2099. struct team *team;
  2100. int err;
  2101. team = team_nl_team_get(info);
  2102. if (!team)
  2103. return -EINVAL;
  2104. err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
  2105. NLM_F_ACK, team_nl_send_unicast, NULL);
  2106. team_nl_team_put(team);
  2107. return err;
  2108. }
  2109. static struct genl_ops team_nl_ops[] = {
  2110. {
  2111. .cmd = TEAM_CMD_NOOP,
  2112. .doit = team_nl_cmd_noop,
  2113. .policy = team_nl_policy,
  2114. },
  2115. {
  2116. .cmd = TEAM_CMD_OPTIONS_SET,
  2117. .doit = team_nl_cmd_options_set,
  2118. .policy = team_nl_policy,
  2119. .flags = GENL_ADMIN_PERM,
  2120. },
  2121. {
  2122. .cmd = TEAM_CMD_OPTIONS_GET,
  2123. .doit = team_nl_cmd_options_get,
  2124. .policy = team_nl_policy,
  2125. .flags = GENL_ADMIN_PERM,
  2126. },
  2127. {
  2128. .cmd = TEAM_CMD_PORT_LIST_GET,
  2129. .doit = team_nl_cmd_port_list_get,
  2130. .policy = team_nl_policy,
  2131. .flags = GENL_ADMIN_PERM,
  2132. },
  2133. };
  2134. static struct genl_multicast_group team_change_event_mcgrp = {
  2135. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  2136. };
  2137. static int team_nl_send_multicast(struct sk_buff *skb,
  2138. struct team *team, u32 portid)
  2139. {
  2140. return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
  2141. team_change_event_mcgrp.id, GFP_KERNEL);
  2142. }
  2143. static int team_nl_send_event_options_get(struct team *team,
  2144. struct list_head *sel_opt_inst_list)
  2145. {
  2146. return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
  2147. sel_opt_inst_list);
  2148. }
  2149. static int team_nl_send_event_port_get(struct team *team,
  2150. struct team_port *port)
  2151. {
  2152. return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
  2153. port);
  2154. }
  2155. static int team_nl_init(void)
  2156. {
  2157. int err;
  2158. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  2159. ARRAY_SIZE(team_nl_ops));
  2160. if (err)
  2161. return err;
  2162. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  2163. if (err)
  2164. goto err_change_event_grp_reg;
  2165. return 0;
  2166. err_change_event_grp_reg:
  2167. genl_unregister_family(&team_nl_family);
  2168. return err;
  2169. }
  2170. static void team_nl_fini(void)
  2171. {
  2172. genl_unregister_family(&team_nl_family);
  2173. }
  2174. /******************
  2175. * Change checkers
  2176. ******************/
  2177. static void __team_options_change_check(struct team *team)
  2178. {
  2179. int err;
  2180. struct team_option_inst *opt_inst;
  2181. LIST_HEAD(sel_opt_inst_list);
  2182. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2183. if (opt_inst->changed)
  2184. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2185. }
  2186. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2187. if (err && err != -ESRCH)
  2188. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2189. err);
  2190. }
  2191. /* rtnl lock is held */
  2192. static void __team_port_change_send(struct team_port *port, bool linkup)
  2193. {
  2194. int err;
  2195. port->changed = true;
  2196. port->state.linkup = linkup;
  2197. team_refresh_port_linkup(port);
  2198. if (linkup) {
  2199. struct ethtool_cmd ecmd;
  2200. err = __ethtool_get_settings(port->dev, &ecmd);
  2201. if (!err) {
  2202. port->state.speed = ethtool_cmd_speed(&ecmd);
  2203. port->state.duplex = ecmd.duplex;
  2204. goto send_event;
  2205. }
  2206. }
  2207. port->state.speed = 0;
  2208. port->state.duplex = 0;
  2209. send_event:
  2210. err = team_nl_send_event_port_get(port->team, port);
  2211. if (err && err != -ESRCH)
  2212. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
  2213. port->dev->name, err);
  2214. }
  2215. static void __team_carrier_check(struct team *team)
  2216. {
  2217. struct team_port *port;
  2218. bool team_linkup;
  2219. if (team->user_carrier_enabled)
  2220. return;
  2221. team_linkup = false;
  2222. list_for_each_entry(port, &team->port_list, list) {
  2223. if (port->linkup) {
  2224. team_linkup = true;
  2225. break;
  2226. }
  2227. }
  2228. if (team_linkup)
  2229. netif_carrier_on(team->dev);
  2230. else
  2231. netif_carrier_off(team->dev);
  2232. }
  2233. static void __team_port_change_check(struct team_port *port, bool linkup)
  2234. {
  2235. if (port->state.linkup != linkup)
  2236. __team_port_change_send(port, linkup);
  2237. __team_carrier_check(port->team);
  2238. }
  2239. static void __team_port_change_port_added(struct team_port *port, bool linkup)
  2240. {
  2241. __team_port_change_send(port, linkup);
  2242. __team_carrier_check(port->team);
  2243. }
  2244. static void __team_port_change_port_removed(struct team_port *port)
  2245. {
  2246. port->removed = true;
  2247. __team_port_change_send(port, false);
  2248. __team_carrier_check(port->team);
  2249. }
  2250. static void team_port_change_check(struct team_port *port, bool linkup)
  2251. {
  2252. struct team *team = port->team;
  2253. mutex_lock(&team->lock);
  2254. __team_port_change_check(port, linkup);
  2255. mutex_unlock(&team->lock);
  2256. }
  2257. /************************************
  2258. * Net device notifier event handler
  2259. ************************************/
  2260. static int team_device_event(struct notifier_block *unused,
  2261. unsigned long event, void *ptr)
  2262. {
  2263. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2264. struct team_port *port;
  2265. port = team_port_get_rtnl(dev);
  2266. if (!port)
  2267. return NOTIFY_DONE;
  2268. switch (event) {
  2269. case NETDEV_UP:
  2270. if (netif_carrier_ok(dev))
  2271. team_port_change_check(port, true);
  2272. case NETDEV_DOWN:
  2273. team_port_change_check(port, false);
  2274. case NETDEV_CHANGE:
  2275. if (netif_running(port->dev))
  2276. team_port_change_check(port,
  2277. !!netif_carrier_ok(port->dev));
  2278. break;
  2279. case NETDEV_UNREGISTER:
  2280. team_del_slave(port->team->dev, dev);
  2281. break;
  2282. case NETDEV_FEAT_CHANGE:
  2283. team_compute_features(port->team);
  2284. break;
  2285. case NETDEV_CHANGEMTU:
  2286. /* Forbid to change mtu of underlaying device */
  2287. return NOTIFY_BAD;
  2288. case NETDEV_PRE_TYPE_CHANGE:
  2289. /* Forbid to change type of underlaying device */
  2290. return NOTIFY_BAD;
  2291. }
  2292. return NOTIFY_DONE;
  2293. }
  2294. static struct notifier_block team_notifier_block __read_mostly = {
  2295. .notifier_call = team_device_event,
  2296. };
  2297. /***********************
  2298. * Module init and exit
  2299. ***********************/
  2300. static int __init team_module_init(void)
  2301. {
  2302. int err;
  2303. register_netdevice_notifier(&team_notifier_block);
  2304. err = rtnl_link_register(&team_link_ops);
  2305. if (err)
  2306. goto err_rtnl_reg;
  2307. err = team_nl_init();
  2308. if (err)
  2309. goto err_nl_init;
  2310. return 0;
  2311. err_nl_init:
  2312. rtnl_link_unregister(&team_link_ops);
  2313. err_rtnl_reg:
  2314. unregister_netdevice_notifier(&team_notifier_block);
  2315. return err;
  2316. }
  2317. static void __exit team_module_exit(void)
  2318. {
  2319. team_nl_fini();
  2320. rtnl_link_unregister(&team_link_ops);
  2321. unregister_netdevice_notifier(&team_notifier_block);
  2322. }
  2323. module_init(team_module_init);
  2324. module_exit(team_module_exit);
  2325. MODULE_LICENSE("GPL v2");
  2326. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2327. MODULE_DESCRIPTION("Ethernet team device driver");
  2328. MODULE_ALIAS_RTNL_LINK(DRV_NAME);