team.c 37 KB

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