bond_options.c 39 KB

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