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