team.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  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. * Multiqueue Tx port select override
  551. *************************************/
  552. static int team_queue_override_init(struct team *team)
  553. {
  554. struct list_head *listarr;
  555. unsigned int queue_cnt = team->dev->num_tx_queues - 1;
  556. unsigned int i;
  557. if (!queue_cnt)
  558. return 0;
  559. listarr = kmalloc(sizeof(struct list_head) * queue_cnt, GFP_KERNEL);
  560. if (!listarr)
  561. return -ENOMEM;
  562. team->qom_lists = listarr;
  563. for (i = 0; i < queue_cnt; i++)
  564. INIT_LIST_HEAD(listarr++);
  565. return 0;
  566. }
  567. static void team_queue_override_fini(struct team *team)
  568. {
  569. kfree(team->qom_lists);
  570. }
  571. static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
  572. {
  573. return &team->qom_lists[queue_id - 1];
  574. }
  575. /*
  576. * note: already called with rcu_read_lock
  577. */
  578. static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
  579. {
  580. struct list_head *qom_list;
  581. struct team_port *port;
  582. if (!team->queue_override_enabled || !skb->queue_mapping)
  583. return false;
  584. qom_list = __team_get_qom_list(team, skb->queue_mapping);
  585. list_for_each_entry_rcu(port, qom_list, qom_list) {
  586. if (!team_dev_queue_xmit(team, port, skb))
  587. return true;
  588. }
  589. return false;
  590. }
  591. static void __team_queue_override_port_del(struct team *team,
  592. struct team_port *port)
  593. {
  594. list_del_rcu(&port->qom_list);
  595. synchronize_rcu();
  596. INIT_LIST_HEAD(&port->qom_list);
  597. }
  598. static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
  599. struct team_port *cur)
  600. {
  601. if (port->priority < cur->priority)
  602. return true;
  603. if (port->priority > cur->priority)
  604. return false;
  605. if (port->index < cur->index)
  606. return true;
  607. return false;
  608. }
  609. static void __team_queue_override_port_add(struct team *team,
  610. struct team_port *port)
  611. {
  612. struct team_port *cur;
  613. struct list_head *qom_list;
  614. struct list_head *node;
  615. if (!port->queue_id || !team_port_enabled(port))
  616. return;
  617. qom_list = __team_get_qom_list(team, port->queue_id);
  618. node = qom_list;
  619. list_for_each_entry(cur, qom_list, qom_list) {
  620. if (team_queue_override_port_has_gt_prio_than(port, cur))
  621. break;
  622. node = &cur->qom_list;
  623. }
  624. list_add_tail_rcu(&port->qom_list, node);
  625. }
  626. static void __team_queue_override_enabled_check(struct team *team)
  627. {
  628. struct team_port *port;
  629. bool enabled = false;
  630. list_for_each_entry(port, &team->port_list, list) {
  631. if (!list_empty(&port->qom_list)) {
  632. enabled = true;
  633. break;
  634. }
  635. }
  636. if (enabled == team->queue_override_enabled)
  637. return;
  638. netdev_dbg(team->dev, "%s queue override\n",
  639. enabled ? "Enabling" : "Disabling");
  640. team->queue_override_enabled = enabled;
  641. }
  642. static void team_queue_override_port_refresh(struct team *team,
  643. struct team_port *port)
  644. {
  645. __team_queue_override_port_del(team, port);
  646. __team_queue_override_port_add(team, port);
  647. __team_queue_override_enabled_check(team);
  648. }
  649. /****************
  650. * Port handling
  651. ****************/
  652. static bool team_port_find(const struct team *team,
  653. const struct team_port *port)
  654. {
  655. struct team_port *cur;
  656. list_for_each_entry(cur, &team->port_list, list)
  657. if (cur == port)
  658. return true;
  659. return false;
  660. }
  661. /*
  662. * Enable/disable port by adding to enabled port hashlist and setting
  663. * port->index (Might be racy so reader could see incorrect ifindex when
  664. * processing a flying packet, but that is not a problem). Write guarded
  665. * by team->lock.
  666. */
  667. static void team_port_enable(struct team *team,
  668. struct team_port *port)
  669. {
  670. if (team_port_enabled(port))
  671. return;
  672. port->index = team->en_port_count++;
  673. hlist_add_head_rcu(&port->hlist,
  674. team_port_index_hash(team, port->index));
  675. team_adjust_ops(team);
  676. team_queue_override_port_refresh(team, port);
  677. if (team->ops.port_enabled)
  678. team->ops.port_enabled(team, port);
  679. }
  680. static void __reconstruct_port_hlist(struct team *team, int rm_index)
  681. {
  682. int i;
  683. struct team_port *port;
  684. for (i = rm_index + 1; i < team->en_port_count; i++) {
  685. port = team_get_port_by_index(team, i);
  686. hlist_del_rcu(&port->hlist);
  687. port->index--;
  688. hlist_add_head_rcu(&port->hlist,
  689. team_port_index_hash(team, port->index));
  690. }
  691. }
  692. static void team_port_disable(struct team *team,
  693. struct team_port *port)
  694. {
  695. if (!team_port_enabled(port))
  696. return;
  697. if (team->ops.port_disabled)
  698. team->ops.port_disabled(team, port);
  699. hlist_del_rcu(&port->hlist);
  700. __reconstruct_port_hlist(team, port->index);
  701. port->index = -1;
  702. team_queue_override_port_refresh(team, port);
  703. __team_adjust_ops(team, team->en_port_count - 1);
  704. /*
  705. * Wait until readers see adjusted ops. This ensures that
  706. * readers never see team->en_port_count == 0
  707. */
  708. synchronize_rcu();
  709. team->en_port_count--;
  710. }
  711. #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
  712. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  713. NETIF_F_HIGHDMA | NETIF_F_LRO)
  714. static void __team_compute_features(struct team *team)
  715. {
  716. struct team_port *port;
  717. u32 vlan_features = TEAM_VLAN_FEATURES;
  718. unsigned short max_hard_header_len = ETH_HLEN;
  719. unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
  720. list_for_each_entry(port, &team->port_list, list) {
  721. vlan_features = netdev_increment_features(vlan_features,
  722. port->dev->vlan_features,
  723. TEAM_VLAN_FEATURES);
  724. dst_release_flag &= port->dev->priv_flags;
  725. if (port->dev->hard_header_len > max_hard_header_len)
  726. max_hard_header_len = port->dev->hard_header_len;
  727. }
  728. team->dev->vlan_features = vlan_features;
  729. team->dev->hard_header_len = max_hard_header_len;
  730. flags = team->dev->priv_flags & ~IFF_XMIT_DST_RELEASE;
  731. team->dev->priv_flags = flags | dst_release_flag;
  732. netdev_change_features(team->dev);
  733. }
  734. static void team_compute_features(struct team *team)
  735. {
  736. mutex_lock(&team->lock);
  737. __team_compute_features(team);
  738. mutex_unlock(&team->lock);
  739. }
  740. static int team_port_enter(struct team *team, struct team_port *port)
  741. {
  742. int err = 0;
  743. dev_hold(team->dev);
  744. port->dev->priv_flags |= IFF_TEAM_PORT;
  745. if (team->ops.port_enter) {
  746. err = team->ops.port_enter(team, port);
  747. if (err) {
  748. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  749. port->dev->name);
  750. goto err_port_enter;
  751. }
  752. }
  753. return 0;
  754. err_port_enter:
  755. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  756. dev_put(team->dev);
  757. return err;
  758. }
  759. static void team_port_leave(struct team *team, struct team_port *port)
  760. {
  761. if (team->ops.port_leave)
  762. team->ops.port_leave(team, port);
  763. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  764. dev_put(team->dev);
  765. }
  766. #ifdef CONFIG_NET_POLL_CONTROLLER
  767. static int team_port_enable_netpoll(struct team *team, struct team_port *port)
  768. {
  769. struct netpoll *np;
  770. int err;
  771. np = kzalloc(sizeof(*np), GFP_KERNEL);
  772. if (!np)
  773. return -ENOMEM;
  774. err = __netpoll_setup(np, port->dev);
  775. if (err) {
  776. kfree(np);
  777. return err;
  778. }
  779. port->np = np;
  780. return err;
  781. }
  782. static void team_port_disable_netpoll(struct team_port *port)
  783. {
  784. struct netpoll *np = port->np;
  785. if (!np)
  786. return;
  787. port->np = NULL;
  788. /* Wait for transmitting packets to finish before freeing. */
  789. synchronize_rcu_bh();
  790. __netpoll_cleanup(np);
  791. kfree(np);
  792. }
  793. static struct netpoll_info *team_netpoll_info(struct team *team)
  794. {
  795. return team->dev->npinfo;
  796. }
  797. #else
  798. static int team_port_enable_netpoll(struct team *team, struct team_port *port)
  799. {
  800. return 0;
  801. }
  802. static void team_port_disable_netpoll(struct team_port *port)
  803. {
  804. }
  805. static struct netpoll_info *team_netpoll_info(struct team *team)
  806. {
  807. return NULL;
  808. }
  809. #endif
  810. static void __team_port_change_check(struct team_port *port, bool linkup);
  811. static int team_port_add(struct team *team, struct net_device *port_dev)
  812. {
  813. struct net_device *dev = team->dev;
  814. struct team_port *port;
  815. char *portname = port_dev->name;
  816. int err;
  817. if (port_dev->flags & IFF_LOOPBACK ||
  818. port_dev->type != ARPHRD_ETHER) {
  819. netdev_err(dev, "Device %s is of an unsupported type\n",
  820. portname);
  821. return -EINVAL;
  822. }
  823. if (team_port_exists(port_dev)) {
  824. netdev_err(dev, "Device %s is already a port "
  825. "of a team device\n", portname);
  826. return -EBUSY;
  827. }
  828. if (port_dev->flags & IFF_UP) {
  829. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  830. portname);
  831. return -EBUSY;
  832. }
  833. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  834. GFP_KERNEL);
  835. if (!port)
  836. return -ENOMEM;
  837. port->dev = port_dev;
  838. port->team = team;
  839. INIT_LIST_HEAD(&port->qom_list);
  840. port->orig.mtu = port_dev->mtu;
  841. err = dev_set_mtu(port_dev, dev->mtu);
  842. if (err) {
  843. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  844. goto err_set_mtu;
  845. }
  846. memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
  847. err = team_port_enter(team, port);
  848. if (err) {
  849. netdev_err(dev, "Device %s failed to enter team mode\n",
  850. portname);
  851. goto err_port_enter;
  852. }
  853. err = dev_open(port_dev);
  854. if (err) {
  855. netdev_dbg(dev, "Device %s opening failed\n",
  856. portname);
  857. goto err_dev_open;
  858. }
  859. err = vlan_vids_add_by_dev(port_dev, dev);
  860. if (err) {
  861. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  862. portname);
  863. goto err_vids_add;
  864. }
  865. if (team_netpoll_info(team)) {
  866. err = team_port_enable_netpoll(team, port);
  867. if (err) {
  868. netdev_err(dev, "Failed to enable netpoll on device %s\n",
  869. portname);
  870. goto err_enable_netpoll;
  871. }
  872. }
  873. err = netdev_set_master(port_dev, dev);
  874. if (err) {
  875. netdev_err(dev, "Device %s failed to set master\n", portname);
  876. goto err_set_master;
  877. }
  878. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  879. port);
  880. if (err) {
  881. netdev_err(dev, "Device %s failed to register rx_handler\n",
  882. portname);
  883. goto err_handler_register;
  884. }
  885. err = __team_option_inst_add_port(team, port);
  886. if (err) {
  887. netdev_err(dev, "Device %s failed to add per-port options\n",
  888. portname);
  889. goto err_option_port_add;
  890. }
  891. port->index = -1;
  892. team_port_enable(team, port);
  893. list_add_tail_rcu(&port->list, &team->port_list);
  894. __team_compute_features(team);
  895. __team_port_change_check(port, !!netif_carrier_ok(port_dev));
  896. __team_options_change_check(team);
  897. netdev_info(dev, "Port device %s added\n", portname);
  898. return 0;
  899. err_option_port_add:
  900. netdev_rx_handler_unregister(port_dev);
  901. err_handler_register:
  902. netdev_set_master(port_dev, NULL);
  903. err_set_master:
  904. team_port_disable_netpoll(port);
  905. err_enable_netpoll:
  906. vlan_vids_del_by_dev(port_dev, dev);
  907. err_vids_add:
  908. dev_close(port_dev);
  909. err_dev_open:
  910. team_port_leave(team, port);
  911. team_port_set_orig_mac(port);
  912. err_port_enter:
  913. dev_set_mtu(port_dev, port->orig.mtu);
  914. err_set_mtu:
  915. kfree(port);
  916. return err;
  917. }
  918. static int team_port_del(struct team *team, struct net_device *port_dev)
  919. {
  920. struct net_device *dev = team->dev;
  921. struct team_port *port;
  922. char *portname = port_dev->name;
  923. port = team_port_get_rtnl(port_dev);
  924. if (!port || !team_port_find(team, port)) {
  925. netdev_err(dev, "Device %s does not act as a port of this team\n",
  926. portname);
  927. return -ENOENT;
  928. }
  929. __team_option_inst_mark_removed_port(team, port);
  930. __team_options_change_check(team);
  931. __team_option_inst_del_port(team, port);
  932. port->removed = true;
  933. __team_port_change_check(port, false);
  934. team_port_disable(team, port);
  935. list_del_rcu(&port->list);
  936. netdev_rx_handler_unregister(port_dev);
  937. netdev_set_master(port_dev, NULL);
  938. team_port_disable_netpoll(port);
  939. vlan_vids_del_by_dev(port_dev, dev);
  940. dev_close(port_dev);
  941. team_port_leave(team, port);
  942. team_port_set_orig_mac(port);
  943. dev_set_mtu(port_dev, port->orig.mtu);
  944. synchronize_rcu();
  945. kfree(port);
  946. netdev_info(dev, "Port device %s removed\n", portname);
  947. __team_compute_features(team);
  948. return 0;
  949. }
  950. /*****************
  951. * Net device ops
  952. *****************/
  953. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  954. {
  955. ctx->data.str_val = team->mode->kind;
  956. return 0;
  957. }
  958. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  959. {
  960. return team_change_mode(team, ctx->data.str_val);
  961. }
  962. static int team_port_en_option_get(struct team *team,
  963. struct team_gsetter_ctx *ctx)
  964. {
  965. struct team_port *port = ctx->info->port;
  966. ctx->data.bool_val = team_port_enabled(port);
  967. return 0;
  968. }
  969. static int team_port_en_option_set(struct team *team,
  970. struct team_gsetter_ctx *ctx)
  971. {
  972. struct team_port *port = ctx->info->port;
  973. if (ctx->data.bool_val)
  974. team_port_enable(team, port);
  975. else
  976. team_port_disable(team, port);
  977. return 0;
  978. }
  979. static int team_user_linkup_option_get(struct team *team,
  980. struct team_gsetter_ctx *ctx)
  981. {
  982. struct team_port *port = ctx->info->port;
  983. ctx->data.bool_val = port->user.linkup;
  984. return 0;
  985. }
  986. static int team_user_linkup_option_set(struct team *team,
  987. struct team_gsetter_ctx *ctx)
  988. {
  989. struct team_port *port = ctx->info->port;
  990. port->user.linkup = ctx->data.bool_val;
  991. team_refresh_port_linkup(port);
  992. return 0;
  993. }
  994. static int team_user_linkup_en_option_get(struct team *team,
  995. struct team_gsetter_ctx *ctx)
  996. {
  997. struct team_port *port = ctx->info->port;
  998. ctx->data.bool_val = port->user.linkup_enabled;
  999. return 0;
  1000. }
  1001. static int team_user_linkup_en_option_set(struct team *team,
  1002. struct team_gsetter_ctx *ctx)
  1003. {
  1004. struct team_port *port = ctx->info->port;
  1005. port->user.linkup_enabled = ctx->data.bool_val;
  1006. team_refresh_port_linkup(port);
  1007. return 0;
  1008. }
  1009. static int team_priority_option_get(struct team *team,
  1010. struct team_gsetter_ctx *ctx)
  1011. {
  1012. struct team_port *port = ctx->info->port;
  1013. ctx->data.s32_val = port->priority;
  1014. return 0;
  1015. }
  1016. static int team_priority_option_set(struct team *team,
  1017. struct team_gsetter_ctx *ctx)
  1018. {
  1019. struct team_port *port = ctx->info->port;
  1020. port->priority = ctx->data.s32_val;
  1021. team_queue_override_port_refresh(team, port);
  1022. return 0;
  1023. }
  1024. static int team_queue_id_option_get(struct team *team,
  1025. struct team_gsetter_ctx *ctx)
  1026. {
  1027. struct team_port *port = ctx->info->port;
  1028. ctx->data.u32_val = port->queue_id;
  1029. return 0;
  1030. }
  1031. static int team_queue_id_option_set(struct team *team,
  1032. struct team_gsetter_ctx *ctx)
  1033. {
  1034. struct team_port *port = ctx->info->port;
  1035. if (port->queue_id == ctx->data.u32_val)
  1036. return 0;
  1037. if (ctx->data.u32_val >= team->dev->real_num_tx_queues)
  1038. return -EINVAL;
  1039. port->queue_id = ctx->data.u32_val;
  1040. team_queue_override_port_refresh(team, port);
  1041. return 0;
  1042. }
  1043. static const struct team_option team_options[] = {
  1044. {
  1045. .name = "mode",
  1046. .type = TEAM_OPTION_TYPE_STRING,
  1047. .getter = team_mode_option_get,
  1048. .setter = team_mode_option_set,
  1049. },
  1050. {
  1051. .name = "enabled",
  1052. .type = TEAM_OPTION_TYPE_BOOL,
  1053. .per_port = true,
  1054. .getter = team_port_en_option_get,
  1055. .setter = team_port_en_option_set,
  1056. },
  1057. {
  1058. .name = "user_linkup",
  1059. .type = TEAM_OPTION_TYPE_BOOL,
  1060. .per_port = true,
  1061. .getter = team_user_linkup_option_get,
  1062. .setter = team_user_linkup_option_set,
  1063. },
  1064. {
  1065. .name = "user_linkup_enabled",
  1066. .type = TEAM_OPTION_TYPE_BOOL,
  1067. .per_port = true,
  1068. .getter = team_user_linkup_en_option_get,
  1069. .setter = team_user_linkup_en_option_set,
  1070. },
  1071. {
  1072. .name = "priority",
  1073. .type = TEAM_OPTION_TYPE_S32,
  1074. .per_port = true,
  1075. .getter = team_priority_option_get,
  1076. .setter = team_priority_option_set,
  1077. },
  1078. {
  1079. .name = "queue_id",
  1080. .type = TEAM_OPTION_TYPE_U32,
  1081. .per_port = true,
  1082. .getter = team_queue_id_option_get,
  1083. .setter = team_queue_id_option_set,
  1084. },
  1085. };
  1086. static struct lock_class_key team_netdev_xmit_lock_key;
  1087. static struct lock_class_key team_netdev_addr_lock_key;
  1088. static void team_set_lockdep_class_one(struct net_device *dev,
  1089. struct netdev_queue *txq,
  1090. void *unused)
  1091. {
  1092. lockdep_set_class(&txq->_xmit_lock, &team_netdev_xmit_lock_key);
  1093. }
  1094. static void team_set_lockdep_class(struct net_device *dev)
  1095. {
  1096. lockdep_set_class(&dev->addr_list_lock, &team_netdev_addr_lock_key);
  1097. netdev_for_each_tx_queue(dev, team_set_lockdep_class_one, NULL);
  1098. }
  1099. static int team_init(struct net_device *dev)
  1100. {
  1101. struct team *team = netdev_priv(dev);
  1102. int i;
  1103. int err;
  1104. team->dev = dev;
  1105. mutex_init(&team->lock);
  1106. team_set_no_mode(team);
  1107. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  1108. if (!team->pcpu_stats)
  1109. return -ENOMEM;
  1110. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1111. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1112. INIT_LIST_HEAD(&team->port_list);
  1113. err = team_queue_override_init(team);
  1114. if (err)
  1115. goto err_team_queue_override_init;
  1116. team_adjust_ops(team);
  1117. INIT_LIST_HEAD(&team->option_list);
  1118. INIT_LIST_HEAD(&team->option_inst_list);
  1119. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1120. if (err)
  1121. goto err_options_register;
  1122. netif_carrier_off(dev);
  1123. team_set_lockdep_class(dev);
  1124. return 0;
  1125. err_options_register:
  1126. team_queue_override_fini(team);
  1127. err_team_queue_override_init:
  1128. free_percpu(team->pcpu_stats);
  1129. return err;
  1130. }
  1131. static void team_uninit(struct net_device *dev)
  1132. {
  1133. struct team *team = netdev_priv(dev);
  1134. struct team_port *port;
  1135. struct team_port *tmp;
  1136. mutex_lock(&team->lock);
  1137. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1138. team_port_del(team, port->dev);
  1139. __team_change_mode(team, NULL); /* cleanup */
  1140. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1141. team_queue_override_fini(team);
  1142. mutex_unlock(&team->lock);
  1143. }
  1144. static void team_destructor(struct net_device *dev)
  1145. {
  1146. struct team *team = netdev_priv(dev);
  1147. free_percpu(team->pcpu_stats);
  1148. free_netdev(dev);
  1149. }
  1150. static int team_open(struct net_device *dev)
  1151. {
  1152. netif_carrier_on(dev);
  1153. return 0;
  1154. }
  1155. static int team_close(struct net_device *dev)
  1156. {
  1157. netif_carrier_off(dev);
  1158. return 0;
  1159. }
  1160. /*
  1161. * note: already called with rcu_read_lock
  1162. */
  1163. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1164. {
  1165. struct team *team = netdev_priv(dev);
  1166. bool tx_success;
  1167. unsigned int len = skb->len;
  1168. tx_success = team_queue_override_transmit(team, skb);
  1169. if (!tx_success)
  1170. tx_success = team->ops.transmit(team, skb);
  1171. if (tx_success) {
  1172. struct team_pcpu_stats *pcpu_stats;
  1173. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1174. u64_stats_update_begin(&pcpu_stats->syncp);
  1175. pcpu_stats->tx_packets++;
  1176. pcpu_stats->tx_bytes += len;
  1177. u64_stats_update_end(&pcpu_stats->syncp);
  1178. } else {
  1179. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1180. }
  1181. return NETDEV_TX_OK;
  1182. }
  1183. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb)
  1184. {
  1185. /*
  1186. * This helper function exists to help dev_pick_tx get the correct
  1187. * destination queue. Using a helper function skips a call to
  1188. * skb_tx_hash and will put the skbs in the queue we expect on their
  1189. * way down to the team driver.
  1190. */
  1191. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1192. /*
  1193. * Save the original txq to restore before passing to the driver
  1194. */
  1195. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1196. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1197. do {
  1198. txq -= dev->real_num_tx_queues;
  1199. } while (txq >= dev->real_num_tx_queues);
  1200. }
  1201. return txq;
  1202. }
  1203. static void team_change_rx_flags(struct net_device *dev, int change)
  1204. {
  1205. struct team *team = netdev_priv(dev);
  1206. struct team_port *port;
  1207. int inc;
  1208. rcu_read_lock();
  1209. list_for_each_entry_rcu(port, &team->port_list, list) {
  1210. if (change & IFF_PROMISC) {
  1211. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1212. dev_set_promiscuity(port->dev, inc);
  1213. }
  1214. if (change & IFF_ALLMULTI) {
  1215. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1216. dev_set_allmulti(port->dev, inc);
  1217. }
  1218. }
  1219. rcu_read_unlock();
  1220. }
  1221. static void team_set_rx_mode(struct net_device *dev)
  1222. {
  1223. struct team *team = netdev_priv(dev);
  1224. struct team_port *port;
  1225. rcu_read_lock();
  1226. list_for_each_entry_rcu(port, &team->port_list, list) {
  1227. dev_uc_sync(port->dev, dev);
  1228. dev_mc_sync(port->dev, dev);
  1229. }
  1230. rcu_read_unlock();
  1231. }
  1232. static int team_set_mac_address(struct net_device *dev, void *p)
  1233. {
  1234. struct team *team = netdev_priv(dev);
  1235. struct team_port *port;
  1236. int err;
  1237. err = eth_mac_addr(dev, p);
  1238. if (err)
  1239. return err;
  1240. rcu_read_lock();
  1241. list_for_each_entry_rcu(port, &team->port_list, list)
  1242. if (team->ops.port_change_mac)
  1243. team->ops.port_change_mac(team, port);
  1244. rcu_read_unlock();
  1245. return 0;
  1246. }
  1247. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1248. {
  1249. struct team *team = netdev_priv(dev);
  1250. struct team_port *port;
  1251. int err;
  1252. /*
  1253. * Alhough this is reader, it's guarded by team lock. It's not possible
  1254. * to traverse list in reverse under rcu_read_lock
  1255. */
  1256. mutex_lock(&team->lock);
  1257. list_for_each_entry(port, &team->port_list, list) {
  1258. err = dev_set_mtu(port->dev, new_mtu);
  1259. if (err) {
  1260. netdev_err(dev, "Device %s failed to change mtu",
  1261. port->dev->name);
  1262. goto unwind;
  1263. }
  1264. }
  1265. mutex_unlock(&team->lock);
  1266. dev->mtu = new_mtu;
  1267. return 0;
  1268. unwind:
  1269. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1270. dev_set_mtu(port->dev, dev->mtu);
  1271. mutex_unlock(&team->lock);
  1272. return err;
  1273. }
  1274. static struct rtnl_link_stats64 *
  1275. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1276. {
  1277. struct team *team = netdev_priv(dev);
  1278. struct team_pcpu_stats *p;
  1279. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1280. u32 rx_dropped = 0, tx_dropped = 0;
  1281. unsigned int start;
  1282. int i;
  1283. for_each_possible_cpu(i) {
  1284. p = per_cpu_ptr(team->pcpu_stats, i);
  1285. do {
  1286. start = u64_stats_fetch_begin_bh(&p->syncp);
  1287. rx_packets = p->rx_packets;
  1288. rx_bytes = p->rx_bytes;
  1289. rx_multicast = p->rx_multicast;
  1290. tx_packets = p->tx_packets;
  1291. tx_bytes = p->tx_bytes;
  1292. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  1293. stats->rx_packets += rx_packets;
  1294. stats->rx_bytes += rx_bytes;
  1295. stats->multicast += rx_multicast;
  1296. stats->tx_packets += tx_packets;
  1297. stats->tx_bytes += tx_bytes;
  1298. /*
  1299. * rx_dropped & tx_dropped are u32, updated
  1300. * without syncp protection.
  1301. */
  1302. rx_dropped += p->rx_dropped;
  1303. tx_dropped += p->tx_dropped;
  1304. }
  1305. stats->rx_dropped = rx_dropped;
  1306. stats->tx_dropped = tx_dropped;
  1307. return stats;
  1308. }
  1309. static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
  1310. {
  1311. struct team *team = netdev_priv(dev);
  1312. struct team_port *port;
  1313. int err;
  1314. /*
  1315. * Alhough this is reader, it's guarded by team lock. It's not possible
  1316. * to traverse list in reverse under rcu_read_lock
  1317. */
  1318. mutex_lock(&team->lock);
  1319. list_for_each_entry(port, &team->port_list, list) {
  1320. err = vlan_vid_add(port->dev, vid);
  1321. if (err)
  1322. goto unwind;
  1323. }
  1324. mutex_unlock(&team->lock);
  1325. return 0;
  1326. unwind:
  1327. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1328. vlan_vid_del(port->dev, vid);
  1329. mutex_unlock(&team->lock);
  1330. return err;
  1331. }
  1332. static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
  1333. {
  1334. struct team *team = netdev_priv(dev);
  1335. struct team_port *port;
  1336. rcu_read_lock();
  1337. list_for_each_entry_rcu(port, &team->port_list, list)
  1338. vlan_vid_del(port->dev, vid);
  1339. rcu_read_unlock();
  1340. return 0;
  1341. }
  1342. #ifdef CONFIG_NET_POLL_CONTROLLER
  1343. static void team_poll_controller(struct net_device *dev)
  1344. {
  1345. }
  1346. static void __team_netpoll_cleanup(struct team *team)
  1347. {
  1348. struct team_port *port;
  1349. list_for_each_entry(port, &team->port_list, list)
  1350. team_port_disable_netpoll(port);
  1351. }
  1352. static void team_netpoll_cleanup(struct net_device *dev)
  1353. {
  1354. struct team *team = netdev_priv(dev);
  1355. mutex_lock(&team->lock);
  1356. __team_netpoll_cleanup(team);
  1357. mutex_unlock(&team->lock);
  1358. }
  1359. static int team_netpoll_setup(struct net_device *dev,
  1360. struct netpoll_info *npifo)
  1361. {
  1362. struct team *team = netdev_priv(dev);
  1363. struct team_port *port;
  1364. int err = 0;
  1365. mutex_lock(&team->lock);
  1366. list_for_each_entry(port, &team->port_list, list) {
  1367. err = team_port_enable_netpoll(team, port);
  1368. if (err) {
  1369. __team_netpoll_cleanup(team);
  1370. break;
  1371. }
  1372. }
  1373. mutex_unlock(&team->lock);
  1374. return err;
  1375. }
  1376. #endif
  1377. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  1378. {
  1379. struct team *team = netdev_priv(dev);
  1380. int err;
  1381. mutex_lock(&team->lock);
  1382. err = team_port_add(team, port_dev);
  1383. mutex_unlock(&team->lock);
  1384. return err;
  1385. }
  1386. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1387. {
  1388. struct team *team = netdev_priv(dev);
  1389. int err;
  1390. mutex_lock(&team->lock);
  1391. err = team_port_del(team, port_dev);
  1392. mutex_unlock(&team->lock);
  1393. return err;
  1394. }
  1395. static netdev_features_t team_fix_features(struct net_device *dev,
  1396. netdev_features_t features)
  1397. {
  1398. struct team_port *port;
  1399. struct team *team = netdev_priv(dev);
  1400. netdev_features_t mask;
  1401. mask = features;
  1402. features &= ~NETIF_F_ONE_FOR_ALL;
  1403. features |= NETIF_F_ALL_FOR_ALL;
  1404. rcu_read_lock();
  1405. list_for_each_entry_rcu(port, &team->port_list, list) {
  1406. features = netdev_increment_features(features,
  1407. port->dev->features,
  1408. mask);
  1409. }
  1410. rcu_read_unlock();
  1411. return features;
  1412. }
  1413. static const struct net_device_ops team_netdev_ops = {
  1414. .ndo_init = team_init,
  1415. .ndo_uninit = team_uninit,
  1416. .ndo_open = team_open,
  1417. .ndo_stop = team_close,
  1418. .ndo_start_xmit = team_xmit,
  1419. .ndo_select_queue = team_select_queue,
  1420. .ndo_change_rx_flags = team_change_rx_flags,
  1421. .ndo_set_rx_mode = team_set_rx_mode,
  1422. .ndo_set_mac_address = team_set_mac_address,
  1423. .ndo_change_mtu = team_change_mtu,
  1424. .ndo_get_stats64 = team_get_stats64,
  1425. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1426. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1427. #ifdef CONFIG_NET_POLL_CONTROLLER
  1428. .ndo_poll_controller = team_poll_controller,
  1429. .ndo_netpoll_setup = team_netpoll_setup,
  1430. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1431. #endif
  1432. .ndo_add_slave = team_add_slave,
  1433. .ndo_del_slave = team_del_slave,
  1434. .ndo_fix_features = team_fix_features,
  1435. };
  1436. /***********************
  1437. * rt netlink interface
  1438. ***********************/
  1439. static void team_setup(struct net_device *dev)
  1440. {
  1441. ether_setup(dev);
  1442. dev->netdev_ops = &team_netdev_ops;
  1443. dev->destructor = team_destructor;
  1444. dev->tx_queue_len = 0;
  1445. dev->flags |= IFF_MULTICAST;
  1446. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1447. /*
  1448. * Indicate we support unicast address filtering. That way core won't
  1449. * bring us to promisc mode in case a unicast addr is added.
  1450. * Let this up to underlay drivers.
  1451. */
  1452. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1453. dev->features |= NETIF_F_LLTX;
  1454. dev->features |= NETIF_F_GRO;
  1455. dev->hw_features = NETIF_F_HW_VLAN_TX |
  1456. NETIF_F_HW_VLAN_RX |
  1457. NETIF_F_HW_VLAN_FILTER;
  1458. dev->features |= dev->hw_features;
  1459. }
  1460. static int team_newlink(struct net *src_net, struct net_device *dev,
  1461. struct nlattr *tb[], struct nlattr *data[])
  1462. {
  1463. int err;
  1464. if (tb[IFLA_ADDRESS] == NULL)
  1465. eth_hw_addr_random(dev);
  1466. err = register_netdevice(dev);
  1467. if (err)
  1468. return err;
  1469. return 0;
  1470. }
  1471. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  1472. {
  1473. if (tb[IFLA_ADDRESS]) {
  1474. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1475. return -EINVAL;
  1476. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1477. return -EADDRNOTAVAIL;
  1478. }
  1479. return 0;
  1480. }
  1481. static unsigned int team_get_num_tx_queues(void)
  1482. {
  1483. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1484. }
  1485. static unsigned int team_get_num_rx_queues(void)
  1486. {
  1487. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1488. }
  1489. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1490. .kind = DRV_NAME,
  1491. .priv_size = sizeof(struct team),
  1492. .setup = team_setup,
  1493. .newlink = team_newlink,
  1494. .validate = team_validate,
  1495. .get_num_tx_queues = team_get_num_tx_queues,
  1496. .get_num_rx_queues = team_get_num_rx_queues,
  1497. };
  1498. /***********************************
  1499. * Generic netlink custom interface
  1500. ***********************************/
  1501. static struct genl_family team_nl_family = {
  1502. .id = GENL_ID_GENERATE,
  1503. .name = TEAM_GENL_NAME,
  1504. .version = TEAM_GENL_VERSION,
  1505. .maxattr = TEAM_ATTR_MAX,
  1506. .netnsok = true,
  1507. };
  1508. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1509. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1510. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1511. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1512. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1513. };
  1514. static const struct nla_policy
  1515. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1516. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1517. [TEAM_ATTR_OPTION_NAME] = {
  1518. .type = NLA_STRING,
  1519. .len = TEAM_STRING_MAX_LEN,
  1520. },
  1521. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1522. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1523. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1524. };
  1525. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1526. {
  1527. struct sk_buff *msg;
  1528. void *hdr;
  1529. int err;
  1530. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1531. if (!msg)
  1532. return -ENOMEM;
  1533. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  1534. &team_nl_family, 0, TEAM_CMD_NOOP);
  1535. if (IS_ERR(hdr)) {
  1536. err = PTR_ERR(hdr);
  1537. goto err_msg_put;
  1538. }
  1539. genlmsg_end(msg, hdr);
  1540. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  1541. err_msg_put:
  1542. nlmsg_free(msg);
  1543. return err;
  1544. }
  1545. /*
  1546. * Netlink cmd functions should be locked by following two functions.
  1547. * Since dev gets held here, that ensures dev won't disappear in between.
  1548. */
  1549. static struct team *team_nl_team_get(struct genl_info *info)
  1550. {
  1551. struct net *net = genl_info_net(info);
  1552. int ifindex;
  1553. struct net_device *dev;
  1554. struct team *team;
  1555. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1556. return NULL;
  1557. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1558. dev = dev_get_by_index(net, ifindex);
  1559. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1560. if (dev)
  1561. dev_put(dev);
  1562. return NULL;
  1563. }
  1564. team = netdev_priv(dev);
  1565. mutex_lock(&team->lock);
  1566. return team;
  1567. }
  1568. static void team_nl_team_put(struct team *team)
  1569. {
  1570. mutex_unlock(&team->lock);
  1571. dev_put(team->dev);
  1572. }
  1573. static int team_nl_send_generic(struct genl_info *info, struct team *team,
  1574. int (*fill_func)(struct sk_buff *skb,
  1575. struct genl_info *info,
  1576. int flags, struct team *team))
  1577. {
  1578. struct sk_buff *skb;
  1579. int err;
  1580. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1581. if (!skb)
  1582. return -ENOMEM;
  1583. err = fill_func(skb, info, NLM_F_ACK, team);
  1584. if (err < 0)
  1585. goto err_fill;
  1586. err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
  1587. return err;
  1588. err_fill:
  1589. nlmsg_free(skb);
  1590. return err;
  1591. }
  1592. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1593. struct team *team, u32 pid);
  1594. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 pid)
  1595. {
  1596. return genlmsg_unicast(dev_net(team->dev), skb, pid);
  1597. }
  1598. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1599. struct team_option_inst *opt_inst)
  1600. {
  1601. struct nlattr *option_item;
  1602. struct team_option *option = opt_inst->option;
  1603. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1604. struct team_gsetter_ctx ctx;
  1605. int err;
  1606. ctx.info = opt_inst_info;
  1607. err = team_option_get(team, opt_inst, &ctx);
  1608. if (err)
  1609. return err;
  1610. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1611. if (!option_item)
  1612. return -EMSGSIZE;
  1613. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1614. goto nest_cancel;
  1615. if (opt_inst_info->port &&
  1616. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1617. opt_inst_info->port->dev->ifindex))
  1618. goto nest_cancel;
  1619. if (opt_inst->option->array_size &&
  1620. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1621. opt_inst_info->array_index))
  1622. goto nest_cancel;
  1623. switch (option->type) {
  1624. case TEAM_OPTION_TYPE_U32:
  1625. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1626. goto nest_cancel;
  1627. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1628. goto nest_cancel;
  1629. break;
  1630. case TEAM_OPTION_TYPE_STRING:
  1631. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1632. goto nest_cancel;
  1633. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1634. ctx.data.str_val))
  1635. goto nest_cancel;
  1636. break;
  1637. case TEAM_OPTION_TYPE_BINARY:
  1638. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1639. goto nest_cancel;
  1640. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1641. ctx.data.bin_val.ptr))
  1642. goto nest_cancel;
  1643. break;
  1644. case TEAM_OPTION_TYPE_BOOL:
  1645. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1646. goto nest_cancel;
  1647. if (ctx.data.bool_val &&
  1648. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1649. goto nest_cancel;
  1650. break;
  1651. case TEAM_OPTION_TYPE_S32:
  1652. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1653. goto nest_cancel;
  1654. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1655. goto nest_cancel;
  1656. break;
  1657. default:
  1658. BUG();
  1659. }
  1660. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1661. goto nest_cancel;
  1662. if (opt_inst->changed) {
  1663. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1664. goto nest_cancel;
  1665. opt_inst->changed = false;
  1666. }
  1667. nla_nest_end(skb, option_item);
  1668. return 0;
  1669. nest_cancel:
  1670. nla_nest_cancel(skb, option_item);
  1671. return -EMSGSIZE;
  1672. }
  1673. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1674. struct team *team, u32 pid,
  1675. team_nl_send_func_t *send_func)
  1676. {
  1677. int err;
  1678. if (*pskb) {
  1679. err = send_func(*pskb, team, pid);
  1680. if (err)
  1681. return err;
  1682. }
  1683. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1684. if (!*pskb)
  1685. return -ENOMEM;
  1686. return 0;
  1687. }
  1688. static int team_nl_send_options_get(struct team *team, u32 pid, u32 seq,
  1689. int flags, team_nl_send_func_t *send_func,
  1690. struct list_head *sel_opt_inst_list)
  1691. {
  1692. struct nlattr *option_list;
  1693. struct nlmsghdr *nlh;
  1694. void *hdr;
  1695. struct team_option_inst *opt_inst;
  1696. int err;
  1697. struct sk_buff *skb = NULL;
  1698. bool incomplete;
  1699. int i;
  1700. opt_inst = list_first_entry(sel_opt_inst_list,
  1701. struct team_option_inst, tmp_list);
  1702. start_again:
  1703. err = __send_and_alloc_skb(&skb, team, pid, send_func);
  1704. if (err)
  1705. return err;
  1706. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags | NLM_F_MULTI,
  1707. TEAM_CMD_OPTIONS_GET);
  1708. if (IS_ERR(hdr))
  1709. return PTR_ERR(hdr);
  1710. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1711. goto nla_put_failure;
  1712. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1713. if (!option_list)
  1714. goto nla_put_failure;
  1715. i = 0;
  1716. incomplete = false;
  1717. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  1718. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  1719. if (err) {
  1720. if (err == -EMSGSIZE) {
  1721. if (!i)
  1722. goto errout;
  1723. incomplete = true;
  1724. break;
  1725. }
  1726. goto errout;
  1727. }
  1728. i++;
  1729. }
  1730. nla_nest_end(skb, option_list);
  1731. genlmsg_end(skb, hdr);
  1732. if (incomplete)
  1733. goto start_again;
  1734. send_done:
  1735. nlh = nlmsg_put(skb, pid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1736. if (!nlh) {
  1737. err = __send_and_alloc_skb(&skb, team, pid, send_func);
  1738. if (err)
  1739. goto errout;
  1740. goto send_done;
  1741. }
  1742. return send_func(skb, team, pid);
  1743. nla_put_failure:
  1744. err = -EMSGSIZE;
  1745. errout:
  1746. genlmsg_cancel(skb, hdr);
  1747. nlmsg_free(skb);
  1748. return err;
  1749. }
  1750. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1751. {
  1752. struct team *team;
  1753. struct team_option_inst *opt_inst;
  1754. int err;
  1755. LIST_HEAD(sel_opt_inst_list);
  1756. team = team_nl_team_get(info);
  1757. if (!team)
  1758. return -EINVAL;
  1759. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  1760. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  1761. err = team_nl_send_options_get(team, info->snd_pid, info->snd_seq,
  1762. NLM_F_ACK, team_nl_send_unicast,
  1763. &sel_opt_inst_list);
  1764. team_nl_team_put(team);
  1765. return err;
  1766. }
  1767. static int team_nl_send_event_options_get(struct team *team,
  1768. struct list_head *sel_opt_inst_list);
  1769. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1770. {
  1771. struct team *team;
  1772. int err = 0;
  1773. int i;
  1774. struct nlattr *nl_option;
  1775. LIST_HEAD(opt_inst_list);
  1776. team = team_nl_team_get(info);
  1777. if (!team)
  1778. return -EINVAL;
  1779. err = -EINVAL;
  1780. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  1781. err = -EINVAL;
  1782. goto team_put;
  1783. }
  1784. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  1785. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  1786. struct nlattr *attr;
  1787. struct nlattr *attr_data;
  1788. enum team_option_type opt_type;
  1789. int opt_port_ifindex = 0; /* != 0 for per-port options */
  1790. u32 opt_array_index = 0;
  1791. bool opt_is_array = false;
  1792. struct team_option_inst *opt_inst;
  1793. char *opt_name;
  1794. bool opt_found = false;
  1795. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  1796. err = -EINVAL;
  1797. goto team_put;
  1798. }
  1799. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  1800. nl_option, team_nl_option_policy);
  1801. if (err)
  1802. goto team_put;
  1803. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  1804. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  1805. err = -EINVAL;
  1806. goto team_put;
  1807. }
  1808. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  1809. case NLA_U32:
  1810. opt_type = TEAM_OPTION_TYPE_U32;
  1811. break;
  1812. case NLA_STRING:
  1813. opt_type = TEAM_OPTION_TYPE_STRING;
  1814. break;
  1815. case NLA_BINARY:
  1816. opt_type = TEAM_OPTION_TYPE_BINARY;
  1817. break;
  1818. case NLA_FLAG:
  1819. opt_type = TEAM_OPTION_TYPE_BOOL;
  1820. break;
  1821. case NLA_S32:
  1822. opt_type = TEAM_OPTION_TYPE_S32;
  1823. break;
  1824. default:
  1825. goto team_put;
  1826. }
  1827. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  1828. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  1829. err = -EINVAL;
  1830. goto team_put;
  1831. }
  1832. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  1833. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  1834. if (attr)
  1835. opt_port_ifindex = nla_get_u32(attr);
  1836. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  1837. if (attr) {
  1838. opt_is_array = true;
  1839. opt_array_index = nla_get_u32(attr);
  1840. }
  1841. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  1842. struct team_option *option = opt_inst->option;
  1843. struct team_gsetter_ctx ctx;
  1844. struct team_option_inst_info *opt_inst_info;
  1845. int tmp_ifindex;
  1846. opt_inst_info = &opt_inst->info;
  1847. tmp_ifindex = opt_inst_info->port ?
  1848. opt_inst_info->port->dev->ifindex : 0;
  1849. if (option->type != opt_type ||
  1850. strcmp(option->name, opt_name) ||
  1851. tmp_ifindex != opt_port_ifindex ||
  1852. (option->array_size && !opt_is_array) ||
  1853. opt_inst_info->array_index != opt_array_index)
  1854. continue;
  1855. opt_found = true;
  1856. ctx.info = opt_inst_info;
  1857. switch (opt_type) {
  1858. case TEAM_OPTION_TYPE_U32:
  1859. ctx.data.u32_val = nla_get_u32(attr_data);
  1860. break;
  1861. case TEAM_OPTION_TYPE_STRING:
  1862. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  1863. err = -EINVAL;
  1864. goto team_put;
  1865. }
  1866. ctx.data.str_val = nla_data(attr_data);
  1867. break;
  1868. case TEAM_OPTION_TYPE_BINARY:
  1869. ctx.data.bin_val.len = nla_len(attr_data);
  1870. ctx.data.bin_val.ptr = nla_data(attr_data);
  1871. break;
  1872. case TEAM_OPTION_TYPE_BOOL:
  1873. ctx.data.bool_val = attr_data ? true : false;
  1874. break;
  1875. case TEAM_OPTION_TYPE_S32:
  1876. ctx.data.s32_val = nla_get_s32(attr_data);
  1877. break;
  1878. default:
  1879. BUG();
  1880. }
  1881. err = team_option_set(team, opt_inst, &ctx);
  1882. if (err)
  1883. goto team_put;
  1884. opt_inst->changed = true;
  1885. list_add(&opt_inst->tmp_list, &opt_inst_list);
  1886. }
  1887. if (!opt_found) {
  1888. err = -ENOENT;
  1889. goto team_put;
  1890. }
  1891. }
  1892. err = team_nl_send_event_options_get(team, &opt_inst_list);
  1893. team_put:
  1894. team_nl_team_put(team);
  1895. return err;
  1896. }
  1897. static int team_nl_fill_port_list_get(struct sk_buff *skb,
  1898. u32 pid, u32 seq, int flags,
  1899. struct team *team,
  1900. bool fillall)
  1901. {
  1902. struct nlattr *port_list;
  1903. void *hdr;
  1904. struct team_port *port;
  1905. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1906. TEAM_CMD_PORT_LIST_GET);
  1907. if (IS_ERR(hdr))
  1908. return PTR_ERR(hdr);
  1909. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  1910. goto nla_put_failure;
  1911. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  1912. if (!port_list)
  1913. goto nla_put_failure;
  1914. list_for_each_entry(port, &team->port_list, list) {
  1915. struct nlattr *port_item;
  1916. /* Include only changed ports if fill all mode is not on */
  1917. if (!fillall && !port->changed)
  1918. continue;
  1919. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  1920. if (!port_item)
  1921. goto nla_put_failure;
  1922. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  1923. goto nla_put_failure;
  1924. if (port->changed) {
  1925. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  1926. goto nla_put_failure;
  1927. port->changed = false;
  1928. }
  1929. if ((port->removed &&
  1930. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  1931. (port->state.linkup &&
  1932. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  1933. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  1934. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  1935. goto nla_put_failure;
  1936. nla_nest_end(skb, port_item);
  1937. }
  1938. nla_nest_end(skb, port_list);
  1939. return genlmsg_end(skb, hdr);
  1940. nla_put_failure:
  1941. genlmsg_cancel(skb, hdr);
  1942. return -EMSGSIZE;
  1943. }
  1944. static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
  1945. struct genl_info *info, int flags,
  1946. struct team *team)
  1947. {
  1948. return team_nl_fill_port_list_get(skb, info->snd_pid,
  1949. info->snd_seq, NLM_F_ACK,
  1950. team, true);
  1951. }
  1952. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  1953. struct genl_info *info)
  1954. {
  1955. struct team *team;
  1956. int err;
  1957. team = team_nl_team_get(info);
  1958. if (!team)
  1959. return -EINVAL;
  1960. err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
  1961. team_nl_team_put(team);
  1962. return err;
  1963. }
  1964. static struct genl_ops team_nl_ops[] = {
  1965. {
  1966. .cmd = TEAM_CMD_NOOP,
  1967. .doit = team_nl_cmd_noop,
  1968. .policy = team_nl_policy,
  1969. },
  1970. {
  1971. .cmd = TEAM_CMD_OPTIONS_SET,
  1972. .doit = team_nl_cmd_options_set,
  1973. .policy = team_nl_policy,
  1974. .flags = GENL_ADMIN_PERM,
  1975. },
  1976. {
  1977. .cmd = TEAM_CMD_OPTIONS_GET,
  1978. .doit = team_nl_cmd_options_get,
  1979. .policy = team_nl_policy,
  1980. .flags = GENL_ADMIN_PERM,
  1981. },
  1982. {
  1983. .cmd = TEAM_CMD_PORT_LIST_GET,
  1984. .doit = team_nl_cmd_port_list_get,
  1985. .policy = team_nl_policy,
  1986. .flags = GENL_ADMIN_PERM,
  1987. },
  1988. };
  1989. static struct genl_multicast_group team_change_event_mcgrp = {
  1990. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  1991. };
  1992. static int team_nl_send_multicast(struct sk_buff *skb,
  1993. struct team *team, u32 pid)
  1994. {
  1995. return genlmsg_multicast_netns(dev_net(team->dev), skb, 0,
  1996. team_change_event_mcgrp.id, GFP_KERNEL);
  1997. }
  1998. static int team_nl_send_event_options_get(struct team *team,
  1999. struct list_head *sel_opt_inst_list)
  2000. {
  2001. return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
  2002. sel_opt_inst_list);
  2003. }
  2004. static int team_nl_send_event_port_list_get(struct team *team)
  2005. {
  2006. struct sk_buff *skb;
  2007. int err;
  2008. struct net *net = dev_net(team->dev);
  2009. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2010. if (!skb)
  2011. return -ENOMEM;
  2012. err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
  2013. if (err < 0)
  2014. goto err_fill;
  2015. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  2016. GFP_KERNEL);
  2017. return err;
  2018. err_fill:
  2019. nlmsg_free(skb);
  2020. return err;
  2021. }
  2022. static int team_nl_init(void)
  2023. {
  2024. int err;
  2025. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  2026. ARRAY_SIZE(team_nl_ops));
  2027. if (err)
  2028. return err;
  2029. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  2030. if (err)
  2031. goto err_change_event_grp_reg;
  2032. return 0;
  2033. err_change_event_grp_reg:
  2034. genl_unregister_family(&team_nl_family);
  2035. return err;
  2036. }
  2037. static void team_nl_fini(void)
  2038. {
  2039. genl_unregister_family(&team_nl_family);
  2040. }
  2041. /******************
  2042. * Change checkers
  2043. ******************/
  2044. static void __team_options_change_check(struct team *team)
  2045. {
  2046. int err;
  2047. struct team_option_inst *opt_inst;
  2048. LIST_HEAD(sel_opt_inst_list);
  2049. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2050. if (opt_inst->changed)
  2051. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2052. }
  2053. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2054. if (err)
  2055. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2056. err);
  2057. }
  2058. /* rtnl lock is held */
  2059. static void __team_port_change_check(struct team_port *port, bool linkup)
  2060. {
  2061. int err;
  2062. if (!port->removed && port->state.linkup == linkup)
  2063. return;
  2064. port->changed = true;
  2065. port->state.linkup = linkup;
  2066. team_refresh_port_linkup(port);
  2067. if (linkup) {
  2068. struct ethtool_cmd ecmd;
  2069. err = __ethtool_get_settings(port->dev, &ecmd);
  2070. if (!err) {
  2071. port->state.speed = ethtool_cmd_speed(&ecmd);
  2072. port->state.duplex = ecmd.duplex;
  2073. goto send_event;
  2074. }
  2075. }
  2076. port->state.speed = 0;
  2077. port->state.duplex = 0;
  2078. send_event:
  2079. err = team_nl_send_event_port_list_get(port->team);
  2080. if (err)
  2081. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
  2082. port->dev->name);
  2083. }
  2084. static void team_port_change_check(struct team_port *port, bool linkup)
  2085. {
  2086. struct team *team = port->team;
  2087. mutex_lock(&team->lock);
  2088. __team_port_change_check(port, linkup);
  2089. mutex_unlock(&team->lock);
  2090. }
  2091. /************************************
  2092. * Net device notifier event handler
  2093. ************************************/
  2094. static int team_device_event(struct notifier_block *unused,
  2095. unsigned long event, void *ptr)
  2096. {
  2097. struct net_device *dev = (struct net_device *) ptr;
  2098. struct team_port *port;
  2099. port = team_port_get_rtnl(dev);
  2100. if (!port)
  2101. return NOTIFY_DONE;
  2102. switch (event) {
  2103. case NETDEV_UP:
  2104. if (netif_carrier_ok(dev))
  2105. team_port_change_check(port, true);
  2106. case NETDEV_DOWN:
  2107. team_port_change_check(port, false);
  2108. case NETDEV_CHANGE:
  2109. if (netif_running(port->dev))
  2110. team_port_change_check(port,
  2111. !!netif_carrier_ok(port->dev));
  2112. break;
  2113. case NETDEV_UNREGISTER:
  2114. team_del_slave(port->team->dev, dev);
  2115. break;
  2116. case NETDEV_FEAT_CHANGE:
  2117. team_compute_features(port->team);
  2118. break;
  2119. case NETDEV_CHANGEMTU:
  2120. /* Forbid to change mtu of underlaying device */
  2121. return NOTIFY_BAD;
  2122. case NETDEV_PRE_TYPE_CHANGE:
  2123. /* Forbid to change type of underlaying device */
  2124. return NOTIFY_BAD;
  2125. }
  2126. return NOTIFY_DONE;
  2127. }
  2128. static struct notifier_block team_notifier_block __read_mostly = {
  2129. .notifier_call = team_device_event,
  2130. };
  2131. /***********************
  2132. * Module init and exit
  2133. ***********************/
  2134. static int __init team_module_init(void)
  2135. {
  2136. int err;
  2137. register_netdevice_notifier(&team_notifier_block);
  2138. err = rtnl_link_register(&team_link_ops);
  2139. if (err)
  2140. goto err_rtnl_reg;
  2141. err = team_nl_init();
  2142. if (err)
  2143. goto err_nl_init;
  2144. return 0;
  2145. err_nl_init:
  2146. rtnl_link_unregister(&team_link_ops);
  2147. err_rtnl_reg:
  2148. unregister_netdevice_notifier(&team_notifier_block);
  2149. return err;
  2150. }
  2151. static void __exit team_module_exit(void)
  2152. {
  2153. team_nl_fini();
  2154. rtnl_link_unregister(&team_link_ops);
  2155. unregister_netdevice_notifier(&team_notifier_block);
  2156. }
  2157. module_init(team_module_init);
  2158. module_exit(team_module_exit);
  2159. MODULE_LICENSE("GPL v2");
  2160. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2161. MODULE_DESCRIPTION("Ethernet team device driver");
  2162. MODULE_ALIAS_RTNL_LINK(DRV_NAME);