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