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