team.c 37 KB

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