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