team.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039
  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 <net/switchdev.h>
  31. #include <generated/utsrelease.h>
  32. #include <linux/if_team.h>
  33. #define DRV_NAME "team"
  34. /**********
  35. * Helpers
  36. **********/
  37. #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
  38. static struct team_port *team_port_get_rtnl(const struct net_device *dev)
  39. {
  40. struct team_port *port = rtnl_dereference(dev->rx_handler_data);
  41. return team_port_exists(dev) ? port : NULL;
  42. }
  43. /*
  44. * Since the ability to change device address for open port device is tested in
  45. * team_port_add, this function can be called without control of return value
  46. */
  47. static int __set_port_dev_addr(struct net_device *port_dev,
  48. const unsigned char *dev_addr)
  49. {
  50. struct sockaddr_storage addr;
  51. memcpy(addr.__data, dev_addr, port_dev->addr_len);
  52. addr.ss_family = port_dev->type;
  53. return dev_set_mac_address(port_dev, (struct sockaddr *)&addr);
  54. }
  55. static int team_port_set_orig_dev_addr(struct team_port *port)
  56. {
  57. return __set_port_dev_addr(port->dev, port->orig.dev_addr);
  58. }
  59. static int team_port_set_team_dev_addr(struct team *team,
  60. struct team_port *port)
  61. {
  62. return __set_port_dev_addr(port->dev, team->dev->dev_addr);
  63. }
  64. int team_modeop_port_enter(struct team *team, struct team_port *port)
  65. {
  66. return team_port_set_team_dev_addr(team, port);
  67. }
  68. EXPORT_SYMBOL(team_modeop_port_enter);
  69. void team_modeop_port_change_dev_addr(struct team *team,
  70. struct team_port *port)
  71. {
  72. team_port_set_team_dev_addr(team, port);
  73. }
  74. EXPORT_SYMBOL(team_modeop_port_change_dev_addr);
  75. static void team_lower_state_changed(struct team_port *port)
  76. {
  77. struct netdev_lag_lower_state_info info;
  78. info.link_up = port->linkup;
  79. info.tx_enabled = team_port_enabled(port);
  80. netdev_lower_state_changed(port->dev, &info);
  81. }
  82. static void team_refresh_port_linkup(struct team_port *port)
  83. {
  84. bool new_linkup = port->user.linkup_enabled ? port->user.linkup :
  85. port->state.linkup;
  86. if (port->linkup != new_linkup) {
  87. port->linkup = new_linkup;
  88. team_lower_state_changed(port);
  89. }
  90. }
  91. /*******************
  92. * Options handling
  93. *******************/
  94. struct team_option_inst { /* One for each option instance */
  95. struct list_head list;
  96. struct list_head tmp_list;
  97. struct team_option *option;
  98. struct team_option_inst_info info;
  99. bool changed;
  100. bool removed;
  101. };
  102. static struct team_option *__team_find_option(struct team *team,
  103. const char *opt_name)
  104. {
  105. struct team_option *option;
  106. list_for_each_entry(option, &team->option_list, list) {
  107. if (strcmp(option->name, opt_name) == 0)
  108. return option;
  109. }
  110. return NULL;
  111. }
  112. static void __team_option_inst_del(struct team_option_inst *opt_inst)
  113. {
  114. list_del(&opt_inst->list);
  115. kfree(opt_inst);
  116. }
  117. static void __team_option_inst_del_option(struct team *team,
  118. struct team_option *option)
  119. {
  120. struct team_option_inst *opt_inst, *tmp;
  121. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  122. if (opt_inst->option == option)
  123. __team_option_inst_del(opt_inst);
  124. }
  125. }
  126. static int __team_option_inst_add(struct team *team, struct team_option *option,
  127. struct team_port *port)
  128. {
  129. struct team_option_inst *opt_inst;
  130. unsigned int array_size;
  131. unsigned int i;
  132. int err;
  133. array_size = option->array_size;
  134. if (!array_size)
  135. array_size = 1; /* No array but still need one instance */
  136. for (i = 0; i < array_size; i++) {
  137. opt_inst = kmalloc(sizeof(*opt_inst), GFP_KERNEL);
  138. if (!opt_inst)
  139. return -ENOMEM;
  140. opt_inst->option = option;
  141. opt_inst->info.port = port;
  142. opt_inst->info.array_index = i;
  143. opt_inst->changed = true;
  144. opt_inst->removed = false;
  145. list_add_tail(&opt_inst->list, &team->option_inst_list);
  146. if (option->init) {
  147. err = option->init(team, &opt_inst->info);
  148. if (err)
  149. return err;
  150. }
  151. }
  152. return 0;
  153. }
  154. static int __team_option_inst_add_option(struct team *team,
  155. struct team_option *option)
  156. {
  157. int err;
  158. if (!option->per_port) {
  159. err = __team_option_inst_add(team, option, NULL);
  160. if (err)
  161. goto inst_del_option;
  162. }
  163. return 0;
  164. inst_del_option:
  165. __team_option_inst_del_option(team, option);
  166. return err;
  167. }
  168. static void __team_option_inst_mark_removed_option(struct team *team,
  169. struct team_option *option)
  170. {
  171. struct team_option_inst *opt_inst;
  172. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  173. if (opt_inst->option == option) {
  174. opt_inst->changed = true;
  175. opt_inst->removed = true;
  176. }
  177. }
  178. }
  179. static void __team_option_inst_del_port(struct team *team,
  180. struct team_port *port)
  181. {
  182. struct team_option_inst *opt_inst, *tmp;
  183. list_for_each_entry_safe(opt_inst, tmp, &team->option_inst_list, list) {
  184. if (opt_inst->option->per_port &&
  185. opt_inst->info.port == port)
  186. __team_option_inst_del(opt_inst);
  187. }
  188. }
  189. static int __team_option_inst_add_port(struct team *team,
  190. struct team_port *port)
  191. {
  192. struct team_option *option;
  193. int err;
  194. list_for_each_entry(option, &team->option_list, list) {
  195. if (!option->per_port)
  196. continue;
  197. err = __team_option_inst_add(team, option, port);
  198. if (err)
  199. goto inst_del_port;
  200. }
  201. return 0;
  202. inst_del_port:
  203. __team_option_inst_del_port(team, port);
  204. return err;
  205. }
  206. static void __team_option_inst_mark_removed_port(struct team *team,
  207. struct team_port *port)
  208. {
  209. struct team_option_inst *opt_inst;
  210. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  211. if (opt_inst->info.port == port) {
  212. opt_inst->changed = true;
  213. opt_inst->removed = true;
  214. }
  215. }
  216. }
  217. static int __team_options_register(struct team *team,
  218. const struct team_option *option,
  219. size_t option_count)
  220. {
  221. int i;
  222. struct team_option **dst_opts;
  223. int err;
  224. dst_opts = kcalloc(option_count, sizeof(struct team_option *),
  225. GFP_KERNEL);
  226. if (!dst_opts)
  227. return -ENOMEM;
  228. for (i = 0; i < option_count; i++, option++) {
  229. if (__team_find_option(team, option->name)) {
  230. err = -EEXIST;
  231. goto alloc_rollback;
  232. }
  233. dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
  234. if (!dst_opts[i]) {
  235. err = -ENOMEM;
  236. goto alloc_rollback;
  237. }
  238. }
  239. for (i = 0; i < option_count; i++) {
  240. err = __team_option_inst_add_option(team, dst_opts[i]);
  241. if (err)
  242. goto inst_rollback;
  243. list_add_tail(&dst_opts[i]->list, &team->option_list);
  244. }
  245. kfree(dst_opts);
  246. return 0;
  247. inst_rollback:
  248. for (i--; i >= 0; i--)
  249. __team_option_inst_del_option(team, dst_opts[i]);
  250. i = option_count - 1;
  251. alloc_rollback:
  252. for (i--; i >= 0; i--)
  253. kfree(dst_opts[i]);
  254. kfree(dst_opts);
  255. return err;
  256. }
  257. static void __team_options_mark_removed(struct team *team,
  258. const struct team_option *option,
  259. size_t option_count)
  260. {
  261. int i;
  262. for (i = 0; i < option_count; i++, option++) {
  263. struct team_option *del_opt;
  264. del_opt = __team_find_option(team, option->name);
  265. if (del_opt)
  266. __team_option_inst_mark_removed_option(team, del_opt);
  267. }
  268. }
  269. static void __team_options_unregister(struct team *team,
  270. const struct team_option *option,
  271. size_t option_count)
  272. {
  273. int i;
  274. for (i = 0; i < option_count; i++, option++) {
  275. struct team_option *del_opt;
  276. del_opt = __team_find_option(team, option->name);
  277. if (del_opt) {
  278. __team_option_inst_del_option(team, del_opt);
  279. list_del(&del_opt->list);
  280. kfree(del_opt);
  281. }
  282. }
  283. }
  284. static void __team_options_change_check(struct team *team);
  285. int team_options_register(struct team *team,
  286. const struct team_option *option,
  287. size_t option_count)
  288. {
  289. int err;
  290. err = __team_options_register(team, option, option_count);
  291. if (err)
  292. return err;
  293. __team_options_change_check(team);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(team_options_register);
  297. void team_options_unregister(struct team *team,
  298. const struct team_option *option,
  299. size_t option_count)
  300. {
  301. __team_options_mark_removed(team, option, option_count);
  302. __team_options_change_check(team);
  303. __team_options_unregister(team, option, option_count);
  304. }
  305. EXPORT_SYMBOL(team_options_unregister);
  306. static int team_option_get(struct team *team,
  307. struct team_option_inst *opt_inst,
  308. struct team_gsetter_ctx *ctx)
  309. {
  310. if (!opt_inst->option->getter)
  311. return -EOPNOTSUPP;
  312. return opt_inst->option->getter(team, ctx);
  313. }
  314. static int team_option_set(struct team *team,
  315. struct team_option_inst *opt_inst,
  316. struct team_gsetter_ctx *ctx)
  317. {
  318. if (!opt_inst->option->setter)
  319. return -EOPNOTSUPP;
  320. return opt_inst->option->setter(team, ctx);
  321. }
  322. void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info)
  323. {
  324. struct team_option_inst *opt_inst;
  325. opt_inst = container_of(opt_inst_info, struct team_option_inst, info);
  326. opt_inst->changed = true;
  327. }
  328. EXPORT_SYMBOL(team_option_inst_set_change);
  329. void team_options_change_check(struct team *team)
  330. {
  331. __team_options_change_check(team);
  332. }
  333. EXPORT_SYMBOL(team_options_change_check);
  334. /****************
  335. * Mode handling
  336. ****************/
  337. static LIST_HEAD(mode_list);
  338. static DEFINE_SPINLOCK(mode_list_lock);
  339. struct team_mode_item {
  340. struct list_head list;
  341. const struct team_mode *mode;
  342. };
  343. static struct team_mode_item *__find_mode(const char *kind)
  344. {
  345. struct team_mode_item *mitem;
  346. list_for_each_entry(mitem, &mode_list, list) {
  347. if (strcmp(mitem->mode->kind, kind) == 0)
  348. return mitem;
  349. }
  350. return NULL;
  351. }
  352. static bool is_good_mode_name(const char *name)
  353. {
  354. while (*name != '\0') {
  355. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  356. return false;
  357. name++;
  358. }
  359. return true;
  360. }
  361. int team_mode_register(const struct team_mode *mode)
  362. {
  363. int err = 0;
  364. struct team_mode_item *mitem;
  365. if (!is_good_mode_name(mode->kind) ||
  366. mode->priv_size > TEAM_MODE_PRIV_SIZE)
  367. return -EINVAL;
  368. mitem = kmalloc(sizeof(*mitem), GFP_KERNEL);
  369. if (!mitem)
  370. return -ENOMEM;
  371. spin_lock(&mode_list_lock);
  372. if (__find_mode(mode->kind)) {
  373. err = -EEXIST;
  374. kfree(mitem);
  375. goto unlock;
  376. }
  377. mitem->mode = mode;
  378. list_add_tail(&mitem->list, &mode_list);
  379. unlock:
  380. spin_unlock(&mode_list_lock);
  381. return err;
  382. }
  383. EXPORT_SYMBOL(team_mode_register);
  384. void team_mode_unregister(const struct team_mode *mode)
  385. {
  386. struct team_mode_item *mitem;
  387. spin_lock(&mode_list_lock);
  388. mitem = __find_mode(mode->kind);
  389. if (mitem) {
  390. list_del_init(&mitem->list);
  391. kfree(mitem);
  392. }
  393. spin_unlock(&mode_list_lock);
  394. }
  395. EXPORT_SYMBOL(team_mode_unregister);
  396. static const struct team_mode *team_mode_get(const char *kind)
  397. {
  398. struct team_mode_item *mitem;
  399. const struct team_mode *mode = NULL;
  400. spin_lock(&mode_list_lock);
  401. mitem = __find_mode(kind);
  402. if (!mitem) {
  403. spin_unlock(&mode_list_lock);
  404. request_module("team-mode-%s", kind);
  405. spin_lock(&mode_list_lock);
  406. mitem = __find_mode(kind);
  407. }
  408. if (mitem) {
  409. mode = mitem->mode;
  410. if (!try_module_get(mode->owner))
  411. mode = NULL;
  412. }
  413. spin_unlock(&mode_list_lock);
  414. return mode;
  415. }
  416. static void team_mode_put(const struct team_mode *mode)
  417. {
  418. module_put(mode->owner);
  419. }
  420. static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
  421. {
  422. dev_kfree_skb_any(skb);
  423. return false;
  424. }
  425. static rx_handler_result_t team_dummy_receive(struct team *team,
  426. struct team_port *port,
  427. struct sk_buff *skb)
  428. {
  429. return RX_HANDLER_ANOTHER;
  430. }
  431. static const struct team_mode __team_no_mode = {
  432. .kind = "*NOMODE*",
  433. };
  434. static bool team_is_mode_set(struct team *team)
  435. {
  436. return team->mode != &__team_no_mode;
  437. }
  438. static void team_set_no_mode(struct team *team)
  439. {
  440. team->user_carrier_enabled = false;
  441. team->mode = &__team_no_mode;
  442. }
  443. static void team_adjust_ops(struct team *team)
  444. {
  445. /*
  446. * To avoid checks in rx/tx skb paths, ensure here that non-null and
  447. * correct ops are always set.
  448. */
  449. if (!team->en_port_count || !team_is_mode_set(team) ||
  450. !team->mode->ops->transmit)
  451. team->ops.transmit = team_dummy_transmit;
  452. else
  453. team->ops.transmit = team->mode->ops->transmit;
  454. if (!team->en_port_count || !team_is_mode_set(team) ||
  455. !team->mode->ops->receive)
  456. team->ops.receive = team_dummy_receive;
  457. else
  458. team->ops.receive = team->mode->ops->receive;
  459. }
  460. /*
  461. * We can benefit from the fact that it's ensured no port is present
  462. * at the time of mode change. Therefore no packets are in fly so there's no
  463. * need to set mode operations in any special way.
  464. */
  465. static int __team_change_mode(struct team *team,
  466. const struct team_mode *new_mode)
  467. {
  468. /* Check if mode was previously set and do cleanup if so */
  469. if (team_is_mode_set(team)) {
  470. void (*exit_op)(struct team *team) = team->ops.exit;
  471. /* Clear ops area so no callback is called any longer */
  472. memset(&team->ops, 0, sizeof(struct team_mode_ops));
  473. team_adjust_ops(team);
  474. if (exit_op)
  475. exit_op(team);
  476. team_mode_put(team->mode);
  477. team_set_no_mode(team);
  478. /* zero private data area */
  479. memset(&team->mode_priv, 0,
  480. sizeof(struct team) - offsetof(struct team, mode_priv));
  481. }
  482. if (!new_mode)
  483. return 0;
  484. if (new_mode->ops->init) {
  485. int err;
  486. err = new_mode->ops->init(team);
  487. if (err)
  488. return err;
  489. }
  490. team->mode = new_mode;
  491. memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
  492. team_adjust_ops(team);
  493. return 0;
  494. }
  495. static int team_change_mode(struct team *team, const char *kind)
  496. {
  497. const struct team_mode *new_mode;
  498. struct net_device *dev = team->dev;
  499. int err;
  500. if (!list_empty(&team->port_list)) {
  501. netdev_err(dev, "No ports can be present during mode change\n");
  502. return -EBUSY;
  503. }
  504. if (team_is_mode_set(team) && strcmp(team->mode->kind, kind) == 0) {
  505. netdev_err(dev, "Unable to change to the same mode the team is in\n");
  506. return -EINVAL;
  507. }
  508. new_mode = team_mode_get(kind);
  509. if (!new_mode) {
  510. netdev_err(dev, "Mode \"%s\" not found\n", kind);
  511. return -EINVAL;
  512. }
  513. err = __team_change_mode(team, new_mode);
  514. if (err) {
  515. netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
  516. team_mode_put(new_mode);
  517. return err;
  518. }
  519. netdev_info(dev, "Mode changed to \"%s\"\n", kind);
  520. return 0;
  521. }
  522. /*********************
  523. * Peers notification
  524. *********************/
  525. static void team_notify_peers_work(struct work_struct *work)
  526. {
  527. struct team *team;
  528. int val;
  529. team = container_of(work, struct team, notify_peers.dw.work);
  530. if (!rtnl_trylock()) {
  531. schedule_delayed_work(&team->notify_peers.dw, 0);
  532. return;
  533. }
  534. val = atomic_dec_if_positive(&team->notify_peers.count_pending);
  535. if (val < 0) {
  536. rtnl_unlock();
  537. return;
  538. }
  539. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, team->dev);
  540. rtnl_unlock();
  541. if (val)
  542. schedule_delayed_work(&team->notify_peers.dw,
  543. msecs_to_jiffies(team->notify_peers.interval));
  544. }
  545. static void team_notify_peers(struct team *team)
  546. {
  547. if (!team->notify_peers.count || !netif_running(team->dev))
  548. return;
  549. atomic_add(team->notify_peers.count, &team->notify_peers.count_pending);
  550. schedule_delayed_work(&team->notify_peers.dw, 0);
  551. }
  552. static void team_notify_peers_init(struct team *team)
  553. {
  554. INIT_DELAYED_WORK(&team->notify_peers.dw, team_notify_peers_work);
  555. }
  556. static void team_notify_peers_fini(struct team *team)
  557. {
  558. cancel_delayed_work_sync(&team->notify_peers.dw);
  559. }
  560. /*******************************
  561. * Send multicast group rejoins
  562. *******************************/
  563. static void team_mcast_rejoin_work(struct work_struct *work)
  564. {
  565. struct team *team;
  566. int val;
  567. team = container_of(work, struct team, mcast_rejoin.dw.work);
  568. if (!rtnl_trylock()) {
  569. schedule_delayed_work(&team->mcast_rejoin.dw, 0);
  570. return;
  571. }
  572. val = atomic_dec_if_positive(&team->mcast_rejoin.count_pending);
  573. if (val < 0) {
  574. rtnl_unlock();
  575. return;
  576. }
  577. call_netdevice_notifiers(NETDEV_RESEND_IGMP, team->dev);
  578. rtnl_unlock();
  579. if (val)
  580. schedule_delayed_work(&team->mcast_rejoin.dw,
  581. msecs_to_jiffies(team->mcast_rejoin.interval));
  582. }
  583. static void team_mcast_rejoin(struct team *team)
  584. {
  585. if (!team->mcast_rejoin.count || !netif_running(team->dev))
  586. return;
  587. atomic_add(team->mcast_rejoin.count, &team->mcast_rejoin.count_pending);
  588. schedule_delayed_work(&team->mcast_rejoin.dw, 0);
  589. }
  590. static void team_mcast_rejoin_init(struct team *team)
  591. {
  592. INIT_DELAYED_WORK(&team->mcast_rejoin.dw, team_mcast_rejoin_work);
  593. }
  594. static void team_mcast_rejoin_fini(struct team *team)
  595. {
  596. cancel_delayed_work_sync(&team->mcast_rejoin.dw);
  597. }
  598. /************************
  599. * Rx path frame handler
  600. ************************/
  601. /* note: already called with rcu_read_lock */
  602. static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
  603. {
  604. struct sk_buff *skb = *pskb;
  605. struct team_port *port;
  606. struct team *team;
  607. rx_handler_result_t res;
  608. skb = skb_share_check(skb, GFP_ATOMIC);
  609. if (!skb)
  610. return RX_HANDLER_CONSUMED;
  611. *pskb = skb;
  612. port = team_port_get_rcu(skb->dev);
  613. team = port->team;
  614. if (!team_port_enabled(port)) {
  615. /* allow exact match delivery for disabled ports */
  616. res = RX_HANDLER_EXACT;
  617. } else {
  618. res = team->ops.receive(team, port, skb);
  619. }
  620. if (res == RX_HANDLER_ANOTHER) {
  621. struct team_pcpu_stats *pcpu_stats;
  622. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  623. u64_stats_update_begin(&pcpu_stats->syncp);
  624. pcpu_stats->rx_packets++;
  625. pcpu_stats->rx_bytes += skb->len;
  626. if (skb->pkt_type == PACKET_MULTICAST)
  627. pcpu_stats->rx_multicast++;
  628. u64_stats_update_end(&pcpu_stats->syncp);
  629. skb->dev = team->dev;
  630. } else if (res == RX_HANDLER_EXACT) {
  631. this_cpu_inc(team->pcpu_stats->rx_nohandler);
  632. } else {
  633. this_cpu_inc(team->pcpu_stats->rx_dropped);
  634. }
  635. return res;
  636. }
  637. /*************************************
  638. * Multiqueue Tx port select override
  639. *************************************/
  640. static int team_queue_override_init(struct team *team)
  641. {
  642. struct list_head *listarr;
  643. unsigned int queue_cnt = team->dev->num_tx_queues - 1;
  644. unsigned int i;
  645. if (!queue_cnt)
  646. return 0;
  647. listarr = kmalloc_array(queue_cnt, sizeof(struct list_head),
  648. GFP_KERNEL);
  649. if (!listarr)
  650. return -ENOMEM;
  651. team->qom_lists = listarr;
  652. for (i = 0; i < queue_cnt; i++)
  653. INIT_LIST_HEAD(listarr++);
  654. return 0;
  655. }
  656. static void team_queue_override_fini(struct team *team)
  657. {
  658. kfree(team->qom_lists);
  659. }
  660. static struct list_head *__team_get_qom_list(struct team *team, u16 queue_id)
  661. {
  662. return &team->qom_lists[queue_id - 1];
  663. }
  664. /*
  665. * note: already called with rcu_read_lock
  666. */
  667. static bool team_queue_override_transmit(struct team *team, struct sk_buff *skb)
  668. {
  669. struct list_head *qom_list;
  670. struct team_port *port;
  671. if (!team->queue_override_enabled || !skb->queue_mapping)
  672. return false;
  673. qom_list = __team_get_qom_list(team, skb->queue_mapping);
  674. list_for_each_entry_rcu(port, qom_list, qom_list) {
  675. if (!team_dev_queue_xmit(team, port, skb))
  676. return true;
  677. }
  678. return false;
  679. }
  680. static void __team_queue_override_port_del(struct team *team,
  681. struct team_port *port)
  682. {
  683. if (!port->queue_id)
  684. return;
  685. list_del_rcu(&port->qom_list);
  686. }
  687. static bool team_queue_override_port_has_gt_prio_than(struct team_port *port,
  688. struct team_port *cur)
  689. {
  690. if (port->priority < cur->priority)
  691. return true;
  692. if (port->priority > cur->priority)
  693. return false;
  694. if (port->index < cur->index)
  695. return true;
  696. return false;
  697. }
  698. static void __team_queue_override_port_add(struct team *team,
  699. struct team_port *port)
  700. {
  701. struct team_port *cur;
  702. struct list_head *qom_list;
  703. struct list_head *node;
  704. if (!port->queue_id)
  705. return;
  706. qom_list = __team_get_qom_list(team, port->queue_id);
  707. node = qom_list;
  708. list_for_each_entry(cur, qom_list, qom_list) {
  709. if (team_queue_override_port_has_gt_prio_than(port, cur))
  710. break;
  711. node = &cur->qom_list;
  712. }
  713. list_add_tail_rcu(&port->qom_list, node);
  714. }
  715. static void __team_queue_override_enabled_check(struct team *team)
  716. {
  717. struct team_port *port;
  718. bool enabled = false;
  719. list_for_each_entry(port, &team->port_list, list) {
  720. if (port->queue_id) {
  721. enabled = true;
  722. break;
  723. }
  724. }
  725. if (enabled == team->queue_override_enabled)
  726. return;
  727. netdev_dbg(team->dev, "%s queue override\n",
  728. enabled ? "Enabling" : "Disabling");
  729. team->queue_override_enabled = enabled;
  730. }
  731. static void team_queue_override_port_prio_changed(struct team *team,
  732. struct team_port *port)
  733. {
  734. if (!port->queue_id || team_port_enabled(port))
  735. return;
  736. __team_queue_override_port_del(team, port);
  737. __team_queue_override_port_add(team, port);
  738. __team_queue_override_enabled_check(team);
  739. }
  740. static void team_queue_override_port_change_queue_id(struct team *team,
  741. struct team_port *port,
  742. u16 new_queue_id)
  743. {
  744. if (team_port_enabled(port)) {
  745. __team_queue_override_port_del(team, port);
  746. port->queue_id = new_queue_id;
  747. __team_queue_override_port_add(team, port);
  748. __team_queue_override_enabled_check(team);
  749. } else {
  750. port->queue_id = new_queue_id;
  751. }
  752. }
  753. static void team_queue_override_port_add(struct team *team,
  754. struct team_port *port)
  755. {
  756. __team_queue_override_port_add(team, port);
  757. __team_queue_override_enabled_check(team);
  758. }
  759. static void team_queue_override_port_del(struct team *team,
  760. struct team_port *port)
  761. {
  762. __team_queue_override_port_del(team, port);
  763. __team_queue_override_enabled_check(team);
  764. }
  765. /****************
  766. * Port handling
  767. ****************/
  768. static bool team_port_find(const struct team *team,
  769. const struct team_port *port)
  770. {
  771. struct team_port *cur;
  772. list_for_each_entry(cur, &team->port_list, list)
  773. if (cur == port)
  774. return true;
  775. return false;
  776. }
  777. /*
  778. * Enable/disable port by adding to enabled port hashlist and setting
  779. * port->index (Might be racy so reader could see incorrect ifindex when
  780. * processing a flying packet, but that is not a problem). Write guarded
  781. * by team->lock.
  782. */
  783. static void team_port_enable(struct team *team,
  784. struct team_port *port)
  785. {
  786. if (team_port_enabled(port))
  787. return;
  788. port->index = team->en_port_count++;
  789. hlist_add_head_rcu(&port->hlist,
  790. team_port_index_hash(team, port->index));
  791. team_adjust_ops(team);
  792. team_queue_override_port_add(team, port);
  793. if (team->ops.port_enabled)
  794. team->ops.port_enabled(team, port);
  795. team_notify_peers(team);
  796. team_mcast_rejoin(team);
  797. team_lower_state_changed(port);
  798. }
  799. static void __reconstruct_port_hlist(struct team *team, int rm_index)
  800. {
  801. int i;
  802. struct team_port *port;
  803. for (i = rm_index + 1; i < team->en_port_count; i++) {
  804. port = team_get_port_by_index(team, i);
  805. hlist_del_rcu(&port->hlist);
  806. port->index--;
  807. hlist_add_head_rcu(&port->hlist,
  808. team_port_index_hash(team, port->index));
  809. }
  810. }
  811. static void team_port_disable(struct team *team,
  812. struct team_port *port)
  813. {
  814. if (!team_port_enabled(port))
  815. return;
  816. if (team->ops.port_disabled)
  817. team->ops.port_disabled(team, port);
  818. hlist_del_rcu(&port->hlist);
  819. __reconstruct_port_hlist(team, port->index);
  820. port->index = -1;
  821. team->en_port_count--;
  822. team_queue_override_port_del(team, port);
  823. team_adjust_ops(team);
  824. team_lower_state_changed(port);
  825. }
  826. #define TEAM_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
  827. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  828. NETIF_F_HIGHDMA | NETIF_F_LRO)
  829. #define TEAM_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
  830. NETIF_F_RXCSUM | NETIF_F_ALL_TSO)
  831. static void __team_compute_features(struct team *team)
  832. {
  833. struct team_port *port;
  834. netdev_features_t vlan_features = TEAM_VLAN_FEATURES &
  835. NETIF_F_ALL_FOR_ALL;
  836. netdev_features_t enc_features = TEAM_ENC_FEATURES;
  837. unsigned short max_hard_header_len = ETH_HLEN;
  838. unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
  839. IFF_XMIT_DST_RELEASE_PERM;
  840. list_for_each_entry(port, &team->port_list, list) {
  841. vlan_features = netdev_increment_features(vlan_features,
  842. port->dev->vlan_features,
  843. TEAM_VLAN_FEATURES);
  844. enc_features =
  845. netdev_increment_features(enc_features,
  846. port->dev->hw_enc_features,
  847. TEAM_ENC_FEATURES);
  848. dst_release_flag &= port->dev->priv_flags;
  849. if (port->dev->hard_header_len > max_hard_header_len)
  850. max_hard_header_len = port->dev->hard_header_len;
  851. }
  852. team->dev->vlan_features = vlan_features;
  853. team->dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
  854. NETIF_F_HW_VLAN_CTAG_TX |
  855. NETIF_F_HW_VLAN_STAG_TX |
  856. NETIF_F_GSO_UDP_L4;
  857. team->dev->hard_header_len = max_hard_header_len;
  858. team->dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  859. if (dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
  860. team->dev->priv_flags |= IFF_XMIT_DST_RELEASE;
  861. }
  862. static void team_compute_features(struct team *team)
  863. {
  864. mutex_lock(&team->lock);
  865. __team_compute_features(team);
  866. mutex_unlock(&team->lock);
  867. netdev_change_features(team->dev);
  868. }
  869. static int team_port_enter(struct team *team, struct team_port *port)
  870. {
  871. int err = 0;
  872. dev_hold(team->dev);
  873. if (team->ops.port_enter) {
  874. err = team->ops.port_enter(team, port);
  875. if (err) {
  876. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  877. port->dev->name);
  878. goto err_port_enter;
  879. }
  880. }
  881. return 0;
  882. err_port_enter:
  883. dev_put(team->dev);
  884. return err;
  885. }
  886. static void team_port_leave(struct team *team, struct team_port *port)
  887. {
  888. if (team->ops.port_leave)
  889. team->ops.port_leave(team, port);
  890. dev_put(team->dev);
  891. }
  892. #ifdef CONFIG_NET_POLL_CONTROLLER
  893. static int __team_port_enable_netpoll(struct team_port *port)
  894. {
  895. struct netpoll *np;
  896. int err;
  897. np = kzalloc(sizeof(*np), GFP_KERNEL);
  898. if (!np)
  899. return -ENOMEM;
  900. err = __netpoll_setup(np, port->dev);
  901. if (err) {
  902. kfree(np);
  903. return err;
  904. }
  905. port->np = np;
  906. return err;
  907. }
  908. static int team_port_enable_netpoll(struct team_port *port)
  909. {
  910. if (!port->team->dev->npinfo)
  911. return 0;
  912. return __team_port_enable_netpoll(port);
  913. }
  914. static void team_port_disable_netpoll(struct team_port *port)
  915. {
  916. struct netpoll *np = port->np;
  917. if (!np)
  918. return;
  919. port->np = NULL;
  920. /* Wait for transmitting packets to finish before freeing. */
  921. synchronize_rcu_bh();
  922. __netpoll_cleanup(np);
  923. kfree(np);
  924. }
  925. #else
  926. static int team_port_enable_netpoll(struct team_port *port)
  927. {
  928. return 0;
  929. }
  930. static void team_port_disable_netpoll(struct team_port *port)
  931. {
  932. }
  933. #endif
  934. static int team_upper_dev_link(struct team *team, struct team_port *port,
  935. struct netlink_ext_ack *extack)
  936. {
  937. struct netdev_lag_upper_info lag_upper_info;
  938. int err;
  939. lag_upper_info.tx_type = team->mode->lag_tx_type;
  940. lag_upper_info.hash_type = NETDEV_LAG_HASH_UNKNOWN;
  941. err = netdev_master_upper_dev_link(port->dev, team->dev, NULL,
  942. &lag_upper_info, extack);
  943. if (err)
  944. return err;
  945. port->dev->priv_flags |= IFF_TEAM_PORT;
  946. return 0;
  947. }
  948. static void team_upper_dev_unlink(struct team *team, struct team_port *port)
  949. {
  950. netdev_upper_dev_unlink(port->dev, team->dev);
  951. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  952. }
  953. static void __team_port_change_port_added(struct team_port *port, bool linkup);
  954. static int team_dev_type_check_change(struct net_device *dev,
  955. struct net_device *port_dev);
  956. static int team_port_add(struct team *team, struct net_device *port_dev,
  957. struct netlink_ext_ack *extack)
  958. {
  959. struct net_device *dev = team->dev;
  960. struct team_port *port;
  961. char *portname = port_dev->name;
  962. int err;
  963. if (port_dev->flags & IFF_LOOPBACK) {
  964. NL_SET_ERR_MSG(extack, "Loopback device can't be added as a team port");
  965. netdev_err(dev, "Device %s is loopback device. Loopback devices can't be added as a team port\n",
  966. portname);
  967. return -EINVAL;
  968. }
  969. if (team_port_exists(port_dev)) {
  970. NL_SET_ERR_MSG(extack, "Device is already a port of a team device");
  971. netdev_err(dev, "Device %s is already a port "
  972. "of a team device\n", portname);
  973. return -EBUSY;
  974. }
  975. if (dev == port_dev) {
  976. NL_SET_ERR_MSG(extack, "Cannot enslave team device to itself");
  977. netdev_err(dev, "Cannot enslave team device to itself\n");
  978. return -EINVAL;
  979. }
  980. if (netdev_has_upper_dev(dev, port_dev)) {
  981. NL_SET_ERR_MSG(extack, "Device is already an upper device of the team interface");
  982. netdev_err(dev, "Device %s is already an upper device of the team interface\n",
  983. portname);
  984. return -EBUSY;
  985. }
  986. if (port_dev->features & NETIF_F_VLAN_CHALLENGED &&
  987. vlan_uses_dev(dev)) {
  988. NL_SET_ERR_MSG(extack, "Device is VLAN challenged and team device has VLAN set up");
  989. netdev_err(dev, "Device %s is VLAN challenged and team device has VLAN set up\n",
  990. portname);
  991. return -EPERM;
  992. }
  993. err = team_dev_type_check_change(dev, port_dev);
  994. if (err)
  995. return err;
  996. if (port_dev->flags & IFF_UP) {
  997. NL_SET_ERR_MSG(extack, "Device is up. Set it down before adding it as a team port");
  998. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  999. portname);
  1000. return -EBUSY;
  1001. }
  1002. port = kzalloc(sizeof(struct team_port) + team->mode->port_priv_size,
  1003. GFP_KERNEL);
  1004. if (!port)
  1005. return -ENOMEM;
  1006. port->dev = port_dev;
  1007. port->team = team;
  1008. INIT_LIST_HEAD(&port->qom_list);
  1009. port->orig.mtu = port_dev->mtu;
  1010. err = dev_set_mtu(port_dev, dev->mtu);
  1011. if (err) {
  1012. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  1013. goto err_set_mtu;
  1014. }
  1015. memcpy(port->orig.dev_addr, port_dev->dev_addr, port_dev->addr_len);
  1016. err = team_port_enter(team, port);
  1017. if (err) {
  1018. netdev_err(dev, "Device %s failed to enter team mode\n",
  1019. portname);
  1020. goto err_port_enter;
  1021. }
  1022. err = dev_open(port_dev);
  1023. if (err) {
  1024. netdev_dbg(dev, "Device %s opening failed\n",
  1025. portname);
  1026. goto err_dev_open;
  1027. }
  1028. err = vlan_vids_add_by_dev(port_dev, dev);
  1029. if (err) {
  1030. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  1031. portname);
  1032. goto err_vids_add;
  1033. }
  1034. err = team_port_enable_netpoll(port);
  1035. if (err) {
  1036. netdev_err(dev, "Failed to enable netpoll on device %s\n",
  1037. portname);
  1038. goto err_enable_netpoll;
  1039. }
  1040. if (!(dev->features & NETIF_F_LRO))
  1041. dev_disable_lro(port_dev);
  1042. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  1043. port);
  1044. if (err) {
  1045. netdev_err(dev, "Device %s failed to register rx_handler\n",
  1046. portname);
  1047. goto err_handler_register;
  1048. }
  1049. err = team_upper_dev_link(team, port, extack);
  1050. if (err) {
  1051. netdev_err(dev, "Device %s failed to set upper link\n",
  1052. portname);
  1053. goto err_set_upper_link;
  1054. }
  1055. err = __team_option_inst_add_port(team, port);
  1056. if (err) {
  1057. netdev_err(dev, "Device %s failed to add per-port options\n",
  1058. portname);
  1059. goto err_option_port_add;
  1060. }
  1061. /* set promiscuity level to new slave */
  1062. if (dev->flags & IFF_PROMISC) {
  1063. err = dev_set_promiscuity(port_dev, 1);
  1064. if (err)
  1065. goto err_set_slave_promisc;
  1066. }
  1067. /* set allmulti level to new slave */
  1068. if (dev->flags & IFF_ALLMULTI) {
  1069. err = dev_set_allmulti(port_dev, 1);
  1070. if (err) {
  1071. if (dev->flags & IFF_PROMISC)
  1072. dev_set_promiscuity(port_dev, -1);
  1073. goto err_set_slave_promisc;
  1074. }
  1075. }
  1076. netif_addr_lock_bh(dev);
  1077. dev_uc_sync_multiple(port_dev, dev);
  1078. dev_mc_sync_multiple(port_dev, dev);
  1079. netif_addr_unlock_bh(dev);
  1080. port->index = -1;
  1081. list_add_tail_rcu(&port->list, &team->port_list);
  1082. team_port_enable(team, port);
  1083. __team_compute_features(team);
  1084. __team_port_change_port_added(port, !!netif_oper_up(port_dev));
  1085. __team_options_change_check(team);
  1086. netdev_info(dev, "Port device %s added\n", portname);
  1087. return 0;
  1088. err_set_slave_promisc:
  1089. __team_option_inst_del_port(team, port);
  1090. err_option_port_add:
  1091. team_upper_dev_unlink(team, port);
  1092. err_set_upper_link:
  1093. netdev_rx_handler_unregister(port_dev);
  1094. err_handler_register:
  1095. team_port_disable_netpoll(port);
  1096. err_enable_netpoll:
  1097. vlan_vids_del_by_dev(port_dev, dev);
  1098. err_vids_add:
  1099. dev_close(port_dev);
  1100. err_dev_open:
  1101. team_port_leave(team, port);
  1102. team_port_set_orig_dev_addr(port);
  1103. err_port_enter:
  1104. dev_set_mtu(port_dev, port->orig.mtu);
  1105. err_set_mtu:
  1106. kfree(port);
  1107. return err;
  1108. }
  1109. static void __team_port_change_port_removed(struct team_port *port);
  1110. static int team_port_del(struct team *team, struct net_device *port_dev)
  1111. {
  1112. struct net_device *dev = team->dev;
  1113. struct team_port *port;
  1114. char *portname = port_dev->name;
  1115. port = team_port_get_rtnl(port_dev);
  1116. if (!port || !team_port_find(team, port)) {
  1117. netdev_err(dev, "Device %s does not act as a port of this team\n",
  1118. portname);
  1119. return -ENOENT;
  1120. }
  1121. team_port_disable(team, port);
  1122. list_del_rcu(&port->list);
  1123. if (dev->flags & IFF_PROMISC)
  1124. dev_set_promiscuity(port_dev, -1);
  1125. if (dev->flags & IFF_ALLMULTI)
  1126. dev_set_allmulti(port_dev, -1);
  1127. team_upper_dev_unlink(team, port);
  1128. netdev_rx_handler_unregister(port_dev);
  1129. team_port_disable_netpoll(port);
  1130. vlan_vids_del_by_dev(port_dev, dev);
  1131. dev_uc_unsync(port_dev, dev);
  1132. dev_mc_unsync(port_dev, dev);
  1133. dev_close(port_dev);
  1134. team_port_leave(team, port);
  1135. __team_option_inst_mark_removed_port(team, port);
  1136. __team_options_change_check(team);
  1137. __team_option_inst_del_port(team, port);
  1138. __team_port_change_port_removed(port);
  1139. team_port_set_orig_dev_addr(port);
  1140. dev_set_mtu(port_dev, port->orig.mtu);
  1141. kfree_rcu(port, rcu);
  1142. netdev_info(dev, "Port device %s removed\n", portname);
  1143. __team_compute_features(team);
  1144. return 0;
  1145. }
  1146. /*****************
  1147. * Net device ops
  1148. *****************/
  1149. static int team_mode_option_get(struct team *team, struct team_gsetter_ctx *ctx)
  1150. {
  1151. ctx->data.str_val = team->mode->kind;
  1152. return 0;
  1153. }
  1154. static int team_mode_option_set(struct team *team, struct team_gsetter_ctx *ctx)
  1155. {
  1156. return team_change_mode(team, ctx->data.str_val);
  1157. }
  1158. static int team_notify_peers_count_get(struct team *team,
  1159. struct team_gsetter_ctx *ctx)
  1160. {
  1161. ctx->data.u32_val = team->notify_peers.count;
  1162. return 0;
  1163. }
  1164. static int team_notify_peers_count_set(struct team *team,
  1165. struct team_gsetter_ctx *ctx)
  1166. {
  1167. team->notify_peers.count = ctx->data.u32_val;
  1168. return 0;
  1169. }
  1170. static int team_notify_peers_interval_get(struct team *team,
  1171. struct team_gsetter_ctx *ctx)
  1172. {
  1173. ctx->data.u32_val = team->notify_peers.interval;
  1174. return 0;
  1175. }
  1176. static int team_notify_peers_interval_set(struct team *team,
  1177. struct team_gsetter_ctx *ctx)
  1178. {
  1179. team->notify_peers.interval = ctx->data.u32_val;
  1180. return 0;
  1181. }
  1182. static int team_mcast_rejoin_count_get(struct team *team,
  1183. struct team_gsetter_ctx *ctx)
  1184. {
  1185. ctx->data.u32_val = team->mcast_rejoin.count;
  1186. return 0;
  1187. }
  1188. static int team_mcast_rejoin_count_set(struct team *team,
  1189. struct team_gsetter_ctx *ctx)
  1190. {
  1191. team->mcast_rejoin.count = ctx->data.u32_val;
  1192. return 0;
  1193. }
  1194. static int team_mcast_rejoin_interval_get(struct team *team,
  1195. struct team_gsetter_ctx *ctx)
  1196. {
  1197. ctx->data.u32_val = team->mcast_rejoin.interval;
  1198. return 0;
  1199. }
  1200. static int team_mcast_rejoin_interval_set(struct team *team,
  1201. struct team_gsetter_ctx *ctx)
  1202. {
  1203. team->mcast_rejoin.interval = ctx->data.u32_val;
  1204. return 0;
  1205. }
  1206. static int team_port_en_option_get(struct team *team,
  1207. struct team_gsetter_ctx *ctx)
  1208. {
  1209. struct team_port *port = ctx->info->port;
  1210. ctx->data.bool_val = team_port_enabled(port);
  1211. return 0;
  1212. }
  1213. static int team_port_en_option_set(struct team *team,
  1214. struct team_gsetter_ctx *ctx)
  1215. {
  1216. struct team_port *port = ctx->info->port;
  1217. if (ctx->data.bool_val)
  1218. team_port_enable(team, port);
  1219. else
  1220. team_port_disable(team, port);
  1221. return 0;
  1222. }
  1223. static int team_user_linkup_option_get(struct team *team,
  1224. struct team_gsetter_ctx *ctx)
  1225. {
  1226. struct team_port *port = ctx->info->port;
  1227. ctx->data.bool_val = port->user.linkup;
  1228. return 0;
  1229. }
  1230. static void __team_carrier_check(struct team *team);
  1231. static int team_user_linkup_option_set(struct team *team,
  1232. struct team_gsetter_ctx *ctx)
  1233. {
  1234. struct team_port *port = ctx->info->port;
  1235. port->user.linkup = ctx->data.bool_val;
  1236. team_refresh_port_linkup(port);
  1237. __team_carrier_check(port->team);
  1238. return 0;
  1239. }
  1240. static int team_user_linkup_en_option_get(struct team *team,
  1241. struct team_gsetter_ctx *ctx)
  1242. {
  1243. struct team_port *port = ctx->info->port;
  1244. ctx->data.bool_val = port->user.linkup_enabled;
  1245. return 0;
  1246. }
  1247. static int team_user_linkup_en_option_set(struct team *team,
  1248. struct team_gsetter_ctx *ctx)
  1249. {
  1250. struct team_port *port = ctx->info->port;
  1251. port->user.linkup_enabled = ctx->data.bool_val;
  1252. team_refresh_port_linkup(port);
  1253. __team_carrier_check(port->team);
  1254. return 0;
  1255. }
  1256. static int team_priority_option_get(struct team *team,
  1257. struct team_gsetter_ctx *ctx)
  1258. {
  1259. struct team_port *port = ctx->info->port;
  1260. ctx->data.s32_val = port->priority;
  1261. return 0;
  1262. }
  1263. static int team_priority_option_set(struct team *team,
  1264. struct team_gsetter_ctx *ctx)
  1265. {
  1266. struct team_port *port = ctx->info->port;
  1267. s32 priority = ctx->data.s32_val;
  1268. if (port->priority == priority)
  1269. return 0;
  1270. port->priority = priority;
  1271. team_queue_override_port_prio_changed(team, port);
  1272. return 0;
  1273. }
  1274. static int team_queue_id_option_get(struct team *team,
  1275. struct team_gsetter_ctx *ctx)
  1276. {
  1277. struct team_port *port = ctx->info->port;
  1278. ctx->data.u32_val = port->queue_id;
  1279. return 0;
  1280. }
  1281. static int team_queue_id_option_set(struct team *team,
  1282. struct team_gsetter_ctx *ctx)
  1283. {
  1284. struct team_port *port = ctx->info->port;
  1285. u16 new_queue_id = ctx->data.u32_val;
  1286. if (port->queue_id == new_queue_id)
  1287. return 0;
  1288. if (new_queue_id >= team->dev->real_num_tx_queues)
  1289. return -EINVAL;
  1290. team_queue_override_port_change_queue_id(team, port, new_queue_id);
  1291. return 0;
  1292. }
  1293. static const struct team_option team_options[] = {
  1294. {
  1295. .name = "mode",
  1296. .type = TEAM_OPTION_TYPE_STRING,
  1297. .getter = team_mode_option_get,
  1298. .setter = team_mode_option_set,
  1299. },
  1300. {
  1301. .name = "notify_peers_count",
  1302. .type = TEAM_OPTION_TYPE_U32,
  1303. .getter = team_notify_peers_count_get,
  1304. .setter = team_notify_peers_count_set,
  1305. },
  1306. {
  1307. .name = "notify_peers_interval",
  1308. .type = TEAM_OPTION_TYPE_U32,
  1309. .getter = team_notify_peers_interval_get,
  1310. .setter = team_notify_peers_interval_set,
  1311. },
  1312. {
  1313. .name = "mcast_rejoin_count",
  1314. .type = TEAM_OPTION_TYPE_U32,
  1315. .getter = team_mcast_rejoin_count_get,
  1316. .setter = team_mcast_rejoin_count_set,
  1317. },
  1318. {
  1319. .name = "mcast_rejoin_interval",
  1320. .type = TEAM_OPTION_TYPE_U32,
  1321. .getter = team_mcast_rejoin_interval_get,
  1322. .setter = team_mcast_rejoin_interval_set,
  1323. },
  1324. {
  1325. .name = "enabled",
  1326. .type = TEAM_OPTION_TYPE_BOOL,
  1327. .per_port = true,
  1328. .getter = team_port_en_option_get,
  1329. .setter = team_port_en_option_set,
  1330. },
  1331. {
  1332. .name = "user_linkup",
  1333. .type = TEAM_OPTION_TYPE_BOOL,
  1334. .per_port = true,
  1335. .getter = team_user_linkup_option_get,
  1336. .setter = team_user_linkup_option_set,
  1337. },
  1338. {
  1339. .name = "user_linkup_enabled",
  1340. .type = TEAM_OPTION_TYPE_BOOL,
  1341. .per_port = true,
  1342. .getter = team_user_linkup_en_option_get,
  1343. .setter = team_user_linkup_en_option_set,
  1344. },
  1345. {
  1346. .name = "priority",
  1347. .type = TEAM_OPTION_TYPE_S32,
  1348. .per_port = true,
  1349. .getter = team_priority_option_get,
  1350. .setter = team_priority_option_set,
  1351. },
  1352. {
  1353. .name = "queue_id",
  1354. .type = TEAM_OPTION_TYPE_U32,
  1355. .per_port = true,
  1356. .getter = team_queue_id_option_get,
  1357. .setter = team_queue_id_option_set,
  1358. },
  1359. };
  1360. static int team_init(struct net_device *dev)
  1361. {
  1362. struct team *team = netdev_priv(dev);
  1363. int i;
  1364. int err;
  1365. team->dev = dev;
  1366. mutex_init(&team->lock);
  1367. team_set_no_mode(team);
  1368. team->pcpu_stats = netdev_alloc_pcpu_stats(struct team_pcpu_stats);
  1369. if (!team->pcpu_stats)
  1370. return -ENOMEM;
  1371. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  1372. INIT_HLIST_HEAD(&team->en_port_hlist[i]);
  1373. INIT_LIST_HEAD(&team->port_list);
  1374. err = team_queue_override_init(team);
  1375. if (err)
  1376. goto err_team_queue_override_init;
  1377. team_adjust_ops(team);
  1378. INIT_LIST_HEAD(&team->option_list);
  1379. INIT_LIST_HEAD(&team->option_inst_list);
  1380. team_notify_peers_init(team);
  1381. team_mcast_rejoin_init(team);
  1382. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  1383. if (err)
  1384. goto err_options_register;
  1385. netif_carrier_off(dev);
  1386. netdev_lockdep_set_classes(dev);
  1387. return 0;
  1388. err_options_register:
  1389. team_mcast_rejoin_fini(team);
  1390. team_notify_peers_fini(team);
  1391. team_queue_override_fini(team);
  1392. err_team_queue_override_init:
  1393. free_percpu(team->pcpu_stats);
  1394. return err;
  1395. }
  1396. static void team_uninit(struct net_device *dev)
  1397. {
  1398. struct team *team = netdev_priv(dev);
  1399. struct team_port *port;
  1400. struct team_port *tmp;
  1401. mutex_lock(&team->lock);
  1402. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  1403. team_port_del(team, port->dev);
  1404. __team_change_mode(team, NULL); /* cleanup */
  1405. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  1406. team_mcast_rejoin_fini(team);
  1407. team_notify_peers_fini(team);
  1408. team_queue_override_fini(team);
  1409. mutex_unlock(&team->lock);
  1410. netdev_change_features(dev);
  1411. }
  1412. static void team_destructor(struct net_device *dev)
  1413. {
  1414. struct team *team = netdev_priv(dev);
  1415. free_percpu(team->pcpu_stats);
  1416. }
  1417. static int team_open(struct net_device *dev)
  1418. {
  1419. return 0;
  1420. }
  1421. static int team_close(struct net_device *dev)
  1422. {
  1423. return 0;
  1424. }
  1425. /*
  1426. * note: already called with rcu_read_lock
  1427. */
  1428. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  1429. {
  1430. struct team *team = netdev_priv(dev);
  1431. bool tx_success;
  1432. unsigned int len = skb->len;
  1433. tx_success = team_queue_override_transmit(team, skb);
  1434. if (!tx_success)
  1435. tx_success = team->ops.transmit(team, skb);
  1436. if (tx_success) {
  1437. struct team_pcpu_stats *pcpu_stats;
  1438. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  1439. u64_stats_update_begin(&pcpu_stats->syncp);
  1440. pcpu_stats->tx_packets++;
  1441. pcpu_stats->tx_bytes += len;
  1442. u64_stats_update_end(&pcpu_stats->syncp);
  1443. } else {
  1444. this_cpu_inc(team->pcpu_stats->tx_dropped);
  1445. }
  1446. return NETDEV_TX_OK;
  1447. }
  1448. static u16 team_select_queue(struct net_device *dev, struct sk_buff *skb,
  1449. struct net_device *sb_dev,
  1450. select_queue_fallback_t fallback)
  1451. {
  1452. /*
  1453. * This helper function exists to help dev_pick_tx get the correct
  1454. * destination queue. Using a helper function skips a call to
  1455. * skb_tx_hash and will put the skbs in the queue we expect on their
  1456. * way down to the team driver.
  1457. */
  1458. u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  1459. /*
  1460. * Save the original txq to restore before passing to the driver
  1461. */
  1462. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  1463. if (unlikely(txq >= dev->real_num_tx_queues)) {
  1464. do {
  1465. txq -= dev->real_num_tx_queues;
  1466. } while (txq >= dev->real_num_tx_queues);
  1467. }
  1468. return txq;
  1469. }
  1470. static void team_change_rx_flags(struct net_device *dev, int change)
  1471. {
  1472. struct team *team = netdev_priv(dev);
  1473. struct team_port *port;
  1474. int inc;
  1475. rcu_read_lock();
  1476. list_for_each_entry_rcu(port, &team->port_list, list) {
  1477. if (change & IFF_PROMISC) {
  1478. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  1479. dev_set_promiscuity(port->dev, inc);
  1480. }
  1481. if (change & IFF_ALLMULTI) {
  1482. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  1483. dev_set_allmulti(port->dev, inc);
  1484. }
  1485. }
  1486. rcu_read_unlock();
  1487. }
  1488. static void team_set_rx_mode(struct net_device *dev)
  1489. {
  1490. struct team *team = netdev_priv(dev);
  1491. struct team_port *port;
  1492. rcu_read_lock();
  1493. list_for_each_entry_rcu(port, &team->port_list, list) {
  1494. dev_uc_sync_multiple(port->dev, dev);
  1495. dev_mc_sync_multiple(port->dev, dev);
  1496. }
  1497. rcu_read_unlock();
  1498. }
  1499. static int team_set_mac_address(struct net_device *dev, void *p)
  1500. {
  1501. struct sockaddr *addr = p;
  1502. struct team *team = netdev_priv(dev);
  1503. struct team_port *port;
  1504. if (dev->type == ARPHRD_ETHER && !is_valid_ether_addr(addr->sa_data))
  1505. return -EADDRNOTAVAIL;
  1506. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1507. mutex_lock(&team->lock);
  1508. list_for_each_entry(port, &team->port_list, list)
  1509. if (team->ops.port_change_dev_addr)
  1510. team->ops.port_change_dev_addr(team, port);
  1511. mutex_unlock(&team->lock);
  1512. return 0;
  1513. }
  1514. static int team_change_mtu(struct net_device *dev, int new_mtu)
  1515. {
  1516. struct team *team = netdev_priv(dev);
  1517. struct team_port *port;
  1518. int err;
  1519. /*
  1520. * Alhough this is reader, it's guarded by team lock. It's not possible
  1521. * to traverse list in reverse under rcu_read_lock
  1522. */
  1523. mutex_lock(&team->lock);
  1524. team->port_mtu_change_allowed = true;
  1525. list_for_each_entry(port, &team->port_list, list) {
  1526. err = dev_set_mtu(port->dev, new_mtu);
  1527. if (err) {
  1528. netdev_err(dev, "Device %s failed to change mtu",
  1529. port->dev->name);
  1530. goto unwind;
  1531. }
  1532. }
  1533. team->port_mtu_change_allowed = false;
  1534. mutex_unlock(&team->lock);
  1535. dev->mtu = new_mtu;
  1536. return 0;
  1537. unwind:
  1538. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1539. dev_set_mtu(port->dev, dev->mtu);
  1540. team->port_mtu_change_allowed = false;
  1541. mutex_unlock(&team->lock);
  1542. return err;
  1543. }
  1544. static void
  1545. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1546. {
  1547. struct team *team = netdev_priv(dev);
  1548. struct team_pcpu_stats *p;
  1549. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  1550. u32 rx_dropped = 0, tx_dropped = 0, rx_nohandler = 0;
  1551. unsigned int start;
  1552. int i;
  1553. for_each_possible_cpu(i) {
  1554. p = per_cpu_ptr(team->pcpu_stats, i);
  1555. do {
  1556. start = u64_stats_fetch_begin_irq(&p->syncp);
  1557. rx_packets = p->rx_packets;
  1558. rx_bytes = p->rx_bytes;
  1559. rx_multicast = p->rx_multicast;
  1560. tx_packets = p->tx_packets;
  1561. tx_bytes = p->tx_bytes;
  1562. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  1563. stats->rx_packets += rx_packets;
  1564. stats->rx_bytes += rx_bytes;
  1565. stats->multicast += rx_multicast;
  1566. stats->tx_packets += tx_packets;
  1567. stats->tx_bytes += tx_bytes;
  1568. /*
  1569. * rx_dropped, tx_dropped & rx_nohandler are u32,
  1570. * updated without syncp protection.
  1571. */
  1572. rx_dropped += p->rx_dropped;
  1573. tx_dropped += p->tx_dropped;
  1574. rx_nohandler += p->rx_nohandler;
  1575. }
  1576. stats->rx_dropped = rx_dropped;
  1577. stats->tx_dropped = tx_dropped;
  1578. stats->rx_nohandler = rx_nohandler;
  1579. }
  1580. static int team_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  1581. {
  1582. struct team *team = netdev_priv(dev);
  1583. struct team_port *port;
  1584. int err;
  1585. /*
  1586. * Alhough this is reader, it's guarded by team lock. It's not possible
  1587. * to traverse list in reverse under rcu_read_lock
  1588. */
  1589. mutex_lock(&team->lock);
  1590. list_for_each_entry(port, &team->port_list, list) {
  1591. err = vlan_vid_add(port->dev, proto, vid);
  1592. if (err)
  1593. goto unwind;
  1594. }
  1595. mutex_unlock(&team->lock);
  1596. return 0;
  1597. unwind:
  1598. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  1599. vlan_vid_del(port->dev, proto, vid);
  1600. mutex_unlock(&team->lock);
  1601. return err;
  1602. }
  1603. static int team_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
  1604. {
  1605. struct team *team = netdev_priv(dev);
  1606. struct team_port *port;
  1607. mutex_lock(&team->lock);
  1608. list_for_each_entry(port, &team->port_list, list)
  1609. vlan_vid_del(port->dev, proto, vid);
  1610. mutex_unlock(&team->lock);
  1611. return 0;
  1612. }
  1613. #ifdef CONFIG_NET_POLL_CONTROLLER
  1614. static void team_poll_controller(struct net_device *dev)
  1615. {
  1616. }
  1617. static void __team_netpoll_cleanup(struct team *team)
  1618. {
  1619. struct team_port *port;
  1620. list_for_each_entry(port, &team->port_list, list)
  1621. team_port_disable_netpoll(port);
  1622. }
  1623. static void team_netpoll_cleanup(struct net_device *dev)
  1624. {
  1625. struct team *team = netdev_priv(dev);
  1626. mutex_lock(&team->lock);
  1627. __team_netpoll_cleanup(team);
  1628. mutex_unlock(&team->lock);
  1629. }
  1630. static int team_netpoll_setup(struct net_device *dev,
  1631. struct netpoll_info *npifo)
  1632. {
  1633. struct team *team = netdev_priv(dev);
  1634. struct team_port *port;
  1635. int err = 0;
  1636. mutex_lock(&team->lock);
  1637. list_for_each_entry(port, &team->port_list, list) {
  1638. err = __team_port_enable_netpoll(port);
  1639. if (err) {
  1640. __team_netpoll_cleanup(team);
  1641. break;
  1642. }
  1643. }
  1644. mutex_unlock(&team->lock);
  1645. return err;
  1646. }
  1647. #endif
  1648. static int team_add_slave(struct net_device *dev, struct net_device *port_dev,
  1649. struct netlink_ext_ack *extack)
  1650. {
  1651. struct team *team = netdev_priv(dev);
  1652. int err;
  1653. mutex_lock(&team->lock);
  1654. err = team_port_add(team, port_dev, extack);
  1655. mutex_unlock(&team->lock);
  1656. if (!err)
  1657. netdev_change_features(dev);
  1658. return err;
  1659. }
  1660. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  1661. {
  1662. struct team *team = netdev_priv(dev);
  1663. int err;
  1664. mutex_lock(&team->lock);
  1665. err = team_port_del(team, port_dev);
  1666. mutex_unlock(&team->lock);
  1667. if (!err)
  1668. netdev_change_features(dev);
  1669. return err;
  1670. }
  1671. static netdev_features_t team_fix_features(struct net_device *dev,
  1672. netdev_features_t features)
  1673. {
  1674. struct team_port *port;
  1675. struct team *team = netdev_priv(dev);
  1676. netdev_features_t mask;
  1677. mask = features;
  1678. features &= ~NETIF_F_ONE_FOR_ALL;
  1679. features |= NETIF_F_ALL_FOR_ALL;
  1680. rcu_read_lock();
  1681. list_for_each_entry_rcu(port, &team->port_list, list) {
  1682. features = netdev_increment_features(features,
  1683. port->dev->features,
  1684. mask);
  1685. }
  1686. rcu_read_unlock();
  1687. features = netdev_add_tso_features(features, mask);
  1688. return features;
  1689. }
  1690. static int team_change_carrier(struct net_device *dev, bool new_carrier)
  1691. {
  1692. struct team *team = netdev_priv(dev);
  1693. team->user_carrier_enabled = true;
  1694. if (new_carrier)
  1695. netif_carrier_on(dev);
  1696. else
  1697. netif_carrier_off(dev);
  1698. return 0;
  1699. }
  1700. static const struct net_device_ops team_netdev_ops = {
  1701. .ndo_init = team_init,
  1702. .ndo_uninit = team_uninit,
  1703. .ndo_open = team_open,
  1704. .ndo_stop = team_close,
  1705. .ndo_start_xmit = team_xmit,
  1706. .ndo_select_queue = team_select_queue,
  1707. .ndo_change_rx_flags = team_change_rx_flags,
  1708. .ndo_set_rx_mode = team_set_rx_mode,
  1709. .ndo_set_mac_address = team_set_mac_address,
  1710. .ndo_change_mtu = team_change_mtu,
  1711. .ndo_get_stats64 = team_get_stats64,
  1712. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  1713. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  1714. #ifdef CONFIG_NET_POLL_CONTROLLER
  1715. .ndo_poll_controller = team_poll_controller,
  1716. .ndo_netpoll_setup = team_netpoll_setup,
  1717. .ndo_netpoll_cleanup = team_netpoll_cleanup,
  1718. #endif
  1719. .ndo_add_slave = team_add_slave,
  1720. .ndo_del_slave = team_del_slave,
  1721. .ndo_fix_features = team_fix_features,
  1722. .ndo_change_carrier = team_change_carrier,
  1723. .ndo_features_check = passthru_features_check,
  1724. };
  1725. /***********************
  1726. * ethtool interface
  1727. ***********************/
  1728. static void team_ethtool_get_drvinfo(struct net_device *dev,
  1729. struct ethtool_drvinfo *drvinfo)
  1730. {
  1731. strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
  1732. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1733. }
  1734. static const struct ethtool_ops team_ethtool_ops = {
  1735. .get_drvinfo = team_ethtool_get_drvinfo,
  1736. .get_link = ethtool_op_get_link,
  1737. };
  1738. /***********************
  1739. * rt netlink interface
  1740. ***********************/
  1741. static void team_setup_by_port(struct net_device *dev,
  1742. struct net_device *port_dev)
  1743. {
  1744. dev->header_ops = port_dev->header_ops;
  1745. dev->type = port_dev->type;
  1746. dev->hard_header_len = port_dev->hard_header_len;
  1747. dev->addr_len = port_dev->addr_len;
  1748. dev->mtu = port_dev->mtu;
  1749. memcpy(dev->broadcast, port_dev->broadcast, port_dev->addr_len);
  1750. eth_hw_addr_inherit(dev, port_dev);
  1751. }
  1752. static int team_dev_type_check_change(struct net_device *dev,
  1753. struct net_device *port_dev)
  1754. {
  1755. struct team *team = netdev_priv(dev);
  1756. char *portname = port_dev->name;
  1757. int err;
  1758. if (dev->type == port_dev->type)
  1759. return 0;
  1760. if (!list_empty(&team->port_list)) {
  1761. netdev_err(dev, "Device %s is of different type\n", portname);
  1762. return -EBUSY;
  1763. }
  1764. err = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, dev);
  1765. err = notifier_to_errno(err);
  1766. if (err) {
  1767. netdev_err(dev, "Refused to change device type\n");
  1768. return err;
  1769. }
  1770. dev_uc_flush(dev);
  1771. dev_mc_flush(dev);
  1772. team_setup_by_port(dev, port_dev);
  1773. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
  1774. return 0;
  1775. }
  1776. static void team_setup(struct net_device *dev)
  1777. {
  1778. ether_setup(dev);
  1779. dev->max_mtu = ETH_MAX_MTU;
  1780. dev->netdev_ops = &team_netdev_ops;
  1781. dev->ethtool_ops = &team_ethtool_ops;
  1782. dev->needs_free_netdev = true;
  1783. dev->priv_destructor = team_destructor;
  1784. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  1785. dev->priv_flags |= IFF_NO_QUEUE;
  1786. dev->priv_flags |= IFF_TEAM;
  1787. /*
  1788. * Indicate we support unicast address filtering. That way core won't
  1789. * bring us to promisc mode in case a unicast addr is added.
  1790. * Let this up to underlay drivers.
  1791. */
  1792. dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
  1793. dev->features |= NETIF_F_LLTX;
  1794. dev->features |= NETIF_F_GRO;
  1795. /* Don't allow team devices to change network namespaces. */
  1796. dev->features |= NETIF_F_NETNS_LOCAL;
  1797. dev->hw_features = TEAM_VLAN_FEATURES |
  1798. NETIF_F_HW_VLAN_CTAG_RX |
  1799. NETIF_F_HW_VLAN_CTAG_FILTER;
  1800. dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
  1801. dev->features |= dev->hw_features;
  1802. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1803. }
  1804. static int team_newlink(struct net *src_net, struct net_device *dev,
  1805. struct nlattr *tb[], struct nlattr *data[],
  1806. struct netlink_ext_ack *extack)
  1807. {
  1808. if (tb[IFLA_ADDRESS] == NULL)
  1809. eth_hw_addr_random(dev);
  1810. return register_netdevice(dev);
  1811. }
  1812. static int team_validate(struct nlattr *tb[], struct nlattr *data[],
  1813. struct netlink_ext_ack *extack)
  1814. {
  1815. if (tb[IFLA_ADDRESS]) {
  1816. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1817. return -EINVAL;
  1818. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1819. return -EADDRNOTAVAIL;
  1820. }
  1821. return 0;
  1822. }
  1823. static unsigned int team_get_num_tx_queues(void)
  1824. {
  1825. return TEAM_DEFAULT_NUM_TX_QUEUES;
  1826. }
  1827. static unsigned int team_get_num_rx_queues(void)
  1828. {
  1829. return TEAM_DEFAULT_NUM_RX_QUEUES;
  1830. }
  1831. static struct rtnl_link_ops team_link_ops __read_mostly = {
  1832. .kind = DRV_NAME,
  1833. .priv_size = sizeof(struct team),
  1834. .setup = team_setup,
  1835. .newlink = team_newlink,
  1836. .validate = team_validate,
  1837. .get_num_tx_queues = team_get_num_tx_queues,
  1838. .get_num_rx_queues = team_get_num_rx_queues,
  1839. };
  1840. /***********************************
  1841. * Generic netlink custom interface
  1842. ***********************************/
  1843. static struct genl_family team_nl_family;
  1844. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  1845. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  1846. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  1847. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  1848. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  1849. };
  1850. static const struct nla_policy
  1851. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  1852. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  1853. [TEAM_ATTR_OPTION_NAME] = {
  1854. .type = NLA_STRING,
  1855. .len = TEAM_STRING_MAX_LEN,
  1856. },
  1857. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  1858. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  1859. [TEAM_ATTR_OPTION_DATA] = { .type = NLA_BINARY },
  1860. [TEAM_ATTR_OPTION_PORT_IFINDEX] = { .type = NLA_U32 },
  1861. [TEAM_ATTR_OPTION_ARRAY_INDEX] = { .type = NLA_U32 },
  1862. };
  1863. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  1864. {
  1865. struct sk_buff *msg;
  1866. void *hdr;
  1867. int err;
  1868. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1869. if (!msg)
  1870. return -ENOMEM;
  1871. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1872. &team_nl_family, 0, TEAM_CMD_NOOP);
  1873. if (!hdr) {
  1874. err = -EMSGSIZE;
  1875. goto err_msg_put;
  1876. }
  1877. genlmsg_end(msg, hdr);
  1878. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  1879. err_msg_put:
  1880. nlmsg_free(msg);
  1881. return err;
  1882. }
  1883. /*
  1884. * Netlink cmd functions should be locked by following two functions.
  1885. * Since dev gets held here, that ensures dev won't disappear in between.
  1886. */
  1887. static struct team *team_nl_team_get(struct genl_info *info)
  1888. {
  1889. struct net *net = genl_info_net(info);
  1890. int ifindex;
  1891. struct net_device *dev;
  1892. struct team *team;
  1893. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  1894. return NULL;
  1895. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  1896. dev = dev_get_by_index(net, ifindex);
  1897. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  1898. if (dev)
  1899. dev_put(dev);
  1900. return NULL;
  1901. }
  1902. team = netdev_priv(dev);
  1903. mutex_lock(&team->lock);
  1904. return team;
  1905. }
  1906. static void team_nl_team_put(struct team *team)
  1907. {
  1908. mutex_unlock(&team->lock);
  1909. dev_put(team->dev);
  1910. }
  1911. typedef int team_nl_send_func_t(struct sk_buff *skb,
  1912. struct team *team, u32 portid);
  1913. static int team_nl_send_unicast(struct sk_buff *skb, struct team *team, u32 portid)
  1914. {
  1915. return genlmsg_unicast(dev_net(team->dev), skb, portid);
  1916. }
  1917. static int team_nl_fill_one_option_get(struct sk_buff *skb, struct team *team,
  1918. struct team_option_inst *opt_inst)
  1919. {
  1920. struct nlattr *option_item;
  1921. struct team_option *option = opt_inst->option;
  1922. struct team_option_inst_info *opt_inst_info = &opt_inst->info;
  1923. struct team_gsetter_ctx ctx;
  1924. int err;
  1925. ctx.info = opt_inst_info;
  1926. err = team_option_get(team, opt_inst, &ctx);
  1927. if (err)
  1928. return err;
  1929. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1930. if (!option_item)
  1931. return -EMSGSIZE;
  1932. if (nla_put_string(skb, TEAM_ATTR_OPTION_NAME, option->name))
  1933. goto nest_cancel;
  1934. if (opt_inst_info->port &&
  1935. nla_put_u32(skb, TEAM_ATTR_OPTION_PORT_IFINDEX,
  1936. opt_inst_info->port->dev->ifindex))
  1937. goto nest_cancel;
  1938. if (opt_inst->option->array_size &&
  1939. nla_put_u32(skb, TEAM_ATTR_OPTION_ARRAY_INDEX,
  1940. opt_inst_info->array_index))
  1941. goto nest_cancel;
  1942. switch (option->type) {
  1943. case TEAM_OPTION_TYPE_U32:
  1944. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32))
  1945. goto nest_cancel;
  1946. if (nla_put_u32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.u32_val))
  1947. goto nest_cancel;
  1948. break;
  1949. case TEAM_OPTION_TYPE_STRING:
  1950. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING))
  1951. goto nest_cancel;
  1952. if (nla_put_string(skb, TEAM_ATTR_OPTION_DATA,
  1953. ctx.data.str_val))
  1954. goto nest_cancel;
  1955. break;
  1956. case TEAM_OPTION_TYPE_BINARY:
  1957. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_BINARY))
  1958. goto nest_cancel;
  1959. if (nla_put(skb, TEAM_ATTR_OPTION_DATA, ctx.data.bin_val.len,
  1960. ctx.data.bin_val.ptr))
  1961. goto nest_cancel;
  1962. break;
  1963. case TEAM_OPTION_TYPE_BOOL:
  1964. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_FLAG))
  1965. goto nest_cancel;
  1966. if (ctx.data.bool_val &&
  1967. nla_put_flag(skb, TEAM_ATTR_OPTION_DATA))
  1968. goto nest_cancel;
  1969. break;
  1970. case TEAM_OPTION_TYPE_S32:
  1971. if (nla_put_u8(skb, TEAM_ATTR_OPTION_TYPE, NLA_S32))
  1972. goto nest_cancel;
  1973. if (nla_put_s32(skb, TEAM_ATTR_OPTION_DATA, ctx.data.s32_val))
  1974. goto nest_cancel;
  1975. break;
  1976. default:
  1977. BUG();
  1978. }
  1979. if (opt_inst->removed && nla_put_flag(skb, TEAM_ATTR_OPTION_REMOVED))
  1980. goto nest_cancel;
  1981. if (opt_inst->changed) {
  1982. if (nla_put_flag(skb, TEAM_ATTR_OPTION_CHANGED))
  1983. goto nest_cancel;
  1984. opt_inst->changed = false;
  1985. }
  1986. nla_nest_end(skb, option_item);
  1987. return 0;
  1988. nest_cancel:
  1989. nla_nest_cancel(skb, option_item);
  1990. return -EMSGSIZE;
  1991. }
  1992. static int __send_and_alloc_skb(struct sk_buff **pskb,
  1993. struct team *team, u32 portid,
  1994. team_nl_send_func_t *send_func)
  1995. {
  1996. int err;
  1997. if (*pskb) {
  1998. err = send_func(*pskb, team, portid);
  1999. if (err)
  2000. return err;
  2001. }
  2002. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2003. if (!*pskb)
  2004. return -ENOMEM;
  2005. return 0;
  2006. }
  2007. static int team_nl_send_options_get(struct team *team, u32 portid, u32 seq,
  2008. int flags, team_nl_send_func_t *send_func,
  2009. struct list_head *sel_opt_inst_list)
  2010. {
  2011. struct nlattr *option_list;
  2012. struct nlmsghdr *nlh;
  2013. void *hdr;
  2014. struct team_option_inst *opt_inst;
  2015. int err;
  2016. struct sk_buff *skb = NULL;
  2017. bool incomplete;
  2018. int i;
  2019. opt_inst = list_first_entry(sel_opt_inst_list,
  2020. struct team_option_inst, tmp_list);
  2021. start_again:
  2022. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2023. if (err)
  2024. return err;
  2025. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2026. TEAM_CMD_OPTIONS_GET);
  2027. if (!hdr) {
  2028. nlmsg_free(skb);
  2029. return -EMSGSIZE;
  2030. }
  2031. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2032. goto nla_put_failure;
  2033. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  2034. if (!option_list)
  2035. goto nla_put_failure;
  2036. i = 0;
  2037. incomplete = false;
  2038. list_for_each_entry_from(opt_inst, sel_opt_inst_list, tmp_list) {
  2039. err = team_nl_fill_one_option_get(skb, team, opt_inst);
  2040. if (err) {
  2041. if (err == -EMSGSIZE) {
  2042. if (!i)
  2043. goto errout;
  2044. incomplete = true;
  2045. break;
  2046. }
  2047. goto errout;
  2048. }
  2049. i++;
  2050. }
  2051. nla_nest_end(skb, option_list);
  2052. genlmsg_end(skb, hdr);
  2053. if (incomplete)
  2054. goto start_again;
  2055. send_done:
  2056. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2057. if (!nlh) {
  2058. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2059. if (err)
  2060. return err;
  2061. goto send_done;
  2062. }
  2063. return send_func(skb, team, portid);
  2064. nla_put_failure:
  2065. err = -EMSGSIZE;
  2066. errout:
  2067. nlmsg_free(skb);
  2068. return err;
  2069. }
  2070. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  2071. {
  2072. struct team *team;
  2073. struct team_option_inst *opt_inst;
  2074. int err;
  2075. LIST_HEAD(sel_opt_inst_list);
  2076. team = team_nl_team_get(info);
  2077. if (!team)
  2078. return -EINVAL;
  2079. list_for_each_entry(opt_inst, &team->option_inst_list, list)
  2080. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2081. err = team_nl_send_options_get(team, info->snd_portid, info->snd_seq,
  2082. NLM_F_ACK, team_nl_send_unicast,
  2083. &sel_opt_inst_list);
  2084. team_nl_team_put(team);
  2085. return err;
  2086. }
  2087. static int team_nl_send_event_options_get(struct team *team,
  2088. struct list_head *sel_opt_inst_list);
  2089. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  2090. {
  2091. struct team *team;
  2092. int err = 0;
  2093. int i;
  2094. struct nlattr *nl_option;
  2095. rtnl_lock();
  2096. team = team_nl_team_get(info);
  2097. if (!team) {
  2098. err = -EINVAL;
  2099. goto rtnl_unlock;
  2100. }
  2101. err = -EINVAL;
  2102. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  2103. err = -EINVAL;
  2104. goto team_put;
  2105. }
  2106. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  2107. struct nlattr *opt_attrs[TEAM_ATTR_OPTION_MAX + 1];
  2108. struct nlattr *attr;
  2109. struct nlattr *attr_data;
  2110. LIST_HEAD(opt_inst_list);
  2111. enum team_option_type opt_type;
  2112. int opt_port_ifindex = 0; /* != 0 for per-port options */
  2113. u32 opt_array_index = 0;
  2114. bool opt_is_array = false;
  2115. struct team_option_inst *opt_inst;
  2116. char *opt_name;
  2117. bool opt_found = false;
  2118. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  2119. err = -EINVAL;
  2120. goto team_put;
  2121. }
  2122. err = nla_parse_nested(opt_attrs, TEAM_ATTR_OPTION_MAX,
  2123. nl_option, team_nl_option_policy,
  2124. info->extack);
  2125. if (err)
  2126. goto team_put;
  2127. if (!opt_attrs[TEAM_ATTR_OPTION_NAME] ||
  2128. !opt_attrs[TEAM_ATTR_OPTION_TYPE]) {
  2129. err = -EINVAL;
  2130. goto team_put;
  2131. }
  2132. switch (nla_get_u8(opt_attrs[TEAM_ATTR_OPTION_TYPE])) {
  2133. case NLA_U32:
  2134. opt_type = TEAM_OPTION_TYPE_U32;
  2135. break;
  2136. case NLA_STRING:
  2137. opt_type = TEAM_OPTION_TYPE_STRING;
  2138. break;
  2139. case NLA_BINARY:
  2140. opt_type = TEAM_OPTION_TYPE_BINARY;
  2141. break;
  2142. case NLA_FLAG:
  2143. opt_type = TEAM_OPTION_TYPE_BOOL;
  2144. break;
  2145. case NLA_S32:
  2146. opt_type = TEAM_OPTION_TYPE_S32;
  2147. break;
  2148. default:
  2149. goto team_put;
  2150. }
  2151. attr_data = opt_attrs[TEAM_ATTR_OPTION_DATA];
  2152. if (opt_type != TEAM_OPTION_TYPE_BOOL && !attr_data) {
  2153. err = -EINVAL;
  2154. goto team_put;
  2155. }
  2156. opt_name = nla_data(opt_attrs[TEAM_ATTR_OPTION_NAME]);
  2157. attr = opt_attrs[TEAM_ATTR_OPTION_PORT_IFINDEX];
  2158. if (attr)
  2159. opt_port_ifindex = nla_get_u32(attr);
  2160. attr = opt_attrs[TEAM_ATTR_OPTION_ARRAY_INDEX];
  2161. if (attr) {
  2162. opt_is_array = true;
  2163. opt_array_index = nla_get_u32(attr);
  2164. }
  2165. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2166. struct team_option *option = opt_inst->option;
  2167. struct team_gsetter_ctx ctx;
  2168. struct team_option_inst_info *opt_inst_info;
  2169. int tmp_ifindex;
  2170. opt_inst_info = &opt_inst->info;
  2171. tmp_ifindex = opt_inst_info->port ?
  2172. opt_inst_info->port->dev->ifindex : 0;
  2173. if (option->type != opt_type ||
  2174. strcmp(option->name, opt_name) ||
  2175. tmp_ifindex != opt_port_ifindex ||
  2176. (option->array_size && !opt_is_array) ||
  2177. opt_inst_info->array_index != opt_array_index)
  2178. continue;
  2179. opt_found = true;
  2180. ctx.info = opt_inst_info;
  2181. switch (opt_type) {
  2182. case TEAM_OPTION_TYPE_U32:
  2183. ctx.data.u32_val = nla_get_u32(attr_data);
  2184. break;
  2185. case TEAM_OPTION_TYPE_STRING:
  2186. if (nla_len(attr_data) > TEAM_STRING_MAX_LEN) {
  2187. err = -EINVAL;
  2188. goto team_put;
  2189. }
  2190. ctx.data.str_val = nla_data(attr_data);
  2191. break;
  2192. case TEAM_OPTION_TYPE_BINARY:
  2193. ctx.data.bin_val.len = nla_len(attr_data);
  2194. ctx.data.bin_val.ptr = nla_data(attr_data);
  2195. break;
  2196. case TEAM_OPTION_TYPE_BOOL:
  2197. ctx.data.bool_val = attr_data ? true : false;
  2198. break;
  2199. case TEAM_OPTION_TYPE_S32:
  2200. ctx.data.s32_val = nla_get_s32(attr_data);
  2201. break;
  2202. default:
  2203. BUG();
  2204. }
  2205. err = team_option_set(team, opt_inst, &ctx);
  2206. if (err)
  2207. goto team_put;
  2208. opt_inst->changed = true;
  2209. list_add(&opt_inst->tmp_list, &opt_inst_list);
  2210. }
  2211. if (!opt_found) {
  2212. err = -ENOENT;
  2213. goto team_put;
  2214. }
  2215. err = team_nl_send_event_options_get(team, &opt_inst_list);
  2216. if (err)
  2217. break;
  2218. }
  2219. team_put:
  2220. team_nl_team_put(team);
  2221. rtnl_unlock:
  2222. rtnl_unlock();
  2223. return err;
  2224. }
  2225. static int team_nl_fill_one_port_get(struct sk_buff *skb,
  2226. struct team_port *port)
  2227. {
  2228. struct nlattr *port_item;
  2229. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  2230. if (!port_item)
  2231. goto nest_cancel;
  2232. if (nla_put_u32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex))
  2233. goto nest_cancel;
  2234. if (port->changed) {
  2235. if (nla_put_flag(skb, TEAM_ATTR_PORT_CHANGED))
  2236. goto nest_cancel;
  2237. port->changed = false;
  2238. }
  2239. if ((port->removed &&
  2240. nla_put_flag(skb, TEAM_ATTR_PORT_REMOVED)) ||
  2241. (port->state.linkup &&
  2242. nla_put_flag(skb, TEAM_ATTR_PORT_LINKUP)) ||
  2243. nla_put_u32(skb, TEAM_ATTR_PORT_SPEED, port->state.speed) ||
  2244. nla_put_u8(skb, TEAM_ATTR_PORT_DUPLEX, port->state.duplex))
  2245. goto nest_cancel;
  2246. nla_nest_end(skb, port_item);
  2247. return 0;
  2248. nest_cancel:
  2249. nla_nest_cancel(skb, port_item);
  2250. return -EMSGSIZE;
  2251. }
  2252. static int team_nl_send_port_list_get(struct team *team, u32 portid, u32 seq,
  2253. int flags, team_nl_send_func_t *send_func,
  2254. struct team_port *one_port)
  2255. {
  2256. struct nlattr *port_list;
  2257. struct nlmsghdr *nlh;
  2258. void *hdr;
  2259. struct team_port *port;
  2260. int err;
  2261. struct sk_buff *skb = NULL;
  2262. bool incomplete;
  2263. int i;
  2264. port = list_first_entry_or_null(&team->port_list,
  2265. struct team_port, list);
  2266. start_again:
  2267. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2268. if (err)
  2269. return err;
  2270. hdr = genlmsg_put(skb, portid, seq, &team_nl_family, flags | NLM_F_MULTI,
  2271. TEAM_CMD_PORT_LIST_GET);
  2272. if (!hdr) {
  2273. nlmsg_free(skb);
  2274. return -EMSGSIZE;
  2275. }
  2276. if (nla_put_u32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex))
  2277. goto nla_put_failure;
  2278. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  2279. if (!port_list)
  2280. goto nla_put_failure;
  2281. i = 0;
  2282. incomplete = false;
  2283. /* If one port is selected, called wants to send port list containing
  2284. * only this port. Otherwise go through all listed ports and send all
  2285. */
  2286. if (one_port) {
  2287. err = team_nl_fill_one_port_get(skb, one_port);
  2288. if (err)
  2289. goto errout;
  2290. } else if (port) {
  2291. list_for_each_entry_from(port, &team->port_list, list) {
  2292. err = team_nl_fill_one_port_get(skb, port);
  2293. if (err) {
  2294. if (err == -EMSGSIZE) {
  2295. if (!i)
  2296. goto errout;
  2297. incomplete = true;
  2298. break;
  2299. }
  2300. goto errout;
  2301. }
  2302. i++;
  2303. }
  2304. }
  2305. nla_nest_end(skb, port_list);
  2306. genlmsg_end(skb, hdr);
  2307. if (incomplete)
  2308. goto start_again;
  2309. send_done:
  2310. nlh = nlmsg_put(skb, portid, seq, NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2311. if (!nlh) {
  2312. err = __send_and_alloc_skb(&skb, team, portid, send_func);
  2313. if (err)
  2314. return err;
  2315. goto send_done;
  2316. }
  2317. return send_func(skb, team, portid);
  2318. nla_put_failure:
  2319. err = -EMSGSIZE;
  2320. errout:
  2321. nlmsg_free(skb);
  2322. return err;
  2323. }
  2324. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  2325. struct genl_info *info)
  2326. {
  2327. struct team *team;
  2328. int err;
  2329. team = team_nl_team_get(info);
  2330. if (!team)
  2331. return -EINVAL;
  2332. err = team_nl_send_port_list_get(team, info->snd_portid, info->snd_seq,
  2333. NLM_F_ACK, team_nl_send_unicast, NULL);
  2334. team_nl_team_put(team);
  2335. return err;
  2336. }
  2337. static const struct genl_ops team_nl_ops[] = {
  2338. {
  2339. .cmd = TEAM_CMD_NOOP,
  2340. .doit = team_nl_cmd_noop,
  2341. .policy = team_nl_policy,
  2342. },
  2343. {
  2344. .cmd = TEAM_CMD_OPTIONS_SET,
  2345. .doit = team_nl_cmd_options_set,
  2346. .policy = team_nl_policy,
  2347. .flags = GENL_ADMIN_PERM,
  2348. },
  2349. {
  2350. .cmd = TEAM_CMD_OPTIONS_GET,
  2351. .doit = team_nl_cmd_options_get,
  2352. .policy = team_nl_policy,
  2353. .flags = GENL_ADMIN_PERM,
  2354. },
  2355. {
  2356. .cmd = TEAM_CMD_PORT_LIST_GET,
  2357. .doit = team_nl_cmd_port_list_get,
  2358. .policy = team_nl_policy,
  2359. .flags = GENL_ADMIN_PERM,
  2360. },
  2361. };
  2362. static const struct genl_multicast_group team_nl_mcgrps[] = {
  2363. { .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME, },
  2364. };
  2365. static struct genl_family team_nl_family __ro_after_init = {
  2366. .name = TEAM_GENL_NAME,
  2367. .version = TEAM_GENL_VERSION,
  2368. .maxattr = TEAM_ATTR_MAX,
  2369. .netnsok = true,
  2370. .module = THIS_MODULE,
  2371. .ops = team_nl_ops,
  2372. .n_ops = ARRAY_SIZE(team_nl_ops),
  2373. .mcgrps = team_nl_mcgrps,
  2374. .n_mcgrps = ARRAY_SIZE(team_nl_mcgrps),
  2375. };
  2376. static int team_nl_send_multicast(struct sk_buff *skb,
  2377. struct team *team, u32 portid)
  2378. {
  2379. return genlmsg_multicast_netns(&team_nl_family, dev_net(team->dev),
  2380. skb, 0, 0, GFP_KERNEL);
  2381. }
  2382. static int team_nl_send_event_options_get(struct team *team,
  2383. struct list_head *sel_opt_inst_list)
  2384. {
  2385. return team_nl_send_options_get(team, 0, 0, 0, team_nl_send_multicast,
  2386. sel_opt_inst_list);
  2387. }
  2388. static int team_nl_send_event_port_get(struct team *team,
  2389. struct team_port *port)
  2390. {
  2391. return team_nl_send_port_list_get(team, 0, 0, 0, team_nl_send_multicast,
  2392. port);
  2393. }
  2394. static int __init team_nl_init(void)
  2395. {
  2396. return genl_register_family(&team_nl_family);
  2397. }
  2398. static void team_nl_fini(void)
  2399. {
  2400. genl_unregister_family(&team_nl_family);
  2401. }
  2402. /******************
  2403. * Change checkers
  2404. ******************/
  2405. static void __team_options_change_check(struct team *team)
  2406. {
  2407. int err;
  2408. struct team_option_inst *opt_inst;
  2409. LIST_HEAD(sel_opt_inst_list);
  2410. list_for_each_entry(opt_inst, &team->option_inst_list, list) {
  2411. if (opt_inst->changed)
  2412. list_add_tail(&opt_inst->tmp_list, &sel_opt_inst_list);
  2413. }
  2414. err = team_nl_send_event_options_get(team, &sel_opt_inst_list);
  2415. if (err && err != -ESRCH)
  2416. netdev_warn(team->dev, "Failed to send options change via netlink (err %d)\n",
  2417. err);
  2418. }
  2419. /* rtnl lock is held */
  2420. static void __team_port_change_send(struct team_port *port, bool linkup)
  2421. {
  2422. int err;
  2423. port->changed = true;
  2424. port->state.linkup = linkup;
  2425. team_refresh_port_linkup(port);
  2426. if (linkup) {
  2427. struct ethtool_link_ksettings ecmd;
  2428. err = __ethtool_get_link_ksettings(port->dev, &ecmd);
  2429. if (!err) {
  2430. port->state.speed = ecmd.base.speed;
  2431. port->state.duplex = ecmd.base.duplex;
  2432. goto send_event;
  2433. }
  2434. }
  2435. port->state.speed = 0;
  2436. port->state.duplex = 0;
  2437. send_event:
  2438. err = team_nl_send_event_port_get(port->team, port);
  2439. if (err && err != -ESRCH)
  2440. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink (err %d)\n",
  2441. port->dev->name, err);
  2442. }
  2443. static void __team_carrier_check(struct team *team)
  2444. {
  2445. struct team_port *port;
  2446. bool team_linkup;
  2447. if (team->user_carrier_enabled)
  2448. return;
  2449. team_linkup = false;
  2450. list_for_each_entry(port, &team->port_list, list) {
  2451. if (port->linkup) {
  2452. team_linkup = true;
  2453. break;
  2454. }
  2455. }
  2456. if (team_linkup)
  2457. netif_carrier_on(team->dev);
  2458. else
  2459. netif_carrier_off(team->dev);
  2460. }
  2461. static void __team_port_change_check(struct team_port *port, bool linkup)
  2462. {
  2463. if (port->state.linkup != linkup)
  2464. __team_port_change_send(port, linkup);
  2465. __team_carrier_check(port->team);
  2466. }
  2467. static void __team_port_change_port_added(struct team_port *port, bool linkup)
  2468. {
  2469. __team_port_change_send(port, linkup);
  2470. __team_carrier_check(port->team);
  2471. }
  2472. static void __team_port_change_port_removed(struct team_port *port)
  2473. {
  2474. port->removed = true;
  2475. __team_port_change_send(port, false);
  2476. __team_carrier_check(port->team);
  2477. }
  2478. static void team_port_change_check(struct team_port *port, bool linkup)
  2479. {
  2480. struct team *team = port->team;
  2481. mutex_lock(&team->lock);
  2482. __team_port_change_check(port, linkup);
  2483. mutex_unlock(&team->lock);
  2484. }
  2485. /************************************
  2486. * Net device notifier event handler
  2487. ************************************/
  2488. static int team_device_event(struct notifier_block *unused,
  2489. unsigned long event, void *ptr)
  2490. {
  2491. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2492. struct team_port *port;
  2493. port = team_port_get_rtnl(dev);
  2494. if (!port)
  2495. return NOTIFY_DONE;
  2496. switch (event) {
  2497. case NETDEV_UP:
  2498. if (netif_oper_up(dev))
  2499. team_port_change_check(port, true);
  2500. break;
  2501. case NETDEV_DOWN:
  2502. team_port_change_check(port, false);
  2503. break;
  2504. case NETDEV_CHANGE:
  2505. if (netif_running(port->dev))
  2506. team_port_change_check(port,
  2507. !!netif_oper_up(port->dev));
  2508. break;
  2509. case NETDEV_UNREGISTER:
  2510. team_del_slave(port->team->dev, dev);
  2511. break;
  2512. case NETDEV_FEAT_CHANGE:
  2513. team_compute_features(port->team);
  2514. break;
  2515. case NETDEV_PRECHANGEMTU:
  2516. /* Forbid to change mtu of underlaying device */
  2517. if (!port->team->port_mtu_change_allowed)
  2518. return NOTIFY_BAD;
  2519. break;
  2520. case NETDEV_PRE_TYPE_CHANGE:
  2521. /* Forbid to change type of underlaying device */
  2522. return NOTIFY_BAD;
  2523. case NETDEV_RESEND_IGMP:
  2524. /* Propagate to master device */
  2525. call_netdevice_notifiers(event, port->team->dev);
  2526. break;
  2527. }
  2528. return NOTIFY_DONE;
  2529. }
  2530. static struct notifier_block team_notifier_block __read_mostly = {
  2531. .notifier_call = team_device_event,
  2532. };
  2533. /***********************
  2534. * Module init and exit
  2535. ***********************/
  2536. static int __init team_module_init(void)
  2537. {
  2538. int err;
  2539. register_netdevice_notifier(&team_notifier_block);
  2540. err = rtnl_link_register(&team_link_ops);
  2541. if (err)
  2542. goto err_rtnl_reg;
  2543. err = team_nl_init();
  2544. if (err)
  2545. goto err_nl_init;
  2546. return 0;
  2547. err_nl_init:
  2548. rtnl_link_unregister(&team_link_ops);
  2549. err_rtnl_reg:
  2550. unregister_netdevice_notifier(&team_notifier_block);
  2551. return err;
  2552. }
  2553. static void __exit team_module_exit(void)
  2554. {
  2555. team_nl_fini();
  2556. rtnl_link_unregister(&team_link_ops);
  2557. unregister_netdevice_notifier(&team_notifier_block);
  2558. }
  2559. module_init(team_module_init);
  2560. module_exit(team_module_exit);
  2561. MODULE_LICENSE("GPL v2");
  2562. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  2563. MODULE_DESCRIPTION("Ethernet team device driver");
  2564. MODULE_ALIAS_RTNL_LINK(DRV_NAME);