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