1 /* Copyright (c) 2008, 2009 Nicira Networks
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * In addition, as a special exception, Nicira Networks gives permission
17 * to link the code of its release of vswitchd with the OpenSSL project's
18 * "OpenSSL" library (or with modified versions of it that use the same
19 * license as the "OpenSSL" library), and distribute the linked
20 * executables. You must obey the GNU General Public License in all
21 * respects for all of the code used other than "OpenSSL". If you modify
22 * this file, you may extend this exception to your version of the file,
23 * but you are not obligated to do so. If you do not wish to do so,
24 * delete this exception statement from your version.
32 #include <arpa/inet.h>
36 #include <openflow/openflow.h>
41 #include <sys/socket.h>
48 #include "dynamic-string.h"
52 #include "mac-learning.h"
55 #include "ofp-print.h"
58 #include "poll-loop.h"
59 #include "port-array.h"
60 #include "proc-net-compat.h"
62 #include "secchan/ofproto.h"
63 #include "socket-util.h"
70 #include "vconn-ssl.h"
71 #include "xenserver.h"
74 #define THIS_MODULE VLM_bridge
82 extern uint64_t mgmt_id
;
85 struct port
*port
; /* Containing port. */
86 size_t port_ifidx
; /* Index within containing port. */
88 char *name
; /* Host network device name. */
89 int dp_ifidx
; /* Index within kernel datapath. */
91 uint8_t mac
[ETH_ADDR_LEN
]; /* Ethernet address (all zeros if unknowns). */
93 tag_type tag
; /* Tag associated with this interface. */
94 bool enabled
; /* May be chosen for flows? */
95 long long delay_expires
; /* Time after which 'enabled' may change. */
98 #define BOND_MASK 0xff
100 int iface_idx
; /* Index of assigned iface, or -1 if none. */
101 uint64_t tx_bytes
; /* Count of bytes recently transmitted. */
102 tag_type iface_tag
; /* Tag associated with iface_idx. */
105 #define MAX_MIRRORS 32
106 typedef uint32_t mirror_mask_t
;
107 #define MIRROR_MASK_C(X) UINT32_C(X)
108 BUILD_ASSERT_DECL(sizeof(mirror_mask_t
) * CHAR_BIT
>= MAX_MIRRORS
);
110 struct bridge
*bridge
;
114 /* Selection criteria. */
115 struct svec src_ports
;
116 struct svec dst_ports
;
121 struct port
*out_port
;
125 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
127 struct bridge
*bridge
;
129 int vlan
; /* -1=trunk port, else a 12-bit VLAN ID. */
130 unsigned long *trunks
; /* Bitmap of trunked VLANs, if 'vlan' == -1. */
133 /* An ordinary bridge port has 1 interface.
134 * A bridge port for bonding has at least 2 interfaces. */
135 struct iface
**ifaces
;
136 size_t n_ifaces
, allocated_ifaces
;
139 struct bond_entry
*bond_hash
; /* An array of (BOND_MASK + 1) elements. */
140 int active_iface
; /* Ifidx on which bcasts accepted, or -1. */
141 tag_type active_iface_tag
; /* Tag for bcast flows. */
142 tag_type no_ifaces_tag
; /* Tag for flows when all ifaces disabled. */
143 int updelay
, downdelay
; /* Delay before iface goes up/down, in ms. */
145 /* Port mirroring info. */
146 mirror_mask_t src_mirrors
; /* Mirrors triggered when packet received. */
147 mirror_mask_t dst_mirrors
; /* Mirrors triggered when packet sent. */
148 bool is_mirror_output_port
; /* Does port mirroring send frames here? */
150 /* Spanning tree info. */
151 enum stp_state stp_state
; /* Always STP_FORWARDING if STP not in use. */
152 tag_type stp_state_tag
; /* Tag for STP state change. */
155 #define DP_MAX_PORTS 255
157 struct list node
; /* Node in global list of bridges. */
158 char *name
; /* User-specified arbitrary name. */
159 struct mac_learning
*ml
; /* MAC learning table, or null not to learn. */
160 bool sent_config_request
; /* Successfully sent config request? */
161 uint8_t default_ea
[ETH_ADDR_LEN
]; /* Default MAC. */
163 /* Support for remote controllers. */
164 char *controller
; /* NULL if there is no remote controller;
165 * "discover" to do controller discovery;
166 * otherwise a vconn name. */
168 /* OpenFlow switch processing. */
169 struct ofproto
*ofproto
; /* OpenFlow switch. */
171 /* Kernel datapath information. */
172 struct dpif dpif
; /* Kernel datapath. */
173 struct port_array ifaces
; /* Indexed by kernel datapath port number. */
177 size_t n_ports
, allocated_ports
;
180 bool has_bonded_ports
;
181 long long int bond_next_rebalance
;
186 /* Flow statistics gathering. */
187 time_t next_stats_request
;
189 /* Port mirroring. */
190 struct mirror
*mirrors
[MAX_MIRRORS
];
194 long long int stp_last_tick
;
197 /* List of all bridges. */
198 static struct list all_bridges
= LIST_INITIALIZER(&all_bridges
);
200 /* Maximum number of datapaths. */
201 enum { DP_MAX
= 256 };
203 static struct bridge
*bridge_create(const char *name
);
204 static void bridge_destroy(struct bridge
*);
205 static struct bridge
*bridge_lookup(const char *name
);
206 static int bridge_run_one(struct bridge
*);
207 static void bridge_reconfigure_one(struct bridge
*);
208 static void bridge_reconfigure_controller(struct bridge
*);
209 static void bridge_get_all_ifaces(const struct bridge
*, struct svec
*ifaces
);
210 static void bridge_fetch_dp_ifaces(struct bridge
*);
211 static void bridge_flush(struct bridge
*);
212 static void bridge_pick_local_hw_addr(struct bridge
*,
213 uint8_t ea
[ETH_ADDR_LEN
],
214 const char **devname
);
215 static uint64_t bridge_pick_datapath_id(struct bridge
*,
216 const uint8_t bridge_ea
[ETH_ADDR_LEN
],
217 const char *devname
);
218 static uint64_t dpid_from_hash(const void *, size_t nbytes
);
220 static void bond_init(void);
221 static void bond_run(struct bridge
*);
222 static void bond_wait(struct bridge
*);
223 static void bond_rebalance_port(struct port
*);
224 static void bond_send_learning_packets(struct port
*);
226 static void port_create(struct bridge
*, const char *name
);
227 static void port_reconfigure(struct port
*);
228 static void port_destroy(struct port
*);
229 static struct port
*port_lookup(const struct bridge
*, const char *name
);
230 static struct iface
*port_lookup_iface(const struct port
*, const char *name
);
231 static struct port
*port_from_dp_ifidx(const struct bridge
*,
233 static void port_update_bond_compat(struct port
*);
234 static void port_update_vlan_compat(struct port
*);
236 static void mirror_create(struct bridge
*, const char *name
);
237 static void mirror_destroy(struct mirror
*);
238 static void mirror_reconfigure(struct bridge
*);
239 static void mirror_reconfigure_one(struct mirror
*);
240 static bool vlan_is_mirrored(const struct mirror
*, int vlan
);
242 static void brstp_reconfigure(struct bridge
*);
243 static void brstp_adjust_timers(struct bridge
*);
244 static void brstp_run(struct bridge
*);
245 static void brstp_wait(struct bridge
*);
247 static void iface_create(struct port
*, const char *name
);
248 static void iface_destroy(struct iface
*);
249 static struct iface
*iface_lookup(const struct bridge
*, const char *name
);
250 static struct iface
*iface_from_dp_ifidx(const struct bridge
*,
253 /* Hooks into ofproto processing. */
254 static struct ofhooks bridge_ofhooks
;
256 /* Public functions. */
258 /* Adds the name of each interface used by a bridge, including local and
259 * internal ports, to 'svec'. */
261 bridge_get_ifaces(struct svec
*svec
)
263 struct bridge
*br
, *next
;
266 LIST_FOR_EACH_SAFE (br
, next
, struct bridge
, node
, &all_bridges
) {
267 for (i
= 0; i
< br
->n_ports
; i
++) {
268 struct port
*port
= br
->ports
[i
];
270 for (j
= 0; j
< port
->n_ifaces
; j
++) {
271 struct iface
*iface
= port
->ifaces
[j
];
272 if (iface
->dp_ifidx
< 0) {
273 VLOG_ERR("%s interface not in dp%u, ignoring",
274 iface
->name
, dpif_id(&br
->dpif
));
276 if (iface
->dp_ifidx
!= ODPP_LOCAL
) {
277 svec_add(svec
, iface
->name
);
285 /* The caller must already have called cfg_read(). */
294 for (i
= 0; i
< DP_MAX
; i
++) {
298 sprintf(devname
, "dp%d", i
);
299 retval
= dpif_open(devname
, &dpif
);
301 char dpif_name
[IF_NAMESIZE
];
302 if (dpif_get_name(&dpif
, dpif_name
, sizeof dpif_name
)
303 || !cfg_has("bridge.%s.port", dpif_name
)) {
307 } else if (retval
!= ENODEV
) {
308 VLOG_ERR("failed to delete datapath dp%d: %s",
309 i
, strerror(retval
));
313 bridge_reconfigure();
318 config_string_change(const char *key
, char **valuep
)
320 const char *value
= cfg_get_string(0, "%s", key
);
321 if (value
&& (!*valuep
|| strcmp(value
, *valuep
))) {
323 *valuep
= xstrdup(value
);
331 bridge_configure_ssl(void)
333 /* XXX SSL should be configurable on a per-bridge basis.
334 * XXX should be possible to de-configure SSL. */
335 static char *private_key_file
;
336 static char *certificate_file
;
337 static char *cacert_file
;
339 if (config_string_change("ssl.private-key", &private_key_file
)) {
340 vconn_ssl_set_private_key_file(private_key_file
);
343 if (config_string_change("ssl.certificate", &certificate_file
)) {
344 vconn_ssl_set_certificate_file(certificate_file
);
347 if (config_string_change("ssl.ca-cert", &cacert_file
)) {
348 vconn_ssl_set_ca_cert_file(cacert_file
,
349 cfg_get_bool(0, "ssl.bootstrap-ca-cert"));
355 bridge_reconfigure(void)
357 struct svec old_br
, new_br
, raw_new_br
;
358 struct bridge
*br
, *next
;
361 COVERAGE_INC(bridge_reconfigure
);
363 /* Collect old bridges. */
365 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
366 svec_add(&old_br
, br
->name
);
369 /* Collect new bridges. */
370 svec_init(&raw_new_br
);
371 cfg_get_subsections(&raw_new_br
, "bridge");
373 for (i
= 0; i
< raw_new_br
.n
; i
++) {
374 const char *name
= raw_new_br
.names
[i
];
375 if ((!strncmp(name
, "dp", 2) && isdigit(name
[2])) ||
376 (!strncmp(name
, "nl:", 3) && isdigit(name
[3]))) {
377 VLOG_ERR("%s is not a valid bridge name (bridges may not be "
378 "named \"dp\" or \"nl:\" followed by a digit)", name
);
380 svec_add(&new_br
, name
);
383 svec_destroy(&raw_new_br
);
385 /* Get rid of deleted bridges and add new bridges. */
388 assert(svec_is_unique(&old_br
));
389 assert(svec_is_unique(&new_br
));
390 LIST_FOR_EACH_SAFE (br
, next
, struct bridge
, node
, &all_bridges
) {
391 if (!svec_contains(&new_br
, br
->name
)) {
395 for (i
= 0; i
< new_br
.n
; i
++) {
396 const char *name
= new_br
.names
[i
];
397 if (!svec_contains(&old_br
, name
)) {
401 svec_destroy(&old_br
);
402 svec_destroy(&new_br
);
406 bridge_configure_ssl();
409 /* Reconfigure all bridges. */
410 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
411 bridge_reconfigure_one(br
);
414 /* Add and delete ports on all datapaths.
416 * The kernel will reject any attempt to add a given port to a datapath if
417 * that port already belongs to a different datapath, so we must do all
418 * port deletions before any port additions. */
419 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
420 struct odp_port
*dpif_ports
;
422 struct svec want_ifaces
;
424 dpif_port_list(&br
->dpif
, &dpif_ports
, &n_dpif_ports
);
425 bridge_get_all_ifaces(br
, &want_ifaces
);
426 for (i
= 0; i
< n_dpif_ports
; i
++) {
427 const struct odp_port
*p
= &dpif_ports
[i
];
428 if (!svec_contains(&want_ifaces
, p
->devname
)
429 && strcmp(p
->devname
, br
->name
)) {
430 int retval
= dpif_port_del(&br
->dpif
, p
->port
);
432 VLOG_ERR("failed to remove %s interface from dp%u: %s",
433 p
->devname
, dpif_id(&br
->dpif
), strerror(retval
));
437 svec_destroy(&want_ifaces
);
440 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
441 struct odp_port
*dpif_ports
;
443 struct svec cur_ifaces
, want_ifaces
, add_ifaces
;
446 dpif_port_list(&br
->dpif
, &dpif_ports
, &n_dpif_ports
);
447 svec_init(&cur_ifaces
);
448 for (i
= 0; i
< n_dpif_ports
; i
++) {
449 svec_add(&cur_ifaces
, dpif_ports
[i
].devname
);
452 svec_sort_unique(&cur_ifaces
);
453 bridge_get_all_ifaces(br
, &want_ifaces
);
454 svec_diff(&want_ifaces
, &cur_ifaces
, &add_ifaces
, NULL
, NULL
);
457 for (i
= 0; i
< add_ifaces
.n
; i
++) {
458 const char *if_name
= add_ifaces
.names
[i
];
460 int internal
= cfg_get_bool(0, "iface.%s.internal", if_name
);
461 int error
= dpif_port_add(&br
->dpif
, if_name
, next_port_no
++,
462 internal
? ODP_PORT_INTERNAL
: 0);
463 if (error
!= EEXIST
) {
464 if (next_port_no
>= 256) {
465 VLOG_ERR("ran out of valid port numbers on dp%u",
470 VLOG_ERR("failed to add %s interface to dp%u: %s",
471 if_name
, dpif_id(&br
->dpif
), strerror(error
));
478 svec_destroy(&cur_ifaces
);
479 svec_destroy(&want_ifaces
);
480 svec_destroy(&add_ifaces
);
482 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
485 struct iface
*local_iface
= NULL
;
487 uint8_t engine_type
= br
->dpif
.minor
;
488 uint8_t engine_id
= br
->dpif
.minor
;
489 bool add_id_to_iface
= false;
490 struct svec nf_hosts
;
492 bridge_fetch_dp_ifaces(br
);
493 for (i
= 0; i
< br
->n_ports
; ) {
494 struct port
*port
= br
->ports
[i
];
496 for (j
= 0; j
< port
->n_ifaces
; ) {
497 struct iface
*iface
= port
->ifaces
[j
];
498 if (iface
->dp_ifidx
< 0) {
499 VLOG_ERR("%s interface not in dp%u, dropping",
500 iface
->name
, dpif_id(&br
->dpif
));
501 iface_destroy(iface
);
503 if (iface
->dp_ifidx
== ODPP_LOCAL
) {
506 VLOG_DBG("dp%u has interface %s on port %d",
507 dpif_id(&br
->dpif
), iface
->name
, iface
->dp_ifidx
);
511 if (!port
->n_ifaces
) {
512 VLOG_ERR("%s port has no interfaces, dropping", port
->name
);
519 /* Pick local port hardware address, datapath ID. */
520 bridge_pick_local_hw_addr(br
, ea
, &devname
);
522 int error
= netdev_nodev_set_etheraddr(local_iface
->name
, ea
);
524 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
525 VLOG_ERR_RL(&rl
, "bridge %s: failed to set bridge "
526 "Ethernet address: %s",
527 br
->name
, strerror(error
));
531 dpid
= bridge_pick_datapath_id(br
, ea
, devname
);
532 ofproto_set_datapath_id(br
->ofproto
, dpid
);
534 /* Set NetFlow configuration on this bridge. */
535 if (cfg_has("netflow.%s.engine-type", br
->name
)) {
536 engine_type
= cfg_get_int(0, "netflow.%s.engine-type",
539 if (cfg_has("netflow.%s.engine-id", br
->name
)) {
540 engine_id
= cfg_get_int(0, "netflow.%s.engine-id", br
->name
);
542 if (cfg_has("netflow.%s.add-id-to-iface", br
->name
)) {
543 add_id_to_iface
= cfg_get_bool(0, "netflow.%s.add-id-to-iface",
546 if (add_id_to_iface
&& engine_id
> 0x7f) {
547 VLOG_WARN("bridge %s: netflow port mangling may conflict with "
548 "another vswitch, choose an engine id less than 128",
551 if (add_id_to_iface
&& br
->n_ports
> 0x1ff) {
552 VLOG_WARN("bridge %s: netflow port mangling will conflict with "
553 "another port when 512 or more ports are used",
556 svec_init(&nf_hosts
);
557 cfg_get_all_keys(&nf_hosts
, "netflow.%s.host", br
->name
);
558 if (ofproto_set_netflow(br
->ofproto
, &nf_hosts
, engine_type
,
559 engine_id
, add_id_to_iface
)) {
560 VLOG_ERR("bridge %s: problem setting netflow collectors",
564 /* Update the controller and related settings. It would be more
565 * straightforward to call this from bridge_reconfigure_one(), but we
566 * can't do it there for two reasons. First, and most importantly, at
567 * that point we don't know the dp_ifidx of any interfaces that have
568 * been added to the bridge (because we haven't actually added them to
569 * the datapath). Second, at that point we haven't set the datapath ID
570 * yet; when a controller is configured, resetting the datapath ID will
571 * immediately disconnect from the controller, so it's better to set
572 * the datapath ID before the controller. */
573 bridge_reconfigure_controller(br
);
575 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
576 for (i
= 0; i
< br
->n_ports
; i
++) {
577 struct port
*port
= br
->ports
[i
];
578 port_update_vlan_compat(port
);
581 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
582 brstp_reconfigure(br
);
587 bridge_pick_local_hw_addr(struct bridge
*br
, uint8_t ea
[ETH_ADDR_LEN
],
588 const char **devname
)
590 uint64_t requested_ea
;
596 /* Did the user request a particular MAC? */
597 requested_ea
= cfg_get_mac(0, "bridge.%s.mac", br
->name
);
599 eth_addr_from_uint64(requested_ea
, ea
);
600 if (eth_addr_is_multicast(ea
)) {
601 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
602 "address "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(ea
));
603 } else if (eth_addr_is_zero(ea
)) {
604 VLOG_ERR("bridge %s: cannot set MAC address to zero", br
->name
);
610 /* Otherwise choose the minimum MAC address among all of the interfaces.
611 * (Xen uses FE:FF:FF:FF:FF:FF for virtual interfaces so this will get the
612 * MAC of the physical interface in such an environment.) */
613 memset(ea
, 0xff, sizeof ea
);
614 for (i
= 0; i
< br
->n_ports
; i
++) {
615 struct port
*port
= br
->ports
[i
];
616 if (port
->is_mirror_output_port
) {
619 for (j
= 0; j
< port
->n_ifaces
; j
++) {
620 struct iface
*iface
= port
->ifaces
[j
];
621 uint8_t iface_ea
[ETH_ADDR_LEN
];
622 if (iface
->dp_ifidx
== ODPP_LOCAL
623 || cfg_get_bool(0, "iface.%s.internal", iface
->name
)) {
626 error
= netdev_nodev_get_etheraddr(iface
->name
, iface_ea
);
628 if (!eth_addr_is_multicast(iface_ea
) &&
629 !eth_addr_is_reserved(iface_ea
) &&
630 !eth_addr_is_zero(iface_ea
) &&
631 memcmp(iface_ea
, ea
, ETH_ADDR_LEN
) < 0) {
632 memcpy(ea
, iface_ea
, ETH_ADDR_LEN
);
633 *devname
= iface
->name
;
636 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
637 VLOG_ERR_RL(&rl
, "failed to obtain Ethernet address of %s: %s",
638 iface
->name
, strerror(error
));
642 if (eth_addr_is_multicast(ea
) || eth_addr_is_vif(ea
)) {
643 memcpy(ea
, br
->default_ea
, ETH_ADDR_LEN
);
645 VLOG_WARN("bridge %s: using default bridge Ethernet "
646 "address "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(ea
));
648 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT
,
649 br
->name
, ETH_ADDR_ARGS(ea
));
653 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
654 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
655 * a network device, then that network device's name must be passed in as
656 * 'devname'; if 'bridge_ea' was derived some other way, then 'devname' must be
657 * passed in as a null pointer. */
659 bridge_pick_datapath_id(struct bridge
*br
,
660 const uint8_t bridge_ea
[ETH_ADDR_LEN
],
664 * The procedure for choosing a bridge MAC address will, in the most
665 * ordinary case, also choose a unique MAC that we can use as a datapath
666 * ID. In some special cases, though, multiple bridges will end up with
667 * the same MAC address. This is OK for the bridges, but it will confuse
668 * the OpenFlow controller, because each datapath needs a unique datapath
671 * Datapath IDs must be unique. It is also very desirable that they be
672 * stable from one run to the next, so that policy set on a datapath
677 dpid
= cfg_get_dpid(0, "bridge.%s.datapath-id", br
->name
);
684 if (!netdev_get_vlan_vid(devname
, &vlan
)) {
686 * A bridge whose MAC address is taken from a VLAN network device
687 * (that is, a network device created with vconfig(8) or similar
688 * tool) will have the same MAC address as a bridge on the VLAN
689 * device's physical network device.
691 * Handle this case by hashing the physical network device MAC
692 * along with the VLAN identifier.
694 uint8_t buf
[ETH_ADDR_LEN
+ 2];
695 memcpy(buf
, bridge_ea
, ETH_ADDR_LEN
);
696 buf
[ETH_ADDR_LEN
] = vlan
>> 8;
697 buf
[ETH_ADDR_LEN
+ 1] = vlan
;
698 return dpid_from_hash(buf
, sizeof buf
);
701 * Assume that this bridge's MAC address is unique, since it
702 * doesn't fit any of the cases we handle specially.
707 * A purely internal bridge, that is, one that has no non-virtual
708 * network devices on it at all, is more difficult because it has no
709 * natural unique identifier at all.
711 * When the host is a XenServer, we handle this case by hashing the
712 * host's UUID with the name of the bridge. Names of bridges are
713 * persistent across XenServer reboots, although they can be reused if
714 * an internal network is destroyed and then a new one is later
715 * created, so this is fairly effective.
717 * When the host is not a XenServer, we punt by using a random MAC
718 * address on each run.
720 const char *host_uuid
= xenserver_get_host_uuid();
722 char *combined
= xasprintf("%s,%s", host_uuid
, br
->name
);
723 dpid
= dpid_from_hash(combined
, strlen(combined
));
729 return eth_addr_to_uint64(bridge_ea
);
733 dpid_from_hash(const void *data
, size_t n
)
735 uint8_t hash
[SHA1HashSize
];
737 BUILD_ASSERT_DECL(sizeof hash
>= ETH_ADDR_LEN
);
738 SHA1Bytes(data
, n
, hash
);
739 eth_addr_mark_random(hash
);
740 return eth_addr_to_uint64(hash
);
746 struct bridge
*br
, *next
;
750 LIST_FOR_EACH_SAFE (br
, next
, struct bridge
, node
, &all_bridges
) {
751 int error
= bridge_run_one(br
);
753 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
754 VLOG_ERR_RL(&rl
, "bridge %s: datapath was destroyed externally, "
755 "forcing reconfiguration", br
->name
);
769 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
770 ofproto_wait(br
->ofproto
);
771 if (br
->controller
) {
776 mac_learning_wait(br
->ml
);
783 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
784 * configuration changes. */
786 bridge_flush(struct bridge
*br
)
788 COVERAGE_INC(bridge_flush
);
791 mac_learning_flush(br
->ml
);
795 /* Bridge reconfiguration functions. */
797 static struct bridge
*
798 bridge_create(const char *name
)
803 assert(!bridge_lookup(name
));
804 br
= xcalloc(1, sizeof *br
);
806 error
= dpif_create(name
, &br
->dpif
);
807 if (error
== EEXIST
) {
808 error
= dpif_open(name
, &br
->dpif
);
810 VLOG_ERR("datapath %s already exists but cannot be opened: %s",
811 name
, strerror(error
));
815 dpif_flow_flush(&br
->dpif
);
817 VLOG_ERR("failed to create datapath %s: %s", name
, strerror(error
));
822 error
= ofproto_create(name
, &bridge_ofhooks
, br
, &br
->ofproto
);
824 VLOG_ERR("failed to create switch %s: %s", name
, strerror(error
));
825 dpif_delete(&br
->dpif
);
826 dpif_close(&br
->dpif
);
831 br
->name
= xstrdup(name
);
832 br
->ml
= mac_learning_create();
833 br
->sent_config_request
= false;
834 eth_addr_random(br
->default_ea
);
836 port_array_init(&br
->ifaces
);
839 br
->bond_next_rebalance
= time_msec() + 10000;
841 list_push_back(&all_bridges
, &br
->node
);
843 VLOG_INFO("created bridge %s on dp%u", br
->name
, dpif_id(&br
->dpif
));
849 bridge_destroy(struct bridge
*br
)
854 while (br
->n_ports
> 0) {
855 port_destroy(br
->ports
[br
->n_ports
- 1]);
857 list_remove(&br
->node
);
858 error
= dpif_delete(&br
->dpif
);
859 if (error
&& error
!= ENOENT
) {
860 VLOG_ERR("failed to delete dp%u: %s",
861 dpif_id(&br
->dpif
), strerror(error
));
863 dpif_close(&br
->dpif
);
864 ofproto_destroy(br
->ofproto
);
865 free(br
->controller
);
866 mac_learning_destroy(br
->ml
);
867 port_array_destroy(&br
->ifaces
);
874 static struct bridge
*
875 bridge_lookup(const char *name
)
879 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
880 if (!strcmp(br
->name
, name
)) {
888 bridge_exists(const char *name
)
890 return bridge_lookup(name
) ? true : false;
894 bridge_get_datapathid(const char *name
)
896 struct bridge
*br
= bridge_lookup(name
);
897 return br
? ofproto_get_datapath_id(br
->ofproto
) : 0;
901 bridge_run_one(struct bridge
*br
)
905 error
= ofproto_run1(br
->ofproto
);
911 mac_learning_run(br
->ml
, ofproto_get_revalidate_set(br
->ofproto
));
916 error
= ofproto_run2(br
->ofproto
, br
->flush
);
923 bridge_get_controller(const struct bridge
*br
)
925 const char *controller
;
927 controller
= cfg_get_string(0, "bridge.%s.controller", br
->name
);
929 controller
= cfg_get_string(0, "mgmt.controller");
931 return controller
&& controller
[0] ? controller
: NULL
;
935 bridge_reconfigure_one(struct bridge
*br
)
937 struct svec old_ports
, new_ports
, ifaces
;
938 struct svec listeners
, old_listeners
;
939 struct svec snoops
, old_snoops
;
942 /* Collect old ports. */
943 svec_init(&old_ports
);
944 for (i
= 0; i
< br
->n_ports
; i
++) {
945 svec_add(&old_ports
, br
->ports
[i
]->name
);
947 svec_sort(&old_ports
);
948 assert(svec_is_unique(&old_ports
));
950 /* Collect new ports. */
951 svec_init(&new_ports
);
952 cfg_get_all_keys(&new_ports
, "bridge.%s.port", br
->name
);
953 svec_sort(&new_ports
);
954 if (bridge_get_controller(br
) && !svec_contains(&new_ports
, br
->name
)) {
955 svec_add(&new_ports
, br
->name
);
956 svec_sort(&new_ports
);
958 if (!svec_is_unique(&new_ports
)) {
959 VLOG_WARN("bridge %s: %s specified twice as bridge port",
960 br
->name
, svec_get_duplicate(&new_ports
));
961 svec_unique(&new_ports
);
964 ofproto_set_mgmt_id(br
->ofproto
, mgmt_id
);
966 /* Get rid of deleted ports and add new ports. */
967 for (i
= 0; i
< br
->n_ports
; ) {
968 struct port
*port
= br
->ports
[i
];
969 if (!svec_contains(&new_ports
, port
->name
)) {
975 for (i
= 0; i
< new_ports
.n
; i
++) {
976 const char *name
= new_ports
.names
[i
];
977 if (!svec_contains(&old_ports
, name
)) {
978 port_create(br
, name
);
981 svec_destroy(&old_ports
);
982 svec_destroy(&new_ports
);
984 /* Reconfigure all ports. */
985 for (i
= 0; i
< br
->n_ports
; i
++) {
986 port_reconfigure(br
->ports
[i
]);
989 /* Check and delete duplicate interfaces. */
991 for (i
= 0; i
< br
->n_ports
; ) {
992 struct port
*port
= br
->ports
[i
];
993 for (j
= 0; j
< port
->n_ifaces
; ) {
994 struct iface
*iface
= port
->ifaces
[j
];
995 if (svec_contains(&ifaces
, iface
->name
)) {
996 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
998 br
->name
, iface
->name
, port
->name
);
999 iface_destroy(iface
);
1001 svec_add(&ifaces
, iface
->name
);
1006 if (!port
->n_ifaces
) {
1007 VLOG_ERR("%s port has no interfaces, dropping", port
->name
);
1013 svec_destroy(&ifaces
);
1015 /* Delete all flows if we're switching from connected to standalone or vice
1016 * versa. (XXX Should we delete all flows if we are switching from one
1017 * controller to another?) */
1019 /* Configure OpenFlow management listeners. */
1020 svec_init(&listeners
);
1021 cfg_get_all_strings(&listeners
, "bridge.%s.openflow.listeners", br
->name
);
1023 svec_add_nocopy(&listeners
, xasprintf("punix:%s/%s.mgmt",
1024 ovs_rundir
, br
->name
));
1025 } else if (listeners
.n
== 1 && !strcmp(listeners
.names
[0], "none")) {
1026 svec_clear(&listeners
);
1028 svec_sort_unique(&listeners
);
1030 svec_init(&old_listeners
);
1031 ofproto_get_listeners(br
->ofproto
, &old_listeners
);
1032 svec_sort_unique(&old_listeners
);
1034 if (!svec_equal(&listeners
, &old_listeners
)) {
1035 ofproto_set_listeners(br
->ofproto
, &listeners
);
1037 svec_destroy(&listeners
);
1038 svec_destroy(&old_listeners
);
1040 /* Configure OpenFlow controller connection snooping. */
1042 cfg_get_all_strings(&snoops
, "bridge.%s.openflow.snoops", br
->name
);
1044 svec_add_nocopy(&snoops
, xasprintf("punix:%s/%s.snoop",
1045 ovs_rundir
, br
->name
));
1046 } else if (snoops
.n
== 1 && !strcmp(snoops
.names
[0], "none")) {
1047 svec_clear(&snoops
);
1049 svec_sort_unique(&snoops
);
1051 svec_init(&old_snoops
);
1052 ofproto_get_snoops(br
->ofproto
, &old_snoops
);
1053 svec_sort_unique(&old_snoops
);
1055 if (!svec_equal(&snoops
, &old_snoops
)) {
1056 ofproto_set_snoops(br
->ofproto
, &snoops
);
1058 svec_destroy(&snoops
);
1059 svec_destroy(&old_snoops
);
1061 mirror_reconfigure(br
);
1065 bridge_reconfigure_controller(struct bridge
*br
)
1067 char *pfx
= xasprintf("bridge.%s.controller", br
->name
);
1068 const char *controller
;
1070 controller
= bridge_get_controller(br
);
1071 if ((br
->controller
!= NULL
) != (controller
!= NULL
)) {
1072 ofproto_flush_flows(br
->ofproto
);
1074 free(br
->controller
);
1075 br
->controller
= controller
? xstrdup(controller
) : NULL
;
1078 const char *fail_mode
;
1079 int max_backoff
, probe
;
1080 int rate_limit
, burst_limit
;
1082 if (!strcmp(controller
, "discover")) {
1083 ofproto_set_discovery(br
->ofproto
, true,
1084 cfg_get_string(0, "%s.accept-regex", pfx
),
1085 cfg_get_bool(0, "%s.update-resolv.conf",
1088 struct netdev
*netdev
;
1092 in_band
= (!cfg_is_valid(CFG_BOOL
| CFG_REQUIRED
,
1094 || cfg_get_bool(0, "%s.in-band", pfx
));
1095 ofproto_set_discovery(br
->ofproto
, false, NULL
, NULL
);
1096 ofproto_set_in_band(br
->ofproto
, in_band
);
1098 error
= netdev_open(br
->name
, NETDEV_ETH_TYPE_NONE
, &netdev
);
1100 if (cfg_is_valid(CFG_IP
| CFG_REQUIRED
, "%s.ip", pfx
)) {
1101 struct in_addr ip
, mask
, gateway
;
1102 ip
.s_addr
= cfg_get_ip(0, "%s.ip", pfx
);
1103 mask
.s_addr
= cfg_get_ip(0, "%s.netmask", pfx
);
1104 gateway
.s_addr
= cfg_get_ip(0, "%s.gateway", pfx
);
1106 netdev_turn_flags_on(netdev
, NETDEV_UP
, true);
1108 mask
.s_addr
= guess_netmask(ip
.s_addr
);
1110 if (!netdev_set_in4(netdev
, ip
, mask
)) {
1111 VLOG_INFO("bridge %s: configured IP address "IP_FMT
", "
1113 br
->name
, IP_ARGS(&ip
.s_addr
),
1114 IP_ARGS(&mask
.s_addr
));
1117 if (gateway
.s_addr
) {
1118 if (!netdev_add_router(gateway
)) {
1119 VLOG_INFO("bridge %s: configured gateway "IP_FMT
,
1120 br
->name
, IP_ARGS(&gateway
.s_addr
));
1124 netdev_close(netdev
);
1128 fail_mode
= cfg_get_string(0, "%s.fail-mode", pfx
);
1130 fail_mode
= cfg_get_string(0, "mgmt.fail-mode");
1132 ofproto_set_failure(br
->ofproto
,
1134 || !strcmp(fail_mode
, "standalone")
1135 || !strcmp(fail_mode
, "open")));
1137 probe
= cfg_get_int(0, "%s.inactivity-probe", pfx
);
1138 ofproto_set_probe_interval(br
->ofproto
,
1139 probe
? probe
: cfg_get_int(0, "mgmt.inactivity-probe"));
1141 max_backoff
= cfg_get_int(0, "%s.max-backoff", pfx
);
1143 max_backoff
= cfg_get_int(0, "mgmt.max-backoff");
1148 ofproto_set_max_backoff(br
->ofproto
, max_backoff
);
1150 rate_limit
= cfg_get_int(0, "%s.rate-limit", pfx
);
1152 rate_limit
= cfg_get_int(0, "mgmt.rate-limit");
1154 burst_limit
= cfg_get_int(0, "%s.burst-limit", pfx
);
1156 burst_limit
= cfg_get_int(0, "mgmt.burst-limit");
1158 ofproto_set_rate_limit(br
->ofproto
, rate_limit
, burst_limit
);
1160 ofproto_set_stp(br
->ofproto
, cfg_get_bool(0, "%s.stp", pfx
));
1162 if (cfg_has("%s.commands.acl", pfx
)) {
1163 struct svec command_acls
;
1166 svec_init(&command_acls
);
1167 cfg_get_all_strings(&command_acls
, "%s.commands.acl", pfx
);
1168 command_acl
= svec_join(&command_acls
, ",", "");
1170 ofproto_set_remote_execution(br
->ofproto
, command_acl
,
1171 cfg_get_string(0, "%s.commands.dir",
1174 svec_destroy(&command_acls
);
1177 ofproto_set_remote_execution(br
->ofproto
, NULL
, NULL
);
1180 union ofp_action action
;
1183 /* Set up a flow that matches every packet and directs them to
1184 * OFPP_NORMAL (which goes to us). */
1185 memset(&action
, 0, sizeof action
);
1186 action
.type
= htons(OFPAT_OUTPUT
);
1187 action
.output
.len
= htons(sizeof action
);
1188 action
.output
.port
= htons(OFPP_NORMAL
);
1189 memset(&flow
, 0, sizeof flow
);
1190 ofproto_add_flow(br
->ofproto
, &flow
, OFPFW_ALL
, 0,
1193 ofproto_set_in_band(br
->ofproto
, false);
1194 ofproto_set_max_backoff(br
->ofproto
, 1);
1195 ofproto_set_probe_interval(br
->ofproto
, 5);
1196 ofproto_set_failure(br
->ofproto
, false);
1197 ofproto_set_stp(br
->ofproto
, false);
1201 ofproto_set_controller(br
->ofproto
, br
->controller
);
1205 bridge_get_all_ifaces(const struct bridge
*br
, struct svec
*ifaces
)
1210 for (i
= 0; i
< br
->n_ports
; i
++) {
1211 struct port
*port
= br
->ports
[i
];
1212 for (j
= 0; j
< port
->n_ifaces
; j
++) {
1213 struct iface
*iface
= port
->ifaces
[j
];
1214 svec_add(ifaces
, iface
->name
);
1218 assert(svec_is_unique(ifaces
));
1221 /* For robustness, in case the administrator moves around datapath ports behind
1222 * our back, we re-check all the datapath port numbers here.
1224 * This function will set the 'dp_ifidx' members of interfaces that have
1225 * disappeared to -1, so only call this function from a context where those
1226 * 'struct iface's will be removed from the bridge. Otherwise, the -1
1227 * 'dp_ifidx'es will cause trouble later when we try to send them to the
1228 * datapath, which doesn't support UINT16_MAX+1 ports. */
1230 bridge_fetch_dp_ifaces(struct bridge
*br
)
1232 struct odp_port
*dpif_ports
;
1233 size_t n_dpif_ports
;
1236 /* Reset all interface numbers. */
1237 for (i
= 0; i
< br
->n_ports
; i
++) {
1238 struct port
*port
= br
->ports
[i
];
1239 for (j
= 0; j
< port
->n_ifaces
; j
++) {
1240 struct iface
*iface
= port
->ifaces
[j
];
1241 iface
->dp_ifidx
= -1;
1244 port_array_clear(&br
->ifaces
);
1246 dpif_port_list(&br
->dpif
, &dpif_ports
, &n_dpif_ports
);
1247 for (i
= 0; i
< n_dpif_ports
; i
++) {
1248 struct odp_port
*p
= &dpif_ports
[i
];
1249 struct iface
*iface
= iface_lookup(br
, p
->devname
);
1251 if (iface
->dp_ifidx
>= 0) {
1252 VLOG_WARN("dp%u reported interface %s twice",
1253 dpif_id(&br
->dpif
), p
->devname
);
1254 } else if (iface_from_dp_ifidx(br
, p
->port
)) {
1255 VLOG_WARN("dp%u reported interface %"PRIu16
" twice",
1256 dpif_id(&br
->dpif
), p
->port
);
1258 port_array_set(&br
->ifaces
, p
->port
, iface
);
1259 iface
->dp_ifidx
= p
->port
;
1266 /* Bridge packet processing functions. */
1269 bond_hash(const uint8_t mac
[ETH_ADDR_LEN
])
1271 return hash_bytes(mac
, ETH_ADDR_LEN
, 0) & BOND_MASK
;
1274 static struct bond_entry
*
1275 lookup_bond_entry(const struct port
*port
, const uint8_t mac
[ETH_ADDR_LEN
])
1277 return &port
->bond_hash
[bond_hash(mac
)];
1281 bond_choose_iface(const struct port
*port
)
1284 for (i
= 0; i
< port
->n_ifaces
; i
++) {
1285 if (port
->ifaces
[i
]->enabled
) {
1293 choose_output_iface(const struct port
*port
, const uint8_t *dl_src
,
1294 uint16_t *dp_ifidx
, tag_type
*tags
)
1296 struct iface
*iface
;
1298 assert(port
->n_ifaces
);
1299 if (port
->n_ifaces
== 1) {
1300 iface
= port
->ifaces
[0];
1302 struct bond_entry
*e
= lookup_bond_entry(port
, dl_src
);
1303 if (e
->iface_idx
< 0 || e
->iface_idx
>= port
->n_ifaces
1304 || !port
->ifaces
[e
->iface_idx
]->enabled
) {
1305 /* XXX select interface properly. The current interface selection
1306 * is only good for testing the rebalancing code. */
1307 e
->iface_idx
= bond_choose_iface(port
);
1308 if (e
->iface_idx
< 0) {
1309 *tags
|= port
->no_ifaces_tag
;
1312 e
->iface_tag
= tag_create_random();
1314 *tags
|= e
->iface_tag
;
1315 iface
= port
->ifaces
[e
->iface_idx
];
1317 *dp_ifidx
= iface
->dp_ifidx
;
1318 *tags
|= iface
->tag
; /* Currently only used for bonding. */
1323 bond_link_status_update(struct iface
*iface
, bool carrier
)
1325 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
1326 struct port
*port
= iface
->port
;
1328 if ((carrier
== iface
->enabled
) == (iface
->delay_expires
== LLONG_MAX
)) {
1329 /* Nothing to do. */
1332 VLOG_INFO_RL(&rl
, "interface %s: carrier %s",
1333 iface
->name
, carrier
? "detected" : "dropped");
1334 if (carrier
== iface
->enabled
) {
1335 iface
->delay_expires
= LLONG_MAX
;
1336 VLOG_INFO_RL(&rl
, "interface %s: will not be %s",
1337 iface
->name
, carrier
? "disabled" : "enabled");
1339 int delay
= carrier
? port
->updelay
: port
->downdelay
;
1340 iface
->delay_expires
= time_msec() + delay
;
1343 "interface %s: will be %s if it stays %s for %d ms",
1345 carrier
? "enabled" : "disabled",
1346 carrier
? "up" : "down",
1353 bond_choose_active_iface(struct port
*port
)
1355 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
1357 port
->active_iface
= bond_choose_iface(port
);
1358 port
->active_iface_tag
= tag_create_random();
1359 if (port
->active_iface
>= 0) {
1360 VLOG_INFO_RL(&rl
, "port %s: active interface is now %s",
1361 port
->name
, port
->ifaces
[port
->active_iface
]->name
);
1363 VLOG_WARN_RL(&rl
, "port %s: all ports disabled, no active interface",
1369 bond_enable_slave(struct iface
*iface
, bool enable
)
1371 struct port
*port
= iface
->port
;
1372 struct bridge
*br
= port
->bridge
;
1374 iface
->delay_expires
= LLONG_MAX
;
1375 if (enable
== iface
->enabled
) {
1379 iface
->enabled
= enable
;
1380 if (!iface
->enabled
) {
1381 VLOG_WARN("interface %s: enabled", iface
->name
);
1382 ofproto_revalidate(br
->ofproto
, iface
->tag
);
1383 if (iface
->port_ifidx
== port
->active_iface
) {
1384 ofproto_revalidate(br
->ofproto
,
1385 port
->active_iface_tag
);
1386 bond_choose_active_iface(port
);
1388 bond_send_learning_packets(port
);
1390 VLOG_WARN("interface %s: disabled", iface
->name
);
1391 if (port
->active_iface
< 0) {
1392 ofproto_revalidate(br
->ofproto
, port
->no_ifaces_tag
);
1393 bond_choose_active_iface(port
);
1394 bond_send_learning_packets(port
);
1396 iface
->tag
= tag_create_random();
1401 bond_run(struct bridge
*br
)
1405 for (i
= 0; i
< br
->n_ports
; i
++) {
1406 struct port
*port
= br
->ports
[i
];
1407 if (port
->n_ifaces
< 2) {
1410 for (j
= 0; j
< port
->n_ifaces
; j
++) {
1411 struct iface
*iface
= port
->ifaces
[j
];
1412 if (time_msec() >= iface
->delay_expires
) {
1413 bond_enable_slave(iface
, !iface
->enabled
);
1420 bond_wait(struct bridge
*br
)
1424 for (i
= 0; i
< br
->n_ports
; i
++) {
1425 struct port
*port
= br
->ports
[i
];
1426 if (port
->n_ifaces
< 2) {
1429 for (j
= 0; j
< port
->n_ifaces
; j
++) {
1430 struct iface
*iface
= port
->ifaces
[j
];
1431 if (iface
->delay_expires
!= LLONG_MAX
) {
1432 poll_timer_wait(iface
->delay_expires
- time_msec());
1439 set_dst(struct dst
*p
, const flow_t
*flow
,
1440 const struct port
*in_port
, const struct port
*out_port
,
1445 * XXX This uses too many tags: any broadcast flow will get one tag per
1446 * destination port, and thus a broadcast on a switch of any size is likely
1447 * to have all tag bits set. We should figure out a way to be smarter.
1449 * This is OK when STP is disabled, because stp_state_tag is 0 then. */
1450 *tags
|= out_port
->stp_state_tag
;
1451 if (!(out_port
->stp_state
& (STP_DISABLED
| STP_FORWARDING
))) {
1455 p
->vlan
= (out_port
->vlan
>= 0 ? OFP_VLAN_NONE
1456 : in_port
->vlan
>= 0 ? in_port
->vlan
1457 : ntohs(flow
->dl_vlan
));
1458 return choose_output_iface(out_port
, flow
->dl_src
, &p
->dp_ifidx
, tags
);
1462 swap_dst(struct dst
*p
, struct dst
*q
)
1464 struct dst tmp
= *p
;
1469 /* Moves all the dsts with vlan == 'vlan' to the front of the 'n_dsts' in
1470 * 'dsts'. (This may help performance by reducing the number of VLAN changes
1471 * that we push to the datapath. We could in fact fully sort the array by
1472 * vlan, but in most cases there are at most two different vlan tags so that's
1473 * possibly overkill.) */
1475 partition_dsts(struct dst
*dsts
, size_t n_dsts
, int vlan
)
1477 struct dst
*first
= dsts
;
1478 struct dst
*last
= dsts
+ n_dsts
;
1480 while (first
!= last
) {
1482 * - All dsts < first have vlan == 'vlan'.
1483 * - All dsts >= last have vlan != 'vlan'.
1484 * - first < last. */
1485 while (first
->vlan
== vlan
) {
1486 if (++first
== last
) {
1491 /* Same invariants, plus one additional:
1492 * - first->vlan != vlan.
1494 while (last
[-1].vlan
!= vlan
) {
1495 if (--last
== first
) {
1500 /* Same invariants, plus one additional:
1501 * - last[-1].vlan == vlan.*/
1502 swap_dst(first
++, --last
);
1507 mirror_mask_ffs(mirror_mask_t mask
)
1509 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask
));
1514 dst_is_duplicate(const struct dst
*dsts
, size_t n_dsts
,
1515 const struct dst
*test
)
1518 for (i
= 0; i
< n_dsts
; i
++) {
1519 if (dsts
[i
].vlan
== test
->vlan
&& dsts
[i
].dp_ifidx
== test
->dp_ifidx
) {
1527 port_trunks_vlan(const struct port
*port
, uint16_t vlan
)
1529 return port
->vlan
< 0 && bitmap_is_set(port
->trunks
, vlan
);
1533 port_includes_vlan(const struct port
*port
, uint16_t vlan
)
1535 return vlan
== port
->vlan
|| port_trunks_vlan(port
, vlan
);
1539 compose_dsts(const struct bridge
*br
, const flow_t
*flow
, uint16_t vlan
,
1540 const struct port
*in_port
, const struct port
*out_port
,
1541 struct dst dsts
[], tag_type
*tags
)
1543 mirror_mask_t mirrors
= in_port
->src_mirrors
;
1544 struct dst
*dst
= dsts
;
1547 *tags
|= in_port
->stp_state_tag
;
1548 if (out_port
== FLOOD_PORT
) {
1549 /* XXX use ODP_FLOOD if no vlans or bonding. */
1550 /* XXX even better, define each VLAN as a datapath port group */
1551 for (i
= 0; i
< br
->n_ports
; i
++) {
1552 struct port
*port
= br
->ports
[i
];
1553 if (port
!= in_port
&& port_includes_vlan(port
, vlan
)
1554 && !port
->is_mirror_output_port
1555 && set_dst(dst
, flow
, in_port
, port
, tags
)) {
1556 mirrors
|= port
->dst_mirrors
;
1560 } else if (out_port
&& set_dst(dst
, flow
, in_port
, out_port
, tags
)) {
1561 mirrors
|= out_port
->dst_mirrors
;
1566 struct mirror
*m
= br
->mirrors
[mirror_mask_ffs(mirrors
) - 1];
1567 if (!m
->n_vlans
|| vlan_is_mirrored(m
, vlan
)) {
1569 if (set_dst(dst
, flow
, in_port
, m
->out_port
, tags
)
1570 && !dst_is_duplicate(dsts
, dst
- dsts
, dst
)) {
1574 for (i
= 0; i
< br
->n_ports
; i
++) {
1575 struct port
*port
= br
->ports
[i
];
1576 if (port_includes_vlan(port
, m
->out_vlan
)
1577 && set_dst(dst
, flow
, in_port
, port
, tags
)
1578 && !dst_is_duplicate(dsts
, dst
- dsts
, dst
))
1580 if (port
->vlan
< 0) {
1581 dst
->vlan
= m
->out_vlan
;
1583 if (dst
->dp_ifidx
== flow
->in_port
1584 && dst
->vlan
== vlan
) {
1585 /* Don't send out input port on same VLAN. */
1593 mirrors
&= mirrors
- 1;
1596 partition_dsts(dsts
, dst
- dsts
, ntohs(flow
->dl_vlan
));
1601 print_dsts(const struct dst
*dsts
, size_t n
)
1603 for (; n
--; dsts
++) {
1604 printf(">p%"PRIu16
, dsts
->dp_ifidx
);
1605 if (dsts
->vlan
!= OFP_VLAN_NONE
) {
1606 printf("v%"PRIu16
, dsts
->vlan
);
1612 compose_actions(struct bridge
*br
, const flow_t
*flow
, uint16_t vlan
,
1613 const struct port
*in_port
, const struct port
*out_port
,
1614 tag_type
*tags
, struct odp_actions
*actions
)
1616 struct dst dsts
[DP_MAX_PORTS
* (MAX_MIRRORS
+ 1)];
1618 const struct dst
*p
;
1621 n_dsts
= compose_dsts(br
, flow
, vlan
, in_port
, out_port
, dsts
, tags
);
1623 cur_vlan
= ntohs(flow
->dl_vlan
);
1624 for (p
= dsts
; p
< &dsts
[n_dsts
]; p
++) {
1625 union odp_action
*a
;
1626 if (p
->vlan
!= cur_vlan
) {
1627 if (p
->vlan
== OFP_VLAN_NONE
) {
1628 odp_actions_add(actions
, ODPAT_STRIP_VLAN
);
1630 a
= odp_actions_add(actions
, ODPAT_SET_VLAN_VID
);
1631 a
->vlan_vid
.vlan_vid
= htons(p
->vlan
);
1635 a
= odp_actions_add(actions
, ODPAT_OUTPUT
);
1636 a
->output
.port
= p
->dp_ifidx
;
1641 is_bcast_arp_reply(const flow_t
*flow
, const struct ofpbuf
*packet
)
1643 struct arp_eth_header
*arp
= (struct arp_eth_header
*) packet
->data
;
1644 return (flow
->dl_type
== htons(ETH_TYPE_ARP
)
1645 && eth_addr_is_broadcast(flow
->dl_dst
)
1646 && packet
->size
>= sizeof(struct arp_eth_header
)
1647 && arp
->ar_op
== ARP_OP_REQUEST
);
1650 /* If the composed actions may be applied to any packet in the given 'flow',
1651 * returns true. Otherwise, the actions should only be applied to 'packet', or
1652 * not at all, if 'packet' was NULL. */
1654 process_flow(struct bridge
*br
, const flow_t
*flow
,
1655 const struct ofpbuf
*packet
, struct odp_actions
*actions
,
1658 struct iface
*in_iface
;
1659 struct port
*in_port
;
1660 struct port
*out_port
= NULL
; /* By default, drop the packet/flow. */
1663 /* Find the interface and port structure for the received packet. */
1664 in_iface
= iface_from_dp_ifidx(br
, flow
->in_port
);
1666 /* No interface? Something fishy... */
1667 if (packet
!= NULL
) {
1668 /* Odd. A few possible reasons here:
1670 * - We deleted an interface but there are still a few packets
1671 * queued up from it.
1673 * - Someone externally added an interface (e.g. with "ovs-dpctl
1674 * add-if") that we don't know about.
1676 * - Packet arrived on the local port but the local port is not
1677 * one of our bridge ports.
1679 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1681 VLOG_WARN_RL(&rl
, "bridge %s: received packet on unknown "
1682 "interface %"PRIu16
, br
->name
, flow
->in_port
);
1685 /* Return without adding any actions, to drop packets on this flow. */
1688 in_port
= in_iface
->port
;
1690 /* Figure out what VLAN this packet belongs to.
1692 * Note that dl_vlan of 0 and of OFP_VLAN_NONE both mean that the packet
1693 * belongs to VLAN 0, so we should treat both cases identically. (In the
1694 * former case, the packet has an 802.1Q header that specifies VLAN 0,
1695 * presumably to allow a priority to be specified. In the latter case, the
1696 * packet does not have any 802.1Q header.) */
1697 vlan
= ntohs(flow
->dl_vlan
);
1698 if (vlan
== OFP_VLAN_NONE
) {
1701 if (in_port
->vlan
>= 0) {
1703 /* XXX support double tagging? */
1704 if (packet
!= NULL
) {
1705 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1706 VLOG_WARN_RL(&rl
, "bridge %s: dropping VLAN %"PRIu16
" tagged "
1707 "packet received on port %s configured with "
1708 "implicit VLAN %"PRIu16
,
1709 br
->name
, ntohs(flow
->dl_vlan
),
1710 in_port
->name
, in_port
->vlan
);
1714 vlan
= in_port
->vlan
;
1716 if (!port_includes_vlan(in_port
, vlan
)) {
1717 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1718 VLOG_WARN_RL(&rl
, "bridge %s: dropping VLAN %d tagged "
1719 "packet received on port %s not configured for "
1721 br
->name
, vlan
, in_port
->name
, vlan
);
1726 /* Drop frames for ports that STP wants entirely killed (both for
1727 * forwarding and for learning). Later, after we do learning, we'll drop
1728 * the frames that STP wants to do learning but not forwarding on. */
1729 if (in_port
->stp_state
& (STP_LISTENING
| STP_BLOCKING
)) {
1733 /* Drop frames for reserved multicast addresses. */
1734 if (eth_addr_is_reserved(flow
->dl_dst
)) {
1738 /* Drop frames on ports reserved for mirroring. */
1739 if (in_port
->is_mirror_output_port
) {
1740 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1741 VLOG_WARN_RL(&rl
, "bridge %s: dropping packet received on port %s, "
1742 "which is reserved exclusively for mirroring",
1743 br
->name
, in_port
->name
);
1747 /* Drop multicast and broadcast packets on inactive bonded interfaces, to
1748 * avoid receiving duplicates. */
1749 if (in_port
->n_ifaces
> 1 && eth_addr_is_multicast(flow
->dl_dst
)) {
1750 *tags
|= in_port
->active_iface_tag
;
1751 if (in_port
->active_iface
!= in_iface
->port_ifidx
) {
1757 out_port
= FLOOD_PORT
;
1763 /* Don't try to learn from revalidation. */
1765 } else if (in_port
->n_ifaces
> 1) {
1766 /* If the packet arrived on a bonded port, don't learn from it
1767 * unless we haven't learned any port at all for that address
1768 * (because we probably sent the packet on one bonded interface and
1769 * got it back on the other). Broadcast ARP replies are an
1770 * exception to this rule: the host has moved to another switch. */
1771 int src_idx
= mac_learning_lookup(br
->ml
, flow
->dl_src
, vlan
);
1772 may_learn
= (src_idx
< 0
1773 || src_idx
== in_port
->port_idx
1774 || is_bcast_arp_reply(flow
, packet
));
1779 /* Learn source MAC. */
1781 tag_type rev_tag
= mac_learning_learn(br
->ml
, flow
->dl_src
,
1782 vlan
, in_port
->port_idx
);
1784 /* The log messages here could actually be useful in debugging,
1785 * so keep the rate limit relatively high. */
1786 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(30,
1788 VLOG_DBG_RL(&rl
, "bridge %s: learned that "ETH_ADDR_FMT
" is "
1789 "on port %s in VLAN %d",
1790 br
->name
, ETH_ADDR_ARGS(flow
->dl_src
),
1791 in_port
->name
, vlan
);
1792 ofproto_revalidate(br
->ofproto
, rev_tag
);
1796 /* Determine output port. */
1797 out_port_idx
= mac_learning_lookup_tag(br
->ml
, flow
->dl_dst
, vlan
,
1799 if (out_port_idx
>= 0 && out_port_idx
< br
->n_ports
) {
1800 out_port
= br
->ports
[out_port_idx
];
1804 /* Don't send packets out their input ports. Don't forward frames that STP
1805 * wants us to discard. */
1806 if (in_port
== out_port
|| in_port
->stp_state
== STP_LEARNING
) {
1811 compose_actions(br
, flow
, vlan
, in_port
, out_port
, tags
, actions
);
1814 * We send out only a single packet, instead of setting up a flow, if the
1815 * packet is an ARP directed to broadcast that arrived on a bonded
1816 * interface. In such a situation ARP requests and replies must be handled
1817 * differently, but OpenFlow unfortunately can't distinguish them.
1819 return (in_port
->n_ifaces
< 2
1820 || flow
->dl_type
!= htons(ETH_TYPE_ARP
)
1821 || !eth_addr_is_broadcast(flow
->dl_dst
));
1824 /* Careful: 'opp' is in host byte order and opp->port_no is an OFP port
1827 bridge_port_changed_ofhook_cb(enum ofp_port_reason reason
,
1828 const struct ofp_phy_port
*opp
,
1831 struct bridge
*br
= br_
;
1832 struct iface
*iface
;
1835 iface
= iface_from_dp_ifidx(br
, ofp_port_to_odp_port(opp
->port_no
));
1841 if (reason
== OFPPR_DELETE
) {
1842 VLOG_WARN("bridge %s: interface %s deleted unexpectedly",
1843 br
->name
, iface
->name
);
1844 iface_destroy(iface
);
1845 if (!port
->n_ifaces
) {
1846 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1847 br
->name
, port
->name
);
1853 memcpy(iface
->mac
, opp
->hw_addr
, ETH_ADDR_LEN
);
1854 if (port
->n_ifaces
> 1) {
1855 bool up
= !(opp
->state
& OFPPS_LINK_DOWN
);
1856 bond_link_status_update(iface
, up
);
1857 port_update_bond_compat(port
);
1863 bridge_normal_ofhook_cb(const flow_t
*flow
, const struct ofpbuf
*packet
,
1864 struct odp_actions
*actions
, tag_type
*tags
, void *br_
)
1866 struct bridge
*br
= br_
;
1869 if (flow
->dl_type
== htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
1870 && eth_addr_equals(flow
->dl_dst
, stp_eth_addr
)) {
1871 brstp_receive(br
, flow
, payload
);
1876 COVERAGE_INC(bridge_process_flow
);
1877 return process_flow(br
, flow
, packet
, actions
, tags
);
1881 bridge_account_flow_ofhook_cb(const flow_t
*flow
,
1882 const union odp_action
*actions
,
1883 size_t n_actions
, unsigned long long int n_bytes
,
1886 struct bridge
*br
= br_
;
1887 const union odp_action
*a
;
1889 if (!br
->has_bonded_ports
) {
1893 for (a
= actions
; a
< &actions
[n_actions
]; a
++) {
1894 if (a
->type
== ODPAT_OUTPUT
) {
1895 struct port
*port
= port_from_dp_ifidx(br
, a
->output
.port
);
1896 if (port
&& port
->n_ifaces
>= 2) {
1897 struct bond_entry
*e
= lookup_bond_entry(port
, flow
->dl_src
);
1898 e
->tx_bytes
+= n_bytes
;
1905 bridge_account_checkpoint_ofhook_cb(void *br_
)
1907 struct bridge
*br
= br_
;
1910 if (!br
->has_bonded_ports
) {
1914 /* The current ofproto implementation calls this callback at least once a
1915 * second, so this timer implementation is sufficient. */
1916 if (time_msec() < br
->bond_next_rebalance
) {
1919 br
->bond_next_rebalance
= time_msec() + 10000;
1921 for (i
= 0; i
< br
->n_ports
; i
++) {
1922 struct port
*port
= br
->ports
[i
];
1923 if (port
->n_ifaces
> 1) {
1924 bond_rebalance_port(port
);
1929 static struct ofhooks bridge_ofhooks
= {
1930 bridge_port_changed_ofhook_cb
,
1931 bridge_normal_ofhook_cb
,
1932 bridge_account_flow_ofhook_cb
,
1933 bridge_account_checkpoint_ofhook_cb
,
1936 /* Bonding functions. */
1938 /* Statistics for a single interface on a bonded port, used for load-based
1939 * bond rebalancing. */
1940 struct slave_balance
{
1941 struct iface
*iface
; /* The interface. */
1942 uint64_t tx_bytes
; /* Sum of hashes[*]->tx_bytes. */
1944 /* All the "bond_entry"s that are assigned to this interface, in order of
1945 * increasing tx_bytes. */
1946 struct bond_entry
**hashes
;
1950 /* Sorts pointers to pointers to bond_entries in ascending order by the
1951 * interface to which they are assigned, and within a single interface in
1952 * ascending order of bytes transmitted. */
1954 compare_bond_entries(const void *a_
, const void *b_
)
1956 const struct bond_entry
*const *ap
= a_
;
1957 const struct bond_entry
*const *bp
= b_
;
1958 const struct bond_entry
*a
= *ap
;
1959 const struct bond_entry
*b
= *bp
;
1960 if (a
->iface_idx
!= b
->iface_idx
) {
1961 return a
->iface_idx
> b
->iface_idx
? 1 : -1;
1962 } else if (a
->tx_bytes
!= b
->tx_bytes
) {
1963 return a
->tx_bytes
> b
->tx_bytes
? 1 : -1;
1969 /* Sorts slave_balances so that enabled ports come first, and otherwise in
1970 * *descending* order by number of bytes transmitted. */
1972 compare_slave_balance(const void *a_
, const void *b_
)
1974 const struct slave_balance
*a
= a_
;
1975 const struct slave_balance
*b
= b_
;
1976 if (a
->iface
->enabled
!= b
->iface
->enabled
) {
1977 return a
->iface
->enabled
? -1 : 1;
1978 } else if (a
->tx_bytes
!= b
->tx_bytes
) {
1979 return a
->tx_bytes
> b
->tx_bytes
? -1 : 1;
1986 swap_bals(struct slave_balance
*a
, struct slave_balance
*b
)
1988 struct slave_balance tmp
= *a
;
1993 /* Restores the 'n_bals' slave_balance structures in 'bals' to sorted order
1994 * given that 'p' (and only 'p') might be in the wrong location.
1996 * This function invalidates 'p', since it might now be in a different memory
1999 resort_bals(struct slave_balance
*p
,
2000 struct slave_balance bals
[], size_t n_bals
)
2003 for (; p
> bals
&& p
->tx_bytes
> p
[-1].tx_bytes
; p
--) {
2004 swap_bals(p
, p
- 1);
2006 for (; p
< &bals
[n_bals
- 1] && p
->tx_bytes
< p
[1].tx_bytes
; p
++) {
2007 swap_bals(p
, p
+ 1);
2013 log_bals(const struct slave_balance
*bals
, size_t n_bals
, struct port
*port
)
2015 if (VLOG_IS_DBG_ENABLED()) {
2016 struct ds ds
= DS_EMPTY_INITIALIZER
;
2017 const struct slave_balance
*b
;
2019 for (b
= bals
; b
< bals
+ n_bals
; b
++) {
2023 ds_put_char(&ds
, ',');
2025 ds_put_format(&ds
, " %s %"PRIu64
"kB",
2026 b
->iface
->name
, b
->tx_bytes
/ 1024);
2028 if (!b
->iface
->enabled
) {
2029 ds_put_cstr(&ds
, " (disabled)");
2031 if (b
->n_hashes
> 0) {
2032 ds_put_cstr(&ds
, " (");
2033 for (i
= 0; i
< b
->n_hashes
; i
++) {
2034 const struct bond_entry
*e
= b
->hashes
[i
];
2036 ds_put_cstr(&ds
, " + ");
2038 ds_put_format(&ds
, "h%td: %"PRIu64
"kB",
2039 e
- port
->bond_hash
, e
->tx_bytes
/ 1024);
2041 ds_put_cstr(&ds
, ")");
2044 VLOG_DBG("bond %s:%s", port
->name
, ds_cstr(&ds
));
2049 /* Shifts 'hash' from 'from' to 'to' within 'port'. */
2051 bond_shift_load(struct slave_balance
*from
, struct slave_balance
*to
,
2052 struct bond_entry
*hash
)
2054 struct port
*port
= from
->iface
->port
;
2055 uint64_t delta
= hash
->tx_bytes
;
2057 VLOG_INFO("bond %s: shift %"PRIu64
"kB of load (with hash %td) "
2058 "from %s to %s (now carrying %"PRIu64
"kB and "
2059 "%"PRIu64
"kB load, respectively)",
2060 port
->name
, delta
/ 1024, hash
- port
->bond_hash
,
2061 from
->iface
->name
, to
->iface
->name
,
2062 (from
->tx_bytes
- delta
) / 1024,
2063 (to
->tx_bytes
+ delta
) / 1024);
2065 /* Delete element from from->hashes.
2067 * We don't bother to add the element to to->hashes because not only would
2068 * it require more work, the only purpose it would be to allow that hash to
2069 * be migrated to another slave in this rebalancing run, and there is no
2070 * point in doing that. */
2071 if (from
->hashes
[0] == hash
) {
2074 int i
= hash
- from
->hashes
[0];
2075 memmove(from
->hashes
+ i
, from
->hashes
+ i
+ 1,
2076 (from
->n_hashes
- (i
+ 1)) * sizeof *from
->hashes
);
2080 /* Shift load away from 'from' to 'to'. */
2081 from
->tx_bytes
-= delta
;
2082 to
->tx_bytes
+= delta
;
2084 /* Arrange for flows to be revalidated. */
2085 ofproto_revalidate(port
->bridge
->ofproto
, hash
->iface_tag
);
2086 hash
->iface_idx
= to
->iface
->port_ifidx
;
2087 hash
->iface_tag
= tag_create_random();
2091 bond_rebalance_port(struct port
*port
)
2093 struct slave_balance bals
[DP_MAX_PORTS
];
2095 struct bond_entry
*hashes
[BOND_MASK
+ 1];
2096 struct slave_balance
*b
, *from
, *to
;
2097 struct bond_entry
*e
;
2100 /* Sets up 'bals' to describe each of the port's interfaces, sorted in
2101 * descending order of tx_bytes, so that bals[0] represents the most
2102 * heavily loaded slave and bals[n_bals - 1] represents the least heavily
2105 * The code is a bit tricky: to avoid dynamically allocating a 'hashes'
2106 * array for each slave_balance structure, we sort our local array of
2107 * hashes in order by slave, so that all of the hashes for a given slave
2108 * become contiguous in memory, and then we point each 'hashes' members of
2109 * a slave_balance structure to the start of a contiguous group. */
2110 n_bals
= port
->n_ifaces
;
2111 for (b
= bals
; b
< &bals
[n_bals
]; b
++) {
2112 b
->iface
= port
->ifaces
[b
- bals
];
2117 for (i
= 0; i
<= BOND_MASK
; i
++) {
2118 hashes
[i
] = &port
->bond_hash
[i
];
2120 qsort(hashes
, BOND_MASK
+ 1, sizeof *hashes
, compare_bond_entries
);
2121 for (i
= 0; i
<= BOND_MASK
; i
++) {
2123 if (e
->iface_idx
>= 0 && e
->iface_idx
< port
->n_ifaces
) {
2124 b
= &bals
[e
->iface_idx
];
2125 b
->tx_bytes
+= e
->tx_bytes
;
2127 b
->hashes
= &hashes
[i
];
2132 qsort(bals
, n_bals
, sizeof *bals
, compare_slave_balance
);
2133 log_bals(bals
, n_bals
, port
);
2135 /* Discard slaves that aren't enabled (which were sorted to the back of the
2136 * array earlier). */
2137 while (!bals
[n_bals
- 1].iface
->enabled
) {
2144 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
2145 to
= &bals
[n_bals
- 1];
2146 for (from
= bals
; from
< to
; ) {
2147 uint64_t overload
= from
->tx_bytes
- to
->tx_bytes
;
2148 if (overload
< to
->tx_bytes
>> 5 || overload
< 100000) {
2149 /* The extra load on 'from' (and all less-loaded slaves), compared
2150 * to that of 'to' (the least-loaded slave), is less than ~3%, or
2151 * it is less than ~1Mbps. No point in rebalancing. */
2153 } else if (from
->n_hashes
== 1) {
2154 /* 'from' only carries a single MAC hash, so we can't shift any
2155 * load away from it, even though we want to. */
2158 /* 'from' is carrying significantly more load than 'to', and that
2159 * load is split across at least two different hashes. Pick a hash
2160 * to migrate to 'to' (the least-loaded slave), given that doing so
2161 * must not cause 'to''s load to exceed 'from''s load.
2163 * The sort order we use means that we prefer to shift away the
2164 * smallest hashes instead of the biggest ones. There is little
2165 * reason behind this decision; we could use the opposite sort
2166 * order to shift away big hashes ahead of small ones. */
2169 for (i
= 0; i
< from
->n_hashes
; i
++) {
2170 uint64_t delta
= from
->hashes
[i
]->tx_bytes
;
2171 if (to
->tx_bytes
+ delta
< from
->tx_bytes
- delta
) {
2175 if (i
< from
->n_hashes
) {
2176 bond_shift_load(from
, to
, from
->hashes
[i
]);
2178 /* Re-sort 'bals'. Note that this may make 'from' and 'to'
2179 * point to different slave_balance structures. It is only
2180 * valid to do these two operations in a row at all because we
2181 * know that 'from' will not move past 'to' and vice versa. */
2182 resort_bals(from
, bals
, n_bals
);
2183 resort_bals(to
, bals
, n_bals
);
2190 /* Implement exponentially weighted moving average. A weight of 1/2 causes
2191 * historical data to decay to <1% in 7 rebalancing runs. */
2192 for (e
= &port
->bond_hash
[0]; e
<= &port
->bond_hash
[BOND_MASK
]; e
++) {
2198 bond_send_learning_packets(struct port
*port
)
2200 struct bridge
*br
= port
->bridge
;
2201 struct mac_entry
*e
;
2202 struct ofpbuf packet
;
2203 int error
, n_packets
, n_errors
;
2205 if (!port
->n_ifaces
|| port
->active_iface
< 0 || !br
->ml
) {
2209 ofpbuf_init(&packet
, 128);
2210 error
= n_packets
= n_errors
= 0;
2211 LIST_FOR_EACH (e
, struct mac_entry
, lru_node
, &br
->ml
->lrus
) {
2212 static const char s
[] = "Open vSwitch Bond Failover";
2213 union ofp_action actions
[2], *a
;
2214 struct eth_header
*eth
;
2215 struct llc_snap_header
*llc_snap
;
2221 if (e
->port
== port
->port_idx
2222 || !choose_output_iface(port
, e
->mac
, &dp_ifidx
, &tags
)) {
2226 /* Compose packet to send. */
2227 ofpbuf_clear(&packet
);
2228 eth
= ofpbuf_put_zeros(&packet
, ETH_HEADER_LEN
);
2229 llc_snap
= ofpbuf_put_zeros(&packet
, LLC_SNAP_HEADER_LEN
);
2230 ofpbuf_put(&packet
, s
, sizeof s
); /* Includes null byte. */
2231 ofpbuf_put(&packet
, e
->mac
, ETH_ADDR_LEN
);
2233 memcpy(eth
->eth_dst
, eth_addr_broadcast
, ETH_ADDR_LEN
);
2234 memcpy(eth
->eth_src
, e
->mac
, ETH_ADDR_LEN
);
2235 eth
->eth_type
= htons(packet
.size
- ETH_HEADER_LEN
);
2237 llc_snap
->llc
.llc_dsap
= LLC_DSAP_SNAP
;
2238 llc_snap
->llc
.llc_ssap
= LLC_SSAP_SNAP
;
2239 llc_snap
->llc
.llc_cntl
= LLC_CNTL_SNAP
;
2240 memcpy(llc_snap
->snap
.snap_org
, "\x00\x23\x20", 3);
2241 llc_snap
->snap
.snap_type
= htons(0xf177); /* Random number. */
2243 /* Compose actions. */
2244 memset(actions
, 0, sizeof actions
);
2247 a
->vlan_vid
.type
= htons(OFPAT_SET_VLAN_VID
);
2248 a
->vlan_vid
.len
= htons(sizeof *a
);
2249 a
->vlan_vid
.vlan_vid
= htons(e
->vlan
);
2252 a
->output
.type
= htons(OFPAT_OUTPUT
);
2253 a
->output
.len
= htons(sizeof *a
);
2254 a
->output
.port
= htons(odp_port_to_ofp_port(dp_ifidx
));
2259 flow_extract(&packet
, ODPP_NONE
, &flow
);
2260 retval
= ofproto_send_packet(br
->ofproto
, &flow
, actions
, a
- actions
,
2267 ofpbuf_uninit(&packet
);
2270 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2271 VLOG_WARN_RL(&rl
, "bond %s: %d errors sending %d gratuitous learning "
2272 "packets, last error was: %s",
2273 port
->name
, n_errors
, n_packets
, strerror(error
));
2275 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2276 port
->name
, n_packets
);
2280 /* Bonding unixctl user interface functions. */
2283 bond_unixctl_list(struct unixctl_conn
*conn
, const char *args UNUSED
)
2285 struct ds ds
= DS_EMPTY_INITIALIZER
;
2286 const struct bridge
*br
;
2288 ds_put_cstr(&ds
, "bridge\tbond\tslaves\n");
2290 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
2293 for (i
= 0; i
< br
->n_ports
; i
++) {
2294 const struct port
*port
= br
->ports
[i
];
2295 if (port
->n_ifaces
> 1) {
2298 ds_put_format(&ds
, "%s\t%s\t", br
->name
, port
->name
);
2299 for (j
= 0; j
< port
->n_ifaces
; j
++) {
2300 const struct iface
*iface
= port
->ifaces
[j
];
2302 ds_put_cstr(&ds
, ", ");
2304 ds_put_cstr(&ds
, iface
->name
);
2306 ds_put_char(&ds
, '\n');
2310 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));
2314 static struct port
*
2315 bond_find(const char *name
)
2317 const struct bridge
*br
;
2319 LIST_FOR_EACH (br
, struct bridge
, node
, &all_bridges
) {
2322 for (i
= 0; i
< br
->n_ports
; i
++) {
2323 struct port
*port
= br
->ports
[i
];
2324 if (!strcmp(port
->name
, name
) && port
->n_ifaces
> 1) {
2333 bond_unixctl_show(struct unixctl_conn
*conn
, const char *args
)
2335 struct ds ds
= DS_EMPTY_INITIALIZER
;
2336 const struct port
*port
;
2339 port
= bond_find(args
);
2341 unixctl_command_reply(conn
, 501, "no such bond");
2345 ds_put_format(&ds
, "updelay: %d ms\n", port
->updelay
);
2346 ds_put_format(&ds
, "downdelay: %d ms\n", port
->downdelay
);
2347 ds_put_format(&ds
, "next rebalance: %lld ms\n",
2348 port
->bridge
->bond_next_rebalance
- time_msec());
2349 for (j
= 0; j
< port
->n_ifaces
; j
++) {
2350 const struct iface
*iface
= port
->ifaces
[j
];
2351 struct bond_entry
*be
;
2354 ds_put_format(&ds
, "slave %s: %s\n",
2355 iface
->name
, iface
->enabled
? "enabled" : "disabled");
2356 if (j
== port
->active_iface
) {
2357 ds_put_cstr(&ds
, "\tactive slave\n");
2359 if (iface
->delay_expires
!= LLONG_MAX
) {
2360 ds_put_format(&ds
, "\t%s expires in %lld ms\n",
2361 iface
->enabled
? "downdelay" : "updelay",
2362 iface
->delay_expires
- time_msec());
2366 for (be
= port
->bond_hash
; be
<= &port
->bond_hash
[BOND_MASK
]; be
++) {
2367 int hash
= be
- port
->bond_hash
;
2368 struct mac_entry
*me
;
2370 if (be
->iface_idx
!= j
) {
2374 ds_put_format(&ds
, "\thash %d: %lld kB load\n",
2375 hash
, be
->tx_bytes
/ 1024);
2378 if (!port
->bridge
->ml
) {
2382 LIST_FOR_EACH (me
, struct mac_entry
, lru_node
,
2383 &port
->bridge
->ml
->lrus
) {
2386 if (bond_hash(me
->mac
) == hash
2387 && me
->port
!= port
->port_idx
2388 && choose_output_iface(port
, me
->mac
, &dp_ifidx
, &tags
)
2389 && dp_ifidx
== iface
->dp_ifidx
)
2391 ds_put_format(&ds
, "\t\t"ETH_ADDR_FMT
"\n",
2392 ETH_ADDR_ARGS(me
->mac
));
2397 unixctl_command_reply(conn
, 200, ds_cstr(&ds
));
2402 bond_unixctl_migrate(struct unixctl_conn
*conn
, const char *args_
)
2404 char *args
= (char *) args_
;
2405 char *save_ptr
= NULL
;
2406 char *bond_s
, *hash_s
, *slave_s
;
2407 uint8_t mac
[ETH_ADDR_LEN
];
2409 struct iface
*iface
;
2410 struct bond_entry
*entry
;
2413 bond_s
= strtok_r(args
, " ", &save_ptr
);
2414 hash_s
= strtok_r(NULL
, " ", &save_ptr
);
2415 slave_s
= strtok_r(NULL
, " ", &save_ptr
);
2417 unixctl_command_reply(conn
, 501,
2418 "usage: bond/migrate BOND HASH SLAVE");
2422 port
= bond_find(bond_s
);
2424 unixctl_command_reply(conn
, 501, "no such bond");
2428 if (sscanf(hash_s
, "%"SCNx8
":%"SCNx8
":%"SCNx8
":%"SCNx8
":%"SCNx8
":%"SCNx8
,
2429 &mac
[0], &mac
[1], &mac
[2], &mac
[3], &mac
[4], &mac
[5]) == 6) {
2430 hash
= bond_hash(mac
);
2431 } else if (strspn(hash_s
, "0123456789") == strlen(hash_s
)) {
2432 hash
= atoi(hash_s
) & BOND_MASK
;
2434 unixctl_command_reply(conn
, 501, "bad hash");
2438 iface
= port_lookup_iface(port
, slave_s
);
2440 unixctl_command_reply(conn
, 501, "no such slave");
2444 if (!iface
->enabled
) {
2445 unixctl_command_reply(conn
, 501, "cannot migrate to disabled slave");
2449 entry
= &port
->bond_hash
[hash
];
2450 ofproto_revalidate(port
->bridge
->ofproto
, entry
->iface_tag
);
2451 entry
->iface_idx
= iface
->port_ifidx
;
2452 entry
->iface_tag
= tag_create_random();
2453 unixctl_command_reply(conn
, 200, "migrated");
2457 bond_unixctl_set_active_slave(struct unixctl_conn
*conn
, const char *args_
)
2459 char *args
= (char *) args_
;
2460 char *save_ptr
= NULL
;
2461 char *bond_s
, *slave_s
;
2463 struct iface
*iface
;
2465 bond_s
= strtok_r(args
, " ", &save_ptr
);
2466 slave_s
= strtok_r(NULL
, " ", &save_ptr
);
2468 unixctl_command_reply(conn
, 501,
2469 "usage: bond/set-active-slave BOND SLAVE");
2473 port
= bond_find(bond_s
);
2475 unixctl_command_reply(conn
, 501, "no such bond");
2479 iface
= port_lookup_iface(port
, slave_s
);
2481 unixctl_command_reply(conn
, 501, "no such slave");
2485 if (!iface
->enabled
) {
2486 unixctl_command_reply(conn
, 501, "cannot make disabled slave active");
2490 if (port
->active_iface
!= iface
->port_ifidx
) {
2491 ofproto_revalidate(port
->bridge
->ofproto
, port
->active_iface_tag
);
2492 port
->active_iface
= iface
->port_ifidx
;
2493 port
->active_iface_tag
= tag_create_random();
2494 VLOG_INFO("port %s: active interface is now %s",
2495 port
->name
, iface
->name
);
2496 bond_send_learning_packets(port
);
2497 unixctl_command_reply(conn
, 200, "done");
2499 unixctl_command_reply(conn
, 200, "no change");
2504 enable_slave(struct unixctl_conn
*conn
, const char *args_
, bool enable
)
2506 char *args
= (char *) args_
;
2507 char *save_ptr
= NULL
;
2508 char *bond_s
, *slave_s
;
2510 struct iface
*iface
;
2512 bond_s
= strtok_r(args
, " ", &save_ptr
);
2513 slave_s
= strtok_r(NULL
, " ", &save_ptr
);
2515 unixctl_command_reply(conn
, 501,
2516 "usage: bond/enable/disable-slave BOND SLAVE");
2520 port
= bond_find(bond_s
);
2522 unixctl_command_reply(conn
, 501, "no such bond");
2526 iface
= port_lookup_iface(port
, slave_s
);
2528 unixctl_command_reply(conn
, 501, "no such slave");
2532 bond_enable_slave(iface
, enable
);
2533 unixctl_command_reply(conn
, 501, enable
? "enabled" : "disabled");
2537 bond_unixctl_enable_slave(struct unixctl_conn
*conn
, const char *args
)
2539 enable_slave(conn
, args
, true);
2543 bond_unixctl_disable_slave(struct unixctl_conn
*conn
, const char *args
)
2545 enable_slave(conn
, args
, false);
2551 unixctl_command_register("bond/list", bond_unixctl_list
);
2552 unixctl_command_register("bond/show", bond_unixctl_show
);
2553 unixctl_command_register("bond/migrate", bond_unixctl_migrate
);
2554 unixctl_command_register("bond/set-active-slave",
2555 bond_unixctl_set_active_slave
);
2556 unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave
);
2557 unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave
);
2560 /* Port functions. */
2563 port_create(struct bridge
*br
, const char *name
)
2567 port
= xcalloc(1, sizeof *port
);
2569 port
->port_idx
= br
->n_ports
;
2571 port
->trunks
= NULL
;
2572 port
->name
= xstrdup(name
);
2573 port
->active_iface
= -1;
2574 port
->stp_state
= STP_DISABLED
;
2575 port
->stp_state_tag
= 0;
2577 if (br
->n_ports
>= br
->allocated_ports
) {
2578 br
->ports
= x2nrealloc(br
->ports
, &br
->allocated_ports
,
2581 br
->ports
[br
->n_ports
++] = port
;
2583 VLOG_INFO("created port %s on bridge %s", port
->name
, br
->name
);
2588 port_reconfigure(struct port
*port
)
2590 bool bonded
= cfg_has_section("bonding.%s", port
->name
);
2591 struct svec old_ifaces
, new_ifaces
;
2592 unsigned long *trunks
;
2596 /* Collect old and new interfaces. */
2597 svec_init(&old_ifaces
);
2598 svec_init(&new_ifaces
);
2599 for (i
= 0; i
< port
->n_ifaces
; i
++) {
2600 svec_add(&old_ifaces
, port
->ifaces
[i
]->name
);
2602 svec_sort(&old_ifaces
);
2604 cfg_get_all_keys(&new_ifaces
, "bonding.%s.slave", port
->name
);
2605 if (!new_ifaces
.n
) {
2606 VLOG_ERR("port %s: no interfaces specified for bonded port",
2608 } else if (new_ifaces
.n
== 1) {
2609 VLOG_WARN("port %s: only 1 interface specified for bonded port",
2613 port
->updelay
= cfg_get_int(0, "bonding.%s.updelay", port
->name
);
2614 if (port
->updelay
< 0) {
2617 port
->downdelay
= cfg_get_int(0, "bonding.%s.downdelay", port
->name
);
2618 if (port
->downdelay
< 0) {
2619 port
->downdelay
= 0;
2622 svec_init(&new_ifaces
);
2623 svec_add(&new_ifaces
, port
->name
);
2626 /* Get rid of deleted interfaces and add new interfaces. */
2627 for (i
= 0; i
< port
->n_ifaces
; i
++) {
2628 struct iface
*iface
= port
->ifaces
[i
];
2629 if (!svec_contains(&new_ifaces
, iface
->name
)) {
2630 iface_destroy(iface
);
2635 for (i
= 0; i
< new_ifaces
.n
; i
++) {
2636 const char *name
= new_ifaces
.names
[i
];
2637 if (!svec_contains(&old_ifaces
, name
)) {
2638 iface_create(port
, name
);
2644 if (cfg_has("vlan.%s.tag", port
->name
)) {
2646 vlan
= cfg_get_vlan(0, "vlan.%s.tag", port
->name
);
2647 if (vlan
>= 0 && vlan
<= 4095) {
2648 VLOG_DBG("port %s: assigning VLAN tag %d", port
->name
, vlan
);
2651 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
2652 * they even work as-is. But they have not been tested. */
2653 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
2657 if (port
->vlan
!= vlan
) {
2659 bridge_flush(port
->bridge
);
2662 /* Get trunked VLANs. */
2665 size_t n_trunks
, n_errors
;
2668 trunks
= bitmap_allocate(4096);
2669 n_trunks
= cfg_count("vlan.%s.trunks", port
->name
);
2671 for (i
= 0; i
< n_trunks
; i
++) {
2672 int trunk
= cfg_get_vlan(i
, "vlan.%s.trunks", port
->name
);
2674 bitmap_set1(trunks
, trunk
);
2680 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
2681 port
->name
, n_trunks
);
2683 if (n_errors
== n_trunks
) {
2685 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
2688 bitmap_set_multiple(trunks
, 0, 4096, 1);
2691 if (cfg_has("vlan.%s.trunks", port
->name
)) {
2692 VLOG_ERR("ignoring vlan.%s.trunks in favor of vlan.%s.vlan",
2693 port
->name
, port
->name
);
2697 ? port
->trunks
!= NULL
2698 : port
->trunks
== NULL
|| !bitmap_equal(trunks
, port
->trunks
, 4096)) {
2699 bridge_flush(port
->bridge
);
2701 bitmap_free(port
->trunks
);
2702 port
->trunks
= trunks
;
2704 svec_destroy(&old_ifaces
);
2705 svec_destroy(&new_ifaces
);
2709 port_destroy(struct port
*port
)
2712 struct bridge
*br
= port
->bridge
;
2716 proc_net_compat_update_vlan(port
->name
, NULL
, 0);
2718 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
2719 struct mirror
*m
= br
->mirrors
[i
];
2720 if (m
&& m
->out_port
== port
) {
2725 while (port
->n_ifaces
> 0) {
2726 iface_destroy(port
->ifaces
[port
->n_ifaces
- 1]);
2729 del
= br
->ports
[port
->port_idx
] = br
->ports
[--br
->n_ports
];
2730 del
->port_idx
= port
->port_idx
;
2733 bitmap_free(port
->trunks
);
2740 static struct port
*
2741 port_from_dp_ifidx(const struct bridge
*br
, uint16_t dp_ifidx
)
2743 struct iface
*iface
= iface_from_dp_ifidx(br
, dp_ifidx
);
2744 return iface
? iface
->port
: NULL
;
2747 static struct port
*
2748 port_lookup(const struct bridge
*br
, const char *name
)
2752 for (i
= 0; i
< br
->n_ports
; i
++) {
2753 struct port
*port
= br
->ports
[i
];
2754 if (!strcmp(port
->name
, name
)) {
2761 static struct iface
*
2762 port_lookup_iface(const struct port
*port
, const char *name
)
2766 for (j
= 0; j
< port
->n_ifaces
; j
++) {
2767 struct iface
*iface
= port
->ifaces
[j
];
2768 if (!strcmp(iface
->name
, name
)) {
2776 port_update_bonding(struct port
*port
)
2778 if (port
->n_ifaces
< 2) {
2779 /* Not a bonded port. */
2780 if (port
->bond_hash
) {
2781 free(port
->bond_hash
);
2782 port
->bond_hash
= NULL
;
2783 proc_net_compat_update_bond(port
->name
, NULL
);
2786 if (!port
->bond_hash
) {
2789 port
->bond_hash
= xcalloc(BOND_MASK
+ 1, sizeof *port
->bond_hash
);
2790 for (i
= 0; i
<= BOND_MASK
; i
++) {
2791 struct bond_entry
*e
= &port
->bond_hash
[i
];
2795 port
->no_ifaces_tag
= tag_create_random();
2796 bond_choose_active_iface(port
);
2798 port_update_bond_compat(port
);
2803 port_update_bond_compat(struct port
*port
)
2805 struct compat_bond bond
;
2808 if (port
->n_ifaces
< 2) {
2813 bond
.updelay
= port
->updelay
;
2814 bond
.downdelay
= port
->downdelay
;
2815 bond
.n_slaves
= port
->n_ifaces
;
2816 bond
.slaves
= xmalloc(port
->n_ifaces
* sizeof *bond
.slaves
);
2817 for (i
= 0; i
< port
->n_ifaces
; i
++) {
2818 struct iface
*iface
= port
->ifaces
[i
];
2819 struct compat_bond_slave
*slave
= &bond
.slaves
[i
];
2820 slave
->name
= iface
->name
;
2821 slave
->up
= ((iface
->enabled
&& iface
->delay_expires
== LLONG_MAX
) ||
2822 (!iface
->enabled
&& iface
->delay_expires
!= LLONG_MAX
));
2826 memcpy(slave
->mac
, iface
->mac
, ETH_ADDR_LEN
);
2828 proc_net_compat_update_bond(port
->name
, &bond
);
2833 port_update_vlan_compat(struct port
*port
)
2835 struct bridge
*br
= port
->bridge
;
2836 char *vlandev_name
= NULL
;
2838 if (port
->vlan
> 0) {
2839 /* Figure out the name that the VLAN device should actually have, if it
2840 * existed. This takes some work because the VLAN device would not
2841 * have port->name in its name; rather, it would have the trunk port's
2842 * name, and 'port' would be attached to a bridge that also had the
2843 * VLAN device one of its ports. So we need to find a trunk port that
2844 * includes port->vlan.
2846 * There might be more than one candidate. This doesn't happen on
2847 * XenServer, so if it happens we just pick the first choice in
2848 * alphabetical order instead of creating multiple VLAN devices. */
2850 for (i
= 0; i
< br
->n_ports
; i
++) {
2851 struct port
*p
= br
->ports
[i
];
2852 if (port_trunks_vlan(p
, port
->vlan
)
2854 && (!vlandev_name
|| strcmp(p
->name
, vlandev_name
) <= 0))
2856 const uint8_t *ea
= p
->ifaces
[0]->mac
;
2857 if (!eth_addr_is_multicast(ea
) &&
2858 !eth_addr_is_reserved(ea
) &&
2859 !eth_addr_is_zero(ea
)) {
2860 vlandev_name
= p
->name
;
2865 proc_net_compat_update_vlan(port
->name
, vlandev_name
, port
->vlan
);
2868 /* Interface functions. */
2871 iface_create(struct port
*port
, const char *name
)
2873 struct iface
*iface
;
2875 iface
= xcalloc(1, sizeof *iface
);
2877 iface
->port_ifidx
= port
->n_ifaces
;
2878 iface
->name
= xstrdup(name
);
2879 iface
->dp_ifidx
= -1;
2880 iface
->tag
= tag_create_random();
2881 iface
->delay_expires
= LLONG_MAX
;
2883 netdev_nodev_get_etheraddr(name
, iface
->mac
);
2884 netdev_nodev_get_carrier(name
, &iface
->enabled
);
2886 if (port
->n_ifaces
>= port
->allocated_ifaces
) {
2887 port
->ifaces
= x2nrealloc(port
->ifaces
, &port
->allocated_ifaces
,
2888 sizeof *port
->ifaces
);
2890 port
->ifaces
[port
->n_ifaces
++] = iface
;
2891 if (port
->n_ifaces
> 1) {
2892 port
->bridge
->has_bonded_ports
= true;
2895 VLOG_DBG("attached network device %s to port %s", iface
->name
, port
->name
);
2897 port_update_bonding(port
);
2898 bridge_flush(port
->bridge
);
2902 iface_destroy(struct iface
*iface
)
2905 struct port
*port
= iface
->port
;
2906 struct bridge
*br
= port
->bridge
;
2907 bool del_active
= port
->active_iface
== iface
->port_ifidx
;
2910 if (iface
->dp_ifidx
>= 0) {
2911 port_array_set(&br
->ifaces
, iface
->dp_ifidx
, NULL
);
2914 del
= port
->ifaces
[iface
->port_ifidx
] = port
->ifaces
[--port
->n_ifaces
];
2915 del
->port_ifidx
= iface
->port_ifidx
;
2921 ofproto_revalidate(port
->bridge
->ofproto
, port
->active_iface_tag
);
2922 bond_choose_active_iface(port
);
2923 bond_send_learning_packets(port
);
2926 port_update_bonding(port
);
2927 bridge_flush(port
->bridge
);
2931 static struct iface
*
2932 iface_lookup(const struct bridge
*br
, const char *name
)
2936 for (i
= 0; i
< br
->n_ports
; i
++) {
2937 struct port
*port
= br
->ports
[i
];
2938 for (j
= 0; j
< port
->n_ifaces
; j
++) {
2939 struct iface
*iface
= port
->ifaces
[j
];
2940 if (!strcmp(iface
->name
, name
)) {
2948 static struct iface
*
2949 iface_from_dp_ifidx(const struct bridge
*br
, uint16_t dp_ifidx
)
2951 return port_array_get(&br
->ifaces
, dp_ifidx
);
2954 /* Port mirroring. */
2957 mirror_reconfigure(struct bridge
*br
)
2959 struct svec old_mirrors
, new_mirrors
;
2962 /* Collect old and new mirrors. */
2963 svec_init(&old_mirrors
);
2964 svec_init(&new_mirrors
);
2965 cfg_get_subsections(&new_mirrors
, "mirror.%s", br
->name
);
2966 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
2967 if (br
->mirrors
[i
]) {
2968 svec_add(&old_mirrors
, br
->mirrors
[i
]->name
);
2972 /* Get rid of deleted mirrors and add new mirrors. */
2973 svec_sort(&old_mirrors
);
2974 assert(svec_is_unique(&old_mirrors
));
2975 svec_sort(&new_mirrors
);
2976 assert(svec_is_unique(&new_mirrors
));
2977 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
2978 struct mirror
*m
= br
->mirrors
[i
];
2979 if (m
&& !svec_contains(&new_mirrors
, m
->name
)) {
2983 for (i
= 0; i
< new_mirrors
.n
; i
++) {
2984 const char *name
= new_mirrors
.names
[i
];
2985 if (!svec_contains(&old_mirrors
, name
)) {
2986 mirror_create(br
, name
);
2989 svec_destroy(&old_mirrors
);
2990 svec_destroy(&new_mirrors
);
2992 /* Reconfigure all mirrors. */
2993 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
2994 if (br
->mirrors
[i
]) {
2995 mirror_reconfigure_one(br
->mirrors
[i
]);
2999 /* Update port reserved status. */
3000 for (i
= 0; i
< br
->n_ports
; i
++) {
3001 br
->ports
[i
]->is_mirror_output_port
= false;
3003 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
3004 struct mirror
*m
= br
->mirrors
[i
];
3005 if (m
&& m
->out_port
) {
3006 m
->out_port
->is_mirror_output_port
= true;
3012 mirror_create(struct bridge
*br
, const char *name
)
3017 for (i
= 0; ; i
++) {
3018 if (i
>= MAX_MIRRORS
) {
3019 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
3020 "cannot create %s", br
->name
, MAX_MIRRORS
, name
);
3023 if (!br
->mirrors
[i
]) {
3028 VLOG_INFO("created port mirror %s on bridge %s", name
, br
->name
);
3031 br
->mirrors
[i
] = m
= xcalloc(1, sizeof *m
);
3034 m
->name
= xstrdup(name
);
3035 svec_init(&m
->src_ports
);
3036 svec_init(&m
->dst_ports
);
3044 mirror_destroy(struct mirror
*m
)
3047 struct bridge
*br
= m
->bridge
;
3050 for (i
= 0; i
< br
->n_ports
; i
++) {
3051 br
->ports
[i
]->src_mirrors
&= ~(MIRROR_MASK_C(1) << m
->idx
);
3052 br
->ports
[i
]->dst_mirrors
&= ~(MIRROR_MASK_C(1) << m
->idx
);
3055 svec_destroy(&m
->src_ports
);
3056 svec_destroy(&m
->dst_ports
);
3059 m
->bridge
->mirrors
[m
->idx
] = NULL
;
3067 prune_ports(struct mirror
*m
, struct svec
*ports
)
3072 svec_sort_unique(ports
);
3075 for (i
= 0; i
< ports
->n
; i
++) {
3076 const char *name
= ports
->names
[i
];
3077 if (port_lookup(m
->bridge
, name
)) {
3078 svec_add(&tmp
, name
);
3080 VLOG_WARN("mirror.%s.%s: cannot match on nonexistent port %s",
3081 m
->bridge
->name
, m
->name
, name
);
3084 svec_swap(ports
, &tmp
);
3089 prune_vlans(struct mirror
*m
, struct svec
*vlan_strings
, int **vlans
)
3093 /* This isn't perfect: it won't combine "0" and "00", and the textual sort
3094 * order won't give us numeric sort order. But that's good enough for what
3095 * we need right now. */
3096 svec_sort_unique(vlan_strings
);
3098 *vlans
= xmalloc(sizeof *vlans
* vlan_strings
->n
);
3100 for (i
= 0; i
< vlan_strings
->n
; i
++) {
3101 const char *name
= vlan_strings
->names
[i
];
3103 if (!str_to_int(name
, 10, &vlan
) || vlan
< 0 || vlan
> 4095) {
3104 VLOG_WARN("mirror.%s.%s.select.vlan: ignoring invalid VLAN %s",
3105 m
->bridge
->name
, m
->name
, name
);
3107 (*vlans
)[n_vlans
++] = vlan
;
3114 vlan_is_mirrored(const struct mirror
*m
, int vlan
)
3118 for (i
= 0; i
< m
->n_vlans
; i
++) {
3119 if (m
->vlans
[i
] == vlan
) {
3127 port_trunks_any_mirrored_vlan(const struct mirror
*m
, const struct port
*p
)
3131 for (i
= 0; i
< m
->n_vlans
; i
++) {
3132 if (port_trunks_vlan(p
, m
->vlans
[i
])) {
3140 mirror_reconfigure_one(struct mirror
*m
)
3142 char *pfx
= xasprintf("mirror.%s.%s", m
->bridge
->name
, m
->name
);
3143 struct svec src_ports
, dst_ports
, ports
;
3144 struct svec vlan_strings
;
3145 mirror_mask_t mirror_bit
;
3146 const char *out_port_name
;
3147 struct port
*out_port
;
3152 bool mirror_all_ports
;
3154 /* Get output port. */
3155 out_port_name
= cfg_get_key(0, "mirror.%s.%s.output.port",
3156 m
->bridge
->name
, m
->name
);
3157 if (out_port_name
) {
3158 out_port
= port_lookup(m
->bridge
, out_port_name
);
3160 VLOG_ERR("%s.output.port: bridge %s does not have a port "
3161 "named %s", pfx
, m
->bridge
->name
, out_port_name
);
3168 if (cfg_has("%s.output.vlan", pfx
)) {
3169 VLOG_ERR("%s.output.port and %s.output.vlan both specified; "
3170 "ignoring %s.output.vlan", pfx
, pfx
, pfx
);
3172 } else if (cfg_has("%s.output.vlan", pfx
)) {
3174 out_vlan
= cfg_get_vlan(0, "%s.output.vlan", pfx
);
3176 VLOG_ERR("%s: neither %s.output.port nor %s.output.vlan specified, "
3177 "but exactly one is required; disabling port mirror %s",
3178 pfx
, pfx
, pfx
, pfx
);
3184 /* Get all the ports, and drop duplicates and ports that don't exist. */
3185 svec_init(&src_ports
);
3186 svec_init(&dst_ports
);
3188 cfg_get_all_keys(&src_ports
, "%s.select.src-port", pfx
);
3189 cfg_get_all_keys(&dst_ports
, "%s.select.dst-port", pfx
);
3190 cfg_get_all_keys(&ports
, "%s.select.port", pfx
);
3191 svec_append(&src_ports
, &ports
);
3192 svec_append(&dst_ports
, &ports
);
3193 svec_destroy(&ports
);
3194 prune_ports(m
, &src_ports
);
3195 prune_ports(m
, &dst_ports
);
3197 /* Get all the vlans, and drop duplicate and invalid vlans. */
3198 svec_init(&vlan_strings
);
3199 cfg_get_all_keys(&vlan_strings
, "%s.select.vlan", pfx
);
3200 n_vlans
= prune_vlans(m
, &vlan_strings
, &vlans
);
3201 svec_destroy(&vlan_strings
);
3203 /* Update mirror data. */
3204 if (!svec_equal(&m
->src_ports
, &src_ports
)
3205 || !svec_equal(&m
->dst_ports
, &dst_ports
)
3206 || m
->n_vlans
!= n_vlans
3207 || memcmp(m
->vlans
, vlans
, sizeof *vlans
* n_vlans
)
3208 || m
->out_port
!= out_port
3209 || m
->out_vlan
!= out_vlan
) {
3210 bridge_flush(m
->bridge
);
3212 svec_swap(&m
->src_ports
, &src_ports
);
3213 svec_swap(&m
->dst_ports
, &dst_ports
);
3216 m
->n_vlans
= n_vlans
;
3217 m
->out_port
= out_port
;
3218 m
->out_vlan
= out_vlan
;
3220 /* If no selection criteria have been given, mirror for all ports. */
3221 mirror_all_ports
= (!m
->src_ports
.n
) && (!m
->dst_ports
.n
) && (!m
->n_vlans
);
3224 mirror_bit
= MIRROR_MASK_C(1) << m
->idx
;
3225 for (i
= 0; i
< m
->bridge
->n_ports
; i
++) {
3226 struct port
*port
= m
->bridge
->ports
[i
];
3228 if (mirror_all_ports
3229 || svec_contains(&m
->src_ports
, port
->name
)
3232 ? port_trunks_any_mirrored_vlan(m
, port
)
3233 : vlan_is_mirrored(m
, port
->vlan
)))) {
3234 port
->src_mirrors
|= mirror_bit
;
3236 port
->src_mirrors
&= ~mirror_bit
;
3239 if (mirror_all_ports
|| svec_contains(&m
->dst_ports
, port
->name
)) {
3240 port
->dst_mirrors
|= mirror_bit
;
3242 port
->dst_mirrors
&= ~mirror_bit
;
3247 svec_destroy(&src_ports
);
3248 svec_destroy(&dst_ports
);
3252 /* Spanning tree protocol. */
3254 static void brstp_update_port_state(struct port
*);
3257 brstp_send_bpdu(struct ofpbuf
*pkt
, int port_no
, void *br_
)
3259 struct bridge
*br
= br_
;
3260 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3261 struct iface
*iface
= iface_from_dp_ifidx(br
, port_no
);
3263 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on unknown port %d",
3265 } else if (eth_addr_is_zero(iface
->mac
)) {
3266 VLOG_WARN_RL(&rl
, "%s: cannot send BPDU on port %d with unknown MAC",
3269 union ofp_action action
;
3270 struct eth_header
*eth
= pkt
->l2
;
3273 memcpy(eth
->eth_src
, iface
->mac
, ETH_ADDR_LEN
);
3275 memset(&action
, 0, sizeof action
);
3276 action
.type
= htons(OFPAT_OUTPUT
);
3277 action
.output
.len
= htons(sizeof action
);
3278 action
.output
.port
= htons(port_no
);
3280 flow_extract(pkt
, ODPP_NONE
, &flow
);
3281 ofproto_send_packet(br
->ofproto
, &flow
, &action
, 1, pkt
);
3287 brstp_reconfigure(struct bridge
*br
)
3291 if (!cfg_get_bool(0, "stp.%s.enabled", br
->name
)) {
3293 stp_destroy(br
->stp
);
3299 uint64_t bridge_address
, bridge_id
;
3300 int bridge_priority
;
3302 bridge_address
= cfg_get_mac(0, "stp.%s.address", br
->name
);
3303 if (!bridge_address
) {
3305 bridge_address
= (stp_get_bridge_id(br
->stp
)
3306 & ((UINT64_C(1) << 48) - 1));
3308 uint8_t mac
[ETH_ADDR_LEN
];
3309 eth_addr_random(mac
);
3310 bridge_address
= eth_addr_to_uint64(mac
);
3314 if (cfg_is_valid(CFG_INT
| CFG_REQUIRED
, "stp.%s.priority",
3316 bridge_priority
= cfg_get_int(0, "stp.%s.priority", br
->name
);
3318 bridge_priority
= STP_DEFAULT_BRIDGE_PRIORITY
;
3321 bridge_id
= bridge_address
| ((uint64_t) bridge_priority
<< 48);
3323 br
->stp
= stp_create(br
->name
, bridge_id
, brstp_send_bpdu
, br
);
3324 br
->stp_last_tick
= time_msec();
3327 if (bridge_id
!= stp_get_bridge_id(br
->stp
)) {
3328 stp_set_bridge_id(br
->stp
, bridge_id
);
3333 for (i
= 0; i
< br
->n_ports
; i
++) {
3334 struct port
*p
= br
->ports
[i
];
3336 struct stp_port
*sp
;
3337 int path_cost
, priority
;
3343 dp_ifidx
= p
->ifaces
[0]->dp_ifidx
;
3344 if (dp_ifidx
< 0 || dp_ifidx
>= STP_MAX_PORTS
) {
3348 sp
= stp_get_port(br
->stp
, dp_ifidx
);
3349 enable
= (!cfg_is_valid(CFG_BOOL
| CFG_REQUIRED
,
3350 "stp.%s.port.%s.enabled",
3352 || cfg_get_bool(0, "stp.%s.port.%s.enabled",
3353 br
->name
, p
->name
));
3354 if (p
->is_mirror_output_port
) {
3357 if (enable
!= (stp_port_get_state(sp
) != STP_DISABLED
)) {
3358 bridge_flush(br
); /* Might not be necessary. */
3360 stp_port_enable(sp
);
3362 stp_port_disable(sp
);
3366 path_cost
= cfg_get_int(0, "stp.%s.port.%s.path-cost",
3368 stp_port_set_path_cost(sp
, path_cost
? path_cost
: 19 /* XXX */);
3370 priority
= (cfg_is_valid(CFG_INT
| CFG_REQUIRED
,
3371 "stp.%s.port.%s.priority",
3373 ? cfg_get_int(0, "stp.%s.port.%s.priority",
3375 : STP_DEFAULT_PORT_PRIORITY
);
3376 stp_port_set_priority(sp
, priority
);
3379 brstp_adjust_timers(br
);
3381 for (i
= 0; i
< br
->n_ports
; i
++) {
3382 brstp_update_port_state(br
->ports
[i
]);
3387 brstp_update_port_state(struct port
*p
)
3389 struct bridge
*br
= p
->bridge
;
3390 enum stp_state state
;
3392 /* Figure out new state. */
3393 state
= STP_DISABLED
;
3394 if (br
->stp
&& p
->n_ifaces
> 0) {
3395 int dp_ifidx
= p
->ifaces
[0]->dp_ifidx
;
3396 if (dp_ifidx
>= 0 && dp_ifidx
< STP_MAX_PORTS
) {
3397 state
= stp_port_get_state(stp_get_port(br
->stp
, dp_ifidx
));
3402 if (p
->stp_state
!= state
) {
3403 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
3404 VLOG_INFO_RL(&rl
, "port %s: STP state changed from %s to %s",
3405 p
->name
, stp_state_name(p
->stp_state
),
3406 stp_state_name(state
));
3407 if (p
->stp_state
== STP_DISABLED
) {
3410 ofproto_revalidate(p
->bridge
->ofproto
, p
->stp_state_tag
);
3412 p
->stp_state
= state
;
3413 p
->stp_state_tag
= (p
->stp_state
== STP_DISABLED
? 0
3414 : tag_create_random());
3419 brstp_adjust_timers(struct bridge
*br
)
3421 int hello_time
= cfg_get_int(0, "stp.%s.hello-time", br
->name
);
3422 int max_age
= cfg_get_int(0, "stp.%s.max-age", br
->name
);
3423 int forward_delay
= cfg_get_int(0, "stp.%s.forward-delay", br
->name
);
3425 stp_set_hello_time(br
->stp
, hello_time
? hello_time
: 2000);
3426 stp_set_max_age(br
->stp
, max_age
? max_age
: 20000);
3427 stp_set_forward_delay(br
->stp
, forward_delay
? forward_delay
: 15000);
3431 brstp_run(struct bridge
*br
)
3434 long long int now
= time_msec();
3435 long long int elapsed
= now
- br
->stp_last_tick
;
3436 struct stp_port
*sp
;
3439 stp_tick(br
->stp
, MIN(INT_MAX
, elapsed
));
3440 br
->stp_last_tick
= now
;
3442 while (stp_get_changed_port(br
->stp
, &sp
)) {
3443 struct port
*p
= port_from_dp_ifidx(br
, stp_port_no(sp
));
3445 brstp_update_port_state(p
);
3452 brstp_wait(struct bridge
*br
)
3455 poll_timer_wait(1000);