team.c 40 KB

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