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[BOND_OPT_LAST] = {
  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. /* Searches for an option by name */
  365. const struct bond_option *bond_opt_get_by_name(const char *name)
  366. {
  367. const struct bond_option *opt;
  368. int option;
  369. for (option = 0; option < BOND_OPT_LAST; option++) {
  370. opt = bond_opt_get(option);
  371. if (opt && !strcmp(opt->name, name))
  372. return opt;
  373. }
  374. return NULL;
  375. }
  376. /* Searches for a value in opt's values[] table */
  377. const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
  378. {
  379. const struct bond_option *opt;
  380. int i;
  381. opt = bond_opt_get(option);
  382. if (WARN_ON(!opt))
  383. return NULL;
  384. for (i = 0; opt->values && opt->values[i].string; i++)
  385. if (opt->values[i].value == val)
  386. return &opt->values[i];
  387. return NULL;
  388. }
  389. /* Searches for a value in opt's values[] table which matches the flagmask */
  390. static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
  391. u32 flagmask)
  392. {
  393. int i;
  394. for (i = 0; opt->values && opt->values[i].string; i++)
  395. if (opt->values[i].flags & flagmask)
  396. return &opt->values[i];
  397. return NULL;
  398. }
  399. /* If maxval is missing then there's no range to check. In case minval is
  400. * missing then it's considered to be 0.
  401. */
  402. static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
  403. {
  404. const struct bond_opt_value *minval, *maxval;
  405. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  406. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  407. if (!maxval || (minval && val < minval->value) || val > maxval->value)
  408. return false;
  409. return true;
  410. }
  411. /**
  412. * bond_opt_parse - parse option value
  413. * @opt: the option to parse against
  414. * @val: value to parse
  415. *
  416. * This function tries to extract the value from @val and check if it's
  417. * a possible match for the option and returns NULL if a match isn't found,
  418. * or the struct_opt_value that matched. It also strips the new line from
  419. * @val->string if it's present.
  420. */
  421. const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
  422. struct bond_opt_value *val)
  423. {
  424. char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
  425. const struct bond_opt_value *tbl;
  426. const struct bond_opt_value *ret = NULL;
  427. bool checkval;
  428. int i, rv;
  429. /* No parsing if the option wants a raw val */
  430. if (opt->flags & BOND_OPTFLAG_RAWVAL)
  431. return val;
  432. tbl = opt->values;
  433. if (!tbl)
  434. goto out;
  435. /* ULLONG_MAX is used to bypass string processing */
  436. checkval = val->value != ULLONG_MAX;
  437. if (!checkval) {
  438. if (!val->string)
  439. goto out;
  440. p = strchr(val->string, '\n');
  441. if (p)
  442. *p = '\0';
  443. for (p = val->string; *p; p++)
  444. if (!(isdigit(*p) || isspace(*p)))
  445. break;
  446. /* The following code extracts the string to match or the value
  447. * and sets checkval appropriately
  448. */
  449. if (*p) {
  450. rv = sscanf(val->string, "%32s", valstr);
  451. } else {
  452. rv = sscanf(val->string, "%llu", &val->value);
  453. checkval = true;
  454. }
  455. if (!rv)
  456. goto out;
  457. }
  458. for (i = 0; tbl[i].string; i++) {
  459. /* Check for exact match */
  460. if (checkval) {
  461. if (val->value == tbl[i].value)
  462. ret = &tbl[i];
  463. } else {
  464. if (!strcmp(valstr, "default") &&
  465. (tbl[i].flags & BOND_VALFLAG_DEFAULT))
  466. ret = &tbl[i];
  467. if (!strcmp(valstr, tbl[i].string))
  468. ret = &tbl[i];
  469. }
  470. /* Found an exact match */
  471. if (ret)
  472. goto out;
  473. }
  474. /* Possible range match */
  475. if (checkval && bond_opt_check_range(opt, val->value))
  476. ret = val;
  477. out:
  478. return ret;
  479. }
  480. /* Check opt's dependencies against bond mode and currently set options */
  481. static int bond_opt_check_deps(struct bonding *bond,
  482. const struct bond_option *opt)
  483. {
  484. struct bond_params *params = &bond->params;
  485. if (test_bit(params->mode, &opt->unsuppmodes))
  486. return -EACCES;
  487. if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
  488. return -ENOTEMPTY;
  489. if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
  490. return -EBUSY;
  491. return 0;
  492. }
  493. static void bond_opt_dep_print(struct bonding *bond,
  494. const struct bond_option *opt)
  495. {
  496. const struct bond_opt_value *modeval;
  497. struct bond_params *params;
  498. params = &bond->params;
  499. modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
  500. if (test_bit(params->mode, &opt->unsuppmodes))
  501. netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
  502. opt->name, modeval->string, modeval->value);
  503. }
  504. static void bond_opt_error_interpret(struct bonding *bond,
  505. const struct bond_option *opt,
  506. int error, const struct bond_opt_value *val)
  507. {
  508. const struct bond_opt_value *minval, *maxval;
  509. char *p;
  510. switch (error) {
  511. case -EINVAL:
  512. if (val) {
  513. if (val->string) {
  514. /* sometimes RAWVAL opts may have new lines */
  515. p = strchr(val->string, '\n');
  516. if (p)
  517. *p = '\0';
  518. netdev_err(bond->dev, "option %s: invalid value (%s)\n",
  519. opt->name, val->string);
  520. } else {
  521. netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
  522. opt->name, val->value);
  523. }
  524. }
  525. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  526. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  527. if (!maxval)
  528. break;
  529. netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
  530. opt->name, minval ? minval->value : 0, maxval->value);
  531. break;
  532. case -EACCES:
  533. bond_opt_dep_print(bond, opt);
  534. break;
  535. case -ENOTEMPTY:
  536. netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
  537. opt->name);
  538. break;
  539. case -EBUSY:
  540. netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
  541. opt->name);
  542. break;
  543. default:
  544. break;
  545. }
  546. }
  547. /**
  548. * __bond_opt_set - set a bonding option
  549. * @bond: target bond device
  550. * @option: option to set
  551. * @val: value to set it to
  552. *
  553. * This function is used to change the bond's option value, it can be
  554. * used for both enabling/changing an option and for disabling it. RTNL lock
  555. * must be obtained before calling this function.
  556. */
  557. int __bond_opt_set(struct bonding *bond,
  558. unsigned int option, struct bond_opt_value *val)
  559. {
  560. const struct bond_opt_value *retval = NULL;
  561. const struct bond_option *opt;
  562. int ret = -ENOENT;
  563. ASSERT_RTNL();
  564. opt = bond_opt_get(option);
  565. if (WARN_ON(!val) || WARN_ON(!opt))
  566. goto out;
  567. ret = bond_opt_check_deps(bond, opt);
  568. if (ret)
  569. goto out;
  570. retval = bond_opt_parse(opt, val);
  571. if (!retval) {
  572. ret = -EINVAL;
  573. goto out;
  574. }
  575. ret = opt->set(bond, retval);
  576. out:
  577. if (ret)
  578. bond_opt_error_interpret(bond, opt, ret, val);
  579. else if (bond->dev->reg_state == NETREG_REGISTERED)
  580. call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
  581. return ret;
  582. }
  583. /**
  584. * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
  585. * @bond: target bond device
  586. * @option: option to set
  587. * @buf: value to set it to
  588. *
  589. * This function tries to acquire RTNL without blocking and if successful
  590. * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
  591. */
  592. int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
  593. {
  594. struct bond_opt_value optval;
  595. int ret;
  596. if (!rtnl_trylock())
  597. return restart_syscall();
  598. bond_opt_initstr(&optval, buf);
  599. ret = __bond_opt_set(bond, option, &optval);
  600. rtnl_unlock();
  601. return ret;
  602. }
  603. /**
  604. * bond_opt_get - get a pointer to an option
  605. * @option: option for which to return a pointer
  606. *
  607. * This function checks if option is valid and if so returns a pointer
  608. * to its entry in the bond_opts[] option array.
  609. */
  610. const struct bond_option *bond_opt_get(unsigned int option)
  611. {
  612. if (!BOND_OPT_VALID(option))
  613. return NULL;
  614. return &bond_opts[option];
  615. }
  616. static int bond_option_mode_set(struct bonding *bond,
  617. const struct bond_opt_value *newval)
  618. {
  619. if (!bond_mode_uses_arp(newval->value) && bond->params.arp_interval) {
  620. netdev_info(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
  621. newval->string);
  622. /* disable arp monitoring */
  623. bond->params.arp_interval = 0;
  624. /* set miimon to default value */
  625. bond->params.miimon = BOND_DEFAULT_MIIMON;
  626. netdev_info(bond->dev, "Setting MII monitoring interval to %d\n",
  627. bond->params.miimon);
  628. }
  629. /* don't cache arp_validate between modes */
  630. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  631. bond->params.mode = newval->value;
  632. return 0;
  633. }
  634. static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
  635. struct slave *slave)
  636. {
  637. return bond_uses_primary(bond) && slave ? slave->dev : NULL;
  638. }
  639. struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
  640. {
  641. struct slave *slave = rcu_dereference(bond->curr_active_slave);
  642. return __bond_option_active_slave_get(bond, slave);
  643. }
  644. static int bond_option_active_slave_set(struct bonding *bond,
  645. const struct bond_opt_value *newval)
  646. {
  647. char ifname[IFNAMSIZ] = { 0, };
  648. struct net_device *slave_dev;
  649. int ret = 0;
  650. sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
  651. if (!strlen(ifname) || newval->string[0] == '\n') {
  652. slave_dev = NULL;
  653. } else {
  654. slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  655. if (!slave_dev)
  656. return -ENODEV;
  657. }
  658. if (slave_dev) {
  659. if (!netif_is_bond_slave(slave_dev)) {
  660. netdev_err(bond->dev, "Device %s is not bonding slave\n",
  661. slave_dev->name);
  662. return -EINVAL;
  663. }
  664. if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
  665. netdev_err(bond->dev, "Device %s is not our slave\n",
  666. slave_dev->name);
  667. return -EINVAL;
  668. }
  669. }
  670. block_netpoll_tx();
  671. /* check to see if we are clearing active */
  672. if (!slave_dev) {
  673. netdev_info(bond->dev, "Clearing current active slave\n");
  674. RCU_INIT_POINTER(bond->curr_active_slave, NULL);
  675. bond_select_active_slave(bond);
  676. } else {
  677. struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
  678. struct slave *new_active = bond_slave_get_rtnl(slave_dev);
  679. BUG_ON(!new_active);
  680. if (new_active == old_active) {
  681. /* do nothing */
  682. netdev_info(bond->dev, "%s is already the current active slave\n",
  683. new_active->dev->name);
  684. } else {
  685. if (old_active && (new_active->link == BOND_LINK_UP) &&
  686. bond_slave_is_up(new_active)) {
  687. netdev_info(bond->dev, "Setting %s as active slave\n",
  688. new_active->dev->name);
  689. bond_change_active_slave(bond, new_active);
  690. } else {
  691. netdev_err(bond->dev, "Could not set %s as active slave; either %s is down or the link is down\n",
  692. new_active->dev->name,
  693. new_active->dev->name);
  694. ret = -EINVAL;
  695. }
  696. }
  697. }
  698. unblock_netpoll_tx();
  699. return ret;
  700. }
  701. /* There are two tricky bits here. First, if MII monitoring is activated, then
  702. * we must disable ARP monitoring. Second, if the timer isn't running, we must
  703. * start it.
  704. */
  705. static int bond_option_miimon_set(struct bonding *bond,
  706. const struct bond_opt_value *newval)
  707. {
  708. netdev_info(bond->dev, "Setting MII monitoring interval to %llu\n",
  709. newval->value);
  710. bond->params.miimon = newval->value;
  711. if (bond->params.updelay)
  712. netdev_info(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
  713. bond->params.updelay * bond->params.miimon);
  714. if (bond->params.downdelay)
  715. netdev_info(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
  716. bond->params.downdelay * bond->params.miimon);
  717. if (newval->value && bond->params.arp_interval) {
  718. netdev_info(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
  719. bond->params.arp_interval = 0;
  720. if (bond->params.arp_validate)
  721. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  722. }
  723. if (bond->dev->flags & IFF_UP) {
  724. /* If the interface is up, we may need to fire off
  725. * the MII timer. If the interface is down, the
  726. * timer will get fired off when the open function
  727. * is called.
  728. */
  729. if (!newval->value) {
  730. cancel_delayed_work_sync(&bond->mii_work);
  731. } else {
  732. cancel_delayed_work_sync(&bond->arp_work);
  733. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  734. }
  735. }
  736. return 0;
  737. }
  738. /* Set up and down delays. These must be multiples of the
  739. * MII monitoring value, and are stored internally as the multiplier.
  740. * Thus, we must translate to MS for the real world.
  741. */
  742. static int bond_option_updelay_set(struct bonding *bond,
  743. const struct bond_opt_value *newval)
  744. {
  745. int value = newval->value;
  746. if (!bond->params.miimon) {
  747. netdev_err(bond->dev, "Unable to set up delay as MII monitoring is disabled\n");
  748. return -EPERM;
  749. }
  750. if ((value % bond->params.miimon) != 0) {
  751. netdev_warn(bond->dev, "up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  752. value, bond->params.miimon,
  753. (value / bond->params.miimon) *
  754. bond->params.miimon);
  755. }
  756. bond->params.updelay = value / bond->params.miimon;
  757. netdev_info(bond->dev, "Setting up delay to %d\n",
  758. bond->params.updelay * bond->params.miimon);
  759. return 0;
  760. }
  761. static int bond_option_downdelay_set(struct bonding *bond,
  762. const struct bond_opt_value *newval)
  763. {
  764. int value = newval->value;
  765. if (!bond->params.miimon) {
  766. netdev_err(bond->dev, "Unable to set down delay as MII monitoring is disabled\n");
  767. return -EPERM;
  768. }
  769. if ((value % bond->params.miimon) != 0) {
  770. netdev_warn(bond->dev, "down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
  771. value, bond->params.miimon,
  772. (value / bond->params.miimon) *
  773. bond->params.miimon);
  774. }
  775. bond->params.downdelay = value / bond->params.miimon;
  776. netdev_info(bond->dev, "Setting down delay to %d\n",
  777. bond->params.downdelay * bond->params.miimon);
  778. return 0;
  779. }
  780. static int bond_option_use_carrier_set(struct bonding *bond,
  781. const struct bond_opt_value *newval)
  782. {
  783. netdev_info(bond->dev, "Setting use_carrier to %llu\n",
  784. newval->value);
  785. bond->params.use_carrier = newval->value;
  786. return 0;
  787. }
  788. /* There are two tricky bits here. First, if ARP monitoring is activated, then
  789. * we must disable MII monitoring. Second, if the ARP timer isn't running,
  790. * we must start it.
  791. */
  792. static int bond_option_arp_interval_set(struct bonding *bond,
  793. const struct bond_opt_value *newval)
  794. {
  795. netdev_info(bond->dev, "Setting ARP monitoring interval to %llu\n",
  796. newval->value);
  797. bond->params.arp_interval = newval->value;
  798. if (newval->value) {
  799. if (bond->params.miimon) {
  800. netdev_info(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
  801. bond->params.miimon = 0;
  802. }
  803. if (!bond->params.arp_targets[0])
  804. netdev_info(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
  805. }
  806. if (bond->dev->flags & IFF_UP) {
  807. /* If the interface is up, we may need to fire off
  808. * the ARP timer. If the interface is down, the
  809. * timer will get fired off when the open function
  810. * is called.
  811. */
  812. if (!newval->value) {
  813. if (bond->params.arp_validate)
  814. bond->recv_probe = NULL;
  815. cancel_delayed_work_sync(&bond->arp_work);
  816. } else {
  817. /* arp_validate can be set only in active-backup mode */
  818. bond->recv_probe = bond_arp_rcv;
  819. cancel_delayed_work_sync(&bond->mii_work);
  820. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  821. }
  822. }
  823. return 0;
  824. }
  825. static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
  826. __be32 target,
  827. unsigned long last_rx)
  828. {
  829. __be32 *targets = bond->params.arp_targets;
  830. struct list_head *iter;
  831. struct slave *slave;
  832. if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
  833. bond_for_each_slave(bond, slave, iter)
  834. slave->target_last_arp_rx[slot] = last_rx;
  835. targets[slot] = target;
  836. }
  837. }
  838. static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  839. {
  840. __be32 *targets = bond->params.arp_targets;
  841. int ind;
  842. if (!bond_is_ip_target_ok(target)) {
  843. netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
  844. &target);
  845. return -EINVAL;
  846. }
  847. if (bond_get_targets_ip(targets, target) != -1) { /* dup */
  848. netdev_err(bond->dev, "ARP target %pI4 is already present\n",
  849. &target);
  850. return -EINVAL;
  851. }
  852. ind = bond_get_targets_ip(targets, 0); /* first free slot */
  853. if (ind == -1) {
  854. netdev_err(bond->dev, "ARP target table is full!\n");
  855. return -EINVAL;
  856. }
  857. netdev_info(bond->dev, "Adding ARP target %pI4\n", &target);
  858. _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
  859. return 0;
  860. }
  861. static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  862. {
  863. return _bond_option_arp_ip_target_add(bond, target);
  864. }
  865. static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
  866. {
  867. __be32 *targets = bond->params.arp_targets;
  868. struct list_head *iter;
  869. struct slave *slave;
  870. unsigned long *targets_rx;
  871. int ind, i;
  872. if (!bond_is_ip_target_ok(target)) {
  873. netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
  874. &target);
  875. return -EINVAL;
  876. }
  877. ind = bond_get_targets_ip(targets, target);
  878. if (ind == -1) {
  879. netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
  880. &target);
  881. return -EINVAL;
  882. }
  883. if (ind == 0 && !targets[1] && bond->params.arp_interval)
  884. netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
  885. netdev_info(bond->dev, "Removing ARP target %pI4\n", &target);
  886. bond_for_each_slave(bond, slave, iter) {
  887. targets_rx = slave->target_last_arp_rx;
  888. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  889. targets_rx[i] = targets_rx[i+1];
  890. targets_rx[i] = 0;
  891. }
  892. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  893. targets[i] = targets[i+1];
  894. targets[i] = 0;
  895. return 0;
  896. }
  897. void bond_option_arp_ip_targets_clear(struct bonding *bond)
  898. {
  899. int i;
  900. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  901. _bond_options_arp_ip_target_set(bond, i, 0, 0);
  902. }
  903. static int bond_option_arp_ip_targets_set(struct bonding *bond,
  904. const struct bond_opt_value *newval)
  905. {
  906. int ret = -EPERM;
  907. __be32 target;
  908. if (newval->string) {
  909. if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
  910. netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
  911. &target);
  912. return ret;
  913. }
  914. if (newval->string[0] == '+')
  915. ret = bond_option_arp_ip_target_add(bond, target);
  916. else if (newval->string[0] == '-')
  917. ret = bond_option_arp_ip_target_rem(bond, target);
  918. else
  919. netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
  920. } else {
  921. target = newval->value;
  922. ret = bond_option_arp_ip_target_add(bond, target);
  923. }
  924. return ret;
  925. }
  926. static int bond_option_arp_validate_set(struct bonding *bond,
  927. const struct bond_opt_value *newval)
  928. {
  929. netdev_info(bond->dev, "Setting arp_validate to %s (%llu)\n",
  930. newval->string, newval->value);
  931. if (bond->dev->flags & IFF_UP) {
  932. if (!newval->value)
  933. bond->recv_probe = NULL;
  934. else if (bond->params.arp_interval)
  935. bond->recv_probe = bond_arp_rcv;
  936. }
  937. bond->params.arp_validate = newval->value;
  938. return 0;
  939. }
  940. static int bond_option_arp_all_targets_set(struct bonding *bond,
  941. const struct bond_opt_value *newval)
  942. {
  943. netdev_info(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
  944. newval->string, newval->value);
  945. bond->params.arp_all_targets = newval->value;
  946. return 0;
  947. }
  948. static int bond_option_primary_set(struct bonding *bond,
  949. const struct bond_opt_value *newval)
  950. {
  951. char *p, *primary = newval->string;
  952. struct list_head *iter;
  953. struct slave *slave;
  954. block_netpoll_tx();
  955. p = strchr(primary, '\n');
  956. if (p)
  957. *p = '\0';
  958. /* check to see if we are clearing primary */
  959. if (!strlen(primary)) {
  960. netdev_info(bond->dev, "Setting primary slave to None\n");
  961. RCU_INIT_POINTER(bond->primary_slave, NULL);
  962. memset(bond->params.primary, 0, sizeof(bond->params.primary));
  963. bond_select_active_slave(bond);
  964. goto out;
  965. }
  966. bond_for_each_slave(bond, slave, iter) {
  967. if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
  968. netdev_info(bond->dev, "Setting %s as primary slave\n",
  969. slave->dev->name);
  970. rcu_assign_pointer(bond->primary_slave, slave);
  971. strcpy(bond->params.primary, slave->dev->name);
  972. bond_select_active_slave(bond);
  973. goto out;
  974. }
  975. }
  976. if (rtnl_dereference(bond->primary_slave)) {
  977. netdev_info(bond->dev, "Setting primary slave to None\n");
  978. RCU_INIT_POINTER(bond->primary_slave, NULL);
  979. bond_select_active_slave(bond);
  980. }
  981. strncpy(bond->params.primary, primary, IFNAMSIZ);
  982. bond->params.primary[IFNAMSIZ - 1] = 0;
  983. netdev_info(bond->dev, "Recording %s as primary, but it has not been enslaved to %s yet\n",
  984. primary, bond->dev->name);
  985. out:
  986. unblock_netpoll_tx();
  987. return 0;
  988. }
  989. static int bond_option_primary_reselect_set(struct bonding *bond,
  990. const struct bond_opt_value *newval)
  991. {
  992. netdev_info(bond->dev, "Setting primary_reselect to %s (%llu)\n",
  993. newval->string, newval->value);
  994. bond->params.primary_reselect = newval->value;
  995. block_netpoll_tx();
  996. bond_select_active_slave(bond);
  997. unblock_netpoll_tx();
  998. return 0;
  999. }
  1000. static int bond_option_fail_over_mac_set(struct bonding *bond,
  1001. const struct bond_opt_value *newval)
  1002. {
  1003. netdev_info(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
  1004. newval->string, newval->value);
  1005. bond->params.fail_over_mac = newval->value;
  1006. return 0;
  1007. }
  1008. static int bond_option_xmit_hash_policy_set(struct bonding *bond,
  1009. const struct bond_opt_value *newval)
  1010. {
  1011. netdev_info(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
  1012. newval->string, newval->value);
  1013. bond->params.xmit_policy = newval->value;
  1014. return 0;
  1015. }
  1016. static int bond_option_resend_igmp_set(struct bonding *bond,
  1017. const struct bond_opt_value *newval)
  1018. {
  1019. netdev_info(bond->dev, "Setting resend_igmp to %llu\n",
  1020. newval->value);
  1021. bond->params.resend_igmp = newval->value;
  1022. return 0;
  1023. }
  1024. static int bond_option_num_peer_notif_set(struct bonding *bond,
  1025. const struct bond_opt_value *newval)
  1026. {
  1027. bond->params.num_peer_notif = newval->value;
  1028. return 0;
  1029. }
  1030. static int bond_option_all_slaves_active_set(struct bonding *bond,
  1031. const struct bond_opt_value *newval)
  1032. {
  1033. struct list_head *iter;
  1034. struct slave *slave;
  1035. if (newval->value == bond->params.all_slaves_active)
  1036. return 0;
  1037. bond->params.all_slaves_active = newval->value;
  1038. bond_for_each_slave(bond, slave, iter) {
  1039. if (!bond_is_active_slave(slave)) {
  1040. if (newval->value)
  1041. slave->inactive = 0;
  1042. else
  1043. slave->inactive = 1;
  1044. }
  1045. }
  1046. return 0;
  1047. }
  1048. static int bond_option_min_links_set(struct bonding *bond,
  1049. const struct bond_opt_value *newval)
  1050. {
  1051. netdev_info(bond->dev, "Setting min links value to %llu\n",
  1052. newval->value);
  1053. bond->params.min_links = newval->value;
  1054. bond_set_carrier(bond);
  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. }