team.c 49 KB

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