bond_options.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/net/bond/bond_options.c - bonding options
  4. * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
  5. * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/hex.h>
  9. #include <linux/if.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/rcupdate.h>
  13. #include <linux/ctype.h>
  14. #include <linux/inet.h>
  15. #include <linux/sched/signal.h>
  16. #include <net/bonding.h>
  17. #include <net/ndisc.h>
  18. static int bond_option_active_slave_set(struct bonding *bond,
  19. const struct bond_opt_value *newval);
  20. static int bond_option_miimon_set(struct bonding *bond,
  21. const struct bond_opt_value *newval);
  22. static int bond_option_updelay_set(struct bonding *bond,
  23. const struct bond_opt_value *newval);
  24. static int bond_option_downdelay_set(struct bonding *bond,
  25. const struct bond_opt_value *newval);
  26. static int bond_option_peer_notif_delay_set(struct bonding *bond,
  27. const struct bond_opt_value *newval);
  28. static int bond_option_use_carrier_set(struct bonding *bond,
  29. const struct bond_opt_value *newval);
  30. static int bond_option_arp_interval_set(struct bonding *bond,
  31. const struct bond_opt_value *newval);
  32. static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
  33. static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
  34. static int bond_option_arp_ip_targets_set(struct bonding *bond,
  35. const struct bond_opt_value *newval);
  36. static int bond_option_ns_ip6_targets_set(struct bonding *bond,
  37. const struct bond_opt_value *newval);
  38. static int bond_option_arp_validate_set(struct bonding *bond,
  39. const struct bond_opt_value *newval);
  40. static int bond_option_arp_all_targets_set(struct bonding *bond,
  41. const struct bond_opt_value *newval);
  42. static int bond_option_prio_set(struct bonding *bond,
  43. const struct bond_opt_value *newval);
  44. static int bond_option_primary_set(struct bonding *bond,
  45. const struct bond_opt_value *newval);
  46. static int bond_option_primary_reselect_set(struct bonding *bond,
  47. const struct bond_opt_value *newval);
  48. static int bond_option_fail_over_mac_set(struct bonding *bond,
  49. const struct bond_opt_value *newval);
  50. static int bond_option_xmit_hash_policy_set(struct bonding *bond,
  51. const struct bond_opt_value *newval);
  52. static int bond_option_resend_igmp_set(struct bonding *bond,
  53. const struct bond_opt_value *newval);
  54. static int bond_option_num_peer_notif_set(struct bonding *bond,
  55. const struct bond_opt_value *newval);
  56. static int bond_option_all_slaves_active_set(struct bonding *bond,
  57. const struct bond_opt_value *newval);
  58. static int bond_option_min_links_set(struct bonding *bond,
  59. const struct bond_opt_value *newval);
  60. static int bond_option_lp_interval_set(struct bonding *bond,
  61. const struct bond_opt_value *newval);
  62. static int bond_option_pps_set(struct bonding *bond,
  63. const struct bond_opt_value *newval);
  64. static int bond_option_lacp_active_set(struct bonding *bond,
  65. const struct bond_opt_value *newval);
  66. static int bond_option_lacp_rate_set(struct bonding *bond,
  67. const struct bond_opt_value *newval);
  68. static int bond_option_ad_select_set(struct bonding *bond,
  69. const struct bond_opt_value *newval);
  70. static int bond_option_queue_id_set(struct bonding *bond,
  71. const struct bond_opt_value *newval);
  72. static int bond_option_mode_set(struct bonding *bond,
  73. const struct bond_opt_value *newval);
  74. static int bond_option_slaves_set(struct bonding *bond,
  75. const struct bond_opt_value *newval);
  76. static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
  77. const struct bond_opt_value *newval);
  78. static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
  79. const struct bond_opt_value *newval);
  80. static int bond_option_actor_port_prio_set(struct bonding *bond,
  81. const struct bond_opt_value *newval);
  82. static int bond_option_ad_actor_system_set(struct bonding *bond,
  83. const struct bond_opt_value *newval);
  84. static int bond_option_ad_user_port_key_set(struct bonding *bond,
  85. const struct bond_opt_value *newval);
  86. static int bond_option_missed_max_set(struct bonding *bond,
  87. const struct bond_opt_value *newval);
  88. static int bond_option_coupled_control_set(struct bonding *bond,
  89. const struct bond_opt_value *newval);
  90. static int bond_option_broadcast_neigh_set(struct bonding *bond,
  91. const struct bond_opt_value *newval);
  92. static const struct bond_opt_value bond_mode_tbl[] = {
  93. { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT},
  94. { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
  95. { "balance-xor", BOND_MODE_XOR, 0},
  96. { "broadcast", BOND_MODE_BROADCAST, 0},
  97. { "802.3ad", BOND_MODE_8023AD, 0},
  98. { "balance-tlb", BOND_MODE_TLB, 0},
  99. { "balance-alb", BOND_MODE_ALB, 0},
  100. { NULL, -1, 0},
  101. };
  102. static const struct bond_opt_value bond_pps_tbl[] = {
  103. { "default", 1, BOND_VALFLAG_DEFAULT},
  104. { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
  105. { NULL, -1, 0},
  106. };
  107. static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
  108. { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
  109. { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
  110. { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
  111. { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
  112. { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
  113. { "vlan+srcmac", BOND_XMIT_POLICY_VLAN_SRCMAC, 0},
  114. { NULL, -1, 0},
  115. };
  116. static const struct bond_opt_value bond_arp_validate_tbl[] = {
  117. { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT},
  118. { "active", BOND_ARP_VALIDATE_ACTIVE, 0},
  119. { "backup", BOND_ARP_VALIDATE_BACKUP, 0},
  120. { "all", BOND_ARP_VALIDATE_ALL, 0},
  121. { "filter", BOND_ARP_FILTER, 0},
  122. { "filter_active", BOND_ARP_FILTER_ACTIVE, 0},
  123. { "filter_backup", BOND_ARP_FILTER_BACKUP, 0},
  124. { NULL, -1, 0},
  125. };
  126. static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
  127. { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
  128. { "all", BOND_ARP_TARGETS_ALL, 0},
  129. { NULL, -1, 0},
  130. };
  131. static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
  132. { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
  133. { "active", BOND_FOM_ACTIVE, 0},
  134. { "follow", BOND_FOM_FOLLOW, 0},
  135. { NULL, -1, 0},
  136. };
  137. static const struct bond_opt_value bond_intmax_tbl[] = {
  138. { "off", 0, BOND_VALFLAG_DEFAULT},
  139. { "maxval", INT_MAX, BOND_VALFLAG_MAX},
  140. { NULL, -1, 0}
  141. };
  142. static const struct bond_opt_value bond_lacp_active[] = {
  143. { "off", 0, 0},
  144. { "on", 1, BOND_VALFLAG_DEFAULT},
  145. { NULL, -1, 0}
  146. };
  147. static const struct bond_opt_value bond_lacp_rate_tbl[] = {
  148. { "slow", AD_LACP_SLOW, 0},
  149. { "fast", AD_LACP_FAST, 0},
  150. { NULL, -1, 0},
  151. };
  152. static const struct bond_opt_value bond_ad_select_tbl[] = {
  153. { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
  154. { "bandwidth", BOND_AD_BANDWIDTH, 0},
  155. { "count", BOND_AD_COUNT, 0},
  156. { "actor_port_prio", BOND_AD_PRIO, 0},
  157. { NULL, -1, 0},
  158. };
  159. static const struct bond_opt_value bond_num_peer_notif_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_peer_notif_delay_tbl[] = {
  166. { "off", 0, 0},
  167. { "maxval", 300000, BOND_VALFLAG_MAX},
  168. { NULL, -1, 0}
  169. };
  170. static const struct bond_opt_value bond_primary_reselect_tbl[] = {
  171. { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
  172. { "better", BOND_PRI_RESELECT_BETTER, 0},
  173. { "failure", BOND_PRI_RESELECT_FAILURE, 0},
  174. { NULL, -1},
  175. };
  176. static const struct bond_opt_value bond_use_carrier_tbl[] = {
  177. { "on", 1, BOND_VALFLAG_DEFAULT},
  178. { NULL, -1, 0}
  179. };
  180. static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
  181. { "off", 0, BOND_VALFLAG_DEFAULT},
  182. { "on", 1, 0},
  183. { NULL, -1, 0}
  184. };
  185. static const struct bond_opt_value bond_resend_igmp_tbl[] = {
  186. { "off", 0, 0},
  187. { "maxval", 255, BOND_VALFLAG_MAX},
  188. { "default", 1, BOND_VALFLAG_DEFAULT},
  189. { NULL, -1, 0}
  190. };
  191. static const struct bond_opt_value bond_lp_interval_tbl[] = {
  192. { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
  193. { "maxval", INT_MAX, BOND_VALFLAG_MAX},
  194. { NULL, -1, 0},
  195. };
  196. static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
  197. { "off", 0, 0},
  198. { "on", 1, BOND_VALFLAG_DEFAULT},
  199. { NULL, -1, 0}
  200. };
  201. static const struct bond_opt_value bond_ad_actor_sys_prio_tbl[] = {
  202. { "minval", 1, BOND_VALFLAG_MIN},
  203. { "maxval", 65535, BOND_VALFLAG_MAX | BOND_VALFLAG_DEFAULT},
  204. { NULL, -1, 0},
  205. };
  206. static const struct bond_opt_value bond_ad_user_port_key_tbl[] = {
  207. { "minval", 0, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
  208. { "maxval", 1023, BOND_VALFLAG_MAX},
  209. { NULL, -1, 0},
  210. };
  211. static const struct bond_opt_value bond_missed_max_tbl[] = {
  212. { "minval", 1, BOND_VALFLAG_MIN},
  213. { "maxval", 255, BOND_VALFLAG_MAX},
  214. { "default", 2, BOND_VALFLAG_DEFAULT},
  215. { NULL, -1, 0},
  216. };
  217. static const struct bond_opt_value bond_coupled_control_tbl[] = {
  218. { "on", 1, BOND_VALFLAG_DEFAULT},
  219. { "off", 0, 0},
  220. { NULL, -1, 0},
  221. };
  222. static const struct bond_opt_value bond_broadcast_neigh_tbl[] = {
  223. { "off", 0, BOND_VALFLAG_DEFAULT},
  224. { "on", 1, 0},
  225. { NULL, -1, 0}
  226. };
  227. static const struct bond_option bond_opts[BOND_OPT_LAST] = {
  228. [BOND_OPT_MODE] = {
  229. .id = BOND_OPT_MODE,
  230. .name = "mode",
  231. .desc = "bond device mode",
  232. .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
  233. .values = bond_mode_tbl,
  234. .set = bond_option_mode_set
  235. },
  236. [BOND_OPT_PACKETS_PER_SLAVE] = {
  237. .id = BOND_OPT_PACKETS_PER_SLAVE,
  238. .name = "packets_per_slave",
  239. .desc = "Packets to send per slave in RR mode",
  240. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
  241. .values = bond_pps_tbl,
  242. .set = bond_option_pps_set
  243. },
  244. [BOND_OPT_XMIT_HASH] = {
  245. .id = BOND_OPT_XMIT_HASH,
  246. .name = "xmit_hash_policy",
  247. .desc = "balance-xor, 802.3ad, and tlb hashing method",
  248. .values = bond_xmit_hashtype_tbl,
  249. .set = bond_option_xmit_hash_policy_set
  250. },
  251. [BOND_OPT_ARP_VALIDATE] = {
  252. .id = BOND_OPT_ARP_VALIDATE,
  253. .name = "arp_validate",
  254. .desc = "validate src/dst of ARP probes",
  255. .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
  256. BIT(BOND_MODE_ALB),
  257. .values = bond_arp_validate_tbl,
  258. .set = bond_option_arp_validate_set
  259. },
  260. [BOND_OPT_ARP_ALL_TARGETS] = {
  261. .id = BOND_OPT_ARP_ALL_TARGETS,
  262. .name = "arp_all_targets",
  263. .desc = "fail on any/all arp targets timeout",
  264. .values = bond_arp_all_targets_tbl,
  265. .set = bond_option_arp_all_targets_set
  266. },
  267. [BOND_OPT_FAIL_OVER_MAC] = {
  268. .id = BOND_OPT_FAIL_OVER_MAC,
  269. .name = "fail_over_mac",
  270. .desc = "For active-backup, do not set all slaves to the same MAC",
  271. .flags = BOND_OPTFLAG_NOSLAVES,
  272. .values = bond_fail_over_mac_tbl,
  273. .set = bond_option_fail_over_mac_set
  274. },
  275. [BOND_OPT_ARP_INTERVAL] = {
  276. .id = BOND_OPT_ARP_INTERVAL,
  277. .name = "arp_interval",
  278. .desc = "arp interval in milliseconds",
  279. .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
  280. BIT(BOND_MODE_ALB),
  281. .values = bond_intmax_tbl,
  282. .set = bond_option_arp_interval_set
  283. },
  284. [BOND_OPT_MISSED_MAX] = {
  285. .id = BOND_OPT_MISSED_MAX,
  286. .name = "arp_missed_max",
  287. .desc = "Maximum number of missed ARP interval",
  288. .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
  289. BIT(BOND_MODE_ALB),
  290. .values = bond_missed_max_tbl,
  291. .set = bond_option_missed_max_set
  292. },
  293. [BOND_OPT_ARP_TARGETS] = {
  294. .id = BOND_OPT_ARP_TARGETS,
  295. .name = "arp_ip_target",
  296. .desc = "arp targets in n.n.n.n form",
  297. .flags = BOND_OPTFLAG_RAWVAL,
  298. .set = bond_option_arp_ip_targets_set
  299. },
  300. [BOND_OPT_NS_TARGETS] = {
  301. .id = BOND_OPT_NS_TARGETS,
  302. .name = "ns_ip6_target",
  303. .desc = "NS targets in ffff:ffff::ffff:ffff form",
  304. .flags = BOND_OPTFLAG_RAWVAL,
  305. .set = bond_option_ns_ip6_targets_set
  306. },
  307. [BOND_OPT_DOWNDELAY] = {
  308. .id = BOND_OPT_DOWNDELAY,
  309. .name = "downdelay",
  310. .desc = "Delay before considering link down, in milliseconds",
  311. .values = bond_intmax_tbl,
  312. .set = bond_option_downdelay_set
  313. },
  314. [BOND_OPT_UPDELAY] = {
  315. .id = BOND_OPT_UPDELAY,
  316. .name = "updelay",
  317. .desc = "Delay before considering link up, in milliseconds",
  318. .values = bond_intmax_tbl,
  319. .set = bond_option_updelay_set
  320. },
  321. [BOND_OPT_LACP_ACTIVE] = {
  322. .id = BOND_OPT_LACP_ACTIVE,
  323. .name = "lacp_active",
  324. .desc = "Send LACPDU frames with configured lacp rate or acts as speak when spoken to",
  325. .flags = BOND_OPTFLAG_IFDOWN,
  326. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  327. .values = bond_lacp_active,
  328. .set = bond_option_lacp_active_set
  329. },
  330. [BOND_OPT_LACP_RATE] = {
  331. .id = BOND_OPT_LACP_RATE,
  332. .name = "lacp_rate",
  333. .desc = "LACPDU tx rate to request from 802.3ad partner",
  334. .flags = BOND_OPTFLAG_IFDOWN,
  335. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  336. .values = bond_lacp_rate_tbl,
  337. .set = bond_option_lacp_rate_set
  338. },
  339. [BOND_OPT_MINLINKS] = {
  340. .id = BOND_OPT_MINLINKS,
  341. .name = "min_links",
  342. .desc = "Minimum number of available links before turning on carrier",
  343. .values = bond_intmax_tbl,
  344. .set = bond_option_min_links_set
  345. },
  346. [BOND_OPT_AD_SELECT] = {
  347. .id = BOND_OPT_AD_SELECT,
  348. .name = "ad_select",
  349. .desc = "802.3ad aggregation selection logic",
  350. .flags = BOND_OPTFLAG_IFDOWN,
  351. .values = bond_ad_select_tbl,
  352. .set = bond_option_ad_select_set
  353. },
  354. [BOND_OPT_NUM_PEER_NOTIF] = {
  355. .id = BOND_OPT_NUM_PEER_NOTIF,
  356. .name = "num_unsol_na",
  357. .desc = "Number of peer notifications to send on failover event",
  358. .values = bond_num_peer_notif_tbl,
  359. .set = bond_option_num_peer_notif_set
  360. },
  361. [BOND_OPT_MIIMON] = {
  362. .id = BOND_OPT_MIIMON,
  363. .name = "miimon",
  364. .desc = "Link check interval in milliseconds",
  365. .values = bond_intmax_tbl,
  366. .set = bond_option_miimon_set
  367. },
  368. [BOND_OPT_PRIO] = {
  369. .id = BOND_OPT_PRIO,
  370. .name = "prio",
  371. .desc = "Link priority for failover re-selection",
  372. .flags = BOND_OPTFLAG_RAWVAL,
  373. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
  374. BIT(BOND_MODE_TLB) |
  375. BIT(BOND_MODE_ALB)),
  376. .set = bond_option_prio_set
  377. },
  378. [BOND_OPT_PRIMARY] = {
  379. .id = BOND_OPT_PRIMARY,
  380. .name = "primary",
  381. .desc = "Primary network device to use",
  382. .flags = BOND_OPTFLAG_RAWVAL,
  383. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
  384. BIT(BOND_MODE_TLB) |
  385. BIT(BOND_MODE_ALB)),
  386. .set = bond_option_primary_set
  387. },
  388. [BOND_OPT_PRIMARY_RESELECT] = {
  389. .id = BOND_OPT_PRIMARY_RESELECT,
  390. .name = "primary_reselect",
  391. .desc = "Reselect primary slave once it comes up",
  392. .values = bond_primary_reselect_tbl,
  393. .set = bond_option_primary_reselect_set
  394. },
  395. [BOND_OPT_USE_CARRIER] = {
  396. .id = BOND_OPT_USE_CARRIER,
  397. .name = "use_carrier",
  398. .desc = "option obsolete, use_carrier cannot be disabled",
  399. .values = bond_use_carrier_tbl,
  400. .set = bond_option_use_carrier_set
  401. },
  402. [BOND_OPT_ACTIVE_SLAVE] = {
  403. .id = BOND_OPT_ACTIVE_SLAVE,
  404. .name = "active_slave",
  405. .desc = "Currently active slave",
  406. .flags = BOND_OPTFLAG_RAWVAL,
  407. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
  408. BIT(BOND_MODE_TLB) |
  409. BIT(BOND_MODE_ALB)),
  410. .set = bond_option_active_slave_set
  411. },
  412. [BOND_OPT_QUEUE_ID] = {
  413. .id = BOND_OPT_QUEUE_ID,
  414. .name = "queue_id",
  415. .desc = "Set queue id of a slave",
  416. .flags = BOND_OPTFLAG_RAWVAL,
  417. .set = bond_option_queue_id_set
  418. },
  419. [BOND_OPT_ALL_SLAVES_ACTIVE] = {
  420. .id = BOND_OPT_ALL_SLAVES_ACTIVE,
  421. .name = "all_slaves_active",
  422. .desc = "Keep all frames received on an interface by setting active flag for all slaves",
  423. .values = bond_all_slaves_active_tbl,
  424. .set = bond_option_all_slaves_active_set
  425. },
  426. [BOND_OPT_RESEND_IGMP] = {
  427. .id = BOND_OPT_RESEND_IGMP,
  428. .name = "resend_igmp",
  429. .desc = "Number of IGMP membership reports to send on link failure",
  430. .values = bond_resend_igmp_tbl,
  431. .set = bond_option_resend_igmp_set
  432. },
  433. [BOND_OPT_LP_INTERVAL] = {
  434. .id = BOND_OPT_LP_INTERVAL,
  435. .name = "lp_interval",
  436. .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
  437. .values = bond_lp_interval_tbl,
  438. .set = bond_option_lp_interval_set
  439. },
  440. [BOND_OPT_SLAVES] = {
  441. .id = BOND_OPT_SLAVES,
  442. .name = "slaves",
  443. .desc = "Slave membership management",
  444. .flags = BOND_OPTFLAG_RAWVAL,
  445. .set = bond_option_slaves_set
  446. },
  447. [BOND_OPT_TLB_DYNAMIC_LB] = {
  448. .id = BOND_OPT_TLB_DYNAMIC_LB,
  449. .name = "tlb_dynamic_lb",
  450. .desc = "Enable dynamic flow shuffling",
  451. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB) | BIT(BOND_MODE_ALB)),
  452. .values = bond_tlb_dynamic_lb_tbl,
  453. .flags = BOND_OPTFLAG_IFDOWN,
  454. .set = bond_option_tlb_dynamic_lb_set,
  455. },
  456. [BOND_OPT_AD_ACTOR_SYS_PRIO] = {
  457. .id = BOND_OPT_AD_ACTOR_SYS_PRIO,
  458. .name = "ad_actor_sys_prio",
  459. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  460. .values = bond_ad_actor_sys_prio_tbl,
  461. .set = bond_option_ad_actor_sys_prio_set,
  462. },
  463. [BOND_OPT_ACTOR_PORT_PRIO] = {
  464. .id = BOND_OPT_ACTOR_PORT_PRIO,
  465. .name = "actor_port_prio",
  466. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  467. .flags = BOND_OPTFLAG_RAWVAL,
  468. .set = bond_option_actor_port_prio_set,
  469. },
  470. [BOND_OPT_AD_ACTOR_SYSTEM] = {
  471. .id = BOND_OPT_AD_ACTOR_SYSTEM,
  472. .name = "ad_actor_system",
  473. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  474. .flags = BOND_OPTFLAG_RAWVAL,
  475. .set = bond_option_ad_actor_system_set,
  476. },
  477. [BOND_OPT_AD_USER_PORT_KEY] = {
  478. .id = BOND_OPT_AD_USER_PORT_KEY,
  479. .name = "ad_user_port_key",
  480. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  481. .flags = BOND_OPTFLAG_IFDOWN,
  482. .values = bond_ad_user_port_key_tbl,
  483. .set = bond_option_ad_user_port_key_set,
  484. },
  485. [BOND_OPT_NUM_PEER_NOTIF_ALIAS] = {
  486. .id = BOND_OPT_NUM_PEER_NOTIF_ALIAS,
  487. .name = "num_grat_arp",
  488. .desc = "Number of peer notifications to send on failover event",
  489. .values = bond_num_peer_notif_tbl,
  490. .set = bond_option_num_peer_notif_set
  491. },
  492. [BOND_OPT_PEER_NOTIF_DELAY] = {
  493. .id = BOND_OPT_PEER_NOTIF_DELAY,
  494. .name = "peer_notif_delay",
  495. .desc = "Delay between each peer notification on failover event, in milliseconds",
  496. .values = bond_peer_notif_delay_tbl,
  497. .set = bond_option_peer_notif_delay_set
  498. },
  499. [BOND_OPT_COUPLED_CONTROL] = {
  500. .id = BOND_OPT_COUPLED_CONTROL,
  501. .name = "coupled_control",
  502. .desc = "Opt into using coupled control MUX for LACP states",
  503. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  504. .flags = BOND_OPTFLAG_IFDOWN,
  505. .values = bond_coupled_control_tbl,
  506. .set = bond_option_coupled_control_set,
  507. },
  508. [BOND_OPT_BROADCAST_NEIGH] = {
  509. .id = BOND_OPT_BROADCAST_NEIGH,
  510. .name = "broadcast_neighbor",
  511. .desc = "Broadcast neighbor packets to all active slaves",
  512. .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
  513. .values = bond_broadcast_neigh_tbl,
  514. .set = bond_option_broadcast_neigh_set,
  515. }
  516. };
  517. /* Searches for an option by name */
  518. const struct bond_option *bond_opt_get_by_name(const char *name)
  519. {
  520. const struct bond_option *opt;
  521. int option;
  522. for (option = 0; option < BOND_OPT_LAST; option++) {
  523. opt = bond_opt_get(option);
  524. if (opt && !strcmp(opt->name, name))
  525. return opt;
  526. }
  527. return NULL;
  528. }
  529. /* Searches for a value in opt's values[] table */
  530. const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
  531. {
  532. const struct bond_option *opt;
  533. int i;
  534. opt = bond_opt_get(option);
  535. if (WARN_ON(!opt))
  536. return NULL;
  537. for (i = 0; opt->values && opt->values[i].string; i++)
  538. if (opt->values[i].value == val)
  539. return &opt->values[i];
  540. return NULL;
  541. }
  542. /* Searches for a value in opt's values[] table which matches the flagmask */
  543. static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
  544. u32 flagmask)
  545. {
  546. int i;
  547. for (i = 0; opt->values && opt->values[i].string; i++)
  548. if (opt->values[i].flags & flagmask)
  549. return &opt->values[i];
  550. return NULL;
  551. }
  552. /* If maxval is missing then there's no range to check. In case minval is
  553. * missing then it's considered to be 0.
  554. */
  555. static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
  556. {
  557. const struct bond_opt_value *minval, *maxval;
  558. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  559. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  560. if (!maxval || (minval && val < minval->value) || val > maxval->value)
  561. return false;
  562. return true;
  563. }
  564. /**
  565. * bond_opt_parse - parse option value
  566. * @opt: the option to parse against
  567. * @val: value to parse
  568. *
  569. * This function tries to extract the value from @val and check if it's
  570. * a possible match for the option and returns NULL if a match isn't found,
  571. * or the struct_opt_value that matched. It also strips the new line from
  572. * @val->string if it's present.
  573. */
  574. const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
  575. struct bond_opt_value *val)
  576. {
  577. char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
  578. const struct bond_opt_value *tbl;
  579. const struct bond_opt_value *ret = NULL;
  580. bool checkval;
  581. int i, rv;
  582. /* No parsing if the option wants a raw val */
  583. if (opt->flags & BOND_OPTFLAG_RAWVAL)
  584. return val;
  585. tbl = opt->values;
  586. if (!tbl)
  587. goto out;
  588. /* ULLONG_MAX is used to bypass string processing */
  589. checkval = val->value != ULLONG_MAX;
  590. if (!checkval) {
  591. if (!val->string)
  592. goto out;
  593. p = strchr(val->string, '\n');
  594. if (p)
  595. *p = '\0';
  596. for (p = val->string; *p; p++)
  597. if (!(isdigit(*p) || isspace(*p)))
  598. break;
  599. /* The following code extracts the string to match or the value
  600. * and sets checkval appropriately
  601. */
  602. if (*p) {
  603. rv = sscanf(val->string, "%32s", valstr);
  604. } else {
  605. rv = sscanf(val->string, "%llu", &val->value);
  606. checkval = true;
  607. }
  608. if (!rv)
  609. goto out;
  610. }
  611. for (i = 0; tbl[i].string; i++) {
  612. /* Check for exact match */
  613. if (checkval) {
  614. if (val->value == tbl[i].value)
  615. ret = &tbl[i];
  616. } else {
  617. if (!strcmp(valstr, "default") &&
  618. (tbl[i].flags & BOND_VALFLAG_DEFAULT))
  619. ret = &tbl[i];
  620. if (!strcmp(valstr, tbl[i].string))
  621. ret = &tbl[i];
  622. }
  623. /* Found an exact match */
  624. if (ret)
  625. goto out;
  626. }
  627. /* Possible range match */
  628. if (checkval && bond_opt_check_range(opt, val->value))
  629. ret = val;
  630. out:
  631. return ret;
  632. }
  633. /* Check opt's dependencies against bond mode and currently set options */
  634. static int bond_opt_check_deps(struct bonding *bond,
  635. const struct bond_option *opt)
  636. {
  637. struct bond_params *params = &bond->params;
  638. if (test_bit(params->mode, &opt->unsuppmodes))
  639. return -EACCES;
  640. if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
  641. return -ENOTEMPTY;
  642. if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
  643. return -EBUSY;
  644. return 0;
  645. }
  646. static void bond_opt_dep_print(struct bonding *bond,
  647. const struct bond_option *opt,
  648. struct nlattr *bad_attr,
  649. struct netlink_ext_ack *extack)
  650. {
  651. const struct bond_opt_value *modeval;
  652. struct bond_params *params;
  653. params = &bond->params;
  654. modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
  655. if (test_bit(params->mode, &opt->unsuppmodes)) {
  656. netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
  657. opt->name, modeval->string, modeval->value);
  658. NL_SET_ERR_MSG_ATTR(extack, bad_attr,
  659. "option not supported in mode");
  660. }
  661. }
  662. static void bond_opt_error_interpret(struct bonding *bond,
  663. const struct bond_option *opt,
  664. int error, const struct bond_opt_value *val,
  665. struct nlattr *bad_attr,
  666. struct netlink_ext_ack *extack)
  667. {
  668. const struct bond_opt_value *minval, *maxval;
  669. char *p;
  670. switch (error) {
  671. case -EINVAL:
  672. NL_SET_ERR_MSG_ATTR(extack, bad_attr, "invalid option value");
  673. if (val) {
  674. if (val->string) {
  675. /* sometimes RAWVAL opts may have new lines */
  676. p = strchr(val->string, '\n');
  677. if (p)
  678. *p = '\0';
  679. netdev_err(bond->dev, "option %s: invalid value (%s)\n",
  680. opt->name, val->string);
  681. } else {
  682. netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
  683. opt->name, val->value);
  684. }
  685. }
  686. minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
  687. maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
  688. if (!maxval)
  689. break;
  690. netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
  691. opt->name, minval ? minval->value : 0, maxval->value);
  692. break;
  693. case -EACCES:
  694. bond_opt_dep_print(bond, opt, bad_attr, extack);
  695. break;
  696. case -ENOTEMPTY:
  697. NL_SET_ERR_MSG_ATTR(extack, bad_attr,
  698. "unable to set option because the bond device has slaves");
  699. netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
  700. opt->name);
  701. break;
  702. case -EBUSY:
  703. NL_SET_ERR_MSG_ATTR(extack, bad_attr,
  704. "unable to set option because the bond is up");
  705. netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
  706. opt->name);
  707. break;
  708. case -ENODEV:
  709. if (val && val->string) {
  710. p = strchr(val->string, '\n');
  711. if (p)
  712. *p = '\0';
  713. netdev_err(bond->dev, "option %s: interface %s does not exist!\n",
  714. opt->name, val->string);
  715. NL_SET_ERR_MSG_ATTR(extack, bad_attr,
  716. "interface does not exist");
  717. }
  718. break;
  719. default:
  720. break;
  721. }
  722. }
  723. /**
  724. * __bond_opt_set - set a bonding option
  725. * @bond: target bond device
  726. * @option: option to set
  727. * @val: value to set it to
  728. * @bad_attr: netlink attribue that caused the error
  729. * @extack: extended netlink error structure, used when an error message
  730. * needs to be returned to the caller via netlink
  731. *
  732. * This function is used to change the bond's option value, it can be
  733. * used for both enabling/changing an option and for disabling it. RTNL lock
  734. * must be obtained before calling this function.
  735. */
  736. int __bond_opt_set(struct bonding *bond,
  737. unsigned int option, struct bond_opt_value *val,
  738. struct nlattr *bad_attr, struct netlink_ext_ack *extack)
  739. {
  740. const struct bond_opt_value *retval = NULL;
  741. const struct bond_option *opt;
  742. int ret = -ENOENT;
  743. ASSERT_RTNL();
  744. opt = bond_opt_get(option);
  745. if (WARN_ON(!val) || WARN_ON(!opt))
  746. goto out;
  747. ret = bond_opt_check_deps(bond, opt);
  748. if (ret)
  749. goto out;
  750. retval = bond_opt_parse(opt, val);
  751. if (!retval) {
  752. ret = -EINVAL;
  753. goto out;
  754. }
  755. ret = opt->set(bond, retval);
  756. out:
  757. if (ret)
  758. bond_opt_error_interpret(bond, opt, ret, val, bad_attr, extack);
  759. return ret;
  760. }
  761. /**
  762. * __bond_opt_set_notify - set a bonding option
  763. * @bond: target bond device
  764. * @option: option to set
  765. * @val: value to set it to
  766. *
  767. * This function is used to change the bond's option value and trigger
  768. * a notification to user sapce. It can be used for both enabling/changing
  769. * an option and for disabling it. RTNL lock must be obtained before calling
  770. * this function.
  771. */
  772. int __bond_opt_set_notify(struct bonding *bond,
  773. unsigned int option, struct bond_opt_value *val)
  774. {
  775. int ret;
  776. ASSERT_RTNL();
  777. ret = __bond_opt_set(bond, option, val, NULL, NULL);
  778. if (!ret && (bond->dev->reg_state == NETREG_REGISTERED))
  779. call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
  780. return ret;
  781. }
  782. /**
  783. * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
  784. * @bond: target bond device
  785. * @option: option to set
  786. * @buf: value to set it to
  787. *
  788. * This function tries to acquire RTNL without blocking and if successful
  789. * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
  790. */
  791. int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
  792. {
  793. struct bond_opt_value optval;
  794. int ret;
  795. if (!rtnl_trylock())
  796. return restart_syscall();
  797. bond_opt_initstr(&optval, buf);
  798. ret = __bond_opt_set_notify(bond, option, &optval);
  799. rtnl_unlock();
  800. return ret;
  801. }
  802. /**
  803. * bond_opt_get - get a pointer to an option
  804. * @option: option for which to return a pointer
  805. *
  806. * This function checks if option is valid and if so returns a pointer
  807. * to its entry in the bond_opts[] option array.
  808. */
  809. const struct bond_option *bond_opt_get(unsigned int option)
  810. {
  811. if (!BOND_OPT_VALID(option))
  812. return NULL;
  813. return &bond_opts[option];
  814. }
  815. static bool bond_set_xfrm_features(struct bonding *bond)
  816. {
  817. if (!IS_ENABLED(CONFIG_XFRM_OFFLOAD))
  818. return false;
  819. if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
  820. bond->dev->wanted_features |= BOND_XFRM_FEATURES;
  821. else
  822. bond->dev->wanted_features &= ~BOND_XFRM_FEATURES;
  823. return true;
  824. }
  825. static int bond_option_mode_set(struct bonding *bond,
  826. const struct bond_opt_value *newval)
  827. {
  828. if (bond->xdp_prog && !bond_xdp_check(bond, newval->value))
  829. return -EOPNOTSUPP;
  830. if (!bond_mode_uses_arp(newval->value)) {
  831. if (bond->params.arp_interval) {
  832. netdev_dbg(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
  833. newval->string);
  834. /* disable arp monitoring */
  835. bond->params.arp_interval = 0;
  836. }
  837. if (!bond->params.miimon) {
  838. /* set miimon to default value */
  839. bond->params.miimon = BOND_DEFAULT_MIIMON;
  840. netdev_dbg(bond->dev, "Setting MII monitoring interval to %d\n",
  841. bond->params.miimon);
  842. }
  843. }
  844. if (newval->value == BOND_MODE_ALB)
  845. bond->params.tlb_dynamic_lb = 1;
  846. /* don't cache arp_validate between modes */
  847. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  848. bond->params.mode = newval->value;
  849. /* When changing mode, the bond device is down, we may reduce
  850. * the bond_bcast_neigh_enabled in bond_close() if broadcast_neighbor
  851. * enabled in 8023ad mode. Therefore, only clear broadcast_neighbor
  852. * to 0.
  853. */
  854. bond->params.broadcast_neighbor = 0;
  855. if (bond->dev->reg_state == NETREG_REGISTERED) {
  856. bool update = false;
  857. update |= bond_set_xfrm_features(bond);
  858. if (update)
  859. netdev_update_features(bond->dev);
  860. }
  861. bond_xdp_set_features(bond->dev);
  862. return 0;
  863. }
  864. static int bond_option_active_slave_set(struct bonding *bond,
  865. const struct bond_opt_value *newval)
  866. {
  867. char ifname[IFNAMSIZ] = { 0, };
  868. struct net_device *slave_dev;
  869. int ret = 0;
  870. sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
  871. if (!strlen(ifname) || newval->string[0] == '\n') {
  872. slave_dev = NULL;
  873. } else {
  874. slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  875. if (!slave_dev)
  876. return -ENODEV;
  877. }
  878. if (slave_dev) {
  879. if (!netif_is_bond_slave(slave_dev)) {
  880. slave_err(bond->dev, slave_dev, "Device is not bonding slave\n");
  881. return -EINVAL;
  882. }
  883. if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
  884. slave_err(bond->dev, slave_dev, "Device is not our slave\n");
  885. return -EINVAL;
  886. }
  887. }
  888. block_netpoll_tx();
  889. /* check to see if we are clearing active */
  890. if (!slave_dev) {
  891. netdev_dbg(bond->dev, "Clearing current active slave\n");
  892. bond_change_active_slave(bond, NULL);
  893. bond_select_active_slave(bond);
  894. } else {
  895. struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
  896. struct slave *new_active = bond_slave_get_rtnl(slave_dev);
  897. BUG_ON(!new_active);
  898. if (new_active == old_active) {
  899. /* do nothing */
  900. slave_dbg(bond->dev, new_active->dev, "is already the current active slave\n");
  901. } else {
  902. if (old_active && (new_active->link == BOND_LINK_UP) &&
  903. bond_slave_is_up(new_active)) {
  904. slave_dbg(bond->dev, new_active->dev, "Setting as active slave\n");
  905. bond_change_active_slave(bond, new_active);
  906. } else {
  907. slave_err(bond->dev, new_active->dev, "Could not set as active slave; either %s is down or the link is down\n",
  908. new_active->dev->name);
  909. ret = -EINVAL;
  910. }
  911. }
  912. }
  913. unblock_netpoll_tx();
  914. return ret;
  915. }
  916. /* There are two tricky bits here. First, if MII monitoring is activated, then
  917. * we must disable ARP monitoring. Second, if the timer isn't running, we must
  918. * start it.
  919. */
  920. static int bond_option_miimon_set(struct bonding *bond,
  921. const struct bond_opt_value *newval)
  922. {
  923. netdev_dbg(bond->dev, "Setting MII monitoring interval to %llu\n",
  924. newval->value);
  925. bond->params.miimon = newval->value;
  926. if (bond->params.updelay)
  927. netdev_dbg(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
  928. bond->params.updelay * bond->params.miimon);
  929. if (bond->params.downdelay)
  930. netdev_dbg(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
  931. bond->params.downdelay * bond->params.miimon);
  932. if (bond->params.peer_notif_delay)
  933. netdev_dbg(bond->dev, "Note: Updating peer_notif_delay (to %d) since it is a multiple of the miimon value\n",
  934. bond->params.peer_notif_delay * bond->params.miimon);
  935. if (newval->value && bond->params.arp_interval) {
  936. netdev_dbg(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
  937. bond->params.arp_interval = 0;
  938. if (bond->params.arp_validate)
  939. bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
  940. }
  941. if (bond->dev->flags & IFF_UP) {
  942. /* If the interface is up, we may need to fire off
  943. * the MII timer. If the interface is down, the
  944. * timer will get fired off when the open function
  945. * is called.
  946. */
  947. if (!newval->value) {
  948. cancel_delayed_work_sync(&bond->mii_work);
  949. } else {
  950. cancel_delayed_work_sync(&bond->arp_work);
  951. queue_delayed_work(bond->wq, &bond->mii_work, 0);
  952. }
  953. }
  954. return 0;
  955. }
  956. /* Set up, down and peer notification delays. These must be multiples
  957. * of the MII monitoring value, and are stored internally as the
  958. * multiplier. Thus, we must translate to MS for the real world.
  959. */
  960. static int _bond_option_delay_set(struct bonding *bond,
  961. const struct bond_opt_value *newval,
  962. const char *name,
  963. int *target)
  964. {
  965. int value = newval->value;
  966. if (!bond->params.miimon) {
  967. netdev_err(bond->dev, "Unable to set %s as MII monitoring is disabled\n",
  968. name);
  969. return -EPERM;
  970. }
  971. if ((value % bond->params.miimon) != 0) {
  972. netdev_warn(bond->dev,
  973. "%s (%d) is not a multiple of miimon (%d), value rounded to %d ms\n",
  974. name,
  975. value, bond->params.miimon,
  976. (value / bond->params.miimon) *
  977. bond->params.miimon);
  978. }
  979. *target = value / bond->params.miimon;
  980. netdev_dbg(bond->dev, "Setting %s to %d\n",
  981. name,
  982. *target * bond->params.miimon);
  983. return 0;
  984. }
  985. static int bond_option_updelay_set(struct bonding *bond,
  986. const struct bond_opt_value *newval)
  987. {
  988. return _bond_option_delay_set(bond, newval, "up delay",
  989. &bond->params.updelay);
  990. }
  991. static int bond_option_downdelay_set(struct bonding *bond,
  992. const struct bond_opt_value *newval)
  993. {
  994. return _bond_option_delay_set(bond, newval, "down delay",
  995. &bond->params.downdelay);
  996. }
  997. static int bond_option_peer_notif_delay_set(struct bonding *bond,
  998. const struct bond_opt_value *newval)
  999. {
  1000. int ret = _bond_option_delay_set(bond, newval,
  1001. "peer notification delay",
  1002. &bond->params.peer_notif_delay);
  1003. return ret;
  1004. }
  1005. static int bond_option_use_carrier_set(struct bonding *bond,
  1006. const struct bond_opt_value *newval)
  1007. {
  1008. return 0;
  1009. }
  1010. /* There are two tricky bits here. First, if ARP monitoring is activated, then
  1011. * we must disable MII monitoring. Second, if the ARP timer isn't running,
  1012. * we must start it.
  1013. */
  1014. static int bond_option_arp_interval_set(struct bonding *bond,
  1015. const struct bond_opt_value *newval)
  1016. {
  1017. netdev_dbg(bond->dev, "Setting ARP monitoring interval to %llu\n",
  1018. newval->value);
  1019. bond->params.arp_interval = newval->value;
  1020. if (newval->value) {
  1021. if (bond->params.miimon) {
  1022. netdev_dbg(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
  1023. bond->params.miimon = 0;
  1024. }
  1025. if (!bond->params.arp_targets[0])
  1026. netdev_dbg(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
  1027. }
  1028. if (bond->dev->flags & IFF_UP) {
  1029. /* If the interface is up, we may need to fire off
  1030. * the ARP timer. If the interface is down, the
  1031. * timer will get fired off when the open function
  1032. * is called.
  1033. */
  1034. if (!newval->value) {
  1035. if (bond->params.arp_validate)
  1036. bond->recv_probe = NULL;
  1037. cancel_delayed_work_sync(&bond->arp_work);
  1038. } else {
  1039. /* arp_validate can be set only in active-backup mode */
  1040. bond->recv_probe = bond_rcv_validate;
  1041. cancel_delayed_work_sync(&bond->mii_work);
  1042. queue_delayed_work(bond->wq, &bond->arp_work, 0);
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
  1048. __be32 target,
  1049. unsigned long last_rx)
  1050. {
  1051. __be32 *targets = bond->params.arp_targets;
  1052. struct list_head *iter;
  1053. struct slave *slave;
  1054. if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
  1055. bond_for_each_slave(bond, slave, iter)
  1056. WRITE_ONCE(slave->target_last_arp_rx[slot], last_rx);
  1057. targets[slot] = target;
  1058. }
  1059. }
  1060. static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  1061. {
  1062. __be32 *targets = bond->params.arp_targets;
  1063. int ind;
  1064. if (!bond_is_ip_target_ok(target)) {
  1065. netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
  1066. &target);
  1067. return -EINVAL;
  1068. }
  1069. if (bond_get_targets_ip(targets, target) != -1) { /* dup */
  1070. netdev_err(bond->dev, "ARP target %pI4 is already present\n",
  1071. &target);
  1072. return -EINVAL;
  1073. }
  1074. ind = bond_get_targets_ip(targets, 0); /* first free slot */
  1075. if (ind == -1) {
  1076. netdev_err(bond->dev, "ARP target table is full!\n");
  1077. return -EINVAL;
  1078. }
  1079. netdev_dbg(bond->dev, "Adding ARP target %pI4\n", &target);
  1080. _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
  1081. return 0;
  1082. }
  1083. static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
  1084. {
  1085. return _bond_option_arp_ip_target_add(bond, target);
  1086. }
  1087. static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
  1088. {
  1089. __be32 *targets = bond->params.arp_targets;
  1090. struct list_head *iter;
  1091. struct slave *slave;
  1092. unsigned long *targets_rx;
  1093. int ind, i;
  1094. if (!bond_is_ip_target_ok(target)) {
  1095. netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
  1096. &target);
  1097. return -EINVAL;
  1098. }
  1099. ind = bond_get_targets_ip(targets, target);
  1100. if (ind == -1) {
  1101. netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
  1102. &target);
  1103. return -EINVAL;
  1104. }
  1105. if (ind == 0 && !targets[1] && bond->params.arp_interval)
  1106. netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
  1107. netdev_dbg(bond->dev, "Removing ARP target %pI4\n", &target);
  1108. bond_for_each_slave(bond, slave, iter) {
  1109. targets_rx = slave->target_last_arp_rx;
  1110. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  1111. WRITE_ONCE(targets_rx[i], READ_ONCE(targets_rx[i+1]));
  1112. WRITE_ONCE(targets_rx[i], 0);
  1113. }
  1114. for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
  1115. targets[i] = targets[i+1];
  1116. targets[i] = 0;
  1117. return 0;
  1118. }
  1119. void bond_option_arp_ip_targets_clear(struct bonding *bond)
  1120. {
  1121. int i;
  1122. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  1123. _bond_options_arp_ip_target_set(bond, i, 0, 0);
  1124. }
  1125. static int bond_option_arp_ip_targets_set(struct bonding *bond,
  1126. const struct bond_opt_value *newval)
  1127. {
  1128. int ret = -EPERM;
  1129. __be32 target;
  1130. if (newval->string) {
  1131. if (strlen(newval->string) < 1 ||
  1132. !in4_pton(newval->string + 1, -1, (u8 *)&target, -1, NULL)) {
  1133. netdev_err(bond->dev, "invalid ARP target specified\n");
  1134. return ret;
  1135. }
  1136. if (newval->string[0] == '+')
  1137. ret = bond_option_arp_ip_target_add(bond, target);
  1138. else if (newval->string[0] == '-')
  1139. ret = bond_option_arp_ip_target_rem(bond, target);
  1140. else
  1141. netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
  1142. } else {
  1143. target = newval->value;
  1144. ret = bond_option_arp_ip_target_add(bond, target);
  1145. }
  1146. return ret;
  1147. }
  1148. #if IS_ENABLED(CONFIG_IPV6)
  1149. static bool slave_can_set_ns_maddr(const struct bonding *bond, struct slave *slave)
  1150. {
  1151. return BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
  1152. !bond_is_active_slave(slave) &&
  1153. slave->dev->flags & IFF_MULTICAST;
  1154. }
  1155. /**
  1156. * slave_set_ns_maddrs - add/del all NS mac addresses for slave
  1157. * @bond: bond device
  1158. * @slave: slave device
  1159. * @add: add or remove all the NS mac addresses
  1160. *
  1161. * This function tries to add or delete all the NS mac addresses on the slave
  1162. *
  1163. * Note, the IPv6 NS target address is the unicast address in Neighbor
  1164. * Solicitation (NS) message. The dest address of NS message should be
  1165. * solicited-node multicast address of the target. The dest mac of NS message
  1166. * is converted from the solicited-node multicast address.
  1167. *
  1168. * This function is called when
  1169. * * arp_validate changes
  1170. * * enslaving, releasing new slaves
  1171. */
  1172. static void slave_set_ns_maddrs(struct bonding *bond, struct slave *slave, bool add)
  1173. {
  1174. struct in6_addr *targets = bond->params.ns_targets;
  1175. char slot_maddr[MAX_ADDR_LEN];
  1176. struct in6_addr mcaddr;
  1177. int i;
  1178. if (!slave_can_set_ns_maddr(bond, slave))
  1179. return;
  1180. for (i = 0; i < BOND_MAX_NS_TARGETS; i++) {
  1181. if (ipv6_addr_any(&targets[i]))
  1182. break;
  1183. addrconf_addr_solict_mult(&targets[i], &mcaddr);
  1184. if (!ndisc_mc_map(&mcaddr, slot_maddr, slave->dev, 0)) {
  1185. if (add)
  1186. dev_mc_add(slave->dev, slot_maddr);
  1187. else
  1188. dev_mc_del(slave->dev, slot_maddr);
  1189. }
  1190. }
  1191. }
  1192. void bond_slave_ns_maddrs_add(struct bonding *bond, struct slave *slave)
  1193. {
  1194. if (!bond->params.arp_validate)
  1195. return;
  1196. slave_set_ns_maddrs(bond, slave, true);
  1197. }
  1198. void bond_slave_ns_maddrs_del(struct bonding *bond, struct slave *slave)
  1199. {
  1200. if (!bond->params.arp_validate)
  1201. return;
  1202. slave_set_ns_maddrs(bond, slave, false);
  1203. }
  1204. /**
  1205. * slave_set_ns_maddr - set new NS mac address for slave
  1206. * @bond: bond device
  1207. * @slave: slave device
  1208. * @target: the new IPv6 target
  1209. * @slot: the old IPv6 target in the slot
  1210. *
  1211. * This function tries to replace the old mac address to new one on the slave.
  1212. *
  1213. * Note, the target/slot IPv6 address is the unicast address in Neighbor
  1214. * Solicitation (NS) message. The dest address of NS message should be
  1215. * solicited-node multicast address of the target. The dest mac of NS message
  1216. * is converted from the solicited-node multicast address.
  1217. *
  1218. * This function is called when
  1219. * * An IPv6 NS target is added or removed.
  1220. */
  1221. static void slave_set_ns_maddr(struct bonding *bond, struct slave *slave,
  1222. struct in6_addr *target, struct in6_addr *slot)
  1223. {
  1224. char mac_addr[MAX_ADDR_LEN];
  1225. struct in6_addr mcast_addr;
  1226. if (!bond->params.arp_validate || !slave_can_set_ns_maddr(bond, slave))
  1227. return;
  1228. /* remove the previous mac addr from slave */
  1229. addrconf_addr_solict_mult(slot, &mcast_addr);
  1230. if (!ipv6_addr_any(slot) &&
  1231. !ndisc_mc_map(&mcast_addr, mac_addr, slave->dev, 0))
  1232. dev_mc_del(slave->dev, mac_addr);
  1233. /* add new mac addr on slave if target is set */
  1234. addrconf_addr_solict_mult(target, &mcast_addr);
  1235. if (!ipv6_addr_any(target) &&
  1236. !ndisc_mc_map(&mcast_addr, mac_addr, slave->dev, 0))
  1237. dev_mc_add(slave->dev, mac_addr);
  1238. }
  1239. static void _bond_options_ns_ip6_target_set(struct bonding *bond, int slot,
  1240. struct in6_addr *target,
  1241. unsigned long last_rx)
  1242. {
  1243. struct in6_addr *targets = bond->params.ns_targets;
  1244. struct list_head *iter;
  1245. struct slave *slave;
  1246. if (slot >= 0 && slot < BOND_MAX_NS_TARGETS) {
  1247. bond_for_each_slave(bond, slave, iter) {
  1248. WRITE_ONCE(slave->target_last_arp_rx[slot], last_rx);
  1249. slave_set_ns_maddr(bond, slave, target, &targets[slot]);
  1250. }
  1251. targets[slot] = *target;
  1252. }
  1253. }
  1254. void bond_option_ns_ip6_targets_clear(struct bonding *bond)
  1255. {
  1256. struct in6_addr addr_any = in6addr_any;
  1257. int i;
  1258. for (i = 0; i < BOND_MAX_NS_TARGETS; i++)
  1259. _bond_options_ns_ip6_target_set(bond, i, &addr_any, 0);
  1260. }
  1261. static int bond_option_ns_ip6_targets_set(struct bonding *bond,
  1262. const struct bond_opt_value *newval)
  1263. {
  1264. struct in6_addr *target = (struct in6_addr *)newval->extra;
  1265. struct in6_addr *targets = bond->params.ns_targets;
  1266. struct in6_addr addr_any = in6addr_any;
  1267. int index;
  1268. if (!bond_is_ip6_target_ok(target)) {
  1269. netdev_err(bond->dev, "invalid NS target %pI6c specified for addition\n",
  1270. target);
  1271. return -EINVAL;
  1272. }
  1273. if (bond_get_targets_ip6(targets, target) != -1) { /* dup */
  1274. netdev_err(bond->dev, "NS target %pI6c is already present\n",
  1275. target);
  1276. return -EINVAL;
  1277. }
  1278. index = bond_get_targets_ip6(targets, &addr_any); /* first free slot */
  1279. if (index == -1) {
  1280. netdev_err(bond->dev, "NS target table is full!\n");
  1281. return -EINVAL;
  1282. }
  1283. netdev_dbg(bond->dev, "Adding NS target %pI6c\n", target);
  1284. _bond_options_ns_ip6_target_set(bond, index, target, jiffies);
  1285. return 0;
  1286. }
  1287. #else
  1288. static int bond_option_ns_ip6_targets_set(struct bonding *bond,
  1289. const struct bond_opt_value *newval)
  1290. {
  1291. return -EPERM;
  1292. }
  1293. static void slave_set_ns_maddrs(struct bonding *bond, struct slave *slave, bool add) {}
  1294. void bond_slave_ns_maddrs_add(struct bonding *bond, struct slave *slave) {}
  1295. void bond_slave_ns_maddrs_del(struct bonding *bond, struct slave *slave) {}
  1296. #endif
  1297. static int bond_option_arp_validate_set(struct bonding *bond,
  1298. const struct bond_opt_value *newval)
  1299. {
  1300. bool changed = !!bond->params.arp_validate != !!newval->value;
  1301. struct list_head *iter;
  1302. struct slave *slave;
  1303. netdev_dbg(bond->dev, "Setting arp_validate to %s (%llu)\n",
  1304. newval->string, newval->value);
  1305. bond->params.arp_validate = newval->value;
  1306. if (changed) {
  1307. bond_for_each_slave(bond, slave, iter)
  1308. slave_set_ns_maddrs(bond, slave, !!bond->params.arp_validate);
  1309. }
  1310. return 0;
  1311. }
  1312. static int bond_option_arp_all_targets_set(struct bonding *bond,
  1313. const struct bond_opt_value *newval)
  1314. {
  1315. netdev_dbg(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
  1316. newval->string, newval->value);
  1317. bond->params.arp_all_targets = newval->value;
  1318. return 0;
  1319. }
  1320. static int bond_option_missed_max_set(struct bonding *bond,
  1321. const struct bond_opt_value *newval)
  1322. {
  1323. netdev_dbg(bond->dev, "Setting missed max to %s (%llu)\n",
  1324. newval->string, newval->value);
  1325. bond->params.missed_max = newval->value;
  1326. return 0;
  1327. }
  1328. static int bond_option_prio_set(struct bonding *bond,
  1329. const struct bond_opt_value *newval)
  1330. {
  1331. struct slave *slave;
  1332. slave = bond_slave_get_rtnl(newval->slave_dev);
  1333. if (!slave) {
  1334. netdev_dbg(newval->slave_dev, "%s called on NULL slave\n", __func__);
  1335. return -ENODEV;
  1336. }
  1337. slave->prio = newval->value;
  1338. if (rtnl_dereference(bond->primary_slave))
  1339. slave_warn(bond->dev, slave->dev,
  1340. "prio updated, but will not affect failover re-selection as primary slave have been set\n");
  1341. else
  1342. bond_select_active_slave(bond);
  1343. return 0;
  1344. }
  1345. static int bond_option_primary_set(struct bonding *bond,
  1346. const struct bond_opt_value *newval)
  1347. {
  1348. char *p, *primary = newval->string;
  1349. struct list_head *iter;
  1350. struct slave *slave;
  1351. block_netpoll_tx();
  1352. p = strchr(primary, '\n');
  1353. if (p)
  1354. *p = '\0';
  1355. /* check to see if we are clearing primary */
  1356. if (!strlen(primary)) {
  1357. netdev_dbg(bond->dev, "Setting primary slave to None\n");
  1358. RCU_INIT_POINTER(bond->primary_slave, NULL);
  1359. memset(bond->params.primary, 0, sizeof(bond->params.primary));
  1360. bond_select_active_slave(bond);
  1361. goto out;
  1362. }
  1363. bond_for_each_slave(bond, slave, iter) {
  1364. if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
  1365. slave_dbg(bond->dev, slave->dev, "Setting as primary slave\n");
  1366. rcu_assign_pointer(bond->primary_slave, slave);
  1367. strcpy(bond->params.primary, slave->dev->name);
  1368. bond->force_primary = true;
  1369. bond_select_active_slave(bond);
  1370. goto out;
  1371. }
  1372. }
  1373. if (rtnl_dereference(bond->primary_slave)) {
  1374. netdev_dbg(bond->dev, "Setting primary slave to None\n");
  1375. RCU_INIT_POINTER(bond->primary_slave, NULL);
  1376. bond_select_active_slave(bond);
  1377. }
  1378. strscpy_pad(bond->params.primary, primary, IFNAMSIZ);
  1379. netdev_dbg(bond->dev, "Recording %s as primary, but it has not been enslaved yet\n",
  1380. primary);
  1381. out:
  1382. unblock_netpoll_tx();
  1383. return 0;
  1384. }
  1385. static int bond_option_primary_reselect_set(struct bonding *bond,
  1386. const struct bond_opt_value *newval)
  1387. {
  1388. netdev_dbg(bond->dev, "Setting primary_reselect to %s (%llu)\n",
  1389. newval->string, newval->value);
  1390. bond->params.primary_reselect = newval->value;
  1391. block_netpoll_tx();
  1392. bond_select_active_slave(bond);
  1393. unblock_netpoll_tx();
  1394. return 0;
  1395. }
  1396. static int bond_option_fail_over_mac_set(struct bonding *bond,
  1397. const struct bond_opt_value *newval)
  1398. {
  1399. netdev_dbg(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
  1400. newval->string, newval->value);
  1401. bond->params.fail_over_mac = newval->value;
  1402. return 0;
  1403. }
  1404. static int bond_option_xmit_hash_policy_set(struct bonding *bond,
  1405. const struct bond_opt_value *newval)
  1406. {
  1407. if (bond->xdp_prog && !__bond_xdp_check(BOND_MODE(bond), newval->value))
  1408. return -EOPNOTSUPP;
  1409. netdev_dbg(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
  1410. newval->string, newval->value);
  1411. bond->params.xmit_policy = newval->value;
  1412. return 0;
  1413. }
  1414. static int bond_option_resend_igmp_set(struct bonding *bond,
  1415. const struct bond_opt_value *newval)
  1416. {
  1417. netdev_dbg(bond->dev, "Setting resend_igmp to %llu\n",
  1418. newval->value);
  1419. bond->params.resend_igmp = newval->value;
  1420. return 0;
  1421. }
  1422. static int bond_option_num_peer_notif_set(struct bonding *bond,
  1423. const struct bond_opt_value *newval)
  1424. {
  1425. bond->params.num_peer_notif = newval->value;
  1426. return 0;
  1427. }
  1428. static int bond_option_all_slaves_active_set(struct bonding *bond,
  1429. const struct bond_opt_value *newval)
  1430. {
  1431. struct list_head *iter;
  1432. struct slave *slave;
  1433. if (newval->value == bond->params.all_slaves_active)
  1434. return 0;
  1435. bond->params.all_slaves_active = newval->value;
  1436. bond_for_each_slave(bond, slave, iter) {
  1437. if (!bond_is_active_slave(slave)) {
  1438. if (newval->value)
  1439. slave->inactive = 0;
  1440. else
  1441. slave->inactive = 1;
  1442. }
  1443. }
  1444. return 0;
  1445. }
  1446. static int bond_option_min_links_set(struct bonding *bond,
  1447. const struct bond_opt_value *newval)
  1448. {
  1449. netdev_dbg(bond->dev, "Setting min links value to %llu\n",
  1450. newval->value);
  1451. bond->params.min_links = newval->value;
  1452. bond_set_carrier(bond);
  1453. return 0;
  1454. }
  1455. static int bond_option_lp_interval_set(struct bonding *bond,
  1456. const struct bond_opt_value *newval)
  1457. {
  1458. bond->params.lp_interval = newval->value;
  1459. return 0;
  1460. }
  1461. static int bond_option_pps_set(struct bonding *bond,
  1462. const struct bond_opt_value *newval)
  1463. {
  1464. netdev_dbg(bond->dev, "Setting packets per slave to %llu\n",
  1465. newval->value);
  1466. bond->params.packets_per_slave = newval->value;
  1467. if (newval->value > 0) {
  1468. bond->params.reciprocal_packets_per_slave =
  1469. reciprocal_value(newval->value);
  1470. } else {
  1471. /* reciprocal_packets_per_slave is unused if
  1472. * packets_per_slave is 0 or 1, just initialize it
  1473. */
  1474. bond->params.reciprocal_packets_per_slave =
  1475. (struct reciprocal_value) { 0 };
  1476. }
  1477. return 0;
  1478. }
  1479. static int bond_option_lacp_active_set(struct bonding *bond,
  1480. const struct bond_opt_value *newval)
  1481. {
  1482. netdev_dbg(bond->dev, "Setting LACP active to %s (%llu)\n",
  1483. newval->string, newval->value);
  1484. bond->params.lacp_active = newval->value;
  1485. bond_3ad_update_lacp_active(bond);
  1486. return 0;
  1487. }
  1488. static int bond_option_lacp_rate_set(struct bonding *bond,
  1489. const struct bond_opt_value *newval)
  1490. {
  1491. netdev_dbg(bond->dev, "Setting LACP rate to %s (%llu)\n",
  1492. newval->string, newval->value);
  1493. bond->params.lacp_fast = newval->value;
  1494. bond_3ad_update_lacp_rate(bond);
  1495. return 0;
  1496. }
  1497. static int bond_option_ad_select_set(struct bonding *bond,
  1498. const struct bond_opt_value *newval)
  1499. {
  1500. netdev_dbg(bond->dev, "Setting ad_select to %s (%llu)\n",
  1501. newval->string, newval->value);
  1502. bond->params.ad_select = newval->value;
  1503. return 0;
  1504. }
  1505. static int bond_option_queue_id_set(struct bonding *bond,
  1506. const struct bond_opt_value *newval)
  1507. {
  1508. struct slave *slave, *update_slave;
  1509. struct net_device *sdev;
  1510. struct list_head *iter;
  1511. char *delim;
  1512. int ret = 0;
  1513. u16 qid;
  1514. /* delim will point to queue id if successful */
  1515. delim = strchr(newval->string, ':');
  1516. if (!delim)
  1517. goto err_no_cmd;
  1518. /* Terminate string that points to device name and bump it
  1519. * up one, so we can read the queue id there.
  1520. */
  1521. *delim = '\0';
  1522. if (sscanf(++delim, "%hd\n", &qid) != 1)
  1523. goto err_no_cmd;
  1524. /* Check buffer length, valid ifname and queue id */
  1525. if (!dev_valid_name(newval->string) ||
  1526. qid > bond->dev->real_num_tx_queues)
  1527. goto err_no_cmd;
  1528. /* Get the pointer to that interface if it exists */
  1529. sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
  1530. if (!sdev)
  1531. goto err_no_cmd;
  1532. /* Search for thes slave and check for duplicate qids */
  1533. update_slave = NULL;
  1534. bond_for_each_slave(bond, slave, iter) {
  1535. if (sdev == slave->dev)
  1536. /* We don't need to check the matching
  1537. * slave for dups, since we're overwriting it
  1538. */
  1539. update_slave = slave;
  1540. else if (qid && qid == slave->queue_id) {
  1541. goto err_no_cmd;
  1542. }
  1543. }
  1544. if (!update_slave)
  1545. goto err_no_cmd;
  1546. /* Actually set the qids for the slave */
  1547. WRITE_ONCE(update_slave->queue_id, qid);
  1548. out:
  1549. return ret;
  1550. err_no_cmd:
  1551. netdev_dbg(bond->dev, "invalid input for queue_id set\n");
  1552. ret = -EPERM;
  1553. goto out;
  1554. }
  1555. static int bond_option_slaves_set(struct bonding *bond,
  1556. const struct bond_opt_value *newval)
  1557. {
  1558. char command[IFNAMSIZ + 1] = { 0, };
  1559. struct net_device *dev;
  1560. char *ifname;
  1561. int ret;
  1562. sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
  1563. ifname = command + 1;
  1564. if ((strlen(command) <= 1) ||
  1565. (command[0] != '+' && command[0] != '-') ||
  1566. !dev_valid_name(ifname))
  1567. goto err_no_cmd;
  1568. dev = __dev_get_by_name(dev_net(bond->dev), ifname);
  1569. if (!dev) {
  1570. netdev_dbg(bond->dev, "interface %s does not exist!\n",
  1571. ifname);
  1572. ret = -ENODEV;
  1573. goto out;
  1574. }
  1575. switch (command[0]) {
  1576. case '+':
  1577. slave_dbg(bond->dev, dev, "Enslaving interface\n");
  1578. ret = bond_enslave(bond->dev, dev, NULL);
  1579. break;
  1580. case '-':
  1581. slave_dbg(bond->dev, dev, "Releasing interface\n");
  1582. ret = bond_release(bond->dev, dev);
  1583. break;
  1584. default:
  1585. /* should not run here. */
  1586. goto err_no_cmd;
  1587. }
  1588. out:
  1589. return ret;
  1590. err_no_cmd:
  1591. netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
  1592. ret = -EPERM;
  1593. goto out;
  1594. }
  1595. static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
  1596. const struct bond_opt_value *newval)
  1597. {
  1598. netdev_dbg(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
  1599. newval->string, newval->value);
  1600. bond->params.tlb_dynamic_lb = newval->value;
  1601. return 0;
  1602. }
  1603. static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
  1604. const struct bond_opt_value *newval)
  1605. {
  1606. netdev_dbg(bond->dev, "Setting ad_actor_sys_prio to %llu\n",
  1607. newval->value);
  1608. bond->params.ad_actor_sys_prio = newval->value;
  1609. bond_3ad_update_ad_actor_settings(bond);
  1610. return 0;
  1611. }
  1612. static int bond_option_actor_port_prio_set(struct bonding *bond,
  1613. const struct bond_opt_value *newval)
  1614. {
  1615. struct slave *slave;
  1616. slave = bond_slave_get_rtnl(newval->slave_dev);
  1617. if (!slave) {
  1618. netdev_dbg(bond->dev, "%s called on NULL slave\n", __func__);
  1619. return -ENODEV;
  1620. }
  1621. netdev_dbg(newval->slave_dev, "Setting actor_port_prio to %llu\n",
  1622. newval->value);
  1623. SLAVE_AD_INFO(slave)->port_priority = newval->value;
  1624. bond_3ad_update_ad_actor_settings(bond);
  1625. return 0;
  1626. }
  1627. static int bond_option_ad_actor_system_set(struct bonding *bond,
  1628. const struct bond_opt_value *newval)
  1629. {
  1630. u8 macaddr[ETH_ALEN];
  1631. u8 *mac;
  1632. if (newval->string) {
  1633. if (!mac_pton(newval->string, macaddr))
  1634. goto err;
  1635. mac = macaddr;
  1636. } else {
  1637. mac = (u8 *)&newval->value;
  1638. }
  1639. if (is_multicast_ether_addr(mac))
  1640. goto err;
  1641. netdev_dbg(bond->dev, "Setting ad_actor_system to %pM\n", mac);
  1642. ether_addr_copy(bond->params.ad_actor_system, mac);
  1643. bond_3ad_update_ad_actor_settings(bond);
  1644. return 0;
  1645. err:
  1646. netdev_err(bond->dev, "Invalid ad_actor_system MAC address.\n");
  1647. return -EINVAL;
  1648. }
  1649. static int bond_option_ad_user_port_key_set(struct bonding *bond,
  1650. const struct bond_opt_value *newval)
  1651. {
  1652. netdev_dbg(bond->dev, "Setting ad_user_port_key to %llu\n",
  1653. newval->value);
  1654. bond->params.ad_user_port_key = newval->value;
  1655. return 0;
  1656. }
  1657. static int bond_option_coupled_control_set(struct bonding *bond,
  1658. const struct bond_opt_value *newval)
  1659. {
  1660. netdev_info(bond->dev, "Setting coupled_control to %s (%llu)\n",
  1661. newval->string, newval->value);
  1662. bond->params.coupled_control = newval->value;
  1663. return 0;
  1664. }
  1665. static int bond_option_broadcast_neigh_set(struct bonding *bond,
  1666. const struct bond_opt_value *newval)
  1667. {
  1668. if (bond->params.broadcast_neighbor == newval->value)
  1669. return 0;
  1670. bond->params.broadcast_neighbor = newval->value;
  1671. if (bond->dev->flags & IFF_UP) {
  1672. if (bond->params.broadcast_neighbor)
  1673. static_branch_inc(&bond_bcast_neigh_enabled);
  1674. else
  1675. static_branch_dec(&bond_bcast_neigh_enabled);
  1676. }
  1677. netdev_dbg(bond->dev, "Setting broadcast_neighbor to %s (%llu)\n",
  1678. newval->string, newval->value);
  1679. return 0;
  1680. }