bond_options.c 41 KB

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