team.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. /*
  2. * net/drivers/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/if_vlan.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/socket.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/rtnetlink.h>
  25. #include <net/rtnetlink.h>
  26. #include <net/genetlink.h>
  27. #include <net/netlink.h>
  28. #include <linux/if_team.h>
  29. #define DRV_NAME "team"
  30. /**********
  31. * Helpers
  32. **********/
  33. #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
  34. static struct team_port *team_port_get_rcu(const struct net_device *dev)
  35. {
  36. struct team_port *port = rcu_dereference(dev->rx_handler_data);
  37. return team_port_exists(dev) ? port : NULL;
  38. }
  39. static struct team_port *team_port_get_rtnl(const struct net_device *dev)
  40. {
  41. struct team_port *port = rtnl_dereference(dev->rx_handler_data);
  42. return team_port_exists(dev) ? port : NULL;
  43. }
  44. /*
  45. * Since the ability to change mac address for open port device is tested in
  46. * team_port_add, this function can be called without control of return value
  47. */
  48. static int __set_port_mac(struct net_device *port_dev,
  49. const unsigned char *dev_addr)
  50. {
  51. struct sockaddr addr;
  52. memcpy(addr.sa_data, dev_addr, ETH_ALEN);
  53. addr.sa_family = ARPHRD_ETHER;
  54. return dev_set_mac_address(port_dev, &addr);
  55. }
  56. static int team_port_set_orig_mac(struct team_port *port)
  57. {
  58. return __set_port_mac(port->dev, port->orig.dev_addr);
  59. }
  60. int team_port_set_team_mac(struct team_port *port)
  61. {
  62. return __set_port_mac(port->dev, port->team->dev->dev_addr);
  63. }
  64. EXPORT_SYMBOL(team_port_set_team_mac);
  65. static void team_refresh_port_linkup(struct team_port *port)
  66. {
  67. port->linkup = port->user.linkup_enabled ? port->user.linkup :
  68. port->state.linkup;
  69. }
  70. /*******************
  71. * Options handling
  72. *******************/
  73. struct team_option_inst { /* One for each option instance */
  74. struct list_head list;
  75. struct team_option *option;
  76. struct team_port *port; /* != NULL if per-port */
  77. bool changed;
  78. bool removed;
  79. };
  80. static struct team_option *__team_find_option(struct team *team,
  81. const char *opt_name)
  82. {
  83. struct team_option *option;
  84. list_for_each_entry(option, &team->option_list, list) {
  85. if (strcmp(option->name, opt_name) == 0)
  86. return option;
  87. }
  88. return NULL;
  89. }
  90. static int __team_option_inst_add(struct team *team, struct team_option *option,
  91. struct team_port *port)
  92. {
  93. struct team_option_inst *opt_inst;
  94. opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
  95. if (!opt_inst)
  96. return -ENOMEM;
  97. opt_inst->option = option;
  98. opt_inst->port = port;
  99. opt_inst->changed = true;
  100. opt_inst->removed = false;
  101. list_add_tail(&opt_inst->list, &team->option_inst_list);
  102. return 0;
  103. }
  104. static void __team_option_inst_del(struct team_option_inst *opt_inst)
  105. {
  106. list_del(&opt_inst->list);
  107. kfree(opt_inst);
  108. }
  109. static void __team_option_inst_del_option(struct team *team,
  110. struct team_option *option)
  111. {
  112. struct team_option_inst *opt_inst, *tmp;
  113. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  114. if (opt_inst->option == option)
  115. __team_option_inst_del(opt_inst);
  116. }
  117. }
  118. static int __team_option_inst_add_option(struct team *team,
  119. struct team_option *option)
  120. {
  121. struct team_port *port;
  122. int err;
  123. if (!option->per_port)
  124. return __team_option_inst_add(team, option, 0);
  125. list_for_each_entry(port, &team->port_list, list) {
  126. err = __team_option_inst_add(team, option, port);
  127. if (err)
  128. goto inst_del_option;
  129. }
  130. return 0;
  131. inst_del_option:
  132. __team_option_inst_del_option(team, option);
  133. return err;
  134. }
  135. static void __team_option_inst_mark_removed_option(struct team *team,
  136. struct team_option *option)
  137. {
  138. struct team_option_inst *opt_inst;
  139. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  140. if (opt_inst->option == option) {
  141. opt_inst->changed = true;
  142. opt_inst->removed = true;
  143. }
  144. }
  145. }
  146. static void __team_option_inst_del_port(struct team *team,
  147. struct team_port *port)
  148. {
  149. struct team_option_inst *opt_inst, *tmp;
  150. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  151. if (opt_inst->option->per_port &&
  152. opt_inst->port == port)
  153. __team_option_inst_del(opt_inst);
  154. }
  155. }
  156. static int __team_option_inst_add_port(struct team *team,
  157. struct team_port *port)
  158. {
  159. struct team_option *option;
  160. int err;
  161. list_for_each_entry(option, &team->option_list, list) {
  162. if (!option->per_port)
  163. continue;
  164. err = __team_option_inst_add(team, option, port);
  165. if (err)
  166. goto inst_del_port;
  167. }
  168. return 0;
  169. inst_del_port:
  170. __team_option_inst_del_port(team, port);
  171. return err;
  172. }
  173. static void __team_option_inst_mark_removed_port(struct team *team,
  174. struct team_port *port)
  175. {
  176. struct team_option_inst *opt_inst;
  177. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  178. if (opt_inst->port == port) {
  179. opt_inst->changed = true;
  180. opt_inst->removed = true;
  181. }
  182. }
  183. }
  184. static int __team_options_register(struct team *team,
  185. const struct team_option *option,
  186. size_t option_count)
  187. {
  188. int i;
  189. struct team_option **dst_opts;
  190. int err;
  191. dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
  192. GFP_KERNEL);
  193. if (!dst_opts)
  194. return -ENOMEM;
  195. for (i = 0; i < option_count; i++, option++) {
  196. if (__team_find_option(team, option->name)) {
  197. err = -EEXIST;
  198. goto alloc_rollback;
  199. }
  200. dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
  201. if (!dst_opts[i]) {
  202. err = -ENOMEM;
  203. goto alloc_rollback;
  204. }
  205. }
  206. for (i = 0; i < option_count; i++) {
  207. err = __team_option_inst_add_option(team, dst_opts[i]);
  208. if (err)
  209. goto inst_rollback;
  210. list_add_tail(&dst_opts[i]->list, &team->option_list);
  211. }
  212. kfree(dst_opts);
  213. return 0;
  214. inst_rollback:
  215. for (i--; i >= 0; i--)
  216. __team_option_inst_del_option(team, dst_opts[i]);
  217. i = option_count - 1;
  218. alloc_rollback:
  219. for (i--; i >= 0; i--)
  220. kfree(dst_opts[i]);
  221. kfree(dst_opts);
  222. return err;
  223. }
  224. static void __team_options_mark_removed(struct team *team,
  225. const struct team_option *option,
  226. size_t option_count)
  227. {
  228. int i;
  229. for (i = 0; i < option_count; i++, option++) {
  230. struct team_option *del_opt;
  231. del_opt = __team_find_option(team, option->name);
  232. if (del_opt)
  233. __team_option_inst_mark_removed_option(team, del_opt);
  234. }
  235. }
  236. static void __team_options_unregister(struct team *team,
  237. const struct team_option *option,
  238. size_t option_count)
  239. {
  240. int i;
  241. for (i = 0; i < option_count; i++, option++) {
  242. struct team_option *del_opt;
  243. del_opt = __team_find_option(team, option->name);
  244. if (del_opt) {
  245. __team_option_inst_del_option(team, del_opt);
  246. list_del(&del_opt->list);
  247. kfree(del_opt);
  248. }
  249. }
  250. }
  251. static void __team_options_change_check(struct team *team);
  252. int team_options_register(struct team *team,
  253. const struct team_option *option,
  254. size_t option_count)
  255. {
  256. int err;
  257. err = __team_options_register(team, option, option_count);
  258. if (err)
  259. return err;
  260. __team_options_change_check(team);
  261. return 0;
  262. }
  263. EXPORT_SYMBOL(team_options_register);
  264. void team_options_unregister(struct team *team,
  265. const struct team_option *option,
  266. size_t option_count)
  267. {
  268. __team_options_mark_removed(team, option, option_count);
  269. __team_options_change_check(team);
  270. __team_options_unregister(team, option, option_count);
  271. }
  272. EXPORT_SYMBOL(team_options_unregister);
  273. static int team_option_port_add(struct team *team, struct team_port *port)
  274. {
  275. int err;
  276. err = __team_option_inst_add_port(team, port);
  277. if (err)
  278. return err;
  279. __team_options_change_check(team);
  280. return 0;
  281. }
  282. static void team_option_port_del(struct team *team, struct team_port *port)
  283. {
  284. __team_option_inst_mark_removed_port(team, port);
  285. __team_options_change_check(team);
  286. __team_option_inst_del_port(team, port);
  287. }
  288. static int team_option_get(struct team *team,
  289. struct team_option_inst *opt_inst,
  290. struct team_gsetter_ctx *ctx)
  291. {
  292. return opt_inst->option->getter(team, ctx);
  293. }
  294. static int team_option_set(struct team *team,
  295. struct team_option_inst *opt_inst,
  296. struct team_gsetter_ctx *ctx)
  297. {
  298. int err;
  299. err = opt_inst->option->setter(team, ctx);
  300. if (err)
  301. return err;
  302. opt_inst->changed = true;
  303. __team_options_change_check(team);
  304. return err;
  305. }
  306. /****************
  307. * Mode handling
  308. ****************/
  309. static LIST_HEAD(mode_list);
  310. static DEFINE_SPINLOCK(mode_list_lock);
  311. struct team_mode_item {
  312. struct list_head list;
  313. const struct team_mode *mode;
  314. };
  315. static struct team_mode_item *__find_mode(const char *kind)
  316. {
  317. struct team_mode_item *mitem;
  318. list_for_each_entry(mitem, &mode_list, list) {
  319. if (strcmp(mitem->mode->kind, kind) == 0)
  320. return mitem;
  321. }
  322. return NULL;
  323. }
  324. static bool is_good_mode_name(const char *name)
  325. {
  326. while (*name != '\0') {
  327. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  328. return false;
  329. name++;
  330. }
  331. return true;
  332. }
  333. int team_mode_register(const struct team_mode *mode)
  334. {
  335. int err = 0;
  336. struct team_mode_item *mitem;
  337. if (!is_good_mode_name(mode->kind) ||
  338. mode->priv_size > TEAM_MODE_PRIV_SIZE)
  339. return -EINVAL;
  340. mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
  341. if (!mitem)
  342. return -ENOMEM;
  343. spin_lock(&mode_list_lock);
  344. if (__find_mode(mode->kind)) {
  345. err = -EEXIST;
  346. kfree(mitem);
  347. goto unlock;
  348. }
  349. mitem->mode = mode;
  350. list_add_tail(&mitem->list, &mode_list);
  351. unlock:
  352. spin_unlock(&mode_list_lock);
  353. return err;
  354. }
  355. EXPORT_SYMBOL(team_mode_register);
  356. void team_mode_unregister(const struct team_mode *mode)
  357. {
  358. struct team_mode_item *mitem;
  359. spin_lock(&mode_list_lock);
  360. mitem = __find_mode(mode->kind);
  361. if (mitem) {
  362. list_del_init(&mitem->list);
  363. kfree(mitem);
  364. }
  365. spin_unlock(&mode_list_lock);
  366. }
  367. EXPORT_SYMBOL(team_mode_unregister);
  368. static const struct team_mode *team_mode_get(const char *kind)
  369. {
  370. struct team_mode_item *mitem;
  371. const struct team_mode *mode = NULL;
  372. spin_lock(&mode_list_lock);
  373. mitem = __find_mode(kind);
  374. if (!mitem) {
  375. spin_unlock(&mode_list_lock);
  376. request_module("team-mode-%s", kind);
  377. spin_lock(&mode_list_lock);
  378. mitem = __find_mode(kind);
  379. }
  380. if (mitem) {
  381. mode = mitem->mode;
  382. if (!try_module_get(mode->owner))
  383. mode = NULL;
  384. }
  385. spin_unlock(&mode_list_lock);
  386. return mode;
  387. }
  388. static void team_mode_put(const struct team_mode *mode)
  389. {
  390. module_put(mode->owner);
  391. }
  392. static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
  393. {
  394. dev_kfree_skb_any(skb);
  395. return false;
  396. }
  397. rx_handler_result_t team_dummy_receive(struct team *team,
  398. struct team_port *port,
  399. struct sk_buff *skb)
  400. {
  401. return RX_HANDLER_ANOTHER;
  402. }
  403. static const struct team_mode __team_no_mode = {
  404. .kind = "*NOMODE*",
  405. };
  406. static bool team_is_mode_set(struct team *team)
  407. {
  408. return team->mode != &__team_no_mode;
  409. }
  410. static void team_set_no_mode(struct team *team)
  411. {
  412. team->mode = &__team_no_mode;
  413. }
  414. static void team_adjust_ops(struct team *team)
  415. {
  416. /*
  417. * To avoid checks in rx/tx skb paths, ensure here that non-null and
  418. * correct ops are always set.
  419. */
  420. if (list_empty(&team->port_list) ||
  421. !team_is_mode_set(team) || !team->mode->ops->transmit)
  422. team->ops.transmit = team_dummy_transmit;
  423. else
  424. team->ops.transmit = team->mode->ops->transmit;
  425. if (list_empty(&team->port_list) ||
  426. !team_is_mode_set(team) || !team->mode->ops->receive)
  427. team->ops.receive = team_dummy_receive;
  428. else
  429. team->ops.receive = team->mode->ops->receive;
  430. }
  431. /*
  432. * We can benefit from the fact that it's ensured no port is present
  433. * at the time of mode change. Therefore no packets are in fly so there's no
  434. * need to set mode operations in any special way.
  435. */
  436. static int __team_change_mode(struct team *team,
  437. const struct team_mode *new_mode)
  438. {
  439. /* Check if mode was previously set and do cleanup if so */
  440. if (team_is_mode_set(team)) {
  441. void (*exit_op)(struct team *team) = team->ops.exit;
  442. /* Clear ops area so no callback is called any longer */
  443. memset(&team->ops, 0, sizeof(struct team_mode_ops));
  444. team_adjust_ops(team);
  445. if (exit_op)
  446. exit_op(team);
  447. team_mode_put(team->mode);
  448. team_set_no_mode(team);
  449. /* zero private data area */
  450. memset(&team->mode_priv, 0,
  451. sizeof(struct team) - offsetof(struct team, mode_priv));
  452. }
  453. if (!new_mode)
  454. return 0;
  455. if (new_mode->ops->init) {
  456. int err;
  457. err = new_mode->ops->init(team);
  458. if (err)
  459. return err;
  460. }
  461. team->mode = new_mode;
  462. memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
  463. team_adjust_ops(team);
  464. return 0;
  465. }
  466. static int team_change_mode(struct team *team, const char *kind)
  467. {
  468. const struct team_mode *new_mode;
  469. struct net_device *dev = team->dev;
  470. int err;
  471. if (!list_empty(&team->port_list)) {
  472. netdev_err(dev, "No ports can be present during mode change\n");
  473. return -EBUSY;
  474. }
  475. if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
  476. netdev_err(dev, "Unable to change to the same mode the team is in\n");
  477. return -EINVAL;
  478. }
  479. new_mode = team_mode_get(kind);
  480. if (!new_mode) {
  481. netdev_err(dev, "Mode \"%s\" not found\n", kind);
  482. return -EINVAL;
  483. }
  484. err = __team_change_mode(team, new_mode);
  485. if (err) {
  486. netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
  487. team_mode_put(new_mode);
  488. return err;
  489. }
  490. netdev_info(dev, "Mode changed to \"%s\"\n", kind);
  491. return 0;
  492. }
  493. /************************
  494. * Rx path frame handler
  495. ************************/
  496. static bool team_port_enabled(struct team_port *port);
  497. /* note: already called with rcu_read_lock */
  498. static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
  499. {
  500. struct sk_buff *skb = *pskb;
  501. struct team_port *port;
  502. struct team *team;
  503. rx_handler_result_t res;
  504. skb = skb_share_check(skb, GFP_ATOMIC);
  505. if (!skb)
  506. return RX_HANDLER_CONSUMED;
  507. *pskb = skb;
  508. port = team_port_get_rcu(skb->dev);
  509. team = port->team;
  510. if (!team_port_enabled(port)) {
  511. /* allow exact match delivery for disabled ports */
  512. res = RX_HANDLER_EXACT;
  513. } else {
  514. res = team->ops.receive(team, port, skb);
  515. }
  516. if (res == RX_HANDLER_ANOTHER) {
  517. struct team_pcpu_stats *pcpu_stats;
  518. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  519. u64_stats_update_begin(&pcpu_stats->syncp);
  520. pcpu_stats->rx_packets++;
  521. pcpu_stats->rx_bytes += skb->len;
  522. if (skb->pkt_type == PACKET_MULTICAST)
  523. pcpu_stats->rx_multicast++;
  524. u64_stats_update_end(&pcpu_stats->syncp);
  525. skb->dev = team->dev;
  526. } else {
  527. this_cpu_inc(team->pcpu_stats->rx_dropped);
  528. }
  529. return res;
  530. }
  531. /****************
  532. * Port handling
  533. ****************/
  534. static bool team_port_find(const struct team *team,
  535. const struct team_port *port)
  536. {
  537. struct team_port *cur;
  538. list_for_each_entry(cur, &team->port_list, list)
  539. if (cur == port)
  540. return true;
  541. return false;
  542. }
  543. static bool team_port_enabled(struct team_port *port)
  544. {
  545. return port->index != -1;
  546. }
  547. /*
  548. * Enable/disable port by adding to enabled port hashlist and setting
  549. * port->index (Might be racy so reader could see incorrect ifindex when
  550. * processing a flying packet, but that is not a problem). Write guarded
  551. * by team->lock.
  552. */
  553. static void team_port_enable(struct team *team,
  554. struct team_port *port)
  555. {
  556. if (team_port_enabled(port))
  557. return;
  558. port->index = team->en_port_count++;
  559. hlist_add_head_rcu(&port->hlist,
  560. team_port_index_hash(team, port->index));
  561. }
  562. static void __reconstruct_port_hlist(struct team *team, int rm_index)
  563. {
  564. int i;
  565. struct team_port *port;
  566. for (i = rm_index + 1; i < team->en_port_count; i++) {
  567. port = team_get_port_by_index(team, i);
  568. hlist_del_rcu(&port->hlist);
  569. port->index--;
  570. hlist_add_head_rcu(&port->hlist,
  571. team_port_index_hash(team, port->index));
  572. }
  573. }
  574. static void team_port_disable(struct team *team,
  575. struct team_port *port)
  576. {
  577. int rm_index = port->index;
  578. if (!team_port_enabled(port))
  579. return;
  580. hlist_del_rcu(&port->hlist);
  581. __reconstruct_port_hlist(team, rm_index);
  582. team->en_port_count--;
  583. port->index = -1;
  584. }
  585. #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
  586. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  587. NETIF_F_HIGHDMA | NETIF_F_LRO)
  588. static void __team_compute_features(struct team *team)
  589. {
  590. struct team_port *port;
  591. u32 vlan_features = TEAM_VLAN_FEATURES;
  592. unsigned short max_hard_header_len = ETH_HLEN;
  593. list_for_each_entry(port, &team->port_list, list) {
  594. vlan_features = netdev_increment_features(vlan_features,
  595. port->dev->vlan_features,
  596. TEAM_VLAN_FEATURES);
  597. if (port->dev->hard_header_len > max_hard_header_len)
  598. max_hard_header_len = port->dev->hard_header_len;
  599. }
  600. team->dev->vlan_features = vlan_features;
  601. team->dev->hard_header_len = max_hard_header_len;
  602. netdev_change_features(team->dev);
  603. }
  604. static void team_compute_features(struct team *team)
  605. {
  606. mutex_lock(&team->lock);
  607. __team_compute_features(team);
  608. mutex_unlock(&team->lock);
  609. }
  610. static int team_port_enter(struct team *team, struct team_port *port)
  611. {
  612. int err = 0;
  613. dev_hold(team->dev);
  614. port->dev->priv_flags |= IFF_TEAM_PORT;
  615. if (team->ops.port_enter) {
  616. err = team->ops.port_enter(team, port);
  617. if (err) {
  618. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  619. port->dev->name);
  620. goto err_port_enter;
  621. }
  622. }
  623. return 0;
  624. err_port_enter:
  625. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  626. dev_put(team->dev);
  627. return err;
  628. }
  629. static void team_port_leave(struct team *team, struct team_port *port)
  630. {
  631. if (team->ops.port_leave)
  632. team->ops.port_leave(team, port);
  633. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  634. dev_put(team->dev);
  635. }
  636. static void __team_port_change_check(struct team_port *port, bool linkup);
  637. static int team_port_add(struct team *team, struct net_device *port_dev)
  638. {
  639. struct net_device *dev = team->dev;
  640. struct team_port *port;
  641. char *portname = port_dev->name;
  642. int err;
  643. if (port_dev->flags & IFF_LOOPBACK ||
  644. port_dev->type != ARPHRD_ETHER) {
  645. netdev_err(dev, "Device %s is of an unsupported type\n",
  646. portname);
  647. return -EINVAL;
  648. }
  649. if (team_port_exists(port_dev)) {
  650. netdev_err(dev, "Device %s is already a port "
  651. "of a team device\n", portname);
  652. return -EBUSY;
  653. }
  654. if (port_dev->flags & IFF_UP) {
  655. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  656. portname);
  657. return -EBUSY;
  658. }
  659. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  660. GFP_KERNEL);
  661. if (!port)
  662. return -ENOMEM;
  663. port->dev = port_dev;
  664. port->team = team;
  665. port->orig.mtu = port_dev->mtu;
  666. err = dev_set_mtu(port_dev, dev->mtu);
  667. if (err) {
  668. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  669. goto err_set_mtu;
  670. }
  671. memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
  672. err = team_port_enter(team, port);
  673. if (err) {
  674. netdev_err(dev, "Device %s failed to enter team mode\n",
  675. portname);
  676. goto err_port_enter;
  677. }
  678. err = dev_open(port_dev);
  679. if (err) {
  680. netdev_dbg(dev, "Device %s opening failed\n",
  681. portname);
  682. goto err_dev_open;
  683. }
  684. err = vlan_vids_add_by_dev(port_dev, dev);
  685. if (err) {
  686. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  687. portname);
  688. goto err_vids_add;
  689. }
  690. err = netdev_set_master(port_dev, dev);
  691. if (err) {
  692. netdev_err(dev, "Device %s failed to set master\n", portname);
  693. goto err_set_master;
  694. }
  695. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  696. port);
  697. if (err) {
  698. netdev_err(dev, "Device %s failed to register rx_handler\n",
  699. portname);
  700. goto err_handler_register;
  701. }
  702. err = team_option_port_add(team, port);
  703. if (err) {
  704. netdev_err(dev, "Device %s failed to add per-port options\n",
  705. portname);
  706. goto err_option_port_add;
  707. }
  708. port->index = -1;
  709. team_port_enable(team, port);
  710. list_add_tail_rcu(&port->list, &team->port_list);
  711. team_adjust_ops(team);
  712. __team_compute_features(team);
  713. __team_port_change_check(port, !!netif_carrier_ok(port_dev));
  714. netdev_info(dev, "Port device %s added\n", portname);
  715. return 0;
  716. err_option_port_add:
  717. netdev_rx_handler_unregister(port_dev);
  718. err_handler_register:
  719. netdev_set_master(port_dev, NULL);
  720. err_set_master:
  721. vlan_vids_del_by_dev(port_dev, dev);
  722. err_vids_add:
  723. dev_close(port_dev);
  724. err_dev_open:
  725. team_port_leave(team, port);
  726. team_port_set_orig_mac(port);
  727. err_port_enter:
  728. dev_set_mtu(port_dev, port->orig.mtu);
  729. err_set_mtu:
  730. kfree(port);
  731. return err;
  732. }
  733. static int team_port_del(struct team *team, struct net_device *port_dev)
  734. {
  735. struct net_device *dev = team->dev;
  736. struct team_port *port;
  737. char *portname = port_dev->name;
  738. port = team_port_get_rtnl(port_dev);
  739. if (!port || !team_port_find(team, port)) {
  740. netdev_err(dev, "Device %s does not act as a port of this team\n",
  741. portname);
  742. return -ENOENT;
  743. }
  744. port->removed = true;
  745. __team_port_change_check(port, false);
  746. team_port_disable(team, port);
  747. list_del_rcu(&port->list);
  748. team_adjust_ops(team);
  749. team_option_port_del(team, port);
  750. netdev_rx_handler_unregister(port_dev);
  751. netdev_set_master(port_dev, NULL);
  752. vlan_vids_del_by_dev(port_dev, dev);
  753. dev_close(port_dev);
  754. team_port_leave(team, port);
  755. team_port_set_orig_mac(port);
  756. dev_set_mtu(port_dev, port->orig.mtu);
  757. synchronize_rcu();
  758. kfree(port);
  759. netdev_info(dev, "Port device %s removed\n", portname);
  760. __team_compute_features(team);
  761. return 0;
  762. }
  763. /*****************
  764. * Net device ops
  765. *****************/
  766. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  767. {
  768. ctx->data.str_val = team->mode->kind;
  769. return 0;
  770. }
  771. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  772. {
  773. return team_change_mode(team, ctx->data.str_val);
  774. }
  775. static int team_port_en_option_get(struct team *team,
  776. struct team_gsetter_ctx *ctx)
  777. {
  778. ctx->data.bool_val = team_port_enabled(ctx->port);
  779. return 0;
  780. }
  781. static int team_port_en_option_set(struct team *team,
  782. struct team_gsetter_ctx *ctx)
  783. {
  784. if (ctx->data.bool_val)
  785. team_port_enable(team, ctx->port);
  786. else
  787. team_port_disable(team, ctx->port);
  788. return 0;
  789. }
  790. static int team_user_linkup_option_get(struct team *team,
  791. struct team_gsetter_ctx *ctx)
  792. {
  793. ctx->data.bool_val = ctx->port->user.linkup;
  794. return 0;
  795. }
  796. static int team_user_linkup_option_set(struct team *team,
  797. struct team_gsetter_ctx *ctx)
  798. {
  799. ctx->port->user.linkup = ctx->data.bool_val;
  800. team_refresh_port_linkup(ctx->port);
  801. return 0;
  802. }
  803. static int team_user_linkup_en_option_get(struct team *team,
  804. struct team_gsetter_ctx *ctx)
  805. {
  806. struct team_port *port = ctx->port;
  807. ctx->data.bool_val = port->user.linkup_enabled;
  808. return 0;
  809. }
  810. static int team_user_linkup_en_option_set(struct team *team,
  811. struct team_gsetter_ctx *ctx)
  812. {
  813. struct team_port *port = ctx->port;
  814. port->user.linkup_enabled = ctx->data.bool_val;
  815. team_refresh_port_linkup(ctx->port);
  816. return 0;
  817. }
  818. static const struct team_option team_options[] = {
  819. {
  820. .name = "mode",
  821. .type = TEAM_OPTION_TYPE_STRING,
  822. .getter = team_mode_option_get,
  823. .setter = team_mode_option_set,
  824. },
  825. {
  826. .name = "enabled",
  827. .type = TEAM_OPTION_TYPE_BOOL,
  828. .per_port = true,
  829. .getter = team_port_en_option_get,
  830. .setter = team_port_en_option_set,
  831. },
  832. {
  833. .name = "user_linkup",
  834. .type = TEAM_OPTION_TYPE_BOOL,
  835. .per_port = true,
  836. .getter = team_user_linkup_option_get,
  837. .setter = team_user_linkup_option_set,
  838. },
  839. {
  840. .name = "user_linkup_enabled",
  841. .type = TEAM_OPTION_TYPE_BOOL,
  842. .per_port = true,
  843. .getter = team_user_linkup_en_option_get,
  844. .setter = team_user_linkup_en_option_set,
  845. },
  846. };
  847. static int team_init(struct net_device *dev)
  848. {
  849. struct team *team = netdev_priv(dev);
  850. int i;
  851. int err;
  852. team->dev = dev;
  853. mutex_init(&team->lock);
  854. team_set_no_mode(team);
  855. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  856. if (!team->pcpu_stats)
  857. return -ENOMEM;
  858. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  859. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  860. INIT_LIST_HEAD(&team->port_list);
  861. team_adjust_ops(team);
  862. INIT_LIST_HEAD(&team->option_list);
  863. INIT_LIST_HEAD(&team->option_inst_list);
  864. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  865. if (err)
  866. goto err_options_register;
  867. netif_carrier_off(dev);
  868. return 0;
  869. err_options_register:
  870. free_percpu(team->pcpu_stats);
  871. return err;
  872. }
  873. static void team_uninit(struct net_device *dev)
  874. {
  875. struct team *team = netdev_priv(dev);
  876. struct team_port *port;
  877. struct team_port *tmp;
  878. mutex_lock(&team->lock);
  879. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  880. team_port_del(team, port->dev);
  881. __team_change_mode(team, NULL); /* cleanup */
  882. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  883. mutex_unlock(&team->lock);
  884. }
  885. static void team_destructor(struct net_device *dev)
  886. {
  887. struct team *team = netdev_priv(dev);
  888. free_percpu(team->pcpu_stats);
  889. free_netdev(dev);
  890. }
  891. static int team_open(struct net_device *dev)
  892. {
  893. netif_carrier_on(dev);
  894. return 0;
  895. }
  896. static int team_close(struct net_device *dev)
  897. {
  898. netif_carrier_off(dev);
  899. return 0;
  900. }
  901. /*
  902. * note: already called with rcu_read_lock
  903. */
  904. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  905. {
  906. struct team *team = netdev_priv(dev);
  907. bool tx_success = false;
  908. unsigned int len = skb->len;
  909. tx_success = team->ops.transmit(team, skb);
  910. if (tx_success) {
  911. struct team_pcpu_stats *pcpu_stats;
  912. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  913. u64_stats_update_begin(&pcpu_stats->syncp);
  914. pcpu_stats->tx_packets++;
  915. pcpu_stats->tx_bytes += len;
  916. u64_stats_update_end(&pcpu_stats->syncp);
  917. } else {
  918. this_cpu_inc(team->pcpu_stats->tx_dropped);
  919. }
  920. return NETDEV_TX_OK;
  921. }
  922. static void team_change_rx_flags(struct net_device *dev, int change)
  923. {
  924. struct team *team = netdev_priv(dev);
  925. struct team_port *port;
  926. int inc;
  927. rcu_read_lock();
  928. list_for_each_entry_rcu(port, &team->port_list, list) {
  929. if (change & IFF_PROMISC) {
  930. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  931. dev_set_promiscuity(port->dev, inc);
  932. }
  933. if (change & IFF_ALLMULTI) {
  934. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  935. dev_set_allmulti(port->dev, inc);
  936. }
  937. }
  938. rcu_read_unlock();
  939. }
  940. static void team_set_rx_mode(struct net_device *dev)
  941. {
  942. struct team *team = netdev_priv(dev);
  943. struct team_port *port;
  944. rcu_read_lock();
  945. list_for_each_entry_rcu(port, &team->port_list, list) {
  946. dev_uc_sync(port->dev, dev);
  947. dev_mc_sync(port->dev, dev);
  948. }
  949. rcu_read_unlock();
  950. }
  951. static int team_set_mac_address(struct net_device *dev, void *p)
  952. {
  953. struct team *team = netdev_priv(dev);
  954. struct team_port *port;
  955. struct sockaddr *addr = p;
  956. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  957. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  958. rcu_read_lock();
  959. list_for_each_entry_rcu(port, &team->port_list, list)
  960. if (team->ops.port_change_mac)
  961. team->ops.port_change_mac(team, port);
  962. rcu_read_unlock();
  963. return 0;
  964. }
  965. static int team_change_mtu(struct net_device *dev, int new_mtu)
  966. {
  967. struct team *team = netdev_priv(dev);
  968. struct team_port *port;
  969. int err;
  970. /*
  971. * Alhough this is reader, it's guarded by team lock. It's not possible
  972. * to traverse list in reverse under rcu_read_lock
  973. */
  974. mutex_lock(&team->lock);
  975. list_for_each_entry(port, &team->port_list, list) {
  976. err = dev_set_mtu(port->dev, new_mtu);
  977. if (err) {
  978. netdev_err(dev, "Device %s failed to change mtu",
  979. port->dev->name);
  980. goto unwind;
  981. }
  982. }
  983. mutex_unlock(&team->lock);
  984. dev->mtu = new_mtu;
  985. return 0;
  986. unwind:
  987. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  988. dev_set_mtu(port->dev, dev->mtu);
  989. mutex_unlock(&team->lock);
  990. return err;
  991. }
  992. static struct rtnl_link_stats64 *
  993. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  994. {
  995. struct team *team = netdev_priv(dev);
  996. struct team_pcpu_stats *p;
  997. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  998. u32 rx_dropped = 0, tx_dropped = 0;
  999. unsigned int start;
  1000. int i;
  1001. for_each_possible_cpu(i) {
  1002. p = per_cpu_ptr(team->pcpu_stats, i);
  1003. do {
  1004. start = u64_stats_fetch_begin_bh(&p->syncp);
  1005. rx_packets = p->rx_packets;
  1006. rx_bytes = p->rx_bytes;
  1007. rx_multicast = p->rx_multicast;
  1008. tx_packets = p->tx_packets;
  1009. tx_bytes = p->tx_bytes;
  1010. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  1011. stats->rx_packets += rx_packets;
  1012. stats->rx_bytes += rx_bytes;
  1013. stats->multicast += rx_multicast;
  1014. stats->tx_packets += tx_packets;
  1015. stats->tx_bytes += tx_bytes;
  1016. /*
  1017. * rx_dropped & tx_dropped are u32, updated
  1018. * without syncp protection.
  1019. */
  1020. rx_dropped += p->rx_dropped;
  1021. tx_dropped += p->tx_dropped;
  1022. }
  1023. stats->rx_dropped = rx_dropped;
  1024. stats->tx_dropped = tx_dropped;
  1025. return stats;
  1026. }
  1027. static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
  1028. {
  1029. struct team *team = netdev_priv(dev);
  1030. struct team_port *port;
  1031. int err;
  1032. /*
  1033. * Alhough this is reader, it's guarded by team lock. It's not possible
  1034. * to traverse list in reverse under rcu_read_lock
  1035. */
  1036. mutex_lock(&team->lock);
  1037. list_for_each_entry(port, &team->port_list, list) {
  1038. err = vlan_vid_add(port->dev, vid);
  1039. if (err)
  1040. goto unwind;
  1041. }
  1042. mutex_unlock(&team->lock);
  1043. return 0;
  1044. unwind:
  1045. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1046. vlan_vid_del(port->dev, vid);
  1047. mutex_unlock(&team->lock);
  1048. return err;
  1049. }
  1050. static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
  1051. {
  1052. struct team *team = netdev_priv(dev);
  1053. struct team_port *port;
  1054. rcu_read_lock();
  1055. list_for_each_entry_rcu(port, &team->port_list, list)
  1056. vlan_vid_del(port->dev, vid);
  1057. rcu_read_unlock();
  1058. return 0;
  1059. }
  1060. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1061. {
  1062. struct team *team = netdev_priv(dev);
  1063. int err;
  1064. mutex_lock(&team->lock);
  1065. err = team_port_add(team, port_dev);
  1066. mutex_unlock(&team->lock);
  1067. return err;
  1068. }
  1069. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1070. {
  1071. struct team *team = netdev_priv(dev);
  1072. int err;
  1073. mutex_lock(&team->lock);
  1074. err = team_port_del(team, port_dev);
  1075. mutex_unlock(&team->lock);
  1076. return err;
  1077. }
  1078. static netdev_features_t team_fix_features(struct net_device *dev,
  1079. netdev_features_t features)
  1080. {
  1081. struct team_port *port;
  1082. struct team *team = netdev_priv(dev);
  1083. netdev_features_t mask;
  1084. mask = features;
  1085. features &= ~NETIF_F_ONE_FOR_ALL;
  1086. features |= NETIF_F_ALL_FOR_ALL;
  1087. rcu_read_lock();
  1088. list_for_each_entry_rcu(port, &team->port_list, list) {
  1089. features = netdev_increment_features(features,
  1090. port->dev->features,
  1091. mask);
  1092. }
  1093. rcu_read_unlock();
  1094. return features;
  1095. }
  1096. static const struct net_device_ops team_netdev_ops = {
  1097. .ndo_init = team_init,
  1098. .ndo_uninit = team_uninit,
  1099. .ndo_open = team_open,
  1100. .ndo_stop = team_close,
  1101. .ndo_start_xmit = team_xmit,
  1102. .ndo_change_rx_flags = team_change_rx_flags,
  1103. .ndo_set_rx_mode = team_set_rx_mode,
  1104. .ndo_set_mac_address = team_set_mac_address,
  1105. .ndo_change_mtu = team_change_mtu,
  1106. .ndo_get_stats64 = team_get_stats64,
  1107. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1108. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1109. .ndo_add_slave = team_add_slave,
  1110. .ndo_del_slave = team_del_slave,
  1111. .ndo_fix_features = team_fix_features,
  1112. };
  1113. /***********************
  1114. * rt netlink interface
  1115. ***********************/
  1116. static void team_setup(struct net_device *dev)
  1117. {
  1118. ether_setup(dev);
  1119. dev->netdev_ops = &team_netdev_ops;
  1120. dev->destructor = team_destructor;
  1121. dev->tx_queue_len = 0;
  1122. dev->flags |= IFF_MULTICAST;
  1123. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1124. /*
  1125. * Indicate we support unicast address filtering. That way core won't
  1126. * bring us to promisc mode in case a unicast addr is added.
  1127. * Let this up to underlay drivers.
  1128. */
  1129. dev->priv_flags |= IFF_UNICAST_FLT;
  1130. dev->features |= NETIF_F_LLTX;
  1131. dev->features |= NETIF_F_GRO;
  1132. dev->hw_features = NETIF_F_HW_VLAN_TX |
  1133. NETIF_F_HW_VLAN_RX |
  1134. NETIF_F_HW_VLAN_FILTER;
  1135. dev->features |= dev->hw_features;
  1136. }
  1137. static int team_newlink(struct net *src_net, struct net_device *dev,
  1138. struct nlattr *tb[], struct nlattr *data[])
  1139. {
  1140. int err;
  1141. if (tb[IFLA_ADDRESS] == NULL)
  1142. eth_hw_addr_random(dev);
  1143. err = register_netdevice(dev);
  1144. if (err)
  1145. return err;
  1146. return 0;
  1147. }
  1148. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1149. {
  1150. if (tb[IFLA_ADDRESS]) {
  1151. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1152. return -EINVAL;
  1153. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1154. return -EADDRNOTAVAIL;
  1155. }
  1156. return 0;
  1157. }
  1158. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1159. .kind = DRV_NAME,
  1160. .priv_size = sizeof(struct team),
  1161. .setup = team_setup,
  1162. .newlink = team_newlink,
  1163. .validate = team_validate,
  1164. };
  1165. /***********************************
  1166. * Generic netlink custom interface
  1167. ***********************************/
  1168. static struct genl_family team_nl_family = {
  1169. .id = GENL_ID_GENERATE,
  1170. .name = TEAM_GENL_NAME,
  1171. .version = TEAM_GENL_VERSION,
  1172. .maxattr = TEAM_ATTR_MAX,
  1173. .netnsok = true,
  1174. };
  1175. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1176. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1177. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1178. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1179. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1180. };
  1181. static const struct nla_policy
  1182. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1183. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1184. [TEAM_ATTR_OPTION_NAME] = {
  1185. .type = NLA_STRING,
  1186. .len = TEAM_STRING_MAX_LEN,
  1187. },
  1188. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1189. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1190. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1191. };
  1192. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1193. {
  1194. struct sk_buff *msg;
  1195. void *hdr;
  1196. int err;
  1197. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1198. if (!msg)
  1199. return -ENOMEM;
  1200. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  1201. &team_nl_family, 0, TEAM_CMD_NOOP);
  1202. if (IS_ERR(hdr)) {
  1203. err = PTR_ERR(hdr);
  1204. goto err_msg_put;
  1205. }
  1206. genlmsg_end(msg, hdr);
  1207. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  1208. err_msg_put:
  1209. nlmsg_free(msg);
  1210. return err;
  1211. }
  1212. /*
  1213. * Netlink cmd functions should be locked by following two functions.
  1214. * Since dev gets held here, that ensures dev won't disappear in between.
  1215. */
  1216. static struct team *team_nl_team_get(struct genl_info *info)
  1217. {
  1218. struct net *net = genl_info_net(info);
  1219. int ifindex;
  1220. struct net_device *dev;
  1221. struct team *team;
  1222. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1223. return NULL;
  1224. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1225. dev = dev_get_by_index(net, ifindex);
  1226. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1227. if (dev)
  1228. dev_put(dev);
  1229. return NULL;
  1230. }
  1231. team = netdev_priv(dev);
  1232. mutex_lock(&team->lock);
  1233. return team;
  1234. }
  1235. static void team_nl_team_put(struct team *team)
  1236. {
  1237. mutex_unlock(&team->lock);
  1238. dev_put(team->dev);
  1239. }
  1240. static int team_nl_send_generic(struct genl_info *info, struct team *team,
  1241. int (*fill_func)(struct sk_buff *skb,
  1242. struct genl_info *info,
  1243. int flags, struct team *team))
  1244. {
  1245. struct sk_buff *skb;
  1246. int err;
  1247. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1248. if (!skb)
  1249. return -ENOMEM;
  1250. err = fill_func(skb, info, NLM_F_ACK, team);
  1251. if (err < 0)
  1252. goto err_fill;
  1253. err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
  1254. return err;
  1255. err_fill:
  1256. nlmsg_free(skb);
  1257. return err;
  1258. }
  1259. static int team_nl_fill_options_get(struct sk_buff *skb,
  1260. u32 pid, u32 seq, int flags,
  1261. struct team *team, bool fillall)
  1262. {
  1263. struct nlattr *option_list;
  1264. void *hdr;
  1265. struct team_option_inst *opt_inst;
  1266. int err;
  1267. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1268. TEAM_CMD_OPTIONS_GET);
  1269. if (IS_ERR(hdr))
  1270. return PTR_ERR(hdr);
  1271. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1272. goto nla_put_failure;
  1273. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1274. if (!option_list)
  1275. return -EMSGSIZE;
  1276. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  1277. struct nlattr *option_item;
  1278. struct team_option *option = opt_inst->option;
  1279. struct team_gsetter_ctx ctx;
  1280. /* Include only changed options if fill all mode is not on */
  1281. if (!fillall && !opt_inst->changed)
  1282. continue;
  1283. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1284. if (!option_item)
  1285. goto nla_put_failure;
  1286. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1287. goto nla_put_failure;
  1288. if (opt_inst->changed) {
  1289. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1290. goto nla_put_failure;
  1291. opt_inst->changed = false;
  1292. }
  1293. if (opt_inst->removed &&
  1294. nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1295. goto nla_put_failure;
  1296. if (opt_inst->port &&
  1297. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1298. opt_inst->port->dev->ifindex))
  1299. goto nla_put_failure;
  1300. ctx.port = opt_inst->port;
  1301. switch (option->type) {
  1302. case TEAM_OPTION_TYPE_U32:
  1303. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1304. goto nla_put_failure;
  1305. err = team_option_get(team, opt_inst, &ctx);
  1306. if (err)
  1307. goto errout;
  1308. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA,
  1309. ctx.data.u32_val))
  1310. goto nla_put_failure;
  1311. break;
  1312. case TEAM_OPTION_TYPE_STRING:
  1313. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1314. goto nla_put_failure;
  1315. err = team_option_get(team, opt_inst, &ctx);
  1316. if (err)
  1317. goto errout;
  1318. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1319. ctx.data.str_val))
  1320. goto nla_put_failure;
  1321. break;
  1322. case TEAM_OPTION_TYPE_BINARY:
  1323. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1324. goto nla_put_failure;
  1325. err = team_option_get(team, opt_inst, &ctx);
  1326. if (err)
  1327. goto errout;
  1328. if (nla_put(skb, TEAM_ATTR_OPTION_DATA,
  1329. ctx.data.bin_val.len, ctx.data.bin_val.ptr))
  1330. goto nla_put_failure;
  1331. break;
  1332. case TEAM_OPTION_TYPE_BOOL:
  1333. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1334. goto nla_put_failure;
  1335. err = team_option_get(team, opt_inst, &ctx);
  1336. if (err)
  1337. goto errout;
  1338. if (ctx.data.bool_val &&
  1339. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1340. goto nla_put_failure;
  1341. break;
  1342. default:
  1343. BUG();
  1344. }
  1345. nla_nest_end(skb, option_item);
  1346. }
  1347. nla_nest_end(skb, option_list);
  1348. return genlmsg_end(skb, hdr);
  1349. nla_put_failure:
  1350. err = -EMSGSIZE;
  1351. errout:
  1352. genlmsg_cancel(skb, hdr);
  1353. return err;
  1354. }
  1355. static int team_nl_fill_options_get_all(struct sk_buff *skb,
  1356. struct genl_info *info, int flags,
  1357. struct team *team)
  1358. {
  1359. return team_nl_fill_options_get(skb, info->snd_pid,
  1360. info->snd_seq, NLM_F_ACK,
  1361. team, true);
  1362. }
  1363. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1364. {
  1365. struct team *team;
  1366. int err;
  1367. team = team_nl_team_get(info);
  1368. if (!team)
  1369. return -EINVAL;
  1370. err = team_nl_send_generic(info, team, team_nl_fill_options_get_all);
  1371. team_nl_team_put(team);
  1372. return err;
  1373. }
  1374. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1375. {
  1376. struct team *team;
  1377. int err = 0;
  1378. int i;
  1379. struct nlattr *nl_option;
  1380. team = team_nl_team_get(info);
  1381. if (!team)
  1382. return -EINVAL;
  1383. err = -EINVAL;
  1384. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  1385. err = -EINVAL;
  1386. goto team_put;
  1387. }
  1388. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  1389. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  1390. struct nlattr *attr_port_ifindex;
  1391. struct nlattr *attr_data;
  1392. enum team_option_type opt_type;
  1393. int opt_port_ifindex = 0; /* != 0 for per-port options */
  1394. struct team_option_inst *opt_inst;
  1395. char *opt_name;
  1396. bool opt_found = false;
  1397. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  1398. err = -EINVAL;
  1399. goto team_put;
  1400. }
  1401. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  1402. nl_option, team_nl_option_policy);
  1403. if (err)
  1404. goto team_put;
  1405. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  1406. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  1407. err = -EINVAL;
  1408. goto team_put;
  1409. }
  1410. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  1411. case NLA_U32:
  1412. opt_type = TEAM_OPTION_TYPE_U32;
  1413. break;
  1414. case NLA_STRING:
  1415. opt_type = TEAM_OPTION_TYPE_STRING;
  1416. break;
  1417. case NLA_BINARY:
  1418. opt_type = TEAM_OPTION_TYPE_BINARY;
  1419. break;
  1420. case NLA_FLAG:
  1421. opt_type = TEAM_OPTION_TYPE_BOOL;
  1422. break;
  1423. default:
  1424. goto team_put;
  1425. }
  1426. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  1427. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  1428. err = -EINVAL;
  1429. goto team_put;
  1430. }
  1431. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  1432. attr_port_ifindex = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  1433. if (attr_port_ifindex)
  1434. opt_port_ifindex = nla_get_u32(attr_port_ifindex);
  1435. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  1436. struct team_option *option = opt_inst->option;
  1437. struct team_gsetter_ctx ctx;
  1438. int tmp_ifindex;
  1439. tmp_ifindex = opt_inst->port ?
  1440. opt_inst->port->dev->ifindex : 0;
  1441. if (option->type != opt_type ||
  1442. strcmp(option->name, opt_name) ||
  1443. tmp_ifindex != opt_port_ifindex)
  1444. continue;
  1445. opt_found = true;
  1446. ctx.port = opt_inst->port;
  1447. switch (opt_type) {
  1448. case TEAM_OPTION_TYPE_U32:
  1449. ctx.data.u32_val = nla_get_u32(attr_data);
  1450. break;
  1451. case TEAM_OPTION_TYPE_STRING:
  1452. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  1453. err = -EINVAL;
  1454. goto team_put;
  1455. }
  1456. ctx.data.str_val = nla_data(attr_data);
  1457. break;
  1458. case TEAM_OPTION_TYPE_BINARY:
  1459. ctx.data.bin_val.len = nla_len(attr_data);
  1460. ctx.data.bin_val.ptr = nla_data(attr_data);
  1461. break;
  1462. case TEAM_OPTION_TYPE_BOOL:
  1463. ctx.data.bool_val = attr_data ? true : false;
  1464. break;
  1465. default:
  1466. BUG();
  1467. }
  1468. err = team_option_set(team, opt_inst, &ctx);
  1469. if (err)
  1470. goto team_put;
  1471. }
  1472. if (!opt_found) {
  1473. err = -ENOENT;
  1474. goto team_put;
  1475. }
  1476. }
  1477. team_put:
  1478. team_nl_team_put(team);
  1479. return err;
  1480. }
  1481. static int team_nl_fill_port_list_get(struct sk_buff *skb,
  1482. u32 pid, u32 seq, int flags,
  1483. struct team *team,
  1484. bool fillall)
  1485. {
  1486. struct nlattr *port_list;
  1487. void *hdr;
  1488. struct team_port *port;
  1489. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1490. TEAM_CMD_PORT_LIST_GET);
  1491. if (IS_ERR(hdr))
  1492. return PTR_ERR(hdr);
  1493. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1494. goto nla_put_failure;
  1495. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  1496. if (!port_list)
  1497. return -EMSGSIZE;
  1498. list_for_each_entry(port, &team->port_list, list) {
  1499. struct nlattr *port_item;
  1500. /* Include only changed ports if fill all mode is not on */
  1501. if (!fillall && !port->changed)
  1502. continue;
  1503. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  1504. if (!port_item)
  1505. goto nla_put_failure;
  1506. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  1507. goto nla_put_failure;
  1508. if (port->changed) {
  1509. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  1510. goto nla_put_failure;
  1511. port->changed = false;
  1512. }
  1513. if ((port->removed &&
  1514. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  1515. (port->state.linkup &&
  1516. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  1517. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  1518. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  1519. goto nla_put_failure;
  1520. nla_nest_end(skb, port_item);
  1521. }
  1522. nla_nest_end(skb, port_list);
  1523. return genlmsg_end(skb, hdr);
  1524. nla_put_failure:
  1525. genlmsg_cancel(skb, hdr);
  1526. return -EMSGSIZE;
  1527. }
  1528. static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
  1529. struct genl_info *info, int flags,
  1530. struct team *team)
  1531. {
  1532. return team_nl_fill_port_list_get(skb, info->snd_pid,
  1533. info->snd_seq, NLM_F_ACK,
  1534. team, true);
  1535. }
  1536. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  1537. struct genl_info *info)
  1538. {
  1539. struct team *team;
  1540. int err;
  1541. team = team_nl_team_get(info);
  1542. if (!team)
  1543. return -EINVAL;
  1544. err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
  1545. team_nl_team_put(team);
  1546. return err;
  1547. }
  1548. static struct genl_ops team_nl_ops[] = {
  1549. {
  1550. .cmd = TEAM_CMD_NOOP,
  1551. .doit = team_nl_cmd_noop,
  1552. .policy = team_nl_policy,
  1553. },
  1554. {
  1555. .cmd = TEAM_CMD_OPTIONS_SET,
  1556. .doit = team_nl_cmd_options_set,
  1557. .policy = team_nl_policy,
  1558. .flags = GENL_ADMIN_PERM,
  1559. },
  1560. {
  1561. .cmd = TEAM_CMD_OPTIONS_GET,
  1562. .doit = team_nl_cmd_options_get,
  1563. .policy = team_nl_policy,
  1564. .flags = GENL_ADMIN_PERM,
  1565. },
  1566. {
  1567. .cmd = TEAM_CMD_PORT_LIST_GET,
  1568. .doit = team_nl_cmd_port_list_get,
  1569. .policy = team_nl_policy,
  1570. .flags = GENL_ADMIN_PERM,
  1571. },
  1572. };
  1573. static struct genl_multicast_group team_change_event_mcgrp = {
  1574. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  1575. };
  1576. static int team_nl_send_event_options_get(struct team *team)
  1577. {
  1578. struct sk_buff *skb;
  1579. int err;
  1580. struct net *net = dev_net(team->dev);
  1581. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1582. if (!skb)
  1583. return -ENOMEM;
  1584. err = team_nl_fill_options_get(skb, 0, 0, 0, team, false);
  1585. if (err < 0)
  1586. goto err_fill;
  1587. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1588. GFP_KERNEL);
  1589. return err;
  1590. err_fill:
  1591. nlmsg_free(skb);
  1592. return err;
  1593. }
  1594. static int team_nl_send_event_port_list_get(struct team *team)
  1595. {
  1596. struct sk_buff *skb;
  1597. int err;
  1598. struct net *net = dev_net(team->dev);
  1599. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1600. if (!skb)
  1601. return -ENOMEM;
  1602. err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
  1603. if (err < 0)
  1604. goto err_fill;
  1605. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1606. GFP_KERNEL);
  1607. return err;
  1608. err_fill:
  1609. nlmsg_free(skb);
  1610. return err;
  1611. }
  1612. static int team_nl_init(void)
  1613. {
  1614. int err;
  1615. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  1616. ARRAY_SIZE(team_nl_ops));
  1617. if (err)
  1618. return err;
  1619. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  1620. if (err)
  1621. goto err_change_event_grp_reg;
  1622. return 0;
  1623. err_change_event_grp_reg:
  1624. genl_unregister_family(&team_nl_family);
  1625. return err;
  1626. }
  1627. static void team_nl_fini(void)
  1628. {
  1629. genl_unregister_family(&team_nl_family);
  1630. }
  1631. /******************
  1632. * Change checkers
  1633. ******************/
  1634. static void __team_options_change_check(struct team *team)
  1635. {
  1636. int err;
  1637. err = team_nl_send_event_options_get(team);
  1638. if (err)
  1639. netdev_warn(team->dev, "Failed to send options change via netlink\n");
  1640. }
  1641. /* rtnl lock is held */
  1642. static void __team_port_change_check(struct team_port *port, bool linkup)
  1643. {
  1644. int err;
  1645. if (!port->removed && port->state.linkup == linkup)
  1646. return;
  1647. port->changed = true;
  1648. port->state.linkup = linkup;
  1649. team_refresh_port_linkup(port);
  1650. if (linkup) {
  1651. struct ethtool_cmd ecmd;
  1652. err = __ethtool_get_settings(port->dev, &ecmd);
  1653. if (!err) {
  1654. port->state.speed = ethtool_cmd_speed(&ecmd);
  1655. port->state.duplex = ecmd.duplex;
  1656. goto send_event;
  1657. }
  1658. }
  1659. port->state.speed = 0;
  1660. port->state.duplex = 0;
  1661. send_event:
  1662. err = team_nl_send_event_port_list_get(port->team);
  1663. if (err)
  1664. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
  1665. port->dev->name);
  1666. }
  1667. static void team_port_change_check(struct team_port *port, bool linkup)
  1668. {
  1669. struct team *team = port->team;
  1670. mutex_lock(&team->lock);
  1671. __team_port_change_check(port, linkup);
  1672. mutex_unlock(&team->lock);
  1673. }
  1674. /************************************
  1675. * Net device notifier event handler
  1676. ************************************/
  1677. static int team_device_event(struct notifier_block *unused,
  1678. unsigned long event, void *ptr)
  1679. {
  1680. struct net_device *dev = (struct net_device *) ptr;
  1681. struct team_port *port;
  1682. port = team_port_get_rtnl(dev);
  1683. if (!port)
  1684. return NOTIFY_DONE;
  1685. switch (event) {
  1686. case NETDEV_UP:
  1687. if (netif_carrier_ok(dev))
  1688. team_port_change_check(port, true);
  1689. case NETDEV_DOWN:
  1690. team_port_change_check(port, false);
  1691. case NETDEV_CHANGE:
  1692. if (netif_running(port->dev))
  1693. team_port_change_check(port,
  1694. !!netif_carrier_ok(port->dev));
  1695. break;
  1696. case NETDEV_UNREGISTER:
  1697. team_del_slave(port->team->dev, dev);
  1698. break;
  1699. case NETDEV_FEAT_CHANGE:
  1700. team_compute_features(port->team);
  1701. break;
  1702. case NETDEV_CHANGEMTU:
  1703. /* Forbid to change mtu of underlaying device */
  1704. return NOTIFY_BAD;
  1705. case NETDEV_PRE_TYPE_CHANGE:
  1706. /* Forbid to change type of underlaying device */
  1707. return NOTIFY_BAD;
  1708. }
  1709. return NOTIFY_DONE;
  1710. }
  1711. static struct notifier_block team_notifier_block __read_mostly = {
  1712. .notifier_call = team_device_event,
  1713. };
  1714. /***********************
  1715. * Module init and exit
  1716. ***********************/
  1717. static int __init team_module_init(void)
  1718. {
  1719. int err;
  1720. register_netdevice_notifier(&team_notifier_block);
  1721. err = rtnl_link_register(&team_link_ops);
  1722. if (err)
  1723. goto err_rtnl_reg;
  1724. err = team_nl_init();
  1725. if (err)
  1726. goto err_nl_init;
  1727. return 0;
  1728. err_nl_init:
  1729. rtnl_link_unregister(&team_link_ops);
  1730. err_rtnl_reg:
  1731. unregister_netdevice_notifier(&team_notifier_block);
  1732. return err;
  1733. }
  1734. static void __exit team_module_exit(void)
  1735. {
  1736. team_nl_fini();
  1737. rtnl_link_unregister(&team_link_ops);
  1738. unregister_netdevice_notifier(&team_notifier_block);
  1739. }
  1740. module_init(team_module_init);
  1741. module_exit(team_module_exit);
  1742. MODULE_LICENSE("GPL v2");
  1743. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  1744. MODULE_DESCRIPTION("Ethernet team device driver");
  1745. MODULE_ALIAS_RTNL_LINK(DRV_NAME);