bond_options.c 41 KB

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