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