team.c 51 KB

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