team.c 49 KB

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