]> git.proxmox.com Git - mirror_ovs.git/blame - lib/bond.c
include/linux/openvswitch.h: Make OVSP_LOCAL 32-bit.
[mirror_ovs.git] / lib / bond.c
CommitLineData
f620b43a 1/*
477879ea 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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>
24
25#include "coverage.h"
26#include "dynamic-string.h"
27#include "flow.h"
28#include "hmap.h"
bdebeece 29#include "lacp.h"
f620b43a
BP
30#include "list.h"
31#include "netdev.h"
32#include "odp-util.h"
33#include "ofpbuf.h"
34#include "packets.h"
35#include "poll-loop.h"
fc1d4f01 36#include "shash.h"
f620b43a
BP
37#include "tag.h"
38#include "timeval.h"
39#include "unixctl.h"
40#include "vlog.h"
41
42VLOG_DEFINE_THIS_MODULE(bond);
43
f620b43a
BP
44/* Bit-mask for hashing a flow down to a bucket.
45 * There are (BOND_MASK + 1) buckets. */
46#define BOND_MASK 0xff
47
48/* A hash bucket for mapping a flow to a slave.
49 * "struct bond" has an array of (BOND_MASK + 1) of these. */
50struct bond_entry {
51 struct bond_slave *slave; /* Assigned slave, NULL if unassigned. */
52 uint64_t tx_bytes; /* Count of bytes recently transmitted. */
53 tag_type tag; /* Tag for entry<->slave association. */
54 struct list list_node; /* In bond_slave's 'entries' list. */
55};
56
57/* A bond slave, that is, one of the links comprising a bond. */
58struct bond_slave {
59 struct hmap_node hmap_node; /* In struct bond's slaves hmap. */
60 struct bond *bond; /* The bond that contains this slave. */
61 void *aux; /* Client-provided handle for this slave. */
62
63 struct netdev *netdev; /* Network device, owned by the client. */
1ea24138 64 unsigned int change_seq; /* Tracks changes in 'netdev'. */
f620b43a
BP
65 char *name; /* Name (a copy of netdev_get_name(netdev)). */
66
67 /* Link status. */
68 long long delay_expires; /* Time after which 'enabled' may change. */
f620b43a 69 bool enabled; /* May be chosen for flows? */
296f6519 70 bool may_enable; /* Client considers this slave bondable. */
f620b43a
BP
71 tag_type tag; /* Tag associated with this slave. */
72
73 /* Rebalancing info. Used only by bond_rebalance(). */
74 struct list bal_node; /* In bond_rebalance()'s 'bals' list. */
75 struct list entries; /* 'struct bond_entry's assigned here. */
76 uint64_t tx_bytes; /* Sum across 'tx_bytes' of entries. */
fb0b29a3
EJ
77
78 /* BM_STABLE specific bonding info. */
75135fa0 79 uint32_t stb_id; /* ID used for 'stb_slaves' ordering. */
f620b43a
BP
80};
81
82/* A bond, that is, a set of network devices grouped to improve performance or
83 * robustness. */
84struct bond {
85 struct hmap_node hmap_node; /* In 'all_bonds' hmap. */
86 char *name; /* Name provided by client. */
87
88 /* Slaves. */
89 struct hmap slaves;
90
91 /* Bonding info. */
92 enum bond_mode balance; /* Balancing mode, one of BM_*. */
93 struct bond_slave *active_slave;
94 tag_type no_slaves_tag; /* Tag for flows when all slaves disabled. */
95 int updelay, downdelay; /* Delay before slave goes up/down, in ms. */
bdebeece 96 enum lacp_status lacp_status; /* Status of LACP negotiations. */
62904702 97 bool bond_revalidate; /* True if flows need revalidation. */
672d18b2 98 uint32_t basis; /* Basis for flow hash function. */
f620b43a
BP
99
100 /* SLB specific bonding info. */
101 struct bond_entry *hash; /* An array of (BOND_MASK + 1) elements. */
102 int rebalance_interval; /* Interval between rebalances, in ms. */
103 long long int next_rebalance; /* Next rebalancing time. */
104 bool send_learning_packets;
105
fb0b29a3 106 /* BM_STABLE specific bonding info. */
8bb6f31d 107 tag_type stb_tag; /* Tag associated with this bond. */
f620b43a 108
f620b43a
BP
109 /* Legacy compatibility. */
110 long long int next_fake_iface_update; /* LLONG_MAX if disabled. */
111
112 /* Tag set saved for next bond_run(). This tag set is a kluge for cases
113 * where we can't otherwise provide revalidation feedback to the client.
114 * That's only unixctl commands now; I hope no other cases will arise. */
115 struct tag_set unixctl_tags;
116};
117
118static struct hmap all_bonds = HMAP_INITIALIZER(&all_bonds);
119
95aafb2a 120static void bond_entry_reset(struct bond *);
f620b43a 121static struct bond_slave *bond_slave_lookup(struct bond *, const void *slave_);
f620b43a
BP
122static void bond_enable_slave(struct bond_slave *, bool enable,
123 struct tag_set *);
124static void bond_link_status_update(struct bond_slave *, struct tag_set *);
125static void bond_choose_active_slave(struct bond *, struct tag_set *);
f620b43a 126static unsigned int bond_hash_src(const uint8_t mac[ETH_ADDR_LEN],
672d18b2
EJ
127 uint16_t vlan, uint32_t basis);
128static unsigned int bond_hash_tcp(const struct flow *, uint16_t vlan,
129 uint32_t basis);
f620b43a
BP
130static struct bond_entry *lookup_bond_entry(const struct bond *,
131 const struct flow *,
132 uint16_t vlan);
133static tag_type bond_get_active_slave_tag(const struct bond *);
134static struct bond_slave *choose_output_slave(const struct bond *,
135 const struct flow *,
00ed8314 136 uint16_t vlan, tag_type *tags);
f620b43a
BP
137static void bond_update_fake_slave_stats(struct bond *);
138
139/* Attempts to parse 's' as the name of a bond balancing mode. If successful,
140 * stores the mode in '*balance' and returns true. Otherwise returns false
141 * without modifying '*balance'. */
142bool
143bond_mode_from_string(enum bond_mode *balance, const char *s)
144{
145 if (!strcmp(s, bond_mode_to_string(BM_TCP))) {
146 *balance = BM_TCP;
147 } else if (!strcmp(s, bond_mode_to_string(BM_SLB))) {
148 *balance = BM_SLB;
fb0b29a3
EJ
149 } else if (!strcmp(s, bond_mode_to_string(BM_STABLE))) {
150 *balance = BM_STABLE;
f620b43a
BP
151 } else if (!strcmp(s, bond_mode_to_string(BM_AB))) {
152 *balance = BM_AB;
153 } else {
154 return false;
155 }
156 return true;
157}
158
159/* Returns a string representing 'balance'. */
160const char *
161bond_mode_to_string(enum bond_mode balance) {
162 switch (balance) {
163 case BM_TCP:
164 return "balance-tcp";
165 case BM_SLB:
166 return "balance-slb";
fb0b29a3
EJ
167 case BM_STABLE:
168 return "stable";
f620b43a
BP
169 case BM_AB:
170 return "active-backup";
171 }
172 NOT_REACHED();
173}
174
f620b43a
BP
175\f
176/* Creates and returns a new bond whose configuration is initially taken from
177 * 's'.
178 *
179 * The caller should register each slave on the new bond by calling
180 * bond_slave_register(). */
181struct bond *
182bond_create(const struct bond_settings *s)
183{
184 struct bond *bond;
185
186 bond = xzalloc(sizeof *bond);
187 hmap_init(&bond->slaves);
188 bond->no_slaves_tag = tag_create_random();
7321e30e 189 bond->stb_tag = tag_create_random();
f620b43a
BP
190 bond->next_fake_iface_update = LLONG_MAX;
191
192 bond_reconfigure(bond, s);
193
194 tag_set_init(&bond->unixctl_tags);
195
196 return bond;
197}
198
199/* Frees 'bond'. */
200void
201bond_destroy(struct bond *bond)
202{
203 struct bond_slave *slave, *next_slave;
204
205 if (!bond) {
206 return;
207 }
208
209 hmap_remove(&all_bonds, &bond->hmap_node);
210
211 HMAP_FOR_EACH_SAFE (slave, next_slave, hmap_node, &bond->slaves) {
212 hmap_remove(&bond->slaves, &slave->hmap_node);
213 /* Client owns 'slave->netdev'. */
214 free(slave->name);
215 free(slave);
216 }
217 hmap_destroy(&bond->slaves);
218
219 free(bond->hash);
f620b43a
BP
220 free(bond->name);
221 free(bond);
222}
223
224/* Updates 'bond''s overall configuration to 's'.
225 *
226 * The caller should register each slave on 'bond' by calling
227 * bond_slave_register(). This is optional if none of the slaves'
4d6fb5eb 228 * configuration has changed. In any case it can't hurt.
59d7b2b6
EJ
229 *
230 * Returns true if the configuration has changed in such a way that requires
231 * flow revalidation.
232 * */
233bool
f620b43a
BP
234bond_reconfigure(struct bond *bond, const struct bond_settings *s)
235{
59d7b2b6
EJ
236 bool revalidate = false;
237
f620b43a
BP
238 if (!bond->name || strcmp(bond->name, s->name)) {
239 if (bond->name) {
240 hmap_remove(&all_bonds, &bond->hmap_node);
241 free(bond->name);
242 }
243 bond->name = xstrdup(s->name);
244 hmap_insert(&all_bonds, &bond->hmap_node, hash_string(bond->name, 0));
245 }
246
f620b43a
BP
247 bond->updelay = s->up_delay;
248 bond->downdelay = s->down_delay;
bc1b010c
EJ
249
250 if (bond->rebalance_interval != s->rebalance_interval) {
251 bond->rebalance_interval = s->rebalance_interval;
252 revalidate = true;
253 }
f620b43a 254
59d7b2b6
EJ
255 if (bond->balance != s->balance) {
256 bond->balance = s->balance;
257 revalidate = true;
f6af6c0e
EJ
258
259 if (bond->balance == BM_STABLE) {
260 VLOG_WARN_ONCE("Stable bond mode is deprecated and may be removed"
261 " in February 2013. Please email"
262 " dev@openvswitch.org with concerns.");
263 }
59d7b2b6
EJ
264 }
265
672d18b2
EJ
266 if (bond->basis != s->basis) {
267 bond->basis = s->basis;
268 revalidate = true;
269 }
270
f620b43a
BP
271 if (s->fake_iface) {
272 if (bond->next_fake_iface_update == LLONG_MAX) {
273 bond->next_fake_iface_update = time_msec();
274 }
275 } else {
276 bond->next_fake_iface_update = LLONG_MAX;
277 }
59d7b2b6 278
62904702
EJ
279 if (bond->bond_revalidate) {
280 revalidate = true;
281 bond->bond_revalidate = false;
282 }
283
95aafb2a
EJ
284 if (bond->balance == BM_AB || !bond->hash || revalidate) {
285 bond_entry_reset(bond);
286 }
287
59d7b2b6 288 return revalidate;
f620b43a
BP
289}
290
f8ddccd2 291static void
1ea24138 292bond_slave_set_netdev__(struct bond_slave *slave, struct netdev *netdev)
f8ddccd2
BP
293{
294 if (slave->netdev != netdev) {
f8ddccd2 295 slave->netdev = netdev;
1ea24138 296 slave->change_seq = 0;
f8ddccd2
BP
297 }
298}
299
f620b43a
BP
300/* Registers 'slave_' as a slave of 'bond'. The 'slave_' pointer is an
301 * arbitrary client-provided pointer that uniquely identifies a slave within a
302 * bond. If 'slave_' already exists within 'bond' then this function
303 * reconfigures the existing slave.
304 *
e1e90548
EJ
305 * 'stb_id' is used in BM_STABLE bonds to guarantee consistent slave choices
306 * across restarts and distributed vswitch instances. It should be unique per
307 * slave, and preferably consistent across restarts and reconfigurations.
308 *
f620b43a
BP
309 * 'netdev' must be the network device that 'slave_' represents. It is owned
310 * by the client, so the client must not close it before either unregistering
311 * 'slave_' or destroying 'bond'.
4d6fb5eb 312 */
f620b43a 313void
75135fa0 314bond_slave_register(struct bond *bond, void *slave_, uint32_t stb_id,
e1e90548 315 struct netdev *netdev)
f620b43a
BP
316{
317 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
318
319 if (!slave) {
320 slave = xzalloc(sizeof *slave);
321
322 hmap_insert(&bond->slaves, &slave->hmap_node, hash_pointer(slave_, 0));
323 slave->bond = bond;
324 slave->aux = slave_;
325 slave->delay_expires = LLONG_MAX;
244b2160 326 slave->name = xstrdup(netdev_get_name(netdev));
7321e30e 327 bond->bond_revalidate = true;
244b2160 328
b3c18f66 329 slave->enabled = false;
c8544aa1 330 bond_enable_slave(slave, netdev_get_carrier(netdev), NULL);
f620b43a
BP
331 }
332
e1e90548 333 if (slave->stb_id != stb_id) {
e1e90548 334 slave->stb_id = stb_id;
7321e30e 335 bond->bond_revalidate = true;
e1e90548
EJ
336 }
337
1ea24138 338 bond_slave_set_netdev__(slave, netdev);
a6934aa9 339
f620b43a
BP
340 free(slave->name);
341 slave->name = xstrdup(netdev_get_name(netdev));
f620b43a
BP
342}
343
f8ddccd2
BP
344/* Updates the network device to be used with 'slave_' to 'netdev'.
345 *
346 * This is useful if the caller closes and re-opens the network device
347 * registered with bond_slave_register() but doesn't need to change anything
348 * else. */
349void
350bond_slave_set_netdev(struct bond *bond, void *slave_, struct netdev *netdev)
351{
352 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
353 if (slave) {
1ea24138 354 bond_slave_set_netdev__(slave, netdev);
f8ddccd2
BP
355 }
356}
357
f620b43a
BP
358/* Unregisters 'slave_' from 'bond'. If 'bond' does not contain such a slave
359 * then this function has no effect.
360 *
361 * Unregistering a slave invalidates all flows. */
362void
363bond_slave_unregister(struct bond *bond, const void *slave_)
364{
365 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
366 bool del_active;
367
368 if (!slave) {
369 return;
370 }
371
b3c18f66
EJ
372 bond_enable_slave(slave, false, NULL);
373
f620b43a
BP
374 del_active = bond->active_slave == slave;
375 if (bond->hash) {
376 struct bond_entry *e;
377 for (e = bond->hash; e <= &bond->hash[BOND_MASK]; e++) {
378 if (e->slave == slave) {
379 e->slave = NULL;
380 }
381 }
382 }
383
384 free(slave->name);
385
386 hmap_remove(&bond->slaves, &slave->hmap_node);
387 /* Client owns 'slave->netdev'. */
388 free(slave);
389
390 if (del_active) {
391 struct tag_set tags;
392
393 tag_set_init(&tags);
394 bond_choose_active_slave(bond, &tags);
395 bond->send_learning_packets = true;
396 }
397}
398
296f6519
EJ
399/* Should be called on each slave in 'bond' before bond_run() to indicate
400 * whether or not 'slave_' may be enabled. This function is intended to allow
401 * other protocols to have some impact on bonding decisions. For example LACP
402 * or high level link monitoring protocols may decide that a given slave should
403 * not be able to send traffic. */
4d6fb5eb 404void
296f6519 405bond_slave_set_may_enable(struct bond *bond, void *slave_, bool may_enable)
4d6fb5eb 406{
296f6519 407 bond_slave_lookup(bond, slave_)->may_enable = may_enable;
4d6fb5eb
EJ
408}
409
f620b43a
BP
410/* Performs periodic maintenance on 'bond'. The caller must provide 'tags' to
411 * allow tagged flows to be invalidated.
412 *
413 * The caller should check bond_should_send_learning_packets() afterward. */
414void
bdebeece 415bond_run(struct bond *bond, struct tag_set *tags, enum lacp_status lacp_status)
f620b43a
BP
416{
417 struct bond_slave *slave;
418
bdebeece
EJ
419 if (bond->lacp_status != lacp_status) {
420 bond->lacp_status = lacp_status;
4592d0e2
EJ
421 bond->bond_revalidate = true;
422 }
4d6fb5eb 423
f620b43a
BP
424 /* Enable slaves based on link status and LACP feedback. */
425 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
426 bond_link_status_update(slave, tags);
1ea24138 427 slave->change_seq = netdev_change_seq(slave->netdev);
f620b43a
BP
428 }
429 if (!bond->active_slave || !bond->active_slave->enabled) {
430 bond_choose_active_slave(bond, tags);
431 }
432
433 /* Update fake bond interface stats. */
434 if (time_msec() >= bond->next_fake_iface_update) {
435 bond_update_fake_slave_stats(bond);
436 bond->next_fake_iface_update = time_msec() + 1000;
437 }
438
62904702
EJ
439 if (bond->bond_revalidate) {
440 bond->bond_revalidate = false;
dc9908b3 441
95aafb2a 442 bond_entry_reset(bond);
8bb6f31d
EJ
443 if (bond->balance != BM_STABLE) {
444 struct bond_slave *slave;
445
446 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
447 tag_set_add(tags, slave->tag);
448 }
449 } else {
450 tag_set_add(tags, bond->stb_tag);
dc9908b3 451 }
0008fbcb 452 tag_set_add(tags, bond->no_slaves_tag);
dc9908b3
EJ
453 }
454
f620b43a
BP
455 /* Invalidate any tags required by */
456 tag_set_union(tags, &bond->unixctl_tags);
457 tag_set_init(&bond->unixctl_tags);
458}
459
460/* Causes poll_block() to wake up when 'bond' needs something to be done. */
461void
462bond_wait(struct bond *bond)
463{
464 struct bond_slave *slave;
465
f620b43a
BP
466 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
467 if (slave->delay_expires != LLONG_MAX) {
468 poll_timer_wait_until(slave->delay_expires);
469 }
1ea24138
EJ
470
471 if (slave->change_seq != netdev_change_seq(slave->netdev)) {
472 poll_immediate_wake();
473 }
f620b43a
BP
474 }
475
476 if (bond->next_fake_iface_update != LLONG_MAX) {
477 poll_timer_wait_until(bond->next_fake_iface_update);
478 }
479
480 /* Ensure that any saved tags get revalidated right away. */
481 if (!tag_set_is_empty(&bond->unixctl_tags)) {
482 poll_immediate_wake();
483 }
484
485 /* We don't wait for bond->next_rebalance because rebalancing can only run
486 * at a flow account checkpoint. ofproto does checkpointing on its own
487 * schedule and bond_rebalance() gets called afterward, so we'd just be
488 * waking up for no purpose. */
489}
490\f
491/* MAC learning table interaction. */
492
493static bool
494may_send_learning_packets(const struct bond *bond)
495{
bdebeece 496 return bond->lacp_status == LACP_DISABLED
64e2748d 497 && (bond->balance == BM_SLB || bond->balance == BM_AB)
bdebeece 498 && bond->active_slave;
f620b43a
BP
499}
500
501/* Returns true if 'bond' needs the client to send out packets to assist with
502 * MAC learning on 'bond'. If this function returns true, then the client
503 * should iterate through its MAC learning table for the bridge on which 'bond'
504 * is located. For each MAC that has been learned on a port other than 'bond',
ea131871 505 * it should call bond_compose_learning_packet().
f620b43a 506 *
477879ea
BP
507 * This function will only return true if 'bond' is in SLB or active-backup
508 * mode and LACP is not negotiated. Otherwise sending learning packets isn't
509 * necessary.
f620b43a
BP
510 *
511 * Calling this function resets the state that it checks. */
512bool
513bond_should_send_learning_packets(struct bond *bond)
514{
515 bool send = bond->send_learning_packets && may_send_learning_packets(bond);
516 bond->send_learning_packets = false;
517 return send;
518}
519
520/* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'.
521 *
ea131871
JG
522 * See bond_should_send_learning_packets() for description of usage. The
523 * caller should send the composed packet on the port associated with
524 * port_aux and takes ownership of the returned ofpbuf. */
525struct ofpbuf *
526bond_compose_learning_packet(struct bond *bond,
527 const uint8_t eth_src[ETH_ADDR_LEN],
528 uint16_t vlan, void **port_aux)
f620b43a
BP
529{
530 struct bond_slave *slave;
ea131871 531 struct ofpbuf *packet;
00ed8314 532 tag_type tags = 0;
f620b43a 533 struct flow flow;
f620b43a
BP
534
535 assert(may_send_learning_packets(bond));
f620b43a
BP
536
537 memset(&flow, 0, sizeof flow);
538 memcpy(flow.dl_src, eth_src, ETH_ADDR_LEN);
00ed8314 539 slave = choose_output_slave(bond, &flow, vlan, &tags);
f620b43a 540
ea131871 541 packet = ofpbuf_new(0);
2ea838ac 542 compose_rarp(packet, eth_src);
f620b43a 543 if (vlan) {
ea131871 544 eth_push_vlan(packet, htons(vlan));
f620b43a 545 }
f620b43a 546
ea131871
JG
547 *port_aux = slave->aux;
548 return packet;
f620b43a
BP
549}
550\f
551/* Checks whether a packet that arrived on 'slave_' within 'bond', with an
552 * Ethernet destination address of 'eth_dst', should be admitted.
553 *
554 * The return value is one of the following:
555 *
556 * - BV_ACCEPT: Admit the packet.
557 *
558 * - BV_DROP: Drop the packet.
559 *
560 * - BV_DROP_IF_MOVED: Consult the MAC learning table for the packet's
561 * Ethernet source address and VLAN. If there is none, or if the packet
562 * is on the learned port, then admit the packet. If a different port has
563 * been learned, however, drop the packet (and do not use it for MAC
564 * learning).
565 */
566enum bond_verdict
567bond_check_admissibility(struct bond *bond, const void *slave_,
568 const uint8_t eth_dst[ETH_ADDR_LEN], tag_type *tags)
569{
9a1c6450
EJ
570 struct bond_slave *slave = bond_slave_lookup(bond, slave_);
571
572 /* LACP bonds have very loose admissibility restrictions because we can
573 * assume the remote switch is aware of the bond and will "do the right
574 * thing". However, as a precaution we drop packets on disabled slaves
575 * because no correctly implemented partner switch should be sending
bdebeece
EJ
576 * packets to them.
577 *
578 * If LACP is configured, but LACP negotiations have been unsuccessful, we
579 * drop all incoming traffic. */
580 switch (bond->lacp_status) {
581 case LACP_NEGOTIATED: return slave->enabled ? BV_ACCEPT : BV_DROP;
582 case LACP_CONFIGURED: return BV_DROP;
583 case LACP_DISABLED: break;
f620b43a
BP
584 }
585
586 /* Drop all multicast packets on inactive slaves. */
587 if (eth_addr_is_multicast(eth_dst)) {
588 *tags |= bond_get_active_slave_tag(bond);
589 if (bond->active_slave != bond_slave_lookup(bond, slave_)) {
590 return BV_DROP;
591 }
592 }
593
f931a4c9
BP
594 switch (bond->balance) {
595 case BM_AB:
596 /* Drop all packets which arrive on backup slaves. This is similar to
597 * how Linux bonding handles active-backup bonds. */
7ba7dcf0
EJ
598 *tags |= bond_get_active_slave_tag(bond);
599 if (bond->active_slave != slave) {
600 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
601
e6b2255c
BP
602 VLOG_DBG_RL(&rl, "active-backup bond received packet on backup"
603 " slave (%s) destined for " ETH_ADDR_FMT,
604 slave->name, ETH_ADDR_ARGS(eth_dst));
7ba7dcf0
EJ
605 return BV_DROP;
606 }
f931a4c9
BP
607 return BV_ACCEPT;
608
609 case BM_TCP:
bdebeece
EJ
610 /* TCP balanced bonds require successful LACP negotiated. Based on the
611 * above check, LACP is off on this bond. Therfore, we drop all
612 * incoming traffic. */
613 return BV_DROP;
614
f931a4c9
BP
615 case BM_SLB:
616 /* Drop all packets for which we have learned a different input port,
617 * because we probably sent the packet on one slave and got it back on
618 * the other. Gratuitous ARP packets are an exception to this rule:
619 * the host has moved to another switch. The exception to the
620 * exception is if we locked the learning table to avoid reflections on
621 * bond slaves. */
622 return BV_DROP_IF_MOVED;
623
624 case BM_STABLE:
625 return BV_ACCEPT;
7ba7dcf0
EJ
626 }
627
f931a4c9 628 NOT_REACHED();
f620b43a
BP
629}
630
631/* Returns the slave (registered on 'bond' by bond_slave_register()) to which
632 * a packet with the given 'flow' and 'vlan' should be forwarded. Returns
633 * NULL if the packet should be dropped because no slaves are enabled.
634 *
635 * 'vlan' is not necessarily the same as 'flow->vlan_tci'. First, 'vlan'
636 * should be a VID only (i.e. excluding the PCP bits). Second,
637 * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be
638 * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the
639 * packet belongs to (so for an access port it will be the access port's VLAN).
640 *
641 * Adds a tag to '*tags' that associates the flow with the returned slave.
642 */
643void *
644bond_choose_output_slave(struct bond *bond, const struct flow *flow,
645 uint16_t vlan, tag_type *tags)
646{
00ed8314 647 struct bond_slave *slave = choose_output_slave(bond, flow, vlan, tags);
f620b43a 648 if (slave) {
8bb6f31d 649 *tags |= bond->balance == BM_STABLE ? bond->stb_tag : slave->tag;
f620b43a
BP
650 return slave->aux;
651 } else {
652 *tags |= bond->no_slaves_tag;
653 return NULL;
654 }
655}
f620b43a
BP
656\f
657/* Rebalancing. */
658
1b137691
EJ
659static bool
660bond_is_balanced(const struct bond *bond)
661{
bc1b010c
EJ
662 return bond->rebalance_interval
663 && (bond->balance == BM_SLB || bond->balance == BM_TCP);
1b137691
EJ
664}
665
f620b43a
BP
666/* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
667void
668bond_account(struct bond *bond, const struct flow *flow, uint16_t vlan,
669 uint64_t n_bytes)
670{
1b137691 671 if (bond_is_balanced(bond)) {
f620b43a 672 lookup_bond_entry(bond, flow, vlan)->tx_bytes += n_bytes;
f620b43a
BP
673 }
674}
675
676static struct bond_slave *
677bond_slave_from_bal_node(struct list *bal)
678{
679 return CONTAINER_OF(bal, struct bond_slave, bal_node);
680}
681
682static void
683log_bals(struct bond *bond, const struct list *bals)
684{
685 if (VLOG_IS_DBG_ENABLED()) {
686 struct ds ds = DS_EMPTY_INITIALIZER;
687 const struct bond_slave *slave;
688
689 LIST_FOR_EACH (slave, bal_node, bals) {
690 if (ds.length) {
691 ds_put_char(&ds, ',');
692 }
693 ds_put_format(&ds, " %s %"PRIu64"kB",
694 slave->name, slave->tx_bytes / 1024);
695
696 if (!slave->enabled) {
697 ds_put_cstr(&ds, " (disabled)");
698 }
699 if (!list_is_empty(&slave->entries)) {
700 struct bond_entry *e;
701
702 ds_put_cstr(&ds, " (");
703 LIST_FOR_EACH (e, list_node, &slave->entries) {
704 if (&e->list_node != list_front(&slave->entries)) {
705 ds_put_cstr(&ds, " + ");
706 }
707 ds_put_format(&ds, "h%td: %"PRIu64"kB",
708 e - bond->hash, e->tx_bytes / 1024);
709 }
710 ds_put_cstr(&ds, ")");
711 }
712 }
713 VLOG_DBG("bond %s:%s", bond->name, ds_cstr(&ds));
714 ds_destroy(&ds);
715 }
716}
717
718/* Shifts 'hash' from its current slave to 'to'. */
719static void
720bond_shift_load(struct bond_entry *hash, struct bond_slave *to,
721 struct tag_set *set)
722{
723 struct bond_slave *from = hash->slave;
724 struct bond *bond = from->bond;
725 uint64_t delta = hash->tx_bytes;
726
727 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
728 "from %s to %s (now carrying %"PRIu64"kB and "
729 "%"PRIu64"kB load, respectively)",
730 bond->name, delta / 1024, hash - bond->hash,
731 from->name, to->name,
732 (from->tx_bytes - delta) / 1024,
733 (to->tx_bytes + delta) / 1024);
734
735 /* Shift load away from 'from' to 'to'. */
736 from->tx_bytes -= delta;
737 to->tx_bytes += delta;
738
739 /* Arrange for flows to be revalidated. */
740 tag_set_add(set, hash->tag);
741 hash->slave = to;
742 hash->tag = tag_create_random();
743}
744
745/* Pick and returns a bond_entry to migrate to 'to' (the least-loaded slave),
746 * given that doing so must decrease the ratio of the load on the two slaves by
747 * at least 0.1. Returns NULL if there is no appropriate entry.
748 *
749 * The list of entries isn't sorted. I don't know of a reason to prefer to
750 * shift away small hashes or large hashes. */
751static struct bond_entry *
752choose_entry_to_migrate(const struct bond_slave *from, uint64_t to_tx_bytes)
753{
754 struct bond_entry *e;
755
756 if (list_is_short(&from->entries)) {
757 /* 'from' carries no more than one MAC hash, so shifting load away from
758 * it would be pointless. */
759 return NULL;
760 }
761
762 LIST_FOR_EACH (e, list_node, &from->entries) {
763 double old_ratio, new_ratio;
764 uint64_t delta;
765
766 if (to_tx_bytes == 0) {
767 /* Nothing on the new slave, move it. */
768 return e;
769 }
770
771 delta = e->tx_bytes;
772 old_ratio = (double)from->tx_bytes / to_tx_bytes;
773 new_ratio = (double)(from->tx_bytes - delta) / (to_tx_bytes + delta);
774 if (old_ratio - new_ratio > 0.1) {
775 /* Would decrease the ratio, move it. */
776 return e;
777 }
778 }
779
780 return NULL;
781}
782
783/* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is
784 * maintained. */
785static void
786insert_bal(struct list *bals, struct bond_slave *slave)
787{
788 struct bond_slave *pos;
789
790 LIST_FOR_EACH (pos, bal_node, bals) {
791 if (slave->tx_bytes > pos->tx_bytes) {
792 break;
793 }
794 }
795 list_insert(&pos->bal_node, &slave->bal_node);
796}
797
798/* Removes 'slave' from its current list and then inserts it into 'bals' so
799 * that descending order of 'tx_bytes' is maintained. */
800static void
801reinsert_bal(struct list *bals, struct bond_slave *slave)
802{
803 list_remove(&slave->bal_node);
804 insert_bal(bals, slave);
805}
806
807/* If 'bond' needs rebalancing, does so.
808 *
809 * The caller should have called bond_account() for each active flow, to ensure
810 * that flow data is consistently accounted at this point. */
811void
812bond_rebalance(struct bond *bond, struct tag_set *tags)
813{
814 struct bond_slave *slave;
815 struct bond_entry *e;
816 struct list bals;
817
1b137691 818 if (!bond_is_balanced(bond) || time_msec() < bond->next_rebalance) {
f620b43a
BP
819 return;
820 }
821 bond->next_rebalance = time_msec() + bond->rebalance_interval;
822
823 /* Add each bond_entry to its slave's 'entries' list.
824 * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */
825 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
826 slave->tx_bytes = 0;
827 list_init(&slave->entries);
828 }
829 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
830 if (e->slave && e->tx_bytes) {
831 e->slave->tx_bytes += e->tx_bytes;
832 list_push_back(&e->slave->entries, &e->list_node);
833 }
834 }
835
836 /* Add enabled slaves to 'bals' in descending order of tx_bytes.
837 *
838 * XXX This is O(n**2) in the number of slaves but it could be O(n lg n)
839 * with a proper list sort algorithm. */
840 list_init(&bals);
841 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
842 if (slave->enabled) {
843 insert_bal(&bals, slave);
844 }
845 }
846 log_bals(bond, &bals);
847
848 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
849 while (!list_is_short(&bals)) {
850 struct bond_slave *from = bond_slave_from_bal_node(list_front(&bals));
851 struct bond_slave *to = bond_slave_from_bal_node(list_back(&bals));
852 uint64_t overload;
853
854 overload = from->tx_bytes - to->tx_bytes;
855 if (overload < to->tx_bytes >> 5 || overload < 100000) {
856 /* The extra load on 'from' (and all less-loaded slaves), compared
857 * to that of 'to' (the least-loaded slave), is less than ~3%, or
858 * it is less than ~1Mbps. No point in rebalancing. */
859 break;
860 }
861
862 /* 'from' is carrying significantly more load than 'to', and that load
863 * is split across at least two different hashes. */
864 e = choose_entry_to_migrate(from, to->tx_bytes);
865 if (e) {
866 bond_shift_load(e, to, tags);
867
868 /* Delete element from from->entries.
869 *
870 * We don't add the element to to->hashes. That would only allow
871 * 'e' to be migrated to another slave in this rebalancing run, and
872 * there is no point in doing that. */
873 list_remove(&e->list_node);
874
875 /* Re-sort 'bals'. */
876 reinsert_bal(&bals, from);
877 reinsert_bal(&bals, to);
878 } else {
879 /* Can't usefully migrate anything away from 'from'.
880 * Don't reconsider it. */
881 list_remove(&from->bal_node);
882 }
883 }
884
885 /* Implement exponentially weighted moving average. A weight of 1/2 causes
886 * historical data to decay to <1% in 7 rebalancing runs. 1,000,000 bytes
887 * take 20 rebalancing runs to decay to 0 and get deleted entirely. */
888 for (e = &bond->hash[0]; e <= &bond->hash[BOND_MASK]; e++) {
889 e->tx_bytes /= 2;
890 if (!e->tx_bytes) {
891 e->slave = NULL;
892 }
893 }
894}
895\f
896/* Bonding unixctl user interface functions. */
897
898static struct bond *
899bond_find(const char *name)
900{
901 struct bond *bond;
902
903 HMAP_FOR_EACH_WITH_HASH (bond, hmap_node, hash_string(name, 0),
904 &all_bonds) {
905 if (!strcmp(bond->name, name)) {
906 return bond;
907 }
908 }
909 return NULL;
910}
911
912static struct bond_slave *
913bond_lookup_slave(struct bond *bond, const char *slave_name)
914{
915 struct bond_slave *slave;
916
917 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
918 if (!strcmp(slave->name, slave_name)) {
919 return slave;
920 }
921 }
922 return NULL;
923}
924
925static void
926bond_unixctl_list(struct unixctl_conn *conn,
0e15264f
BP
927 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
928 void *aux OVS_UNUSED)
f620b43a
BP
929{
930 struct ds ds = DS_EMPTY_INITIALIZER;
931 const struct bond *bond;
932
933 ds_put_cstr(&ds, "bond\ttype\tslaves\n");
934
935 HMAP_FOR_EACH (bond, hmap_node, &all_bonds) {
936 const struct bond_slave *slave;
937 size_t i;
938
939 ds_put_format(&ds, "%s\t%s\t",
940 bond->name, bond_mode_to_string(bond->balance));
941
942 i = 0;
943 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
944 if (i++ > 0) {
945 ds_put_cstr(&ds, ", ");
946 }
947 ds_put_cstr(&ds, slave->name);
948 }
949 ds_put_char(&ds, '\n');
950 }
bde9f75d 951 unixctl_command_reply(conn, ds_cstr(&ds));
f620b43a
BP
952 ds_destroy(&ds);
953}
954
955static void
c33a8a25 956bond_print_details(struct ds *ds, const struct bond *bond)
f620b43a 957{
fc1d4f01
EJ
958 struct shash slave_shash = SHASH_INITIALIZER(&slave_shash);
959 const struct shash_node **sorted_slaves = NULL;
f620b43a 960 const struct bond_slave *slave;
fc1d4f01 961 int i;
f620b43a 962
c33a8a25
EJ
963 ds_put_format(ds, "---- %s ----\n", bond->name);
964 ds_put_format(ds, "bond_mode: %s\n",
f620b43a
BP
965 bond_mode_to_string(bond->balance));
966
c33a8a25 967 ds_put_format(ds, "bond-hash-basis: %"PRIu32"\n", bond->basis);
672d18b2 968
c33a8a25
EJ
969 ds_put_format(ds, "updelay: %d ms\n", bond->updelay);
970 ds_put_format(ds, "downdelay: %d ms\n", bond->downdelay);
f620b43a 971
1b137691 972 if (bond_is_balanced(bond)) {
c33a8a25 973 ds_put_format(ds, "next rebalance: %lld ms\n",
f620b43a
BP
974 bond->next_rebalance - time_msec());
975 }
976
bdebeece
EJ
977 ds_put_cstr(ds, "lacp_status: ");
978 switch (bond->lacp_status) {
979 case LACP_NEGOTIATED:
980 ds_put_cstr(ds, "negotiated\n");
981 break;
982 case LACP_CONFIGURED:
983 ds_put_cstr(ds, "configured\n");
984 break;
985 case LACP_DISABLED:
986 ds_put_cstr(ds, "off\n");
987 break;
988 default:
989 ds_put_cstr(ds, "<unknown>\n");
990 break;
991 }
4d6fb5eb 992
f620b43a 993 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
fc1d4f01
EJ
994 shash_add(&slave_shash, slave->name, slave);
995 }
996 sorted_slaves = shash_sort(&slave_shash);
997
998 for (i = 0; i < shash_count(&slave_shash); i++) {
f620b43a 999 struct bond_entry *be;
f620b43a 1000
fc1d4f01
EJ
1001 slave = sorted_slaves[i]->data;
1002
f620b43a 1003 /* Basic info. */
c33a8a25 1004 ds_put_format(ds, "\nslave %s: %s\n",
f620b43a
BP
1005 slave->name, slave->enabled ? "enabled" : "disabled");
1006 if (slave == bond->active_slave) {
c33a8a25 1007 ds_put_cstr(ds, "\tactive slave\n");
f620b43a
BP
1008 }
1009 if (slave->delay_expires != LLONG_MAX) {
c33a8a25 1010 ds_put_format(ds, "\t%s expires in %lld ms\n",
f620b43a
BP
1011 slave->enabled ? "downdelay" : "updelay",
1012 slave->delay_expires - time_msec());
1013 }
1014
c33a8a25 1015 ds_put_format(ds, "\tmay_enable: %s\n",
296f6519 1016 slave->may_enable ? "true" : "false");
4d6fb5eb 1017
1b137691 1018 if (!bond_is_balanced(bond)) {
f620b43a
BP
1019 continue;
1020 }
1021
1022 /* Hashes. */
f620b43a
BP
1023 for (be = bond->hash; be <= &bond->hash[BOND_MASK]; be++) {
1024 int hash = be - bond->hash;
1025
1026 if (be->slave != slave) {
1027 continue;
1028 }
1029
c33a8a25 1030 ds_put_format(ds, "\thash %d: %"PRIu64" kB load\n",
f620b43a
BP
1031 hash, be->tx_bytes / 1024);
1032
7b9f1974 1033 /* XXX How can we list the MACs assigned to hashes of SLB bonds? */
f620b43a
BP
1034 }
1035 }
fc1d4f01
EJ
1036 shash_destroy(&slave_shash);
1037 free(sorted_slaves);
c33a8a25
EJ
1038 ds_put_cstr(ds, "\n");
1039}
1040
1041static void
1042bond_unixctl_show(struct unixctl_conn *conn,
1043 int argc, const char *argv[],
1044 void *aux OVS_UNUSED)
1045{
1046 struct ds ds = DS_EMPTY_INITIALIZER;
1047
1048 if (argc > 1) {
1049 const struct bond *bond = bond_find(argv[1]);
1050
1051 if (!bond) {
bde9f75d 1052 unixctl_command_reply_error(conn, "no such bond");
c33a8a25
EJ
1053 return;
1054 }
1055 bond_print_details(&ds, bond);
1056 } else {
1057 const struct bond *bond;
1058
1059 HMAP_FOR_EACH (bond, hmap_node, &all_bonds) {
1060 bond_print_details(&ds, bond);
1061 }
1062 }
1063
bde9f75d 1064 unixctl_command_reply(conn, ds_cstr(&ds));
f620b43a
BP
1065 ds_destroy(&ds);
1066}
1067
1068static void
0e15264f
BP
1069bond_unixctl_migrate(struct unixctl_conn *conn,
1070 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1071 void *aux OVS_UNUSED)
1072{
0e15264f
BP
1073 const char *bond_s = argv[1];
1074 const char *hash_s = argv[2];
1075 const char *slave_s = argv[3];
f620b43a
BP
1076 struct bond *bond;
1077 struct bond_slave *slave;
1078 struct bond_entry *entry;
1079 int hash;
1080
f620b43a
BP
1081 bond = bond_find(bond_s);
1082 if (!bond) {
bde9f75d 1083 unixctl_command_reply_error(conn, "no such bond");
f620b43a
BP
1084 return;
1085 }
1086
1087 if (bond->balance != BM_SLB) {
bde9f75d 1088 unixctl_command_reply_error(conn, "not an SLB bond");
f620b43a
BP
1089 return;
1090 }
1091
1092 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
1093 hash = atoi(hash_s) & BOND_MASK;
1094 } else {
bde9f75d 1095 unixctl_command_reply_error(conn, "bad hash");
f620b43a
BP
1096 return;
1097 }
1098
1099 slave = bond_lookup_slave(bond, slave_s);
1100 if (!slave) {
bde9f75d 1101 unixctl_command_reply_error(conn, "no such slave");
f620b43a
BP
1102 return;
1103 }
1104
1105 if (!slave->enabled) {
bde9f75d 1106 unixctl_command_reply_error(conn, "cannot migrate to disabled slave");
f620b43a
BP
1107 return;
1108 }
1109
1110 entry = &bond->hash[hash];
1111 tag_set_add(&bond->unixctl_tags, entry->tag);
1112 entry->slave = slave;
1113 entry->tag = tag_create_random();
bde9f75d 1114 unixctl_command_reply(conn, "migrated");
f620b43a
BP
1115}
1116
1117static void
0e15264f
BP
1118bond_unixctl_set_active_slave(struct unixctl_conn *conn,
1119 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1120 void *aux OVS_UNUSED)
1121{
0e15264f
BP
1122 const char *bond_s = argv[1];
1123 const char *slave_s = argv[2];
f620b43a
BP
1124 struct bond *bond;
1125 struct bond_slave *slave;
1126
f620b43a
BP
1127 bond = bond_find(bond_s);
1128 if (!bond) {
bde9f75d 1129 unixctl_command_reply_error(conn, "no such bond");
f620b43a
BP
1130 return;
1131 }
1132
1133 slave = bond_lookup_slave(bond, slave_s);
1134 if (!slave) {
bde9f75d 1135 unixctl_command_reply_error(conn, "no such slave");
f620b43a
BP
1136 return;
1137 }
1138
1139 if (!slave->enabled) {
bde9f75d 1140 unixctl_command_reply_error(conn, "cannot make disabled slave active");
f620b43a
BP
1141 return;
1142 }
1143
1144 if (bond->active_slave != slave) {
1145 tag_set_add(&bond->unixctl_tags, bond_get_active_slave_tag(bond));
1146 bond->active_slave = slave;
1147 bond->active_slave->tag = tag_create_random();
1148 VLOG_INFO("bond %s: active interface is now %s",
1149 bond->name, slave->name);
1150 bond->send_learning_packets = true;
bde9f75d 1151 unixctl_command_reply(conn, "done");
f620b43a 1152 } else {
bde9f75d 1153 unixctl_command_reply(conn, "no change");
f620b43a
BP
1154 }
1155}
1156
1157static void
0e15264f 1158enable_slave(struct unixctl_conn *conn, const char *argv[], bool enable)
f620b43a 1159{
0e15264f
BP
1160 const char *bond_s = argv[1];
1161 const char *slave_s = argv[2];
f620b43a
BP
1162 struct bond *bond;
1163 struct bond_slave *slave;
1164
f620b43a
BP
1165 bond = bond_find(bond_s);
1166 if (!bond) {
bde9f75d 1167 unixctl_command_reply_error(conn, "no such bond");
f620b43a
BP
1168 return;
1169 }
1170
1171 slave = bond_lookup_slave(bond, slave_s);
1172 if (!slave) {
bde9f75d 1173 unixctl_command_reply_error(conn, "no such slave");
f620b43a
BP
1174 return;
1175 }
1176
1177 bond_enable_slave(slave, enable, &bond->unixctl_tags);
bde9f75d 1178 unixctl_command_reply(conn, enable ? "enabled" : "disabled");
f620b43a
BP
1179}
1180
1181static void
0e15264f
BP
1182bond_unixctl_enable_slave(struct unixctl_conn *conn,
1183 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1184 void *aux OVS_UNUSED)
1185{
0e15264f 1186 enable_slave(conn, argv, true);
f620b43a
BP
1187}
1188
1189static void
0e15264f
BP
1190bond_unixctl_disable_slave(struct unixctl_conn *conn,
1191 int argc OVS_UNUSED, const char *argv[],
f620b43a
BP
1192 void *aux OVS_UNUSED)
1193{
0e15264f 1194 enable_slave(conn, argv, false);
f620b43a
BP
1195}
1196
1197static void
0e15264f 1198bond_unixctl_hash(struct unixctl_conn *conn, int argc, const char *argv[],
f620b43a
BP
1199 void *aux OVS_UNUSED)
1200{
0e15264f
BP
1201 const char *mac_s = argv[1];
1202 const char *vlan_s = argc > 2 ? argv[2] : NULL;
1203 const char *basis_s = argc > 3 ? argv[3] : NULL;
f620b43a
BP
1204 uint8_t mac[ETH_ADDR_LEN];
1205 uint8_t hash;
1206 char *hash_cstr;
1207 unsigned int vlan;
672d18b2 1208 uint32_t basis;
f620b43a
BP
1209
1210 if (vlan_s) {
1211 if (sscanf(vlan_s, "%u", &vlan) != 1) {
bde9f75d 1212 unixctl_command_reply_error(conn, "invalid vlan");
f620b43a
BP
1213 return;
1214 }
1215 } else {
dc155bff 1216 vlan = 0;
f620b43a
BP
1217 }
1218
672d18b2
EJ
1219 if (basis_s) {
1220 if (sscanf(basis_s, "%"PRIu32, &basis) != 1) {
bde9f75d 1221 unixctl_command_reply_error(conn, "invalid basis");
672d18b2
EJ
1222 return;
1223 }
1224 } else {
1225 basis = 0;
1226 }
1227
f620b43a
BP
1228 if (sscanf(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
1229 == ETH_ADDR_SCAN_COUNT) {
672d18b2 1230 hash = bond_hash_src(mac, vlan, basis) & BOND_MASK;
f620b43a
BP
1231
1232 hash_cstr = xasprintf("%u", hash);
bde9f75d 1233 unixctl_command_reply(conn, hash_cstr);
f620b43a
BP
1234 free(hash_cstr);
1235 } else {
bde9f75d 1236 unixctl_command_reply_error(conn, "invalid mac");
f620b43a
BP
1237 }
1238}
1239
1240void
1241bond_init(void)
1242{
0e15264f 1243 unixctl_command_register("bond/list", "", 0, 0, bond_unixctl_list, NULL);
c33a8a25
EJ
1244 unixctl_command_register("bond/show", "[port]", 0, 1, bond_unixctl_show,
1245 NULL);
0e15264f 1246 unixctl_command_register("bond/migrate", "port hash slave", 3, 3,
7ff2009a 1247 bond_unixctl_migrate, NULL);
0e15264f 1248 unixctl_command_register("bond/set-active-slave", "port slave", 2, 2,
f620b43a 1249 bond_unixctl_set_active_slave, NULL);
0e15264f 1250 unixctl_command_register("bond/enable-slave", "port slave", 2, 2,
7ff2009a 1251 bond_unixctl_enable_slave, NULL);
0e15264f 1252 unixctl_command_register("bond/disable-slave", "port slave", 2, 2,
7ff2009a 1253 bond_unixctl_disable_slave, NULL);
0e15264f 1254 unixctl_command_register("bond/hash", "mac [vlan] [basis]", 1, 3,
7ff2009a 1255 bond_unixctl_hash, NULL);
f620b43a
BP
1256}
1257\f
95aafb2a
EJ
1258static void
1259bond_entry_reset(struct bond *bond)
1260{
1261 if (bond->balance != BM_AB) {
1262 size_t hash_len = (BOND_MASK + 1) * sizeof *bond->hash;
1263
1264 if (!bond->hash) {
1265 bond->hash = xmalloc(hash_len);
1266 }
1267 memset(bond->hash, 0, hash_len);
1268
1269 bond->next_rebalance = time_msec() + bond->rebalance_interval;
1270 } else {
1271 free(bond->hash);
1272 bond->hash = NULL;
1273 }
1274}
1275
f620b43a
BP
1276static struct bond_slave *
1277bond_slave_lookup(struct bond *bond, const void *slave_)
1278{
1279 struct bond_slave *slave;
1280
1281 HMAP_FOR_EACH_IN_BUCKET (slave, hmap_node, hash_pointer(slave_, 0),
1282 &bond->slaves) {
1283 if (slave->aux == slave_) {
1284 return slave;
1285 }
1286 }
1287
1288 return NULL;
1289}
1290
f620b43a
BP
1291static void
1292bond_enable_slave(struct bond_slave *slave, bool enable, struct tag_set *tags)
1293{
7321e30e 1294 struct bond *bond = slave->bond;
f620b43a
BP
1295 slave->delay_expires = LLONG_MAX;
1296 if (enable != slave->enabled) {
1297 slave->enabled = enable;
1298 if (!slave->enabled) {
1299 VLOG_WARN("interface %s: disabled", slave->name);
b3c18f66
EJ
1300 if (tags) {
1301 tag_set_add(tags, slave->tag);
1302 }
f620b43a
BP
1303 } else {
1304 VLOG_WARN("interface %s: enabled", slave->name);
1305 slave->tag = tag_create_random();
1306 }
7321e30e
EJ
1307
1308 if (bond->balance == BM_STABLE) {
1309 bond->bond_revalidate = true;
1310 }
f620b43a
BP
1311 }
1312}
1313
1314static void
1315bond_link_status_update(struct bond_slave *slave, struct tag_set *tags)
1316{
1317 struct bond *bond = slave->bond;
1318 bool up;
1319
296f6519 1320 up = netdev_get_carrier(slave->netdev) && slave->may_enable;
f620b43a
BP
1321 if ((up == slave->enabled) != (slave->delay_expires == LLONG_MAX)) {
1322 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1323 VLOG_INFO_RL(&rl, "interface %s: link state %s",
1324 slave->name, up ? "up" : "down");
1325 if (up == slave->enabled) {
1326 slave->delay_expires = LLONG_MAX;
1327 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
1328 slave->name, up ? "disabled" : "enabled");
1329 } else {
bdebeece 1330 int delay = (bond->lacp_status != LACP_DISABLED ? 0
f620b43a
BP
1331 : up ? bond->updelay : bond->downdelay);
1332 slave->delay_expires = time_msec() + delay;
1333 if (delay) {
1334 VLOG_INFO_RL(&rl, "interface %s: will be %s if it stays %s "
1335 "for %d ms",
1336 slave->name,
1337 up ? "enabled" : "disabled",
1338 up ? "up" : "down",
1339 delay);
1340 }
1341 }
1342 }
1343
1344 if (time_msec() >= slave->delay_expires) {
1345 bond_enable_slave(slave, up, tags);
1346 }
1347}
1348
f620b43a 1349static unsigned int
672d18b2 1350bond_hash_src(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan, uint32_t basis)
f620b43a 1351{
672d18b2 1352 return hash_3words(hash_bytes(mac, ETH_ADDR_LEN, 0), vlan, basis);
f620b43a
BP
1353}
1354
1355static unsigned int
672d18b2 1356bond_hash_tcp(const struct flow *flow, uint16_t vlan, uint32_t basis)
f620b43a
BP
1357{
1358 struct flow hash_flow = *flow;
d84d4b88 1359 hash_flow.vlan_tci = htons(vlan);
f620b43a
BP
1360
1361 /* The symmetric quality of this hash function is not required, but
1362 * flow_hash_symmetric_l4 already exists, and is sufficient for our
1363 * purposes, so we use it out of convenience. */
672d18b2 1364 return flow_hash_symmetric_l4(&hash_flow, basis);
f620b43a
BP
1365}
1366
fb0b29a3
EJ
1367static unsigned int
1368bond_hash(const struct bond *bond, const struct flow *flow, uint16_t vlan)
1369{
bdebeece 1370 assert(bond->balance == BM_TCP || bond->balance == BM_SLB);
fb0b29a3 1371
bdebeece 1372 return (bond->balance == BM_TCP
672d18b2
EJ
1373 ? bond_hash_tcp(flow, vlan, bond->basis)
1374 : bond_hash_src(flow->dl_src, vlan, bond->basis));
fb0b29a3
EJ
1375}
1376
f620b43a
BP
1377static struct bond_entry *
1378lookup_bond_entry(const struct bond *bond, const struct flow *flow,
1379 uint16_t vlan)
1380{
fb0b29a3 1381 return &bond->hash[bond_hash(bond, flow, vlan) & BOND_MASK];
f620b43a
BP
1382}
1383
7321e30e
EJ
1384/* This function uses Highest Random Weight hashing to choose an output slave.
1385 * This approach only reassigns a minimal number of flows when slaves are
1386 * enabled or disabled. Unfortunately, it has O(n) performance against the
1387 * number of slaves. There exist algorithms which are O(1), but have slightly
1388 * more complex implementations and require the use of memory. This may need
1389 * to be reimplemented if it becomes a performance bottleneck. */
1390static struct bond_slave *
bc1b010c 1391choose_stb_slave(const struct bond *bond, uint32_t flow_hash)
7321e30e
EJ
1392{
1393 struct bond_slave *best, *slave;
bc1b010c 1394 uint32_t best_hash;
7321e30e
EJ
1395
1396 best = NULL;
1397 best_hash = 0;
7321e30e
EJ
1398 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1399 if (slave->enabled) {
1400 uint32_t hash;
1401
1402 hash = hash_2words(flow_hash, slave->stb_id);
1403 if (!best || hash > best_hash) {
1404 best = slave;
1405 best_hash = hash;
1406 }
1407 }
1408 }
1409
1410 return best;
1411}
1412
f620b43a
BP
1413static struct bond_slave *
1414choose_output_slave(const struct bond *bond, const struct flow *flow,
00ed8314 1415 uint16_t vlan, tag_type *tags)
f620b43a
BP
1416{
1417 struct bond_entry *e;
1418
bdebeece
EJ
1419 if (bond->lacp_status == LACP_CONFIGURED) {
1420 /* LACP has been configured on this bond but negotiations were
1421 * unsuccussful. Drop all traffic. */
1422 return NULL;
1423 }
1424
f620b43a
BP
1425 switch (bond->balance) {
1426 case BM_AB:
1427 return bond->active_slave;
1428
fb0b29a3 1429 case BM_STABLE:
bc1b010c 1430 return choose_stb_slave(bond, bond_hash_tcp(flow, vlan, bond->basis));
bdebeece 1431
f620b43a 1432 case BM_TCP:
bdebeece
EJ
1433 if (bond->lacp_status != LACP_NEGOTIATED) {
1434 /* Must have LACP negotiations for TCP balanced bonds. */
1435 return NULL;
1436 }
1437 /* Fall Through. */
1438 case BM_SLB:
bc1b010c
EJ
1439 if (!bond_is_balanced(bond)) {
1440 return choose_stb_slave(bond, bond_hash(bond, flow, vlan));
1441 }
f620b43a
BP
1442 e = lookup_bond_entry(bond, flow, vlan);
1443 if (!e->slave || !e->slave->enabled) {
c804cadf
EJ
1444 e->slave = CONTAINER_OF(hmap_random_node(&bond->slaves),
1445 struct bond_slave, hmap_node);
1446 if (!e->slave->enabled) {
1447 e->slave = bond->active_slave;
1448 }
f620b43a
BP
1449 e->tag = tag_create_random();
1450 }
00ed8314 1451 *tags |= e->tag;
f620b43a
BP
1452 return e->slave;
1453
1454 default:
1455 NOT_REACHED();
1456 }
1457}
1458
1459static struct bond_slave *
1460bond_choose_slave(const struct bond *bond)
1461{
1462 struct bond_slave *slave, *best;
1463
1464 /* Find an enabled slave. */
1465 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1466 if (slave->enabled) {
1467 return slave;
1468 }
1469 }
1470
1471 /* All interfaces are disabled. Find an interface that will be enabled
1472 * after its updelay expires. */
1473 best = NULL;
1474 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1475 if (slave->delay_expires != LLONG_MAX
296f6519 1476 && slave->may_enable
f620b43a
BP
1477 && (!best || slave->delay_expires < best->delay_expires)) {
1478 best = slave;
1479 }
1480 }
1481 return best;
1482}
1483
1484static void
1485bond_choose_active_slave(struct bond *bond, struct tag_set *tags)
1486{
1487 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1488 struct bond_slave *old_active_slave = bond->active_slave;
1489
1490 bond->active_slave = bond_choose_slave(bond);
1491 if (bond->active_slave) {
1492 if (bond->active_slave->enabled) {
1493 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s",
1494 bond->name, bond->active_slave->name);
1495 } else {
1496 VLOG_INFO_RL(&rl, "bond %s: active interface is now %s, skipping "
1497 "remaining %lld ms updelay (since no interface was "
1498 "enabled)", bond->name, bond->active_slave->name,
1499 bond->active_slave->delay_expires - time_msec());
1500 bond_enable_slave(bond->active_slave, true, tags);
1501 }
1502
1503 if (!old_active_slave) {
1504 tag_set_add(tags, bond->no_slaves_tag);
1505 }
1506
1507 bond->send_learning_packets = true;
1508 } else if (old_active_slave) {
1509 VLOG_WARN_RL(&rl, "bond %s: all interfaces disabled", bond->name);
1510 }
1511}
1512
1513/* Returns the tag for 'bond''s active slave, or 'bond''s no_slaves_tag if
1514 * there is no active slave. */
1515static tag_type
1516bond_get_active_slave_tag(const struct bond *bond)
1517{
1518 return (bond->active_slave
1519 ? bond->active_slave->tag
1520 : bond->no_slaves_tag);
1521}
1522
1523/* Attempts to make the sum of the bond slaves' statistics appear on the fake
1524 * bond interface. */
1525static void
1526bond_update_fake_slave_stats(struct bond *bond)
1527{
1528 struct netdev_stats bond_stats;
1529 struct bond_slave *slave;
1530 struct netdev *bond_dev;
1531
1532 memset(&bond_stats, 0, sizeof bond_stats);
1533
1534 HMAP_FOR_EACH (slave, hmap_node, &bond->slaves) {
1535 struct netdev_stats slave_stats;
1536
1537 if (!netdev_get_stats(slave->netdev, &slave_stats)) {
1538 /* XXX: We swap the stats here because they are swapped back when
1539 * reported by the internal device. The reason for this is
1540 * internal devices normally represent packets going into the
1541 * system but when used as fake bond device they represent packets
1542 * leaving the system. We really should do this in the internal
1543 * device itself because changing it here reverses the counts from
1544 * the perspective of the switch. However, the internal device
1545 * doesn't know what type of device it represents so we have to do
1546 * it here for now. */
1547 bond_stats.tx_packets += slave_stats.rx_packets;
1548 bond_stats.tx_bytes += slave_stats.rx_bytes;
1549 bond_stats.rx_packets += slave_stats.tx_packets;
1550 bond_stats.rx_bytes += slave_stats.tx_bytes;
1551 }
1552 }
1553
18812dff 1554 if (!netdev_open(bond->name, "system", &bond_dev)) {
f620b43a
BP
1555 netdev_set_stats(bond_dev, &bond_stats);
1556 netdev_close(bond_dev);
1557 }
1558}