team.c 55 KB

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