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