bond_netlink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
  3. * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
  4. * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/if_link.h>
  16. #include <linux/if_ether.h>
  17. #include <net/netlink.h>
  18. #include <net/rtnetlink.h>
  19. #include <net/bonding.h>
  20. static size_t bond_get_slave_size(const struct net_device *bond_dev,
  21. const struct net_device *slave_dev)
  22. {
  23. return nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_STATE */
  24. nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_MII_STATUS */
  25. nla_total_size(sizeof(u32)) + /* IFLA_BOND_SLAVE_LINK_FAILURE_COUNT */
  26. nla_total_size(MAX_ADDR_LEN) + /* IFLA_BOND_SLAVE_PERM_HWADDR */
  27. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_QUEUE_ID */
  28. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_AGGREGATOR_ID */
  29. nla_total_size(sizeof(u8)) + /* IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE */
  30. nla_total_size(sizeof(u16)) + /* IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE */
  31. 0;
  32. }
  33. static int bond_fill_slave_info(struct sk_buff *skb,
  34. const struct net_device *bond_dev,
  35. const struct net_device *slave_dev)
  36. {
  37. struct slave *slave = bond_slave_get_rtnl(slave_dev);
  38. if (nla_put_u8(skb, IFLA_BOND_SLAVE_STATE, bond_slave_state(slave)))
  39. goto nla_put_failure;
  40. if (nla_put_u8(skb, IFLA_BOND_SLAVE_MII_STATUS, slave->link))
  41. goto nla_put_failure;
  42. if (nla_put_u32(skb, IFLA_BOND_SLAVE_LINK_FAILURE_COUNT,
  43. slave->link_failure_count))
  44. goto nla_put_failure;
  45. if (nla_put(skb, IFLA_BOND_SLAVE_PERM_HWADDR,
  46. slave_dev->addr_len, slave->perm_hwaddr))
  47. goto nla_put_failure;
  48. if (nla_put_u16(skb, IFLA_BOND_SLAVE_QUEUE_ID, slave->queue_id))
  49. goto nla_put_failure;
  50. if (BOND_MODE(slave->bond) == BOND_MODE_8023AD) {
  51. const struct aggregator *agg;
  52. const struct port *ad_port;
  53. ad_port = &SLAVE_AD_INFO(slave)->port;
  54. agg = SLAVE_AD_INFO(slave)->port.aggregator;
  55. if (agg) {
  56. if (nla_put_u16(skb, IFLA_BOND_SLAVE_AD_AGGREGATOR_ID,
  57. agg->aggregator_identifier))
  58. goto nla_put_failure;
  59. if (nla_put_u8(skb,
  60. IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE,
  61. ad_port->actor_oper_port_state))
  62. goto nla_put_failure;
  63. if (nla_put_u16(skb,
  64. IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE,
  65. ad_port->partner_oper.port_state))
  66. goto nla_put_failure;
  67. }
  68. }
  69. return 0;
  70. nla_put_failure:
  71. return -EMSGSIZE;
  72. }
  73. static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
  74. [IFLA_BOND_MODE] = { .type = NLA_U8 },
  75. [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
  76. [IFLA_BOND_MIIMON] = { .type = NLA_U32 },
  77. [IFLA_BOND_UPDELAY] = { .type = NLA_U32 },
  78. [IFLA_BOND_DOWNDELAY] = { .type = NLA_U32 },
  79. [IFLA_BOND_USE_CARRIER] = { .type = NLA_U8 },
  80. [IFLA_BOND_ARP_INTERVAL] = { .type = NLA_U32 },
  81. [IFLA_BOND_ARP_IP_TARGET] = { .type = NLA_NESTED },
  82. [IFLA_BOND_ARP_VALIDATE] = { .type = NLA_U32 },
  83. [IFLA_BOND_ARP_ALL_TARGETS] = { .type = NLA_U32 },
  84. [IFLA_BOND_PRIMARY] = { .type = NLA_U32 },
  85. [IFLA_BOND_PRIMARY_RESELECT] = { .type = NLA_U8 },
  86. [IFLA_BOND_FAIL_OVER_MAC] = { .type = NLA_U8 },
  87. [IFLA_BOND_XMIT_HASH_POLICY] = { .type = NLA_U8 },
  88. [IFLA_BOND_RESEND_IGMP] = { .type = NLA_U32 },
  89. [IFLA_BOND_NUM_PEER_NOTIF] = { .type = NLA_U8 },
  90. [IFLA_BOND_ALL_SLAVES_ACTIVE] = { .type = NLA_U8 },
  91. [IFLA_BOND_MIN_LINKS] = { .type = NLA_U32 },
  92. [IFLA_BOND_LP_INTERVAL] = { .type = NLA_U32 },
  93. [IFLA_BOND_PACKETS_PER_SLAVE] = { .type = NLA_U32 },
  94. [IFLA_BOND_AD_LACP_RATE] = { .type = NLA_U8 },
  95. [IFLA_BOND_AD_SELECT] = { .type = NLA_U8 },
  96. [IFLA_BOND_AD_INFO] = { .type = NLA_NESTED },
  97. [IFLA_BOND_AD_ACTOR_SYS_PRIO] = { .type = NLA_U16 },
  98. [IFLA_BOND_AD_USER_PORT_KEY] = { .type = NLA_U16 },
  99. [IFLA_BOND_AD_ACTOR_SYSTEM] = { .type = NLA_BINARY,
  100. .len = ETH_ALEN },
  101. [IFLA_BOND_TLB_DYNAMIC_LB] = { .type = NLA_U8 },
  102. };
  103. static const struct nla_policy bond_slave_policy[IFLA_BOND_SLAVE_MAX + 1] = {
  104. [IFLA_BOND_SLAVE_QUEUE_ID] = { .type = NLA_U16 },
  105. };
  106. static int bond_validate(struct nlattr *tb[], struct nlattr *data[],
  107. struct netlink_ext_ack *extack)
  108. {
  109. if (tb[IFLA_ADDRESS]) {
  110. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  111. return -EINVAL;
  112. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  113. return -EADDRNOTAVAIL;
  114. }
  115. return 0;
  116. }
  117. static int bond_slave_changelink(struct net_device *bond_dev,
  118. struct net_device *slave_dev,
  119. struct nlattr *tb[], struct nlattr *data[],
  120. struct netlink_ext_ack *extack)
  121. {
  122. struct bonding *bond = netdev_priv(bond_dev);
  123. struct bond_opt_value newval;
  124. int err;
  125. if (!data)
  126. return 0;
  127. if (data[IFLA_BOND_SLAVE_QUEUE_ID]) {
  128. u16 queue_id = nla_get_u16(data[IFLA_BOND_SLAVE_QUEUE_ID]);
  129. char queue_id_str[IFNAMSIZ + 7];
  130. /* queue_id option setting expects slave_name:queue_id */
  131. snprintf(queue_id_str, sizeof(queue_id_str), "%s:%u\n",
  132. slave_dev->name, queue_id);
  133. bond_opt_initstr(&newval, queue_id_str);
  134. err = __bond_opt_set(bond, BOND_OPT_QUEUE_ID, &newval);
  135. if (err)
  136. return err;
  137. }
  138. return 0;
  139. }
  140. static int bond_changelink(struct net_device *bond_dev, struct nlattr *tb[],
  141. struct nlattr *data[],
  142. struct netlink_ext_ack *extack)
  143. {
  144. struct bonding *bond = netdev_priv(bond_dev);
  145. struct bond_opt_value newval;
  146. int miimon = 0;
  147. int err;
  148. if (!data)
  149. return 0;
  150. if (data[IFLA_BOND_MODE]) {
  151. int mode = nla_get_u8(data[IFLA_BOND_MODE]);
  152. bond_opt_initval(&newval, mode);
  153. err = __bond_opt_set(bond, BOND_OPT_MODE, &newval);
  154. if (err)
  155. return err;
  156. }
  157. if (data[IFLA_BOND_ACTIVE_SLAVE]) {
  158. int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
  159. struct net_device *slave_dev;
  160. char *active_slave = "";
  161. if (ifindex != 0) {
  162. slave_dev = __dev_get_by_index(dev_net(bond_dev),
  163. ifindex);
  164. if (!slave_dev)
  165. return -ENODEV;
  166. active_slave = slave_dev->name;
  167. }
  168. bond_opt_initstr(&newval, active_slave);
  169. err = __bond_opt_set(bond, BOND_OPT_ACTIVE_SLAVE, &newval);
  170. if (err)
  171. return err;
  172. }
  173. if (data[IFLA_BOND_MIIMON]) {
  174. miimon = nla_get_u32(data[IFLA_BOND_MIIMON]);
  175. bond_opt_initval(&newval, miimon);
  176. err = __bond_opt_set(bond, BOND_OPT_MIIMON, &newval);
  177. if (err)
  178. return err;
  179. }
  180. if (data[IFLA_BOND_UPDELAY]) {
  181. int updelay = nla_get_u32(data[IFLA_BOND_UPDELAY]);
  182. bond_opt_initval(&newval, updelay);
  183. err = __bond_opt_set(bond, BOND_OPT_UPDELAY, &newval);
  184. if (err)
  185. return err;
  186. }
  187. if (data[IFLA_BOND_DOWNDELAY]) {
  188. int downdelay = nla_get_u32(data[IFLA_BOND_DOWNDELAY]);
  189. bond_opt_initval(&newval, downdelay);
  190. err = __bond_opt_set(bond, BOND_OPT_DOWNDELAY, &newval);
  191. if (err)
  192. return err;
  193. }
  194. if (data[IFLA_BOND_USE_CARRIER]) {
  195. int use_carrier = nla_get_u8(data[IFLA_BOND_USE_CARRIER]);
  196. bond_opt_initval(&newval, use_carrier);
  197. err = __bond_opt_set(bond, BOND_OPT_USE_CARRIER, &newval);
  198. if (err)
  199. return err;
  200. }
  201. if (data[IFLA_BOND_ARP_INTERVAL]) {
  202. int arp_interval = nla_get_u32(data[IFLA_BOND_ARP_INTERVAL]);
  203. if (arp_interval && miimon) {
  204. netdev_err(bond->dev, "ARP monitoring cannot be used with MII monitoring\n");
  205. return -EINVAL;
  206. }
  207. bond_opt_initval(&newval, arp_interval);
  208. err = __bond_opt_set(bond, BOND_OPT_ARP_INTERVAL, &newval);
  209. if (err)
  210. return err;
  211. }
  212. if (data[IFLA_BOND_ARP_IP_TARGET]) {
  213. struct nlattr *attr;
  214. int i = 0, rem;
  215. bond_option_arp_ip_targets_clear(bond);
  216. nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
  217. __be32 target;
  218. if (nla_len(attr) < sizeof(target))
  219. return -EINVAL;
  220. target = nla_get_be32(attr);
  221. bond_opt_initval(&newval, (__force u64)target);
  222. err = __bond_opt_set(bond, BOND_OPT_ARP_TARGETS,
  223. &newval);
  224. if (err)
  225. break;
  226. i++;
  227. }
  228. if (i == 0 && bond->params.arp_interval)
  229. netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
  230. if (err)
  231. return err;
  232. }
  233. if (data[IFLA_BOND_ARP_VALIDATE]) {
  234. int arp_validate = nla_get_u32(data[IFLA_BOND_ARP_VALIDATE]);
  235. if (arp_validate && miimon) {
  236. netdev_err(bond->dev, "ARP validating cannot be used with MII monitoring\n");
  237. return -EINVAL;
  238. }
  239. bond_opt_initval(&newval, arp_validate);
  240. err = __bond_opt_set(bond, BOND_OPT_ARP_VALIDATE, &newval);
  241. if (err)
  242. return err;
  243. }
  244. if (data[IFLA_BOND_ARP_ALL_TARGETS]) {
  245. int arp_all_targets =
  246. nla_get_u32(data[IFLA_BOND_ARP_ALL_TARGETS]);
  247. bond_opt_initval(&newval, arp_all_targets);
  248. err = __bond_opt_set(bond, BOND_OPT_ARP_ALL_TARGETS, &newval);
  249. if (err)
  250. return err;
  251. }
  252. if (data[IFLA_BOND_PRIMARY]) {
  253. int ifindex = nla_get_u32(data[IFLA_BOND_PRIMARY]);
  254. struct net_device *dev;
  255. char *primary = "";
  256. dev = __dev_get_by_index(dev_net(bond_dev), ifindex);
  257. if (dev)
  258. primary = dev->name;
  259. bond_opt_initstr(&newval, primary);
  260. err = __bond_opt_set(bond, BOND_OPT_PRIMARY, &newval);
  261. if (err)
  262. return err;
  263. }
  264. if (data[IFLA_BOND_PRIMARY_RESELECT]) {
  265. int primary_reselect =
  266. nla_get_u8(data[IFLA_BOND_PRIMARY_RESELECT]);
  267. bond_opt_initval(&newval, primary_reselect);
  268. err = __bond_opt_set(bond, BOND_OPT_PRIMARY_RESELECT, &newval);
  269. if (err)
  270. return err;
  271. }
  272. if (data[IFLA_BOND_FAIL_OVER_MAC]) {
  273. int fail_over_mac =
  274. nla_get_u8(data[IFLA_BOND_FAIL_OVER_MAC]);
  275. bond_opt_initval(&newval, fail_over_mac);
  276. err = __bond_opt_set(bond, BOND_OPT_FAIL_OVER_MAC, &newval);
  277. if (err)
  278. return err;
  279. }
  280. if (data[IFLA_BOND_XMIT_HASH_POLICY]) {
  281. int xmit_hash_policy =
  282. nla_get_u8(data[IFLA_BOND_XMIT_HASH_POLICY]);
  283. bond_opt_initval(&newval, xmit_hash_policy);
  284. err = __bond_opt_set(bond, BOND_OPT_XMIT_HASH, &newval);
  285. if (err)
  286. return err;
  287. }
  288. if (data[IFLA_BOND_RESEND_IGMP]) {
  289. int resend_igmp =
  290. nla_get_u32(data[IFLA_BOND_RESEND_IGMP]);
  291. bond_opt_initval(&newval, resend_igmp);
  292. err = __bond_opt_set(bond, BOND_OPT_RESEND_IGMP, &newval);
  293. if (err)
  294. return err;
  295. }
  296. if (data[IFLA_BOND_NUM_PEER_NOTIF]) {
  297. int num_peer_notif =
  298. nla_get_u8(data[IFLA_BOND_NUM_PEER_NOTIF]);
  299. bond_opt_initval(&newval, num_peer_notif);
  300. err = __bond_opt_set(bond, BOND_OPT_NUM_PEER_NOTIF, &newval);
  301. if (err)
  302. return err;
  303. }
  304. if (data[IFLA_BOND_ALL_SLAVES_ACTIVE]) {
  305. int all_slaves_active =
  306. nla_get_u8(data[IFLA_BOND_ALL_SLAVES_ACTIVE]);
  307. bond_opt_initval(&newval, all_slaves_active);
  308. err = __bond_opt_set(bond, BOND_OPT_ALL_SLAVES_ACTIVE, &newval);
  309. if (err)
  310. return err;
  311. }
  312. if (data[IFLA_BOND_MIN_LINKS]) {
  313. int min_links =
  314. nla_get_u32(data[IFLA_BOND_MIN_LINKS]);
  315. bond_opt_initval(&newval, min_links);
  316. err = __bond_opt_set(bond, BOND_OPT_MINLINKS, &newval);
  317. if (err)
  318. return err;
  319. }
  320. if (data[IFLA_BOND_LP_INTERVAL]) {
  321. int lp_interval =
  322. nla_get_u32(data[IFLA_BOND_LP_INTERVAL]);
  323. bond_opt_initval(&newval, lp_interval);
  324. err = __bond_opt_set(bond, BOND_OPT_LP_INTERVAL, &newval);
  325. if (err)
  326. return err;
  327. }
  328. if (data[IFLA_BOND_PACKETS_PER_SLAVE]) {
  329. int packets_per_slave =
  330. nla_get_u32(data[IFLA_BOND_PACKETS_PER_SLAVE]);
  331. bond_opt_initval(&newval, packets_per_slave);
  332. err = __bond_opt_set(bond, BOND_OPT_PACKETS_PER_SLAVE, &newval);
  333. if (err)
  334. return err;
  335. }
  336. if (data[IFLA_BOND_AD_LACP_RATE]) {
  337. int lacp_rate =
  338. nla_get_u8(data[IFLA_BOND_AD_LACP_RATE]);
  339. bond_opt_initval(&newval, lacp_rate);
  340. err = __bond_opt_set(bond, BOND_OPT_LACP_RATE, &newval);
  341. if (err)
  342. return err;
  343. }
  344. if (data[IFLA_BOND_AD_SELECT]) {
  345. int ad_select =
  346. nla_get_u8(data[IFLA_BOND_AD_SELECT]);
  347. bond_opt_initval(&newval, ad_select);
  348. err = __bond_opt_set(bond, BOND_OPT_AD_SELECT, &newval);
  349. if (err)
  350. return err;
  351. }
  352. if (data[IFLA_BOND_AD_ACTOR_SYS_PRIO]) {
  353. int actor_sys_prio =
  354. nla_get_u16(data[IFLA_BOND_AD_ACTOR_SYS_PRIO]);
  355. bond_opt_initval(&newval, actor_sys_prio);
  356. err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYS_PRIO, &newval);
  357. if (err)
  358. return err;
  359. }
  360. if (data[IFLA_BOND_AD_USER_PORT_KEY]) {
  361. int port_key =
  362. nla_get_u16(data[IFLA_BOND_AD_USER_PORT_KEY]);
  363. bond_opt_initval(&newval, port_key);
  364. err = __bond_opt_set(bond, BOND_OPT_AD_USER_PORT_KEY, &newval);
  365. if (err)
  366. return err;
  367. }
  368. if (data[IFLA_BOND_AD_ACTOR_SYSTEM]) {
  369. if (nla_len(data[IFLA_BOND_AD_ACTOR_SYSTEM]) != ETH_ALEN)
  370. return -EINVAL;
  371. bond_opt_initval(&newval,
  372. nla_get_u64(data[IFLA_BOND_AD_ACTOR_SYSTEM]));
  373. err = __bond_opt_set(bond, BOND_OPT_AD_ACTOR_SYSTEM, &newval);
  374. if (err)
  375. return err;
  376. }
  377. if (data[IFLA_BOND_TLB_DYNAMIC_LB]) {
  378. int dynamic_lb = nla_get_u8(data[IFLA_BOND_TLB_DYNAMIC_LB]);
  379. bond_opt_initval(&newval, dynamic_lb);
  380. err = __bond_opt_set(bond, BOND_OPT_TLB_DYNAMIC_LB, &newval);
  381. if (err)
  382. return err;
  383. }
  384. return 0;
  385. }
  386. static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
  387. struct nlattr *tb[], struct nlattr *data[],
  388. struct netlink_ext_ack *extack)
  389. {
  390. int err;
  391. err = bond_changelink(bond_dev, tb, data, extack);
  392. if (err < 0)
  393. return err;
  394. err = register_netdevice(bond_dev);
  395. netif_carrier_off(bond_dev);
  396. if (!err) {
  397. struct bonding *bond = netdev_priv(bond_dev);
  398. bond_work_init_all(bond);
  399. }
  400. return err;
  401. }
  402. static size_t bond_get_size(const struct net_device *bond_dev)
  403. {
  404. return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
  405. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ACTIVE_SLAVE */
  406. nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIIMON */
  407. nla_total_size(sizeof(u32)) + /* IFLA_BOND_UPDELAY */
  408. nla_total_size(sizeof(u32)) + /* IFLA_BOND_DOWNDELAY */
  409. nla_total_size(sizeof(u8)) + /* IFLA_BOND_USE_CARRIER */
  410. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_INTERVAL */
  411. /* IFLA_BOND_ARP_IP_TARGET */
  412. nla_total_size(sizeof(struct nlattr)) +
  413. nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
  414. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_VALIDATE */
  415. nla_total_size(sizeof(u32)) + /* IFLA_BOND_ARP_ALL_TARGETS */
  416. nla_total_size(sizeof(u32)) + /* IFLA_BOND_PRIMARY */
  417. nla_total_size(sizeof(u8)) + /* IFLA_BOND_PRIMARY_RESELECT */
  418. nla_total_size(sizeof(u8)) + /* IFLA_BOND_FAIL_OVER_MAC */
  419. nla_total_size(sizeof(u8)) + /* IFLA_BOND_XMIT_HASH_POLICY */
  420. nla_total_size(sizeof(u32)) + /* IFLA_BOND_RESEND_IGMP */
  421. nla_total_size(sizeof(u8)) + /* IFLA_BOND_NUM_PEER_NOTIF */
  422. nla_total_size(sizeof(u8)) + /* IFLA_BOND_ALL_SLAVES_ACTIVE */
  423. nla_total_size(sizeof(u32)) + /* IFLA_BOND_MIN_LINKS */
  424. nla_total_size(sizeof(u32)) + /* IFLA_BOND_LP_INTERVAL */
  425. nla_total_size(sizeof(u32)) + /* IFLA_BOND_PACKETS_PER_SLAVE */
  426. nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_LACP_RATE */
  427. nla_total_size(sizeof(u8)) + /* IFLA_BOND_AD_SELECT */
  428. nla_total_size(sizeof(struct nlattr)) + /* IFLA_BOND_AD_INFO */
  429. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_AGGREGATOR */
  430. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_NUM_PORTS */
  431. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_ACTOR_KEY */
  432. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_INFO_PARTNER_KEY*/
  433. nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_INFO_PARTNER_MAC*/
  434. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_ACTOR_SYS_PRIO */
  435. nla_total_size(sizeof(u16)) + /* IFLA_BOND_AD_USER_PORT_KEY */
  436. nla_total_size(ETH_ALEN) + /* IFLA_BOND_AD_ACTOR_SYSTEM */
  437. nla_total_size(sizeof(u8)) + /* IFLA_BOND_TLB_DYNAMIC_LB */
  438. 0;
  439. }
  440. static int bond_option_active_slave_get_ifindex(struct bonding *bond)
  441. {
  442. const struct net_device *slave;
  443. int ifindex;
  444. rcu_read_lock();
  445. slave = bond_option_active_slave_get_rcu(bond);
  446. ifindex = slave ? slave->ifindex : 0;
  447. rcu_read_unlock();
  448. return ifindex;
  449. }
  450. static int bond_fill_info(struct sk_buff *skb,
  451. const struct net_device *bond_dev)
  452. {
  453. struct bonding *bond = netdev_priv(bond_dev);
  454. unsigned int packets_per_slave;
  455. int ifindex, i, targets_added;
  456. struct nlattr *targets;
  457. struct slave *primary;
  458. if (nla_put_u8(skb, IFLA_BOND_MODE, BOND_MODE(bond)))
  459. goto nla_put_failure;
  460. ifindex = bond_option_active_slave_get_ifindex(bond);
  461. if (ifindex && nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, ifindex))
  462. goto nla_put_failure;
  463. if (nla_put_u32(skb, IFLA_BOND_MIIMON, bond->params.miimon))
  464. goto nla_put_failure;
  465. if (nla_put_u32(skb, IFLA_BOND_UPDELAY,
  466. bond->params.updelay * bond->params.miimon))
  467. goto nla_put_failure;
  468. if (nla_put_u32(skb, IFLA_BOND_DOWNDELAY,
  469. bond->params.downdelay * bond->params.miimon))
  470. goto nla_put_failure;
  471. if (nla_put_u8(skb, IFLA_BOND_USE_CARRIER, bond->params.use_carrier))
  472. goto nla_put_failure;
  473. if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
  474. goto nla_put_failure;
  475. targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
  476. if (!targets)
  477. goto nla_put_failure;
  478. targets_added = 0;
  479. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
  480. if (bond->params.arp_targets[i]) {
  481. if (nla_put_be32(skb, i, bond->params.arp_targets[i]))
  482. goto nla_put_failure;
  483. targets_added = 1;
  484. }
  485. }
  486. if (targets_added)
  487. nla_nest_end(skb, targets);
  488. else
  489. nla_nest_cancel(skb, targets);
  490. if (nla_put_u32(skb, IFLA_BOND_ARP_VALIDATE, bond->params.arp_validate))
  491. goto nla_put_failure;
  492. if (nla_put_u32(skb, IFLA_BOND_ARP_ALL_TARGETS,
  493. bond->params.arp_all_targets))
  494. goto nla_put_failure;
  495. primary = rtnl_dereference(bond->primary_slave);
  496. if (primary &&
  497. nla_put_u32(skb, IFLA_BOND_PRIMARY, primary->dev->ifindex))
  498. goto nla_put_failure;
  499. if (nla_put_u8(skb, IFLA_BOND_PRIMARY_RESELECT,
  500. bond->params.primary_reselect))
  501. goto nla_put_failure;
  502. if (nla_put_u8(skb, IFLA_BOND_FAIL_OVER_MAC,
  503. bond->params.fail_over_mac))
  504. goto nla_put_failure;
  505. if (nla_put_u8(skb, IFLA_BOND_XMIT_HASH_POLICY,
  506. bond->params.xmit_policy))
  507. goto nla_put_failure;
  508. if (nla_put_u32(skb, IFLA_BOND_RESEND_IGMP,
  509. bond->params.resend_igmp))
  510. goto nla_put_failure;
  511. if (nla_put_u8(skb, IFLA_BOND_NUM_PEER_NOTIF,
  512. bond->params.num_peer_notif))
  513. goto nla_put_failure;
  514. if (nla_put_u8(skb, IFLA_BOND_ALL_SLAVES_ACTIVE,
  515. bond->params.all_slaves_active))
  516. goto nla_put_failure;
  517. if (nla_put_u32(skb, IFLA_BOND_MIN_LINKS,
  518. bond->params.min_links))
  519. goto nla_put_failure;
  520. if (nla_put_u32(skb, IFLA_BOND_LP_INTERVAL,
  521. bond->params.lp_interval))
  522. goto nla_put_failure;
  523. packets_per_slave = bond->params.packets_per_slave;
  524. if (nla_put_u32(skb, IFLA_BOND_PACKETS_PER_SLAVE,
  525. packets_per_slave))
  526. goto nla_put_failure;
  527. if (nla_put_u8(skb, IFLA_BOND_AD_LACP_RATE,
  528. bond->params.lacp_fast))
  529. goto nla_put_failure;
  530. if (nla_put_u8(skb, IFLA_BOND_AD_SELECT,
  531. bond->params.ad_select))
  532. goto nla_put_failure;
  533. if (nla_put_u8(skb, IFLA_BOND_TLB_DYNAMIC_LB,
  534. bond->params.tlb_dynamic_lb))
  535. goto nla_put_failure;
  536. if (BOND_MODE(bond) == BOND_MODE_8023AD) {
  537. struct ad_info info;
  538. if (capable(CAP_NET_ADMIN)) {
  539. if (nla_put_u16(skb, IFLA_BOND_AD_ACTOR_SYS_PRIO,
  540. bond->params.ad_actor_sys_prio))
  541. goto nla_put_failure;
  542. if (nla_put_u16(skb, IFLA_BOND_AD_USER_PORT_KEY,
  543. bond->params.ad_user_port_key))
  544. goto nla_put_failure;
  545. if (nla_put(skb, IFLA_BOND_AD_ACTOR_SYSTEM,
  546. ETH_ALEN, &bond->params.ad_actor_system))
  547. goto nla_put_failure;
  548. }
  549. if (!bond_3ad_get_active_agg_info(bond, &info)) {
  550. struct nlattr *nest;
  551. nest = nla_nest_start(skb, IFLA_BOND_AD_INFO);
  552. if (!nest)
  553. goto nla_put_failure;
  554. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_AGGREGATOR,
  555. info.aggregator_id))
  556. goto nla_put_failure;
  557. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_NUM_PORTS,
  558. info.ports))
  559. goto nla_put_failure;
  560. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_ACTOR_KEY,
  561. info.actor_key))
  562. goto nla_put_failure;
  563. if (nla_put_u16(skb, IFLA_BOND_AD_INFO_PARTNER_KEY,
  564. info.partner_key))
  565. goto nla_put_failure;
  566. if (nla_put(skb, IFLA_BOND_AD_INFO_PARTNER_MAC,
  567. sizeof(info.partner_system),
  568. &info.partner_system))
  569. goto nla_put_failure;
  570. nla_nest_end(skb, nest);
  571. }
  572. }
  573. return 0;
  574. nla_put_failure:
  575. return -EMSGSIZE;
  576. }
  577. struct rtnl_link_ops bond_link_ops __read_mostly = {
  578. .kind = "bond",
  579. .priv_size = sizeof(struct bonding),
  580. .setup = bond_setup,
  581. .maxtype = IFLA_BOND_MAX,
  582. .policy = bond_policy,
  583. .validate = bond_validate,
  584. .newlink = bond_newlink,
  585. .changelink = bond_changelink,
  586. .get_size = bond_get_size,
  587. .fill_info = bond_fill_info,
  588. .get_num_tx_queues = bond_get_num_tx_queues,
  589. .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
  590. as for TX queues */
  591. .slave_maxtype = IFLA_BOND_SLAVE_MAX,
  592. .slave_policy = bond_slave_policy,
  593. .slave_changelink = bond_slave_changelink,
  594. .get_slave_size = bond_get_slave_size,
  595. .fill_slave_info = bond_fill_slave_info,
  596. };
  597. int __init bond_netlink_init(void)
  598. {
  599. return rtnl_link_register(&bond_link_ops);
  600. }
  601. void bond_netlink_fini(void)
  602. {
  603. rtnl_link_unregister(&bond_link_ops);
  604. }
  605. MODULE_ALIAS_RTNL_LINK("bond");