team.c 63 KB

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