bond_options.c 49 KB

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