team.c 49 KB

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