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