bond_options.c 38 KB

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