bond_options.c 35 KB

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