team.c 62 KB

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