team.c 39 KB

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