]> git.proxmox.com Git - mirror_ovs.git/blob - vswitchd/bridge.c
vswitch: Add ovs-appctl commands for debugging and managing bonds.
[mirror_ovs.git] / vswitchd / bridge.c
1 /* Copyright (c) 2008, 2009 Nicira Networks
2 *
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.
7 *
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.
12 *
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/>.
15 *
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.
25 *
26 */
27
28 #include <config.h>
29 #include "bridge.h"
30 #include <assert.h>
31 #include <errno.h>
32 #include <arpa/inet.h>
33 #include <ctype.h>
34 #include <inttypes.h>
35 #include <net/if.h>
36 #include <openflow/openflow.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #include <strings.h>
40 #include <sys/stat.h>
41 #include <sys/socket.h>
42 #include <unistd.h>
43 #include "bitmap.h"
44 #include "cfg.h"
45 #include "coverage.h"
46 #include "dirs.h"
47 #include "dpif.h"
48 #include "dynamic-string.h"
49 #include "flow.h"
50 #include "hash.h"
51 #include "list.h"
52 #include "mac-learning.h"
53 #include "netdev.h"
54 #include "odp-util.h"
55 #include "ofp-print.h"
56 #include "ofpbuf.h"
57 #include "packets.h"
58 #include "poll-loop.h"
59 #include "port-array.h"
60 #include "proc-net-compat.h"
61 #include "process.h"
62 #include "secchan/ofproto.h"
63 #include "socket-util.h"
64 #include "stp.h"
65 #include "svec.h"
66 #include "timeval.h"
67 #include "util.h"
68 #include "unixctl.h"
69 #include "vconn.h"
70 #include "vconn-ssl.h"
71 #include "xenserver.h"
72 #include "xtoxll.h"
73
74 #define THIS_MODULE VLM_bridge
75 #include "vlog.h"
76
77 struct dst {
78 uint16_t vlan;
79 uint16_t dp_ifidx;
80 };
81
82 extern uint64_t mgmt_id;
83
84 struct iface {
85 struct port *port; /* Containing port. */
86 size_t port_ifidx; /* Index within containing port. */
87
88 char *name; /* Host network device name. */
89 int dp_ifidx; /* Index within kernel datapath. */
90
91 uint8_t mac[ETH_ADDR_LEN]; /* Ethernet address (all zeros if unknowns). */
92
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. */
96 };
97
98 #define BOND_MASK 0xff
99 struct bond_entry {
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. */
103 };
104
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);
109 struct mirror {
110 struct bridge *bridge;
111 size_t idx;
112 char *name;
113
114 /* Selection criteria. */
115 struct svec src_ports;
116 struct svec dst_ports;
117 int *vlans;
118 size_t n_vlans;
119
120 /* Output. */
121 struct port *out_port;
122 int out_vlan;
123 };
124
125 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
126 struct port {
127 struct bridge *bridge;
128 size_t port_idx;
129 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
130 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1. */
131 char *name;
132
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;
137
138 /* Bonding info. */
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. */
144
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? */
149
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. */
153 };
154
155 #define DP_MAX_PORTS 255
156 struct bridge {
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. */
162
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. */
167
168 /* OpenFlow switch processing. */
169 struct ofproto *ofproto; /* OpenFlow switch. */
170
171 /* Kernel datapath information. */
172 struct dpif dpif; /* Kernel datapath. */
173 struct port_array ifaces; /* Indexed by kernel datapath port number. */
174
175 /* Bridge ports. */
176 struct port **ports;
177 size_t n_ports, allocated_ports;
178
179 /* Bonding. */
180 bool has_bonded_ports;
181 long long int bond_next_rebalance;
182
183 /* Flow tracking. */
184 bool flush;
185
186 /* Flow statistics gathering. */
187 time_t next_stats_request;
188
189 /* Port mirroring. */
190 struct mirror *mirrors[MAX_MIRRORS];
191
192 /* Spanning tree. */
193 struct stp *stp;
194 long long int stp_last_tick;
195 };
196
197 /* List of all bridges. */
198 static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
199
200 /* Maximum number of datapaths. */
201 enum { DP_MAX = 256 };
202
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);
219
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 *);
225
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 *,
232 uint16_t dp_ifidx);
233 static void port_update_bond_compat(struct port *);
234 static void port_update_vlan_compat(struct port *);
235
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);
241
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 *);
246
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 *,
251 uint16_t dp_ifidx);
252
253 /* Hooks into ofproto processing. */
254 static struct ofhooks bridge_ofhooks;
255 \f
256 /* Public functions. */
257
258 /* Adds the name of each interface used by a bridge, including local and
259 * internal ports, to 'svec'. */
260 void
261 bridge_get_ifaces(struct svec *svec)
262 {
263 struct bridge *br, *next;
264 size_t i, j;
265
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];
269
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));
275 } else {
276 if (iface->dp_ifidx != ODPP_LOCAL) {
277 svec_add(svec, iface->name);
278 }
279 }
280 }
281 }
282 }
283 }
284
285 /* The caller must already have called cfg_read(). */
286 void
287 bridge_init(void)
288 {
289 int retval;
290 int i;
291
292 bond_init();
293
294 for (i = 0; i < DP_MAX; i++) {
295 struct dpif dpif;
296 char devname[16];
297
298 sprintf(devname, "dp%d", i);
299 retval = dpif_open(devname, &dpif);
300 if (!retval) {
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)) {
304 dpif_delete(&dpif);
305 }
306 dpif_close(&dpif);
307 } else if (retval != ENODEV) {
308 VLOG_ERR("failed to delete datapath dp%d: %s",
309 i, strerror(retval));
310 }
311 }
312
313 bridge_reconfigure();
314 }
315
316 #ifdef HAVE_OPENSSL
317 static bool
318 config_string_change(const char *key, char **valuep)
319 {
320 const char *value = cfg_get_string(0, "%s", key);
321 if (value && (!*valuep || strcmp(value, *valuep))) {
322 free(*valuep);
323 *valuep = xstrdup(value);
324 return true;
325 } else {
326 return false;
327 }
328 }
329
330 static void
331 bridge_configure_ssl(void)
332 {
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;
338
339 if (config_string_change("ssl.private-key", &private_key_file)) {
340 vconn_ssl_set_private_key_file(private_key_file);
341 }
342
343 if (config_string_change("ssl.certificate", &certificate_file)) {
344 vconn_ssl_set_certificate_file(certificate_file);
345 }
346
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"));
350 }
351 }
352 #endif
353
354 void
355 bridge_reconfigure(void)
356 {
357 struct svec old_br, new_br, raw_new_br;
358 struct bridge *br, *next;
359 size_t i, j;
360
361 COVERAGE_INC(bridge_reconfigure);
362
363 /* Collect old bridges. */
364 svec_init(&old_br);
365 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
366 svec_add(&old_br, br->name);
367 }
368
369 /* Collect new bridges. */
370 svec_init(&raw_new_br);
371 cfg_get_subsections(&raw_new_br, "bridge");
372 svec_init(&new_br);
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);
379 } else {
380 svec_add(&new_br, name);
381 }
382 }
383 svec_destroy(&raw_new_br);
384
385 /* Get rid of deleted bridges and add new bridges. */
386 svec_sort(&old_br);
387 svec_sort(&new_br);
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)) {
392 bridge_destroy(br);
393 }
394 }
395 for (i = 0; i < new_br.n; i++) {
396 const char *name = new_br.names[i];
397 if (!svec_contains(&old_br, name)) {
398 bridge_create(name);
399 }
400 }
401 svec_destroy(&old_br);
402 svec_destroy(&new_br);
403
404 #ifdef HAVE_OPENSSL
405 /* Configure SSL. */
406 bridge_configure_ssl();
407 #endif
408
409 /* Reconfigure all bridges. */
410 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
411 bridge_reconfigure_one(br);
412 }
413
414 /* Add and delete ports on all datapaths.
415 *
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;
421 size_t n_dpif_ports;
422 struct svec want_ifaces;
423
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);
431 if (retval) {
432 VLOG_ERR("failed to remove %s interface from dp%u: %s",
433 p->devname, dpif_id(&br->dpif), strerror(retval));
434 }
435 }
436 }
437 svec_destroy(&want_ifaces);
438 free(dpif_ports);
439 }
440 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
441 struct odp_port *dpif_ports;
442 size_t n_dpif_ports;
443 struct svec cur_ifaces, want_ifaces, add_ifaces;
444 int next_port_no;
445
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);
450 }
451 free(dpif_ports);
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);
455
456 next_port_no = 1;
457 for (i = 0; i < add_ifaces.n; i++) {
458 const char *if_name = add_ifaces.names[i];
459 for (;;) {
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",
466 dpif_id(&br->dpif));
467 goto out;
468 }
469 if (error) {
470 VLOG_ERR("failed to add %s interface to dp%u: %s",
471 if_name, dpif_id(&br->dpif), strerror(error));
472 }
473 break;
474 }
475 }
476 }
477 out:
478 svec_destroy(&cur_ifaces);
479 svec_destroy(&want_ifaces);
480 svec_destroy(&add_ifaces);
481 }
482 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
483 uint8_t ea[8];
484 uint64_t dpid;
485 struct iface *local_iface = NULL;
486 const char *devname;
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;
491
492 bridge_fetch_dp_ifaces(br);
493 for (i = 0; i < br->n_ports; ) {
494 struct port *port = br->ports[i];
495
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);
502 } else {
503 if (iface->dp_ifidx == ODPP_LOCAL) {
504 local_iface = iface;
505 }
506 VLOG_DBG("dp%u has interface %s on port %d",
507 dpif_id(&br->dpif), iface->name, iface->dp_ifidx);
508 j++;
509 }
510 }
511 if (!port->n_ifaces) {
512 VLOG_ERR("%s port has no interfaces, dropping", port->name);
513 port_destroy(port);
514 continue;
515 }
516 i++;
517 }
518
519 /* Pick local port hardware address, datapath ID. */
520 bridge_pick_local_hw_addr(br, ea, &devname);
521 if (local_iface) {
522 int error = netdev_nodev_set_etheraddr(local_iface->name, ea);
523 if (error) {
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));
528 }
529 }
530
531 dpid = bridge_pick_datapath_id(br, ea, devname);
532 ofproto_set_datapath_id(br->ofproto, dpid);
533
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",
537 br->name);
538 }
539 if (cfg_has("netflow.%s.engine-id", br->name)) {
540 engine_id = cfg_get_int(0, "netflow.%s.engine-id", br->name);
541 }
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",
544 br->name);
545 }
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",
549 br->name);
550 }
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",
554 br->name);
555 }
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",
561 br->name);
562 }
563
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);
574 }
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);
579 }
580 }
581 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
582 brstp_reconfigure(br);
583 }
584 }
585
586 static void
587 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
588 const char **devname)
589 {
590 uint64_t requested_ea;
591 size_t i, j;
592 int error;
593
594 *devname = NULL;
595
596 /* Did the user request a particular MAC? */
597 requested_ea = cfg_get_mac(0, "bridge.%s.mac", br->name);
598 if (requested_ea) {
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);
605 } else {
606 return;
607 }
608 }
609
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) {
617 continue;
618 }
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)) {
624 continue;
625 }
626 error = netdev_nodev_get_etheraddr(iface->name, iface_ea);
627 if (!error) {
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;
634 }
635 } else {
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));
639 }
640 }
641 }
642 if (eth_addr_is_multicast(ea) || eth_addr_is_vif(ea)) {
643 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
644 *devname = NULL;
645 VLOG_WARN("bridge %s: using default bridge Ethernet "
646 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
647 } else {
648 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
649 br->name, ETH_ADDR_ARGS(ea));
650 }
651 }
652
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. */
658 static uint64_t
659 bridge_pick_datapath_id(struct bridge *br,
660 const uint8_t bridge_ea[ETH_ADDR_LEN],
661 const char *devname)
662 {
663 /*
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
669 * ID.
670 *
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
673 * "sticks".
674 */
675 uint64_t dpid;
676
677 dpid = cfg_get_dpid(0, "bridge.%s.datapath-id", br->name);
678 if (dpid) {
679 return dpid;
680 }
681
682 if (devname) {
683 int vlan;
684 if (!netdev_get_vlan_vid(devname, &vlan)) {
685 /*
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.
690 *
691 * Handle this case by hashing the physical network device MAC
692 * along with the VLAN identifier.
693 */
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);
699 } else {
700 /*
701 * Assume that this bridge's MAC address is unique, since it
702 * doesn't fit any of the cases we handle specially.
703 */
704 }
705 } else {
706 /*
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.
710 *
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.
716 *
717 * When the host is not a XenServer, we punt by using a random MAC
718 * address on each run.
719 */
720 const char *host_uuid = xenserver_get_host_uuid();
721 if (host_uuid) {
722 char *combined = xasprintf("%s,%s", host_uuid, br->name);
723 dpid = dpid_from_hash(combined, strlen(combined));
724 free(combined);
725 return dpid;
726 }
727 }
728
729 return eth_addr_to_uint64(bridge_ea);
730 }
731
732 static uint64_t
733 dpid_from_hash(const void *data, size_t n)
734 {
735 uint8_t hash[SHA1HashSize];
736
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);
741 }
742
743 int
744 bridge_run(void)
745 {
746 struct bridge *br, *next;
747 int retval;
748
749 retval = 0;
750 LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
751 int error = bridge_run_one(br);
752 if (error) {
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);
756 if (!retval) {
757 retval = error;
758 }
759 }
760 }
761 return retval;
762 }
763
764 void
765 bridge_wait(void)
766 {
767 struct bridge *br;
768
769 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
770 ofproto_wait(br->ofproto);
771 if (br->controller) {
772 continue;
773 }
774
775 if (br->ml) {
776 mac_learning_wait(br->ml);
777 }
778 bond_wait(br);
779 brstp_wait(br);
780 }
781 }
782
783 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
784 * configuration changes. */
785 static void
786 bridge_flush(struct bridge *br)
787 {
788 COVERAGE_INC(bridge_flush);
789 br->flush = true;
790 if (br->ml) {
791 mac_learning_flush(br->ml);
792 }
793 }
794 \f
795 /* Bridge reconfiguration functions. */
796
797 static struct bridge *
798 bridge_create(const char *name)
799 {
800 struct bridge *br;
801 int error;
802
803 assert(!bridge_lookup(name));
804 br = xcalloc(1, sizeof *br);
805
806 error = dpif_create(name, &br->dpif);
807 if (error == EEXIST) {
808 error = dpif_open(name, &br->dpif);
809 if (error) {
810 VLOG_ERR("datapath %s already exists but cannot be opened: %s",
811 name, strerror(error));
812 free(br);
813 return NULL;
814 }
815 dpif_flow_flush(&br->dpif);
816 } else if (error) {
817 VLOG_ERR("failed to create datapath %s: %s", name, strerror(error));
818 free(br);
819 return NULL;
820 }
821
822 error = ofproto_create(name, &bridge_ofhooks, br, &br->ofproto);
823 if (error) {
824 VLOG_ERR("failed to create switch %s: %s", name, strerror(error));
825 dpif_delete(&br->dpif);
826 dpif_close(&br->dpif);
827 free(br);
828 return NULL;
829 }
830
831 br->name = xstrdup(name);
832 br->ml = mac_learning_create();
833 br->sent_config_request = false;
834 eth_addr_random(br->default_ea);
835
836 port_array_init(&br->ifaces);
837
838 br->flush = false;
839 br->bond_next_rebalance = time_msec() + 10000;
840
841 list_push_back(&all_bridges, &br->node);
842
843 VLOG_INFO("created bridge %s on dp%u", br->name, dpif_id(&br->dpif));
844
845 return br;
846 }
847
848 static void
849 bridge_destroy(struct bridge *br)
850 {
851 if (br) {
852 int error;
853
854 while (br->n_ports > 0) {
855 port_destroy(br->ports[br->n_ports - 1]);
856 }
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));
862 }
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);
868 free(br->ports);
869 free(br->name);
870 free(br);
871 }
872 }
873
874 static struct bridge *
875 bridge_lookup(const char *name)
876 {
877 struct bridge *br;
878
879 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
880 if (!strcmp(br->name, name)) {
881 return br;
882 }
883 }
884 return NULL;
885 }
886
887 bool
888 bridge_exists(const char *name)
889 {
890 return bridge_lookup(name) ? true : false;
891 }
892
893 uint64_t
894 bridge_get_datapathid(const char *name)
895 {
896 struct bridge *br = bridge_lookup(name);
897 return br ? ofproto_get_datapath_id(br->ofproto) : 0;
898 }
899
900 static int
901 bridge_run_one(struct bridge *br)
902 {
903 int error;
904
905 error = ofproto_run1(br->ofproto);
906 if (error) {
907 return error;
908 }
909
910 if (br->ml) {
911 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
912 }
913 bond_run(br);
914 brstp_run(br);
915
916 error = ofproto_run2(br->ofproto, br->flush);
917 br->flush = false;
918
919 return error;
920 }
921
922 static const char *
923 bridge_get_controller(const struct bridge *br)
924 {
925 const char *controller;
926
927 controller = cfg_get_string(0, "bridge.%s.controller", br->name);
928 if (!controller) {
929 controller = cfg_get_string(0, "mgmt.controller");
930 }
931 return controller && controller[0] ? controller : NULL;
932 }
933
934 static void
935 bridge_reconfigure_one(struct bridge *br)
936 {
937 struct svec old_ports, new_ports, ifaces;
938 struct svec listeners, old_listeners;
939 struct svec snoops, old_snoops;
940 size_t i, j;
941
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);
946 }
947 svec_sort(&old_ports);
948 assert(svec_is_unique(&old_ports));
949
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);
957 }
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);
962 }
963
964 ofproto_set_mgmt_id(br->ofproto, mgmt_id);
965
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)) {
970 port_destroy(port);
971 } else {
972 i++;
973 }
974 }
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);
979 }
980 }
981 svec_destroy(&old_ports);
982 svec_destroy(&new_ports);
983
984 /* Reconfigure all ports. */
985 for (i = 0; i < br->n_ports; i++) {
986 port_reconfigure(br->ports[i]);
987 }
988
989 /* Check and delete duplicate interfaces. */
990 svec_init(&ifaces);
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, "
997 "removing from %s",
998 br->name, iface->name, port->name);
999 iface_destroy(iface);
1000 } else {
1001 svec_add(&ifaces, iface->name);
1002 svec_sort(&ifaces);
1003 j++;
1004 }
1005 }
1006 if (!port->n_ifaces) {
1007 VLOG_ERR("%s port has no interfaces, dropping", port->name);
1008 port_destroy(port);
1009 } else {
1010 i++;
1011 }
1012 }
1013 svec_destroy(&ifaces);
1014
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?) */
1018
1019 /* Configure OpenFlow management listeners. */
1020 svec_init(&listeners);
1021 cfg_get_all_strings(&listeners, "bridge.%s.openflow.listeners", br->name);
1022 if (!listeners.n) {
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);
1027 }
1028 svec_sort_unique(&listeners);
1029
1030 svec_init(&old_listeners);
1031 ofproto_get_listeners(br->ofproto, &old_listeners);
1032 svec_sort_unique(&old_listeners);
1033
1034 if (!svec_equal(&listeners, &old_listeners)) {
1035 ofproto_set_listeners(br->ofproto, &listeners);
1036 }
1037 svec_destroy(&listeners);
1038 svec_destroy(&old_listeners);
1039
1040 /* Configure OpenFlow controller connection snooping. */
1041 svec_init(&snoops);
1042 cfg_get_all_strings(&snoops, "bridge.%s.openflow.snoops", br->name);
1043 if (!snoops.n) {
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);
1048 }
1049 svec_sort_unique(&snoops);
1050
1051 svec_init(&old_snoops);
1052 ofproto_get_snoops(br->ofproto, &old_snoops);
1053 svec_sort_unique(&old_snoops);
1054
1055 if (!svec_equal(&snoops, &old_snoops)) {
1056 ofproto_set_snoops(br->ofproto, &snoops);
1057 }
1058 svec_destroy(&snoops);
1059 svec_destroy(&old_snoops);
1060
1061 mirror_reconfigure(br);
1062 }
1063
1064 static void
1065 bridge_reconfigure_controller(struct bridge *br)
1066 {
1067 char *pfx = xasprintf("bridge.%s.controller", br->name);
1068 const char *controller;
1069
1070 controller = bridge_get_controller(br);
1071 if ((br->controller != NULL) != (controller != NULL)) {
1072 ofproto_flush_flows(br->ofproto);
1073 }
1074 free(br->controller);
1075 br->controller = controller ? xstrdup(controller) : NULL;
1076
1077 if (controller) {
1078 const char *fail_mode;
1079 int max_backoff, probe;
1080 int rate_limit, burst_limit;
1081
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",
1086 pfx));
1087 } else {
1088 struct netdev *netdev;
1089 bool in_band;
1090 int error;
1091
1092 in_band = (!cfg_is_valid(CFG_BOOL | CFG_REQUIRED,
1093 "%s.in-band", pfx)
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);
1097
1098 error = netdev_open(br->name, NETDEV_ETH_TYPE_NONE, &netdev);
1099 if (!error) {
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);
1105
1106 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1107 if (!mask.s_addr) {
1108 mask.s_addr = guess_netmask(ip.s_addr);
1109 }
1110 if (!netdev_set_in4(netdev, ip, mask)) {
1111 VLOG_INFO("bridge %s: configured IP address "IP_FMT", "
1112 "netmask "IP_FMT,
1113 br->name, IP_ARGS(&ip.s_addr),
1114 IP_ARGS(&mask.s_addr));
1115 }
1116
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));
1121 }
1122 }
1123 }
1124 netdev_close(netdev);
1125 }
1126 }
1127
1128 fail_mode = cfg_get_string(0, "%s.fail-mode", pfx);
1129 if (!fail_mode) {
1130 fail_mode = cfg_get_string(0, "mgmt.fail-mode");
1131 }
1132 ofproto_set_failure(br->ofproto,
1133 (!fail_mode
1134 || !strcmp(fail_mode, "standalone")
1135 || !strcmp(fail_mode, "open")));
1136
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"));
1140
1141 max_backoff = cfg_get_int(0, "%s.max-backoff", pfx);
1142 if (!max_backoff) {
1143 max_backoff = cfg_get_int(0, "mgmt.max-backoff");
1144 if (!max_backoff) {
1145 max_backoff = 15;
1146 }
1147 }
1148 ofproto_set_max_backoff(br->ofproto, max_backoff);
1149
1150 rate_limit = cfg_get_int(0, "%s.rate-limit", pfx);
1151 if (!rate_limit) {
1152 rate_limit = cfg_get_int(0, "mgmt.rate-limit");
1153 }
1154 burst_limit = cfg_get_int(0, "%s.burst-limit", pfx);
1155 if (!burst_limit) {
1156 burst_limit = cfg_get_int(0, "mgmt.burst-limit");
1157 }
1158 ofproto_set_rate_limit(br->ofproto, rate_limit, burst_limit);
1159
1160 ofproto_set_stp(br->ofproto, cfg_get_bool(0, "%s.stp", pfx));
1161
1162 if (cfg_has("%s.commands.acl", pfx)) {
1163 struct svec command_acls;
1164 char *command_acl;
1165
1166 svec_init(&command_acls);
1167 cfg_get_all_strings(&command_acls, "%s.commands.acl", pfx);
1168 command_acl = svec_join(&command_acls, ",", "");
1169
1170 ofproto_set_remote_execution(br->ofproto, command_acl,
1171 cfg_get_string(0, "%s.commands.dir",
1172 pfx));
1173
1174 svec_destroy(&command_acls);
1175 free(command_acl);
1176 } else {
1177 ofproto_set_remote_execution(br->ofproto, NULL, NULL);
1178 }
1179 } else {
1180 union ofp_action action;
1181 flow_t flow;
1182
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,
1191 &action, 1, 0);
1192
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);
1198 }
1199 free(pfx);
1200
1201 ofproto_set_controller(br->ofproto, br->controller);
1202 }
1203
1204 static void
1205 bridge_get_all_ifaces(const struct bridge *br, struct svec *ifaces)
1206 {
1207 size_t i, j;
1208
1209 svec_init(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);
1215 }
1216 }
1217 svec_sort(ifaces);
1218 assert(svec_is_unique(ifaces));
1219 }
1220
1221 /* For robustness, in case the administrator moves around datapath ports behind
1222 * our back, we re-check all the datapath port numbers here.
1223 *
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. */
1229 static void
1230 bridge_fetch_dp_ifaces(struct bridge *br)
1231 {
1232 struct odp_port *dpif_ports;
1233 size_t n_dpif_ports;
1234 size_t i, j;
1235
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;
1242 }
1243 }
1244 port_array_clear(&br->ifaces);
1245
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);
1250 if (iface) {
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);
1257 } else {
1258 port_array_set(&br->ifaces, p->port, iface);
1259 iface->dp_ifidx = p->port;
1260 }
1261 }
1262 }
1263 free(dpif_ports);
1264 }
1265 \f
1266 /* Bridge packet processing functions. */
1267
1268 static int
1269 bond_hash(const uint8_t mac[ETH_ADDR_LEN])
1270 {
1271 return hash_bytes(mac, ETH_ADDR_LEN, 0) & BOND_MASK;
1272 }
1273
1274 static struct bond_entry *
1275 lookup_bond_entry(const struct port *port, const uint8_t mac[ETH_ADDR_LEN])
1276 {
1277 return &port->bond_hash[bond_hash(mac)];
1278 }
1279
1280 static int
1281 bond_choose_iface(const struct port *port)
1282 {
1283 size_t i;
1284 for (i = 0; i < port->n_ifaces; i++) {
1285 if (port->ifaces[i]->enabled) {
1286 return i;
1287 }
1288 }
1289 return -1;
1290 }
1291
1292 static bool
1293 choose_output_iface(const struct port *port, const uint8_t *dl_src,
1294 uint16_t *dp_ifidx, tag_type *tags)
1295 {
1296 struct iface *iface;
1297
1298 assert(port->n_ifaces);
1299 if (port->n_ifaces == 1) {
1300 iface = port->ifaces[0];
1301 } else {
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;
1310 return false;
1311 }
1312 e->iface_tag = tag_create_random();
1313 }
1314 *tags |= e->iface_tag;
1315 iface = port->ifaces[e->iface_idx];
1316 }
1317 *dp_ifidx = iface->dp_ifidx;
1318 *tags |= iface->tag; /* Currently only used for bonding. */
1319 return true;
1320 }
1321
1322 static void
1323 bond_link_status_update(struct iface *iface, bool carrier)
1324 {
1325 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1326 struct port *port = iface->port;
1327
1328 if ((carrier == iface->enabled) == (iface->delay_expires == LLONG_MAX)) {
1329 /* Nothing to do. */
1330 return;
1331 }
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");
1338 } else {
1339 int delay = carrier ? port->updelay : port->downdelay;
1340 iface->delay_expires = time_msec() + delay;
1341 if (delay) {
1342 VLOG_INFO_RL(&rl,
1343 "interface %s: will be %s if it stays %s for %d ms",
1344 iface->name,
1345 carrier ? "enabled" : "disabled",
1346 carrier ? "up" : "down",
1347 delay);
1348 }
1349 }
1350 }
1351
1352 static void
1353 bond_choose_active_iface(struct port *port)
1354 {
1355 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1356
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);
1362 } else {
1363 VLOG_WARN_RL(&rl, "port %s: all ports disabled, no active interface",
1364 port->name);
1365 }
1366 }
1367
1368 static void
1369 bond_enable_slave(struct iface *iface, bool enable)
1370 {
1371 struct port *port = iface->port;
1372 struct bridge *br = port->bridge;
1373
1374 iface->delay_expires = LLONG_MAX;
1375 if (enable == iface->enabled) {
1376 return;
1377 }
1378
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);
1387 }
1388 bond_send_learning_packets(port);
1389 } else {
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);
1395 }
1396 iface->tag = tag_create_random();
1397 }
1398 }
1399
1400 static void
1401 bond_run(struct bridge *br)
1402 {
1403 size_t i, j;
1404
1405 for (i = 0; i < br->n_ports; i++) {
1406 struct port *port = br->ports[i];
1407 if (port->n_ifaces < 2) {
1408 continue;
1409 }
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);
1414 }
1415 }
1416 }
1417 }
1418
1419 static void
1420 bond_wait(struct bridge *br)
1421 {
1422 size_t i, j;
1423
1424 for (i = 0; i < br->n_ports; i++) {
1425 struct port *port = br->ports[i];
1426 if (port->n_ifaces < 2) {
1427 continue;
1428 }
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());
1433 }
1434 }
1435 }
1436 }
1437
1438 static bool
1439 set_dst(struct dst *p, const flow_t *flow,
1440 const struct port *in_port, const struct port *out_port,
1441 tag_type *tags)
1442 {
1443 /* STP handling.
1444 *
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.
1448 *
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))) {
1452 return false;
1453 }
1454
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);
1459 }
1460
1461 static void
1462 swap_dst(struct dst *p, struct dst *q)
1463 {
1464 struct dst tmp = *p;
1465 *p = *q;
1466 *q = tmp;
1467 }
1468
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.) */
1474 static void
1475 partition_dsts(struct dst *dsts, size_t n_dsts, int vlan)
1476 {
1477 struct dst *first = dsts;
1478 struct dst *last = dsts + n_dsts;
1479
1480 while (first != last) {
1481 /* Invariants:
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) {
1487 return;
1488 }
1489 }
1490
1491 /* Same invariants, plus one additional:
1492 * - first->vlan != vlan.
1493 */
1494 while (last[-1].vlan != vlan) {
1495 if (--last == first) {
1496 return;
1497 }
1498 }
1499
1500 /* Same invariants, plus one additional:
1501 * - last[-1].vlan == vlan.*/
1502 swap_dst(first++, --last);
1503 }
1504 }
1505
1506 static int
1507 mirror_mask_ffs(mirror_mask_t mask)
1508 {
1509 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
1510 return ffs(mask);
1511 }
1512
1513 static bool
1514 dst_is_duplicate(const struct dst *dsts, size_t n_dsts,
1515 const struct dst *test)
1516 {
1517 size_t i;
1518 for (i = 0; i < n_dsts; i++) {
1519 if (dsts[i].vlan == test->vlan && dsts[i].dp_ifidx == test->dp_ifidx) {
1520 return true;
1521 }
1522 }
1523 return false;
1524 }
1525
1526 static bool
1527 port_trunks_vlan(const struct port *port, uint16_t vlan)
1528 {
1529 return port->vlan < 0 && bitmap_is_set(port->trunks, vlan);
1530 }
1531
1532 static bool
1533 port_includes_vlan(const struct port *port, uint16_t vlan)
1534 {
1535 return vlan == port->vlan || port_trunks_vlan(port, vlan);
1536 }
1537
1538 static size_t
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)
1542 {
1543 mirror_mask_t mirrors = in_port->src_mirrors;
1544 struct dst *dst = dsts;
1545 size_t i;
1546
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;
1557 dst++;
1558 }
1559 }
1560 } else if (out_port && set_dst(dst, flow, in_port, out_port, tags)) {
1561 mirrors |= out_port->dst_mirrors;
1562 dst++;
1563 }
1564
1565 while (mirrors) {
1566 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
1567 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
1568 if (m->out_port) {
1569 if (set_dst(dst, flow, in_port, m->out_port, tags)
1570 && !dst_is_duplicate(dsts, dst - dsts, dst)) {
1571 dst++;
1572 }
1573 } else {
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))
1579 {
1580 if (port->vlan < 0) {
1581 dst->vlan = m->out_vlan;
1582 }
1583 if (dst->dp_ifidx == flow->in_port
1584 && dst->vlan == vlan) {
1585 /* Don't send out input port on same VLAN. */
1586 continue;
1587 }
1588 dst++;
1589 }
1590 }
1591 }
1592 }
1593 mirrors &= mirrors - 1;
1594 }
1595
1596 partition_dsts(dsts, dst - dsts, ntohs(flow->dl_vlan));
1597 return dst - dsts;
1598 }
1599
1600 static void UNUSED
1601 print_dsts(const struct dst *dsts, size_t n)
1602 {
1603 for (; n--; dsts++) {
1604 printf(">p%"PRIu16, dsts->dp_ifidx);
1605 if (dsts->vlan != OFP_VLAN_NONE) {
1606 printf("v%"PRIu16, dsts->vlan);
1607 }
1608 }
1609 }
1610
1611 static void
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)
1615 {
1616 struct dst dsts[DP_MAX_PORTS * (MAX_MIRRORS + 1)];
1617 size_t n_dsts;
1618 const struct dst *p;
1619 uint16_t cur_vlan;
1620
1621 n_dsts = compose_dsts(br, flow, vlan, in_port, out_port, dsts, tags);
1622
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);
1629 } else {
1630 a = odp_actions_add(actions, ODPAT_SET_VLAN_VID);
1631 a->vlan_vid.vlan_vid = htons(p->vlan);
1632 }
1633 cur_vlan = p->vlan;
1634 }
1635 a = odp_actions_add(actions, ODPAT_OUTPUT);
1636 a->output.port = p->dp_ifidx;
1637 }
1638 }
1639
1640 static bool
1641 is_bcast_arp_reply(const flow_t *flow, const struct ofpbuf *packet)
1642 {
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);
1648 }
1649
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. */
1653 static bool
1654 process_flow(struct bridge *br, const flow_t *flow,
1655 const struct ofpbuf *packet, struct odp_actions *actions,
1656 tag_type *tags)
1657 {
1658 struct iface *in_iface;
1659 struct port *in_port;
1660 struct port *out_port = NULL; /* By default, drop the packet/flow. */
1661 int vlan;
1662
1663 /* Find the interface and port structure for the received packet. */
1664 in_iface = iface_from_dp_ifidx(br, flow->in_port);
1665 if (!in_iface) {
1666 /* No interface? Something fishy... */
1667 if (packet != NULL) {
1668 /* Odd. A few possible reasons here:
1669 *
1670 * - We deleted an interface but there are still a few packets
1671 * queued up from it.
1672 *
1673 * - Someone externally added an interface (e.g. with "ovs-dpctl
1674 * add-if") that we don't know about.
1675 *
1676 * - Packet arrived on the local port but the local port is not
1677 * one of our bridge ports.
1678 */
1679 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1680
1681 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
1682 "interface %"PRIu16, br->name, flow->in_port);
1683 }
1684
1685 /* Return without adding any actions, to drop packets on this flow. */
1686 return true;
1687 }
1688 in_port = in_iface->port;
1689
1690 /* Figure out what VLAN this packet belongs to.
1691 *
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) {
1699 vlan = 0;
1700 }
1701 if (in_port->vlan >= 0) {
1702 if (vlan) {
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);
1711 }
1712 goto done;
1713 }
1714 vlan = in_port->vlan;
1715 } else {
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 "
1720 "trunking VLAN %d",
1721 br->name, vlan, in_port->name, vlan);
1722 goto done;
1723 }
1724 }
1725
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)) {
1730 goto done;
1731 }
1732
1733 /* Drop frames for reserved multicast addresses. */
1734 if (eth_addr_is_reserved(flow->dl_dst)) {
1735 goto done;
1736 }
1737
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);
1744 goto done;
1745 }
1746
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) {
1752 goto done;
1753 }
1754 }
1755
1756 /* MAC learning. */
1757 out_port = FLOOD_PORT;
1758 if (br->ml) {
1759 int out_port_idx;
1760 bool may_learn;
1761
1762 if (!packet) {
1763 /* Don't try to learn from revalidation. */
1764 may_learn = false;
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));
1775 } else {
1776 may_learn = true;
1777 }
1778
1779 /* Learn source MAC. */
1780 if (may_learn) {
1781 tag_type rev_tag = mac_learning_learn(br->ml, flow->dl_src,
1782 vlan, in_port->port_idx);
1783 if (rev_tag) {
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,
1787 300);
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);
1793 }
1794 }
1795
1796 /* Determine output port. */
1797 out_port_idx = mac_learning_lookup_tag(br->ml, flow->dl_dst, vlan,
1798 tags);
1799 if (out_port_idx >= 0 && out_port_idx < br->n_ports) {
1800 out_port = br->ports[out_port_idx];
1801 }
1802 }
1803
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) {
1807 out_port = NULL;
1808 }
1809
1810 done:
1811 compose_actions(br, flow, vlan, in_port, out_port, tags, actions);
1812
1813 /*
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.
1818 */
1819 return (in_port->n_ifaces < 2
1820 || flow->dl_type != htons(ETH_TYPE_ARP)
1821 || !eth_addr_is_broadcast(flow->dl_dst));
1822 }
1823
1824 /* Careful: 'opp' is in host byte order and opp->port_no is an OFP port
1825 * number. */
1826 static void
1827 bridge_port_changed_ofhook_cb(enum ofp_port_reason reason,
1828 const struct ofp_phy_port *opp,
1829 void *br_)
1830 {
1831 struct bridge *br = br_;
1832 struct iface *iface;
1833 struct port *port;
1834
1835 iface = iface_from_dp_ifidx(br, ofp_port_to_odp_port(opp->port_no));
1836 if (!iface) {
1837 return;
1838 }
1839 port = iface->port;
1840
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);
1848 port_destroy(port);
1849 }
1850
1851 bridge_flush(br);
1852 } else {
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);
1858 }
1859 }
1860 }
1861
1862 static bool
1863 bridge_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
1864 struct odp_actions *actions, tag_type *tags, void *br_)
1865 {
1866 struct bridge *br = br_;
1867
1868 #if 0
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);
1872 return true;
1873 }
1874 #endif
1875
1876 COVERAGE_INC(bridge_process_flow);
1877 return process_flow(br, flow, packet, actions, tags);
1878 }
1879
1880 static void
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,
1884 void *br_)
1885 {
1886 struct bridge *br = br_;
1887 const union odp_action *a;
1888
1889 if (!br->has_bonded_ports) {
1890 return;
1891 }
1892
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;
1899 }
1900 }
1901 }
1902 }
1903
1904 static void
1905 bridge_account_checkpoint_ofhook_cb(void *br_)
1906 {
1907 struct bridge *br = br_;
1908 size_t i;
1909
1910 if (!br->has_bonded_ports) {
1911 return;
1912 }
1913
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) {
1917 return;
1918 }
1919 br->bond_next_rebalance = time_msec() + 10000;
1920
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);
1925 }
1926 }
1927 }
1928
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,
1934 };
1935 \f
1936 /* Bonding functions. */
1937
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. */
1943
1944 /* All the "bond_entry"s that are assigned to this interface, in order of
1945 * increasing tx_bytes. */
1946 struct bond_entry **hashes;
1947 size_t n_hashes;
1948 };
1949
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. */
1953 static int
1954 compare_bond_entries(const void *a_, const void *b_)
1955 {
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;
1964 } else {
1965 return 0;
1966 }
1967 }
1968
1969 /* Sorts slave_balances so that enabled ports come first, and otherwise in
1970 * *descending* order by number of bytes transmitted. */
1971 static int
1972 compare_slave_balance(const void *a_, const void *b_)
1973 {
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;
1980 } else {
1981 return 0;
1982 }
1983 }
1984
1985 static void
1986 swap_bals(struct slave_balance *a, struct slave_balance *b)
1987 {
1988 struct slave_balance tmp = *a;
1989 *a = *b;
1990 *b = tmp;
1991 }
1992
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.
1995 *
1996 * This function invalidates 'p', since it might now be in a different memory
1997 * location. */
1998 static void
1999 resort_bals(struct slave_balance *p,
2000 struct slave_balance bals[], size_t n_bals)
2001 {
2002 if (n_bals > 1) {
2003 for (; p > bals && p->tx_bytes > p[-1].tx_bytes; p--) {
2004 swap_bals(p, p - 1);
2005 }
2006 for (; p < &bals[n_bals - 1] && p->tx_bytes < p[1].tx_bytes; p++) {
2007 swap_bals(p, p + 1);
2008 }
2009 }
2010 }
2011
2012 static void
2013 log_bals(const struct slave_balance *bals, size_t n_bals, struct port *port)
2014 {
2015 if (VLOG_IS_DBG_ENABLED()) {
2016 struct ds ds = DS_EMPTY_INITIALIZER;
2017 const struct slave_balance *b;
2018
2019 for (b = bals; b < bals + n_bals; b++) {
2020 size_t i;
2021
2022 if (b > bals) {
2023 ds_put_char(&ds, ',');
2024 }
2025 ds_put_format(&ds, " %s %"PRIu64"kB",
2026 b->iface->name, b->tx_bytes / 1024);
2027
2028 if (!b->iface->enabled) {
2029 ds_put_cstr(&ds, " (disabled)");
2030 }
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];
2035 if (i > 0) {
2036 ds_put_cstr(&ds, " + ");
2037 }
2038 ds_put_format(&ds, "h%td: %"PRIu64"kB",
2039 e - port->bond_hash, e->tx_bytes / 1024);
2040 }
2041 ds_put_cstr(&ds, ")");
2042 }
2043 }
2044 VLOG_DBG("bond %s:%s", port->name, ds_cstr(&ds));
2045 ds_destroy(&ds);
2046 }
2047 }
2048
2049 /* Shifts 'hash' from 'from' to 'to' within 'port'. */
2050 static void
2051 bond_shift_load(struct slave_balance *from, struct slave_balance *to,
2052 struct bond_entry *hash)
2053 {
2054 struct port *port = from->iface->port;
2055 uint64_t delta = hash->tx_bytes;
2056
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);
2064
2065 /* Delete element from from->hashes.
2066 *
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) {
2072 from->hashes++;
2073 } else {
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);
2077 }
2078 from->n_hashes--;
2079
2080 /* Shift load away from 'from' to 'to'. */
2081 from->tx_bytes -= delta;
2082 to->tx_bytes += delta;
2083
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();
2088 }
2089
2090 static void
2091 bond_rebalance_port(struct port *port)
2092 {
2093 struct slave_balance bals[DP_MAX_PORTS];
2094 size_t n_bals;
2095 struct bond_entry *hashes[BOND_MASK + 1];
2096 struct slave_balance *b, *from, *to;
2097 struct bond_entry *e;
2098 size_t i;
2099
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
2103 * loaded slave.
2104 *
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];
2113 b->tx_bytes = 0;
2114 b->hashes = NULL;
2115 b->n_hashes = 0;
2116 }
2117 for (i = 0; i <= BOND_MASK; i++) {
2118 hashes[i] = &port->bond_hash[i];
2119 }
2120 qsort(hashes, BOND_MASK + 1, sizeof *hashes, compare_bond_entries);
2121 for (i = 0; i <= BOND_MASK; i++) {
2122 e = hashes[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;
2126 if (!b->hashes) {
2127 b->hashes = &hashes[i];
2128 }
2129 b->n_hashes++;
2130 }
2131 }
2132 qsort(bals, n_bals, sizeof *bals, compare_slave_balance);
2133 log_bals(bals, n_bals, port);
2134
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) {
2138 n_bals--;
2139 if (!n_bals) {
2140 return;
2141 }
2142 }
2143
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. */
2152 break;
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. */
2156 from++;
2157 } else {
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.
2162 *
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. */
2167 size_t i;
2168
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) {
2172 break;
2173 }
2174 }
2175 if (i < from->n_hashes) {
2176 bond_shift_load(from, to, from->hashes[i]);
2177
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);
2184 } else {
2185 from++;
2186 }
2187 }
2188 }
2189
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++) {
2193 e->tx_bytes /= 2;
2194 }
2195 }
2196
2197 static void
2198 bond_send_learning_packets(struct port *port)
2199 {
2200 struct bridge *br = port->bridge;
2201 struct mac_entry *e;
2202 struct ofpbuf packet;
2203 int error, n_packets, n_errors;
2204
2205 if (!port->n_ifaces || port->active_iface < 0 || !br->ml) {
2206 return;
2207 }
2208
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;
2216 uint16_t dp_ifidx;
2217 tag_type tags = 0;
2218 flow_t flow;
2219 int retval;
2220
2221 if (e->port == port->port_idx
2222 || !choose_output_iface(port, e->mac, &dp_ifidx, &tags)) {
2223 continue;
2224 }
2225
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);
2232
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);
2236
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. */
2242
2243 /* Compose actions. */
2244 memset(actions, 0, sizeof actions);
2245 a = actions;
2246 if (e->vlan) {
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);
2250 a++;
2251 }
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));
2255 a++;
2256
2257 /* Send packet. */
2258 n_packets++;
2259 flow_extract(&packet, ODPP_NONE, &flow);
2260 retval = ofproto_send_packet(br->ofproto, &flow, actions, a - actions,
2261 &packet);
2262 if (retval) {
2263 error = retval;
2264 n_errors++;
2265 }
2266 }
2267 ofpbuf_uninit(&packet);
2268
2269 if (n_errors) {
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));
2274 } else {
2275 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2276 port->name, n_packets);
2277 }
2278 }
2279 \f
2280 /* Bonding unixctl user interface functions. */
2281
2282 static void
2283 bond_unixctl_list(struct unixctl_conn *conn, const char *args UNUSED)
2284 {
2285 struct ds ds = DS_EMPTY_INITIALIZER;
2286 const struct bridge *br;
2287
2288 ds_put_cstr(&ds, "bridge\tbond\tslaves\n");
2289
2290 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
2291 size_t i;
2292
2293 for (i = 0; i < br->n_ports; i++) {
2294 const struct port *port = br->ports[i];
2295 if (port->n_ifaces > 1) {
2296 size_t j;
2297
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];
2301 if (j) {
2302 ds_put_cstr(&ds, ", ");
2303 }
2304 ds_put_cstr(&ds, iface->name);
2305 }
2306 ds_put_char(&ds, '\n');
2307 }
2308 }
2309 }
2310 unixctl_command_reply(conn, 200, ds_cstr(&ds));
2311 ds_destroy(&ds);
2312 }
2313
2314 static struct port *
2315 bond_find(const char *name)
2316 {
2317 const struct bridge *br;
2318
2319 LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
2320 size_t i;
2321
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) {
2325 return port;
2326 }
2327 }
2328 }
2329 return NULL;
2330 }
2331
2332 static void
2333 bond_unixctl_show(struct unixctl_conn *conn, const char *args)
2334 {
2335 struct ds ds = DS_EMPTY_INITIALIZER;
2336 const struct port *port;
2337 size_t j;
2338
2339 port = bond_find(args);
2340 if (!port) {
2341 unixctl_command_reply(conn, 501, "no such bond");
2342 return;
2343 }
2344
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;
2352
2353 /* Basic info. */
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");
2358 }
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());
2363 }
2364
2365 /* Hashes. */
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;
2369
2370 if (be->iface_idx != j) {
2371 continue;
2372 }
2373
2374 ds_put_format(&ds, "\thash %d: %lld kB load\n",
2375 hash, be->tx_bytes / 1024);
2376
2377 /* MACs. */
2378 if (!port->bridge->ml) {
2379 break;
2380 }
2381
2382 LIST_FOR_EACH (me, struct mac_entry, lru_node,
2383 &port->bridge->ml->lrus) {
2384 uint16_t dp_ifidx;
2385 tag_type tags = 0;
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)
2390 {
2391 ds_put_format(&ds, "\t\t"ETH_ADDR_FMT"\n",
2392 ETH_ADDR_ARGS(me->mac));
2393 }
2394 }
2395 }
2396 }
2397 unixctl_command_reply(conn, 200, ds_cstr(&ds));
2398 ds_destroy(&ds);
2399 }
2400
2401 static void
2402 bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_)
2403 {
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];
2408 struct port *port;
2409 struct iface *iface;
2410 struct bond_entry *entry;
2411 int hash;
2412
2413 bond_s = strtok_r(args, " ", &save_ptr);
2414 hash_s = strtok_r(NULL, " ", &save_ptr);
2415 slave_s = strtok_r(NULL, " ", &save_ptr);
2416 if (!slave_s) {
2417 unixctl_command_reply(conn, 501,
2418 "usage: bond/migrate BOND HASH SLAVE");
2419 return;
2420 }
2421
2422 port = bond_find(bond_s);
2423 if (!port) {
2424 unixctl_command_reply(conn, 501, "no such bond");
2425 return;
2426 }
2427
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;
2433 } else {
2434 unixctl_command_reply(conn, 501, "bad hash");
2435 return;
2436 }
2437
2438 iface = port_lookup_iface(port, slave_s);
2439 if (!iface) {
2440 unixctl_command_reply(conn, 501, "no such slave");
2441 return;
2442 }
2443
2444 if (!iface->enabled) {
2445 unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
2446 return;
2447 }
2448
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");
2454 }
2455
2456 static void
2457 bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_)
2458 {
2459 char *args = (char *) args_;
2460 char *save_ptr = NULL;
2461 char *bond_s, *slave_s;
2462 struct port *port;
2463 struct iface *iface;
2464
2465 bond_s = strtok_r(args, " ", &save_ptr);
2466 slave_s = strtok_r(NULL, " ", &save_ptr);
2467 if (!slave_s) {
2468 unixctl_command_reply(conn, 501,
2469 "usage: bond/set-active-slave BOND SLAVE");
2470 return;
2471 }
2472
2473 port = bond_find(bond_s);
2474 if (!port) {
2475 unixctl_command_reply(conn, 501, "no such bond");
2476 return;
2477 }
2478
2479 iface = port_lookup_iface(port, slave_s);
2480 if (!iface) {
2481 unixctl_command_reply(conn, 501, "no such slave");
2482 return;
2483 }
2484
2485 if (!iface->enabled) {
2486 unixctl_command_reply(conn, 501, "cannot make disabled slave active");
2487 return;
2488 }
2489
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");
2498 } else {
2499 unixctl_command_reply(conn, 200, "no change");
2500 }
2501 }
2502
2503 static void
2504 enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
2505 {
2506 char *args = (char *) args_;
2507 char *save_ptr = NULL;
2508 char *bond_s, *slave_s;
2509 struct port *port;
2510 struct iface *iface;
2511
2512 bond_s = strtok_r(args, " ", &save_ptr);
2513 slave_s = strtok_r(NULL, " ", &save_ptr);
2514 if (!slave_s) {
2515 unixctl_command_reply(conn, 501,
2516 "usage: bond/enable/disable-slave BOND SLAVE");
2517 return;
2518 }
2519
2520 port = bond_find(bond_s);
2521 if (!port) {
2522 unixctl_command_reply(conn, 501, "no such bond");
2523 return;
2524 }
2525
2526 iface = port_lookup_iface(port, slave_s);
2527 if (!iface) {
2528 unixctl_command_reply(conn, 501, "no such slave");
2529 return;
2530 }
2531
2532 bond_enable_slave(iface, enable);
2533 unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
2534 }
2535
2536 static void
2537 bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args)
2538 {
2539 enable_slave(conn, args, true);
2540 }
2541
2542 static void
2543 bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args)
2544 {
2545 enable_slave(conn, args, false);
2546 }
2547
2548 static void
2549 bond_init(void)
2550 {
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);
2558 }
2559 \f
2560 /* Port functions. */
2561
2562 static void
2563 port_create(struct bridge *br, const char *name)
2564 {
2565 struct port *port;
2566
2567 port = xcalloc(1, sizeof *port);
2568 port->bridge = br;
2569 port->port_idx = br->n_ports;
2570 port->vlan = -1;
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;
2576
2577 if (br->n_ports >= br->allocated_ports) {
2578 br->ports = x2nrealloc(br->ports, &br->allocated_ports,
2579 sizeof *br->ports);
2580 }
2581 br->ports[br->n_ports++] = port;
2582
2583 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2584 bridge_flush(br);
2585 }
2586
2587 static void
2588 port_reconfigure(struct port *port)
2589 {
2590 bool bonded = cfg_has_section("bonding.%s", port->name);
2591 struct svec old_ifaces, new_ifaces;
2592 unsigned long *trunks;
2593 int vlan;
2594 size_t i;
2595
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);
2601 }
2602 svec_sort(&old_ifaces);
2603 if (bonded) {
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",
2607 port->name);
2608 } else if (new_ifaces.n == 1) {
2609 VLOG_WARN("port %s: only 1 interface specified for bonded port",
2610 port->name);
2611 }
2612
2613 port->updelay = cfg_get_int(0, "bonding.%s.updelay", port->name);
2614 if (port->updelay < 0) {
2615 port->updelay = 0;
2616 }
2617 port->downdelay = cfg_get_int(0, "bonding.%s.downdelay", port->name);
2618 if (port->downdelay < 0) {
2619 port->downdelay = 0;
2620 }
2621 } else {
2622 svec_init(&new_ifaces);
2623 svec_add(&new_ifaces, port->name);
2624 }
2625
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);
2631 } else {
2632 i++;
2633 }
2634 }
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);
2639 }
2640 }
2641
2642 /* Get VLAN tag. */
2643 vlan = -1;
2644 if (cfg_has("vlan.%s.tag", port->name)) {
2645 if (!bonded) {
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);
2649 }
2650 } else {
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",
2654 port->name);
2655 }
2656 }
2657 if (port->vlan != vlan) {
2658 port->vlan = vlan;
2659 bridge_flush(port->bridge);
2660 }
2661
2662 /* Get trunked VLANs. */
2663 trunks = NULL;
2664 if (vlan < 0) {
2665 size_t n_trunks, n_errors;
2666 size_t i;
2667
2668 trunks = bitmap_allocate(4096);
2669 n_trunks = cfg_count("vlan.%s.trunks", port->name);
2670 n_errors = 0;
2671 for (i = 0; i < n_trunks; i++) {
2672 int trunk = cfg_get_vlan(i, "vlan.%s.trunks", port->name);
2673 if (trunk >= 0) {
2674 bitmap_set1(trunks, trunk);
2675 } else {
2676 n_errors++;
2677 }
2678 }
2679 if (n_errors) {
2680 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
2681 port->name, n_trunks);
2682 }
2683 if (n_errors == n_trunks) {
2684 if (n_errors) {
2685 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
2686 port->name);
2687 }
2688 bitmap_set_multiple(trunks, 0, 4096, 1);
2689 }
2690 } else {
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);
2694 }
2695 }
2696 if (trunks == NULL
2697 ? port->trunks != NULL
2698 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
2699 bridge_flush(port->bridge);
2700 }
2701 bitmap_free(port->trunks);
2702 port->trunks = trunks;
2703
2704 svec_destroy(&old_ifaces);
2705 svec_destroy(&new_ifaces);
2706 }
2707
2708 static void
2709 port_destroy(struct port *port)
2710 {
2711 if (port) {
2712 struct bridge *br = port->bridge;
2713 struct port *del;
2714 size_t i;
2715
2716 proc_net_compat_update_vlan(port->name, NULL, 0);
2717
2718 for (i = 0; i < MAX_MIRRORS; i++) {
2719 struct mirror *m = br->mirrors[i];
2720 if (m && m->out_port == port) {
2721 mirror_destroy(m);
2722 }
2723 }
2724
2725 while (port->n_ifaces > 0) {
2726 iface_destroy(port->ifaces[port->n_ifaces - 1]);
2727 }
2728
2729 del = br->ports[port->port_idx] = br->ports[--br->n_ports];
2730 del->port_idx = port->port_idx;
2731
2732 free(port->ifaces);
2733 bitmap_free(port->trunks);
2734 free(port->name);
2735 free(port);
2736 bridge_flush(br);
2737 }
2738 }
2739
2740 static struct port *
2741 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
2742 {
2743 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
2744 return iface ? iface->port : NULL;
2745 }
2746
2747 static struct port *
2748 port_lookup(const struct bridge *br, const char *name)
2749 {
2750 size_t i;
2751
2752 for (i = 0; i < br->n_ports; i++) {
2753 struct port *port = br->ports[i];
2754 if (!strcmp(port->name, name)) {
2755 return port;
2756 }
2757 }
2758 return NULL;
2759 }
2760
2761 static struct iface *
2762 port_lookup_iface(const struct port *port, const char *name)
2763 {
2764 size_t j;
2765
2766 for (j = 0; j < port->n_ifaces; j++) {
2767 struct iface *iface = port->ifaces[j];
2768 if (!strcmp(iface->name, name)) {
2769 return iface;
2770 }
2771 }
2772 return NULL;
2773 }
2774
2775 static void
2776 port_update_bonding(struct port *port)
2777 {
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);
2784 }
2785 } else {
2786 if (!port->bond_hash) {
2787 size_t i;
2788
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];
2792 e->iface_idx = -1;
2793 e->tx_bytes = 0;
2794 }
2795 port->no_ifaces_tag = tag_create_random();
2796 bond_choose_active_iface(port);
2797 }
2798 port_update_bond_compat(port);
2799 }
2800 }
2801
2802 static void
2803 port_update_bond_compat(struct port *port)
2804 {
2805 struct compat_bond bond;
2806 size_t i;
2807
2808 if (port->n_ifaces < 2) {
2809 return;
2810 }
2811
2812 bond.up = false;
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));
2823 if (slave->up) {
2824 bond.up = true;
2825 }
2826 memcpy(slave->mac, iface->mac, ETH_ADDR_LEN);
2827 }
2828 proc_net_compat_update_bond(port->name, &bond);
2829 free(bond.slaves);
2830 }
2831
2832 static void
2833 port_update_vlan_compat(struct port *port)
2834 {
2835 struct bridge *br = port->bridge;
2836 char *vlandev_name = NULL;
2837
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.
2845 *
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. */
2849 size_t i;
2850 for (i = 0; i < br->n_ports; i++) {
2851 struct port *p = br->ports[i];
2852 if (port_trunks_vlan(p, port->vlan)
2853 && p->n_ifaces
2854 && (!vlandev_name || strcmp(p->name, vlandev_name) <= 0))
2855 {
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;
2861 }
2862 }
2863 }
2864 }
2865 proc_net_compat_update_vlan(port->name, vlandev_name, port->vlan);
2866 }
2867 \f
2868 /* Interface functions. */
2869
2870 static void
2871 iface_create(struct port *port, const char *name)
2872 {
2873 struct iface *iface;
2874
2875 iface = xcalloc(1, sizeof *iface);
2876 iface->port = port;
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;
2882
2883 netdev_nodev_get_etheraddr(name, iface->mac);
2884 netdev_nodev_get_carrier(name, &iface->enabled);
2885
2886 if (port->n_ifaces >= port->allocated_ifaces) {
2887 port->ifaces = x2nrealloc(port->ifaces, &port->allocated_ifaces,
2888 sizeof *port->ifaces);
2889 }
2890 port->ifaces[port->n_ifaces++] = iface;
2891 if (port->n_ifaces > 1) {
2892 port->bridge->has_bonded_ports = true;
2893 }
2894
2895 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
2896
2897 port_update_bonding(port);
2898 bridge_flush(port->bridge);
2899 }
2900
2901 static void
2902 iface_destroy(struct iface *iface)
2903 {
2904 if (iface) {
2905 struct port *port = iface->port;
2906 struct bridge *br = port->bridge;
2907 bool del_active = port->active_iface == iface->port_ifidx;
2908 struct iface *del;
2909
2910 if (iface->dp_ifidx >= 0) {
2911 port_array_set(&br->ifaces, iface->dp_ifidx, NULL);
2912 }
2913
2914 del = port->ifaces[iface->port_ifidx] = port->ifaces[--port->n_ifaces];
2915 del->port_ifidx = iface->port_ifidx;
2916
2917 free(iface->name);
2918 free(iface);
2919
2920 if (del_active) {
2921 ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
2922 bond_choose_active_iface(port);
2923 bond_send_learning_packets(port);
2924 }
2925
2926 port_update_bonding(port);
2927 bridge_flush(port->bridge);
2928 }
2929 }
2930
2931 static struct iface *
2932 iface_lookup(const struct bridge *br, const char *name)
2933 {
2934 size_t i, j;
2935
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)) {
2941 return iface;
2942 }
2943 }
2944 }
2945 return NULL;
2946 }
2947
2948 static struct iface *
2949 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
2950 {
2951 return port_array_get(&br->ifaces, dp_ifidx);
2952 }
2953 \f
2954 /* Port mirroring. */
2955
2956 static void
2957 mirror_reconfigure(struct bridge *br)
2958 {
2959 struct svec old_mirrors, new_mirrors;
2960 size_t i;
2961
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);
2969 }
2970 }
2971
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)) {
2980 mirror_destroy(m);
2981 }
2982 }
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);
2987 }
2988 }
2989 svec_destroy(&old_mirrors);
2990 svec_destroy(&new_mirrors);
2991
2992 /* Reconfigure all mirrors. */
2993 for (i = 0; i < MAX_MIRRORS; i++) {
2994 if (br->mirrors[i]) {
2995 mirror_reconfigure_one(br->mirrors[i]);
2996 }
2997 }
2998
2999 /* Update port reserved status. */
3000 for (i = 0; i < br->n_ports; i++) {
3001 br->ports[i]->is_mirror_output_port = false;
3002 }
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;
3007 }
3008 }
3009 }
3010
3011 static void
3012 mirror_create(struct bridge *br, const char *name)
3013 {
3014 struct mirror *m;
3015 size_t i;
3016
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);
3021 return;
3022 }
3023 if (!br->mirrors[i]) {
3024 break;
3025 }
3026 }
3027
3028 VLOG_INFO("created port mirror %s on bridge %s", name, br->name);
3029 bridge_flush(br);
3030
3031 br->mirrors[i] = m = xcalloc(1, sizeof *m);
3032 m->bridge = br;
3033 m->idx = i;
3034 m->name = xstrdup(name);
3035 svec_init(&m->src_ports);
3036 svec_init(&m->dst_ports);
3037 m->vlans = NULL;
3038 m->n_vlans = 0;
3039 m->out_vlan = -1;
3040 m->out_port = NULL;
3041 }
3042
3043 static void
3044 mirror_destroy(struct mirror *m)
3045 {
3046 if (m) {
3047 struct bridge *br = m->bridge;
3048 size_t i;
3049
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);
3053 }
3054
3055 svec_destroy(&m->src_ports);
3056 svec_destroy(&m->dst_ports);
3057 free(m->vlans);
3058
3059 m->bridge->mirrors[m->idx] = NULL;
3060 free(m);
3061
3062 bridge_flush(br);
3063 }
3064 }
3065
3066 static void
3067 prune_ports(struct mirror *m, struct svec *ports)
3068 {
3069 struct svec tmp;
3070 size_t i;
3071
3072 svec_sort_unique(ports);
3073
3074 svec_init(&tmp);
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);
3079 } else {
3080 VLOG_WARN("mirror.%s.%s: cannot match on nonexistent port %s",
3081 m->bridge->name, m->name, name);
3082 }
3083 }
3084 svec_swap(ports, &tmp);
3085 svec_destroy(&tmp);
3086 }
3087
3088 static size_t
3089 prune_vlans(struct mirror *m, struct svec *vlan_strings, int **vlans)
3090 {
3091 size_t n_vlans, i;
3092
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);
3097
3098 *vlans = xmalloc(sizeof *vlans * vlan_strings->n);
3099 n_vlans = 0;
3100 for (i = 0; i < vlan_strings->n; i++) {
3101 const char *name = vlan_strings->names[i];
3102 int vlan;
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);
3106 } else {
3107 (*vlans)[n_vlans++] = vlan;
3108 }
3109 }
3110 return n_vlans;
3111 }
3112
3113 static bool
3114 vlan_is_mirrored(const struct mirror *m, int vlan)
3115 {
3116 size_t i;
3117
3118 for (i = 0; i < m->n_vlans; i++) {
3119 if (m->vlans[i] == vlan) {
3120 return true;
3121 }
3122 }
3123 return false;
3124 }
3125
3126 static bool
3127 port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
3128 {
3129 size_t i;
3130
3131 for (i = 0; i < m->n_vlans; i++) {
3132 if (port_trunks_vlan(p, m->vlans[i])) {
3133 return true;
3134 }
3135 }
3136 return false;
3137 }
3138
3139 static void
3140 mirror_reconfigure_one(struct mirror *m)
3141 {
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;
3148 int out_vlan;
3149 size_t n_vlans;
3150 int *vlans;
3151 size_t i;
3152 bool mirror_all_ports;
3153
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);
3159 if (!out_port) {
3160 VLOG_ERR("%s.output.port: bridge %s does not have a port "
3161 "named %s", pfx, m->bridge->name, out_port_name);
3162 mirror_destroy(m);
3163 free(pfx);
3164 return;
3165 }
3166 out_vlan = -1;
3167
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);
3171 }
3172 } else if (cfg_has("%s.output.vlan", pfx)) {
3173 out_port = NULL;
3174 out_vlan = cfg_get_vlan(0, "%s.output.vlan", pfx);
3175 } else {
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);
3179 mirror_destroy(m);
3180 free(pfx);
3181 return;
3182 }
3183
3184 /* Get all the ports, and drop duplicates and ports that don't exist. */
3185 svec_init(&src_ports);
3186 svec_init(&dst_ports);
3187 svec_init(&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);
3196
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);
3202
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);
3211 }
3212 svec_swap(&m->src_ports, &src_ports);
3213 svec_swap(&m->dst_ports, &dst_ports);
3214 free(m->vlans);
3215 m->vlans = vlans;
3216 m->n_vlans = n_vlans;
3217 m->out_port = out_port;
3218 m->out_vlan = out_vlan;
3219
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);
3222
3223 /* Update ports. */
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];
3227
3228 if (mirror_all_ports
3229 || svec_contains(&m->src_ports, port->name)
3230 || (m->n_vlans
3231 && (!port->vlan
3232 ? port_trunks_any_mirrored_vlan(m, port)
3233 : vlan_is_mirrored(m, port->vlan)))) {
3234 port->src_mirrors |= mirror_bit;
3235 } else {
3236 port->src_mirrors &= ~mirror_bit;
3237 }
3238
3239 if (mirror_all_ports || svec_contains(&m->dst_ports, port->name)) {
3240 port->dst_mirrors |= mirror_bit;
3241 } else {
3242 port->dst_mirrors &= ~mirror_bit;
3243 }
3244 }
3245
3246 /* Clean up. */
3247 svec_destroy(&src_ports);
3248 svec_destroy(&dst_ports);
3249 free(pfx);
3250 }
3251 \f
3252 /* Spanning tree protocol. */
3253
3254 static void brstp_update_port_state(struct port *);
3255
3256 static void
3257 brstp_send_bpdu(struct ofpbuf *pkt, int port_no, void *br_)
3258 {
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);
3262 if (!iface) {
3263 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
3264 br->name, port_no);
3265 } else if (eth_addr_is_zero(iface->mac)) {
3266 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d with unknown MAC",
3267 br->name, port_no);
3268 } else {
3269 union ofp_action action;
3270 struct eth_header *eth = pkt->l2;
3271 flow_t flow;
3272
3273 memcpy(eth->eth_src, iface->mac, ETH_ADDR_LEN);
3274
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);
3279
3280 flow_extract(pkt, ODPP_NONE, &flow);
3281 ofproto_send_packet(br->ofproto, &flow, &action, 1, pkt);
3282 }
3283 ofpbuf_delete(pkt);
3284 }
3285
3286 static void
3287 brstp_reconfigure(struct bridge *br)
3288 {
3289 size_t i;
3290
3291 if (!cfg_get_bool(0, "stp.%s.enabled", br->name)) {
3292 if (br->stp) {
3293 stp_destroy(br->stp);
3294 br->stp = NULL;
3295
3296 bridge_flush(br);
3297 }
3298 } else {
3299 uint64_t bridge_address, bridge_id;
3300 int bridge_priority;
3301
3302 bridge_address = cfg_get_mac(0, "stp.%s.address", br->name);
3303 if (!bridge_address) {
3304 if (br->stp) {
3305 bridge_address = (stp_get_bridge_id(br->stp)
3306 & ((UINT64_C(1) << 48) - 1));
3307 } else {
3308 uint8_t mac[ETH_ADDR_LEN];
3309 eth_addr_random(mac);
3310 bridge_address = eth_addr_to_uint64(mac);
3311 }
3312 }
3313
3314 if (cfg_is_valid(CFG_INT | CFG_REQUIRED, "stp.%s.priority",
3315 br->name)) {
3316 bridge_priority = cfg_get_int(0, "stp.%s.priority", br->name);
3317 } else {
3318 bridge_priority = STP_DEFAULT_BRIDGE_PRIORITY;
3319 }
3320
3321 bridge_id = bridge_address | ((uint64_t) bridge_priority << 48);
3322 if (!br->stp) {
3323 br->stp = stp_create(br->name, bridge_id, brstp_send_bpdu, br);
3324 br->stp_last_tick = time_msec();
3325 bridge_flush(br);
3326 } else {
3327 if (bridge_id != stp_get_bridge_id(br->stp)) {
3328 stp_set_bridge_id(br->stp, bridge_id);
3329 bridge_flush(br);
3330 }
3331 }
3332
3333 for (i = 0; i < br->n_ports; i++) {
3334 struct port *p = br->ports[i];
3335 int dp_ifidx;
3336 struct stp_port *sp;
3337 int path_cost, priority;
3338 bool enable;
3339
3340 if (!p->n_ifaces) {
3341 continue;
3342 }
3343 dp_ifidx = p->ifaces[0]->dp_ifidx;
3344 if (dp_ifidx < 0 || dp_ifidx >= STP_MAX_PORTS) {
3345 continue;
3346 }
3347
3348 sp = stp_get_port(br->stp, dp_ifidx);
3349 enable = (!cfg_is_valid(CFG_BOOL | CFG_REQUIRED,
3350 "stp.%s.port.%s.enabled",
3351 br->name, p->name)
3352 || cfg_get_bool(0, "stp.%s.port.%s.enabled",
3353 br->name, p->name));
3354 if (p->is_mirror_output_port) {
3355 enable = false;
3356 }
3357 if (enable != (stp_port_get_state(sp) != STP_DISABLED)) {
3358 bridge_flush(br); /* Might not be necessary. */
3359 if (enable) {
3360 stp_port_enable(sp);
3361 } else {
3362 stp_port_disable(sp);
3363 }
3364 }
3365
3366 path_cost = cfg_get_int(0, "stp.%s.port.%s.path-cost",
3367 br->name, p->name);
3368 stp_port_set_path_cost(sp, path_cost ? path_cost : 19 /* XXX */);
3369
3370 priority = (cfg_is_valid(CFG_INT | CFG_REQUIRED,
3371 "stp.%s.port.%s.priority",
3372 br->name, p->name)
3373 ? cfg_get_int(0, "stp.%s.port.%s.priority",
3374 br->name, p->name)
3375 : STP_DEFAULT_PORT_PRIORITY);
3376 stp_port_set_priority(sp, priority);
3377 }
3378
3379 brstp_adjust_timers(br);
3380 }
3381 for (i = 0; i < br->n_ports; i++) {
3382 brstp_update_port_state(br->ports[i]);
3383 }
3384 }
3385
3386 static void
3387 brstp_update_port_state(struct port *p)
3388 {
3389 struct bridge *br = p->bridge;
3390 enum stp_state state;
3391
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));
3398 }
3399 }
3400
3401 /* Update state. */
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) {
3408 bridge_flush(br);
3409 } else {
3410 ofproto_revalidate(p->bridge->ofproto, p->stp_state_tag);
3411 }
3412 p->stp_state = state;
3413 p->stp_state_tag = (p->stp_state == STP_DISABLED ? 0
3414 : tag_create_random());
3415 }
3416 }
3417
3418 static void
3419 brstp_adjust_timers(struct bridge *br)
3420 {
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);
3424
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);
3428 }
3429
3430 static void
3431 brstp_run(struct bridge *br)
3432 {
3433 if (br->stp) {
3434 long long int now = time_msec();
3435 long long int elapsed = now - br->stp_last_tick;
3436 struct stp_port *sp;
3437
3438 if (elapsed > 0) {
3439 stp_tick(br->stp, MIN(INT_MAX, elapsed));
3440 br->stp_last_tick = now;
3441 }
3442 while (stp_get_changed_port(br->stp, &sp)) {
3443 struct port *p = port_from_dp_ifidx(br, stp_port_no(sp));
3444 if (p) {
3445 brstp_update_port_state(p);
3446 }
3447 }
3448 }
3449 }
3450
3451 static void
3452 brstp_wait(struct bridge *br)
3453 {
3454 if (br->stp) {
3455 poll_timer_wait(1000);
3456 }
3457 }