bond_options.c 41 KB

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