team.c 56 KB

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