team.c 51 KB

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