bond_options.c 39 KB

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