team.c 74 KB

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