team.c 37 KB

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