team.c 46 KB

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