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