]> git.proxmox.com Git - ovs.git/blame - vswitchd/bridge.c
vswitchd: Drop 'need_refresh' member from struct iface.
[ovs.git] / vswitchd / bridge.c
CommitLineData
45c580a3 1/* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks
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"
18#include <assert.h>
19#include <errno.h>
064af421 20#include <inttypes.h>
064af421 21#include <stdlib.h>
064af421 22#include "bitmap.h"
f620b43a 23#include "bond.h"
b31bcf60 24#include "cfm.h"
064af421 25#include "coverage.h"
a7ff9bd7 26#include "daemon.h"
064af421 27#include "dirs.h"
064af421 28#include "dynamic-string.h"
064af421 29#include "hash.h"
d9a8717a 30#include "hmap.h"
f145afdc 31#include "hmapx.h"
cd11000b 32#include "jsonrpc.h"
6aa74308 33#include "lacp.h"
064af421 34#include "list.h"
e764773c 35#include "mac-learning.h"
254750ce 36#include "meta-flow.h"
064af421 37#include "netdev.h"
064af421
BP
38#include "ofp-print.h"
39#include "ofpbuf.h"
8cd4882f 40#include "ofproto/ofproto.h"
064af421 41#include "poll-loop.h"
76343538 42#include "sha1.h"
6c88d577 43#include "shash.h"
064af421 44#include "socket-util.h"
5bd31620 45#include "stream.h"
fe55ad15 46#include "stream-ssl.h"
b3c01ed3 47#include "sset.h"
ce887677 48#include "system-stats.h"
064af421
BP
49#include "timeval.h"
50#include "util.h"
da285df4 51#include "unixctl.h"
52a90c29 52#include "vlandev.h"
eaa67ba8 53#include "lib/vswitch-idl.h"
064af421 54#include "xenserver.h"
5136ce49 55#include "vlog.h"
72b06300 56#include "sflow_api.h"
0fb7b915 57#include "vlan-bitmap.h"
064af421 58
d98e6007 59VLOG_DEFINE_THIS_MODULE(bridge);
064af421 60
d76f09ea
BP
61COVERAGE_DEFINE(bridge_reconfigure);
62
bae7208e
EJ
63/* Configuration of an uninstantiated iface. */
64struct if_cfg {
65 struct hmap_node hmap_node; /* Node in bridge's if_cfg_todo. */
66 const struct ovsrec_interface *cfg; /* Interface record. */
67 const struct ovsrec_port *parent; /* Parent port record. */
68};
69
70/* OpenFlow port slated for removal from ofproto. */
71struct ofpp_garbage {
72 struct list list_node; /* Node in bridge's ofpp_garbage. */
73 uint16_t ofp_port; /* Port to be deleted. */
74};
75
064af421 76struct iface {
0c6aea3f 77 /* These members are always valid. */
83db7968 78 struct list port_elem; /* Element in struct port's "ifaces" list. */
ebea37cc 79 struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
064af421 80 struct port *port; /* Containing port. */
064af421 81 char *name; /* Host network device name. */
064af421 82
0c6aea3f 83 /* These members are valid only after bridge_reconfigure() causes them to
1e0b752d 84 * be initialized. */
892815f5
BP
85 struct hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
86 int ofp_port; /* OpenFlow port number, -1 if unknown. */
0c6aea3f 87 struct netdev *netdev; /* Network device. */
6cefe1da 88 const char *type; /* Usually same as cfg->type. */
76343538 89 const struct ovsrec_interface *cfg;
064af421
BP
90};
91
064af421 92struct mirror {
fa066f01
BP
93 struct uuid uuid; /* UUID of this "mirror" record in database. */
94 struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
064af421 95 struct bridge *bridge;
064af421 96 char *name;
9d24de3b 97 const struct ovsrec_mirror *cfg;
064af421
BP
98};
99
064af421 100struct port {
8052fb14 101 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
6e492d81 102 struct bridge *bridge;
8052fb14
BP
103 char *name;
104
1e0b752d 105 const struct ovsrec_port *cfg;
064af421
BP
106
107 /* An ordinary bridge port has 1 interface.
108 * A bridge port for bonding has at least 2 interfaces. */
83db7968 109 struct list ifaces; /* List of "struct iface"s. */
064af421
BP
110};
111
064af421 112struct bridge {
764072fd 113 struct hmap_node node; /* In 'all_bridges'. */
064af421 114 char *name; /* User-specified arbitrary name. */
66da9bef 115 char *type; /* Datapath type. */
abe457eb 116 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
064af421 117 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
1e0b752d 118 const struct ovsrec_bridge *cfg;
064af421 119
064af421
BP
120 /* OpenFlow switch processing. */
121 struct ofproto *ofproto; /* OpenFlow switch. */
122
064af421 123 /* Bridge ports. */
8052fb14 124 struct hmap ports; /* "struct port"s indexed by name. */
892815f5 125 struct hmap ifaces; /* "struct iface"s indexed by ofp_port. */
ebea37cc 126 struct hmap iface_by_name; /* "struct iface"s indexed by name. */
064af421 127
bae7208e
EJ
128 struct list ofpp_garbage; /* "struct ofpp_garbage" slated for removal. */
129 struct hmap if_cfg_todo; /* "struct if_cfg"s slated for creation.
130 Indexed on 'cfg->name'. */
131
064af421 132 /* Port mirroring. */
fa066f01 133 struct hmap mirrors; /* "struct mirror" indexed by UUID. */
cfea354b
BP
134
135 /* Synthetic local port if necessary. */
136 struct ovsrec_port synth_local_port;
137 struct ovsrec_interface synth_local_iface;
138 struct ovsrec_interface *synth_local_ifacep;
064af421
BP
139};
140
764072fd
BP
141/* All bridges, indexed by name. */
142static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
064af421 143
c5187f17
BP
144/* OVSDB IDL used to obtain configuration. */
145static struct ovsdb_idl *idl;
146
854a94d9
BP
147/* Most recently processed IDL sequence number. */
148static unsigned int idl_seqno;
149
cd0cd65f
BP
150/* Each time this timer expires, the bridge fetches systems and interface
151 * statistics and pushes them into the database. */
152#define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
153static long long int stats_timer = LLONG_MIN;
018f1525 154
815cd583
EJ
155/* Stores the time after which rate limited statistics may be written to the
156 * database. Only updated when changes to the database require rate limiting.
157 */
158#define DB_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
159static long long int db_limiter = LLONG_MIN;
6586adf5 160
c7e7bb21
EJ
161/* In some datapaths, creating and destroying OpenFlow ports can be extremely
162 * expensive. This can cause bridge_reconfigure() to take a long time during
163 * which no other work can be done. To deal with this problem, we limit port
164 * adds and deletions to a window of OFP_PORT_ACTION_WINDOW milliseconds per
165 * call to bridge_reconfigure(). If there is more work to do after the limit
166 * is reached, 'need_reconfigure', is flagged and it's done on the next loop.
167 * This allows the rest of the code to catch up on important things like
168 * forwarding packets. */
169#define OFP_PORT_ACTION_WINDOW 10
bae7208e 170static bool reconfiguring = false;
c7e7bb21 171
66da9bef 172static void add_del_bridges(const struct ovsrec_open_vswitch *);
bae7208e 173static void bridge_update_ofprotos(void);
66da9bef 174static void bridge_create(const struct ovsrec_bridge *);
064af421
BP
175static void bridge_destroy(struct bridge *);
176static struct bridge *bridge_lookup(const char *name);
8ca79daa 177static unixctl_cb_func bridge_unixctl_dump_flows;
fa05809b 178static unixctl_cb_func bridge_unixctl_reconnect;
1a048029 179static size_t bridge_get_controllers(const struct bridge *br,
76ce9432 180 struct ovsrec_controller ***controllersp);
52a90c29
BP
181static void bridge_add_del_ports(struct bridge *,
182 const unsigned long int *splinter_vlans);
892815f5 183static void bridge_refresh_ofp_port(struct bridge *);
6f90b8f4 184static void bridge_configure_datapath_id(struct bridge *);
084f5290 185static void bridge_configure_flow_eviction_threshold(struct bridge *);
6f90b8f4 186static void bridge_configure_netflow(struct bridge *);
8402c74b 187static void bridge_configure_forward_bpdu(struct bridge *);
e764773c 188static void bridge_configure_mac_idle_time(struct bridge *);
6f90b8f4 189static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
21f7563c 190static void bridge_configure_stp(struct bridge *);
254750ce 191static void bridge_configure_tables(struct bridge *);
fa066f01
BP
192static void bridge_configure_remotes(struct bridge *,
193 const struct sockaddr_in *managers,
194 size_t n_managers);
064af421
BP
195static void bridge_pick_local_hw_addr(struct bridge *,
196 uint8_t ea[ETH_ADDR_LEN],
07c318f4 197 struct iface **hw_addr_iface);
064af421
BP
198static uint64_t bridge_pick_datapath_id(struct bridge *,
199 const uint8_t bridge_ea[ETH_ADDR_LEN],
07c318f4 200 struct iface *hw_addr_iface);
bae7208e
EJ
201static void bridge_queue_if_cfg(struct bridge *,
202 const struct ovsrec_interface *,
203 const struct ovsrec_port *);
064af421 204static uint64_t dpid_from_hash(const void *, size_t nbytes);
e8192d80
BP
205static bool bridge_has_bond_fake_iface(const struct bridge *,
206 const char *name);
207static bool port_is_bond_fake_iface(const struct port *);
064af421 208
e8fe3026 209static unixctl_cb_func qos_unixctl_show;
8c4c1387 210
66da9bef 211static struct port *port_create(struct bridge *, const struct ovsrec_port *);
66da9bef 212static void port_del_ifaces(struct port *);
064af421
BP
213static void port_destroy(struct port *);
214static struct port *port_lookup(const struct bridge *, const char *name);
fa066f01
BP
215static void port_configure(struct port *);
216static struct lacp_settings *port_configure_lacp(struct port *,
217 struct lacp_settings *);
76ed83fc
BP
218static void port_configure_bond(struct port *, struct bond_settings *,
219 uint32_t *bond_stable_ids);
06b592bc 220static bool port_is_synthetic(const struct port *);
fa066f01
BP
221
222static void bridge_configure_mirrors(struct bridge *);
223static struct mirror *mirror_create(struct bridge *,
224 const struct ovsrec_mirror *);
064af421 225static void mirror_destroy(struct mirror *);
9d24de3b
JP
226static bool mirror_configure(struct mirror *);
227static void mirror_refresh_stats(struct mirror *);
064af421 228
fa066f01 229static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
bae7208e
EJ
230static void iface_create(struct bridge *, struct if_cfg *, int ofp_port);
231static void iface_refresh_type(struct iface *);
064af421
BP
232static void iface_destroy(struct iface *);
233static struct iface *iface_lookup(const struct bridge *, const char *name);
e8fe3026 234static struct iface *iface_find(const char *name);
bae7208e 235static struct if_cfg *if_cfg_lookup(const struct bridge *, const char *name);
892815f5
BP
236static struct iface *iface_from_ofp_port(const struct bridge *,
237 uint16_t ofp_port);
52df17e7 238static void iface_set_mac(struct iface *);
bcd49a45 239static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
3fc5a86a 240static void iface_clear_db_record(const struct ovsrec_interface *if_cfg);
66da9bef
BP
241static void iface_configure_qos(struct iface *, const struct ovsrec_qos *);
242static void iface_configure_cfm(struct iface *);
8f3fe844 243static void iface_refresh_cfm_stats(struct iface *);
1101a0b4
AE
244static void iface_refresh_stats(struct iface *);
245static void iface_refresh_status(struct iface *);
cfea354b 246static bool iface_is_synthetic(const struct iface *);
43776b8f
BP
247static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
248 struct shash *);
ea763e0e
EJ
249static void shash_to_ovs_idl_map(struct shash *,
250 char ***keys, char ***values, size_t *n);
52a90c29
BP
251
252/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
253 *
254 * This is deprecated. It is only for compatibility with broken device drivers
255 * in old versions of Linux that do not properly support VLANs when VLAN
256 * devices are not used. When broken device drivers are no longer in
257 * widespread use, we will delete these interfaces. */
258
259/* True if VLAN splinters are enabled on any interface, false otherwise.*/
260static bool vlan_splinters_enabled_anywhere;
261
262static bool vlan_splinters_is_enabled(const struct ovsrec_interface *);
263static unsigned long int *collect_splinter_vlans(
264 const struct ovsrec_open_vswitch *);
265static void configure_splinter_port(struct port *);
266static void add_vlan_splinter_ports(struct bridge *,
267 const unsigned long int *splinter_vlans,
268 struct shash *ports);
064af421
BP
269\f
270/* Public functions. */
271
c5187f17
BP
272/* Initializes the bridge module, configuring it to obtain its configuration
273 * from an OVSDB server accessed over 'remote', which should be a string in a
274 * form acceptable to ovsdb_idl_create(). */
064af421 275void
c5187f17
BP
276bridge_init(const char *remote)
277{
278 /* Create connection to database. */
ef73f86c 279 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
854a94d9 280 idl_seqno = ovsdb_idl_get_seqno(idl);
06b6d651 281 ovsdb_idl_set_lock(idl, "ovs_vswitchd");
c5187f17 282
ef73f86c
BP
283 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
284 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
e85bbd75 285 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
62f3aaed
BP
286 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
287 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
288 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
289 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
e85bbd75 290
62f3aaed 291 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
21f7563c 292 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_status);
e85bbd75
BP
293 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
294
21f7563c 295 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_status);
80740385 296 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_statistics);
e85bbd75
BP
297 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
298 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
299
62f3aaed
BP
300 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
301 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
302 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
303 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
65c3058c 304 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_resets);
62f3aaed 305 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
ef73f86c
BP
306 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
307 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
62f3aaed 308 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
b4117094 309 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault);
b9380396 310 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault_status);
1de11730 311 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_mpids);
3967a833 312 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_health);
b4117094 313 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_lacp_current);
e85bbd75
BP
314 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
315
62f3aaed
BP
316 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
317 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
318 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
319 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
320
62f3aaed
BP
321 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
322
323 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
324
325 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
9d24de3b 326 ovsdb_idl_omit_alert(idl, &ovsrec_mirror_col_statistics);
62f3aaed
BP
327
328 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
329
330 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
331
332 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
333 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
334 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
335 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
336 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
337
338 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
339
c5187f17 340 /* Register unixctl commands. */
0e15264f
BP
341 unixctl_command_register("qos/show", "interface", 1, 1,
342 qos_unixctl_show, NULL);
343 unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
7ff2009a 344 bridge_unixctl_dump_flows, NULL);
0e15264f 345 unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
7ff2009a 346 bridge_unixctl_reconnect, NULL);
5827ce14 347 lacp_init();
c5187f17 348 bond_init();
9ac3fce4 349 cfm_init();
fe4a02e4 350 stp_init();
c5187f17
BP
351}
352
ee45ad81
BP
353void
354bridge_exit(void)
355{
356 struct bridge *br, *next_br;
357
764072fd 358 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
ee45ad81
BP
359 bridge_destroy(br);
360 }
361 ovsdb_idl_destroy(idl);
362}
363
cd11000b
BP
364/* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
365 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
366 * responsible for freeing '*managersp' (with free()).
367 *
368 * You may be asking yourself "why does ovs-vswitchd care?", because
369 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
370 * should not be and in fact is not directly involved in that. But
371 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
372 * it has to tell in-band control where the managers are to enable that.
94db5407 373 * (Thus, only managers connected in-band are collected.)
cd11000b
BP
374 */
375static void
94db5407
BP
376collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
377 struct sockaddr_in **managersp, size_t *n_managersp)
cd11000b
BP
378{
379 struct sockaddr_in *managers = NULL;
380 size_t n_managers = 0;
b3c01ed3 381 struct sset targets;
94db5407
BP
382 size_t i;
383
289df16d
AE
384 /* Collect all of the potential targets from the "targets" columns of the
385 * rows pointed to by "manager_options", excluding any that are
386 * out-of-band. */
b3c01ed3 387 sset_init(&targets);
94db5407
BP
388 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
389 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
390
391 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
b3c01ed3 392 sset_find_and_delete(&targets, m->target);
94db5407 393 } else {
b3c01ed3 394 sset_add(&targets, m->target);
94db5407
BP
395 }
396 }
cd11000b 397
94db5407 398 /* Now extract the targets' IP addresses. */
b3c01ed3
BP
399 if (!sset_is_empty(&targets)) {
400 const char *target;
cd11000b 401
b3c01ed3
BP
402 managers = xmalloc(sset_count(&targets) * sizeof *managers);
403 SSET_FOR_EACH (target, &targets) {
94db5407 404 struct sockaddr_in *sin = &managers[n_managers];
cd11000b 405
ac4c900d
AA
406 if (stream_parse_target_with_default_ports(target,
407 JSONRPC_TCP_PORT,
408 JSONRPC_SSL_PORT,
409 sin)) {
cd11000b
BP
410 n_managers++;
411 }
412 }
413 }
b3c01ed3 414 sset_destroy(&targets);
cd11000b
BP
415
416 *managersp = managers;
417 *n_managersp = n_managers;
418}
419
c5187f17 420static void
76343538 421bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
064af421 422{
52a90c29 423 unsigned long int *splinter_vlans;
bae7208e 424 struct bridge *br;
064af421
BP
425
426 COVERAGE_INC(bridge_reconfigure);
427
bae7208e
EJ
428 assert(!reconfiguring);
429 reconfiguring = true;
c7e7bb21 430
bae7208e
EJ
431 /* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
432 * to 'ovs_cfg' while update the "if_cfg_queue", with only very minimal
433 * configuration otherwise.
66da9bef 434 *
bae7208e
EJ
435 * This is mostly an update to bridge data structures. Nothing is pushed
436 * down to ofproto or lower layers. */
66da9bef 437 add_del_bridges(ovs_cfg);
52a90c29 438 splinter_vlans = collect_splinter_vlans(ovs_cfg);
764072fd 439 HMAP_FOR_EACH (br, node, &all_bridges) {
52a90c29 440 bridge_add_del_ports(br, splinter_vlans);
76343538 441 }
52a90c29 442 free(splinter_vlans);
064af421 443
bae7208e
EJ
444 /* Delete datapaths that are no longer configured, and create ones which
445 * don't exist but should. */
446 bridge_update_ofprotos();
447
448 /* Make sure each "struct iface" has a correct ofp_port in its ofproto. */
764072fd 449 HMAP_FOR_EACH (br, node, &all_bridges) {
bae7208e
EJ
450 bridge_refresh_ofp_port(br);
451 }
452
453 /* Clear database records for "if_cfg"s which haven't been instantiated. */
454 HMAP_FOR_EACH (br, node, &all_bridges) {
455 struct if_cfg *if_cfg;
456
457 HMAP_FOR_EACH (if_cfg, hmap_node, &br->if_cfg_todo) {
458 iface_clear_db_record(if_cfg->cfg);
76343538 459 }
064af421 460 }
bae7208e 461}
064af421 462
bae7208e
EJ
463static bool
464bridge_reconfigure_ofp(void)
465{
466 long long int deadline;
467 struct bridge *br;
468
469 time_refresh();
470 deadline = time_msec() + OFP_PORT_ACTION_WINDOW;
471
472 /* The kernel will reject any attempt to add a given port to a datapath if
473 * that port already belongs to a different datapath, so we must do all
474 * port deletions before any port additions. */
475 HMAP_FOR_EACH (br, node, &all_bridges) {
476 struct ofpp_garbage *garbage, *next;
477
478 LIST_FOR_EACH_SAFE (garbage, next, list_node, &br->ofpp_garbage) {
479 ofproto_port_del(br->ofproto, garbage->ofp_port);
480 list_remove(&garbage->list_node);
481 free(garbage);
482
483 time_refresh();
484 if (time_msec() >= deadline) {
485 return false;
e56365a5 486 }
064af421
BP
487 }
488 }
bae7208e 489
e8192d80 490 HMAP_FOR_EACH (br, node, &all_bridges) {
bae7208e
EJ
491 struct if_cfg *if_cfg, *next;
492
493 HMAP_FOR_EACH_SAFE (if_cfg, next, hmap_node, &br->if_cfg_todo) {
494 iface_create(br, if_cfg, -1);
495 time_refresh();
496 if (time_msec() >= deadline) {
497 return false;
498 }
499 }
064af421 500 }
064af421 501
bae7208e
EJ
502 return true;
503}
504
505static bool
506bridge_reconfigure_continue(const struct ovsrec_open_vswitch *ovs_cfg)
507{
508 struct sockaddr_in *managers;
509 int sflow_bridge_number;
510 size_t n_managers;
511 struct bridge *br;
512 bool done;
513
514 assert(reconfiguring);
515 done = bridge_reconfigure_ofp();
516
66da9bef
BP
517 /* Complete the configuration. */
518 sflow_bridge_number = 0;
519 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
764072fd 520 HMAP_FOR_EACH (br, node, &all_bridges) {
8052fb14 521 struct port *port;
064af421 522
f145afdc
BP
523 /* We need the datapath ID early to allow LACP ports to use it as the
524 * default system ID. */
525 bridge_configure_datapath_id(br);
526
8052fb14 527 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968 528 struct iface *iface;
52df17e7 529
fa066f01 530 port_configure(port);
c1c9c9c4 531
48e1b7fb 532 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
66da9bef
BP
533 iface_configure_cfm(iface);
534 iface_configure_qos(iface, port->cfg->qos);
7ae79235 535 iface_set_mac(iface);
064af421
BP
536 }
537 }
fa066f01 538 bridge_configure_mirrors(br);
084f5290 539 bridge_configure_flow_eviction_threshold(br);
8402c74b 540 bridge_configure_forward_bpdu(br);
e764773c 541 bridge_configure_mac_idle_time(br);
fa066f01 542 bridge_configure_remotes(br, managers, n_managers);
66da9bef
BP
543 bridge_configure_netflow(br);
544 bridge_configure_sflow(br, &sflow_bridge_number);
21f7563c 545 bridge_configure_stp(br);
254750ce 546 bridge_configure_tables(br);
064af421 547 }
66da9bef 548 free(managers);
064af421 549
bae7208e 550 if (done) {
c7e7bb21
EJ
551 /* ovs-vswitchd has completed initialization, so allow the process that
552 * forked us to exit successfully. */
553 daemonize_complete();
bae7208e 554 reconfiguring = false;
c7e7bb21 555 }
bae7208e
EJ
556
557 return done;
66da9bef 558}
064af421 559
bae7208e
EJ
560/* Delete ofprotos which aren't configured or have the wrong type. Create
561 * ofprotos which don't exist but need to. */
66da9bef 562static void
bae7208e 563bridge_update_ofprotos(void)
66da9bef 564{
bae7208e 565 struct bridge *br, *next;
f79e673f
BP
566 struct sset names;
567 struct sset types;
66da9bef 568 const char *type;
6c88d577 569
bae7208e 570 /* Delete ofprotos with no bridge or with the wrong type. */
f79e673f
BP
571 sset_init(&names);
572 sset_init(&types);
573 ofproto_enumerate_types(&types);
574 SSET_FOR_EACH (type, &types) {
66da9bef 575 const char *name;
3a6ccc8c 576
f79e673f
BP
577 ofproto_enumerate_names(type, &names);
578 SSET_FOR_EACH (name, &names) {
bae7208e 579 br = bridge_lookup(name);
66da9bef 580 if (!br || strcmp(type, br->type)) {
f79e673f 581 ofproto_delete(name, type);
3a6ccc8c 582 }
b31bcf60
EJ
583 }
584 }
f79e673f
BP
585 sset_destroy(&names);
586 sset_destroy(&types);
76343538 587
bae7208e
EJ
588 /* Add ofprotos for bridges which don't have one yet. */
589 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
590 struct bridge *br2;
591 int error;
592
593 if (br->ofproto) {
594 continue;
595 }
596
597 /* Remove ports from any datapath with the same name as 'br'. If we
598 * don't do this, creating 'br''s ofproto will fail because a port with
599 * the same name as its local port already exists. */
600 HMAP_FOR_EACH (br2, node, &all_bridges) {
601 struct ofproto_port ofproto_port;
602
603 if (!br2->ofproto) {
604 continue;
605 }
606
607 if (!ofproto_port_query_by_name(br2->ofproto, br->name,
608 &ofproto_port)) {
609 error = ofproto_port_del(br2->ofproto, ofproto_port.ofp_port);
610 if (error) {
611 VLOG_ERR("failed to delete port %s: %s", ofproto_port.name,
612 strerror(error));
613 }
614 ofproto_port_destroy(&ofproto_port);
615 }
616 }
617
618 error = ofproto_create(br->name, br->type, &br->ofproto);
619 if (error) {
620 VLOG_ERR("failed to create bridge %s: %s", br->name,
621 strerror(error));
622 bridge_destroy(br);
623 }
66da9bef 624 }
093e47f4 625}
c3827f61 626
fa066f01
BP
627static void
628port_configure(struct port *port)
629{
630 const struct ovsrec_port *cfg = port->cfg;
631 struct bond_settings bond_settings;
632 struct lacp_settings lacp_settings;
633 struct ofproto_bundle_settings s;
634 struct iface *iface;
82057f51 635
52a90c29
BP
636 if (cfg->vlan_mode && !strcmp(cfg->vlan_mode, "splinter")) {
637 configure_splinter_port(port);
638 return;
639 }
640
fa066f01
BP
641 /* Get name. */
642 s.name = port->name;
3a6ccc8c 643
fa066f01
BP
644 /* Get slaves. */
645 s.n_slaves = 0;
646 s.slaves = xmalloc(list_size(&port->ifaces) * sizeof *s.slaves);
647 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
892815f5 648 s.slaves[s.n_slaves++] = iface->ofp_port;
fa066f01 649 }
c3827f61 650
fa066f01
BP
651 /* Get VLAN tag. */
652 s.vlan = -1;
acaf1486
BP
653 if (cfg->tag && *cfg->tag >= 0 && *cfg->tag <= 4095) {
654 s.vlan = *cfg->tag;
fa066f01 655 }
b0ec0f27 656
fa066f01
BP
657 /* Get VLAN trunks. */
658 s.trunks = NULL;
ecac4ebf 659 if (cfg->n_trunks) {
fa066f01 660 s.trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
ecac4ebf
BP
661 }
662
663 /* Get VLAN mode. */
664 if (cfg->vlan_mode) {
665 if (!strcmp(cfg->vlan_mode, "access")) {
666 s.vlan_mode = PORT_VLAN_ACCESS;
667 } else if (!strcmp(cfg->vlan_mode, "trunk")) {
668 s.vlan_mode = PORT_VLAN_TRUNK;
669 } else if (!strcmp(cfg->vlan_mode, "native-tagged")) {
670 s.vlan_mode = PORT_VLAN_NATIVE_TAGGED;
671 } else if (!strcmp(cfg->vlan_mode, "native-untagged")) {
672 s.vlan_mode = PORT_VLAN_NATIVE_UNTAGGED;
673 } else {
674 /* This "can't happen" because ovsdb-server should prevent it. */
675 VLOG_ERR("unknown VLAN mode %s", cfg->vlan_mode);
676 s.vlan_mode = PORT_VLAN_TRUNK;
677 }
678 } else {
679 if (s.vlan >= 0) {
680 s.vlan_mode = PORT_VLAN_ACCESS;
681 if (cfg->n_trunks) {
682 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
683 port->name);
684 }
685 } else {
686 s.vlan_mode = PORT_VLAN_TRUNK;
687 }
064af421 688 }
f67f1354 689 s.use_priority_tags = !strcmp("true", ovsrec_port_get_other_config_value(
5e9ceccd 690 cfg, "priority-tags", ""));
064af421 691
fa066f01
BP
692 /* Get LACP settings. */
693 s.lacp = port_configure_lacp(port, &lacp_settings);
694 if (s.lacp) {
695 size_t i = 0;
064af421 696
fa066f01
BP
697 s.lacp_slaves = xmalloc(s.n_slaves * sizeof *s.lacp_slaves);
698 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
699 iface_configure_lacp(iface, &s.lacp_slaves[i++]);
064af421 700 }
fa066f01
BP
701 } else {
702 s.lacp_slaves = NULL;
703 }
064af421 704
fa066f01
BP
705 /* Get bond settings. */
706 if (s.n_slaves > 1) {
fa066f01 707 s.bond = &bond_settings;
76ed83fc
BP
708 s.bond_stable_ids = xmalloc(s.n_slaves * sizeof *s.bond_stable_ids);
709 port_configure_bond(port, &bond_settings, s.bond_stable_ids);
fa066f01
BP
710 } else {
711 s.bond = NULL;
76ed83fc 712 s.bond_stable_ids = NULL;
1670c579
EJ
713
714 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
715 netdev_set_miimon_interval(iface->netdev, 0);
716 }
fa066f01 717 }
093e47f4 718
fa066f01
BP
719 /* Register. */
720 ofproto_bundle_register(port->bridge->ofproto, port, &s);
76343538 721
fa066f01 722 /* Clean up. */
4e51de4c 723 free(s.slaves);
fa066f01
BP
724 free(s.trunks);
725 free(s.lacp_slaves);
76ed83fc 726 free(s.bond_stable_ids);
fa066f01 727}
76343538 728
6f90b8f4
BP
729/* Pick local port hardware address and datapath ID for 'br'. */
730static void
731bridge_configure_datapath_id(struct bridge *br)
732{
733 uint8_t ea[ETH_ADDR_LEN];
734 uint64_t dpid;
735 struct iface *local_iface;
736 struct iface *hw_addr_iface;
737 char *dpid_string;
76343538 738
6f90b8f4 739 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
892815f5 740 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
6f90b8f4
BP
741 if (local_iface) {
742 int error = netdev_set_etheraddr(local_iface->netdev, ea);
743 if (error) {
744 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
745 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
746 "Ethernet address: %s",
747 br->name, strerror(error));
748 }
749 }
750 memcpy(br->ea, ea, ETH_ADDR_LEN);
76343538 751
6f90b8f4
BP
752 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
753 ofproto_set_datapath_id(br->ofproto, dpid);
76343538 754
6f90b8f4
BP
755 dpid_string = xasprintf("%016"PRIx64, dpid);
756 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
757 free(dpid_string);
758}
064af421 759
6f90b8f4
BP
760/* Set NetFlow configuration on 'br'. */
761static void
762bridge_configure_netflow(struct bridge *br)
763{
764 struct ovsrec_netflow *cfg = br->cfg->netflow;
765 struct netflow_options opts;
72b06300 766
6f90b8f4
BP
767 if (!cfg) {
768 ofproto_set_netflow(br->ofproto, NULL);
769 return;
770 }
a4af0040 771
6f90b8f4 772 memset(&opts, 0, sizeof opts);
72b06300 773
6f90b8f4
BP
774 /* Get default NetFlow configuration from datapath.
775 * Apply overrides from 'cfg'. */
776 ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
777 if (cfg->engine_type) {
778 opts.engine_type = *cfg->engine_type;
779 }
780 if (cfg->engine_id) {
781 opts.engine_id = *cfg->engine_id;
782 }
72b06300 783
6f90b8f4
BP
784 /* Configure active timeout interval. */
785 opts.active_timeout = cfg->active_timeout;
786 if (!opts.active_timeout) {
787 opts.active_timeout = -1;
788 } else if (opts.active_timeout < 0) {
789 VLOG_WARN("bridge %s: active timeout interval set to negative "
790 "value, using default instead (%d seconds)", br->name,
791 NF_ACTIVE_TIMEOUT_DEFAULT);
792 opts.active_timeout = -1;
793 }
72b06300 794
6f90b8f4
BP
795 /* Add engine ID to interface number to disambiguate bridgs? */
796 opts.add_id_to_iface = cfg->add_id_to_interface;
797 if (opts.add_id_to_iface) {
798 if (opts.engine_id > 0x7f) {
799 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
800 "another vswitch, choose an engine id less than 128",
801 br->name);
802 }
803 if (hmap_count(&br->ports) > 508) {
804 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
805 "another port when more than 508 ports are used",
806 br->name);
807 }
808 }
72b06300 809
6f90b8f4
BP
810 /* Collectors. */
811 sset_init(&opts.collectors);
812 sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
a4af0040 813
6f90b8f4
BP
814 /* Configure. */
815 if (ofproto_set_netflow(br->ofproto, &opts)) {
816 VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
817 }
818 sset_destroy(&opts.collectors);
819}
72b06300 820
6f90b8f4
BP
821/* Set sFlow configuration on 'br'. */
822static void
823bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
824{
825 const struct ovsrec_sflow *cfg = br->cfg->sflow;
826 struct ovsrec_controller **controllers;
827 struct ofproto_sflow_options oso;
828 size_t n_controllers;
829 size_t i;
72b06300 830
6f90b8f4
BP
831 if (!cfg) {
832 ofproto_set_sflow(br->ofproto, NULL);
833 return;
064af421 834 }
8052fb14 835
6f90b8f4 836 memset(&oso, 0, sizeof oso);
52df17e7 837
6f90b8f4
BP
838 sset_init(&oso.targets);
839 sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
c1c9c9c4 840
6f90b8f4
BP
841 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
842 if (cfg->sampling) {
843 oso.sampling_rate = *cfg->sampling;
064af421 844 }
6f90b8f4
BP
845
846 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
847 if (cfg->polling) {
848 oso.polling_interval = *cfg->polling;
064af421 849 }
093e47f4 850
6f90b8f4
BP
851 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
852 if (cfg->header) {
853 oso.header_len = *cfg->header;
854 }
392730c4 855
6f90b8f4
BP
856 oso.sub_id = (*sflow_bridge_number)++;
857 oso.agent_device = cfg->agent;
392730c4 858
6f90b8f4
BP
859 oso.control_ip = NULL;
860 n_controllers = bridge_get_controllers(br, &controllers);
861 for (i = 0; i < n_controllers; i++) {
862 if (controllers[i]->local_ip) {
863 oso.control_ip = controllers[i]->local_ip;
864 break;
b31bcf60
EJ
865 }
866 }
6f90b8f4 867 ofproto_set_sflow(br->ofproto, &oso);
b31bcf60 868
6f90b8f4
BP
869 sset_destroy(&oso.targets);
870}
a7ff9bd7 871
21f7563c
JP
872static void
873port_configure_stp(const struct ofproto *ofproto, struct port *port,
874 struct ofproto_port_stp_settings *port_s,
875 int *port_num_counter, unsigned long *port_num_bitmap)
876{
877 const char *config_str;
878 struct iface *iface;
879
f67f1354
EJ
880 config_str = ovsrec_port_get_other_config_value(port->cfg, "stp-enable",
881 NULL);
21f7563c
JP
882 if (config_str && !strcmp(config_str, "false")) {
883 port_s->enable = false;
884 return;
885 } else {
886 port_s->enable = true;
887 }
888
889 /* STP over bonds is not supported. */
890 if (!list_is_singleton(&port->ifaces)) {
891 VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
892 port->name);
893 port_s->enable = false;
894 return;
895 }
896
897 iface = CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
898
899 /* Internal ports shouldn't participate in spanning tree, so
900 * skip them. */
901 if (!strcmp(iface->type, "internal")) {
902 VLOG_DBG("port %s: disable STP on internal ports", port->name);
903 port_s->enable = false;
904 return;
905 }
906
907 /* STP on mirror output ports is not supported. */
908 if (ofproto_is_mirror_output_bundle(ofproto, port)) {
909 VLOG_DBG("port %s: disable STP on mirror ports", port->name);
910 port_s->enable = false;
911 return;
912 }
913
f67f1354
EJ
914 config_str = ovsrec_port_get_other_config_value(port->cfg, "stp-port-num",
915 NULL);
21f7563c
JP
916 if (config_str) {
917 unsigned long int port_num = strtoul(config_str, NULL, 0);
918 int port_idx = port_num - 1;
919
920 if (port_num < 1 || port_num > STP_MAX_PORTS) {
921 VLOG_ERR("port %s: invalid stp-port-num", port->name);
922 port_s->enable = false;
923 return;
924 }
925
926 if (bitmap_is_set(port_num_bitmap, port_idx)) {
927 VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
928 port->name, port_num);
929 port_s->enable = false;
930 return;
931 }
932 bitmap_set1(port_num_bitmap, port_idx);
933 port_s->port_num = port_idx;
934 } else {
935 if (*port_num_counter > STP_MAX_PORTS) {
936 VLOG_ERR("port %s: too many STP ports, disabling", port->name);
937 port_s->enable = false;
938 return;
939 }
940
941 port_s->port_num = (*port_num_counter)++;
942 }
943
f67f1354
EJ
944 config_str = ovsrec_port_get_other_config_value(port->cfg, "stp-path-cost",
945 NULL);
21f7563c
JP
946 if (config_str) {
947 port_s->path_cost = strtoul(config_str, NULL, 10);
948 } else {
6c038611 949 enum netdev_features current;
21f7563c
JP
950
951 if (netdev_get_features(iface->netdev, &current, NULL, NULL, NULL)) {
952 /* Couldn't get speed, so assume 100Mb/s. */
953 port_s->path_cost = 19;
954 } else {
955 unsigned int mbps;
956
957 mbps = netdev_features_to_bps(current) / 1000000;
958 port_s->path_cost = stp_convert_speed_to_cost(mbps);
959 }
960 }
961
f67f1354
EJ
962 config_str = ovsrec_port_get_other_config_value(port->cfg,
963 "stp-port-priority",
964 NULL);
21f7563c
JP
965 if (config_str) {
966 port_s->priority = strtoul(config_str, NULL, 0);
967 } else {
968 port_s->priority = STP_DEFAULT_PORT_PRIORITY;
969 }
970}
971
972/* Set spanning tree configuration on 'br'. */
973static void
974bridge_configure_stp(struct bridge *br)
975{
976 if (!br->cfg->stp_enable) {
977 ofproto_set_stp(br->ofproto, NULL);
978 } else {
979 struct ofproto_stp_settings br_s;
980 const char *config_str;
981 struct port *port;
982 int port_num_counter;
983 unsigned long *port_num_bitmap;
984
f67f1354
EJ
985 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
986 "stp-system-id",
987 NULL);
21f7563c
JP
988 if (config_str) {
989 uint8_t ea[ETH_ADDR_LEN];
990
991 if (eth_addr_from_string(config_str, ea)) {
992 br_s.system_id = eth_addr_to_uint64(ea);
993 } else {
994 br_s.system_id = eth_addr_to_uint64(br->ea);
995 VLOG_ERR("bridge %s: invalid stp-system-id, defaulting "
996 "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
997 }
998 } else {
999 br_s.system_id = eth_addr_to_uint64(br->ea);
1000 }
1001
f67f1354
EJ
1002 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
1003 "stp-priority",
1004 NULL);
21f7563c
JP
1005 if (config_str) {
1006 br_s.priority = strtoul(config_str, NULL, 0);
1007 } else {
1008 br_s.priority = STP_DEFAULT_BRIDGE_PRIORITY;
1009 }
1010
f67f1354
EJ
1011 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
1012 "stp-hello-time",
1013 NULL);
21f7563c
JP
1014 if (config_str) {
1015 br_s.hello_time = strtoul(config_str, NULL, 10) * 1000;
1016 } else {
1017 br_s.hello_time = STP_DEFAULT_HELLO_TIME;
1018 }
1019
f67f1354
EJ
1020 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
1021 "stp-max-age",
1022 NULL);
21f7563c
JP
1023 if (config_str) {
1024 br_s.max_age = strtoul(config_str, NULL, 10) * 1000;
1025 } else {
1026 br_s.max_age = STP_DEFAULT_MAX_AGE;
1027 }
1028
f67f1354
EJ
1029 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
1030 "stp-forward-delay",
1031 NULL);
21f7563c
JP
1032 if (config_str) {
1033 br_s.fwd_delay = strtoul(config_str, NULL, 10) * 1000;
1034 } else {
1035 br_s.fwd_delay = STP_DEFAULT_FWD_DELAY;
1036 }
1037
1038 /* Configure STP on the bridge. */
1039 if (ofproto_set_stp(br->ofproto, &br_s)) {
1040 VLOG_ERR("bridge %s: could not enable STP", br->name);
1041 return;
1042 }
1043
1044 /* Users must either set the port number with the "stp-port-num"
1045 * configuration on all ports or none. If manual configuration
1046 * is not done, then we allocate them sequentially. */
1047 port_num_counter = 0;
1048 port_num_bitmap = bitmap_allocate(STP_MAX_PORTS);
1049 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1050 struct ofproto_port_stp_settings port_s;
1051 struct iface *iface;
1052
1053 port_configure_stp(br->ofproto, port, &port_s,
1054 &port_num_counter, port_num_bitmap);
1055
1056 /* As bonds are not supported, just apply configuration to
1057 * all interfaces. */
1058 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1059 if (ofproto_port_set_stp(br->ofproto, iface->ofp_port,
1060 &port_s)) {
1061 VLOG_ERR("port %s: could not enable STP", port->name);
1062 continue;
1063 }
1064 }
1065 }
1066
1067 if (bitmap_scan(port_num_bitmap, 0, STP_MAX_PORTS) != STP_MAX_PORTS
1068 && port_num_counter) {
1069 VLOG_ERR("bridge %s: must manually configure all STP port "
1070 "IDs or none, disabling", br->name);
1071 ofproto_set_stp(br->ofproto, NULL);
1072 }
1073 bitmap_free(port_num_bitmap);
1074 }
1075}
1076
e8192d80
BP
1077static bool
1078bridge_has_bond_fake_iface(const struct bridge *br, const char *name)
1079{
1080 const struct port *port = port_lookup(br, name);
1081 return port && port_is_bond_fake_iface(port);
093e47f4
BP
1082}
1083
e8192d80
BP
1084static bool
1085port_is_bond_fake_iface(const struct port *port)
093e47f4 1086{
e8192d80
BP
1087 return port->cfg->bond_fake_iface && !list_is_short(&port->ifaces);
1088}
093e47f4 1089
66da9bef
BP
1090static void
1091add_del_bridges(const struct ovsrec_open_vswitch *cfg)
1092{
1093 struct bridge *br, *next;
1094 struct shash new_br;
1095 size_t i;
1096
1097 /* Collect new bridges' names and types. */
1098 shash_init(&new_br);
1099 for (i = 0; i < cfg->n_bridges; i++) {
5af5b532 1100 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
66da9bef 1101 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
5af5b532
BP
1102
1103 if (strchr(br_cfg->name, '/')) {
1104 /* Prevent remote ovsdb-server users from accessing arbitrary
1105 * directories, e.g. consider a bridge named "../../../etc/". */
1106 VLOG_WARN_RL(&rl, "ignoring bridge with invalid name \"%s\"",
1107 br_cfg->name);
1108 } else if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
1109 VLOG_WARN_RL(&rl, "bridge %s specified twice", br_cfg->name);
66da9bef
BP
1110 }
1111 }
1112
1113 /* Get rid of deleted bridges or those whose types have changed.
1114 * Update 'cfg' of bridges that still exist. */
1115 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
1116 br->cfg = shash_find_data(&new_br, br->name);
f79e673f
BP
1117 if (!br->cfg || strcmp(br->type, ofproto_normalize_type(
1118 br->cfg->datapath_type))) {
66da9bef
BP
1119 bridge_destroy(br);
1120 }
1121 }
1122
1123 /* Add new bridges. */
1124 for (i = 0; i < cfg->n_bridges; i++) {
1125 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
1126 struct bridge *br = bridge_lookup(br_cfg->name);
1127 if (!br) {
1128 bridge_create(br_cfg);
1129 }
1130 }
1131
1132 shash_destroy(&new_br);
1133}
1134
66da9bef 1135static void
892815f5 1136iface_set_ofp_port(struct iface *iface, int ofp_port)
66da9bef
BP
1137{
1138 struct bridge *br = iface->port->bridge;
1139
892815f5
BP
1140 assert(iface->ofp_port < 0 && ofp_port >= 0);
1141 iface->ofp_port = ofp_port;
1142 hmap_insert(&br->ifaces, &iface->ofp_port_node, hash_int(ofp_port, 0));
2e281761 1143 iface_set_ofport(iface->cfg, ofp_port);
66da9bef
BP
1144}
1145
bae7208e
EJ
1146static void
1147bridge_ofproto_port_del(struct bridge *br, struct ofproto_port ofproto_port)
1148{
1149 int error = ofproto_port_del(br->ofproto, ofproto_port.ofp_port);
1150 if (error) {
1151 VLOG_WARN("bridge %s: failed to remove %s interface (%s)",
1152 br->name, ofproto_port.name, strerror(error));
1153 } else {
1154 VLOG_INFO("bridge %s: removed interface %s (%d)", br->name,
1155 ofproto_port.name, ofproto_port.ofp_port);
1156 }
1157}
1158
1159/* Update bridges "if_cfg"s, "struct port"s, and "struct iface"s to be
1160 * consistent with the ofp_ports in "br->ofproto". */
66da9bef 1161static void
892815f5 1162bridge_refresh_ofp_port(struct bridge *br)
66da9bef
BP
1163{
1164 struct ofproto_port_dump dump;
1165 struct ofproto_port ofproto_port;
bae7208e 1166 struct port *port, *port_next;
66da9bef 1167
bae7208e 1168 /* Clear each "struct iface"s ofp_port so we can get its correct value. */
66da9bef
BP
1169 hmap_clear(&br->ifaces);
1170 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1171 struct iface *iface;
1172
1173 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
892815f5 1174 iface->ofp_port = -1;
66da9bef
BP
1175 }
1176 }
1177
bae7208e
EJ
1178 /* Obtain the correct "ofp_port"s from ofproto. Find any if_cfg's which
1179 * already exist in the datapath and promote them to full fledged "struct
1180 * iface"s. Mark ports in the datapath which don't belong as garbage. */
66da9bef 1181 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
66da9bef
BP
1182 struct iface *iface = iface_lookup(br, ofproto_port.name);
1183 if (iface) {
892815f5 1184 if (iface->ofp_port >= 0) {
66da9bef
BP
1185 VLOG_WARN("bridge %s: interface %s reported twice",
1186 br->name, ofproto_port.name);
892815f5 1187 } else if (iface_from_ofp_port(br, ofproto_port.ofp_port)) {
66da9bef 1188 VLOG_WARN("bridge %s: interface %"PRIu16" reported twice",
892815f5 1189 br->name, ofproto_port.ofp_port);
bae7208e 1190 } else if (!strcmp(ofproto_port.type, iface->type)) {
892815f5 1191 iface_set_ofp_port(iface, ofproto_port.ofp_port);
bae7208e
EJ
1192 } else {
1193 /* Port has incorrect type so delete it later. */
1194 }
1195 } else {
1196 struct if_cfg *if_cfg = if_cfg_lookup(br, ofproto_port.name);
1197
1198 if (if_cfg) {
1199 iface_create(br, if_cfg, ofproto_port.ofp_port);
1200 } else if (bridge_has_bond_fake_iface(br, ofproto_port.name)
1201 && strcmp(ofproto_port.type, "internal")) {
1202 /* Bond fake iface with the wrong type. */
1203 bridge_ofproto_port_del(br, ofproto_port);
1204 } else {
1205 struct ofpp_garbage *garbage = xmalloc(sizeof *garbage);
1206 garbage->ofp_port = ofproto_port.ofp_port;
1207 list_push_front(&br->ofpp_garbage, &garbage->list_node);
1208 }
1209 }
1210 }
1211
1212 /* Some ifaces may not have "ofp_port"s in ofproto and therefore don't
1213 * deserve to have "struct iface"s. Demote these to "if_cfg"s so that
1214 * later they can be added to ofproto. */
1215 HMAP_FOR_EACH_SAFE (port, port_next, hmap_node, &br->ports) {
1216 struct iface *iface, *iface_next;
1217
1218 LIST_FOR_EACH_SAFE (iface, iface_next, port_elem, &port->ifaces) {
1219 if (iface->ofp_port < 0) {
1220 bridge_queue_if_cfg(br, iface->cfg, port->cfg);
1221 iface_destroy(iface);
66da9bef
BP
1222 }
1223 }
bae7208e
EJ
1224
1225 if (list_is_empty(&port->ifaces)) {
1226 port_destroy(port);
1227 }
66da9bef
BP
1228 }
1229}
1230
bae7208e
EJ
1231/* Creates a new iface on 'br' based on 'if_cfg'. The new iface has OpenFlow
1232 * port number 'ofp_port'. If ofp_port is negative, an OpenFlow port is
1233 * automatically allocated for the iface. Takes ownership of and
1234 * deallocates 'if_cfg'. */
66da9bef 1235static void
bae7208e 1236iface_create(struct bridge *br, struct if_cfg *if_cfg, int ofp_port)
66da9bef 1237{
bae7208e
EJ
1238 struct iface *iface;
1239 struct port *port;
1240 int error;
66da9bef 1241
bae7208e 1242 assert(!iface_lookup(br, if_cfg->cfg->name));
66da9bef 1243
bae7208e
EJ
1244 port = port_lookup(br, if_cfg->parent->name);
1245 if (!port) {
1246 port = port_create(br, if_cfg->parent);
1247 }
66da9bef 1248
bae7208e
EJ
1249 iface = xzalloc(sizeof *iface);
1250 iface->port = port;
1251 iface->name = xstrdup(if_cfg->cfg->name);
1252 iface->ofp_port = -1;
1253 iface->netdev = NULL;
1254 iface->cfg = if_cfg->cfg;
bae7208e
EJ
1255 hmap_insert(&br->iface_by_name, &iface->name_node,
1256 hash_string(iface->name, 0));
1257 list_push_back(&port->ifaces, &iface->port_elem);
1258 iface_refresh_type(iface);
1259 if (ofp_port >= 0) {
1260 iface_set_ofp_port(iface, ofp_port);
1261 }
c7e7bb21 1262
bae7208e
EJ
1263 hmap_remove(&br->if_cfg_todo, &if_cfg->hmap_node);
1264 free(if_cfg);
1265 if_cfg = NULL;
52a90c29 1266
bae7208e
EJ
1267 error = netdev_open(iface->name, iface->type, &iface->netdev);
1268 if (error) {
1269 VLOG_WARN("could not open network device %s (%s)", iface->name,
1270 strerror(error));
1271 }
de5cdb90 1272
bae7208e
EJ
1273 if (iface->netdev
1274 && port->cfg->vlan_mode
1275 && !strcmp(port->cfg->vlan_mode, "splinter")) {
1276 netdev_turn_flags_on(iface->netdev, NETDEV_UP, true);
1277 }
de5cdb90 1278
bae7208e
EJ
1279 /* Configure the netdev. */
1280 if (iface->netdev) {
1281 struct shash args;
de5cdb90 1282
bae7208e
EJ
1283 shash_init(&args);
1284 shash_from_ovs_idl_map(iface->cfg->key_options,
1285 iface->cfg->value_options,
1286 iface->cfg->n_options, &args);
1287 error = netdev_set_config(iface->netdev, &args);
1288 shash_destroy(&args);
66da9bef 1289
bae7208e
EJ
1290 if (error) {
1291 VLOG_WARN("could not configure network device %s (%s)",
1292 iface->name, strerror(error));
1293 netdev_close(iface->netdev);
1294 iface->netdev = NULL;
1295 }
1296 }
66da9bef 1297
bae7208e
EJ
1298 /* Add the port, if necessary. */
1299 if (iface->netdev && iface->ofp_port < 0) {
1300 uint16_t new_ofp_port;
1301 int error;
8cea3c07 1302
bae7208e
EJ
1303 error = ofproto_port_add(br->ofproto, iface->netdev, &new_ofp_port);
1304 if (!error) {
1305 VLOG_INFO("bridge %s: added interface %s (%d)", br->name,
5167b870 1306 iface->name, new_ofp_port);
bae7208e
EJ
1307 iface_set_ofp_port(iface, new_ofp_port);
1308 } else {
1309 netdev_close(iface->netdev);
1310 iface->netdev = NULL;
66da9bef 1311 }
bae7208e 1312 }
c7e7bb21 1313
7f81a52a
BP
1314 /* Initially populate stats columns. */
1315 if (iface->netdev) {
bae7208e
EJ
1316 iface_refresh_stats(iface);
1317 iface_refresh_status(iface);
bae7208e
EJ
1318 }
1319
1320 /* Delete the iface if we failed. */
1321 if (iface->netdev && iface->ofp_port >= 0) {
1322 VLOG_DBG("bridge %s: interface %s is on port %d",
1323 br->name, iface->name, iface->ofp_port);
1324 } else {
8b5da7a6
BP
1325 struct ofproto_port ofproto_port;
1326
bae7208e
EJ
1327 if (iface->netdev) {
1328 VLOG_ERR("bridge %s: missing %s interface, dropping",
1329 br->name, iface->name);
1330 } else {
1331 /* We already reported a related error, don't bother
1332 * duplicating it. */
66da9bef 1333 }
bae7208e
EJ
1334 if (!ofproto_port_query_by_name(br->ofproto, port->name,
1335 &ofproto_port)) {
1336 VLOG_INFO("bridge %s: removed interface %s (%d)",
1337 br->name, port->name, ofproto_port.ofp_port);
1338 bridge_ofproto_port_del(br, ofproto_port);
1339 ofproto_port_destroy(&ofproto_port);
1340 }
1341 iface_clear_db_record(iface->cfg);
1342 iface_destroy(iface);
1343 }
66da9bef 1344
bae7208e
EJ
1345 if (list_is_empty(&port->ifaces)) {
1346 port_destroy(port);
1347 return;
1348 }
1349
1350 /* Add bond fake iface if necessary. */
1351 if (port_is_bond_fake_iface(port)) {
8b5da7a6
BP
1352 struct ofproto_port ofproto_port;
1353
bae7208e
EJ
1354 if (ofproto_port_query_by_name(br->ofproto, port->name,
1355 &ofproto_port)) {
1356 struct netdev *netdev;
1357 int error;
1358
1359 error = netdev_open(port->name, "internal", &netdev);
1360 if (!error) {
1361 ofproto_port_add(br->ofproto, netdev, NULL);
1362 netdev_close(netdev);
66da9bef 1363 } else {
bae7208e
EJ
1364 VLOG_WARN("could not open network device %s (%s)",
1365 port->name, strerror(error));
66da9bef 1366 }
bae7208e
EJ
1367 } else {
1368 /* Already exists, nothing to do. */
1369 ofproto_port_destroy(&ofproto_port);
66da9bef
BP
1370 }
1371 }
1372}
1373
084f5290
SH
1374/* Set Flow eviction threshold */
1375static void
1376bridge_configure_flow_eviction_threshold(struct bridge *br)
1377{
1378 const char *threshold_str;
1379 unsigned threshold;
1380
f67f1354
EJ
1381 threshold_str =
1382 ovsrec_bridge_get_other_config_value(br->cfg,
1383 "flow-eviction-threshold",
1384 NULL);
084f5290
SH
1385 if (threshold_str) {
1386 threshold = strtoul(threshold_str, NULL, 10);
1387 } else {
1388 threshold = OFPROTO_FLOW_EVICTON_THRESHOLD_DEFAULT;
1389 }
1390 ofproto_set_flow_eviction_threshold(br->ofproto, threshold);
1391}
1392
8402c74b
SS
1393/* Set forward BPDU option. */
1394static void
1395bridge_configure_forward_bpdu(struct bridge *br)
1396{
1397 const char *forward_bpdu_str;
1398 bool forward_bpdu = false;
1399
f67f1354
EJ
1400 forward_bpdu_str = ovsrec_bridge_get_other_config_value(br->cfg,
1401 "forward-bpdu",
1402 NULL);
8402c74b
SS
1403 if (forward_bpdu_str && !strcmp(forward_bpdu_str, "true")) {
1404 forward_bpdu = true;
1405 }
1406 ofproto_set_forward_bpdu(br->ofproto, forward_bpdu);
1407}
1408
e764773c
BP
1409/* Set MAC aging time for 'br'. */
1410static void
1411bridge_configure_mac_idle_time(struct bridge *br)
1412{
1413 const char *idle_time_str;
1414 int idle_time;
1415
f67f1354
EJ
1416 idle_time_str = ovsrec_bridge_get_other_config_value(br->cfg,
1417 "mac-aging-time",
1418 NULL);
e764773c
BP
1419 idle_time = (idle_time_str && atoi(idle_time_str)
1420 ? atoi(idle_time_str)
1421 : MAC_ENTRY_DEFAULT_IDLE_TIME);
1422 ofproto_set_mac_idle_time(br->ofproto, idle_time);
1423}
1424
064af421
BP
1425static void
1426bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
07c318f4 1427 struct iface **hw_addr_iface)
064af421 1428{
f145afdc 1429 struct hmapx mirror_output_ports;
093e47f4 1430 const char *hwaddr;
8052fb14 1431 struct port *port;
3a48ace3 1432 bool found_addr = false;
064af421 1433 int error;
f145afdc 1434 int i;
064af421 1435
07c318f4 1436 *hw_addr_iface = NULL;
064af421
BP
1437
1438 /* Did the user request a particular MAC? */
f67f1354 1439 hwaddr = ovsrec_bridge_get_other_config_value(br->cfg, "hwaddr", NULL);
093e47f4 1440 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
064af421
BP
1441 if (eth_addr_is_multicast(ea)) {
1442 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
1443 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1444 } else if (eth_addr_is_zero(ea)) {
1445 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
1446 } else {
1447 return;
1448 }
1449 }
1450
f145afdc
BP
1451 /* Mirror output ports don't participate in picking the local hardware
1452 * address. ofproto can't help us find out whether a given port is a
1453 * mirror output because we haven't configured mirrors yet, so we need to
1454 * accumulate them ourselves. */
1455 hmapx_init(&mirror_output_ports);
1456 for (i = 0; i < br->cfg->n_mirrors; i++) {
1457 struct ovsrec_mirror *m = br->cfg->mirrors[i];
1458 if (m->output_port) {
1459 hmapx_add(&mirror_output_ports, m->output_port);
1460 }
1461 }
1462
141f4942
BP
1463 /* Otherwise choose the minimum non-local MAC address among all of the
1464 * interfaces. */
8052fb14 1465 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
58b7527e 1466 uint8_t iface_ea[ETH_ADDR_LEN];
83db7968 1467 struct iface *candidate;
58b7527e
BP
1468 struct iface *iface;
1469
1470 /* Mirror output ports don't participate. */
f145afdc 1471 if (hmapx_contains(&mirror_output_ports, port->cfg)) {
064af421
BP
1472 continue;
1473 }
58b7527e
BP
1474
1475 /* Choose the MAC address to represent the port. */
83db7968 1476 iface = NULL;
76343538 1477 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
ba09980a
BP
1478 /* Find the interface with this Ethernet address (if any) so that
1479 * we can provide the correct devname to the caller. */
83db7968 1480 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
ba09980a 1481 uint8_t candidate_ea[ETH_ADDR_LEN];
8fef8c71 1482 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
ba09980a
BP
1483 && eth_addr_equals(iface_ea, candidate_ea)) {
1484 iface = candidate;
1485 }
1486 }
58b7527e
BP
1487 } else {
1488 /* Choose the interface whose MAC address will represent the port.
1489 * The Linux kernel bonding code always chooses the MAC address of
1490 * the first slave added to a bond, and the Fedora networking
1491 * scripts always add slaves to a bond in alphabetical order, so
1492 * for compatibility we choose the interface with the name that is
1493 * first in alphabetical order. */
83db7968
BP
1494 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1495 if (!iface || strcmp(candidate->name, iface->name) < 0) {
58b7527e
BP
1496 iface = candidate;
1497 }
1498 }
1499
1500 /* The local port doesn't count (since we're trying to choose its
141f4942 1501 * MAC address anyway). */
892815f5 1502 if (iface->ofp_port == OFPP_LOCAL) {
064af421
BP
1503 continue;
1504 }
58b7527e
BP
1505
1506 /* Grab MAC. */
07c318f4 1507 error = netdev_get_etheraddr(iface->netdev, iface_ea);
58b7527e 1508 if (error) {
58b7527e 1509 continue;
064af421
BP
1510 }
1511 }
58b7527e
BP
1512
1513 /* Compare against our current choice. */
1514 if (!eth_addr_is_multicast(iface_ea) &&
141f4942 1515 !eth_addr_is_local(iface_ea) &&
58b7527e
BP
1516 !eth_addr_is_reserved(iface_ea) &&
1517 !eth_addr_is_zero(iface_ea) &&
0d1fe4a3 1518 (!found_addr || eth_addr_compare_3way(iface_ea, ea) < 0))
58b7527e 1519 {
0babc06f 1520 memcpy(ea, iface_ea, ETH_ADDR_LEN);
8fef8c71 1521 *hw_addr_iface = iface;
3a48ace3 1522 found_addr = true;
58b7527e 1523 }
064af421 1524 }
3a48ace3
JP
1525 if (found_addr) {
1526 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1527 br->name, ETH_ADDR_ARGS(ea));
1528 } else {
7e041ba5 1529 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
064af421 1530 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
07c318f4 1531 *hw_addr_iface = NULL;
7e041ba5
EJ
1532 VLOG_WARN_RL(&rl, "bridge %s: using default bridge Ethernet "
1533 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
064af421 1534 }
f145afdc
BP
1535
1536 hmapx_destroy(&mirror_output_ports);
064af421
BP
1537}
1538
1539/* Choose and returns the datapath ID for bridge 'br' given that the bridge
1540 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
07c318f4
BP
1541 * an interface on 'br', then that interface must be passed in as
1542 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1543 * 'hw_addr_iface' must be passed in as a null pointer. */
064af421
BP
1544static uint64_t
1545bridge_pick_datapath_id(struct bridge *br,
1546 const uint8_t bridge_ea[ETH_ADDR_LEN],
07c318f4 1547 struct iface *hw_addr_iface)
064af421
BP
1548{
1549 /*
1550 * The procedure for choosing a bridge MAC address will, in the most
1551 * ordinary case, also choose a unique MAC that we can use as a datapath
1552 * ID. In some special cases, though, multiple bridges will end up with
1553 * the same MAC address. This is OK for the bridges, but it will confuse
1554 * the OpenFlow controller, because each datapath needs a unique datapath
1555 * ID.
1556 *
1557 * Datapath IDs must be unique. It is also very desirable that they be
1558 * stable from one run to the next, so that policy set on a datapath
1559 * "sticks".
1560 */
093e47f4 1561 const char *datapath_id;
064af421
BP
1562 uint64_t dpid;
1563
f67f1354
EJ
1564 datapath_id = ovsrec_bridge_get_other_config_value(br->cfg, "datapath-id",
1565 NULL);
093e47f4 1566 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
064af421
BP
1567 return dpid;
1568 }
1569
03eae5f8 1570 if (!hw_addr_iface) {
064af421
BP
1571 /*
1572 * A purely internal bridge, that is, one that has no non-virtual
03eae5f8 1573 * network devices on it at all, is difficult because it has no
064af421
BP
1574 * natural unique identifier at all.
1575 *
1576 * When the host is a XenServer, we handle this case by hashing the
1577 * host's UUID with the name of the bridge. Names of bridges are
1578 * persistent across XenServer reboots, although they can be reused if
1579 * an internal network is destroyed and then a new one is later
1580 * created, so this is fairly effective.
1581 *
1582 * When the host is not a XenServer, we punt by using a random MAC
1583 * address on each run.
1584 */
1585 const char *host_uuid = xenserver_get_host_uuid();
1586 if (host_uuid) {
1587 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1588 dpid = dpid_from_hash(combined, strlen(combined));
1589 free(combined);
1590 return dpid;
1591 }
1592 }
1593
1594 return eth_addr_to_uint64(bridge_ea);
1595}
1596
1597static uint64_t
1598dpid_from_hash(const void *data, size_t n)
1599{
5eccf359 1600 uint8_t hash[SHA1_DIGEST_SIZE];
064af421
BP
1601
1602 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
5eccf359 1603 sha1_bytes(data, n, hash);
064af421
BP
1604 eth_addr_mark_random(hash);
1605 return eth_addr_to_uint64(hash);
1606}
1607
ea83a2fc 1608static void
ea763e0e 1609iface_refresh_status(struct iface *iface)
ea83a2fc 1610{
ea763e0e
EJ
1611 struct shash sh;
1612
6c038611 1613 enum netdev_features current;
e210037e 1614 enum netdev_flags flags;
e210037e
AE
1615 int64_t bps;
1616 int mtu;
1617 int64_t mtu_64;
1618 int error;
1619
cfea354b
BP
1620 if (iface_is_synthetic(iface)) {
1621 return;
1622 }
1623
ea763e0e
EJ
1624 shash_init(&sh);
1625
2c2ea5a8 1626 if (!netdev_get_drv_info(iface->netdev, &sh)) {
ea763e0e
EJ
1627 size_t n;
1628 char **keys, **values;
ea83a2fc 1629
ea763e0e
EJ
1630 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1631 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1632
1633 free(keys);
1634 free(values);
1635 } else {
1636 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1637 }
1638
1639 shash_destroy_free_data(&sh);
e210037e
AE
1640
1641 error = netdev_get_flags(iface->netdev, &flags);
1642 if (!error) {
0acaddd0
EJ
1643 ovsrec_interface_set_admin_state(iface->cfg,
1644 flags & NETDEV_UP ? "up" : "down");
e210037e
AE
1645 }
1646 else {
1647 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1648 }
1649
1650 error = netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
1651 if (!error) {
1652 ovsrec_interface_set_duplex(iface->cfg,
1653 netdev_features_is_full_duplex(current)
1654 ? "full" : "half");
1655 /* warning: uint64_t -> int64_t conversion */
1656 bps = netdev_features_to_bps(current);
1657 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1658 }
1659 else {
1660 ovsrec_interface_set_duplex(iface->cfg, NULL);
1661 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1662 }
1663
e210037e 1664 error = netdev_get_mtu(iface->netdev, &mtu);
9b020780 1665 if (!error) {
e210037e
AE
1666 mtu_64 = mtu;
1667 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1668 }
1669 else {
1670 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1671 }
ea83a2fc
EJ
1672}
1673
0dfd1cb4 1674/* Writes 'iface''s CFM statistics to the database. */
8f3fe844 1675static void
b31bcf60
EJ
1676iface_refresh_cfm_stats(struct iface *iface)
1677{
93b8df38 1678 const struct ovsrec_interface *cfg = iface->cfg;
1de11730
EJ
1679 int fault, error;
1680 const uint64_t *rmps;
1681 size_t n_rmps;
3967a833 1682 int health;
b31bcf60 1683
21d48660
EJ
1684 if (iface_is_synthetic(iface)) {
1685 return;
1686 }
1687
a5610457
EJ
1688 fault = ofproto_port_get_cfm_fault(iface->port->bridge->ofproto,
1689 iface->ofp_port);
8f3fe844 1690 if (fault >= 0) {
b9380396 1691 const char *reasons[CFM_FAULT_N_REASONS];
a5610457 1692 bool fault_bool = fault;
b9380396
EJ
1693 size_t i, j;
1694
1695 j = 0;
1696 for (i = 0; i < CFM_FAULT_N_REASONS; i++) {
1697 int reason = 1 << i;
1698 if (fault & reason) {
1699 reasons[j++] = cfm_fault_reason_to_str(reason);
1700 }
1701 }
1702
a5610457 1703 ovsrec_interface_set_cfm_fault(cfg, &fault_bool, 1);
b9380396 1704 ovsrec_interface_set_cfm_fault_status(cfg, (char **) reasons, j);
0f0d2ae5
EJ
1705 } else {
1706 ovsrec_interface_set_cfm_fault(cfg, NULL, 0);
b9380396 1707 ovsrec_interface_set_cfm_fault_status(cfg, NULL, 0);
6586adf5 1708 }
1de11730
EJ
1709
1710 error = ofproto_port_get_cfm_remote_mpids(iface->port->bridge->ofproto,
1711 iface->ofp_port, &rmps, &n_rmps);
1712 if (error >= 0) {
1713 ovsrec_interface_set_cfm_remote_mpids(cfg, (const int64_t *)rmps,
1714 n_rmps);
1715 } else {
1716 ovsrec_interface_set_cfm_remote_mpids(cfg, NULL, 0);
1717 }
3967a833
MM
1718
1719 health = ofproto_port_get_cfm_health(iface->port->bridge->ofproto,
1720 iface->ofp_port);
1721 if (health >= 0) {
1722 int64_t cfm_health = health;
1723 ovsrec_interface_set_cfm_health(cfg, &cfm_health, 1);
1724 } else {
1725 ovsrec_interface_set_cfm_health(cfg, NULL, 0);
1726 }
b31bcf60
EJ
1727}
1728
018f1525
BP
1729static void
1730iface_refresh_stats(struct iface *iface)
1731{
98dbe2dd
BP
1732#define IFACE_STATS \
1733 IFACE_STAT(rx_packets, "rx_packets") \
1734 IFACE_STAT(tx_packets, "tx_packets") \
1735 IFACE_STAT(rx_bytes, "rx_bytes") \
1736 IFACE_STAT(tx_bytes, "tx_bytes") \
1737 IFACE_STAT(rx_dropped, "rx_dropped") \
1738 IFACE_STAT(tx_dropped, "tx_dropped") \
1739 IFACE_STAT(rx_errors, "rx_errors") \
1740 IFACE_STAT(tx_errors, "tx_errors") \
1741 IFACE_STAT(rx_frame_errors, "rx_frame_err") \
1742 IFACE_STAT(rx_over_errors, "rx_over_err") \
1743 IFACE_STAT(rx_crc_errors, "rx_crc_err") \
1744 IFACE_STAT(collisions, "collisions")
1745
1746#define IFACE_STAT(MEMBER, NAME) NAME,
1747 static char *keys[] = { IFACE_STATS };
1748#undef IFACE_STAT
1749 int64_t values[ARRAY_SIZE(keys)];
1750 int i;
018f1525
BP
1751
1752 struct netdev_stats stats;
1753
cfea354b
BP
1754 if (iface_is_synthetic(iface)) {
1755 return;
1756 }
1757
018f1525
BP
1758 /* Intentionally ignore return value, since errors will set 'stats' to
1759 * all-1s, and we will deal with that correctly below. */
1760 netdev_get_stats(iface->netdev, &stats);
1761
98dbe2dd
BP
1762 /* Copy statistics into values[] array. */
1763 i = 0;
1764#define IFACE_STAT(MEMBER, NAME) values[i++] = stats.MEMBER;
1765 IFACE_STATS;
1766#undef IFACE_STAT
1767 assert(i == ARRAY_SIZE(keys));
018f1525 1768
0acaddd0
EJ
1769 ovsrec_interface_set_statistics(iface->cfg, keys, values,
1770 ARRAY_SIZE(keys));
98dbe2dd 1771#undef IFACE_STATS
018f1525
BP
1772}
1773
21f7563c
JP
1774static void
1775br_refresh_stp_status(struct bridge *br)
1776{
1777 struct ofproto *ofproto = br->ofproto;
1778 struct ofproto_stp_status status;
1779 char *keys[3], *values[3];
1780 size_t i;
1781
1782 if (ofproto_get_stp_status(ofproto, &status)) {
1783 return;
1784 }
1785
1786 if (!status.enabled) {
1787 ovsrec_bridge_set_status(br->cfg, NULL, NULL, 0);
1788 return;
1789 }
1790
1791 keys[0] = "stp_bridge_id",
1792 values[0] = xasprintf(STP_ID_FMT, STP_ID_ARGS(status.bridge_id));
1793 keys[1] = "stp_designated_root",
1794 values[1] = xasprintf(STP_ID_FMT, STP_ID_ARGS(status.designated_root));
1795 keys[2] = "stp_root_path_cost",
1796 values[2] = xasprintf("%d", status.root_path_cost);
1797
1798 ovsrec_bridge_set_status(br->cfg, keys, values, ARRAY_SIZE(values));
1799
1800 for (i = 0; i < ARRAY_SIZE(values); i++) {
1801 free(values[i]);
1802 }
1803}
1804
1805static void
1806port_refresh_stp_status(struct port *port)
1807{
1808 struct ofproto *ofproto = port->bridge->ofproto;
1809 struct iface *iface;
1810 struct ofproto_port_stp_status status;
80740385
JP
1811 char *keys[4];
1812 char *str_values[4];
1813 int64_t int_values[3];
21f7563c
JP
1814 size_t i;
1815
06b592bc
EJ
1816 if (port_is_synthetic(port)) {
1817 return;
1818 }
1819
21f7563c
JP
1820 /* STP doesn't currently support bonds. */
1821 if (!list_is_singleton(&port->ifaces)) {
1822 ovsrec_port_set_status(port->cfg, NULL, NULL, 0);
1823 return;
1824 }
1825
1826 iface = CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
1827
1828 if (ofproto_port_get_stp_status(ofproto, iface->ofp_port, &status)) {
1829 return;
1830 }
1831
1832 if (!status.enabled) {
1833 ovsrec_port_set_status(port->cfg, NULL, NULL, 0);
80740385 1834 ovsrec_port_set_statistics(port->cfg, NULL, NULL, 0);
21f7563c
JP
1835 return;
1836 }
1837
80740385
JP
1838 /* Set Status column. */
1839 keys[0] = "stp_port_id";
1840 str_values[0] = xasprintf(STP_PORT_ID_FMT, status.port_id);
21f7563c 1841 keys[1] = "stp_state";
80740385 1842 str_values[1] = xstrdup(stp_state_name(status.state));
21f7563c 1843 keys[2] = "stp_sec_in_state";
80740385 1844 str_values[2] = xasprintf("%u", status.sec_in_state);
21f7563c 1845 keys[3] = "stp_role";
80740385 1846 str_values[3] = xstrdup(stp_role_name(status.role));
21f7563c 1847
80740385
JP
1848 ovsrec_port_set_status(port->cfg, keys, str_values,
1849 ARRAY_SIZE(str_values));
21f7563c 1850
80740385
JP
1851 for (i = 0; i < ARRAY_SIZE(str_values); i++) {
1852 free(str_values[i]);
21f7563c 1853 }
80740385
JP
1854
1855 /* Set Statistics column. */
1856 keys[0] = "stp_tx_count";
1857 int_values[0] = status.tx_count;
1858 keys[1] = "stp_rx_count";
1859 int_values[1] = status.rx_count;
1860 keys[2] = "stp_error_count";
1861 int_values[2] = status.error_count;
1862
1863 ovsrec_port_set_statistics(port->cfg, keys, int_values,
1864 ARRAY_SIZE(int_values));
21f7563c
JP
1865}
1866
3fe80505
BP
1867static bool
1868enable_system_stats(const struct ovsrec_open_vswitch *cfg)
1869{
1870 const char *enable;
1871
1872 /* Use other-config:enable-system-stats by preference. */
f67f1354
EJ
1873 enable = ovsrec_open_vswitch_get_other_config_value(cfg,
1874 "enable-statistics",
1875 NULL);
3fe80505
BP
1876 if (enable) {
1877 return !strcmp(enable, "true");
1878 }
1879
1880 /* Disable by default. */
1881 return false;
1882}
1883
ce887677
BP
1884static void
1885refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1886{
1887 struct ovsdb_datum datum;
1888 struct shash stats;
1889
1890 shash_init(&stats);
3fe80505
BP
1891 if (enable_system_stats(cfg)) {
1892 get_system_stats(&stats);
1893 }
ce887677
BP
1894
1895 ovsdb_datum_from_shash(&datum, &stats);
1896 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1897 &datum);
1898}
1899
bffc0589
AE
1900static inline const char *
1901nx_role_to_str(enum nx_role role)
1902{
1903 switch (role) {
1904 case NX_ROLE_OTHER:
1905 return "other";
1906 case NX_ROLE_MASTER:
1907 return "master";
1908 case NX_ROLE_SLAVE:
1909 return "slave";
1910 default:
1911 return "*** INVALID ROLE ***";
1912 }
1913}
1914
1915static void
5d279086 1916refresh_controller_status(void)
bffc0589 1917{
5d279086 1918 struct bridge *br;
bffc0589
AE
1919 struct shash info;
1920 const struct ovsrec_controller *cfg;
1921
5d279086
AE
1922 shash_init(&info);
1923
1924 /* Accumulate status for controllers on all bridges. */
1925 HMAP_FOR_EACH (br, node, &all_bridges) {
1926 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1927 }
bffc0589 1928
5d279086 1929 /* Update each controller in the database with current status. */
bffc0589 1930 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
eb9b8307
AE
1931 struct ofproto_controller_info *cinfo =
1932 shash_find_data(&info, cfg->target);
1933
1934 if (cinfo) {
1935 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1936 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1937 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1938 (char **) cinfo->pairs.values,
1939 cinfo->pairs.n);
1940 } else {
1941 ovsrec_controller_set_is_connected(cfg, false);
1942 ovsrec_controller_set_role(cfg, NULL);
1943 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1944 }
bffc0589
AE
1945 }
1946
1947 ofproto_free_ofproto_controller_info(&info);
1948}
1949
8f3fe844
EJ
1950static void
1951refresh_cfm_stats(void)
1952{
1953 static struct ovsdb_idl_txn *txn = NULL;
1954
1955 if (!txn) {
1956 struct bridge *br;
1957
1958 txn = ovsdb_idl_txn_create(idl);
1959
1960 HMAP_FOR_EACH (br, node, &all_bridges) {
1961 struct iface *iface;
1962
1963 HMAP_FOR_EACH (iface, name_node, &br->iface_by_name) {
1964 iface_refresh_cfm_stats(iface);
1965 }
1966 }
1967 }
1968
1969 if (ovsdb_idl_txn_commit(txn) != TXN_INCOMPLETE) {
1970 ovsdb_idl_txn_destroy(txn);
1971 txn = NULL;
1972 }
1973}
1974
5fcc0d00
BP
1975/* Performs periodic activity required by bridges that needs to be done with
1976 * the least possible latency.
1977 *
1978 * It makes sense to call this function a couple of times per poll loop, to
1979 * provide a significant performance boost on some benchmarks with ofprotos
1980 * that use the ofproto-dpif implementation. */
1981void
1982bridge_run_fast(void)
1983{
1984 struct bridge *br;
1985
1986 HMAP_FOR_EACH (br, node, &all_bridges) {
1987 ofproto_run_fast(br->ofproto);
1988 }
1989}
1990
c5187f17 1991void
064af421
BP
1992bridge_run(void)
1993{
bae7208e 1994 static const struct ovsrec_open_vswitch null_cfg;
d54ff998 1995 const struct ovsrec_open_vswitch *cfg;
bae7208e 1996 struct ovsdb_idl_txn *reconf_txn = NULL;
d54ff998 1997
52a90c29 1998 bool vlan_splinters_changed;
c5187f17 1999 struct bridge *br;
064af421 2000
06b6d651 2001 /* (Re)configure if necessary. */
bae7208e
EJ
2002 if (!reconfiguring) {
2003 ovsdb_idl_run(idl);
06b6d651 2004
bae7208e
EJ
2005 if (ovsdb_idl_is_lock_contended(idl)) {
2006 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2007 struct bridge *br, *next_br;
06b6d651 2008
bae7208e
EJ
2009 VLOG_ERR_RL(&rl, "another ovs-vswitchd process is running, "
2010 "disabling this process until it goes away");
2011
2012 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
2013 bridge_destroy(br);
2014 }
2015 return;
2016 } else if (!ovsdb_idl_has_lock(idl)) {
2017 return;
06b6d651 2018 }
06b6d651
BP
2019 }
2020 cfg = ovsrec_open_vswitch_first(idl);
2021
c5187f17 2022 /* Let each bridge do the work that it needs to do. */
764072fd 2023 HMAP_FOR_EACH (br, node, &all_bridges) {
5fcc0d00 2024 ofproto_run(br->ofproto);
c5187f17
BP
2025 }
2026
d6da96ce
BP
2027 /* Re-configure SSL. We do this on every trip through the main loop,
2028 * instead of just when the database changes, because the contents of the
2029 * key and certificate files can change without the database changing.
2030 *
2031 * We do this before bridge_reconfigure() because that function might
2032 * initiate SSL connections and thus requires SSL to be configured. */
2033 if (cfg && cfg->ssl) {
2034 const struct ovsrec_ssl *ssl = cfg->ssl;
2035
2036 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
2037 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
2038 }
bf8f2167 2039
bae7208e
EJ
2040 if (!reconfiguring) {
2041 /* If VLAN splinters are in use, then we need to reconfigure if VLAN
2042 * usage has changed. */
2043 vlan_splinters_changed = false;
2044 if (vlan_splinters_enabled_anywhere) {
2045 HMAP_FOR_EACH (br, node, &all_bridges) {
2046 if (ofproto_has_vlan_usage_changed(br->ofproto)) {
2047 vlan_splinters_changed = true;
2048 break;
2049 }
2050 }
2051 }
2052
2053 if (ovsdb_idl_get_seqno(idl) != idl_seqno || vlan_splinters_changed) {
2054 idl_seqno = ovsdb_idl_get_seqno(idl);
2055 if (cfg) {
2056 reconf_txn = ovsdb_idl_txn_create(idl);
2057 bridge_reconfigure(cfg);
2058 } else {
2059 /* We still need to reconfigure to avoid dangling pointers to
2060 * now-destroyed ovsrec structures inside bridge data. */
2061 bridge_reconfigure(&null_cfg);
52a90c29
BP
2062 }
2063 }
2064 }
2065
bae7208e 2066 if (reconfiguring) {
c5187f17 2067 if (cfg) {
bae7208e
EJ
2068 if (!reconf_txn) {
2069 reconf_txn = ovsdb_idl_txn_create(idl);
2070 }
2071 if (bridge_reconfigure_continue(cfg)) {
2072 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
2073 }
1e0b752d 2074 } else {
bae7208e 2075 bridge_reconfigure_continue(&null_cfg);
064af421
BP
2076 }
2077 }
018f1525 2078
bae7208e
EJ
2079 if (reconf_txn) {
2080 ovsdb_idl_txn_commit(reconf_txn);
2081 ovsdb_idl_txn_destroy(reconf_txn);
2082 reconf_txn = NULL;
2083 }
2084
cd0cd65f
BP
2085 /* Refresh system and interface stats if necessary. */
2086 if (time_msec() >= stats_timer) {
ce887677
BP
2087 if (cfg) {
2088 struct ovsdb_idl_txn *txn;
018f1525 2089
ce887677 2090 txn = ovsdb_idl_txn_create(idl);
764072fd 2091 HMAP_FOR_EACH (br, node, &all_bridges) {
8052fb14 2092 struct port *port;
9d24de3b 2093 struct mirror *m;
018f1525 2094
8052fb14 2095 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
83db7968 2096 struct iface *iface;
018f1525 2097
83db7968 2098 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
ce887677 2099 iface_refresh_stats(iface);
ea763e0e 2100 iface_refresh_status(iface);
ce887677 2101 }
018f1525 2102 }
9d24de3b
JP
2103
2104 HMAP_FOR_EACH (m, hmap_node, &br->mirrors) {
2105 mirror_refresh_stats(m);
2106 }
2107
018f1525 2108 }
ce887677 2109 refresh_system_stats(cfg);
5d279086 2110 refresh_controller_status();
ce887677
BP
2111 ovsdb_idl_txn_commit(txn);
2112 ovsdb_idl_txn_destroy(txn); /* XXX */
018f1525 2113 }
018f1525 2114
cd0cd65f 2115 stats_timer = time_msec() + STATS_INTERVAL;
018f1525 2116 }
6586adf5 2117
815cd583 2118 if (time_msec() >= db_limiter) {
6586adf5 2119 struct ovsdb_idl_txn *txn;
6586adf5
EJ
2120
2121 txn = ovsdb_idl_txn_create(idl);
764072fd 2122 HMAP_FOR_EACH (br, node, &all_bridges) {
15236eb4 2123 struct iface *iface;
21f7563c
JP
2124 struct port *port;
2125
2126 br_refresh_stp_status(br);
2127
2128 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2129 port_refresh_stp_status(port);
2130 }
15236eb4
EJ
2131
2132 HMAP_FOR_EACH (iface, name_node, &br->iface_by_name) {
70aa337d 2133 const char *link_state;
65c3058c 2134 int64_t link_resets;
15236eb4 2135 int current;
6586adf5 2136
15236eb4
EJ
2137 if (iface_is_synthetic(iface)) {
2138 continue;
2139 }
6586adf5 2140
15236eb4
EJ
2141 current = ofproto_port_is_lacp_current(br->ofproto,
2142 iface->ofp_port);
2143 if (current >= 0) {
2144 bool bl = current;
2145 ovsrec_interface_set_lacp_current(iface->cfg, &bl, 1);
2146 } else {
2147 ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
6586adf5 2148 }
70aa337d
EJ
2149
2150 link_state = netdev_get_carrier(iface->netdev) ? "up" : "down";
2151 ovsrec_interface_set_link_state(iface->cfg, link_state);
65c3058c
EJ
2152
2153 link_resets = netdev_get_carrier_resets(iface->netdev);
2154 ovsrec_interface_set_link_resets(iface->cfg, &link_resets, 1);
6586adf5
EJ
2155 }
2156 }
2157
15236eb4 2158 if (ovsdb_idl_txn_commit(txn) != TXN_UNCHANGED) {
815cd583 2159 db_limiter = time_msec() + DB_LIMIT_INTERVAL;
6586adf5 2160 }
6586adf5
EJ
2161 ovsdb_idl_txn_destroy(txn);
2162 }
8f3fe844
EJ
2163
2164 refresh_cfm_stats();
064af421
BP
2165}
2166
2167void
2168bridge_wait(void)
2169{
c5187f17 2170 ovsdb_idl_wait(idl);
c7e7bb21 2171
bae7208e 2172 if (reconfiguring) {
c7e7bb21
EJ
2173 poll_immediate_wake();
2174 }
2175
06b6d651
BP
2176 if (!hmap_is_empty(&all_bridges)) {
2177 struct bridge *br;
6586adf5 2178
06b6d651
BP
2179 HMAP_FOR_EACH (br, node, &all_bridges) {
2180 ofproto_wait(br->ofproto);
2181 }
2182 poll_timer_wait_until(stats_timer);
2183
2184 if (db_limiter > time_msec()) {
2185 poll_timer_wait_until(db_limiter);
2186 }
6586adf5 2187 }
064af421 2188}
8c4c1387 2189\f
e8fe3026
EJ
2190/* QoS unixctl user interface functions. */
2191
2192struct qos_unixctl_show_cbdata {
2193 struct ds *ds;
2194 struct iface *iface;
2195};
2196
2197static void
2198qos_unixctl_show_cb(unsigned int queue_id,
2199 const struct shash *details,
2200 void *aux)
2201{
2202 struct qos_unixctl_show_cbdata *data = aux;
2203 struct ds *ds = data->ds;
2204 struct iface *iface = data->iface;
2205 struct netdev_queue_stats stats;
2206 struct shash_node *node;
2207 int error;
2208
2209 ds_put_cstr(ds, "\n");
2210 if (queue_id) {
2211 ds_put_format(ds, "Queue %u:\n", queue_id);
2212 } else {
2213 ds_put_cstr(ds, "Default:\n");
2214 }
2215
2216 SHASH_FOR_EACH (node, details) {
2217 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
2218 }
2219
2220 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
2221 if (!error) {
2222 if (stats.tx_packets != UINT64_MAX) {
2223 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
2224 }
2225
2226 if (stats.tx_bytes != UINT64_MAX) {
2227 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
2228 }
2229
2230 if (stats.tx_errors != UINT64_MAX) {
2231 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
2232 }
2233 } else {
2234 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
2235 queue_id, strerror(error));
2236 }
2237}
2238
2239static void
0e15264f
BP
2240qos_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
2241 const char *argv[], void *aux OVS_UNUSED)
e8fe3026
EJ
2242{
2243 struct ds ds = DS_EMPTY_INITIALIZER;
2244 struct shash sh = SHASH_INITIALIZER(&sh);
2245 struct iface *iface;
2246 const char *type;
2247 struct shash_node *node;
2248 struct qos_unixctl_show_cbdata data;
2249 int error;
2250
0e15264f 2251 iface = iface_find(argv[1]);
e8fe3026 2252 if (!iface) {
bde9f75d 2253 unixctl_command_reply_error(conn, "no such interface");
e8fe3026
EJ
2254 return;
2255 }
2256
2257 netdev_get_qos(iface->netdev, &type, &sh);
2258
2259 if (*type != '\0') {
2260 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
2261
2262 SHASH_FOR_EACH (node, &sh) {
2263 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
2264 }
2265
2266 data.ds = &ds;
2267 data.iface = iface;
2268 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
2269
2270 if (error) {
2271 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
2272 }
bde9f75d 2273 unixctl_command_reply(conn, ds_cstr(&ds));
e8fe3026
EJ
2274 } else {
2275 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
bde9f75d 2276 unixctl_command_reply_error(conn, ds_cstr(&ds));
e8fe3026
EJ
2277 }
2278
2279 shash_destroy_free_data(&sh);
2280 ds_destroy(&ds);
2281}
2282\f
064af421 2283/* Bridge reconfiguration functions. */
66da9bef 2284static void
1a6f1e2a 2285bridge_create(const struct ovsrec_bridge *br_cfg)
064af421
BP
2286{
2287 struct bridge *br;
064af421 2288
1a6f1e2a 2289 assert(!bridge_lookup(br_cfg->name));
ec6fde61 2290 br = xzalloc(sizeof *br);
064af421 2291
1a6f1e2a 2292 br->name = xstrdup(br_cfg->name);
f79e673f 2293 br->type = xstrdup(ofproto_normalize_type(br_cfg->datapath_type));
1a6f1e2a 2294 br->cfg = br_cfg;
e3f55cb8
BP
2295
2296 /* Derive the default Ethernet address from the bridge's UUID. This should
2297 * be unique and it will be stable between ovs-vswitchd runs. */
2298 memcpy(br->default_ea, &br_cfg->header_.uuid, ETH_ADDR_LEN);
2299 eth_addr_mark_random(br->default_ea);
064af421 2300
8052fb14 2301 hmap_init(&br->ports);
d9a8717a 2302 hmap_init(&br->ifaces);
ebea37cc 2303 hmap_init(&br->iface_by_name);
fa066f01 2304 hmap_init(&br->mirrors);
4a1ee6ae 2305
bae7208e
EJ
2306 hmap_init(&br->if_cfg_todo);
2307 list_init(&br->ofpp_garbage);
2308
764072fd 2309 hmap_insert(&all_bridges, &br->node, hash_string(br->name, 0));
064af421
BP
2310}
2311
2312static void
2313bridge_destroy(struct bridge *br)
2314{
2315 if (br) {
fa066f01
BP
2316 struct mirror *mirror, *next_mirror;
2317 struct port *port, *next_port;
bae7208e
EJ
2318 struct if_cfg *if_cfg, *next_if_cfg;
2319 struct ofpp_garbage *garbage, *next_garbage;
064af421 2320
fa066f01 2321 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
8052fb14 2322 port_destroy(port);
064af421 2323 }
fa066f01
BP
2324 HMAP_FOR_EACH_SAFE (mirror, next_mirror, hmap_node, &br->mirrors) {
2325 mirror_destroy(mirror);
f76e2dfc 2326 }
bae7208e
EJ
2327 HMAP_FOR_EACH_SAFE (if_cfg, next_if_cfg, hmap_node, &br->if_cfg_todo) {
2328 hmap_remove(&br->if_cfg_todo, &if_cfg->hmap_node);
2329 free(if_cfg);
2330 }
2331 LIST_FOR_EACH_SAFE (garbage, next_garbage, list_node,
2332 &br->ofpp_garbage) {
2333 list_remove(&garbage->list_node);
2334 free(garbage);
2335 }
2336
764072fd 2337 hmap_remove(&all_bridges, &br->node);
6d6ab93e 2338 ofproto_destroy(br->ofproto);
d9a8717a 2339 hmap_destroy(&br->ifaces);
8052fb14 2340 hmap_destroy(&br->ports);
ebea37cc 2341 hmap_destroy(&br->iface_by_name);
fa066f01 2342 hmap_destroy(&br->mirrors);
bae7208e 2343 hmap_destroy(&br->if_cfg_todo);
064af421 2344 free(br->name);
66da9bef 2345 free(br->type);
064af421
BP
2346 free(br);
2347 }
2348}
2349
2350static struct bridge *
2351bridge_lookup(const char *name)
2352{
2353 struct bridge *br;
2354
764072fd 2355 HMAP_FOR_EACH_WITH_HASH (br, node, hash_string(name, 0), &all_bridges) {
064af421
BP
2356 if (!strcmp(br->name, name)) {
2357 return br;
2358 }
2359 }
2360 return NULL;
2361}
2362
4f2cad2c
JP
2363/* Handle requests for a listing of all flows known by the OpenFlow
2364 * stack, including those normally hidden. */
2365static void
0e15264f
BP
2366bridge_unixctl_dump_flows(struct unixctl_conn *conn, int argc OVS_UNUSED,
2367 const char *argv[], void *aux OVS_UNUSED)
4f2cad2c
JP
2368{
2369 struct bridge *br;
2370 struct ds results;
d295e8e9 2371
0e15264f 2372 br = bridge_lookup(argv[1]);
4f2cad2c 2373 if (!br) {
bde9f75d 2374 unixctl_command_reply_error(conn, "Unknown bridge");
4f2cad2c
JP
2375 return;
2376 }
2377
2378 ds_init(&results);
2379 ofproto_get_all_flows(br->ofproto, &results);
2380
bde9f75d 2381 unixctl_command_reply(conn, ds_cstr(&results));
4f2cad2c
JP
2382 ds_destroy(&results);
2383}
2384
fa05809b
BP
2385/* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
2386 * connections and reconnect. If BRIDGE is not specified, then all bridges
2387 * drop their controller connections and reconnect. */
2388static void
0e15264f
BP
2389bridge_unixctl_reconnect(struct unixctl_conn *conn, int argc,
2390 const char *argv[], void *aux OVS_UNUSED)
fa05809b
BP
2391{
2392 struct bridge *br;
0e15264f
BP
2393 if (argc > 1) {
2394 br = bridge_lookup(argv[1]);
fa05809b 2395 if (!br) {
bde9f75d 2396 unixctl_command_reply_error(conn, "Unknown bridge");
fa05809b
BP
2397 return;
2398 }
2399 ofproto_reconnect_controllers(br->ofproto);
2400 } else {
764072fd 2401 HMAP_FOR_EACH (br, node, &all_bridges) {
fa05809b
BP
2402 ofproto_reconnect_controllers(br->ofproto);
2403 }
2404 }
bde9f75d 2405 unixctl_command_reply(conn, NULL);
fa05809b
BP
2406}
2407
76ce9432 2408static size_t
1a048029 2409bridge_get_controllers(const struct bridge *br,
76ce9432 2410 struct ovsrec_controller ***controllersp)
064af421 2411{
76ce9432
BP
2412 struct ovsrec_controller **controllers;
2413 size_t n_controllers;
064af421 2414
1a048029
JP
2415 controllers = br->cfg->controller;
2416 n_controllers = br->cfg->n_controller;
76343538 2417
76ce9432
BP
2418 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
2419 controllers = NULL;
2420 n_controllers = 0;
064af421 2421 }
76343538 2422
76ce9432
BP
2423 if (controllersp) {
2424 *controllersp = controllers;
2425 }
2426 return n_controllers;
064af421
BP
2427}
2428
bae7208e
EJ
2429static void
2430bridge_queue_if_cfg(struct bridge *br,
2431 const struct ovsrec_interface *cfg,
2432 const struct ovsrec_port *parent)
2433{
2434 struct if_cfg *if_cfg = xmalloc(sizeof *if_cfg);
2435
2436 if_cfg->cfg = cfg;
2437 if_cfg->parent = parent;
2438 hmap_insert(&br->if_cfg_todo, &if_cfg->hmap_node,
2439 hash_string(if_cfg->cfg->name, 0));
2440}
2441
2442/* Deletes "struct port"s and "struct iface"s under 'br' which aren't
2443 * consistent with 'br->cfg'. Updates 'br->if_cfg_queue' with interfaces which
2444 * 'br' needs to complete its configuration. */
064af421 2445static void
52a90c29
BP
2446bridge_add_del_ports(struct bridge *br,
2447 const unsigned long int *splinter_vlans)
064af421 2448{
bae7208e 2449 struct shash_node *port_node;
8052fb14 2450 struct port *port, *next;
8052fb14 2451 struct shash new_ports;
6ae39834 2452 size_t i;
064af421 2453
bae7208e
EJ
2454 assert(hmap_is_empty(&br->if_cfg_todo));
2455
064af421 2456 /* Collect new ports. */
76343538
BP
2457 shash_init(&new_ports);
2458 for (i = 0; i < br->cfg->n_ports; i++) {
2459 const char *name = br->cfg->ports[i]->name;
2460 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
2461 VLOG_WARN("bridge %s: %s specified twice as bridge port",
2462 br->name, name);
2463 }
2464 }
b5827b24
BP
2465 if (bridge_get_controllers(br, NULL)
2466 && !shash_find(&new_ports, br->name)) {
cfea354b
BP
2467 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
2468 br->name, br->name);
72865317 2469
cfea354b
BP
2470 br->synth_local_port.interfaces = &br->synth_local_ifacep;
2471 br->synth_local_port.n_interfaces = 1;
2472 br->synth_local_port.name = br->name;
2473
2474 br->synth_local_iface.name = br->name;
66da9bef 2475 br->synth_local_iface.type = "internal";
cfea354b
BP
2476
2477 br->synth_local_ifacep = &br->synth_local_iface;
2478
2479 shash_add(&new_ports, br->name, &br->synth_local_port);
064af421 2480 }
064af421 2481
52a90c29
BP
2482 if (splinter_vlans) {
2483 add_vlan_splinter_ports(br, splinter_vlans, &new_ports);
2484 }
2485
4a1ee6ae 2486 /* Get rid of deleted ports.
a70e4b2a 2487 * Get rid of deleted interfaces on ports that still exist. */
8052fb14 2488 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
66da9bef
BP
2489 port->cfg = shash_find_data(&new_ports, port->name);
2490 if (!port->cfg) {
4a1ee6ae
BP
2491 port_destroy(port);
2492 } else {
66da9bef 2493 port_del_ifaces(port);
064af421
BP
2494 }
2495 }
4a1ee6ae 2496
bae7208e
EJ
2497 SHASH_FOR_EACH (port_node, &new_ports) {
2498 const struct ovsrec_port *port = port_node->data;
2499 size_t i;
2500
2501 for (i = 0; i < port->n_interfaces; i++) {
2502 const struct ovsrec_interface *cfg = port->interfaces[i];
2503 struct iface *iface = iface_lookup(br, cfg->name);
2504
2505 if (iface) {
2506 iface->cfg = cfg;
2507 iface_refresh_type(iface);
2508 } else {
2509 bridge_queue_if_cfg(br, cfg, port);
2510 }
ceb4559f 2511 }
064af421 2512 }
bae7208e 2513
76343538 2514 shash_destroy(&new_ports);
064af421
BP
2515}
2516
7d674866
BP
2517/* Initializes 'oc' appropriately as a management service controller for
2518 * 'br'.
2519 *
2520 * The caller must free oc->target when it is no longer needed. */
2521static void
2522bridge_ofproto_controller_for_mgmt(const struct bridge *br,
2523 struct ofproto_controller *oc)
2524{
b43c6fe2 2525 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
7d674866
BP
2526 oc->max_backoff = 0;
2527 oc->probe_interval = 60;
2528 oc->band = OFPROTO_OUT_OF_BAND;
7d674866
BP
2529 oc->rate_limit = 0;
2530 oc->burst_limit = 0;
9886b662 2531 oc->enable_async_msgs = true;
7d674866
BP
2532}
2533
2534/* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
2535static void
2536bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
2537 struct ofproto_controller *oc)
2538{
f125905c
MM
2539 const char *config_str;
2540
7d674866
BP
2541 oc->target = c->target;
2542 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
2543 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
2544 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
2545 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
7d674866
BP
2546 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
2547 oc->burst_limit = (c->controller_burst_limit
2548 ? *c->controller_burst_limit : 0);
9886b662
BP
2549 oc->enable_async_msgs = (!c->enable_async_messages
2550 || *c->enable_async_messages);
f125905c 2551 config_str = ovsrec_controller_get_other_config_value(c, "dscp", NULL);
cea15768
EJ
2552
2553 oc->dscp = DSCP_DEFAULT;
f125905c 2554 if (config_str) {
cea15768
EJ
2555 int dscp = atoi(config_str);
2556
2557 if (dscp >= 0 && dscp <= 63) {
2558 oc->dscp = dscp;
2559 }
f125905c 2560 }
7d674866
BP
2561}
2562
2563/* Configures the IP stack for 'br''s local interface properly according to the
2564 * configuration in 'c'. */
2565static void
2566bridge_configure_local_iface_netdev(struct bridge *br,
2567 struct ovsrec_controller *c)
2568{
2569 struct netdev *netdev;
2570 struct in_addr mask, gateway;
2571
2572 struct iface *local_iface;
2573 struct in_addr ip;
2574
7d674866 2575 /* If there's no local interface or no IP address, give up. */
892815f5 2576 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
7d674866
BP
2577 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
2578 return;
2579 }
2580
2581 /* Bring up the local interface. */
2582 netdev = local_iface->netdev;
2583 netdev_turn_flags_on(netdev, NETDEV_UP, true);
2584
2585 /* Configure the IP address and netmask. */
2586 if (!c->local_netmask
2587 || !inet_aton(c->local_netmask, &mask)
2588 || !mask.s_addr) {
2589 mask.s_addr = guess_netmask(ip.s_addr);
2590 }
2591 if (!netdev_set_in4(netdev, ip, mask)) {
2592 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
2593 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
2594 }
2595
2596 /* Configure the default gateway. */
2597 if (c->local_gateway
2598 && inet_aton(c->local_gateway, &gateway)
2599 && gateway.s_addr) {
2600 if (!netdev_add_router(netdev, gateway)) {
2601 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
2602 br->name, IP_ARGS(&gateway.s_addr));
2603 }
2604 }
2605}
2606
cb4ef1ea
BP
2607/* Returns true if 'a' and 'b' are the same except that any number of slashes
2608 * in either string are treated as equal to any number of slashes in the other,
2609 * e.g. "x///y" is equal to "x/y". */
2610static bool
2611equal_pathnames(const char *a, const char *b)
2612{
2613 while (*a == *b) {
2614 if (*a == '/') {
2615 a += strspn(a, "/");
2616 b += strspn(b, "/");
2617 } else if (*a == '\0') {
2618 return true;
2619 } else {
2620 a++;
2621 b++;
2622 }
2623 }
2624 return false;
2625}
2626
064af421 2627static void
fa066f01
BP
2628bridge_configure_remotes(struct bridge *br,
2629 const struct sockaddr_in *managers, size_t n_managers)
064af421 2630{
b1da6250
BP
2631 const char *disable_ib_str, *queue_id_str;
2632 bool disable_in_band = false;
2633 int queue_id;
2634
76ce9432
BP
2635 struct ovsrec_controller **controllers;
2636 size_t n_controllers;
7d674866 2637
66da9bef 2638 enum ofproto_fail_mode fail_mode;
7d674866
BP
2639
2640 struct ofproto_controller *ocs;
2641 size_t n_ocs;
2642 size_t i;
064af421 2643
8731b2b6 2644 /* Check if we should disable in-band control on this bridge. */
f67f1354
EJ
2645 disable_ib_str = ovsrec_bridge_get_other_config_value(br->cfg,
2646 "disable-in-band",
2647 NULL);
8731b2b6
JP
2648 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
2649 disable_in_band = true;
2650 }
2651
b1da6250 2652 /* Set OpenFlow queue ID for in-band control. */
f67f1354
EJ
2653 queue_id_str = ovsrec_bridge_get_other_config_value(br->cfg,
2654 "in-band-queue",
2655 NULL);
b1da6250
BP
2656 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
2657 ofproto_set_in_band_queue(br->ofproto, queue_id);
2658
8731b2b6
JP
2659 if (disable_in_band) {
2660 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
2661 } else {
2662 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
2663 }
cd11000b 2664
1a048029 2665 n_controllers = bridge_get_controllers(br, &controllers);
064af421 2666
7d674866
BP
2667 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
2668 n_ocs = 0;
064af421 2669
7d674866
BP
2670 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
2671 for (i = 0; i < n_controllers; i++) {
2672 struct ovsrec_controller *c = controllers[i];
76ce9432 2673
7d674866
BP
2674 if (!strncmp(c->target, "punix:", 6)
2675 || !strncmp(c->target, "unix:", 5)) {
2676 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
cb4ef1ea
BP
2677 char *whitelist;
2678
2679 whitelist = xasprintf("unix:%s/%s.controller",
2680 ovs_rundir(), br->name);
2681 if (!equal_pathnames(c->target, whitelist)) {
2682 /* Prevent remote ovsdb-server users from accessing arbitrary
2683 * Unix domain sockets and overwriting arbitrary local
2684 * files. */
2685 VLOG_ERR_RL(&rl, "bridge %s: Not adding Unix domain socket "
2686 "controller \"%s\" due to possibility for remote "
2687 "exploit. Instead, specify whitelisted \"%s\" or "
2688 "connect to \"unix:%s/%s.mgmt\" (which is always "
2689 "available without special configuration).",
2690 br->name, c->target, whitelist,
2691 ovs_rundir(), br->name);
2692 free(whitelist);
2693 continue;
2694 }
4d6fb5eb 2695
cb4ef1ea 2696 free(whitelist);
4d6fb5eb
EJ
2697 }
2698
7d674866 2699 bridge_configure_local_iface_netdev(br, c);
8731b2b6
JP
2700 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
2701 if (disable_in_band) {
2702 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
f620b43a 2703 }
8731b2b6 2704 n_ocs++;
be02e7c3 2705 }
be02e7c3 2706
7d674866
BP
2707 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
2708 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
2709 free(ocs);
66da9bef
BP
2710
2711 /* Set the fail-mode. */
2712 fail_mode = !br->cfg->fail_mode
2713 || !strcmp(br->cfg->fail_mode, "standalone")
2714 ? OFPROTO_FAIL_STANDALONE
2715 : OFPROTO_FAIL_SECURE;
2716 ofproto_set_fail_mode(br->ofproto, fail_mode);
2717
2718 /* Configure OpenFlow controller connection snooping. */
2719 if (!ofproto_has_snoops(br->ofproto)) {
2720 struct sset snoops;
5827ce14 2721
66da9bef
BP
2722 sset_init(&snoops);
2723 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
2724 ovs_rundir(), br->name));
2725 ofproto_set_snoops(br->ofproto, &snoops);
2726 sset_destroy(&snoops);
064af421
BP
2727 }
2728}
254750ce
BP
2729
2730static void
2731bridge_configure_tables(struct bridge *br)
2732{
2733 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2734 int n_tables;
2735 int i, j;
2736
2737 n_tables = ofproto_get_n_tables(br->ofproto);
2738 j = 0;
2739 for (i = 0; i < n_tables; i++) {
2740 struct ofproto_table_settings s;
2741
2742 s.name = NULL;
2743 s.max_flows = UINT_MAX;
2744 s.groups = NULL;
2745 s.n_groups = 0;
2746
2747 if (j < br->cfg->n_flow_tables && i == br->cfg->key_flow_tables[j]) {
2748 struct ovsrec_flow_table *cfg = br->cfg->value_flow_tables[j++];
2749
2750 s.name = cfg->name;
2751 if (cfg->n_flow_limit && *cfg->flow_limit < UINT_MAX) {
2752 s.max_flows = *cfg->flow_limit;
2753 }
2754 if (cfg->overflow_policy
2755 && !strcmp(cfg->overflow_policy, "evict")) {
2756 size_t k;
2757
2758 s.groups = xmalloc(cfg->n_groups * sizeof *s.groups);
2759 for (k = 0; k < cfg->n_groups; k++) {
2760 const char *string = cfg->groups[k];
2761 char *msg;
2762
2763 msg = mf_parse_subfield__(&s.groups[k], &string);
2764 if (msg) {
2765 VLOG_WARN_RL(&rl, "bridge %s table %d: error parsing "
2766 "'groups' (%s)", br->name, i, msg);
2767 free(msg);
2768 } else if (*string) {
2769 VLOG_WARN_RL(&rl, "bridge %s table %d: 'groups' "
2770 "element '%s' contains trailing garbage",
2771 br->name, i, cfg->groups[k]);
2772 } else {
2773 s.n_groups++;
2774 }
2775 }
2776 }
2777 }
2778
2779 ofproto_configure_table(br->ofproto, i, &s);
2780
2781 free(s.groups);
2782 }
2783 for (; j < br->cfg->n_flow_tables; j++) {
2784 VLOG_WARN_RL(&rl, "bridge %s: ignoring configuration for flow table "
2785 "%"PRId64" not supported by this datapath", br->name,
2786 br->cfg->key_flow_tables[j]);
2787 }
2788}
064af421 2789\f
fa066f01 2790/* Port functions. */
064af421 2791
f620b43a 2792static struct port *
fa066f01 2793port_create(struct bridge *br, const struct ovsrec_port *cfg)
064af421 2794{
f620b43a
BP
2795 struct port *port;
2796
2797 port = xzalloc(sizeof *port);
2798 port->bridge = br;
fa066f01
BP
2799 port->name = xstrdup(cfg->name);
2800 port->cfg = cfg;
f620b43a
BP
2801 list_init(&port->ifaces);
2802
2803 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
f620b43a 2804 return port;
064af421
BP
2805}
2806
fa066f01 2807/* Deletes interfaces from 'port' that are no longer configured for it. */
064af421 2808static void
fa066f01 2809port_del_ifaces(struct port *port)
064af421 2810{
f620b43a
BP
2811 struct iface *iface, *next;
2812 struct sset new_ifaces;
2813 size_t i;
064af421 2814
f620b43a
BP
2815 /* Collect list of new interfaces. */
2816 sset_init(&new_ifaces);
fa066f01
BP
2817 for (i = 0; i < port->cfg->n_interfaces; i++) {
2818 const char *name = port->cfg->interfaces[i]->name;
93f331d3 2819 const char *type = port->cfg->interfaces[i]->type;
00794817
BP
2820 if (strcmp(type, "null")) {
2821 sset_add(&new_ifaces, name);
2822 }
f620b43a 2823 }
064af421 2824
f620b43a
BP
2825 /* Get rid of deleted interfaces. */
2826 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2827 if (!sset_contains(&new_ifaces, iface->name)) {
2828 iface_destroy(iface);
064af421 2829 }
064af421 2830 }
f620b43a
BP
2831
2832 sset_destroy(&new_ifaces);
064af421
BP
2833}
2834
064af421
BP
2835static void
2836port_destroy(struct port *port)
2837{
2838 if (port) {
2839 struct bridge *br = port->bridge;
83db7968 2840 struct iface *iface, *next;
064af421 2841
fa066f01
BP
2842 if (br->ofproto) {
2843 ofproto_bundle_unregister(br->ofproto, port);
064af421
BP
2844 }
2845
83db7968
BP
2846 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2847 iface_destroy(iface);
064af421
BP
2848 }
2849
8052fb14 2850 hmap_remove(&br->ports, &port->hmap_node);
064af421
BP
2851 free(port->name);
2852 free(port);
064af421
BP
2853 }
2854}
2855
064af421
BP
2856static struct port *
2857port_lookup(const struct bridge *br, const char *name)
2858{
8052fb14
BP
2859 struct port *port;
2860
2861 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
2862 &br->ports) {
2863 if (!strcmp(port->name, name)) {
2864 return port;
2865 }
2866 }
2867 return NULL;
064af421
BP
2868}
2869
7a673515
BP
2870static bool
2871enable_lacp(struct port *port, bool *activep)
2872{
2873 if (!port->cfg->lacp) {
2874 /* XXX when LACP implementation has been sufficiently tested, enable by
2875 * default and make active on bonded ports. */
2876 return false;
2877 } else if (!strcmp(port->cfg->lacp, "off")) {
2878 return false;
2879 } else if (!strcmp(port->cfg->lacp, "active")) {
2880 *activep = true;
2881 return true;
2882 } else if (!strcmp(port->cfg->lacp, "passive")) {
2883 *activep = false;
2884 return true;
2885 } else {
2886 VLOG_WARN("port %s: unknown LACP mode %s",
2887 port->name, port->cfg->lacp);
2888 return false;
2889 }
2890}
2891
fa066f01
BP
2892static struct lacp_settings *
2893port_configure_lacp(struct port *port, struct lacp_settings *s)
5827ce14 2894{
e567943d 2895 const char *lacp_time, *system_id;
b5a25389 2896 int priority;
5827ce14 2897
fa066f01
BP
2898 if (!enable_lacp(port, &s->active)) {
2899 return NULL;
abd4a95d
EJ
2900 }
2901
fa066f01 2902 s->name = port->name;
e567943d 2903
f67f1354
EJ
2904 system_id = ovsrec_port_get_other_config_value(port->cfg, "lacp-system-id",
2905 NULL);
a9bf011b
EJ
2906 if (system_id) {
2907 if (sscanf(system_id, ETH_ADDR_SCAN_FMT,
2908 ETH_ADDR_SCAN_ARGS(s->id)) != ETH_ADDR_SCAN_COUNT) {
2909 VLOG_WARN("port %s: LACP system ID (%s) must be an Ethernet"
2910 " address.", port->name, system_id);
2911 return NULL;
2912 }
2913 } else {
e567943d
EJ
2914 memcpy(s->id, port->bridge->ea, ETH_ADDR_LEN);
2915 }
b5a25389 2916
69fdadb1
EJ
2917 if (eth_addr_is_zero(s->id)) {
2918 VLOG_WARN("port %s: Invalid zero LACP system ID.", port->name);
2919 return NULL;
2920 }
2921
b5a25389 2922 /* Prefer bondable links if unspecified. */
f67f1354
EJ
2923 priority = atoi(ovsrec_port_get_other_config_value(port->cfg,
2924 "lacp-system-priority",
2925 "0"));
fa066f01
BP
2926 s->priority = (priority > 0 && priority <= UINT16_MAX
2927 ? priority
2928 : UINT16_MAX - !list_is_short(&port->ifaces));
b5a25389 2929
f67f1354
EJ
2930 lacp_time = ovsrec_port_get_other_config_value(port->cfg, "lacp-time",
2931 "slow");
bf83f7c8 2932 s->fast = !strcasecmp(lacp_time, "fast");
fa066f01
BP
2933 return s;
2934}
5827ce14 2935
fa066f01
BP
2936static void
2937iface_configure_lacp(struct iface *iface, struct lacp_slave_settings *s)
2938{
00794817 2939 int priority, portid, key;
5827ce14 2940
f67f1354
EJ
2941 portid = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
2942 "lacp-port-id",
2943 "0"));
2944 priority =
2945 atoi(ovsrec_interface_get_other_config_value(iface->cfg,
2946 "lacp-port-priority",
2947 "0"));
2948 key = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
2949 "lacp-aggregation-key",
2950 "0"));
fa066f01
BP
2951
2952 if (portid <= 0 || portid > UINT16_MAX) {
892815f5 2953 portid = iface->ofp_port;
5827ce14
EJ
2954 }
2955
fa066f01
BP
2956 if (priority <= 0 || priority > UINT16_MAX) {
2957 priority = UINT16_MAX;
2958 }
5827ce14 2959
00794817
BP
2960 if (key < 0 || key > UINT16_MAX) {
2961 key = 0;
5827ce14 2962 }
00794817 2963
fa066f01 2964 s->name = iface->name;
fa066f01
BP
2965 s->id = portid;
2966 s->priority = priority;
00794817 2967 s->key = key;
bb5bc6c0
BP
2968}
2969
c25c91fd 2970static void
76ed83fc
BP
2971port_configure_bond(struct port *port, struct bond_settings *s,
2972 uint32_t *bond_stable_ids)
c25c91fd 2973{
f620b43a 2974 const char *detect_s;
7a673515 2975 struct iface *iface;
1670c579 2976 int miimon_interval;
76ed83fc 2977 size_t i;
c25c91fd 2978
fa066f01 2979 s->name = port->name;
4df08875 2980 s->balance = BM_AB;
4c57c3bc
EJ
2981 if (port->cfg->bond_mode) {
2982 if (!bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
2983 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
2984 port->name, port->cfg->bond_mode,
2985 bond_mode_to_string(s->balance));
2986 }
2987 } else {
2988 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2989
4df08875
EJ
2990 /* XXX: Post version 1.5.*, the default bond_mode changed from SLB to
2991 * active-backup. At some point we should remove this warning. */
4c57c3bc 2992 VLOG_WARN_RL(&rl, "port %s: Using the default bond_mode %s. Note that"
4df08875
EJ
2993 " in previous versions, the default bond_mode was"
2994 " balance-slb", port->name,
4c57c3bc 2995 bond_mode_to_string(s->balance));
f620b43a 2996 }
6c2d2a9f
BP
2997 if (s->balance == BM_SLB && port->bridge->cfg->n_flood_vlans) {
2998 VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
2999 "please use another bond type or disable flood_vlans",
3000 port->name);
3001 }
bb5bc6c0 3002
f67f1354
EJ
3003 miimon_interval =
3004 atoi(ovsrec_port_get_other_config_value(port->cfg,
3005 "bond-miimon-interval", "0"));
1670c579
EJ
3006 if (miimon_interval <= 0) {
3007 miimon_interval = 200;
f620b43a
BP
3008 }
3009
f67f1354
EJ
3010 detect_s = ovsrec_port_get_other_config_value(port->cfg,
3011 "bond-detect-mode",
3012 "carrier");
1670c579
EJ
3013 if (!strcmp(detect_s, "carrier")) {
3014 miimon_interval = 0;
3015 } else if (strcmp(detect_s, "miimon")) {
3016 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
3017 "defaulting to carrier", port->name, detect_s);
3018 miimon_interval = 0;
f620b43a
BP
3019 }
3020
fa066f01
BP
3021 s->up_delay = MAX(0, port->cfg->bond_updelay);
3022 s->down_delay = MAX(0, port->cfg->bond_downdelay);
f67f1354
EJ
3023 s->basis = atoi(ovsrec_port_get_other_config_value(port->cfg,
3024 "bond-hash-basis",
3025 "0"));
fa066f01 3026 s->rebalance_interval = atoi(
f67f1354
EJ
3027 ovsrec_port_get_other_config_value(port->cfg,
3028 "bond-rebalance-interval",
3029 "10000"));
bc1b010c 3030 if (s->rebalance_interval && s->rebalance_interval < 1000) {
fa066f01 3031 s->rebalance_interval = 1000;
f620b43a
BP
3032 }
3033
fa066f01 3034 s->fake_iface = port->cfg->bond_fake_iface;
7a673515 3035
76ed83fc 3036 i = 0;
7a673515 3037 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
632e2b95
EJ
3038 long long stable_id;
3039
f67f1354
EJ
3040 stable_id =
3041 atoll(ovsrec_interface_get_other_config_value(iface->cfg,
3042 "bond-stable-id",
3043 "0"));
632e2b95 3044 if (stable_id <= 0 || stable_id >= UINT32_MAX) {
76ed83fc 3045 stable_id = iface->ofp_port;
632e2b95 3046 }
76ed83fc 3047 bond_stable_ids[i++] = stable_id;
1670c579
EJ
3048
3049 netdev_set_miimon_interval(iface->netdev, miimon_interval);
064af421
BP
3050 }
3051}
06b592bc
EJ
3052
3053/* Returns true if 'port' is synthetic, that is, if we constructed it locally
3054 * instead of obtaining it from the database. */
3055static bool
3056port_is_synthetic(const struct port *port)
3057{
3058 return ovsdb_idl_row_is_synthetic(&port->cfg->header_);
3059}
064af421
BP
3060\f
3061/* Interface functions. */
3062
bae7208e
EJ
3063static void
3064iface_refresh_type(struct iface *iface)
064af421 3065{
bae7208e
EJ
3066 /* Determine interface type. The local port always has type
3067 * "internal". Other ports take their type from the database and
3068 * default to "system" if none is specified. */
3069 iface->type = (!strcmp(iface->name, iface->port->bridge->name) ? "internal"
3070 : iface->cfg->type[0] ? iface->cfg->type
3071 : "system");
064af421
BP
3072}
3073
3074static void
3075iface_destroy(struct iface *iface)
3076{
3077 if (iface) {
3078 struct port *port = iface->port;
3079 struct bridge *br = port->bridge;
c17f0d5e 3080
892815f5
BP
3081 if (br->ofproto && iface->ofp_port >= 0) {
3082 ofproto_port_unregister(br->ofproto, iface->ofp_port);
5827ce14
EJ
3083 }
3084
892815f5
BP
3085 if (iface->ofp_port >= 0) {
3086 hmap_remove(&br->ifaces, &iface->ofp_port_node);
064af421
BP
3087 }
3088
83db7968 3089 list_remove(&iface->port_elem);
ebea37cc 3090 hmap_remove(&br->iface_by_name, &iface->name_node);
064af421 3091
0c6aea3f 3092 netdev_close(iface->netdev);
064af421 3093
a740f0de
JG
3094 free(iface->name);
3095 free(iface);
064af421
BP
3096 }
3097}
3098
3099static struct iface *
3100iface_lookup(const struct bridge *br, const char *name)
3101{
ebea37cc
BP
3102 struct iface *iface;
3103
3104 HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
3105 &br->iface_by_name) {
3106 if (!strcmp(iface->name, name)) {
3107 return iface;
3108 }
3109 }
3110
3111 return NULL;
064af421
BP
3112}
3113
e8fe3026
EJ
3114static struct iface *
3115iface_find(const char *name)
3116{
3117 const struct bridge *br;
3118
764072fd 3119 HMAP_FOR_EACH (br, node, &all_bridges) {
e8fe3026
EJ
3120 struct iface *iface = iface_lookup(br, name);
3121
3122 if (iface) {
3123 return iface;
3124 }
3125 }
3126 return NULL;
3127}
3128
bae7208e
EJ
3129static struct if_cfg *
3130if_cfg_lookup(const struct bridge *br, const char *name)
3131{
3132 struct if_cfg *if_cfg;
3133
3134 HMAP_FOR_EACH_WITH_HASH (if_cfg, hmap_node, hash_string(name, 0),
3135 &br->if_cfg_todo) {
3136 if (!strcmp(if_cfg->cfg->name, name)) {
3137 return if_cfg;
3138 }
3139 }
3140
3141 return NULL;
3142}
3143
064af421 3144static struct iface *
892815f5 3145iface_from_ofp_port(const struct bridge *br, uint16_t ofp_port)
064af421 3146{
d9a8717a
BP
3147 struct iface *iface;
3148
892815f5
BP
3149 HMAP_FOR_EACH_IN_BUCKET (iface, ofp_port_node,
3150 hash_int(ofp_port, 0), &br->ifaces) {
3151 if (iface->ofp_port == ofp_port) {
d9a8717a
BP
3152 return iface;
3153 }
3154 }
3155 return NULL;
064af421 3156}
557d8e6c 3157
52df17e7
BP
3158/* Set Ethernet address of 'iface', if one is specified in the configuration
3159 * file. */
3160static void
3161iface_set_mac(struct iface *iface)
3162{
76343538 3163 uint8_t ea[ETH_ADDR_LEN];
52df17e7 3164
ede2fd6d
BP
3165 if (!strcmp(iface->type, "internal")
3166 && iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
892815f5 3167 if (iface->ofp_port == OFPP_LOCAL) {
ede2fd6d
BP
3168 VLOG_ERR("interface %s: ignoring mac in Interface record "
3169 "(use Bridge record to set local port's mac)",
3170 iface->name);
3171 } else if (eth_addr_is_multicast(ea)) {
52df17e7
BP
3172 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3173 iface->name);
52df17e7 3174 } else {
4d678233 3175 int error = netdev_set_etheraddr(iface->netdev, ea);
52df17e7
BP
3176 if (error) {
3177 VLOG_ERR("interface %s: setting MAC failed (%s)",
3178 iface->name, strerror(error));
3179 }
3180 }
3181 }
3182}
c1c9c9c4 3183
bcd49a45
BP
3184/* Sets the ofport column of 'if_cfg' to 'ofport'. */
3185static void
3186iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
3187{
cfea354b 3188 if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
bcd49a45
BP
3189 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
3190 }
3191}
3192
3fc5a86a
BP
3193/* Clears all of the fields in 'if_cfg' that indicate interface status, and
3194 * sets the "ofport" field to -1.
3195 *
3196 * This is appropriate when 'if_cfg''s interface cannot be created or is
3197 * otherwise invalid. */
3198static void
3199iface_clear_db_record(const struct ovsrec_interface *if_cfg)
3200{
3201 if (!ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
3202 iface_set_ofport(if_cfg, -1);
3203 ovsrec_interface_set_status(if_cfg, NULL, NULL, 0);
3204 ovsrec_interface_set_admin_state(if_cfg, NULL);
3205 ovsrec_interface_set_duplex(if_cfg, NULL);
3206 ovsrec_interface_set_link_speed(if_cfg, NULL, 0);
3207 ovsrec_interface_set_link_state(if_cfg, NULL);
3208 ovsrec_interface_set_mtu(if_cfg, NULL, 0);
3209 ovsrec_interface_set_cfm_fault(if_cfg, NULL, 0);
b9380396 3210 ovsrec_interface_set_cfm_fault_status(if_cfg, NULL, 0);
3fc5a86a
BP
3211 ovsrec_interface_set_cfm_remote_mpids(if_cfg, NULL, 0);
3212 ovsrec_interface_set_lacp_current(if_cfg, NULL, 0);
3213 ovsrec_interface_set_statistics(if_cfg, NULL, NULL, 0);
3214 }
3215}
3216
43776b8f
BP
3217/* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
3218 *
3219 * The value strings in '*shash' are taken directly from values[], not copied,
3220 * so the caller should not modify or free them. */
c1c9c9c4
BP
3221static void
3222shash_from_ovs_idl_map(char **keys, char **values, size_t n,
3223 struct shash *shash)
3224{
3225 size_t i;
3226
3227 shash_init(shash);
3228 for (i = 0; i < n; i++) {
3229 shash_add(shash, keys[i], values[i]);
3230 }
3231}
3232
ea763e0e
EJ
3233/* Creates 'keys' and 'values' arrays from 'shash'.
3234 *
3235 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
3236 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
3237 * are populated with with strings taken directly from 'shash' and thus have
3238 * the same ownership of the key-value pairs in shash.
3239 */
3240static void
3241shash_to_ovs_idl_map(struct shash *shash,
3242 char ***keys, char ***values, size_t *n)
3243{
3244 size_t i, count;
3245 char **k, **v;
3246 struct shash_node *sn;
3247
3248 count = shash_count(shash);
3249
3250 k = xmalloc(count * sizeof *k);
3251 v = xmalloc(count * sizeof *v);
3252
3253 i = 0;
3254 SHASH_FOR_EACH(sn, shash) {
3255 k[i] = sn->name;
3256 v[i] = sn->data;
3257 i++;
3258 }
3259
3260 *n = count;
3261 *keys = k;
3262 *values = v;
3263}
3264
c1c9c9c4
BP
3265struct iface_delete_queues_cbdata {
3266 struct netdev *netdev;
44fca7f9 3267 const struct ovsdb_datum *queues;
c1c9c9c4
BP
3268};
3269
3270static bool
44fca7f9 3271queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
c1c9c9c4 3272{
44fca7f9 3273 union ovsdb_atom atom;
c1c9c9c4 3274
44fca7f9
BP
3275 atom.integer = target;
3276 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
c1c9c9c4
BP
3277}
3278
3279static void
3280iface_delete_queues(unsigned int queue_id,
3281 const struct shash *details OVS_UNUSED, void *cbdata_)
3282{
3283 struct iface_delete_queues_cbdata *cbdata = cbdata_;
3284
44fca7f9 3285 if (!queue_ids_include(cbdata->queues, queue_id)) {
c1c9c9c4
BP
3286 netdev_delete_queue(cbdata->netdev, queue_id);
3287 }
3288}
3289
3290static void
66da9bef 3291iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
c1c9c9c4 3292{
8b36f51e
EJ
3293 struct ofpbuf queues_buf;
3294
3295 ofpbuf_init(&queues_buf, 0);
3296
daa8bd2b 3297 if (!qos || qos->type[0] == '\0' || qos->n_queues < 1) {
c1c9c9c4
BP
3298 netdev_set_qos(iface->netdev, NULL, NULL);
3299 } else {
3300 struct iface_delete_queues_cbdata cbdata;
3301 struct shash details;
6a6e60bd 3302 bool queue_zero;
c1c9c9c4
BP
3303 size_t i;
3304
3305 /* Configure top-level Qos for 'iface'. */
3306 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
3307 qos->n_other_config, &details);
3308 netdev_set_qos(iface->netdev, qos->type, &details);
3309 shash_destroy(&details);
3310
3311 /* Deconfigure queues that were deleted. */
3312 cbdata.netdev = iface->netdev;
44fca7f9
BP
3313 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
3314 OVSDB_TYPE_UUID);
c1c9c9c4
BP
3315 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
3316
3317 /* Configure queues for 'iface'. */
6a6e60bd 3318 queue_zero = false;
c1c9c9c4
BP
3319 for (i = 0; i < qos->n_queues; i++) {
3320 const struct ovsrec_queue *queue = qos->value_queues[i];
3321 unsigned int queue_id = qos->key_queues[i];
3322
6a6e60bd
BP
3323 if (queue_id == 0) {
3324 queue_zero = true;
3325 }
3326
8b36f51e
EJ
3327 if (queue->n_dscp == 1) {
3328 struct ofproto_port_queue *port_queue;
3329
3330 port_queue = ofpbuf_put_uninit(&queues_buf,
3331 sizeof *port_queue);
3332 port_queue->queue = queue_id;
3333 port_queue->dscp = queue->dscp[0];
3334 }
3335
c1c9c9c4
BP
3336 shash_from_ovs_idl_map(queue->key_other_config,
3337 queue->value_other_config,
3338 queue->n_other_config, &details);
3339 netdev_set_queue(iface->netdev, queue_id, &details);
3340 shash_destroy(&details);
3341 }
6a6e60bd 3342 if (!queue_zero) {
2c999774
BP
3343 shash_init(&details);
3344 netdev_set_queue(iface->netdev, 0, &details);
3345 shash_destroy(&details);
6a6e60bd 3346 }
c1c9c9c4 3347 }
fa066f01 3348
8b36f51e
EJ
3349 if (iface->ofp_port >= 0) {
3350 const struct ofproto_port_queue *port_queues = queues_buf.data;
3351 size_t n_queues = queues_buf.size / sizeof *port_queues;
3352
3353 ofproto_port_set_queues(iface->port->bridge->ofproto, iface->ofp_port,
3354 port_queues, n_queues);
3355 }
3356
fa066f01
BP
3357 netdev_set_policing(iface->netdev,
3358 iface->cfg->ingress_policing_rate,
3359 iface->cfg->ingress_policing_burst);
8b36f51e
EJ
3360
3361 ofpbuf_uninit(&queues_buf);
c1c9c9c4 3362}
b31bcf60
EJ
3363
3364static void
66da9bef 3365iface_configure_cfm(struct iface *iface)
b31bcf60 3366{
93b8df38 3367 const struct ovsrec_interface *cfg = iface->cfg;
86dc6501 3368 const char *extended_str, *opstate_str;
189cb9e4 3369 const char *cfm_ccm_vlan;
a5610457 3370 struct cfm_settings s;
b31bcf60 3371
144216a3 3372 if (!cfg->n_cfm_mpid) {
892815f5 3373 ofproto_port_clear_cfm(iface->port->bridge->ofproto, iface->ofp_port);
b31bcf60
EJ
3374 return;
3375 }
3376
a5610457 3377 s.mpid = *cfg->cfm_mpid;
f67f1354
EJ
3378 s.interval = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
3379 "cfm_interval",
3380 "0"));
3381 cfm_ccm_vlan = ovsrec_interface_get_other_config_value(iface->cfg,
3382 "cfm_ccm_vlan",
3383 "0");
3384 s.ccm_pcp = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
3385 "cfm_ccm_pcp",
3386 "0"));
a5610457
EJ
3387 if (s.interval <= 0) {
3388 s.interval = 1000;
b31bcf60 3389 }
b31bcf60 3390
189cb9e4
EJ
3391 if (!strcasecmp("random", cfm_ccm_vlan)) {
3392 s.ccm_vlan = CFM_RANDOM_VLAN;
3393 } else {
3394 s.ccm_vlan = atoi(cfm_ccm_vlan);
3395 if (s.ccm_vlan == CFM_RANDOM_VLAN) {
3396 s.ccm_vlan = 0;
3397 }
3398 }
3399
f67f1354
EJ
3400 extended_str = ovsrec_interface_get_other_config_value(iface->cfg,
3401 "cfm_extended",
3402 "false");
ef9819b5
EJ
3403 s.extended = !strcasecmp("true", extended_str);
3404
f67f1354
EJ
3405 opstate_str = ovsrec_interface_get_other_config_value(iface->cfg,
3406 "cfm_opstate",
3407 "up");
86dc6501
EJ
3408 s.opup = !strcasecmp("up", opstate_str);
3409
a5610457 3410 ofproto_port_set_cfm(iface->port->bridge->ofproto, iface->ofp_port, &s);
b31bcf60 3411}
0b8024eb 3412
cfea354b
BP
3413/* Returns true if 'iface' is synthetic, that is, if we constructed it locally
3414 * instead of obtaining it from the database. */
3415static bool
3416iface_is_synthetic(const struct iface *iface)
3417{
3418 return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
3419}
f125905c 3420
064af421
BP
3421\f
3422/* Port mirroring. */
3423
dd0d105c
BP
3424static struct mirror *
3425mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
3426{
fa066f01 3427 struct mirror *m;
dd0d105c 3428
fa066f01
BP
3429 HMAP_FOR_EACH_IN_BUCKET (m, hmap_node, uuid_hash(uuid), &br->mirrors) {
3430 if (uuid_equals(uuid, &m->uuid)) {
dd0d105c
BP
3431 return m;
3432 }
3433 }
3434 return NULL;
3435}
3436
064af421 3437static void
fa066f01 3438bridge_configure_mirrors(struct bridge *br)
064af421 3439{
fa066f01
BP
3440 const struct ovsdb_datum *mc;
3441 unsigned long *flood_vlans;
3442 struct mirror *m, *next;
3443 size_t i;
064af421 3444
dd0d105c 3445 /* Get rid of deleted mirrors. */
fa066f01
BP
3446 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
3447 HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mirrors) {
3448 union ovsdb_atom atom;
3449
3450 atom.uuid = m->uuid;
3451 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3452 mirror_destroy(m);
064af421
BP
3453 }
3454 }
3455
dd0d105c 3456 /* Add new mirrors and reconfigure existing ones. */
37e7f427 3457 for (i = 0; i < br->cfg->n_mirrors; i++) {
fa066f01 3458 const struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
dd0d105c 3459 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
fa066f01
BP
3460 if (!m) {
3461 m = mirror_create(br, cfg);
064af421 3462 }
9d24de3b
JP
3463 m->cfg = cfg;
3464 if (!mirror_configure(m)) {
fa066f01 3465 mirror_destroy(m);
064af421
BP
3466 }
3467 }
f2d7fd66 3468
8f30d09a 3469 /* Update flooded vlans (for RSPAN). */
fa066f01
BP
3470 flood_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
3471 br->cfg->n_flood_vlans);
3472 ofproto_set_flood_vlans(br->ofproto, flood_vlans);
3473 bitmap_free(flood_vlans);
064af421
BP
3474}
3475
fa066f01
BP
3476static struct mirror *
3477mirror_create(struct bridge *br, const struct ovsrec_mirror *cfg)
064af421
BP
3478{
3479 struct mirror *m;
064af421 3480
fa066f01 3481 m = xzalloc(sizeof *m);
8bbc128e 3482 m->uuid = cfg->header_.uuid;
fa066f01 3483 hmap_insert(&br->mirrors, &m->hmap_node, uuid_hash(&m->uuid));
064af421 3484 m->bridge = br;
dd0d105c 3485 m->name = xstrdup(cfg->name);
37e7f427 3486
fa066f01 3487 return m;
064af421
BP
3488}
3489
3490static void
3491mirror_destroy(struct mirror *m)
3492{
3493 if (m) {
3494 struct bridge *br = m->bridge;
064af421 3495
fa066f01
BP
3496 if (br->ofproto) {
3497 ofproto_mirror_unregister(br->ofproto, m);
064af421
BP
3498 }
3499
fa066f01 3500 hmap_remove(&br->mirrors, &m->hmap_node);
786880a5 3501 free(m->name);
064af421 3502 free(m);
064af421
BP
3503 }
3504}
3505
3506static void
fa066f01
BP
3507mirror_collect_ports(struct mirror *m,
3508 struct ovsrec_port **in_ports, int n_in_ports,
3509 void ***out_portsp, size_t *n_out_portsp)
064af421 3510{
fa066f01
BP
3511 void **out_ports = xmalloc(n_in_ports * sizeof *out_ports);
3512 size_t n_out_ports = 0;
064af421
BP
3513 size_t i;
3514
fa066f01
BP
3515 for (i = 0; i < n_in_ports; i++) {
3516 const char *name = in_ports[i]->name;
3517 struct port *port = port_lookup(m->bridge, name);
3518 if (port) {
3519 out_ports[n_out_ports++] = port;
064af421 3520 } else {
37e7f427
BP
3521 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
3522 "port %s", m->bridge->name, m->name, name);
064af421
BP
3523 }
3524 }
fa066f01
BP
3525 *out_portsp = out_ports;
3526 *n_out_portsp = n_out_ports;
064af421
BP
3527}
3528
3529static bool
9d24de3b 3530mirror_configure(struct mirror *m)
064af421 3531{
9d24de3b 3532 const struct ovsrec_mirror *cfg = m->cfg;
fa066f01 3533 struct ofproto_mirror_settings s;
064af421 3534
dd0d105c
BP
3535 /* Set name. */
3536 if (strcmp(cfg->name, m->name)) {
3537 free(m->name);
3538 m->name = xstrdup(cfg->name);
3539 }
fa066f01 3540 s.name = m->name;
dd0d105c 3541
fa066f01 3542 /* Get output port or VLAN. */
37e7f427 3543 if (cfg->output_port) {
fa066f01 3544 s.out_bundle = port_lookup(m->bridge, cfg->output_port->name);
abe529af 3545 if (!s.out_bundle) {
37e7f427
BP
3546 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
3547 m->bridge->name, m->name);
fa066f01 3548 return false;
064af421 3549 }
fa066f01 3550 s.out_vlan = UINT16_MAX;
064af421 3551
37e7f427
BP
3552 if (cfg->output_vlan) {
3553 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
3554 "output vlan; ignoring output vlan",
3555 m->bridge->name, m->name);
064af421 3556 }
37e7f427 3557 } else if (cfg->output_vlan) {
fa066f01
BP
3558 /* The database should prevent invalid VLAN values. */
3559 s.out_bundle = NULL;
3560 s.out_vlan = *cfg->output_vlan;
064af421 3561 } else {
37e7f427
BP
3562 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
3563 m->bridge->name, m->name);
fa066f01 3564 return false;
064af421
BP
3565 }
3566
fa066f01 3567 /* Get port selection. */
939ff267 3568 if (cfg->select_all) {
fa066f01
BP
3569 size_t n_ports = hmap_count(&m->bridge->ports);
3570 void **ports = xmalloc(n_ports * sizeof *ports);
abe529af 3571 struct port *port;
fa066f01
BP
3572 size_t i;
3573
3574 i = 0;
8052fb14 3575 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
fa066f01 3576 ports[i++] = port;
939ff267 3577 }
fa066f01
BP
3578
3579 s.srcs = ports;
3580 s.n_srcs = n_ports;
3581
3582 s.dsts = ports;
3583 s.n_dsts = n_ports;
939ff267 3584 } else {
fa066f01
BP
3585 /* Get ports, dropping ports that don't exist.
3586 * The IDL ensures that there are no duplicates. */
939ff267 3587 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
fa066f01 3588 &s.srcs, &s.n_srcs);
939ff267 3589 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
fa066f01 3590 &s.dsts, &s.n_dsts);
064af421
BP
3591 }
3592
fa066f01
BP
3593 /* Get VLAN selection. */
3594 s.src_vlans = vlan_bitmap_from_array(cfg->select_vlan, cfg->n_select_vlan);
3595
3596 /* Configure. */
3597 ofproto_mirror_register(m->bridge->ofproto, m, &s);
3598
064af421 3599 /* Clean up. */
fa066f01
BP
3600 if (s.srcs != s.dsts) {
3601 free(s.dsts);
3602 }
3603 free(s.srcs);
3604 free(s.src_vlans);
3605
3606 return true;
064af421 3607}
52a90c29
BP
3608\f
3609/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
3610 *
3611 * This is deprecated. It is only for compatibility with broken device drivers
3612 * in old versions of Linux that do not properly support VLANs when VLAN
3613 * devices are not used. When broken device drivers are no longer in
3614 * widespread use, we will delete these interfaces. */
3615
3616static void **blocks;
3617static size_t n_blocks, allocated_blocks;
3618
3619/* Adds 'block' to a list of blocks that have to be freed with free() when the
3620 * VLAN splinters are reconfigured. */
3621static void
3622register_block(void *block)
3623{
3624 if (n_blocks >= allocated_blocks) {
3625 blocks = x2nrealloc(blocks, &allocated_blocks, sizeof *blocks);
3626 }
3627 blocks[n_blocks++] = block;
3628}
3629
3630/* Frees all of the blocks registered with register_block(). */
3631static void
3632free_registered_blocks(void)
3633{
3634 size_t i;
3635
3636 for (i = 0; i < n_blocks; i++) {
3637 free(blocks[i]);
3638 }
3639 n_blocks = 0;
3640}
3641
3642/* Returns true if VLAN splinters are enabled on 'iface_cfg', false
3643 * otherwise. */
3644static bool
3645vlan_splinters_is_enabled(const struct ovsrec_interface *iface_cfg)
3646{
3647 const char *value;
3648
f67f1354
EJ
3649 value = ovsrec_interface_get_other_config_value(iface_cfg,
3650 "enable-vlan-splinters",
3651 "");
52a90c29
BP
3652 return !strcmp(value, "true");
3653}
3654
3655/* Figures out the set of VLANs that are in use for the purpose of VLAN
3656 * splinters.
3657 *
3658 * If VLAN splinters are enabled on at least one interface and any VLANs are in
3659 * use, returns a 4096-bit bitmap with a 1-bit for each in-use VLAN (bits 0 and
3660 * 4095 will not be set). The caller is responsible for freeing the bitmap,
3661 * with free().
3662 *
3663 * If VLANs splinters are not enabled on any interface or if no VLANs are in
3664 * use, returns NULL.
3665 *
3666 * Updates 'vlan_splinters_enabled_anywhere'. */
3667static unsigned long int *
3668collect_splinter_vlans(const struct ovsrec_open_vswitch *ovs_cfg)
3669{
3670 unsigned long int *splinter_vlans;
3671 struct sset splinter_ifaces;
3672 const char *real_dev_name;
3673 struct shash *real_devs;
3674 struct shash_node *node;
3675 struct bridge *br;
3676 size_t i;
3677
7c70698f
BP
3678 /* Free space allocated for synthesized ports and interfaces, since we're
3679 * in the process of reconstructing all of them. */
3680 free_registered_blocks();
3681
45c580a3 3682 splinter_vlans = bitmap_allocate(4096);
52a90c29 3683 sset_init(&splinter_ifaces);
45c580a3 3684 vlan_splinters_enabled_anywhere = false;
52a90c29
BP
3685 for (i = 0; i < ovs_cfg->n_bridges; i++) {
3686 struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
3687 size_t j;
3688
3689 for (j = 0; j < br_cfg->n_ports; j++) {
3690 struct ovsrec_port *port_cfg = br_cfg->ports[j];
3691 int k;
3692
3693 for (k = 0; k < port_cfg->n_interfaces; k++) {
3694 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
3695
3696 if (vlan_splinters_is_enabled(iface_cfg)) {
45c580a3 3697 vlan_splinters_enabled_anywhere = true;
52a90c29 3698 sset_add(&splinter_ifaces, iface_cfg->name);
52a90c29
BP
3699 vlan_bitmap_from_array__(port_cfg->trunks,
3700 port_cfg->n_trunks,
3701 splinter_vlans);
3702 }
3703 }
45c580a3
BP
3704
3705 if (port_cfg->tag && *port_cfg->tag > 0 && *port_cfg->tag < 4095) {
3706 bitmap_set1(splinter_vlans, *port_cfg->tag);
3707 }
52a90c29
BP
3708 }
3709 }
3710
45c580a3
BP
3711 if (!vlan_splinters_enabled_anywhere) {
3712 free(splinter_vlans);
52a90c29
BP
3713 sset_destroy(&splinter_ifaces);
3714 return NULL;
3715 }
3716
3717 HMAP_FOR_EACH (br, node, &all_bridges) {
3718 if (br->ofproto) {
3719 ofproto_get_vlan_usage(br->ofproto, splinter_vlans);
3720 }
3721 }
3722
3723 /* Don't allow VLANs 0 or 4095 to be splintered. VLAN 0 should appear on
3724 * the real device. VLAN 4095 is reserved and Linux doesn't allow a VLAN
3725 * device to be created for it. */
3726 bitmap_set0(splinter_vlans, 0);
3727 bitmap_set0(splinter_vlans, 4095);
3728
3729 /* Delete all VLAN devices that we don't need. */
3730 vlandev_refresh();
3731 real_devs = vlandev_get_real_devs();
3732 SHASH_FOR_EACH (node, real_devs) {
3733 const struct vlan_real_dev *real_dev = node->data;
3734 const struct vlan_dev *vlan_dev;
3735 bool real_dev_has_splinters;
3736
3737 real_dev_has_splinters = sset_contains(&splinter_ifaces,
3738 real_dev->name);
3739 HMAP_FOR_EACH (vlan_dev, hmap_node, &real_dev->vlan_devs) {
3740 if (!real_dev_has_splinters
3741 || !bitmap_is_set(splinter_vlans, vlan_dev->vid)) {
3742 struct netdev *netdev;
3743
3744 if (!netdev_open(vlan_dev->name, "system", &netdev)) {
3745 if (!netdev_get_in4(netdev, NULL, NULL) ||
3746 !netdev_get_in6(netdev, NULL)) {
3747 vlandev_del(vlan_dev->name);
3748 } else {
3749 /* It has an IP address configured, so we don't own
3750 * it. Don't delete it. */
3751 }
3752 netdev_close(netdev);
3753 }
3754 }
3755
3756 }
3757 }
3758
3759 /* Add all VLAN devices that we need. */
3760 SSET_FOR_EACH (real_dev_name, &splinter_ifaces) {
3761 int vid;
3762
3763 BITMAP_FOR_EACH_1 (vid, 4096, splinter_vlans) {
3764 if (!vlandev_get_name(real_dev_name, vid)) {
3765 vlandev_add(real_dev_name, vid);
3766 }
3767 }
3768 }
3769
3770 vlandev_refresh();
3771
3772 sset_destroy(&splinter_ifaces);
3773
3774 if (bitmap_scan(splinter_vlans, 0, 4096) >= 4096) {
3775 free(splinter_vlans);
3776 return NULL;
3777 }
3778 return splinter_vlans;
3779}
3780
3781/* Pushes the configure of VLAN splinter port 'port' (e.g. eth0.9) down to
3782 * ofproto. */
3783static void
3784configure_splinter_port(struct port *port)
3785{
3786 struct ofproto *ofproto = port->bridge->ofproto;
3787 uint16_t realdev_ofp_port;
3788 const char *realdev_name;
3789 struct iface *vlandev, *realdev;
3790
3791 ofproto_bundle_unregister(port->bridge->ofproto, port);
3792
3793 vlandev = CONTAINER_OF(list_front(&port->ifaces), struct iface,
3794 port_elem);
3795
f67f1354
EJ
3796 realdev_name = ovsrec_port_get_other_config_value(port->cfg,
3797 "realdev", NULL);
52a90c29
BP
3798 realdev = iface_lookup(port->bridge, realdev_name);
3799 realdev_ofp_port = realdev ? realdev->ofp_port : 0;
3800
3801 ofproto_port_set_realdev(ofproto, vlandev->ofp_port, realdev_ofp_port,
3802 *port->cfg->tag);
3803}
3804
3805static struct ovsrec_port *
3806synthesize_splinter_port(const char *real_dev_name,
3807 const char *vlan_dev_name, int vid)
3808{
3809 struct ovsrec_interface *iface;
3810 struct ovsrec_port *port;
3811
3812 iface = xzalloc(sizeof *iface);
3813 iface->name = xstrdup(vlan_dev_name);
3814 iface->type = "system";
3815
3816 port = xzalloc(sizeof *port);
3817 port->interfaces = xmemdup(&iface, sizeof iface);
3818 port->n_interfaces = 1;
3819 port->name = xstrdup(vlan_dev_name);
3820 port->vlan_mode = "splinter";
3821 port->tag = xmalloc(sizeof *port->tag);
3822 *port->tag = vid;
3823 port->key_other_config = xmalloc(sizeof *port->key_other_config);
3824 port->key_other_config[0] = "realdev";
3825 port->value_other_config = xmalloc(sizeof *port->value_other_config);
3826 port->value_other_config[0] = xstrdup(real_dev_name);
3827 port->n_other_config = 1;
3828
3829 register_block(iface);
3830 register_block(iface->name);
3831 register_block(port);
3832 register_block(port->interfaces);
3833 register_block(port->name);
3834 register_block(port->tag);
3835 register_block(port->key_other_config);
3836 register_block(port->value_other_config);
3837 register_block(port->value_other_config[0]);
3838
3839 return port;
3840}
3841
3842/* For each interface with 'br' that has VLAN splinters enabled, adds a
3843 * corresponding ovsrec_port to 'ports' for each splinter VLAN marked with a
3844 * 1-bit in the 'splinter_vlans' bitmap. */
3845static void
3846add_vlan_splinter_ports(struct bridge *br,
3847 const unsigned long int *splinter_vlans,
3848 struct shash *ports)
3849{
3850 size_t i;
3851
52a90c29
BP
3852 /* We iterate through 'br->cfg->ports' instead of 'ports' here because
3853 * we're modifying 'ports'. */
3854 for (i = 0; i < br->cfg->n_ports; i++) {
3855 const char *name = br->cfg->ports[i]->name;
3856 struct ovsrec_port *port_cfg = shash_find_data(ports, name);
3857 size_t j;
3858
3859 for (j = 0; j < port_cfg->n_interfaces; j++) {
3860 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[j];
3861
3862 if (vlan_splinters_is_enabled(iface_cfg)) {
3863 const char *real_dev_name;
3864 uint16_t vid;
3865
3866 real_dev_name = iface_cfg->name;
3867 BITMAP_FOR_EACH_1 (vid, 4096, splinter_vlans) {
3868 const char *vlan_dev_name;
3869
3870 vlan_dev_name = vlandev_get_name(real_dev_name, vid);
3871 if (vlan_dev_name
3872 && !shash_find(ports, vlan_dev_name)) {
3873 shash_add(ports, vlan_dev_name,
3874 synthesize_splinter_port(
3875 real_dev_name, vlan_dev_name, vid));
3876 }
3877 }
3878 }
3879 }
3880 }
3881}
9d24de3b
JP
3882
3883static void
3884mirror_refresh_stats(struct mirror *m)
3885{
3886 struct ofproto *ofproto = m->bridge->ofproto;
3887 uint64_t tx_packets, tx_bytes;
3888 char *keys[2];
3889 int64_t values[2];
3890 size_t stat_cnt = 0;
3891
3892 if (ofproto_mirror_get_stats(ofproto, m, &tx_packets, &tx_bytes)) {
3893 ovsrec_mirror_set_statistics(m->cfg, NULL, NULL, 0);
3894 return;
3895 }
3896
3897 if (tx_packets != UINT64_MAX) {
3898 keys[stat_cnt] = "tx_packets";
3899 values[stat_cnt] = tx_packets;
3900 stat_cnt++;
3901 }
3902 if (tx_bytes != UINT64_MAX) {
3903 keys[stat_cnt] = "tx_bytes";
3904 values[stat_cnt] = tx_bytes;
3905 stat_cnt++;
3906 }
3907
3908 ovsrec_mirror_set_statistics(m->cfg, keys, values, stat_cnt);
3909}