team.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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 = vlan_vids_add_by_dev(port_dev, dev);
  484. if (err) {
  485. netdev_err(dev, "Failed to add vlan ids to device %s\n",
  486. portname);
  487. goto err_vids_add;
  488. }
  489. err = netdev_set_master(port_dev, dev);
  490. if (err) {
  491. netdev_err(dev, "Device %s failed to set master\n", portname);
  492. goto err_set_master;
  493. }
  494. err = netdev_rx_handler_register(port_dev, team_handle_frame,
  495. port);
  496. if (err) {
  497. netdev_err(dev, "Device %s failed to register rx_handler\n",
  498. portname);
  499. goto err_handler_register;
  500. }
  501. team_port_list_add_port(team, port);
  502. team_adjust_ops(team);
  503. __team_compute_features(team);
  504. __team_port_change_check(port, !!netif_carrier_ok(port_dev));
  505. netdev_info(dev, "Port device %s added\n", portname);
  506. return 0;
  507. err_handler_register:
  508. netdev_set_master(port_dev, NULL);
  509. err_set_master:
  510. vlan_vids_del_by_dev(port_dev, dev);
  511. err_vids_add:
  512. dev_close(port_dev);
  513. err_dev_open:
  514. team_port_leave(team, port);
  515. team_port_set_orig_mac(port);
  516. err_port_enter:
  517. dev_set_mtu(port_dev, port->orig.mtu);
  518. err_set_mtu:
  519. kfree(port);
  520. return err;
  521. }
  522. static int team_port_del(struct team *team, struct net_device *port_dev)
  523. {
  524. struct net_device *dev = team->dev;
  525. struct team_port *port;
  526. char *portname = port_dev->name;
  527. port = team_port_get_rtnl(port_dev);
  528. if (!port || !team_port_find(team, port)) {
  529. netdev_err(dev, "Device %s does not act as a port of this team\n",
  530. portname);
  531. return -ENOENT;
  532. }
  533. __team_port_change_check(port, false);
  534. team_port_list_del_port(team, port);
  535. team_adjust_ops(team);
  536. netdev_rx_handler_unregister(port_dev);
  537. netdev_set_master(port_dev, NULL);
  538. vlan_vids_del_by_dev(port_dev, dev);
  539. dev_close(port_dev);
  540. team_port_leave(team, port);
  541. team_port_set_orig_mac(port);
  542. dev_set_mtu(port_dev, port->orig.mtu);
  543. synchronize_rcu();
  544. kfree(port);
  545. netdev_info(dev, "Port device %s removed\n", portname);
  546. __team_compute_features(team);
  547. return 0;
  548. }
  549. /*****************
  550. * Net device ops
  551. *****************/
  552. static const char team_no_mode_kind[] = "*NOMODE*";
  553. static int team_mode_option_get(struct team *team, void *arg)
  554. {
  555. const char **str = arg;
  556. *str = team->mode ? team->mode->kind : team_no_mode_kind;
  557. return 0;
  558. }
  559. static int team_mode_option_set(struct team *team, void *arg)
  560. {
  561. const char **str = arg;
  562. return team_change_mode(team, *str);
  563. }
  564. static const struct team_option team_options[] = {
  565. {
  566. .name = "mode",
  567. .type = TEAM_OPTION_TYPE_STRING,
  568. .getter = team_mode_option_get,
  569. .setter = team_mode_option_set,
  570. },
  571. };
  572. static int team_init(struct net_device *dev)
  573. {
  574. struct team *team = netdev_priv(dev);
  575. int i;
  576. int err;
  577. team->dev = dev;
  578. mutex_init(&team->lock);
  579. team->pcpu_stats = alloc_percpu(struct team_pcpu_stats);
  580. if (!team->pcpu_stats)
  581. return -ENOMEM;
  582. for (i = 0; i < TEAM_PORT_HASHENTRIES; i++)
  583. INIT_HLIST_HEAD(&team->port_hlist[i]);
  584. INIT_LIST_HEAD(&team->port_list);
  585. team_adjust_ops(team);
  586. INIT_LIST_HEAD(&team->option_list);
  587. err = team_options_register(team, team_options, ARRAY_SIZE(team_options));
  588. if (err)
  589. goto err_options_register;
  590. netif_carrier_off(dev);
  591. return 0;
  592. err_options_register:
  593. free_percpu(team->pcpu_stats);
  594. return err;
  595. }
  596. static void team_uninit(struct net_device *dev)
  597. {
  598. struct team *team = netdev_priv(dev);
  599. struct team_port *port;
  600. struct team_port *tmp;
  601. mutex_lock(&team->lock);
  602. list_for_each_entry_safe(port, tmp, &team->port_list, list)
  603. team_port_del(team, port->dev);
  604. __team_change_mode(team, NULL); /* cleanup */
  605. __team_options_unregister(team, team_options, ARRAY_SIZE(team_options));
  606. mutex_unlock(&team->lock);
  607. }
  608. static void team_destructor(struct net_device *dev)
  609. {
  610. struct team *team = netdev_priv(dev);
  611. free_percpu(team->pcpu_stats);
  612. free_netdev(dev);
  613. }
  614. static int team_open(struct net_device *dev)
  615. {
  616. netif_carrier_on(dev);
  617. return 0;
  618. }
  619. static int team_close(struct net_device *dev)
  620. {
  621. netif_carrier_off(dev);
  622. return 0;
  623. }
  624. /*
  625. * note: already called with rcu_read_lock
  626. */
  627. static netdev_tx_t team_xmit(struct sk_buff *skb, struct net_device *dev)
  628. {
  629. struct team *team = netdev_priv(dev);
  630. bool tx_success = false;
  631. unsigned int len = skb->len;
  632. tx_success = team->ops.transmit(team, skb);
  633. if (tx_success) {
  634. struct team_pcpu_stats *pcpu_stats;
  635. pcpu_stats = this_cpu_ptr(team->pcpu_stats);
  636. u64_stats_update_begin(&pcpu_stats->syncp);
  637. pcpu_stats->tx_packets++;
  638. pcpu_stats->tx_bytes += len;
  639. u64_stats_update_end(&pcpu_stats->syncp);
  640. } else {
  641. this_cpu_inc(team->pcpu_stats->tx_dropped);
  642. }
  643. return NETDEV_TX_OK;
  644. }
  645. static void team_change_rx_flags(struct net_device *dev, int change)
  646. {
  647. struct team *team = netdev_priv(dev);
  648. struct team_port *port;
  649. int inc;
  650. rcu_read_lock();
  651. list_for_each_entry_rcu(port, &team->port_list, list) {
  652. if (change & IFF_PROMISC) {
  653. inc = dev->flags & IFF_PROMISC ? 1 : -1;
  654. dev_set_promiscuity(port->dev, inc);
  655. }
  656. if (change & IFF_ALLMULTI) {
  657. inc = dev->flags & IFF_ALLMULTI ? 1 : -1;
  658. dev_set_allmulti(port->dev, inc);
  659. }
  660. }
  661. rcu_read_unlock();
  662. }
  663. static void team_set_rx_mode(struct net_device *dev)
  664. {
  665. struct team *team = netdev_priv(dev);
  666. struct team_port *port;
  667. rcu_read_lock();
  668. list_for_each_entry_rcu(port, &team->port_list, list) {
  669. dev_uc_sync(port->dev, dev);
  670. dev_mc_sync(port->dev, dev);
  671. }
  672. rcu_read_unlock();
  673. }
  674. static int team_set_mac_address(struct net_device *dev, void *p)
  675. {
  676. struct team *team = netdev_priv(dev);
  677. struct team_port *port;
  678. struct sockaddr *addr = p;
  679. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  680. rcu_read_lock();
  681. list_for_each_entry_rcu(port, &team->port_list, list)
  682. if (team->ops.port_change_mac)
  683. team->ops.port_change_mac(team, port);
  684. rcu_read_unlock();
  685. return 0;
  686. }
  687. static int team_change_mtu(struct net_device *dev, int new_mtu)
  688. {
  689. struct team *team = netdev_priv(dev);
  690. struct team_port *port;
  691. int err;
  692. /*
  693. * Alhough this is reader, it's guarded by team lock. It's not possible
  694. * to traverse list in reverse under rcu_read_lock
  695. */
  696. mutex_lock(&team->lock);
  697. list_for_each_entry(port, &team->port_list, list) {
  698. err = dev_set_mtu(port->dev, new_mtu);
  699. if (err) {
  700. netdev_err(dev, "Device %s failed to change mtu",
  701. port->dev->name);
  702. goto unwind;
  703. }
  704. }
  705. mutex_unlock(&team->lock);
  706. dev->mtu = new_mtu;
  707. return 0;
  708. unwind:
  709. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  710. dev_set_mtu(port->dev, dev->mtu);
  711. mutex_unlock(&team->lock);
  712. return err;
  713. }
  714. static struct rtnl_link_stats64 *
  715. team_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  716. {
  717. struct team *team = netdev_priv(dev);
  718. struct team_pcpu_stats *p;
  719. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  720. u32 rx_dropped = 0, tx_dropped = 0;
  721. unsigned int start;
  722. int i;
  723. for_each_possible_cpu(i) {
  724. p = per_cpu_ptr(team->pcpu_stats, i);
  725. do {
  726. start = u64_stats_fetch_begin_bh(&p->syncp);
  727. rx_packets = p->rx_packets;
  728. rx_bytes = p->rx_bytes;
  729. rx_multicast = p->rx_multicast;
  730. tx_packets = p->tx_packets;
  731. tx_bytes = p->tx_bytes;
  732. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  733. stats->rx_packets += rx_packets;
  734. stats->rx_bytes += rx_bytes;
  735. stats->multicast += rx_multicast;
  736. stats->tx_packets += tx_packets;
  737. stats->tx_bytes += tx_bytes;
  738. /*
  739. * rx_dropped & tx_dropped are u32, updated
  740. * without syncp protection.
  741. */
  742. rx_dropped += p->rx_dropped;
  743. tx_dropped += p->tx_dropped;
  744. }
  745. stats->rx_dropped = rx_dropped;
  746. stats->tx_dropped = tx_dropped;
  747. return stats;
  748. }
  749. static int team_vlan_rx_add_vid(struct net_device *dev, uint16_t vid)
  750. {
  751. struct team *team = netdev_priv(dev);
  752. struct team_port *port;
  753. int err;
  754. /*
  755. * Alhough this is reader, it's guarded by team lock. It's not possible
  756. * to traverse list in reverse under rcu_read_lock
  757. */
  758. mutex_lock(&team->lock);
  759. list_for_each_entry(port, &team->port_list, list) {
  760. err = vlan_vid_add(port->dev, vid);
  761. if (err)
  762. goto unwind;
  763. }
  764. mutex_unlock(&team->lock);
  765. return 0;
  766. unwind:
  767. list_for_each_entry_continue_reverse(port, &team->port_list, list)
  768. vlan_vid_del(port->dev, vid);
  769. mutex_unlock(&team->lock);
  770. return err;
  771. }
  772. static int team_vlan_rx_kill_vid(struct net_device *dev, uint16_t vid)
  773. {
  774. struct team *team = netdev_priv(dev);
  775. struct team_port *port;
  776. rcu_read_lock();
  777. list_for_each_entry_rcu(port, &team->port_list, list)
  778. vlan_vid_del(port->dev, vid);
  779. rcu_read_unlock();
  780. return 0;
  781. }
  782. static int team_add_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_add(team, port_dev);
  788. mutex_unlock(&team->lock);
  789. return err;
  790. }
  791. static int team_del_slave(struct net_device *dev, struct net_device *port_dev)
  792. {
  793. struct team *team = netdev_priv(dev);
  794. int err;
  795. mutex_lock(&team->lock);
  796. err = team_port_del(team, port_dev);
  797. mutex_unlock(&team->lock);
  798. return err;
  799. }
  800. static netdev_features_t team_fix_features(struct net_device *dev,
  801. netdev_features_t features)
  802. {
  803. struct team_port *port;
  804. struct team *team = netdev_priv(dev);
  805. netdev_features_t mask;
  806. mask = features;
  807. features &= ~NETIF_F_ONE_FOR_ALL;
  808. features |= NETIF_F_ALL_FOR_ALL;
  809. rcu_read_lock();
  810. list_for_each_entry_rcu(port, &team->port_list, list) {
  811. features = netdev_increment_features(features,
  812. port->dev->features,
  813. mask);
  814. }
  815. rcu_read_unlock();
  816. return features;
  817. }
  818. static const struct net_device_ops team_netdev_ops = {
  819. .ndo_init = team_init,
  820. .ndo_uninit = team_uninit,
  821. .ndo_open = team_open,
  822. .ndo_stop = team_close,
  823. .ndo_start_xmit = team_xmit,
  824. .ndo_change_rx_flags = team_change_rx_flags,
  825. .ndo_set_rx_mode = team_set_rx_mode,
  826. .ndo_set_mac_address = team_set_mac_address,
  827. .ndo_change_mtu = team_change_mtu,
  828. .ndo_get_stats64 = team_get_stats64,
  829. .ndo_vlan_rx_add_vid = team_vlan_rx_add_vid,
  830. .ndo_vlan_rx_kill_vid = team_vlan_rx_kill_vid,
  831. .ndo_add_slave = team_add_slave,
  832. .ndo_del_slave = team_del_slave,
  833. .ndo_fix_features = team_fix_features,
  834. };
  835. /***********************
  836. * rt netlink interface
  837. ***********************/
  838. static void team_setup(struct net_device *dev)
  839. {
  840. ether_setup(dev);
  841. dev->netdev_ops = &team_netdev_ops;
  842. dev->destructor = team_destructor;
  843. dev->tx_queue_len = 0;
  844. dev->flags |= IFF_MULTICAST;
  845. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  846. /*
  847. * Indicate we support unicast address filtering. That way core won't
  848. * bring us to promisc mode in case a unicast addr is added.
  849. * Let this up to underlay drivers.
  850. */
  851. dev->priv_flags |= IFF_UNICAST_FLT;
  852. dev->features |= NETIF_F_LLTX;
  853. dev->features |= NETIF_F_GRO;
  854. dev->hw_features = NETIF_F_HW_VLAN_TX |
  855. NETIF_F_HW_VLAN_RX |
  856. NETIF_F_HW_VLAN_FILTER;
  857. dev->features |= dev->hw_features;
  858. }
  859. static int team_newlink(struct net *src_net, struct net_device *dev,
  860. struct nlattr *tb[], struct nlattr *data[])
  861. {
  862. int err;
  863. if (tb[IFLA_ADDRESS] == NULL)
  864. random_ether_addr(dev->dev_addr);
  865. err = register_netdevice(dev);
  866. if (err)
  867. return err;
  868. return 0;
  869. }
  870. static int team_validate(struct nlattr *tb[], struct nlattr *data[])
  871. {
  872. if (tb[IFLA_ADDRESS]) {
  873. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  874. return -EINVAL;
  875. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  876. return -EADDRNOTAVAIL;
  877. }
  878. return 0;
  879. }
  880. static struct rtnl_link_ops team_link_ops __read_mostly = {
  881. .kind = DRV_NAME,
  882. .priv_size = sizeof(struct team),
  883. .setup = team_setup,
  884. .newlink = team_newlink,
  885. .validate = team_validate,
  886. };
  887. /***********************************
  888. * Generic netlink custom interface
  889. ***********************************/
  890. static struct genl_family team_nl_family = {
  891. .id = GENL_ID_GENERATE,
  892. .name = TEAM_GENL_NAME,
  893. .version = TEAM_GENL_VERSION,
  894. .maxattr = TEAM_ATTR_MAX,
  895. .netnsok = true,
  896. };
  897. static const struct nla_policy team_nl_policy[TEAM_ATTR_MAX + 1] = {
  898. [TEAM_ATTR_UNSPEC] = { .type = NLA_UNSPEC, },
  899. [TEAM_ATTR_TEAM_IFINDEX] = { .type = NLA_U32 },
  900. [TEAM_ATTR_LIST_OPTION] = { .type = NLA_NESTED },
  901. [TEAM_ATTR_LIST_PORT] = { .type = NLA_NESTED },
  902. };
  903. static const struct nla_policy
  904. team_nl_option_policy[TEAM_ATTR_OPTION_MAX + 1] = {
  905. [TEAM_ATTR_OPTION_UNSPEC] = { .type = NLA_UNSPEC, },
  906. [TEAM_ATTR_OPTION_NAME] = {
  907. .type = NLA_STRING,
  908. .len = TEAM_STRING_MAX_LEN,
  909. },
  910. [TEAM_ATTR_OPTION_CHANGED] = { .type = NLA_FLAG },
  911. [TEAM_ATTR_OPTION_TYPE] = { .type = NLA_U8 },
  912. [TEAM_ATTR_OPTION_DATA] = {
  913. .type = NLA_BINARY,
  914. .len = TEAM_STRING_MAX_LEN,
  915. },
  916. };
  917. static int team_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  918. {
  919. struct sk_buff *msg;
  920. void *hdr;
  921. int err;
  922. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  923. if (!msg)
  924. return -ENOMEM;
  925. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  926. &team_nl_family, 0, TEAM_CMD_NOOP);
  927. if (IS_ERR(hdr)) {
  928. err = PTR_ERR(hdr);
  929. goto err_msg_put;
  930. }
  931. genlmsg_end(msg, hdr);
  932. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  933. err_msg_put:
  934. nlmsg_free(msg);
  935. return err;
  936. }
  937. /*
  938. * Netlink cmd functions should be locked by following two functions.
  939. * Since dev gets held here, that ensures dev won't disappear in between.
  940. */
  941. static struct team *team_nl_team_get(struct genl_info *info)
  942. {
  943. struct net *net = genl_info_net(info);
  944. int ifindex;
  945. struct net_device *dev;
  946. struct team *team;
  947. if (!info->attrs[TEAM_ATTR_TEAM_IFINDEX])
  948. return NULL;
  949. ifindex = nla_get_u32(info->attrs[TEAM_ATTR_TEAM_IFINDEX]);
  950. dev = dev_get_by_index(net, ifindex);
  951. if (!dev || dev->netdev_ops != &team_netdev_ops) {
  952. if (dev)
  953. dev_put(dev);
  954. return NULL;
  955. }
  956. team = netdev_priv(dev);
  957. mutex_lock(&team->lock);
  958. return team;
  959. }
  960. static void team_nl_team_put(struct team *team)
  961. {
  962. mutex_unlock(&team->lock);
  963. dev_put(team->dev);
  964. }
  965. static int team_nl_send_generic(struct genl_info *info, struct team *team,
  966. int (*fill_func)(struct sk_buff *skb,
  967. struct genl_info *info,
  968. int flags, struct team *team))
  969. {
  970. struct sk_buff *skb;
  971. int err;
  972. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  973. if (!skb)
  974. return -ENOMEM;
  975. err = fill_func(skb, info, NLM_F_ACK, team);
  976. if (err < 0)
  977. goto err_fill;
  978. err = genlmsg_unicast(genl_info_net(info), skb, info->snd_pid);
  979. return err;
  980. err_fill:
  981. nlmsg_free(skb);
  982. return err;
  983. }
  984. static int team_nl_fill_options_get_changed(struct sk_buff *skb,
  985. u32 pid, u32 seq, int flags,
  986. struct team *team,
  987. struct team_option *changed_option)
  988. {
  989. struct nlattr *option_list;
  990. void *hdr;
  991. struct team_option *option;
  992. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  993. TEAM_CMD_OPTIONS_GET);
  994. if (IS_ERR(hdr))
  995. return PTR_ERR(hdr);
  996. NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
  997. option_list = nla_nest_start(skb, TEAM_ATTR_LIST_OPTION);
  998. if (!option_list)
  999. return -EMSGSIZE;
  1000. list_for_each_entry(option, &team->option_list, list) {
  1001. struct nlattr *option_item;
  1002. long arg;
  1003. option_item = nla_nest_start(skb, TEAM_ATTR_ITEM_OPTION);
  1004. if (!option_item)
  1005. goto nla_put_failure;
  1006. NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_NAME, option->name);
  1007. if (option == changed_option)
  1008. NLA_PUT_FLAG(skb, TEAM_ATTR_OPTION_CHANGED);
  1009. switch (option->type) {
  1010. case TEAM_OPTION_TYPE_U32:
  1011. NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_U32);
  1012. team_option_get(team, option, &arg);
  1013. NLA_PUT_U32(skb, TEAM_ATTR_OPTION_DATA, arg);
  1014. break;
  1015. case TEAM_OPTION_TYPE_STRING:
  1016. NLA_PUT_U8(skb, TEAM_ATTR_OPTION_TYPE, NLA_STRING);
  1017. team_option_get(team, option, &arg);
  1018. NLA_PUT_STRING(skb, TEAM_ATTR_OPTION_DATA,
  1019. (char *) arg);
  1020. break;
  1021. default:
  1022. BUG();
  1023. }
  1024. nla_nest_end(skb, option_item);
  1025. }
  1026. nla_nest_end(skb, option_list);
  1027. return genlmsg_end(skb, hdr);
  1028. nla_put_failure:
  1029. genlmsg_cancel(skb, hdr);
  1030. return -EMSGSIZE;
  1031. }
  1032. static int team_nl_fill_options_get(struct sk_buff *skb,
  1033. struct genl_info *info, int flags,
  1034. struct team *team)
  1035. {
  1036. return team_nl_fill_options_get_changed(skb, info->snd_pid,
  1037. info->snd_seq, NLM_F_ACK,
  1038. team, NULL);
  1039. }
  1040. static int team_nl_cmd_options_get(struct sk_buff *skb, struct genl_info *info)
  1041. {
  1042. struct team *team;
  1043. int err;
  1044. team = team_nl_team_get(info);
  1045. if (!team)
  1046. return -EINVAL;
  1047. err = team_nl_send_generic(info, team, team_nl_fill_options_get);
  1048. team_nl_team_put(team);
  1049. return err;
  1050. }
  1051. static int team_nl_cmd_options_set(struct sk_buff *skb, struct genl_info *info)
  1052. {
  1053. struct team *team;
  1054. int err = 0;
  1055. int i;
  1056. struct nlattr *nl_option;
  1057. team = team_nl_team_get(info);
  1058. if (!team)
  1059. return -EINVAL;
  1060. err = -EINVAL;
  1061. if (!info->attrs[TEAM_ATTR_LIST_OPTION]) {
  1062. err = -EINVAL;
  1063. goto team_put;
  1064. }
  1065. nla_for_each_nested(nl_option, info->attrs[TEAM_ATTR_LIST_OPTION], i) {
  1066. struct nlattr *mode_attrs[TEAM_ATTR_OPTION_MAX + 1];
  1067. enum team_option_type opt_type;
  1068. struct team_option *option;
  1069. char *opt_name;
  1070. bool opt_found = false;
  1071. if (nla_type(nl_option) != TEAM_ATTR_ITEM_OPTION) {
  1072. err = -EINVAL;
  1073. goto team_put;
  1074. }
  1075. err = nla_parse_nested(mode_attrs, TEAM_ATTR_OPTION_MAX,
  1076. nl_option, team_nl_option_policy);
  1077. if (err)
  1078. goto team_put;
  1079. if (!mode_attrs[TEAM_ATTR_OPTION_NAME] ||
  1080. !mode_attrs[TEAM_ATTR_OPTION_TYPE] ||
  1081. !mode_attrs[TEAM_ATTR_OPTION_DATA]) {
  1082. err = -EINVAL;
  1083. goto team_put;
  1084. }
  1085. switch (nla_get_u8(mode_attrs[TEAM_ATTR_OPTION_TYPE])) {
  1086. case NLA_U32:
  1087. opt_type = TEAM_OPTION_TYPE_U32;
  1088. break;
  1089. case NLA_STRING:
  1090. opt_type = TEAM_OPTION_TYPE_STRING;
  1091. break;
  1092. default:
  1093. goto team_put;
  1094. }
  1095. opt_name = nla_data(mode_attrs[TEAM_ATTR_OPTION_NAME]);
  1096. list_for_each_entry(option, &team->option_list, list) {
  1097. long arg;
  1098. struct nlattr *opt_data_attr;
  1099. if (option->type != opt_type ||
  1100. strcmp(option->name, opt_name))
  1101. continue;
  1102. opt_found = true;
  1103. opt_data_attr = mode_attrs[TEAM_ATTR_OPTION_DATA];
  1104. switch (opt_type) {
  1105. case TEAM_OPTION_TYPE_U32:
  1106. arg = nla_get_u32(opt_data_attr);
  1107. break;
  1108. case TEAM_OPTION_TYPE_STRING:
  1109. arg = (long) nla_data(opt_data_attr);
  1110. break;
  1111. default:
  1112. BUG();
  1113. }
  1114. err = team_option_set(team, option, &arg);
  1115. if (err)
  1116. goto team_put;
  1117. }
  1118. if (!opt_found) {
  1119. err = -ENOENT;
  1120. goto team_put;
  1121. }
  1122. }
  1123. team_put:
  1124. team_nl_team_put(team);
  1125. return err;
  1126. }
  1127. static int team_nl_fill_port_list_get_changed(struct sk_buff *skb,
  1128. u32 pid, u32 seq, int flags,
  1129. struct team *team,
  1130. struct team_port *changed_port)
  1131. {
  1132. struct nlattr *port_list;
  1133. void *hdr;
  1134. struct team_port *port;
  1135. hdr = genlmsg_put(skb, pid, seq, &team_nl_family, flags,
  1136. TEAM_CMD_PORT_LIST_GET);
  1137. if (IS_ERR(hdr))
  1138. return PTR_ERR(hdr);
  1139. NLA_PUT_U32(skb, TEAM_ATTR_TEAM_IFINDEX, team->dev->ifindex);
  1140. port_list = nla_nest_start(skb, TEAM_ATTR_LIST_PORT);
  1141. if (!port_list)
  1142. return -EMSGSIZE;
  1143. list_for_each_entry(port, &team->port_list, list) {
  1144. struct nlattr *port_item;
  1145. port_item = nla_nest_start(skb, TEAM_ATTR_ITEM_PORT);
  1146. if (!port_item)
  1147. goto nla_put_failure;
  1148. NLA_PUT_U32(skb, TEAM_ATTR_PORT_IFINDEX, port->dev->ifindex);
  1149. if (port == changed_port)
  1150. NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_CHANGED);
  1151. if (port->linkup)
  1152. NLA_PUT_FLAG(skb, TEAM_ATTR_PORT_LINKUP);
  1153. NLA_PUT_U32(skb, TEAM_ATTR_PORT_SPEED, port->speed);
  1154. NLA_PUT_U8(skb, TEAM_ATTR_PORT_DUPLEX, port->duplex);
  1155. nla_nest_end(skb, port_item);
  1156. }
  1157. nla_nest_end(skb, port_list);
  1158. return genlmsg_end(skb, hdr);
  1159. nla_put_failure:
  1160. genlmsg_cancel(skb, hdr);
  1161. return -EMSGSIZE;
  1162. }
  1163. static int team_nl_fill_port_list_get(struct sk_buff *skb,
  1164. struct genl_info *info, int flags,
  1165. struct team *team)
  1166. {
  1167. return team_nl_fill_port_list_get_changed(skb, info->snd_pid,
  1168. info->snd_seq, NLM_F_ACK,
  1169. team, NULL);
  1170. }
  1171. static int team_nl_cmd_port_list_get(struct sk_buff *skb,
  1172. struct genl_info *info)
  1173. {
  1174. struct team *team;
  1175. int err;
  1176. team = team_nl_team_get(info);
  1177. if (!team)
  1178. return -EINVAL;
  1179. err = team_nl_send_generic(info, team, team_nl_fill_port_list_get);
  1180. team_nl_team_put(team);
  1181. return err;
  1182. }
  1183. static struct genl_ops team_nl_ops[] = {
  1184. {
  1185. .cmd = TEAM_CMD_NOOP,
  1186. .doit = team_nl_cmd_noop,
  1187. .policy = team_nl_policy,
  1188. },
  1189. {
  1190. .cmd = TEAM_CMD_OPTIONS_SET,
  1191. .doit = team_nl_cmd_options_set,
  1192. .policy = team_nl_policy,
  1193. .flags = GENL_ADMIN_PERM,
  1194. },
  1195. {
  1196. .cmd = TEAM_CMD_OPTIONS_GET,
  1197. .doit = team_nl_cmd_options_get,
  1198. .policy = team_nl_policy,
  1199. .flags = GENL_ADMIN_PERM,
  1200. },
  1201. {
  1202. .cmd = TEAM_CMD_PORT_LIST_GET,
  1203. .doit = team_nl_cmd_port_list_get,
  1204. .policy = team_nl_policy,
  1205. .flags = GENL_ADMIN_PERM,
  1206. },
  1207. };
  1208. static struct genl_multicast_group team_change_event_mcgrp = {
  1209. .name = TEAM_GENL_CHANGE_EVENT_MC_GRP_NAME,
  1210. };
  1211. static int team_nl_send_event_options_get(struct team *team,
  1212. struct team_option *changed_option)
  1213. {
  1214. struct sk_buff *skb;
  1215. int err;
  1216. struct net *net = dev_net(team->dev);
  1217. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1218. if (!skb)
  1219. return -ENOMEM;
  1220. err = team_nl_fill_options_get_changed(skb, 0, 0, 0, team,
  1221. changed_option);
  1222. if (err < 0)
  1223. goto err_fill;
  1224. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1225. GFP_KERNEL);
  1226. return err;
  1227. err_fill:
  1228. nlmsg_free(skb);
  1229. return err;
  1230. }
  1231. static int team_nl_send_event_port_list_get(struct team_port *port)
  1232. {
  1233. struct sk_buff *skb;
  1234. int err;
  1235. struct net *net = dev_net(port->team->dev);
  1236. skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1237. if (!skb)
  1238. return -ENOMEM;
  1239. err = team_nl_fill_port_list_get_changed(skb, 0, 0, 0,
  1240. port->team, port);
  1241. if (err < 0)
  1242. goto err_fill;
  1243. err = genlmsg_multicast_netns(net, skb, 0, team_change_event_mcgrp.id,
  1244. GFP_KERNEL);
  1245. return err;
  1246. err_fill:
  1247. nlmsg_free(skb);
  1248. return err;
  1249. }
  1250. static int team_nl_init(void)
  1251. {
  1252. int err;
  1253. err = genl_register_family_with_ops(&team_nl_family, team_nl_ops,
  1254. ARRAY_SIZE(team_nl_ops));
  1255. if (err)
  1256. return err;
  1257. err = genl_register_mc_group(&team_nl_family, &team_change_event_mcgrp);
  1258. if (err)
  1259. goto err_change_event_grp_reg;
  1260. return 0;
  1261. err_change_event_grp_reg:
  1262. genl_unregister_family(&team_nl_family);
  1263. return err;
  1264. }
  1265. static void team_nl_fini(void)
  1266. {
  1267. genl_unregister_family(&team_nl_family);
  1268. }
  1269. /******************
  1270. * Change checkers
  1271. ******************/
  1272. static void __team_options_change_check(struct team *team,
  1273. struct team_option *changed_option)
  1274. {
  1275. int err;
  1276. err = team_nl_send_event_options_get(team, changed_option);
  1277. if (err)
  1278. netdev_warn(team->dev, "Failed to send options change via netlink\n");
  1279. }
  1280. /* rtnl lock is held */
  1281. static void __team_port_change_check(struct team_port *port, bool linkup)
  1282. {
  1283. int err;
  1284. if (port->linkup == linkup)
  1285. return;
  1286. port->linkup = linkup;
  1287. if (linkup) {
  1288. struct ethtool_cmd ecmd;
  1289. err = __ethtool_get_settings(port->dev, &ecmd);
  1290. if (!err) {
  1291. port->speed = ethtool_cmd_speed(&ecmd);
  1292. port->duplex = ecmd.duplex;
  1293. goto send_event;
  1294. }
  1295. }
  1296. port->speed = 0;
  1297. port->duplex = 0;
  1298. send_event:
  1299. err = team_nl_send_event_port_list_get(port);
  1300. if (err)
  1301. netdev_warn(port->team->dev, "Failed to send port change of device %s via netlink\n",
  1302. port->dev->name);
  1303. }
  1304. static void team_port_change_check(struct team_port *port, bool linkup)
  1305. {
  1306. struct team *team = port->team;
  1307. mutex_lock(&team->lock);
  1308. __team_port_change_check(port, linkup);
  1309. mutex_unlock(&team->lock);
  1310. }
  1311. /************************************
  1312. * Net device notifier event handler
  1313. ************************************/
  1314. static int team_device_event(struct notifier_block *unused,
  1315. unsigned long event, void *ptr)
  1316. {
  1317. struct net_device *dev = (struct net_device *) ptr;
  1318. struct team_port *port;
  1319. port = team_port_get_rtnl(dev);
  1320. if (!port)
  1321. return NOTIFY_DONE;
  1322. switch (event) {
  1323. case NETDEV_UP:
  1324. if (netif_carrier_ok(dev))
  1325. team_port_change_check(port, true);
  1326. case NETDEV_DOWN:
  1327. team_port_change_check(port, false);
  1328. case NETDEV_CHANGE:
  1329. if (netif_running(port->dev))
  1330. team_port_change_check(port,
  1331. !!netif_carrier_ok(port->dev));
  1332. break;
  1333. case NETDEV_UNREGISTER:
  1334. team_del_slave(port->team->dev, dev);
  1335. break;
  1336. case NETDEV_FEAT_CHANGE:
  1337. team_compute_features(port->team);
  1338. break;
  1339. case NETDEV_CHANGEMTU:
  1340. /* Forbid to change mtu of underlaying device */
  1341. return NOTIFY_BAD;
  1342. case NETDEV_PRE_TYPE_CHANGE:
  1343. /* Forbid to change type of underlaying device */
  1344. return NOTIFY_BAD;
  1345. }
  1346. return NOTIFY_DONE;
  1347. }
  1348. static struct notifier_block team_notifier_block __read_mostly = {
  1349. .notifier_call = team_device_event,
  1350. };
  1351. /***********************
  1352. * Module init and exit
  1353. ***********************/
  1354. static int __init team_module_init(void)
  1355. {
  1356. int err;
  1357. register_netdevice_notifier(&team_notifier_block);
  1358. err = rtnl_link_register(&team_link_ops);
  1359. if (err)
  1360. goto err_rtnl_reg;
  1361. err = team_nl_init();
  1362. if (err)
  1363. goto err_nl_init;
  1364. return 0;
  1365. err_nl_init:
  1366. rtnl_link_unregister(&team_link_ops);
  1367. err_rtnl_reg:
  1368. unregister_netdevice_notifier(&team_notifier_block);
  1369. return err;
  1370. }
  1371. static void __exit team_module_exit(void)
  1372. {
  1373. team_nl_fini();
  1374. rtnl_link_unregister(&team_link_ops);
  1375. unregister_netdevice_notifier(&team_notifier_block);
  1376. }
  1377. module_init(team_module_init);
  1378. module_exit(team_module_exit);
  1379. MODULE_LICENSE("GPL v2");
  1380. MODULE_AUTHOR("Jiri Pirko <jpirko@redhat.com>");
  1381. MODULE_DESCRIPTION("Ethernet team device driver");
  1382. MODULE_ALIAS_RTNL_LINK(DRV_NAME);