team.c 62 KB

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