]> git.proxmox.com Git - mirror_ovs.git/blame - vswitchd/bridge.c
ovn: Add DHCP support for option 252.
[mirror_ovs.git] / vswitchd / bridge.c
CommitLineData
fed8962a 1/* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
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"
064af421 18#include <errno.h>
064af421 19#include <inttypes.h>
064af421 20#include <stdlib.h>
842733c3 21
a5fb0e29 22#include "async-append.h"
ccc09689 23#include "bfd.h"
064af421 24#include "bitmap.h"
b31bcf60 25#include "cfm.h"
7f8f2757 26#include "connectivity.h"
064af421 27#include "coverage.h"
a7ff9bd7 28#include "daemon.h"
064af421 29#include "dirs.h"
842733c3 30#include "dpif.h"
01961bbd 31#include "dpdk.h"
064af421 32#include "hash.h"
ee89ea7b 33#include "openvswitch/hmap.h"
f145afdc 34#include "hmapx.h"
25d436fb 35#include "if-notifier.h"
cd11000b 36#include "jsonrpc.h"
6aa74308 37#include "lacp.h"
e764773c 38#include "mac-learning.h"
dc2b70ba 39#include "mcast-snooping.h"
064af421 40#include "netdev.h"
178742f9 41#include "nx-match.h"
9a54394a 42#include "ofproto/bond.h"
8cd4882f 43#include "ofproto/ofproto.h"
25d436fb
BW
44#include "openvswitch/dynamic-string.h"
45#include "openvswitch/list.h"
46#include "openvswitch/meta-flow.h"
47#include "openvswitch/ofp-print.h"
25d436fb
BW
48#include "openvswitch/ofpbuf.h"
49#include "openvswitch/vlog.h"
50#include "ovs-lldp.h"
7c5a3bbf 51#include "ovs-numa.h"
25d436fb 52#include "packets.h"
fd016ae3 53#include "openvswitch/poll-loop.h"
7f8f2757 54#include "seq.h"
25d436fb 55#include "sflow_api.h"
76343538 56#include "sha1.h"
ee89ea7b 57#include "openvswitch/shash.h"
79f1cbe9 58#include "smap.h"
064af421 59#include "socket-util.h"
5bd31620 60#include "stream.h"
fe55ad15 61#include "stream-ssl.h"
b3c01ed3 62#include "sset.h"
ce887677 63#include "system-stats.h"
064af421 64#include "timeval.h"
208ff139 65#include "tnl-ports.h"
064af421 66#include "util.h"
da285df4 67#include "unixctl.h"
eaa67ba8 68#include "lib/vswitch-idl.h"
064af421 69#include "xenserver.h"
0fb7b915 70#include "vlan-bitmap.h"
064af421 71
d98e6007 72VLOG_DEFINE_THIS_MODULE(bridge);
064af421 73
d76f09ea
BP
74COVERAGE_DEFINE(bridge_reconfigure);
75
064af421 76struct iface {
2a73b1d7
BP
77 /* These members are always valid.
78 *
79 * They are immutable: they never change between iface_create() and
80 * iface_destroy(). */
ca6ba700 81 struct ovs_list port_elem; /* Element in struct port's "ifaces" list. */
ebea37cc 82 struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
2a73b1d7 83 struct hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
064af421 84 struct port *port; /* Containing port. */
064af421 85 char *name; /* Host network device name. */
0c6aea3f 86 struct netdev *netdev; /* Network device. */
2a73b1d7 87 ofp_port_t ofp_port; /* OpenFlow port number. */
6a5f9a8f 88 uint64_t change_seq;
2a73b1d7
BP
89
90 /* These members are valid only within bridge_reconfigure(). */
6cefe1da 91 const char *type; /* Usually same as cfg->type. */
76343538 92 const struct ovsrec_interface *cfg;
064af421
BP
93};
94
064af421 95struct mirror {
fa066f01
BP
96 struct uuid uuid; /* UUID of this "mirror" record in database. */
97 struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
064af421 98 struct bridge *bridge;
064af421 99 char *name;
9d24de3b 100 const struct ovsrec_mirror *cfg;
064af421
BP
101};
102
064af421 103struct port {
8052fb14 104 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
6e492d81 105 struct bridge *bridge;
8052fb14
BP
106 char *name;
107
1e0b752d 108 const struct ovsrec_port *cfg;
064af421
BP
109
110 /* An ordinary bridge port has 1 interface.
111 * A bridge port for bonding has at least 2 interfaces. */
ca6ba700 112 struct ovs_list ifaces; /* List of "struct iface"s. */
064af421
BP
113};
114
064af421 115struct bridge {
764072fd 116 struct hmap_node node; /* In 'all_bridges'. */
064af421 117 char *name; /* User-specified arbitrary name. */
66da9bef 118 char *type; /* Datapath type. */
74ff3298
JR
119 struct eth_addr ea; /* Bridge Ethernet Address. */
120 struct eth_addr default_ea; /* Default MAC. */
1e0b752d 121 const struct ovsrec_bridge *cfg;
064af421 122
064af421
BP
123 /* OpenFlow switch processing. */
124 struct ofproto *ofproto; /* OpenFlow switch. */
125
064af421 126 /* Bridge ports. */
8052fb14 127 struct hmap ports; /* "struct port"s indexed by name. */
892815f5 128 struct hmap ifaces; /* "struct iface"s indexed by ofp_port. */
ebea37cc 129 struct hmap iface_by_name; /* "struct iface"s indexed by name. */
064af421 130
064af421 131 /* Port mirroring. */
fa066f01 132 struct hmap mirrors; /* "struct mirror" indexed by UUID. */
cfea354b 133
99eef98b
DF
134 /* Auto Attach */
135 struct hmap mappings; /* "struct" indexed by UUID */
136
2a73b1d7
BP
137 /* Used during reconfiguration. */
138 struct shash wanted_ports;
139
cfea354b
BP
140 /* Synthetic local port if necessary. */
141 struct ovsrec_port synth_local_port;
142 struct ovsrec_interface synth_local_iface;
143 struct ovsrec_interface *synth_local_ifacep;
064af421
BP
144};
145
99eef98b
DF
146struct aa_mapping {
147 struct hmap_node hmap_node; /* In struct bridge's "mappings" hmap. */
148 struct bridge *bridge;
72c642e5
BP
149 uint32_t isid;
150 uint16_t vlan;
99eef98b
DF
151 char *br_name;
152};
153
764072fd
BP
154/* All bridges, indexed by name. */
155static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
064af421 156
c5187f17
BP
157/* OVSDB IDL used to obtain configuration. */
158static struct ovsdb_idl *idl;
159
63ff04e8
BP
160/* We want to complete daemonization, fully detaching from our parent process,
161 * only after we have completed our initial configuration, committed our state
162 * to the database, and received confirmation back from the database server
163 * that it applied the commit. This allows our parent process to know that,
164 * post-detach, ephemeral fields such as datapath-id and ofport are very likely
165 * to have already been filled in. (It is only "very likely" rather than
166 * certain because there is always a slim possibility that the transaction will
167 * fail or that some other client has added new bridges, ports, etc. while
168 * ovs-vswitchd was configuring using an old configuration.)
169 *
170 * We only need to do this once for our initial configuration at startup, so
171 * 'initial_config_done' tracks whether we've already done it. While we are
172 * waiting for a response to our commit, 'daemonize_txn' tracks the transaction
173 * itself and is otherwise NULL. */
174static bool initial_config_done;
175static struct ovsdb_idl_txn *daemonize_txn;
176
854a94d9
BP
177/* Most recently processed IDL sequence number. */
178static unsigned int idl_seqno;
179
7f8f2757
JS
180/* Track changes to port connectivity. */
181static uint64_t connectivity_seqno = LLONG_MIN;
182
6a5f9a8f
AW
183/* Status update to database.
184 *
185 * Some information in the database must be kept as up-to-date as possible to
186 * allow controllers to respond rapidly to network outages. Those status are
187 * updated via the 'status_txn'.
188 *
189 * We use the global connectivity sequence number to detect the status change.
190 * Also, to prevent the status update from sending too much to the database,
191 * we check the return status of each update transaction and do not start new
192 * update if the previous transaction status is 'TXN_INCOMPLETE'.
193 *
194 * 'statux_txn' is NULL if there is no ongoing status update.
15c9fbd8
AW
195 *
196 * If the previous database transaction was failed (is not 'TXN_SUCCESS',
197 * 'TXN_UNCHANGED' or 'TXN_INCOMPLETE'), 'status_txn_try_again' is set to true,
198 * which will cause the main thread wake up soon and retry the status update.
6a5f9a8f
AW
199 */
200static struct ovsdb_idl_txn *status_txn;
15c9fbd8 201static bool status_txn_try_again;
6a5f9a8f
AW
202
203/* When the status update transaction returns 'TXN_INCOMPLETE', should register a
204 * timeout in 'STATUS_CHECK_AGAIN_MSEC' to check again. */
205#define STATUS_CHECK_AGAIN_MSEC 100
206
131d04dc
AW
207/* Statistics update to database. */
208static struct ovsdb_idl_txn *stats_txn;
209
35a22d8c 210/* Each time this timer expires, the bridge fetches interface and mirror
cd0cd65f 211 * statistics and pushes them into the database. */
12eb035b
AW
212static int stats_timer_interval;
213static long long int stats_timer = LLONG_MIN;
018f1525 214
99eef98b
DF
215/* Each time this timer expires, the bridge fetches the list of port/VLAN
216 * membership that has been modified by the AA.
217 */
218#define AA_REFRESH_INTERVAL (1000) /* In milliseconds. */
219static long long int aa_refresh_timer = LLONG_MIN;
220
e21c6643
TLSC
221/* Whenever system interfaces are added, removed or change state, the bridge
222 * will be reconfigured.
223 */
224static struct if_notifier *ifnotifier;
0612d739
TLSC
225static struct seq *ifaces_changed;
226static uint64_t last_ifaces_changed;
e21c6643 227
66da9bef 228static void add_del_bridges(const struct ovsrec_open_vswitch *);
aeed7879 229static void bridge_run__(void);
66da9bef 230static void bridge_create(const struct ovsrec_bridge *);
9aad5a5a 231static void bridge_destroy(struct bridge *, bool del);
064af421 232static struct bridge *bridge_lookup(const char *name);
8ca79daa 233static unixctl_cb_func bridge_unixctl_dump_flows;
fa05809b 234static unixctl_cb_func bridge_unixctl_reconnect;
1a048029 235static size_t bridge_get_controllers(const struct bridge *br,
76ce9432 236 struct ovsrec_controller ***controllersp);
2a73b1d7 237static void bridge_collect_wanted_ports(struct bridge *,
2a73b1d7
BP
238 struct shash *wanted_ports);
239static void bridge_delete_ofprotos(void);
240static void bridge_delete_or_reconfigure_ports(struct bridge *);
241static void bridge_del_ports(struct bridge *,
242 const struct shash *wanted_ports);
243static void bridge_add_ports(struct bridge *,
244 const struct shash *wanted_ports);
245
6f90b8f4
BP
246static void bridge_configure_datapath_id(struct bridge *);
247static void bridge_configure_netflow(struct bridge *);
8402c74b 248static void bridge_configure_forward_bpdu(struct bridge *);
c4069512 249static void bridge_configure_mac_table(struct bridge *);
dc2b70ba 250static void bridge_configure_mcast_snooping(struct bridge *);
6f90b8f4 251static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
29089a54 252static void bridge_configure_ipfix(struct bridge *);
7af77bbd 253static void bridge_configure_spanning_tree(struct bridge *);
254750ce 254static void bridge_configure_tables(struct bridge *);
8b6ff729 255static void bridge_configure_dp_desc(struct bridge *);
99eef98b
DF
256static void bridge_configure_aa(struct bridge *);
257static void bridge_aa_refresh_queued(struct bridge *);
258static bool bridge_aa_need_refresh(struct bridge *);
fa066f01
BP
259static void bridge_configure_remotes(struct bridge *,
260 const struct sockaddr_in *managers,
261 size_t n_managers);
74ff3298 262static void bridge_pick_local_hw_addr(struct bridge *, struct eth_addr *ea,
07c318f4 263 struct iface **hw_addr_iface);
064af421 264static uint64_t bridge_pick_datapath_id(struct bridge *,
74ff3298 265 const struct eth_addr bridge_ea,
07c318f4 266 struct iface *hw_addr_iface);
064af421 267static uint64_t dpid_from_hash(const void *, size_t nbytes);
e8192d80
BP
268static bool bridge_has_bond_fake_iface(const struct bridge *,
269 const char *name);
270static bool port_is_bond_fake_iface(const struct port *);
064af421 271
3d657a0a 272static unixctl_cb_func qos_unixctl_show_types;
e8fe3026 273static unixctl_cb_func qos_unixctl_show;
8c4c1387 274
66da9bef 275static struct port *port_create(struct bridge *, const struct ovsrec_port *);
66da9bef 276static void port_del_ifaces(struct port *);
064af421
BP
277static void port_destroy(struct port *);
278static struct port *port_lookup(const struct bridge *, const char *name);
fa066f01
BP
279static void port_configure(struct port *);
280static struct lacp_settings *port_configure_lacp(struct port *,
281 struct lacp_settings *);
df53d41c 282static void port_configure_bond(struct port *, struct bond_settings *);
06b592bc 283static bool port_is_synthetic(const struct port *);
fa066f01 284
35a22d8c
BP
285static void reconfigure_system_stats(const struct ovsrec_open_vswitch *);
286static void run_system_stats(void);
287
fa066f01
BP
288static void bridge_configure_mirrors(struct bridge *);
289static struct mirror *mirror_create(struct bridge *,
290 const struct ovsrec_mirror *);
064af421 291static void mirror_destroy(struct mirror *);
9d24de3b
JP
292static bool mirror_configure(struct mirror *);
293static void mirror_refresh_stats(struct mirror *);
064af421 294
fa066f01 295static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
2a73b1d7
BP
296static bool iface_create(struct bridge *, const struct ovsrec_interface *,
297 const struct ovsrec_port *);
05ba03e0
JP
298static bool iface_is_internal(const struct ovsrec_interface *iface,
299 const struct ovsrec_bridge *br);
b54441b3
BP
300static const char *iface_get_type(const struct ovsrec_interface *,
301 const struct ovsrec_bridge *);
064af421 302static void iface_destroy(struct iface *);
96be8de5 303static void iface_destroy__(struct iface *);
064af421 304static struct iface *iface_lookup(const struct bridge *, const char *name);
e8fe3026 305static struct iface *iface_find(const char *name);
892815f5 306static struct iface *iface_from_ofp_port(const struct bridge *,
4e022ec0 307 ofp_port_t ofp_port);
21a955fc 308static void iface_set_mac(const struct bridge *, const struct port *, struct iface *);
4e022ec0 309static void iface_set_ofport(const struct ovsrec_interface *, ofp_port_t ofport);
bbe6109d 310static void iface_clear_db_record(const struct ovsrec_interface *if_cfg, char *errp);
66da9bef
BP
311static void iface_configure_qos(struct iface *, const struct ovsrec_qos *);
312static void iface_configure_cfm(struct iface *);
8f3fe844 313static void iface_refresh_cfm_stats(struct iface *);
1101a0b4 314static void iface_refresh_stats(struct iface *);
6a5f9a8f
AW
315static void iface_refresh_netdev_status(struct iface *);
316static void iface_refresh_ofproto_status(struct iface *);
cfea354b 317static bool iface_is_synthetic(const struct iface *);
4abb8608
BP
318static ofp_port_t iface_get_requested_ofp_port(
319 const struct ovsrec_interface *);
4e022ec0 320static ofp_port_t iface_pick_ofport(const struct ovsrec_interface *);
52a90c29 321
842733c3 322
842733c3
MG
323static void discover_types(const struct ovsrec_open_vswitch *cfg);
324
b0408fca
JP
325static void
326bridge_init_ofproto(const struct ovsrec_open_vswitch *cfg)
327{
328 struct shash iface_hints;
329 static bool initialized = false;
330 int i;
331
332 if (initialized) {
333 return;
334 }
335
336 shash_init(&iface_hints);
337
b099cd5f
EJ
338 if (cfg) {
339 for (i = 0; i < cfg->n_bridges; i++) {
340 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
341 int j;
342
343 for (j = 0; j < br_cfg->n_ports; j++) {
344 struct ovsrec_port *port_cfg = br_cfg->ports[j];
345 int k;
346
347 for (k = 0; k < port_cfg->n_interfaces; k++) {
348 struct ovsrec_interface *if_cfg = port_cfg->interfaces[k];
349 struct iface_hint *iface_hint;
350
351 iface_hint = xmalloc(sizeof *iface_hint);
352 iface_hint->br_name = br_cfg->name;
353 iface_hint->br_type = br_cfg->datapath_type;
558e2cc5 354 iface_hint->ofp_port = iface_pick_ofport(if_cfg);
b099cd5f
EJ
355
356 shash_add(&iface_hints, if_cfg->name, iface_hint);
357 }
b0408fca
JP
358 }
359 }
360 }
361
362 ofproto_init(&iface_hints);
363
364 shash_destroy_free_data(&iface_hints);
365 initialized = true;
366}
e21c6643
TLSC
367
368static void
369if_change_cb(void *aux OVS_UNUSED)
370{
0612d739
TLSC
371 seq_change(ifaces_changed);
372}
373
374static bool
375if_notifier_changed(struct if_notifier *notifier OVS_UNUSED)
376{
377 uint64_t new_seq;
378 bool changed = false;
379 new_seq = seq_read(ifaces_changed);
380 if (new_seq != last_ifaces_changed) {
381 changed = true;
382 last_ifaces_changed = new_seq;
383 }
384 seq_wait(ifaces_changed, last_ifaces_changed);
385 return changed;
e21c6643 386}
064af421
BP
387\f
388/* Public functions. */
389
c5187f17
BP
390/* Initializes the bridge module, configuring it to obtain its configuration
391 * from an OVSDB server accessed over 'remote', which should be a string in a
392 * form acceptable to ovsdb_idl_create(). */
064af421 393void
c5187f17
BP
394bridge_init(const char *remote)
395{
396 /* Create connection to database. */
fba6bd1d 397 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true, true);
854a94d9 398 idl_seqno = ovsdb_idl_get_seqno(idl);
06b6d651 399 ovsdb_idl_set_lock(idl, "ovs_vswitchd");
8cdec725 400 ovsdb_idl_verify_write_only(idl);
c5187f17 401
ef73f86c
BP
402 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
403 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
842733c3
MG
404 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_datapath_types);
405 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_iface_types);
e85bbd75 406 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
62f3aaed
BP
407 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
408 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
409 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
410 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
3e52fa56
AC
411 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_dpdk_version);
412 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_dpdk_initialized);
e85bbd75 413
62f3aaed 414 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
b5cbbcf6 415 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_version);
21f7563c 416 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_status);
9efd308e
DV
417 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_rstp_status);
418 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_stp_enable);
419 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_rstp_enable);
e85bbd75
BP
420 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
421
21f7563c 422 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_status);
9efd308e
DV
423 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_rstp_status);
424 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_rstp_statistics);
80740385 425 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_statistics);
3e5aeeb5 426 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_bond_active_slave);
e85bbd75 427 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
99eef98b
DF
428 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_trunks);
429 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_vlan_mode);
62f3aaed
BP
430 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
431 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
432 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
433 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
65c3058c 434 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_resets);
df867eda 435 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mac_in_use);
ea401d9a 436 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ifindex);
62f3aaed 437 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
ef73f86c
BP
438 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
439 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
62f3aaed 440 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
b4117094 441 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault);
b9380396 442 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault_status);
1de11730 443 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_mpids);
76c4290d 444 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_flap_count);
3967a833 445 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_health);
4104aaf1 446 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_opstate);
ccc09689 447 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_bfd_status);
b4117094 448 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_lacp_current);
bbe6109d 449 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_error);
e85bbd75
BP
450 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
451
62f3aaed
BP
452 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
453 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
454 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
455 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
456
62f3aaed
BP
457 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
458
459 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
460
461 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
9d24de3b 462 ovsdb_idl_omit_alert(idl, &ovsrec_mirror_col_statistics);
62f3aaed
BP
463
464 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
62f3aaed 465 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
29089a54
RL
466 ovsdb_idl_omit(idl, &ovsrec_ipfix_col_external_ids);
467 ovsdb_idl_omit(idl, &ovsrec_flow_sample_collector_set_col_external_ids);
62f3aaed
BP
468
469 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
470 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
471 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
472 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
473 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
474
475 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
476
c5187f17 477 /* Register unixctl commands. */
3d657a0a
IS
478 unixctl_command_register("qos/show-types", "interface", 1, 1,
479 qos_unixctl_show_types, NULL);
0e15264f
BP
480 unixctl_command_register("qos/show", "interface", 1, 1,
481 qos_unixctl_show, NULL);
482 unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
7ff2009a 483 bridge_unixctl_dump_flows, NULL);
0e15264f 484 unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
7ff2009a 485 bridge_unixctl_reconnect, NULL);
5827ce14 486 lacp_init();
c5187f17 487 bond_init();
9ac3fce4 488 cfm_init();
0fc1f5c0 489 bfd_init();
7c5a3bbf 490 ovs_numa_init();
fe4a02e4 491 stp_init();
99eef98b 492 lldp_init();
9efd308e 493 rstp_init();
0612d739
TLSC
494 ifaces_changed = seq_create();
495 last_ifaces_changed = seq_read(ifaces_changed);
e21c6643 496 ifnotifier = if_notifier_create(if_change_cb, NULL);
c5187f17
BP
497}
498
ee45ad81 499void
fe13ccdc 500bridge_exit(bool delete_datapath)
ee45ad81
BP
501{
502 struct bridge *br, *next_br;
503
e21c6643 504 if_notifier_destroy(ifnotifier);
0612d739 505 seq_destroy(ifaces_changed);
764072fd 506 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
fe13ccdc 507 bridge_destroy(br, delete_datapath);
ee45ad81
BP
508 }
509 ovsdb_idl_destroy(idl);
510}
511
cd11000b
BP
512/* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
513 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
514 * responsible for freeing '*managersp' (with free()).
515 *
516 * You may be asking yourself "why does ovs-vswitchd care?", because
517 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
518 * should not be and in fact is not directly involved in that. But
519 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
520 * it has to tell in-band control where the managers are to enable that.
f0a5ac68
AW
521 * (Thus, only managers connected in-band and with non-loopback addresses
522 * are collected.)
cd11000b
BP
523 */
524static void
94db5407
BP
525collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
526 struct sockaddr_in **managersp, size_t *n_managersp)
cd11000b
BP
527{
528 struct sockaddr_in *managers = NULL;
529 size_t n_managers = 0;
b3c01ed3 530 struct sset targets;
94db5407
BP
531 size_t i;
532
289df16d
AE
533 /* Collect all of the potential targets from the "targets" columns of the
534 * rows pointed to by "manager_options", excluding any that are
535 * out-of-band. */
b3c01ed3 536 sset_init(&targets);
94db5407
BP
537 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
538 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
539
540 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
b3c01ed3 541 sset_find_and_delete(&targets, m->target);
94db5407 542 } else {
b3c01ed3 543 sset_add(&targets, m->target);
94db5407
BP
544 }
545 }
cd11000b 546
94db5407 547 /* Now extract the targets' IP addresses. */
b3c01ed3
BP
548 if (!sset_is_empty(&targets)) {
549 const char *target;
cd11000b 550
b3c01ed3
BP
551 managers = xmalloc(sset_count(&targets) * sizeof *managers);
552 SSET_FOR_EACH (target, &targets) {
02334943
JR
553 union {
554 struct sockaddr_storage ss;
555 struct sockaddr_in in;
556 } sa;
cd11000b 557
f0a5ac68 558 /* Ignore loopback. */
d4763d1d 559 if (stream_parse_target_with_default_port(target, OVSDB_PORT,
02334943 560 &sa.ss)
f0a5ac68
AW
561 && sa.ss.ss_family == AF_INET
562 && sa.in.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
02334943 563 managers[n_managers++] = sa.in;
cd11000b
BP
564 }
565 }
566 }
b3c01ed3 567 sset_destroy(&targets);
cd11000b
BP
568
569 *managersp = managers;
570 *n_managersp = n_managers;
571}
572
d4f6865c
DDP
573static void
574config_ofproto_types(const struct smap *other_config)
575{
576 struct sset types;
577 const char *type;
578
579 /* Pass custom configuration to datapath types. */
580 sset_init(&types);
581 ofproto_enumerate_types(&types);
582 SSET_FOR_EACH (type, &types) {
583 ofproto_type_set_config(type, other_config);
584 }
585 sset_destroy(&types);
586}
587
c5187f17 588static void
76343538 589bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
064af421 590{
2a73b1d7
BP
591 struct sockaddr_in *managers;
592 struct bridge *br, *next;
593 int sflow_bridge_number;
594 size_t n_managers;
064af421
BP
595
596 COVERAGE_INC(bridge_reconfigure);
597
e79a6c83
EJ
598 ofproto_set_flow_limit(smap_get_int(&ovs_cfg->other_config, "flow-limit",
599 OFPROTO_FLOW_LIMIT_DEFAULT));
72310b04
JS
600 ofproto_set_max_idle(smap_get_int(&ovs_cfg->other_config, "max-idle",
601 OFPROTO_MAX_IDLE_DEFAULT));
f0fb825a
EG
602 ofproto_set_vlan_limit(smap_get_int(&ovs_cfg->other_config, "vlan-limit",
603 LEGACY_MAX_VLAN_HEADERS));
7fdd2082
FL
604 ofproto_set_bundle_idle_timeout(smap_get_int(&ovs_cfg->other_config,
605 "bundle-idle-timeout", 0));
6567010f 606 ofproto_set_threads(
e79a6c83
EJ
607 smap_get_int(&ovs_cfg->other_config, "n-handler-threads", 0),
608 smap_get_int(&ovs_cfg->other_config, "n-revalidator-threads", 0));
448a4b2f 609
bae7208e 610 /* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
2a73b1d7 611 * to 'ovs_cfg', with only very minimal configuration otherwise.
66da9bef 612 *
bae7208e
EJ
613 * This is mostly an update to bridge data structures. Nothing is pushed
614 * down to ofproto or lower layers. */
66da9bef 615 add_del_bridges(ovs_cfg);
764072fd 616 HMAP_FOR_EACH (br, node, &all_bridges) {
42deb67d 617 bridge_collect_wanted_ports(br, &br->wanted_ports);
2a73b1d7 618 bridge_del_ports(br, &br->wanted_ports);
76343538 619 }
064af421 620
2a73b1d7
BP
621 /* Start pushing configuration changes down to the ofproto layer:
622 *
623 * - Delete ofprotos that are no longer configured.
624 *
625 * - Delete ports that are no longer configured.
626 *
627 * - Reconfigure existing ports to their desired configurations, or
628 * delete them if not possible.
629 *
630 * We have to do all the deletions before we can do any additions, because
631 * the ports to be added might require resources that will be freed up by
632 * deletions (they might especially overlap in name). */
633 bridge_delete_ofprotos();
bae7208e 634 HMAP_FOR_EACH (br, node, &all_bridges) {
2a73b1d7
BP
635 if (br->ofproto) {
636 bridge_delete_or_reconfigure_ports(br);
76343538 637 }
064af421 638 }
35a22d8c 639
2a73b1d7
BP
640 /* Finish pushing configuration changes to the ofproto layer:
641 *
642 * - Create ofprotos that are missing.
643 *
644 * - Add ports that are missing. */
645 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
646 if (!br->ofproto) {
647 int error;
bae7208e 648
2a73b1d7
BP
649 error = ofproto_create(br->name, br->type, &br->ofproto);
650 if (error) {
651 VLOG_ERR("failed to create bridge %s: %s", br->name,
652 ovs_strerror(error));
653 shash_destroy(&br->wanted_ports);
9aad5a5a 654 bridge_destroy(br, true);
b5cbbcf6
AZ
655 } else {
656 /* Trigger storing datapath version. */
657 seq_change(connectivity_seq_get());
e56365a5 658 }
064af421
BP
659 }
660 }
83ede47a
SR
661
662 config_ofproto_types(&ovs_cfg->other_config);
663
e8192d80 664 HMAP_FOR_EACH (br, node, &all_bridges) {
2a73b1d7
BP
665 bridge_add_ports(br, &br->wanted_ports);
666 shash_destroy(&br->wanted_ports);
064af421 667 }
064af421 668
2a73b1d7 669 reconfigure_system_stats(ovs_cfg);
bae7208e 670
66da9bef
BP
671 /* Complete the configuration. */
672 sflow_bridge_number = 0;
673 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
764072fd 674 HMAP_FOR_EACH (br, node, &all_bridges) {
8052fb14 675 struct port *port;
064af421 676
f145afdc
BP
677 /* We need the datapath ID early to allow LACP ports to use it as the
678 * default system ID. */
679 bridge_configure_datapath_id(br);
680
8052fb14 681 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968 682 struct iface *iface;
52df17e7 683
fa066f01 684 port_configure(port);
c1c9c9c4 685
48e1b7fb 686 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
59804c80 687 iface_set_ofport(iface->cfg, iface->ofp_port);
bbe6109d
TG
688 /* Clear eventual previous errors */
689 ovsrec_interface_set_error(iface->cfg, NULL);
66da9bef
BP
690 iface_configure_cfm(iface);
691 iface_configure_qos(iface, port->cfg->qos);
21a955fc 692 iface_set_mac(br, port, iface);
ccc09689
EJ
693 ofproto_port_set_bfd(br->ofproto, iface->ofp_port,
694 &iface->cfg->bfd);
99eef98b
DF
695 ofproto_port_set_lldp(br->ofproto, iface->ofp_port,
696 &iface->cfg->lldp);
91364d18
IM
697 ofproto_port_set_config(br->ofproto, iface->ofp_port,
698 &iface->cfg->other_config);
064af421
BP
699 }
700 }
fa066f01 701 bridge_configure_mirrors(br);
8402c74b 702 bridge_configure_forward_bpdu(br);
c4069512 703 bridge_configure_mac_table(br);
dc2b70ba 704 bridge_configure_mcast_snooping(br);
fa066f01 705 bridge_configure_remotes(br, managers, n_managers);
66da9bef
BP
706 bridge_configure_netflow(br);
707 bridge_configure_sflow(br, &sflow_bridge_number);
29089a54 708 bridge_configure_ipfix(br);
7af77bbd 709 bridge_configure_spanning_tree(br);
254750ce 710 bridge_configure_tables(br);
8b6ff729 711 bridge_configure_dp_desc(br);
99eef98b 712 bridge_configure_aa(br);
064af421 713 }
66da9bef 714 free(managers);
aeed7879
BP
715
716 /* The ofproto-dpif provider does some final reconfiguration in its
717 * ->type_run() function. We have to call it before notifying the database
718 * client that reconfiguration is complete, otherwise there is a very
719 * narrow race window in which e.g. ofproto/trace will not recognize the
720 * new configuration (sometimes this causes unit test failures). */
721 bridge_run__();
66da9bef 722}
064af421 723
bae7208e
EJ
724/* Delete ofprotos which aren't configured or have the wrong type. Create
725 * ofprotos which don't exist but need to. */
66da9bef 726static void
2a73b1d7 727bridge_delete_ofprotos(void)
66da9bef 728{
2a73b1d7 729 struct bridge *br;
f79e673f
BP
730 struct sset names;
731 struct sset types;
66da9bef 732 const char *type;
6c88d577 733
bae7208e 734 /* Delete ofprotos with no bridge or with the wrong type. */
f79e673f
BP
735 sset_init(&names);
736 sset_init(&types);
737 ofproto_enumerate_types(&types);
738 SSET_FOR_EACH (type, &types) {
66da9bef 739 const char *name;
3a6ccc8c 740
f79e673f
BP
741 ofproto_enumerate_names(type, &names);
742 SSET_FOR_EACH (name, &names) {
bae7208e 743 br = bridge_lookup(name);
66da9bef 744 if (!br || strcmp(type, br->type)) {
f79e673f 745 ofproto_delete(name, type);
3a6ccc8c 746 }
b31bcf60
EJ
747 }
748 }
f79e673f
BP
749 sset_destroy(&names);
750 sset_destroy(&types);
2a73b1d7 751}
76343538 752
2a73b1d7
BP
753static ofp_port_t *
754add_ofp_port(ofp_port_t port, ofp_port_t *ports, size_t *n, size_t *allocated)
755{
756 if (*n >= *allocated) {
757 ports = x2nrealloc(ports, allocated, sizeof *ports);
758 }
759 ports[(*n)++] = port;
760 return ports;
761}
bae7208e 762
7c12e200 763/* Configures the MTU of 'netdev' based on the "mtu_request" column
3a414a0a 764 * in 'iface_cfg'. */
7c12e200
DDP
765static int
766iface_set_netdev_mtu(const struct ovsrec_interface *iface_cfg,
3a414a0a 767 struct netdev *netdev)
7c12e200 768{
3a414a0a
DDP
769 if (iface_cfg->n_mtu_request == 1) {
770 /* The user explicitly asked for this MTU. */
771 netdev_mtu_user_config(netdev, true);
772 /* Try to set the MTU to the requested value. */
7c12e200
DDP
773 return netdev_set_mtu(netdev, *iface_cfg->mtu_request);
774 }
3a414a0a
DDP
775
776 /* The user didn't explicitly asked for any MTU. */
777 netdev_mtu_user_config(netdev, false);
7c12e200
DDP
778 return 0;
779}
780
2a73b1d7
BP
781static void
782bridge_delete_or_reconfigure_ports(struct bridge *br)
783{
784 struct ofproto_port ofproto_port;
785 struct ofproto_port_dump dump;
786
96be8de5
BP
787 struct sset ofproto_ports;
788 struct port *port, *port_next;
789
2a73b1d7
BP
790 /* List of "ofp_port"s to delete. We make a list instead of deleting them
791 * right away because ofproto implementations aren't necessarily able to
792 * iterate through a changing list of ports in an entirely robust way. */
793 ofp_port_t *del;
794 size_t n, allocated;
795 size_t i;
796
797 del = NULL;
798 n = allocated = 0;
96be8de5 799 sset_init(&ofproto_ports);
bae7208e 800
96be8de5
BP
801 /* Main task: Iterate over the ports in 'br->ofproto' and remove the ports
802 * that are not configured in the database. (This commonly happens when
803 * ports have been deleted, e.g. with "ovs-vsctl del-port".)
804 *
805 * Side tasks: Reconfigure the ports that are still in 'br'. Delete ports
806 * that have the wrong OpenFlow port number (and arrange to add them back
807 * with the correct OpenFlow port number). */
2a73b1d7 808 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
4abb8608 809 ofp_port_t requested_ofp_port;
2a73b1d7 810 struct iface *iface;
bae7208e 811
96be8de5
BP
812 sset_add(&ofproto_ports, ofproto_port.name);
813
2a73b1d7
BP
814 iface = iface_lookup(br, ofproto_port.name);
815 if (!iface) {
816 /* No such iface is configured, so we should delete this
817 * ofproto_port.
818 *
819 * As a corner case exception, keep the port if it's a bond fake
820 * interface. */
821 if (bridge_has_bond_fake_iface(br, ofproto_port.name)
822 && !strcmp(ofproto_port.type, "internal")) {
bae7208e
EJ
823 continue;
824 }
2a73b1d7
BP
825 goto delete;
826 }
bae7208e 827
c381bca5
BP
828 const char *netdev_type = ofproto_port_open_type(br->ofproto,
829 iface->type);
830 if (strcmp(ofproto_port.type, netdev_type)
bbe6109d 831 || netdev_set_config(iface->netdev, &iface->cfg->options, NULL)) {
2a73b1d7
BP
832 /* The interface is the wrong type or can't be configured.
833 * Delete it. */
834 goto delete;
bae7208e 835 }
56abcf49 836
3a414a0a 837 iface_set_netdev_mtu(iface->cfg, iface->netdev);
bae7208e 838
4abb8608
BP
839 /* If the requested OpenFlow port for 'iface' changed, and it's not
840 * already the correct port, then we might want to temporarily delete
841 * this interface, so we can add it back again with the new OpenFlow
842 * port number. */
843 requested_ofp_port = iface_get_requested_ofp_port(iface->cfg);
844 if (iface->ofp_port != OFPP_LOCAL &&
845 requested_ofp_port != OFPP_NONE &&
846 requested_ofp_port != iface->ofp_port) {
847 ofp_port_t victim_request;
848 struct iface *victim;
849
850 /* Check for an existing OpenFlow port currently occupying
851 * 'iface''s requested port number. If there isn't one, then
852 * delete this port. Otherwise we need to consider further. */
853 victim = iface_from_ofp_port(br, requested_ofp_port);
854 if (!victim) {
855 goto delete;
856 }
857
858 /* 'victim' is a port currently using 'iface''s requested port
859 * number. Unless 'victim' specifically requested that port
860 * number, too, then we can delete both 'iface' and 'victim'
861 * temporarily. (We'll add both of them back again later with new
862 * OpenFlow port numbers.)
863 *
864 * If 'victim' did request port number 'requested_ofp_port', just
865 * like 'iface', then that's a configuration inconsistency that we
866 * can't resolve. We might as well let it keep its current port
867 * number. */
868 victim_request = iface_get_requested_ofp_port(victim->cfg);
869 if (victim_request != requested_ofp_port) {
870 del = add_ofp_port(victim->ofp_port, del, &n, &allocated);
871 iface_destroy(victim);
872 goto delete;
873 }
874 }
875
2a73b1d7
BP
876 /* Keep it. */
877 continue;
878
879 delete:
880 iface_destroy(iface);
881 del = add_ofp_port(ofproto_port.ofp_port, del, &n, &allocated);
882 }
2a73b1d7
BP
883 for (i = 0; i < n; i++) {
884 ofproto_port_del(br->ofproto, del[i]);
885 }
886 free(del);
96be8de5
BP
887
888 /* Iterate over this module's idea of interfaces in 'br'. Remove any ports
889 * that we didn't see when we iterated through the datapath, i.e. ports
890 * that disappeared underneath use. This is an unusual situation, but it
891 * can happen in some cases:
892 *
893 * - An admin runs a command like "ovs-dpctl del-port" (which is a bad
894 * idea but could happen).
895 *
896 * - The port represented a device that disappeared, e.g. a tuntap
897 * device destroyed via "tunctl -d", a physical Ethernet device
898 * whose module was just unloaded via "rmmod", or a virtual NIC for a
899 * VM whose VM was just terminated. */
900 HMAP_FOR_EACH_SAFE (port, port_next, hmap_node, &br->ports) {
901 struct iface *iface, *iface_next;
902
903 LIST_FOR_EACH_SAFE (iface, iface_next, port_elem, &port->ifaces) {
904 if (!sset_contains(&ofproto_ports, iface->name)) {
905 iface_destroy__(iface);
906 }
907 }
908
417e7e66 909 if (ovs_list_is_empty(&port->ifaces)) {
96be8de5
BP
910 port_destroy(port);
911 }
912 }
913 sset_destroy(&ofproto_ports);
2a73b1d7
BP
914}
915
916static void
4abb8608
BP
917bridge_add_ports__(struct bridge *br, const struct shash *wanted_ports,
918 bool with_requested_port)
2a73b1d7
BP
919{
920 struct shash_node *port_node;
921
922 SHASH_FOR_EACH (port_node, wanted_ports) {
923 const struct ovsrec_port *port_cfg = port_node->data;
924 size_t i;
925
926 for (i = 0; i < port_cfg->n_interfaces; i++) {
927 const struct ovsrec_interface *iface_cfg = port_cfg->interfaces[i];
4abb8608
BP
928 ofp_port_t requested_ofp_port;
929
930 requested_ofp_port = iface_get_requested_ofp_port(iface_cfg);
931 if ((requested_ofp_port != OFPP_NONE) == with_requested_port) {
932 struct iface *iface = iface_lookup(br, iface_cfg->name);
2a73b1d7 933
4abb8608
BP
934 if (!iface) {
935 iface_create(br, iface_cfg, port_cfg);
936 }
2a73b1d7 937 }
bae7208e 938 }
66da9bef 939 }
093e47f4 940}
c3827f61 941
4abb8608
BP
942static void
943bridge_add_ports(struct bridge *br, const struct shash *wanted_ports)
944{
945 /* First add interfaces that request a particular port number. */
946 bridge_add_ports__(br, wanted_ports, true);
947
948 /* Then add interfaces that want automatic port number assignment.
949 * We add these afterward to avoid accidentally taking a specifically
950 * requested port number. */
951 bridge_add_ports__(br, wanted_ports, false);
952}
953
fa066f01
BP
954static void
955port_configure(struct port *port)
956{
957 const struct ovsrec_port *cfg = port->cfg;
958 struct bond_settings bond_settings;
959 struct lacp_settings lacp_settings;
960 struct ofproto_bundle_settings s;
961 struct iface *iface;
82057f51 962
fa066f01
BP
963 /* Get name. */
964 s.name = port->name;
3a6ccc8c 965
fa066f01
BP
966 /* Get slaves. */
967 s.n_slaves = 0;
417e7e66 968 s.slaves = xmalloc(ovs_list_size(&port->ifaces) * sizeof *s.slaves);
fa066f01 969 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
892815f5 970 s.slaves[s.n_slaves++] = iface->ofp_port;
fa066f01 971 }
c3827f61 972
fa066f01
BP
973 /* Get VLAN tag. */
974 s.vlan = -1;
acaf1486
BP
975 if (cfg->tag && *cfg->tag >= 0 && *cfg->tag <= 4095) {
976 s.vlan = *cfg->tag;
fa066f01 977 }
b0ec0f27 978
fa066f01
BP
979 /* Get VLAN trunks. */
980 s.trunks = NULL;
ecac4ebf 981 if (cfg->n_trunks) {
fa066f01 982 s.trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
ecac4ebf
BP
983 }
984
fed8962a
EG
985 s.cvlans = NULL;
986 if (cfg->n_cvlans) {
987 s.cvlans = vlan_bitmap_from_array(cfg->cvlans, cfg->n_cvlans);
988 }
989
ecac4ebf
BP
990 /* Get VLAN mode. */
991 if (cfg->vlan_mode) {
992 if (!strcmp(cfg->vlan_mode, "access")) {
993 s.vlan_mode = PORT_VLAN_ACCESS;
994 } else if (!strcmp(cfg->vlan_mode, "trunk")) {
995 s.vlan_mode = PORT_VLAN_TRUNK;
996 } else if (!strcmp(cfg->vlan_mode, "native-tagged")) {
997 s.vlan_mode = PORT_VLAN_NATIVE_TAGGED;
998 } else if (!strcmp(cfg->vlan_mode, "native-untagged")) {
999 s.vlan_mode = PORT_VLAN_NATIVE_UNTAGGED;
fed8962a
EG
1000 } else if (!strcmp(cfg->vlan_mode, "dot1q-tunnel")) {
1001 s.vlan_mode = PORT_VLAN_DOT1Q_TUNNEL;
ecac4ebf
BP
1002 } else {
1003 /* This "can't happen" because ovsdb-server should prevent it. */
65a0903b
TG
1004 VLOG_WARN("port %s: unknown VLAN mode %s, falling "
1005 "back to trunk mode", port->name, cfg->vlan_mode);
ecac4ebf
BP
1006 s.vlan_mode = PORT_VLAN_TRUNK;
1007 }
1008 } else {
1009 if (s.vlan >= 0) {
1010 s.vlan_mode = PORT_VLAN_ACCESS;
fed8962a 1011 if (cfg->n_trunks || cfg->n_cvlans) {
65a0903b
TG
1012 VLOG_WARN("port %s: ignoring trunks in favor of implicit vlan",
1013 port->name);
ecac4ebf
BP
1014 }
1015 } else {
1016 s.vlan_mode = PORT_VLAN_TRUNK;
1017 }
064af421 1018 }
fed8962a
EG
1019
1020 const char *qe = smap_get_def(&cfg->other_config, "qinq-ethtype", "");
1021 s.qinq_ethtype = (!strcmp(qe, "802.1q")
1022 ? ETH_TYPE_VLAN_8021Q
1023 : ETH_TYPE_VLAN_8021AD);
1024
a699f614
EJ
1025 s.use_priority_tags = smap_get_bool(&cfg->other_config, "priority-tags",
1026 false);
064af421 1027
fa066f01
BP
1028 /* Get LACP settings. */
1029 s.lacp = port_configure_lacp(port, &lacp_settings);
1030 if (s.lacp) {
1031 size_t i = 0;
064af421 1032
fa066f01
BP
1033 s.lacp_slaves = xmalloc(s.n_slaves * sizeof *s.lacp_slaves);
1034 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1035 iface_configure_lacp(iface, &s.lacp_slaves[i++]);
064af421 1036 }
fa066f01
BP
1037 } else {
1038 s.lacp_slaves = NULL;
1039 }
064af421 1040
fa066f01
BP
1041 /* Get bond settings. */
1042 if (s.n_slaves > 1) {
fa066f01 1043 s.bond = &bond_settings;
df53d41c 1044 port_configure_bond(port, &bond_settings);
fa066f01
BP
1045 } else {
1046 s.bond = NULL;
1670c579
EJ
1047 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1048 netdev_set_miimon_interval(iface->netdev, 0);
1049 }
fa066f01 1050 }
093e47f4 1051
c005f976 1052 /* Protected port mode */
adb4185d 1053 s.protected = cfg->protected_;
c005f976 1054
fa066f01
BP
1055 /* Register. */
1056 ofproto_bundle_register(port->bridge->ofproto, port, &s);
76343538 1057
fa066f01 1058 /* Clean up. */
c71caf1b 1059 free(s.cvlans);
4e51de4c 1060 free(s.slaves);
fa066f01
BP
1061 free(s.trunks);
1062 free(s.lacp_slaves);
1063}
76343538 1064
6f90b8f4
BP
1065/* Pick local port hardware address and datapath ID for 'br'. */
1066static void
1067bridge_configure_datapath_id(struct bridge *br)
1068{
74ff3298 1069 struct eth_addr ea;
6f90b8f4
BP
1070 uint64_t dpid;
1071 struct iface *local_iface;
1072 struct iface *hw_addr_iface;
1073 char *dpid_string;
76343538 1074
74ff3298 1075 bridge_pick_local_hw_addr(br, &ea, &hw_addr_iface);
892815f5 1076 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
6f90b8f4
BP
1077 if (local_iface) {
1078 int error = netdev_set_etheraddr(local_iface->netdev, ea);
1079 if (error) {
1080 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1081 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
1082 "Ethernet address: %s",
10a89ef0 1083 br->name, ovs_strerror(error));
6f90b8f4
BP
1084 }
1085 }
74ff3298 1086 br->ea = ea;
76343538 1087
6f90b8f4 1088 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
e825ace2
BP
1089 if (dpid != ofproto_get_datapath_id(br->ofproto)) {
1090 VLOG_INFO("bridge %s: using datapath ID %016"PRIx64, br->name, dpid);
1091 ofproto_set_datapath_id(br->ofproto, dpid);
1092 }
76343538 1093
6f90b8f4
BP
1094 dpid_string = xasprintf("%016"PRIx64, dpid);
1095 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
1096 free(dpid_string);
1097}
064af421 1098
7beaa082
SH
1099/* Returns a bitmap of "enum ofputil_protocol"s that are allowed for use with
1100 * 'br'. */
1101static uint32_t
1102bridge_get_allowed_versions(struct bridge *br)
1103{
3852cc1b 1104 if (!br->cfg->n_protocols) {
7beaa082 1105 return 0;
3852cc1b 1106 }
7beaa082 1107
aa233d57
BP
1108 return ofputil_versions_from_strings(br->cfg->protocols,
1109 br->cfg->n_protocols);
7beaa082
SH
1110}
1111
6f90b8f4
BP
1112/* Set NetFlow configuration on 'br'. */
1113static void
1114bridge_configure_netflow(struct bridge *br)
1115{
1116 struct ovsrec_netflow *cfg = br->cfg->netflow;
1117 struct netflow_options opts;
72b06300 1118
6f90b8f4
BP
1119 if (!cfg) {
1120 ofproto_set_netflow(br->ofproto, NULL);
1121 return;
1122 }
a4af0040 1123
6f90b8f4 1124 memset(&opts, 0, sizeof opts);
72b06300 1125
6f90b8f4
BP
1126 /* Get default NetFlow configuration from datapath.
1127 * Apply overrides from 'cfg'. */
1128 ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
1129 if (cfg->engine_type) {
1130 opts.engine_type = *cfg->engine_type;
1131 }
1132 if (cfg->engine_id) {
1133 opts.engine_id = *cfg->engine_id;
1134 }
72b06300 1135
6f90b8f4
BP
1136 /* Configure active timeout interval. */
1137 opts.active_timeout = cfg->active_timeout;
1138 if (!opts.active_timeout) {
1139 opts.active_timeout = -1;
1140 } else if (opts.active_timeout < 0) {
1141 VLOG_WARN("bridge %s: active timeout interval set to negative "
1142 "value, using default instead (%d seconds)", br->name,
1143 NF_ACTIVE_TIMEOUT_DEFAULT);
1144 opts.active_timeout = -1;
1145 }
72b06300 1146
6f90b8f4
BP
1147 /* Add engine ID to interface number to disambiguate bridgs? */
1148 opts.add_id_to_iface = cfg->add_id_to_interface;
1149 if (opts.add_id_to_iface) {
1150 if (opts.engine_id > 0x7f) {
1151 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
1152 "another vswitch, choose an engine id less than 128",
1153 br->name);
1154 }
1155 if (hmap_count(&br->ports) > 508) {
1156 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
1157 "another port when more than 508 ports are used",
1158 br->name);
1159 }
1160 }
72b06300 1161
6f90b8f4
BP
1162 /* Collectors. */
1163 sset_init(&opts.collectors);
1164 sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
a4af0040 1165
6f90b8f4
BP
1166 /* Configure. */
1167 if (ofproto_set_netflow(br->ofproto, &opts)) {
1168 VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
1169 }
1170 sset_destroy(&opts.collectors);
1171}
72b06300 1172
6f90b8f4
BP
1173/* Set sFlow configuration on 'br'. */
1174static void
1175bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
1176{
1177 const struct ovsrec_sflow *cfg = br->cfg->sflow;
1178 struct ovsrec_controller **controllers;
1179 struct ofproto_sflow_options oso;
1180 size_t n_controllers;
1181 size_t i;
72b06300 1182
6f90b8f4
BP
1183 if (!cfg) {
1184 ofproto_set_sflow(br->ofproto, NULL);
1185 return;
064af421 1186 }
8052fb14 1187
6f90b8f4 1188 memset(&oso, 0, sizeof oso);
52df17e7 1189
6f90b8f4
BP
1190 sset_init(&oso.targets);
1191 sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
c1c9c9c4 1192
6f90b8f4
BP
1193 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
1194 if (cfg->sampling) {
1195 oso.sampling_rate = *cfg->sampling;
064af421 1196 }
6f90b8f4
BP
1197
1198 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
1199 if (cfg->polling) {
1200 oso.polling_interval = *cfg->polling;
064af421 1201 }
093e47f4 1202
6f90b8f4
BP
1203 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
1204 if (cfg->header) {
1205 oso.header_len = *cfg->header;
1206 }
392730c4 1207
6f90b8f4
BP
1208 oso.sub_id = (*sflow_bridge_number)++;
1209 oso.agent_device = cfg->agent;
392730c4 1210
6f90b8f4
BP
1211 oso.control_ip = NULL;
1212 n_controllers = bridge_get_controllers(br, &controllers);
1213 for (i = 0; i < n_controllers; i++) {
1214 if (controllers[i]->local_ip) {
1215 oso.control_ip = controllers[i]->local_ip;
1216 break;
b31bcf60
EJ
1217 }
1218 }
6f90b8f4 1219 ofproto_set_sflow(br->ofproto, &oso);
b31bcf60 1220
6f90b8f4
BP
1221 sset_destroy(&oso.targets);
1222}
a7ff9bd7 1223
88afd5fc
RL
1224/* Returns whether a IPFIX row is valid. */
1225static bool
1226ovsrec_ipfix_is_valid(const struct ovsrec_ipfix *ipfix)
1227{
1228 return ipfix && ipfix->n_targets > 0;
1229}
1230
3388e51a
RL
1231/* Returns whether a Flow_Sample_Collector_Set row is valid. */
1232static bool
1233ovsrec_fscs_is_valid(const struct ovsrec_flow_sample_collector_set *fscs,
1234 const struct bridge *br)
1235{
88afd5fc 1236 return ovsrec_ipfix_is_valid(fscs->ipfix) && fscs->bridge == br->cfg;
3388e51a
RL
1237}
1238
29089a54
RL
1239/* Set IPFIX configuration on 'br'. */
1240static void
1241bridge_configure_ipfix(struct bridge *br)
1242{
1243 const struct ovsrec_ipfix *be_cfg = br->cfg->ipfix;
88afd5fc 1244 bool valid_be_cfg = ovsrec_ipfix_is_valid(be_cfg);
29089a54
RL
1245 const struct ovsrec_flow_sample_collector_set *fe_cfg;
1246 struct ofproto_ipfix_bridge_exporter_options be_opts;
1247 struct ofproto_ipfix_flow_exporter_options *fe_opts = NULL;
1248 size_t n_fe_opts = 0;
c97320eb 1249 const char *virtual_obs_id;
29089a54
RL
1250
1251 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg, idl) {
3388e51a 1252 if (ovsrec_fscs_is_valid(fe_cfg, br)) {
29089a54
RL
1253 n_fe_opts++;
1254 }
1255 }
1256
88afd5fc 1257 if (!valid_be_cfg && n_fe_opts == 0) {
29089a54
RL
1258 ofproto_set_ipfix(br->ofproto, NULL, NULL, 0);
1259 return;
1260 }
1261
88afd5fc 1262 if (valid_be_cfg) {
29089a54
RL
1263 memset(&be_opts, 0, sizeof be_opts);
1264
1265 sset_init(&be_opts.targets);
1266 sset_add_array(&be_opts.targets, be_cfg->targets, be_cfg->n_targets);
1267
1268 if (be_cfg->sampling) {
1269 be_opts.sampling_rate = *be_cfg->sampling;
1270 } else {
1271 be_opts.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
1272 }
1273 if (be_cfg->obs_domain_id) {
1274 be_opts.obs_domain_id = *be_cfg->obs_domain_id;
1275 }
1276 if (be_cfg->obs_point_id) {
1277 be_opts.obs_point_id = *be_cfg->obs_point_id;
1278 }
978427a5
RL
1279 if (be_cfg->cache_active_timeout) {
1280 be_opts.cache_active_timeout = *be_cfg->cache_active_timeout;
1281 }
1282 if (be_cfg->cache_max_flows) {
1283 be_opts.cache_max_flows = *be_cfg->cache_max_flows;
1284 }
8b7ea2d4
WZ
1285
1286 be_opts.enable_tunnel_sampling = smap_get_bool(&be_cfg->other_config,
1287 "enable-tunnel-sampling", true);
1288
1289 be_opts.enable_input_sampling = !smap_get_bool(&be_cfg->other_config,
1290 "enable-input-sampling", false);
1291
1292 be_opts.enable_output_sampling = !smap_get_bool(&be_cfg->other_config,
1293 "enable-output-sampling", false);
c97320eb
WZ
1294
1295 virtual_obs_id = smap_get(&be_cfg->other_config, "virtual_obs_id");
2225c0b9 1296 be_opts.virtual_obs_id = nullable_xstrdup(virtual_obs_id);
29089a54
RL
1297 }
1298
1299 if (n_fe_opts > 0) {
1300 struct ofproto_ipfix_flow_exporter_options *opts;
1301 fe_opts = xcalloc(n_fe_opts, sizeof *fe_opts);
1302 opts = fe_opts;
1303 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg, idl) {
3388e51a 1304 if (ovsrec_fscs_is_valid(fe_cfg, br)) {
29089a54
RL
1305 opts->collector_set_id = fe_cfg->id;
1306 sset_init(&opts->targets);
1307 sset_add_array(&opts->targets, fe_cfg->ipfix->targets,
1308 fe_cfg->ipfix->n_targets);
978427a5
RL
1309 opts->cache_active_timeout = fe_cfg->ipfix->cache_active_timeout
1310 ? *fe_cfg->ipfix->cache_active_timeout : 0;
1311 opts->cache_max_flows = fe_cfg->ipfix->cache_max_flows
1312 ? *fe_cfg->ipfix->cache_max_flows : 0;
f69f713b
BY
1313 opts->enable_tunnel_sampling = smap_get_bool(
1314 &fe_cfg->ipfix->other_config,
1315 "enable-tunnel-sampling", true);
c97320eb
WZ
1316 virtual_obs_id = smap_get(&fe_cfg->ipfix->other_config,
1317 "virtual_obs_id");
2225c0b9 1318 opts->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
29089a54
RL
1319 opts++;
1320 }
1321 }
1322 }
1323
88afd5fc 1324 ofproto_set_ipfix(br->ofproto, valid_be_cfg ? &be_opts : NULL, fe_opts,
29089a54
RL
1325 n_fe_opts);
1326
88afd5fc 1327 if (valid_be_cfg) {
29089a54 1328 sset_destroy(&be_opts.targets);
c97320eb 1329 free(be_opts.virtual_obs_id);
29089a54
RL
1330 }
1331
1332 if (n_fe_opts > 0) {
1333 struct ofproto_ipfix_flow_exporter_options *opts = fe_opts;
1334 size_t i;
1335 for (i = 0; i < n_fe_opts; i++) {
1336 sset_destroy(&opts->targets);
c97320eb 1337 free(opts->virtual_obs_id);
29089a54
RL
1338 opts++;
1339 }
1340 free(fe_opts);
1341 }
1342}
1343
21f7563c
JP
1344static void
1345port_configure_stp(const struct ofproto *ofproto, struct port *port,
1346 struct ofproto_port_stp_settings *port_s,
1347 int *port_num_counter, unsigned long *port_num_bitmap)
1348{
1349 const char *config_str;
1350 struct iface *iface;
1351
079b5942 1352 if (!smap_get_bool(&port->cfg->other_config, "stp-enable", true)) {
21f7563c
JP
1353 port_s->enable = false;
1354 return;
1355 } else {
1356 port_s->enable = true;
1357 }
1358
1359 /* STP over bonds is not supported. */
417e7e66 1360 if (!ovs_list_is_singleton(&port->ifaces)) {
21f7563c
JP
1361 VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
1362 port->name);
1363 port_s->enable = false;
1364 return;
1365 }
1366
417e7e66 1367 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
21f7563c
JP
1368
1369 /* Internal ports shouldn't participate in spanning tree, so
1370 * skip them. */
1371 if (!strcmp(iface->type, "internal")) {
1372 VLOG_DBG("port %s: disable STP on internal ports", port->name);
1373 port_s->enable = false;
1374 return;
1375 }
1376
1377 /* STP on mirror output ports is not supported. */
1378 if (ofproto_is_mirror_output_bundle(ofproto, port)) {
1379 VLOG_DBG("port %s: disable STP on mirror ports", port->name);
1380 port_s->enable = false;
1381 return;
1382 }
1383
a699f614 1384 config_str = smap_get(&port->cfg->other_config, "stp-port-num");
21f7563c
JP
1385 if (config_str) {
1386 unsigned long int port_num = strtoul(config_str, NULL, 0);
1387 int port_idx = port_num - 1;
1388
1389 if (port_num < 1 || port_num > STP_MAX_PORTS) {
1390 VLOG_ERR("port %s: invalid stp-port-num", port->name);
1391 port_s->enable = false;
1392 return;
1393 }
1394
1395 if (bitmap_is_set(port_num_bitmap, port_idx)) {
1396 VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
1397 port->name, port_num);
1398 port_s->enable = false;
1399 return;
1400 }
1401 bitmap_set1(port_num_bitmap, port_idx);
1402 port_s->port_num = port_idx;
1403 } else {
6019f124 1404 if (*port_num_counter >= STP_MAX_PORTS) {
21f7563c
JP
1405 VLOG_ERR("port %s: too many STP ports, disabling", port->name);
1406 port_s->enable = false;
1407 return;
1408 }
1409
1410 port_s->port_num = (*port_num_counter)++;
1411 }
1412
a699f614 1413 config_str = smap_get(&port->cfg->other_config, "stp-path-cost");
21f7563c
JP
1414 if (config_str) {
1415 port_s->path_cost = strtoul(config_str, NULL, 10);
1416 } else {
6c038611 1417 enum netdev_features current;
d02a5f8e 1418 unsigned int mbps;
21f7563c 1419
d02a5f8e
BP
1420 netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
1421 mbps = netdev_features_to_bps(current, 100 * 1000 * 1000) / 1000000;
1422 port_s->path_cost = stp_convert_speed_to_cost(mbps);
21f7563c
JP
1423 }
1424
a699f614 1425 config_str = smap_get(&port->cfg->other_config, "stp-port-priority");
21f7563c
JP
1426 if (config_str) {
1427 port_s->priority = strtoul(config_str, NULL, 0);
1428 } else {
1429 port_s->priority = STP_DEFAULT_PORT_PRIORITY;
1430 }
1431}
1432
9efd308e
DV
1433static void
1434port_configure_rstp(const struct ofproto *ofproto, struct port *port,
1435 struct ofproto_port_rstp_settings *port_s, int *port_num_counter)
1436{
1437 const char *config_str;
1438 struct iface *iface;
1439
1440 if (!smap_get_bool(&port->cfg->other_config, "rstp-enable", true)) {
1441 port_s->enable = false;
1442 return;
1443 } else {
1444 port_s->enable = true;
1445 }
1446
1447 /* RSTP over bonds is not supported. */
417e7e66 1448 if (!ovs_list_is_singleton(&port->ifaces)) {
9efd308e
DV
1449 VLOG_ERR("port %s: cannot enable RSTP on bonds, disabling",
1450 port->name);
1451 port_s->enable = false;
1452 return;
1453 }
1454
417e7e66 1455 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
9efd308e
DV
1456
1457 /* Internal ports shouldn't participate in spanning tree, so
1458 * skip them. */
1459 if (!strcmp(iface->type, "internal")) {
1460 VLOG_DBG("port %s: disable RSTP on internal ports", port->name);
1461 port_s->enable = false;
1462 return;
1463 }
1464
1465 /* RSTP on mirror output ports is not supported. */
1466 if (ofproto_is_mirror_output_bundle(ofproto, port)) {
1467 VLOG_DBG("port %s: disable RSTP on mirror ports", port->name);
1468 port_s->enable = false;
1469 return;
1470 }
1471
1472 config_str = smap_get(&port->cfg->other_config, "rstp-port-num");
1473 if (config_str) {
1474 unsigned long int port_num = strtoul(config_str, NULL, 0);
1475 if (port_num < 1 || port_num > RSTP_MAX_PORTS) {
1476 VLOG_ERR("port %s: invalid rstp-port-num", port->name);
1477 port_s->enable = false;
1478 return;
1479 }
1480 port_s->port_num = port_num;
4b30ba05 1481 } else {
9efd308e
DV
1482 if (*port_num_counter >= RSTP_MAX_PORTS) {
1483 VLOG_ERR("port %s: too many RSTP ports, disabling", port->name);
1484 port_s->enable = false;
1485 return;
1486 }
4b30ba05
JR
1487 /* If rstp-port-num is not specified, use 0.
1488 * rstp_port_set_port_number() will look for the first free one. */
9efd308e
DV
1489 port_s->port_num = 0;
1490 }
1491
5b776960 1492 /* Increment the port num counter, because we only support
1493 * RSTP_MAX_PORTS rstp ports. */
1494 (*port_num_counter)++;
1495
9efd308e
DV
1496 config_str = smap_get(&port->cfg->other_config, "rstp-path-cost");
1497 if (config_str) {
1498 port_s->path_cost = strtoul(config_str, NULL, 10);
1499 } else {
1500 enum netdev_features current;
1501 unsigned int mbps;
1502
1503 netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
1504 mbps = netdev_features_to_bps(current, 100 * 1000 * 1000) / 1000000;
1505 port_s->path_cost = rstp_convert_speed_to_cost(mbps);
1506 }
1507
1508 config_str = smap_get(&port->cfg->other_config, "rstp-port-priority");
1509 if (config_str) {
1510 port_s->priority = strtoul(config_str, NULL, 0);
1511 } else {
1512 port_s->priority = RSTP_DEFAULT_PORT_PRIORITY;
1513 }
1514
13c1637f
BP
1515 port_s->admin_p2p_mac_state = smap_get_ullong(
1516 &port->cfg->other_config, "rstp-admin-p2p-mac",
1517 RSTP_ADMIN_P2P_MAC_FORCE_TRUE);
67e8c1ac
JR
1518
1519 port_s->admin_port_state = smap_get_bool(&port->cfg->other_config,
1520 "rstp-admin-port-state", true);
1521
9efd308e
DV
1522 port_s->admin_edge_port = smap_get_bool(&port->cfg->other_config,
1523 "rstp-port-admin-edge", false);
1524 port_s->auto_edge = smap_get_bool(&port->cfg->other_config,
1525 "rstp-port-auto-edge", true);
1526 port_s->mcheck = smap_get_bool(&port->cfg->other_config,
1527 "rstp-port-mcheck", false);
1528}
1529
21f7563c
JP
1530/* Set spanning tree configuration on 'br'. */
1531static void
7af77bbd 1532bridge_configure_stp(struct bridge *br, bool enable_stp)
21f7563c 1533{
7af77bbd 1534 if (!enable_stp) {
9efd308e 1535 ofproto_set_stp(br->ofproto, NULL);
21f7563c
JP
1536 } else {
1537 struct ofproto_stp_settings br_s;
1538 const char *config_str;
1539 struct port *port;
1540 int port_num_counter;
1541 unsigned long *port_num_bitmap;
1542
a699f614 1543 config_str = smap_get(&br->cfg->other_config, "stp-system-id");
21f7563c 1544 if (config_str) {
74ff3298 1545 struct eth_addr ea;
21f7563c 1546
74ff3298 1547 if (eth_addr_from_string(config_str, &ea)) {
21f7563c
JP
1548 br_s.system_id = eth_addr_to_uint64(ea);
1549 } else {
1550 br_s.system_id = eth_addr_to_uint64(br->ea);
1551 VLOG_ERR("bridge %s: invalid stp-system-id, defaulting "
1552 "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
1553 }
1554 } else {
1555 br_s.system_id = eth_addr_to_uint64(br->ea);
1556 }
1557
13c1637f
BP
1558 br_s.priority = smap_get_ullong(&br->cfg->other_config, "stp-priority",
1559 STP_DEFAULT_BRIDGE_PRIORITY);
1560 br_s.hello_time = smap_get_ullong(&br->cfg->other_config,
1561 "stp-hello-time",
1562 STP_DEFAULT_HELLO_TIME);
21f7563c 1563
13c1637f 1564 br_s.max_age = smap_get_ullong(&br->cfg->other_config, "stp-max-age",
f038406c 1565 STP_DEFAULT_MAX_AGE / 1000) * 1000;
13c1637f
BP
1566 br_s.fwd_delay = smap_get_ullong(&br->cfg->other_config,
1567 "stp-forward-delay",
1568 STP_DEFAULT_FWD_DELAY / 1000) * 1000;
21f7563c
JP
1569
1570 /* Configure STP on the bridge. */
1571 if (ofproto_set_stp(br->ofproto, &br_s)) {
1572 VLOG_ERR("bridge %s: could not enable STP", br->name);
1573 return;
1574 }
1575
1576 /* Users must either set the port number with the "stp-port-num"
1577 * configuration on all ports or none. If manual configuration
1578 * is not done, then we allocate them sequentially. */
1579 port_num_counter = 0;
1580 port_num_bitmap = bitmap_allocate(STP_MAX_PORTS);
1581 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1582 struct ofproto_port_stp_settings port_s;
1583 struct iface *iface;
1584
1585 port_configure_stp(br->ofproto, port, &port_s,
1586 &port_num_counter, port_num_bitmap);
1587
1588 /* As bonds are not supported, just apply configuration to
1589 * all interfaces. */
1590 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1591 if (ofproto_port_set_stp(br->ofproto, iface->ofp_port,
1592 &port_s)) {
1593 VLOG_ERR("port %s: could not enable STP", port->name);
1594 continue;
1595 }
1596 }
1597 }
1598
b71273f6 1599 if (bitmap_scan(port_num_bitmap, 1, 0, STP_MAX_PORTS) != STP_MAX_PORTS
21f7563c
JP
1600 && port_num_counter) {
1601 VLOG_ERR("bridge %s: must manually configure all STP port "
1602 "IDs or none, disabling", br->name);
1603 ofproto_set_stp(br->ofproto, NULL);
1604 }
1605 bitmap_free(port_num_bitmap);
1606 }
1607}
1608
9efd308e 1609static void
7af77bbd 1610bridge_configure_rstp(struct bridge *br, bool enable_rstp)
9efd308e 1611{
7af77bbd 1612 if (!enable_rstp) {
9efd308e 1613 ofproto_set_rstp(br->ofproto, NULL);
9efd308e
DV
1614 } else {
1615 struct ofproto_rstp_settings br_s;
1616 const char *config_str;
1617 struct port *port;
1618 int port_num_counter;
1619
1620 config_str = smap_get(&br->cfg->other_config, "rstp-address");
1621 if (config_str) {
74ff3298 1622 struct eth_addr ea;
9efd308e 1623
74ff3298 1624 if (eth_addr_from_string(config_str, &ea)) {
9efd308e
DV
1625 br_s.address = eth_addr_to_uint64(ea);
1626 }
1627 else {
1628 br_s.address = eth_addr_to_uint64(br->ea);
1629 VLOG_ERR("bridge %s: invalid rstp-address, defaulting "
1630 "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
1631 }
1632 }
1633 else {
1634 br_s.address = eth_addr_to_uint64(br->ea);
1635 }
1636
13c1637f
BP
1637 const struct smap *oc = &br->cfg->other_config;
1638 br_s.priority = smap_get_ullong(oc, "rstp-priority",
1639 RSTP_DEFAULT_PRIORITY);
1640 br_s.ageing_time = smap_get_ullong(oc, "rstp-ageing-time",
1641 RSTP_DEFAULT_AGEING_TIME);
1642 br_s.force_protocol_version = smap_get_ullong(
1643 oc, "rstp-force-protocol-version", FPV_DEFAULT);
1644 br_s.bridge_max_age = smap_get_ullong(oc, "rstp-max-age",
1645 RSTP_DEFAULT_BRIDGE_MAX_AGE);
1646 br_s.bridge_forward_delay = smap_get_ullong(
1647 oc, "rstp-forward-delay", RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
1648 br_s.transmit_hold_count = smap_get_ullong(
1649 oc, "rstp-transmit-hold-count", RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
9efd308e
DV
1650
1651 /* Configure RSTP on the bridge. */
1652 if (ofproto_set_rstp(br->ofproto, &br_s)) {
1653 VLOG_ERR("bridge %s: could not enable RSTP", br->name);
1654 return;
1655 }
1656
1657 port_num_counter = 0;
1658 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1659 struct ofproto_port_rstp_settings port_s;
1660 struct iface *iface;
1661
1662 port_configure_rstp(br->ofproto, port, &port_s,
1663 &port_num_counter);
1664
1665 /* As bonds are not supported, just apply configuration to
1666 * all interfaces. */
1667 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1668 if (ofproto_port_set_rstp(br->ofproto, iface->ofp_port,
1669 &port_s)) {
1670 VLOG_ERR("port %s: could not enable RSTP", port->name);
1671 continue;
1672 }
1673 }
1674 }
1675 }
1676}
1677
7af77bbd
BP
1678static void
1679bridge_configure_spanning_tree(struct bridge *br)
1680{
1681 bool enable_rstp = br->cfg->rstp_enable;
1682 bool enable_stp = br->cfg->stp_enable;
1683
1684 if (enable_rstp && enable_stp) {
1685 VLOG_WARN("%s: RSTP and STP are mutually exclusive but both are "
1686 "configured; enabling RSTP", br->name);
1687 enable_stp = false;
1688 }
1689
1690 bridge_configure_stp(br, enable_stp);
1691 bridge_configure_rstp(br, enable_rstp);
1692}
1693
e8192d80
BP
1694static bool
1695bridge_has_bond_fake_iface(const struct bridge *br, const char *name)
1696{
1697 const struct port *port = port_lookup(br, name);
1698 return port && port_is_bond_fake_iface(port);
093e47f4
BP
1699}
1700
e8192d80
BP
1701static bool
1702port_is_bond_fake_iface(const struct port *port)
093e47f4 1703{
417e7e66 1704 return port->cfg->bond_fake_iface && !ovs_list_is_short(&port->ifaces);
e8192d80 1705}
093e47f4 1706
66da9bef
BP
1707static void
1708add_del_bridges(const struct ovsrec_open_vswitch *cfg)
1709{
1710 struct bridge *br, *next;
e6448d2f 1711 struct shash_node *node;
66da9bef
BP
1712 struct shash new_br;
1713 size_t i;
1714
1715 /* Collect new bridges' names and types. */
1716 shash_init(&new_br);
1717 for (i = 0; i < cfg->n_bridges; i++) {
5af5b532 1718 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
66da9bef 1719 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
5af5b532 1720
3cbe33df 1721 if (strchr(br_cfg->name, '/') || strchr(br_cfg->name, '\\')) {
5af5b532 1722 /* Prevent remote ovsdb-server users from accessing arbitrary
3cbe33df
BP
1723 * directories, e.g. consider a bridge named "../../../etc/".
1724 *
1725 * Prohibiting "\" is only necessary on Windows but it's no great
1726 * loss elsewhere. */
5af5b532
BP
1727 VLOG_WARN_RL(&rl, "ignoring bridge with invalid name \"%s\"",
1728 br_cfg->name);
1729 } else if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
1730 VLOG_WARN_RL(&rl, "bridge %s specified twice", br_cfg->name);
66da9bef
BP
1731 }
1732 }
1733
1734 /* Get rid of deleted bridges or those whose types have changed.
1735 * Update 'cfg' of bridges that still exist. */
1736 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
1737 br->cfg = shash_find_data(&new_br, br->name);
f79e673f
BP
1738 if (!br->cfg || strcmp(br->type, ofproto_normalize_type(
1739 br->cfg->datapath_type))) {
9aad5a5a 1740 bridge_destroy(br, true);
66da9bef
BP
1741 }
1742 }
1743
1744 /* Add new bridges. */
e6448d2f
DDP
1745 SHASH_FOR_EACH(node, &new_br) {
1746 const struct ovsrec_bridge *br_cfg = node->data;
71f21279 1747 if (!bridge_lookup(br_cfg->name)) {
66da9bef
BP
1748 bridge_create(br_cfg);
1749 }
1750 }
1751
1752 shash_destroy(&new_br);
1753}
1754
2a485ee8
BP
1755/* Configures 'netdev' based on the "options" column in 'iface_cfg'.
1756 * Returns 0 if successful, otherwise a positive errno value. */
1757static int
1758iface_set_netdev_config(const struct ovsrec_interface *iface_cfg,
bbe6109d 1759 struct netdev *netdev, char **errp)
2a485ee8 1760{
bbe6109d 1761 return netdev_set_config(netdev, &iface_cfg->options, errp);
2a485ee8
BP
1762}
1763
fb2ec1e9
YT
1764/* Opens a network device for 'if_cfg' and configures it. Adds the network
1765 * device to br->ofproto and stores the OpenFlow port number in '*ofp_portp'.
cc086465
BP
1766 *
1767 * If successful, returns 0 and stores the network device in '*netdevp'. On
1768 * failure, returns a positive errno value and stores NULL in '*netdevp'. */
1769static int
1770iface_do_create(const struct bridge *br,
2a73b1d7 1771 const struct ovsrec_interface *iface_cfg,
bbe6109d
TG
1772 ofp_port_t *ofp_portp, struct netdev **netdevp,
1773 char **errp)
cc086465 1774{
2291eb08 1775 struct netdev *netdev = NULL;
cc086465 1776 int error;
a5bdd3b2 1777 const char *type;
cc086465 1778
94a53842
AW
1779 if (netdev_is_reserved_name(iface_cfg->name)) {
1780 VLOG_WARN("could not create interface %s, name is reserved",
1781 iface_cfg->name);
1782 error = EINVAL;
1783 goto error;
1784 }
1785
c381bca5 1786 type = ofproto_port_open_type(br->ofproto,
a5bdd3b2
TLSC
1787 iface_get_type(iface_cfg, br->cfg));
1788 error = netdev_open(iface_cfg->name, type, &netdev);
cc086465 1789 if (error) {
bbe6109d
TG
1790 VLOG_WARN_BUF(errp, "could not open network device %s (%s)",
1791 iface_cfg->name, ovs_strerror(error));
cc086465
BP
1792 goto error;
1793 }
1794
bbe6109d 1795 error = iface_set_netdev_config(iface_cfg, netdev, errp);
cc086465
BP
1796 if (error) {
1797 goto error;
1798 }
1799
3a414a0a 1800 iface_set_netdev_mtu(iface_cfg, netdev);
7c12e200 1801
2a73b1d7
BP
1802 *ofp_portp = iface_pick_ofport(iface_cfg);
1803 error = ofproto_port_add(br->ofproto, netdev, ofp_portp);
1804 if (error) {
7f381c2e
DDP
1805 VLOG_WARN_BUF(errp, "could not add network device %s to ofproto (%s)",
1806 iface_cfg->name, ovs_strerror(error));
2a73b1d7 1807 goto error;
cc086465
BP
1808 }
1809
2a73b1d7
BP
1810 VLOG_INFO("bridge %s: added interface %s on port %d",
1811 br->name, iface_cfg->name, *ofp_portp);
1812
cc086465
BP
1813 *netdevp = netdev;
1814 return 0;
1815
1816error:
1817 *netdevp = NULL;
1818 netdev_close(netdev);
1819 return error;
1820}
1821
bae7208e 1822/* Creates a new iface on 'br' based on 'if_cfg'. The new iface has OpenFlow
c60c33fb 1823 * port number 'ofp_port'. If ofp_port is OFPP_NONE, an OpenFlow port is
bae7208e 1824 * automatically allocated for the iface. Takes ownership of and
01546f5d
BP
1825 * deallocates 'if_cfg'.
1826 *
1827 * Return true if an iface is successfully created, false otherwise. */
1828static bool
2a73b1d7
BP
1829iface_create(struct bridge *br, const struct ovsrec_interface *iface_cfg,
1830 const struct ovsrec_port *port_cfg)
66da9bef 1831{
cc086465 1832 struct netdev *netdev;
bae7208e 1833 struct iface *iface;
2a73b1d7 1834 ofp_port_t ofp_port;
bae7208e 1835 struct port *port;
bbe6109d 1836 char *errp = NULL;
bae7208e 1837 int error;
52a90c29 1838
a6b7506d 1839 /* Do the bits that can fail up front. */
cb22974d 1840 ovs_assert(!iface_lookup(br, iface_cfg->name));
42deb67d 1841 error = iface_do_create(br, iface_cfg, &ofp_port, &netdev, &errp);
bae7208e 1842 if (error) {
bbe6109d
TG
1843 iface_clear_db_record(iface_cfg, errp);
1844 free(errp);
2a73b1d7 1845 return false;
bae7208e 1846 }
c7e7bb21 1847
cc086465
BP
1848 /* Get or create the port structure. */
1849 port = port_lookup(br, port_cfg->name);
1850 if (!port) {
1851 port = port_create(br, port_cfg);
bae7208e
EJ
1852 }
1853
cc086465
BP
1854 /* Create the iface structure. */
1855 iface = xzalloc(sizeof *iface);
417e7e66 1856 ovs_list_push_back(&port->ifaces, &iface->port_elem);
cc086465
BP
1857 hmap_insert(&br->iface_by_name, &iface->name_node,
1858 hash_string(iface_cfg->name, 0));
1859 iface->port = port;
1860 iface->name = xstrdup(iface_cfg->name);
2a73b1d7 1861 iface->ofp_port = ofp_port;
cc086465
BP
1862 iface->netdev = netdev;
1863 iface->type = iface_get_type(iface_cfg, br->cfg);
1864 iface->cfg = iface_cfg;
2a73b1d7
BP
1865 hmap_insert(&br->ifaces, &iface->ofp_port_node,
1866 hash_ofp_port(ofp_port));
8b5da7a6 1867
cc086465
BP
1868 /* Populate initial status in database. */
1869 iface_refresh_stats(iface);
6a5f9a8f 1870 iface_refresh_netdev_status(iface);
bae7208e
EJ
1871
1872 /* Add bond fake iface if necessary. */
1873 if (port_is_bond_fake_iface(port)) {
8b5da7a6
BP
1874 struct ofproto_port ofproto_port;
1875
bae7208e
EJ
1876 if (ofproto_port_query_by_name(br->ofproto, port->name,
1877 &ofproto_port)) {
bae7208e
EJ
1878 error = netdev_open(port->name, "internal", &netdev);
1879 if (!error) {
4abb8608 1880 ofp_port_t fake_ofp_port = OFPP_NONE;
0d7bb1b4 1881 ofproto_port_add(br->ofproto, netdev, &fake_ofp_port);
bae7208e 1882 netdev_close(netdev);
66da9bef 1883 } else {
bae7208e 1884 VLOG_WARN("could not open network device %s (%s)",
10a89ef0 1885 port->name, ovs_strerror(error));
66da9bef 1886 }
bae7208e
EJ
1887 } else {
1888 /* Already exists, nothing to do. */
1889 ofproto_port_destroy(&ofproto_port);
66da9bef
BP
1890 }
1891 }
01546f5d 1892
2a73b1d7 1893 return true;
66da9bef
BP
1894}
1895
8402c74b
SS
1896/* Set forward BPDU option. */
1897static void
1898bridge_configure_forward_bpdu(struct bridge *br)
1899{
a699f614
EJ
1900 ofproto_set_forward_bpdu(br->ofproto,
1901 smap_get_bool(&br->cfg->other_config,
1902 "forward-bpdu",
1903 false));
8402c74b
SS
1904}
1905
c4069512 1906/* Set MAC learning table configuration for 'br'. */
e764773c 1907static void
c4069512 1908bridge_configure_mac_table(struct bridge *br)
e764773c 1909{
13c1637f
BP
1910 const struct smap *oc = &br->cfg->other_config;
1911 int idle_time = smap_get_int(oc, "mac-aging-time", 0);
1912 if (!idle_time) {
1913 idle_time = MAC_ENTRY_DEFAULT_IDLE_TIME;
1914 }
c4069512 1915
13c1637f
BP
1916 int mac_table_size = smap_get_int(oc, "mac-table-size", 0);
1917 if (!mac_table_size) {
1918 mac_table_size = MAC_DEFAULT_MAX;
1919 }
c4069512
BP
1920
1921 ofproto_set_mac_table_config(br->ofproto, idle_time, mac_table_size);
e764773c
BP
1922}
1923
dc2b70ba
FL
1924/* Set multicast snooping table configuration for 'br'. */
1925static void
1926bridge_configure_mcast_snooping(struct bridge *br)
1927{
1928 if (!br->cfg->mcast_snooping_enable) {
1929 ofproto_set_mcast_snooping(br->ofproto, NULL);
1930 } else {
1931 struct port *port;
1932 struct ofproto_mcast_snooping_settings br_s;
13c1637f
BP
1933
1934 const struct smap *oc = &br->cfg->other_config;
1935 int idle_time = smap_get_int(oc, "mcast-snooping-aging-time", 0);
1936 br_s.idle_time = idle_time ? idle_time : MCAST_ENTRY_DEFAULT_IDLE_TIME;
1937 int max_entries = smap_get_int(oc, "mcast-snooping-table-size", 0);
1938 br_s.max_entries = (max_entries
1939 ? max_entries
dc2b70ba
FL
1940 : MCAST_DEFAULT_MAX_ENTRIES);
1941
13c1637f
BP
1942 br_s.flood_unreg = !smap_get_bool(
1943 oc, "mcast-snooping-disable-flood-unregistered", false);
dc2b70ba
FL
1944
1945 /* Configure multicast snooping on the bridge */
1946 if (ofproto_set_mcast_snooping(br->ofproto, &br_s)) {
1947 VLOG_ERR("bridge %s: could not enable multicast snooping",
1948 br->name);
1949 return;
1950 }
1951
1952 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
8e04a33f
FL
1953 struct ofproto_mcast_snooping_port_settings port_s;
1954 port_s.flood = smap_get_bool(&port->cfg->other_config,
dc2b70ba 1955 "mcast-snooping-flood", false);
8e04a33f
FL
1956 port_s.flood_reports = smap_get_bool(&port->cfg->other_config,
1957 "mcast-snooping-flood-reports", false);
1958 if (ofproto_port_set_mcast_snooping(br->ofproto, port, &port_s)) {
dc2b70ba
FL
1959 VLOG_ERR("port %s: could not configure mcast snooping",
1960 port->name);
1961 }
1962 }
1963 }
1964}
1965
064af421 1966static void
74ff3298 1967find_local_hw_addr(const struct bridge *br, struct eth_addr *ea,
21a955fc 1968 const struct port *fake_br, struct iface **hw_addr_iface)
064af421 1969{
f145afdc 1970 struct hmapx mirror_output_ports;
8052fb14 1971 struct port *port;
3a48ace3 1972 bool found_addr = false;
064af421 1973 int error;
f145afdc 1974 int i;
064af421 1975
f145afdc
BP
1976 /* Mirror output ports don't participate in picking the local hardware
1977 * address. ofproto can't help us find out whether a given port is a
1978 * mirror output because we haven't configured mirrors yet, so we need to
1979 * accumulate them ourselves. */
1980 hmapx_init(&mirror_output_ports);
1981 for (i = 0; i < br->cfg->n_mirrors; i++) {
1982 struct ovsrec_mirror *m = br->cfg->mirrors[i];
1983 if (m->output_port) {
1984 hmapx_add(&mirror_output_ports, m->output_port);
1985 }
1986 }
1987
141f4942
BP
1988 /* Otherwise choose the minimum non-local MAC address among all of the
1989 * interfaces. */
8052fb14 1990 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
74ff3298 1991 struct eth_addr iface_ea;
83db7968 1992 struct iface *candidate;
58b7527e
BP
1993 struct iface *iface;
1994
1995 /* Mirror output ports don't participate. */
f145afdc 1996 if (hmapx_contains(&mirror_output_ports, port->cfg)) {
064af421
BP
1997 continue;
1998 }
58b7527e
BP
1999
2000 /* Choose the MAC address to represent the port. */
83db7968 2001 iface = NULL;
74ff3298
JR
2002 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac,
2003 &iface_ea)) {
ba09980a
BP
2004 /* Find the interface with this Ethernet address (if any) so that
2005 * we can provide the correct devname to the caller. */
83db7968 2006 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
74ff3298
JR
2007 struct eth_addr candidate_ea;
2008 if (!netdev_get_etheraddr(candidate->netdev, &candidate_ea)
ba09980a
BP
2009 && eth_addr_equals(iface_ea, candidate_ea)) {
2010 iface = candidate;
2011 }
2012 }
58b7527e
BP
2013 } else {
2014 /* Choose the interface whose MAC address will represent the port.
2015 * The Linux kernel bonding code always chooses the MAC address of
2016 * the first slave added to a bond, and the Fedora networking
2017 * scripts always add slaves to a bond in alphabetical order, so
2018 * for compatibility we choose the interface with the name that is
2019 * first in alphabetical order. */
83db7968
BP
2020 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
2021 if (!iface || strcmp(candidate->name, iface->name) < 0) {
58b7527e
BP
2022 iface = candidate;
2023 }
2024 }
2025
f57f0806
BP
2026 /* A port always has at least one interface. */
2027 ovs_assert(iface != NULL);
2028
58b7527e 2029 /* The local port doesn't count (since we're trying to choose its
141f4942 2030 * MAC address anyway). */
892815f5 2031 if (iface->ofp_port == OFPP_LOCAL) {
064af421
BP
2032 continue;
2033 }
58b7527e 2034
21a955fc
HS
2035 /* For fake bridges we only choose from ports with the same tag */
2036 if (fake_br && fake_br->cfg && fake_br->cfg->tag) {
2037 if (!port->cfg->tag) {
2038 continue;
2039 }
2040 if (*port->cfg->tag != *fake_br->cfg->tag) {
2041 continue;
2042 }
2043 }
2044
58b7527e 2045 /* Grab MAC. */
74ff3298 2046 error = netdev_get_etheraddr(iface->netdev, &iface_ea);
58b7527e 2047 if (error) {
58b7527e 2048 continue;
064af421
BP
2049 }
2050 }
58b7527e
BP
2051
2052 /* Compare against our current choice. */
2053 if (!eth_addr_is_multicast(iface_ea) &&
141f4942 2054 !eth_addr_is_local(iface_ea) &&
58b7527e
BP
2055 !eth_addr_is_reserved(iface_ea) &&
2056 !eth_addr_is_zero(iface_ea) &&
74ff3298 2057 (!found_addr || eth_addr_compare_3way(iface_ea, *ea) < 0))
58b7527e 2058 {
74ff3298 2059 *ea = iface_ea;
8fef8c71 2060 *hw_addr_iface = iface;
3a48ace3 2061 found_addr = true;
58b7527e 2062 }
064af421 2063 }
f145afdc 2064
51b67a10 2065 if (!found_addr) {
74ff3298 2066 *ea = br->default_ea;
51b67a10
JP
2067 *hw_addr_iface = NULL;
2068 }
2069
f145afdc 2070 hmapx_destroy(&mirror_output_ports);
064af421
BP
2071}
2072
4bc1cfde 2073static void
74ff3298 2074bridge_pick_local_hw_addr(struct bridge *br, struct eth_addr *ea,
4bc1cfde
HS
2075 struct iface **hw_addr_iface)
2076{
4bc1cfde
HS
2077 *hw_addr_iface = NULL;
2078
2079 /* Did the user request a particular MAC? */
13c1637f
BP
2080 const char *hwaddr = smap_get_def(&br->cfg->other_config, "hwaddr", "");
2081 if (eth_addr_from_string(hwaddr, ea)) {
74ff3298 2082 if (eth_addr_is_multicast(*ea)) {
4bc1cfde 2083 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
74ff3298
JR
2084 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(*ea));
2085 } else if (eth_addr_is_zero(*ea)) {
4bc1cfde
HS
2086 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
2087 } else {
2088 return;
2089 }
2090 }
2091
2092 /* Find a local hw address */
21a955fc 2093 find_local_hw_addr(br, ea, NULL, hw_addr_iface);
4bc1cfde
HS
2094}
2095
064af421
BP
2096/* Choose and returns the datapath ID for bridge 'br' given that the bridge
2097 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
07c318f4
BP
2098 * an interface on 'br', then that interface must be passed in as
2099 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
2100 * 'hw_addr_iface' must be passed in as a null pointer. */
064af421
BP
2101static uint64_t
2102bridge_pick_datapath_id(struct bridge *br,
74ff3298 2103 const struct eth_addr bridge_ea,
07c318f4 2104 struct iface *hw_addr_iface)
064af421
BP
2105{
2106 /*
2107 * The procedure for choosing a bridge MAC address will, in the most
2108 * ordinary case, also choose a unique MAC that we can use as a datapath
2109 * ID. In some special cases, though, multiple bridges will end up with
2110 * the same MAC address. This is OK for the bridges, but it will confuse
2111 * the OpenFlow controller, because each datapath needs a unique datapath
2112 * ID.
2113 *
2114 * Datapath IDs must be unique. It is also very desirable that they be
2115 * stable from one run to the next, so that policy set on a datapath
2116 * "sticks".
2117 */
093e47f4 2118 const char *datapath_id;
064af421
BP
2119 uint64_t dpid;
2120
13c1637f
BP
2121 datapath_id = smap_get_def(&br->cfg->other_config, "datapath-id", "");
2122 if (dpid_from_string(datapath_id, &dpid)) {
064af421
BP
2123 return dpid;
2124 }
2125
03eae5f8 2126 if (!hw_addr_iface) {
064af421
BP
2127 /*
2128 * A purely internal bridge, that is, one that has no non-virtual
03eae5f8 2129 * network devices on it at all, is difficult because it has no
064af421
BP
2130 * natural unique identifier at all.
2131 *
2132 * When the host is a XenServer, we handle this case by hashing the
2133 * host's UUID with the name of the bridge. Names of bridges are
2134 * persistent across XenServer reboots, although they can be reused if
2135 * an internal network is destroyed and then a new one is later
2136 * created, so this is fairly effective.
2137 *
2138 * When the host is not a XenServer, we punt by using a random MAC
2139 * address on each run.
2140 */
2141 const char *host_uuid = xenserver_get_host_uuid();
2142 if (host_uuid) {
2143 char *combined = xasprintf("%s,%s", host_uuid, br->name);
2144 dpid = dpid_from_hash(combined, strlen(combined));
2145 free(combined);
2146 return dpid;
2147 }
2148 }
2149
2150 return eth_addr_to_uint64(bridge_ea);
2151}
2152
2153static uint64_t
2154dpid_from_hash(const void *data, size_t n)
2155{
74ff3298
JR
2156 union {
2157 uint8_t bytes[SHA1_DIGEST_SIZE];
2158 struct eth_addr ea;
2159 } hash;
2160
2161 sha1_bytes(data, n, hash.bytes);
2162 eth_addr_mark_random(&hash.ea);
2163 return eth_addr_to_uint64(hash.ea);
064af421
BP
2164}
2165
ea83a2fc 2166static void
6a5f9a8f 2167iface_refresh_netdev_status(struct iface *iface)
ea83a2fc 2168{
79f1cbe9 2169 struct smap smap;
ea763e0e 2170
6c038611 2171 enum netdev_features current;
6a5f9a8f
AW
2172 enum netdev_flags flags;
2173 const char *link_state;
74ff3298 2174 struct eth_addr mac;
6a5f9a8f
AW
2175 int64_t bps, mtu_64, ifindex64, link_resets;
2176 int mtu, error;
e210037e 2177
cfea354b
BP
2178 if (iface_is_synthetic(iface)) {
2179 return;
2180 }
2181
15c9fbd8
AW
2182 if (iface->change_seq == netdev_get_change_seq(iface->netdev)
2183 && !status_txn_try_again) {
6a5f9a8f
AW
2184 return;
2185 }
2186
2187 iface->change_seq = netdev_get_change_seq(iface->netdev);
2188
79f1cbe9 2189 smap_init(&smap);
ea763e0e 2190
275707c3 2191 if (!netdev_get_status(iface->netdev, &smap)) {
a699f614 2192 ovsrec_interface_set_status(iface->cfg, &smap);
ea763e0e 2193 } else {
a699f614 2194 ovsrec_interface_set_status(iface->cfg, NULL);
ea763e0e
EJ
2195 }
2196
79f1cbe9 2197 smap_destroy(&smap);
e210037e 2198
6a5f9a8f
AW
2199 error = netdev_get_flags(iface->netdev, &flags);
2200 if (!error) {
2201 const char *state = flags & NETDEV_UP ? "up" : "down";
2202
2203 ovsrec_interface_set_admin_state(iface->cfg, state);
2204 } else {
2205 ovsrec_interface_set_admin_state(iface->cfg, NULL);
2206 }
2207
2208 link_state = netdev_get_carrier(iface->netdev) ? "up" : "down";
2209 ovsrec_interface_set_link_state(iface->cfg, link_state);
2210
2211 link_resets = netdev_get_carrier_resets(iface->netdev);
2212 ovsrec_interface_set_link_resets(iface->cfg, &link_resets, 1);
2213
e210037e 2214 error = netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
d02a5f8e
BP
2215 bps = !error ? netdev_features_to_bps(current, 0) : 0;
2216 if (bps) {
e210037e
AE
2217 ovsrec_interface_set_duplex(iface->cfg,
2218 netdev_features_is_full_duplex(current)
2219 ? "full" : "half");
e210037e 2220 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
d0db8de2 2221 } else {
e210037e
AE
2222 ovsrec_interface_set_duplex(iface->cfg, NULL);
2223 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
2224 }
2225
e210037e 2226 error = netdev_get_mtu(iface->netdev, &mtu);
9b020780 2227 if (!error) {
e210037e
AE
2228 mtu_64 = mtu;
2229 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
8d6db33e 2230 } else {
e210037e
AE
2231 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
2232 }
df867eda 2233
74ff3298 2234 error = netdev_get_etheraddr(iface->netdev, &mac);
df867eda 2235 if (!error) {
b0d390e5 2236 char mac_string[ETH_ADDR_STRLEN + 1];
df867eda 2237
b0d390e5
BP
2238 snprintf(mac_string, sizeof mac_string,
2239 ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
df867eda
JP
2240 ovsrec_interface_set_mac_in_use(iface->cfg, mac_string);
2241 } else {
2242 ovsrec_interface_set_mac_in_use(iface->cfg, NULL);
2243 }
ea401d9a
NM
2244
2245 /* The netdev may return a negative number (such as -EOPNOTSUPP)
2246 * if there is no valid ifindex number. */
2247 ifindex64 = netdev_get_ifindex(iface->netdev);
2248 if (ifindex64 < 0) {
2249 ifindex64 = 0;
2250 }
2251 ovsrec_interface_set_ifindex(iface->cfg, &ifindex64, 1);
ea83a2fc
EJ
2252}
2253
6a5f9a8f
AW
2254static void
2255iface_refresh_ofproto_status(struct iface *iface)
2256{
8f5514fe 2257 int current;
6a5f9a8f
AW
2258
2259 if (iface_is_synthetic(iface)) {
2260 return;
2261 }
2262
2263 current = ofproto_port_is_lacp_current(iface->port->bridge->ofproto,
2264 iface->ofp_port);
2265 if (current >= 0) {
2266 bool bl = current;
2267 ovsrec_interface_set_lacp_current(iface->cfg, &bl, 1);
2268 } else {
2269 ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
2270 }
2271
8f5514fe 2272 if (ofproto_port_cfm_status_changed(iface->port->bridge->ofproto,
15c9fbd8
AW
2273 iface->ofp_port)
2274 || status_txn_try_again) {
8f5514fe
AW
2275 iface_refresh_cfm_stats(iface);
2276 }
6a5f9a8f 2277
8f5514fe 2278 if (ofproto_port_bfd_status_changed(iface->port->bridge->ofproto,
15c9fbd8
AW
2279 iface->ofp_port)
2280 || status_txn_try_again) {
8f5514fe
AW
2281 struct smap smap;
2282
2283 smap_init(&smap);
2284 ofproto_port_get_bfd_status(iface->port->bridge->ofproto,
2285 iface->ofp_port, &smap);
6a5f9a8f 2286 ovsrec_interface_set_bfd_status(iface->cfg, &smap);
8f5514fe 2287 smap_destroy(&smap);
6a5f9a8f 2288 }
6a5f9a8f
AW
2289}
2290
353079d0
EJ
2291/* Writes 'iface''s CFM statistics to the database. 'iface' must not be
2292 * synthetic. */
8f3fe844 2293static void
b31bcf60
EJ
2294iface_refresh_cfm_stats(struct iface *iface)
2295{
93b8df38 2296 const struct ovsrec_interface *cfg = iface->cfg;
685acfd9 2297 struct cfm_status status;
88bf179a 2298 int error;
9a9e3786 2299
88bf179a
AW
2300 error = ofproto_port_get_cfm_status(iface->port->bridge->ofproto,
2301 iface->ofp_port, &status);
8f5514fe 2302 if (error > 0) {
9a9e3786
BP
2303 ovsrec_interface_set_cfm_fault(cfg, NULL, 0);
2304 ovsrec_interface_set_cfm_fault_status(cfg, NULL, 0);
2305 ovsrec_interface_set_cfm_remote_opstate(cfg, NULL);
76c4290d 2306 ovsrec_interface_set_cfm_flap_count(cfg, NULL, 0);
9a9e3786
BP
2307 ovsrec_interface_set_cfm_health(cfg, NULL, 0);
2308 ovsrec_interface_set_cfm_remote_mpids(cfg, NULL, 0);
2309 } else {
b9380396 2310 const char *reasons[CFM_FAULT_N_REASONS];
9a9e3786 2311 int64_t cfm_health = status.health;
76c4290d 2312 int64_t cfm_flap_count = status.flap_count;
9a9e3786 2313 bool faulted = status.faults != 0;
b9380396
EJ
2314 size_t i, j;
2315
9a9e3786
BP
2316 ovsrec_interface_set_cfm_fault(cfg, &faulted, 1);
2317
b9380396
EJ
2318 j = 0;
2319 for (i = 0; i < CFM_FAULT_N_REASONS; i++) {
2320 int reason = 1 << i;
9a9e3786 2321 if (status.faults & reason) {
b9380396
EJ
2322 reasons[j++] = cfm_fault_reason_to_str(reason);
2323 }
2324 }
47058293 2325 ovsrec_interface_set_cfm_fault_status(cfg, reasons, j);
1c0333b6 2326
76c4290d
AW
2327 ovsrec_interface_set_cfm_flap_count(cfg, &cfm_flap_count, 1);
2328
18637fdc
BP
2329 if (status.remote_opstate >= 0) {
2330 const char *remote_opstate = status.remote_opstate ? "up" : "down";
2331 ovsrec_interface_set_cfm_remote_opstate(cfg, remote_opstate);
2332 } else {
2333 ovsrec_interface_set_cfm_remote_opstate(cfg, NULL);
2334 }
2335
9a9e3786
BP
2336 ovsrec_interface_set_cfm_remote_mpids(cfg,
2337 (const int64_t *)status.rmps,
2338 status.n_rmps);
4cd9ab26
BP
2339 if (cfm_health >= 0) {
2340 ovsrec_interface_set_cfm_health(cfg, &cfm_health, 1);
2341 } else {
2342 ovsrec_interface_set_cfm_health(cfg, NULL, 0);
2343 }
13b1b2ae
EJ
2344
2345 free(status.rmps);
3967a833 2346 }
b31bcf60
EJ
2347}
2348
018f1525
BP
2349static void
2350iface_refresh_stats(struct iface *iface)
2351{
971f4b39
MW
2352 struct netdev_custom_stats custom_stats;
2353 struct netdev_stats stats;
2354 int n;
2355 uint32_t i, counters_size;
2356
98dbe2dd 2357#define IFACE_STATS \
d6e3feb5 2358 IFACE_STAT(rx_packets, "rx_packets") \
2359 IFACE_STAT(tx_packets, "tx_packets") \
2360 IFACE_STAT(rx_bytes, "rx_bytes") \
2361 IFACE_STAT(tx_bytes, "tx_bytes") \
2362 IFACE_STAT(rx_dropped, "rx_dropped") \
2363 IFACE_STAT(tx_dropped, "tx_dropped") \
2364 IFACE_STAT(rx_errors, "rx_errors") \
2365 IFACE_STAT(tx_errors, "tx_errors") \
2366 IFACE_STAT(rx_frame_errors, "rx_frame_err") \
2367 IFACE_STAT(rx_over_errors, "rx_over_err") \
2368 IFACE_STAT(rx_crc_errors, "rx_crc_err") \
2369 IFACE_STAT(collisions, "collisions") \
2370 IFACE_STAT(rx_1_to_64_packets, "rx_1_to_64_packets") \
2371 IFACE_STAT(rx_65_to_127_packets, "rx_65_to_127_packets") \
2372 IFACE_STAT(rx_128_to_255_packets, "rx_128_to_255_packets") \
2373 IFACE_STAT(rx_256_to_511_packets, "rx_256_to_511_packets") \
2374 IFACE_STAT(rx_512_to_1023_packets, "rx_512_to_1023_packets") \
2383dfe6 2375 IFACE_STAT(rx_1024_to_1522_packets, "rx_1024_to_1522_packets") \
d6e3feb5 2376 IFACE_STAT(rx_1523_to_max_packets, "rx_1523_to_max_packets") \
2377 IFACE_STAT(tx_1_to_64_packets, "tx_1_to_64_packets") \
2378 IFACE_STAT(tx_65_to_127_packets, "tx_65_to_127_packets") \
2379 IFACE_STAT(tx_128_to_255_packets, "tx_128_to_255_packets") \
2380 IFACE_STAT(tx_256_to_511_packets, "tx_256_to_511_packets") \
2381 IFACE_STAT(tx_512_to_1023_packets, "tx_512_to_1023_packets") \
2383dfe6 2382 IFACE_STAT(tx_1024_to_1522_packets, "tx_1024_to_1522_packets") \
d6e3feb5 2383 IFACE_STAT(tx_1523_to_max_packets, "tx_1523_to_max_packets") \
2384 IFACE_STAT(tx_multicast_packets, "tx_multicast_packets") \
2385 IFACE_STAT(rx_broadcast_packets, "rx_broadcast_packets") \
2386 IFACE_STAT(tx_broadcast_packets, "tx_broadcast_packets") \
2387 IFACE_STAT(rx_undersized_errors, "rx_undersized_errors") \
2388 IFACE_STAT(rx_oversize_errors, "rx_oversize_errors") \
2389 IFACE_STAT(rx_fragmented_errors, "rx_fragmented_errors") \
2390 IFACE_STAT(rx_jabber_errors, "rx_jabber_errors")
98dbe2dd 2391
c2553db9
BP
2392#define IFACE_STAT(MEMBER, NAME) + 1
2393 enum { N_IFACE_STATS = IFACE_STATS };
98dbe2dd 2394#undef IFACE_STAT
018f1525 2395
cfea354b
BP
2396 if (iface_is_synthetic(iface)) {
2397 return;
2398 }
2399
971f4b39
MW
2400 netdev_get_custom_stats(iface->netdev, &custom_stats);
2401
2402 counters_size = custom_stats.size + N_IFACE_STATS;
2403 int64_t *values = xmalloc(counters_size * sizeof(int64_t));
2404 const char **keys = xmalloc(counters_size * sizeof(char *));
2405
018f1525
BP
2406 /* Intentionally ignore return value, since errors will set 'stats' to
2407 * all-1s, and we will deal with that correctly below. */
2408 netdev_get_stats(iface->netdev, &stats);
2409
c2553db9
BP
2410 /* Copy statistics into keys[] and values[]. */
2411 n = 0;
2412#define IFACE_STAT(MEMBER, NAME) \
2413 if (stats.MEMBER != UINT64_MAX) { \
2414 keys[n] = NAME; \
2415 values[n] = stats.MEMBER; \
2416 n++; \
2417 }
98dbe2dd
BP
2418 IFACE_STATS;
2419#undef IFACE_STAT
971f4b39
MW
2420
2421 /* Copy custom statistics into keys[] and values[]. */
2422 if (custom_stats.size && custom_stats.counters) {
2423 for (i = 0 ; i < custom_stats.size ; i++) {
2424 values[n] = custom_stats.counters[i].value;
2425 keys[n] = custom_stats.counters[i].name;
2426 n++;
2427 }
2428 }
2429
2430 ovs_assert(n <= counters_size);
018f1525 2431
c2553db9 2432 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
98dbe2dd 2433#undef IFACE_STATS
971f4b39
MW
2434
2435 free(values);
2436 free(keys);
08129599 2437 netdev_free_custom_stats_counters(&custom_stats);
018f1525
BP
2438}
2439
b5cbbcf6
AZ
2440static void
2441br_refresh_datapath_info(struct bridge *br)
2442{
2443 const char *version;
2444
2445 version = (br->ofproto && br->ofproto->ofproto_class->get_datapath_version
2446 ? br->ofproto->ofproto_class->get_datapath_version(br->ofproto)
2447 : NULL);
2448
2449 ovsrec_bridge_set_datapath_version(br->cfg,
2450 version ? version : "<unknown>");
2451}
2452
21f7563c
JP
2453static void
2454br_refresh_stp_status(struct bridge *br)
2455{
a699f614 2456 struct smap smap = SMAP_INITIALIZER(&smap);
21f7563c
JP
2457 struct ofproto *ofproto = br->ofproto;
2458 struct ofproto_stp_status status;
21f7563c
JP
2459
2460 if (ofproto_get_stp_status(ofproto, &status)) {
2461 return;
2462 }
2463
2464 if (!status.enabled) {
a699f614 2465 ovsrec_bridge_set_status(br->cfg, NULL);
21f7563c
JP
2466 return;
2467 }
2468
a699f614
EJ
2469 smap_add_format(&smap, "stp_bridge_id", STP_ID_FMT,
2470 STP_ID_ARGS(status.bridge_id));
2471 smap_add_format(&smap, "stp_designated_root", STP_ID_FMT,
2472 STP_ID_ARGS(status.designated_root));
2473 smap_add_format(&smap, "stp_root_path_cost", "%d", status.root_path_cost);
21f7563c 2474
a699f614
EJ
2475 ovsrec_bridge_set_status(br->cfg, &smap);
2476 smap_destroy(&smap);
21f7563c
JP
2477}
2478
2479static void
2480port_refresh_stp_status(struct port *port)
2481{
2482 struct ofproto *ofproto = port->bridge->ofproto;
2483 struct iface *iface;
2484 struct ofproto_port_stp_status status;
a699f614 2485 struct smap smap;
21f7563c 2486
06b592bc
EJ
2487 if (port_is_synthetic(port)) {
2488 return;
2489 }
2490
21f7563c 2491 /* STP doesn't currently support bonds. */
417e7e66 2492 if (!ovs_list_is_singleton(&port->ifaces)) {
a699f614 2493 ovsrec_port_set_status(port->cfg, NULL);
21f7563c
JP
2494 return;
2495 }
2496
417e7e66 2497 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
21f7563c
JP
2498 if (ofproto_port_get_stp_status(ofproto, iface->ofp_port, &status)) {
2499 return;
2500 }
2501
2502 if (!status.enabled) {
a699f614 2503 ovsrec_port_set_status(port->cfg, NULL);
21f7563c
JP
2504 return;
2505 }
2506
80740385 2507 /* Set Status column. */
a699f614 2508 smap_init(&smap);
fd13c6b5 2509 smap_add_format(&smap, "stp_port_id", "%d", status.port_id);
a699f614
EJ
2510 smap_add(&smap, "stp_state", stp_state_name(status.state));
2511 smap_add_format(&smap, "stp_sec_in_state", "%u", status.sec_in_state);
2512 smap_add(&smap, "stp_role", stp_role_name(status.role));
2513 ovsrec_port_set_status(port->cfg, &smap);
2514 smap_destroy(&smap);
fd28ce3a
JS
2515}
2516
2517static void
2518port_refresh_stp_stats(struct port *port)
2519{
2520 struct ofproto *ofproto = port->bridge->ofproto;
2521 struct iface *iface;
2522 struct ofproto_port_stp_stats stats;
47058293 2523 const char *keys[3];
fd28ce3a
JS
2524 int64_t int_values[3];
2525
2526 if (port_is_synthetic(port)) {
2527 return;
2528 }
2529
2530 /* STP doesn't currently support bonds. */
417e7e66 2531 if (!ovs_list_is_singleton(&port->ifaces)) {
fd28ce3a
JS
2532 return;
2533 }
2534
417e7e66 2535 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
fd28ce3a
JS
2536 if (ofproto_port_get_stp_stats(ofproto, iface->ofp_port, &stats)) {
2537 return;
2538 }
2539
2540 if (!stats.enabled) {
2541 ovsrec_port_set_statistics(port->cfg, NULL, NULL, 0);
2542 return;
2543 }
80740385
JP
2544
2545 /* Set Statistics column. */
2546 keys[0] = "stp_tx_count";
fd28ce3a 2547 int_values[0] = stats.tx_count;
80740385 2548 keys[1] = "stp_rx_count";
fd28ce3a 2549 int_values[1] = stats.rx_count;
80740385 2550 keys[2] = "stp_error_count";
fd28ce3a 2551 int_values[2] = stats.error_count;
80740385
JP
2552
2553 ovsrec_port_set_statistics(port->cfg, keys, int_values,
2554 ARRAY_SIZE(int_values));
21f7563c
JP
2555}
2556
9efd308e
DV
2557static void
2558br_refresh_rstp_status(struct bridge *br)
2559{
2560 struct smap smap = SMAP_INITIALIZER(&smap);
2561 struct ofproto *ofproto = br->ofproto;
2562 struct ofproto_rstp_status status;
2563
2564 if (ofproto_get_rstp_status(ofproto, &status)) {
2565 return;
2566 }
2567 if (!status.enabled) {
2568 ovsrec_bridge_set_rstp_status(br->cfg, NULL);
2569 return;
2570 }
2571 smap_add_format(&smap, "rstp_bridge_id", RSTP_ID_FMT,
2572 RSTP_ID_ARGS(status.bridge_id));
9c64e6b8 2573 smap_add_format(&smap, "rstp_root_path_cost", "%"PRIu32,
9efd308e
DV
2574 status.root_path_cost);
2575 smap_add_format(&smap, "rstp_root_id", RSTP_ID_FMT,
2576 RSTP_ID_ARGS(status.root_id));
2577 smap_add_format(&smap, "rstp_designated_id", RSTP_ID_FMT,
2578 RSTP_ID_ARGS(status.designated_id));
2579 smap_add_format(&smap, "rstp_designated_port_id", RSTP_PORT_ID_FMT,
2580 status.designated_port_id);
2581 smap_add_format(&smap, "rstp_bridge_port_id", RSTP_PORT_ID_FMT,
2582 status.bridge_port_id);
2583 ovsrec_bridge_set_rstp_status(br->cfg, &smap);
2584 smap_destroy(&smap);
2585}
2586
2587static void
2588port_refresh_rstp_status(struct port *port)
2589{
2590 struct ofproto *ofproto = port->bridge->ofproto;
2591 struct iface *iface;
2592 struct ofproto_port_rstp_status status;
2550f026
BP
2593 const char *keys[4];
2594 int64_t int_values[4];
9efd308e
DV
2595 struct smap smap;
2596
2597 if (port_is_synthetic(port)) {
2598 return;
2599 }
2600
2601 /* RSTP doesn't currently support bonds. */
417e7e66 2602 if (!ovs_list_is_singleton(&port->ifaces)) {
9efd308e
DV
2603 ovsrec_port_set_rstp_status(port->cfg, NULL);
2604 return;
2605 }
2606
417e7e66 2607 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
9efd308e
DV
2608 if (ofproto_port_get_rstp_status(ofproto, iface->ofp_port, &status)) {
2609 return;
2610 }
2611
2612 if (!status.enabled) {
2613 ovsrec_port_set_rstp_status(port->cfg, NULL);
2614 ovsrec_port_set_rstp_statistics(port->cfg, NULL, NULL, 0);
2615 return;
2616 }
2617 /* Set Status column. */
2618 smap_init(&smap);
2619
2620 smap_add_format(&smap, "rstp_port_id", RSTP_PORT_ID_FMT,
2621 status.port_id);
2622 smap_add_format(&smap, "rstp_port_role", "%s",
2623 rstp_port_role_name(status.role));
2624 smap_add_format(&smap, "rstp_port_state", "%s",
2625 rstp_state_name(status.state));
9c64e6b8
JR
2626 smap_add_format(&smap, "rstp_designated_bridge_id", RSTP_ID_FMT,
2627 RSTP_ID_ARGS(status.designated_bridge_id));
2628 smap_add_format(&smap, "rstp_designated_port_id", RSTP_PORT_ID_FMT,
2629 status.designated_port_id);
2630 smap_add_format(&smap, "rstp_designated_path_cost", "%"PRIu32,
2631 status.designated_path_cost);
9efd308e
DV
2632
2633 ovsrec_port_set_rstp_status(port->cfg, &smap);
2634 smap_destroy(&smap);
2635
2636 /* Set Statistics column. */
2637 keys[0] = "rstp_tx_count";
2638 int_values[0] = status.tx_count;
2639 keys[1] = "rstp_rx_count";
2640 int_values[1] = status.rx_count;
2641 keys[2] = "rstp_uptime";
2642 int_values[2] = status.uptime;
2550f026
BP
2643 keys[3] = "rstp_error_count";
2644 int_values[3] = status.error_count;
9efd308e
DV
2645 ovsrec_port_set_rstp_statistics(port->cfg, keys, int_values,
2646 ARRAY_SIZE(int_values));
2647}
2648
3e5aeeb5
AZ
2649static void
2650port_refresh_bond_status(struct port *port, bool force_update)
2651{
74ff3298 2652 struct eth_addr mac;
3e5aeeb5
AZ
2653
2654 /* Return if port is not a bond */
417e7e66 2655 if (ovs_list_is_singleton(&port->ifaces)) {
3e5aeeb5
AZ
2656 return;
2657 }
2658
74ff3298 2659 if (bond_get_changed_active_slave(port->name, &mac, force_update)) {
3e5aeeb5
AZ
2660 struct ds mac_s;
2661
2662 ds_init(&mac_s);
2663 ds_put_format(&mac_s, ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
2664 ovsrec_port_set_bond_active_slave(port->cfg, ds_cstr(&mac_s));
2665 ds_destroy(&mac_s);
2666 }
2667}
2668
3fe80505
BP
2669static bool
2670enable_system_stats(const struct ovsrec_open_vswitch *cfg)
2671{
a699f614 2672 return smap_get_bool(&cfg->other_config, "enable-statistics", false);
3fe80505
BP
2673}
2674
ce887677 2675static void
35a22d8c 2676reconfigure_system_stats(const struct ovsrec_open_vswitch *cfg)
ce887677 2677{
35a22d8c 2678 bool enable = enable_system_stats(cfg);
ce887677 2679
35a22d8c
BP
2680 system_stats_enable(enable);
2681 if (!enable) {
2682 ovsrec_open_vswitch_set_statistics(cfg, NULL);
3fe80505 2683 }
35a22d8c
BP
2684}
2685
2686static void
2687run_system_stats(void)
2688{
2689 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(idl);
2690 struct smap *stats;
2691
2692 stats = system_stats_run();
2693 if (stats && cfg) {
2694 struct ovsdb_idl_txn *txn;
2695 struct ovsdb_datum datum;
2696
2697 txn = ovsdb_idl_txn_create(idl);
2698 ovsdb_datum_from_smap(&datum, stats);
9b03e59d 2699 smap_destroy(stats);
35a22d8c
BP
2700 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
2701 &datum);
2702 ovsdb_idl_txn_commit(txn);
2703 ovsdb_idl_txn_destroy(txn);
ce887677 2704
35a22d8c
BP
2705 free(stats);
2706 }
ce887677
BP
2707}
2708
61c75840 2709static const char *
f4f1ea7e 2710ofp12_controller_role_to_str(enum ofp12_controller_role role)
bffc0589
AE
2711{
2712 switch (role) {
f4f1ea7e 2713 case OFPCR12_ROLE_EQUAL:
bffc0589 2714 return "other";
f4f1ea7e 2715 case OFPCR12_ROLE_MASTER:
bffc0589 2716 return "master";
f4f1ea7e 2717 case OFPCR12_ROLE_SLAVE:
bffc0589 2718 return "slave";
f4f1ea7e 2719 case OFPCR12_ROLE_NOCHANGE:
bffc0589
AE
2720 default:
2721 return "*** INVALID ROLE ***";
2722 }
2723}
2724
2725static void
5d279086 2726refresh_controller_status(void)
bffc0589 2727{
5d279086 2728 struct bridge *br;
5d279086
AE
2729
2730 /* Accumulate status for controllers on all bridges. */
2731 HMAP_FOR_EACH (br, node, &all_bridges) {
85c55772 2732 struct shash info = SHASH_INITIALIZER(&info);
5d279086 2733 ofproto_get_ofproto_controller_info(br->ofproto, &info);
bffc0589 2734
85c55772
AZ
2735 /* Update each controller of the bridge in the database with
2736 * current status. */
2737 struct ovsrec_controller **controllers;
2738 size_t n_controllers = bridge_get_controllers(br, &controllers);
2739 size_t i;
2740 for (i = 0; i < n_controllers; i++) {
2741 struct ovsrec_controller *cfg = controllers[i];
2742 struct ofproto_controller_info *cinfo =
2743 shash_find_data(&info, cfg->target);
eb9b8307 2744
1ea21845
AZ
2745 /* cinfo is NULL when 'cfg->target' is a passive connection. */
2746 if (cinfo) {
2747 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
2748 const char *role = ofp12_controller_role_to_str(cinfo->role);
2749 ovsrec_controller_set_role(cfg, role);
2750 ovsrec_controller_set_status(cfg, &cinfo->pairs);
2751 } else {
2752 ovsrec_controller_set_is_connected(cfg, false);
2753 ovsrec_controller_set_role(cfg, NULL);
2754 ovsrec_controller_set_status(cfg, NULL);
2755 }
eb9b8307 2756 }
bffc0589 2757
85c55772
AZ
2758 ofproto_free_ofproto_controller_info(&info);
2759 }
bffc0589 2760}
b40dcae7 2761\f
9c537baf
AW
2762/* Update interface and mirror statistics if necessary. */
2763static void
2764run_stats_update(void)
2765{
9c537baf
AW
2766 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(idl);
2767 int stats_interval;
2768
2769 if (!cfg) {
2770 return;
2771 }
2772
2773 /* Statistics update interval should always be greater than or equal to
2774 * 5000 ms. */
2775 stats_interval = MAX(smap_get_int(&cfg->other_config,
2776 "stats-update-interval",
2777 5000), 5000);
2778 if (stats_timer_interval != stats_interval) {
2779 stats_timer_interval = stats_interval;
2780 stats_timer = LLONG_MIN;
2781 }
2782
2783 if (time_msec() >= stats_timer) {
2784 enum ovsdb_idl_txn_status status;
2785
2786 /* Rate limit the update. Do not start a new update if the
2787 * previous one is not done. */
2788 if (!stats_txn) {
2789 struct bridge *br;
2790
2791 stats_txn = ovsdb_idl_txn_create(idl);
2792 HMAP_FOR_EACH (br, node, &all_bridges) {
2793 struct port *port;
2794 struct mirror *m;
2795
2796 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2797 struct iface *iface;
2798
2799 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2800 iface_refresh_stats(iface);
2801 }
2802 port_refresh_stp_stats(port);
2803 }
2804 HMAP_FOR_EACH (m, hmap_node, &br->mirrors) {
2805 mirror_refresh_stats(m);
2806 }
2807 }
2808 refresh_controller_status();
2809 }
2810
2811 status = ovsdb_idl_txn_commit(stats_txn);
2812 if (status != TXN_INCOMPLETE) {
2813 stats_timer = time_msec() + stats_timer_interval;
2814 ovsdb_idl_txn_destroy(stats_txn);
2815 stats_txn = NULL;
2816 }
2817 }
2818}
2819
131d04dc
AW
2820static void
2821stats_update_wait(void)
2822{
2823 /* If the 'stats_txn' is non-null (transaction incomplete), waits for the
2824 * transaction to complete. Otherwise, waits for the 'stats_timer'. */
2825 if (stats_txn) {
2826 ovsdb_idl_txn_wait(stats_txn);
2827 } else {
2828 poll_timer_wait_until(stats_timer);
2829 }
2830}
2831
9c537baf
AW
2832/* Update bridge/port/interface status if necessary. */
2833static void
2834run_status_update(void)
2835{
ba8ec115
JS
2836 if (!status_txn) {
2837 uint64_t seq;
9c537baf
AW
2838
2839 /* Rate limit the update. Do not start a new update if the
2840 * previous one is not done. */
ba8ec115
JS
2841 seq = seq_read(connectivity_seq_get());
2842 if (seq != connectivity_seqno || status_txn_try_again) {
3e52fa56
AC
2843 const struct ovsrec_open_vswitch *cfg =
2844 ovsrec_open_vswitch_first(idl);
9c537baf
AW
2845 struct bridge *br;
2846
2847 connectivity_seqno = seq;
2848 status_txn = ovsdb_idl_txn_create(idl);
3e52fa56 2849 dpdk_status(cfg);
9c537baf
AW
2850 HMAP_FOR_EACH (br, node, &all_bridges) {
2851 struct port *port;
2852
2853 br_refresh_stp_status(br);
2854 br_refresh_rstp_status(br);
b5cbbcf6 2855 br_refresh_datapath_info(br);
9c537baf
AW
2856 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2857 struct iface *iface;
2858
2859 port_refresh_stp_status(port);
2860 port_refresh_rstp_status(port);
3e5aeeb5 2861 port_refresh_bond_status(port, status_txn_try_again);
9c537baf
AW
2862 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2863 iface_refresh_netdev_status(iface);
2864 iface_refresh_ofproto_status(iface);
2865 }
2866 }
2867 }
2868 }
ba8ec115
JS
2869 }
2870
2871 /* Commit the transaction and get the status. If the transaction finishes,
2872 * then destroy the transaction. Otherwise, keep it so that we can check
2873 * progress the next time that this function is called. */
2874 if (status_txn) {
2875 enum ovsdb_idl_txn_status status;
9c537baf
AW
2876
2877 status = ovsdb_idl_txn_commit(status_txn);
2878 if (status != TXN_INCOMPLETE) {
2879 ovsdb_idl_txn_destroy(status_txn);
2880 status_txn = NULL;
2881
2882 /* Sets the 'status_txn_try_again' if the transaction fails. */
2883 if (status == TXN_SUCCESS || status == TXN_UNCHANGED) {
2884 status_txn_try_again = false;
2885 } else {
2886 status_txn_try_again = true;
2887 }
2888 }
2889 }
99eef98b
DF
2890
2891 /* Refresh AA port status if necessary. */
2892 if (time_msec() >= aa_refresh_timer) {
2893 struct bridge *br;
2894
2895 HMAP_FOR_EACH (br, node, &all_bridges) {
2896 if (bridge_aa_need_refresh(br)) {
2897 struct ovsdb_idl_txn *txn;
2898
2899 txn = ovsdb_idl_txn_create(idl);
2900 bridge_aa_refresh_queued(br);
2901 ovsdb_idl_txn_commit(txn);
2902 ovsdb_idl_txn_destroy(txn);
2903 }
2904 }
2905
2906 aa_refresh_timer = time_msec() + AA_REFRESH_INTERVAL;
2907 }
9c537baf
AW
2908}
2909
2910static void
2911status_update_wait(void)
2912{
2913 /* If the 'status_txn' is non-null (transaction incomplete), waits for the
2914 * transaction to complete. If the status update to database needs to be
2915 * run again (transaction fails), registers a timeout in
2916 * 'STATUS_CHECK_AGAIN_MSEC'. Otherwise, waits on the global connectivity
2917 * sequence number. */
2918 if (status_txn) {
2919 ovsdb_idl_txn_wait(status_txn);
2920 } else if (status_txn_try_again) {
2921 poll_timer_wait_until(time_msec() + STATUS_CHECK_AGAIN_MSEC);
2922 } else {
2923 seq_wait(connectivity_seq_get(), connectivity_seqno);
2924 }
2925}
2926
aeed7879
BP
2927static void
2928bridge_run__(void)
2929{
2930 struct bridge *br;
2931 struct sset types;
2932 const char *type;
2933
2934 /* Let each datapath type do the work that it needs to do. */
2935 sset_init(&types);
2936 ofproto_enumerate_types(&types);
2937 SSET_FOR_EACH (type, &types) {
2938 ofproto_type_run(type);
2939 }
2940 sset_destroy(&types);
2941
2942 /* Let each bridge do the work that it needs to do. */
2943 HMAP_FOR_EACH (br, node, &all_bridges) {
2944 ofproto_run(br->ofproto);
2945 }
2946}
2947
c5187f17 2948void
064af421
BP
2949bridge_run(void)
2950{
8c0f519f 2951 static struct ovsrec_open_vswitch null_cfg;
d54ff998
BP
2952 const struct ovsrec_open_vswitch *cfg;
2953
8c0f519f 2954 ovsrec_open_vswitch_init(&null_cfg);
edce886c 2955
2a73b1d7 2956 ovsdb_idl_run(idl);
06b6d651 2957
e21c6643
TLSC
2958 if_notifier_run();
2959
2a73b1d7
BP
2960 if (ovsdb_idl_is_lock_contended(idl)) {
2961 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2962 struct bridge *br, *next_br;
06b6d651 2963
2a73b1d7
BP
2964 VLOG_ERR_RL(&rl, "another ovs-vswitchd process is running, "
2965 "disabling this process (pid %ld) until it goes away",
2966 (long int) getpid());
bae7208e 2967
2a73b1d7 2968 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
9aad5a5a 2969 bridge_destroy(br, false);
06b6d651 2970 }
2a73b1d7
BP
2971 /* Since we will not be running system_stats_run() in this process
2972 * with the current situation of multiple ovs-vswitchd daemons,
2973 * disable system stats collection. */
2974 system_stats_enable(false);
2975 return;
b3cceba0
AW
2976 } else if (!ovsdb_idl_has_lock(idl)
2977 || !ovsdb_idl_has_ever_connected(idl)) {
2978 /* Returns if not holding the lock or not done retrieving db
2979 * contents. */
2a73b1d7 2980 return;
06b6d651
BP
2981 }
2982 cfg = ovsrec_open_vswitch_first(idl);
2983
bab69409 2984 if (cfg) {
53611f7b 2985 netdev_set_flow_api_enabled(&cfg->other_config);
bab69409
AC
2986 dpdk_init(&cfg->other_config);
2987 }
2988
b0408fca
JP
2989 /* Initialize the ofproto library. This only needs to run once, but
2990 * it must be done after the configuration is set. If the
2991 * initialization has already occurred, bridge_init_ofproto()
2992 * returns immediately. */
2993 bridge_init_ofproto(cfg);
2994
40358701
GS
2995 /* Once the value of flow-restore-wait is false, we no longer should
2996 * check its value from the database. */
2997 if (cfg && ofproto_get_flow_restore_wait()) {
2998 ofproto_set_flow_restore_wait(smap_get_bool(&cfg->other_config,
2999 "flow-restore-wait", false));
3000 }
3001
aeed7879 3002 bridge_run__();
c5187f17 3003
d6da96ce
BP
3004 /* Re-configure SSL. We do this on every trip through the main loop,
3005 * instead of just when the database changes, because the contents of the
3006 * key and certificate files can change without the database changing.
3007 *
3008 * We do this before bridge_reconfigure() because that function might
3009 * initiate SSL connections and thus requires SSL to be configured. */
3010 if (cfg && cfg->ssl) {
3011 const struct ovsrec_ssl *ssl = cfg->ssl;
3012
3013 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
3014 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
3015 }
bf8f2167 3016
0612d739
TLSC
3017 if (ovsdb_idl_get_seqno(idl) != idl_seqno ||
3018 if_notifier_changed(ifnotifier)) {
2a73b1d7 3019 struct ovsdb_idl_txn *txn;
ff02e9a5 3020
2a73b1d7
BP
3021 idl_seqno = ovsdb_idl_get_seqno(idl);
3022 txn = ovsdb_idl_txn_create(idl);
3023 bridge_reconfigure(cfg ? cfg : &null_cfg);
ff02e9a5 3024
2a73b1d7
BP
3025 if (cfg) {
3026 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
842733c3 3027 discover_types(cfg);
064af421 3028 }
018f1525 3029
2a73b1d7
BP
3030 /* If we are completing our initial configuration for this run
3031 * of ovs-vswitchd, then keep the transaction around to monitor
3032 * it for completion. */
3033 if (initial_config_done) {
15c9fbd8
AW
3034 /* Always sets the 'status_txn_try_again' to check again,
3035 * in case that this transaction fails. */
3036 status_txn_try_again = true;
2a73b1d7
BP
3037 ovsdb_idl_txn_commit(txn);
3038 ovsdb_idl_txn_destroy(txn);
3039 } else {
3040 initial_config_done = true;
3041 daemonize_txn = txn;
3042 }
bae7208e
EJ
3043 }
3044
63ff04e8
BP
3045 if (daemonize_txn) {
3046 enum ovsdb_idl_txn_status status = ovsdb_idl_txn_commit(daemonize_txn);
3047 if (status != TXN_INCOMPLETE) {
3048 ovsdb_idl_txn_destroy(daemonize_txn);
3049 daemonize_txn = NULL;
3050
3051 /* ovs-vswitchd has completed initialization, so allow the
3052 * process that forked us to exit successfully. */
3053 daemonize_complete();
3054
888e0cf4 3055 vlog_enable_async();
a5fb0e29 3056
63ff04e8
BP
3057 VLOG_INFO_ONCE("%s (Open vSwitch) %s", program_name, VERSION);
3058 }
3059 }
3060
9c537baf
AW
3061 run_stats_update();
3062 run_status_update();
35a22d8c 3063 run_system_stats();
064af421
BP
3064}
3065
3066void
3067bridge_wait(void)
3068{
11a574a7
JP
3069 struct sset types;
3070 const char *type;
3071
c5187f17 3072 ovsdb_idl_wait(idl);
63ff04e8
BP
3073 if (daemonize_txn) {
3074 ovsdb_idl_txn_wait(daemonize_txn);
3075 }
c7e7bb21 3076
e21c6643 3077 if_notifier_wait();
e21c6643 3078
11a574a7
JP
3079 sset_init(&types);
3080 ofproto_enumerate_types(&types);
3081 SSET_FOR_EACH (type, &types) {
3082 ofproto_type_wait(type);
3083 }
3084 sset_destroy(&types);
3085
06b6d651
BP
3086 if (!hmap_is_empty(&all_bridges)) {
3087 struct bridge *br;
6586adf5 3088
06b6d651
BP
3089 HMAP_FOR_EACH (br, node, &all_bridges) {
3090 ofproto_wait(br->ofproto);
3091 }
131d04dc 3092 stats_update_wait();
500ce35e 3093 status_update_wait();
6586adf5 3094 }
35a22d8c
BP
3095
3096 system_stats_wait();
064af421 3097}
0d085684
BP
3098
3099/* Adds some memory usage statistics for bridges into 'usage', for use with
3100 * memory_report(). */
3101void
3102bridge_get_memory_usage(struct simap *usage)
3103{
3104 struct bridge *br;
1c030aa5
EJ
3105 struct sset types;
3106 const char *type;
3107
3108 sset_init(&types);
3109 ofproto_enumerate_types(&types);
3110 SSET_FOR_EACH (type, &types) {
3111 ofproto_type_get_memory_usage(type, usage);
3112 }
3113 sset_destroy(&types);
0d085684
BP
3114
3115 HMAP_FOR_EACH (br, node, &all_bridges) {
3116 ofproto_get_memory_usage(br->ofproto, usage);
3117 }
3118}
8c4c1387 3119\f
e8fe3026
EJ
3120/* QoS unixctl user interface functions. */
3121
3122struct qos_unixctl_show_cbdata {
3123 struct ds *ds;
3124 struct iface *iface;
3125};
3126
3127static void
89454bf4
BP
3128qos_unixctl_show_queue(unsigned int queue_id,
3129 const struct smap *details,
3130 struct iface *iface,
3131 struct ds *ds)
e8fe3026 3132{
e8fe3026 3133 struct netdev_queue_stats stats;
79f1cbe9 3134 struct smap_node *node;
e8fe3026
EJ
3135 int error;
3136
3137 ds_put_cstr(ds, "\n");
3138 if (queue_id) {
3139 ds_put_format(ds, "Queue %u:\n", queue_id);
3140 } else {
3141 ds_put_cstr(ds, "Default:\n");
3142 }
3143
79f1cbe9 3144 SMAP_FOR_EACH (node, details) {
5a0e4aec 3145 ds_put_format(ds, " %s: %s\n", node->key, node->value);
e8fe3026
EJ
3146 }
3147
3148 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
3149 if (!error) {
3150 if (stats.tx_packets != UINT64_MAX) {
5a0e4aec 3151 ds_put_format(ds, " tx_packets: %"PRIu64"\n", stats.tx_packets);
e8fe3026
EJ
3152 }
3153
3154 if (stats.tx_bytes != UINT64_MAX) {
5a0e4aec 3155 ds_put_format(ds, " tx_bytes: %"PRIu64"\n", stats.tx_bytes);
e8fe3026
EJ
3156 }
3157
3158 if (stats.tx_errors != UINT64_MAX) {
5a0e4aec 3159 ds_put_format(ds, " tx_errors: %"PRIu64"\n", stats.tx_errors);
e8fe3026
EJ
3160 }
3161 } else {
5a0e4aec 3162 ds_put_format(ds, " Failed to get statistics for queue %u: %s",
10a89ef0 3163 queue_id, ovs_strerror(error));
e8fe3026
EJ
3164 }
3165}
3166
3d657a0a
IS
3167static void
3168qos_unixctl_show_types(struct unixctl_conn *conn, int argc OVS_UNUSED,
3169 const char *argv[], void *aux OVS_UNUSED)
3170{
3171 struct ds ds = DS_EMPTY_INITIALIZER;
3172 struct sset types = SSET_INITIALIZER(&types);
3173 struct iface *iface;
3174 const char * types_name;
3175 int error;
3176
3177 iface = iface_find(argv[1]);
3178 if (!iface) {
3179 unixctl_command_reply_error(conn, "no such interface");
3180 return;
3181 }
3182
3183 error = netdev_get_qos_types(iface->netdev, &types);
3184 if (!error) {
3185 if (!sset_is_empty(&types)) {
3186 SSET_FOR_EACH (types_name, &types) {
3187 ds_put_format(&ds, "QoS type: %s\n", types_name);
3188 }
3189 unixctl_command_reply(conn, ds_cstr(&ds));
3190 } else {
3191 ds_put_format(&ds, "No QoS types supported for interface: %s\n",
3192 iface->name);
3193 unixctl_command_reply(conn, ds_cstr(&ds));
3194 }
3195 } else {
3196 ds_put_format(&ds, "%s: failed to retrieve supported QoS types (%s)",
3197 iface->name, ovs_strerror(error));
3198 unixctl_command_reply_error(conn, ds_cstr(&ds));
3199 }
3200
3201 sset_destroy(&types);
3202 ds_destroy(&ds);
3203}
3204
e8fe3026 3205static void
0e15264f
BP
3206qos_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3207 const char *argv[], void *aux OVS_UNUSED)
e8fe3026
EJ
3208{
3209 struct ds ds = DS_EMPTY_INITIALIZER;
79f1cbe9 3210 struct smap smap = SMAP_INITIALIZER(&smap);
e8fe3026
EJ
3211 struct iface *iface;
3212 const char *type;
79f1cbe9 3213 struct smap_node *node;
c3b76f3e 3214 int error;
e8fe3026 3215
0e15264f 3216 iface = iface_find(argv[1]);
e8fe3026 3217 if (!iface) {
bde9f75d 3218 unixctl_command_reply_error(conn, "no such interface");
e8fe3026
EJ
3219 return;
3220 }
3221
c3b76f3e
IS
3222 error = netdev_get_qos(iface->netdev, &type, &smap);
3223 if (!error) {
3224 if (*type != '\0') {
3225 struct netdev_queue_dump dump;
3226 struct smap details;
3227 unsigned int queue_id;
e8fe3026 3228
c3b76f3e 3229 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
89454bf4 3230
c3b76f3e
IS
3231 SMAP_FOR_EACH (node, &smap) {
3232 ds_put_format(&ds, "%s: %s\n", node->key, node->value);
3233 }
e8fe3026 3234
c3b76f3e
IS
3235 smap_init(&details);
3236 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
3237 qos_unixctl_show_queue(queue_id, &details, iface, &ds);
3238 }
3239 smap_destroy(&details);
e8fe3026 3240
c3b76f3e
IS
3241 unixctl_command_reply(conn, ds_cstr(&ds));
3242 } else {
3243 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
d59831e9 3244 unixctl_command_reply(conn, ds_cstr(&ds));
e8fe3026 3245 }
e8fe3026 3246 } else {
c3b76f3e
IS
3247 ds_put_format(&ds, "%s: failed to retrieve QOS configuration (%s)\n",
3248 iface->name, ovs_strerror(error));
bde9f75d 3249 unixctl_command_reply_error(conn, ds_cstr(&ds));
e8fe3026
EJ
3250 }
3251
79f1cbe9 3252 smap_destroy(&smap);
e8fe3026
EJ
3253 ds_destroy(&ds);
3254}
3255\f
064af421 3256/* Bridge reconfiguration functions. */
66da9bef 3257static void
1a6f1e2a 3258bridge_create(const struct ovsrec_bridge *br_cfg)
064af421
BP
3259{
3260 struct bridge *br;
064af421 3261
cb22974d 3262 ovs_assert(!bridge_lookup(br_cfg->name));
ec6fde61 3263 br = xzalloc(sizeof *br);
064af421 3264
1a6f1e2a 3265 br->name = xstrdup(br_cfg->name);
f79e673f 3266 br->type = xstrdup(ofproto_normalize_type(br_cfg->datapath_type));
1a6f1e2a 3267 br->cfg = br_cfg;
e3f55cb8
BP
3268
3269 /* Derive the default Ethernet address from the bridge's UUID. This should
3270 * be unique and it will be stable between ovs-vswitchd runs. */
74ff3298
JR
3271 memcpy(&br->default_ea, &br_cfg->header_.uuid, ETH_ADDR_LEN);
3272 eth_addr_mark_random(&br->default_ea);
064af421 3273
8052fb14 3274 hmap_init(&br->ports);
d9a8717a 3275 hmap_init(&br->ifaces);
ebea37cc 3276 hmap_init(&br->iface_by_name);
fa066f01 3277 hmap_init(&br->mirrors);
4a1ee6ae 3278
99eef98b 3279 hmap_init(&br->mappings);
764072fd 3280 hmap_insert(&all_bridges, &br->node, hash_string(br->name, 0));
064af421
BP
3281}
3282
3283static void
9aad5a5a 3284bridge_destroy(struct bridge *br, bool del)
064af421
BP
3285{
3286 if (br) {
fa066f01
BP
3287 struct mirror *mirror, *next_mirror;
3288 struct port *port, *next_port;
064af421 3289
fa066f01 3290 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
8052fb14 3291 port_destroy(port);
064af421 3292 }
fa066f01
BP
3293 HMAP_FOR_EACH_SAFE (mirror, next_mirror, hmap_node, &br->mirrors) {
3294 mirror_destroy(mirror);
f76e2dfc 3295 }
bae7208e 3296
764072fd 3297 hmap_remove(&all_bridges, &br->node);
9aad5a5a 3298 ofproto_destroy(br->ofproto, del);
d9a8717a 3299 hmap_destroy(&br->ifaces);
8052fb14 3300 hmap_destroy(&br->ports);
ebea37cc 3301 hmap_destroy(&br->iface_by_name);
fa066f01 3302 hmap_destroy(&br->mirrors);
99eef98b 3303 hmap_destroy(&br->mappings);
064af421 3304 free(br->name);
66da9bef 3305 free(br->type);
064af421
BP
3306 free(br);
3307 }
3308}
3309
3310static struct bridge *
3311bridge_lookup(const char *name)
3312{
3313 struct bridge *br;
3314
764072fd 3315 HMAP_FOR_EACH_WITH_HASH (br, node, hash_string(name, 0), &all_bridges) {
064af421
BP
3316 if (!strcmp(br->name, name)) {
3317 return br;
3318 }
3319 }
3320 return NULL;
3321}
3322
4f2cad2c
JP
3323/* Handle requests for a listing of all flows known by the OpenFlow
3324 * stack, including those normally hidden. */
3325static void
0e15264f
BP
3326bridge_unixctl_dump_flows(struct unixctl_conn *conn, int argc OVS_UNUSED,
3327 const char *argv[], void *aux OVS_UNUSED)
4f2cad2c
JP
3328{
3329 struct bridge *br;
3330 struct ds results;
d295e8e9 3331
0e15264f 3332 br = bridge_lookup(argv[1]);
4f2cad2c 3333 if (!br) {
bde9f75d 3334 unixctl_command_reply_error(conn, "Unknown bridge");
4f2cad2c
JP
3335 return;
3336 }
3337
3338 ds_init(&results);
3339 ofproto_get_all_flows(br->ofproto, &results);
3340
bde9f75d 3341 unixctl_command_reply(conn, ds_cstr(&results));
4f2cad2c
JP
3342 ds_destroy(&results);
3343}
3344
fa05809b
BP
3345/* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
3346 * connections and reconnect. If BRIDGE is not specified, then all bridges
3347 * drop their controller connections and reconnect. */
3348static void
0e15264f
BP
3349bridge_unixctl_reconnect(struct unixctl_conn *conn, int argc,
3350 const char *argv[], void *aux OVS_UNUSED)
fa05809b
BP
3351{
3352 struct bridge *br;
0e15264f
BP
3353 if (argc > 1) {
3354 br = bridge_lookup(argv[1]);
fa05809b 3355 if (!br) {
bde9f75d 3356 unixctl_command_reply_error(conn, "Unknown bridge");
fa05809b
BP
3357 return;
3358 }
3359 ofproto_reconnect_controllers(br->ofproto);
3360 } else {
764072fd 3361 HMAP_FOR_EACH (br, node, &all_bridges) {
fa05809b
BP
3362 ofproto_reconnect_controllers(br->ofproto);
3363 }
3364 }
bde9f75d 3365 unixctl_command_reply(conn, NULL);
fa05809b
BP
3366}
3367
76ce9432 3368static size_t
1a048029 3369bridge_get_controllers(const struct bridge *br,
76ce9432 3370 struct ovsrec_controller ***controllersp)
064af421 3371{
76ce9432
BP
3372 struct ovsrec_controller **controllers;
3373 size_t n_controllers;
064af421 3374
1a048029
JP
3375 controllers = br->cfg->controller;
3376 n_controllers = br->cfg->n_controller;
76343538 3377
76ce9432
BP
3378 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
3379 controllers = NULL;
3380 n_controllers = 0;
064af421 3381 }
76343538 3382
76ce9432
BP
3383 if (controllersp) {
3384 *controllersp = controllers;
3385 }
3386 return n_controllers;
064af421
BP
3387}
3388
bae7208e 3389static void
2a73b1d7 3390bridge_collect_wanted_ports(struct bridge *br,
2a73b1d7 3391 struct shash *wanted_ports)
bae7208e 3392{
6ae39834 3393 size_t i;
064af421 3394
2a73b1d7 3395 shash_init(wanted_ports);
bae7208e 3396
76343538
BP
3397 for (i = 0; i < br->cfg->n_ports; i++) {
3398 const char *name = br->cfg->ports[i]->name;
2a73b1d7 3399 if (!shash_add_once(wanted_ports, name, br->cfg->ports[i])) {
76343538
BP
3400 VLOG_WARN("bridge %s: %s specified twice as bridge port",
3401 br->name, name);
3402 }
3403 }
b5827b24 3404 if (bridge_get_controllers(br, NULL)
2a73b1d7 3405 && !shash_find(wanted_ports, br->name)) {
cfea354b
BP
3406 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
3407 br->name, br->name);
72865317 3408
a699f614
EJ
3409 ovsrec_interface_init(&br->synth_local_iface);
3410 ovsrec_port_init(&br->synth_local_port);
3411
cfea354b
BP
3412 br->synth_local_port.interfaces = &br->synth_local_ifacep;
3413 br->synth_local_port.n_interfaces = 1;
3414 br->synth_local_port.name = br->name;
3415
3416 br->synth_local_iface.name = br->name;
66da9bef 3417 br->synth_local_iface.type = "internal";
cfea354b
BP
3418
3419 br->synth_local_ifacep = &br->synth_local_iface;
3420
2a73b1d7 3421 shash_add(wanted_ports, br->name, &br->synth_local_port);
064af421 3422 }
2a73b1d7
BP
3423}
3424
3425/* Deletes "struct port"s and "struct iface"s under 'br' which aren't
3426 * consistent with 'br->cfg'. Updates 'br->if_cfg_queue' with interfaces which
3427 * 'br' needs to complete its configuration. */
3428static void
3429bridge_del_ports(struct bridge *br, const struct shash *wanted_ports)
3430{
3431 struct shash_node *port_node;
3432 struct port *port, *next;
52a90c29 3433
4a1ee6ae 3434 /* Get rid of deleted ports.
a70e4b2a 3435 * Get rid of deleted interfaces on ports that still exist. */
8052fb14 3436 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
2a73b1d7 3437 port->cfg = shash_find_data(wanted_ports, port->name);
66da9bef 3438 if (!port->cfg) {
4a1ee6ae
BP
3439 port_destroy(port);
3440 } else {
66da9bef 3441 port_del_ifaces(port);
064af421
BP
3442 }
3443 }
4a1ee6ae 3444
2a73b1d7
BP
3445 /* Update iface->cfg and iface->type in interfaces that still exist. */
3446 SHASH_FOR_EACH (port_node, wanted_ports) {
71f21279 3447 const struct ovsrec_port *port_rec = port_node->data;
bae7208e
EJ
3448 size_t i;
3449
71f21279
BP
3450 for (i = 0; i < port_rec->n_interfaces; i++) {
3451 const struct ovsrec_interface *cfg = port_rec->interfaces[i];
bae7208e 3452 struct iface *iface = iface_lookup(br, cfg->name);
046f1f89 3453 const char *type = iface_get_type(cfg, br->cfg);
bae7208e
EJ
3454
3455 if (iface) {
3456 iface->cfg = cfg;
046f1f89 3457 iface->type = type;
0faed346 3458 } else {
2a73b1d7 3459 /* We will add new interfaces later. */
bae7208e 3460 }
ceb4559f 3461 }
064af421 3462 }
064af421
BP
3463}
3464
7d674866
BP
3465/* Initializes 'oc' appropriately as a management service controller for
3466 * 'br'.
3467 *
3468 * The caller must free oc->target when it is no longer needed. */
3469static void
3470bridge_ofproto_controller_for_mgmt(const struct bridge *br,
3471 struct ofproto_controller *oc)
3472{
b43c6fe2 3473 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
7d674866
BP
3474 oc->max_backoff = 0;
3475 oc->probe_interval = 60;
3476 oc->band = OFPROTO_OUT_OF_BAND;
7d674866
BP
3477 oc->rate_limit = 0;
3478 oc->burst_limit = 0;
9886b662 3479 oc->enable_async_msgs = true;
10bf2380 3480 oc->dscp = 0;
7d674866
BP
3481}
3482
3483/* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
3484static void
3485bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
3486 struct ofproto_controller *oc)
3487{
a699f614 3488 int dscp;
f125905c 3489
7d674866
BP
3490 oc->target = c->target;
3491 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
3492 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
3493 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
3494 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
7d674866
BP
3495 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
3496 oc->burst_limit = (c->controller_burst_limit
3497 ? *c->controller_burst_limit : 0);
9886b662
BP
3498 oc->enable_async_msgs = (!c->enable_async_messages
3499 || *c->enable_async_messages);
a699f614
EJ
3500 dscp = smap_get_int(&c->other_config, "dscp", DSCP_DEFAULT);
3501 if (dscp < 0 || dscp > 63) {
3502 dscp = DSCP_DEFAULT;
f125905c 3503 }
a699f614 3504 oc->dscp = dscp;
7d674866
BP
3505}
3506
3507/* Configures the IP stack for 'br''s local interface properly according to the
3508 * configuration in 'c'. */
3509static void
3510bridge_configure_local_iface_netdev(struct bridge *br,
3511 struct ovsrec_controller *c)
3512{
3513 struct netdev *netdev;
3514 struct in_addr mask, gateway;
3515
3516 struct iface *local_iface;
3517 struct in_addr ip;
3518
7d674866 3519 /* If there's no local interface or no IP address, give up. */
892815f5 3520 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
e7695092 3521 if (!local_iface || !c->local_ip || !ip_parse(c->local_ip, &ip.s_addr)) {
7d674866
BP
3522 return;
3523 }
3524
3525 /* Bring up the local interface. */
3526 netdev = local_iface->netdev;
4b609110 3527 netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
7d674866
BP
3528
3529 /* Configure the IP address and netmask. */
3530 if (!c->local_netmask
e7695092 3531 || !ip_parse(c->local_netmask, &mask.s_addr)
7d674866
BP
3532 || !mask.s_addr) {
3533 mask.s_addr = guess_netmask(ip.s_addr);
3534 }
3535 if (!netdev_set_in4(netdev, ip, mask)) {
3536 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
ed36537e 3537 br->name, IP_ARGS(ip.s_addr), IP_ARGS(mask.s_addr));
7d674866
BP
3538 }
3539
3540 /* Configure the default gateway. */
3541 if (c->local_gateway
e7695092 3542 && ip_parse(c->local_gateway, &gateway.s_addr)
7d674866
BP
3543 && gateway.s_addr) {
3544 if (!netdev_add_router(netdev, gateway)) {
3545 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
ed36537e 3546 br->name, IP_ARGS(gateway.s_addr));
7d674866
BP
3547 }
3548 }
3549}
3550
cb4ef1ea
BP
3551/* Returns true if 'a' and 'b' are the same except that any number of slashes
3552 * in either string are treated as equal to any number of slashes in the other,
329e3462
PR
3553 * e.g. "x///y" is equal to "x/y".
3554 *
3555 * Also, if 'b_stoplen' bytes from 'b' are found to be equal to corresponding
3556 * bytes from 'a', the function considers this success. Specify 'b_stoplen' as
3557 * SIZE_MAX to compare all of 'a' to all of 'b' rather than just a prefix of
3558 * 'b' against a prefix of 'a'.
3559 */
cb4ef1ea 3560static bool
329e3462 3561equal_pathnames(const char *a, const char *b, size_t b_stoplen)
cb4ef1ea 3562{
329e3462 3563 const char *b_start = b;
4a1d4e86
BP
3564 for (;;) {
3565 if (b - b_start >= b_stoplen) {
3566 return true;
3567 } else if (*a != *b) {
3568 return false;
3569 } else if (*a == '/') {
cb4ef1ea
BP
3570 a += strspn(a, "/");
3571 b += strspn(b, "/");
3572 } else if (*a == '\0') {
3573 return true;
3574 } else {
3575 a++;
3576 b++;
3577 }
3578 }
cb4ef1ea
BP
3579}
3580
064af421 3581static void
fa066f01
BP
3582bridge_configure_remotes(struct bridge *br,
3583 const struct sockaddr_in *managers, size_t n_managers)
064af421 3584{
a699f614 3585 bool disable_in_band;
b1da6250 3586
76ce9432
BP
3587 struct ovsrec_controller **controllers;
3588 size_t n_controllers;
7d674866 3589
66da9bef 3590 enum ofproto_fail_mode fail_mode;
7d674866
BP
3591
3592 struct ofproto_controller *ocs;
3593 size_t n_ocs;
3594 size_t i;
064af421 3595
8731b2b6 3596 /* Check if we should disable in-band control on this bridge. */
a699f614
EJ
3597 disable_in_band = smap_get_bool(&br->cfg->other_config, "disable-in-band",
3598 false);
8731b2b6 3599
b1da6250 3600 /* Set OpenFlow queue ID for in-band control. */
a699f614
EJ
3601 ofproto_set_in_band_queue(br->ofproto,
3602 smap_get_int(&br->cfg->other_config,
3603 "in-band-queue", -1));
b1da6250 3604
8731b2b6
JP
3605 if (disable_in_band) {
3606 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
3607 } else {
3608 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
3609 }
cd11000b 3610
1a048029 3611 n_controllers = bridge_get_controllers(br, &controllers);
064af421 3612
7d674866
BP
3613 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
3614 n_ocs = 0;
064af421 3615
7d674866
BP
3616 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
3617 for (i = 0; i < n_controllers; i++) {
3618 struct ovsrec_controller *c = controllers[i];
76ce9432 3619
81d2f75c
AA
3620 if (daemon_should_self_confine()
3621 && (!strncmp(c->target, "punix:", 6)
3622 || !strncmp(c->target, "unix:", 5))) {
7d674866 3623 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
cb4ef1ea
BP
3624 char *whitelist;
3625
329e3462
PR
3626 if (!strncmp(c->target, "unix:", 5)) {
3627 /* Connect to a listening socket */
3628 whitelist = xasprintf("unix:%s/", ovs_rundir());
2c487bc8
PR
3629 if (strchr(c->target, '/') &&
3630 !equal_pathnames(c->target, whitelist,
3631 strlen(whitelist))) {
3632 /* Absolute path specified, but not in ovs_rundir */
329e3462
PR
3633 VLOG_ERR_RL(&rl, "bridge %s: Not connecting to socket "
3634 "controller \"%s\" due to possibility for "
3635 "remote exploit. Instead, specify socket "
3636 "in whitelisted \"%s\" or connect to "
3637 "\"unix:%s/%s.mgmt\" (which is always "
3638 "available without special configuration).",
3639 br->name, c->target, whitelist,
cb4ef1ea 3640 ovs_rundir(), br->name);
329e3462
PR
3641 free(whitelist);
3642 continue;
3643 }
3644 } else {
14dd55a3 3645 whitelist = xasprintf("punix:%s/%s.",
329e3462 3646 ovs_rundir(), br->name);
14dd55a3
AW
3647 if (!equal_pathnames(c->target, whitelist, strlen(whitelist))
3648 || strchr(c->target + strlen(whitelist), '/')) {
329e3462
PR
3649 /* Prevent remote ovsdb-server users from accessing
3650 * arbitrary Unix domain sockets and overwriting arbitrary
3651 * local files. */
3652 VLOG_ERR_RL(&rl, "bridge %s: Not adding Unix domain socket "
3653 "controller \"%s\" due to possibility of "
3654 "overwriting local files. Instead, specify "
14dd55a3
AW
3655 "path in whitelisted format \"%s*\" or "
3656 "connect to \"unix:%s/%s.mgmt\" (which is "
3657 "always available without special "
3658 "configuration).",
329e3462
PR
3659 br->name, c->target, whitelist,
3660 ovs_rundir(), br->name);
3661 free(whitelist);
3662 continue;
3663 }
cb4ef1ea 3664 }
4d6fb5eb 3665
cb4ef1ea 3666 free(whitelist);
4d6fb5eb
EJ
3667 }
3668
7d674866 3669 bridge_configure_local_iface_netdev(br, c);
8731b2b6
JP
3670 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
3671 if (disable_in_band) {
3672 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
f620b43a 3673 }
8731b2b6 3674 n_ocs++;
be02e7c3 3675 }
be02e7c3 3676
7beaa082
SH
3677 ofproto_set_controllers(br->ofproto, ocs, n_ocs,
3678 bridge_get_allowed_versions(br));
7d674866
BP
3679 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
3680 free(ocs);
66da9bef
BP
3681
3682 /* Set the fail-mode. */
3683 fail_mode = !br->cfg->fail_mode
3684 || !strcmp(br->cfg->fail_mode, "standalone")
3685 ? OFPROTO_FAIL_STANDALONE
3686 : OFPROTO_FAIL_SECURE;
3687 ofproto_set_fail_mode(br->ofproto, fail_mode);
3688
3689 /* Configure OpenFlow controller connection snooping. */
3690 if (!ofproto_has_snoops(br->ofproto)) {
3691 struct sset snoops;
5827ce14 3692
66da9bef
BP
3693 sset_init(&snoops);
3694 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
3695 ovs_rundir(), br->name));
3696 ofproto_set_snoops(br->ofproto, &snoops);
3697 sset_destroy(&snoops);
064af421
BP
3698 }
3699}
254750ce
BP
3700
3701static void
3702bridge_configure_tables(struct bridge *br)
3703{
3704 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3705 int n_tables;
71f21279 3706 int i, j;
254750ce
BP
3707
3708 n_tables = ofproto_get_n_tables(br->ofproto);
3709 j = 0;
3710 for (i = 0; i < n_tables; i++) {
3711 struct ofproto_table_settings s;
480cda3a 3712 bool use_default_prefixes = true;
254750ce
BP
3713
3714 s.name = NULL;
3715 s.max_flows = UINT_MAX;
3716 s.groups = NULL;
82c22d34 3717 s.enable_eviction = false;
254750ce 3718 s.n_groups = 0;
13751fd8
JR
3719 s.n_prefix_fields = 0;
3720 memset(s.prefix_fields, ~0, sizeof(s.prefix_fields));
254750ce
BP
3721
3722 if (j < br->cfg->n_flow_tables && i == br->cfg->key_flow_tables[j]) {
3723 struct ovsrec_flow_table *cfg = br->cfg->value_flow_tables[j++];
3724
3725 s.name = cfg->name;
3726 if (cfg->n_flow_limit && *cfg->flow_limit < UINT_MAX) {
3727 s.max_flows = *cfg->flow_limit;
3728 }
254750ce 3729
82c22d34
BP
3730 s.enable_eviction = (cfg->overflow_policy
3731 && !strcmp(cfg->overflow_policy, "evict"));
3732 if (cfg->n_groups) {
254750ce 3733 s.groups = xmalloc(cfg->n_groups * sizeof *s.groups);
71f21279 3734 for (int k = 0; k < cfg->n_groups; k++) {
254750ce
BP
3735 const char *string = cfg->groups[k];
3736 char *msg;
3737
3738 msg = mf_parse_subfield__(&s.groups[k], &string);
3739 if (msg) {
3740 VLOG_WARN_RL(&rl, "bridge %s table %d: error parsing "
3741 "'groups' (%s)", br->name, i, msg);
3742 free(msg);
3743 } else if (*string) {
3744 VLOG_WARN_RL(&rl, "bridge %s table %d: 'groups' "
3745 "element '%s' contains trailing garbage",
3746 br->name, i, cfg->groups[k]);
3747 } else {
3748 s.n_groups++;
3749 }
3750 }
3751 }
82c22d34 3752
13751fd8
JR
3753 /* Prefix lookup fields. */
3754 s.n_prefix_fields = 0;
71f21279 3755 for (int k = 0; k < cfg->n_prefixes; k++) {
13751fd8 3756 const char *name = cfg->prefixes[k];
f017d986
JR
3757 const struct mf_field *mf;
3758
3759 if (strcmp(name, "none") == 0) {
480cda3a
JR
3760 use_default_prefixes = false;
3761 s.n_prefix_fields = 0;
3762 break;
f017d986
JR
3763 }
3764 mf = mf_from_name(name);
13751fd8
JR
3765 if (!mf) {
3766 VLOG_WARN("bridge %s: 'prefixes' with unknown field: %s",
3767 br->name, name);
3768 continue;
3769 }
3770 if (mf->flow_be32ofs < 0 || mf->n_bits % 32) {
3771 VLOG_WARN("bridge %s: 'prefixes' with incompatible field: "
3772 "%s", br->name, name);
3773 continue;
3774 }
3775 if (s.n_prefix_fields >= ARRAY_SIZE(s.prefix_fields)) {
3776 VLOG_WARN("bridge %s: 'prefixes' with too many fields, "
3777 "field not used: %s", br->name, name);
3778 continue;
3779 }
480cda3a 3780 use_default_prefixes = false;
13751fd8
JR
3781 s.prefix_fields[s.n_prefix_fields++] = mf->id;
3782 }
480cda3a
JR
3783 }
3784 if (use_default_prefixes) {
3785 /* Use default values. */
3786 s.n_prefix_fields = ARRAY_SIZE(default_prefix_fields);
3787 memcpy(s.prefix_fields, default_prefix_fields,
3788 sizeof default_prefix_fields);
3789 } else {
480cda3a 3790 struct ds ds = DS_EMPTY_INITIALIZER;
71f21279 3791 for (int k = 0; k < s.n_prefix_fields; k++) {
480cda3a
JR
3792 if (k) {
3793 ds_put_char(&ds, ',');
13751fd8 3794 }
480cda3a
JR
3795 ds_put_cstr(&ds, mf_from_id(s.prefix_fields[k])->name);
3796 }
3797 if (s.n_prefix_fields == 0) {
3798 ds_put_cstr(&ds, "none");
13751fd8 3799 }
480cda3a
JR
3800 VLOG_INFO("bridge %s table %d: Prefix lookup with: %s.",
3801 br->name, i, ds_cstr(&ds));
3802 ds_destroy(&ds);
254750ce
BP
3803 }
3804
3805 ofproto_configure_table(br->ofproto, i, &s);
3806
3807 free(s.groups);
3808 }
3809 for (; j < br->cfg->n_flow_tables; j++) {
3810 VLOG_WARN_RL(&rl, "bridge %s: ignoring configuration for flow table "
3811 "%"PRId64" not supported by this datapath", br->name,
3812 br->cfg->key_flow_tables[j]);
3813 }
3814}
8b6ff729
BP
3815
3816static void
3817bridge_configure_dp_desc(struct bridge *br)
3818{
3819 ofproto_set_dp_desc(br->ofproto,
3820 smap_get(&br->cfg->other_config, "dp-desc"));
3821}
99eef98b
DF
3822
3823static struct aa_mapping *
3824bridge_aa_mapping_find(struct bridge *br, const int64_t isid)
3825{
3826 struct aa_mapping *m;
3827
3828 HMAP_FOR_EACH_IN_BUCKET (m,
3829 hmap_node,
3830 hash_bytes(&isid, sizeof isid, 0),
3831 &br->mappings) {
3832 if (isid == m->isid) {
3833 return m;
3834 }
3835 }
3836 return NULL;
3837}
3838
3839static struct aa_mapping *
3840bridge_aa_mapping_create(struct bridge *br,
3841 const int64_t isid,
3842 const int64_t vlan)
3843{
3844 struct aa_mapping *m;
3845
3846 m = xzalloc(sizeof *m);
3847 m->bridge = br;
3848 m->isid = isid;
3849 m->vlan = vlan;
3850 m->br_name = xstrdup(br->name);
3851 hmap_insert(&br->mappings,
3852 &m->hmap_node,
3853 hash_bytes(&isid, sizeof isid, 0));
3854
3855 return m;
3856}
3857
3858static void
3859bridge_aa_mapping_destroy(struct aa_mapping *m)
3860{
3861 if (m) {
3862 struct bridge *br = m->bridge;
3863
3864 if (br->ofproto) {
3865 ofproto_aa_mapping_unregister(br->ofproto, m);
3866 }
3867
3868 hmap_remove(&br->mappings, &m->hmap_node);
3869 if (m->br_name) {
3870 free(m->br_name);
3871 }
3872 free(m);
3873 }
3874}
3875
3876static bool
3877bridge_aa_mapping_configure(struct aa_mapping *m)
3878{
3879 struct aa_mapping_settings s;
3880
3881 s.isid = m->isid;
3882 s.vlan = m->vlan;
3883
3884 /* Configure. */
3885 ofproto_aa_mapping_register(m->bridge->ofproto, m, &s);
3886
3887 return true;
3888}
3889
3890static void
3891bridge_configure_aa(struct bridge *br)
3892{
3893 const struct ovsdb_datum *mc;
3894 struct ovsrec_autoattach *auto_attach = br->cfg->auto_attach;
3895 struct aa_settings aa_s;
3896 struct aa_mapping *m, *next;
3897 size_t i;
3898
3899 if (!auto_attach) {
3900 ofproto_set_aa(br->ofproto, NULL, NULL);
3901 return;
3902 }
3903
3904 memset(&aa_s, 0, sizeof aa_s);
3905 aa_s.system_description = auto_attach->system_description;
3906 aa_s.system_name = auto_attach->system_name;
3907 ofproto_set_aa(br->ofproto, NULL, &aa_s);
3908
3909 mc = ovsrec_autoattach_get_mappings(auto_attach,
3910 OVSDB_TYPE_INTEGER,
3911 OVSDB_TYPE_INTEGER);
3912 HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mappings) {
3913 union ovsdb_atom atom;
3914
3915 atom.integer = m->isid;
e9804ffd 3916 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_INTEGER) == UINT_MAX) {
72c642e5
BP
3917 VLOG_INFO("Deleting isid=%"PRIu32", vlan=%"PRIu16,
3918 m->isid, m->vlan);
99eef98b
DF
3919 bridge_aa_mapping_destroy(m);
3920 }
3921 }
3922
3923 /* Add new mappings and reconfigure existing ones. */
3924 for (i = 0; i < auto_attach->n_mappings; ++i) {
71f21279 3925 m = bridge_aa_mapping_find(br, auto_attach->key_mappings[i]);
99eef98b
DF
3926
3927 if (!m) {
3928 VLOG_INFO("Adding isid=%"PRId64", vlan=%"PRId64,
3929 auto_attach->key_mappings[i],
3930 auto_attach->value_mappings[i]);
3931 m = bridge_aa_mapping_create(br,
3932 auto_attach->key_mappings[i],
3933 auto_attach->value_mappings[i]);
3934
3935 if (!bridge_aa_mapping_configure(m)) {
3936 bridge_aa_mapping_destroy(m);
3937 }
3938 }
3939 }
3940}
3941
3942static bool
3943bridge_aa_need_refresh(struct bridge *br)
3944{
3945 return ofproto_aa_vlan_get_queue_size(br->ofproto) > 0;
3946}
3947
3948static void
3949bridge_aa_update_trunks(struct port *port, struct bridge_aa_vlan *m)
3950{
3951 int64_t *trunks = NULL;
3952 unsigned int i = 0;
3953 bool found = false, reconfigure = false;
3954
3955 for (i = 0; i < port->cfg->n_trunks; i++) {
3956 if (port->cfg->trunks[i] == m->vlan) {
3957 found = true;
3958 break;
3959 }
3960 }
3961
3962 switch (m->oper) {
3963 case BRIDGE_AA_VLAN_OPER_ADD:
3964 if (!found) {
3965 trunks = xmalloc(sizeof *trunks * (port->cfg->n_trunks + 1));
3966
3967 for (i = 0; i < port->cfg->n_trunks; i++) {
3968 trunks[i] = port->cfg->trunks[i];
3969 }
3970 trunks[i++] = m->vlan;
3971 reconfigure = true;
3972 }
3973
3974 break;
3975
3976 case BRIDGE_AA_VLAN_OPER_REMOVE:
3977 if (found) {
3978 unsigned int j = 0;
3979
3980 trunks = xmalloc(sizeof *trunks * (port->cfg->n_trunks - 1));
3981
3982 for (i = 0; i < port->cfg->n_trunks; i++) {
3983 if (port->cfg->trunks[i] != m->vlan) {
3984 trunks[j++] = port->cfg->trunks[i];
3985 }
3986 }
3987 i = j;
3988 reconfigure = true;
3989 }
3990
3991 break;
3992
3993 case BRIDGE_AA_VLAN_OPER_UNDEF:
3994 default:
3995 VLOG_WARN("unrecognized operation %u", m->oper);
3996 break;
3997 }
3998
3999 if (reconfigure) {
4000 /* VLAN switching under trunk mode cause the trunk port to switch all
4001 * VLANs, see ovs-vswitchd.conf.db
4002 */
4003 if (i == 0) {
4004 static char *vlan_mode_access = "access";
4005 ovsrec_port_set_vlan_mode(port->cfg, vlan_mode_access);
4006 }
4007
4008 if (i == 1) {
4009 static char *vlan_mode_trunk = "trunk";
4010 ovsrec_port_set_vlan_mode(port->cfg, vlan_mode_trunk);
4011 }
4012
4013 ovsrec_port_set_trunks(port->cfg, trunks, i);
4014
4015 /* Force reconfigure of the port. */
4016 port_configure(port);
4017 }
08125baf
BP
4018
4019 free(trunks);
99eef98b
DF
4020}
4021
4022static void
4023bridge_aa_refresh_queued(struct bridge *br)
4024{
4025 struct ovs_list *list = xmalloc(sizeof *list);
494356bf 4026 struct bridge_aa_vlan *node, *next;
99eef98b 4027
417e7e66 4028 ovs_list_init(list);
99eef98b
DF
4029 ofproto_aa_vlan_get_queued(br->ofproto, list);
4030
494356bf 4031 LIST_FOR_EACH_SAFE (node, next, list_node, list) {
99eef98b
DF
4032 struct port *port;
4033
4034 VLOG_INFO("ifname=%s, vlan=%u, oper=%u", node->port_name, node->vlan,
4035 node->oper);
4036
4037 port = port_lookup(br, node->port_name);
4038 if (port) {
4039 bridge_aa_update_trunks(port, node);
4040 }
4041
417e7e66 4042 ovs_list_remove(&node->list_node);
99eef98b
DF
4043 free(node->port_name);
4044 free(node);
4045 }
4046
4047 free(list);
4048}
4049
064af421 4050\f
fa066f01 4051/* Port functions. */
064af421 4052
f620b43a 4053static struct port *
fa066f01 4054port_create(struct bridge *br, const struct ovsrec_port *cfg)
064af421 4055{
f620b43a
BP
4056 struct port *port;
4057
4058 port = xzalloc(sizeof *port);
4059 port->bridge = br;
fa066f01
BP
4060 port->name = xstrdup(cfg->name);
4061 port->cfg = cfg;
417e7e66 4062 ovs_list_init(&port->ifaces);
f620b43a
BP
4063
4064 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
f620b43a 4065 return port;
064af421
BP
4066}
4067
fa066f01 4068/* Deletes interfaces from 'port' that are no longer configured for it. */
064af421 4069static void
fa066f01 4070port_del_ifaces(struct port *port)
064af421 4071{
f620b43a
BP
4072 struct iface *iface, *next;
4073 struct sset new_ifaces;
4074 size_t i;
064af421 4075
f620b43a
BP
4076 /* Collect list of new interfaces. */
4077 sset_init(&new_ifaces);
fa066f01 4078 for (i = 0; i < port->cfg->n_interfaces; i++) {
ee60eefe 4079 sset_add(&new_ifaces, port->cfg->interfaces[i]->name);
f620b43a 4080 }
064af421 4081
f620b43a
BP
4082 /* Get rid of deleted interfaces. */
4083 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
4084 if (!sset_contains(&new_ifaces, iface->name)) {
4085 iface_destroy(iface);
064af421 4086 }
064af421 4087 }
f620b43a
BP
4088
4089 sset_destroy(&new_ifaces);
064af421
BP
4090}
4091
064af421
BP
4092static void
4093port_destroy(struct port *port)
4094{
4095 if (port) {
4096 struct bridge *br = port->bridge;
83db7968 4097 struct iface *iface, *next;
064af421 4098
fa066f01
BP
4099 if (br->ofproto) {
4100 ofproto_bundle_unregister(br->ofproto, port);
064af421
BP
4101 }
4102
83db7968 4103 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2a73b1d7 4104 iface_destroy__(iface);
064af421
BP
4105 }
4106
8052fb14 4107 hmap_remove(&br->ports, &port->hmap_node);
064af421
BP
4108 free(port->name);
4109 free(port);
064af421
BP
4110 }
4111}
4112
064af421
BP
4113static struct port *
4114port_lookup(const struct bridge *br, const char *name)
4115{
8052fb14
BP
4116 struct port *port;
4117
4118 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
4119 &br->ports) {
4120 if (!strcmp(port->name, name)) {
4121 return port;
4122 }
4123 }
4124 return NULL;
064af421
BP
4125}
4126
7a673515
BP
4127static bool
4128enable_lacp(struct port *port, bool *activep)
4129{
4130 if (!port->cfg->lacp) {
4131 /* XXX when LACP implementation has been sufficiently tested, enable by
4132 * default and make active on bonded ports. */
4133 return false;
4134 } else if (!strcmp(port->cfg->lacp, "off")) {
4135 return false;
4136 } else if (!strcmp(port->cfg->lacp, "active")) {
4137 *activep = true;
4138 return true;
4139 } else if (!strcmp(port->cfg->lacp, "passive")) {
4140 *activep = false;
4141 return true;
4142 } else {
4143 VLOG_WARN("port %s: unknown LACP mode %s",
4144 port->name, port->cfg->lacp);
4145 return false;
4146 }
4147}
4148
fa066f01
BP
4149static struct lacp_settings *
4150port_configure_lacp(struct port *port, struct lacp_settings *s)
5827ce14 4151{
e567943d 4152 const char *lacp_time, *system_id;
b5a25389 4153 int priority;
5827ce14 4154
fa066f01
BP
4155 if (!enable_lacp(port, &s->active)) {
4156 return NULL;
abd4a95d
EJ
4157 }
4158
fa066f01 4159 s->name = port->name;
e567943d 4160
a699f614 4161 system_id = smap_get(&port->cfg->other_config, "lacp-system-id");
a9bf011b 4162 if (system_id) {
c2c28dfd
BP
4163 if (!ovs_scan(system_id, ETH_ADDR_SCAN_FMT,
4164 ETH_ADDR_SCAN_ARGS(s->id))) {
a9bf011b
EJ
4165 VLOG_WARN("port %s: LACP system ID (%s) must be an Ethernet"
4166 " address.", port->name, system_id);
4167 return NULL;
4168 }
4169 } else {
74ff3298 4170 s->id = port->bridge->ea;
e567943d 4171 }
b5a25389 4172
69fdadb1
EJ
4173 if (eth_addr_is_zero(s->id)) {
4174 VLOG_WARN("port %s: Invalid zero LACP system ID.", port->name);
4175 return NULL;
4176 }
4177
b5a25389 4178 /* Prefer bondable links if unspecified. */
a699f614
EJ
4179 priority = smap_get_int(&port->cfg->other_config, "lacp-system-priority",
4180 0);
fa066f01
BP
4181 s->priority = (priority > 0 && priority <= UINT16_MAX
4182 ? priority
417e7e66 4183 : UINT16_MAX - !ovs_list_is_short(&port->ifaces));
b5a25389 4184
13c1637f
BP
4185 lacp_time = smap_get_def(&port->cfg->other_config, "lacp-time", "");
4186 s->fast = !strcasecmp(lacp_time, "fast");
9dd165e0
RK
4187
4188 s->fallback_ab_cfg = smap_get_bool(&port->cfg->other_config,
4189 "lacp-fallback-ab", false);
4190
fa066f01
BP
4191 return s;
4192}
5827ce14 4193
fa066f01
BP
4194static void
4195iface_configure_lacp(struct iface *iface, struct lacp_slave_settings *s)
4196{
00794817 4197 int priority, portid, key;
5827ce14 4198
a699f614
EJ
4199 portid = smap_get_int(&iface->cfg->other_config, "lacp-port-id", 0);
4200 priority = smap_get_int(&iface->cfg->other_config, "lacp-port-priority",
4201 0);
4202 key = smap_get_int(&iface->cfg->other_config, "lacp-aggregation-key", 0);
fa066f01
BP
4203
4204 if (portid <= 0 || portid > UINT16_MAX) {
4e022ec0 4205 portid = ofp_to_u16(iface->ofp_port);
5827ce14
EJ
4206 }
4207
fa066f01
BP
4208 if (priority <= 0 || priority > UINT16_MAX) {
4209 priority = UINT16_MAX;
4210 }
5827ce14 4211
00794817
BP
4212 if (key < 0 || key > UINT16_MAX) {
4213 key = 0;
5827ce14 4214 }
00794817 4215
fa066f01 4216 s->name = iface->name;
fa066f01
BP
4217 s->id = portid;
4218 s->priority = priority;
00794817 4219 s->key = key;
bb5bc6c0
BP
4220}
4221
c25c91fd 4222static void
df53d41c 4223port_configure_bond(struct port *port, struct bond_settings *s)
c25c91fd 4224{
f620b43a 4225 const char *detect_s;
7a673515 4226 struct iface *iface;
3e5aeeb5 4227 const char *mac_s;
1670c579 4228 int miimon_interval;
c25c91fd 4229
fa066f01 4230 s->name = port->name;
4df08875 4231 s->balance = BM_AB;
4c57c3bc
EJ
4232 if (port->cfg->bond_mode) {
4233 if (!bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
4234 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
4235 port->name, port->cfg->bond_mode,
4236 bond_mode_to_string(s->balance));
4237 }
4238 } else {
4239 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4240
4df08875
EJ
4241 /* XXX: Post version 1.5.*, the default bond_mode changed from SLB to
4242 * active-backup. At some point we should remove this warning. */
4c57c3bc 4243 VLOG_WARN_RL(&rl, "port %s: Using the default bond_mode %s. Note that"
4df08875
EJ
4244 " in previous versions, the default bond_mode was"
4245 " balance-slb", port->name,
4c57c3bc 4246 bond_mode_to_string(s->balance));
f620b43a 4247 }
6c2d2a9f
BP
4248 if (s->balance == BM_SLB && port->bridge->cfg->n_flood_vlans) {
4249 VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
4250 "please use another bond type or disable flood_vlans",
4251 port->name);
4252 }
bb5bc6c0 4253
a699f614
EJ
4254 miimon_interval = smap_get_int(&port->cfg->other_config,
4255 "bond-miimon-interval", 0);
1670c579
EJ
4256 if (miimon_interval <= 0) {
4257 miimon_interval = 200;
f620b43a
BP
4258 }
4259
a699f614
EJ
4260 detect_s = smap_get(&port->cfg->other_config, "bond-detect-mode");
4261 if (!detect_s || !strcmp(detect_s, "carrier")) {
1670c579
EJ
4262 miimon_interval = 0;
4263 } else if (strcmp(detect_s, "miimon")) {
4264 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
4265 "defaulting to carrier", port->name, detect_s);
4266 miimon_interval = 0;
f620b43a
BP
4267 }
4268
fa066f01
BP
4269 s->up_delay = MAX(0, port->cfg->bond_updelay);
4270 s->down_delay = MAX(0, port->cfg->bond_downdelay);
a699f614
EJ
4271 s->basis = smap_get_int(&port->cfg->other_config, "bond-hash-basis", 0);
4272 s->rebalance_interval = smap_get_int(&port->cfg->other_config,
4273 "bond-rebalance-interval", 10000);
bc1b010c 4274 if (s->rebalance_interval && s->rebalance_interval < 1000) {
fa066f01 4275 s->rebalance_interval = 1000;
f620b43a
BP
4276 }
4277
9dd165e0
RK
4278 s->lacp_fallback_ab_cfg = smap_get_bool(&port->cfg->other_config,
4279 "lacp-fallback-ab", false);
4280
7a673515 4281 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1670c579 4282 netdev_set_miimon_interval(iface->netdev, miimon_interval);
064af421 4283 }
3e5aeeb5
AZ
4284
4285 mac_s = port->cfg->bond_active_slave;
4286 if (!mac_s || !ovs_scan(mac_s, ETH_ADDR_SCAN_FMT,
4287 ETH_ADDR_SCAN_ARGS(s->active_slave_mac))) {
4288 /* OVSDB did not store the last active interface */
74ff3298 4289 s->active_slave_mac = eth_addr_zero;
3e5aeeb5 4290 }
064af421 4291}
06b592bc
EJ
4292
4293/* Returns true if 'port' is synthetic, that is, if we constructed it locally
4294 * instead of obtaining it from the database. */
4295static bool
4296port_is_synthetic(const struct port *port)
4297{
4298 return ovsdb_idl_row_is_synthetic(&port->cfg->header_);
4299}
064af421
BP
4300\f
4301/* Interface functions. */
4302
05ba03e0
JP
4303static bool
4304iface_is_internal(const struct ovsrec_interface *iface,
4305 const struct ovsrec_bridge *br)
4306{
4307 /* The local port and "internal" ports are always "internal". */
4308 return !strcmp(iface->type, "internal") || !strcmp(iface->name, br->name);
4309}
4310
b54441b3
BP
4311/* Returns the correct network device type for interface 'iface' in bridge
4312 * 'br'. */
4313static const char *
4314iface_get_type(const struct ovsrec_interface *iface,
4315 const struct ovsrec_bridge *br)
4316{
b4f4b737
JP
4317 const char *type;
4318
4319 /* The local port always has type "internal". Other ports take
4320 * their type from the database and default to "system" if none is
4321 * specified. */
05ba03e0 4322 if (iface_is_internal(iface, br)) {
b4f4b737 4323 type = "internal";
acf60855 4324 } else {
b4f4b737 4325 type = iface->type[0] ? iface->type : "system";
acf60855 4326 }
b4f4b737 4327
a5bdd3b2 4328 return type;
064af421
BP
4329}
4330
4331static void
2a73b1d7 4332iface_destroy__(struct iface *iface)
064af421
BP
4333{
4334 if (iface) {
4335 struct port *port = iface->port;
4336 struct bridge *br = port->bridge;
c17f0d5e 4337
f3eba0d2
AZ
4338 VLOG_INFO("bridge %s: deleted interface %s on port %d",
4339 br->name, iface->name, iface->ofp_port);
4340
c60c33fb 4341 if (br->ofproto && iface->ofp_port != OFPP_NONE) {
892815f5 4342 ofproto_port_unregister(br->ofproto, iface->ofp_port);
5827ce14
EJ
4343 }
4344
c60c33fb 4345 if (iface->ofp_port != OFPP_NONE) {
892815f5 4346 hmap_remove(&br->ifaces, &iface->ofp_port_node);
064af421
BP
4347 }
4348
417e7e66 4349 ovs_list_remove(&iface->port_elem);
ebea37cc 4350 hmap_remove(&br->iface_by_name, &iface->name_node);
064af421 4351
208ff139
TL
4352 tnl_port_map_delete_ipdev(netdev_get_name(iface->netdev));
4353
fe83f81d
RW
4354 /* The user is changing configuration here, so netdev_remove needs to be
4355 * used as opposed to netdev_close */
4356 netdev_remove(iface->netdev);
064af421 4357
a740f0de
JG
4358 free(iface->name);
4359 free(iface);
064af421
BP
4360 }
4361}
4362
2a73b1d7
BP
4363static void
4364iface_destroy(struct iface *iface)
4365{
4366 if (iface) {
4367 struct port *port = iface->port;
4368
4369 iface_destroy__(iface);
417e7e66 4370 if (ovs_list_is_empty(&port->ifaces)) {
2a73b1d7
BP
4371 port_destroy(port);
4372 }
4373 }
4374}
4375
064af421
BP
4376static struct iface *
4377iface_lookup(const struct bridge *br, const char *name)
4378{
ebea37cc
BP
4379 struct iface *iface;
4380
4381 HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
4382 &br->iface_by_name) {
4383 if (!strcmp(iface->name, name)) {
4384 return iface;
4385 }
4386 }
4387
4388 return NULL;
064af421
BP
4389}
4390
e8fe3026
EJ
4391static struct iface *
4392iface_find(const char *name)
4393{
4394 const struct bridge *br;
4395
764072fd 4396 HMAP_FOR_EACH (br, node, &all_bridges) {
e8fe3026
EJ
4397 struct iface *iface = iface_lookup(br, name);
4398
4399 if (iface) {
4400 return iface;
4401 }
4402 }
4403 return NULL;
4404}
4405
064af421 4406static struct iface *
4e022ec0 4407iface_from_ofp_port(const struct bridge *br, ofp_port_t ofp_port)
064af421 4408{
d9a8717a
BP
4409 struct iface *iface;
4410
f9c0c3ec 4411 HMAP_FOR_EACH_IN_BUCKET (iface, ofp_port_node, hash_ofp_port(ofp_port),
4e022ec0 4412 &br->ifaces) {
892815f5 4413 if (iface->ofp_port == ofp_port) {
d9a8717a
BP
4414 return iface;
4415 }
4416 }
4417 return NULL;
064af421 4418}
557d8e6c 4419
52df17e7
BP
4420/* Set Ethernet address of 'iface', if one is specified in the configuration
4421 * file. */
4422static void
21a955fc 4423iface_set_mac(const struct bridge *br, const struct port *port, struct iface *iface)
52df17e7 4424{
74ff3298 4425 struct eth_addr ea, *mac = NULL;
21a955fc 4426 struct iface *hw_addr_iface;
52df17e7 4427
ddd43de9
HS
4428 if (strcmp(iface->type, "internal")) {
4429 return;
4430 }
4431
74ff3298
JR
4432 if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, &ea)) {
4433 mac = &ea;
21a955fc
HS
4434 } else if (port->cfg->fake_bridge) {
4435 /* Fake bridge and no MAC set in the configuration. Pick a local one. */
74ff3298
JR
4436 find_local_hw_addr(br, &ea, port, &hw_addr_iface);
4437 mac = &ea;
ddd43de9
HS
4438 }
4439
4440 if (mac) {
892815f5 4441 if (iface->ofp_port == OFPP_LOCAL) {
ede2fd6d
BP
4442 VLOG_ERR("interface %s: ignoring mac in Interface record "
4443 "(use Bridge record to set local port's mac)",
4444 iface->name);
74ff3298 4445 } else if (eth_addr_is_multicast(*mac)) {
52df17e7
BP
4446 VLOG_ERR("interface %s: cannot set MAC to multicast address",
4447 iface->name);
12228368 4448 } else if (eth_addr_is_zero(*mac)) {
4449 VLOG_ERR("interface %s: cannot set MAC to all zero address",
4450 iface->name);
52df17e7 4451 } else {
74ff3298 4452 int error = netdev_set_etheraddr(iface->netdev, *mac);
52df17e7
BP
4453 if (error) {
4454 VLOG_ERR("interface %s: setting MAC failed (%s)",
10a89ef0 4455 iface->name, ovs_strerror(error));
52df17e7
BP
4456 }
4457 }
4458 }
4459}
c1c9c9c4 4460
bcd49a45
BP
4461/* Sets the ofport column of 'if_cfg' to 'ofport'. */
4462static void
4e022ec0 4463iface_set_ofport(const struct ovsrec_interface *if_cfg, ofp_port_t ofport)
bcd49a45 4464{
cfea354b 4465 if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
820b35a3
BP
4466 int64_t port = ofport == OFPP_NONE ? -1 : ofp_to_u16(ofport);
4467 ovsrec_interface_set_ofport(if_cfg, &port, 1);
bcd49a45
BP
4468 }
4469}
4470
3fc5a86a
BP
4471/* Clears all of the fields in 'if_cfg' that indicate interface status, and
4472 * sets the "ofport" field to -1.
4473 *
4474 * This is appropriate when 'if_cfg''s interface cannot be created or is
4475 * otherwise invalid. */
4476static void
bbe6109d 4477iface_clear_db_record(const struct ovsrec_interface *if_cfg, char *errp)
3fc5a86a
BP
4478{
4479 if (!ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
59804c80 4480 iface_set_ofport(if_cfg, OFPP_NONE);
bbe6109d 4481 ovsrec_interface_set_error(if_cfg, errp);
a699f614 4482 ovsrec_interface_set_status(if_cfg, NULL);
3fc5a86a
BP
4483 ovsrec_interface_set_admin_state(if_cfg, NULL);
4484 ovsrec_interface_set_duplex(if_cfg, NULL);
4485 ovsrec_interface_set_link_speed(if_cfg, NULL, 0);
4486 ovsrec_interface_set_link_state(if_cfg, NULL);
df867eda 4487 ovsrec_interface_set_mac_in_use(if_cfg, NULL);
3fc5a86a
BP
4488 ovsrec_interface_set_mtu(if_cfg, NULL, 0);
4489 ovsrec_interface_set_cfm_fault(if_cfg, NULL, 0);
b9380396 4490 ovsrec_interface_set_cfm_fault_status(if_cfg, NULL, 0);
3fc5a86a
BP
4491 ovsrec_interface_set_cfm_remote_mpids(if_cfg, NULL, 0);
4492 ovsrec_interface_set_lacp_current(if_cfg, NULL, 0);
4493 ovsrec_interface_set_statistics(if_cfg, NULL, NULL, 0);
ea401d9a 4494 ovsrec_interface_set_ifindex(if_cfg, NULL, 0);
3fc5a86a
BP
4495 }
4496}
4497
c1c9c9c4 4498static bool
44fca7f9 4499queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
c1c9c9c4 4500{
44fca7f9 4501 union ovsdb_atom atom;
c1c9c9c4 4502
44fca7f9
BP
4503 atom.integer = target;
4504 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
c1c9c9c4
BP
4505}
4506
c1c9c9c4 4507static void
66da9bef 4508iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
c1c9c9c4 4509{
8b36f51e
EJ
4510 struct ofpbuf queues_buf;
4511
4512 ofpbuf_init(&queues_buf, 0);
4513
677d9158 4514 if (!qos || qos->type[0] == '\0') {
c1c9c9c4
BP
4515 netdev_set_qos(iface->netdev, NULL, NULL);
4516 } else {
89454bf4
BP
4517 const struct ovsdb_datum *queues;
4518 struct netdev_queue_dump dump;
4519 unsigned int queue_id;
4520 struct smap details;
6a6e60bd 4521 bool queue_zero;
c1c9c9c4
BP
4522 size_t i;
4523
4524 /* Configure top-level Qos for 'iface'. */
a699f614 4525 netdev_set_qos(iface->netdev, qos->type, &qos->other_config);
c1c9c9c4
BP
4526
4527 /* Deconfigure queues that were deleted. */
89454bf4
BP
4528 queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
4529 OVSDB_TYPE_UUID);
4530 smap_init(&details);
4531 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
4532 if (!queue_ids_include(queues, queue_id)) {
4533 netdev_delete_queue(iface->netdev, queue_id);
4534 }
4535 }
4536 smap_destroy(&details);
c1c9c9c4
BP
4537
4538 /* Configure queues for 'iface'. */
6a6e60bd 4539 queue_zero = false;
c1c9c9c4
BP
4540 for (i = 0; i < qos->n_queues; i++) {
4541 const struct ovsrec_queue *queue = qos->value_queues[i];
71f21279 4542 queue_id = qos->key_queues[i];
c1c9c9c4 4543
6a6e60bd
BP
4544 if (queue_id == 0) {
4545 queue_zero = true;
4546 }
4547
8b36f51e
EJ
4548 if (queue->n_dscp == 1) {
4549 struct ofproto_port_queue *port_queue;
4550
4551 port_queue = ofpbuf_put_uninit(&queues_buf,
4552 sizeof *port_queue);
4553 port_queue->queue = queue_id;
4554 port_queue->dscp = queue->dscp[0];
4555 }
4556
a699f614 4557 netdev_set_queue(iface->netdev, queue_id, &queue->other_config);
c1c9c9c4 4558 }
6a6e60bd 4559 if (!queue_zero) {
79f1cbe9 4560 smap_init(&details);
2c999774 4561 netdev_set_queue(iface->netdev, 0, &details);
79f1cbe9 4562 smap_destroy(&details);
6a6e60bd 4563 }
c1c9c9c4 4564 }
fa066f01 4565
c60c33fb 4566 if (iface->ofp_port != OFPP_NONE) {
6fd6ed71
PS
4567 const struct ofproto_port_queue *port_queues = queues_buf.data;
4568 size_t n_queues = queues_buf.size / sizeof *port_queues;
8b36f51e
EJ
4569
4570 ofproto_port_set_queues(iface->port->bridge->ofproto, iface->ofp_port,
4571 port_queues, n_queues);
4572 }
4573
fa066f01 4574 netdev_set_policing(iface->netdev,
c7952afb
BP
4575 MIN(UINT32_MAX, iface->cfg->ingress_policing_rate),
4576 MIN(UINT32_MAX, iface->cfg->ingress_policing_burst));
8b36f51e
EJ
4577
4578 ofpbuf_uninit(&queues_buf);
c1c9c9c4 4579}
b31bcf60
EJ
4580
4581static void
66da9bef 4582iface_configure_cfm(struct iface *iface)
b31bcf60 4583{
93b8df38 4584 const struct ovsrec_interface *cfg = iface->cfg;
a699f614 4585 const char *opstate_str;
189cb9e4 4586 const char *cfm_ccm_vlan;
a5610457 4587 struct cfm_settings s;
b363bae4 4588 struct smap netdev_args;
b31bcf60 4589
144216a3 4590 if (!cfg->n_cfm_mpid) {
892815f5 4591 ofproto_port_clear_cfm(iface->port->bridge->ofproto, iface->ofp_port);
b31bcf60
EJ
4592 return;
4593 }
4594
b363bae4
EJ
4595 s.check_tnl_key = false;
4596 smap_init(&netdev_args);
4597 if (!netdev_get_config(iface->netdev, &netdev_args)) {
4598 const char *key = smap_get(&netdev_args, "key");
4599 const char *in_key = smap_get(&netdev_args, "in_key");
4600
4601 s.check_tnl_key = (key && !strcmp(key, "flow"))
4602 || (in_key && !strcmp(in_key, "flow"));
4603 }
4604 smap_destroy(&netdev_args);
4605
a5610457 4606 s.mpid = *cfg->cfm_mpid;
a699f614
EJ
4607 s.interval = smap_get_int(&iface->cfg->other_config, "cfm_interval", 0);
4608 cfm_ccm_vlan = smap_get(&iface->cfg->other_config, "cfm_ccm_vlan");
4609 s.ccm_pcp = smap_get_int(&iface->cfg->other_config, "cfm_ccm_pcp", 0);
4610
a5610457
EJ
4611 if (s.interval <= 0) {
4612 s.interval = 1000;
b31bcf60 4613 }
b31bcf60 4614
a699f614
EJ
4615 if (!cfm_ccm_vlan) {
4616 s.ccm_vlan = 0;
4617 } else if (!strcasecmp("random", cfm_ccm_vlan)) {
189cb9e4
EJ
4618 s.ccm_vlan = CFM_RANDOM_VLAN;
4619 } else {
4620 s.ccm_vlan = atoi(cfm_ccm_vlan);
4621 if (s.ccm_vlan == CFM_RANDOM_VLAN) {
4622 s.ccm_vlan = 0;
4623 }
4624 }
4625
a699f614
EJ
4626 s.extended = smap_get_bool(&iface->cfg->other_config, "cfm_extended",
4627 false);
90967e95 4628 s.demand = smap_get_bool(&iface->cfg->other_config, "cfm_demand", false);
ef9819b5 4629
a699f614
EJ
4630 opstate_str = smap_get(&iface->cfg->other_config, "cfm_opstate");
4631 s.opup = !opstate_str || !strcasecmp("up", opstate_str);
86dc6501 4632
a5610457 4633 ofproto_port_set_cfm(iface->port->bridge->ofproto, iface->ofp_port, &s);
b31bcf60 4634}
0b8024eb 4635
cfea354b
BP
4636/* Returns true if 'iface' is synthetic, that is, if we constructed it locally
4637 * instead of obtaining it from the database. */
4638static bool
4639iface_is_synthetic(const struct iface *iface)
4640{
4641 return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
4642}
f125905c 4643
4e022ec0 4644static ofp_port_t
4abb8608
BP
4645iface_validate_ofport__(size_t n, int64_t *ofport)
4646{
4647 return (n && *ofport >= 1 && *ofport < ofp_to_u16(OFPP_MAX)
4648 ? u16_to_ofp(*ofport)
4649 : OFPP_NONE);
4650}
4651
4652static ofp_port_t
4653iface_get_requested_ofp_port(const struct ovsrec_interface *cfg)
558e2cc5 4654{
4abb8608 4655 return iface_validate_ofport__(cfg->n_ofport_request, cfg->ofport_request);
558e2cc5
GS
4656}
4657
4abb8608
BP
4658static ofp_port_t
4659iface_pick_ofport(const struct ovsrec_interface *cfg)
4660{
4661 ofp_port_t requested_ofport = iface_get_requested_ofp_port(cfg);
4662 return (requested_ofport != OFPP_NONE
4663 ? requested_ofport
4664 : iface_validate_ofport__(cfg->n_ofport, cfg->ofport));
4665}
064af421
BP
4666\f
4667/* Port mirroring. */
4668
dd0d105c
BP
4669static struct mirror *
4670mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
4671{
fa066f01 4672 struct mirror *m;
dd0d105c 4673
fa066f01
BP
4674 HMAP_FOR_EACH_IN_BUCKET (m, hmap_node, uuid_hash(uuid), &br->mirrors) {
4675 if (uuid_equals(uuid, &m->uuid)) {
dd0d105c
BP
4676 return m;
4677 }
4678 }
4679 return NULL;
4680}
4681
064af421 4682static void
fa066f01 4683bridge_configure_mirrors(struct bridge *br)
064af421 4684{
fa066f01
BP
4685 const struct ovsdb_datum *mc;
4686 unsigned long *flood_vlans;
4687 struct mirror *m, *next;
4688 size_t i;
064af421 4689
dd0d105c 4690 /* Get rid of deleted mirrors. */
fa066f01
BP
4691 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
4692 HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mirrors) {
4693 union ovsdb_atom atom;
4694
4695 atom.uuid = m->uuid;
4696 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
4697 mirror_destroy(m);
064af421
BP
4698 }
4699 }
4700
dd0d105c 4701 /* Add new mirrors and reconfigure existing ones. */
37e7f427 4702 for (i = 0; i < br->cfg->n_mirrors; i++) {
fa066f01 4703 const struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
71f21279 4704 m = mirror_find_by_uuid(br, &cfg->header_.uuid);
fa066f01
BP
4705 if (!m) {
4706 m = mirror_create(br, cfg);
064af421 4707 }
9d24de3b
JP
4708 m->cfg = cfg;
4709 if (!mirror_configure(m)) {
fa066f01 4710 mirror_destroy(m);
064af421
BP
4711 }
4712 }
f2d7fd66 4713
8f30d09a 4714 /* Update flooded vlans (for RSPAN). */
fa066f01
BP
4715 flood_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
4716 br->cfg->n_flood_vlans);
4717 ofproto_set_flood_vlans(br->ofproto, flood_vlans);
4718 bitmap_free(flood_vlans);
064af421
BP
4719}
4720
fa066f01
BP
4721static struct mirror *
4722mirror_create(struct bridge *br, const struct ovsrec_mirror *cfg)
064af421
BP
4723{
4724 struct mirror *m;
064af421 4725
fa066f01 4726 m = xzalloc(sizeof *m);
8bbc128e 4727 m->uuid = cfg->header_.uuid;
fa066f01 4728 hmap_insert(&br->mirrors, &m->hmap_node, uuid_hash(&m->uuid));
064af421 4729 m->bridge = br;
dd0d105c 4730 m->name = xstrdup(cfg->name);
37e7f427 4731
fa066f01 4732 return m;
064af421
BP
4733}
4734
4735static void
4736mirror_destroy(struct mirror *m)
4737{
4738 if (m) {
4739 struct bridge *br = m->bridge;
064af421 4740
fa066f01
BP
4741 if (br->ofproto) {
4742 ofproto_mirror_unregister(br->ofproto, m);
064af421
BP
4743 }
4744
fa066f01 4745 hmap_remove(&br->mirrors, &m->hmap_node);
786880a5 4746 free(m->name);
064af421 4747 free(m);
064af421
BP
4748 }
4749}
4750
4751static void
fa066f01
BP
4752mirror_collect_ports(struct mirror *m,
4753 struct ovsrec_port **in_ports, int n_in_ports,
4754 void ***out_portsp, size_t *n_out_portsp)
064af421 4755{
fa066f01
BP
4756 void **out_ports = xmalloc(n_in_ports * sizeof *out_ports);
4757 size_t n_out_ports = 0;
064af421
BP
4758 size_t i;
4759
fa066f01
BP
4760 for (i = 0; i < n_in_ports; i++) {
4761 const char *name = in_ports[i]->name;
4762 struct port *port = port_lookup(m->bridge, name);
4763 if (port) {
4764 out_ports[n_out_ports++] = port;
064af421 4765 } else {
37e7f427
BP
4766 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
4767 "port %s", m->bridge->name, m->name, name);
064af421
BP
4768 }
4769 }
fa066f01
BP
4770 *out_portsp = out_ports;
4771 *n_out_portsp = n_out_ports;
064af421
BP
4772}
4773
4774static bool
9d24de3b 4775mirror_configure(struct mirror *m)
064af421 4776{
9d24de3b 4777 const struct ovsrec_mirror *cfg = m->cfg;
fa066f01 4778 struct ofproto_mirror_settings s;
064af421 4779
dd0d105c
BP
4780 /* Set name. */
4781 if (strcmp(cfg->name, m->name)) {
4782 free(m->name);
4783 m->name = xstrdup(cfg->name);
4784 }
fa066f01 4785 s.name = m->name;
dd0d105c 4786
fa066f01 4787 /* Get output port or VLAN. */
37e7f427 4788 if (cfg->output_port) {
fa066f01 4789 s.out_bundle = port_lookup(m->bridge, cfg->output_port->name);
abe529af 4790 if (!s.out_bundle) {
37e7f427
BP
4791 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
4792 m->bridge->name, m->name);
fa066f01 4793 return false;
064af421 4794 }
fa066f01 4795 s.out_vlan = UINT16_MAX;
064af421 4796
37e7f427
BP
4797 if (cfg->output_vlan) {
4798 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
4799 "output vlan; ignoring output vlan",
4800 m->bridge->name, m->name);
064af421 4801 }
37e7f427 4802 } else if (cfg->output_vlan) {
fa066f01
BP
4803 /* The database should prevent invalid VLAN values. */
4804 s.out_bundle = NULL;
4805 s.out_vlan = *cfg->output_vlan;
064af421 4806 } else {
37e7f427
BP
4807 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
4808 m->bridge->name, m->name);
fa066f01 4809 return false;
064af421
BP
4810 }
4811
1356dbd1
WT
4812 if (cfg->snaplen) {
4813 s.snaplen = *cfg->snaplen;
4814 } else {
4815 s.snaplen = 0;
4816 }
4817
fa066f01 4818 /* Get port selection. */
939ff267 4819 if (cfg->select_all) {
fa066f01
BP
4820 size_t n_ports = hmap_count(&m->bridge->ports);
4821 void **ports = xmalloc(n_ports * sizeof *ports);
abe529af 4822 struct port *port;
fa066f01
BP
4823 size_t i;
4824
4825 i = 0;
8052fb14 4826 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
fa066f01 4827 ports[i++] = port;
939ff267 4828 }
fa066f01
BP
4829
4830 s.srcs = ports;
4831 s.n_srcs = n_ports;
4832
4833 s.dsts = ports;
4834 s.n_dsts = n_ports;
939ff267 4835 } else {
fa066f01
BP
4836 /* Get ports, dropping ports that don't exist.
4837 * The IDL ensures that there are no duplicates. */
939ff267 4838 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
fa066f01 4839 &s.srcs, &s.n_srcs);
939ff267 4840 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
fa066f01 4841 &s.dsts, &s.n_dsts);
064af421
BP
4842 }
4843
fa066f01
BP
4844 /* Get VLAN selection. */
4845 s.src_vlans = vlan_bitmap_from_array(cfg->select_vlan, cfg->n_select_vlan);
4846
4847 /* Configure. */
4848 ofproto_mirror_register(m->bridge->ofproto, m, &s);
4849
064af421 4850 /* Clean up. */
fa066f01
BP
4851 if (s.srcs != s.dsts) {
4852 free(s.dsts);
4853 }
4854 free(s.srcs);
4855 free(s.src_vlans);
4856
4857 return true;
064af421 4858}
9d24de3b 4859
42deb67d 4860\f
9d24de3b
JP
4861static void
4862mirror_refresh_stats(struct mirror *m)
4863{
4864 struct ofproto *ofproto = m->bridge->ofproto;
4865 uint64_t tx_packets, tx_bytes;
47058293 4866 const char *keys[2];
9d24de3b
JP
4867 int64_t values[2];
4868 size_t stat_cnt = 0;
4869
4870 if (ofproto_mirror_get_stats(ofproto, m, &tx_packets, &tx_bytes)) {
4871 ovsrec_mirror_set_statistics(m->cfg, NULL, NULL, 0);
4872 return;
4873 }
4874
4875 if (tx_packets != UINT64_MAX) {
4876 keys[stat_cnt] = "tx_packets";
4877 values[stat_cnt] = tx_packets;
4878 stat_cnt++;
4879 }
4880 if (tx_bytes != UINT64_MAX) {
4881 keys[stat_cnt] = "tx_bytes";
4882 values[stat_cnt] = tx_bytes;
4883 stat_cnt++;
4884 }
4885
4886 ovsrec_mirror_set_statistics(m->cfg, keys, values, stat_cnt);
4887}
842733c3
MG
4888
4889/*
4890 * Add registered netdev and dpif types to ovsdb to allow external
4891 * applications to query the capabilities of the Open vSwitch instance
4892 * running on the node.
4893 */
4894static void
4895discover_types(const struct ovsrec_open_vswitch *cfg)
4896{
4897 struct sset types;
4898
4899 /* Datapath types. */
4900 sset_init(&types);
4901 dp_enumerate_types(&types);
4902 const char **datapath_types = sset_array(&types);
4903 ovsrec_open_vswitch_set_datapath_types(cfg, datapath_types,
4904 sset_count(&types));
4905 free(datapath_types);
4906 sset_destroy(&types);
4907
4908 /* Port types. */
4909 sset_init(&types);
4910 netdev_enumerate_types(&types);
4911 const char **iface_types = sset_array(&types);
4912 ovsrec_open_vswitch_set_iface_types(cfg, iface_types, sset_count(&types));
4913 free(iface_types);
4914 sset_destroy(&types);
4915}