bond_options.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. * drivers/net/bond/bond_options.c - bonding options
  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/errno.h>
  12. #include <linux/if.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/ctype.h>
  17. #include <linux/inet.h>
  18. #include <net/bonding.h>
  19. static int bond_option_active_slave_set(struct bonding *bond,
  20. const struct bond_opt_value *newval);
  21. static int bond_option_miimon_set(struct bonding *bond,
  22. const struct bond_opt_value *newval);
  23. static int bond_option_updelay_set(struct bonding *bond,
  24. const struct bond_opt_value *newval);
  25. static int bond_option_downdelay_set(struct bonding *bond,
  26. const struct bond_opt_value *newval);
  27. static int bond_option_use_carrier_set(struct bonding *bond,
  28. const struct bond_opt_value *newval);
  29. static int bond_option_arp_interval_set(struct bonding *bond,
  30. const struct bond_opt_value *newval);
  31. static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
  32. static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
  33. static int bond_option_arp_ip_targets_set(struct bonding *bond,
  34. const struct bond_opt_value *newval);
  35. static int bond_option_arp_validate_set(struct bonding *bond,
  36. const struct bond_opt_value *newval);
  37. static int bond_option_arp_all_targets_set(struct bonding *bond,
  38. const struct bond_opt_value *newval);
  39. static int bond_option_primary_set(struct bonding *bond,
  40. const struct bond_opt_value *newval);
  41. static int bond_option_primary_reselect_set(struct bonding *bond,
  42. const struct bond_opt_value *newval);
  43. static int bond_option_fail_over_mac_set(struct bonding *bond,
  44. const struct bond_opt_value *newval);
  45. static int bond_option_xmit_hash_policy_set(struct bonding *bond,
  46. const struct bond_opt_value *newval);
  47. static int bond_option_resend_igmp_set(struct bonding *bond,
  48. const struct bond_opt_value *newval);
  49. static int bond_option_num_peer_notif_set(struct bonding *bond,
  50. const struct bond_opt_value *newval);
  51. static int bond_option_all_slaves_active_set(struct bonding *bond,
  52. const struct bond_opt_value *newval);
  53. static int bond_option_min_links_set(struct bonding *bond,
  54. const struct bond_opt_value *newval);
  55. static int bond_option_lp_interval_set(struct bonding *bond,
  56. const struct bond_opt_value *newval);
  57. static int bond_option_pps_set(struct bonding *bond,
  58. const struct bond_opt_value *newval);
  59. static int bond_option_lacp_rate_set(struct bonding *bond,
  60. const struct bond_opt_value *newval);
  61. static int bond_option_ad_select_set(struct bonding *bond,
  62. const struct bond_opt_value *newval);
  63. static int bond_option_queue_id_set(struct bonding *bond,
  64. const struct bond_opt_value *newval);
  65. static int bond_option_mode_set(struct bonding *bond,
  66. const struct bond_opt_value *newval);
  67. static int bond_option_slaves_set(struct bonding *bond,
  68. const struct bond_opt_value *newval);
  69. static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
  70. const struct bond_opt_value *newval);
  71. static const struct bond_opt_value bond_mode_tbl[] = {
  72. { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT},
  73. { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
  74. { "balance-xor", BOND_MODE_XOR, 0},
  75. { "broadcast", BOND_MODE_BROADCAST, 0},
  76. { "802.3ad", BOND_MODE_8023AD, 0},
  77. { "balance-tlb", BOND_MODE_TLB, 0},
  78. { "balance-alb", BOND_MODE_ALB, 0},
  79. { NULL, -1, 0},
  80. };
  81. static const struct bond_opt_value bond_pps_tbl[] = {
  82. { "default", 1, BOND_VALFLAG_DEFAULT},
  83. { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
  84. { NULL, -1, 0},
  85. };
  86. static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
  87. { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
  88. { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
  89. { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
  90. { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
  91. { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
  92. { NULL, -1, 0},
  93. };
  94. static const struct bond_opt_value bond_arp_validate_tbl[] = {
  95. { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT},
  96. { "active", BOND_ARP_VALIDATE_ACTIVE, 0},
  97. { "backup", BOND_ARP_VALIDATE_BACKUP, 0},
  98. { "all", BOND_ARP_VALIDATE_ALL, 0},
  99. { "filter", BOND_ARP_FILTER, 0},
  100. { "filter_active", BOND_ARP_FILTER_ACTIVE, 0},
  101. { "filter_backup", BOND_ARP_FILTER_BACKUP, 0},
  102. { NULL, -1, 0},
  103. };
  104. static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
  105. { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
  106. { "all", BOND_ARP_TARGETS_ALL, 0},
  107. { NULL, -1, 0},
  108. };
  109. static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
  110. { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
  111. { "active", BOND_FOM_ACTIVE, 0},
  112. { "follow", BOND_FOM_FOLLOW, 0},
  113. { NULL, -1, 0},
  114. };
  115. static const struct bond_opt_value bond_intmax_tbl[] = {
  116. { "off", 0, BOND_VALFLAG_DEFAULT},
  117. { "maxval", INT_MAX, BOND_VALFLAG_MAX},
  118. { NULL, -1, 0}
  119. };
  120. static const struct bond_opt_value bond_lacp_rate_tbl[] = {
  121. { "slow", AD_LACP_SLOW, 0},
  122. { "fast", AD_LACP_FAST, 0},
  123. { NULL, -1, 0},
  124. };
  125. static const struct bond_opt_value bond_ad_select_tbl[] = {
  126. { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
  127. { "bandwidth", BOND_AD_BANDWIDTH, 0},
  128. { "count", BOND_AD_COUNT, 0},
  129. { NULL, -1, 0},
  130. };
  131. static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
  132. { "off", 0, 0},
  133. { "maxval", 255, BOND_VALFLAG_MAX},
  134. { "default", 1, BOND_VALFLAG_DEFAULT},
  135. { NULL, -1, 0}
  136. };
  137. static const struct bond_opt_value bond_primary_reselect_tbl[] = {
  138. { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
  139. { "better", BOND_PRI_RESELECT_BETTER, 0},
  140. { "failure", BOND_PRI_RESELECT_FAILURE, 0},
  141. { NULL, -1},
  142. };
  143. static const struct bond_opt_value bond_use_carrier_tbl[] = {
  144. { "off", 0, 0},
  145. { "on", 1, BOND_VALFLAG_DEFAULT},
  146. { NULL, -1, 0}
  147. };
  148. static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
  149. { "off", 0, BOND_VALFLAG_DEFAULT},
  150. { "on", 1, 0},
  151. { NULL, -1, 0}
  152. };
  153. static const struct bond_opt_value bond_resend_igmp_tbl[] = {
  154. { "off", 0, 0},
  155. { "maxval", 255, BOND_VALFLAG_MAX},
  156. { "default", 1, BOND_VALFLAG_DEFAULT},
  157. { NULL, -1, 0}
  158. };
  159. static const struct bond_opt_value bond_lp_interval_tbl[] = {
  160. { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
  161. { "maxval", INT_MAX, BOND_VALFLAG_MAX},
  162. { NULL, -1, 0},
  163. };
  164. static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
  165. { "off", 0, 0},
  166. { "on", 1, BOND_VALFLAG_DEFAULT},
  167. { NULL, -1, 0}
  168. };
  169. static const struct bond_option bond_opts[] = {
  170. [BOND_OPT_MODE] = {
  171. .id = BOND_OPT_MODE,
  172. .name = "mode",
  173. .desc = "bond device mode",
  174. .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
  175. .values = bond_mode_tbl,
  176. .set = bond_option_mode_set
  177. },
  178. [BOND_OPT_PACKETS_PER_SLAVE] = {
  179. .id = BOND_OPT_PACKETS_PER_SLAVE,
  180. .name = "packets_per_slave",
  181. .desc = "Packets to send per slave in RR mode",
  182. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
  183. .values = bond_pps_tbl,
  184. .set = bond_option_pps_set
  185. },
  186. [BOND_OPT_XMIT_HASH] = {
  187. .id = BOND_OPT_XMIT_HASH,
  188. .name = "xmit_hash_policy",
  189. .desc = "balance-xor, 802.3ad, and tlb hashing method",
  190. .values = bond_xmit_hashtype_tbl,
  191. .set = bond_option_xmit_hash_policy_set
  192. },
  193. [BOND_OPT_ARP_VALIDATE] = {
  194. .id = BOND_OPT_ARP_VALIDATE,
  195. .name = "arp_validate",
  196. .desc = "validate src/dst of ARP probes",
  197. .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
  198. BIT(BOND_MODE_ALB),
  199. .values = bond_arp_validate_tbl,
  200. .set = bond_option_arp_validate_set
  201. },
  202. [BOND_OPT_ARP_ALL_TARGETS] = {
  203. .id = BOND_OPT_ARP_ALL_TARGETS,
  204. .name = "arp_all_targets",
  205. .desc = "fail on any/all arp targets timeout",
  206. .values = bond_arp_all_targets_tbl,
  207. .set = bond_option_arp_all_targets_set
  208. },
  209. [BOND_OPT_FAIL_OVER_MAC] = {
  210. .id = BOND_OPT_FAIL_OVER_MAC,
  211. .name = "fail_over_mac",
  212. .desc = "For active-backup, do not set all slaves to the same MAC",
  213. .flags = BOND_OPTFLAG_NOSLAVES,
  214. .values = bond_fail_over_mac_tbl,
  215. .set = bond_option_fail_over_mac_set
  216. },
  217. [BOND_OPT_ARP_INTERVAL] = {
  218. .id = BOND_OPT_ARP_INTERVAL,
  219. .name = "arp_interval",
  220. .desc = "arp interval in milliseconds",
  221. .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
  222. BIT(BOND_MODE_ALB),
  223. .values = bond_intmax_tbl,
  224. .set = bond_option_arp_interval_set
  225. },
  226. [BOND_OPT_ARP_TARGETS] = {
  227. .id = BOND_OPT_ARP_TARGETS,
  228. .name = "arp_ip_target",
  229. .desc = "arp targets in n.n.n.n form",
  230. .flags = BOND_OPTFLAG_RAWVAL,
  231. .set = bond_option_arp_ip_targets_set
  232. },
  233. [BOND_OPT_DOWNDELAY] = {
  234. .id = BOND_OPT_DOWNDELAY,
  235. .name = "downdelay",
  236. .desc = "Delay before considering link down, in milliseconds",
  237. .values = bond_intmax_tbl,
  238. .set = bond_option_downdelay_set
  239. },
  240. [BOND_OPT_UPDELAY] = {
  241. .id = BOND_OPT_UPDELAY,
  242. .name = "updelay",
  243. .desc = "Delay before considering link up, in milliseconds",
  244. .values = bond_intmax_tbl,
  245. .set = bond_option_updelay_set
  246. },
  247. [BOND_OPT_LACP_RATE] = {
  248. .id = BOND_OPT_LACP_RATE,
  249. .name = "lacp_rate",
  250. .desc = "LACPDU tx rate to request from 802.3ad partner",
  251. .flags = BOND_OPTFLAG_IFDOWN,
  252. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  253. .values = bond_lacp_rate_tbl,
  254. .set = bond_option_lacp_rate_set
  255. },
  256. [BOND_OPT_MINLINKS] = {
  257. .id = BOND_OPT_MINLINKS,
  258. .name = "min_links",
  259. .desc = "Minimum number of available links before turning on carrier",
  260. .values = bond_intmax_tbl,
  261. .set = bond_option_min_links_set
  262. },
  263. [BOND_OPT_AD_SELECT] = {
  264. .id = BOND_OPT_AD_SELECT,
  265. .name = "ad_select",
  266. .desc = "803.ad aggregation selection logic",
  267. .flags = BOND_OPTFLAG_IFDOWN,
  268. .values = bond_ad_select_tbl,
  269. .set = bond_option_ad_select_set
  270. },
  271. [BOND_OPT_NUM_PEER_NOTIF] = {
  272. .id = BOND_OPT_NUM_PEER_NOTIF,
  273. .name = "num_unsol_na",
  274. .desc = "Number of peer notifications to send on failover event",
  275. .values = bond_num_peer_notif_tbl,
  276. .set = bond_option_num_peer_notif_set
  277. },
  278. [BOND_OPT_MIIMON] = {
  279. .id = BOND_OPT_MIIMON,
  280. .name = "miimon",
  281. .desc = "Link check interval in milliseconds",
  282. .values = bond_intmax_tbl,
  283. .set = bond_option_miimon_set
  284. },
  285. [BOND_OPT_PRIMARY] = {
  286. .id = BOND_OPT_PRIMARY,
  287. .name = "primary",
  288. .desc = "Primary network device to use",
  289. .flags = BOND_OPTFLAG_RAWVAL,
  290. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
  291. BIT(BOND_MODE_TLB) |
  292. BIT(BOND_MODE_ALB)),
  293. .set = bond_option_primary_set
  294. },
  295. [BOND_OPT_PRIMARY_RESELECT] = {
  296. .id = BOND_OPT_PRIMARY_RESELECT,
  297. .name = "primary_reselect",
  298. .desc = "Reselect primary slave once it comes up",
  299. .values = bond_primary_reselect_tbl,
  300. .set = bond_option_primary_reselect_set
  301. },
  302. [BOND_OPT_USE_CARRIER] = {
  303. .id = BOND_OPT_USE_CARRIER,
  304. .name = "use_carrier",
  305. .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
  306. .values = bond_use_carrier_tbl,
  307. .set = bond_option_use_carrier_set
  308. },
  309. [BOND_OPT_ACTIVE_SLAVE] = {
  310. .id = BOND_OPT_ACTIVE_SLAVE,
  311. .name = "active_slave",
  312. .desc = "Currently active slave",
  313. .flags = BOND_OPTFLAG_RAWVAL,
  314. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
  315. BIT(BOND_MODE_TLB) |
  316. BIT(BOND_MODE_ALB)),
  317. .set = bond_option_active_slave_set
  318. },
  319. [BOND_OPT_QUEUE_ID] = {
  320. .id = BOND_OPT_QUEUE_ID,
  321. .name = "queue_id",
  322. .desc = "Set queue id of a slave",
  323. .flags = BOND_OPTFLAG_RAWVAL,
  324. .set = bond_option_queue_id_set
  325. },
  326. [BOND_OPT_ALL_SLAVES_ACTIVE] = {
  327. .id = BOND_OPT_ALL_SLAVES_ACTIVE,
  328. .name = "all_slaves_active",
  329. .desc = "Keep all frames received on an interface by setting active flag for all slaves",
  330. .values = bond_all_slaves_active_tbl,
  331. .set = bond_option_all_slaves_active_set
  332. },
  333. [BOND_OPT_RESEND_IGMP] = {
  334. .id = BOND_OPT_RESEND_IGMP,
  335. .name = "resend_igmp",
  336. .desc = "Number of IGMP membership reports to send on link failure",
  337. .values = bond_resend_igmp_tbl,
  338. .set = bond_option_resend_igmp_set
  339. },
  340. [BOND_OPT_LP_INTERVAL] = {
  341. .id = BOND_OPT_LP_INTERVAL,
  342. .name = "lp_interval",
  343. .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
  344. .values = bond_lp_interval_tbl,
  345. .set = bond_option_lp_interval_set
  346. },
  347. [BOND_OPT_SLAVES] = {
  348. .id = BOND_OPT_SLAVES,
  349. .name = "slaves",
  350. .desc = "Slave membership management",
  351. .flags = BOND_OPTFLAG_RAWVAL,
  352. .set = bond_option_slaves_set
  353. },
  354. [BOND_OPT_TLB_DYNAMIC_LB] = {
  355. .id = BOND_OPT_TLB_DYNAMIC_LB,
  356. .name = "tlb_dynamic_lb",
  357. .desc = "Enable dynamic flow shuffling",
  358. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB)),
  359. .values = bond_tlb_dynamic_lb_tbl,
  360. .flags = BOND_OPTFLAG_IFDOWN,
  361. .set = bond_option_tlb_dynamic_lb_set,
  362. },
  363. { }
  364. };
  365. /* Searches for an option by name */
  366. const struct bond_option *bond_opt_get_by_name(const char *name)
  367. {
  368. const struct bond_option *opt;
  369. int option;
  370. for (option = 0; option < BOND_OPT_LAST; option++) {
  371. opt = bond_opt_get(option);
  372. if (opt && !strcmp(opt->name, name))
  373. return opt;
  374. }
  375. return NULL;
  376. }
  377. /* Searches for a value in opt's values[] table */
  378. const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
  379. {
  380. const struct bond_option *opt;
  381. int i;
  382. opt = bond_opt_get(option);
  383. if (WARN_ON(!opt))
  384. return NULL;
  385. for (i = 0; opt->values && opt->values[i].string; i++)
  386. if (opt->values[i].value == val)
  387. return &opt->values[i];
  388. return NULL;
  389. }
  390. /* Searches for a value in opt's values[] table which matches the flagmask */
  391. static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
  392. u32 flagmask)
  393. {
  394. int i;
  395. for (i = 0; opt->values && opt->values[i].string; i++)
  396. if (opt->values[i].flags & flagmask)
  397. return &opt->values[i];
  398. return NULL;
  399. }
  400. /* If maxval is missing then there's no range to check. In case minval is
  401. * missing then it's considered to be 0.
  402. */
  403. static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
  404. {
  405. const struct bond_opt_value *minval, *maxval;
  406. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  407. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  408. if (!maxval || (minval && val < minval->value) || val > maxval->value)
  409. return false;
  410. return true;
  411. }
  412. /**
  413. * bond_opt_parse - parse option value
  414. * @opt: the option to parse against
  415. * @val: value to parse
  416. *
  417. * This function tries to extract the value from @val and check if it's
  418. * a possible match for the option and returns NULL if a match isn't found,
  419. * or the struct_opt_value that matched. It also strips the new line from
  420. * @val->string if it's present.
  421. */
  422. const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
  423. struct bond_opt_value *val)
  424. {
  425. char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
  426. const struct bond_opt_value *tbl;
  427. const struct bond_opt_value *ret = NULL;
  428. bool checkval;
  429. int i, rv;
  430. /* No parsing if the option wants a raw val */
  431. if (opt->flags & BOND_OPTFLAG_RAWVAL)
  432. return val;
  433. tbl = opt->values;
  434. if (!tbl)
  435. goto out;
  436. /* ULLONG_MAX is used to bypass string processing */
  437. checkval = val->value != ULLONG_MAX;
  438. if (!checkval) {
  439. if (!val->string)
  440. goto out;
  441. p = strchr(val->string, '\n');
  442. if (p)
  443. *p = '\0';
  444. for (p = val->string; *p; p++)
  445. if (!(isdigit(*p) || isspace(*p)))
  446. break;
  447. /* The following code extracts the string to match or the value
  448. * and sets checkval appropriately
  449. */
  450. if (*p) {
  451. rv = sscanf(val->string, "%32s", valstr);
  452. } else {
  453. rv = sscanf(val->string, "%llu", &val->value);
  454. checkval = true;
  455. }
  456. if (!rv)
  457. goto out;
  458. }
  459. for (i = 0; tbl[i].string; i++) {
  460. /* Check for exact match */
  461. if (checkval) {
  462. if (val->value == tbl[i].value)
  463. ret = &tbl[i];
  464. } else {
  465. if (!strcmp(valstr, "default") &&
  466. (tbl[i].flags & BOND_VALFLAG_DEFAULT))
  467. ret = &tbl[i];
  468. if (!strcmp(valstr, tbl[i].string))
  469. ret = &tbl[i];
  470. }
  471. /* Found an exact match */
  472. if (ret)
  473. goto out;
  474. }
  475. /* Possible range match */
  476. if (checkval && bond_opt_check_range(opt, val->value))
  477. ret = val;
  478. out:
  479. return ret;
  480. }
  481. /* Check opt's dependencies against bond mode and currently set options */
  482. static int bond_opt_check_deps(struct bonding *bond,
  483. const struct bond_option *opt)
  484. {
  485. struct bond_params *params = &bond->params;
  486. if (test_bit(params->mode, &opt->unsuppmodes))
  487. return -EACCES;
  488. if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
  489. return -ENOTEMPTY;
  490. if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
  491. return -EBUSY;
  492. return 0;
  493. }
  494. static void bond_opt_dep_print(struct bonding *bond,
  495. const struct bond_option *opt)
  496. {
  497. const struct bond_opt_value *modeval;
  498. struct bond_params *params;
  499. params = &bond->params;
  500. modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
  501. if (test_bit(params->mode, &opt->unsuppmodes))
  502. netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
  503. opt->name, modeval->string, modeval->value);
  504. }
  505. static void bond_opt_error_interpret(struct bonding *bond,
  506. const struct bond_option *opt,
  507. int error, const struct bond_opt_value *val)
  508. {
  509. const struct bond_opt_value *minval, *maxval;
  510. char *p;
  511. switch (error) {
  512. case -EINVAL:
  513. if (val) {
  514. if (val->string) {
  515. /* sometimes RAWVAL opts may have new lines */
  516. p = strchr(val->string, '\n');
  517. if (p)
  518. *p = '\0';
  519. netdev_err(bond->dev, "option %s: invalid value (%s)\n",
  520. opt->name, val->string);
  521. } else {
  522. netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
  523. opt->name, val->value);
  524. }
  525. }
  526. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  527. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  528. if (!maxval)
  529. break;
  530. netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
  531. opt->name, minval ? minval->value : 0, maxval->value);
  532. break;
  533. case -EACCES:
  534. bond_opt_dep_print(bond, opt);
  535. break;
  536. case -ENOTEMPTY:
  537. netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
  538. opt->name);
  539. break;
  540. case -EBUSY:
  541. netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
  542. opt->name);
  543. break;
  544. default:
  545. break;
  546. }
  547. }
  548. /**
  549. * __bond_opt_set - set a bonding option
  550. * @bond: target bond device
  551. * @option: option to set
  552. * @val: value to set it to
  553. *
  554. * This function is used to change the bond's option value, it can be
  555. * used for both enabling/changing an option and for disabling it. RTNL lock
  556. * must be obtained before calling this function.
  557. */
  558. int __bond_opt_set(struct bonding *bond,
  559. unsigned int option, struct bond_opt_value *val)
  560. {
  561. const struct bond_opt_value *retval = NULL;
  562. const struct bond_option *opt;
  563. int ret = -ENOENT;
  564. ASSERT_RTNL();
  565. opt = bond_opt_get(option);
  566. if (WARN_ON(!val) || WARN_ON(!opt))
  567. goto out;
  568. ret = bond_opt_check_deps(bond, opt);
  569. if (ret)
  570. goto out;
  571. retval = bond_opt_parse(opt, val);
  572. if (!retval) {
  573. ret = -EINVAL;
  574. goto out;
  575. }
  576. ret = opt->set(bond, retval);
  577. out:
  578. if (ret)
  579. bond_opt_error_interpret(bond, opt, ret, val);
  580. else
  581. call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
  582. return ret;
  583. }
  584. /**
  585. * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
  586. * @bond: target bond device
  587. * @option: option to set
  588. * @buf: value to set it to
  589. *
  590. * This function tries to acquire RTNL without blocking and if successful
  591. * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
  592. */
  593. int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
  594. {
  595. struct bond_opt_value optval;
  596. int ret;
  597. if (!rtnl_trylock())
  598. return restart_syscall();
  599. bond_opt_initstr(&optval, buf);
  600. ret = __bond_opt_set(bond, option, &optval);
  601. rtnl_unlock();
  602. return ret;
  603. }
  604. /**
  605. * bond_opt_get - get a pointer to an option
  606. * @option: option for which to return a pointer
  607. *
  608. * This function checks if option is valid and if so returns a pointer
  609. * to its entry in the bond_opts[] option array.
  610. */
  611. const struct bond_option *bond_opt_get(unsigned int option)
  612. {
  613. if (!BOND_OPT_VALID(option))
  614. return NULL;
  615. return &bond_opts[option];
  616. }
  617. static int bond_option_mode_set(struct bonding *bond,
  618. const struct bond_opt_value *newval)
  619. {
  620. if (!bond_mode_uses_arp(newval->value) && bond->params.arp_interval) {
  621. netdev_info(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
  622. newval->string);
  623. /* disable arp monitoring */
  624. bond->params.arp_interval = 0;
  625. /* set miimon to default value */
  626. bond->params.miimon = BOND_DEFAULT_MIIMON;
  627. netdev_info(bond->dev, "Setting MII monitoring interval to %d\n",
  628. bond->params.miimon);
  629. }
  630. /* don't cache arp_validate between modes */
  631. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  632. bond->params.mode = newval->value;
  633. return 0;
  634. }
  635. static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
  636. struct slave *slave)
  637. {
  638. return bond_uses_primary(bond) && slave ? slave->dev : NULL;
  639. }
  640. struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
  641. {
  642. struct slave *slave = rcu_dereference(bond->curr_active_slave);
  643. return __bond_option_active_slave_get(bond, slave);
  644. }
  645. static int bond_option_active_slave_set(struct bonding *bond,
  646. const struct bond_opt_value *newval)
  647. {
  648. char ifname[IFNAMSIZ] = { 0, };
  649. struct net_device *slave_dev;
  650. int ret = 0;
  651. sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
  652. if (!strlen(ifname) || newval->string[0] == '\n') {
  653. slave_dev = NULL;
  654. } else {
  655. slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  656. if (!slave_dev)
  657. return -ENODEV;
  658. }
  659. if (slave_dev) {
  660. if (!netif_is_bond_slave(slave_dev)) {
  661. netdev_err(bond->dev, "Device %s is not bonding slave\n",
  662. slave_dev->name);
  663. return -EINVAL;
  664. }
  665. if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
  666. netdev_err(bond->dev, "Device %s is not our slave\n",
  667. slave_dev->name);
  668. return -EINVAL;
  669. }
  670. }
  671. block_netpoll_tx();
  672. /* check to see if we are clearing active */
  673. if (!slave_dev) {
  674. netdev_info(bond->dev, "Clearing current active slave\n");
  675. RCU_INIT_POINTER(bond->curr_active_slave, NULL);
  676. bond_select_active_slave(bond);
  677. } else {
  678. struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
  679. struct slave *new_active = bond_slave_get_rtnl(slave_dev);
  680. BUG_ON(!new_active);
  681. if (new_active == old_active) {
  682. /* do nothing */
  683. netdev_info(bond->dev, "%s is already the current active slave\n",
  684. new_active->dev->name);
  685. } else {
  686. if (old_active && (new_active->link == BOND_LINK_UP) &&
  687. bond_slave_is_up(new_active)) {
  688. netdev_info(bond->dev, "Setting %s as active slave\n",
  689. new_active->dev->name);
  690. bond_change_active_slave(bond, new_active);
  691. } else {
  692. netdev_err(bond->dev, "Could not set %s as active slave; either %s is down or the link is down\n",
  693. new_active->dev->name,
  694. new_active->dev->name);
  695. ret = -EINVAL;
  696. }
  697. }
  698. }
  699. unblock_netpoll_tx();
  700. return ret;
  701. }
  702. /* There are two tricky bits here. First, if MII monitoring is activated, then
  703. * we must disable ARP monitoring. Second, if the timer isn't running, we must
  704. * start it.
  705. */
  706. static int bond_option_miimon_set(struct bonding *bond,
  707. const struct bond_opt_value *newval)
  708. {
  709. netdev_info(bond->dev, "Setting MII monitoring interval to %llu\n",
  710. newval->value);
  711. bond->params.miimon = newval->value;
  712. if (bond->params.updelay)
  713. netdev_info(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
  714. bond->params.updelay * bond->params.miimon);
  715. if (bond->params.downdelay)
  716. netdev_info(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
  717. bond->params.downdelay * bond->params.miimon);
  718. if (newval->value && bond->params.arp_interval) {
  719. netdev_info(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
  720. bond->params.arp_interval = 0;
  721. if (bond->params.arp_validate)
  722. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  723. }
  724. if (bond->dev->flags & IFF_UP) {
  725. /* If the interface is up, we may need to fire off
  726. * the MII timer. If the interface is down, the
  727. * timer will get fired off when the open function
  728. * is called.
  729. */
  730. if (!newval->value) {
  731. cancel_delayed_work_sync(&bond->mii_work);
  732. } else {
  733. cancel_delayed_work_sync(&bond->arp_work);
  734. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  735. }
  736. }
  737. return 0;
  738. }
  739. /* Set up and down delays. These must be multiples of the
  740. * MII monitoring value, and are stored internally as the multiplier.
  741. * Thus, we must translate to MS for the real world.
  742. */
  743. static int bond_option_updelay_set(struct bonding *bond,
  744. const struct bond_opt_value *newval)
  745. {
  746. int value = newval->value;
  747. if (!bond->params.miimon) {
  748. netdev_err(bond->dev, "Unable to set up delay as MII monitoring is disabled\n");
  749. return -EPERM;
  750. }
  751. if ((value % bond->params.miimon) != 0) {
  752. netdev_warn(bond->dev, "up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  753. value, bond->params.miimon,
  754. (value / bond->params.miimon) *
  755. bond->params.miimon);
  756. }
  757. bond->params.updelay = value / bond->params.miimon;
  758. netdev_info(bond->dev, "Setting up delay to %d\n",
  759. bond->params.updelay * bond->params.miimon);
  760. return 0;
  761. }
  762. static int bond_option_downdelay_set(struct bonding *bond,
  763. const struct bond_opt_value *newval)
  764. {
  765. int value = newval->value;
  766. if (!bond->params.miimon) {
  767. netdev_err(bond->dev, "Unable to set down delay as MII monitoring is disabled\n");
  768. return -EPERM;
  769. }
  770. if ((value % bond->params.miimon) != 0) {
  771. netdev_warn(bond->dev, "down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
  772. value, bond->params.miimon,
  773. (value / bond->params.miimon) *
  774. bond->params.miimon);
  775. }
  776. bond->params.downdelay = value / bond->params.miimon;
  777. netdev_info(bond->dev, "Setting down delay to %d\n",
  778. bond->params.downdelay * bond->params.miimon);
  779. return 0;
  780. }
  781. static int bond_option_use_carrier_set(struct bonding *bond,
  782. const struct bond_opt_value *newval)
  783. {
  784. netdev_info(bond->dev, "Setting use_carrier to %llu\n",
  785. newval->value);
  786. bond->params.use_carrier = newval->value;
  787. return 0;
  788. }
  789. /* There are two tricky bits here. First, if ARP monitoring is activated, then
  790. * we must disable MII monitoring. Second, if the ARP timer isn't running,
  791. * we must start it.
  792. */
  793. static int bond_option_arp_interval_set(struct bonding *bond,
  794. const struct bond_opt_value *newval)
  795. {
  796. netdev_info(bond->dev, "Setting ARP monitoring interval to %llu\n",
  797. newval->value);
  798. bond->params.arp_interval = newval->value;
  799. if (newval->value) {
  800. if (bond->params.miimon) {
  801. netdev_info(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
  802. bond->params.miimon = 0;
  803. }
  804. if (!bond->params.arp_targets[0])
  805. netdev_info(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
  806. }
  807. if (bond->dev->flags & IFF_UP) {
  808. /* If the interface is up, we may need to fire off
  809. * the ARP timer. If the interface is down, the
  810. * timer will get fired off when the open function
  811. * is called.
  812. */
  813. if (!newval->value) {
  814. if (bond->params.arp_validate)
  815. bond->recv_probe = NULL;
  816. cancel_delayed_work_sync(&bond->arp_work);
  817. } else {
  818. /* arp_validate can be set only in active-backup mode */
  819. bond->recv_probe = bond_arp_rcv;
  820. cancel_delayed_work_sync(&bond->mii_work);
  821. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  822. }
  823. }
  824. return 0;
  825. }
  826. static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
  827. __be32 target,
  828. unsigned long last_rx)
  829. {
  830. __be32 *targets = bond->params.arp_targets;
  831. struct list_head *iter;
  832. struct slave *slave;
  833. if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
  834. bond_for_each_slave(bond, slave, iter)
  835. slave->target_last_arp_rx[slot] = last_rx;
  836. targets[slot] = target;
  837. }
  838. }
  839. static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  840. {
  841. __be32 *targets = bond->params.arp_targets;
  842. int ind;
  843. if (!bond_is_ip_target_ok(target)) {
  844. netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
  845. &target);
  846. return -EINVAL;
  847. }
  848. if (bond_get_targets_ip(targets, target) != -1) { /* dup */
  849. netdev_err(bond->dev, "ARP target %pI4 is already present\n",
  850. &target);
  851. return -EINVAL;
  852. }
  853. ind = bond_get_targets_ip(targets, 0); /* first free slot */
  854. if (ind == -1) {
  855. netdev_err(bond->dev, "ARP target table is full!\n");
  856. return -EINVAL;
  857. }
  858. netdev_info(bond->dev, "Adding ARP target %pI4\n", &target);
  859. _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
  860. return 0;
  861. }
  862. static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  863. {
  864. return _bond_option_arp_ip_target_add(bond, target);
  865. }
  866. static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
  867. {
  868. __be32 *targets = bond->params.arp_targets;
  869. struct list_head *iter;
  870. struct slave *slave;
  871. unsigned long *targets_rx;
  872. int ind, i;
  873. if (!bond_is_ip_target_ok(target)) {
  874. netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
  875. &target);
  876. return -EINVAL;
  877. }
  878. ind = bond_get_targets_ip(targets, target);
  879. if (ind == -1) {
  880. netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
  881. &target);
  882. return -EINVAL;
  883. }
  884. if (ind == 0 && !targets[1] && bond->params.arp_interval)
  885. netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
  886. netdev_info(bond->dev, "Removing ARP target %pI4\n", &target);
  887. bond_for_each_slave(bond, slave, iter) {
  888. targets_rx = slave->target_last_arp_rx;
  889. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  890. targets_rx[i] = targets_rx[i+1];
  891. targets_rx[i] = 0;
  892. }
  893. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  894. targets[i] = targets[i+1];
  895. targets[i] = 0;
  896. return 0;
  897. }
  898. void bond_option_arp_ip_targets_clear(struct bonding *bond)
  899. {
  900. int i;
  901. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  902. _bond_options_arp_ip_target_set(bond, i, 0, 0);
  903. }
  904. static int bond_option_arp_ip_targets_set(struct bonding *bond,
  905. const struct bond_opt_value *newval)
  906. {
  907. int ret = -EPERM;
  908. __be32 target;
  909. if (newval->string) {
  910. if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
  911. netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
  912. &target);
  913. return ret;
  914. }
  915. if (newval->string[0] == '+')
  916. ret = bond_option_arp_ip_target_add(bond, target);
  917. else if (newval->string[0] == '-')
  918. ret = bond_option_arp_ip_target_rem(bond, target);
  919. else
  920. netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
  921. } else {
  922. target = newval->value;
  923. ret = bond_option_arp_ip_target_add(bond, target);
  924. }
  925. return ret;
  926. }
  927. static int bond_option_arp_validate_set(struct bonding *bond,
  928. const struct bond_opt_value *newval)
  929. {
  930. netdev_info(bond->dev, "Setting arp_validate to %s (%llu)\n",
  931. newval->string, newval->value);
  932. if (bond->dev->flags & IFF_UP) {
  933. if (!newval->value)
  934. bond->recv_probe = NULL;
  935. else if (bond->params.arp_interval)
  936. bond->recv_probe = bond_arp_rcv;
  937. }
  938. bond->params.arp_validate = newval->value;
  939. return 0;
  940. }
  941. static int bond_option_arp_all_targets_set(struct bonding *bond,
  942. const struct bond_opt_value *newval)
  943. {
  944. netdev_info(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
  945. newval->string, newval->value);
  946. bond->params.arp_all_targets = newval->value;
  947. return 0;
  948. }
  949. static int bond_option_primary_set(struct bonding *bond,
  950. const struct bond_opt_value *newval)
  951. {
  952. char *p, *primary = newval->string;
  953. struct list_head *iter;
  954. struct slave *slave;
  955. block_netpoll_tx();
  956. p = strchr(primary, '\n');
  957. if (p)
  958. *p = '\0';
  959. /* check to see if we are clearing primary */
  960. if (!strlen(primary)) {
  961. netdev_info(bond->dev, "Setting primary slave to None\n");
  962. RCU_INIT_POINTER(bond->primary_slave, NULL);
  963. memset(bond->params.primary, 0, sizeof(bond->params.primary));
  964. bond_select_active_slave(bond);
  965. goto out;
  966. }
  967. bond_for_each_slave(bond, slave, iter) {
  968. if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
  969. netdev_info(bond->dev, "Setting %s as primary slave\n",
  970. slave->dev->name);
  971. rcu_assign_pointer(bond->primary_slave, slave);
  972. strcpy(bond->params.primary, slave->dev->name);
  973. bond_select_active_slave(bond);
  974. goto out;
  975. }
  976. }
  977. if (rtnl_dereference(bond->primary_slave)) {
  978. netdev_info(bond->dev, "Setting primary slave to None\n");
  979. RCU_INIT_POINTER(bond->primary_slave, NULL);
  980. bond_select_active_slave(bond);
  981. }
  982. strncpy(bond->params.primary, primary, IFNAMSIZ);
  983. bond->params.primary[IFNAMSIZ - 1] = 0;
  984. netdev_info(bond->dev, "Recording %s as primary, but it has not been enslaved to %s yet\n",
  985. primary, bond->dev->name);
  986. out:
  987. unblock_netpoll_tx();
  988. return 0;
  989. }
  990. static int bond_option_primary_reselect_set(struct bonding *bond,
  991. const struct bond_opt_value *newval)
  992. {
  993. netdev_info(bond->dev, "Setting primary_reselect to %s (%llu)\n",
  994. newval->string, newval->value);
  995. bond->params.primary_reselect = newval->value;
  996. block_netpoll_tx();
  997. bond_select_active_slave(bond);
  998. unblock_netpoll_tx();
  999. return 0;
  1000. }
  1001. static int bond_option_fail_over_mac_set(struct bonding *bond,
  1002. const struct bond_opt_value *newval)
  1003. {
  1004. netdev_info(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
  1005. newval->string, newval->value);
  1006. bond->params.fail_over_mac = newval->value;
  1007. return 0;
  1008. }
  1009. static int bond_option_xmit_hash_policy_set(struct bonding *bond,
  1010. const struct bond_opt_value *newval)
  1011. {
  1012. netdev_info(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
  1013. newval->string, newval->value);
  1014. bond->params.xmit_policy = newval->value;
  1015. return 0;
  1016. }
  1017. static int bond_option_resend_igmp_set(struct bonding *bond,
  1018. const struct bond_opt_value *newval)
  1019. {
  1020. netdev_info(bond->dev, "Setting resend_igmp to %llu\n",
  1021. newval->value);
  1022. bond->params.resend_igmp = newval->value;
  1023. return 0;
  1024. }
  1025. static int bond_option_num_peer_notif_set(struct bonding *bond,
  1026. const struct bond_opt_value *newval)
  1027. {
  1028. bond->params.num_peer_notif = newval->value;
  1029. return 0;
  1030. }
  1031. static int bond_option_all_slaves_active_set(struct bonding *bond,
  1032. const struct bond_opt_value *newval)
  1033. {
  1034. struct list_head *iter;
  1035. struct slave *slave;
  1036. if (newval->value == bond->params.all_slaves_active)
  1037. return 0;
  1038. bond->params.all_slaves_active = newval->value;
  1039. bond_for_each_slave(bond, slave, iter) {
  1040. if (!bond_is_active_slave(slave)) {
  1041. if (newval->value)
  1042. slave->inactive = 0;
  1043. else
  1044. slave->inactive = 1;
  1045. }
  1046. }
  1047. return 0;
  1048. }
  1049. static int bond_option_min_links_set(struct bonding *bond,
  1050. const struct bond_opt_value *newval)
  1051. {
  1052. netdev_info(bond->dev, "Setting min links value to %llu\n",
  1053. newval->value);
  1054. bond->params.min_links = newval->value;
  1055. return 0;
  1056. }
  1057. static int bond_option_lp_interval_set(struct bonding *bond,
  1058. const struct bond_opt_value *newval)
  1059. {
  1060. bond->params.lp_interval = newval->value;
  1061. return 0;
  1062. }
  1063. static int bond_option_pps_set(struct bonding *bond,
  1064. const struct bond_opt_value *newval)
  1065. {
  1066. bond->params.packets_per_slave = newval->value;
  1067. if (newval->value > 0) {
  1068. bond->params.reciprocal_packets_per_slave =
  1069. reciprocal_value(newval->value);
  1070. } else {
  1071. /* reciprocal_packets_per_slave is unused if
  1072. * packets_per_slave is 0 or 1, just initialize it
  1073. */
  1074. bond->params.reciprocal_packets_per_slave =
  1075. (struct reciprocal_value) { 0 };
  1076. }
  1077. return 0;
  1078. }
  1079. static int bond_option_lacp_rate_set(struct bonding *bond,
  1080. const struct bond_opt_value *newval)
  1081. {
  1082. netdev_info(bond->dev, "Setting LACP rate to %s (%llu)\n",
  1083. newval->string, newval->value);
  1084. bond->params.lacp_fast = newval->value;
  1085. bond_3ad_update_lacp_rate(bond);
  1086. return 0;
  1087. }
  1088. static int bond_option_ad_select_set(struct bonding *bond,
  1089. const struct bond_opt_value *newval)
  1090. {
  1091. netdev_info(bond->dev, "Setting ad_select to %s (%llu)\n",
  1092. newval->string, newval->value);
  1093. bond->params.ad_select = newval->value;
  1094. return 0;
  1095. }
  1096. static int bond_option_queue_id_set(struct bonding *bond,
  1097. const struct bond_opt_value *newval)
  1098. {
  1099. struct slave *slave, *update_slave;
  1100. struct net_device *sdev;
  1101. struct list_head *iter;
  1102. char *delim;
  1103. int ret = 0;
  1104. u16 qid;
  1105. /* delim will point to queue id if successful */
  1106. delim = strchr(newval->string, ':');
  1107. if (!delim)
  1108. goto err_no_cmd;
  1109. /* Terminate string that points to device name and bump it
  1110. * up one, so we can read the queue id there.
  1111. */
  1112. *delim = '\0';
  1113. if (sscanf(++delim, "%hd\n", &qid) != 1)
  1114. goto err_no_cmd;
  1115. /* Check buffer length, valid ifname and queue id */
  1116. if (!dev_valid_name(newval->string) ||
  1117. qid > bond->dev->real_num_tx_queues)
  1118. goto err_no_cmd;
  1119. /* Get the pointer to that interface if it exists */
  1120. sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
  1121. if (!sdev)
  1122. goto err_no_cmd;
  1123. /* Search for thes slave and check for duplicate qids */
  1124. update_slave = NULL;
  1125. bond_for_each_slave(bond, slave, iter) {
  1126. if (sdev == slave->dev)
  1127. /* We don't need to check the matching
  1128. * slave for dups, since we're overwriting it
  1129. */
  1130. update_slave = slave;
  1131. else if (qid && qid == slave->queue_id) {
  1132. goto err_no_cmd;
  1133. }
  1134. }
  1135. if (!update_slave)
  1136. goto err_no_cmd;
  1137. /* Actually set the qids for the slave */
  1138. update_slave->queue_id = qid;
  1139. out:
  1140. return ret;
  1141. err_no_cmd:
  1142. netdev_info(bond->dev, "invalid input for queue_id set\n");
  1143. ret = -EPERM;
  1144. goto out;
  1145. }
  1146. static int bond_option_slaves_set(struct bonding *bond,
  1147. const struct bond_opt_value *newval)
  1148. {
  1149. char command[IFNAMSIZ + 1] = { 0, };
  1150. struct net_device *dev;
  1151. char *ifname;
  1152. int ret;
  1153. sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
  1154. ifname = command + 1;
  1155. if ((strlen(command) <= 1) ||
  1156. !dev_valid_name(ifname))
  1157. goto err_no_cmd;
  1158. dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  1159. if (!dev) {
  1160. netdev_info(bond->dev, "interface %s does not exist!\n",
  1161. ifname);
  1162. ret = -ENODEV;
  1163. goto out;
  1164. }
  1165. switch (command[0]) {
  1166. case '+':
  1167. netdev_info(bond->dev, "Adding slave %s\n", dev->name);
  1168. ret = bond_enslave(bond->dev, dev);
  1169. break;
  1170. case '-':
  1171. netdev_info(bond->dev, "Removing slave %s\n", dev->name);
  1172. ret = bond_release(bond->dev, dev);
  1173. break;
  1174. default:
  1175. goto err_no_cmd;
  1176. }
  1177. out:
  1178. return ret;
  1179. err_no_cmd:
  1180. netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
  1181. ret = -EPERM;
  1182. goto out;
  1183. }
  1184. static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
  1185. const struct bond_opt_value *newval)
  1186. {
  1187. netdev_info(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
  1188. newval->string, newval->value);
  1189. bond->params.tlb_dynamic_lb = newval->value;
  1190. return 0;
  1191. }