team.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. /*
  2. * net/drivers/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/if_vlan.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/socket.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/rtnetlink.h>
  25. #include <net/rtnetlink.h>
  26. #include <net/genetlink.h>
  27. #include <net/netlink.h>
  28. #include <linux/if_team.h>
  29. #define DRV_NAME "team"
  30. /**********
  31. * Helpers
  32. **********/
  33. #define team_port_exists(dev) (dev->priv_flags & IFF_TEAM_PORT)
  34. static struct team_port *team_port_get_rcu(const struct net_device *dev)
  35. {
  36. return rcu_dereference(dev->rx_handler_data);
  37. }
  38. static struct team_port *team_port_get_rtnl(const struct net_device *dev)
  39. {
  40. struct team_port *port = rtnl_dereference(dev->rx_handler_data);
  41. return team_port_exists(dev) ? port : NULL;
  42. }
  43. /*
  44. * Since the ability to change mac address for open port device is tested in
  45. * team_port_add, this function can be called without control of return value
  46. */
  47. static int __set_port_mac(struct net_device *port_dev,
  48. const unsigned char *dev_addr)
  49. {
  50. struct sockaddr addr;
  51. memcpy(addr.sa_data, dev_addr, ETH_ALEN);
  52. addr.sa_family = ARPHRD_ETHER;
  53. return dev_set_mac_address(port_dev, &addr);
  54. }
  55. int team_port_set_orig_mac(struct team_port *port)
  56. {
  57. return __set_port_mac(port->dev, port->orig.dev_addr);
  58. }
  59. int team_port_set_team_mac(struct team_port *port)
  60. {
  61. return __set_port_mac(port->dev, port->team->dev->dev_addr);
  62. }
  63. EXPORT_SYMBOL(team_port_set_team_mac);
  64. /*******************
  65. * Options handling
  66. *******************/
  67. struct team_option *__team_find_option(struct team *team, const char *opt_name)
  68. {
  69. struct team_option *option;
  70. list_for_each_entry(option, &team->option_list, list) {
  71. if (strcmp(option->name, opt_name) == 0)
  72. return option;
  73. }
  74. return NULL;
  75. }
  76. int __team_options_register(struct team *team,
  77. const struct team_option *option,
  78. size_t option_count)
  79. {
  80. int i;
  81. struct team_option **dst_opts;
  82. int err;
  83. dst_opts = kzalloc(sizeof(struct team_option *) * option_count,
  84. GFP_KERNEL);
  85. if (!dst_opts)
  86. return -ENOMEM;
  87. for (i = 0; i < option_count; i++, option++) {
  88. if (__team_find_option(team, option->name)) {
  89. err = -EEXIST;
  90. goto rollback;
  91. }
  92. dst_opts[i] = kmemdup(option, sizeof(*option), GFP_KERNEL);
  93. if (!dst_opts[i]) {
  94. err = -ENOMEM;
  95. goto rollback;
  96. }
  97. }
  98. for (i = 0; i < option_count; i++) {
  99. dst_opts[i]->changed = true;
  100. dst_opts[i]->removed = false;
  101. list_add_tail(&dst_opts[i]->list, &team->option_list);
  102. }
  103. kfree(dst_opts);
  104. return 0;
  105. rollback:
  106. for (i = 0; i < option_count; i++)
  107. kfree(dst_opts[i]);
  108. kfree(dst_opts);
  109. return err;
  110. }
  111. static void __team_options_mark_removed(struct team *team,
  112. const struct team_option *option,
  113. size_t option_count)
  114. {
  115. int i;
  116. for (i = 0; i < option_count; i++, option++) {
  117. struct team_option *del_opt;
  118. del_opt = __team_find_option(team, option->name);
  119. if (del_opt) {
  120. del_opt->changed = true;
  121. del_opt->removed = true;
  122. }
  123. }
  124. }
  125. static void __team_options_unregister(struct team *team,
  126. const struct team_option *option,
  127. size_t option_count)
  128. {
  129. int i;
  130. for (i = 0; i < option_count; i++, option++) {
  131. struct team_option *del_opt;
  132. del_opt = __team_find_option(team, option->name);
  133. if (del_opt) {
  134. list_del(&del_opt->list);
  135. kfree(del_opt);
  136. }
  137. }
  138. }
  139. static void __team_options_change_check(struct team *team);
  140. int team_options_register(struct team *team,
  141. const struct team_option *option,
  142. size_t option_count)
  143. {
  144. int err;
  145. err = __team_options_register(team, option, option_count);
  146. if (err)
  147. return err;
  148. __team_options_change_check(team);
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(team_options_register);
  152. void team_options_unregister(struct team *team,
  153. const struct team_option *option,
  154. size_t option_count)
  155. {
  156. __team_options_mark_removed(team, option, option_count);
  157. __team_options_change_check(team);
  158. __team_options_unregister(team, option, option_count);
  159. }
  160. EXPORT_SYMBOL(team_options_unregister);
  161. static int team_option_get(struct team *team, struct team_option *option,
  162. void *arg)
  163. {
  164. return option->getter(team, arg);
  165. }
  166. static int team_option_set(struct team *team, struct team_option *option,
  167. void *arg)
  168. {
  169. int err;
  170. err = option->setter(team, arg);
  171. if (err)
  172. return err;
  173. option->changed = true;
  174. __team_options_change_check(team);
  175. return err;
  176. }
  177. /****************
  178. * Mode handling
  179. ****************/
  180. static LIST_HEAD(mode_list);
  181. static DEFINE_SPINLOCK(mode_list_lock);
  182. static struct team_mode *__find_mode(const char *kind)
  183. {
  184. struct team_mode *mode;
  185. list_for_each_entry(mode, &mode_list, list) {
  186. if (strcmp(mode->kind, kind) == 0)
  187. return mode;
  188. }
  189. return NULL;
  190. }
  191. static bool is_good_mode_name(const char *name)
  192. {
  193. while (*name != '\0') {
  194. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  195. return false;
  196. name++;
  197. }
  198. return true;
  199. }
  200. int team_mode_register(struct team_mode *mode)
  201. {
  202. int err = 0;
  203. if (!is_good_mode_name(mode->kind) ||
  204. mode->priv_size > TEAM_MODE_PRIV_SIZE)
  205. return -EINVAL;
  206. spin_lock(&mode_list_lock);
  207. if (__find_mode(mode->kind)) {
  208. err = -EEXIST;
  209. goto unlock;
  210. }
  211. list_add_tail(&mode->list, &mode_list);
  212. unlock:
  213. spin_unlock(&mode_list_lock);
  214. return err;
  215. }
  216. EXPORT_SYMBOL(team_mode_register);
  217. int team_mode_unregister(struct team_mode *mode)
  218. {
  219. spin_lock(&mode_list_lock);
  220. list_del_init(&mode->list);
  221. spin_unlock(&mode_list_lock);
  222. return 0;
  223. }
  224. EXPORT_SYMBOL(team_mode_unregister);
  225. static struct team_mode *team_mode_get(const char *kind)
  226. {
  227. struct team_mode *mode;
  228. spin_lock(&mode_list_lock);
  229. mode = __find_mode(kind);
  230. if (!mode) {
  231. spin_unlock(&mode_list_lock);
  232. request_module("team-mode-%s", kind);
  233. spin_lock(&mode_list_lock);
  234. mode = __find_mode(kind);
  235. }
  236. if (mode)
  237. if (!try_module_get(mode->owner))
  238. mode = NULL;
  239. spin_unlock(&mode_list_lock);
  240. return mode;
  241. }
  242. static void team_mode_put(const struct team_mode *mode)
  243. {
  244. module_put(mode->owner);
  245. }
  246. static bool team_dummy_transmit(struct team *team, struct sk_buff *skb)
  247. {
  248. dev_kfree_skb_any(skb);
  249. return false;
  250. }
  251. rx_handler_result_t team_dummy_receive(struct team *team,
  252. struct team_port *port,
  253. struct sk_buff *skb)
  254. {
  255. return RX_HANDLER_ANOTHER;
  256. }
  257. static void team_adjust_ops(struct team *team)
  258. {
  259. /*
  260. * To avoid checks in rx/tx skb paths, ensure here that non-null and
  261. * correct ops are always set.
  262. */
  263. if (list_empty(&team->port_list) ||
  264. !team->mode || !team->mode->ops->transmit)
  265. team->ops.transmit = team_dummy_transmit;
  266. else
  267. team->ops.transmit = team->mode->ops->transmit;
  268. if (list_empty(&team->port_list) ||
  269. !team->mode || !team->mode->ops->receive)
  270. team->ops.receive = team_dummy_receive;
  271. else
  272. team->ops.receive = team->mode->ops->receive;
  273. }
  274. /*
  275. * We can benefit from the fact that it's ensured no port is present
  276. * at the time of mode change. Therefore no packets are in fly so there's no
  277. * need to set mode operations in any special way.
  278. */
  279. static int __team_change_mode(struct team *team,
  280. const struct team_mode *new_mode)
  281. {
  282. /* Check if mode was previously set and do cleanup if so */
  283. if (team->mode) {
  284. void (*exit_op)(struct team *team) = team->ops.exit;
  285. /* Clear ops area so no callback is called any longer */
  286. memset(&team->ops, 0, sizeof(struct team_mode_ops));
  287. team_adjust_ops(team);
  288. if (exit_op)
  289. exit_op(team);
  290. team_mode_put(team->mode);
  291. team->mode = NULL;
  292. /* zero private data area */
  293. memset(&team->mode_priv, 0,
  294. sizeof(struct team) - offsetof(struct team, mode_priv));
  295. }
  296. if (!new_mode)
  297. return 0;
  298. if (new_mode->ops->init) {
  299. int err;
  300. err = new_mode->ops->init(team);
  301. if (err)
  302. return err;
  303. }
  304. team->mode = new_mode;
  305. memcpy(&team->ops, new_mode->ops, sizeof(struct team_mode_ops));
  306. team_adjust_ops(team);
  307. return 0;
  308. }
  309. static int team_change_mode(struct team *team, const char *kind)
  310. {
  311. struct team_mode *new_mode;
  312. struct net_device *dev = team->dev;
  313. int err;
  314. if (!list_empty(&team->port_list)) {
  315. netdev_err(dev, "No ports can be present during mode change\n");
  316. return -EBUSY;
  317. }
  318. if (team->mode && strcmp(team->mode->kind, kind) == 0) {
  319. netdev_err(dev, "Unable to change to the same mode the team is in\n");
  320. return -EINVAL;
  321. }
  322. new_mode = team_mode_get(kind);
  323. if (!new_mode) {
  324. netdev_err(dev, "Mode \"%s\" not found\n", kind);
  325. return -EINVAL;
  326. }
  327. err = __team_change_mode(team, new_mode);
  328. if (err) {
  329. netdev_err(dev, "Failed to change to mode \"%s\"\n", kind);
  330. team_mode_put(new_mode);
  331. return err;
  332. }
  333. netdev_info(dev, "Mode changed to \"%s\"\n", kind);
  334. return 0;
  335. }
  336. /************************
  337. * Rx path frame handler
  338. ************************/
  339. /* note: already called with rcu_read_lock */
  340. static rx_handler_result_t team_handle_frame(struct sk_buff **pskb)
  341. {
  342. struct sk_buff *skb = *pskb;
  343. struct team_port *port;
  344. struct team *team;
  345. rx_handler_result_t res;
  346. skb = skb_share_check(skb, GFP_ATOMIC);
  347. if (!skb)
  348. return RX_HANDLER_CONSUMED;
  349. *pskb = skb;
  350. port = team_port_get_rcu(skb->dev);
  351. team = port->team;
  352. res = team->ops.receive(team, port, skb);
  353. if (res == RX_HANDLER_ANOTHER) {
  354. struct team_pcpu_stats *pcpu_stats;
  355. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  356. u64_stats_update_begin(&pcpu_stats->syncp);
  357. pcpu_stats->rx_packets++;
  358. pcpu_stats->rx_bytes += skb->len;
  359. if (skb->pkt_type == PACKET_MULTICAST)
  360. pcpu_stats->rx_multicast++;
  361. u64_stats_update_end(&pcpu_stats->syncp);
  362. skb->dev = team->dev;
  363. } else {
  364. this_cpu_inc(team->pcpu_stats->rx_dropped);
  365. }
  366. return res;
  367. }
  368. /****************
  369. * Port handling
  370. ****************/
  371. static bool team_port_find(const struct team *team,
  372. const struct team_port *port)
  373. {
  374. struct team_port *cur;
  375. list_for_each_entry(cur, &team->port_list, list)
  376. if (cur == port)
  377. return true;
  378. return false;
  379. }
  380. /*
  381. * Add/delete port to the team port list. Write guarded by rtnl_lock.
  382. * Takes care of correct port->index setup (might be racy).
  383. */
  384. static void team_port_list_add_port(struct team *team,
  385. struct team_port *port)
  386. {
  387. port->index = team->port_count++;
  388. hlist_add_head_rcu(&port->hlist,
  389. team_port_index_hash(team, port->index));
  390. list_add_tail_rcu(&port->list, &team->port_list);
  391. }
  392. static void __reconstruct_port_hlist(struct team *team, int rm_index)
  393. {
  394. int i;
  395. struct team_port *port;
  396. for (i = rm_index + 1; i < team->port_count; i++) {
  397. port = team_get_port_by_index(team, i);
  398. hlist_del_rcu(&port->hlist);
  399. port->index--;
  400. hlist_add_head_rcu(&port->hlist,
  401. team_port_index_hash(team, port->index));
  402. }
  403. }
  404. static void team_port_list_del_port(struct team *team,
  405. struct team_port *port)
  406. {
  407. int rm_index = port->index;
  408. hlist_del_rcu(&port->hlist);
  409. list_del_rcu(&port->list);
  410. __reconstruct_port_hlist(team, rm_index);
  411. team->port_count--;
  412. }
  413. #define TEAM_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
  414. NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
  415. NETIF_F_HIGHDMA | NETIF_F_LRO)
  416. static void __team_compute_features(struct team *team)
  417. {
  418. struct team_port *port;
  419. u32 vlan_features = TEAM_VLAN_FEATURES;
  420. unsigned short max_hard_header_len = ETH_HLEN;
  421. list_for_each_entry(port, &team->port_list, list) {
  422. vlan_features = netdev_increment_features(vlan_features,
  423. port->dev->vlan_features,
  424. TEAM_VLAN_FEATURES);
  425. if (port->dev->hard_header_len > max_hard_header_len)
  426. max_hard_header_len = port->dev->hard_header_len;
  427. }
  428. team->dev->vlan_features = vlan_features;
  429. team->dev->hard_header_len = max_hard_header_len;
  430. netdev_change_features(team->dev);
  431. }
  432. static void team_compute_features(struct team *team)
  433. {
  434. mutex_lock(&team->lock);
  435. __team_compute_features(team);
  436. mutex_unlock(&team->lock);
  437. }
  438. static int team_port_enter(struct team *team, struct team_port *port)
  439. {
  440. int err = 0;
  441. dev_hold(team->dev);
  442. port->dev->priv_flags |= IFF_TEAM_PORT;
  443. if (team->ops.port_enter) {
  444. err = team->ops.port_enter(team, port);
  445. if (err) {
  446. netdev_err(team->dev, "Device %s failed to enter team mode\n",
  447. port->dev->name);
  448. goto err_port_enter;
  449. }
  450. }
  451. return 0;
  452. err_port_enter:
  453. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  454. dev_put(team->dev);
  455. return err;
  456. }
  457. static void team_port_leave(struct team *team, struct team_port *port)
  458. {
  459. if (team->ops.port_leave)
  460. team->ops.port_leave(team, port);
  461. port->dev->priv_flags &= ~IFF_TEAM_PORT;
  462. dev_put(team->dev);
  463. }
  464. static void __team_port_change_check(struct team_port *port, bool linkup);
  465. static int team_port_add(struct team *team, struct net_device *port_dev)
  466. {
  467. struct net_device *dev = team->dev;
  468. struct team_port *port;
  469. char *portname = port_dev->name;
  470. int err;
  471. if (port_dev->flags & IFF_LOOPBACK ||
  472. port_dev->type != ARPHRD_ETHER) {
  473. netdev_err(dev, "Device %s is of an unsupported type\n",
  474. portname);
  475. return -EINVAL;
  476. }
  477. if (team_port_exists(port_dev)) {
  478. netdev_err(dev, "Device %s is already a port "
  479. "of a team device\n", portname);
  480. return -EBUSY;
  481. }
  482. if (port_dev->flags & IFF_UP) {
  483. netdev_err(dev, "Device %s is up. Set it down before adding it as a team port\n",
  484. portname);
  485. return -EBUSY;
  486. }
  487. port = kzalloc(sizeof(struct team_port), GFP_KERNEL);
  488. if (!port)
  489. return -ENOMEM;
  490. port->dev = port_dev;
  491. port->team = team;
  492. port->orig.mtu = port_dev->mtu;
  493. err = dev_set_mtu(port_dev, dev->mtu);
  494. if (err) {
  495. netdev_dbg(dev, "Error %d calling dev_set_mtu\n", err);
  496. goto err_set_mtu;
  497. }
  498. memcpy(port->orig.dev_addr, port_dev->dev_addr, ETH_ALEN);
  499. err = team_port_enter(team, port);
  500. if (err) {
  501. netdev_err(dev, "Device %s failed to enter team mode\n",
  502. portname);
  503. goto err_port_enter;
  504. }
  505. err = dev_open(port_dev);
  506. if (err) {
  507. netdev_dbg(dev, "Device %s opening failed\n",
  508. portname);
  509. goto err_dev_open;
  510. }
  511. err = vlan_vids_add_by_dev(port_dev, dev);
  512. if (err) {
  513. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  514. portname);
  515. goto err_vids_add;
  516. }
  517. err = netdev_set_master(port_dev, dev);
  518. if (err) {
  519. netdev_err(dev, "Device %s failed to set master\n", portname);
  520. goto err_set_master;
  521. }
  522. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  523. port);
  524. if (err) {
  525. netdev_err(dev, "Device %s failed to register rx_handler\n",
  526. portname);
  527. goto err_handler_register;
  528. }
  529. team_port_list_add_port(team, port);
  530. team_adjust_ops(team);
  531. __team_compute_features(team);
  532. __team_port_change_check(port, !!netif_carrier_ok(port_dev));
  533. netdev_info(dev, "Port device %s added\n", portname);
  534. return 0;
  535. err_handler_register:
  536. netdev_set_master(port_dev, NULL);
  537. err_set_master:
  538. vlan_vids_del_by_dev(port_dev, dev);
  539. err_vids_add:
  540. dev_close(port_dev);
  541. err_dev_open:
  542. team_port_leave(team, port);
  543. team_port_set_orig_mac(port);
  544. err_port_enter:
  545. dev_set_mtu(port_dev, port->orig.mtu);
  546. err_set_mtu:
  547. kfree(port);
  548. return err;
  549. }
  550. static int team_port_del(struct team *team, struct net_device *port_dev)
  551. {
  552. struct net_device *dev = team->dev;
  553. struct team_port *port;
  554. char *portname = port_dev->name;
  555. port = team_port_get_rtnl(port_dev);
  556. if (!port || !team_port_find(team, port)) {
  557. netdev_err(dev, "Device %s does not act as a port of this team\n",
  558. portname);
  559. return -ENOENT;
  560. }
  561. port->removed = true;
  562. __team_port_change_check(port, false);
  563. team_port_list_del_port(team, port);
  564. team_adjust_ops(team);
  565. netdev_rx_handler_unregister(port_dev);
  566. netdev_set_master(port_dev, NULL);
  567. vlan_vids_del_by_dev(port_dev, dev);
  568. dev_close(port_dev);
  569. team_port_leave(team, port);
  570. team_port_set_orig_mac(port);
  571. dev_set_mtu(port_dev, port->orig.mtu);
  572. synchronize_rcu();
  573. kfree(port);
  574. netdev_info(dev, "Port device %s removed\n", portname);
  575. __team_compute_features(team);
  576. return 0;
  577. }
  578. /*****************
  579. * Net device ops
  580. *****************/
  581. static const char team_no_mode_kind[] = "*NOMODE*";
  582. static int team_mode_option_get(struct team *team, void *arg)
  583. {
  584. const char **str = arg;
  585. *str = team->mode ? team->mode->kind : team_no_mode_kind;
  586. return 0;
  587. }
  588. static int team_mode_option_set(struct team *team, void *arg)
  589. {
  590. const char **str = arg;
  591. return team_change_mode(team, *str);
  592. }
  593. static const struct team_option team_options[] = {
  594. {
  595. .name = "mode",
  596. .type = TEAM_OPTION_TYPE_STRING,
  597. .getter = team_mode_option_get,
  598. .setter = team_mode_option_set,
  599. },
  600. };
  601. static int team_init(struct net_device *dev)
  602. {
  603. struct team *team = netdev_priv(dev);
  604. int i;
  605. int err;
  606. team->dev = dev;
  607. mutex_init(&team->lock);
  608. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  609. if (!team->pcpu_stats)
  610. return -ENOMEM;
  611. for_each_possible_cpu(i) {
  612. struct team_pcpu_stats *team_stats;
  613. team_stats = per_cpu_ptr(team->pcpu_stats, i);
  614. u64_stats_init(&team_stats->syncp);
  615. }
  616. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  617. INIT_HLIST_HEAD(&team->port_hlist[i]);
  618. INIT_LIST_HEAD(&team->port_list);
  619. team_adjust_ops(team);
  620. INIT_LIST_HEAD(&team->option_list);
  621. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  622. if (err)
  623. goto err_options_register;
  624. netif_carrier_off(dev);
  625. return 0;
  626. err_options_register:
  627. free_percpu(team->pcpu_stats);
  628. return err;
  629. }
  630. static void team_uninit(struct net_device *dev)
  631. {
  632. struct team *team = netdev_priv(dev);
  633. struct team_port *port;
  634. struct team_port *tmp;
  635. mutex_lock(&team->lock);
  636. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  637. team_port_del(team, port->dev);
  638. __team_change_mode(team, NULL); /* cleanup */
  639. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  640. mutex_unlock(&team->lock);
  641. }
  642. static void team_destructor(struct net_device *dev)
  643. {
  644. struct team *team = netdev_priv(dev);
  645. free_percpu(team->pcpu_stats);
  646. free_netdev(dev);
  647. }
  648. static int team_open(struct net_device *dev)
  649. {
  650. netif_carrier_on(dev);
  651. return 0;
  652. }
  653. static int team_close(struct net_device *dev)
  654. {
  655. netif_carrier_off(dev);
  656. return 0;
  657. }
  658. /*
  659. * note: already called with rcu_read_lock
  660. */
  661. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  662. {
  663. struct team *team = netdev_priv(dev);
  664. bool tx_success = false;
  665. unsigned int len = skb->len;
  666. tx_success = team->ops.transmit(team, skb);
  667. if (tx_success) {
  668. struct team_pcpu_stats *pcpu_stats;
  669. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  670. u64_stats_update_begin(&pcpu_stats->syncp);
  671. pcpu_stats->tx_packets++;
  672. pcpu_stats->tx_bytes += len;
  673. u64_stats_update_end(&pcpu_stats->syncp);
  674. } else {
  675. this_cpu_inc(team->pcpu_stats->tx_dropped);
  676. }
  677. return NETDEV_TX_OK;
  678. }
  679. static void team_change_rx_flags(struct net_device *dev, int change)
  680. {
  681. struct team *team = netdev_priv(dev);
  682. struct team_port *port;
  683. int inc;
  684. rcu_read_lock();
  685. list_for_each_entry_rcu(port, &team->port_list, list) {
  686. if (change & IFF_PROMISC) {
  687. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  688. dev_set_promiscuity(port->dev, inc);
  689. }
  690. if (change & IFF_ALLMULTI) {
  691. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  692. dev_set_allmulti(port->dev, inc);
  693. }
  694. }
  695. rcu_read_unlock();
  696. }
  697. static void team_set_rx_mode(struct net_device *dev)
  698. {
  699. struct team *team = netdev_priv(dev);
  700. struct team_port *port;
  701. rcu_read_lock();
  702. list_for_each_entry_rcu(port, &team->port_list, list) {
  703. dev_uc_sync(port->dev, dev);
  704. dev_mc_sync(port->dev, dev);
  705. }
  706. rcu_read_unlock();
  707. }
  708. static int team_set_mac_address(struct net_device *dev, void *p)
  709. {
  710. struct team *team = netdev_priv(dev);
  711. struct team_port *port;
  712. struct sockaddr *addr = p;
  713. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  714. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  715. rcu_read_lock();
  716. list_for_each_entry_rcu(port, &team->port_list, list)
  717. if (team->ops.port_change_mac)
  718. team->ops.port_change_mac(team, port);
  719. rcu_read_unlock();
  720. return 0;
  721. }
  722. static int team_change_mtu(struct net_device *dev, int new_mtu)
  723. {
  724. struct team *team = netdev_priv(dev);
  725. struct team_port *port;
  726. int err;
  727. /*
  728. * Alhough this is reader, it's guarded by team lock. It's not possible
  729. * to traverse list in reverse under rcu_read_lock
  730. */
  731. mutex_lock(&team->lock);
  732. team->port_mtu_change_allowed = true;
  733. list_for_each_entry(port, &team->port_list, list) {
  734. err = dev_set_mtu(port->dev, new_mtu);
  735. if (err) {
  736. netdev_err(dev, "Device %s failed to change mtu",
  737. port->dev->name);
  738. goto unwind;
  739. }
  740. }
  741. team->port_mtu_change_allowed = false;
  742. mutex_unlock(&team->lock);
  743. dev->mtu = new_mtu;
  744. return 0;
  745. unwind:
  746. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  747. dev_set_mtu(port->dev, dev->mtu);
  748. team->port_mtu_change_allowed = false;
  749. mutex_unlock(&team->lock);
  750. return err;
  751. }
  752. static struct rtnl_link_stats64 *
  753. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  754. {
  755. struct team *team = netdev_priv(dev);
  756. struct team_pcpu_stats *p;
  757. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  758. u32 rx_dropped = 0, tx_dropped = 0;
  759. unsigned int start;
  760. int i;
  761. for_each_possible_cpu(i) {
  762. p = per_cpu_ptr(team->pcpu_stats, i);
  763. do {
  764. start = u64_stats_fetch_begin_irq(&p->syncp);
  765. rx_packets = p->rx_packets;
  766. rx_bytes = p->rx_bytes;
  767. rx_multicast = p->rx_multicast;
  768. tx_packets = p->tx_packets;
  769. tx_bytes = p->tx_bytes;
  770. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  771. stats->rx_packets += rx_packets;
  772. stats->rx_bytes += rx_bytes;
  773. stats->multicast += rx_multicast;
  774. stats->tx_packets += tx_packets;
  775. stats->tx_bytes += tx_bytes;
  776. /*
  777. * rx_dropped & tx_dropped are u32, updated
  778. * without syncp protection.
  779. */
  780. rx_dropped += p->rx_dropped;
  781. tx_dropped += p->tx_dropped;
  782. }
  783. stats->rx_dropped = rx_dropped;
  784. stats->tx_dropped = tx_dropped;
  785. return stats;
  786. }
  787. static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
  788. {
  789. struct team *team = netdev_priv(dev);
  790. struct team_port *port;
  791. int err;
  792. /*
  793. * Alhough this is reader, it's guarded by team lock. It's not possible
  794. * to traverse list in reverse under rcu_read_lock
  795. */
  796. mutex_lock(&team->lock);
  797. list_for_each_entry(port, &team->port_list, list) {
  798. err = vlan_vid_add(port->dev, vid);
  799. if (err)
  800. goto unwind;
  801. }
  802. mutex_unlock(&team->lock);
  803. return 0;
  804. unwind:
  805. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  806. vlan_vid_del(port->dev, vid);
  807. mutex_unlock(&team->lock);
  808. return err;
  809. }
  810. static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
  811. {
  812. struct team *team = netdev_priv(dev);
  813. struct team_port *port;
  814. rcu_read_lock();
  815. list_for_each_entry_rcu(port, &team->port_list, list)
  816. vlan_vid_del(port->dev, vid);
  817. rcu_read_unlock();
  818. return 0;
  819. }
  820. static int team_add_slave(struct net_device *dev, struct net_device *port_dev)
  821. {
  822. struct team *team = netdev_priv(dev);
  823. int err;
  824. mutex_lock(&team->lock);
  825. err = team_port_add(team, port_dev);
  826. mutex_unlock(&team->lock);
  827. return err;
  828. }
  829. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  830. {
  831. struct team *team = netdev_priv(dev);
  832. int err;
  833. mutex_lock(&team->lock);
  834. err = team_port_del(team, port_dev);
  835. mutex_unlock(&team->lock);
  836. return err;
  837. }
  838. static netdev_features_t team_fix_features(struct net_device *dev,
  839. netdev_features_t features)
  840. {
  841. struct team_port *port;
  842. struct team *team = netdev_priv(dev);
  843. netdev_features_t mask;
  844. mask = features;
  845. features &= ~NETIF_F_ONE_FOR_ALL;
  846. features |= NETIF_F_ALL_FOR_ALL;
  847. rcu_read_lock();
  848. list_for_each_entry_rcu(port, &team->port_list, list) {
  849. features = netdev_increment_features(features,
  850. port->dev->features,
  851. mask);
  852. }
  853. rcu_read_unlock();
  854. return features;
  855. }
  856. static const struct net_device_ops team_netdev_ops = {
  857. .ndo_init = team_init,
  858. .ndo_uninit = team_uninit,
  859. .ndo_open = team_open,
  860. .ndo_stop = team_close,
  861. .ndo_start_xmit = team_xmit,
  862. .ndo_change_rx_flags = team_change_rx_flags,
  863. .ndo_set_rx_mode = team_set_rx_mode,
  864. .ndo_set_mac_address = team_set_mac_address,
  865. .ndo_change_mtu = team_change_mtu,
  866. .ndo_get_stats64 = team_get_stats64,
  867. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  868. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  869. .ndo_add_slave = team_add_slave,
  870. .ndo_del_slave = team_del_slave,
  871. .ndo_fix_features = team_fix_features,
  872. };
  873. /***********************
  874. * rt netlink interface
  875. ***********************/
  876. static void team_setup(struct net_device *dev)
  877. {
  878. ether_setup(dev);
  879. dev->netdev_ops = &team_netdev_ops;
  880. dev->destructor = team_destructor;
  881. dev->tx_queue_len = 0;
  882. dev->flags |= IFF_MULTICAST;
  883. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  884. /*
  885. * Indicate we support unicast address filtering. That way core won't
  886. * bring us to promisc mode in case a unicast addr is added.
  887. * Let this up to underlay drivers.
  888. */
  889. dev->priv_flags |= IFF_UNICAST_FLT;
  890. dev->features |= NETIF_F_LLTX;
  891. dev->features |= NETIF_F_GRO;
  892. dev->hw_features = NETIF_F_HW_VLAN_TX |
  893. NETIF_F_HW_VLAN_RX |
  894. NETIF_F_HW_VLAN_FILTER;
  895. dev->features |= dev->hw_features;
  896. }
  897. static int team_newlink(struct net *src_net, struct net_device *dev,
  898. struct nlattr *tb[], struct nlattr *data[])
  899. {
  900. int err;
  901. if (tb[IFLA_ADDRESS] == NULL)
  902. eth_hw_addr_random(dev);
  903. err = register_netdevice(dev);
  904. if (err)
  905. return err;
  906. return 0;
  907. }
  908. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  909. {
  910. if (tb[IFLA_ADDRESS]) {
  911. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  912. return -EINVAL;
  913. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  914. return -EADDRNOTAVAIL;
  915. }
  916. return 0;
  917. }
  918. static struct rtnl_link_ops team_link_ops __read_mostly = {
  919. .kind = DRV_NAME,
  920. .priv_size = sizeof(struct team),
  921. .setup = team_setup,
  922. .newlink = team_newlink,
  923. .validate = team_validate,
  924. };
  925. /***********************************
  926. * Generic netlink custom interface
  927. ***********************************/
  928. static struct genl_family team_nl_family = {
  929. .id = GENL_ID_GENERATE,
  930. .name = TEAM_GENL_NAME,
  931. .version = TEAM_GENL_VERSION,
  932. .maxattr = TEAM_ATTR_MAX,
  933. .netnsok = true,
  934. };
  935. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  936. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  937. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  938. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  939. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  940. };
  941. static const struct nla_policy
  942. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  943. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  944. [TEAM_ATTR_OPTION_NAME] = {
  945. .type = NLA_STRING,
  946. .len = TEAM_STRING_MAX_LEN,
  947. },
  948. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  949. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  950. [TEAM_ATTR_OPTION_DATA] = {
  951. .type = NLA_BINARY,
  952. .len = TEAM_STRING_MAX_LEN,
  953. },
  954. };
  955. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  956. {
  957. struct sk_buff *msg;
  958. void *hdr;
  959. int err;
  960. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  961. if (!msg)
  962. return -ENOMEM;
  963. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  964. &team_nl_family, 0, TEAM_CMD_NOOP);
  965. if (IS_ERR(hdr)) {
  966. err = PTR_ERR(hdr);
  967. goto err_msg_put;
  968. }
  969. genlmsg_end(msg, hdr);
  970. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  971. err_msg_put:
  972. nlmsg_free(msg);
  973. return err;
  974. }
  975. /*
  976. * Netlink cmd functions should be locked by following two functions.
  977. * Since dev gets held here, that ensures dev won't disappear in between.
  978. */
  979. static struct team *team_nl_team_get(struct genl_info *info)
  980. {
  981. struct net *net = genl_info_net(info);
  982. int ifindex;
  983. struct net_device *dev;
  984. struct team *team;
  985. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  986. return NULL;
  987. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  988. dev = dev_get_by_index(net, ifindex);
  989. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  990. if (dev)
  991. dev_put(dev);
  992. return NULL;
  993. }
  994. team = netdev_priv(dev);
  995. mutex_lock(&team->lock);
  996. return team;
  997. }
  998. static void team_nl_team_put(struct team *team)
  999. {
  1000. mutex_unlock(&team->lock);
  1001. dev_put(team->dev);
  1002. }
  1003. static int team_nl_send_generic(struct genl_info *info, struct team *team,
  1004. int (*fill_func)(struct sk_buff *skb,
  1005. struct genl_info *info,
  1006. int flags, struct team *team))
  1007. {
  1008. struct sk_buff *skb;
  1009. int err;
  1010. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1011. if (!skb)
  1012. return -ENOMEM;
  1013. err = fill_func(skb, info, NLM_F_ACK, team);
  1014. if (err < 0)
  1015. goto err_fill;
  1016. err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
  1017. return err;
  1018. err_fill:
  1019. nlmsg_free(skb);
  1020. return err;
  1021. }
  1022. static int team_nl_fill_options_get(struct sk_buff *skb,
  1023. u32 pid, u32 seq, int flags,
  1024. struct team *team, bool fillall)
  1025. {
  1026. struct nlattr *option_list;
  1027. void *hdr;
  1028. struct team_option *option;
  1029. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1030. TEAM_CMD_OPTIONS_GET);
  1031. if (IS_ERR(hdr))
  1032. return PTR_ERR(hdr);
  1033. NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
  1034. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  1035. if (!option_list)
  1036. return -EMSGSIZE;
  1037. list_for_each_entry(option, &team->option_list, list) {
  1038. struct nlattr *option_item;
  1039. long arg;
  1040. /* Include only changed options if fill all mode is not on */
  1041. if (!fillall && !option->changed)
  1042. continue;
  1043. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1044. if (!option_item)
  1045. goto nla_put_failure;
  1046. NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_NAME, option->name);
  1047. if (option->changed) {
  1048. NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_CHANGED);
  1049. option->changed = false;
  1050. }
  1051. if (option->removed)
  1052. NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_REMOVED);
  1053. switch (option->type) {
  1054. case TEAM_OPTION_TYPE_U32:
  1055. NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32);
  1056. team_option_get(team, option, &arg);
  1057. NLA_PUT_U32(skb, TEAM_ATTR_OPTION_DATA, arg);
  1058. break;
  1059. case TEAM_OPTION_TYPE_STRING:
  1060. NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING);
  1061. team_option_get(team, option, &arg);
  1062. NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_DATA,
  1063. (char *) arg);
  1064. break;
  1065. default:
  1066. BUG();
  1067. }
  1068. nla_nest_end(skb, option_item);
  1069. }
  1070. nla_nest_end(skb, option_list);
  1071. return genlmsg_end(skb, hdr);
  1072. nla_put_failure:
  1073. genlmsg_cancel(skb, hdr);
  1074. return -EMSGSIZE;
  1075. }
  1076. static int team_nl_fill_options_get_all(struct sk_buff *skb,
  1077. struct genl_info *info, int flags,
  1078. struct team *team)
  1079. {
  1080. return team_nl_fill_options_get(skb, info->snd_pid,
  1081. info->snd_seq, NLM_F_ACK,
  1082. team, true);
  1083. }
  1084. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1085. {
  1086. struct team *team;
  1087. int err;
  1088. team = team_nl_team_get(info);
  1089. if (!team)
  1090. return -EINVAL;
  1091. err = team_nl_send_generic(info, team, team_nl_fill_options_get_all);
  1092. team_nl_team_put(team);
  1093. return err;
  1094. }
  1095. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1096. {
  1097. struct team *team;
  1098. int err = 0;
  1099. int i;
  1100. struct nlattr *nl_option;
  1101. team = team_nl_team_get(info);
  1102. if (!team)
  1103. return -EINVAL;
  1104. err = -EINVAL;
  1105. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  1106. err = -EINVAL;
  1107. goto team_put;
  1108. }
  1109. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  1110. struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1];
  1111. enum team_option_type opt_type;
  1112. struct team_option *option;
  1113. char *opt_name;
  1114. bool opt_found = false;
  1115. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  1116. err = -EINVAL;
  1117. goto team_put;
  1118. }
  1119. err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX,
  1120. nl_option, team_nl_option_policy);
  1121. if (err)
  1122. goto team_put;
  1123. if (!mode_attrs[TEAM_ATTR_OPTION_NAME] ||
  1124. !mode_attrs[TEAM_ATTR_OPTION_TYPE] ||
  1125. !mode_attrs[TEAM_ATTR_OPTION_DATA]) {
  1126. err = -EINVAL;
  1127. goto team_put;
  1128. }
  1129. switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) {
  1130. case NLA_U32:
  1131. opt_type = TEAM_OPTION_TYPE_U32;
  1132. break;
  1133. case NLA_STRING:
  1134. opt_type = TEAM_OPTION_TYPE_STRING;
  1135. break;
  1136. default:
  1137. goto team_put;
  1138. }
  1139. opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]);
  1140. list_for_each_entry(option, &team->option_list, list) {
  1141. long arg;
  1142. struct nlattr *opt_data_attr;
  1143. if (option->type != opt_type ||
  1144. strcmp(option->name, opt_name))
  1145. continue;
  1146. opt_found = true;
  1147. opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA];
  1148. switch (opt_type) {
  1149. case TEAM_OPTION_TYPE_U32:
  1150. arg = nla_get_u32(opt_data_attr);
  1151. break;
  1152. case TEAM_OPTION_TYPE_STRING:
  1153. arg = (long) nla_data(opt_data_attr);
  1154. break;
  1155. default:
  1156. BUG();
  1157. }
  1158. err = team_option_set(team, option, &arg);
  1159. if (err)
  1160. goto team_put;
  1161. }
  1162. if (!opt_found) {
  1163. err = -ENOENT;
  1164. goto team_put;
  1165. }
  1166. }
  1167. team_put:
  1168. team_nl_team_put(team);
  1169. return err;
  1170. }
  1171. static int team_nl_fill_port_list_get(struct sk_buff *skb,
  1172. u32 pid, u32 seq, int flags,
  1173. struct team *team,
  1174. bool fillall)
  1175. {
  1176. struct nlattr *port_list;
  1177. void *hdr;
  1178. struct team_port *port;
  1179. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1180. TEAM_CMD_PORT_LIST_GET);
  1181. if (IS_ERR(hdr))
  1182. return PTR_ERR(hdr);
  1183. NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
  1184. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  1185. if (!port_list)
  1186. return -EMSGSIZE;
  1187. list_for_each_entry(port, &team->port_list, list) {
  1188. struct nlattr *port_item;
  1189. /* Include only changed ports if fill all mode is not on */
  1190. if (!fillall && !port->changed)
  1191. continue;
  1192. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  1193. if (!port_item)
  1194. goto nla_put_failure;
  1195. NLA_PUT_U32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex);
  1196. if (port->changed) {
  1197. NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_CHANGED);
  1198. port->changed = false;
  1199. }
  1200. if (port->removed)
  1201. NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_REMOVED);
  1202. if (port->linkup)
  1203. NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_LINKUP);
  1204. NLA_PUT_U32(skb, TEAM_ATTR_PORT_SPEED, port->speed);
  1205. NLA_PUT_U8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex);
  1206. nla_nest_end(skb, port_item);
  1207. }
  1208. nla_nest_end(skb, port_list);
  1209. return genlmsg_end(skb, hdr);
  1210. nla_put_failure:
  1211. genlmsg_cancel(skb, hdr);
  1212. return -EMSGSIZE;
  1213. }
  1214. static int team_nl_fill_port_list_get_all(struct sk_buff *skb,
  1215. struct genl_info *info, int flags,
  1216. struct team *team)
  1217. {
  1218. return team_nl_fill_port_list_get(skb, info->snd_pid,
  1219. info->snd_seq, NLM_F_ACK,
  1220. team, true);
  1221. }
  1222. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  1223. struct genl_info *info)
  1224. {
  1225. struct team *team;
  1226. int err;
  1227. team = team_nl_team_get(info);
  1228. if (!team)
  1229. return -EINVAL;
  1230. err = team_nl_send_generic(info, team, team_nl_fill_port_list_get_all);
  1231. team_nl_team_put(team);
  1232. return err;
  1233. }
  1234. static struct genl_ops team_nl_ops[] = {
  1235. {
  1236. .cmd = TEAM_CMD_NOOP,
  1237. .doit = team_nl_cmd_noop,
  1238. .policy = team_nl_policy,
  1239. },
  1240. {
  1241. .cmd = TEAM_CMD_OPTIONS_SET,
  1242. .doit = team_nl_cmd_options_set,
  1243. .policy = team_nl_policy,
  1244. .flags = GENL_ADMIN_PERM,
  1245. },
  1246. {
  1247. .cmd = TEAM_CMD_OPTIONS_GET,
  1248. .doit = team_nl_cmd_options_get,
  1249. .policy = team_nl_policy,
  1250. .flags = GENL_ADMIN_PERM,
  1251. },
  1252. {
  1253. .cmd = TEAM_CMD_PORT_LIST_GET,
  1254. .doit = team_nl_cmd_port_list_get,
  1255. .policy = team_nl_policy,
  1256. .flags = GENL_ADMIN_PERM,
  1257. },
  1258. };
  1259. static struct genl_multicast_group team_change_event_mcgrp = {
  1260. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  1261. };
  1262. static int team_nl_send_event_options_get(struct team *team)
  1263. {
  1264. struct sk_buff *skb;
  1265. int err;
  1266. struct net *net = dev_net(team->dev);
  1267. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1268. if (!skb)
  1269. return -ENOMEM;
  1270. err = team_nl_fill_options_get(skb, 0, 0, 0, team, false);
  1271. if (err < 0)
  1272. goto err_fill;
  1273. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1274. GFP_KERNEL);
  1275. return err;
  1276. err_fill:
  1277. nlmsg_free(skb);
  1278. return err;
  1279. }
  1280. static int team_nl_send_event_port_list_get(struct team *team)
  1281. {
  1282. struct sk_buff *skb;
  1283. int err;
  1284. struct net *net = dev_net(team->dev);
  1285. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1286. if (!skb)
  1287. return -ENOMEM;
  1288. err = team_nl_fill_port_list_get(skb, 0, 0, 0, team, false);
  1289. if (err < 0)
  1290. goto err_fill;
  1291. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1292. GFP_KERNEL);
  1293. return err;
  1294. err_fill:
  1295. nlmsg_free(skb);
  1296. return err;
  1297. }
  1298. static int team_nl_init(void)
  1299. {
  1300. int err;
  1301. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  1302. ARRAY_SIZE(team_nl_ops));
  1303. if (err)
  1304. return err;
  1305. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  1306. if (err)
  1307. goto err_change_event_grp_reg;
  1308. return 0;
  1309. err_change_event_grp_reg:
  1310. genl_unregister_family(&team_nl_family);
  1311. return err;
  1312. }
  1313. static void team_nl_fini(void)
  1314. {
  1315. genl_unregister_family(&team_nl_family);
  1316. }
  1317. /******************
  1318. * Change checkers
  1319. ******************/
  1320. static void __team_options_change_check(struct team *team)
  1321. {
  1322. int err;
  1323. err = team_nl_send_event_options_get(team);
  1324. if (err)
  1325. netdev_warn(team->dev, "Failed to send options change via netlink\n");
  1326. }
  1327. /* rtnl lock is held */
  1328. static void __team_port_change_check(struct team_port *port, bool linkup)
  1329. {
  1330. int err;
  1331. if (!port->removed && port->linkup == linkup)
  1332. return;
  1333. port->changed = true;
  1334. port->linkup = linkup;
  1335. if (linkup) {
  1336. struct ethtool_cmd ecmd;
  1337. err = __ethtool_get_settings(port->dev, &ecmd);
  1338. if (!err) {
  1339. port->speed = ethtool_cmd_speed(&ecmd);
  1340. port->duplex = ecmd.duplex;
  1341. goto send_event;
  1342. }
  1343. }
  1344. port->speed = 0;
  1345. port->duplex = 0;
  1346. send_event:
  1347. err = team_nl_send_event_port_list_get(port->team);
  1348. if (err)
  1349. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
  1350. port->dev->name);
  1351. }
  1352. static void team_port_change_check(struct team_port *port, bool linkup)
  1353. {
  1354. struct team *team = port->team;
  1355. mutex_lock(&team->lock);
  1356. __team_port_change_check(port, linkup);
  1357. mutex_unlock(&team->lock);
  1358. }
  1359. /************************************
  1360. * Net device notifier event handler
  1361. ************************************/
  1362. static int team_device_event(struct notifier_block *unused,
  1363. unsigned long event, void *ptr)
  1364. {
  1365. struct net_device *dev = (struct net_device *) ptr;
  1366. struct team_port *port;
  1367. port = team_port_get_rtnl(dev);
  1368. if (!port)
  1369. return NOTIFY_DONE;
  1370. switch (event) {
  1371. case NETDEV_UP:
  1372. if (netif_carrier_ok(dev))
  1373. team_port_change_check(port, true);
  1374. case NETDEV_DOWN:
  1375. team_port_change_check(port, false);
  1376. case NETDEV_CHANGE:
  1377. if (netif_running(port->dev))
  1378. team_port_change_check(port,
  1379. !!netif_carrier_ok(port->dev));
  1380. break;
  1381. case NETDEV_UNREGISTER:
  1382. team_del_slave(port->team->dev, dev);
  1383. break;
  1384. case NETDEV_FEAT_CHANGE:
  1385. team_compute_features(port->team);
  1386. break;
  1387. case NETDEV_CHANGEMTU:
  1388. /* Forbid to change mtu of underlaying device */
  1389. if (!port->team->port_mtu_change_allowed)
  1390. return NOTIFY_BAD;
  1391. break;
  1392. case NETDEV_PRE_TYPE_CHANGE:
  1393. /* Forbid to change type of underlaying device */
  1394. return NOTIFY_BAD;
  1395. }
  1396. return NOTIFY_DONE;
  1397. }
  1398. static struct notifier_block team_notifier_block __read_mostly = {
  1399. .notifier_call = team_device_event,
  1400. };
  1401. /***********************
  1402. * Module init and exit
  1403. ***********************/
  1404. static int __init team_module_init(void)
  1405. {
  1406. int err;
  1407. register_netdevice_notifier(&team_notifier_block);
  1408. err = rtnl_link_register(&team_link_ops);
  1409. if (err)
  1410. goto err_rtnl_reg;
  1411. err = team_nl_init();
  1412. if (err)
  1413. goto err_nl_init;
  1414. return 0;
  1415. err_nl_init:
  1416. rtnl_link_unregister(&team_link_ops);
  1417. err_rtnl_reg:
  1418. unregister_netdevice_notifier(&team_notifier_block);
  1419. return err;
  1420. }
  1421. static void __exit team_module_exit(void)
  1422. {
  1423. team_nl_fini();
  1424. rtnl_link_unregister(&team_link_ops);
  1425. unregister_netdevice_notifier(&team_notifier_block);
  1426. }
  1427. module_init(team_module_init);
  1428. module_exit(team_module_exit);
  1429. MODULE_LICENSE("GPL v2");
  1430. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  1431. MODULE_DESCRIPTION("Ethernet team device driver");
  1432. MODULE_ALIAS_RTNL_LINK(DRV_NAME);