]>
Commit | Line | Data |
---|---|---|
f620b43a | 1 | /* |
c460a6a7 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. |
f620b43a BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include "bond.h" | |
20 | ||
21 | #include <limits.h> | |
22 | #include <stdint.h> | |
23 | #include <stdlib.h> | |
75fad143 | 24 | #include <math.h> |
f620b43a | 25 | |
da4a6191 | 26 | #include "connectivity.h" |
f620b43a | 27 | #include "coverage.h" |
b598f214 | 28 | #include "dp-packet.h" |
f620b43a | 29 | #include "flow.h" |
ee89ea7b | 30 | #include "openvswitch/hmap.h" |
bdebeece | 31 | #include "lacp.h" |
f620b43a BP |
32 | #include "netdev.h" |
33 | #include "odp-util.h" | |
b598f214 BW |
34 | #include "ofproto/ofproto-dpif.h" |
35 | #include "ofproto/ofproto-dpif-rid.h" | |
36 | #include "ofproto/ofproto-provider.h" | |
37 | #include "openvswitch/dynamic-string.h" | |
38 | #include "openvswitch/list.h" | |
39 | #include "openvswitch/match.h" | |
40 | #include "openvswitch/ofp-actions.h" | |
41 | #include "openvswitch/ofp-util.h" | |
64c96779 | 42 | #include "openvswitch/ofpbuf.h" |
b598f214 | 43 | #include "openvswitch/vlog.h" |
f620b43a BP |
44 | #include "packets.h" |
45 | #include "poll-loop.h" | |
da4a6191 | 46 | #include "seq.h" |
ee89ea7b | 47 | #include "openvswitch/shash.h" |
f620b43a BP |
48 | #include "timeval.h" |
49 | #include "unixctl.h" | |
ee89ea7b | 50 | #include "util.h" |
f620b43a BP |
51 | |
52 | VLOG_DEFINE_THIS_MODULE(bond); | |
53 | ||
f1c8a79c AW |
54 | static struct ovs_rwlock rwlock = OVS_RWLOCK_INITIALIZER; |
55 | static struct hmap all_bonds__ = HMAP_INITIALIZER(&all_bonds__); | |
56 | static struct hmap *const all_bonds OVS_GUARDED_BY(rwlock) = &all_bonds__; | |
57 | ||
9e1a6910 | 58 | /* Bit-mask for hashing a flow down to a bucket. */ |
f620b43a | 59 | #define BOND_MASK 0xff |
9e1a6910 | 60 | #define BOND_BUCKETS (BOND_MASK + 1) |
f620b43a BP |
61 | |
62 | /* A hash bucket for mapping a flow to a slave. | |
9e1a6910 | 63 | * "struct bond" has an array of BOND_BUCKETS of these. */ |
f620b43a BP |
64 | struct bond_entry { |
65 | struct bond_slave *slave; /* Assigned slave, NULL if unassigned. */ | |
c6855ec5 JS |
66 | uint64_t tx_bytes /* Count of bytes recently transmitted. */ |
67 | OVS_GUARDED_BY(rwlock); | |
ca6ba700 | 68 | struct ovs_list list_node; /* In bond_slave's 'entries' list. */ |
adcf00ba | 69 | |
c6855ec5 JS |
70 | /* Recirculation. |
71 | * | |
72 | * 'pr_rule' is the post-recirculation rule for this entry. | |
73 | * 'pr_tx_bytes' is the most recently seen statistics for 'pr_rule', which | |
74 | * is used to determine delta (applied to 'tx_bytes' above.) */ | |
75 | struct rule *pr_rule; | |
76 | uint64_t pr_tx_bytes OVS_GUARDED_BY(rwlock); | |
f620b43a BP |
77 | }; |
78 | ||
79 | /* A bond slave, that is, one of the links comprising a bond. */ | |
80 | struct bond_slave { | |
81 | struct hmap_node hmap_node; /* In struct bond's slaves hmap. */ | |
ca6ba700 | 82 | struct ovs_list list_node; /* In struct bond's enabled_slaves list. */ |
f620b43a BP |
83 | struct bond *bond; /* The bond that contains this slave. */ |
84 | void *aux; /* Client-provided handle for this slave. */ | |
85 | ||
86 | struct netdev *netdev; /* Network device, owned by the client. */ | |
6422372c | 87 | uint64_t change_seq; /* Tracks changes in 'netdev'. */ |
0746a84f | 88 | ofp_port_t ofp_port; /* OpenFlow port number. */ |
f620b43a BP |
89 | char *name; /* Name (a copy of netdev_get_name(netdev)). */ |
90 | ||
91 | /* Link status. */ | |
92 | long long delay_expires; /* Time after which 'enabled' may change. */ | |
f620b43a | 93 | bool enabled; /* May be chosen for flows? */ |
296f6519 | 94 | bool may_enable; /* Client considers this slave bondable. */ |
f620b43a BP |
95 | |
96 | /* Rebalancing info. Used only by bond_rebalance(). */ | |
ca6ba700 TG |
97 | struct ovs_list bal_node; /* In bond_rebalance()'s 'bals' list. */ |
98 | struct ovs_list entries; /* 'struct bond_entry's assigned here. */ | |
f620b43a BP |
99 | uint64_t tx_bytes; /* Sum across 'tx_bytes' of entries. */ |
100 | }; | |
101 | ||
102 | /* A bond, that is, a set of network devices grouped to improve performance or | |
103 | * robustness. */ | |
104 | struct bond { | |
105 | struct hmap_node hmap_node; /* In 'all_bonds' hmap. */ | |
106 | char *name; /* Name provided by client. */ | |
adcf00ba | 107 | struct ofproto_dpif *ofproto; /* The bridge this bond belongs to. */ |
f620b43a BP |
108 | |
109 | /* Slaves. */ | |
110 | struct hmap slaves; | |
111 | ||
f1c8a79c AW |
112 | /* Enabled slaves. |
113 | * | |
114 | * Any reader or writer of 'enabled_slaves' must hold 'mutex'. | |
115 | * (To prevent the bond_slave from disappearing they must also hold | |
116 | * 'rwlock'.) */ | |
117 | struct ovs_mutex mutex OVS_ACQ_AFTER(rwlock); | |
ca6ba700 | 118 | struct ovs_list enabled_slaves OVS_GUARDED; /* Contains struct bond_slaves. */ |
f1c8a79c | 119 | |
f620b43a BP |
120 | /* Bonding info. */ |
121 | enum bond_mode balance; /* Balancing mode, one of BM_*. */ | |
122 | struct bond_slave *active_slave; | |
f620b43a | 123 | int updelay, downdelay; /* Delay before slave goes up/down, in ms. */ |
bdebeece | 124 | enum lacp_status lacp_status; /* Status of LACP negotiations. */ |
62904702 | 125 | bool bond_revalidate; /* True if flows need revalidation. */ |
672d18b2 | 126 | uint32_t basis; /* Basis for flow hash function. */ |
f620b43a BP |
127 | |
128 | /* SLB specific bonding info. */ | |
9e1a6910 | 129 | struct bond_entry *hash; /* An array of BOND_BUCKETS elements. */ |
f620b43a BP |
130 | int rebalance_interval; /* Interval between rebalances, in ms. */ |
131 | long long int next_rebalance; /* Next rebalancing time. */ | |
132 | bool send_learning_packets; | |
adcf00ba AZ |
133 | uint32_t recirc_id; /* Non zero if recirculation can be used.*/ |
134 | struct hmap pr_rule_ops; /* Helps to maintain post recirculation rules.*/ | |
f620b43a | 135 | |
3e5aeeb5 AZ |
136 | /* Store active slave to OVSDB. */ |
137 | bool active_slave_changed; /* Set to true whenever the bond changes | |
138 | active slave. It will be reset to false | |
139 | after it is stored into OVSDB */ | |
140 | ||
141 | /* Interface name may not be persistent across an OS reboot, use | |
142 | * MAC address for identifing the active slave */ | |
74ff3298 | 143 | struct eth_addr active_slave_mac; |
3e5aeeb5 | 144 | /* The MAC address of the active interface. */ |
f620b43a | 145 | /* Legacy compatibility. */ |
9dd165e0 | 146 | bool lacp_fallback_ab; /* Fallback to active-backup on LACP failure. */ |
f620b43a | 147 | |
37bec3d3 | 148 | struct ovs_refcount ref_cnt; |
f620b43a BP |
149 | }; |
150 | ||
adcf00ba AZ |
151 | /* What to do with an bond_recirc_rule. */ |
152 | enum bond_op { | |
153 | ADD, /* Add the rule to ofproto's flow table. */ | |
154 | DEL, /* Delete the rule from the ofproto's flow table. */ | |
155 | }; | |
156 | ||
157 | /* A rule to add to or delete from ofproto's internal flow table. */ | |
158 | struct bond_pr_rule_op { | |
159 | struct hmap_node hmap_node; | |
160 | struct match match; | |
161 | ofp_port_t out_ofport; | |
162 | enum bond_op op; | |
6c932bc8 | 163 | struct rule **pr_rule; |
adcf00ba AZ |
164 | }; |
165 | ||
3bfd3972 EJ |
166 | static void bond_entry_reset(struct bond *) OVS_REQ_WRLOCK(rwlock); |
167 | static struct bond_slave *bond_slave_lookup(struct bond *, const void *slave_) | |
168 | OVS_REQ_RDLOCK(rwlock); | |
4a1b8f30 EJ |
169 | static void bond_enable_slave(struct bond_slave *, bool enable) |
170 | OVS_REQ_WRLOCK(rwlock); | |
171 | static void bond_link_status_update(struct bond_slave *) | |
3bfd3972 | 172 | OVS_REQ_WRLOCK(rwlock); |
4a1b8f30 | 173 | static void bond_choose_active_slave(struct bond *) |
9e1a6910 | 174 | OVS_REQ_WRLOCK(rwlock); |
74ff3298 | 175 | static unsigned int bond_hash_src(const struct eth_addr mac, |
672d18b2 EJ |
176 | uint16_t vlan, uint32_t basis); |
177 | static unsigned int bond_hash_tcp(const struct flow *, uint16_t vlan, | |
178 | uint32_t basis); | |
f620b43a BP |
179 | static struct bond_entry *lookup_bond_entry(const struct bond *, |
180 | const struct flow *, | |
3bfd3972 EJ |
181 | uint16_t vlan) |
182 | OVS_REQ_RDLOCK(rwlock); | |
f1c8a79c AW |
183 | static struct bond_slave *get_enabled_slave(struct bond *) |
184 | OVS_REQ_RDLOCK(rwlock); | |
f620b43a BP |
185 | static struct bond_slave *choose_output_slave(const struct bond *, |
186 | const struct flow *, | |
bcd2633a | 187 | struct flow_wildcards *, |
4a1b8f30 | 188 | uint16_t vlan) |
3bfd3972 | 189 | OVS_REQ_RDLOCK(rwlock); |
f620b43a BP |
190 | |
191 | /* Attempts to parse 's' as the name of a bond balancing mode. If successful, | |
192 | * stores the mode in '*balance' and returns true. Otherwise returns false | |
193 | * without modifying '*balance'. */ | |
194 | bool | |
195 | bond_mode_from_string(enum bond_mode *balance, const char *s) | |
196 | { | |
197 | if (!strcmp(s, bond_mode_to_string(BM_TCP))) { | |
198 | *balance = BM_TCP; | |
199 | } else if (!strcmp(s, bond_mode_to_string(BM_SLB))) { | |
200 | *balance = BM_SLB; | |
201 | } else if (!strcmp(s, bond_mode_to_string(BM_AB))) { | |
202 | *balance = BM_AB; | |
203 | } else { | |
204 | return false; | |
205 | } | |
206 | return true; | |
207 | } | |
208 | ||
209 | /* Returns a string representing 'balance'. */ | |
210 | const char * | |
211 | bond_mode_to_string(enum bond_mode balance) { | |
212 | switch (balance) { | |
213 | case BM_TCP: | |
214 | return "balance-tcp"; | |
215 | case BM_SLB: | |
216 | return "balance-slb"; | |
217 | case BM_AB: | |
218 | return "active-backup"; | |
219 | } | |
428b2edd | 220 | OVS_NOT_REACHED(); |
f620b43a BP |
221 | } |
222 | ||
f620b43a BP |
223 | \f |
224 | /* Creates and returns a new bond whose configuration is initially taken from | |
225 | * 's'. | |
226 | * | |
227 | * The caller should register each slave on the new bond by calling | |
228 | * bond_slave_register(). */ | |
229 | struct bond * | |
adcf00ba | 230 | bond_create(const struct bond_settings *s, struct ofproto_dpif *ofproto) |
f620b43a BP |
231 | { |
232 | struct bond *bond; | |
233 | ||
234 | bond = xzalloc(sizeof *bond); | |
adcf00ba | 235 | bond->ofproto = ofproto; |
f620b43a | 236 | hmap_init(&bond->slaves); |
417e7e66 | 237 | ovs_list_init(&bond->enabled_slaves); |
f1c8a79c | 238 | ovs_mutex_init(&bond->mutex); |
37bec3d3 | 239 | ovs_refcount_init(&bond->ref_cnt); |
adcf00ba AZ |
240 | hmap_init(&bond->pr_rule_ops); |
241 | ||
f620b43a | 242 | bond_reconfigure(bond, s); |
f620b43a BP |
243 | return bond; |
244 | } | |
245 | ||
03366a2d EJ |
246 | struct bond * |
247 | bond_ref(const struct bond *bond_) | |
248 | { | |
249 | struct bond *bond = CONST_CAST(struct bond *, bond_); | |
250 | ||
bca0b3b4 | 251 | if (bond) { |
37bec3d3 | 252 | ovs_refcount_ref(&bond->ref_cnt); |
bca0b3b4 | 253 | } |
03366a2d EJ |
254 | return bond; |
255 | } | |
256 | ||
f620b43a BP |
257 | /* Frees 'bond'. */ |
258 | void | |
03366a2d | 259 | bond_unref(struct bond *bond) |
f620b43a | 260 | { |
4ec3d7c7 DDP |
261 | struct bond_pr_rule_op *pr_op; |
262 | struct bond_slave *slave; | |
f620b43a | 263 | |
24f83812 | 264 | if (!bond || ovs_refcount_unref_relaxed(&bond->ref_cnt) != 1) { |
03366a2d EJ |
265 | return; |
266 | } | |
267 | ||
3bfd3972 EJ |
268 | ovs_rwlock_wrlock(&rwlock); |
269 | hmap_remove(all_bonds, &bond->hmap_node); | |
270 | ovs_rwlock_unlock(&rwlock); | |
f620b43a | 271 | |
4ec3d7c7 | 272 | HMAP_FOR_EACH_POP (slave, hmap_node, &bond->slaves) { |
f620b43a BP |
273 | /* Client owns 'slave->netdev'. */ |
274 | free(slave->name); | |
275 | free(slave); | |
276 | } | |
277 | hmap_destroy(&bond->slaves); | |
278 | ||
f1c8a79c | 279 | ovs_mutex_destroy(&bond->mutex); |
f620b43a | 280 | free(bond->hash); |
f620b43a | 281 | free(bond->name); |
adcf00ba | 282 | |
4ec3d7c7 | 283 | HMAP_FOR_EACH_POP (pr_op, hmap_node, &bond->pr_rule_ops) { |
adcf00ba AZ |
284 | free(pr_op); |
285 | } | |
286 | hmap_destroy(&bond->pr_rule_ops); | |
287 | ||
288 | if (bond->recirc_id) { | |
e672ff9b | 289 | recirc_free_id(bond->recirc_id); |
adcf00ba AZ |
290 | } |
291 | ||
f620b43a BP |
292 | free(bond); |
293 | } | |
294 | ||
adcf00ba AZ |
295 | static void |
296 | add_pr_rule(struct bond *bond, const struct match *match, | |
6c932bc8 | 297 | ofp_port_t out_ofport, struct rule **rule) |
adcf00ba AZ |
298 | { |
299 | uint32_t hash = match_hash(match, 0); | |
300 | struct bond_pr_rule_op *pr_op; | |
301 | ||
302 | HMAP_FOR_EACH_WITH_HASH(pr_op, hmap_node, hash, &bond->pr_rule_ops) { | |
303 | if (match_equal(&pr_op->match, match)) { | |
304 | pr_op->op = ADD; | |
305 | pr_op->out_ofport = out_ofport; | |
306 | pr_op->pr_rule = rule; | |
307 | return; | |
308 | } | |
309 | } | |
310 | ||
311 | pr_op = xmalloc(sizeof *pr_op); | |
312 | pr_op->match = *match; | |
313 | pr_op->op = ADD; | |
314 | pr_op->out_ofport = out_ofport; | |
315 | pr_op->pr_rule = rule; | |
316 | hmap_insert(&bond->pr_rule_ops, &pr_op->hmap_node, hash); | |
317 | } | |
318 | ||
319 | static void | |
320 | update_recirc_rules(struct bond *bond) | |
ca8127fd | 321 | OVS_REQ_WRLOCK(rwlock) |
adcf00ba AZ |
322 | { |
323 | struct match match; | |
324 | struct bond_pr_rule_op *pr_op, *next_op; | |
325 | uint64_t ofpacts_stub[128 / 8]; | |
326 | struct ofpbuf ofpacts; | |
327 | int i; | |
328 | ||
329 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); | |
330 | ||
331 | HMAP_FOR_EACH(pr_op, hmap_node, &bond->pr_rule_ops) { | |
332 | pr_op->op = DEL; | |
333 | } | |
334 | ||
6c932bc8 AZ |
335 | if (bond->hash && bond->recirc_id) { |
336 | for (i = 0; i < BOND_BUCKETS; i++) { | |
337 | struct bond_slave *slave = bond->hash[i].slave; | |
adcf00ba | 338 | |
6c932bc8 AZ |
339 | if (slave) { |
340 | match_init_catchall(&match); | |
341 | match_set_recirc_id(&match, bond->recirc_id); | |
6c932bc8 | 342 | match_set_dp_hash_masked(&match, i, BOND_MASK); |
adcf00ba | 343 | |
6c932bc8 AZ |
344 | add_pr_rule(bond, &match, slave->ofp_port, |
345 | &bond->hash[i].pr_rule); | |
346 | } | |
adcf00ba AZ |
347 | } |
348 | } | |
349 | ||
350 | HMAP_FOR_EACH_SAFE(pr_op, next_op, hmap_node, &bond->pr_rule_ops) { | |
351 | int error; | |
adcf00ba AZ |
352 | switch (pr_op->op) { |
353 | case ADD: | |
354 | ofpbuf_clear(&ofpacts); | |
355 | ofpact_put_OUTPUT(&ofpacts)->port = pr_op->out_ofport; | |
356 | error = ofproto_dpif_add_internal_flow(bond->ofproto, | |
357 | &pr_op->match, | |
290ad78a | 358 | RECIRC_RULE_PRIORITY, 0, |
6c932bc8 | 359 | &ofpacts, pr_op->pr_rule); |
adcf00ba AZ |
360 | if (error) { |
361 | char *err_s = match_to_string(&pr_op->match, | |
362 | RECIRC_RULE_PRIORITY); | |
363 | ||
364 | VLOG_ERR("failed to add post recirculation flow %s", err_s); | |
365 | free(err_s); | |
adcf00ba AZ |
366 | } |
367 | break; | |
368 | ||
369 | case DEL: | |
370 | error = ofproto_dpif_delete_internal_flow(bond->ofproto, | |
371 | &pr_op->match, | |
372 | RECIRC_RULE_PRIORITY); | |
373 | if (error) { | |
374 | char *err_s = match_to_string(&pr_op->match, | |
375 | RECIRC_RULE_PRIORITY); | |
376 | ||
377 | VLOG_ERR("failed to remove post recirculation flow %s", err_s); | |
378 | free(err_s); | |
379 | } | |
380 | ||
381 | hmap_remove(&bond->pr_rule_ops, &pr_op->hmap_node); | |
6c932bc8 | 382 | *pr_op->pr_rule = NULL; |
adcf00ba AZ |
383 | free(pr_op); |
384 | break; | |
385 | } | |
386 | } | |
387 | ||
388 | ofpbuf_uninit(&ofpacts); | |
389 | } | |
390 | ||
391 | ||
f620b43a BP |
392 | /* Updates 'bond''s overall configuration to 's'. |
393 | * | |
394 | * The caller should register each slave on 'bond' by calling | |
395 | * bond_slave_register(). This is optional if none of the slaves' | |
4d6fb5eb | 396 | * configuration has changed. In any case it can't hurt. |
59d7b2b6 EJ |
397 | * |
398 | * Returns true if the configuration has changed in such a way that requires | |
399 | * flow revalidation. | |
400 | * */ | |
401 | bool | |
f620b43a BP |
402 | bond_reconfigure(struct bond *bond, const struct bond_settings *s) |
403 | { | |
59d7b2b6 EJ |
404 | bool revalidate = false; |
405 | ||
3bfd3972 | 406 | ovs_rwlock_wrlock(&rwlock); |
f620b43a BP |
407 | if (!bond->name || strcmp(bond->name, s->name)) { |
408 | if (bond->name) { | |
3bfd3972 | 409 | hmap_remove(all_bonds, &bond->hmap_node); |
f620b43a BP |
410 | free(bond->name); |
411 | } | |
412 | bond->name = xstrdup(s->name); | |
3bfd3972 | 413 | hmap_insert(all_bonds, &bond->hmap_node, hash_string(bond->name, 0)); |
f620b43a BP |
414 | } |
415 | ||
f620b43a BP |
416 | bond->updelay = s->up_delay; |
417 | bond->downdelay = s->down_delay; | |
bc1b010c | 418 | |
9dd165e0 RK |
419 | if (bond->lacp_fallback_ab != s->lacp_fallback_ab_cfg) { |
420 | bond->lacp_fallback_ab = s->lacp_fallback_ab_cfg; | |
421 | revalidate = true; | |
422 | } | |
423 | ||
bc1b010c EJ |
424 | if (bond->rebalance_interval != s->rebalance_interval) { |
425 | bond->rebalance_interval = s->rebalance_interval; | |
426 | revalidate = true; | |
427 | } | |
f620b43a | 428 | |
59d7b2b6 EJ |
429 | if (bond->balance != s->balance) { |
430 | bond->balance = s->balance; | |
431 | revalidate = true; | |
432 | } | |
433 | ||
672d18b2 EJ |
434 | if (bond->basis != s->basis) { |
435 | bond->basis = s->basis; | |
436 | revalidate = true; | |
437 | } | |
438 | ||
62904702 EJ |
439 | if (bond->bond_revalidate) { |
440 | revalidate = true; | |
441 | bond->bond_revalidate = false; | |
442 | } | |
443 | ||
adcf00ba AZ |
444 | if (bond->balance != BM_AB) { |
445 | if (!bond->recirc_id) { | |
e672ff9b | 446 | bond->recirc_id = recirc_alloc_id(bond->ofproto); |
adcf00ba AZ |
447 | } |
448 | } else if (bond->recirc_id) { | |
e672ff9b | 449 | recirc_free_id(bond->recirc_id); |
adcf00ba AZ |
450 | bond->recirc_id = 0; |
451 | } | |
452 | ||
95aafb2a EJ |
453 | if (bond->balance == BM_AB || !bond->hash || revalidate) { |
454 | bond_entry_reset(bond); | |
455 | } | |
456 | ||
74ff3298 | 457 | bond->active_slave_mac = s->active_slave_mac; |
3e5aeeb5 AZ |
458 | bond->active_slave_changed = false; |
459 | ||
3bfd3972 | 460 | ovs_rwlock_unlock(&rwlock); |
59d7b2b6 | 461 | return revalidate; |
f620b43a BP |
462 | } |
463 | ||
3e5aeeb5 | 464 | static struct bond_slave * |
74ff3298 | 465 | bond_find_slave_by_mac(const struct bond *bond, const struct eth_addr mac) |
3e5aeeb5 AZ |
466 | { |
467 | struct bond_slave *slave; | |
468 | ||
469 | /* Find the last active slave */ | |
470 | HMAP_FOR_EACH(slave, hmap_node, &bond->slaves) { | |
74ff3298 | 471 | struct eth_addr slave_mac; |
3e5aeeb5 | 472 | |
74ff3298 | 473 | if (netdev_get_etheraddr(slave->netdev, &slave_mac)) { |
3e5aeeb5 AZ |
474 | continue; |
475 | } | |
476 | ||
74ff3298 | 477 | if (eth_addr_equals(slave_mac, mac)) { |
3e5aeeb5 AZ |
478 | return slave; |
479 | } | |
480 | } | |
481 | ||
482 | return NULL; | |
483 | } | |
484 | ||
485 | static void | |
486 | bond_active_slave_changed(struct bond *bond) | |
487 | { | |
74ff3298 | 488 | struct eth_addr mac; |
3e5aeeb5 | 489 | |
74ff3298 JR |
490 | netdev_get_etheraddr(bond->active_slave->netdev, &mac); |
491 | bond->active_slave_mac = mac; | |
3e5aeeb5 AZ |
492 | bond->active_slave_changed = true; |
493 | seq_change(connectivity_seq_get()); | |
494 | } | |
495 | ||
f8ddccd2 | 496 | static void |
1ea24138 | 497 | bond_slave_set_netdev__(struct bond_slave *slave, struct netdev *netdev) |
3bfd3972 | 498 | OVS_REQ_WRLOCK(rwlock) |
f8ddccd2 BP |
499 | { |
500 | if (slave->netdev != netdev) { | |
f8ddccd2 | 501 | slave->netdev = netdev; |
1ea24138 | 502 | slave->change_seq = 0; |
f8ddccd2 BP |
503 | } |
504 | } | |
505 | ||
f620b43a BP |
506 | /* Registers 'slave_' as a slave of 'bond'. The 'slave_' pointer is an |
507 | * arbitrary client-provided pointer that uniquely identifies a slave within a | |
508 | * bond. If 'slave_' already exists within 'bond' then this function | |
509 | * reconfigures the existing slave. | |
510 | * | |
511 | * 'netdev' must be the network device that 'slave_' represents. It is owned | |
512 | * by the client, so the client must not close it before either unregistering | |
513 | * 'slave_' or destroying 'bond'. | |
4d6fb5eb | 514 | */ |
f620b43a | 515 | void |
adcf00ba AZ |
516 | bond_slave_register(struct bond *bond, void *slave_, |
517 | ofp_port_t ofport, struct netdev *netdev) | |
f620b43a | 518 | { |
3bfd3972 | 519 | struct bond_slave *slave; |
f620b43a | 520 | |
3bfd3972 EJ |
521 | ovs_rwlock_wrlock(&rwlock); |
522 | slave = bond_slave_lookup(bond, slave_); | |
f620b43a BP |
523 | if (!slave) { |
524 | slave = xzalloc(sizeof *slave); | |
525 | ||
526 | hmap_insert(&bond->slaves, &slave->hmap_node, hash_pointer(slave_, 0)); | |
527 | slave->bond = bond; | |
528 | slave->aux = slave_; | |
adcf00ba | 529 | slave->ofp_port = ofport; |
f620b43a | 530 | slave->delay_expires = LLONG_MAX; |
244b2160 | 531 | slave->name = xstrdup(netdev_get_name(netdev)); |
7321e30e | 532 | bond->bond_revalidate = true; |
244b2160 | 533 | |
b3c18f66 | 534 | slave->enabled = false; |
4a1b8f30 | 535 | bond_enable_slave(slave, netdev_get_carrier(netdev)); |
f620b43a BP |
536 | } |
537 | ||
1ea24138 | 538 | bond_slave_set_netdev__(slave, netdev); |
a6934aa9 | 539 | |
f620b43a BP |
540 | free(slave->name); |
541 | slave->name = xstrdup(netdev_get_name(netdev)); | |
3bfd3972 | 542 | ovs_rwlock_unlock(&rwlock); |
f620b43a BP |
543 | } |
544 | ||
f8ddccd2 BP |
545 | /* Updates the network device to be used with 'slave_' to 'netdev'. |
546 | * | |
547 | * This is useful if the caller closes and re-opens the network device | |
548 | * registered with bond_slave_register() but doesn't need to change anything | |
549 | * else. */ | |
550 | void | |
551 | bond_slave_set_netdev(struct bond *bond, void *slave_, struct netdev *netdev) | |
552 | { | |
3bfd3972 EJ |
553 | struct bond_slave *slave; |
554 | ||
555 | ovs_rwlock_wrlock(&rwlock); | |
556 | slave = bond_slave_lookup(bond, slave_); | |
f8ddccd2 | 557 | if (slave) { |
1ea24138 | 558 | bond_slave_set_netdev__(slave, netdev); |
f8ddccd2 | 559 | } |
3bfd3972 | 560 | ovs_rwlock_unlock(&rwlock); |
f8ddccd2 BP |
561 | } |
562 | ||
f620b43a BP |
563 | /* Unregisters 'slave_' from 'bond'. If 'bond' does not contain such a slave |
564 | * then this function has no effect. | |
565 | * | |
566 | * Unregistering a slave invalidates all flows. */ | |
567 | void | |
568 | bond_slave_unregister(struct bond *bond, const void *slave_) | |
569 | { | |
3bfd3972 | 570 | struct bond_slave *slave; |
f620b43a BP |
571 | bool del_active; |
572 | ||
3bfd3972 EJ |
573 | ovs_rwlock_wrlock(&rwlock); |
574 | slave = bond_slave_lookup(bond, slave_); | |
f620b43a | 575 | if (!slave) { |
3bfd3972 | 576 | goto out; |
f620b43a BP |
577 | } |
578 | ||
4a1b8f30 EJ |
579 | bond->bond_revalidate = true; |
580 | bond_enable_slave(slave, false); | |
b3c18f66 | 581 | |
f620b43a BP |
582 | del_active = bond->active_slave == slave; |
583 | if (bond->hash) { | |
584 | struct bond_entry *e; | |
585 | for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) { | |
586 | if (e->slave == slave) { | |
587 | e->slave = NULL; | |
588 | } | |
589 | } | |
590 | } | |
591 | ||
592 | free(slave->name); | |
593 | ||
594 | hmap_remove(&bond->slaves, &slave->hmap_node); | |
595 | /* Client owns 'slave->netdev'. */ | |
596 | free(slave); | |
597 | ||
598 | if (del_active) { | |
4a1b8f30 | 599 | bond_choose_active_slave(bond); |
f620b43a BP |
600 | bond->send_learning_packets = true; |
601 | } | |
3bfd3972 EJ |
602 | out: |
603 | ovs_rwlock_unlock(&rwlock); | |
f620b43a BP |
604 | } |
605 | ||
296f6519 EJ |
606 | /* Should be called on each slave in 'bond' before bond_run() to indicate |
607 | * whether or not 'slave_' may be enabled. This function is intended to allow | |
608 | * other protocols to have some impact on bonding decisions. For example LACP | |
609 | * or high level link monitoring protocols may decide that a given slave should | |
610 | * not be able to send traffic. */ | |
4d6fb5eb | 611 | void |
296f6519 | 612 | bond_slave_set_may_enable(struct bond *bond, void *slave_, bool may_enable) |
4d6fb5eb | 613 | { |
3bfd3972 | 614 | ovs_rwlock_wrlock(&rwlock); |
296f6519 | 615 | bond_slave_lookup(bond, slave_)->may_enable = may_enable; |
3bfd3972 | 616 | ovs_rwlock_unlock(&rwlock); |
4d6fb5eb EJ |
617 | } |
618 | ||
4a1b8f30 EJ |
619 | /* Performs periodic maintenance on 'bond'. |
620 | * | |
621 | * Returns true if the caller should revalidate its flows. | |
f620b43a BP |
622 | * |
623 | * The caller should check bond_should_send_learning_packets() afterward. */ | |
4a1b8f30 EJ |
624 | bool |
625 | bond_run(struct bond *bond, enum lacp_status lacp_status) | |
f620b43a BP |
626 | { |
627 | struct bond_slave *slave; | |
4a1b8f30 | 628 | bool revalidate; |
f620b43a | 629 | |
3bfd3972 | 630 | ovs_rwlock_wrlock(&rwlock); |
bdebeece EJ |
631 | if (bond->lacp_status != lacp_status) { |
632 | bond->lacp_status = lacp_status; | |
4592d0e2 EJ |
633 | bond->bond_revalidate = true; |
634 | } | |
4d6fb5eb | 635 | |
f620b43a BP |
636 | /* Enable slaves based on link status and LACP feedback. */ |
637 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { | |
4a1b8f30 | 638 | bond_link_status_update(slave); |
da4a6191 | 639 | slave->change_seq = seq_read(connectivity_seq_get()); |
f620b43a BP |
640 | } |
641 | if (!bond->active_slave || !bond->active_slave->enabled) { | |
4a1b8f30 | 642 | bond_choose_active_slave(bond); |
f620b43a BP |
643 | } |
644 | ||
4a1b8f30 EJ |
645 | revalidate = bond->bond_revalidate; |
646 | bond->bond_revalidate = false; | |
3bfd3972 | 647 | ovs_rwlock_unlock(&rwlock); |
4a1b8f30 EJ |
648 | |
649 | return revalidate; | |
f620b43a BP |
650 | } |
651 | ||
652 | /* Causes poll_block() to wake up when 'bond' needs something to be done. */ | |
653 | void | |
654 | bond_wait(struct bond *bond) | |
655 | { | |
656 | struct bond_slave *slave; | |
657 | ||
3bfd3972 | 658 | ovs_rwlock_rdlock(&rwlock); |
f620b43a BP |
659 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { |
660 | if (slave->delay_expires != LLONG_MAX) { | |
661 | poll_timer_wait_until(slave->delay_expires); | |
662 | } | |
1ea24138 | 663 | |
da4a6191 | 664 | seq_wait(connectivity_seq_get(), slave->change_seq); |
f620b43a BP |
665 | } |
666 | ||
bbc13389 | 667 | if (bond->bond_revalidate) { |
f620b43a BP |
668 | poll_immediate_wake(); |
669 | } | |
3bfd3972 | 670 | ovs_rwlock_unlock(&rwlock); |
f620b43a BP |
671 | |
672 | /* We don't wait for bond->next_rebalance because rebalancing can only run | |
673 | * at a flow account checkpoint. ofproto does checkpointing on its own | |
674 | * schedule and bond_rebalance() gets called afterward, so we'd just be | |
675 | * waking up for no purpose. */ | |
676 | } | |
677 | \f | |
678 | /* MAC learning table interaction. */ | |
679 | ||
680 | static bool | |
681 | may_send_learning_packets(const struct bond *bond) | |
682 | { | |
9dd165e0 RK |
683 | return ((bond->lacp_status == LACP_DISABLED |
684 | && (bond->balance == BM_SLB || bond->balance == BM_AB)) | |
685 | || (bond->lacp_fallback_ab && bond->lacp_status == LACP_CONFIGURED)) | |
bdebeece | 686 | && bond->active_slave; |
f620b43a BP |
687 | } |
688 | ||
689 | /* Returns true if 'bond' needs the client to send out packets to assist with | |
690 | * MAC learning on 'bond'. If this function returns true, then the client | |
691 | * should iterate through its MAC learning table for the bridge on which 'bond' | |
692 | * is located. For each MAC that has been learned on a port other than 'bond', | |
ea131871 | 693 | * it should call bond_compose_learning_packet(). |
f620b43a | 694 | * |
477879ea BP |
695 | * This function will only return true if 'bond' is in SLB or active-backup |
696 | * mode and LACP is not negotiated. Otherwise sending learning packets isn't | |
697 | * necessary. | |
f620b43a BP |
698 | * |
699 | * Calling this function resets the state that it checks. */ | |
700 | bool | |
701 | bond_should_send_learning_packets(struct bond *bond) | |
702 | { | |
3bfd3972 EJ |
703 | bool send; |
704 | ||
705 | ovs_rwlock_wrlock(&rwlock); | |
706 | send = bond->send_learning_packets && may_send_learning_packets(bond); | |
f620b43a | 707 | bond->send_learning_packets = false; |
3bfd3972 | 708 | ovs_rwlock_unlock(&rwlock); |
f620b43a BP |
709 | return send; |
710 | } | |
711 | ||
712 | /* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'. | |
713 | * | |
ea131871 JG |
714 | * See bond_should_send_learning_packets() for description of usage. The |
715 | * caller should send the composed packet on the port associated with | |
716 | * port_aux and takes ownership of the returned ofpbuf. */ | |
cf62fa4c | 717 | struct dp_packet * |
74ff3298 | 718 | bond_compose_learning_packet(struct bond *bond, const struct eth_addr eth_src, |
ea131871 | 719 | uint16_t vlan, void **port_aux) |
f620b43a BP |
720 | { |
721 | struct bond_slave *slave; | |
cf62fa4c | 722 | struct dp_packet *packet; |
f620b43a | 723 | struct flow flow; |
f620b43a | 724 | |
3bfd3972 | 725 | ovs_rwlock_rdlock(&rwlock); |
cb22974d | 726 | ovs_assert(may_send_learning_packets(bond)); |
f620b43a | 727 | memset(&flow, 0, sizeof flow); |
74ff3298 | 728 | flow.dl_src = eth_src; |
4a1b8f30 | 729 | slave = choose_output_slave(bond, &flow, NULL, vlan); |
f620b43a | 730 | |
cf62fa4c | 731 | packet = dp_packet_new(0); |
2ea838ac | 732 | compose_rarp(packet, eth_src); |
f620b43a | 733 | if (vlan) { |
1bf02876 | 734 | eth_push_vlan(packet, htons(ETH_TYPE_VLAN), htons(vlan)); |
f620b43a | 735 | } |
f620b43a | 736 | |
ea131871 | 737 | *port_aux = slave->aux; |
3bfd3972 | 738 | ovs_rwlock_unlock(&rwlock); |
ea131871 | 739 | return packet; |
f620b43a BP |
740 | } |
741 | \f | |
742 | /* Checks whether a packet that arrived on 'slave_' within 'bond', with an | |
743 | * Ethernet destination address of 'eth_dst', should be admitted. | |
744 | * | |
745 | * The return value is one of the following: | |
746 | * | |
747 | * - BV_ACCEPT: Admit the packet. | |
748 | * | |
749 | * - BV_DROP: Drop the packet. | |
750 | * | |
751 | * - BV_DROP_IF_MOVED: Consult the MAC learning table for the packet's | |
752 | * Ethernet source address and VLAN. If there is none, or if the packet | |
753 | * is on the learned port, then admit the packet. If a different port has | |
754 | * been learned, however, drop the packet (and do not use it for MAC | |
755 | * learning). | |
756 | */ | |
757 | enum bond_verdict | |
758 | bond_check_admissibility(struct bond *bond, const void *slave_, | |
74ff3298 | 759 | const struct eth_addr eth_dst) |
f620b43a | 760 | { |
3bfd3972 EJ |
761 | enum bond_verdict verdict = BV_DROP; |
762 | struct bond_slave *slave; | |
9a1c6450 | 763 | |
3bfd3972 EJ |
764 | ovs_rwlock_rdlock(&rwlock); |
765 | slave = bond_slave_lookup(bond, slave_); | |
4222bbc8 | 766 | if (!slave) { |
3bfd3972 | 767 | goto out; |
4222bbc8 EJ |
768 | } |
769 | ||
9a1c6450 EJ |
770 | /* LACP bonds have very loose admissibility restrictions because we can |
771 | * assume the remote switch is aware of the bond and will "do the right | |
772 | * thing". However, as a precaution we drop packets on disabled slaves | |
773 | * because no correctly implemented partner switch should be sending | |
bdebeece EJ |
774 | * packets to them. |
775 | * | |
776 | * If LACP is configured, but LACP negotiations have been unsuccessful, we | |
9dd165e0 | 777 | * drop all incoming traffic except if lacp_fallback_ab is enabled. */ |
bdebeece | 778 | switch (bond->lacp_status) { |
3bfd3972 EJ |
779 | case LACP_NEGOTIATED: |
780 | verdict = slave->enabled ? BV_ACCEPT : BV_DROP; | |
781 | goto out; | |
782 | case LACP_CONFIGURED: | |
9dd165e0 RK |
783 | if (!bond->lacp_fallback_ab) { |
784 | goto out; | |
785 | } | |
3bfd3972 EJ |
786 | case LACP_DISABLED: |
787 | break; | |
f620b43a BP |
788 | } |
789 | ||
790 | /* Drop all multicast packets on inactive slaves. */ | |
791 | if (eth_addr_is_multicast(eth_dst)) { | |
4222bbc8 | 792 | if (bond->active_slave != slave) { |
3bfd3972 | 793 | goto out; |
f620b43a BP |
794 | } |
795 | } | |
796 | ||
f931a4c9 | 797 | switch (bond->balance) { |
9dd165e0 RK |
798 | case BM_TCP: |
799 | /* TCP balanced bonds require successful LACP negotiations. Based on the | |
800 | * above check, LACP is off or lacp_fallback_ab is true on this bond. | |
801 | * If lacp_fallback_ab is true fall through to BM_AB case else, we | |
802 | * drop all incoming traffic. */ | |
803 | if (!bond->lacp_fallback_ab) { | |
804 | goto out; | |
805 | } | |
806 | ||
f931a4c9 BP |
807 | case BM_AB: |
808 | /* Drop all packets which arrive on backup slaves. This is similar to | |
809 | * how Linux bonding handles active-backup bonds. */ | |
7ba7dcf0 EJ |
810 | if (bond->active_slave != slave) { |
811 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
812 | ||
e6b2255c BP |
813 | VLOG_DBG_RL(&rl, "active-backup bond received packet on backup" |
814 | " slave (%s) destined for " ETH_ADDR_FMT, | |
815 | slave->name, ETH_ADDR_ARGS(eth_dst)); | |
3bfd3972 | 816 | goto out; |
7ba7dcf0 | 817 | } |
3bfd3972 EJ |
818 | verdict = BV_ACCEPT; |
819 | goto out; | |
f931a4c9 | 820 | |
f931a4c9 BP |
821 | case BM_SLB: |
822 | /* Drop all packets for which we have learned a different input port, | |
823 | * because we probably sent the packet on one slave and got it back on | |
824 | * the other. Gratuitous ARP packets are an exception to this rule: | |
825 | * the host has moved to another switch. The exception to the | |
826 | * exception is if we locked the learning table to avoid reflections on | |
827 | * bond slaves. */ | |
3bfd3972 EJ |
828 | verdict = BV_DROP_IF_MOVED; |
829 | goto out; | |
7ba7dcf0 EJ |
830 | } |
831 | ||
428b2edd | 832 | OVS_NOT_REACHED(); |
3bfd3972 EJ |
833 | out: |
834 | ovs_rwlock_unlock(&rwlock); | |
835 | return verdict; | |
836 | ||
f620b43a BP |
837 | } |
838 | ||
839 | /* Returns the slave (registered on 'bond' by bond_slave_register()) to which | |
840 | * a packet with the given 'flow' and 'vlan' should be forwarded. Returns | |
841 | * NULL if the packet should be dropped because no slaves are enabled. | |
842 | * | |
843 | * 'vlan' is not necessarily the same as 'flow->vlan_tci'. First, 'vlan' | |
844 | * should be a VID only (i.e. excluding the PCP bits). Second, | |
845 | * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be | |
846 | * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the | |
847 | * packet belongs to (so for an access port it will be the access port's VLAN). | |
848 | * | |
bcd2633a JP |
849 | * If 'wc' is non-NULL, bitwise-OR's 'wc' with the set of bits that were |
850 | * significant in the selection. At some point earlier, 'wc' should | |
851 | * have been initialized (e.g., by flow_wildcards_init_catchall()). | |
f620b43a BP |
852 | */ |
853 | void * | |
854 | bond_choose_output_slave(struct bond *bond, const struct flow *flow, | |
4a1b8f30 | 855 | struct flow_wildcards *wc, uint16_t vlan) |
f620b43a | 856 | { |
3bfd3972 | 857 | struct bond_slave *slave; |
b5d5d7d3 | 858 | void *aux; |
3bfd3972 EJ |
859 | |
860 | ovs_rwlock_rdlock(&rwlock); | |
4a1b8f30 | 861 | slave = choose_output_slave(bond, flow, wc, vlan); |
b5d5d7d3 | 862 | aux = slave ? slave->aux : NULL; |
3bfd3972 | 863 | ovs_rwlock_unlock(&rwlock); |
b5d5d7d3 AW |
864 | |
865 | return aux; | |
f620b43a | 866 | } |
f620b43a | 867 | \f |
adcf00ba AZ |
868 | /* Recirculation. */ |
869 | static void | |
870 | bond_entry_account(struct bond_entry *entry, uint64_t rule_tx_bytes) | |
c6855ec5 | 871 | OVS_REQ_WRLOCK(rwlock) |
adcf00ba AZ |
872 | { |
873 | if (entry->slave) { | |
874 | uint64_t delta; | |
875 | ||
876 | delta = rule_tx_bytes - entry->pr_tx_bytes; | |
877 | entry->tx_bytes += delta; | |
878 | entry->pr_tx_bytes = rule_tx_bytes; | |
879 | } | |
880 | } | |
881 | ||
882 | /* Maintain bond stats using post recirculation rule byte counters.*/ | |
60cda7d6 | 883 | static void |
adcf00ba | 884 | bond_recirculation_account(struct bond *bond) |
80316557 | 885 | OVS_REQ_WRLOCK(rwlock) |
adcf00ba AZ |
886 | { |
887 | int i; | |
888 | ||
adcf00ba AZ |
889 | for (i=0; i<=BOND_MASK; i++) { |
890 | struct bond_entry *entry = &bond->hash[i]; | |
891 | struct rule *rule = entry->pr_rule; | |
892 | ||
893 | if (rule) { | |
894 | uint64_t n_packets OVS_UNUSED; | |
895 | long long int used OVS_UNUSED; | |
896 | uint64_t n_bytes; | |
897 | ||
898 | rule->ofproto->ofproto_class->rule_get_stats( | |
899 | rule, &n_packets, &n_bytes, &used); | |
900 | bond_entry_account(entry, n_bytes); | |
901 | } | |
902 | } | |
adcf00ba AZ |
903 | } |
904 | ||
905 | bool | |
906 | bond_may_recirc(const struct bond *bond, uint32_t *recirc_id, | |
907 | uint32_t *hash_bias) | |
908 | { | |
80316557 | 909 | if (bond->balance == BM_TCP && bond->recirc_id) { |
adcf00ba AZ |
910 | if (recirc_id) { |
911 | *recirc_id = bond->recirc_id; | |
912 | } | |
913 | if (hash_bias) { | |
914 | *hash_bias = bond->basis; | |
915 | } | |
916 | return true; | |
917 | } else { | |
918 | return false; | |
919 | } | |
920 | } | |
921 | ||
ca8127fd AZ |
922 | static void |
923 | bond_update_post_recirc_rules__(struct bond* bond, const bool force) | |
924 | OVS_REQ_WRLOCK(rwlock) | |
adcf00ba AZ |
925 | { |
926 | struct bond_entry *e; | |
927 | bool update_rules = force; /* Always update rules if caller forces it. */ | |
928 | ||
929 | /* Make sure all bond entries are populated */ | |
930 | for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) { | |
931 | if (!e->slave || !e->slave->enabled) { | |
932 | update_rules = true; | |
933 | e->slave = CONTAINER_OF(hmap_random_node(&bond->slaves), | |
934 | struct bond_slave, hmap_node); | |
935 | if (!e->slave->enabled) { | |
936 | e->slave = bond->active_slave; | |
937 | } | |
938 | } | |
939 | } | |
940 | ||
941 | if (update_rules) { | |
942 | update_recirc_rules(bond); | |
943 | } | |
944 | } | |
ca8127fd AZ |
945 | |
946 | void | |
947 | bond_update_post_recirc_rules(struct bond* bond, const bool force) | |
948 | { | |
949 | ovs_rwlock_wrlock(&rwlock); | |
950 | bond_update_post_recirc_rules__(bond, force); | |
951 | ovs_rwlock_unlock(&rwlock); | |
952 | } | |
adcf00ba | 953 | \f |
f620b43a BP |
954 | /* Rebalancing. */ |
955 | ||
1b137691 | 956 | static bool |
3bfd3972 | 957 | bond_is_balanced(const struct bond *bond) OVS_REQ_RDLOCK(rwlock) |
1b137691 | 958 | { |
bc1b010c EJ |
959 | return bond->rebalance_interval |
960 | && (bond->balance == BM_SLB || bond->balance == BM_TCP); | |
1b137691 EJ |
961 | } |
962 | ||
f620b43a BP |
963 | /* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */ |
964 | void | |
965 | bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan, | |
966 | uint64_t n_bytes) | |
967 | { | |
3bfd3972 | 968 | ovs_rwlock_wrlock(&rwlock); |
1b137691 | 969 | if (bond_is_balanced(bond)) { |
f620b43a | 970 | lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes; |
f620b43a | 971 | } |
3bfd3972 | 972 | ovs_rwlock_unlock(&rwlock); |
f620b43a BP |
973 | } |
974 | ||
975 | static struct bond_slave * | |
ca6ba700 | 976 | bond_slave_from_bal_node(struct ovs_list *bal) OVS_REQ_RDLOCK(rwlock) |
f620b43a BP |
977 | { |
978 | return CONTAINER_OF(bal, struct bond_slave, bal_node); | |
979 | } | |
980 | ||
981 | static void | |
ca6ba700 | 982 | log_bals(struct bond *bond, const struct ovs_list *bals) |
c6855ec5 | 983 | OVS_REQ_RDLOCK(rwlock) |
f620b43a BP |
984 | { |
985 | if (VLOG_IS_DBG_ENABLED()) { | |
986 | struct ds ds = DS_EMPTY_INITIALIZER; | |
987 | const struct bond_slave *slave; | |
988 | ||
989 | LIST_FOR_EACH (slave, bal_node, bals) { | |
990 | if (ds.length) { | |
991 | ds_put_char(&ds, ','); | |
992 | } | |
993 | ds_put_format(&ds, " %s %"PRIu64"kB", | |
994 | slave->name, slave->tx_bytes / 1024); | |
995 | ||
996 | if (!slave->enabled) { | |
997 | ds_put_cstr(&ds, " (disabled)"); | |
998 | } | |
417e7e66 | 999 | if (!ovs_list_is_empty(&slave->entries)) { |
f620b43a BP |
1000 | struct bond_entry *e; |
1001 | ||
1002 | ds_put_cstr(&ds, " ("); | |
1003 | LIST_FOR_EACH (e, list_node, &slave->entries) { | |
417e7e66 | 1004 | if (&e->list_node != ovs_list_front(&slave->entries)) { |
f620b43a BP |
1005 | ds_put_cstr(&ds, " + "); |
1006 | } | |
34582733 | 1007 | ds_put_format(&ds, "h%"PRIdPTR": %"PRIu64"kB", |
f620b43a BP |
1008 | e - bond->hash, e->tx_bytes / 1024); |
1009 | } | |
1010 | ds_put_cstr(&ds, ")"); | |
1011 | } | |
1012 | } | |
1013 | VLOG_DBG("bond %s:%s", bond->name, ds_cstr(&ds)); | |
1014 | ds_destroy(&ds); | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | /* Shifts 'hash' from its current slave to 'to'. */ | |
1019 | static void | |
4a1b8f30 | 1020 | bond_shift_load(struct bond_entry *hash, struct bond_slave *to) |
c6855ec5 | 1021 | OVS_REQ_WRLOCK(rwlock) |
f620b43a BP |
1022 | { |
1023 | struct bond_slave *from = hash->slave; | |
1024 | struct bond *bond = from->bond; | |
1025 | uint64_t delta = hash->tx_bytes; | |
1026 | ||
34582733 | 1027 | VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %"PRIdPTR") " |
f620b43a BP |
1028 | "from %s to %s (now carrying %"PRIu64"kB and " |
1029 | "%"PRIu64"kB load, respectively)", | |
1030 | bond->name, delta / 1024, hash - bond->hash, | |
1031 | from->name, to->name, | |
1032 | (from->tx_bytes - delta) / 1024, | |
1033 | (to->tx_bytes + delta) / 1024); | |
1034 | ||
1035 | /* Shift load away from 'from' to 'to'. */ | |
1036 | from->tx_bytes -= delta; | |
1037 | to->tx_bytes += delta; | |
1038 | ||
1039 | /* Arrange for flows to be revalidated. */ | |
dc30ea2d | 1040 | hash->slave = to; |
4a1b8f30 | 1041 | bond->bond_revalidate = true; |
f620b43a BP |
1042 | } |
1043 | ||
09a5d390 BP |
1044 | /* Picks and returns a bond_entry to migrate from 'from' (the most heavily |
1045 | * loaded bond slave) to a bond slave that has 'to_tx_bytes' bytes of load, | |
f620b43a BP |
1046 | * given that doing so must decrease the ratio of the load on the two slaves by |
1047 | * at least 0.1. Returns NULL if there is no appropriate entry. | |
1048 | * | |
1049 | * The list of entries isn't sorted. I don't know of a reason to prefer to | |
1050 | * shift away small hashes or large hashes. */ | |
1051 | static struct bond_entry * | |
1052 | choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes) | |
c6855ec5 | 1053 | OVS_REQ_WRLOCK(rwlock) |
f620b43a BP |
1054 | { |
1055 | struct bond_entry *e; | |
1056 | ||
417e7e66 | 1057 | if (ovs_list_is_short(&from->entries)) { |
f620b43a BP |
1058 | /* 'from' carries no more than one MAC hash, so shifting load away from |
1059 | * it would be pointless. */ | |
1060 | return NULL; | |
1061 | } | |
1062 | ||
1063 | LIST_FOR_EACH (e, list_node, &from->entries) { | |
c460a6a7 AZ |
1064 | uint64_t delta = e->tx_bytes; /* The amount to rebalance. */ |
1065 | uint64_t ideal_tx_bytes = (from->tx_bytes + to_tx_bytes)/2; | |
1066 | /* Note, the ideal traffic is the mid point | |
1067 | * between 'from' and 'to'. This value does | |
1068 | * not change by rebalancing. */ | |
1069 | uint64_t new_low; /* The lower bandwidth between 'to' and 'from' | |
1070 | after rebalancing. */ | |
1071 | ||
1072 | new_low = MIN(from->tx_bytes - delta, to_tx_bytes + delta); | |
1073 | ||
1074 | if ((new_low > to_tx_bytes) && | |
1075 | (new_low - to_tx_bytes >= (ideal_tx_bytes - to_tx_bytes) / 10)) { | |
1076 | /* Only rebalance if the new 'low' is closer to to the mid point, | |
1077 | * and the improvement exceeds 10% of current traffic | |
1078 | * deviation from the ideal split. | |
1079 | * | |
1080 | * The improvement on the 'high' side is always the same as the | |
1081 | * 'low' side. Thus consider 'low' side is sufficient. */ | |
f620b43a BP |
1082 | return e; |
1083 | } | |
1084 | } | |
1085 | ||
1086 | return NULL; | |
1087 | } | |
1088 | ||
1089 | /* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is | |
1090 | * maintained. */ | |
1091 | static void | |
ca6ba700 | 1092 | insert_bal(struct ovs_list *bals, struct bond_slave *slave) |
f620b43a BP |
1093 | { |
1094 | struct bond_slave *pos; | |
1095 | ||
1096 | LIST_FOR_EACH (pos, bal_node, bals) { | |
1097 | if (slave->tx_bytes > pos->tx_bytes) { | |
1098 | break; | |
1099 | } | |
1100 | } | |
417e7e66 | 1101 | ovs_list_insert(&pos->bal_node, &slave->bal_node); |
f620b43a BP |
1102 | } |
1103 | ||
1104 | /* Removes 'slave' from its current list and then inserts it into 'bals' so | |
1105 | * that descending order of 'tx_bytes' is maintained. */ | |
1106 | static void | |
ca6ba700 | 1107 | reinsert_bal(struct ovs_list *bals, struct bond_slave *slave) |
f620b43a | 1108 | { |
417e7e66 | 1109 | ovs_list_remove(&slave->bal_node); |
f620b43a BP |
1110 | insert_bal(bals, slave); |
1111 | } | |
1112 | ||
1113 | /* If 'bond' needs rebalancing, does so. | |
1114 | * | |
adcf00ba AZ |
1115 | * The caller should have called bond_account() for each active flow, or in case |
1116 | * of recirculation is used, have called bond_recirculation_account(bond), | |
1117 | * to ensure that flow data is consistently accounted at this point. | |
60cda7d6 AZ |
1118 | */ |
1119 | void | |
4a1b8f30 | 1120 | bond_rebalance(struct bond *bond) |
f620b43a BP |
1121 | { |
1122 | struct bond_slave *slave; | |
1123 | struct bond_entry *e; | |
ca6ba700 | 1124 | struct ovs_list bals; |
adcf00ba | 1125 | bool rebalanced = false; |
60cda7d6 | 1126 | bool use_recirc; |
f620b43a | 1127 | |
3bfd3972 | 1128 | ovs_rwlock_wrlock(&rwlock); |
1b137691 | 1129 | if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) { |
adcf00ba | 1130 | goto done; |
f620b43a BP |
1131 | } |
1132 | bond->next_rebalance = time_msec() + bond->rebalance_interval; | |
1133 | ||
2494ccd7 | 1134 | use_recirc = ofproto_dpif_get_support(bond->ofproto)->odp.recirc && |
60cda7d6 AZ |
1135 | bond_may_recirc(bond, NULL, NULL); |
1136 | ||
1137 | if (use_recirc) { | |
1138 | bond_recirculation_account(bond); | |
1139 | } | |
1140 | ||
f620b43a BP |
1141 | /* Add each bond_entry to its slave's 'entries' list. |
1142 | * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */ | |
1143 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { | |
1144 | slave->tx_bytes = 0; | |
417e7e66 | 1145 | ovs_list_init(&slave->entries); |
f620b43a BP |
1146 | } |
1147 | for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) { | |
1148 | if (e->slave && e->tx_bytes) { | |
1149 | e->slave->tx_bytes += e->tx_bytes; | |
417e7e66 | 1150 | ovs_list_push_back(&e->slave->entries, &e->list_node); |
f620b43a BP |
1151 | } |
1152 | } | |
1153 | ||
1154 | /* Add enabled slaves to 'bals' in descending order of tx_bytes. | |
1155 | * | |
1156 | * XXX This is O(n**2) in the number of slaves but it could be O(n lg n) | |
1157 | * with a proper list sort algorithm. */ | |
417e7e66 | 1158 | ovs_list_init(&bals); |
f620b43a BP |
1159 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { |
1160 | if (slave->enabled) { | |
1161 | insert_bal(&bals, slave); | |
1162 | } | |
1163 | } | |
1164 | log_bals(bond, &bals); | |
1165 | ||
1166 | /* Shift load from the most-loaded slaves to the least-loaded slaves. */ | |
417e7e66 BW |
1167 | while (!ovs_list_is_short(&bals)) { |
1168 | struct bond_slave *from = bond_slave_from_bal_node(ovs_list_front(&bals)); | |
1169 | struct bond_slave *to = bond_slave_from_bal_node(ovs_list_back(&bals)); | |
f620b43a BP |
1170 | uint64_t overload; |
1171 | ||
1172 | overload = from->tx_bytes - to->tx_bytes; | |
1173 | if (overload < to->tx_bytes >> 5 || overload < 100000) { | |
1174 | /* The extra load on 'from' (and all less-loaded slaves), compared | |
1175 | * to that of 'to' (the least-loaded slave), is less than ~3%, or | |
1176 | * it is less than ~1Mbps. No point in rebalancing. */ | |
1177 | break; | |
1178 | } | |
1179 | ||
09a5d390 BP |
1180 | /* 'from' is carrying significantly more load than 'to'. Pick a hash |
1181 | * to move from 'from' to 'to'. */ | |
f620b43a BP |
1182 | e = choose_entry_to_migrate(from, to->tx_bytes); |
1183 | if (e) { | |
4a1b8f30 | 1184 | bond_shift_load(e, to); |
f620b43a BP |
1185 | |
1186 | /* Delete element from from->entries. | |
1187 | * | |
1188 | * We don't add the element to to->hashes. That would only allow | |
1189 | * 'e' to be migrated to another slave in this rebalancing run, and | |
1190 | * there is no point in doing that. */ | |
417e7e66 | 1191 | ovs_list_remove(&e->list_node); |
f620b43a BP |
1192 | |
1193 | /* Re-sort 'bals'. */ | |
1194 | reinsert_bal(&bals, from); | |
1195 | reinsert_bal(&bals, to); | |
60cda7d6 | 1196 | rebalanced = true; |
f620b43a BP |
1197 | } else { |
1198 | /* Can't usefully migrate anything away from 'from'. | |
1199 | * Don't reconsider it. */ | |
417e7e66 | 1200 | ovs_list_remove(&from->bal_node); |
f620b43a BP |
1201 | } |
1202 | } | |
1203 | ||
1204 | /* Implement exponentially weighted moving average. A weight of 1/2 causes | |
1205 | * historical data to decay to <1% in 7 rebalancing runs. 1,000,000 bytes | |
1206 | * take 20 rebalancing runs to decay to 0 and get deleted entirely. */ | |
1207 | for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) { | |
1208 | e->tx_bytes /= 2; | |
f620b43a | 1209 | } |
adcf00ba | 1210 | |
60cda7d6 | 1211 | if (use_recirc && rebalanced) { |
ca8127fd | 1212 | bond_update_post_recirc_rules__(bond,true); |
60cda7d6 | 1213 | } |
2f486d4c AZ |
1214 | |
1215 | done: | |
3bfd3972 | 1216 | ovs_rwlock_unlock(&rwlock); |
f620b43a BP |
1217 | } |
1218 | \f | |
1219 | /* Bonding unixctl user interface functions. */ | |
1220 | ||
1221 | static struct bond * | |
3bfd3972 | 1222 | bond_find(const char *name) OVS_REQ_RDLOCK(rwlock) |
f620b43a BP |
1223 | { |
1224 | struct bond *bond; | |
1225 | ||
1226 | HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0), | |
3bfd3972 | 1227 | all_bonds) { |
f620b43a BP |
1228 | if (!strcmp(bond->name, name)) { |
1229 | return bond; | |
1230 | } | |
1231 | } | |
1232 | return NULL; | |
1233 | } | |
1234 | ||
1235 | static struct bond_slave * | |
1236 | bond_lookup_slave(struct bond *bond, const char *slave_name) | |
1237 | { | |
1238 | struct bond_slave *slave; | |
1239 | ||
1240 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { | |
1241 | if (!strcmp(slave->name, slave_name)) { | |
1242 | return slave; | |
1243 | } | |
1244 | } | |
1245 | return NULL; | |
1246 | } | |
1247 | ||
1248 | static void | |
1249 | bond_unixctl_list(struct unixctl_conn *conn, | |
0e15264f BP |
1250 | int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, |
1251 | void *aux OVS_UNUSED) | |
f620b43a BP |
1252 | { |
1253 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1254 | const struct bond *bond; | |
1255 | ||
adcf00ba | 1256 | ds_put_cstr(&ds, "bond\ttype\trecircID\tslaves\n"); |
f620b43a | 1257 | |
3bfd3972 EJ |
1258 | ovs_rwlock_rdlock(&rwlock); |
1259 | HMAP_FOR_EACH (bond, hmap_node, all_bonds) { | |
f620b43a BP |
1260 | const struct bond_slave *slave; |
1261 | size_t i; | |
1262 | ||
adcf00ba AZ |
1263 | ds_put_format(&ds, "%s\t%s\t%d\t", bond->name, |
1264 | bond_mode_to_string(bond->balance), bond->recirc_id); | |
f620b43a BP |
1265 | |
1266 | i = 0; | |
1267 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { | |
1268 | if (i++ > 0) { | |
1269 | ds_put_cstr(&ds, ", "); | |
1270 | } | |
1271 | ds_put_cstr(&ds, slave->name); | |
1272 | } | |
1273 | ds_put_char(&ds, '\n'); | |
1274 | } | |
3bfd3972 | 1275 | ovs_rwlock_unlock(&rwlock); |
bde9f75d | 1276 | unixctl_command_reply(conn, ds_cstr(&ds)); |
f620b43a BP |
1277 | ds_destroy(&ds); |
1278 | } | |
1279 | ||
1280 | static void | |
c33a8a25 | 1281 | bond_print_details(struct ds *ds, const struct bond *bond) |
3bfd3972 | 1282 | OVS_REQ_RDLOCK(rwlock) |
f620b43a | 1283 | { |
fc1d4f01 EJ |
1284 | struct shash slave_shash = SHASH_INITIALIZER(&slave_shash); |
1285 | const struct shash_node **sorted_slaves = NULL; | |
f620b43a | 1286 | const struct bond_slave *slave; |
adcf00ba AZ |
1287 | bool may_recirc; |
1288 | uint32_t recirc_id; | |
fc1d4f01 | 1289 | int i; |
f620b43a | 1290 | |
c33a8a25 EJ |
1291 | ds_put_format(ds, "---- %s ----\n", bond->name); |
1292 | ds_put_format(ds, "bond_mode: %s\n", | |
f620b43a BP |
1293 | bond_mode_to_string(bond->balance)); |
1294 | ||
adcf00ba AZ |
1295 | may_recirc = bond_may_recirc(bond, &recirc_id, NULL); |
1296 | ds_put_format(ds, "bond may use recirculation: %s, Recirc-ID : %d\n", | |
1297 | may_recirc ? "yes" : "no", may_recirc ? recirc_id: -1); | |
1298 | ||
c33a8a25 | 1299 | ds_put_format(ds, "bond-hash-basis: %"PRIu32"\n", bond->basis); |
672d18b2 | 1300 | |
c33a8a25 EJ |
1301 | ds_put_format(ds, "updelay: %d ms\n", bond->updelay); |
1302 | ds_put_format(ds, "downdelay: %d ms\n", bond->downdelay); | |
f620b43a | 1303 | |
1b137691 | 1304 | if (bond_is_balanced(bond)) { |
c33a8a25 | 1305 | ds_put_format(ds, "next rebalance: %lld ms\n", |
f620b43a BP |
1306 | bond->next_rebalance - time_msec()); |
1307 | } | |
1308 | ||
bdebeece EJ |
1309 | ds_put_cstr(ds, "lacp_status: "); |
1310 | switch (bond->lacp_status) { | |
1311 | case LACP_NEGOTIATED: | |
1312 | ds_put_cstr(ds, "negotiated\n"); | |
1313 | break; | |
1314 | case LACP_CONFIGURED: | |
1315 | ds_put_cstr(ds, "configured\n"); | |
1316 | break; | |
1317 | case LACP_DISABLED: | |
1318 | ds_put_cstr(ds, "off\n"); | |
1319 | break; | |
1320 | default: | |
1321 | ds_put_cstr(ds, "<unknown>\n"); | |
1322 | break; | |
1323 | } | |
4d6fb5eb | 1324 | |
3e5aeeb5 AZ |
1325 | ds_put_cstr(ds, "active slave mac: "); |
1326 | ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(bond->active_slave_mac)); | |
1327 | slave = bond_find_slave_by_mac(bond, bond->active_slave_mac); | |
1328 | ds_put_format(ds,"(%s)\n", slave ? slave->name : "none"); | |
1329 | ||
f620b43a | 1330 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { |
fc1d4f01 EJ |
1331 | shash_add(&slave_shash, slave->name, slave); |
1332 | } | |
1333 | sorted_slaves = shash_sort(&slave_shash); | |
1334 | ||
1335 | for (i = 0; i < shash_count(&slave_shash); i++) { | |
f620b43a | 1336 | struct bond_entry *be; |
f620b43a | 1337 | |
fc1d4f01 EJ |
1338 | slave = sorted_slaves[i]->data; |
1339 | ||
f620b43a | 1340 | /* Basic info. */ |
c33a8a25 | 1341 | ds_put_format(ds, "\nslave %s: %s\n", |
f620b43a BP |
1342 | slave->name, slave->enabled ? "enabled" : "disabled"); |
1343 | if (slave == bond->active_slave) { | |
c33a8a25 | 1344 | ds_put_cstr(ds, "\tactive slave\n"); |
f620b43a BP |
1345 | } |
1346 | if (slave->delay_expires != LLONG_MAX) { | |
c33a8a25 | 1347 | ds_put_format(ds, "\t%s expires in %lld ms\n", |
f620b43a BP |
1348 | slave->enabled ? "downdelay" : "updelay", |
1349 | slave->delay_expires - time_msec()); | |
1350 | } | |
1351 | ||
c33a8a25 | 1352 | ds_put_format(ds, "\tmay_enable: %s\n", |
296f6519 | 1353 | slave->may_enable ? "true" : "false"); |
4d6fb5eb | 1354 | |
1b137691 | 1355 | if (!bond_is_balanced(bond)) { |
f620b43a BP |
1356 | continue; |
1357 | } | |
1358 | ||
1359 | /* Hashes. */ | |
f620b43a BP |
1360 | for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) { |
1361 | int hash = be - bond->hash; | |
f6ba1f35 | 1362 | uint64_t be_tx_k; |
f620b43a BP |
1363 | |
1364 | if (be->slave != slave) { | |
1365 | continue; | |
1366 | } | |
1367 | ||
f6ba1f35 AZ |
1368 | be_tx_k = be->tx_bytes / 1024; |
1369 | if (be_tx_k) { | |
1370 | ds_put_format(ds, "\thash %d: %"PRIu64" kB load\n", | |
1371 | hash, be_tx_k); | |
1372 | } | |
f620b43a | 1373 | |
7b9f1974 | 1374 | /* XXX How can we list the MACs assigned to hashes of SLB bonds? */ |
f620b43a BP |
1375 | } |
1376 | } | |
fc1d4f01 EJ |
1377 | shash_destroy(&slave_shash); |
1378 | free(sorted_slaves); | |
c33a8a25 EJ |
1379 | ds_put_cstr(ds, "\n"); |
1380 | } | |
1381 | ||
1382 | static void | |
1383 | bond_unixctl_show(struct unixctl_conn *conn, | |
1384 | int argc, const char *argv[], | |
1385 | void *aux OVS_UNUSED) | |
1386 | { | |
1387 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1388 | ||
3bfd3972 | 1389 | ovs_rwlock_rdlock(&rwlock); |
c33a8a25 EJ |
1390 | if (argc > 1) { |
1391 | const struct bond *bond = bond_find(argv[1]); | |
1392 | ||
1393 | if (!bond) { | |
bde9f75d | 1394 | unixctl_command_reply_error(conn, "no such bond"); |
3bfd3972 | 1395 | goto out; |
c33a8a25 EJ |
1396 | } |
1397 | bond_print_details(&ds, bond); | |
1398 | } else { | |
1399 | const struct bond *bond; | |
1400 | ||
3bfd3972 | 1401 | HMAP_FOR_EACH (bond, hmap_node, all_bonds) { |
c33a8a25 EJ |
1402 | bond_print_details(&ds, bond); |
1403 | } | |
1404 | } | |
1405 | ||
bde9f75d | 1406 | unixctl_command_reply(conn, ds_cstr(&ds)); |
f620b43a | 1407 | ds_destroy(&ds); |
3bfd3972 EJ |
1408 | |
1409 | out: | |
1410 | ovs_rwlock_unlock(&rwlock); | |
f620b43a BP |
1411 | } |
1412 | ||
1413 | static void | |
0e15264f BP |
1414 | bond_unixctl_migrate(struct unixctl_conn *conn, |
1415 | int argc OVS_UNUSED, const char *argv[], | |
f620b43a BP |
1416 | void *aux OVS_UNUSED) |
1417 | { | |
0e15264f BP |
1418 | const char *bond_s = argv[1]; |
1419 | const char *hash_s = argv[2]; | |
1420 | const char *slave_s = argv[3]; | |
f620b43a BP |
1421 | struct bond *bond; |
1422 | struct bond_slave *slave; | |
1423 | struct bond_entry *entry; | |
1424 | int hash; | |
1425 | ||
3bfd3972 | 1426 | ovs_rwlock_wrlock(&rwlock); |
f620b43a BP |
1427 | bond = bond_find(bond_s); |
1428 | if (!bond) { | |
bde9f75d | 1429 | unixctl_command_reply_error(conn, "no such bond"); |
3bfd3972 | 1430 | goto out; |
f620b43a BP |
1431 | } |
1432 | ||
1433 | if (bond->balance != BM_SLB) { | |
bde9f75d | 1434 | unixctl_command_reply_error(conn, "not an SLB bond"); |
3bfd3972 | 1435 | goto out; |
f620b43a BP |
1436 | } |
1437 | ||
1438 | if (strspn(hash_s, "0123456789") == strlen(hash_s)) { | |
1439 | hash = atoi(hash_s) & BOND_MASK; | |
1440 | } else { | |
bde9f75d | 1441 | unixctl_command_reply_error(conn, "bad hash"); |
3bfd3972 | 1442 | goto out; |
f620b43a BP |
1443 | } |
1444 | ||
1445 | slave = bond_lookup_slave(bond, slave_s); | |
1446 | if (!slave) { | |
bde9f75d | 1447 | unixctl_command_reply_error(conn, "no such slave"); |
3bfd3972 | 1448 | goto out; |
f620b43a BP |
1449 | } |
1450 | ||
1451 | if (!slave->enabled) { | |
bde9f75d | 1452 | unixctl_command_reply_error(conn, "cannot migrate to disabled slave"); |
3bfd3972 | 1453 | goto out; |
f620b43a BP |
1454 | } |
1455 | ||
1456 | entry = &bond->hash[hash]; | |
4a1b8f30 | 1457 | bond->bond_revalidate = true; |
f620b43a | 1458 | entry->slave = slave; |
bde9f75d | 1459 | unixctl_command_reply(conn, "migrated"); |
3bfd3972 EJ |
1460 | |
1461 | out: | |
1462 | ovs_rwlock_unlock(&rwlock); | |
f620b43a BP |
1463 | } |
1464 | ||
1465 | static void | |
0e15264f BP |
1466 | bond_unixctl_set_active_slave(struct unixctl_conn *conn, |
1467 | int argc OVS_UNUSED, const char *argv[], | |
f620b43a BP |
1468 | void *aux OVS_UNUSED) |
1469 | { | |
0e15264f BP |
1470 | const char *bond_s = argv[1]; |
1471 | const char *slave_s = argv[2]; | |
f620b43a BP |
1472 | struct bond *bond; |
1473 | struct bond_slave *slave; | |
1474 | ||
3bfd3972 | 1475 | ovs_rwlock_wrlock(&rwlock); |
f620b43a BP |
1476 | bond = bond_find(bond_s); |
1477 | if (!bond) { | |
bde9f75d | 1478 | unixctl_command_reply_error(conn, "no such bond"); |
3bfd3972 | 1479 | goto out; |
f620b43a BP |
1480 | } |
1481 | ||
1482 | slave = bond_lookup_slave(bond, slave_s); | |
1483 | if (!slave) { | |
bde9f75d | 1484 | unixctl_command_reply_error(conn, "no such slave"); |
3bfd3972 | 1485 | goto out; |
f620b43a BP |
1486 | } |
1487 | ||
1488 | if (!slave->enabled) { | |
bde9f75d | 1489 | unixctl_command_reply_error(conn, "cannot make disabled slave active"); |
3bfd3972 | 1490 | goto out; |
f620b43a BP |
1491 | } |
1492 | ||
1493 | if (bond->active_slave != slave) { | |
4a1b8f30 | 1494 | bond->bond_revalidate = true; |
f620b43a | 1495 | bond->active_slave = slave; |
f620b43a BP |
1496 | VLOG_INFO("bond %s: active interface is now %s", |
1497 | bond->name, slave->name); | |
1498 | bond->send_learning_packets = true; | |
bde9f75d | 1499 | unixctl_command_reply(conn, "done"); |
3e5aeeb5 | 1500 | bond_active_slave_changed(bond); |
f620b43a | 1501 | } else { |
bde9f75d | 1502 | unixctl_command_reply(conn, "no change"); |
f620b43a | 1503 | } |
3bfd3972 EJ |
1504 | out: |
1505 | ovs_rwlock_unlock(&rwlock); | |
f620b43a BP |
1506 | } |
1507 | ||
1508 | static void | |
0e15264f | 1509 | enable_slave(struct unixctl_conn *conn, const char *argv[], bool enable) |
f620b43a | 1510 | { |
0e15264f BP |
1511 | const char *bond_s = argv[1]; |
1512 | const char *slave_s = argv[2]; | |
f620b43a BP |
1513 | struct bond *bond; |
1514 | struct bond_slave *slave; | |
1515 | ||
3bfd3972 | 1516 | ovs_rwlock_wrlock(&rwlock); |
f620b43a BP |
1517 | bond = bond_find(bond_s); |
1518 | if (!bond) { | |
bde9f75d | 1519 | unixctl_command_reply_error(conn, "no such bond"); |
3bfd3972 | 1520 | goto out; |
f620b43a BP |
1521 | } |
1522 | ||
1523 | slave = bond_lookup_slave(bond, slave_s); | |
1524 | if (!slave) { | |
bde9f75d | 1525 | unixctl_command_reply_error(conn, "no such slave"); |
3bfd3972 | 1526 | goto out; |
f620b43a BP |
1527 | } |
1528 | ||
4a1b8f30 | 1529 | bond_enable_slave(slave, enable); |
bde9f75d | 1530 | unixctl_command_reply(conn, enable ? "enabled" : "disabled"); |
3bfd3972 EJ |
1531 | |
1532 | out: | |
1533 | ovs_rwlock_unlock(&rwlock); | |
f620b43a BP |
1534 | } |
1535 | ||
1536 | static void | |
0e15264f BP |
1537 | bond_unixctl_enable_slave(struct unixctl_conn *conn, |
1538 | int argc OVS_UNUSED, const char *argv[], | |
f620b43a BP |
1539 | void *aux OVS_UNUSED) |
1540 | { | |
0e15264f | 1541 | enable_slave(conn, argv, true); |
f620b43a BP |
1542 | } |
1543 | ||
1544 | static void | |
0e15264f BP |
1545 | bond_unixctl_disable_slave(struct unixctl_conn *conn, |
1546 | int argc OVS_UNUSED, const char *argv[], | |
f620b43a BP |
1547 | void *aux OVS_UNUSED) |
1548 | { | |
0e15264f | 1549 | enable_slave(conn, argv, false); |
f620b43a BP |
1550 | } |
1551 | ||
1552 | static void | |
0e15264f | 1553 | bond_unixctl_hash(struct unixctl_conn *conn, int argc, const char *argv[], |
f620b43a BP |
1554 | void *aux OVS_UNUSED) |
1555 | { | |
0e15264f BP |
1556 | const char *mac_s = argv[1]; |
1557 | const char *vlan_s = argc > 2 ? argv[2] : NULL; | |
1558 | const char *basis_s = argc > 3 ? argv[3] : NULL; | |
74ff3298 | 1559 | struct eth_addr mac; |
f620b43a BP |
1560 | uint8_t hash; |
1561 | char *hash_cstr; | |
1562 | unsigned int vlan; | |
672d18b2 | 1563 | uint32_t basis; |
f620b43a BP |
1564 | |
1565 | if (vlan_s) { | |
c2c28dfd | 1566 | if (!ovs_scan(vlan_s, "%u", &vlan)) { |
bde9f75d | 1567 | unixctl_command_reply_error(conn, "invalid vlan"); |
f620b43a BP |
1568 | return; |
1569 | } | |
1570 | } else { | |
dc155bff | 1571 | vlan = 0; |
f620b43a BP |
1572 | } |
1573 | ||
672d18b2 | 1574 | if (basis_s) { |
c2c28dfd | 1575 | if (!ovs_scan(basis_s, "%"SCNu32, &basis)) { |
bde9f75d | 1576 | unixctl_command_reply_error(conn, "invalid basis"); |
672d18b2 EJ |
1577 | return; |
1578 | } | |
1579 | } else { | |
1580 | basis = 0; | |
1581 | } | |
1582 | ||
c2c28dfd | 1583 | if (ovs_scan(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) { |
672d18b2 | 1584 | hash = bond_hash_src(mac, vlan, basis) & BOND_MASK; |
f620b43a BP |
1585 | |
1586 | hash_cstr = xasprintf("%u", hash); | |
bde9f75d | 1587 | unixctl_command_reply(conn, hash_cstr); |
f620b43a BP |
1588 | free(hash_cstr); |
1589 | } else { | |
bde9f75d | 1590 | unixctl_command_reply_error(conn, "invalid mac"); |
f620b43a BP |
1591 | } |
1592 | } | |
1593 | ||
1594 | void | |
1595 | bond_init(void) | |
1596 | { | |
0e15264f | 1597 | unixctl_command_register("bond/list", "", 0, 0, bond_unixctl_list, NULL); |
c33a8a25 EJ |
1598 | unixctl_command_register("bond/show", "[port]", 0, 1, bond_unixctl_show, |
1599 | NULL); | |
0e15264f | 1600 | unixctl_command_register("bond/migrate", "port hash slave", 3, 3, |
7ff2009a | 1601 | bond_unixctl_migrate, NULL); |
0e15264f | 1602 | unixctl_command_register("bond/set-active-slave", "port slave", 2, 2, |
f620b43a | 1603 | bond_unixctl_set_active_slave, NULL); |
0e15264f | 1604 | unixctl_command_register("bond/enable-slave", "port slave", 2, 2, |
7ff2009a | 1605 | bond_unixctl_enable_slave, NULL); |
0e15264f | 1606 | unixctl_command_register("bond/disable-slave", "port slave", 2, 2, |
7ff2009a | 1607 | bond_unixctl_disable_slave, NULL); |
0e15264f | 1608 | unixctl_command_register("bond/hash", "mac [vlan] [basis]", 1, 3, |
7ff2009a | 1609 | bond_unixctl_hash, NULL); |
f620b43a BP |
1610 | } |
1611 | \f | |
95aafb2a EJ |
1612 | static void |
1613 | bond_entry_reset(struct bond *bond) | |
1614 | { | |
1615 | if (bond->balance != BM_AB) { | |
9e1a6910 | 1616 | size_t hash_len = BOND_BUCKETS * sizeof *bond->hash; |
95aafb2a EJ |
1617 | |
1618 | if (!bond->hash) { | |
1619 | bond->hash = xmalloc(hash_len); | |
1620 | } | |
1621 | memset(bond->hash, 0, hash_len); | |
1622 | ||
1623 | bond->next_rebalance = time_msec() + bond->rebalance_interval; | |
1624 | } else { | |
1625 | free(bond->hash); | |
1626 | bond->hash = NULL; | |
1627 | } | |
1628 | } | |
1629 | ||
f620b43a BP |
1630 | static struct bond_slave * |
1631 | bond_slave_lookup(struct bond *bond, const void *slave_) | |
1632 | { | |
1633 | struct bond_slave *slave; | |
1634 | ||
1635 | HMAP_FOR_EACH_IN_BUCKET (slave, hmap_node, hash_pointer(slave_, 0), | |
1636 | &bond->slaves) { | |
1637 | if (slave->aux == slave_) { | |
1638 | return slave; | |
1639 | } | |
1640 | } | |
1641 | ||
1642 | return NULL; | |
1643 | } | |
1644 | ||
f620b43a | 1645 | static void |
4a1b8f30 | 1646 | bond_enable_slave(struct bond_slave *slave, bool enable) |
f620b43a BP |
1647 | { |
1648 | slave->delay_expires = LLONG_MAX; | |
1649 | if (enable != slave->enabled) { | |
4a1b8f30 | 1650 | slave->bond->bond_revalidate = true; |
f620b43a | 1651 | slave->enabled = enable; |
f1c8a79c AW |
1652 | |
1653 | ovs_mutex_lock(&slave->bond->mutex); | |
1654 | if (enable) { | |
417e7e66 | 1655 | ovs_list_insert(&slave->bond->enabled_slaves, &slave->list_node); |
f1c8a79c | 1656 | } else { |
417e7e66 | 1657 | ovs_list_remove(&slave->list_node); |
f1c8a79c AW |
1658 | } |
1659 | ovs_mutex_unlock(&slave->bond->mutex); | |
1660 | ||
4a1b8f30 EJ |
1661 | VLOG_INFO("interface %s: %s", slave->name, |
1662 | slave->enabled ? "enabled" : "disabled"); | |
f620b43a BP |
1663 | } |
1664 | } | |
1665 | ||
1666 | static void | |
4a1b8f30 | 1667 | bond_link_status_update(struct bond_slave *slave) |
f620b43a BP |
1668 | { |
1669 | struct bond *bond = slave->bond; | |
1670 | bool up; | |
1671 | ||
296f6519 | 1672 | up = netdev_get_carrier(slave->netdev) && slave->may_enable; |
f620b43a BP |
1673 | if ((up == slave->enabled) != (slave->delay_expires == LLONG_MAX)) { |
1674 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
1675 | VLOG_INFO_RL(&rl, "interface %s: link state %s", | |
1676 | slave->name, up ? "up" : "down"); | |
1677 | if (up == slave->enabled) { | |
1678 | slave->delay_expires = LLONG_MAX; | |
1679 | VLOG_INFO_RL(&rl, "interface %s: will not be %s", | |
1680 | slave->name, up ? "disabled" : "enabled"); | |
1681 | } else { | |
bdebeece | 1682 | int delay = (bond->lacp_status != LACP_DISABLED ? 0 |
f620b43a BP |
1683 | : up ? bond->updelay : bond->downdelay); |
1684 | slave->delay_expires = time_msec() + delay; | |
1685 | if (delay) { | |
1686 | VLOG_INFO_RL(&rl, "interface %s: will be %s if it stays %s " | |
1687 | "for %d ms", | |
1688 | slave->name, | |
1689 | up ? "enabled" : "disabled", | |
1690 | up ? "up" : "down", | |
1691 | delay); | |
1692 | } | |
1693 | } | |
1694 | } | |
1695 | ||
1696 | if (time_msec() >= slave->delay_expires) { | |
4a1b8f30 | 1697 | bond_enable_slave(slave, up); |
f620b43a BP |
1698 | } |
1699 | } | |
1700 | ||
f620b43a | 1701 | static unsigned int |
74ff3298 | 1702 | bond_hash_src(const struct eth_addr mac, uint16_t vlan, uint32_t basis) |
f620b43a | 1703 | { |
7e36ac42 | 1704 | return hash_mac(mac, vlan, basis); |
f620b43a BP |
1705 | } |
1706 | ||
1707 | static unsigned int | |
672d18b2 | 1708 | bond_hash_tcp(const struct flow *flow, uint16_t vlan, uint32_t basis) |
f620b43a BP |
1709 | { |
1710 | struct flow hash_flow = *flow; | |
d84d4b88 | 1711 | hash_flow.vlan_tci = htons(vlan); |
f620b43a BP |
1712 | |
1713 | /* The symmetric quality of this hash function is not required, but | |
1714 | * flow_hash_symmetric_l4 already exists, and is sufficient for our | |
1715 | * purposes, so we use it out of convenience. */ | |
672d18b2 | 1716 | return flow_hash_symmetric_l4(&hash_flow, basis); |
f620b43a BP |
1717 | } |
1718 | ||
fb0b29a3 EJ |
1719 | static unsigned int |
1720 | bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan) | |
1721 | { | |
cb22974d | 1722 | ovs_assert(bond->balance == BM_TCP || bond->balance == BM_SLB); |
fb0b29a3 | 1723 | |
bdebeece | 1724 | return (bond->balance == BM_TCP |
672d18b2 EJ |
1725 | ? bond_hash_tcp(flow, vlan, bond->basis) |
1726 | : bond_hash_src(flow->dl_src, vlan, bond->basis)); | |
fb0b29a3 EJ |
1727 | } |
1728 | ||
f620b43a BP |
1729 | static struct bond_entry * |
1730 | lookup_bond_entry(const struct bond *bond, const struct flow *flow, | |
1731 | uint16_t vlan) | |
1732 | { | |
fb0b29a3 | 1733 | return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK]; |
f620b43a BP |
1734 | } |
1735 | ||
f1c8a79c AW |
1736 | /* Selects and returns an enabled slave from the 'enabled_slaves' list |
1737 | * in a round-robin fashion. If the 'enabled_slaves' list is empty, | |
1738 | * returns NULL. */ | |
1739 | static struct bond_slave * | |
1740 | get_enabled_slave(struct bond *bond) | |
1741 | { | |
ca6ba700 | 1742 | struct ovs_list *node; |
f1c8a79c AW |
1743 | |
1744 | ovs_mutex_lock(&bond->mutex); | |
417e7e66 | 1745 | if (ovs_list_is_empty(&bond->enabled_slaves)) { |
f1c8a79c AW |
1746 | ovs_mutex_unlock(&bond->mutex); |
1747 | return NULL; | |
1748 | } | |
1749 | ||
417e7e66 BW |
1750 | node = ovs_list_pop_front(&bond->enabled_slaves); |
1751 | ovs_list_push_back(&bond->enabled_slaves, node); | |
f1c8a79c AW |
1752 | ovs_mutex_unlock(&bond->mutex); |
1753 | ||
1754 | return CONTAINER_OF(node, struct bond_slave, list_node); | |
1755 | } | |
1756 | ||
f620b43a BP |
1757 | static struct bond_slave * |
1758 | choose_output_slave(const struct bond *bond, const struct flow *flow, | |
4a1b8f30 | 1759 | struct flow_wildcards *wc, uint16_t vlan) |
f620b43a BP |
1760 | { |
1761 | struct bond_entry *e; | |
9dd165e0 | 1762 | int balance; |
f620b43a | 1763 | |
9dd165e0 | 1764 | balance = bond->balance; |
bdebeece EJ |
1765 | if (bond->lacp_status == LACP_CONFIGURED) { |
1766 | /* LACP has been configured on this bond but negotiations were | |
9dd165e0 RK |
1767 | * unsuccussful. If lacp_fallback_ab is enabled use active- |
1768 | * backup mode else drop all traffic. */ | |
1769 | if (!bond->lacp_fallback_ab) { | |
1770 | return NULL; | |
1771 | } | |
1772 | balance = BM_AB; | |
bdebeece EJ |
1773 | } |
1774 | ||
9dd165e0 | 1775 | switch (balance) { |
f620b43a BP |
1776 | case BM_AB: |
1777 | return bond->active_slave; | |
1778 | ||
f620b43a | 1779 | case BM_TCP: |
bdebeece EJ |
1780 | if (bond->lacp_status != LACP_NEGOTIATED) { |
1781 | /* Must have LACP negotiations for TCP balanced bonds. */ | |
1782 | return NULL; | |
1783 | } | |
bcd2633a | 1784 | if (wc) { |
6cdd5145 | 1785 | flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4); |
bcd2633a | 1786 | } |
bdebeece EJ |
1787 | /* Fall Through. */ |
1788 | case BM_SLB: | |
bcd2633a | 1789 | if (wc) { |
6cdd5145 | 1790 | flow_mask_hash_fields(flow, wc, NX_HASH_FIELDS_ETH_SRC); |
bcd2633a | 1791 | } |
f620b43a BP |
1792 | e = lookup_bond_entry(bond, flow, vlan); |
1793 | if (!e->slave || !e->slave->enabled) { | |
f1c8a79c | 1794 | e->slave = get_enabled_slave(CONST_CAST(struct bond*, bond)); |
f620b43a BP |
1795 | } |
1796 | return e->slave; | |
1797 | ||
1798 | default: | |
428b2edd | 1799 | OVS_NOT_REACHED(); |
f620b43a BP |
1800 | } |
1801 | } | |
1802 | ||
1803 | static struct bond_slave * | |
1804 | bond_choose_slave(const struct bond *bond) | |
1805 | { | |
1806 | struct bond_slave *slave, *best; | |
1807 | ||
3e5aeeb5 AZ |
1808 | /* Find the last active slave. */ |
1809 | slave = bond_find_slave_by_mac(bond, bond->active_slave_mac); | |
1810 | if (slave && slave->enabled) { | |
1811 | return slave; | |
1812 | } | |
1813 | ||
f620b43a BP |
1814 | /* Find an enabled slave. */ |
1815 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { | |
1816 | if (slave->enabled) { | |
1817 | return slave; | |
1818 | } | |
1819 | } | |
1820 | ||
1821 | /* All interfaces are disabled. Find an interface that will be enabled | |
1822 | * after its updelay expires. */ | |
1823 | best = NULL; | |
1824 | HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) { | |
1825 | if (slave->delay_expires != LLONG_MAX | |
296f6519 | 1826 | && slave->may_enable |
f620b43a BP |
1827 | && (!best || slave->delay_expires < best->delay_expires)) { |
1828 | best = slave; | |
1829 | } | |
1830 | } | |
1831 | return best; | |
1832 | } | |
1833 | ||
1834 | static void | |
4a1b8f30 | 1835 | bond_choose_active_slave(struct bond *bond) |
f620b43a BP |
1836 | { |
1837 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
1838 | struct bond_slave *old_active_slave = bond->active_slave; | |
1839 | ||
1840 | bond->active_slave = bond_choose_slave(bond); | |
1841 | if (bond->active_slave) { | |
1842 | if (bond->active_slave->enabled) { | |
1843 | VLOG_INFO_RL(&rl, "bond %s: active interface is now %s", | |
1844 | bond->name, bond->active_slave->name); | |
1845 | } else { | |
1846 | VLOG_INFO_RL(&rl, "bond %s: active interface is now %s, skipping " | |
1847 | "remaining %lld ms updelay (since no interface was " | |
1848 | "enabled)", bond->name, bond->active_slave->name, | |
1849 | bond->active_slave->delay_expires - time_msec()); | |
4a1b8f30 | 1850 | bond_enable_slave(bond->active_slave, true); |
f620b43a BP |
1851 | } |
1852 | ||
1853 | bond->send_learning_packets = true; | |
3e5aeeb5 AZ |
1854 | |
1855 | if (bond->active_slave != old_active_slave) { | |
1856 | bond_active_slave_changed(bond); | |
1857 | } | |
f620b43a | 1858 | } else if (old_active_slave) { |
d28b9ead | 1859 | VLOG_INFO_RL(&rl, "bond %s: all interfaces disabled", bond->name); |
f620b43a BP |
1860 | } |
1861 | } | |
3e5aeeb5 AZ |
1862 | |
1863 | /* | |
1864 | * Return true if bond has unstored active slave change. | |
1865 | * If return true, 'mac' will store the bond's current active slave's | |
1866 | * MAC address. */ | |
1867 | bool | |
74ff3298 JR |
1868 | bond_get_changed_active_slave(const char *name, struct eth_addr *mac, |
1869 | bool force) | |
3e5aeeb5 AZ |
1870 | { |
1871 | struct bond *bond; | |
1872 | ||
1873 | ovs_rwlock_wrlock(&rwlock); | |
1874 | bond = bond_find(name); | |
1875 | if (bond) { | |
1876 | if (bond->active_slave_changed || force) { | |
74ff3298 | 1877 | *mac = bond->active_slave_mac; |
3e5aeeb5 AZ |
1878 | bond->active_slave_changed = false; |
1879 | ovs_rwlock_unlock(&rwlock); | |
1880 | return true; | |
1881 | } | |
1882 | } | |
1883 | ovs_rwlock_unlock(&rwlock); | |
1884 | ||
1885 | return false; | |
1886 | } |