2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "connectivity.h"
28 #include "dp-packet.h"
30 #include "openvswitch/hmap.h"
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"
42 #include "openvswitch/ofpbuf.h"
43 #include "openvswitch/vlog.h"
45 #include "poll-loop.h"
47 #include "openvswitch/shash.h"
52 VLOG_DEFINE_THIS_MODULE(bond
);
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__
;
58 /* Bit-mask for hashing a flow down to a bucket. */
59 #define BOND_MASK 0xff
60 #define BOND_BUCKETS (BOND_MASK + 1)
62 /* Priority for internal rules created to handle recirculation */
63 #define RECIRC_RULE_PRIORITY 20
65 /* A hash bucket for mapping a flow to a slave.
66 * "struct bond" has an array of BOND_BUCKETS of these. */
68 struct bond_slave
*slave
; /* Assigned slave, NULL if unassigned. */
69 uint64_t tx_bytes
/* Count of bytes recently transmitted. */
70 OVS_GUARDED_BY(rwlock
);
71 struct ovs_list list_node
; /* In bond_slave's 'entries' list. */
75 * 'pr_rule' is the post-recirculation rule for this entry.
76 * 'pr_tx_bytes' is the most recently seen statistics for 'pr_rule', which
77 * is used to determine delta (applied to 'tx_bytes' above.) */
79 uint64_t pr_tx_bytes
OVS_GUARDED_BY(rwlock
);
82 /* A bond slave, that is, one of the links comprising a bond. */
84 struct hmap_node hmap_node
; /* In struct bond's slaves hmap. */
85 struct ovs_list list_node
; /* In struct bond's enabled_slaves list. */
86 struct bond
*bond
; /* The bond that contains this slave. */
87 void *aux
; /* Client-provided handle for this slave. */
89 struct netdev
*netdev
; /* Network device, owned by the client. */
90 uint64_t change_seq
; /* Tracks changes in 'netdev'. */
91 ofp_port_t ofp_port
; /* OpenFlow port number. */
92 char *name
; /* Name (a copy of netdev_get_name(netdev)). */
95 long long delay_expires
; /* Time after which 'enabled' may change. */
96 bool enabled
; /* May be chosen for flows? */
97 bool may_enable
; /* Client considers this slave bondable. */
99 /* Rebalancing info. Used only by bond_rebalance(). */
100 struct ovs_list bal_node
; /* In bond_rebalance()'s 'bals' list. */
101 struct ovs_list entries
; /* 'struct bond_entry's assigned here. */
102 uint64_t tx_bytes
; /* Sum across 'tx_bytes' of entries. */
105 /* A bond, that is, a set of network devices grouped to improve performance or
108 struct hmap_node hmap_node
; /* In 'all_bonds' hmap. */
109 char *name
; /* Name provided by client. */
110 struct ofproto_dpif
*ofproto
; /* The bridge this bond belongs to. */
117 * Any reader or writer of 'enabled_slaves' must hold 'mutex'.
118 * (To prevent the bond_slave from disappearing they must also hold
120 struct ovs_mutex mutex
OVS_ACQ_AFTER(rwlock
);
121 struct ovs_list enabled_slaves OVS_GUARDED
; /* Contains struct bond_slaves. */
124 enum bond_mode balance
; /* Balancing mode, one of BM_*. */
125 struct bond_slave
*active_slave
;
126 int updelay
, downdelay
; /* Delay before slave goes up/down, in ms. */
127 enum lacp_status lacp_status
; /* Status of LACP negotiations. */
128 bool bond_revalidate
; /* True if flows need revalidation. */
129 uint32_t basis
; /* Basis for flow hash function. */
131 /* SLB specific bonding info. */
132 struct bond_entry
*hash
; /* An array of BOND_BUCKETS elements. */
133 int rebalance_interval
; /* Interval between rebalances, in ms. */
134 long long int next_rebalance
; /* Next rebalancing time. */
135 bool send_learning_packets
;
136 uint32_t recirc_id
; /* Non zero if recirculation can be used.*/
137 struct hmap pr_rule_ops
; /* Helps to maintain post recirculation rules.*/
139 /* Store active slave to OVSDB. */
140 bool active_slave_changed
; /* Set to true whenever the bond changes
141 active slave. It will be reset to false
142 after it is stored into OVSDB */
144 /* Interface name may not be persistent across an OS reboot, use
145 * MAC address for identifing the active slave */
146 struct eth_addr active_slave_mac
;
147 /* The MAC address of the active interface. */
148 /* Legacy compatibility. */
149 bool lacp_fallback_ab
; /* Fallback to active-backup on LACP failure. */
151 struct ovs_refcount ref_cnt
;
154 /* What to do with an bond_recirc_rule. */
156 ADD
, /* Add the rule to ofproto's flow table. */
157 DEL
, /* Delete the rule from the ofproto's flow table. */
160 /* A rule to add to or delete from ofproto's internal flow table. */
161 struct bond_pr_rule_op
{
162 struct hmap_node hmap_node
;
164 ofp_port_t out_ofport
;
166 struct rule
**pr_rule
;
169 static void bond_entry_reset(struct bond
*) OVS_REQ_WRLOCK(rwlock
);
170 static struct bond_slave
*bond_slave_lookup(struct bond
*, const void *slave_
)
171 OVS_REQ_RDLOCK(rwlock
);
172 static void bond_enable_slave(struct bond_slave
*, bool enable
)
173 OVS_REQ_WRLOCK(rwlock
);
174 static void bond_link_status_update(struct bond_slave
*)
175 OVS_REQ_WRLOCK(rwlock
);
176 static void bond_choose_active_slave(struct bond
*)
177 OVS_REQ_WRLOCK(rwlock
);
178 static unsigned int bond_hash_src(const struct eth_addr mac
,
179 uint16_t vlan
, uint32_t basis
);
180 static unsigned int bond_hash_tcp(const struct flow
*, uint16_t vlan
,
182 static struct bond_entry
*lookup_bond_entry(const struct bond
*,
185 OVS_REQ_RDLOCK(rwlock
);
186 static struct bond_slave
*get_enabled_slave(struct bond
*)
187 OVS_REQ_RDLOCK(rwlock
);
188 static struct bond_slave
*choose_output_slave(const struct bond
*,
190 struct flow_wildcards
*,
192 OVS_REQ_RDLOCK(rwlock
);
194 /* Attempts to parse 's' as the name of a bond balancing mode. If successful,
195 * stores the mode in '*balance' and returns true. Otherwise returns false
196 * without modifying '*balance'. */
198 bond_mode_from_string(enum bond_mode
*balance
, const char *s
)
200 if (!strcmp(s
, bond_mode_to_string(BM_TCP
))) {
202 } else if (!strcmp(s
, bond_mode_to_string(BM_SLB
))) {
204 } else if (!strcmp(s
, bond_mode_to_string(BM_AB
))) {
212 /* Returns a string representing 'balance'. */
214 bond_mode_to_string(enum bond_mode balance
) {
217 return "balance-tcp";
219 return "balance-slb";
221 return "active-backup";
227 /* Creates and returns a new bond whose configuration is initially taken from
230 * The caller should register each slave on the new bond by calling
231 * bond_slave_register(). */
233 bond_create(const struct bond_settings
*s
, struct ofproto_dpif
*ofproto
)
237 bond
= xzalloc(sizeof *bond
);
238 bond
->ofproto
= ofproto
;
239 hmap_init(&bond
->slaves
);
240 ovs_list_init(&bond
->enabled_slaves
);
241 ovs_mutex_init(&bond
->mutex
);
242 ovs_refcount_init(&bond
->ref_cnt
);
243 hmap_init(&bond
->pr_rule_ops
);
245 bond
->active_slave_mac
= eth_addr_zero
;
246 bond
->active_slave_changed
= false;
248 bond_reconfigure(bond
, s
);
253 bond_ref(const struct bond
*bond_
)
255 struct bond
*bond
= CONST_CAST(struct bond
*, bond_
);
258 ovs_refcount_ref(&bond
->ref_cnt
);
265 bond_unref(struct bond
*bond
)
267 struct bond_pr_rule_op
*pr_op
;
268 struct bond_slave
*slave
;
270 if (!bond
|| ovs_refcount_unref_relaxed(&bond
->ref_cnt
) != 1) {
274 ovs_rwlock_wrlock(&rwlock
);
275 hmap_remove(all_bonds
, &bond
->hmap_node
);
276 ovs_rwlock_unlock(&rwlock
);
278 HMAP_FOR_EACH_POP (slave
, hmap_node
, &bond
->slaves
) {
279 /* Client owns 'slave->netdev'. */
283 hmap_destroy(&bond
->slaves
);
285 ovs_mutex_destroy(&bond
->mutex
);
289 HMAP_FOR_EACH_POP (pr_op
, hmap_node
, &bond
->pr_rule_ops
) {
292 hmap_destroy(&bond
->pr_rule_ops
);
294 if (bond
->recirc_id
) {
295 recirc_free_id(bond
->recirc_id
);
302 add_pr_rule(struct bond
*bond
, const struct match
*match
,
303 ofp_port_t out_ofport
, struct rule
**rule
)
305 uint32_t hash
= match_hash(match
, 0);
306 struct bond_pr_rule_op
*pr_op
;
308 HMAP_FOR_EACH_WITH_HASH(pr_op
, hmap_node
, hash
, &bond
->pr_rule_ops
) {
309 if (match_equal(&pr_op
->match
, match
)) {
311 pr_op
->out_ofport
= out_ofport
;
312 pr_op
->pr_rule
= rule
;
317 pr_op
= xmalloc(sizeof *pr_op
);
318 pr_op
->match
= *match
;
320 pr_op
->out_ofport
= out_ofport
;
321 pr_op
->pr_rule
= rule
;
322 hmap_insert(&bond
->pr_rule_ops
, &pr_op
->hmap_node
, hash
);
326 update_recirc_rules(struct bond
*bond
)
327 OVS_REQ_WRLOCK(rwlock
)
330 struct bond_pr_rule_op
*pr_op
, *next_op
;
331 uint64_t ofpacts_stub
[128 / 8];
332 struct ofpbuf ofpacts
;
335 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
337 HMAP_FOR_EACH(pr_op
, hmap_node
, &bond
->pr_rule_ops
) {
341 if (bond
->hash
&& bond
->recirc_id
) {
342 for (i
= 0; i
< BOND_BUCKETS
; i
++) {
343 struct bond_slave
*slave
= bond
->hash
[i
].slave
;
346 match_init_catchall(&match
);
347 match_set_recirc_id(&match
, bond
->recirc_id
);
348 match_set_dp_hash_masked(&match
, i
, BOND_MASK
);
350 add_pr_rule(bond
, &match
, slave
->ofp_port
,
351 &bond
->hash
[i
].pr_rule
);
356 HMAP_FOR_EACH_SAFE(pr_op
, next_op
, hmap_node
, &bond
->pr_rule_ops
) {
360 ofpbuf_clear(&ofpacts
);
361 ofpact_put_OUTPUT(&ofpacts
)->port
= pr_op
->out_ofport
;
362 error
= ofproto_dpif_add_internal_flow(bond
->ofproto
,
364 RECIRC_RULE_PRIORITY
, 0,
365 &ofpacts
, pr_op
->pr_rule
);
367 char *err_s
= match_to_string(&pr_op
->match
,
368 RECIRC_RULE_PRIORITY
);
370 VLOG_ERR("failed to add post recirculation flow %s", err_s
);
376 error
= ofproto_dpif_delete_internal_flow(bond
->ofproto
,
378 RECIRC_RULE_PRIORITY
);
380 char *err_s
= match_to_string(&pr_op
->match
,
381 RECIRC_RULE_PRIORITY
);
383 VLOG_ERR("failed to remove post recirculation flow %s", err_s
);
387 hmap_remove(&bond
->pr_rule_ops
, &pr_op
->hmap_node
);
388 *pr_op
->pr_rule
= NULL
;
394 ofpbuf_uninit(&ofpacts
);
398 /* Updates 'bond''s overall configuration to 's'.
400 * The caller should register each slave on 'bond' by calling
401 * bond_slave_register(). This is optional if none of the slaves'
402 * configuration has changed. In any case it can't hurt.
404 * Returns true if the configuration has changed in such a way that requires
408 bond_reconfigure(struct bond
*bond
, const struct bond_settings
*s
)
410 bool revalidate
= false;
412 ovs_rwlock_wrlock(&rwlock
);
413 if (!bond
->name
|| strcmp(bond
->name
, s
->name
)) {
415 hmap_remove(all_bonds
, &bond
->hmap_node
);
418 bond
->name
= xstrdup(s
->name
);
419 hmap_insert(all_bonds
, &bond
->hmap_node
, hash_string(bond
->name
, 0));
422 bond
->updelay
= s
->up_delay
;
423 bond
->downdelay
= s
->down_delay
;
425 if (bond
->lacp_fallback_ab
!= s
->lacp_fallback_ab_cfg
) {
426 bond
->lacp_fallback_ab
= s
->lacp_fallback_ab_cfg
;
430 if (bond
->rebalance_interval
!= s
->rebalance_interval
) {
431 bond
->rebalance_interval
= s
->rebalance_interval
;
435 if (bond
->balance
!= s
->balance
) {
436 bond
->balance
= s
->balance
;
440 if (bond
->basis
!= s
->basis
) {
441 bond
->basis
= s
->basis
;
445 if (bond
->bond_revalidate
) {
447 bond
->bond_revalidate
= false;
450 if (bond
->balance
!= BM_AB
) {
451 if (!bond
->recirc_id
) {
452 bond
->recirc_id
= recirc_alloc_id(bond
->ofproto
);
454 } else if (bond
->recirc_id
) {
455 recirc_free_id(bond
->recirc_id
);
459 if (bond
->balance
== BM_AB
|| !bond
->hash
|| revalidate
) {
460 bond_entry_reset(bond
);
463 ovs_rwlock_unlock(&rwlock
);
467 static struct bond_slave
*
468 bond_find_slave_by_mac(const struct bond
*bond
, const struct eth_addr mac
)
470 struct bond_slave
*slave
;
472 /* Find the last active slave */
473 HMAP_FOR_EACH(slave
, hmap_node
, &bond
->slaves
) {
474 struct eth_addr slave_mac
;
476 if (netdev_get_etheraddr(slave
->netdev
, &slave_mac
)) {
480 if (eth_addr_equals(slave_mac
, mac
)) {
489 bond_active_slave_changed(struct bond
*bond
)
491 if (bond
->active_slave
) {
493 netdev_get_etheraddr(bond
->active_slave
->netdev
, &mac
);
494 bond
->active_slave_mac
= mac
;
496 bond
->active_slave_mac
= eth_addr_zero
;
498 bond
->active_slave_changed
= true;
499 seq_change(connectivity_seq_get());
503 bond_slave_set_netdev__(struct bond_slave
*slave
, struct netdev
*netdev
)
504 OVS_REQ_WRLOCK(rwlock
)
506 if (slave
->netdev
!= netdev
) {
507 slave
->netdev
= netdev
;
508 slave
->change_seq
= 0;
512 /* Registers 'slave_' as a slave of 'bond'. The 'slave_' pointer is an
513 * arbitrary client-provided pointer that uniquely identifies a slave within a
514 * bond. If 'slave_' already exists within 'bond' then this function
515 * reconfigures the existing slave.
517 * 'netdev' must be the network device that 'slave_' represents. It is owned
518 * by the client, so the client must not close it before either unregistering
519 * 'slave_' or destroying 'bond'.
522 bond_slave_register(struct bond
*bond
, void *slave_
,
523 ofp_port_t ofport
, struct netdev
*netdev
)
525 struct bond_slave
*slave
;
527 ovs_rwlock_wrlock(&rwlock
);
528 slave
= bond_slave_lookup(bond
, slave_
);
530 slave
= xzalloc(sizeof *slave
);
532 hmap_insert(&bond
->slaves
, &slave
->hmap_node
, hash_pointer(slave_
, 0));
535 slave
->ofp_port
= ofport
;
536 slave
->delay_expires
= LLONG_MAX
;
537 slave
->name
= xstrdup(netdev_get_name(netdev
));
538 bond
->bond_revalidate
= true;
540 slave
->enabled
= false;
541 bond_enable_slave(slave
, netdev_get_carrier(netdev
));
544 bond_slave_set_netdev__(slave
, netdev
);
547 slave
->name
= xstrdup(netdev_get_name(netdev
));
548 ovs_rwlock_unlock(&rwlock
);
551 /* Updates the network device to be used with 'slave_' to 'netdev'.
553 * This is useful if the caller closes and re-opens the network device
554 * registered with bond_slave_register() but doesn't need to change anything
557 bond_slave_set_netdev(struct bond
*bond
, void *slave_
, struct netdev
*netdev
)
559 struct bond_slave
*slave
;
561 ovs_rwlock_wrlock(&rwlock
);
562 slave
= bond_slave_lookup(bond
, slave_
);
564 bond_slave_set_netdev__(slave
, netdev
);
566 ovs_rwlock_unlock(&rwlock
);
569 /* Unregisters 'slave_' from 'bond'. If 'bond' does not contain such a slave
570 * then this function has no effect.
572 * Unregistering a slave invalidates all flows. */
574 bond_slave_unregister(struct bond
*bond
, const void *slave_
)
576 struct bond_slave
*slave
;
579 ovs_rwlock_wrlock(&rwlock
);
580 slave
= bond_slave_lookup(bond
, slave_
);
585 bond
->bond_revalidate
= true;
586 bond_enable_slave(slave
, false);
588 del_active
= bond
->active_slave
== slave
;
590 struct bond_entry
*e
;
591 for (e
= bond
->hash
; e
<= &bond
->hash
[BOND_MASK
]; e
++) {
592 if (e
->slave
== slave
) {
600 hmap_remove(&bond
->slaves
, &slave
->hmap_node
);
601 /* Client owns 'slave->netdev'. */
605 bond_choose_active_slave(bond
);
606 bond
->send_learning_packets
= true;
609 ovs_rwlock_unlock(&rwlock
);
612 /* Should be called on each slave in 'bond' before bond_run() to indicate
613 * whether or not 'slave_' may be enabled. This function is intended to allow
614 * other protocols to have some impact on bonding decisions. For example LACP
615 * or high level link monitoring protocols may decide that a given slave should
616 * not be able to send traffic. */
618 bond_slave_set_may_enable(struct bond
*bond
, void *slave_
, bool may_enable
)
620 ovs_rwlock_wrlock(&rwlock
);
621 bond_slave_lookup(bond
, slave_
)->may_enable
= may_enable
;
622 ovs_rwlock_unlock(&rwlock
);
625 /* Performs periodic maintenance on 'bond'.
627 * Returns true if the caller should revalidate its flows.
629 * The caller should check bond_should_send_learning_packets() afterward. */
631 bond_run(struct bond
*bond
, enum lacp_status lacp_status
)
633 struct bond_slave
*slave
;
636 ovs_rwlock_wrlock(&rwlock
);
637 if (bond
->lacp_status
!= lacp_status
) {
638 bond
->lacp_status
= lacp_status
;
639 bond
->bond_revalidate
= true;
642 /* Enable slaves based on link status and LACP feedback. */
643 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
644 bond_link_status_update(slave
);
645 slave
->change_seq
= seq_read(connectivity_seq_get());
647 if (!bond
->active_slave
|| !bond
->active_slave
->enabled
) {
648 bond_choose_active_slave(bond
);
651 revalidate
= bond
->bond_revalidate
;
652 bond
->bond_revalidate
= false;
653 ovs_rwlock_unlock(&rwlock
);
658 /* Causes poll_block() to wake up when 'bond' needs something to be done. */
660 bond_wait(struct bond
*bond
)
662 struct bond_slave
*slave
;
664 ovs_rwlock_rdlock(&rwlock
);
665 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
666 if (slave
->delay_expires
!= LLONG_MAX
) {
667 poll_timer_wait_until(slave
->delay_expires
);
670 seq_wait(connectivity_seq_get(), slave
->change_seq
);
673 if (bond
->bond_revalidate
) {
674 poll_immediate_wake();
676 ovs_rwlock_unlock(&rwlock
);
678 /* We don't wait for bond->next_rebalance because rebalancing can only run
679 * at a flow account checkpoint. ofproto does checkpointing on its own
680 * schedule and bond_rebalance() gets called afterward, so we'd just be
681 * waking up for no purpose. */
684 /* MAC learning table interaction. */
687 may_send_learning_packets(const struct bond
*bond
)
689 return ((bond
->lacp_status
== LACP_DISABLED
690 && (bond
->balance
== BM_SLB
|| bond
->balance
== BM_AB
))
691 || (bond
->lacp_fallback_ab
&& bond
->lacp_status
== LACP_CONFIGURED
))
692 && bond
->active_slave
;
695 /* Returns true if 'bond' needs the client to send out packets to assist with
696 * MAC learning on 'bond'. If this function returns true, then the client
697 * should iterate through its MAC learning table for the bridge on which 'bond'
698 * is located. For each MAC that has been learned on a port other than 'bond',
699 * it should call bond_compose_learning_packet().
701 * This function will only return true if 'bond' is in SLB or active-backup
702 * mode and LACP is not negotiated. Otherwise sending learning packets isn't
705 * Calling this function resets the state that it checks. */
707 bond_should_send_learning_packets(struct bond
*bond
)
711 ovs_rwlock_wrlock(&rwlock
);
712 send
= bond
->send_learning_packets
&& may_send_learning_packets(bond
);
713 bond
->send_learning_packets
= false;
714 ovs_rwlock_unlock(&rwlock
);
718 /* Sends a gratuitous learning packet on 'bond' from 'eth_src' on 'vlan'.
720 * See bond_should_send_learning_packets() for description of usage. The
721 * caller should send the composed packet on the port associated with
722 * port_aux and takes ownership of the returned ofpbuf. */
724 bond_compose_learning_packet(struct bond
*bond
, const struct eth_addr eth_src
,
725 uint16_t vlan
, void **port_aux
)
727 struct bond_slave
*slave
;
728 struct dp_packet
*packet
;
731 ovs_rwlock_rdlock(&rwlock
);
732 ovs_assert(may_send_learning_packets(bond
));
733 memset(&flow
, 0, sizeof flow
);
734 flow
.dl_src
= eth_src
;
735 slave
= choose_output_slave(bond
, &flow
, NULL
, vlan
);
737 packet
= dp_packet_new(0);
738 compose_rarp(packet
, eth_src
);
740 eth_push_vlan(packet
, htons(ETH_TYPE_VLAN
), htons(vlan
));
743 *port_aux
= slave
->aux
;
744 ovs_rwlock_unlock(&rwlock
);
748 /* Checks whether a packet that arrived on 'slave_' within 'bond', with an
749 * Ethernet destination address of 'eth_dst', should be admitted.
751 * The return value is one of the following:
753 * - BV_ACCEPT: Admit the packet.
755 * - BV_DROP: Drop the packet.
757 * - BV_DROP_IF_MOVED: Consult the MAC learning table for the packet's
758 * Ethernet source address and VLAN. If there is none, or if the packet
759 * is on the learned port, then admit the packet. If a different port has
760 * been learned, however, drop the packet (and do not use it for MAC
764 bond_check_admissibility(struct bond
*bond
, const void *slave_
,
765 const struct eth_addr eth_dst
)
767 enum bond_verdict verdict
= BV_DROP
;
768 struct bond_slave
*slave
;
770 ovs_rwlock_rdlock(&rwlock
);
771 slave
= bond_slave_lookup(bond
, slave_
);
776 /* LACP bonds have very loose admissibility restrictions because we can
777 * assume the remote switch is aware of the bond and will "do the right
778 * thing". However, as a precaution we drop packets on disabled slaves
779 * because no correctly implemented partner switch should be sending
782 * If LACP is configured, but LACP negotiations have been unsuccessful, we
783 * drop all incoming traffic except if lacp_fallback_ab is enabled. */
784 switch (bond
->lacp_status
) {
785 case LACP_NEGOTIATED
:
786 verdict
= slave
->enabled
? BV_ACCEPT
: BV_DROP
;
788 case LACP_CONFIGURED
:
789 if (!bond
->lacp_fallback_ab
) {
794 if (bond
->balance
== BM_TCP
) {
800 /* Drop all multicast packets on inactive slaves. */
801 if (eth_addr_is_multicast(eth_dst
)) {
802 if (bond
->active_slave
!= slave
) {
807 switch (bond
->balance
) {
809 /* TCP balanced bonds require successful LACP negotiations. Based on the
810 * above check, LACP is off or lacp_fallback_ab is true on this bond.
811 * If lacp_fallback_ab is true fall through to BM_AB case else, we
812 * drop all incoming traffic. */
813 if (!bond
->lacp_fallback_ab
) {
818 /* Drop all packets which arrive on backup slaves. This is similar to
819 * how Linux bonding handles active-backup bonds. */
820 if (bond
->active_slave
!= slave
) {
821 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
823 VLOG_DBG_RL(&rl
, "active-backup bond received packet on backup"
824 " slave (%s) destined for " ETH_ADDR_FMT
,
825 slave
->name
, ETH_ADDR_ARGS(eth_dst
));
832 /* Drop all packets for which we have learned a different input port,
833 * because we probably sent the packet on one slave and got it back on
834 * the other. Gratuitous ARP packets are an exception to this rule:
835 * the host has moved to another switch. The exception to the
836 * exception is if we locked the learning table to avoid reflections on
838 verdict
= BV_DROP_IF_MOVED
;
844 ovs_rwlock_unlock(&rwlock
);
849 /* Returns the slave (registered on 'bond' by bond_slave_register()) to which
850 * a packet with the given 'flow' and 'vlan' should be forwarded. Returns
851 * NULL if the packet should be dropped because no slaves are enabled.
853 * 'vlan' is not necessarily the same as 'flow->vlan_tci'. First, 'vlan'
854 * should be a VID only (i.e. excluding the PCP bits). Second,
855 * 'flow->vlan_tci' is the VLAN TCI that appeared on the packet (so it will be
856 * nonzero only for trunk ports), whereas 'vlan' is the logical VLAN that the
857 * packet belongs to (so for an access port it will be the access port's VLAN).
859 * If 'wc' is non-NULL, bitwise-OR's 'wc' with the set of bits that were
860 * significant in the selection. At some point earlier, 'wc' should
861 * have been initialized (e.g., by flow_wildcards_init_catchall()).
864 bond_choose_output_slave(struct bond
*bond
, const struct flow
*flow
,
865 struct flow_wildcards
*wc
, uint16_t vlan
)
867 struct bond_slave
*slave
;
870 ovs_rwlock_rdlock(&rwlock
);
871 slave
= choose_output_slave(bond
, flow
, wc
, vlan
);
872 aux
= slave
? slave
->aux
: NULL
;
873 ovs_rwlock_unlock(&rwlock
);
880 bond_entry_account(struct bond_entry
*entry
, uint64_t rule_tx_bytes
)
881 OVS_REQ_WRLOCK(rwlock
)
886 delta
= rule_tx_bytes
- entry
->pr_tx_bytes
;
887 entry
->tx_bytes
+= delta
;
888 entry
->pr_tx_bytes
= rule_tx_bytes
;
892 /* Maintain bond stats using post recirculation rule byte counters.*/
894 bond_recirculation_account(struct bond
*bond
)
895 OVS_REQ_WRLOCK(rwlock
)
899 for (i
=0; i
<=BOND_MASK
; i
++) {
900 struct bond_entry
*entry
= &bond
->hash
[i
];
901 struct rule
*rule
= entry
->pr_rule
;
904 uint64_t n_packets OVS_UNUSED
;
905 long long int used OVS_UNUSED
;
908 rule
->ofproto
->ofproto_class
->rule_get_stats(
909 rule
, &n_packets
, &n_bytes
, &used
);
910 bond_entry_account(entry
, n_bytes
);
916 bond_may_recirc(const struct bond
*bond
, uint32_t *recirc_id
,
919 bool may_recirc
= bond
->balance
== BM_TCP
&& bond
->recirc_id
;
922 *recirc_id
= may_recirc
? bond
->recirc_id
: 0;
925 *hash_bias
= may_recirc
? bond
->basis
: 0;
932 bond_update_post_recirc_rules__(struct bond
* bond
, const bool force
)
933 OVS_REQ_WRLOCK(rwlock
)
935 struct bond_entry
*e
;
936 bool update_rules
= force
; /* Always update rules if caller forces it. */
938 /* Make sure all bond entries are populated */
939 for (e
= bond
->hash
; e
<= &bond
->hash
[BOND_MASK
]; e
++) {
940 if (!e
->slave
|| !e
->slave
->enabled
) {
942 e
->slave
= CONTAINER_OF(hmap_random_node(&bond
->slaves
),
943 struct bond_slave
, hmap_node
);
944 if (!e
->slave
->enabled
) {
945 e
->slave
= bond
->active_slave
;
951 update_recirc_rules(bond
);
956 bond_update_post_recirc_rules(struct bond
*bond
, uint32_t *recirc_id
,
957 uint32_t *hash_basis
)
959 ovs_rwlock_wrlock(&rwlock
);
960 if (bond_may_recirc(bond
, recirc_id
, hash_basis
)) {
961 bond_update_post_recirc_rules__(bond
, false);
963 ovs_rwlock_unlock(&rwlock
);
970 bond_is_balanced(const struct bond
*bond
) OVS_REQ_RDLOCK(rwlock
)
972 return bond
->rebalance_interval
973 && (bond
->balance
== BM_SLB
|| bond
->balance
== BM_TCP
);
976 /* Notifies 'bond' that 'n_bytes' bytes were sent in 'flow' within 'vlan'. */
978 bond_account(struct bond
*bond
, const struct flow
*flow
, uint16_t vlan
,
981 ovs_rwlock_wrlock(&rwlock
);
982 if (bond_is_balanced(bond
)) {
983 lookup_bond_entry(bond
, flow
, vlan
)->tx_bytes
+= n_bytes
;
985 ovs_rwlock_unlock(&rwlock
);
988 static struct bond_slave
*
989 bond_slave_from_bal_node(struct ovs_list
*bal
) OVS_REQ_RDLOCK(rwlock
)
991 return CONTAINER_OF(bal
, struct bond_slave
, bal_node
);
995 log_bals(struct bond
*bond
, const struct ovs_list
*bals
)
996 OVS_REQ_RDLOCK(rwlock
)
998 if (VLOG_IS_DBG_ENABLED()) {
999 struct ds ds
= DS_EMPTY_INITIALIZER
;
1000 const struct bond_slave
*slave
;
1002 LIST_FOR_EACH (slave
, bal_node
, bals
) {
1004 ds_put_char(&ds
, ',');
1006 ds_put_format(&ds
, " %s %"PRIu64
"kB",
1007 slave
->name
, slave
->tx_bytes
/ 1024);
1009 if (!slave
->enabled
) {
1010 ds_put_cstr(&ds
, " (disabled)");
1012 if (!ovs_list_is_empty(&slave
->entries
)) {
1013 struct bond_entry
*e
;
1015 ds_put_cstr(&ds
, " (");
1016 LIST_FOR_EACH (e
, list_node
, &slave
->entries
) {
1017 if (&e
->list_node
!= ovs_list_front(&slave
->entries
)) {
1018 ds_put_cstr(&ds
, " + ");
1020 ds_put_format(&ds
, "h%"PRIdPTR
": %"PRIu64
"kB",
1021 e
- bond
->hash
, e
->tx_bytes
/ 1024);
1023 ds_put_cstr(&ds
, ")");
1026 VLOG_DBG("bond %s:%s", bond
->name
, ds_cstr(&ds
));
1031 /* Shifts 'hash' from its current slave to 'to'. */
1033 bond_shift_load(struct bond_entry
*hash
, struct bond_slave
*to
)
1034 OVS_REQ_WRLOCK(rwlock
)
1036 struct bond_slave
*from
= hash
->slave
;
1037 struct bond
*bond
= from
->bond
;
1038 uint64_t delta
= hash
->tx_bytes
;
1040 VLOG_INFO("bond %s: shift %"PRIu64
"kB of load (with hash %"PRIdPTR
") "
1041 "from %s to %s (now carrying %"PRIu64
"kB and "
1042 "%"PRIu64
"kB load, respectively)",
1043 bond
->name
, delta
/ 1024, hash
- bond
->hash
,
1044 from
->name
, to
->name
,
1045 (from
->tx_bytes
- delta
) / 1024,
1046 (to
->tx_bytes
+ delta
) / 1024);
1048 /* Shift load away from 'from' to 'to'. */
1049 from
->tx_bytes
-= delta
;
1050 to
->tx_bytes
+= delta
;
1052 /* Arrange for flows to be revalidated. */
1054 bond
->bond_revalidate
= true;
1057 /* Picks and returns a bond_entry to migrate from 'from' (the most heavily
1058 * loaded bond slave) to a bond slave that has 'to_tx_bytes' bytes of load,
1059 * given that doing so must decrease the ratio of the load on the two slaves by
1060 * at least 0.1. Returns NULL if there is no appropriate entry.
1062 * The list of entries isn't sorted. I don't know of a reason to prefer to
1063 * shift away small hashes or large hashes. */
1064 static struct bond_entry
*
1065 choose_entry_to_migrate(const struct bond_slave
*from
, uint64_t to_tx_bytes
)
1066 OVS_REQ_WRLOCK(rwlock
)
1068 struct bond_entry
*e
;
1070 if (ovs_list_is_short(&from
->entries
)) {
1071 /* 'from' carries no more than one MAC hash, so shifting load away from
1072 * it would be pointless. */
1076 LIST_FOR_EACH (e
, list_node
, &from
->entries
) {
1077 uint64_t delta
= e
->tx_bytes
; /* The amount to rebalance. */
1078 uint64_t ideal_tx_bytes
= (from
->tx_bytes
+ to_tx_bytes
)/2;
1079 /* Note, the ideal traffic is the mid point
1080 * between 'from' and 'to'. This value does
1081 * not change by rebalancing. */
1082 uint64_t new_low
; /* The lower bandwidth between 'to' and 'from'
1083 after rebalancing. */
1085 new_low
= MIN(from
->tx_bytes
- delta
, to_tx_bytes
+ delta
);
1087 if ((new_low
> to_tx_bytes
) &&
1088 (new_low
- to_tx_bytes
>= (ideal_tx_bytes
- to_tx_bytes
) / 10)) {
1089 /* Only rebalance if the new 'low' is closer to to the mid point,
1090 * and the improvement exceeds 10% of current traffic
1091 * deviation from the ideal split.
1093 * The improvement on the 'high' side is always the same as the
1094 * 'low' side. Thus consider 'low' side is sufficient. */
1102 /* Inserts 'slave' into 'bals' so that descending order of 'tx_bytes' is
1105 insert_bal(struct ovs_list
*bals
, struct bond_slave
*slave
)
1107 struct bond_slave
*pos
;
1109 LIST_FOR_EACH (pos
, bal_node
, bals
) {
1110 if (slave
->tx_bytes
> pos
->tx_bytes
) {
1114 ovs_list_insert(&pos
->bal_node
, &slave
->bal_node
);
1117 /* Removes 'slave' from its current list and then inserts it into 'bals' so
1118 * that descending order of 'tx_bytes' is maintained. */
1120 reinsert_bal(struct ovs_list
*bals
, struct bond_slave
*slave
)
1122 ovs_list_remove(&slave
->bal_node
);
1123 insert_bal(bals
, slave
);
1126 /* If 'bond' needs rebalancing, does so.
1128 * The caller should have called bond_account() for each active flow, or in case
1129 * of recirculation is used, have called bond_recirculation_account(bond),
1130 * to ensure that flow data is consistently accounted at this point.
1133 bond_rebalance(struct bond
*bond
)
1135 struct bond_slave
*slave
;
1136 struct bond_entry
*e
;
1137 struct ovs_list bals
;
1138 bool rebalanced
= false;
1141 ovs_rwlock_wrlock(&rwlock
);
1142 if (!bond_is_balanced(bond
) || time_msec() < bond
->next_rebalance
) {
1145 bond
->next_rebalance
= time_msec() + bond
->rebalance_interval
;
1147 use_recirc
= bond
->ofproto
->backer
->support
.odp
.recirc
&&
1148 bond_may_recirc(bond
, NULL
, NULL
);
1151 bond_recirculation_account(bond
);
1154 /* Add each bond_entry to its slave's 'entries' list.
1155 * Compute each slave's tx_bytes as the sum of its entries' tx_bytes. */
1156 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
1157 slave
->tx_bytes
= 0;
1158 ovs_list_init(&slave
->entries
);
1160 for (e
= &bond
->hash
[0]; e
<= &bond
->hash
[BOND_MASK
]; e
++) {
1161 if (e
->slave
&& e
->tx_bytes
) {
1162 e
->slave
->tx_bytes
+= e
->tx_bytes
;
1163 ovs_list_push_back(&e
->slave
->entries
, &e
->list_node
);
1167 /* Add enabled slaves to 'bals' in descending order of tx_bytes.
1169 * XXX This is O(n**2) in the number of slaves but it could be O(n lg n)
1170 * with a proper list sort algorithm. */
1171 ovs_list_init(&bals
);
1172 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
1173 if (slave
->enabled
) {
1174 insert_bal(&bals
, slave
);
1177 log_bals(bond
, &bals
);
1179 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
1180 while (!ovs_list_is_short(&bals
)) {
1181 struct bond_slave
*from
= bond_slave_from_bal_node(ovs_list_front(&bals
));
1182 struct bond_slave
*to
= bond_slave_from_bal_node(ovs_list_back(&bals
));
1185 overload
= from
->tx_bytes
- to
->tx_bytes
;
1186 if (overload
< to
->tx_bytes
>> 5 || overload
< 100000) {
1187 /* The extra load on 'from' (and all less-loaded slaves), compared
1188 * to that of 'to' (the least-loaded slave), is less than ~3%, or
1189 * it is less than ~1Mbps. No point in rebalancing. */
1193 /* 'from' is carrying significantly more load than 'to'. Pick a hash
1194 * to move from 'from' to 'to'. */
1195 e
= choose_entry_to_migrate(from
, to
->tx_bytes
);
1197 bond_shift_load(e
, to
);
1199 /* Delete element from from->entries.
1201 * We don't add the element to to->hashes. That would only allow
1202 * 'e' to be migrated to another slave in this rebalancing run, and
1203 * there is no point in doing that. */
1204 ovs_list_remove(&e
->list_node
);
1206 /* Re-sort 'bals'. */
1207 reinsert_bal(&bals
, from
);
1208 reinsert_bal(&bals
, to
);
1211 /* Can't usefully migrate anything away from 'from'.
1212 * Don't reconsider it. */
1213 ovs_list_remove(&from
->bal_node
);
1217 /* Implement exponentially weighted moving average. A weight of 1/2 causes
1218 * historical data to decay to <1% in 7 rebalancing runs. 1,000,000 bytes
1219 * take 20 rebalancing runs to decay to 0 and get deleted entirely. */
1220 for (e
= &bond
->hash
[0]; e
<= &bond
->hash
[BOND_MASK
]; e
++) {
1224 if (use_recirc
&& rebalanced
) {
1225 bond_update_post_recirc_rules__(bond
,true);
1229 ovs_rwlock_unlock(&rwlock
);
1232 /* Bonding unixctl user interface functions. */
1234 static struct bond
*
1235 bond_find(const char *name
) OVS_REQ_RDLOCK(rwlock
)
1239 HMAP_FOR_EACH_WITH_HASH (bond
, hmap_node
, hash_string(name
, 0),
1241 if (!strcmp(bond
->name
, name
)) {
1248 static struct bond_slave
*
1249 bond_lookup_slave(struct bond
*bond
, const char *slave_name
)
1251 struct bond_slave
*slave
;
1253 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
1254 if (!strcmp(slave
->name
, slave_name
)) {
1262 bond_unixctl_list(struct unixctl_conn
*conn
,
1263 int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1264 void *aux OVS_UNUSED
)
1266 struct ds ds
= DS_EMPTY_INITIALIZER
;
1267 const struct bond
*bond
;
1269 ds_put_cstr(&ds
, "bond\ttype\trecircID\tslaves\n");
1271 ovs_rwlock_rdlock(&rwlock
);
1272 HMAP_FOR_EACH (bond
, hmap_node
, all_bonds
) {
1273 const struct bond_slave
*slave
;
1276 ds_put_format(&ds
, "%s\t%s\t%d\t", bond
->name
,
1277 bond_mode_to_string(bond
->balance
), bond
->recirc_id
);
1280 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
1282 ds_put_cstr(&ds
, ", ");
1284 ds_put_cstr(&ds
, slave
->name
);
1286 ds_put_char(&ds
, '\n');
1288 ovs_rwlock_unlock(&rwlock
);
1289 unixctl_command_reply(conn
, ds_cstr(&ds
));
1294 bond_print_details(struct ds
*ds
, const struct bond
*bond
)
1295 OVS_REQ_RDLOCK(rwlock
)
1297 struct shash slave_shash
= SHASH_INITIALIZER(&slave_shash
);
1298 const struct shash_node
**sorted_slaves
= NULL
;
1299 const struct bond_slave
*slave
;
1304 ds_put_format(ds
, "---- %s ----\n", bond
->name
);
1305 ds_put_format(ds
, "bond_mode: %s\n",
1306 bond_mode_to_string(bond
->balance
));
1308 may_recirc
= bond_may_recirc(bond
, &recirc_id
, NULL
);
1309 ds_put_format(ds
, "bond may use recirculation: %s, Recirc-ID : %d\n",
1310 may_recirc
? "yes" : "no", may_recirc
? recirc_id
: -1);
1312 ds_put_format(ds
, "bond-hash-basis: %"PRIu32
"\n", bond
->basis
);
1314 ds_put_format(ds
, "updelay: %d ms\n", bond
->updelay
);
1315 ds_put_format(ds
, "downdelay: %d ms\n", bond
->downdelay
);
1317 if (bond_is_balanced(bond
)) {
1318 ds_put_format(ds
, "next rebalance: %lld ms\n",
1319 bond
->next_rebalance
- time_msec());
1322 ds_put_cstr(ds
, "lacp_status: ");
1323 switch (bond
->lacp_status
) {
1324 case LACP_NEGOTIATED
:
1325 ds_put_cstr(ds
, "negotiated\n");
1327 case LACP_CONFIGURED
:
1328 ds_put_cstr(ds
, "configured\n");
1331 ds_put_cstr(ds
, "off\n");
1334 ds_put_cstr(ds
, "<unknown>\n");
1338 ds_put_format(ds
, "lacp_fallback_ab: %s\n",
1339 bond
->lacp_fallback_ab
? "true" : "false");
1341 ds_put_cstr(ds
, "active slave mac: ");
1342 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(bond
->active_slave_mac
));
1343 slave
= bond_find_slave_by_mac(bond
, bond
->active_slave_mac
);
1344 ds_put_format(ds
,"(%s)\n", slave
? slave
->name
: "none");
1346 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
1347 shash_add(&slave_shash
, slave
->name
, slave
);
1349 sorted_slaves
= shash_sort(&slave_shash
);
1351 for (i
= 0; i
< shash_count(&slave_shash
); i
++) {
1352 struct bond_entry
*be
;
1354 slave
= sorted_slaves
[i
]->data
;
1357 ds_put_format(ds
, "\nslave %s: %s\n",
1358 slave
->name
, slave
->enabled
? "enabled" : "disabled");
1359 if (slave
== bond
->active_slave
) {
1360 ds_put_cstr(ds
, "\tactive slave\n");
1362 if (slave
->delay_expires
!= LLONG_MAX
) {
1363 ds_put_format(ds
, "\t%s expires in %lld ms\n",
1364 slave
->enabled
? "downdelay" : "updelay",
1365 slave
->delay_expires
- time_msec());
1368 ds_put_format(ds
, "\tmay_enable: %s\n",
1369 slave
->may_enable
? "true" : "false");
1371 if (!bond_is_balanced(bond
)) {
1376 for (be
= bond
->hash
; be
<= &bond
->hash
[BOND_MASK
]; be
++) {
1377 int hash
= be
- bond
->hash
;
1380 if (be
->slave
!= slave
) {
1384 be_tx_k
= be
->tx_bytes
/ 1024;
1386 ds_put_format(ds
, "\thash %d: %"PRIu64
" kB load\n",
1390 /* XXX How can we list the MACs assigned to hashes of SLB bonds? */
1393 shash_destroy(&slave_shash
);
1394 free(sorted_slaves
);
1395 ds_put_cstr(ds
, "\n");
1399 bond_unixctl_show(struct unixctl_conn
*conn
,
1400 int argc
, const char *argv
[],
1401 void *aux OVS_UNUSED
)
1403 struct ds ds
= DS_EMPTY_INITIALIZER
;
1405 ovs_rwlock_rdlock(&rwlock
);
1407 const struct bond
*bond
= bond_find(argv
[1]);
1410 unixctl_command_reply_error(conn
, "no such bond");
1413 bond_print_details(&ds
, bond
);
1415 const struct bond
*bond
;
1417 HMAP_FOR_EACH (bond
, hmap_node
, all_bonds
) {
1418 bond_print_details(&ds
, bond
);
1422 unixctl_command_reply(conn
, ds_cstr(&ds
));
1426 ovs_rwlock_unlock(&rwlock
);
1430 bond_unixctl_migrate(struct unixctl_conn
*conn
,
1431 int argc OVS_UNUSED
, const char *argv
[],
1432 void *aux OVS_UNUSED
)
1434 const char *bond_s
= argv
[1];
1435 const char *hash_s
= argv
[2];
1436 const char *slave_s
= argv
[3];
1438 struct bond_slave
*slave
;
1439 struct bond_entry
*entry
;
1442 ovs_rwlock_wrlock(&rwlock
);
1443 bond
= bond_find(bond_s
);
1445 unixctl_command_reply_error(conn
, "no such bond");
1449 if (bond
->balance
!= BM_SLB
) {
1450 unixctl_command_reply_error(conn
, "not an SLB bond");
1454 if (strspn(hash_s
, "0123456789") == strlen(hash_s
)) {
1455 hash
= atoi(hash_s
) & BOND_MASK
;
1457 unixctl_command_reply_error(conn
, "bad hash");
1461 slave
= bond_lookup_slave(bond
, slave_s
);
1463 unixctl_command_reply_error(conn
, "no such slave");
1467 if (!slave
->enabled
) {
1468 unixctl_command_reply_error(conn
, "cannot migrate to disabled slave");
1472 entry
= &bond
->hash
[hash
];
1473 bond
->bond_revalidate
= true;
1474 entry
->slave
= slave
;
1475 unixctl_command_reply(conn
, "migrated");
1478 ovs_rwlock_unlock(&rwlock
);
1482 bond_unixctl_set_active_slave(struct unixctl_conn
*conn
,
1483 int argc OVS_UNUSED
, const char *argv
[],
1484 void *aux OVS_UNUSED
)
1486 const char *bond_s
= argv
[1];
1487 const char *slave_s
= argv
[2];
1489 struct bond_slave
*slave
;
1491 ovs_rwlock_wrlock(&rwlock
);
1492 bond
= bond_find(bond_s
);
1494 unixctl_command_reply_error(conn
, "no such bond");
1498 slave
= bond_lookup_slave(bond
, slave_s
);
1500 unixctl_command_reply_error(conn
, "no such slave");
1504 if (!slave
->enabled
) {
1505 unixctl_command_reply_error(conn
, "cannot make disabled slave active");
1509 if (bond
->active_slave
!= slave
) {
1510 bond
->bond_revalidate
= true;
1511 bond
->active_slave
= slave
;
1512 VLOG_INFO("bond %s: active interface is now %s",
1513 bond
->name
, slave
->name
);
1514 bond
->send_learning_packets
= true;
1515 unixctl_command_reply(conn
, "done");
1516 bond_active_slave_changed(bond
);
1518 unixctl_command_reply(conn
, "no change");
1521 ovs_rwlock_unlock(&rwlock
);
1525 enable_slave(struct unixctl_conn
*conn
, const char *argv
[], bool enable
)
1527 const char *bond_s
= argv
[1];
1528 const char *slave_s
= argv
[2];
1530 struct bond_slave
*slave
;
1532 ovs_rwlock_wrlock(&rwlock
);
1533 bond
= bond_find(bond_s
);
1535 unixctl_command_reply_error(conn
, "no such bond");
1539 slave
= bond_lookup_slave(bond
, slave_s
);
1541 unixctl_command_reply_error(conn
, "no such slave");
1545 bond_enable_slave(slave
, enable
);
1546 unixctl_command_reply(conn
, enable
? "enabled" : "disabled");
1549 ovs_rwlock_unlock(&rwlock
);
1553 bond_unixctl_enable_slave(struct unixctl_conn
*conn
,
1554 int argc OVS_UNUSED
, const char *argv
[],
1555 void *aux OVS_UNUSED
)
1557 enable_slave(conn
, argv
, true);
1561 bond_unixctl_disable_slave(struct unixctl_conn
*conn
,
1562 int argc OVS_UNUSED
, const char *argv
[],
1563 void *aux OVS_UNUSED
)
1565 enable_slave(conn
, argv
, false);
1569 bond_unixctl_hash(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1570 void *aux OVS_UNUSED
)
1572 const char *mac_s
= argv
[1];
1573 const char *vlan_s
= argc
> 2 ? argv
[2] : NULL
;
1574 const char *basis_s
= argc
> 3 ? argv
[3] : NULL
;
1575 struct eth_addr mac
;
1582 if (!ovs_scan(vlan_s
, "%u", &vlan
)) {
1583 unixctl_command_reply_error(conn
, "invalid vlan");
1591 if (!ovs_scan(basis_s
, "%"SCNu32
, &basis
)) {
1592 unixctl_command_reply_error(conn
, "invalid basis");
1599 if (ovs_scan(mac_s
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))) {
1600 hash
= bond_hash_src(mac
, vlan
, basis
) & BOND_MASK
;
1602 hash_cstr
= xasprintf("%u", hash
);
1603 unixctl_command_reply(conn
, hash_cstr
);
1606 unixctl_command_reply_error(conn
, "invalid mac");
1613 unixctl_command_register("bond/list", "", 0, 0, bond_unixctl_list
, NULL
);
1614 unixctl_command_register("bond/show", "[port]", 0, 1, bond_unixctl_show
,
1616 unixctl_command_register("bond/migrate", "port hash slave", 3, 3,
1617 bond_unixctl_migrate
, NULL
);
1618 unixctl_command_register("bond/set-active-slave", "port slave", 2, 2,
1619 bond_unixctl_set_active_slave
, NULL
);
1620 unixctl_command_register("bond/enable-slave", "port slave", 2, 2,
1621 bond_unixctl_enable_slave
, NULL
);
1622 unixctl_command_register("bond/disable-slave", "port slave", 2, 2,
1623 bond_unixctl_disable_slave
, NULL
);
1624 unixctl_command_register("bond/hash", "mac [vlan] [basis]", 1, 3,
1625 bond_unixctl_hash
, NULL
);
1629 bond_entry_reset(struct bond
*bond
)
1631 if (bond
->balance
!= BM_AB
) {
1632 size_t hash_len
= BOND_BUCKETS
* sizeof *bond
->hash
;
1635 bond
->hash
= xmalloc(hash_len
);
1637 memset(bond
->hash
, 0, hash_len
);
1639 bond
->next_rebalance
= time_msec() + bond
->rebalance_interval
;
1646 static struct bond_slave
*
1647 bond_slave_lookup(struct bond
*bond
, const void *slave_
)
1649 struct bond_slave
*slave
;
1651 HMAP_FOR_EACH_IN_BUCKET (slave
, hmap_node
, hash_pointer(slave_
, 0),
1653 if (slave
->aux
== slave_
) {
1662 bond_enable_slave(struct bond_slave
*slave
, bool enable
)
1664 slave
->delay_expires
= LLONG_MAX
;
1665 if (enable
!= slave
->enabled
) {
1666 slave
->bond
->bond_revalidate
= true;
1667 slave
->enabled
= enable
;
1669 ovs_mutex_lock(&slave
->bond
->mutex
);
1671 ovs_list_insert(&slave
->bond
->enabled_slaves
, &slave
->list_node
);
1673 ovs_list_remove(&slave
->list_node
);
1675 ovs_mutex_unlock(&slave
->bond
->mutex
);
1677 VLOG_INFO("interface %s: %s", slave
->name
,
1678 slave
->enabled
? "enabled" : "disabled");
1683 bond_link_status_update(struct bond_slave
*slave
)
1685 struct bond
*bond
= slave
->bond
;
1688 up
= netdev_get_carrier(slave
->netdev
) && slave
->may_enable
;
1689 if ((up
== slave
->enabled
) != (slave
->delay_expires
== LLONG_MAX
)) {
1690 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
1691 VLOG_INFO_RL(&rl
, "interface %s: link state %s",
1692 slave
->name
, up
? "up" : "down");
1693 if (up
== slave
->enabled
) {
1694 slave
->delay_expires
= LLONG_MAX
;
1695 VLOG_INFO_RL(&rl
, "interface %s: will not be %s",
1696 slave
->name
, up
? "disabled" : "enabled");
1698 int delay
= (bond
->lacp_status
!= LACP_DISABLED
? 0
1699 : up
? bond
->updelay
: bond
->downdelay
);
1700 slave
->delay_expires
= time_msec() + delay
;
1702 VLOG_INFO_RL(&rl
, "interface %s: will be %s if it stays %s "
1705 up
? "enabled" : "disabled",
1712 if (time_msec() >= slave
->delay_expires
) {
1713 bond_enable_slave(slave
, up
);
1718 bond_hash_src(const struct eth_addr mac
, uint16_t vlan
, uint32_t basis
)
1720 return hash_mac(mac
, vlan
, basis
);
1724 bond_hash_tcp(const struct flow
*flow
, uint16_t vlan
, uint32_t basis
)
1726 struct flow hash_flow
= *flow
;
1727 hash_flow
.vlan_tci
= htons(vlan
);
1729 /* The symmetric quality of this hash function is not required, but
1730 * flow_hash_symmetric_l4 already exists, and is sufficient for our
1731 * purposes, so we use it out of convenience. */
1732 return flow_hash_symmetric_l4(&hash_flow
, basis
);
1736 bond_hash(const struct bond
*bond
, const struct flow
*flow
, uint16_t vlan
)
1738 ovs_assert(bond
->balance
== BM_TCP
|| bond
->balance
== BM_SLB
);
1740 return (bond
->balance
== BM_TCP
1741 ? bond_hash_tcp(flow
, vlan
, bond
->basis
)
1742 : bond_hash_src(flow
->dl_src
, vlan
, bond
->basis
));
1745 static struct bond_entry
*
1746 lookup_bond_entry(const struct bond
*bond
, const struct flow
*flow
,
1749 return &bond
->hash
[bond_hash(bond
, flow
, vlan
) & BOND_MASK
];
1752 /* Selects and returns an enabled slave from the 'enabled_slaves' list
1753 * in a round-robin fashion. If the 'enabled_slaves' list is empty,
1755 static struct bond_slave
*
1756 get_enabled_slave(struct bond
*bond
)
1758 struct ovs_list
*node
;
1760 ovs_mutex_lock(&bond
->mutex
);
1761 if (ovs_list_is_empty(&bond
->enabled_slaves
)) {
1762 ovs_mutex_unlock(&bond
->mutex
);
1766 node
= ovs_list_pop_front(&bond
->enabled_slaves
);
1767 ovs_list_push_back(&bond
->enabled_slaves
, node
);
1768 ovs_mutex_unlock(&bond
->mutex
);
1770 return CONTAINER_OF(node
, struct bond_slave
, list_node
);
1773 static struct bond_slave
*
1774 choose_output_slave(const struct bond
*bond
, const struct flow
*flow
,
1775 struct flow_wildcards
*wc
, uint16_t vlan
)
1777 struct bond_entry
*e
;
1780 balance
= bond
->balance
;
1781 if (bond
->lacp_status
== LACP_CONFIGURED
) {
1782 /* LACP has been configured on this bond but negotiations were
1783 * unsuccussful. If lacp_fallback_ab is enabled use active-
1784 * backup mode else drop all traffic. */
1785 if (!bond
->lacp_fallback_ab
) {
1793 return bond
->active_slave
;
1796 if (bond
->lacp_status
!= LACP_NEGOTIATED
) {
1797 /* Must have LACP negotiations for TCP balanced bonds. */
1801 flow_mask_hash_fields(flow
, wc
, NX_HASH_FIELDS_SYMMETRIC_L4
);
1806 flow_mask_hash_fields(flow
, wc
, NX_HASH_FIELDS_ETH_SRC
);
1808 e
= lookup_bond_entry(bond
, flow
, vlan
);
1809 if (!e
->slave
|| !e
->slave
->enabled
) {
1810 e
->slave
= get_enabled_slave(CONST_CAST(struct bond
*, bond
));
1819 static struct bond_slave
*
1820 bond_choose_slave(const struct bond
*bond
)
1822 struct bond_slave
*slave
, *best
;
1824 /* Find the last active slave. */
1825 slave
= bond_find_slave_by_mac(bond
, bond
->active_slave_mac
);
1826 if (slave
&& slave
->enabled
) {
1830 /* Find an enabled slave. */
1831 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
1832 if (slave
->enabled
) {
1837 /* All interfaces are disabled. Find an interface that will be enabled
1838 * after its updelay expires. */
1840 HMAP_FOR_EACH (slave
, hmap_node
, &bond
->slaves
) {
1841 if (slave
->delay_expires
!= LLONG_MAX
1842 && slave
->may_enable
1843 && (!best
|| slave
->delay_expires
< best
->delay_expires
)) {
1851 bond_choose_active_slave(struct bond
*bond
)
1853 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
1854 struct bond_slave
*old_active_slave
= bond
->active_slave
;
1856 bond
->active_slave
= bond_choose_slave(bond
);
1857 if (bond
->active_slave
) {
1858 if (bond
->active_slave
->enabled
) {
1859 VLOG_INFO_RL(&rl
, "bond %s: active interface is now %s",
1860 bond
->name
, bond
->active_slave
->name
);
1862 VLOG_INFO_RL(&rl
, "bond %s: active interface is now %s, skipping "
1863 "remaining %lld ms updelay (since no interface was "
1864 "enabled)", bond
->name
, bond
->active_slave
->name
,
1865 bond
->active_slave
->delay_expires
- time_msec());
1866 bond_enable_slave(bond
->active_slave
, true);
1869 bond
->send_learning_packets
= true;
1871 if (bond
->active_slave
!= old_active_slave
) {
1872 bond_active_slave_changed(bond
);
1874 } else if (old_active_slave
) {
1875 bond_active_slave_changed(bond
);
1876 VLOG_INFO_RL(&rl
, "bond %s: all interfaces disabled", bond
->name
);
1881 * Return true if bond has unstored active slave change.
1882 * If return true, 'mac' will store the bond's current active slave's
1885 bond_get_changed_active_slave(const char *name
, struct eth_addr
*mac
,
1890 ovs_rwlock_wrlock(&rwlock
);
1891 bond
= bond_find(name
);
1893 if (bond
->active_slave_changed
|| force
) {
1894 *mac
= bond
->active_slave_mac
;
1895 bond
->active_slave_changed
= false;
1896 ovs_rwlock_unlock(&rwlock
);
1900 ovs_rwlock_unlock(&rwlock
);