bond_options.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. .flags = BOND_OPTFLAG_IFDOWN,
  384. .values = bond_ad_actor_sys_prio_tbl,
  385. .set = bond_option_ad_actor_sys_prio_set,
  386. },
  387. [BOND_OPT_AD_ACTOR_SYSTEM] = {
  388. .id = BOND_OPT_AD_ACTOR_SYSTEM,
  389. .name = "ad_actor_system",
  390. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  391. .flags = BOND_OPTFLAG_RAWVAL | BOND_OPTFLAG_IFDOWN,
  392. .set = bond_option_ad_actor_system_set,
  393. },
  394. [BOND_OPT_AD_USER_PORT_KEY] = {
  395. .id = BOND_OPT_AD_USER_PORT_KEY,
  396. .name = "ad_user_port_key",
  397. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  398. .flags = BOND_OPTFLAG_IFDOWN,
  399. .values = bond_ad_user_port_key_tbl,
  400. .set = bond_option_ad_user_port_key_set,
  401. }
  402. };
  403. /* Searches for an option by name */
  404. const struct bond_option *bond_opt_get_by_name(const char *name)
  405. {
  406. const struct bond_option *opt;
  407. int option;
  408. for (option = 0; option < BOND_OPT_LAST; option++) {
  409. opt = bond_opt_get(option);
  410. if (opt && !strcmp(opt->name, name))
  411. return opt;
  412. }
  413. return NULL;
  414. }
  415. /* Searches for a value in opt's values[] table */
  416. const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
  417. {
  418. const struct bond_option *opt;
  419. int i;
  420. opt = bond_opt_get(option);
  421. if (WARN_ON(!opt))
  422. return NULL;
  423. for (i = 0; opt->values && opt->values[i].string; i++)
  424. if (opt->values[i].value == val)
  425. return &opt->values[i];
  426. return NULL;
  427. }
  428. /* Searches for a value in opt's values[] table which matches the flagmask */
  429. static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
  430. u32 flagmask)
  431. {
  432. int i;
  433. for (i = 0; opt->values && opt->values[i].string; i++)
  434. if (opt->values[i].flags & flagmask)
  435. return &opt->values[i];
  436. return NULL;
  437. }
  438. /* If maxval is missing then there's no range to check. In case minval is
  439. * missing then it's considered to be 0.
  440. */
  441. static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
  442. {
  443. const struct bond_opt_value *minval, *maxval;
  444. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  445. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  446. if (!maxval || (minval && val < minval->value) || val > maxval->value)
  447. return false;
  448. return true;
  449. }
  450. /**
  451. * bond_opt_parse - parse option value
  452. * @opt: the option to parse against
  453. * @val: value to parse
  454. *
  455. * This function tries to extract the value from @val and check if it's
  456. * a possible match for the option and returns NULL if a match isn't found,
  457. * or the struct_opt_value that matched. It also strips the new line from
  458. * @val->string if it's present.
  459. */
  460. const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
  461. struct bond_opt_value *val)
  462. {
  463. char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
  464. const struct bond_opt_value *tbl;
  465. const struct bond_opt_value *ret = NULL;
  466. bool checkval;
  467. int i, rv;
  468. /* No parsing if the option wants a raw val */
  469. if (opt->flags & BOND_OPTFLAG_RAWVAL)
  470. return val;
  471. tbl = opt->values;
  472. if (!tbl)
  473. goto out;
  474. /* ULLONG_MAX is used to bypass string processing */
  475. checkval = val->value != ULLONG_MAX;
  476. if (!checkval) {
  477. if (!val->string)
  478. goto out;
  479. p = strchr(val->string, '\n');
  480. if (p)
  481. *p = '\0';
  482. for (p = val->string; *p; p++)
  483. if (!(isdigit(*p) || isspace(*p)))
  484. break;
  485. /* The following code extracts the string to match or the value
  486. * and sets checkval appropriately
  487. */
  488. if (*p) {
  489. rv = sscanf(val->string, "%32s", valstr);
  490. } else {
  491. rv = sscanf(val->string, "%llu", &val->value);
  492. checkval = true;
  493. }
  494. if (!rv)
  495. goto out;
  496. }
  497. for (i = 0; tbl[i].string; i++) {
  498. /* Check for exact match */
  499. if (checkval) {
  500. if (val->value == tbl[i].value)
  501. ret = &tbl[i];
  502. } else {
  503. if (!strcmp(valstr, "default") &&
  504. (tbl[i].flags & BOND_VALFLAG_DEFAULT))
  505. ret = &tbl[i];
  506. if (!strcmp(valstr, tbl[i].string))
  507. ret = &tbl[i];
  508. }
  509. /* Found an exact match */
  510. if (ret)
  511. goto out;
  512. }
  513. /* Possible range match */
  514. if (checkval && bond_opt_check_range(opt, val->value))
  515. ret = val;
  516. out:
  517. return ret;
  518. }
  519. /* Check opt's dependencies against bond mode and currently set options */
  520. static int bond_opt_check_deps(struct bonding *bond,
  521. const struct bond_option *opt)
  522. {
  523. struct bond_params *params = &bond->params;
  524. if (test_bit(params->mode, &opt->unsuppmodes))
  525. return -EACCES;
  526. if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
  527. return -ENOTEMPTY;
  528. if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
  529. return -EBUSY;
  530. return 0;
  531. }
  532. static void bond_opt_dep_print(struct bonding *bond,
  533. const struct bond_option *opt)
  534. {
  535. const struct bond_opt_value *modeval;
  536. struct bond_params *params;
  537. params = &bond->params;
  538. modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
  539. if (test_bit(params->mode, &opt->unsuppmodes))
  540. netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
  541. opt->name, modeval->string, modeval->value);
  542. }
  543. static void bond_opt_error_interpret(struct bonding *bond,
  544. const struct bond_option *opt,
  545. int error, const struct bond_opt_value *val)
  546. {
  547. const struct bond_opt_value *minval, *maxval;
  548. char *p;
  549. switch (error) {
  550. case -EINVAL:
  551. if (val) {
  552. if (val->string) {
  553. /* sometimes RAWVAL opts may have new lines */
  554. p = strchr(val->string, '\n');
  555. if (p)
  556. *p = '\0';
  557. netdev_err(bond->dev, "option %s: invalid value (%s)\n",
  558. opt->name, val->string);
  559. } else {
  560. netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
  561. opt->name, val->value);
  562. }
  563. }
  564. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  565. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  566. if (!maxval)
  567. break;
  568. netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
  569. opt->name, minval ? minval->value : 0, maxval->value);
  570. break;
  571. case -EACCES:
  572. bond_opt_dep_print(bond, opt);
  573. break;
  574. case -ENOTEMPTY:
  575. netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
  576. opt->name);
  577. break;
  578. case -EBUSY:
  579. netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
  580. opt->name);
  581. break;
  582. default:
  583. break;
  584. }
  585. }
  586. /**
  587. * __bond_opt_set - set a bonding option
  588. * @bond: target bond device
  589. * @option: option to set
  590. * @val: value to set it to
  591. *
  592. * This function is used to change the bond's option value, it can be
  593. * used for both enabling/changing an option and for disabling it. RTNL lock
  594. * must be obtained before calling this function.
  595. */
  596. int __bond_opt_set(struct bonding *bond,
  597. unsigned int option, struct bond_opt_value *val)
  598. {
  599. const struct bond_opt_value *retval = NULL;
  600. const struct bond_option *opt;
  601. int ret = -ENOENT;
  602. ASSERT_RTNL();
  603. opt = bond_opt_get(option);
  604. if (WARN_ON(!val) || WARN_ON(!opt))
  605. goto out;
  606. ret = bond_opt_check_deps(bond, opt);
  607. if (ret)
  608. goto out;
  609. retval = bond_opt_parse(opt, val);
  610. if (!retval) {
  611. ret = -EINVAL;
  612. goto out;
  613. }
  614. ret = opt->set(bond, retval);
  615. out:
  616. if (ret)
  617. bond_opt_error_interpret(bond, opt, ret, val);
  618. else if (bond->dev->reg_state == NETREG_REGISTERED)
  619. call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
  620. return ret;
  621. }
  622. /**
  623. * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
  624. * @bond: target bond device
  625. * @option: option to set
  626. * @buf: value to set it to
  627. *
  628. * This function tries to acquire RTNL without blocking and if successful
  629. * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
  630. */
  631. int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
  632. {
  633. struct bond_opt_value optval;
  634. int ret;
  635. if (!rtnl_trylock())
  636. return restart_syscall();
  637. bond_opt_initstr(&optval, buf);
  638. ret = __bond_opt_set(bond, option, &optval);
  639. rtnl_unlock();
  640. return ret;
  641. }
  642. /**
  643. * bond_opt_get - get a pointer to an option
  644. * @option: option for which to return a pointer
  645. *
  646. * This function checks if option is valid and if so returns a pointer
  647. * to its entry in the bond_opts[] option array.
  648. */
  649. const struct bond_option *bond_opt_get(unsigned int option)
  650. {
  651. if (!BOND_OPT_VALID(option))
  652. return NULL;
  653. return &bond_opts[option];
  654. }
  655. static int bond_option_mode_set(struct bonding *bond,
  656. const struct bond_opt_value *newval)
  657. {
  658. if (!bond_mode_uses_arp(newval->value) && bond->params.arp_interval) {
  659. netdev_info(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
  660. newval->string);
  661. /* disable arp monitoring */
  662. bond->params.arp_interval = 0;
  663. /* set miimon to default value */
  664. bond->params.miimon = BOND_DEFAULT_MIIMON;
  665. netdev_info(bond->dev, "Setting MII monitoring interval to %d\n",
  666. bond->params.miimon);
  667. }
  668. /* don't cache arp_validate between modes */
  669. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  670. bond->params.mode = newval->value;
  671. return 0;
  672. }
  673. static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
  674. struct slave *slave)
  675. {
  676. return bond_uses_primary(bond) && slave ? slave->dev : NULL;
  677. }
  678. struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
  679. {
  680. struct slave *slave = rcu_dereference(bond->curr_active_slave);
  681. return __bond_option_active_slave_get(bond, slave);
  682. }
  683. static int bond_option_active_slave_set(struct bonding *bond,
  684. const struct bond_opt_value *newval)
  685. {
  686. char ifname[IFNAMSIZ] = { 0, };
  687. struct net_device *slave_dev;
  688. int ret = 0;
  689. sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
  690. if (!strlen(ifname) || newval->string[0] == '\n') {
  691. slave_dev = NULL;
  692. } else {
  693. slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  694. if (!slave_dev)
  695. return -ENODEV;
  696. }
  697. if (slave_dev) {
  698. if (!netif_is_bond_slave(slave_dev)) {
  699. netdev_err(bond->dev, "Device %s is not bonding slave\n",
  700. slave_dev->name);
  701. return -EINVAL;
  702. }
  703. if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
  704. netdev_err(bond->dev, "Device %s is not our slave\n",
  705. slave_dev->name);
  706. return -EINVAL;
  707. }
  708. }
  709. block_netpoll_tx();
  710. /* check to see if we are clearing active */
  711. if (!slave_dev) {
  712. netdev_info(bond->dev, "Clearing current active slave\n");
  713. RCU_INIT_POINTER(bond->curr_active_slave, NULL);
  714. bond_select_active_slave(bond);
  715. } else {
  716. struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
  717. struct slave *new_active = bond_slave_get_rtnl(slave_dev);
  718. BUG_ON(!new_active);
  719. if (new_active == old_active) {
  720. /* do nothing */
  721. netdev_info(bond->dev, "%s is already the current active slave\n",
  722. new_active->dev->name);
  723. } else {
  724. if (old_active && (new_active->link == BOND_LINK_UP) &&
  725. bond_slave_is_up(new_active)) {
  726. netdev_info(bond->dev, "Setting %s as active slave\n",
  727. new_active->dev->name);
  728. bond_change_active_slave(bond, new_active);
  729. } else {
  730. netdev_err(bond->dev, "Could not set %s as active slave; either %s is down or the link is down\n",
  731. new_active->dev->name,
  732. new_active->dev->name);
  733. ret = -EINVAL;
  734. }
  735. }
  736. }
  737. unblock_netpoll_tx();
  738. return ret;
  739. }
  740. /* There are two tricky bits here. First, if MII monitoring is activated, then
  741. * we must disable ARP monitoring. Second, if the timer isn't running, we must
  742. * start it.
  743. */
  744. static int bond_option_miimon_set(struct bonding *bond,
  745. const struct bond_opt_value *newval)
  746. {
  747. netdev_info(bond->dev, "Setting MII monitoring interval to %llu\n",
  748. newval->value);
  749. bond->params.miimon = newval->value;
  750. if (bond->params.updelay)
  751. netdev_info(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
  752. bond->params.updelay * bond->params.miimon);
  753. if (bond->params.downdelay)
  754. netdev_info(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
  755. bond->params.downdelay * bond->params.miimon);
  756. if (newval->value && bond->params.arp_interval) {
  757. netdev_info(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
  758. bond->params.arp_interval = 0;
  759. if (bond->params.arp_validate)
  760. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  761. }
  762. if (bond->dev->flags & IFF_UP) {
  763. /* If the interface is up, we may need to fire off
  764. * the MII timer. If the interface is down, the
  765. * timer will get fired off when the open function
  766. * is called.
  767. */
  768. if (!newval->value) {
  769. cancel_delayed_work_sync(&bond->mii_work);
  770. } else {
  771. cancel_delayed_work_sync(&bond->arp_work);
  772. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  773. }
  774. }
  775. return 0;
  776. }
  777. /* Set up and down delays. These must be multiples of the
  778. * MII monitoring value, and are stored internally as the multiplier.
  779. * Thus, we must translate to MS for the real world.
  780. */
  781. static int bond_option_updelay_set(struct bonding *bond,
  782. const struct bond_opt_value *newval)
  783. {
  784. int value = newval->value;
  785. if (!bond->params.miimon) {
  786. netdev_err(bond->dev, "Unable to set up delay as MII monitoring is disabled\n");
  787. return -EPERM;
  788. }
  789. if ((value % bond->params.miimon) != 0) {
  790. netdev_warn(bond->dev, "up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
  791. value, bond->params.miimon,
  792. (value / bond->params.miimon) *
  793. bond->params.miimon);
  794. }
  795. bond->params.updelay = value / bond->params.miimon;
  796. netdev_info(bond->dev, "Setting up delay to %d\n",
  797. bond->params.updelay * bond->params.miimon);
  798. return 0;
  799. }
  800. static int bond_option_downdelay_set(struct bonding *bond,
  801. const struct bond_opt_value *newval)
  802. {
  803. int value = newval->value;
  804. if (!bond->params.miimon) {
  805. netdev_err(bond->dev, "Unable to set down delay as MII monitoring is disabled\n");
  806. return -EPERM;
  807. }
  808. if ((value % bond->params.miimon) != 0) {
  809. netdev_warn(bond->dev, "down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
  810. value, bond->params.miimon,
  811. (value / bond->params.miimon) *
  812. bond->params.miimon);
  813. }
  814. bond->params.downdelay = value / bond->params.miimon;
  815. netdev_info(bond->dev, "Setting down delay to %d\n",
  816. bond->params.downdelay * bond->params.miimon);
  817. return 0;
  818. }
  819. static int bond_option_use_carrier_set(struct bonding *bond,
  820. const struct bond_opt_value *newval)
  821. {
  822. netdev_info(bond->dev, "Setting use_carrier to %llu\n",
  823. newval->value);
  824. bond->params.use_carrier = newval->value;
  825. return 0;
  826. }
  827. /* There are two tricky bits here. First, if ARP monitoring is activated, then
  828. * we must disable MII monitoring. Second, if the ARP timer isn't running,
  829. * we must start it.
  830. */
  831. static int bond_option_arp_interval_set(struct bonding *bond,
  832. const struct bond_opt_value *newval)
  833. {
  834. netdev_info(bond->dev, "Setting ARP monitoring interval to %llu\n",
  835. newval->value);
  836. bond->params.arp_interval = newval->value;
  837. if (newval->value) {
  838. if (bond->params.miimon) {
  839. netdev_info(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
  840. bond->params.miimon = 0;
  841. }
  842. if (!bond->params.arp_targets[0])
  843. netdev_info(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
  844. }
  845. if (bond->dev->flags & IFF_UP) {
  846. /* If the interface is up, we may need to fire off
  847. * the ARP timer. If the interface is down, the
  848. * timer will get fired off when the open function
  849. * is called.
  850. */
  851. if (!newval->value) {
  852. if (bond->params.arp_validate)
  853. bond->recv_probe = NULL;
  854. cancel_delayed_work_sync(&bond->arp_work);
  855. } else {
  856. /* arp_validate can be set only in active-backup mode */
  857. bond->recv_probe = bond_arp_rcv;
  858. cancel_delayed_work_sync(&bond->mii_work);
  859. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  860. }
  861. }
  862. return 0;
  863. }
  864. static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
  865. __be32 target,
  866. unsigned long last_rx)
  867. {
  868. __be32 *targets = bond->params.arp_targets;
  869. struct list_head *iter;
  870. struct slave *slave;
  871. if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
  872. bond_for_each_slave(bond, slave, iter)
  873. slave->target_last_arp_rx[slot] = last_rx;
  874. targets[slot] = target;
  875. }
  876. }
  877. static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  878. {
  879. __be32 *targets = bond->params.arp_targets;
  880. int ind;
  881. if (!bond_is_ip_target_ok(target)) {
  882. netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
  883. &target);
  884. return -EINVAL;
  885. }
  886. if (bond_get_targets_ip(targets, target) != -1) { /* dup */
  887. netdev_err(bond->dev, "ARP target %pI4 is already present\n",
  888. &target);
  889. return -EINVAL;
  890. }
  891. ind = bond_get_targets_ip(targets, 0); /* first free slot */
  892. if (ind == -1) {
  893. netdev_err(bond->dev, "ARP target table is full!\n");
  894. return -EINVAL;
  895. }
  896. netdev_info(bond->dev, "Adding ARP target %pI4\n", &target);
  897. _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
  898. return 0;
  899. }
  900. static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  901. {
  902. return _bond_option_arp_ip_target_add(bond, target);
  903. }
  904. static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
  905. {
  906. __be32 *targets = bond->params.arp_targets;
  907. struct list_head *iter;
  908. struct slave *slave;
  909. unsigned long *targets_rx;
  910. int ind, i;
  911. if (!bond_is_ip_target_ok(target)) {
  912. netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
  913. &target);
  914. return -EINVAL;
  915. }
  916. ind = bond_get_targets_ip(targets, target);
  917. if (ind == -1) {
  918. netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
  919. &target);
  920. return -EINVAL;
  921. }
  922. if (ind == 0 && !targets[1] && bond->params.arp_interval)
  923. netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
  924. netdev_info(bond->dev, "Removing ARP target %pI4\n", &target);
  925. bond_for_each_slave(bond, slave, iter) {
  926. targets_rx = slave->target_last_arp_rx;
  927. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  928. targets_rx[i] = targets_rx[i+1];
  929. targets_rx[i] = 0;
  930. }
  931. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  932. targets[i] = targets[i+1];
  933. targets[i] = 0;
  934. return 0;
  935. }
  936. void bond_option_arp_ip_targets_clear(struct bonding *bond)
  937. {
  938. int i;
  939. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  940. _bond_options_arp_ip_target_set(bond, i, 0, 0);
  941. }
  942. static int bond_option_arp_ip_targets_set(struct bonding *bond,
  943. const struct bond_opt_value *newval)
  944. {
  945. int ret = -EPERM;
  946. __be32 target;
  947. if (newval->string) {
  948. if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
  949. netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
  950. &target);
  951. return ret;
  952. }
  953. if (newval->string[0] == '+')
  954. ret = bond_option_arp_ip_target_add(bond, target);
  955. else if (newval->string[0] == '-')
  956. ret = bond_option_arp_ip_target_rem(bond, target);
  957. else
  958. netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
  959. } else {
  960. target = newval->value;
  961. ret = bond_option_arp_ip_target_add(bond, target);
  962. }
  963. return ret;
  964. }
  965. static int bond_option_arp_validate_set(struct bonding *bond,
  966. const struct bond_opt_value *newval)
  967. {
  968. netdev_info(bond->dev, "Setting arp_validate to %s (%llu)\n",
  969. newval->string, newval->value);
  970. if (bond->dev->flags & IFF_UP) {
  971. if (!newval->value)
  972. bond->recv_probe = NULL;
  973. else if (bond->params.arp_interval)
  974. bond->recv_probe = bond_arp_rcv;
  975. }
  976. bond->params.arp_validate = newval->value;
  977. return 0;
  978. }
  979. static int bond_option_arp_all_targets_set(struct bonding *bond,
  980. const struct bond_opt_value *newval)
  981. {
  982. netdev_info(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
  983. newval->string, newval->value);
  984. bond->params.arp_all_targets = newval->value;
  985. return 0;
  986. }
  987. static int bond_option_primary_set(struct bonding *bond,
  988. const struct bond_opt_value *newval)
  989. {
  990. char *p, *primary = newval->string;
  991. struct list_head *iter;
  992. struct slave *slave;
  993. block_netpoll_tx();
  994. p = strchr(primary, '\n');
  995. if (p)
  996. *p = '\0';
  997. /* check to see if we are clearing primary */
  998. if (!strlen(primary)) {
  999. netdev_info(bond->dev, "Setting primary slave to None\n");
  1000. RCU_INIT_POINTER(bond->primary_slave, NULL);
  1001. memset(bond->params.primary, 0, sizeof(bond->params.primary));
  1002. bond_select_active_slave(bond);
  1003. goto out;
  1004. }
  1005. bond_for_each_slave(bond, slave, iter) {
  1006. if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
  1007. netdev_info(bond->dev, "Setting %s as primary slave\n",
  1008. slave->dev->name);
  1009. rcu_assign_pointer(bond->primary_slave, slave);
  1010. strcpy(bond->params.primary, slave->dev->name);
  1011. bond_select_active_slave(bond);
  1012. goto out;
  1013. }
  1014. }
  1015. if (rtnl_dereference(bond->primary_slave)) {
  1016. netdev_info(bond->dev, "Setting primary slave to None\n");
  1017. RCU_INIT_POINTER(bond->primary_slave, NULL);
  1018. bond_select_active_slave(bond);
  1019. }
  1020. strncpy(bond->params.primary, primary, IFNAMSIZ);
  1021. bond->params.primary[IFNAMSIZ - 1] = 0;
  1022. netdev_info(bond->dev, "Recording %s as primary, but it has not been enslaved to %s yet\n",
  1023. primary, bond->dev->name);
  1024. out:
  1025. unblock_netpoll_tx();
  1026. return 0;
  1027. }
  1028. static int bond_option_primary_reselect_set(struct bonding *bond,
  1029. const struct bond_opt_value *newval)
  1030. {
  1031. netdev_info(bond->dev, "Setting primary_reselect to %s (%llu)\n",
  1032. newval->string, newval->value);
  1033. bond->params.primary_reselect = newval->value;
  1034. block_netpoll_tx();
  1035. bond_select_active_slave(bond);
  1036. unblock_netpoll_tx();
  1037. return 0;
  1038. }
  1039. static int bond_option_fail_over_mac_set(struct bonding *bond,
  1040. const struct bond_opt_value *newval)
  1041. {
  1042. netdev_info(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
  1043. newval->string, newval->value);
  1044. bond->params.fail_over_mac = newval->value;
  1045. return 0;
  1046. }
  1047. static int bond_option_xmit_hash_policy_set(struct bonding *bond,
  1048. const struct bond_opt_value *newval)
  1049. {
  1050. netdev_info(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
  1051. newval->string, newval->value);
  1052. bond->params.xmit_policy = newval->value;
  1053. return 0;
  1054. }
  1055. static int bond_option_resend_igmp_set(struct bonding *bond,
  1056. const struct bond_opt_value *newval)
  1057. {
  1058. netdev_info(bond->dev, "Setting resend_igmp to %llu\n",
  1059. newval->value);
  1060. bond->params.resend_igmp = newval->value;
  1061. return 0;
  1062. }
  1063. static int bond_option_num_peer_notif_set(struct bonding *bond,
  1064. const struct bond_opt_value *newval)
  1065. {
  1066. bond->params.num_peer_notif = newval->value;
  1067. return 0;
  1068. }
  1069. static int bond_option_all_slaves_active_set(struct bonding *bond,
  1070. const struct bond_opt_value *newval)
  1071. {
  1072. struct list_head *iter;
  1073. struct slave *slave;
  1074. if (newval->value == bond->params.all_slaves_active)
  1075. return 0;
  1076. bond->params.all_slaves_active = newval->value;
  1077. bond_for_each_slave(bond, slave, iter) {
  1078. if (!bond_is_active_slave(slave)) {
  1079. if (newval->value)
  1080. slave->inactive = 0;
  1081. else
  1082. slave->inactive = 1;
  1083. }
  1084. }
  1085. return 0;
  1086. }
  1087. static int bond_option_min_links_set(struct bonding *bond,
  1088. const struct bond_opt_value *newval)
  1089. {
  1090. netdev_info(bond->dev, "Setting min links value to %llu\n",
  1091. newval->value);
  1092. bond->params.min_links = newval->value;
  1093. bond_set_carrier(bond);
  1094. return 0;
  1095. }
  1096. static int bond_option_lp_interval_set(struct bonding *bond,
  1097. const struct bond_opt_value *newval)
  1098. {
  1099. bond->params.lp_interval = newval->value;
  1100. return 0;
  1101. }
  1102. static int bond_option_pps_set(struct bonding *bond,
  1103. const struct bond_opt_value *newval)
  1104. {
  1105. bond->params.packets_per_slave = newval->value;
  1106. if (newval->value > 0) {
  1107. bond->params.reciprocal_packets_per_slave =
  1108. reciprocal_value(newval->value);
  1109. } else {
  1110. /* reciprocal_packets_per_slave is unused if
  1111. * packets_per_slave is 0 or 1, just initialize it
  1112. */
  1113. bond->params.reciprocal_packets_per_slave =
  1114. (struct reciprocal_value) { 0 };
  1115. }
  1116. return 0;
  1117. }
  1118. static int bond_option_lacp_rate_set(struct bonding *bond,
  1119. const struct bond_opt_value *newval)
  1120. {
  1121. netdev_info(bond->dev, "Setting LACP rate to %s (%llu)\n",
  1122. newval->string, newval->value);
  1123. bond->params.lacp_fast = newval->value;
  1124. bond_3ad_update_lacp_rate(bond);
  1125. return 0;
  1126. }
  1127. static int bond_option_ad_select_set(struct bonding *bond,
  1128. const struct bond_opt_value *newval)
  1129. {
  1130. netdev_info(bond->dev, "Setting ad_select to %s (%llu)\n",
  1131. newval->string, newval->value);
  1132. bond->params.ad_select = newval->value;
  1133. return 0;
  1134. }
  1135. static int bond_option_queue_id_set(struct bonding *bond,
  1136. const struct bond_opt_value *newval)
  1137. {
  1138. struct slave *slave, *update_slave;
  1139. struct net_device *sdev;
  1140. struct list_head *iter;
  1141. char *delim;
  1142. int ret = 0;
  1143. u16 qid;
  1144. /* delim will point to queue id if successful */
  1145. delim = strchr(newval->string, ':');
  1146. if (!delim)
  1147. goto err_no_cmd;
  1148. /* Terminate string that points to device name and bump it
  1149. * up one, so we can read the queue id there.
  1150. */
  1151. *delim = '\0';
  1152. if (sscanf(++delim, "%hd\n", &qid) != 1)
  1153. goto err_no_cmd;
  1154. /* Check buffer length, valid ifname and queue id */
  1155. if (!dev_valid_name(newval->string) ||
  1156. qid > bond->dev->real_num_tx_queues)
  1157. goto err_no_cmd;
  1158. /* Get the pointer to that interface if it exists */
  1159. sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
  1160. if (!sdev)
  1161. goto err_no_cmd;
  1162. /* Search for thes slave and check for duplicate qids */
  1163. update_slave = NULL;
  1164. bond_for_each_slave(bond, slave, iter) {
  1165. if (sdev == slave->dev)
  1166. /* We don't need to check the matching
  1167. * slave for dups, since we're overwriting it
  1168. */
  1169. update_slave = slave;
  1170. else if (qid && qid == slave->queue_id) {
  1171. goto err_no_cmd;
  1172. }
  1173. }
  1174. if (!update_slave)
  1175. goto err_no_cmd;
  1176. /* Actually set the qids for the slave */
  1177. update_slave->queue_id = qid;
  1178. out:
  1179. return ret;
  1180. err_no_cmd:
  1181. netdev_info(bond->dev, "invalid input for queue_id set\n");
  1182. ret = -EPERM;
  1183. goto out;
  1184. }
  1185. static int bond_option_slaves_set(struct bonding *bond,
  1186. const struct bond_opt_value *newval)
  1187. {
  1188. char command[IFNAMSIZ + 1] = { 0, };
  1189. struct net_device *dev;
  1190. char *ifname;
  1191. int ret;
  1192. sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
  1193. ifname = command + 1;
  1194. if ((strlen(command) <= 1) ||
  1195. !dev_valid_name(ifname))
  1196. goto err_no_cmd;
  1197. dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  1198. if (!dev) {
  1199. netdev_info(bond->dev, "interface %s does not exist!\n",
  1200. ifname);
  1201. ret = -ENODEV;
  1202. goto out;
  1203. }
  1204. switch (command[0]) {
  1205. case '+':
  1206. netdev_info(bond->dev, "Adding slave %s\n", dev->name);
  1207. ret = bond_enslave(bond->dev, dev);
  1208. break;
  1209. case '-':
  1210. netdev_info(bond->dev, "Removing slave %s\n", dev->name);
  1211. ret = bond_release(bond->dev, dev);
  1212. break;
  1213. default:
  1214. goto err_no_cmd;
  1215. }
  1216. out:
  1217. return ret;
  1218. err_no_cmd:
  1219. netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
  1220. ret = -EPERM;
  1221. goto out;
  1222. }
  1223. static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
  1224. const struct bond_opt_value *newval)
  1225. {
  1226. netdev_info(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
  1227. newval->string, newval->value);
  1228. bond->params.tlb_dynamic_lb = newval->value;
  1229. return 0;
  1230. }
  1231. static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
  1232. const struct bond_opt_value *newval)
  1233. {
  1234. netdev_info(bond->dev, "Setting ad_actor_sys_prio to %llu\n",
  1235. newval->value);
  1236. bond->params.ad_actor_sys_prio = newval->value;
  1237. return 0;
  1238. }
  1239. static int bond_option_ad_actor_system_set(struct bonding *bond,
  1240. const struct bond_opt_value *newval)
  1241. {
  1242. u8 macaddr[ETH_ALEN];
  1243. u8 *mac;
  1244. int i;
  1245. if (newval->string) {
  1246. i = sscanf(newval->string, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
  1247. &macaddr[0], &macaddr[1], &macaddr[2],
  1248. &macaddr[3], &macaddr[4], &macaddr[5]);
  1249. if (i != ETH_ALEN)
  1250. goto err;
  1251. mac = macaddr;
  1252. } else {
  1253. mac = (u8 *)&newval->value;
  1254. }
  1255. if (!is_valid_ether_addr(mac))
  1256. goto err;
  1257. netdev_info(bond->dev, "Setting ad_actor_system to %pM\n", mac);
  1258. ether_addr_copy(bond->params.ad_actor_system, mac);
  1259. return 0;
  1260. err:
  1261. netdev_err(bond->dev, "Invalid MAC address.\n");
  1262. return -EINVAL;
  1263. }
  1264. static int bond_option_ad_user_port_key_set(struct bonding *bond,
  1265. const struct bond_opt_value *newval)
  1266. {
  1267. netdev_info(bond->dev, "Setting ad_user_port_key to %llu\n",
  1268. newval->value);
  1269. bond->params.ad_user_port_key = newval->value;
  1270. return 0;
  1271. }