]> git.proxmox.com Git - ovs.git/blame - vswitchd/bridge.c
Convert shash users that don't use the 'data' value to sset instead.
[ovs.git] / vswitchd / bridge.c
CommitLineData
b0ec0f27 1/* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
c93b1d6a 2 *
a14bc59f
BP
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
064af421 6 *
a14bc59f 7 * http://www.apache.org/licenses/LICENSE-2.0
064af421 8 *
a14bc59f
BP
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
064af421
BP
14 */
15
16#include <config.h>
17#include "bridge.h"
10a24935 18#include "byte-order.h"
064af421
BP
19#include <assert.h>
20#include <errno.h>
21#include <arpa/inet.h>
22#include <ctype.h>
23#include <inttypes.h>
9d82ec47 24#include <sys/socket.h>
064af421
BP
25#include <net/if.h>
26#include <openflow/openflow.h>
27#include <signal.h>
28#include <stdlib.h>
29#include <strings.h>
30#include <sys/stat.h>
31#include <sys/socket.h>
7e40e21d 32#include <sys/types.h>
064af421
BP
33#include <unistd.h>
34#include "bitmap.h"
b31bcf60 35#include "cfm.h"
cf3fad8a 36#include "classifier.h"
064af421 37#include "coverage.h"
a7ff9bd7 38#include "daemon.h"
064af421
BP
39#include "dirs.h"
40#include "dpif.h"
41#include "dynamic-string.h"
42#include "flow.h"
43#include "hash.h"
d9a8717a 44#include "hmap.h"
cd11000b 45#include "jsonrpc.h"
6aa74308 46#include "lacp.h"
064af421
BP
47#include "list.h"
48#include "mac-learning.h"
49#include "netdev.h"
cdee00fd 50#include "netlink.h"
064af421
BP
51#include "odp-util.h"
52#include "ofp-print.h"
53#include "ofpbuf.h"
d65349ea 54#include "ofproto/netflow.h"
8cd4882f 55#include "ofproto/ofproto.h"
dd0d105c 56#include "ovsdb-data.h"
da285df4 57#include "packets.h"
064af421 58#include "poll-loop.h"
064af421 59#include "process.h"
76343538 60#include "sha1.h"
6c88d577 61#include "shash.h"
064af421 62#include "socket-util.h"
fe55ad15 63#include "stream-ssl.h"
b3c01ed3 64#include "sset.h"
064af421 65#include "svec.h"
ce887677 66#include "system-stats.h"
064af421
BP
67#include "timeval.h"
68#include "util.h"
da285df4 69#include "unixctl.h"
064af421 70#include "vconn.h"
b36682d8 71#include "vswitchd/vswitch-idl.h"
064af421 72#include "xenserver.h"
5136ce49 73#include "vlog.h"
72b06300 74#include "sflow_api.h"
064af421 75
d98e6007 76VLOG_DEFINE_THIS_MODULE(bridge);
064af421 77
d76f09ea
BP
78COVERAGE_DEFINE(bridge_flush);
79COVERAGE_DEFINE(bridge_process_flow);
ebe482fd
EJ
80COVERAGE_DEFINE(bridge_process_cfm);
81COVERAGE_DEFINE(bridge_process_lacp);
d76f09ea 82COVERAGE_DEFINE(bridge_reconfigure);
750d79a5 83COVERAGE_DEFINE(bridge_lacp_update);
d76f09ea 84
064af421
BP
85struct dst {
86 uint16_t vlan;
87 uint16_t dp_ifidx;
88};
89
0c62a7ad
BP
90struct dst_set {
91 struct dst builtin[32];
92 struct dst *dsts;
93 size_t n, allocated;
94};
95
96static void dst_set_init(struct dst_set *);
97static void dst_set_add(struct dst_set *, const struct dst *);
98static void dst_set_free(struct dst_set *);
99
064af421 100struct iface {
0c6aea3f 101 /* These members are always valid. */
83db7968 102 struct list port_elem; /* Element in struct port's "ifaces" list. */
064af421 103 struct port *port; /* Containing port. */
064af421 104 char *name; /* Host network device name. */
064af421 105 tag_type tag; /* Tag associated with this interface. */
064af421 106 long long delay_expires; /* Time after which 'enabled' may change. */
064af421 107
0c6aea3f 108 /* These members are valid only after bridge_reconfigure() causes them to
1e0b752d 109 * be initialized. */
d9a8717a 110 struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
0c6aea3f
BP
111 int dp_ifidx; /* Index within kernel datapath. */
112 struct netdev *netdev; /* Network device. */
064af421 113 bool enabled; /* May be chosen for flows? */
c835c3bf 114 bool up; /* Is the interface up? */
6cefe1da 115 const char *type; /* Usually same as cfg->type. */
76343538 116 const struct ovsrec_interface *cfg;
c25c91fd
EJ
117
118 /* LACP information. */
6aa74308 119 uint16_t lacp_priority; /* LACP port priority. */
064af421
BP
120};
121
122#define BOND_MASK 0xff
123struct bond_entry {
c17f0d5e 124 struct iface *iface; /* Assigned iface, or NULL if none. */
064af421 125 uint64_t tx_bytes; /* Count of bytes recently transmitted. */
c0cb6f59 126 tag_type tag; /* Tag for bond_entry<->iface association. */
064af421
BP
127};
128
27dcaa1a 129enum bond_mode {
9f5073d8
EJ
130 BM_TCP, /* Transport Layer Load Balance. */
131 BM_SLB, /* Source Load Balance. */
27dcaa1a 132 BM_AB /* Active Backup. */
be02e7c3
EJ
133};
134
064af421
BP
135#define MAX_MIRRORS 32
136typedef uint32_t mirror_mask_t;
137#define MIRROR_MASK_C(X) UINT32_C(X)
138BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
139struct mirror {
140 struct bridge *bridge;
141 size_t idx;
142 char *name;
dd0d105c 143 struct uuid uuid; /* UUID of this "mirror" record in database. */
064af421
BP
144
145 /* Selection criteria. */
b3c01ed3
BP
146 struct sset src_ports; /* Source port names. */
147 struct sset dst_ports; /* Destination port names. */
064af421
BP
148 int *vlans;
149 size_t n_vlans;
150
151 /* Output. */
152 struct port *out_port;
153 int out_vlan;
154};
155
156#define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
157struct port {
158 struct bridge *bridge;
8052fb14
BP
159 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
160 char *name;
161
064af421 162 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
3e9c481c
BP
163 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
164 * NULL if all VLANs are trunked. */
1e0b752d 165 const struct ovsrec_port *cfg;
064af421 166
e5ed989d
EJ
167 /* Monitoring. */
168 struct netdev_monitor *monitor; /* Tracks carrier. NULL if miimon. */
169 long long int miimon_interval; /* Miimon status refresh interval. */
170 long long int miimon_next_update; /* Time of next miimon update. */
171
064af421
BP
172 /* An ordinary bridge port has 1 interface.
173 * A bridge port for bonding has at least 2 interfaces. */
83db7968
BP
174 struct list ifaces; /* List of "struct iface"s. */
175 size_t n_ifaces; /* list_size(ifaces). */
064af421
BP
176
177 /* Bonding info. */
27dcaa1a 178 enum bond_mode bond_mode; /* Type of the bond. BM_SLB is the default. */
990cda85 179 struct iface *active_iface; /* iface on which bcasts accepted, or NULL. */
064af421
BP
180 tag_type no_ifaces_tag; /* Tag for flows when all ifaces disabled. */
181 int updelay, downdelay; /* Delay before iface goes up/down, in ms. */
76343538 182 bool bond_fake_iface; /* Fake a bond interface for legacy compat? */
e5ae7df8 183 long long int bond_next_fake_iface_update; /* Time of next update. */
be02e7c3 184
c25c91fd 185 /* LACP information. */
6aa74308
EJ
186 struct lacp *lacp; /* LACP object. NULL if LACP is disabled. */
187 bool lacp_active; /* True if LACP is active */
269340fa 188 bool lacp_fast; /* True if LACP is in fast mode. */
c25c91fd 189 uint16_t lacp_priority; /* LACP system priority. */
c25c91fd 190
be02e7c3
EJ
191 /* SLB specific bonding info. */
192 struct bond_entry *bond_hash; /* An array of (BOND_MASK + 1) elements. */
c8143c88
BP
193 int bond_rebalance_interval; /* Interval between rebalances, in ms. */
194 long long int bond_next_rebalance; /* Next rebalancing time. */
064af421
BP
195
196 /* Port mirroring info. */
197 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
198 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
199 bool is_mirror_output_port; /* Does port mirroring send frames here? */
064af421
BP
200};
201
064af421
BP
202struct bridge {
203 struct list node; /* Node in global list of bridges. */
204 char *name; /* User-specified arbitrary name. */
93dfc067 205 struct mac_learning *ml; /* MAC learning table. */
abe457eb 206 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
064af421 207 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
1e0b752d 208 const struct ovsrec_bridge *cfg;
064af421 209
064af421
BP
210 /* OpenFlow switch processing. */
211 struct ofproto *ofproto; /* OpenFlow switch. */
212
213 /* Kernel datapath information. */
c228a364 214 struct dpif *dpif; /* Datapath. */
d9a8717a 215 struct hmap ifaces; /* Contains "struct iface"s. */
064af421
BP
216
217 /* Bridge ports. */
8052fb14 218 struct hmap ports; /* "struct port"s indexed by name. */
4a1ee6ae 219 struct shash iface_by_name; /* "struct iface"s indexed by name. */
064af421
BP
220
221 /* Bonding. */
222 bool has_bonded_ports;
064af421
BP
223
224 /* Flow tracking. */
225 bool flush;
226
064af421
BP
227 /* Port mirroring. */
228 struct mirror *mirrors[MAX_MIRRORS];
064af421
BP
229};
230
231/* List of all bridges. */
232static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
233
c5187f17
BP
234/* OVSDB IDL used to obtain configuration. */
235static struct ovsdb_idl *idl;
236
cd0cd65f
BP
237/* Each time this timer expires, the bridge fetches systems and interface
238 * statistics and pushes them into the database. */
239#define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
240static long long int stats_timer = LLONG_MIN;
018f1525 241
6586adf5
EJ
242/* Stores the time after which CFM statistics may be written to the database.
243 * Only updated when changes to the database require rate limiting. */
244#define CFM_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
245static long long int cfm_limiter = LLONG_MIN;
246
1a6f1e2a 247static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
064af421
BP
248static void bridge_destroy(struct bridge *);
249static struct bridge *bridge_lookup(const char *name);
8ca79daa 250static unixctl_cb_func bridge_unixctl_dump_flows;
fa05809b 251static unixctl_cb_func bridge_unixctl_reconnect;
064af421 252static int bridge_run_one(struct bridge *);
1a048029 253static size_t bridge_get_controllers(const struct bridge *br,
76ce9432 254 struct ovsrec_controller ***controllersp);
1a048029
JP
255static void bridge_reconfigure_one(struct bridge *);
256static void bridge_reconfigure_remotes(struct bridge *,
11bbff1b
BP
257 const struct sockaddr_in *managers,
258 size_t n_managers);
76343538 259static void bridge_get_all_ifaces(const struct bridge *, struct shash *ifaces);
064af421
BP
260static void bridge_fetch_dp_ifaces(struct bridge *);
261static void bridge_flush(struct bridge *);
262static void bridge_pick_local_hw_addr(struct bridge *,
263 uint8_t ea[ETH_ADDR_LEN],
07c318f4 264 struct iface **hw_addr_iface);
064af421
BP
265static uint64_t bridge_pick_datapath_id(struct bridge *,
266 const uint8_t bridge_ea[ETH_ADDR_LEN],
07c318f4 267 struct iface *hw_addr_iface);
064af421
BP
268static uint64_t dpid_from_hash(const void *, size_t nbytes);
269
8ca79daa 270static unixctl_cb_func bridge_unixctl_fdb_show;
20c8e971 271static unixctl_cb_func cfm_unixctl_show;
e8fe3026 272static unixctl_cb_func qos_unixctl_show;
8c4c1387 273
da285df4 274static void bond_init(void);
21dcb94f
EJ
275static void bond_run(struct port *);
276static void bond_wait(struct port *);
064af421 277static void bond_rebalance_port(struct port *);
2303f3b2 278static void bond_send_learning_packets(struct port *);
8b2a2f4a 279static void bond_enable_slave(struct iface *iface, bool enable);
064af421 280
21dcb94f
EJ
281static void port_run(struct port *);
282static void port_wait(struct port *);
76343538
BP
283static struct port *port_create(struct bridge *, const char *name);
284static void port_reconfigure(struct port *, const struct ovsrec_port *);
4a1ee6ae 285static void port_del_ifaces(struct port *, const struct ovsrec_port *);
064af421
BP
286static void port_destroy(struct port *);
287static struct port *port_lookup(const struct bridge *, const char *name);
da285df4 288static struct iface *port_lookup_iface(const struct port *, const char *name);
83db7968 289static struct iface *port_get_an_iface(const struct port *);
064af421
BP
290static struct port *port_from_dp_ifidx(const struct bridge *,
291 uint16_t dp_ifidx);
0c6aea3f 292static void port_update_bonding(struct port *);
c25c91fd 293static void port_update_lacp(struct port *);
064af421 294
dd0d105c 295static void mirror_create(struct bridge *, struct ovsrec_mirror *);
064af421
BP
296static void mirror_destroy(struct mirror *);
297static void mirror_reconfigure(struct bridge *);
37e7f427 298static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
064af421
BP
299static bool vlan_is_mirrored(const struct mirror *, int vlan);
300
d295e8e9 301static struct iface *iface_create(struct port *port,
a740f0de 302 const struct ovsrec_interface *if_cfg);
064af421
BP
303static void iface_destroy(struct iface *);
304static struct iface *iface_lookup(const struct bridge *, const char *name);
e8fe3026 305static struct iface *iface_find(const char *name);
064af421
BP
306static struct iface *iface_from_dp_ifidx(const struct bridge *,
307 uint16_t dp_ifidx);
52df17e7 308static void iface_set_mac(struct iface *);
bcd49a45 309static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
c1c9c9c4 310static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
b31bcf60 311static void iface_update_cfm(struct iface *);
6586adf5 312static bool iface_refresh_cfm_stats(struct iface *iface);
0b8024eb
BP
313static void iface_update_carrier(struct iface *);
314static bool iface_get_carrier(const struct iface *);
064af421 315
43776b8f
BP
316static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
317 struct shash *);
ea763e0e
EJ
318static void shash_to_ovs_idl_map(struct shash *,
319 char ***keys, char ***values, size_t *n);
320
064af421
BP
321/* Hooks into ofproto processing. */
322static struct ofhooks bridge_ofhooks;
323\f
324/* Public functions. */
325
c5187f17
BP
326/* Initializes the bridge module, configuring it to obtain its configuration
327 * from an OVSDB server accessed over 'remote', which should be a string in a
328 * form acceptable to ovsdb_idl_create(). */
064af421 329void
c5187f17
BP
330bridge_init(const char *remote)
331{
332 /* Create connection to database. */
ef73f86c 333 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
c5187f17 334
ef73f86c
BP
335 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
336 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
e85bbd75
BP
337 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
338
339 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
340
341 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
342 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
343
ef73f86c
BP
344 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
345 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
e85bbd75
BP
346 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
347
c5187f17
BP
348 /* Register unixctl commands. */
349 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
20c8e971 350 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
e8fe3026 351 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
c5187f17
BP
352 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
353 NULL);
fa05809b
BP
354 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
355 NULL);
6aa74308 356 lacp_init();
c5187f17
BP
357 bond_init();
358}
359
ee45ad81
BP
360void
361bridge_exit(void)
362{
363 struct bridge *br, *next_br;
364
365 LIST_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
366 bridge_destroy(br);
367 }
368 ovsdb_idl_destroy(idl);
369}
370
c5187f17
BP
371/* Performs configuration that is only necessary once at ovs-vswitchd startup,
372 * but for which the ovs-vswitchd configuration 'cfg' is required. */
373static void
374bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
064af421 375{
c5187f17 376 static bool already_configured_once;
76343538 377 struct svec bridge_names;
1a6f1e2a 378 struct svec dpif_names, dpif_types;
d3d22744 379 size_t i;
da285df4 380
c5187f17
BP
381 /* Only do this once per ovs-vswitchd run. */
382 if (already_configured_once) {
383 return;
384 }
385 already_configured_once = true;
8c4c1387 386
cd0cd65f 387 stats_timer = time_msec() + STATS_INTERVAL;
018f1525 388
c5187f17 389 /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
76343538
BP
390 svec_init(&bridge_names);
391 for (i = 0; i < cfg->n_bridges; i++) {
392 svec_add(&bridge_names, cfg->bridges[i]->name);
393 }
394 svec_sort(&bridge_names);
395
c5187f17
BP
396 /* Iterate over all system dpifs and delete any of them that do not appear
397 * in 'cfg'. */
d2a34569 398 svec_init(&dpif_names);
1a6f1e2a
JG
399 svec_init(&dpif_types);
400 dp_enumerate_types(&dpif_types);
401 for (i = 0; i < dpif_types.n; i++) {
1a6f1e2a 402 size_t j;
064af421 403
1a6f1e2a 404 dp_enumerate_names(dpif_types.names[i], &dpif_names);
d3d22744 405
3d8c9535 406 /* Delete each dpif whose name is not in 'bridge_names'. */
1a6f1e2a 407 for (j = 0; j < dpif_names.n; j++) {
3d8c9535
BP
408 if (!svec_contains(&bridge_names, dpif_names.names[j])) {
409 struct dpif *dpif;
410 int retval;
411
412 retval = dpif_open(dpif_names.names[j], dpif_types.names[i],
413 &dpif);
414 if (!retval) {
415 dpif_delete(dpif);
416 dpif_close(dpif);
d3d22744 417 }
064af421 418 }
064af421
BP
419 }
420 }
7128cd6b 421 svec_destroy(&bridge_names);
d0c9d304 422 svec_destroy(&dpif_names);
1a6f1e2a 423 svec_destroy(&dpif_types);
064af421
BP
424}
425
64d64dd7 426/* Callback for iterate_and_prune_ifaces(). */
0c6aea3f 427static bool
64d64dd7 428check_iface(struct bridge *br, struct iface *iface, void *aux OVS_UNUSED)
0c6aea3f 429{
149f577a 430 if (!iface->netdev) {
64d64dd7
BP
431 /* We already reported a related error, don't bother duplicating it. */
432 return false;
0c6aea3f 433 }
149f577a 434
64d64dd7 435 if (iface->dp_ifidx < 0) {
6ae39834
BP
436 VLOG_ERR("%s interface not in %s, dropping",
437 iface->name, dpif_name(br->dpif));
438 return false;
439 }
64d64dd7
BP
440
441 VLOG_DBG("%s has interface %s on port %d", dpif_name(br->dpif),
442 iface->name, iface->dp_ifidx);
443 return true;
6ae39834
BP
444}
445
64d64dd7 446/* Callback for iterate_and_prune_ifaces(). */
795fe1fb 447static bool
67a4917b
BP
448set_iface_properties(struct bridge *br OVS_UNUSED, struct iface *iface,
449 void *aux OVS_UNUSED)
795fe1fb 450{
4d678233 451 /* Set policing attributes. */
76343538
BP
452 netdev_set_policing(iface->netdev,
453 iface->cfg->ingress_policing_rate,
454 iface->cfg->ingress_policing_burst);
4d678233
BP
455
456 /* Set MAC address of internal interfaces other than the local
457 * interface. */
6cefe1da 458 if (iface->dp_ifidx != ODPP_LOCAL && !strcmp(iface->type, "internal")) {
4d678233
BP
459 iface_set_mac(iface);
460 }
461
795fe1fb
BP
462 return true;
463}
464
6ae39834
BP
465/* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
466 * Deletes from 'br' all the interfaces for which 'cb' returns false, and then
467 * deletes from 'br' any ports that no longer have any interfaces. */
468static void
469iterate_and_prune_ifaces(struct bridge *br,
470 bool (*cb)(struct bridge *, struct iface *,
471 void *aux),
472 void *aux)
473{
8052fb14 474 struct port *port, *next_port;
6ae39834 475
8052fb14
BP
476 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
477 struct iface *iface, *next_iface;
83db7968 478
8052fb14 479 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
83db7968 480 if (!cb(br, iface, aux)) {
bcd49a45 481 iface_set_ofport(iface->cfg, -1);
6ae39834
BP
482 iface_destroy(iface);
483 }
484 }
485
8052fb14 486 if (!port->n_ifaces) {
cca46d33 487 VLOG_WARN("%s port has no interfaces, dropping", port->name);
6ae39834
BP
488 port_destroy(port);
489 }
490 }
491}
492
cd11000b
BP
493/* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
494 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
495 * responsible for freeing '*managersp' (with free()).
496 *
497 * You may be asking yourself "why does ovs-vswitchd care?", because
498 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
499 * should not be and in fact is not directly involved in that. But
500 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
501 * it has to tell in-band control where the managers are to enable that.
94db5407 502 * (Thus, only managers connected in-band are collected.)
cd11000b
BP
503 */
504static void
94db5407
BP
505collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
506 struct sockaddr_in **managersp, size_t *n_managersp)
cd11000b
BP
507{
508 struct sockaddr_in *managers = NULL;
509 size_t n_managers = 0;
b3c01ed3 510 struct sset targets;
94db5407
BP
511 size_t i;
512
289df16d
AE
513 /* Collect all of the potential targets from the "targets" columns of the
514 * rows pointed to by "manager_options", excluding any that are
515 * out-of-band. */
b3c01ed3 516 sset_init(&targets);
94db5407
BP
517 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
518 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
519
520 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
b3c01ed3 521 sset_find_and_delete(&targets, m->target);
94db5407 522 } else {
b3c01ed3 523 sset_add(&targets, m->target);
94db5407
BP
524 }
525 }
cd11000b 526
94db5407 527 /* Now extract the targets' IP addresses. */
b3c01ed3
BP
528 if (!sset_is_empty(&targets)) {
529 const char *target;
cd11000b 530
b3c01ed3
BP
531 managers = xmalloc(sset_count(&targets) * sizeof *managers);
532 SSET_FOR_EACH (target, &targets) {
94db5407 533 struct sockaddr_in *sin = &managers[n_managers];
cd11000b 534
94db5407
BP
535 if ((!strncmp(target, "tcp:", 4)
536 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
537 (!strncmp(target, "ssl:", 4)
538 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
cd11000b
BP
539 n_managers++;
540 }
541 }
542 }
b3c01ed3 543 sset_destroy(&targets);
cd11000b
BP
544
545 *managersp = managers;
546 *n_managersp = n_managers;
547}
548
c5187f17 549static void
76343538 550bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
064af421 551{
76343538
BP
552 struct shash old_br, new_br;
553 struct shash_node *node;
064af421 554 struct bridge *br, *next;
cd11000b
BP
555 struct sockaddr_in *managers;
556 size_t n_managers;
6ae39834 557 size_t i;
72b06300 558 int sflow_bridge_number;
064af421
BP
559
560 COVERAGE_INC(bridge_reconfigure);
561
94db5407 562 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
cd11000b 563
632d136c 564 /* Collect old and new bridges. */
76343538
BP
565 shash_init(&old_br);
566 shash_init(&new_br);
4e8e4213 567 LIST_FOR_EACH (br, node, &all_bridges) {
76343538
BP
568 shash_add(&old_br, br->name, br);
569 }
570 for (i = 0; i < ovs_cfg->n_bridges; i++) {
571 const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
572 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
573 VLOG_WARN("more than one bridge named %s", br_cfg->name);
574 }
064af421 575 }
064af421
BP
576
577 /* Get rid of deleted bridges and add new bridges. */
4e8e4213 578 LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
76343538
BP
579 struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
580 if (br_cfg) {
581 br->cfg = br_cfg;
582 } else {
064af421
BP
583 bridge_destroy(br);
584 }
585 }
76343538
BP
586 SHASH_FOR_EACH (node, &new_br) {
587 const char *br_name = node->name;
588 const struct ovsrec_bridge *br_cfg = node->data;
1a6f1e2a
JG
589 br = shash_find_data(&old_br, br_name);
590 if (br) {
591 /* If the bridge datapath type has changed, we need to tear it
592 * down and recreate. */
593 if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
594 bridge_destroy(br);
595 bridge_create(br_cfg);
fa33d64a 596 }
1a6f1e2a
JG
597 } else {
598 bridge_create(br_cfg);
064af421
BP
599 }
600 }
76343538
BP
601 shash_destroy(&old_br);
602 shash_destroy(&new_br);
064af421 603
064af421 604 /* Reconfigure all bridges. */
4e8e4213 605 LIST_FOR_EACH (br, node, &all_bridges) {
1a048029 606 bridge_reconfigure_one(br);
064af421
BP
607 }
608
609 /* Add and delete ports on all datapaths.
610 *
611 * The kernel will reject any attempt to add a given port to a datapath if
612 * that port already belongs to a different datapath, so we must do all
613 * port deletions before any port additions. */
4e8e4213 614 LIST_FOR_EACH (br, node, &all_bridges) {
b0ec0f27 615 struct dpif_port_dump dump;
76343538 616 struct shash want_ifaces;
4c738a8d 617 struct dpif_port dpif_port;
064af421 618
064af421 619 bridge_get_all_ifaces(br, &want_ifaces);
4c738a8d
BP
620 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
621 if (!shash_find(&want_ifaces, dpif_port.name)
622 && strcmp(dpif_port.name, br->name)) {
623 int retval = dpif_port_del(br->dpif, dpif_port.port_no);
064af421 624 if (retval) {
cca46d33
BP
625 VLOG_WARN("failed to remove %s interface from %s: %s",
626 dpif_port.name, dpif_name(br->dpif),
627 strerror(retval));
064af421
BP
628 }
629 }
630 }
76343538 631 shash_destroy(&want_ifaces);
064af421 632 }
4e8e4213 633 LIST_FOR_EACH (br, node, &all_bridges) {
76343538 634 struct shash cur_ifaces, want_ifaces;
b0ec0f27 635 struct dpif_port_dump dump;
4c738a8d 636 struct dpif_port dpif_port;
064af421 637
76343538 638 /* Get the set of interfaces currently in this datapath. */
76343538 639 shash_init(&cur_ifaces);
4c738a8d 640 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
b0ec0f27 641 struct dpif_port *port_info = xmalloc(sizeof *port_info);
4c738a8d
BP
642 dpif_port_clone(port_info, &dpif_port);
643 shash_add(&cur_ifaces, dpif_port.name, port_info);
064af421 644 }
064af421 645
76343538
BP
646 /* Get the set of interfaces we want on this datapath. */
647 bridge_get_all_ifaces(br, &want_ifaces);
6c88d577 648
3a6ccc8c 649 hmap_clear(&br->ifaces);
76343538
BP
650 SHASH_FOR_EACH (node, &want_ifaces) {
651 const char *if_name = node->name;
652 struct iface *iface = node->data;
b0ec0f27
BP
653 struct dpif_port *dpif_port;
654 const char *type;
3a6ccc8c
BP
655 int error;
656
b0ec0f27
BP
657 type = iface ? iface->type : "internal";
658 dpif_port = shash_find_data(&cur_ifaces, if_name);
659
3a6ccc8c
BP
660 /* If we have a port or a netdev already, and it's not the type we
661 * want, then delete the port (if any) and close the netdev (if
662 * any). */
c3827f61
BP
663 if ((dpif_port && strcmp(dpif_port->type, type))
664 || (iface && iface->netdev
665 && strcmp(type, netdev_get_type(iface->netdev)))) {
3a6ccc8c 666 if (dpif_port) {
b0ec0f27 667 error = ofproto_port_del(br->ofproto, dpif_port->port_no);
3a6ccc8c
BP
668 if (error) {
669 continue;
670 }
671 dpif_port = NULL;
76343538 672 }
3a6ccc8c
BP
673 if (iface) {
674 netdev_close(iface->netdev);
675 iface->netdev = NULL;
676 }
677 }
76343538 678
c3827f61
BP
679 /* If the port doesn't exist or we don't have the netdev open,
680 * we need to do more work. */
681 if (!dpif_port || (iface && !iface->netdev)) {
682 struct netdev_options options;
683 struct netdev *netdev;
684 struct shash args;
685
686 /* First open the network device. */
687 options.name = if_name;
688 options.type = type;
689 options.args = &args;
690 options.ethertype = NETDEV_ETH_TYPE_NONE;
691
692 shash_init(&args);
693 if (iface) {
6b4186af 694 shash_from_ovs_idl_map(iface->cfg->key_options,
fe4838bc
JP
695 iface->cfg->value_options,
696 iface->cfg->n_options, &args);
82057f51 697 }
c3827f61
BP
698 error = netdev_open(&options, &netdev);
699 shash_destroy(&args);
82057f51 700
c3827f61
BP
701 if (error) {
702 VLOG_WARN("could not open network device %s (%s)",
703 if_name, strerror(error));
3a6ccc8c 704 continue;
76343538 705 }
3a6ccc8c 706
c3827f61
BP
707 /* Then add the port if we haven't already. */
708 if (!dpif_port) {
709 error = dpif_port_add(br->dpif, netdev, NULL);
3a6ccc8c 710 if (error) {
c3827f61
BP
711 netdev_close(netdev);
712 if (error == EFBIG) {
713 VLOG_ERR("ran out of valid port numbers on %s",
714 dpif_name(br->dpif));
715 break;
716 } else {
cca46d33
BP
717 VLOG_WARN("failed to add %s interface to %s: %s",
718 if_name, dpif_name(br->dpif),
719 strerror(error));
c3827f61
BP
720 continue;
721 }
3a6ccc8c
BP
722 }
723 }
c3827f61
BP
724
725 /* Update 'iface'. */
726 if (iface) {
727 iface->netdev = netdev;
0b8024eb 728 iface->enabled = iface_get_carrier(iface);
c835c3bf 729 iface->up = iface->enabled;
c3827f61
BP
730 }
731 } else if (iface && iface->netdev) {
732 struct shash args;
733
734 shash_init(&args);
6b4186af 735 shash_from_ovs_idl_map(iface->cfg->key_options,
fe4838bc
JP
736 iface->cfg->value_options,
737 iface->cfg->n_options, &args);
6d9e6eb4 738 netdev_set_config(iface->netdev, &args);
c3827f61 739 shash_destroy(&args);
3a6ccc8c 740 }
064af421 741 }
76343538 742 shash_destroy(&want_ifaces);
b0ec0f27
BP
743
744 SHASH_FOR_EACH (node, &cur_ifaces) {
745 struct dpif_port *port_info = node->data;
4c738a8d 746 dpif_port_destroy(port_info);
b0ec0f27
BP
747 free(port_info);
748 }
749 shash_destroy(&cur_ifaces);
064af421 750 }
72b06300 751 sflow_bridge_number = 0;
4e8e4213 752 LIST_FOR_EACH (br, node, &all_bridges) {
064af421
BP
753 uint8_t ea[8];
754 uint64_t dpid;
07c318f4
BP
755 struct iface *local_iface;
756 struct iface *hw_addr_iface;
093e47f4 757 char *dpid_string;
064af421 758
064af421 759 bridge_fetch_dp_ifaces(br);
064af421 760
64d64dd7 761 iterate_and_prune_ifaces(br, check_iface, NULL);
064af421
BP
762
763 /* Pick local port hardware address, datapath ID. */
07c318f4 764 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
27b3a6e0 765 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
064af421 766 if (local_iface) {
07c318f4 767 int error = netdev_set_etheraddr(local_iface->netdev, ea);
064af421
BP
768 if (error) {
769 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
770 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
771 "Ethernet address: %s",
772 br->name, strerror(error));
773 }
774 }
abe457eb 775 memcpy(br->ea, ea, ETH_ADDR_LEN);
064af421 776
07c318f4 777 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
064af421
BP
778 ofproto_set_datapath_id(br->ofproto, dpid);
779
fc09119a 780 dpid_string = xasprintf("%016"PRIx64, dpid);
093e47f4
BP
781 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
782 free(dpid_string);
783
064af421 784 /* Set NetFlow configuration on this bridge. */
76343538
BP
785 if (br->cfg->netflow) {
786 struct ovsrec_netflow *nf_cfg = br->cfg->netflow;
787 struct netflow_options opts;
788
789 memset(&opts, 0, sizeof opts);
790
791 dpif_get_netflow_ids(br->dpif, &opts.engine_type, &opts.engine_id);
792 if (nf_cfg->engine_type) {
cd746526 793 opts.engine_type = *nf_cfg->engine_type;
76343538
BP
794 }
795 if (nf_cfg->engine_id) {
cd746526 796 opts.engine_id = *nf_cfg->engine_id;
76343538
BP
797 }
798
799 opts.active_timeout = nf_cfg->active_timeout;
800 if (!opts.active_timeout) {
801 opts.active_timeout = -1;
802 } else if (opts.active_timeout < 0) {
e9e2856e
JG
803 VLOG_WARN("bridge %s: active timeout interval set to negative "
804 "value, using default instead (%d seconds)", br->name,
805 NF_ACTIVE_TIMEOUT_DEFAULT);
806 opts.active_timeout = -1;
76343538
BP
807 }
808
809 opts.add_id_to_iface = nf_cfg->add_id_to_interface;
810 if (opts.add_id_to_iface) {
811 if (opts.engine_id > 0x7f) {
812 VLOG_WARN("bridge %s: netflow port mangling may conflict "
813 "with another vswitch, choose an engine id less "
814 "than 128", br->name);
815 }
8052fb14 816 if (hmap_count(&br->ports) > 508) {
76343538
BP
817 VLOG_WARN("bridge %s: netflow port mangling will conflict "
818 "with another port when more than 508 ports are "
819 "used", br->name);
820 }
821 }
822
823 opts.collectors.n = nf_cfg->n_targets;
824 opts.collectors.names = nf_cfg->targets;
825 if (ofproto_set_netflow(br->ofproto, &opts)) {
d295e8e9 826 VLOG_ERR("bridge %s: problem setting netflow collectors",
76343538
BP
827 br->name);
828 }
829 } else {
830 ofproto_set_netflow(br->ofproto, NULL);
831 }
064af421 832
a4af0040
JP
833 /* Set sFlow configuration on this bridge. */
834 if (br->cfg->sflow) {
d49d354b 835 const struct ovsrec_sflow *sflow_cfg = br->cfg->sflow;
76ce9432 836 struct ovsrec_controller **controllers;
72b06300 837 struct ofproto_sflow_options oso;
76ce9432 838 size_t n_controllers;
72b06300 839
a4af0040
JP
840 memset(&oso, 0, sizeof oso);
841
842 oso.targets.n = sflow_cfg->n_targets;
843 oso.targets.names = sflow_cfg->targets;
72b06300
BP
844
845 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
a4af0040
JP
846 if (sflow_cfg->sampling) {
847 oso.sampling_rate = *sflow_cfg->sampling;
72b06300
BP
848 }
849
850 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
a4af0040
JP
851 if (sflow_cfg->polling) {
852 oso.polling_interval = *sflow_cfg->polling;
72b06300
BP
853 }
854
855 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
a4af0040
JP
856 if (sflow_cfg->header) {
857 oso.header_len = *sflow_cfg->header;
72b06300
BP
858 }
859
860 oso.sub_id = sflow_bridge_number++;
a4af0040
JP
861 oso.agent_device = sflow_cfg->agent;
862
76ce9432 863 oso.control_ip = NULL;
1a048029 864 n_controllers = bridge_get_controllers(br, &controllers);
76ce9432
BP
865 for (i = 0; i < n_controllers; i++) {
866 if (controllers[i]->local_ip) {
867 oso.control_ip = controllers[i]->local_ip;
868 break;
869 }
870 }
72b06300
BP
871 ofproto_set_sflow(br->ofproto, &oso);
872
7b99db05 873 /* Do not destroy oso.targets because it is owned by sflow_cfg. */
72b06300
BP
874 } else {
875 ofproto_set_sflow(br->ofproto, NULL);
876 }
877
064af421
BP
878 /* Update the controller and related settings. It would be more
879 * straightforward to call this from bridge_reconfigure_one(), but we
880 * can't do it there for two reasons. First, and most importantly, at
881 * that point we don't know the dp_ifidx of any interfaces that have
882 * been added to the bridge (because we haven't actually added them to
883 * the datapath). Second, at that point we haven't set the datapath ID
884 * yet; when a controller is configured, resetting the datapath ID will
885 * immediately disconnect from the controller, so it's better to set
886 * the datapath ID before the controller. */
1a048029 887 bridge_reconfigure_remotes(br, managers, n_managers);
064af421 888 }
4e8e4213 889 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14
BP
890 struct port *port;
891
892 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968 893 struct iface *iface;
52df17e7 894
e5ed989d 895 if (port->monitor) {
83db7968
BP
896 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
897 netdev_monitor_add(port->monitor, iface->netdev);
e5ed989d
EJ
898 }
899 } else {
900 port->miimon_next_update = 0;
901 }
902
c25c91fd 903 port_update_lacp(port);
afabe15a 904 port_update_bonding(port);
c1c9c9c4 905
83db7968
BP
906 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
907 iface_update_qos(iface, port->cfg->qos);
c1c9c9c4 908 }
064af421
BP
909 }
910 }
4e8e4213 911 LIST_FOR_EACH (br, node, &all_bridges) {
4d678233 912 iterate_and_prune_ifaces(br, set_iface_properties, NULL);
064af421 913 }
093e47f4 914
b31bcf60
EJ
915 LIST_FOR_EACH (br, node, &all_bridges) {
916 struct iface *iface;
917 HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
918 iface_update_cfm(iface);
919 }
920 }
921
cd11000b 922 free(managers);
a7ff9bd7
BP
923
924 /* ovs-vswitchd has completed initialization, so allow the process that
925 * forked us to exit successfully. */
926 daemonize_complete();
093e47f4
BP
927}
928
929static const char *
af6278e1
BP
930get_ovsrec_key_value(const struct ovsdb_idl_row *row,
931 const struct ovsdb_idl_column *column,
932 const char *key)
093e47f4 933{
af6278e1
BP
934 const struct ovsdb_datum *datum;
935 union ovsdb_atom atom;
936 unsigned int idx;
093e47f4 937
af6278e1
BP
938 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
939 atom.string = (char *) key;
940 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
941 return idx == UINT_MAX ? NULL : datum->values[idx].string;
064af421
BP
942}
943
c8143c88
BP
944static const char *
945bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
946{
af6278e1
BP
947 return get_ovsrec_key_value(&br_cfg->header_,
948 &ovsrec_bridge_col_other_config, key);
c8143c88
BP
949}
950
064af421
BP
951static void
952bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
07c318f4 953 struct iface **hw_addr_iface)
064af421 954{
093e47f4 955 const char *hwaddr;
8052fb14 956 struct port *port;
064af421
BP
957 int error;
958
07c318f4 959 *hw_addr_iface = NULL;
064af421
BP
960
961 /* Did the user request a particular MAC? */
093e47f4
BP
962 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
963 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
064af421
BP
964 if (eth_addr_is_multicast(ea)) {
965 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
966 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
967 } else if (eth_addr_is_zero(ea)) {
968 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
969 } else {
970 return;
971 }
972 }
973
141f4942
BP
974 /* Otherwise choose the minimum non-local MAC address among all of the
975 * interfaces. */
0b420b10 976 memset(ea, 0xff, ETH_ADDR_LEN);
8052fb14 977 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
58b7527e 978 uint8_t iface_ea[ETH_ADDR_LEN];
83db7968 979 struct iface *candidate;
58b7527e
BP
980 struct iface *iface;
981
982 /* Mirror output ports don't participate. */
064af421
BP
983 if (port->is_mirror_output_port) {
984 continue;
985 }
58b7527e
BP
986
987 /* Choose the MAC address to represent the port. */
83db7968 988 iface = NULL;
76343538 989 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
ba09980a
BP
990 /* Find the interface with this Ethernet address (if any) so that
991 * we can provide the correct devname to the caller. */
83db7968 992 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
ba09980a 993 uint8_t candidate_ea[ETH_ADDR_LEN];
8fef8c71 994 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
ba09980a
BP
995 && eth_addr_equals(iface_ea, candidate_ea)) {
996 iface = candidate;
997 }
998 }
58b7527e
BP
999 } else {
1000 /* Choose the interface whose MAC address will represent the port.
1001 * The Linux kernel bonding code always chooses the MAC address of
1002 * the first slave added to a bond, and the Fedora networking
1003 * scripts always add slaves to a bond in alphabetical order, so
1004 * for compatibility we choose the interface with the name that is
1005 * first in alphabetical order. */
83db7968
BP
1006 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1007 if (!iface || strcmp(candidate->name, iface->name) < 0) {
58b7527e
BP
1008 iface = candidate;
1009 }
1010 }
1011
1012 /* The local port doesn't count (since we're trying to choose its
141f4942
BP
1013 * MAC address anyway). */
1014 if (iface->dp_ifidx == ODPP_LOCAL) {
064af421
BP
1015 continue;
1016 }
58b7527e
BP
1017
1018 /* Grab MAC. */
07c318f4 1019 error = netdev_get_etheraddr(iface->netdev, iface_ea);
58b7527e 1020 if (error) {
064af421
BP
1021 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1022 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
1023 iface->name, strerror(error));
58b7527e 1024 continue;
064af421
BP
1025 }
1026 }
58b7527e
BP
1027
1028 /* Compare against our current choice. */
1029 if (!eth_addr_is_multicast(iface_ea) &&
141f4942 1030 !eth_addr_is_local(iface_ea) &&
58b7527e
BP
1031 !eth_addr_is_reserved(iface_ea) &&
1032 !eth_addr_is_zero(iface_ea) &&
130f6e5f 1033 eth_addr_compare_3way(iface_ea, ea) < 0)
58b7527e 1034 {
0babc06f 1035 memcpy(ea, iface_ea, ETH_ADDR_LEN);
8fef8c71 1036 *hw_addr_iface = iface;
58b7527e 1037 }
064af421 1038 }
141f4942 1039 if (eth_addr_is_multicast(ea)) {
064af421 1040 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
07c318f4 1041 *hw_addr_iface = NULL;
064af421
BP
1042 VLOG_WARN("bridge %s: using default bridge Ethernet "
1043 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1044 } else {
1045 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1046 br->name, ETH_ADDR_ARGS(ea));
1047 }
1048}
1049
1050/* Choose and returns the datapath ID for bridge 'br' given that the bridge
1051 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
07c318f4
BP
1052 * an interface on 'br', then that interface must be passed in as
1053 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1054 * 'hw_addr_iface' must be passed in as a null pointer. */
064af421
BP
1055static uint64_t
1056bridge_pick_datapath_id(struct bridge *br,
1057 const uint8_t bridge_ea[ETH_ADDR_LEN],
07c318f4 1058 struct iface *hw_addr_iface)
064af421
BP
1059{
1060 /*
1061 * The procedure for choosing a bridge MAC address will, in the most
1062 * ordinary case, also choose a unique MAC that we can use as a datapath
1063 * ID. In some special cases, though, multiple bridges will end up with
1064 * the same MAC address. This is OK for the bridges, but it will confuse
1065 * the OpenFlow controller, because each datapath needs a unique datapath
1066 * ID.
1067 *
1068 * Datapath IDs must be unique. It is also very desirable that they be
1069 * stable from one run to the next, so that policy set on a datapath
1070 * "sticks".
1071 */
093e47f4 1072 const char *datapath_id;
064af421
BP
1073 uint64_t dpid;
1074
093e47f4
BP
1075 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1076 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
064af421
BP
1077 return dpid;
1078 }
1079
07c318f4 1080 if (hw_addr_iface) {
064af421 1081 int vlan;
b2f17b15 1082 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
064af421
BP
1083 /*
1084 * A bridge whose MAC address is taken from a VLAN network device
1085 * (that is, a network device created with vconfig(8) or similar
1086 * tool) will have the same MAC address as a bridge on the VLAN
1087 * device's physical network device.
1088 *
1089 * Handle this case by hashing the physical network device MAC
1090 * along with the VLAN identifier.
1091 */
1092 uint8_t buf[ETH_ADDR_LEN + 2];
1093 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1094 buf[ETH_ADDR_LEN] = vlan >> 8;
1095 buf[ETH_ADDR_LEN + 1] = vlan;
1096 return dpid_from_hash(buf, sizeof buf);
1097 } else {
1098 /*
1099 * Assume that this bridge's MAC address is unique, since it
1100 * doesn't fit any of the cases we handle specially.
1101 */
1102 }
1103 } else {
1104 /*
1105 * A purely internal bridge, that is, one that has no non-virtual
1106 * network devices on it at all, is more difficult because it has no
1107 * natural unique identifier at all.
1108 *
1109 * When the host is a XenServer, we handle this case by hashing the
1110 * host's UUID with the name of the bridge. Names of bridges are
1111 * persistent across XenServer reboots, although they can be reused if
1112 * an internal network is destroyed and then a new one is later
1113 * created, so this is fairly effective.
1114 *
1115 * When the host is not a XenServer, we punt by using a random MAC
1116 * address on each run.
1117 */
1118 const char *host_uuid = xenserver_get_host_uuid();
1119 if (host_uuid) {
1120 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1121 dpid = dpid_from_hash(combined, strlen(combined));
1122 free(combined);
1123 return dpid;
1124 }
1125 }
1126
1127 return eth_addr_to_uint64(bridge_ea);
1128}
1129
1130static uint64_t
1131dpid_from_hash(const void *data, size_t n)
1132{
5eccf359 1133 uint8_t hash[SHA1_DIGEST_SIZE];
064af421
BP
1134
1135 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
5eccf359 1136 sha1_bytes(data, n, hash);
064af421
BP
1137 eth_addr_mark_random(hash);
1138 return eth_addr_to_uint64(hash);
1139}
1140
ea83a2fc 1141static void
ea763e0e 1142iface_refresh_status(struct iface *iface)
ea83a2fc 1143{
ea763e0e
EJ
1144 struct shash sh;
1145
e210037e
AE
1146 enum netdev_flags flags;
1147 uint32_t current;
1148 int64_t bps;
1149 int mtu;
1150 int64_t mtu_64;
1151 int error;
1152
ea763e0e
EJ
1153 shash_init(&sh);
1154
1155 if (!netdev_get_status(iface->netdev, &sh)) {
1156 size_t n;
1157 char **keys, **values;
ea83a2fc 1158
ea763e0e
EJ
1159 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1160 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1161
1162 free(keys);
1163 free(values);
1164 } else {
1165 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1166 }
1167
1168 shash_destroy_free_data(&sh);
e210037e
AE
1169
1170 error = netdev_get_flags(iface->netdev, &flags);
1171 if (!error) {
1172 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1173 }
1174 else {
1175 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1176 }
1177
1178 error = netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
1179 if (!error) {
1180 ovsrec_interface_set_duplex(iface->cfg,
1181 netdev_features_is_full_duplex(current)
1182 ? "full" : "half");
1183 /* warning: uint64_t -> int64_t conversion */
1184 bps = netdev_features_to_bps(current);
1185 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1186 }
1187 else {
1188 ovsrec_interface_set_duplex(iface->cfg, NULL);
1189 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1190 }
1191
1192
1193 ovsrec_interface_set_link_state(iface->cfg,
0b8024eb 1194 iface_get_carrier(iface) ? "up" : "down");
e210037e
AE
1195
1196 error = netdev_get_mtu(iface->netdev, &mtu);
f915f1a8 1197 if (!error && mtu != INT_MAX) {
e210037e
AE
1198 mtu_64 = mtu;
1199 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1200 }
1201 else {
1202 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1203 }
ea83a2fc
EJ
1204}
1205
6586adf5
EJ
1206/* Writes 'iface''s CFM statistics to the database. Returns true if anything
1207 * changed, false otherwise. */
1208static bool
b31bcf60
EJ
1209iface_refresh_cfm_stats(struct iface *iface)
1210{
b31bcf60 1211 const struct ovsrec_monitor *mon;
e7934396 1212 const struct cfm *cfm;
6586adf5 1213 bool changed = false;
e7934396 1214 size_t i;
b31bcf60
EJ
1215
1216 mon = iface->cfg->monitor;
e7934396 1217 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
b31bcf60
EJ
1218
1219 if (!cfm || !mon) {
6586adf5 1220 return false;
b31bcf60
EJ
1221 }
1222
1223 for (i = 0; i < mon->n_remote_mps; i++) {
1224 const struct ovsrec_maintenance_point *mp;
1225 const struct remote_mp *rmp;
1226
1227 mp = mon->remote_mps[i];
1228 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1229
6586adf5
EJ
1230 if (mp->n_fault != 1 || mp->fault[0] != rmp->fault) {
1231 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1232 changed = true;
1233 }
b31bcf60
EJ
1234 }
1235
6586adf5
EJ
1236 if (mon->n_fault != 1 || mon->fault[0] != cfm->fault) {
1237 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1238 changed = true;
1239 }
1240
1241 return changed;
b31bcf60
EJ
1242}
1243
018f1525
BP
1244static void
1245iface_refresh_stats(struct iface *iface)
1246{
1247 struct iface_stat {
1248 char *name;
1249 int offset;
1250 };
1251 static const struct iface_stat iface_stats[] = {
1252 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1253 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1254 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1255 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1256 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1257 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1258 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1259 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1260 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1261 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1262 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1263 { "collisions", offsetof(struct netdev_stats, collisions) },
1264 };
1265 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1266 const struct iface_stat *s;
1267
1268 char *keys[N_STATS];
1269 int64_t values[N_STATS];
1270 int n;
1271
1272 struct netdev_stats stats;
1273
1274 /* Intentionally ignore return value, since errors will set 'stats' to
1275 * all-1s, and we will deal with that correctly below. */
1276 netdev_get_stats(iface->netdev, &stats);
1277
1278 n = 0;
1279 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1280 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1281 if (value != UINT64_MAX) {
1282 keys[n] = s->name;
1283 values[n] = value;
1284 n++;
1285 }
1286 }
1287
1288 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1289}
1290
ce887677
BP
1291static void
1292refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1293{
1294 struct ovsdb_datum datum;
1295 struct shash stats;
1296
1297 shash_init(&stats);
1298 get_system_stats(&stats);
1299
1300 ovsdb_datum_from_shash(&datum, &stats);
1301 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1302 &datum);
1303}
1304
bffc0589
AE
1305static inline const char *
1306nx_role_to_str(enum nx_role role)
1307{
1308 switch (role) {
1309 case NX_ROLE_OTHER:
1310 return "other";
1311 case NX_ROLE_MASTER:
1312 return "master";
1313 case NX_ROLE_SLAVE:
1314 return "slave";
1315 default:
1316 return "*** INVALID ROLE ***";
1317 }
1318}
1319
1320static void
1321bridge_refresh_controller_status(const struct bridge *br)
1322{
1323 struct shash info;
1324 const struct ovsrec_controller *cfg;
1325
1326 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1327
1328 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
eb9b8307
AE
1329 struct ofproto_controller_info *cinfo =
1330 shash_find_data(&info, cfg->target);
1331
1332 if (cinfo) {
1333 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1334 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1335 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1336 (char **) cinfo->pairs.values,
1337 cinfo->pairs.n);
1338 } else {
1339 ovsrec_controller_set_is_connected(cfg, false);
1340 ovsrec_controller_set_role(cfg, NULL);
1341 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1342 }
bffc0589
AE
1343 }
1344
1345 ofproto_free_ofproto_controller_info(&info);
1346}
1347
c5187f17 1348void
064af421
BP
1349bridge_run(void)
1350{
d54ff998
BP
1351 const struct ovsrec_open_vswitch *cfg;
1352
c5187f17 1353 bool datapath_destroyed;
d54ff998 1354 bool database_changed;
c5187f17 1355 struct bridge *br;
064af421 1356
c5187f17
BP
1357 /* Let each bridge do the work that it needs to do. */
1358 datapath_destroyed = false;
4e8e4213 1359 LIST_FOR_EACH (br, node, &all_bridges) {
064af421
BP
1360 int error = bridge_run_one(br);
1361 if (error) {
1362 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1363 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1364 "forcing reconfiguration", br->name);
c5187f17
BP
1365 datapath_destroyed = true;
1366 }
1367 }
1368
1369 /* (Re)configure if necessary. */
d54ff998
BP
1370 database_changed = ovsdb_idl_run(idl);
1371 cfg = ovsrec_open_vswitch_first(idl);
d6da96ce
BP
1372#ifdef HAVE_OPENSSL
1373 /* Re-configure SSL. We do this on every trip through the main loop,
1374 * instead of just when the database changes, because the contents of the
1375 * key and certificate files can change without the database changing.
1376 *
1377 * We do this before bridge_reconfigure() because that function might
1378 * initiate SSL connections and thus requires SSL to be configured. */
1379 if (cfg && cfg->ssl) {
1380 const struct ovsrec_ssl *ssl = cfg->ssl;
1381
1382 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1383 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1384 }
1385#endif
d54ff998 1386 if (database_changed || datapath_destroyed) {
c5187f17
BP
1387 if (cfg) {
1388 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1389
1390 bridge_configure_once(cfg);
1391 bridge_reconfigure(cfg);
1392
1393 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1394 ovsdb_idl_txn_commit(txn);
1395 ovsdb_idl_txn_destroy(txn); /* XXX */
1e0b752d
BP
1396 } else {
1397 /* We still need to reconfigure to avoid dangling pointers to
1398 * now-destroyed ovsrec structures inside bridge data. */
1399 static const struct ovsrec_open_vswitch null_cfg;
1400
1401 bridge_reconfigure(&null_cfg);
064af421
BP
1402 }
1403 }
018f1525 1404
cd0cd65f
BP
1405 /* Refresh system and interface stats if necessary. */
1406 if (time_msec() >= stats_timer) {
ce887677
BP
1407 if (cfg) {
1408 struct ovsdb_idl_txn *txn;
018f1525 1409
ce887677 1410 txn = ovsdb_idl_txn_create(idl);
4e8e4213 1411 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14 1412 struct port *port;
018f1525 1413
8052fb14 1414 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968 1415 struct iface *iface;
018f1525 1416
83db7968 1417 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
ce887677 1418 iface_refresh_stats(iface);
ea763e0e 1419 iface_refresh_status(iface);
ce887677 1420 }
018f1525 1421 }
bffc0589 1422 bridge_refresh_controller_status(br);
018f1525 1423 }
ce887677
BP
1424 refresh_system_stats(cfg);
1425 ovsdb_idl_txn_commit(txn);
1426 ovsdb_idl_txn_destroy(txn); /* XXX */
018f1525 1427 }
018f1525 1428
cd0cd65f 1429 stats_timer = time_msec() + STATS_INTERVAL;
018f1525 1430 }
6586adf5
EJ
1431
1432 if (time_msec() >= cfm_limiter) {
1433 struct ovsdb_idl_txn *txn;
1434 bool changed = false;
1435
1436 txn = ovsdb_idl_txn_create(idl);
1437 LIST_FOR_EACH (br, node, &all_bridges) {
1438 struct port *port;
1439
1440 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1441 struct iface *iface;
1442
1443 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1444 changed = iface_refresh_cfm_stats(iface) || changed;
1445 }
1446 }
1447 }
1448
1449 if (changed) {
1450 cfm_limiter = time_msec() + CFM_LIMIT_INTERVAL;
1451 }
1452
1453 ovsdb_idl_txn_commit(txn);
1454 ovsdb_idl_txn_destroy(txn);
1455 }
064af421
BP
1456}
1457
1458void
1459bridge_wait(void)
1460{
1461 struct bridge *br;
1462
4e8e4213 1463 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14 1464 struct port *port;
21dcb94f 1465
064af421 1466 ofproto_wait(br->ofproto);
93dfc067 1467 mac_learning_wait(br->ml);
8052fb14
BP
1468 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1469 port_wait(port);
b31bcf60 1470 }
064af421 1471 }
c5187f17 1472 ovsdb_idl_wait(idl);
cd0cd65f 1473 poll_timer_wait_until(stats_timer);
6586adf5
EJ
1474
1475 if (cfm_limiter > time_msec()) {
1476 poll_timer_wait_until(cfm_limiter);
1477 }
064af421
BP
1478}
1479
1480/* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1481 * configuration changes. */
1482static void
1483bridge_flush(struct bridge *br)
1484{
1485 COVERAGE_INC(bridge_flush);
1486 br->flush = true;
064af421
BP
1487}
1488\f
8c4c1387
BP
1489/* Bridge unixctl user interface functions. */
1490static void
8ca79daa 1491bridge_unixctl_fdb_show(struct unixctl_conn *conn,
c69ee87c 1492 const char *args, void *aux OVS_UNUSED)
8c4c1387
BP
1493{
1494 struct ds ds = DS_EMPTY_INITIALIZER;
1495 const struct bridge *br;
93dfc067 1496 const struct mac_entry *e;
8c4c1387
BP
1497
1498 br = bridge_lookup(args);
1499 if (!br) {
1500 unixctl_command_reply(conn, 501, "no such bridge");
1501 return;
1502 }
1503
1504 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4e8e4213 1505 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
1648ddd7 1506 struct port *port = e->port.p;
93dfc067 1507 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1648ddd7 1508 port_get_an_iface(port)->dp_ifidx,
93dfc067 1509 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
8c4c1387
BP
1510 }
1511 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1512 ds_destroy(&ds);
1513}
1514\f
20c8e971
EJ
1515/* CFM unixctl user interface functions. */
1516static void
1517cfm_unixctl_show(struct unixctl_conn *conn,
1518 const char *args, void *aux OVS_UNUSED)
1519{
1520 struct ds ds = DS_EMPTY_INITIALIZER;
1521 struct iface *iface;
1522 const struct cfm *cfm;
1523
1524 iface = iface_find(args);
1525 if (!iface) {
1526 unixctl_command_reply(conn, 501, "no such interface");
1527 return;
1528 }
1529
1530 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1531
1532 if (!cfm) {
1533 unixctl_command_reply(conn, 501, "CFM not enabled");
1534 return;
1535 }
1536
1537 cfm_dump_ds(cfm, &ds);
1538 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1539 ds_destroy(&ds);
1540}
1541\f
e8fe3026
EJ
1542/* QoS unixctl user interface functions. */
1543
1544struct qos_unixctl_show_cbdata {
1545 struct ds *ds;
1546 struct iface *iface;
1547};
1548
1549static void
1550qos_unixctl_show_cb(unsigned int queue_id,
1551 const struct shash *details,
1552 void *aux)
1553{
1554 struct qos_unixctl_show_cbdata *data = aux;
1555 struct ds *ds = data->ds;
1556 struct iface *iface = data->iface;
1557 struct netdev_queue_stats stats;
1558 struct shash_node *node;
1559 int error;
1560
1561 ds_put_cstr(ds, "\n");
1562 if (queue_id) {
1563 ds_put_format(ds, "Queue %u:\n", queue_id);
1564 } else {
1565 ds_put_cstr(ds, "Default:\n");
1566 }
1567
1568 SHASH_FOR_EACH (node, details) {
1569 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1570 }
1571
1572 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1573 if (!error) {
1574 if (stats.tx_packets != UINT64_MAX) {
1575 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1576 }
1577
1578 if (stats.tx_bytes != UINT64_MAX) {
1579 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1580 }
1581
1582 if (stats.tx_errors != UINT64_MAX) {
1583 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1584 }
1585 } else {
1586 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1587 queue_id, strerror(error));
1588 }
1589}
1590
1591static void
1592qos_unixctl_show(struct unixctl_conn *conn,
1593 const char *args, void *aux OVS_UNUSED)
1594{
1595 struct ds ds = DS_EMPTY_INITIALIZER;
1596 struct shash sh = SHASH_INITIALIZER(&sh);
1597 struct iface *iface;
1598 const char *type;
1599 struct shash_node *node;
1600 struct qos_unixctl_show_cbdata data;
1601 int error;
1602
1603 iface = iface_find(args);
1604 if (!iface) {
1605 unixctl_command_reply(conn, 501, "no such interface");
1606 return;
1607 }
1608
1609 netdev_get_qos(iface->netdev, &type, &sh);
1610
1611 if (*type != '\0') {
1612 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1613
1614 SHASH_FOR_EACH (node, &sh) {
1615 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1616 }
1617
1618 data.ds = &ds;
1619 data.iface = iface;
1620 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1621
1622 if (error) {
1623 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1624 }
1625 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1626 } else {
1627 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1628 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1629 }
1630
1631 shash_destroy_free_data(&sh);
1632 ds_destroy(&ds);
1633}
1634\f
064af421 1635/* Bridge reconfiguration functions. */
064af421 1636static struct bridge *
1a6f1e2a 1637bridge_create(const struct ovsrec_bridge *br_cfg)
064af421
BP
1638{
1639 struct bridge *br;
1640 int error;
1641
1a6f1e2a 1642 assert(!bridge_lookup(br_cfg->name));
ec6fde61 1643 br = xzalloc(sizeof *br);
064af421 1644
1a6f1e2a
JG
1645 error = dpif_create_and_open(br_cfg->name, br_cfg->datapath_type,
1646 &br->dpif);
efacbce6 1647 if (error) {
064af421
BP
1648 free(br);
1649 return NULL;
1650 }
efacbce6 1651 dpif_flow_flush(br->dpif);
064af421 1652
1a6f1e2a
JG
1653 error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
1654 br, &br->ofproto);
064af421 1655 if (error) {
1a6f1e2a
JG
1656 VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
1657 strerror(error));
c228a364
BP
1658 dpif_delete(br->dpif);
1659 dpif_close(br->dpif);
064af421
BP
1660 free(br);
1661 return NULL;
1662 }
1663
1a6f1e2a
JG
1664 br->name = xstrdup(br_cfg->name);
1665 br->cfg = br_cfg;
064af421 1666 br->ml = mac_learning_create();
70150daf 1667 eth_addr_nicira_random(br->default_ea);
064af421 1668
8052fb14 1669 hmap_init(&br->ports);
d9a8717a 1670 hmap_init(&br->ifaces);
4a1ee6ae
BP
1671 shash_init(&br->iface_by_name);
1672
064af421 1673 br->flush = false;
064af421
BP
1674
1675 list_push_back(&all_bridges, &br->node);
1676
c228a364 1677 VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
064af421
BP
1678
1679 return br;
1680}
1681
1682static void
1683bridge_destroy(struct bridge *br)
1684{
1685 if (br) {
8052fb14 1686 struct port *port, *next;
064af421
BP
1687 int error;
1688
8052fb14
BP
1689 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1690 port_destroy(port);
064af421
BP
1691 }
1692 list_remove(&br->node);
6d6ab93e 1693 ofproto_destroy(br->ofproto);
c228a364 1694 error = dpif_delete(br->dpif);
064af421 1695 if (error && error != ENOENT) {
b29ba128 1696 VLOG_ERR("failed to delete %s: %s",
c228a364 1697 dpif_name(br->dpif), strerror(error));
064af421 1698 }
c228a364 1699 dpif_close(br->dpif);
064af421 1700 mac_learning_destroy(br->ml);
d9a8717a 1701 hmap_destroy(&br->ifaces);
8052fb14 1702 hmap_destroy(&br->ports);
4a1ee6ae 1703 shash_destroy(&br->iface_by_name);
064af421
BP
1704 free(br->name);
1705 free(br);
1706 }
1707}
1708
1709static struct bridge *
1710bridge_lookup(const char *name)
1711{
1712 struct bridge *br;
1713
4e8e4213 1714 LIST_FOR_EACH (br, node, &all_bridges) {
064af421
BP
1715 if (!strcmp(br->name, name)) {
1716 return br;
1717 }
1718 }
1719 return NULL;
1720}
1721
4f2cad2c
JP
1722/* Handle requests for a listing of all flows known by the OpenFlow
1723 * stack, including those normally hidden. */
1724static void
8ca79daa 1725bridge_unixctl_dump_flows(struct unixctl_conn *conn,
c69ee87c 1726 const char *args, void *aux OVS_UNUSED)
4f2cad2c
JP
1727{
1728 struct bridge *br;
1729 struct ds results;
d295e8e9 1730
4f2cad2c
JP
1731 br = bridge_lookup(args);
1732 if (!br) {
1733 unixctl_command_reply(conn, 501, "Unknown bridge");
1734 return;
1735 }
1736
1737 ds_init(&results);
1738 ofproto_get_all_flows(br->ofproto, &results);
1739
1740 unixctl_command_reply(conn, 200, ds_cstr(&results));
1741 ds_destroy(&results);
1742}
1743
fa05809b
BP
1744/* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1745 * connections and reconnect. If BRIDGE is not specified, then all bridges
1746 * drop their controller connections and reconnect. */
1747static void
1748bridge_unixctl_reconnect(struct unixctl_conn *conn,
1749 const char *args, void *aux OVS_UNUSED)
1750{
1751 struct bridge *br;
1752 if (args[0] != '\0') {
1753 br = bridge_lookup(args);
1754 if (!br) {
1755 unixctl_command_reply(conn, 501, "Unknown bridge");
1756 return;
1757 }
1758 ofproto_reconnect_controllers(br->ofproto);
1759 } else {
4e8e4213 1760 LIST_FOR_EACH (br, node, &all_bridges) {
fa05809b
BP
1761 ofproto_reconnect_controllers(br->ofproto);
1762 }
1763 }
1764 unixctl_command_reply(conn, 200, NULL);
1765}
1766
064af421
BP
1767static int
1768bridge_run_one(struct bridge *br)
1769{
8052fb14 1770 struct port *port;
064af421
BP
1771 int error;
1772
1773 error = ofproto_run1(br->ofproto);
1774 if (error) {
1775 return error;
1776 }
1777
93dfc067 1778 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
21dcb94f 1779
8052fb14
BP
1780 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1781 port_run(port);
21dcb94f 1782 }
064af421
BP
1783
1784 error = ofproto_run2(br->ofproto, br->flush);
1785 br->flush = false;
1786
1787 return error;
1788}
1789
76ce9432 1790static size_t
1a048029 1791bridge_get_controllers(const struct bridge *br,
76ce9432 1792 struct ovsrec_controller ***controllersp)
064af421 1793{
76ce9432
BP
1794 struct ovsrec_controller **controllers;
1795 size_t n_controllers;
064af421 1796
1a048029
JP
1797 controllers = br->cfg->controller;
1798 n_controllers = br->cfg->n_controller;
76343538 1799
76ce9432
BP
1800 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1801 controllers = NULL;
1802 n_controllers = 0;
064af421 1803 }
76343538 1804
76ce9432
BP
1805 if (controllersp) {
1806 *controllersp = controllers;
1807 }
1808 return n_controllers;
064af421
BP
1809}
1810
1811static void
1a048029 1812bridge_reconfigure_one(struct bridge *br)
064af421 1813{
8052fb14 1814 enum ofproto_fail_mode fail_mode;
064af421 1815 struct svec snoops, old_snoops;
8052fb14 1816 struct port *port, *next;
76343538 1817 struct shash_node *node;
8052fb14 1818 struct shash new_ports;
6ae39834 1819 size_t i;
064af421 1820
064af421 1821 /* Collect new ports. */
76343538
BP
1822 shash_init(&new_ports);
1823 for (i = 0; i < br->cfg->n_ports; i++) {
1824 const char *name = br->cfg->ports[i]->name;
1825 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1826 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1827 br->name, name);
1828 }
1829 }
e073f944
BP
1830
1831 /* If we have a controller, then we need a local port. Complain if the
1832 * user didn't specify one.
1833 *
1834 * XXX perhaps we should synthesize a port ourselves in this case. */
1a048029 1835 if (bridge_get_controllers(br, NULL)) {
72865317
BP
1836 char local_name[IF_NAMESIZE];
1837 int error;
1838
1839 error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
1840 local_name, sizeof local_name);
e073f944
BP
1841 if (!error && !shash_find(&new_ports, local_name)) {
1842 VLOG_WARN("bridge %s: controller specified but no local port "
1843 "(port named %s) defined",
1844 br->name, local_name);
72865317 1845 }
064af421 1846 }
064af421 1847
4a1ee6ae
BP
1848 /* Get rid of deleted ports.
1849 * Get rid of deleted interfaces on ports that still exist. */
8052fb14 1850 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
4a1ee6ae
BP
1851 const struct ovsrec_port *port_cfg;
1852
8052fb14 1853 port_cfg = shash_find_data(&new_ports, port->name);
4a1ee6ae
BP
1854 if (!port_cfg) {
1855 port_destroy(port);
1856 } else {
1857 port_del_ifaces(port, port_cfg);
064af421
BP
1858 }
1859 }
4a1ee6ae
BP
1860
1861 /* Create new ports.
1862 * Add new interfaces to existing ports.
1863 * Reconfigure existing ports. */
76343538 1864 SHASH_FOR_EACH (node, &new_ports) {
8052fb14 1865 struct port *port = port_lookup(br, node->name);
76343538
BP
1866 if (!port) {
1867 port = port_create(br, node->name);
064af421 1868 }
ceb4559f 1869
76343538 1870 port_reconfigure(port, node->data);
ceb4559f
JG
1871 if (!port->n_ifaces) {
1872 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1873 br->name, port->name);
1874 port_destroy(port);
1875 }
064af421 1876 }
76343538 1877 shash_destroy(&new_ports);
064af421 1878
31681a5d
JP
1879 /* Set the fail-mode */
1880 fail_mode = !br->cfg->fail_mode
1881 || !strcmp(br->cfg->fail_mode, "standalone")
1882 ? OFPROTO_FAIL_STANDALONE
1883 : OFPROTO_FAIL_SECURE;
7d674866
BP
1884 if (ofproto_get_fail_mode(br->ofproto) != fail_mode
1885 && !ofproto_has_primary_controller(br->ofproto)) {
abdfe474
JP
1886 ofproto_flush_flows(br->ofproto);
1887 }
31681a5d
JP
1888 ofproto_set_fail_mode(br->ofproto, fail_mode);
1889
064af421
BP
1890 /* Delete all flows if we're switching from connected to standalone or vice
1891 * versa. (XXX Should we delete all flows if we are switching from one
1892 * controller to another?) */
1893
5a243150 1894 /* Configure OpenFlow controller connection snooping. */
76343538
BP
1895 svec_init(&snoops);
1896 svec_add_nocopy(&snoops, xasprintf("punix:%s/%s.snoop",
b43c6fe2 1897 ovs_rundir(), br->name));
76343538
BP
1898 svec_init(&old_snoops);
1899 ofproto_get_snoops(br->ofproto, &old_snoops);
1900 if (!svec_equal(&snoops, &old_snoops)) {
1901 ofproto_set_snoops(br->ofproto, &snoops);
1902 }
1903 svec_destroy(&snoops);
1904 svec_destroy(&old_snoops);
76343538 1905
064af421
BP
1906 mirror_reconfigure(br);
1907}
1908
7d674866
BP
1909/* Initializes 'oc' appropriately as a management service controller for
1910 * 'br'.
1911 *
1912 * The caller must free oc->target when it is no longer needed. */
1913static void
1914bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1915 struct ofproto_controller *oc)
1916{
b43c6fe2 1917 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
7d674866
BP
1918 oc->max_backoff = 0;
1919 oc->probe_interval = 60;
1920 oc->band = OFPROTO_OUT_OF_BAND;
7d674866
BP
1921 oc->rate_limit = 0;
1922 oc->burst_limit = 0;
1923}
1924
1925/* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1926static void
1927bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1928 struct ofproto_controller *oc)
1929{
1930 oc->target = c->target;
1931 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1932 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1933 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1934 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
7d674866
BP
1935 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1936 oc->burst_limit = (c->controller_burst_limit
1937 ? *c->controller_burst_limit : 0);
1938}
1939
1940/* Configures the IP stack for 'br''s local interface properly according to the
1941 * configuration in 'c'. */
1942static void
1943bridge_configure_local_iface_netdev(struct bridge *br,
1944 struct ovsrec_controller *c)
1945{
1946 struct netdev *netdev;
1947 struct in_addr mask, gateway;
1948
1949 struct iface *local_iface;
1950 struct in_addr ip;
1951
7d674866 1952 /* If there's no local interface or no IP address, give up. */
27b3a6e0 1953 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
7d674866
BP
1954 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1955 return;
1956 }
1957
1958 /* Bring up the local interface. */
1959 netdev = local_iface->netdev;
1960 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1961
1962 /* Configure the IP address and netmask. */
1963 if (!c->local_netmask
1964 || !inet_aton(c->local_netmask, &mask)
1965 || !mask.s_addr) {
1966 mask.s_addr = guess_netmask(ip.s_addr);
1967 }
1968 if (!netdev_set_in4(netdev, ip, mask)) {
1969 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1970 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1971 }
1972
1973 /* Configure the default gateway. */
1974 if (c->local_gateway
1975 && inet_aton(c->local_gateway, &gateway)
1976 && gateway.s_addr) {
1977 if (!netdev_add_router(netdev, gateway)) {
1978 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1979 br->name, IP_ARGS(&gateway.s_addr));
1980 }
1981 }
1982}
1983
064af421 1984static void
1a048029 1985bridge_reconfigure_remotes(struct bridge *br,
11bbff1b
BP
1986 const struct sockaddr_in *managers,
1987 size_t n_managers)
064af421 1988{
b1da6250
BP
1989 const char *disable_ib_str, *queue_id_str;
1990 bool disable_in_band = false;
1991 int queue_id;
1992
76ce9432
BP
1993 struct ovsrec_controller **controllers;
1994 size_t n_controllers;
7d674866
BP
1995 bool had_primary;
1996
1997 struct ofproto_controller *ocs;
1998 size_t n_ocs;
1999 size_t i;
064af421 2000
8731b2b6
JP
2001 /* Check if we should disable in-band control on this bridge. */
2002 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
2003 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
2004 disable_in_band = true;
2005 }
2006
b1da6250
BP
2007 /* Set OpenFlow queue ID for in-band control. */
2008 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
2009 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
2010 ofproto_set_in_band_queue(br->ofproto, queue_id);
2011
8731b2b6
JP
2012 if (disable_in_band) {
2013 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
2014 } else {
2015 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
2016 }
7d674866 2017 had_primary = ofproto_has_primary_controller(br->ofproto);
cd11000b 2018
1a048029 2019 n_controllers = bridge_get_controllers(br, &controllers);
064af421 2020
7d674866
BP
2021 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
2022 n_ocs = 0;
064af421 2023
7d674866
BP
2024 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
2025 for (i = 0; i < n_controllers; i++) {
2026 struct ovsrec_controller *c = controllers[i];
76ce9432 2027
7d674866
BP
2028 if (!strncmp(c->target, "punix:", 6)
2029 || !strncmp(c->target, "unix:", 5)) {
2030 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
76ce9432 2031
7d674866
BP
2032 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
2033 * domain sockets and overwriting arbitrary local files. */
2034 VLOG_ERR_RL(&rl, "%s: not adding Unix domain socket controller "
2035 "\"%s\" due to possibility for remote exploit",
2036 dpif_name(br->dpif), c->target);
2037 continue;
2038 }
76ce9432 2039
7d674866 2040 bridge_configure_local_iface_netdev(br, c);
8731b2b6
JP
2041 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
2042 if (disable_in_band) {
2043 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
2044 }
2045 n_ocs++;
7d674866 2046 }
76ce9432 2047
7d674866
BP
2048 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
2049 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
2050 free(ocs);
064af421 2051
7d674866
BP
2052 if (had_primary != ofproto_has_primary_controller(br->ofproto)) {
2053 ofproto_flush_flows(br->ofproto);
2054 }
76ce9432 2055
7d674866
BP
2056 /* If there are no controllers and the bridge is in standalone
2057 * mode, set up a flow that matches every packet and directs
2058 * them to OFPP_NORMAL (which goes to us). Otherwise, the
2059 * switch is in secure mode and we won't pass any traffic until
2060 * a controller has been defined and it tells us to do so. */
2061 if (!n_controllers
2062 && ofproto_get_fail_mode(br->ofproto) == OFPROTO_FAIL_STANDALONE) {
2063 union ofp_action action;
cf3fad8a 2064 struct cls_rule rule;
76ce9432 2065
7d674866
BP
2066 memset(&action, 0, sizeof action);
2067 action.type = htons(OFPAT_OUTPUT);
2068 action.output.len = htons(sizeof action);
2069 action.output.port = htons(OFPP_NORMAL);
cf3fad8a 2070 cls_rule_init_catchall(&rule, 0);
fa8b054f 2071 ofproto_add_flow(br->ofproto, &rule, &action, 1);
064af421 2072 }
064af421
BP
2073}
2074
2075static void
76343538 2076bridge_get_all_ifaces(const struct bridge *br, struct shash *ifaces)
064af421 2077{
8052fb14 2078 struct port *port;
064af421 2079
76343538 2080 shash_init(ifaces);
8052fb14 2081 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968
BP
2082 struct iface *iface;
2083
2084 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
76343538 2085 shash_add_once(ifaces, iface->name, iface);
064af421 2086 }
76343538
BP
2087 if (port->n_ifaces > 1 && port->cfg->bond_fake_iface) {
2088 shash_add_once(ifaces, port->name, NULL);
35c979bf 2089 }
064af421 2090 }
064af421
BP
2091}
2092
2093/* For robustness, in case the administrator moves around datapath ports behind
2094 * our back, we re-check all the datapath port numbers here.
2095 *
2096 * This function will set the 'dp_ifidx' members of interfaces that have
2097 * disappeared to -1, so only call this function from a context where those
2098 * 'struct iface's will be removed from the bridge. Otherwise, the -1
2099 * 'dp_ifidx'es will cause trouble later when we try to send them to the
2100 * datapath, which doesn't support UINT16_MAX+1 ports. */
2101static void
2102bridge_fetch_dp_ifaces(struct bridge *br)
2103{
b0ec0f27 2104 struct dpif_port_dump dump;
4c738a8d 2105 struct dpif_port dpif_port;
8052fb14 2106 struct port *port;
064af421
BP
2107
2108 /* Reset all interface numbers. */
8052fb14 2109 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968
BP
2110 struct iface *iface;
2111
2112 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
064af421
BP
2113 iface->dp_ifidx = -1;
2114 }
2115 }
d9a8717a 2116 hmap_clear(&br->ifaces);
064af421 2117
4c738a8d
BP
2118 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
2119 struct iface *iface = iface_lookup(br, dpif_port.name);
064af421
BP
2120 if (iface) {
2121 if (iface->dp_ifidx >= 0) {
b29ba128 2122 VLOG_WARN("%s reported interface %s twice",
4c738a8d
BP
2123 dpif_name(br->dpif), dpif_port.name);
2124 } else if (iface_from_dp_ifidx(br, dpif_port.port_no)) {
b29ba128 2125 VLOG_WARN("%s reported interface %"PRIu16" twice",
4c738a8d 2126 dpif_name(br->dpif), dpif_port.port_no);
064af421 2127 } else {
4c738a8d 2128 iface->dp_ifidx = dpif_port.port_no;
d9a8717a
BP
2129 hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
2130 hash_int(iface->dp_ifidx, 0));
064af421 2131 }
093e47f4 2132
bcd49a45
BP
2133 iface_set_ofport(iface->cfg,
2134 (iface->dp_ifidx >= 0
2135 ? odp_port_to_ofp_port(iface->dp_ifidx)
2136 : -1));
064af421
BP
2137 }
2138 }
064af421
BP
2139}
2140\f
2141/* Bridge packet processing functions. */
2142
9f5073d8
EJ
2143static bool
2144bond_is_tcp_hash(const struct port *port)
2145{
6aa74308 2146 return port->bond_mode == BM_TCP && lacp_negotiated(port->lacp);
9f5073d8
EJ
2147}
2148
da285df4 2149static int
9f5073d8 2150bond_hash_src(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
da285df4 2151{
e58de0e3 2152 return hash_bytes(mac, ETH_ADDR_LEN, vlan) & BOND_MASK;
da285df4
BP
2153}
2154
9f5073d8
EJ
2155static int bond_hash_tcp(const struct flow *flow, uint16_t vlan)
2156{
2157 struct flow hash_flow;
2158
2159 memcpy(&hash_flow, flow, sizeof hash_flow);
2160 hash_flow.vlan_tci = 0;
2161
2162 /* The symmetric quality of this hash function is not required, but
2163 * flow_hash_symmetric_l4 already exists, and is sufficient for our
2164 * purposes, so we use it out of convenience. */
2165 return flow_hash_symmetric_l4(&hash_flow, vlan) & BOND_MASK;
2166}
2167
064af421 2168static struct bond_entry *
9f5073d8 2169lookup_bond_entry(const struct port *port, const struct flow *flow,
e58de0e3 2170 uint16_t vlan)
064af421 2171{
9f5073d8
EJ
2172 assert(port->bond_mode != BM_AB);
2173
2174 if (bond_is_tcp_hash(port)) {
2175 return &port->bond_hash[bond_hash_tcp(flow, vlan)];
2176 } else {
2177 return &port->bond_hash[bond_hash_src(flow->dl_src, vlan)];
2178 }
064af421
BP
2179}
2180
990cda85 2181static struct iface *
064af421
BP
2182bond_choose_iface(const struct port *port)
2183{
8b2a2f4a 2184 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
990cda85 2185 struct iface *best_down_slave;
83db7968 2186 struct iface *iface;
8b2a2f4a 2187
990cda85 2188 best_down_slave = NULL;
83db7968 2189 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
8b2a2f4a 2190 if (iface->enabled) {
990cda85
BP
2191 return iface;
2192 } else if ((!best_down_slave
2193 || iface->delay_expires < best_down_slave->delay_expires)
6aa74308 2194 && lacp_slave_may_enable(port->lacp, iface)) {
990cda85 2195 best_down_slave = iface;
064af421
BP
2196 }
2197 }
8b2a2f4a 2198
990cda85 2199 if (best_down_slave) {
8b2a2f4a 2200 VLOG_INFO_RL(&rl, "interface %s: skipping remaining %lli ms updelay "
990cda85
BP
2201 "since no other interface is up",
2202 best_down_slave->name,
2203 best_down_slave->delay_expires - time_msec());
2204 bond_enable_slave(best_down_slave, true);
8b2a2f4a
JG
2205 }
2206
2207 return best_down_slave;
064af421
BP
2208}
2209
2210static bool
9f5073d8 2211choose_output_iface(const struct port *port, const struct flow *flow,
e58de0e3 2212 uint16_t vlan, uint16_t *dp_ifidx, tag_type *tags)
064af421
BP
2213{
2214 struct iface *iface;
2215
2216 assert(port->n_ifaces);
2217 if (port->n_ifaces == 1) {
83db7968 2218 iface = port_get_an_iface(port);
27dcaa1a 2219 } else if (port->bond_mode == BM_AB) {
990cda85
BP
2220 iface = port->active_iface;
2221 if (!iface) {
be02e7c3
EJ
2222 *tags |= port->no_ifaces_tag;
2223 return false;
2224 }
9f5073d8
EJ
2225 } else {
2226 struct bond_entry *e = lookup_bond_entry(port, flow, vlan);
c17f0d5e 2227 if (!e->iface || !e->iface->enabled) {
064af421
BP
2228 /* XXX select interface properly. The current interface selection
2229 * is only good for testing the rebalancing code. */
c17f0d5e
BP
2230 e->iface = bond_choose_iface(port);
2231 if (!e->iface) {
064af421
BP
2232 *tags |= port->no_ifaces_tag;
2233 return false;
2234 }
c0cb6f59 2235 e->tag = tag_create_random();
064af421 2236 }
c0cb6f59 2237 *tags |= e->tag;
c17f0d5e 2238 iface = e->iface;
064af421
BP
2239 }
2240 *dp_ifidx = iface->dp_ifidx;
2241 *tags |= iface->tag; /* Currently only used for bonding. */
2242 return true;
2243}
2244
2245static void
c835c3bf 2246bond_link_status_update(struct iface *iface)
064af421
BP
2247{
2248 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
2249 struct port *port = iface->port;
6aa74308 2250 bool up = iface->up && lacp_slave_may_enable(port->lacp, iface);
c25c91fd
EJ
2251 int updelay, downdelay;
2252
2253 updelay = port->updelay;
2254 downdelay = port->downdelay;
2255
6aa74308 2256 if (lacp_negotiated(port->lacp)) {
c25c91fd
EJ
2257 downdelay = 0;
2258 updelay = 0;
2259 }
2260
c25c91fd 2261 if ((up == iface->enabled) == (iface->delay_expires == LLONG_MAX)) {
064af421
BP
2262 /* Nothing to do. */
2263 return;
2264 }
63331829 2265 VLOG_INFO_RL(&rl, "interface %s: link state %s",
c25c91fd
EJ
2266 iface->name, up ? "up" : "down");
2267 if (up == iface->enabled) {
064af421
BP
2268 iface->delay_expires = LLONG_MAX;
2269 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
c25c91fd 2270 iface->name, up ? "disabled" : "enabled");
990cda85 2271 } else if (up && !port->active_iface) {
8b2a2f4a 2272 bond_enable_slave(iface, true);
c25c91fd 2273 if (updelay) {
8b2a2f4a 2274 VLOG_INFO_RL(&rl, "interface %s: skipping %d ms updelay since no "
c25c91fd 2275 "other interface is up", iface->name, updelay);
8b2a2f4a 2276 }
064af421 2277 } else {
c25c91fd 2278 int delay = up ? updelay : downdelay;
064af421
BP
2279 iface->delay_expires = time_msec() + delay;
2280 if (delay) {
2281 VLOG_INFO_RL(&rl,
2282 "interface %s: will be %s if it stays %s for %d ms",
2283 iface->name,
c25c91fd
EJ
2284 up ? "enabled" : "disabled",
2285 up ? "up" : "down",
064af421
BP
2286 delay);
2287 }
2288 }
2289}
2290
2291static void
2292bond_choose_active_iface(struct port *port)
2293{
2294 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
2295
2296 port->active_iface = bond_choose_iface(port);
990cda85 2297 if (port->active_iface) {
064af421 2298 VLOG_INFO_RL(&rl, "port %s: active interface is now %s",
990cda85 2299 port->name, port->active_iface->name);
064af421
BP
2300 } else {
2301 VLOG_WARN_RL(&rl, "port %s: all ports disabled, no active interface",
2302 port->name);
2303 }
2304}
2305
da285df4
BP
2306static void
2307bond_enable_slave(struct iface *iface, bool enable)
2308{
2309 struct port *port = iface->port;
2310 struct bridge *br = port->bridge;
2311
8b2a2f4a
JG
2312 /* This acts as a recursion check. If the act of disabling a slave
2313 * causes a different slave to be enabled, the flag will allow us to
2314 * skip redundant work when we reenter this function. It must be
2315 * cleared on exit to keep things safe with multiple bonds. */
2316 static bool moving_active_iface = false;
2317
da285df4
BP
2318 iface->delay_expires = LLONG_MAX;
2319 if (enable == iface->enabled) {
2320 return;
2321 }
2322
2323 iface->enabled = enable;
2324 if (!iface->enabled) {
17ea75b2 2325 VLOG_WARN("interface %s: disabled", iface->name);
da285df4 2326 ofproto_revalidate(br->ofproto, iface->tag);
990cda85 2327 if (iface == port->active_iface) {
8b2a2f4a
JG
2328 /* Disabling a slave can lead to another slave being immediately
2329 * enabled if there will be no active slaves but one is waiting
2330 * on an updelay. In this case we do not need to run most of the
2331 * code for the newly enabled slave since there was no period
2332 * without an active slave and it is redundant with the disabling
2333 * path. */
2334 moving_active_iface = true;
da285df4
BP
2335 bond_choose_active_iface(port);
2336 }
2337 bond_send_learning_packets(port);
2338 } else {
17ea75b2 2339 VLOG_WARN("interface %s: enabled", iface->name);
990cda85 2340 if (!port->active_iface && !moving_active_iface) {
da285df4
BP
2341 ofproto_revalidate(br->ofproto, port->no_ifaces_tag);
2342 bond_choose_active_iface(port);
2343 bond_send_learning_packets(port);
2344 }
2345 iface->tag = tag_create_random();
2346 }
8b2a2f4a
JG
2347
2348 moving_active_iface = false;
da285df4
BP
2349}
2350
8722022c
BP
2351/* Attempts to make the sum of the bond slaves' statistics appear on the fake
2352 * bond interface. */
2353static void
2354bond_update_fake_iface_stats(struct port *port)
2355{
2356 struct netdev_stats bond_stats;
2357 struct netdev *bond_dev;
83db7968 2358 struct iface *iface;
8722022c
BP
2359
2360 memset(&bond_stats, 0, sizeof bond_stats);
2361
83db7968 2362 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
8722022c
BP
2363 struct netdev_stats slave_stats;
2364
83db7968 2365 if (!netdev_get_stats(iface->netdev, &slave_stats)) {
f4b6076a
JG
2366 /* XXX: We swap the stats here because they are swapped back when
2367 * reported by the internal device. The reason for this is
2368 * internal devices normally represent packets going into the system
2369 * but when used as fake bond device they represent packets leaving
2370 * the system. We really should do this in the internal device
2371 * itself because changing it here reverses the counts from the
2372 * perspective of the switch. However, the internal device doesn't
2373 * know what type of device it represents so we have to do it here
2374 * for now. */
2375 bond_stats.tx_packets += slave_stats.rx_packets;
2376 bond_stats.tx_bytes += slave_stats.rx_bytes;
2377 bond_stats.rx_packets += slave_stats.tx_packets;
2378 bond_stats.rx_bytes += slave_stats.tx_bytes;
8722022c
BP
2379 }
2380 }
2381
2382 if (!netdev_open_default(port->name, &bond_dev)) {
2383 netdev_set_stats(bond_dev, &bond_stats);
2384 netdev_close(bond_dev);
2385 }
2386}
2387
064af421 2388static void
21dcb94f 2389bond_run(struct port *port)
064af421 2390{
83db7968 2391 struct iface *iface;
0bb5c3ec 2392
21dcb94f
EJ
2393 if (port->n_ifaces < 2) {
2394 return;
2395 }
0bb5c3ec 2396
83db7968
BP
2397 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2398 bond_link_status_update(iface);
21dcb94f 2399 }
8722022c 2400
83db7968 2401 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
21dcb94f
EJ
2402 if (time_msec() >= iface->delay_expires) {
2403 bond_enable_slave(iface, !iface->enabled);
8b2a2f4a 2404 }
064af421 2405 }
21dcb94f
EJ
2406
2407 if (port->bond_fake_iface
2408 && time_msec() >= port->bond_next_fake_iface_update) {
2409 bond_update_fake_iface_stats(port);
2410 port->bond_next_fake_iface_update = time_msec() + 1000;
2411 }
064af421
BP
2412}
2413
2414static void
21dcb94f 2415bond_wait(struct port *port)
064af421 2416{
83db7968 2417 struct iface *iface;
064af421 2418
21dcb94f
EJ
2419 if (port->n_ifaces < 2) {
2420 return;
2421 }
63331829 2422
83db7968 2423 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
21dcb94f
EJ
2424 if (iface->delay_expires != LLONG_MAX) {
2425 poll_timer_wait_until(iface->delay_expires);
8722022c 2426 }
064af421 2427 }
21dcb94f
EJ
2428
2429 if (port->bond_fake_iface) {
2430 poll_timer_wait_until(port->bond_next_fake_iface_update);
2431 }
064af421
BP
2432}
2433
2434static bool
0c62a7ad 2435set_dst(struct dst *dst, const struct flow *flow,
064af421
BP
2436 const struct port *in_port, const struct port *out_port,
2437 tag_type *tags)
2438{
0c62a7ad 2439 dst->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
064af421 2440 : in_port->vlan >= 0 ? in_port->vlan
66642cb4
BP
2441 : flow->vlan_tci == 0 ? OFP_VLAN_NONE
2442 : vlan_tci_to_vid(flow->vlan_tci));
9f5073d8 2443 return choose_output_iface(out_port, flow, dst->vlan,
0c62a7ad 2444 &dst->dp_ifidx, tags);
064af421
BP
2445}
2446
2447static void
2448swap_dst(struct dst *p, struct dst *q)
2449{
2450 struct dst tmp = *p;
2451 *p = *q;
2452 *q = tmp;
2453}
2454
2455/* Moves all the dsts with vlan == 'vlan' to the front of the 'n_dsts' in
2456 * 'dsts'. (This may help performance by reducing the number of VLAN changes
2457 * that we push to the datapath. We could in fact fully sort the array by
2458 * vlan, but in most cases there are at most two different vlan tags so that's
2459 * possibly overkill.) */
2460static void
0c62a7ad 2461partition_dsts(struct dst_set *set, int vlan)
064af421 2462{
0c62a7ad
BP
2463 struct dst *first = set->dsts;
2464 struct dst *last = set->dsts + set->n;
064af421
BP
2465
2466 while (first != last) {
2467 /* Invariants:
2468 * - All dsts < first have vlan == 'vlan'.
2469 * - All dsts >= last have vlan != 'vlan'.
2470 * - first < last. */
2471 while (first->vlan == vlan) {
2472 if (++first == last) {
2473 return;
2474 }
2475 }
2476
2477 /* Same invariants, plus one additional:
2478 * - first->vlan != vlan.
2479 */
2480 while (last[-1].vlan != vlan) {
2481 if (--last == first) {
2482 return;
2483 }
2484 }
2485
2486 /* Same invariants, plus one additional:
2487 * - last[-1].vlan == vlan.*/
2488 swap_dst(first++, --last);
2489 }
2490}
2491
2492static int
2493mirror_mask_ffs(mirror_mask_t mask)
2494{
2495 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2496 return ffs(mask);
2497}
2498
0c62a7ad
BP
2499static void
2500dst_set_init(struct dst_set *set)
2501{
2502 set->dsts = set->builtin;
2503 set->n = 0;
2504 set->allocated = ARRAY_SIZE(set->builtin);
2505}
2506
2507static void
2508dst_set_add(struct dst_set *set, const struct dst *dst)
2509{
2510 if (set->n >= set->allocated) {
2511 size_t new_allocated;
2512 struct dst *new_dsts;
2513
2514 new_allocated = set->allocated * 2;
2515 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2516 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2517
2518 dst_set_free(set);
2519
2520 set->dsts = new_dsts;
2521 set->allocated = new_allocated;
2522 }
2523 set->dsts[set->n++] = *dst;
2524}
2525
2526static void
2527dst_set_free(struct dst_set *set)
2528{
2529 if (set->dsts != set->builtin) {
2530 free(set->dsts);
2531 }
2532}
2533
064af421 2534static bool
0c62a7ad 2535dst_is_duplicate(const struct dst_set *set, const struct dst *test)
064af421
BP
2536{
2537 size_t i;
0c62a7ad
BP
2538 for (i = 0; i < set->n; i++) {
2539 if (set->dsts[i].vlan == test->vlan
2540 && set->dsts[i].dp_ifidx == test->dp_ifidx) {
064af421
BP
2541 return true;
2542 }
2543 }
2544 return false;
2545}
2546
2547static bool
2548port_trunks_vlan(const struct port *port, uint16_t vlan)
2549{
3e9c481c
BP
2550 return (port->vlan < 0
2551 && (!port->trunks || bitmap_is_set(port->trunks, vlan)));
064af421
BP
2552}
2553
2554static bool
2555port_includes_vlan(const struct port *port, uint16_t vlan)
2556{
2557 return vlan == port->vlan || port_trunks_vlan(port, vlan);
2558}
2559
a4e2e1f2
EJ
2560static bool
2561port_is_floodable(const struct port *port)
2562{
83db7968 2563 struct iface *iface;
a4e2e1f2 2564
83db7968 2565 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
a4e2e1f2 2566 if (!ofproto_port_is_floodable(port->bridge->ofproto,
83db7968 2567 iface->dp_ifidx)) {
a4e2e1f2
EJ
2568 return false;
2569 }
2570 }
2571 return true;
2572}
2573
7e559ecf
BP
2574/* Returns the tag for 'port''s active iface, or 'port''s no_ifaces_tag if
2575 * there is no active iface. */
2576static tag_type
2577port_get_active_iface_tag(const struct port *port)
2578{
990cda85
BP
2579 return (port->active_iface
2580 ? port->active_iface->tag
7e559ecf
BP
2581 : port->no_ifaces_tag);
2582}
2583
83db7968
BP
2584/* Returns an arbitrary interface within 'port'.
2585 *
2586 * 'port' must have at least one interface. */
2587static struct iface *
2588port_get_an_iface(const struct port *port)
2589{
2590 return CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
2591}
2592
0c62a7ad 2593static void
ae412e7d 2594compose_dsts(const struct bridge *br, const struct flow *flow, uint16_t vlan,
064af421 2595 const struct port *in_port, const struct port *out_port,
0c62a7ad 2596 struct dst_set *set, tag_type *tags, uint16_t *nf_output_iface)
064af421
BP
2597{
2598 mirror_mask_t mirrors = in_port->src_mirrors;
0c62a7ad 2599 struct dst dst;
66642cb4 2600 int flow_vlan;
064af421 2601
66642cb4
BP
2602 flow_vlan = vlan_tci_to_vid(flow->vlan_tci);
2603 if (flow_vlan == 0) {
2604 flow_vlan = OFP_VLAN_NONE;
2605 }
2606
064af421 2607 if (out_port == FLOOD_PORT) {
8052fb14
BP
2608 struct port *port;
2609
2610 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
a4e2e1f2
EJ
2611 if (port != in_port
2612 && port_is_floodable(port)
2613 && port_includes_vlan(port, vlan)
064af421 2614 && !port->is_mirror_output_port
0c62a7ad 2615 && set_dst(&dst, flow, in_port, port, tags)) {
064af421 2616 mirrors |= port->dst_mirrors;
0c62a7ad 2617 dst_set_add(set, &dst);
064af421
BP
2618 }
2619 }
6a07af36 2620 *nf_output_iface = NF_OUT_FLOOD;
0c62a7ad
BP
2621 } else if (out_port && set_dst(&dst, flow, in_port, out_port, tags)) {
2622 dst_set_add(set, &dst);
2623 *nf_output_iface = dst.dp_ifidx;
064af421 2624 mirrors |= out_port->dst_mirrors;
064af421
BP
2625 }
2626
2627 while (mirrors) {
2628 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
2629 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
2630 if (m->out_port) {
0c62a7ad
BP
2631 if (set_dst(&dst, flow, in_port, m->out_port, tags)
2632 && !dst_is_duplicate(set, &dst)) {
2633 dst_set_add(set, &dst);
064af421
BP
2634 }
2635 } else {
8052fb14
BP
2636 struct port *port;
2637
2638 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
064af421 2639 if (port_includes_vlan(port, m->out_vlan)
0c62a7ad 2640 && set_dst(&dst, flow, in_port, port, tags))
064af421
BP
2641 {
2642 if (port->vlan < 0) {
0c62a7ad 2643 dst.vlan = m->out_vlan;
064af421 2644 }
0c62a7ad 2645 if (dst_is_duplicate(set, &dst)) {
274de4d2
BP
2646 continue;
2647 }
43aa5f47
JG
2648
2649 /* Use the vlan tag on the original flow instead of
2650 * the one passed in the vlan parameter. This ensures
2651 * that we compare the vlan from before any implicit
2652 * tagging tags place. This is necessary because
2653 * dst->vlan is the final vlan, after removing implicit
2654 * tags. */
0c62a7ad 2655 if (port == in_port && dst.vlan == flow_vlan) {
064af421
BP
2656 /* Don't send out input port on same VLAN. */
2657 continue;
2658 }
0c62a7ad 2659 dst_set_add(set, &dst);
064af421
BP
2660 }
2661 }
2662 }
2663 }
2664 mirrors &= mirrors - 1;
2665 }
2666
0c62a7ad 2667 partition_dsts(set, flow_vlan);
064af421
BP
2668}
2669
67a4917b 2670static void OVS_UNUSED
0c62a7ad 2671print_dsts(const struct dst_set *set)
064af421 2672{
0c62a7ad
BP
2673 size_t i;
2674
2675 for (i = 0; i < set->n; i++) {
2676 const struct dst *dst = &set->dsts[i];
2677
2678 printf(">p%"PRIu16, dst->dp_ifidx);
2679 if (dst->vlan != OFP_VLAN_NONE) {
2680 printf("v%"PRIu16, dst->vlan);
064af421
BP
2681 }
2682 }
2683}
2684
2685static void
ae412e7d 2686compose_actions(struct bridge *br, const struct flow *flow, uint16_t vlan,
064af421 2687 const struct port *in_port, const struct port *out_port,
cdee00fd 2688 tag_type *tags, struct ofpbuf *actions,
6a07af36 2689 uint16_t *nf_output_iface)
064af421 2690{
0c62a7ad 2691 struct dst_set set;
064af421 2692 uint16_t cur_vlan;
0c62a7ad 2693 size_t i;
064af421 2694
0c62a7ad
BP
2695 dst_set_init(&set);
2696 compose_dsts(br, flow, vlan, in_port, out_port, &set, tags,
2697 nf_output_iface);
064af421 2698
66642cb4
BP
2699 cur_vlan = vlan_tci_to_vid(flow->vlan_tci);
2700 if (cur_vlan == 0) {
2701 cur_vlan = OFP_VLAN_NONE;
2702 }
0c62a7ad
BP
2703 for (i = 0; i < set.n; i++) {
2704 const struct dst *dst = &set.dsts[i];
2705 if (dst->vlan != cur_vlan) {
2706 if (dst->vlan == OFP_VLAN_NONE) {
7aec165d 2707 nl_msg_put_flag(actions, ODP_ACTION_ATTR_STRIP_VLAN);
064af421 2708 } else {
cdee00fd 2709 ovs_be16 tci;
0c62a7ad 2710 tci = htons(dst->vlan & VLAN_VID_MASK);
cdee00fd 2711 tci |= flow->vlan_tci & htons(VLAN_PCP_MASK);
7aec165d 2712 nl_msg_put_be16(actions, ODP_ACTION_ATTR_SET_DL_TCI, tci);
064af421 2713 }
0c62a7ad 2714 cur_vlan = dst->vlan;
064af421 2715 }
7aec165d 2716 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->dp_ifidx);
064af421 2717 }
0c62a7ad 2718 dst_set_free(&set);
064af421
BP
2719}
2720
e96a4d80
JG
2721/* Returns the effective vlan of a packet, taking into account both the
2722 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2723 * the packet is untagged and -1 indicates it has an invalid header and
2724 * should be dropped. */
ae412e7d 2725static int flow_get_vlan(struct bridge *br, const struct flow *flow,
e96a4d80
JG
2726 struct port *in_port, bool have_packet)
2727{
66642cb4 2728 int vlan = vlan_tci_to_vid(flow->vlan_tci);
e96a4d80
JG
2729 if (in_port->vlan >= 0) {
2730 if (vlan) {
2731 /* XXX support double tagging? */
2732 if (have_packet) {
2733 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
66642cb4 2734 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
e96a4d80
JG
2735 "packet received on port %s configured with "
2736 "implicit VLAN %"PRIu16,
66642cb4 2737 br->name, vlan, in_port->name, in_port->vlan);
e96a4d80
JG
2738 }
2739 return -1;
2740 }
2741 vlan = in_port->vlan;
2742 } else {
2743 if (!port_includes_vlan(in_port, vlan)) {
2744 if (have_packet) {
2745 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2746 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2747 "packet received on port %s not configured for "
2748 "trunking VLAN %d",
2749 br->name, vlan, in_port->name, vlan);
2750 }
2751 return -1;
2752 }
2753 }
2754
2755 return vlan;
2756}
2757
7febb910
JG
2758/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2759 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2760 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2761static bool
ae412e7d 2762is_gratuitous_arp(const struct flow *flow)
7febb910
JG
2763{
2764 return (flow->dl_type == htons(ETH_TYPE_ARP)
2765 && eth_addr_is_broadcast(flow->dl_dst)
2766 && (flow->nw_proto == ARP_OP_REPLY
2767 || (flow->nw_proto == ARP_OP_REQUEST
2768 && flow->nw_src == flow->nw_dst)));
2769}
2770
e96a4d80 2771static void
ae412e7d 2772update_learning_table(struct bridge *br, const struct flow *flow, int vlan,
e96a4d80
JG
2773 struct port *in_port)
2774{
db8077c3
BP
2775 struct mac_entry *mac;
2776
2777 if (!mac_learning_may_learn(br->ml, flow->dl_src, vlan)) {
2778 return;
2779 }
2780
2781 mac = mac_learning_insert(br->ml, flow->dl_src, vlan);
2782 if (is_gratuitous_arp(flow)) {
2783 /* We don't want to learn from gratuitous ARP packets that are
2784 * reflected back over bond slaves so we lock the learning table. */
2785 if (in_port->n_ifaces == 1) {
2786 mac_entry_set_grat_arp_lock(mac);
2787 } else if (mac_entry_is_grat_arp_locked(mac)) {
2788 return;
2789 }
2790 }
2791
1648ddd7 2792 if (mac_entry_is_new(mac) || mac->port.p != in_port) {
e96a4d80
JG
2793 /* The log messages here could actually be useful in debugging,
2794 * so keep the rate limit relatively high. */
db8077c3 2795 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
e96a4d80
JG
2796 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2797 "on port %s in VLAN %d",
2798 br->name, ETH_ADDR_ARGS(flow->dl_src),
2799 in_port->name, vlan);
db8077c3 2800
1648ddd7 2801 mac->port.p = in_port;
db8077c3 2802 ofproto_revalidate(br->ofproto, mac_learning_changed(br->ml, mac));
e96a4d80
JG
2803 }
2804}
2805
14a34d00
BP
2806/* Determines whether packets in 'flow' within 'br' should be forwarded or
2807 * dropped. Returns true if they may be forwarded, false if they should be
2808 * dropped.
2809 *
2810 * If 'have_packet' is true, it indicates that the caller is processing a
2811 * received packet. If 'have_packet' is false, then the caller is just
2812 * revalidating an existing flow because configuration has changed. Either
2813 * way, 'have_packet' only affects logging (there is no point in logging errors
2814 * during revalidation).
2815 *
2816 * Sets '*in_portp' to the input port. This will be a null pointer if
2817 * flow->in_port does not designate a known input port (in which case
2818 * is_admissible() returns false).
2819 *
2820 * When returning true, sets '*vlanp' to the effective VLAN of the input
2821 * packet, as returned by flow_get_vlan().
2822 *
2823 * May also add tags to '*tags', although the current implementation only does
2824 * so in one special case.
2825 */
064af421 2826static bool
ae412e7d 2827is_admissible(struct bridge *br, const struct flow *flow, bool have_packet,
14a34d00 2828 tag_type *tags, int *vlanp, struct port **in_portp)
064af421
BP
2829{
2830 struct iface *in_iface;
2831 struct port *in_port;
064af421
BP
2832 int vlan;
2833
2834 /* Find the interface and port structure for the received packet. */
2835 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2836 if (!in_iface) {
2837 /* No interface? Something fishy... */
14a34d00 2838 if (have_packet) {
064af421
BP
2839 /* Odd. A few possible reasons here:
2840 *
2841 * - We deleted an interface but there are still a few packets
2842 * queued up from it.
2843 *
2844 * - Someone externally added an interface (e.g. with "ovs-dpctl
2845 * add-if") that we don't know about.
2846 *
2847 * - Packet arrived on the local port but the local port is not
2848 * one of our bridge ports.
2849 */
2850 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2851
2852 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
d295e8e9 2853 "interface %"PRIu16, br->name, flow->in_port);
064af421
BP
2854 }
2855
14a34d00
BP
2856 *in_portp = NULL;
2857 return false;
064af421 2858 }
14a34d00
BP
2859 *in_portp = in_port = in_iface->port;
2860 *vlanp = vlan = flow_get_vlan(br, flow, in_port, have_packet);
e96a4d80 2861 if (vlan < 0) {
14a34d00 2862 return false;
064af421
BP
2863 }
2864
064af421
BP
2865 /* Drop frames for reserved multicast addresses. */
2866 if (eth_addr_is_reserved(flow->dl_dst)) {
14a34d00 2867 return false;
064af421
BP
2868 }
2869
2870 /* Drop frames on ports reserved for mirroring. */
2871 if (in_port->is_mirror_output_port) {
14a34d00 2872 if (have_packet) {
f925807d
BP
2873 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2874 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2875 "%s, which is reserved exclusively for mirroring",
2876 br->name, in_port->name);
2877 }
14a34d00 2878 return false;
064af421
BP
2879 }
2880
c25c91fd 2881 /* When using LACP, do not accept packets from disabled interfaces. */
6aa74308 2882 if (lacp_negotiated(in_port->lacp) && !in_iface->enabled) {
c25c91fd
EJ
2883 return false;
2884 }
2885
2886 /* Packets received on non-LACP bonds need special attention to avoid
2887 * duplicates. */
6aa74308 2888 if (in_port->n_ifaces > 1 && !lacp_negotiated(in_port->lacp)) {
db8077c3 2889 struct mac_entry *mac;
3a55ef14
JG
2890
2891 if (eth_addr_is_multicast(flow->dl_dst)) {
7e559ecf 2892 *tags |= port_get_active_iface_tag(in_port);
990cda85 2893 if (in_port->active_iface != in_iface) {
3a55ef14 2894 /* Drop all multicast packets on inactive slaves. */
14a34d00 2895 return false;
c93b1d6a 2896 }
064af421 2897 }
3a55ef14
JG
2898
2899 /* Drop all packets for which we have learned a different input
2900 * port, because we probably sent the packet on one slave and got
5d0ae138 2901 * it back on the other. Gratuitous ARP packets are an exception
7febb910
JG
2902 * to this rule: the host has moved to another switch. The exception
2903 * to the exception is if we locked the learning table to avoid
2904 * reflections on bond slaves. If this is the case, just drop the
2905 * packet now. */
db8077c3 2906 mac = mac_learning_lookup(br->ml, flow->dl_src, vlan, NULL);
1648ddd7 2907 if (mac && mac->port.p != in_port &&
db8077c3 2908 (!is_gratuitous_arp(flow) || mac_entry_is_grat_arp_locked(mac))) {
14a34d00 2909 return false;
3a55ef14 2910 }
064af421
BP
2911 }
2912
14a34d00
BP
2913 return true;
2914}
2915
2916/* If the composed actions may be applied to any packet in the given 'flow',
2917 * returns true. Otherwise, the actions should only be applied to 'packet', or
2918 * not at all, if 'packet' was NULL. */
2919static bool
ae412e7d 2920process_flow(struct bridge *br, const struct flow *flow,
cdee00fd 2921 const struct ofpbuf *packet, struct ofpbuf *actions,
14a34d00
BP
2922 tag_type *tags, uint16_t *nf_output_iface)
2923{
2924 struct port *in_port;
2925 struct port *out_port;
db8077c3 2926 struct mac_entry *mac;
14a34d00 2927 int vlan;
14a34d00
BP
2928
2929 /* Check whether we should drop packets in this flow. */
2930 if (!is_admissible(br, flow, packet != NULL, tags, &vlan, &in_port)) {
2931 out_port = NULL;
2932 goto done;
2933 }
2934
93dfc067
JG
2935 /* Learn source MAC (but don't try to learn from revalidation). */
2936 if (packet) {
e96a4d80 2937 update_learning_table(br, flow, vlan, in_port);
93dfc067
JG
2938 }
2939
2940 /* Determine output port. */
db8077c3 2941 mac = mac_learning_lookup(br->ml, flow->dl_dst, vlan, tags);
1648ddd7 2942 if (mac) {
bbb1951c 2943 out_port = mac->port.p;
e96a4d80 2944 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
93dfc067 2945 /* If we are revalidating but don't have a learning entry then
e96a4d80
JG
2946 * eject the flow. Installing a flow that floods packets opens
2947 * up a window of time where we could learn from a packet reflected
2948 * on a bond and blackhole packets before the learning table is
2949 * updated to reflect the correct port. */
93dfc067 2950 return false;
1609aa03
BP
2951 } else {
2952 out_port = FLOOD_PORT;
064af421
BP
2953 }
2954
ba54bf4f
BP
2955 /* Don't send packets out their input ports. */
2956 if (in_port == out_port) {
064af421
BP
2957 out_port = NULL;
2958 }
2959
2960done:
14a34d00
BP
2961 if (in_port) {
2962 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2963 nf_output_iface);
2964 }
064af421 2965
69d60f9f 2966 return true;
064af421
BP
2967}
2968
064af421 2969static bool
ae412e7d 2970bridge_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
cdee00fd 2971 struct ofpbuf *actions, tag_type *tags,
6a07af36 2972 uint16_t *nf_output_iface, void *br_)
064af421
BP
2973{
2974 struct bridge *br = br_;
2975
064af421 2976 COVERAGE_INC(bridge_process_flow);
ebe482fd
EJ
2977 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2978}
2979
2980static bool
2981bridge_special_ofhook_cb(const struct flow *flow,
2982 const struct ofpbuf *packet, void *br_)
2983{
2984 struct iface *iface;
2985 struct bridge *br = br_;
26d79bf2 2986
b31bcf60
EJ
2987 iface = iface_from_dp_ifidx(br, flow->in_port);
2988
e7934396 2989 if (flow->dl_type == htons(ETH_TYPE_LACP)) {
ebe482fd 2990
6aa74308
EJ
2991 if (iface && iface->port->lacp && packet) {
2992 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
2993
2994 if (pdu) {
2995 COVERAGE_INC(bridge_process_lacp);
2996 lacp_process_pdu(iface->port->lacp, iface, pdu);
2997 }
c25c91fd
EJ
2998 }
2999 return false;
b31bcf60
EJ
3000 }
3001
ebe482fd 3002 return true;
064af421
BP
3003}
3004
3005static void
ae412e7d 3006bridge_account_flow_ofhook_cb(const struct flow *flow, tag_type tags,
cdee00fd 3007 const struct nlattr *actions,
cf22f8cb 3008 size_t actions_len,
7006c033 3009 uint64_t n_bytes, void *br_)
064af421
BP
3010{
3011 struct bridge *br = br_;
cdee00fd 3012 const struct nlattr *a;
db0e2ad1 3013 struct port *in_port;
26d79bf2 3014 tag_type dummy = 0;
cdee00fd 3015 unsigned int left;
db0e2ad1 3016 int vlan;
064af421 3017
26d79bf2
BP
3018 /* Feed information from the active flows back into the learning table to
3019 * ensure that table is always in sync with what is actually flowing
3020 * through the datapath.
3021 *
3022 * We test that 'tags' is nonzero to ensure that only flows that include an
3023 * OFPP_NORMAL action are used for learning. This works because
3024 * bridge_normal_ofhook_cb() always sets a nonzero tag value. */
3025 if (tags && is_admissible(br, flow, false, &dummy, &vlan, &in_port)) {
db0e2ad1 3026 update_learning_table(br, flow, vlan, in_port);
e96a4d80
JG
3027 }
3028
26d79bf2 3029 /* Account for bond slave utilization. */
064af421
BP
3030 if (!br->has_bonded_ports) {
3031 return;
3032 }
cdee00fd 3033 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
7aec165d 3034 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
cdee00fd 3035 struct port *out_port = port_from_dp_ifidx(br, nl_attr_get_u32(a));
be02e7c3 3036 if (out_port && out_port->n_ifaces >= 2 &&
f48f70b7 3037 out_port->bond_mode != BM_AB) {
e58de0e3
EJ
3038 uint16_t vlan = (flow->vlan_tci
3039 ? vlan_tci_to_vid(flow->vlan_tci)
3040 : OFP_VLAN_NONE);
9f5073d8 3041 struct bond_entry *e = lookup_bond_entry(out_port, flow, vlan);
064af421
BP
3042 e->tx_bytes += n_bytes;
3043 }
3044 }
3045 }
3046}
3047
3048static void
3049bridge_account_checkpoint_ofhook_cb(void *br_)
3050{
3051 struct bridge *br = br_;
8052fb14 3052 struct port *port;
c8143c88 3053 long long int now;
064af421
BP
3054
3055 if (!br->has_bonded_ports) {
3056 return;
3057 }
3058
c8143c88 3059 now = time_msec();
8052fb14 3060 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
f48f70b7 3061 if (port->n_ifaces > 1 && port->bond_mode != BM_AB
be02e7c3 3062 && now >= port->bond_next_rebalance) {
c8143c88 3063 port->bond_next_rebalance = now + port->bond_rebalance_interval;
064af421
BP
3064 bond_rebalance_port(port);
3065 }
3066 }
3067}
3068
3069static struct ofhooks bridge_ofhooks = {
064af421 3070 bridge_normal_ofhook_cb,
ebe482fd 3071 bridge_special_ofhook_cb,
064af421
BP
3072 bridge_account_flow_ofhook_cb,
3073 bridge_account_checkpoint_ofhook_cb,
3074};
3075\f
2303f3b2
BP
3076/* Bonding functions. */
3077
064af421
BP
3078/* Statistics for a single interface on a bonded port, used for load-based
3079 * bond rebalancing. */
3080struct slave_balance {
3081 struct iface *iface; /* The interface. */
3082 uint64_t tx_bytes; /* Sum of hashes[*]->tx_bytes. */
3083
3084 /* All the "bond_entry"s that are assigned to this interface, in order of
3085 * increasing tx_bytes. */
3086 struct bond_entry **hashes;
3087 size_t n_hashes;
3088};
3089
be02e7c3 3090static const char *
27dcaa1a
EJ
3091bond_mode_to_string(enum bond_mode bm) {
3092 static char *bm_slb = "balance-slb";
3093 static char *bm_ab = "active-backup";
9f5073d8 3094 static char *bm_tcp = "balance-tcp";
be02e7c3 3095
27dcaa1a
EJ
3096 switch (bm) {
3097 case BM_SLB: return bm_slb;
3098 case BM_AB: return bm_ab;
9f5073d8 3099 case BM_TCP: return bm_tcp;
be02e7c3
EJ
3100 }
3101
3102 NOT_REACHED();
3103 return NULL;
3104}
3105
064af421
BP
3106/* Sorts pointers to pointers to bond_entries in ascending order by the
3107 * interface to which they are assigned, and within a single interface in
3108 * ascending order of bytes transmitted. */
3109static int
3110compare_bond_entries(const void *a_, const void *b_)
3111{
3112 const struct bond_entry *const *ap = a_;
3113 const struct bond_entry *const *bp = b_;
3114 const struct bond_entry *a = *ap;
3115 const struct bond_entry *b = *bp;
c17f0d5e
BP
3116 if (a->iface != b->iface) {
3117 return a->iface > b->iface ? 1 : -1;
064af421
BP
3118 } else if (a->tx_bytes != b->tx_bytes) {
3119 return a->tx_bytes > b->tx_bytes ? 1 : -1;
3120 } else {
3121 return 0;
3122 }
3123}
3124
3125/* Sorts slave_balances so that enabled ports come first, and otherwise in
3126 * *descending* order by number of bytes transmitted. */
3127static int
3128compare_slave_balance(const void *a_, const void *b_)
3129{
3130 const struct slave_balance *a = a_;
3131 const struct slave_balance *b = b_;
3132 if (a->iface->enabled != b->iface->enabled) {
3133 return a->iface->enabled ? -1 : 1;
3134 } else if (a->tx_bytes != b->tx_bytes) {
3135 return a->tx_bytes > b->tx_bytes ? -1 : 1;
3136 } else {
3137 return 0;
3138 }
3139}
3140
3141static void
3142swap_bals(struct slave_balance *a, struct slave_balance *b)
3143{
3144 struct slave_balance tmp = *a;
3145 *a = *b;
3146 *b = tmp;
3147}
3148
3149/* Restores the 'n_bals' slave_balance structures in 'bals' to sorted order
3150 * given that 'p' (and only 'p') might be in the wrong location.
3151 *
3152 * This function invalidates 'p', since it might now be in a different memory
3153 * location. */
3154static void
3155resort_bals(struct slave_balance *p,
3156 struct slave_balance bals[], size_t n_bals)
3157{
3158 if (n_bals > 1) {
3159 for (; p > bals && p->tx_bytes > p[-1].tx_bytes; p--) {
3160 swap_bals(p, p - 1);
3161 }
3162 for (; p < &bals[n_bals - 1] && p->tx_bytes < p[1].tx_bytes; p++) {
3163 swap_bals(p, p + 1);
3164 }
3165 }
3166}
3167
3168static void
3169log_bals(const struct slave_balance *bals, size_t n_bals, struct port *port)
3170{
3171 if (VLOG_IS_DBG_ENABLED()) {
3172 struct ds ds = DS_EMPTY_INITIALIZER;
3173 const struct slave_balance *b;
3174
3175 for (b = bals; b < bals + n_bals; b++) {
3176 size_t i;
3177
3178 if (b > bals) {
3179 ds_put_char(&ds, ',');
3180 }
3181 ds_put_format(&ds, " %s %"PRIu64"kB",
3182 b->iface->name, b->tx_bytes / 1024);
3183
3184 if (!b->iface->enabled) {
3185 ds_put_cstr(&ds, " (disabled)");
3186 }
3187 if (b->n_hashes > 0) {
3188 ds_put_cstr(&ds, " (");
3189 for (i = 0; i < b->n_hashes; i++) {
3190 const struct bond_entry *e = b->hashes[i];
3191 if (i > 0) {
3192 ds_put_cstr(&ds, " + ");
3193 }
3194 ds_put_format(&ds, "h%td: %"PRIu64"kB",
3195 e - port->bond_hash, e->tx_bytes / 1024);
3196 }
3197 ds_put_cstr(&ds, ")");
3198 }
3199 }
3200 VLOG_DBG("bond %s:%s", port->name, ds_cstr(&ds));
3201 ds_destroy(&ds);
3202 }
3203}
3204
3205/* Shifts 'hash' from 'from' to 'to' within 'port'. */
3206static void
3207bond_shift_load(struct slave_balance *from, struct slave_balance *to,
5422a9e1 3208 int hash_idx)
064af421 3209{
5422a9e1 3210 struct bond_entry *hash = from->hashes[hash_idx];
064af421
BP
3211 struct port *port = from->iface->port;
3212 uint64_t delta = hash->tx_bytes;
3213
f48f70b7 3214 assert(port->bond_mode != BM_AB);
be02e7c3 3215
064af421
BP
3216 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
3217 "from %s to %s (now carrying %"PRIu64"kB and "
3218 "%"PRIu64"kB load, respectively)",
3219 port->name, delta / 1024, hash - port->bond_hash,
3220 from->iface->name, to->iface->name,
3221 (from->tx_bytes - delta) / 1024,
3222 (to->tx_bytes + delta) / 1024);
3223
3224 /* Delete element from from->hashes.
3225 *
3226 * We don't bother to add the element to to->hashes because not only would
3227 * it require more work, the only purpose it would be to allow that hash to
3228 * be migrated to another slave in this rebalancing run, and there is no
3229 * point in doing that. */
5422a9e1 3230 if (hash_idx == 0) {
064af421
BP
3231 from->hashes++;
3232 } else {
5422a9e1
JG
3233 memmove(from->hashes + hash_idx, from->hashes + hash_idx + 1,
3234 (from->n_hashes - (hash_idx + 1)) * sizeof *from->hashes);
064af421
BP
3235 }
3236 from->n_hashes--;
3237
3238 /* Shift load away from 'from' to 'to'. */
3239 from->tx_bytes -= delta;
3240 to->tx_bytes += delta;
3241
3242 /* Arrange for flows to be revalidated. */
c0cb6f59 3243 ofproto_revalidate(port->bridge->ofproto, hash->tag);
c17f0d5e 3244 hash->iface = to->iface;
c0cb6f59 3245 hash->tag = tag_create_random();
064af421
BP
3246}
3247
3248static void
3249bond_rebalance_port(struct port *port)
3250{
c47cd5f0 3251 struct slave_balance *bals;
064af421
BP
3252 size_t n_bals;
3253 struct bond_entry *hashes[BOND_MASK + 1];
3254 struct slave_balance *b, *from, *to;
3255 struct bond_entry *e;
83db7968 3256 struct iface *iface;
064af421
BP
3257 size_t i;
3258
9f5073d8 3259 assert(port->bond_mode != BM_AB);
be02e7c3 3260
064af421
BP
3261 /* Sets up 'bals' to describe each of the port's interfaces, sorted in
3262 * descending order of tx_bytes, so that bals[0] represents the most
3263 * heavily loaded slave and bals[n_bals - 1] represents the least heavily
3264 * loaded slave.
3265 *
3266 * The code is a bit tricky: to avoid dynamically allocating a 'hashes'
3267 * array for each slave_balance structure, we sort our local array of
3268 * hashes in order by slave, so that all of the hashes for a given slave
3269 * become contiguous in memory, and then we point each 'hashes' members of
3270 * a slave_balance structure to the start of a contiguous group. */
3271 n_bals = port->n_ifaces;
83db7968
BP
3272 b = bals = xmalloc(n_bals * sizeof *bals);
3273 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3274 b->iface = iface;
064af421
BP
3275 b->tx_bytes = 0;
3276 b->hashes = NULL;
3277 b->n_hashes = 0;
83db7968 3278 b++;
064af421 3279 }
83db7968 3280 assert(b == &bals[n_bals]);
064af421
BP
3281 for (i = 0; i <= BOND_MASK; i++) {
3282 hashes[i] = &port->bond_hash[i];
3283 }
3284 qsort(hashes, BOND_MASK + 1, sizeof *hashes, compare_bond_entries);
3285 for (i = 0; i <= BOND_MASK; i++) {
3286 e = hashes[i];
c17f0d5e
BP
3287 if (!e->iface) {
3288 continue;
3289 }
3290
3291 for (b = bals; b < &bals[n_bals]; b++) {
3292 if (b->iface == e->iface) {
3293 b->tx_bytes += e->tx_bytes;
3294 if (!b->hashes) {
3295 b->hashes = &hashes[i];
3296 }
3297 b->n_hashes++;
3298 break;
064af421 3299 }
064af421
BP
3300 }
3301 }
3302 qsort(bals, n_bals, sizeof *bals, compare_slave_balance);
3303 log_bals(bals, n_bals, port);
3304
3305 /* Discard slaves that aren't enabled (which were sorted to the back of the
3306 * array earlier). */
3307 while (!bals[n_bals - 1].iface->enabled) {
3308 n_bals--;
3309 if (!n_bals) {
c47cd5f0 3310 goto exit;
064af421
BP
3311 }
3312 }
3313
3314 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
3315 to = &bals[n_bals - 1];
3316 for (from = bals; from < to; ) {
3317 uint64_t overload = from->tx_bytes - to->tx_bytes;
3318 if (overload < to->tx_bytes >> 5 || overload < 100000) {
3319 /* The extra load on 'from' (and all less-loaded slaves), compared
3320 * to that of 'to' (the least-loaded slave), is less than ~3%, or
3321 * it is less than ~1Mbps. No point in rebalancing. */
3322 break;
3323 } else if (from->n_hashes == 1) {
3324 /* 'from' only carries a single MAC hash, so we can't shift any
3325 * load away from it, even though we want to. */
3326 from++;
3327 } else {
3328 /* 'from' is carrying significantly more load than 'to', and that
3329 * load is split across at least two different hashes. Pick a hash
3330 * to migrate to 'to' (the least-loaded slave), given that doing so
5422a9e1
JG
3331 * must decrease the ratio of the load on the two slaves by at
3332 * least 0.1.
064af421
BP
3333 *
3334 * The sort order we use means that we prefer to shift away the
3335 * smallest hashes instead of the biggest ones. There is little
3336 * reason behind this decision; we could use the opposite sort
3337 * order to shift away big hashes ahead of small ones. */
5422a9e1 3338 bool order_swapped;
064af421
BP
3339
3340 for (i = 0; i < from->n_hashes; i++) {
5422a9e1 3341 double old_ratio, new_ratio;
064af421 3342 uint64_t delta = from->hashes[i]->tx_bytes;
5422a9e1
JG
3343
3344 if (delta == 0 || from->tx_bytes - delta == 0) {
3345 /* Pointless move. */
3346 continue;
3347 }
3348
3349 order_swapped = from->tx_bytes - delta < to->tx_bytes + delta;
3350
3351 if (to->tx_bytes == 0) {
3352 /* Nothing on the new slave, move it. */
3353 break;
3354 }
3355
3356 old_ratio = (double)from->tx_bytes / to->tx_bytes;
3357 new_ratio = (double)(from->tx_bytes - delta) /
3358 (to->tx_bytes + delta);
3359
3360 if (new_ratio == 0) {
3361 /* Should already be covered but check to prevent division
3362 * by zero. */
3363 continue;
3364 }
3365
3366 if (new_ratio < 1) {
3367 new_ratio = 1 / new_ratio;
3368 }
3369
3370 if (old_ratio - new_ratio > 0.1) {
3371 /* Would decrease the ratio, move it. */
064af421
BP
3372 break;
3373 }
3374 }
3375 if (i < from->n_hashes) {
5422a9e1 3376 bond_shift_load(from, to, i);
5422a9e1
JG
3377
3378 /* If the result of the migration changed the relative order of
3379 * 'from' and 'to' swap them back to maintain invariants. */
3380 if (order_swapped) {
3381 swap_bals(from, to);
3382 }
064af421
BP
3383
3384 /* Re-sort 'bals'. Note that this may make 'from' and 'to'
3385 * point to different slave_balance structures. It is only
3386 * valid to do these two operations in a row at all because we
3387 * know that 'from' will not move past 'to' and vice versa. */
3388 resort_bals(from, bals, n_bals);
3389 resort_bals(to, bals, n_bals);
3390 } else {
3391 from++;
3392 }
3393 }
3394 }
3395
3396 /* Implement exponentially weighted moving average. A weight of 1/2 causes
3397 * historical data to decay to <1% in 7 rebalancing runs. */
3398 for (e = &port->bond_hash[0]; e <= &port->bond_hash[BOND_MASK]; e++) {
3399 e->tx_bytes /= 2;
332e0d03 3400 if (!e->tx_bytes) {
c17f0d5e 3401 e->iface = NULL;
332e0d03 3402 }
064af421 3403 }
c47cd5f0
BP
3404
3405exit:
3406 free(bals);
064af421 3407}
2303f3b2
BP
3408
3409static void
3410bond_send_learning_packets(struct port *port)
3411{
3412 struct bridge *br = port->bridge;
3413 struct mac_entry *e;
3414 struct ofpbuf packet;
3415 int error, n_packets, n_errors;
3416
990cda85 3417 if (!port->n_ifaces || !port->active_iface || bond_is_tcp_hash(port)) {
2303f3b2
BP
3418 return;
3419 }
3420
3421 ofpbuf_init(&packet, 128);
3422 error = n_packets = n_errors = 0;
4e8e4213 3423 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
2303f3b2 3424 tag_type tags = 0;
3cf10406 3425 uint16_t dp_ifidx;
ae412e7d 3426 struct flow flow;
2303f3b2
BP
3427 int retval;
3428
1648ddd7 3429 if (e->port.p == port) {
9f5073d8
EJ
3430 continue;
3431 }
3432
3433 compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
3434 e->mac);
3435 flow_extract(&packet, 0, ODPP_NONE, &flow);
3436
3437 if (!choose_output_iface(port, &flow, e->vlan, &dp_ifidx, &tags)) {
2303f3b2
BP
3438 continue;
3439 }
3440
2303f3b2
BP
3441 /* Send packet. */
3442 n_packets++;
3cf10406 3443 retval = ofproto_send_packet(br->ofproto, dp_ifidx, e->vlan, &packet);
2303f3b2
BP
3444 if (retval) {
3445 error = retval;
3446 n_errors++;
3447 }
3448 }
3449 ofpbuf_uninit(&packet);
3450
3451 if (n_errors) {
3452 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3453 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3454 "packets, last error was: %s",
3455 port->name, n_errors, n_packets, strerror(error));
3456 } else {
3457 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3458 port->name, n_packets);
3459 }
3460}
064af421 3461\f
da285df4
BP
3462/* Bonding unixctl user interface functions. */
3463
3464static void
8ca79daa 3465bond_unixctl_list(struct unixctl_conn *conn,
c69ee87c 3466 const char *args OVS_UNUSED, void *aux OVS_UNUSED)
da285df4
BP
3467{
3468 struct ds ds = DS_EMPTY_INITIALIZER;
3469 const struct bridge *br;
3470
be02e7c3 3471 ds_put_cstr(&ds, "bridge\tbond\ttype\tslaves\n");
da285df4 3472
4e8e4213 3473 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14 3474 struct port *port;
da285df4 3475
8052fb14 3476 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
da285df4 3477 if (port->n_ifaces > 1) {
83db7968 3478 struct iface *iface;
da285df4 3479
be02e7c3 3480 ds_put_format(&ds, "%s\t%s\t%s\t", br->name, port->name,
27dcaa1a 3481 bond_mode_to_string(port->bond_mode));
83db7968
BP
3482 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3483 if (&iface->port_elem != list_front(&port->ifaces)) {
da285df4
BP
3484 ds_put_cstr(&ds, ", ");
3485 }
3486 ds_put_cstr(&ds, iface->name);
3487 }
3488 ds_put_char(&ds, '\n');
3489 }
3490 }
3491 }
3492 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3493 ds_destroy(&ds);
3494}
3495
3496static struct port *
3497bond_find(const char *name)
3498{
3499 const struct bridge *br;
3500
4e8e4213 3501 LIST_FOR_EACH (br, node, &all_bridges) {
8052fb14 3502 struct port *port;
da285df4 3503
8052fb14 3504 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
da285df4
BP
3505 if (!strcmp(port->name, name) && port->n_ifaces > 1) {
3506 return port;
3507 }
3508 }
3509 }
3510 return NULL;
3511}
3512
3513static void
8ca79daa 3514bond_unixctl_show(struct unixctl_conn *conn,
c69ee87c 3515 const char *args, void *aux OVS_UNUSED)
da285df4
BP
3516{
3517 struct ds ds = DS_EMPTY_INITIALIZER;
3518 const struct port *port;
83db7968 3519 struct iface *iface;
da285df4
BP
3520
3521 port = bond_find(args);
3522 if (!port) {
3523 unixctl_command_reply(conn, 501, "no such bond");
3524 return;
3525 }
3526
27dcaa1a
EJ
3527 ds_put_format(&ds, "bond_mode: %s\n",
3528 bond_mode_to_string(port->bond_mode));
c25c91fd
EJ
3529
3530 if (port->lacp) {
846f1be5 3531 ds_put_format(&ds, "lacp: %s\n",
6aa74308 3532 port->lacp_active ? "active" : "passive");
c25c91fd 3533 } else {
846f1be5 3534 ds_put_cstr(&ds, "lacp: off\n");
c25c91fd
EJ
3535 }
3536
9f5073d8
EJ
3537 if (port->bond_mode != BM_AB) {
3538 ds_put_format(&ds, "bond-hash-algorithm: %s\n",
3539 bond_is_tcp_hash(port) ? "balance-tcp" : "balance-slb");
3540 }
3541
3542
63331829 3543 ds_put_format(&ds, "bond-detect-mode: %s\n",
e5ed989d 3544 port->monitor ? "carrier" : "miimon");
b42c5c24 3545
e5ed989d 3546 if (!port->monitor) {
b42c5c24 3547 ds_put_format(&ds, "bond-miimon-interval: %lld\n",
e5ed989d 3548 port->miimon_interval);
b42c5c24
EJ
3549 }
3550
da285df4
BP
3551 ds_put_format(&ds, "updelay: %d ms\n", port->updelay);
3552 ds_put_format(&ds, "downdelay: %d ms\n", port->downdelay);
be02e7c3 3553
9f5073d8 3554 if (port->bond_mode != BM_AB) {
be02e7c3
EJ
3555 ds_put_format(&ds, "next rebalance: %lld ms\n",
3556 port->bond_next_rebalance - time_msec());
3557 }
3558
83db7968 3559 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
da285df4 3560 struct bond_entry *be;
9f5073d8 3561 struct flow flow;
da285df4
BP
3562
3563 /* Basic info. */
846f1be5 3564 ds_put_format(&ds, "\nslave %s: %s\n",
da285df4 3565 iface->name, iface->enabled ? "enabled" : "disabled");
990cda85 3566 if (iface == port->active_iface) {
da285df4
BP
3567 ds_put_cstr(&ds, "\tactive slave\n");
3568 }
3569 if (iface->delay_expires != LLONG_MAX) {
3570 ds_put_format(&ds, "\t%s expires in %lld ms\n",
3571 iface->enabled ? "downdelay" : "updelay",
3572 iface->delay_expires - time_msec());
3573 }
3574
9f5073d8 3575 if (port->bond_mode == BM_AB) {
be02e7c3
EJ
3576 continue;
3577 }
3578
da285df4 3579 /* Hashes. */
9f5073d8 3580 memset(&flow, 0, sizeof flow);
da285df4
BP
3581 for (be = port->bond_hash; be <= &port->bond_hash[BOND_MASK]; be++) {
3582 int hash = be - port->bond_hash;
3583 struct mac_entry *me;
3584
c17f0d5e 3585 if (be->iface != iface) {
da285df4
BP
3586 continue;
3587 }
3588
2886875a 3589 ds_put_format(&ds, "\thash %d: %"PRIu64" kB load\n",
da285df4
BP
3590 hash, be->tx_bytes / 1024);
3591
9f5073d8
EJ
3592 if (port->bond_mode != BM_SLB) {
3593 continue;
3594 }
3595
da285df4 3596 /* MACs. */
4e8e4213 3597 LIST_FOR_EACH (me, lru_node, &port->bridge->ml->lrus) {
da285df4
BP
3598 uint16_t dp_ifidx;
3599 tag_type tags = 0;
9f5073d8
EJ
3600
3601 memcpy(flow.dl_src, me->mac, ETH_ADDR_LEN);
3602 if (bond_hash_src(me->mac, me->vlan) == hash
1648ddd7 3603 && me->port.p != port
9f5073d8 3604 && choose_output_iface(port, &flow, me->vlan,
e58de0e3 3605 &dp_ifidx, &tags)
da285df4
BP
3606 && dp_ifidx == iface->dp_ifidx)
3607 {
3608 ds_put_format(&ds, "\t\t"ETH_ADDR_FMT"\n",
3609 ETH_ADDR_ARGS(me->mac));
3610 }
3611 }
3612 }
3613 }
3614 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3615 ds_destroy(&ds);
3616}
3617
3618static void
8ca79daa 3619bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_,
c69ee87c 3620 void *aux OVS_UNUSED)
da285df4
BP
3621{
3622 char *args = (char *) args_;
3623 char *save_ptr = NULL;
3624 char *bond_s, *hash_s, *slave_s;
da285df4
BP
3625 struct port *port;
3626 struct iface *iface;
3627 struct bond_entry *entry;
3628 int hash;
3629
3630 bond_s = strtok_r(args, " ", &save_ptr);
3631 hash_s = strtok_r(NULL, " ", &save_ptr);
3632 slave_s = strtok_r(NULL, " ", &save_ptr);
3633 if (!slave_s) {
3634 unixctl_command_reply(conn, 501,
3635 "usage: bond/migrate BOND HASH SLAVE");
3636 return;
3637 }
3638
3639 port = bond_find(bond_s);
3640 if (!port) {
3641 unixctl_command_reply(conn, 501, "no such bond");
3642 return;
3643 }
3644
27dcaa1a 3645 if (port->bond_mode != BM_SLB) {
be02e7c3
EJ
3646 unixctl_command_reply(conn, 501, "not an SLB bond");
3647 return;
3648 }
3649
557c178b 3650 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
da285df4
BP
3651 hash = atoi(hash_s) & BOND_MASK;
3652 } else {
3653 unixctl_command_reply(conn, 501, "bad hash");
3654 return;
3655 }
3656
3657 iface = port_lookup_iface(port, slave_s);
3658 if (!iface) {
3659 unixctl_command_reply(conn, 501, "no such slave");
3660 return;
3661 }
3662
3663 if (!iface->enabled) {
3664 unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
3665 return;
3666 }
3667
3668 entry = &port->bond_hash[hash];
c0cb6f59 3669 ofproto_revalidate(port->bridge->ofproto, entry->tag);
c17f0d5e 3670 entry->iface = iface;
c0cb6f59 3671 entry->tag = tag_create_random();
da285df4
BP
3672 unixctl_command_reply(conn, 200, "migrated");
3673}
3674
3675static void
8ca79daa 3676bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_,
c69ee87c 3677 void *aux OVS_UNUSED)
da285df4
BP
3678{
3679 char *args = (char *) args_;
3680 char *save_ptr = NULL;
3681 char *bond_s, *slave_s;
3682 struct port *port;
3683 struct iface *iface;
3684
3685 bond_s = strtok_r(args, " ", &save_ptr);
3686 slave_s = strtok_r(NULL, " ", &save_ptr);
3687 if (!slave_s) {
3688 unixctl_command_reply(conn, 501,
3689 "usage: bond/set-active-slave BOND SLAVE");
3690 return;
3691 }
3692
3693 port = bond_find(bond_s);
3694 if (!port) {
3695 unixctl_command_reply(conn, 501, "no such bond");
3696 return;
3697 }
3698
3699 iface = port_lookup_iface(port, slave_s);
3700 if (!iface) {
3701 unixctl_command_reply(conn, 501, "no such slave");
3702 return;
3703 }
3704
3705 if (!iface->enabled) {
3706 unixctl_command_reply(conn, 501, "cannot make disabled slave active");
3707 return;
3708 }
3709
990cda85 3710 if (port->active_iface != iface) {
7e559ecf
BP
3711 ofproto_revalidate(port->bridge->ofproto,
3712 port_get_active_iface_tag(port));
990cda85 3713 port->active_iface = iface;
da285df4
BP
3714 VLOG_INFO("port %s: active interface is now %s",
3715 port->name, iface->name);
3716 bond_send_learning_packets(port);
3717 unixctl_command_reply(conn, 200, "done");
3718 } else {
3719 unixctl_command_reply(conn, 200, "no change");
3720 }
3721}
3722
3723static void
3724enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
3725{
3726 char *args = (char *) args_;
3727 char *save_ptr = NULL;
3728 char *bond_s, *slave_s;
3729 struct port *port;
3730 struct iface *iface;
3731
3732 bond_s = strtok_r(args, " ", &save_ptr);
3733 slave_s = strtok_r(NULL, " ", &save_ptr);
3734 if (!slave_s) {
3735 unixctl_command_reply(conn, 501,
3736 "usage: bond/enable/disable-slave BOND SLAVE");
3737 return;
3738 }
3739
3740 port = bond_find(bond_s);
3741 if (!port) {
3742 unixctl_command_reply(conn, 501, "no such bond");
3743 return;
3744 }
3745
3746 iface = port_lookup_iface(port, slave_s);
3747 if (!iface) {
3748 unixctl_command_reply(conn, 501, "no such slave");
3749 return;
3750 }
3751
3752 bond_enable_slave(iface, enable);
3753 unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
3754}
3755
3756static void
8ca79daa 3757bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args,
c69ee87c 3758 void *aux OVS_UNUSED)
da285df4
BP
3759{
3760 enable_slave(conn, args, true);
3761}
3762
3763static void
8ca79daa 3764bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args,
c69ee87c 3765 void *aux OVS_UNUSED)
da285df4
BP
3766{
3767 enable_slave(conn, args, false);
3768}
3769
e0644b61 3770static void
e58de0e3 3771bond_unixctl_hash(struct unixctl_conn *conn, const char *args_,
c69ee87c 3772 void *aux OVS_UNUSED)
e0644b61 3773{
e58de0e3 3774 char *args = (char *) args_;
a27598cd
EJ
3775 uint8_t mac[ETH_ADDR_LEN];
3776 uint8_t hash;
3777 char *hash_cstr;
e58de0e3
EJ
3778 unsigned int vlan;
3779 char *mac_s, *vlan_s;
3780 char *save_ptr = NULL;
3781
3782 mac_s = strtok_r(args, " ", &save_ptr);
3783 vlan_s = strtok_r(NULL, " ", &save_ptr);
3784
3785 if (vlan_s) {
3786 if (sscanf(vlan_s, "%u", &vlan) != 1) {
3787 unixctl_command_reply(conn, 501, "invalid vlan");
3788 return;
3789 }
3790 } else {
3791 vlan = OFP_VLAN_NONE;
3792 }
a27598cd 3793
e58de0e3 3794 if (sscanf(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
a27598cd 3795 == ETH_ADDR_SCAN_COUNT) {
9f5073d8 3796 hash = bond_hash_src(mac, vlan);
a27598cd
EJ
3797
3798 hash_cstr = xasprintf("%u", hash);
3799 unixctl_command_reply(conn, 200, hash_cstr);
3800 free(hash_cstr);
3801 } else {
3802 unixctl_command_reply(conn, 501, "invalid mac");
3803 }
e0644b61
IC
3804}
3805
da285df4
BP
3806static void
3807bond_init(void)
3808{
8ca79daa
BP
3809 unixctl_command_register("bond/list", bond_unixctl_list, NULL);
3810 unixctl_command_register("bond/show", bond_unixctl_show, NULL);
3811 unixctl_command_register("bond/migrate", bond_unixctl_migrate, NULL);
da285df4 3812 unixctl_command_register("bond/set-active-slave",
8ca79daa
BP
3813 bond_unixctl_set_active_slave, NULL);
3814 unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave,
3815 NULL);
3816 unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave,
3817 NULL);
3818 unixctl_command_register("bond/hash", bond_unixctl_hash, NULL);
da285df4
BP
3819}
3820\f
064af421
BP
3821/* Port functions. */
3822
6aa74308
EJ
3823static void
3824lacp_send_pdu_cb(void *aux, const struct lacp_pdu *pdu)
3825{
3826 struct iface *iface = aux;
3827 uint8_t ea[ETH_ADDR_LEN];
3828 int error;
3829
3830 error = netdev_get_etheraddr(iface->netdev, ea);
3831 if (!error) {
3832 struct ofpbuf packet;
40f78b38 3833 struct lacp_pdu *packet_pdu;
6aa74308 3834
40f78b38
EJ
3835 ofpbuf_init(&packet, 0);
3836 packet_pdu = compose_packet(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
3837 sizeof *packet_pdu);
3838 memcpy(packet_pdu, pdu, sizeof *packet_pdu);
3cf10406
BP
3839 ofproto_send_packet(iface->port->bridge->ofproto,
3840 iface->dp_ifidx, 0, &packet);
6aa74308
EJ
3841 ofpbuf_uninit(&packet);
3842 } else {
3843 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3844 VLOG_ERR_RL(&rl, "iface %s: failed to obtain Ethernet address "
3845 "(%s)", iface->name, strerror(error));
3846 }
3847}
3848
21dcb94f
EJ
3849static void
3850port_run(struct port *port)
3851{
e5ed989d
EJ
3852 if (port->monitor) {
3853 char *devname;
3854
3855 /* Track carrier going up and down on interfaces. */
3856 while (!netdev_monitor_poll(port->monitor, &devname)) {
3857 struct iface *iface;
3858
3859 iface = port_lookup_iface(port, devname);
3860 if (iface) {
0b8024eb 3861 iface_update_carrier(iface);
e5ed989d
EJ
3862 }
3863 free(devname);
3864 }
3865 } else if (time_msec() >= port->miimon_next_update) {
83db7968 3866 struct iface *iface;
e5ed989d 3867
83db7968 3868 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
0b8024eb 3869 iface_update_carrier(iface);
e5ed989d
EJ
3870 }
3871 port->miimon_next_update = time_msec() + port->miimon_interval;
3872 }
3873
6aa74308 3874 if (port->lacp) {
83db7968 3875 struct iface *iface;
e7934396 3876
83db7968 3877 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
6aa74308
EJ
3878 lacp_slave_enable(port->lacp, iface, iface->enabled);
3879 }
3880
3881 lacp_run(port->lacp, lacp_send_pdu_cb);
3882 }
3883
21dcb94f 3884 bond_run(port);
21dcb94f
EJ
3885}
3886
3887static void
3888port_wait(struct port *port)
3889{
e5ed989d
EJ
3890 if (port->monitor) {
3891 netdev_monitor_poll_wait(port->monitor);
3892 } else {
3893 poll_timer_wait_until(port->miimon_next_update);
3894 }
3895
6aa74308
EJ
3896 if (port->lacp) {
3897 lacp_wait(port->lacp);
3898 }
3899
21dcb94f 3900 bond_wait(port);
21dcb94f
EJ
3901}
3902
76343538 3903static struct port *
064af421
BP
3904port_create(struct bridge *br, const char *name)
3905{
3906 struct port *port;
3907
ec6fde61 3908 port = xzalloc(sizeof *port);
064af421 3909 port->bridge = br;
064af421
BP
3910 port->vlan = -1;
3911 port->trunks = NULL;
3912 port->name = xstrdup(name);
990cda85 3913 port->active_iface = NULL;
83db7968 3914 list_init(&port->ifaces);
064af421 3915
8052fb14 3916 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
064af421
BP
3917
3918 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
3919 bridge_flush(br);
76343538
BP
3920
3921 return port;
064af421
BP
3922}
3923
c8143c88
BP
3924static const char *
3925get_port_other_config(const struct ovsrec_port *port, const char *key,
3926 const char *default_value)
3927{
af6278e1
BP
3928 const char *value;
3929
3930 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
3931 key);
c8143c88
BP
3932 return value ? value : default_value;
3933}
3934
c25c91fd
EJ
3935static const char *
3936get_interface_other_config(const struct ovsrec_interface *iface,
3937 const char *key, const char *default_value)
3938{
3939 const char *value;
3940
3941 value = get_ovsrec_key_value(&iface->header_,
3942 &ovsrec_interface_col_other_config, key);
3943 return value ? value : default_value;
3944}
3945
4a1ee6ae
BP
3946static void
3947port_del_ifaces(struct port *port, const struct ovsrec_port *cfg)
3948{
83db7968 3949 struct iface *iface, *next;
b3c01ed3 3950 struct sset new_ifaces;
4a1ee6ae
BP
3951 size_t i;
3952
3953 /* Collect list of new interfaces. */
b3c01ed3 3954 sset_init(&new_ifaces);
4a1ee6ae
BP
3955 for (i = 0; i < cfg->n_interfaces; i++) {
3956 const char *name = cfg->interfaces[i]->name;
b3c01ed3 3957 sset_add(&new_ifaces, name);
4a1ee6ae
BP
3958 }
3959
3960 /* Get rid of deleted interfaces. */
83db7968 3961 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
b3c01ed3 3962 if (!sset_contains(&new_ifaces, iface->name)) {
13f5f5b0 3963 iface_destroy(iface);
4a1ee6ae
BP
3964 }
3965 }
3966
b3c01ed3 3967 sset_destroy(&new_ifaces);
4a1ee6ae
BP
3968}
3969
d5346278
BP
3970/* Expires all MAC learning entries associated with 'port' and forces ofproto
3971 * to revalidate every flow. */
3972static void
3973port_flush_macs(struct port *port)
3974{
3975 struct bridge *br = port->bridge;
3976 struct mac_learning *ml = br->ml;
3977 struct mac_entry *mac, *next_mac;
3978
3979 bridge_flush(br);
3980 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
3981 if (mac->port.p == port) {
3982 mac_learning_expire(ml, mac);
3983 }
3984 }
3985}
3986
064af421 3987static void
76343538 3988port_reconfigure(struct port *port, const struct ovsrec_port *cfg)
064af421 3989{
63331829 3990 const char *detect_mode;
b3c01ed3 3991 struct sset new_ifaces;
c25c91fd 3992 long long int next_rebalance, miimon_next_update, lacp_priority;
d5346278 3993 bool need_flush = false;
064af421
BP
3994 unsigned long *trunks;
3995 int vlan;
3996 size_t i;
3997
76343538
BP
3998 port->cfg = cfg;
3999
4a1ee6ae 4000 /* Update settings. */
76343538
BP
4001 port->updelay = cfg->bond_updelay;
4002 if (port->updelay < 0) {
4003 port->updelay = 0;
4004 }
7ef6b685 4005 port->downdelay = cfg->bond_downdelay;
76343538
BP
4006 if (port->downdelay < 0) {
4007 port->downdelay = 0;
064af421 4008 }
c8143c88
BP
4009 port->bond_rebalance_interval = atoi(
4010 get_port_other_config(cfg, "bond-rebalance-interval", "10000"));
4011 if (port->bond_rebalance_interval < 1000) {
4012 port->bond_rebalance_interval = 1000;
4013 }
4014 next_rebalance = time_msec() + port->bond_rebalance_interval;
4015 if (port->bond_next_rebalance > next_rebalance) {
4016 port->bond_next_rebalance = next_rebalance;
4017 }
064af421 4018
63331829
EJ
4019 detect_mode = get_port_other_config(cfg, "bond-detect-mode",
4020 "carrier");
4021
e5ed989d
EJ
4022 netdev_monitor_destroy(port->monitor);
4023 port->monitor = NULL;
4024
4025 if (strcmp(detect_mode, "miimon")) {
4026 port->monitor = netdev_monitor_create();
4027
4028 if (strcmp(detect_mode, "carrier")) {
4029 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
4030 "defaulting to carrier", port->name, detect_mode);
4031 }
63331829
EJ
4032 }
4033
e5ed989d 4034 port->miimon_interval = atoi(
63331829 4035 get_port_other_config(cfg, "bond-miimon-interval", "200"));
e5ed989d
EJ
4036 if (port->miimon_interval < 100) {
4037 port->miimon_interval = 100;
63331829 4038 }
e5ed989d
EJ
4039 miimon_next_update = time_msec() + port->miimon_interval;
4040 if (port->miimon_next_update > miimon_next_update) {
4041 port->miimon_next_update = miimon_next_update;
63331829
EJ
4042 }
4043
27dcaa1a
EJ
4044 if (!port->cfg->bond_mode ||
4045 !strcmp(port->cfg->bond_mode, bond_mode_to_string(BM_SLB))) {
4046 port->bond_mode = BM_SLB;
4047 } else if (!strcmp(port->cfg->bond_mode, bond_mode_to_string(BM_AB))) {
4048 port->bond_mode = BM_AB;
9f5073d8
EJ
4049 } else if (!strcmp(port->cfg->bond_mode, bond_mode_to_string(BM_TCP))) {
4050 port->bond_mode = BM_TCP;
be02e7c3 4051 } else {
27dcaa1a
EJ
4052 port->bond_mode = BM_SLB;
4053 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
4054 port->name, port->cfg->bond_mode,
4055 bond_mode_to_string(port->bond_mode));
be02e7c3
EJ
4056 }
4057
4a1ee6ae 4058 /* Add new interfaces and update 'cfg' member of existing ones. */
b3c01ed3 4059 sset_init(&new_ifaces);
4a1ee6ae
BP
4060 for (i = 0; i < cfg->n_interfaces; i++) {
4061 const struct ovsrec_interface *if_cfg = cfg->interfaces[i];
76343538
BP
4062 struct iface *iface;
4063
b3c01ed3 4064 if (!sset_add(&new_ifaces, if_cfg->name)) {
4a1ee6ae
BP
4065 VLOG_WARN("port %s: %s specified twice as port interface",
4066 port->name, if_cfg->name);
bcd49a45 4067 iface_set_ofport(if_cfg, -1);
4a1ee6ae
BP
4068 continue;
4069 }
4070
4071 iface = iface_lookup(port->bridge, if_cfg->name);
4072 if (iface) {
4073 if (iface->port != port) {
4074 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
4075 "removing from %s",
4076 port->bridge->name, if_cfg->name, iface->port->name);
4077 continue;
4078 }
149f577a 4079 iface->cfg = if_cfg;
4a1ee6ae 4080 } else {
6cefe1da 4081 iface = iface_create(port, if_cfg);
064af421 4082 }
6cefe1da
BP
4083
4084 /* Determine interface type. The local port always has type
4085 * "internal". Other ports take their type from the database and
4086 * default to "system" if none is specified. */
4087 iface->type = (!strcmp(if_cfg->name, port->bridge->name) ? "internal"
4088 : if_cfg->type[0] ? if_cfg->type
4089 : "system");
c25c91fd
EJ
4090
4091 lacp_priority =
4092 atoi(get_interface_other_config(if_cfg, "lacp-port-priority",
4093 "0"));
4094
4095 if (lacp_priority <= 0 || lacp_priority > UINT16_MAX) {
4096 iface->lacp_priority = UINT16_MAX;
4097 } else {
4098 iface->lacp_priority = lacp_priority;
4099 }
064af421 4100 }
b3c01ed3 4101 sset_destroy(&new_ifaces);
064af421 4102
269340fa
EJ
4103 port->lacp_fast = !strcmp(get_port_other_config(cfg, "lacp-time", "slow"),
4104 "fast");
4105
c25c91fd
EJ
4106 lacp_priority =
4107 atoi(get_port_other_config(cfg, "lacp-system-priority", "0"));
4108
4109 if (lacp_priority <= 0 || lacp_priority > UINT16_MAX) {
4110 /* Prefer bondable links if unspecified. */
4111 port->lacp_priority = port->n_ifaces > 1 ? UINT16_MAX - 1 : UINT16_MAX;
4112 } else {
4113 port->lacp_priority = lacp_priority;
4114 }
4115
4116 if (!port->cfg->lacp) {
4117 /* XXX when LACP implementation has been sufficiently tested, enable by
4118 * default and make active on bonded ports. */
6aa74308
EJ
4119 lacp_destroy(port->lacp);
4120 port->lacp = NULL;
c25c91fd 4121 } else if (!strcmp(port->cfg->lacp, "off")) {
6aa74308
EJ
4122 lacp_destroy(port->lacp);
4123 port->lacp = NULL;
c25c91fd 4124 } else if (!strcmp(port->cfg->lacp, "active")) {
6aa74308
EJ
4125 if (!port->lacp) {
4126 port->lacp = lacp_create();
4127 }
4128 port->lacp_active = true;
c25c91fd 4129 } else if (!strcmp(port->cfg->lacp, "passive")) {
6aa74308
EJ
4130 if (!port->lacp) {
4131 port->lacp = lacp_create();
4132 }
4133 port->lacp_active = false;
c25c91fd
EJ
4134 } else {
4135 VLOG_WARN("port %s: unknown LACP mode %s",
4136 port->name, port->cfg->lacp);
6aa74308
EJ
4137 lacp_destroy(port->lacp);
4138 port->lacp = NULL;
c25c91fd
EJ
4139 }
4140
064af421
BP
4141 /* Get VLAN tag. */
4142 vlan = -1;
76343538
BP
4143 if (cfg->tag) {
4144 if (port->n_ifaces < 2) {
4145 vlan = *cfg->tag;
064af421
BP
4146 if (vlan >= 0 && vlan <= 4095) {
4147 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
76343538
BP
4148 } else {
4149 vlan = -1;
064af421
BP
4150 }
4151 } else {
4152 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
4153 * they even work as-is. But they have not been tested. */
4154 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
4155 port->name);
4156 }
4157 }
4158 if (port->vlan != vlan) {
4159 port->vlan = vlan;
d5346278 4160 need_flush = true;
064af421
BP
4161 }
4162
4163 /* Get trunked VLANs. */
4164 trunks = NULL;
3e9c481c 4165 if (vlan < 0 && cfg->n_trunks) {
76343538 4166 size_t n_errors;
064af421
BP
4167
4168 trunks = bitmap_allocate(4096);
064af421 4169 n_errors = 0;
76343538
BP
4170 for (i = 0; i < cfg->n_trunks; i++) {
4171 int trunk = cfg->trunks[i];
064af421
BP
4172 if (trunk >= 0) {
4173 bitmap_set1(trunks, trunk);
4174 } else {
4175 n_errors++;
4176 }
4177 }
4178 if (n_errors) {
4179 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
76343538 4180 port->name, cfg->n_trunks);
064af421 4181 }
76343538 4182 if (n_errors == cfg->n_trunks) {
3e9c481c 4183 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
76343538 4184 port->name);
3e9c481c
BP
4185 bitmap_free(trunks);
4186 trunks = NULL;
064af421 4187 }
3e9c481c
BP
4188 } else if (vlan >= 0 && cfg->n_trunks) {
4189 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
4190 port->name);
064af421
BP
4191 }
4192 if (trunks == NULL
4193 ? port->trunks != NULL
4194 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
d5346278 4195 need_flush = true;
064af421
BP
4196 }
4197 bitmap_free(port->trunks);
4198 port->trunks = trunks;
d5346278
BP
4199
4200 if (need_flush) {
4201 port_flush_macs(port);
4202 }
064af421
BP
4203}
4204
4205static void
4206port_destroy(struct port *port)
4207{
4208 if (port) {
4209 struct bridge *br = port->bridge;
83db7968 4210 struct iface *iface, *next;
37e7f427 4211 int i;
064af421 4212
064af421
BP
4213 for (i = 0; i < MAX_MIRRORS; i++) {
4214 struct mirror *m = br->mirrors[i];
4215 if (m && m->out_port == port) {
4216 mirror_destroy(m);
4217 }
4218 }
4219
83db7968
BP
4220 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
4221 iface_destroy(iface);
064af421
BP
4222 }
4223
8052fb14 4224 hmap_remove(&br->ports, &port->hmap_node);
064af421 4225
99707a7a
JP
4226 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
4227
d5346278
BP
4228 port_flush_macs(port);
4229
60837272 4230 lacp_destroy(port->lacp);
0bb5c3ec 4231 netdev_monitor_destroy(port->monitor);
064af421 4232 bitmap_free(port->trunks);
fe0cfbf6 4233 free(port->bond_hash);
064af421
BP
4234 free(port->name);
4235 free(port);
064af421
BP
4236 }
4237}
4238
4239static struct port *
4240port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
4241{
4242 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
4243 return iface ? iface->port : NULL;
4244}
4245
4246static struct port *
4247port_lookup(const struct bridge *br, const char *name)
4248{
8052fb14
BP
4249 struct port *port;
4250
4251 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
4252 &br->ports) {
4253 if (!strcmp(port->name, name)) {
4254 return port;
4255 }
4256 }
4257 return NULL;
064af421
BP
4258}
4259
da285df4
BP
4260static struct iface *
4261port_lookup_iface(const struct port *port, const char *name)
4262{
4a1ee6ae
BP
4263 struct iface *iface = iface_lookup(port->bridge, name);
4264 return iface && iface->port == port ? iface : NULL;
da285df4
BP
4265}
4266
c25c91fd
EJ
4267static void
4268port_update_lacp(struct port *port)
4269{
6aa74308 4270 if (port->lacp) {
83db7968 4271 struct iface *iface;
c25c91fd 4272
6aa74308
EJ
4273 lacp_configure(port->lacp, port->name,
4274 port->bridge->ea, port->lacp_priority,
269340fa 4275 port->lacp_active, port->lacp_fast);
c25c91fd 4276
83db7968 4277 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
6aa74308
EJ
4278 lacp_slave_register(port->lacp, iface, iface->name,
4279 iface->dp_ifidx, iface->lacp_priority);
c25c91fd
EJ
4280 }
4281 }
c25c91fd
EJ
4282}
4283
064af421
BP
4284static void
4285port_update_bonding(struct port *port)
4286{
4287 if (port->n_ifaces < 2) {
4288 /* Not a bonded port. */
0b8b6f71
BP
4289 free(port->bond_hash);
4290 port->bond_hash = NULL;
be02e7c3 4291 port->bond_fake_iface = false;
990cda85 4292 port->active_iface = NULL;
55eccb86 4293 port->no_ifaces_tag = 0;
064af421 4294 } else {
0bb5c3ec 4295 size_t i;
064af421 4296
9f5073d8 4297 if (port->bond_mode != BM_AB && !port->bond_hash) {
064af421
BP
4298 port->bond_hash = xcalloc(BOND_MASK + 1, sizeof *port->bond_hash);
4299 for (i = 0; i <= BOND_MASK; i++) {
4300 struct bond_entry *e = &port->bond_hash[i];
c17f0d5e 4301 e->iface = NULL;
064af421
BP
4302 e->tx_bytes = 0;
4303 }
c8143c88
BP
4304 port->bond_next_rebalance
4305 = time_msec() + port->bond_rebalance_interval;
9f5073d8 4306 } else if (port->bond_mode == BM_AB) {
be02e7c3
EJ
4307 free(port->bond_hash);
4308 port->bond_hash = NULL;
064af421 4309 }
55eccb86
EJ
4310
4311 if (!port->no_ifaces_tag) {
4312 port->no_ifaces_tag = tag_create_random();
4313 }
4314
990cda85 4315 if (!port->active_iface) {
55eccb86
EJ
4316 bond_choose_active_iface(port);
4317 }
4318
76343538 4319 port->bond_fake_iface = port->cfg->bond_fake_iface;
55eccb86
EJ
4320 if (port->bond_fake_iface) {
4321 port->bond_next_fake_iface_update = time_msec();
4322 }
0bb5c3ec 4323
064af421
BP
4324 }
4325}
064af421
BP
4326\f
4327/* Interface functions. */
4328
76343538 4329static struct iface *
a740f0de 4330iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
064af421 4331{
4a1ee6ae 4332 struct bridge *br = port->bridge;
064af421 4333 struct iface *iface;
a740f0de 4334 char *name = if_cfg->name;
064af421 4335
ec6fde61 4336 iface = xzalloc(sizeof *iface);
064af421 4337 iface->port = port;
064af421
BP
4338 iface->name = xstrdup(name);
4339 iface->dp_ifidx = -1;
4340 iface->tag = tag_create_random();
064af421 4341 iface->delay_expires = LLONG_MAX;
0c6aea3f 4342 iface->netdev = NULL;
149f577a 4343 iface->cfg = if_cfg;
064af421 4344
2457b24f
JG
4345 shash_add_assert(&br->iface_by_name, iface->name, iface);
4346
83db7968
BP
4347 list_push_back(&port->ifaces, &iface->port_elem);
4348 port->n_ifaces++;
4349
064af421 4350 if (port->n_ifaces > 1) {
4a1ee6ae 4351 br->has_bonded_ports = true;
064af421
BP
4352 }
4353
4354 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
4355
4a1ee6ae 4356 bridge_flush(br);
76343538
BP
4357
4358 return iface;
064af421
BP
4359}
4360
4361static void
4362iface_destroy(struct iface *iface)
4363{
4364 if (iface) {
4365 struct port *port = iface->port;
4366 struct bridge *br = port->bridge;
990cda85 4367 bool del_active = port->active_iface == iface;
c17f0d5e
BP
4368
4369 if (port->bond_hash) {
4370 struct bond_entry *e;
4371 for (e = port->bond_hash; e <= &port->bond_hash[BOND_MASK]; e++) {
4372 if (e->iface == iface) {
4373 e->iface = NULL;
4374 }
4375 }
4376 }
064af421 4377
6aa74308
EJ
4378 if (iface->port->lacp) {
4379 lacp_slave_unregister(iface->port->lacp, iface);
4380 }
4381
e5ed989d 4382 if (port->monitor && iface->netdev) {
7ca5f243
EJ
4383 netdev_monitor_remove(port->monitor, iface->netdev);
4384 }
4385
4a1ee6ae
BP
4386 shash_find_and_delete_assert(&br->iface_by_name, iface->name);
4387
064af421 4388 if (iface->dp_ifidx >= 0) {
d9a8717a 4389 hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
064af421
BP
4390 }
4391
83db7968
BP
4392 list_remove(&iface->port_elem);
4393 port->n_ifaces--;
064af421 4394
0c6aea3f 4395 netdev_close(iface->netdev);
064af421
BP
4396
4397 if (del_active) {
064af421 4398 bond_choose_active_iface(port);
2303f3b2 4399 bond_send_learning_packets(port);
064af421
BP
4400 }
4401
a740f0de
JG
4402 free(iface->name);
4403 free(iface);
4404
064af421
BP
4405 bridge_flush(port->bridge);
4406 }
4407}
4408
4409static struct iface *
4410iface_lookup(const struct bridge *br, const char *name)
4411{
4a1ee6ae 4412 return shash_find_data(&br->iface_by_name, name);
064af421
BP
4413}
4414
e8fe3026
EJ
4415static struct iface *
4416iface_find(const char *name)
4417{
4418 const struct bridge *br;
4419
4420 LIST_FOR_EACH (br, node, &all_bridges) {
4421 struct iface *iface = iface_lookup(br, name);
4422
4423 if (iface) {
4424 return iface;
4425 }
4426 }
4427 return NULL;
4428}
4429
064af421
BP
4430static struct iface *
4431iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
4432{
d9a8717a
BP
4433 struct iface *iface;
4434
4e8e4213 4435 HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
d9a8717a
BP
4436 hash_int(dp_ifidx, 0), &br->ifaces) {
4437 if (iface->dp_ifidx == dp_ifidx) {
4438 return iface;
4439 }
4440 }
4441 return NULL;
064af421 4442}
557d8e6c 4443
52df17e7
BP
4444/* Set Ethernet address of 'iface', if one is specified in the configuration
4445 * file. */
4446static void
4447iface_set_mac(struct iface *iface)
4448{
76343538 4449 uint8_t ea[ETH_ADDR_LEN];
52df17e7 4450
76343538 4451 if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
52df17e7
BP
4452 if (eth_addr_is_multicast(ea)) {
4453 VLOG_ERR("interface %s: cannot set MAC to multicast address",
4454 iface->name);
4455 } else if (iface->dp_ifidx == ODPP_LOCAL) {
4456 VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
4457 iface->name, iface->name);
4458 } else {
4d678233 4459 int error = netdev_set_etheraddr(iface->netdev, ea);
52df17e7
BP
4460 if (error) {
4461 VLOG_ERR("interface %s: setting MAC failed (%s)",
4462 iface->name, strerror(error));
4463 }
4464 }
4465 }
4466}
c1c9c9c4 4467
bcd49a45
BP
4468/* Sets the ofport column of 'if_cfg' to 'ofport'. */
4469static void
4470iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
4471{
4472 if (if_cfg) {
4473 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
4474 }
4475}
4476
43776b8f
BP
4477/* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
4478 *
4479 * The value strings in '*shash' are taken directly from values[], not copied,
4480 * so the caller should not modify or free them. */
c1c9c9c4
BP
4481static void
4482shash_from_ovs_idl_map(char **keys, char **values, size_t n,
4483 struct shash *shash)
4484{
4485 size_t i;
4486
4487 shash_init(shash);
4488 for (i = 0; i < n; i++) {
4489 shash_add(shash, keys[i], values[i]);
4490 }
4491}
4492
ea763e0e
EJ
4493/* Creates 'keys' and 'values' arrays from 'shash'.
4494 *
4495 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
4496 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
4497 * are populated with with strings taken directly from 'shash' and thus have
4498 * the same ownership of the key-value pairs in shash.
4499 */
4500static void
4501shash_to_ovs_idl_map(struct shash *shash,
4502 char ***keys, char ***values, size_t *n)
4503{
4504 size_t i, count;
4505 char **k, **v;
4506 struct shash_node *sn;
4507
4508 count = shash_count(shash);
4509
4510 k = xmalloc(count * sizeof *k);
4511 v = xmalloc(count * sizeof *v);
4512
4513 i = 0;
4514 SHASH_FOR_EACH(sn, shash) {
4515 k[i] = sn->name;
4516 v[i] = sn->data;
4517 i++;
4518 }
4519
4520 *n = count;
4521 *keys = k;
4522 *values = v;
4523}
4524
c1c9c9c4
BP
4525struct iface_delete_queues_cbdata {
4526 struct netdev *netdev;
44fca7f9 4527 const struct ovsdb_datum *queues;
c1c9c9c4
BP
4528};
4529
4530static bool
44fca7f9 4531queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
c1c9c9c4 4532{
44fca7f9 4533 union ovsdb_atom atom;
c1c9c9c4 4534
44fca7f9
BP
4535 atom.integer = target;
4536 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
c1c9c9c4
BP
4537}
4538
4539static void
4540iface_delete_queues(unsigned int queue_id,
4541 const struct shash *details OVS_UNUSED, void *cbdata_)
4542{
4543 struct iface_delete_queues_cbdata *cbdata = cbdata_;
4544
44fca7f9 4545 if (!queue_ids_include(cbdata->queues, queue_id)) {
c1c9c9c4
BP
4546 netdev_delete_queue(cbdata->netdev, queue_id);
4547 }
4548}
4549
e5ed989d 4550static void
0b8024eb 4551iface_update_carrier(struct iface *iface)
e5ed989d 4552{
0b8024eb 4553 bool carrier = iface_get_carrier(iface);
e5ed989d
EJ
4554 if (carrier == iface->up) {
4555 return;
4556 }
4557
4558 iface->up = carrier;
6aa74308
EJ
4559 if (iface->port->lacp) {
4560 lacp_slave_carrier_changed(iface->port->lacp, iface);
e5ed989d
EJ
4561 }
4562}
4563
c1c9c9c4
BP
4564static void
4565iface_update_qos(struct iface *iface, const struct ovsrec_qos *qos)
4566{
4567 if (!qos || qos->type[0] == '\0') {
4568 netdev_set_qos(iface->netdev, NULL, NULL);
4569 } else {
4570 struct iface_delete_queues_cbdata cbdata;
4571 struct shash details;
4572 size_t i;
4573
4574 /* Configure top-level Qos for 'iface'. */
4575 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
4576 qos->n_other_config, &details);
4577 netdev_set_qos(iface->netdev, qos->type, &details);
4578 shash_destroy(&details);
4579
4580 /* Deconfigure queues that were deleted. */
4581 cbdata.netdev = iface->netdev;
44fca7f9
BP
4582 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
4583 OVSDB_TYPE_UUID);
c1c9c9c4
BP
4584 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
4585
4586 /* Configure queues for 'iface'. */
4587 for (i = 0; i < qos->n_queues; i++) {
4588 const struct ovsrec_queue *queue = qos->value_queues[i];
4589 unsigned int queue_id = qos->key_queues[i];
4590
4591 shash_from_ovs_idl_map(queue->key_other_config,
4592 queue->value_other_config,
4593 queue->n_other_config, &details);
4594 netdev_set_queue(iface->netdev, queue_id, &details);
4595 shash_destroy(&details);
4596 }
4597 }
4598}
b31bcf60
EJ
4599
4600static void
4601iface_update_cfm(struct iface *iface)
4602{
4603 size_t i;
e7934396 4604 struct cfm cfm;
b31bcf60
EJ
4605 uint16_t *remote_mps;
4606 struct ovsrec_monitor *mon;
a58727fb 4607 uint8_t maid[CCM_MAID_LEN];
b31bcf60
EJ
4608
4609 mon = iface->cfg->monitor;
4610
4611 if (!mon) {
e7934396 4612 ofproto_iface_clear_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
b31bcf60
EJ
4613 return;
4614 }
4615
b31bcf60
EJ
4616 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
4617 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
4618 return;
4619 }
4620
e7934396
BP
4621 cfm.mpid = mon->mpid;
4622 cfm.interval = mon->interval ? *mon->interval : 1000;
b31bcf60 4623
e7934396 4624 memcpy(cfm.maid, maid, sizeof cfm.maid);
b31bcf60
EJ
4625
4626 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
4627 for(i = 0; i < mon->n_remote_mps; i++) {
4628 remote_mps[i] = mon->remote_mps[i]->mpid;
4629 }
b31bcf60 4630
e7934396
BP
4631 ofproto_iface_set_cfm(iface->port->bridge->ofproto, iface->dp_ifidx,
4632 &cfm, remote_mps, mon->n_remote_mps);
4633 free(remote_mps);
b31bcf60 4634}
0b8024eb
BP
4635
4636/* Read carrier or miimon status directly from 'iface''s netdev, according to
4637 * how 'iface''s port is configured.
4638 *
4639 * Returns true if 'iface' is up, false otherwise. */
4640static bool
4641iface_get_carrier(const struct iface *iface)
4642{
4643 return (iface->port->monitor
4644 ? netdev_get_carrier(iface->netdev)
4645 : netdev_get_miimon(iface->netdev));
4646}
064af421
BP
4647\f
4648/* Port mirroring. */
4649
dd0d105c
BP
4650static struct mirror *
4651mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
4652{
4653 int i;
4654
4655 for (i = 0; i < MAX_MIRRORS; i++) {
4656 struct mirror *m = br->mirrors[i];
4657 if (m && uuid_equals(uuid, &m->uuid)) {
4658 return m;
4659 }
4660 }
4661 return NULL;
4662}
4663
064af421 4664static void
37e7f427 4665mirror_reconfigure(struct bridge *br)
064af421 4666{
f2d7fd66 4667 unsigned long *rspan_vlans;
8052fb14 4668 struct port *port;
37e7f427 4669 int i;
064af421 4670
dd0d105c 4671 /* Get rid of deleted mirrors. */
064af421 4672 for (i = 0; i < MAX_MIRRORS; i++) {
dd0d105c
BP
4673 struct mirror *m = br->mirrors[i];
4674 if (m) {
4675 const struct ovsdb_datum *mc;
4676 union ovsdb_atom atom;
4677
4678 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
4679 atom.uuid = br->mirrors[i]->uuid;
4680 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
4681 mirror_destroy(m);
4682 }
064af421
BP
4683 }
4684 }
4685
dd0d105c 4686 /* Add new mirrors and reconfigure existing ones. */
37e7f427
BP
4687 for (i = 0; i < br->cfg->n_mirrors; i++) {
4688 struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
dd0d105c
BP
4689 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
4690 if (m) {
4691 mirror_reconfigure_one(m, cfg);
4692 } else {
4693 mirror_create(br, cfg);
064af421
BP
4694 }
4695 }
4696
4697 /* Update port reserved status. */
8052fb14
BP
4698 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
4699 port->is_mirror_output_port = false;
064af421
BP
4700 }
4701 for (i = 0; i < MAX_MIRRORS; i++) {
4702 struct mirror *m = br->mirrors[i];
4703 if (m && m->out_port) {
4704 m->out_port->is_mirror_output_port = true;
4705 }
4706 }
f2d7fd66 4707
8f30d09a 4708 /* Update flooded vlans (for RSPAN). */
f2d7fd66 4709 rspan_vlans = NULL;
37e7f427 4710 if (br->cfg->n_flood_vlans) {
f2d7fd66
JG
4711 rspan_vlans = bitmap_allocate(4096);
4712
37e7f427
BP
4713 for (i = 0; i < br->cfg->n_flood_vlans; i++) {
4714 int64_t vlan = br->cfg->flood_vlans[i];
4715 if (vlan >= 0 && vlan < 4096) {
f2d7fd66 4716 bitmap_set1(rspan_vlans, vlan);
37e7f427 4717 VLOG_INFO("bridge %s: disabling learning on vlan %"PRId64,
42061b2a 4718 br->name, vlan);
f2d7fd66 4719 } else {
37e7f427
BP
4720 VLOG_ERR("bridge %s: invalid value %"PRId64 "for flood VLAN",
4721 br->name, vlan);
f2d7fd66
JG
4722 }
4723 }
4724 }
8f30d09a 4725 if (mac_learning_set_flood_vlans(br->ml, rspan_vlans)) {
f2d7fd66 4726 bridge_flush(br);
d5346278 4727 mac_learning_flush(br->ml);
f2d7fd66 4728 }
064af421
BP
4729}
4730
dd0d105c
BP
4731static void
4732mirror_create(struct bridge *br, struct ovsrec_mirror *cfg)
064af421
BP
4733{
4734 struct mirror *m;
4735 size_t i;
4736
4737 for (i = 0; ; i++) {
4738 if (i >= MAX_MIRRORS) {
4739 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
dd0d105c
BP
4740 "cannot create %s", br->name, MAX_MIRRORS, cfg->name);
4741 return;
064af421
BP
4742 }
4743 if (!br->mirrors[i]) {
4744 break;
4745 }
4746 }
4747
dd0d105c 4748 VLOG_INFO("created port mirror %s on bridge %s", cfg->name, br->name);
064af421 4749 bridge_flush(br);
d5346278 4750 mac_learning_flush(br->ml);
064af421 4751
ec6fde61 4752 br->mirrors[i] = m = xzalloc(sizeof *m);
064af421
BP
4753 m->bridge = br;
4754 m->idx = i;
dd0d105c 4755 m->name = xstrdup(cfg->name);
b3c01ed3
BP
4756 sset_init(&m->src_ports);
4757 sset_init(&m->dst_ports);
064af421
BP
4758 m->vlans = NULL;
4759 m->n_vlans = 0;
4760 m->out_vlan = -1;
4761 m->out_port = NULL;
37e7f427 4762
dd0d105c 4763 mirror_reconfigure_one(m, cfg);
064af421
BP
4764}
4765
4766static void
4767mirror_destroy(struct mirror *m)
4768{
4769 if (m) {
4770 struct bridge *br = m->bridge;
8052fb14 4771 struct port *port;
064af421 4772
8052fb14
BP
4773 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
4774 port->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
4775 port->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
064af421
BP
4776 }
4777
b3c01ed3
BP
4778 sset_destroy(&m->src_ports);
4779 sset_destroy(&m->dst_ports);
064af421
BP
4780 free(m->vlans);
4781
4782 m->bridge->mirrors[m->idx] = NULL;
786880a5 4783 free(m->name);
064af421
BP
4784 free(m);
4785
4786 bridge_flush(br);
d5346278 4787 mac_learning_flush(br->ml);
064af421
BP
4788 }
4789}
4790
4791static void
37e7f427 4792mirror_collect_ports(struct mirror *m, struct ovsrec_port **ports, int n_ports,
b3c01ed3 4793 struct sset *names)
064af421 4794{
064af421
BP
4795 size_t i;
4796
37e7f427
BP
4797 for (i = 0; i < n_ports; i++) {
4798 const char *name = ports[i]->name;
064af421 4799 if (port_lookup(m->bridge, name)) {
b3c01ed3 4800 sset_add(names, name);
064af421 4801 } else {
37e7f427
BP
4802 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
4803 "port %s", m->bridge->name, m->name, name);
064af421
BP
4804 }
4805 }
064af421
BP
4806}
4807
4808static size_t
37e7f427
BP
4809mirror_collect_vlans(struct mirror *m, const struct ovsrec_mirror *cfg,
4810 int **vlans)
064af421 4811{
37e7f427
BP
4812 size_t n_vlans;
4813 size_t i;
064af421 4814
0b523ea7 4815 *vlans = xmalloc(sizeof **vlans * cfg->n_select_vlan);
064af421 4816 n_vlans = 0;
37e7f427
BP
4817 for (i = 0; i < cfg->n_select_vlan; i++) {
4818 int64_t vlan = cfg->select_vlan[i];
4819 if (vlan < 0 || vlan > 4095) {
4820 VLOG_WARN("bridge %s: mirror %s selects invalid VLAN %"PRId64,
4821 m->bridge->name, m->name, vlan);
064af421
BP
4822 } else {
4823 (*vlans)[n_vlans++] = vlan;
4824 }
4825 }
4826 return n_vlans;
4827}
4828
4829static bool
4830vlan_is_mirrored(const struct mirror *m, int vlan)
4831{
4832 size_t i;
4833
4834 for (i = 0; i < m->n_vlans; i++) {
4835 if (m->vlans[i] == vlan) {
4836 return true;
4837 }
4838 }
4839 return false;
4840}
4841
4842static bool
4843port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
4844{
4845 size_t i;
4846
4847 for (i = 0; i < m->n_vlans; i++) {
4848 if (port_trunks_vlan(p, m->vlans[i])) {
4849 return true;
4850 }
4851 }
4852 return false;
4853}
4854
4855static void
37e7f427 4856mirror_reconfigure_one(struct mirror *m, struct ovsrec_mirror *cfg)
064af421 4857{
b3c01ed3 4858 struct sset src_ports, dst_ports;
064af421 4859 mirror_mask_t mirror_bit;
064af421 4860 struct port *out_port;
8052fb14 4861 struct port *port;
064af421
BP
4862 int out_vlan;
4863 size_t n_vlans;
4864 int *vlans;
064af421 4865
dd0d105c
BP
4866 /* Set name. */
4867 if (strcmp(cfg->name, m->name)) {
4868 free(m->name);
4869 m->name = xstrdup(cfg->name);
4870 }
4871
064af421 4872 /* Get output port. */
37e7f427
BP
4873 if (cfg->output_port) {
4874 out_port = port_lookup(m->bridge, cfg->output_port->name);
064af421 4875 if (!out_port) {
37e7f427
BP
4876 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
4877 m->bridge->name, m->name);
064af421 4878 mirror_destroy(m);
064af421
BP
4879 return;
4880 }
4881 out_vlan = -1;
4882
37e7f427
BP
4883 if (cfg->output_vlan) {
4884 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
4885 "output vlan; ignoring output vlan",
4886 m->bridge->name, m->name);
064af421 4887 }
37e7f427 4888 } else if (cfg->output_vlan) {
064af421 4889 out_port = NULL;
37e7f427 4890 out_vlan = *cfg->output_vlan;
064af421 4891 } else {
37e7f427
BP
4892 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
4893 m->bridge->name, m->name);
064af421 4894 mirror_destroy(m);
064af421
BP
4895 return;
4896 }
4897
b3c01ed3
BP
4898 sset_init(&src_ports);
4899 sset_init(&dst_ports);
939ff267 4900 if (cfg->select_all) {
8052fb14 4901 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
b3c01ed3
BP
4902 sset_add(&src_ports, port->name);
4903 sset_add(&dst_ports, port->name);
939ff267
BP
4904 }
4905 vlans = NULL;
4906 n_vlans = 0;
4907 } else {
4908 /* Get ports, and drop duplicates and ports that don't exist. */
4909 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
4910 &src_ports);
4911 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
4912 &dst_ports);
064af421 4913
939ff267
BP
4914 /* Get all the vlans, and drop duplicate and invalid vlans. */
4915 n_vlans = mirror_collect_vlans(m, cfg, &vlans);
37e7f427 4916 }
064af421
BP
4917
4918 /* Update mirror data. */
b3c01ed3
BP
4919 if (!sset_equals(&m->src_ports, &src_ports)
4920 || !sset_equals(&m->dst_ports, &dst_ports)
064af421
BP
4921 || m->n_vlans != n_vlans
4922 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
4923 || m->out_port != out_port
4924 || m->out_vlan != out_vlan) {
4925 bridge_flush(m->bridge);
d5346278 4926 mac_learning_flush(m->bridge->ml);
064af421 4927 }
b3c01ed3
BP
4928 sset_swap(&m->src_ports, &src_ports);
4929 sset_swap(&m->dst_ports, &dst_ports);
064af421
BP
4930 free(m->vlans);
4931 m->vlans = vlans;
4932 m->n_vlans = n_vlans;
4933 m->out_port = out_port;
4934 m->out_vlan = out_vlan;
4935
064af421
BP
4936 /* Update ports. */
4937 mirror_bit = MIRROR_MASK_C(1) << m->idx;
8052fb14 4938 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
b3c01ed3 4939 if (sset_contains(&m->src_ports, port->name)
064af421
BP
4940 || (m->n_vlans
4941 && (!port->vlan
4942 ? port_trunks_any_mirrored_vlan(m, port)
4943 : vlan_is_mirrored(m, port->vlan)))) {
4944 port->src_mirrors |= mirror_bit;
4945 } else {
4946 port->src_mirrors &= ~mirror_bit;
4947 }
4948
b3c01ed3 4949 if (sset_contains(&m->dst_ports, port->name)) {
064af421
BP
4950 port->dst_mirrors |= mirror_bit;
4951 } else {
4952 port->dst_mirrors &= ~mirror_bit;
4953 }
4954 }
4955
4956 /* Clean up. */
b3c01ed3
BP
4957 sset_destroy(&src_ports);
4958 sset_destroy(&dst_ports);
064af421 4959}