1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "async-append.h"
26 #include "connectivity.h"
32 #include "openvswitch/hmap.h"
34 #include "if-notifier.h"
37 #include "lib/netdev-dpdk.h"
38 #include "mac-learning.h"
39 #include "mcast-snooping.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/ofp-util.h"
49 #include "openvswitch/ofpbuf.h"
50 #include "openvswitch/vlog.h"
54 #include "poll-loop.h"
56 #include "sflow_api.h"
58 #include "openvswitch/shash.h"
60 #include "socket-util.h"
62 #include "stream-ssl.h"
64 #include "system-stats.h"
68 #include "lib/vswitch-idl.h"
69 #include "xenserver.h"
70 #include "vlan-bitmap.h"
72 VLOG_DEFINE_THIS_MODULE(bridge);
74 COVERAGE_DEFINE(bridge_reconfigure);
77 /* These members are always valid.
79 * They are immutable: they never change between iface_create() and
81 struct ovs_list port_elem; /* Element in struct port's "ifaces" list. */
82 struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
83 struct hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
84 struct port *port; /* Containing port. */
85 char *name; /* Host network device name. */
86 struct netdev *netdev; /* Network device. */
87 ofp_port_t ofp_port; /* OpenFlow port number. */
90 /* These members are valid only within bridge_reconfigure(). */
91 const char *type; /* Usually same as cfg->type. */
92 const char *netdev_type; /* type that should be used for netdev_open. */
93 const struct ovsrec_interface *cfg;
97 struct uuid uuid; /* UUID of this "mirror" record in database. */
98 struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
99 struct bridge *bridge;
101 const struct ovsrec_mirror *cfg;
105 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
106 struct bridge *bridge;
109 const struct ovsrec_port *cfg;
111 /* An ordinary bridge port has 1 interface.
112 * A bridge port for bonding has at least 2 interfaces. */
113 struct ovs_list ifaces; /* List of "struct iface"s. */
117 struct hmap_node node; /* In 'all_bridges'. */
118 char *name; /* User-specified arbitrary name. */
119 char *type; /* Datapath type. */
120 struct eth_addr ea; /* Bridge Ethernet Address. */
121 struct eth_addr default_ea; /* Default MAC. */
122 const struct ovsrec_bridge *cfg;
124 /* OpenFlow switch processing. */
125 struct ofproto *ofproto; /* OpenFlow switch. */
128 struct hmap ports; /* "struct port"s indexed by name. */
129 struct hmap ifaces; /* "struct iface"s indexed by ofp_port. */
130 struct hmap iface_by_name; /* "struct iface"s indexed by name. */
132 /* Port mirroring. */
133 struct hmap mirrors; /* "struct mirror" indexed by UUID. */
136 struct hmap mappings; /* "struct" indexed by UUID */
138 /* Used during reconfiguration. */
139 struct shash wanted_ports;
141 /* Synthetic local port if necessary. */
142 struct ovsrec_port synth_local_port;
143 struct ovsrec_interface synth_local_iface;
144 struct ovsrec_interface *synth_local_ifacep;
148 struct hmap_node hmap_node; /* In struct bridge's "mappings" hmap. */
149 struct bridge *bridge;
155 /* All bridges, indexed by name. */
156 static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
158 /* OVSDB IDL used to obtain configuration. */
159 static struct ovsdb_idl *idl;
161 /* We want to complete daemonization, fully detaching from our parent process,
162 * only after we have completed our initial configuration, committed our state
163 * to the database, and received confirmation back from the database server
164 * that it applied the commit. This allows our parent process to know that,
165 * post-detach, ephemeral fields such as datapath-id and ofport are very likely
166 * to have already been filled in. (It is only "very likely" rather than
167 * certain because there is always a slim possibility that the transaction will
168 * fail or that some other client has added new bridges, ports, etc. while
169 * ovs-vswitchd was configuring using an old configuration.)
171 * We only need to do this once for our initial configuration at startup, so
172 * 'initial_config_done' tracks whether we've already done it. While we are
173 * waiting for a response to our commit, 'daemonize_txn' tracks the transaction
174 * itself and is otherwise NULL. */
175 static bool initial_config_done;
176 static struct ovsdb_idl_txn *daemonize_txn;
178 /* Most recently processed IDL sequence number. */
179 static unsigned int idl_seqno;
181 /* Track changes to port connectivity. */
182 static uint64_t connectivity_seqno = LLONG_MIN;
184 /* Status update to database.
186 * Some information in the database must be kept as up-to-date as possible to
187 * allow controllers to respond rapidly to network outages. Those status are
188 * updated via the 'status_txn'.
190 * We use the global connectivity sequence number to detect the status change.
191 * Also, to prevent the status update from sending too much to the database,
192 * we check the return status of each update transaction and do not start new
193 * update if the previous transaction status is 'TXN_INCOMPLETE'.
195 * 'statux_txn' is NULL if there is no ongoing status update.
197 * If the previous database transaction was failed (is not 'TXN_SUCCESS',
198 * 'TXN_UNCHANGED' or 'TXN_INCOMPLETE'), 'status_txn_try_again' is set to true,
199 * which will cause the main thread wake up soon and retry the status update.
201 static struct ovsdb_idl_txn *status_txn;
202 static bool status_txn_try_again;
204 /* When the status update transaction returns 'TXN_INCOMPLETE', should register a
205 * timeout in 'STATUS_CHECK_AGAIN_MSEC' to check again. */
206 #define STATUS_CHECK_AGAIN_MSEC 100
208 /* Statistics update to database. */
209 static struct ovsdb_idl_txn *stats_txn;
211 /* Each time this timer expires, the bridge fetches interface and mirror
212 * statistics and pushes them into the database. */
213 static int stats_timer_interval;
214 static long long int stats_timer = LLONG_MIN;
216 /* Each time this timer expires, the bridge fetches the list of port/VLAN
217 * membership that has been modified by the AA.
219 #define AA_REFRESH_INTERVAL (1000) /* In milliseconds. */
220 static long long int aa_refresh_timer = LLONG_MIN;
222 /* Whenever system interfaces are added, removed or change state, the bridge
223 * will be reconfigured.
225 static struct if_notifier *ifnotifier;
226 static bool ifaces_changed = false;
228 static void add_del_bridges(const struct ovsrec_open_vswitch *);
229 static void bridge_run__(void);
230 static void bridge_create(const struct ovsrec_bridge *);
231 static void bridge_destroy(struct bridge *, bool del);
232 static struct bridge *bridge_lookup(const char *name);
233 static unixctl_cb_func bridge_unixctl_dump_flows;
234 static unixctl_cb_func bridge_unixctl_reconnect;
235 static size_t bridge_get_controllers(const struct bridge *br,
236 struct ovsrec_controller ***controllersp);
237 static void bridge_collect_wanted_ports(struct bridge *,
238 struct shash *wanted_ports);
239 static void bridge_delete_ofprotos(void);
240 static void bridge_delete_or_reconfigure_ports(struct bridge *);
241 static void bridge_del_ports(struct bridge *,
242 const struct shash *wanted_ports);
243 static void bridge_add_ports(struct bridge *,
244 const struct shash *wanted_ports);
246 static void bridge_configure_datapath_id(struct bridge *);
247 static void bridge_configure_netflow(struct bridge *);
248 static void bridge_configure_forward_bpdu(struct bridge *);
249 static void bridge_configure_mac_table(struct bridge *);
250 static void bridge_configure_mcast_snooping(struct bridge *);
251 static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
252 static void bridge_configure_ipfix(struct bridge *);
253 static void bridge_configure_spanning_tree(struct bridge *);
254 static void bridge_configure_tables(struct bridge *);
255 static void bridge_configure_dp_desc(struct bridge *);
256 static void bridge_configure_aa(struct bridge *);
257 static void bridge_aa_refresh_queued(struct bridge *);
258 static bool bridge_aa_need_refresh(struct bridge *);
259 static void bridge_configure_remotes(struct bridge *,
260 const struct sockaddr_in *managers,
262 static void bridge_pick_local_hw_addr(struct bridge *, struct eth_addr *ea,
263 struct iface **hw_addr_iface);
264 static uint64_t bridge_pick_datapath_id(struct bridge *,
265 const struct eth_addr bridge_ea,
266 struct iface *hw_addr_iface);
267 static uint64_t dpid_from_hash(const void *, size_t nbytes);
268 static bool bridge_has_bond_fake_iface(const struct bridge *,
270 static bool port_is_bond_fake_iface(const struct port *);
272 static unixctl_cb_func qos_unixctl_show_types;
273 static unixctl_cb_func qos_unixctl_show;
275 static struct port *port_create(struct bridge *, const struct ovsrec_port *);
276 static void port_del_ifaces(struct port *);
277 static void port_destroy(struct port *);
278 static struct port *port_lookup(const struct bridge *, const char *name);
279 static void port_configure(struct port *);
280 static struct lacp_settings *port_configure_lacp(struct port *,
281 struct lacp_settings *);
282 static void port_configure_bond(struct port *, struct bond_settings *);
283 static bool port_is_synthetic(const struct port *);
285 static void reconfigure_system_stats(const struct ovsrec_open_vswitch *);
286 static void run_system_stats(void);
288 static void bridge_configure_mirrors(struct bridge *);
289 static struct mirror *mirror_create(struct bridge *,
290 const struct ovsrec_mirror *);
291 static void mirror_destroy(struct mirror *);
292 static bool mirror_configure(struct mirror *);
293 static void mirror_refresh_stats(struct mirror *);
295 static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
296 static bool iface_create(struct bridge *, const struct ovsrec_interface *,
297 const struct ovsrec_port *);
298 static bool iface_is_internal(const struct ovsrec_interface *iface,
299 const struct ovsrec_bridge *br);
300 static const char *iface_get_type(const struct ovsrec_interface *,
301 const struct ovsrec_bridge *);
302 static void iface_destroy(struct iface *);
303 static void iface_destroy__(struct iface *);
304 static struct iface *iface_lookup(const struct bridge *, const char *name);
305 static struct iface *iface_find(const char *name);
306 static struct iface *iface_from_ofp_port(const struct bridge *,
307 ofp_port_t ofp_port);
308 static void iface_set_mac(const struct bridge *, const struct port *, struct iface *);
309 static void iface_set_ofport(const struct ovsrec_interface *, ofp_port_t ofport);
310 static void iface_clear_db_record(const struct ovsrec_interface *if_cfg, char *errp);
311 static void iface_configure_qos(struct iface *, const struct ovsrec_qos *);
312 static void iface_configure_cfm(struct iface *);
313 static void iface_refresh_cfm_stats(struct iface *);
314 static void iface_refresh_stats(struct iface *);
315 static void iface_refresh_netdev_status(struct iface *);
316 static void iface_refresh_ofproto_status(struct iface *);
317 static bool iface_is_synthetic(const struct iface *);
318 static ofp_port_t iface_get_requested_ofp_port(
319 const struct ovsrec_interface *);
320 static ofp_port_t iface_pick_ofport(const struct ovsrec_interface *);
323 static void discover_types(const struct ovsrec_open_vswitch *cfg);
326 bridge_init_ofproto(const struct ovsrec_open_vswitch *cfg)
328 struct shash iface_hints;
329 static bool initialized = false;
336 shash_init(&iface_hints);
339 for (i = 0; i < cfg->n_bridges; i++) {
340 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
343 for (j = 0; j < br_cfg->n_ports; j++) {
344 struct ovsrec_port *port_cfg = br_cfg->ports[j];
347 for (k = 0; k < port_cfg->n_interfaces; k++) {
348 struct ovsrec_interface *if_cfg = port_cfg->interfaces[k];
349 struct iface_hint *iface_hint;
351 iface_hint = xmalloc(sizeof *iface_hint);
352 iface_hint->br_name = br_cfg->name;
353 iface_hint->br_type = br_cfg->datapath_type;
354 iface_hint->ofp_port = iface_pick_ofport(if_cfg);
356 shash_add(&iface_hints, if_cfg->name, iface_hint);
362 ofproto_init(&iface_hints);
364 shash_destroy_free_data(&iface_hints);
369 if_change_cb(void *aux OVS_UNUSED)
371 ifaces_changed = true;
374 /* Public functions. */
376 /* Initializes the bridge module, configuring it to obtain its configuration
377 * from an OVSDB server accessed over 'remote', which should be a string in a
378 * form acceptable to ovsdb_idl_create(). */
380 bridge_init(const char *remote)
382 /* Create connection to database. */
383 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true, true);
384 idl_seqno = ovsdb_idl_get_seqno(idl);
385 ovsdb_idl_set_lock(idl, "ovs_vswitchd");
386 ovsdb_idl_verify_write_only(idl);
388 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
389 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
390 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_datapath_types);
391 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_iface_types);
392 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
393 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
394 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
395 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
396 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
398 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
399 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_version);
400 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_status);
401 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_rstp_status);
402 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_stp_enable);
403 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_rstp_enable);
404 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
406 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_status);
407 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_rstp_status);
408 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_rstp_statistics);
409 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_statistics);
410 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_bond_active_slave);
411 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
412 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_trunks);
413 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_vlan_mode);
414 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
415 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
416 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
417 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
418 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_resets);
419 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mac_in_use);
420 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ifindex);
421 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
422 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
423 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
424 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
425 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault);
426 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault_status);
427 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_mpids);
428 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_flap_count);
429 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_health);
430 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_opstate);
431 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_bfd_status);
432 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_lacp_current);
433 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_error);
434 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
436 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
437 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
438 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
439 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
441 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
443 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
445 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
446 ovsdb_idl_omit_alert(idl, &ovsrec_mirror_col_statistics);
448 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
449 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
450 ovsdb_idl_omit(idl, &ovsrec_ipfix_col_external_ids);
451 ovsdb_idl_omit(idl, &ovsrec_flow_sample_collector_set_col_external_ids);
453 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
454 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
455 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
456 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
457 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
459 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
461 /* Register unixctl commands. */
462 unixctl_command_register("qos/show-types", "interface", 1, 1,
463 qos_unixctl_show_types, NULL);
464 unixctl_command_register("qos/show", "interface", 1, 1,
465 qos_unixctl_show, NULL);
466 unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
467 bridge_unixctl_dump_flows, NULL);
468 unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
469 bridge_unixctl_reconnect, NULL);
478 ifnotifier = if_notifier_create(if_change_cb, NULL);
484 struct bridge *br, *next_br;
486 if_notifier_destroy(ifnotifier);
487 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
488 bridge_destroy(br, false);
490 ovsdb_idl_destroy(idl);
493 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
494 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
495 * responsible for freeing '*managersp' (with free()).
497 * You may be asking yourself "why does ovs-vswitchd care?", because
498 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
499 * should not be and in fact is not directly involved in that. But
500 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
501 * it has to tell in-band control where the managers are to enable that.
502 * (Thus, only managers connected in-band and with non-loopback addresses
506 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
507 struct sockaddr_in **managersp, size_t *n_managersp)
509 struct sockaddr_in *managers = NULL;
510 size_t n_managers = 0;
514 /* Collect all of the potential targets from the "targets" columns of the
515 * rows pointed to by "manager_options", excluding any that are
518 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
519 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
521 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
522 sset_find_and_delete(&targets, m->target);
524 sset_add(&targets, m->target);
528 /* Now extract the targets' IP addresses. */
529 if (!sset_is_empty(&targets)) {
532 managers = xmalloc(sset_count(&targets) * sizeof *managers);
533 SSET_FOR_EACH (target, &targets) {
535 struct sockaddr_storage ss;
536 struct sockaddr_in in;
539 /* Ignore loopback. */
540 if (stream_parse_target_with_default_port(target, OVSDB_PORT,
542 && sa.ss.ss_family == AF_INET
543 && sa.in.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
544 managers[n_managers++] = sa.in;
548 sset_destroy(&targets);
550 *managersp = managers;
551 *n_managersp = n_managers;
555 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
557 struct sockaddr_in *managers;
558 struct bridge *br, *next;
559 int sflow_bridge_number;
562 COVERAGE_INC(bridge_reconfigure);
564 ofproto_set_flow_limit(smap_get_int(&ovs_cfg->other_config, "flow-limit",
565 OFPROTO_FLOW_LIMIT_DEFAULT));
566 ofproto_set_max_idle(smap_get_int(&ovs_cfg->other_config, "max-idle",
567 OFPROTO_MAX_IDLE_DEFAULT));
568 ofproto_set_cpu_mask(smap_get(&ovs_cfg->other_config, "pmd-cpu-mask"));
571 smap_get_int(&ovs_cfg->other_config, "n-handler-threads", 0),
572 smap_get_int(&ovs_cfg->other_config, "n-revalidator-threads", 0));
574 /* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
575 * to 'ovs_cfg', with only very minimal configuration otherwise.
577 * This is mostly an update to bridge data structures. Nothing is pushed
578 * down to ofproto or lower layers. */
579 add_del_bridges(ovs_cfg);
580 HMAP_FOR_EACH (br, node, &all_bridges) {
581 bridge_collect_wanted_ports(br, &br->wanted_ports);
582 bridge_del_ports(br, &br->wanted_ports);
585 /* Start pushing configuration changes down to the ofproto layer:
587 * - Delete ofprotos that are no longer configured.
589 * - Delete ports that are no longer configured.
591 * - Reconfigure existing ports to their desired configurations, or
592 * delete them if not possible.
594 * We have to do all the deletions before we can do any additions, because
595 * the ports to be added might require resources that will be freed up by
596 * deletions (they might especially overlap in name). */
597 bridge_delete_ofprotos();
598 HMAP_FOR_EACH (br, node, &all_bridges) {
600 bridge_delete_or_reconfigure_ports(br);
604 /* Finish pushing configuration changes to the ofproto layer:
606 * - Create ofprotos that are missing.
608 * - Add ports that are missing. */
609 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
613 error = ofproto_create(br->name, br->type, &br->ofproto);
615 VLOG_ERR("failed to create bridge %s: %s", br->name,
616 ovs_strerror(error));
617 shash_destroy(&br->wanted_ports);
618 bridge_destroy(br, true);
620 /* Trigger storing datapath version. */
621 seq_change(connectivity_seq_get());
625 HMAP_FOR_EACH (br, node, &all_bridges) {
626 bridge_add_ports(br, &br->wanted_ports);
627 shash_destroy(&br->wanted_ports);
630 reconfigure_system_stats(ovs_cfg);
632 /* Complete the configuration. */
633 sflow_bridge_number = 0;
634 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
635 HMAP_FOR_EACH (br, node, &all_bridges) {
638 /* We need the datapath ID early to allow LACP ports to use it as the
639 * default system ID. */
640 bridge_configure_datapath_id(br);
642 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
645 port_configure(port);
647 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
648 iface_set_ofport(iface->cfg, iface->ofp_port);
649 /* Clear eventual previous errors */
650 ovsrec_interface_set_error(iface->cfg, NULL);
651 iface_configure_cfm(iface);
652 iface_configure_qos(iface, port->cfg->qos);
653 iface_set_mac(br, port, iface);
654 ofproto_port_set_bfd(br->ofproto, iface->ofp_port,
656 ofproto_port_set_lldp(br->ofproto, iface->ofp_port,
660 bridge_configure_mirrors(br);
661 bridge_configure_forward_bpdu(br);
662 bridge_configure_mac_table(br);
663 bridge_configure_mcast_snooping(br);
664 bridge_configure_remotes(br, managers, n_managers);
665 bridge_configure_netflow(br);
666 bridge_configure_sflow(br, &sflow_bridge_number);
667 bridge_configure_ipfix(br);
668 bridge_configure_spanning_tree(br);
669 bridge_configure_tables(br);
670 bridge_configure_dp_desc(br);
671 bridge_configure_aa(br);
675 /* The ofproto-dpif provider does some final reconfiguration in its
676 * ->type_run() function. We have to call it before notifying the database
677 * client that reconfiguration is complete, otherwise there is a very
678 * narrow race window in which e.g. ofproto/trace will not recognize the
679 * new configuration (sometimes this causes unit test failures). */
683 /* Delete ofprotos which aren't configured or have the wrong type. Create
684 * ofprotos which don't exist but need to. */
686 bridge_delete_ofprotos(void)
693 /* Delete ofprotos with no bridge or with the wrong type. */
696 ofproto_enumerate_types(&types);
697 SSET_FOR_EACH (type, &types) {
700 ofproto_enumerate_names(type, &names);
701 SSET_FOR_EACH (name, &names) {
702 br = bridge_lookup(name);
703 if (!br || strcmp(type, br->type)) {
704 ofproto_delete(name, type);
708 sset_destroy(&names);
709 sset_destroy(&types);
713 add_ofp_port(ofp_port_t port, ofp_port_t *ports, size_t *n, size_t *allocated)
715 if (*n >= *allocated) {
716 ports = x2nrealloc(ports, allocated, sizeof *ports);
718 ports[(*n)++] = port;
723 bridge_delete_or_reconfigure_ports(struct bridge *br)
725 struct ofproto_port ofproto_port;
726 struct ofproto_port_dump dump;
728 struct sset ofproto_ports;
729 struct port *port, *port_next;
731 /* List of "ofp_port"s to delete. We make a list instead of deleting them
732 * right away because ofproto implementations aren't necessarily able to
733 * iterate through a changing list of ports in an entirely robust way. */
740 sset_init(&ofproto_ports);
742 /* Main task: Iterate over the ports in 'br->ofproto' and remove the ports
743 * that are not configured in the database. (This commonly happens when
744 * ports have been deleted, e.g. with "ovs-vsctl del-port".)
746 * Side tasks: Reconfigure the ports that are still in 'br'. Delete ports
747 * that have the wrong OpenFlow port number (and arrange to add them back
748 * with the correct OpenFlow port number). */
749 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
750 ofp_port_t requested_ofp_port;
753 sset_add(&ofproto_ports, ofproto_port.name);
755 iface = iface_lookup(br, ofproto_port.name);
757 /* No such iface is configured, so we should delete this
760 * As a corner case exception, keep the port if it's a bond fake
762 if (bridge_has_bond_fake_iface(br, ofproto_port.name)
763 && !strcmp(ofproto_port.type, "internal")) {
769 if (strcmp(ofproto_port.type, iface->netdev_type)
770 || netdev_set_config(iface->netdev, &iface->cfg->options, NULL)) {
771 /* The interface is the wrong type or can't be configured.
776 /* If the requested OpenFlow port for 'iface' changed, and it's not
777 * already the correct port, then we might want to temporarily delete
778 * this interface, so we can add it back again with the new OpenFlow
780 requested_ofp_port = iface_get_requested_ofp_port(iface->cfg);
781 if (iface->ofp_port != OFPP_LOCAL &&
782 requested_ofp_port != OFPP_NONE &&
783 requested_ofp_port != iface->ofp_port) {
784 ofp_port_t victim_request;
785 struct iface *victim;
787 /* Check for an existing OpenFlow port currently occupying
788 * 'iface''s requested port number. If there isn't one, then
789 * delete this port. Otherwise we need to consider further. */
790 victim = iface_from_ofp_port(br, requested_ofp_port);
795 /* 'victim' is a port currently using 'iface''s requested port
796 * number. Unless 'victim' specifically requested that port
797 * number, too, then we can delete both 'iface' and 'victim'
798 * temporarily. (We'll add both of them back again later with new
799 * OpenFlow port numbers.)
801 * If 'victim' did request port number 'requested_ofp_port', just
802 * like 'iface', then that's a configuration inconsistency that we
803 * can't resolve. We might as well let it keep its current port
805 victim_request = iface_get_requested_ofp_port(victim->cfg);
806 if (victim_request != requested_ofp_port) {
807 del = add_ofp_port(victim->ofp_port, del, &n, &allocated);
808 iface_destroy(victim);
817 iface_destroy(iface);
818 del = add_ofp_port(ofproto_port.ofp_port, del, &n, &allocated);
820 for (i = 0; i < n; i++) {
821 ofproto_port_del(br->ofproto, del[i]);
825 /* Iterate over this module's idea of interfaces in 'br'. Remove any ports
826 * that we didn't see when we iterated through the datapath, i.e. ports
827 * that disappeared underneath use. This is an unusual situation, but it
828 * can happen in some cases:
830 * - An admin runs a command like "ovs-dpctl del-port" (which is a bad
831 * idea but could happen).
833 * - The port represented a device that disappeared, e.g. a tuntap
834 * device destroyed via "tunctl -d", a physical Ethernet device
835 * whose module was just unloaded via "rmmod", or a virtual NIC for a
836 * VM whose VM was just terminated. */
837 HMAP_FOR_EACH_SAFE (port, port_next, hmap_node, &br->ports) {
838 struct iface *iface, *iface_next;
840 LIST_FOR_EACH_SAFE (iface, iface_next, port_elem, &port->ifaces) {
841 if (!sset_contains(&ofproto_ports, iface->name)) {
842 iface_destroy__(iface);
846 if (ovs_list_is_empty(&port->ifaces)) {
850 sset_destroy(&ofproto_ports);
854 bridge_add_ports__(struct bridge *br, const struct shash *wanted_ports,
855 bool with_requested_port)
857 struct shash_node *port_node;
859 SHASH_FOR_EACH (port_node, wanted_ports) {
860 const struct ovsrec_port *port_cfg = port_node->data;
863 for (i = 0; i < port_cfg->n_interfaces; i++) {
864 const struct ovsrec_interface *iface_cfg = port_cfg->interfaces[i];
865 ofp_port_t requested_ofp_port;
867 requested_ofp_port = iface_get_requested_ofp_port(iface_cfg);
868 if ((requested_ofp_port != OFPP_NONE) == with_requested_port) {
869 struct iface *iface = iface_lookup(br, iface_cfg->name);
872 iface_create(br, iface_cfg, port_cfg);
880 bridge_add_ports(struct bridge *br, const struct shash *wanted_ports)
882 /* First add interfaces that request a particular port number. */
883 bridge_add_ports__(br, wanted_ports, true);
885 /* Then add interfaces that want automatic port number assignment.
886 * We add these afterward to avoid accidentally taking a specifically
887 * requested port number. */
888 bridge_add_ports__(br, wanted_ports, false);
892 port_configure(struct port *port)
894 const struct ovsrec_port *cfg = port->cfg;
895 struct bond_settings bond_settings;
896 struct lacp_settings lacp_settings;
897 struct ofproto_bundle_settings s;
905 s.slaves = xmalloc(ovs_list_size(&port->ifaces) * sizeof *s.slaves);
906 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
907 s.slaves[s.n_slaves++] = iface->ofp_port;
912 if (cfg->tag && *cfg->tag >= 0 && *cfg->tag <= 4095) {
916 /* Get VLAN trunks. */
919 s.trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
923 if (cfg->vlan_mode) {
924 if (!strcmp(cfg->vlan_mode, "access")) {
925 s.vlan_mode = PORT_VLAN_ACCESS;
926 } else if (!strcmp(cfg->vlan_mode, "trunk")) {
927 s.vlan_mode = PORT_VLAN_TRUNK;
928 } else if (!strcmp(cfg->vlan_mode, "native-tagged")) {
929 s.vlan_mode = PORT_VLAN_NATIVE_TAGGED;
930 } else if (!strcmp(cfg->vlan_mode, "native-untagged")) {
931 s.vlan_mode = PORT_VLAN_NATIVE_UNTAGGED;
933 /* This "can't happen" because ovsdb-server should prevent it. */
934 VLOG_WARN("port %s: unknown VLAN mode %s, falling "
935 "back to trunk mode", port->name, cfg->vlan_mode);
936 s.vlan_mode = PORT_VLAN_TRUNK;
940 s.vlan_mode = PORT_VLAN_ACCESS;
942 VLOG_WARN("port %s: ignoring trunks in favor of implicit vlan",
946 s.vlan_mode = PORT_VLAN_TRUNK;
949 s.use_priority_tags = smap_get_bool(&cfg->other_config, "priority-tags",
952 /* Get LACP settings. */
953 s.lacp = port_configure_lacp(port, &lacp_settings);
957 s.lacp_slaves = xmalloc(s.n_slaves * sizeof *s.lacp_slaves);
958 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
959 iface_configure_lacp(iface, &s.lacp_slaves[i++]);
962 s.lacp_slaves = NULL;
965 /* Get bond settings. */
966 if (s.n_slaves > 1) {
967 s.bond = &bond_settings;
968 port_configure_bond(port, &bond_settings);
971 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
972 netdev_set_miimon_interval(iface->netdev, 0);
977 ofproto_bundle_register(port->bridge->ofproto, port, &s);
985 /* Pick local port hardware address and datapath ID for 'br'. */
987 bridge_configure_datapath_id(struct bridge *br)
991 struct iface *local_iface;
992 struct iface *hw_addr_iface;
995 bridge_pick_local_hw_addr(br, &ea, &hw_addr_iface);
996 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
998 int error = netdev_set_etheraddr(local_iface->netdev, ea);
1000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1001 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
1002 "Ethernet address: %s",
1003 br->name, ovs_strerror(error));
1008 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
1009 if (dpid != ofproto_get_datapath_id(br->ofproto)) {
1010 VLOG_INFO("bridge %s: using datapath ID %016"PRIx64, br->name, dpid);
1011 ofproto_set_datapath_id(br->ofproto, dpid);
1014 dpid_string = xasprintf("%016"PRIx64, dpid);
1015 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
1019 /* Returns a bitmap of "enum ofputil_protocol"s that are allowed for use with
1022 bridge_get_allowed_versions(struct bridge *br)
1024 if (!br->cfg->n_protocols) {
1028 return ofputil_versions_from_strings(br->cfg->protocols,
1029 br->cfg->n_protocols);
1032 /* Set NetFlow configuration on 'br'. */
1034 bridge_configure_netflow(struct bridge *br)
1036 struct ovsrec_netflow *cfg = br->cfg->netflow;
1037 struct netflow_options opts;
1040 ofproto_set_netflow(br->ofproto, NULL);
1044 memset(&opts, 0, sizeof opts);
1046 /* Get default NetFlow configuration from datapath.
1047 * Apply overrides from 'cfg'. */
1048 ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
1049 if (cfg->engine_type) {
1050 opts.engine_type = *cfg->engine_type;
1052 if (cfg->engine_id) {
1053 opts.engine_id = *cfg->engine_id;
1056 /* Configure active timeout interval. */
1057 opts.active_timeout = cfg->active_timeout;
1058 if (!opts.active_timeout) {
1059 opts.active_timeout = -1;
1060 } else if (opts.active_timeout < 0) {
1061 VLOG_WARN("bridge %s: active timeout interval set to negative "
1062 "value, using default instead (%d seconds)", br->name,
1063 NF_ACTIVE_TIMEOUT_DEFAULT);
1064 opts.active_timeout = -1;
1067 /* Add engine ID to interface number to disambiguate bridgs? */
1068 opts.add_id_to_iface = cfg->add_id_to_interface;
1069 if (opts.add_id_to_iface) {
1070 if (opts.engine_id > 0x7f) {
1071 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
1072 "another vswitch, choose an engine id less than 128",
1075 if (hmap_count(&br->ports) > 508) {
1076 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
1077 "another port when more than 508 ports are used",
1083 sset_init(&opts.collectors);
1084 sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
1087 if (ofproto_set_netflow(br->ofproto, &opts)) {
1088 VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
1090 sset_destroy(&opts.collectors);
1093 /* Set sFlow configuration on 'br'. */
1095 bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
1097 const struct ovsrec_sflow *cfg = br->cfg->sflow;
1098 struct ovsrec_controller **controllers;
1099 struct ofproto_sflow_options oso;
1100 size_t n_controllers;
1104 ofproto_set_sflow(br->ofproto, NULL);
1108 memset(&oso, 0, sizeof oso);
1110 sset_init(&oso.targets);
1111 sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
1113 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
1114 if (cfg->sampling) {
1115 oso.sampling_rate = *cfg->sampling;
1118 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
1120 oso.polling_interval = *cfg->polling;
1123 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
1125 oso.header_len = *cfg->header;
1128 oso.sub_id = (*sflow_bridge_number)++;
1129 oso.agent_device = cfg->agent;
1131 oso.control_ip = NULL;
1132 n_controllers = bridge_get_controllers(br, &controllers);
1133 for (i = 0; i < n_controllers; i++) {
1134 if (controllers[i]->local_ip) {
1135 oso.control_ip = controllers[i]->local_ip;
1139 ofproto_set_sflow(br->ofproto, &oso);
1141 sset_destroy(&oso.targets);
1144 /* Returns whether a IPFIX row is valid. */
1146 ovsrec_ipfix_is_valid(const struct ovsrec_ipfix *ipfix)
1148 return ipfix && ipfix->n_targets > 0;
1151 /* Returns whether a Flow_Sample_Collector_Set row is valid. */
1153 ovsrec_fscs_is_valid(const struct ovsrec_flow_sample_collector_set *fscs,
1154 const struct bridge *br)
1156 return ovsrec_ipfix_is_valid(fscs->ipfix) && fscs->bridge == br->cfg;
1159 /* Set IPFIX configuration on 'br'. */
1161 bridge_configure_ipfix(struct bridge *br)
1163 const struct ovsrec_ipfix *be_cfg = br->cfg->ipfix;
1164 bool valid_be_cfg = ovsrec_ipfix_is_valid(be_cfg);
1165 const struct ovsrec_flow_sample_collector_set *fe_cfg;
1166 struct ofproto_ipfix_bridge_exporter_options be_opts;
1167 struct ofproto_ipfix_flow_exporter_options *fe_opts = NULL;
1168 size_t n_fe_opts = 0;
1169 const char *virtual_obs_id;
1171 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg, idl) {
1172 if (ovsrec_fscs_is_valid(fe_cfg, br)) {
1177 if (!valid_be_cfg && n_fe_opts == 0) {
1178 ofproto_set_ipfix(br->ofproto, NULL, NULL, 0);
1183 memset(&be_opts, 0, sizeof be_opts);
1185 sset_init(&be_opts.targets);
1186 sset_add_array(&be_opts.targets, be_cfg->targets, be_cfg->n_targets);
1188 if (be_cfg->sampling) {
1189 be_opts.sampling_rate = *be_cfg->sampling;
1191 be_opts.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
1193 if (be_cfg->obs_domain_id) {
1194 be_opts.obs_domain_id = *be_cfg->obs_domain_id;
1196 if (be_cfg->obs_point_id) {
1197 be_opts.obs_point_id = *be_cfg->obs_point_id;
1199 if (be_cfg->cache_active_timeout) {
1200 be_opts.cache_active_timeout = *be_cfg->cache_active_timeout;
1202 if (be_cfg->cache_max_flows) {
1203 be_opts.cache_max_flows = *be_cfg->cache_max_flows;
1206 be_opts.enable_tunnel_sampling = smap_get_bool(&be_cfg->other_config,
1207 "enable-tunnel-sampling", true);
1209 be_opts.enable_input_sampling = !smap_get_bool(&be_cfg->other_config,
1210 "enable-input-sampling", false);
1212 be_opts.enable_output_sampling = !smap_get_bool(&be_cfg->other_config,
1213 "enable-output-sampling", false);
1215 virtual_obs_id = smap_get(&be_cfg->other_config, "virtual_obs_id");
1216 be_opts.virtual_obs_id = nullable_xstrdup(virtual_obs_id);
1219 if (n_fe_opts > 0) {
1220 struct ofproto_ipfix_flow_exporter_options *opts;
1221 fe_opts = xcalloc(n_fe_opts, sizeof *fe_opts);
1223 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg, idl) {
1224 if (ovsrec_fscs_is_valid(fe_cfg, br)) {
1225 opts->collector_set_id = fe_cfg->id;
1226 sset_init(&opts->targets);
1227 sset_add_array(&opts->targets, fe_cfg->ipfix->targets,
1228 fe_cfg->ipfix->n_targets);
1229 opts->cache_active_timeout = fe_cfg->ipfix->cache_active_timeout
1230 ? *fe_cfg->ipfix->cache_active_timeout : 0;
1231 opts->cache_max_flows = fe_cfg->ipfix->cache_max_flows
1232 ? *fe_cfg->ipfix->cache_max_flows : 0;
1233 opts->enable_tunnel_sampling = smap_get_bool(
1234 &fe_cfg->ipfix->other_config,
1235 "enable-tunnel-sampling", true);
1236 virtual_obs_id = smap_get(&fe_cfg->ipfix->other_config,
1238 opts->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
1244 ofproto_set_ipfix(br->ofproto, valid_be_cfg ? &be_opts : NULL, fe_opts,
1248 sset_destroy(&be_opts.targets);
1249 free(be_opts.virtual_obs_id);
1252 if (n_fe_opts > 0) {
1253 struct ofproto_ipfix_flow_exporter_options *opts = fe_opts;
1255 for (i = 0; i < n_fe_opts; i++) {
1256 sset_destroy(&opts->targets);
1257 free(opts->virtual_obs_id);
1265 port_configure_stp(const struct ofproto *ofproto, struct port *port,
1266 struct ofproto_port_stp_settings *port_s,
1267 int *port_num_counter, unsigned long *port_num_bitmap)
1269 const char *config_str;
1270 struct iface *iface;
1272 if (!smap_get_bool(&port->cfg->other_config, "stp-enable", true)) {
1273 port_s->enable = false;
1276 port_s->enable = true;
1279 /* STP over bonds is not supported. */
1280 if (!ovs_list_is_singleton(&port->ifaces)) {
1281 VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
1283 port_s->enable = false;
1287 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
1289 /* Internal ports shouldn't participate in spanning tree, so
1291 if (!strcmp(iface->type, "internal")) {
1292 VLOG_DBG("port %s: disable STP on internal ports", port->name);
1293 port_s->enable = false;
1297 /* STP on mirror output ports is not supported. */
1298 if (ofproto_is_mirror_output_bundle(ofproto, port)) {
1299 VLOG_DBG("port %s: disable STP on mirror ports", port->name);
1300 port_s->enable = false;
1304 config_str = smap_get(&port->cfg->other_config, "stp-port-num");
1306 unsigned long int port_num = strtoul(config_str, NULL, 0);
1307 int port_idx = port_num - 1;
1309 if (port_num < 1 || port_num > STP_MAX_PORTS) {
1310 VLOG_ERR("port %s: invalid stp-port-num", port->name);
1311 port_s->enable = false;
1315 if (bitmap_is_set(port_num_bitmap, port_idx)) {
1316 VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
1317 port->name, port_num);
1318 port_s->enable = false;
1321 bitmap_set1(port_num_bitmap, port_idx);
1322 port_s->port_num = port_idx;
1324 if (*port_num_counter >= STP_MAX_PORTS) {
1325 VLOG_ERR("port %s: too many STP ports, disabling", port->name);
1326 port_s->enable = false;
1330 port_s->port_num = (*port_num_counter)++;
1333 config_str = smap_get(&port->cfg->other_config, "stp-path-cost");
1335 port_s->path_cost = strtoul(config_str, NULL, 10);
1337 enum netdev_features current;
1340 netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1341 mbps = netdev_features_to_bps(current, 100 * 1000 * 1000) / 1000000;
1342 port_s->path_cost = stp_convert_speed_to_cost(mbps);
1345 config_str = smap_get(&port->cfg->other_config, "stp-port-priority");
1347 port_s->priority = strtoul(config_str, NULL, 0);
1349 port_s->priority = STP_DEFAULT_PORT_PRIORITY;
1354 port_configure_rstp(const struct ofproto *ofproto, struct port *port,
1355 struct ofproto_port_rstp_settings *port_s, int *port_num_counter)
1357 const char *config_str;
1358 struct iface *iface;
1360 if (!smap_get_bool(&port->cfg->other_config, "rstp-enable", true)) {
1361 port_s->enable = false;
1364 port_s->enable = true;
1367 /* RSTP over bonds is not supported. */
1368 if (!ovs_list_is_singleton(&port->ifaces)) {
1369 VLOG_ERR("port %s: cannot enable RSTP on bonds, disabling",
1371 port_s->enable = false;
1375 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
1377 /* Internal ports shouldn't participate in spanning tree, so
1379 if (!strcmp(iface->type, "internal")) {
1380 VLOG_DBG("port %s: disable RSTP on internal ports", port->name);
1381 port_s->enable = false;
1385 /* RSTP on mirror output ports is not supported. */
1386 if (ofproto_is_mirror_output_bundle(ofproto, port)) {
1387 VLOG_DBG("port %s: disable RSTP on mirror ports", port->name);
1388 port_s->enable = false;
1392 config_str = smap_get(&port->cfg->other_config, "rstp-port-num");
1394 unsigned long int port_num = strtoul(config_str, NULL, 0);
1395 if (port_num < 1 || port_num > RSTP_MAX_PORTS) {
1396 VLOG_ERR("port %s: invalid rstp-port-num", port->name);
1397 port_s->enable = false;
1400 port_s->port_num = port_num;
1402 if (*port_num_counter >= RSTP_MAX_PORTS) {
1403 VLOG_ERR("port %s: too many RSTP ports, disabling", port->name);
1404 port_s->enable = false;
1407 /* If rstp-port-num is not specified, use 0.
1408 * rstp_port_set_port_number() will look for the first free one. */
1409 port_s->port_num = 0;
1412 config_str = smap_get(&port->cfg->other_config, "rstp-path-cost");
1414 port_s->path_cost = strtoul(config_str, NULL, 10);
1416 enum netdev_features current;
1419 netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1420 mbps = netdev_features_to_bps(current, 100 * 1000 * 1000) / 1000000;
1421 port_s->path_cost = rstp_convert_speed_to_cost(mbps);
1424 config_str = smap_get(&port->cfg->other_config, "rstp-port-priority");
1426 port_s->priority = strtoul(config_str, NULL, 0);
1428 port_s->priority = RSTP_DEFAULT_PORT_PRIORITY;
1431 config_str = smap_get(&port->cfg->other_config, "rstp-admin-p2p-mac");
1433 port_s->admin_p2p_mac_state = strtoul(config_str, NULL, 0);
1435 port_s->admin_p2p_mac_state = RSTP_ADMIN_P2P_MAC_FORCE_TRUE;
1438 port_s->admin_port_state = smap_get_bool(&port->cfg->other_config,
1439 "rstp-admin-port-state", true);
1441 port_s->admin_edge_port = smap_get_bool(&port->cfg->other_config,
1442 "rstp-port-admin-edge", false);
1443 port_s->auto_edge = smap_get_bool(&port->cfg->other_config,
1444 "rstp-port-auto-edge", true);
1445 port_s->mcheck = smap_get_bool(&port->cfg->other_config,
1446 "rstp-port-mcheck", false);
1449 /* Set spanning tree configuration on 'br'. */
1451 bridge_configure_stp(struct bridge *br, bool enable_stp)
1454 ofproto_set_stp(br->ofproto, NULL);
1456 struct ofproto_stp_settings br_s;
1457 const char *config_str;
1459 int port_num_counter;
1460 unsigned long *port_num_bitmap;
1462 config_str = smap_get(&br->cfg->other_config, "stp-system-id");
1466 if (eth_addr_from_string(config_str, &ea)) {
1467 br_s.system_id = eth_addr_to_uint64(ea);
1469 br_s.system_id = eth_addr_to_uint64(br->ea);
1470 VLOG_ERR("bridge %s: invalid stp-system-id, defaulting "
1471 "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
1474 br_s.system_id = eth_addr_to_uint64(br->ea);
1477 config_str = smap_get(&br->cfg->other_config, "stp-priority");
1479 br_s.priority = strtoul(config_str, NULL, 0);
1481 br_s.priority = STP_DEFAULT_BRIDGE_PRIORITY;
1484 config_str = smap_get(&br->cfg->other_config, "stp-hello-time");
1486 br_s.hello_time = strtoul(config_str, NULL, 10) * 1000;
1488 br_s.hello_time = STP_DEFAULT_HELLO_TIME;
1491 config_str = smap_get(&br->cfg->other_config, "stp-max-age");
1493 br_s.max_age = strtoul(config_str, NULL, 10) * 1000;
1495 br_s.max_age = STP_DEFAULT_MAX_AGE;
1498 config_str = smap_get(&br->cfg->other_config, "stp-forward-delay");
1500 br_s.fwd_delay = strtoul(config_str, NULL, 10) * 1000;
1502 br_s.fwd_delay = STP_DEFAULT_FWD_DELAY;
1505 /* Configure STP on the bridge. */
1506 if (ofproto_set_stp(br->ofproto, &br_s)) {
1507 VLOG_ERR("bridge %s: could not enable STP", br->name);
1511 /* Users must either set the port number with the "stp-port-num"
1512 * configuration on all ports or none. If manual configuration
1513 * is not done, then we allocate them sequentially. */
1514 port_num_counter = 0;
1515 port_num_bitmap = bitmap_allocate(STP_MAX_PORTS);
1516 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1517 struct ofproto_port_stp_settings port_s;
1518 struct iface *iface;
1520 port_configure_stp(br->ofproto, port, &port_s,
1521 &port_num_counter, port_num_bitmap);
1523 /* As bonds are not supported, just apply configuration to
1524 * all interfaces. */
1525 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1526 if (ofproto_port_set_stp(br->ofproto, iface->ofp_port,
1528 VLOG_ERR("port %s: could not enable STP", port->name);
1534 if (bitmap_scan(port_num_bitmap, 1, 0, STP_MAX_PORTS) != STP_MAX_PORTS
1535 && port_num_counter) {
1536 VLOG_ERR("bridge %s: must manually configure all STP port "
1537 "IDs or none, disabling", br->name);
1538 ofproto_set_stp(br->ofproto, NULL);
1540 bitmap_free(port_num_bitmap);
1545 bridge_configure_rstp(struct bridge *br, bool enable_rstp)
1548 ofproto_set_rstp(br->ofproto, NULL);
1550 struct ofproto_rstp_settings br_s;
1551 const char *config_str;
1553 int port_num_counter;
1555 config_str = smap_get(&br->cfg->other_config, "rstp-address");
1559 if (eth_addr_from_string(config_str, &ea)) {
1560 br_s.address = eth_addr_to_uint64(ea);
1563 br_s.address = eth_addr_to_uint64(br->ea);
1564 VLOG_ERR("bridge %s: invalid rstp-address, defaulting "
1565 "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
1569 br_s.address = eth_addr_to_uint64(br->ea);
1572 config_str = smap_get(&br->cfg->other_config, "rstp-priority");
1574 br_s.priority = strtoul(config_str, NULL, 0);
1576 br_s.priority = RSTP_DEFAULT_PRIORITY;
1579 config_str = smap_get(&br->cfg->other_config, "rstp-ageing-time");
1581 br_s.ageing_time = strtoul(config_str, NULL, 0);
1583 br_s.ageing_time = RSTP_DEFAULT_AGEING_TIME;
1586 config_str = smap_get(&br->cfg->other_config,
1587 "rstp-force-protocol-version");
1589 br_s.force_protocol_version = strtoul(config_str, NULL, 0);
1591 br_s.force_protocol_version = FPV_DEFAULT;
1594 config_str = smap_get(&br->cfg->other_config, "rstp-max-age");
1596 br_s.bridge_max_age = strtoul(config_str, NULL, 10);
1598 br_s.bridge_max_age = RSTP_DEFAULT_BRIDGE_MAX_AGE;
1601 config_str = smap_get(&br->cfg->other_config, "rstp-forward-delay");
1603 br_s.bridge_forward_delay = strtoul(config_str, NULL, 10);
1605 br_s.bridge_forward_delay = RSTP_DEFAULT_BRIDGE_FORWARD_DELAY;
1608 config_str = smap_get(&br->cfg->other_config,
1609 "rstp-transmit-hold-count");
1611 br_s.transmit_hold_count = strtoul(config_str, NULL, 10);
1613 br_s.transmit_hold_count = RSTP_DEFAULT_TRANSMIT_HOLD_COUNT;
1616 /* Configure RSTP on the bridge. */
1617 if (ofproto_set_rstp(br->ofproto, &br_s)) {
1618 VLOG_ERR("bridge %s: could not enable RSTP", br->name);
1622 port_num_counter = 0;
1623 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1624 struct ofproto_port_rstp_settings port_s;
1625 struct iface *iface;
1627 port_configure_rstp(br->ofproto, port, &port_s,
1630 /* As bonds are not supported, just apply configuration to
1631 * all interfaces. */
1632 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1633 if (ofproto_port_set_rstp(br->ofproto, iface->ofp_port,
1635 VLOG_ERR("port %s: could not enable RSTP", port->name);
1644 bridge_configure_spanning_tree(struct bridge *br)
1646 bool enable_rstp = br->cfg->rstp_enable;
1647 bool enable_stp = br->cfg->stp_enable;
1649 if (enable_rstp && enable_stp) {
1650 VLOG_WARN("%s: RSTP and STP are mutually exclusive but both are "
1651 "configured; enabling RSTP", br->name);
1655 bridge_configure_stp(br, enable_stp);
1656 bridge_configure_rstp(br, enable_rstp);
1660 bridge_has_bond_fake_iface(const struct bridge *br, const char *name)
1662 const struct port *port = port_lookup(br, name);
1663 return port && port_is_bond_fake_iface(port);
1667 port_is_bond_fake_iface(const struct port *port)
1669 return port->cfg->bond_fake_iface && !ovs_list_is_short(&port->ifaces);
1673 add_del_bridges(const struct ovsrec_open_vswitch *cfg)
1675 struct bridge *br, *next;
1676 struct shash_node *node;
1677 struct shash new_br;
1680 /* Collect new bridges' names and types. */
1681 shash_init(&new_br);
1682 for (i = 0; i < cfg->n_bridges; i++) {
1683 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1684 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
1686 if (strchr(br_cfg->name, '/') || strchr(br_cfg->name, '\\')) {
1687 /* Prevent remote ovsdb-server users from accessing arbitrary
1688 * directories, e.g. consider a bridge named "../../../etc/".
1690 * Prohibiting "\" is only necessary on Windows but it's no great
1691 * loss elsewhere. */
1692 VLOG_WARN_RL(&rl, "ignoring bridge with invalid name \"%s\"",
1694 } else if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
1695 VLOG_WARN_RL(&rl, "bridge %s specified twice", br_cfg->name);
1699 /* Get rid of deleted bridges or those whose types have changed.
1700 * Update 'cfg' of bridges that still exist. */
1701 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
1702 br->cfg = shash_find_data(&new_br, br->name);
1703 if (!br->cfg || strcmp(br->type, ofproto_normalize_type(
1704 br->cfg->datapath_type))) {
1705 bridge_destroy(br, true);
1709 /* Add new bridges. */
1710 SHASH_FOR_EACH(node, &new_br) {
1711 const struct ovsrec_bridge *br_cfg = node->data;
1712 struct bridge *br = bridge_lookup(br_cfg->name);
1714 bridge_create(br_cfg);
1718 shash_destroy(&new_br);
1721 /* Configures 'netdev' based on the "options" column in 'iface_cfg'.
1722 * Returns 0 if successful, otherwise a positive errno value. */
1724 iface_set_netdev_config(const struct ovsrec_interface *iface_cfg,
1725 struct netdev *netdev, char **errp)
1727 return netdev_set_config(netdev, &iface_cfg->options, errp);
1730 /* Opens a network device for 'if_cfg' and configures it. Adds the network
1731 * device to br->ofproto and stores the OpenFlow port number in '*ofp_portp'.
1733 * If successful, returns 0 and stores the network device in '*netdevp'. On
1734 * failure, returns a positive errno value and stores NULL in '*netdevp'. */
1736 iface_do_create(const struct bridge *br,
1737 const struct ovsrec_interface *iface_cfg,
1738 ofp_port_t *ofp_portp, struct netdev **netdevp,
1741 struct netdev *netdev = NULL;
1745 if (netdev_is_reserved_name(iface_cfg->name)) {
1746 VLOG_WARN("could not create interface %s, name is reserved",
1752 type = ofproto_port_open_type(br->cfg->datapath_type,
1753 iface_get_type(iface_cfg, br->cfg));
1754 error = netdev_open(iface_cfg->name, type, &netdev);
1756 VLOG_WARN_BUF(errp, "could not open network device %s (%s)",
1757 iface_cfg->name, ovs_strerror(error));
1761 error = iface_set_netdev_config(iface_cfg, netdev, errp);
1766 *ofp_portp = iface_pick_ofport(iface_cfg);
1767 error = ofproto_port_add(br->ofproto, netdev, ofp_portp);
1772 VLOG_INFO("bridge %s: added interface %s on port %d",
1773 br->name, iface_cfg->name, *ofp_portp);
1780 netdev_close(netdev);
1784 /* Creates a new iface on 'br' based on 'if_cfg'. The new iface has OpenFlow
1785 * port number 'ofp_port'. If ofp_port is OFPP_NONE, an OpenFlow port is
1786 * automatically allocated for the iface. Takes ownership of and
1787 * deallocates 'if_cfg'.
1789 * Return true if an iface is successfully created, false otherwise. */
1791 iface_create(struct bridge *br, const struct ovsrec_interface *iface_cfg,
1792 const struct ovsrec_port *port_cfg)
1794 struct netdev *netdev;
1795 struct iface *iface;
1796 ofp_port_t ofp_port;
1801 /* Do the bits that can fail up front. */
1802 ovs_assert(!iface_lookup(br, iface_cfg->name));
1803 error = iface_do_create(br, iface_cfg, &ofp_port, &netdev, &errp);
1805 iface_clear_db_record(iface_cfg, errp);
1810 /* Get or create the port structure. */
1811 port = port_lookup(br, port_cfg->name);
1813 port = port_create(br, port_cfg);
1816 /* Create the iface structure. */
1817 iface = xzalloc(sizeof *iface);
1818 ovs_list_push_back(&port->ifaces, &iface->port_elem);
1819 hmap_insert(&br->iface_by_name, &iface->name_node,
1820 hash_string(iface_cfg->name, 0));
1822 iface->name = xstrdup(iface_cfg->name);
1823 iface->ofp_port = ofp_port;
1824 iface->netdev = netdev;
1825 iface->type = iface_get_type(iface_cfg, br->cfg);
1826 iface->netdev_type = ofproto_port_open_type(br->cfg->datapath_type,
1828 iface->cfg = iface_cfg;
1829 hmap_insert(&br->ifaces, &iface->ofp_port_node,
1830 hash_ofp_port(ofp_port));
1832 /* Populate initial status in database. */
1833 iface_refresh_stats(iface);
1834 iface_refresh_netdev_status(iface);
1836 /* Add bond fake iface if necessary. */
1837 if (port_is_bond_fake_iface(port)) {
1838 struct ofproto_port ofproto_port;
1840 if (ofproto_port_query_by_name(br->ofproto, port->name,
1842 struct netdev *netdev;
1845 error = netdev_open(port->name, "internal", &netdev);
1847 ofp_port_t fake_ofp_port = OFPP_NONE;
1848 ofproto_port_add(br->ofproto, netdev, &fake_ofp_port);
1849 netdev_close(netdev);
1851 VLOG_WARN("could not open network device %s (%s)",
1852 port->name, ovs_strerror(error));
1855 /* Already exists, nothing to do. */
1856 ofproto_port_destroy(&ofproto_port);
1863 /* Set forward BPDU option. */
1865 bridge_configure_forward_bpdu(struct bridge *br)
1867 ofproto_set_forward_bpdu(br->ofproto,
1868 smap_get_bool(&br->cfg->other_config,
1873 /* Set MAC learning table configuration for 'br'. */
1875 bridge_configure_mac_table(struct bridge *br)
1877 const char *idle_time_str;
1880 const char *mac_table_size_str;
1883 idle_time_str = smap_get(&br->cfg->other_config, "mac-aging-time");
1884 idle_time = (idle_time_str && atoi(idle_time_str)
1885 ? atoi(idle_time_str)
1886 : MAC_ENTRY_DEFAULT_IDLE_TIME);
1888 mac_table_size_str = smap_get(&br->cfg->other_config, "mac-table-size");
1889 mac_table_size = (mac_table_size_str && atoi(mac_table_size_str)
1890 ? atoi(mac_table_size_str)
1893 ofproto_set_mac_table_config(br->ofproto, idle_time, mac_table_size);
1896 /* Set multicast snooping table configuration for 'br'. */
1898 bridge_configure_mcast_snooping(struct bridge *br)
1900 if (!br->cfg->mcast_snooping_enable) {
1901 ofproto_set_mcast_snooping(br->ofproto, NULL);
1904 struct ofproto_mcast_snooping_settings br_s;
1905 const char *idle_time_str;
1906 const char *max_entries_str;
1908 idle_time_str = smap_get(&br->cfg->other_config,
1909 "mcast-snooping-aging-time");
1910 br_s.idle_time = (idle_time_str && atoi(idle_time_str)
1911 ? atoi(idle_time_str)
1912 : MCAST_ENTRY_DEFAULT_IDLE_TIME);
1914 max_entries_str = smap_get(&br->cfg->other_config,
1915 "mcast-snooping-table-size");
1916 br_s.max_entries = (max_entries_str && atoi(max_entries_str)
1917 ? atoi(max_entries_str)
1918 : MCAST_DEFAULT_MAX_ENTRIES);
1920 br_s.flood_unreg = !smap_get_bool(&br->cfg->other_config,
1921 "mcast-snooping-disable-flood-unregistered",
1924 /* Configure multicast snooping on the bridge */
1925 if (ofproto_set_mcast_snooping(br->ofproto, &br_s)) {
1926 VLOG_ERR("bridge %s: could not enable multicast snooping",
1931 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1932 struct ofproto_mcast_snooping_port_settings port_s;
1933 port_s.flood = smap_get_bool(&port->cfg->other_config,
1934 "mcast-snooping-flood", false);
1935 port_s.flood_reports = smap_get_bool(&port->cfg->other_config,
1936 "mcast-snooping-flood-reports", false);
1937 if (ofproto_port_set_mcast_snooping(br->ofproto, port, &port_s)) {
1938 VLOG_ERR("port %s: could not configure mcast snooping",
1946 find_local_hw_addr(const struct bridge *br, struct eth_addr *ea,
1947 const struct port *fake_br, struct iface **hw_addr_iface)
1949 struct hmapx mirror_output_ports;
1951 bool found_addr = false;
1955 /* Mirror output ports don't participate in picking the local hardware
1956 * address. ofproto can't help us find out whether a given port is a
1957 * mirror output because we haven't configured mirrors yet, so we need to
1958 * accumulate them ourselves. */
1959 hmapx_init(&mirror_output_ports);
1960 for (i = 0; i < br->cfg->n_mirrors; i++) {
1961 struct ovsrec_mirror *m = br->cfg->mirrors[i];
1962 if (m->output_port) {
1963 hmapx_add(&mirror_output_ports, m->output_port);
1967 /* Otherwise choose the minimum non-local MAC address among all of the
1969 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1970 struct eth_addr iface_ea;
1971 struct iface *candidate;
1972 struct iface *iface;
1974 /* Mirror output ports don't participate. */
1975 if (hmapx_contains(&mirror_output_ports, port->cfg)) {
1979 /* Choose the MAC address to represent the port. */
1981 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac,
1983 /* Find the interface with this Ethernet address (if any) so that
1984 * we can provide the correct devname to the caller. */
1985 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1986 struct eth_addr candidate_ea;
1987 if (!netdev_get_etheraddr(candidate->netdev, &candidate_ea)
1988 && eth_addr_equals(iface_ea, candidate_ea)) {
1993 /* Choose the interface whose MAC address will represent the port.
1994 * The Linux kernel bonding code always chooses the MAC address of
1995 * the first slave added to a bond, and the Fedora networking
1996 * scripts always add slaves to a bond in alphabetical order, so
1997 * for compatibility we choose the interface with the name that is
1998 * first in alphabetical order. */
1999 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
2000 if (!iface || strcmp(candidate->name, iface->name) < 0) {
2005 /* A port always has at least one interface. */
2006 ovs_assert(iface != NULL);
2008 /* The local port doesn't count (since we're trying to choose its
2009 * MAC address anyway). */
2010 if (iface->ofp_port == OFPP_LOCAL) {
2014 /* For fake bridges we only choose from ports with the same tag */
2015 if (fake_br && fake_br->cfg && fake_br->cfg->tag) {
2016 if (!port->cfg->tag) {
2019 if (*port->cfg->tag != *fake_br->cfg->tag) {
2025 error = netdev_get_etheraddr(iface->netdev, &iface_ea);
2031 /* Compare against our current choice. */
2032 if (!eth_addr_is_multicast(iface_ea) &&
2033 !eth_addr_is_local(iface_ea) &&
2034 !eth_addr_is_reserved(iface_ea) &&
2035 !eth_addr_is_zero(iface_ea) &&
2036 (!found_addr || eth_addr_compare_3way(iface_ea, *ea) < 0))
2039 *hw_addr_iface = iface;
2045 *ea = br->default_ea;
2046 *hw_addr_iface = NULL;
2049 hmapx_destroy(&mirror_output_ports);
2053 bridge_pick_local_hw_addr(struct bridge *br, struct eth_addr *ea,
2054 struct iface **hw_addr_iface)
2057 *hw_addr_iface = NULL;
2059 /* Did the user request a particular MAC? */
2060 hwaddr = smap_get(&br->cfg->other_config, "hwaddr");
2061 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
2062 if (eth_addr_is_multicast(*ea)) {
2063 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
2064 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(*ea));
2065 } else if (eth_addr_is_zero(*ea)) {
2066 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
2072 /* Find a local hw address */
2073 find_local_hw_addr(br, ea, NULL, hw_addr_iface);
2076 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
2077 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
2078 * an interface on 'br', then that interface must be passed in as
2079 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
2080 * 'hw_addr_iface' must be passed in as a null pointer. */
2082 bridge_pick_datapath_id(struct bridge *br,
2083 const struct eth_addr bridge_ea,
2084 struct iface *hw_addr_iface)
2087 * The procedure for choosing a bridge MAC address will, in the most
2088 * ordinary case, also choose a unique MAC that we can use as a datapath
2089 * ID. In some special cases, though, multiple bridges will end up with
2090 * the same MAC address. This is OK for the bridges, but it will confuse
2091 * the OpenFlow controller, because each datapath needs a unique datapath
2094 * Datapath IDs must be unique. It is also very desirable that they be
2095 * stable from one run to the next, so that policy set on a datapath
2098 const char *datapath_id;
2101 datapath_id = smap_get(&br->cfg->other_config, "datapath-id");
2102 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
2106 if (!hw_addr_iface) {
2108 * A purely internal bridge, that is, one that has no non-virtual
2109 * network devices on it at all, is difficult because it has no
2110 * natural unique identifier at all.
2112 * When the host is a XenServer, we handle this case by hashing the
2113 * host's UUID with the name of the bridge. Names of bridges are
2114 * persistent across XenServer reboots, although they can be reused if
2115 * an internal network is destroyed and then a new one is later
2116 * created, so this is fairly effective.
2118 * When the host is not a XenServer, we punt by using a random MAC
2119 * address on each run.
2121 const char *host_uuid = xenserver_get_host_uuid();
2123 char *combined = xasprintf("%s,%s", host_uuid, br->name);
2124 dpid = dpid_from_hash(combined, strlen(combined));
2130 return eth_addr_to_uint64(bridge_ea);
2134 dpid_from_hash(const void *data, size_t n)
2137 uint8_t bytes[SHA1_DIGEST_SIZE];
2141 sha1_bytes(data, n, hash.bytes);
2142 eth_addr_mark_random(&hash.ea);
2143 return eth_addr_to_uint64(hash.ea);
2147 iface_refresh_netdev_status(struct iface *iface)
2151 enum netdev_features current;
2152 enum netdev_flags flags;
2153 const char *link_state;
2154 struct eth_addr mac;
2155 int64_t bps, mtu_64, ifindex64, link_resets;
2158 if (iface_is_synthetic(iface)) {
2162 if (iface->change_seq == netdev_get_change_seq(iface->netdev)
2163 && !status_txn_try_again) {
2167 iface->change_seq = netdev_get_change_seq(iface->netdev);
2171 if (!netdev_get_status(iface->netdev, &smap)) {
2172 ovsrec_interface_set_status(iface->cfg, &smap);
2174 ovsrec_interface_set_status(iface->cfg, NULL);
2177 smap_destroy(&smap);
2179 error = netdev_get_flags(iface->netdev, &flags);
2181 const char *state = flags & NETDEV_UP ? "up" : "down";
2183 ovsrec_interface_set_admin_state(iface->cfg, state);
2185 ovsrec_interface_set_admin_state(iface->cfg, NULL);
2188 link_state = netdev_get_carrier(iface->netdev) ? "up" : "down";
2189 ovsrec_interface_set_link_state(iface->cfg, link_state);
2191 link_resets = netdev_get_carrier_resets(iface->netdev);
2192 ovsrec_interface_set_link_resets(iface->cfg, &link_resets, 1);
2194 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
2195 bps = !error ? netdev_features_to_bps(current, 0) : 0;
2197 ovsrec_interface_set_duplex(iface->cfg,
2198 netdev_features_is_full_duplex(current)
2200 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
2202 ovsrec_interface_set_duplex(iface->cfg, NULL);
2203 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
2206 error = netdev_get_mtu(iface->netdev, &mtu);
2209 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
2211 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
2214 error = netdev_get_etheraddr(iface->netdev, &mac);
2216 char mac_string[ETH_ADDR_STRLEN + 1];
2218 snprintf(mac_string, sizeof mac_string,
2219 ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
2220 ovsrec_interface_set_mac_in_use(iface->cfg, mac_string);
2222 ovsrec_interface_set_mac_in_use(iface->cfg, NULL);
2225 /* The netdev may return a negative number (such as -EOPNOTSUPP)
2226 * if there is no valid ifindex number. */
2227 ifindex64 = netdev_get_ifindex(iface->netdev);
2228 if (ifindex64 < 0) {
2231 ovsrec_interface_set_ifindex(iface->cfg, &ifindex64, 1);
2235 iface_refresh_ofproto_status(struct iface *iface)
2239 if (iface_is_synthetic(iface)) {
2243 current = ofproto_port_is_lacp_current(iface->port->bridge->ofproto,
2247 ovsrec_interface_set_lacp_current(iface->cfg, &bl, 1);
2249 ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
2252 if (ofproto_port_cfm_status_changed(iface->port->bridge->ofproto,
2254 || status_txn_try_again) {
2255 iface_refresh_cfm_stats(iface);
2258 if (ofproto_port_bfd_status_changed(iface->port->bridge->ofproto,
2260 || status_txn_try_again) {
2264 ofproto_port_get_bfd_status(iface->port->bridge->ofproto,
2265 iface->ofp_port, &smap);
2266 ovsrec_interface_set_bfd_status(iface->cfg, &smap);
2267 smap_destroy(&smap);
2271 /* Writes 'iface''s CFM statistics to the database. 'iface' must not be
2274 iface_refresh_cfm_stats(struct iface *iface)
2276 const struct ovsrec_interface *cfg = iface->cfg;
2277 struct cfm_status status;
2280 error = ofproto_port_get_cfm_status(iface->port->bridge->ofproto,
2281 iface->ofp_port, &status);
2283 ovsrec_interface_set_cfm_fault(cfg, NULL, 0);
2284 ovsrec_interface_set_cfm_fault_status(cfg, NULL, 0);
2285 ovsrec_interface_set_cfm_remote_opstate(cfg, NULL);
2286 ovsrec_interface_set_cfm_flap_count(cfg, NULL, 0);
2287 ovsrec_interface_set_cfm_health(cfg, NULL, 0);
2288 ovsrec_interface_set_cfm_remote_mpids(cfg, NULL, 0);
2290 const char *reasons[CFM_FAULT_N_REASONS];
2291 int64_t cfm_health = status.health;
2292 int64_t cfm_flap_count = status.flap_count;
2293 bool faulted = status.faults != 0;
2296 ovsrec_interface_set_cfm_fault(cfg, &faulted, 1);
2299 for (i = 0; i < CFM_FAULT_N_REASONS; i++) {
2300 int reason = 1 << i;
2301 if (status.faults & reason) {
2302 reasons[j++] = cfm_fault_reason_to_str(reason);
2305 ovsrec_interface_set_cfm_fault_status(cfg, reasons, j);
2307 ovsrec_interface_set_cfm_flap_count(cfg, &cfm_flap_count, 1);
2309 if (status.remote_opstate >= 0) {
2310 const char *remote_opstate = status.remote_opstate ? "up" : "down";
2311 ovsrec_interface_set_cfm_remote_opstate(cfg, remote_opstate);
2313 ovsrec_interface_set_cfm_remote_opstate(cfg, NULL);
2316 ovsrec_interface_set_cfm_remote_mpids(cfg,
2317 (const int64_t *)status.rmps,
2319 if (cfm_health >= 0) {
2320 ovsrec_interface_set_cfm_health(cfg, &cfm_health, 1);
2322 ovsrec_interface_set_cfm_health(cfg, NULL, 0);
2330 iface_refresh_stats(struct iface *iface)
2332 #define IFACE_STATS \
2333 IFACE_STAT(rx_packets, "rx_packets") \
2334 IFACE_STAT(tx_packets, "tx_packets") \
2335 IFACE_STAT(rx_bytes, "rx_bytes") \
2336 IFACE_STAT(tx_bytes, "tx_bytes") \
2337 IFACE_STAT(rx_dropped, "rx_dropped") \
2338 IFACE_STAT(tx_dropped, "tx_dropped") \
2339 IFACE_STAT(rx_errors, "rx_errors") \
2340 IFACE_STAT(tx_errors, "tx_errors") \
2341 IFACE_STAT(rx_frame_errors, "rx_frame_err") \
2342 IFACE_STAT(rx_over_errors, "rx_over_err") \
2343 IFACE_STAT(rx_crc_errors, "rx_crc_err") \
2344 IFACE_STAT(collisions, "collisions") \
2345 IFACE_STAT(rx_1_to_64_packets, "rx_1_to_64_packets") \
2346 IFACE_STAT(rx_65_to_127_packets, "rx_65_to_127_packets") \
2347 IFACE_STAT(rx_128_to_255_packets, "rx_128_to_255_packets") \
2348 IFACE_STAT(rx_256_to_511_packets, "rx_256_to_511_packets") \
2349 IFACE_STAT(rx_512_to_1023_packets, "rx_512_to_1023_packets") \
2350 IFACE_STAT(rx_1024_to_1522_packets, "rx_1024_to_1518_packets") \
2351 IFACE_STAT(rx_1523_to_max_packets, "rx_1523_to_max_packets") \
2352 IFACE_STAT(tx_1_to_64_packets, "tx_1_to_64_packets") \
2353 IFACE_STAT(tx_65_to_127_packets, "tx_65_to_127_packets") \
2354 IFACE_STAT(tx_128_to_255_packets, "tx_128_to_255_packets") \
2355 IFACE_STAT(tx_256_to_511_packets, "tx_256_to_511_packets") \
2356 IFACE_STAT(tx_512_to_1023_packets, "tx_512_to_1023_packets") \
2357 IFACE_STAT(tx_1024_to_1522_packets, "tx_1024_to_1518_packets") \
2358 IFACE_STAT(tx_1523_to_max_packets, "tx_1523_to_max_packets") \
2359 IFACE_STAT(tx_multicast_packets, "tx_multicast_packets") \
2360 IFACE_STAT(rx_broadcast_packets, "rx_broadcast_packets") \
2361 IFACE_STAT(tx_broadcast_packets, "tx_broadcast_packets") \
2362 IFACE_STAT(rx_undersized_errors, "rx_undersized_errors") \
2363 IFACE_STAT(rx_oversize_errors, "rx_oversize_errors") \
2364 IFACE_STAT(rx_fragmented_errors, "rx_fragmented_errors") \
2365 IFACE_STAT(rx_jabber_errors, "rx_jabber_errors")
2367 #define IFACE_STAT(MEMBER, NAME) + 1
2368 enum { N_IFACE_STATS = IFACE_STATS };
2370 int64_t values[N_IFACE_STATS];
2371 const char *keys[N_IFACE_STATS];
2374 struct netdev_stats stats;
2376 if (iface_is_synthetic(iface)) {
2380 /* Intentionally ignore return value, since errors will set 'stats' to
2381 * all-1s, and we will deal with that correctly below. */
2382 netdev_get_stats(iface->netdev, &stats);
2384 /* Copy statistics into keys[] and values[]. */
2386 #define IFACE_STAT(MEMBER, NAME) \
2387 if (stats.MEMBER != UINT64_MAX) { \
2389 values[n] = stats.MEMBER; \
2394 ovs_assert(n <= N_IFACE_STATS);
2396 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
2401 br_refresh_datapath_info(struct bridge *br)
2403 const char *version;
2405 version = (br->ofproto && br->ofproto->ofproto_class->get_datapath_version
2406 ? br->ofproto->ofproto_class->get_datapath_version(br->ofproto)
2409 ovsrec_bridge_set_datapath_version(br->cfg,
2410 version ? version : "<unknown>");
2414 br_refresh_stp_status(struct bridge *br)
2416 struct smap smap = SMAP_INITIALIZER(&smap);
2417 struct ofproto *ofproto = br->ofproto;
2418 struct ofproto_stp_status status;
2420 if (ofproto_get_stp_status(ofproto, &status)) {
2424 if (!status.enabled) {
2425 ovsrec_bridge_set_status(br->cfg, NULL);
2429 smap_add_format(&smap, "stp_bridge_id", STP_ID_FMT,
2430 STP_ID_ARGS(status.bridge_id));
2431 smap_add_format(&smap, "stp_designated_root", STP_ID_FMT,
2432 STP_ID_ARGS(status.designated_root));
2433 smap_add_format(&smap, "stp_root_path_cost", "%d", status.root_path_cost);
2435 ovsrec_bridge_set_status(br->cfg, &smap);
2436 smap_destroy(&smap);
2440 port_refresh_stp_status(struct port *port)
2442 struct ofproto *ofproto = port->bridge->ofproto;
2443 struct iface *iface;
2444 struct ofproto_port_stp_status status;
2447 if (port_is_synthetic(port)) {
2451 /* STP doesn't currently support bonds. */
2452 if (!ovs_list_is_singleton(&port->ifaces)) {
2453 ovsrec_port_set_status(port->cfg, NULL);
2457 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
2458 if (ofproto_port_get_stp_status(ofproto, iface->ofp_port, &status)) {
2462 if (!status.enabled) {
2463 ovsrec_port_set_status(port->cfg, NULL);
2467 /* Set Status column. */
2469 smap_add_format(&smap, "stp_port_id", STP_PORT_ID_FMT, status.port_id);
2470 smap_add(&smap, "stp_state", stp_state_name(status.state));
2471 smap_add_format(&smap, "stp_sec_in_state", "%u", status.sec_in_state);
2472 smap_add(&smap, "stp_role", stp_role_name(status.role));
2473 ovsrec_port_set_status(port->cfg, &smap);
2474 smap_destroy(&smap);
2478 port_refresh_stp_stats(struct port *port)
2480 struct ofproto *ofproto = port->bridge->ofproto;
2481 struct iface *iface;
2482 struct ofproto_port_stp_stats stats;
2483 const char *keys[3];
2484 int64_t int_values[3];
2486 if (port_is_synthetic(port)) {
2490 /* STP doesn't currently support bonds. */
2491 if (!ovs_list_is_singleton(&port->ifaces)) {
2495 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
2496 if (ofproto_port_get_stp_stats(ofproto, iface->ofp_port, &stats)) {
2500 if (!stats.enabled) {
2501 ovsrec_port_set_statistics(port->cfg, NULL, NULL, 0);
2505 /* Set Statistics column. */
2506 keys[0] = "stp_tx_count";
2507 int_values[0] = stats.tx_count;
2508 keys[1] = "stp_rx_count";
2509 int_values[1] = stats.rx_count;
2510 keys[2] = "stp_error_count";
2511 int_values[2] = stats.error_count;
2513 ovsrec_port_set_statistics(port->cfg, keys, int_values,
2514 ARRAY_SIZE(int_values));
2518 br_refresh_rstp_status(struct bridge *br)
2520 struct smap smap = SMAP_INITIALIZER(&smap);
2521 struct ofproto *ofproto = br->ofproto;
2522 struct ofproto_rstp_status status;
2524 if (ofproto_get_rstp_status(ofproto, &status)) {
2527 if (!status.enabled) {
2528 ovsrec_bridge_set_rstp_status(br->cfg, NULL);
2531 smap_add_format(&smap, "rstp_bridge_id", RSTP_ID_FMT,
2532 RSTP_ID_ARGS(status.bridge_id));
2533 smap_add_format(&smap, "rstp_root_path_cost", "%"PRIu32,
2534 status.root_path_cost);
2535 smap_add_format(&smap, "rstp_root_id", RSTP_ID_FMT,
2536 RSTP_ID_ARGS(status.root_id));
2537 smap_add_format(&smap, "rstp_designated_id", RSTP_ID_FMT,
2538 RSTP_ID_ARGS(status.designated_id));
2539 smap_add_format(&smap, "rstp_designated_port_id", RSTP_PORT_ID_FMT,
2540 status.designated_port_id);
2541 smap_add_format(&smap, "rstp_bridge_port_id", RSTP_PORT_ID_FMT,
2542 status.bridge_port_id);
2543 ovsrec_bridge_set_rstp_status(br->cfg, &smap);
2544 smap_destroy(&smap);
2548 port_refresh_rstp_status(struct port *port)
2550 struct ofproto *ofproto = port->bridge->ofproto;
2551 struct iface *iface;
2552 struct ofproto_port_rstp_status status;
2553 const char *keys[4];
2554 int64_t int_values[4];
2557 if (port_is_synthetic(port)) {
2561 /* RSTP doesn't currently support bonds. */
2562 if (!ovs_list_is_singleton(&port->ifaces)) {
2563 ovsrec_port_set_rstp_status(port->cfg, NULL);
2567 iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
2568 if (ofproto_port_get_rstp_status(ofproto, iface->ofp_port, &status)) {
2572 if (!status.enabled) {
2573 ovsrec_port_set_rstp_status(port->cfg, NULL);
2574 ovsrec_port_set_rstp_statistics(port->cfg, NULL, NULL, 0);
2577 /* Set Status column. */
2580 smap_add_format(&smap, "rstp_port_id", RSTP_PORT_ID_FMT,
2582 smap_add_format(&smap, "rstp_port_role", "%s",
2583 rstp_port_role_name(status.role));
2584 smap_add_format(&smap, "rstp_port_state", "%s",
2585 rstp_state_name(status.state));
2586 smap_add_format(&smap, "rstp_designated_bridge_id", RSTP_ID_FMT,
2587 RSTP_ID_ARGS(status.designated_bridge_id));
2588 smap_add_format(&smap, "rstp_designated_port_id", RSTP_PORT_ID_FMT,
2589 status.designated_port_id);
2590 smap_add_format(&smap, "rstp_designated_path_cost", "%"PRIu32,
2591 status.designated_path_cost);
2593 ovsrec_port_set_rstp_status(port->cfg, &smap);
2594 smap_destroy(&smap);
2596 /* Set Statistics column. */
2597 keys[0] = "rstp_tx_count";
2598 int_values[0] = status.tx_count;
2599 keys[1] = "rstp_rx_count";
2600 int_values[1] = status.rx_count;
2601 keys[2] = "rstp_uptime";
2602 int_values[2] = status.uptime;
2603 keys[3] = "rstp_error_count";
2604 int_values[3] = status.error_count;
2605 ovsrec_port_set_rstp_statistics(port->cfg, keys, int_values,
2606 ARRAY_SIZE(int_values));
2610 port_refresh_bond_status(struct port *port, bool force_update)
2612 struct eth_addr mac;
2614 /* Return if port is not a bond */
2615 if (ovs_list_is_singleton(&port->ifaces)) {
2619 if (bond_get_changed_active_slave(port->name, &mac, force_update)) {
2623 ds_put_format(&mac_s, ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
2624 ovsrec_port_set_bond_active_slave(port->cfg, ds_cstr(&mac_s));
2630 enable_system_stats(const struct ovsrec_open_vswitch *cfg)
2632 return smap_get_bool(&cfg->other_config, "enable-statistics", false);
2636 reconfigure_system_stats(const struct ovsrec_open_vswitch *cfg)
2638 bool enable = enable_system_stats(cfg);
2640 system_stats_enable(enable);
2642 ovsrec_open_vswitch_set_statistics(cfg, NULL);
2647 run_system_stats(void)
2649 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(idl);
2652 stats = system_stats_run();
2654 struct ovsdb_idl_txn *txn;
2655 struct ovsdb_datum datum;
2657 txn = ovsdb_idl_txn_create(idl);
2658 ovsdb_datum_from_smap(&datum, stats);
2659 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
2661 ovsdb_idl_txn_commit(txn);
2662 ovsdb_idl_txn_destroy(txn);
2669 ofp12_controller_role_to_str(enum ofp12_controller_role role)
2672 case OFPCR12_ROLE_EQUAL:
2674 case OFPCR12_ROLE_MASTER:
2676 case OFPCR12_ROLE_SLAVE:
2678 case OFPCR12_ROLE_NOCHANGE:
2680 return "*** INVALID ROLE ***";
2685 refresh_controller_status(void)
2689 const struct ovsrec_controller *cfg;
2693 /* Accumulate status for controllers on all bridges. */
2694 HMAP_FOR_EACH (br, node, &all_bridges) {
2695 ofproto_get_ofproto_controller_info(br->ofproto, &info);
2698 /* Update each controller in the database with current status. */
2699 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
2700 struct ofproto_controller_info *cinfo =
2701 shash_find_data(&info, cfg->target);
2704 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
2705 ovsrec_controller_set_role(cfg, ofp12_controller_role_to_str(
2707 ovsrec_controller_set_status(cfg, &cinfo->pairs);
2709 ovsrec_controller_set_is_connected(cfg, false);
2710 ovsrec_controller_set_role(cfg, NULL);
2711 ovsrec_controller_set_status(cfg, NULL);
2715 ofproto_free_ofproto_controller_info(&info);
2718 /* Update interface and mirror statistics if necessary. */
2720 run_stats_update(void)
2722 const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(idl);
2729 /* Statistics update interval should always be greater than or equal to
2731 stats_interval = MAX(smap_get_int(&cfg->other_config,
2732 "stats-update-interval",
2734 if (stats_timer_interval != stats_interval) {
2735 stats_timer_interval = stats_interval;
2736 stats_timer = LLONG_MIN;
2739 if (time_msec() >= stats_timer) {
2740 enum ovsdb_idl_txn_status status;
2742 /* Rate limit the update. Do not start a new update if the
2743 * previous one is not done. */
2747 stats_txn = ovsdb_idl_txn_create(idl);
2748 HMAP_FOR_EACH (br, node, &all_bridges) {
2752 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2753 struct iface *iface;
2755 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2756 iface_refresh_stats(iface);
2758 port_refresh_stp_stats(port);
2760 HMAP_FOR_EACH (m, hmap_node, &br->mirrors) {
2761 mirror_refresh_stats(m);
2764 refresh_controller_status();
2767 status = ovsdb_idl_txn_commit(stats_txn);
2768 if (status != TXN_INCOMPLETE) {
2769 stats_timer = time_msec() + stats_timer_interval;
2770 ovsdb_idl_txn_destroy(stats_txn);
2777 stats_update_wait(void)
2779 /* If the 'stats_txn' is non-null (transaction incomplete), waits for the
2780 * transaction to complete. Otherwise, waits for the 'stats_timer'. */
2782 ovsdb_idl_txn_wait(stats_txn);
2784 poll_timer_wait_until(stats_timer);
2788 /* Update bridge/port/interface status if necessary. */
2790 run_status_update(void)
2795 /* Rate limit the update. Do not start a new update if the
2796 * previous one is not done. */
2797 seq = seq_read(connectivity_seq_get());
2798 if (seq != connectivity_seqno || status_txn_try_again) {
2801 connectivity_seqno = seq;
2802 status_txn = ovsdb_idl_txn_create(idl);
2803 HMAP_FOR_EACH (br, node, &all_bridges) {
2806 br_refresh_stp_status(br);
2807 br_refresh_rstp_status(br);
2808 br_refresh_datapath_info(br);
2809 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2810 struct iface *iface;
2812 port_refresh_stp_status(port);
2813 port_refresh_rstp_status(port);
2814 port_refresh_bond_status(port, status_txn_try_again);
2815 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2816 iface_refresh_netdev_status(iface);
2817 iface_refresh_ofproto_status(iface);
2824 /* Commit the transaction and get the status. If the transaction finishes,
2825 * then destroy the transaction. Otherwise, keep it so that we can check
2826 * progress the next time that this function is called. */
2828 enum ovsdb_idl_txn_status status;
2830 status = ovsdb_idl_txn_commit(status_txn);
2831 if (status != TXN_INCOMPLETE) {
2832 ovsdb_idl_txn_destroy(status_txn);
2835 /* Sets the 'status_txn_try_again' if the transaction fails. */
2836 if (status == TXN_SUCCESS || status == TXN_UNCHANGED) {
2837 status_txn_try_again = false;
2839 status_txn_try_again = true;
2844 /* Refresh AA port status if necessary. */
2845 if (time_msec() >= aa_refresh_timer) {
2848 HMAP_FOR_EACH (br, node, &all_bridges) {
2849 if (bridge_aa_need_refresh(br)) {
2850 struct ovsdb_idl_txn *txn;
2852 txn = ovsdb_idl_txn_create(idl);
2853 bridge_aa_refresh_queued(br);
2854 ovsdb_idl_txn_commit(txn);
2855 ovsdb_idl_txn_destroy(txn);
2859 aa_refresh_timer = time_msec() + AA_REFRESH_INTERVAL;
2864 status_update_wait(void)
2866 /* If the 'status_txn' is non-null (transaction incomplete), waits for the
2867 * transaction to complete. If the status update to database needs to be
2868 * run again (transaction fails), registers a timeout in
2869 * 'STATUS_CHECK_AGAIN_MSEC'. Otherwise, waits on the global connectivity
2870 * sequence number. */
2872 ovsdb_idl_txn_wait(status_txn);
2873 } else if (status_txn_try_again) {
2874 poll_timer_wait_until(time_msec() + STATUS_CHECK_AGAIN_MSEC);
2876 seq_wait(connectivity_seq_get(), connectivity_seqno);
2887 /* Let each datapath type do the work that it needs to do. */
2889 ofproto_enumerate_types(&types);
2890 SSET_FOR_EACH (type, &types) {
2891 ofproto_type_run(type);
2893 sset_destroy(&types);
2895 /* Let each bridge do the work that it needs to do. */
2896 HMAP_FOR_EACH (br, node, &all_bridges) {
2897 ofproto_run(br->ofproto);
2904 static struct ovsrec_open_vswitch null_cfg;
2905 const struct ovsrec_open_vswitch *cfg;
2907 ovsrec_open_vswitch_init(&null_cfg);
2913 if (ovsdb_idl_is_lock_contended(idl)) {
2914 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2915 struct bridge *br, *next_br;
2917 VLOG_ERR_RL(&rl, "another ovs-vswitchd process is running, "
2918 "disabling this process (pid %ld) until it goes away",
2919 (long int) getpid());
2921 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
2922 bridge_destroy(br, false);
2924 /* Since we will not be running system_stats_run() in this process
2925 * with the current situation of multiple ovs-vswitchd daemons,
2926 * disable system stats collection. */
2927 system_stats_enable(false);
2929 } else if (!ovsdb_idl_has_lock(idl)
2930 || !ovsdb_idl_has_ever_connected(idl)) {
2931 /* Returns if not holding the lock or not done retrieving db
2935 cfg = ovsrec_open_vswitch_first(idl);
2938 dpdk_init(&cfg->other_config);
2941 /* Initialize the ofproto library. This only needs to run once, but
2942 * it must be done after the configuration is set. If the
2943 * initialization has already occurred, bridge_init_ofproto()
2944 * returns immediately. */
2945 bridge_init_ofproto(cfg);
2947 /* Once the value of flow-restore-wait is false, we no longer should
2948 * check its value from the database. */
2949 if (cfg && ofproto_get_flow_restore_wait()) {
2950 ofproto_set_flow_restore_wait(smap_get_bool(&cfg->other_config,
2951 "flow-restore-wait", false));
2956 /* Re-configure SSL. We do this on every trip through the main loop,
2957 * instead of just when the database changes, because the contents of the
2958 * key and certificate files can change without the database changing.
2960 * We do this before bridge_reconfigure() because that function might
2961 * initiate SSL connections and thus requires SSL to be configured. */
2962 if (cfg && cfg->ssl) {
2963 const struct ovsrec_ssl *ssl = cfg->ssl;
2965 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
2966 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
2969 if (ovsdb_idl_get_seqno(idl) != idl_seqno || ifaces_changed) {
2970 struct ovsdb_idl_txn *txn;
2972 ifaces_changed = false;
2974 idl_seqno = ovsdb_idl_get_seqno(idl);
2975 txn = ovsdb_idl_txn_create(idl);
2976 bridge_reconfigure(cfg ? cfg : &null_cfg);
2979 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
2980 discover_types(cfg);
2983 /* If we are completing our initial configuration for this run
2984 * of ovs-vswitchd, then keep the transaction around to monitor
2985 * it for completion. */
2986 if (initial_config_done) {
2987 /* Always sets the 'status_txn_try_again' to check again,
2988 * in case that this transaction fails. */
2989 status_txn_try_again = true;
2990 ovsdb_idl_txn_commit(txn);
2991 ovsdb_idl_txn_destroy(txn);
2993 initial_config_done = true;
2994 daemonize_txn = txn;
2998 if (daemonize_txn) {
2999 enum ovsdb_idl_txn_status status = ovsdb_idl_txn_commit(daemonize_txn);
3000 if (status != TXN_INCOMPLETE) {
3001 ovsdb_idl_txn_destroy(daemonize_txn);
3002 daemonize_txn = NULL;
3004 /* ovs-vswitchd has completed initialization, so allow the
3005 * process that forked us to exit successfully. */
3006 daemonize_complete();
3008 vlog_enable_async();
3010 VLOG_INFO_ONCE("%s (Open vSwitch) %s", program_name, VERSION);
3015 run_status_update();
3025 ovsdb_idl_wait(idl);
3026 if (daemonize_txn) {
3027 ovsdb_idl_txn_wait(daemonize_txn);
3031 if (ifaces_changed) {
3032 poll_immediate_wake();
3036 ofproto_enumerate_types(&types);
3037 SSET_FOR_EACH (type, &types) {
3038 ofproto_type_wait(type);
3040 sset_destroy(&types);
3042 if (!hmap_is_empty(&all_bridges)) {
3045 HMAP_FOR_EACH (br, node, &all_bridges) {
3046 ofproto_wait(br->ofproto);
3048 stats_update_wait();
3049 status_update_wait();
3052 system_stats_wait();
3055 /* Adds some memory usage statistics for bridges into 'usage', for use with
3056 * memory_report(). */
3058 bridge_get_memory_usage(struct simap *usage)
3065 ofproto_enumerate_types(&types);
3066 SSET_FOR_EACH (type, &types) {
3067 ofproto_type_get_memory_usage(type, usage);
3069 sset_destroy(&types);
3071 HMAP_FOR_EACH (br, node, &all_bridges) {
3072 ofproto_get_memory_usage(br->ofproto, usage);
3076 /* QoS unixctl user interface functions. */
3078 struct qos_unixctl_show_cbdata {
3080 struct iface *iface;
3084 qos_unixctl_show_queue(unsigned int queue_id,
3085 const struct smap *details,
3086 struct iface *iface,
3089 struct netdev_queue_stats stats;
3090 struct smap_node *node;
3093 ds_put_cstr(ds, "\n");
3095 ds_put_format(ds, "Queue %u:\n", queue_id);
3097 ds_put_cstr(ds, "Default:\n");
3100 SMAP_FOR_EACH (node, details) {
3101 ds_put_format(ds, "\t%s: %s\n", node->key, node->value);
3104 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
3106 if (stats.tx_packets != UINT64_MAX) {
3107 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
3110 if (stats.tx_bytes != UINT64_MAX) {
3111 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
3114 if (stats.tx_errors != UINT64_MAX) {
3115 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
3118 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
3119 queue_id, ovs_strerror(error));
3124 qos_unixctl_show_types(struct unixctl_conn *conn, int argc OVS_UNUSED,
3125 const char *argv[], void *aux OVS_UNUSED)
3127 struct ds ds = DS_EMPTY_INITIALIZER;
3128 struct sset types = SSET_INITIALIZER(&types);
3129 struct iface *iface;
3130 const char * types_name;
3133 iface = iface_find(argv[1]);
3135 unixctl_command_reply_error(conn, "no such interface");
3139 error = netdev_get_qos_types(iface->netdev, &types);
3141 if (!sset_is_empty(&types)) {
3142 SSET_FOR_EACH (types_name, &types) {
3143 ds_put_format(&ds, "QoS type: %s\n", types_name);
3145 unixctl_command_reply(conn, ds_cstr(&ds));
3147 ds_put_format(&ds, "No QoS types supported for interface: %s\n",
3149 unixctl_command_reply(conn, ds_cstr(&ds));
3152 ds_put_format(&ds, "%s: failed to retrieve supported QoS types (%s)",
3153 iface->name, ovs_strerror(error));
3154 unixctl_command_reply_error(conn, ds_cstr(&ds));
3157 sset_destroy(&types);
3162 qos_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3163 const char *argv[], void *aux OVS_UNUSED)
3165 struct ds ds = DS_EMPTY_INITIALIZER;
3166 struct smap smap = SMAP_INITIALIZER(&smap);
3167 struct iface *iface;
3169 struct smap_node *node;
3172 iface = iface_find(argv[1]);
3174 unixctl_command_reply_error(conn, "no such interface");
3178 error = netdev_get_qos(iface->netdev, &type, &smap);
3180 if (*type != '\0') {
3181 struct netdev_queue_dump dump;
3182 struct smap details;
3183 unsigned int queue_id;
3185 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
3187 SMAP_FOR_EACH (node, &smap) {
3188 ds_put_format(&ds, "%s: %s\n", node->key, node->value);
3191 smap_init(&details);
3192 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
3193 qos_unixctl_show_queue(queue_id, &details, iface, &ds);
3195 smap_destroy(&details);
3197 unixctl_command_reply(conn, ds_cstr(&ds));
3199 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
3200 unixctl_command_reply_error(conn, ds_cstr(&ds));
3203 ds_put_format(&ds, "%s: failed to retrieve QOS configuration (%s)\n",
3204 iface->name, ovs_strerror(error));
3205 unixctl_command_reply_error(conn, ds_cstr(&ds));
3208 smap_destroy(&smap);
3212 /* Bridge reconfiguration functions. */
3214 bridge_create(const struct ovsrec_bridge *br_cfg)
3218 ovs_assert(!bridge_lookup(br_cfg->name));
3219 br = xzalloc(sizeof *br);
3221 br->name = xstrdup(br_cfg->name);
3222 br->type = xstrdup(ofproto_normalize_type(br_cfg->datapath_type));
3225 /* Derive the default Ethernet address from the bridge's UUID. This should
3226 * be unique and it will be stable between ovs-vswitchd runs. */
3227 memcpy(&br->default_ea, &br_cfg->header_.uuid, ETH_ADDR_LEN);
3228 eth_addr_mark_random(&br->default_ea);
3230 hmap_init(&br->ports);
3231 hmap_init(&br->ifaces);
3232 hmap_init(&br->iface_by_name);
3233 hmap_init(&br->mirrors);
3235 hmap_init(&br->mappings);
3236 hmap_insert(&all_bridges, &br->node, hash_string(br->name, 0));
3240 bridge_destroy(struct bridge *br, bool del)
3243 struct mirror *mirror, *next_mirror;
3244 struct port *port, *next_port;
3246 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
3249 HMAP_FOR_EACH_SAFE (mirror, next_mirror, hmap_node, &br->mirrors) {
3250 mirror_destroy(mirror);
3253 hmap_remove(&all_bridges, &br->node);
3254 ofproto_destroy(br->ofproto, del);
3255 hmap_destroy(&br->ifaces);
3256 hmap_destroy(&br->ports);
3257 hmap_destroy(&br->iface_by_name);
3258 hmap_destroy(&br->mirrors);
3259 hmap_destroy(&br->mappings);
3266 static struct bridge *
3267 bridge_lookup(const char *name)
3271 HMAP_FOR_EACH_WITH_HASH (br, node, hash_string(name, 0), &all_bridges) {
3272 if (!strcmp(br->name, name)) {
3279 /* Handle requests for a listing of all flows known by the OpenFlow
3280 * stack, including those normally hidden. */
3282 bridge_unixctl_dump_flows(struct unixctl_conn *conn, int argc OVS_UNUSED,
3283 const char *argv[], void *aux OVS_UNUSED)
3288 br = bridge_lookup(argv[1]);
3290 unixctl_command_reply_error(conn, "Unknown bridge");
3295 ofproto_get_all_flows(br->ofproto, &results);
3297 unixctl_command_reply(conn, ds_cstr(&results));
3298 ds_destroy(&results);
3301 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
3302 * connections and reconnect. If BRIDGE is not specified, then all bridges
3303 * drop their controller connections and reconnect. */
3305 bridge_unixctl_reconnect(struct unixctl_conn *conn, int argc,
3306 const char *argv[], void *aux OVS_UNUSED)
3310 br = bridge_lookup(argv[1]);
3312 unixctl_command_reply_error(conn, "Unknown bridge");
3315 ofproto_reconnect_controllers(br->ofproto);
3317 HMAP_FOR_EACH (br, node, &all_bridges) {
3318 ofproto_reconnect_controllers(br->ofproto);
3321 unixctl_command_reply(conn, NULL);
3325 bridge_get_controllers(const struct bridge *br,
3326 struct ovsrec_controller ***controllersp)
3328 struct ovsrec_controller **controllers;
3329 size_t n_controllers;
3331 controllers = br->cfg->controller;
3332 n_controllers = br->cfg->n_controller;
3334 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
3340 *controllersp = controllers;
3342 return n_controllers;
3346 bridge_collect_wanted_ports(struct bridge *br,
3347 struct shash *wanted_ports)
3351 shash_init(wanted_ports);
3353 for (i = 0; i < br->cfg->n_ports; i++) {
3354 const char *name = br->cfg->ports[i]->name;
3355 if (!shash_add_once(wanted_ports, name, br->cfg->ports[i])) {
3356 VLOG_WARN("bridge %s: %s specified twice as bridge port",
3360 if (bridge_get_controllers(br, NULL)
3361 && !shash_find(wanted_ports, br->name)) {
3362 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
3363 br->name, br->name);
3365 ovsrec_interface_init(&br->synth_local_iface);
3366 ovsrec_port_init(&br->synth_local_port);
3368 br->synth_local_port.interfaces = &br->synth_local_ifacep;
3369 br->synth_local_port.n_interfaces = 1;
3370 br->synth_local_port.name = br->name;
3372 br->synth_local_iface.name = br->name;
3373 br->synth_local_iface.type = "internal";
3375 br->synth_local_ifacep = &br->synth_local_iface;
3377 shash_add(wanted_ports, br->name, &br->synth_local_port);
3381 /* Deletes "struct port"s and "struct iface"s under 'br' which aren't
3382 * consistent with 'br->cfg'. Updates 'br->if_cfg_queue' with interfaces which
3383 * 'br' needs to complete its configuration. */
3385 bridge_del_ports(struct bridge *br, const struct shash *wanted_ports)
3387 struct shash_node *port_node;
3388 struct port *port, *next;
3390 /* Get rid of deleted ports.
3391 * Get rid of deleted interfaces on ports that still exist. */
3392 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
3393 port->cfg = shash_find_data(wanted_ports, port->name);
3397 port_del_ifaces(port);
3401 /* Update iface->cfg and iface->type in interfaces that still exist. */
3402 SHASH_FOR_EACH (port_node, wanted_ports) {
3403 const struct ovsrec_port *port = port_node->data;
3406 for (i = 0; i < port->n_interfaces; i++) {
3407 const struct ovsrec_interface *cfg = port->interfaces[i];
3408 struct iface *iface = iface_lookup(br, cfg->name);
3409 const char *type = iface_get_type(cfg, br->cfg);
3410 const char *dp_type = br->cfg->datapath_type;
3411 const char *netdev_type = ofproto_port_open_type(dp_type, type);
3416 iface->netdev_type = netdev_type;
3417 } else if (!strcmp(type, "null")) {
3418 VLOG_WARN_ONCE("%s: The null interface type is deprecated and"
3419 " may be removed in February 2013. Please email"
3420 " dev@openvswitch.org with concerns.",
3423 /* We will add new interfaces later. */
3429 /* Initializes 'oc' appropriately as a management service controller for
3432 * The caller must free oc->target when it is no longer needed. */
3434 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
3435 struct ofproto_controller *oc)
3437 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
3438 oc->max_backoff = 0;
3439 oc->probe_interval = 60;
3440 oc->band = OFPROTO_OUT_OF_BAND;
3442 oc->burst_limit = 0;
3443 oc->enable_async_msgs = true;
3447 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
3449 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
3450 struct ofproto_controller *oc)
3454 oc->target = c->target;
3455 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
3456 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
3457 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
3458 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
3459 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
3460 oc->burst_limit = (c->controller_burst_limit
3461 ? *c->controller_burst_limit : 0);
3462 oc->enable_async_msgs = (!c->enable_async_messages
3463 || *c->enable_async_messages);
3464 dscp = smap_get_int(&c->other_config, "dscp", DSCP_DEFAULT);
3465 if (dscp < 0 || dscp > 63) {
3466 dscp = DSCP_DEFAULT;
3471 /* Configures the IP stack for 'br''s local interface properly according to the
3472 * configuration in 'c'. */
3474 bridge_configure_local_iface_netdev(struct bridge *br,
3475 struct ovsrec_controller *c)
3477 struct netdev *netdev;
3478 struct in_addr mask, gateway;
3480 struct iface *local_iface;
3483 /* If there's no local interface or no IP address, give up. */
3484 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
3485 if (!local_iface || !c->local_ip || !ip_parse(c->local_ip, &ip.s_addr)) {
3489 /* Bring up the local interface. */
3490 netdev = local_iface->netdev;
3491 netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
3493 /* Configure the IP address and netmask. */
3494 if (!c->local_netmask
3495 || !ip_parse(c->local_netmask, &mask.s_addr)
3497 mask.s_addr = guess_netmask(ip.s_addr);
3499 if (!netdev_set_in4(netdev, ip, mask)) {
3500 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
3501 br->name, IP_ARGS(ip.s_addr), IP_ARGS(mask.s_addr));
3504 /* Configure the default gateway. */
3505 if (c->local_gateway
3506 && ip_parse(c->local_gateway, &gateway.s_addr)
3507 && gateway.s_addr) {
3508 if (!netdev_add_router(netdev, gateway)) {
3509 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
3510 br->name, IP_ARGS(gateway.s_addr));
3515 /* Returns true if 'a' and 'b' are the same except that any number of slashes
3516 * in either string are treated as equal to any number of slashes in the other,
3517 * e.g. "x///y" is equal to "x/y".
3519 * Also, if 'b_stoplen' bytes from 'b' are found to be equal to corresponding
3520 * bytes from 'a', the function considers this success. Specify 'b_stoplen' as
3521 * SIZE_MAX to compare all of 'a' to all of 'b' rather than just a prefix of
3522 * 'b' against a prefix of 'a'.
3525 equal_pathnames(const char *a, const char *b, size_t b_stoplen)
3527 const char *b_start = b;
3529 if (b - b_start >= b_stoplen) {
3531 } else if (*a != *b) {
3533 } else if (*a == '/') {
3534 a += strspn(a, "/");
3535 b += strspn(b, "/");
3536 } else if (*a == '\0') {
3546 bridge_configure_remotes(struct bridge *br,
3547 const struct sockaddr_in *managers, size_t n_managers)
3549 bool disable_in_band;
3551 struct ovsrec_controller **controllers;
3552 size_t n_controllers;
3554 enum ofproto_fail_mode fail_mode;
3556 struct ofproto_controller *ocs;
3560 /* Check if we should disable in-band control on this bridge. */
3561 disable_in_band = smap_get_bool(&br->cfg->other_config, "disable-in-band",
3564 /* Set OpenFlow queue ID for in-band control. */
3565 ofproto_set_in_band_queue(br->ofproto,
3566 smap_get_int(&br->cfg->other_config,
3567 "in-band-queue", -1));
3569 if (disable_in_band) {
3570 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
3572 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
3575 n_controllers = bridge_get_controllers(br, &controllers);
3577 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
3580 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
3581 for (i = 0; i < n_controllers; i++) {
3582 struct ovsrec_controller *c = controllers[i];
3584 if (daemon_should_self_confine()
3585 && (!strncmp(c->target, "punix:", 6)
3586 || !strncmp(c->target, "unix:", 5))) {
3587 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3590 if (!strncmp(c->target, "unix:", 5)) {
3591 /* Connect to a listening socket */
3592 whitelist = xasprintf("unix:%s/", ovs_rundir());
3593 if (strchr(c->target, '/') &&
3594 !equal_pathnames(c->target, whitelist,
3595 strlen(whitelist))) {
3596 /* Absolute path specified, but not in ovs_rundir */
3597 VLOG_ERR_RL(&rl, "bridge %s: Not connecting to socket "
3598 "controller \"%s\" due to possibility for "
3599 "remote exploit. Instead, specify socket "
3600 "in whitelisted \"%s\" or connect to "
3601 "\"unix:%s/%s.mgmt\" (which is always "
3602 "available without special configuration).",
3603 br->name, c->target, whitelist,
3604 ovs_rundir(), br->name);
3609 whitelist = xasprintf("punix:%s/%s.",
3610 ovs_rundir(), br->name);
3611 if (!equal_pathnames(c->target, whitelist, strlen(whitelist))
3612 || strchr(c->target + strlen(whitelist), '/')) {
3613 /* Prevent remote ovsdb-server users from accessing
3614 * arbitrary Unix domain sockets and overwriting arbitrary
3616 VLOG_ERR_RL(&rl, "bridge %s: Not adding Unix domain socket "
3617 "controller \"%s\" due to possibility of "
3618 "overwriting local files. Instead, specify "
3619 "path in whitelisted format \"%s*\" or "
3620 "connect to \"unix:%s/%s.mgmt\" (which is "
3621 "always available without special "
3623 br->name, c->target, whitelist,
3624 ovs_rundir(), br->name);
3633 bridge_configure_local_iface_netdev(br, c);
3634 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
3635 if (disable_in_band) {
3636 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
3641 ofproto_set_controllers(br->ofproto, ocs, n_ocs,
3642 bridge_get_allowed_versions(br));
3643 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
3646 /* Set the fail-mode. */
3647 fail_mode = !br->cfg->fail_mode
3648 || !strcmp(br->cfg->fail_mode, "standalone")
3649 ? OFPROTO_FAIL_STANDALONE
3650 : OFPROTO_FAIL_SECURE;
3651 ofproto_set_fail_mode(br->ofproto, fail_mode);
3653 /* Configure OpenFlow controller connection snooping. */
3654 if (!ofproto_has_snoops(br->ofproto)) {
3658 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
3659 ovs_rundir(), br->name));
3660 ofproto_set_snoops(br->ofproto, &snoops);
3661 sset_destroy(&snoops);
3666 bridge_configure_tables(struct bridge *br)
3668 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3672 n_tables = ofproto_get_n_tables(br->ofproto);
3674 for (i = 0; i < n_tables; i++) {
3675 struct ofproto_table_settings s;
3676 bool use_default_prefixes = true;
3679 s.max_flows = UINT_MAX;
3681 s.enable_eviction = false;
3683 s.n_prefix_fields = 0;
3684 memset(s.prefix_fields, ~0, sizeof(s.prefix_fields));
3686 if (j < br->cfg->n_flow_tables && i == br->cfg->key_flow_tables[j]) {
3687 struct ovsrec_flow_table *cfg = br->cfg->value_flow_tables[j++];
3690 if (cfg->n_flow_limit && *cfg->flow_limit < UINT_MAX) {
3691 s.max_flows = *cfg->flow_limit;
3694 s.enable_eviction = (cfg->overflow_policy
3695 && !strcmp(cfg->overflow_policy, "evict"));
3696 if (cfg->n_groups) {
3697 s.groups = xmalloc(cfg->n_groups * sizeof *s.groups);
3698 for (k = 0; k < cfg->n_groups; k++) {
3699 const char *string = cfg->groups[k];
3702 msg = mf_parse_subfield__(&s.groups[k], &string);
3704 VLOG_WARN_RL(&rl, "bridge %s table %d: error parsing "
3705 "'groups' (%s)", br->name, i, msg);
3707 } else if (*string) {
3708 VLOG_WARN_RL(&rl, "bridge %s table %d: 'groups' "
3709 "element '%s' contains trailing garbage",
3710 br->name, i, cfg->groups[k]);
3717 /* Prefix lookup fields. */
3718 s.n_prefix_fields = 0;
3719 for (k = 0; k < cfg->n_prefixes; k++) {
3720 const char *name = cfg->prefixes[k];
3721 const struct mf_field *mf;
3723 if (strcmp(name, "none") == 0) {
3724 use_default_prefixes = false;
3725 s.n_prefix_fields = 0;
3728 mf = mf_from_name(name);
3730 VLOG_WARN("bridge %s: 'prefixes' with unknown field: %s",
3734 if (mf->flow_be32ofs < 0 || mf->n_bits % 32) {
3735 VLOG_WARN("bridge %s: 'prefixes' with incompatible field: "
3736 "%s", br->name, name);
3739 if (s.n_prefix_fields >= ARRAY_SIZE(s.prefix_fields)) {
3740 VLOG_WARN("bridge %s: 'prefixes' with too many fields, "
3741 "field not used: %s", br->name, name);
3744 use_default_prefixes = false;
3745 s.prefix_fields[s.n_prefix_fields++] = mf->id;
3748 if (use_default_prefixes) {
3749 /* Use default values. */
3750 s.n_prefix_fields = ARRAY_SIZE(default_prefix_fields);
3751 memcpy(s.prefix_fields, default_prefix_fields,
3752 sizeof default_prefix_fields);
3755 struct ds ds = DS_EMPTY_INITIALIZER;
3756 for (k = 0; k < s.n_prefix_fields; k++) {
3758 ds_put_char(&ds, ',');
3760 ds_put_cstr(&ds, mf_from_id(s.prefix_fields[k])->name);
3762 if (s.n_prefix_fields == 0) {
3763 ds_put_cstr(&ds, "none");
3765 VLOG_INFO("bridge %s table %d: Prefix lookup with: %s.",
3766 br->name, i, ds_cstr(&ds));
3770 ofproto_configure_table(br->ofproto, i, &s);
3774 for (; j < br->cfg->n_flow_tables; j++) {
3775 VLOG_WARN_RL(&rl, "bridge %s: ignoring configuration for flow table "
3776 "%"PRId64" not supported by this datapath", br->name,
3777 br->cfg->key_flow_tables[j]);
3782 bridge_configure_dp_desc(struct bridge *br)
3784 ofproto_set_dp_desc(br->ofproto,
3785 smap_get(&br->cfg->other_config, "dp-desc"));
3788 static struct aa_mapping *
3789 bridge_aa_mapping_find(struct bridge *br, const int64_t isid)
3791 struct aa_mapping *m;
3793 HMAP_FOR_EACH_IN_BUCKET (m,
3795 hash_bytes(&isid, sizeof isid, 0),
3797 if (isid == m->isid) {
3804 static struct aa_mapping *
3805 bridge_aa_mapping_create(struct bridge *br,
3809 struct aa_mapping *m;
3811 m = xzalloc(sizeof *m);
3815 m->br_name = xstrdup(br->name);
3816 hmap_insert(&br->mappings,
3818 hash_bytes(&isid, sizeof isid, 0));
3824 bridge_aa_mapping_destroy(struct aa_mapping *m)
3827 struct bridge *br = m->bridge;
3830 ofproto_aa_mapping_unregister(br->ofproto, m);
3833 hmap_remove(&br->mappings, &m->hmap_node);
3842 bridge_aa_mapping_configure(struct aa_mapping *m)
3844 struct aa_mapping_settings s;
3850 ofproto_aa_mapping_register(m->bridge->ofproto, m, &s);
3856 bridge_configure_aa(struct bridge *br)
3858 const struct ovsdb_datum *mc;
3859 struct ovsrec_autoattach *auto_attach = br->cfg->auto_attach;
3860 struct aa_settings aa_s;
3861 struct aa_mapping *m, *next;
3865 ofproto_set_aa(br->ofproto, NULL, NULL);
3869 memset(&aa_s, 0, sizeof aa_s);
3870 aa_s.system_description = auto_attach->system_description;
3871 aa_s.system_name = auto_attach->system_name;
3872 ofproto_set_aa(br->ofproto, NULL, &aa_s);
3874 mc = ovsrec_autoattach_get_mappings(auto_attach,
3876 OVSDB_TYPE_INTEGER);
3877 HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mappings) {
3878 union ovsdb_atom atom;
3880 atom.integer = m->isid;
3881 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3882 VLOG_INFO("Deleting isid=%"PRIu32", vlan=%"PRIu16,
3884 bridge_aa_mapping_destroy(m);
3888 /* Add new mappings and reconfigure existing ones. */
3889 for (i = 0; i < auto_attach->n_mappings; ++i) {
3890 struct aa_mapping *m =
3891 bridge_aa_mapping_find(br, auto_attach->key_mappings[i]);
3894 VLOG_INFO("Adding isid=%"PRId64", vlan=%"PRId64,
3895 auto_attach->key_mappings[i],
3896 auto_attach->value_mappings[i]);
3897 m = bridge_aa_mapping_create(br,
3898 auto_attach->key_mappings[i],
3899 auto_attach->value_mappings[i]);
3901 if (!bridge_aa_mapping_configure(m)) {
3902 bridge_aa_mapping_destroy(m);
3909 bridge_aa_need_refresh(struct bridge *br)
3911 return ofproto_aa_vlan_get_queue_size(br->ofproto) > 0;
3915 bridge_aa_update_trunks(struct port *port, struct bridge_aa_vlan *m)
3917 int64_t *trunks = NULL;
3919 bool found = false, reconfigure = false;
3921 for (i = 0; i < port->cfg->n_trunks; i++) {
3922 if (port->cfg->trunks[i] == m->vlan) {
3929 case BRIDGE_AA_VLAN_OPER_ADD:
3931 trunks = xmalloc(sizeof *trunks * (port->cfg->n_trunks + 1));
3933 for (i = 0; i < port->cfg->n_trunks; i++) {
3934 trunks[i] = port->cfg->trunks[i];
3936 trunks[i++] = m->vlan;
3942 case BRIDGE_AA_VLAN_OPER_REMOVE:
3946 trunks = xmalloc(sizeof *trunks * (port->cfg->n_trunks - 1));
3948 for (i = 0; i < port->cfg->n_trunks; i++) {
3949 if (port->cfg->trunks[i] != m->vlan) {
3950 trunks[j++] = port->cfg->trunks[i];
3959 case BRIDGE_AA_VLAN_OPER_UNDEF:
3961 VLOG_WARN("unrecognized operation %u", m->oper);
3966 /* VLAN switching under trunk mode cause the trunk port to switch all
3967 * VLANs, see ovs-vswitchd.conf.db
3970 static char *vlan_mode_access = "access";
3971 ovsrec_port_set_vlan_mode(port->cfg, vlan_mode_access);
3975 static char *vlan_mode_trunk = "trunk";
3976 ovsrec_port_set_vlan_mode(port->cfg, vlan_mode_trunk);
3979 ovsrec_port_set_trunks(port->cfg, trunks, i);
3981 /* Force reconfigure of the port. */
3982 port_configure(port);
3987 bridge_aa_refresh_queued(struct bridge *br)
3989 struct ovs_list *list = xmalloc(sizeof *list);
3990 struct bridge_aa_vlan *node, *next;
3992 ovs_list_init(list);
3993 ofproto_aa_vlan_get_queued(br->ofproto, list);
3995 LIST_FOR_EACH_SAFE (node, next, list_node, list) {
3998 VLOG_INFO("ifname=%s, vlan=%u, oper=%u", node->port_name, node->vlan,
4001 port = port_lookup(br, node->port_name);
4003 bridge_aa_update_trunks(port, node);
4006 ovs_list_remove(&node->list_node);
4007 free(node->port_name);
4015 /* Port functions. */
4017 static struct port *
4018 port_create(struct bridge *br, const struct ovsrec_port *cfg)
4022 port = xzalloc(sizeof *port);
4024 port->name = xstrdup(cfg->name);
4026 ovs_list_init(&port->ifaces);
4028 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
4032 /* Deletes interfaces from 'port' that are no longer configured for it. */
4034 port_del_ifaces(struct port *port)
4036 struct iface *iface, *next;
4037 struct sset new_ifaces;
4040 /* Collect list of new interfaces. */
4041 sset_init(&new_ifaces);
4042 for (i = 0; i < port->cfg->n_interfaces; i++) {
4043 const char *name = port->cfg->interfaces[i]->name;
4044 const char *type = port->cfg->interfaces[i]->type;
4045 if (strcmp(type, "null")) {
4046 sset_add(&new_ifaces, name);
4050 /* Get rid of deleted interfaces. */
4051 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
4052 if (!sset_contains(&new_ifaces, iface->name)) {
4053 iface_destroy(iface);
4057 sset_destroy(&new_ifaces);
4061 port_destroy(struct port *port)
4064 struct bridge *br = port->bridge;
4065 struct iface *iface, *next;
4068 ofproto_bundle_unregister(br->ofproto, port);
4071 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
4072 iface_destroy__(iface);
4075 hmap_remove(&br->ports, &port->hmap_node);
4081 static struct port *
4082 port_lookup(const struct bridge *br, const char *name)
4086 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
4088 if (!strcmp(port->name, name)) {
4096 enable_lacp(struct port *port, bool *activep)
4098 if (!port->cfg->lacp) {
4099 /* XXX when LACP implementation has been sufficiently tested, enable by
4100 * default and make active on bonded ports. */
4102 } else if (!strcmp(port->cfg->lacp, "off")) {
4104 } else if (!strcmp(port->cfg->lacp, "active")) {
4107 } else if (!strcmp(port->cfg->lacp, "passive")) {
4111 VLOG_WARN("port %s: unknown LACP mode %s",
4112 port->name, port->cfg->lacp);
4117 static struct lacp_settings *
4118 port_configure_lacp(struct port *port, struct lacp_settings *s)
4120 const char *lacp_time, *system_id;
4123 if (!enable_lacp(port, &s->active)) {
4127 s->name = port->name;
4129 system_id = smap_get(&port->cfg->other_config, "lacp-system-id");
4131 if (!ovs_scan(system_id, ETH_ADDR_SCAN_FMT,
4132 ETH_ADDR_SCAN_ARGS(s->id))) {
4133 VLOG_WARN("port %s: LACP system ID (%s) must be an Ethernet"
4134 " address.", port->name, system_id);
4138 s->id = port->bridge->ea;
4141 if (eth_addr_is_zero(s->id)) {
4142 VLOG_WARN("port %s: Invalid zero LACP system ID.", port->name);
4146 /* Prefer bondable links if unspecified. */
4147 priority = smap_get_int(&port->cfg->other_config, "lacp-system-priority",
4149 s->priority = (priority > 0 && priority <= UINT16_MAX
4151 : UINT16_MAX - !ovs_list_is_short(&port->ifaces));
4153 lacp_time = smap_get(&port->cfg->other_config, "lacp-time");
4154 s->fast = lacp_time && !strcasecmp(lacp_time, "fast");
4156 s->fallback_ab_cfg = smap_get_bool(&port->cfg->other_config,
4157 "lacp-fallback-ab", false);
4163 iface_configure_lacp(struct iface *iface, struct lacp_slave_settings *s)
4165 int priority, portid, key;
4167 portid = smap_get_int(&iface->cfg->other_config, "lacp-port-id", 0);
4168 priority = smap_get_int(&iface->cfg->other_config, "lacp-port-priority",
4170 key = smap_get_int(&iface->cfg->other_config, "lacp-aggregation-key", 0);
4172 if (portid <= 0 || portid > UINT16_MAX) {
4173 portid = ofp_to_u16(iface->ofp_port);
4176 if (priority <= 0 || priority > UINT16_MAX) {
4177 priority = UINT16_MAX;
4180 if (key < 0 || key > UINT16_MAX) {
4184 s->name = iface->name;
4186 s->priority = priority;
4191 port_configure_bond(struct port *port, struct bond_settings *s)
4193 const char *detect_s;
4194 struct iface *iface;
4196 int miimon_interval;
4198 s->name = port->name;
4200 if (port->cfg->bond_mode) {
4201 if (!bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
4202 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
4203 port->name, port->cfg->bond_mode,
4204 bond_mode_to_string(s->balance));
4207 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4209 /* XXX: Post version 1.5.*, the default bond_mode changed from SLB to
4210 * active-backup. At some point we should remove this warning. */
4211 VLOG_WARN_RL(&rl, "port %s: Using the default bond_mode %s. Note that"
4212 " in previous versions, the default bond_mode was"
4213 " balance-slb", port->name,
4214 bond_mode_to_string(s->balance));
4216 if (s->balance == BM_SLB && port->bridge->cfg->n_flood_vlans) {
4217 VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
4218 "please use another bond type or disable flood_vlans",
4222 miimon_interval = smap_get_int(&port->cfg->other_config,
4223 "bond-miimon-interval", 0);
4224 if (miimon_interval <= 0) {
4225 miimon_interval = 200;
4228 detect_s = smap_get(&port->cfg->other_config, "bond-detect-mode");
4229 if (!detect_s || !strcmp(detect_s, "carrier")) {
4230 miimon_interval = 0;
4231 } else if (strcmp(detect_s, "miimon")) {
4232 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
4233 "defaulting to carrier", port->name, detect_s);
4234 miimon_interval = 0;
4237 s->up_delay = MAX(0, port->cfg->bond_updelay);
4238 s->down_delay = MAX(0, port->cfg->bond_downdelay);
4239 s->basis = smap_get_int(&port->cfg->other_config, "bond-hash-basis", 0);
4240 s->rebalance_interval = smap_get_int(&port->cfg->other_config,
4241 "bond-rebalance-interval", 10000);
4242 if (s->rebalance_interval && s->rebalance_interval < 1000) {
4243 s->rebalance_interval = 1000;
4246 s->lacp_fallback_ab_cfg = smap_get_bool(&port->cfg->other_config,
4247 "lacp-fallback-ab", false);
4249 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
4250 netdev_set_miimon_interval(iface->netdev, miimon_interval);
4253 mac_s = port->cfg->bond_active_slave;
4254 if (!mac_s || !ovs_scan(mac_s, ETH_ADDR_SCAN_FMT,
4255 ETH_ADDR_SCAN_ARGS(s->active_slave_mac))) {
4256 /* OVSDB did not store the last active interface */
4257 s->active_slave_mac = eth_addr_zero;
4261 /* Returns true if 'port' is synthetic, that is, if we constructed it locally
4262 * instead of obtaining it from the database. */
4264 port_is_synthetic(const struct port *port)
4266 return ovsdb_idl_row_is_synthetic(&port->cfg->header_);
4269 /* Interface functions. */
4272 iface_is_internal(const struct ovsrec_interface *iface,
4273 const struct ovsrec_bridge *br)
4275 /* The local port and "internal" ports are always "internal". */
4276 return !strcmp(iface->type, "internal") || !strcmp(iface->name, br->name);
4279 /* Returns the correct network device type for interface 'iface' in bridge
4282 iface_get_type(const struct ovsrec_interface *iface,
4283 const struct ovsrec_bridge *br)
4287 /* The local port always has type "internal". Other ports take
4288 * their type from the database and default to "system" if none is
4290 if (iface_is_internal(iface, br)) {
4293 type = iface->type[0] ? iface->type : "system";
4300 iface_destroy__(struct iface *iface)
4303 struct port *port = iface->port;
4304 struct bridge *br = port->bridge;
4306 if (br->ofproto && iface->ofp_port != OFPP_NONE) {
4307 ofproto_port_unregister(br->ofproto, iface->ofp_port);
4310 if (iface->ofp_port != OFPP_NONE) {
4311 hmap_remove(&br->ifaces, &iface->ofp_port_node);
4314 ovs_list_remove(&iface->port_elem);
4315 hmap_remove(&br->iface_by_name, &iface->name_node);
4317 /* The user is changing configuration here, so netdev_remove needs to be
4318 * used as opposed to netdev_close */
4319 netdev_remove(iface->netdev);
4327 iface_destroy(struct iface *iface)
4330 struct port *port = iface->port;
4332 iface_destroy__(iface);
4333 if (ovs_list_is_empty(&port->ifaces)) {
4339 static struct iface *
4340 iface_lookup(const struct bridge *br, const char *name)
4342 struct iface *iface;
4344 HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
4345 &br->iface_by_name) {
4346 if (!strcmp(iface->name, name)) {
4354 static struct iface *
4355 iface_find(const char *name)
4357 const struct bridge *br;
4359 HMAP_FOR_EACH (br, node, &all_bridges) {
4360 struct iface *iface = iface_lookup(br, name);
4369 static struct iface *
4370 iface_from_ofp_port(const struct bridge *br, ofp_port_t ofp_port)
4372 struct iface *iface;
4374 HMAP_FOR_EACH_IN_BUCKET (iface, ofp_port_node, hash_ofp_port(ofp_port),
4376 if (iface->ofp_port == ofp_port) {
4383 /* Set Ethernet address of 'iface', if one is specified in the configuration
4386 iface_set_mac(const struct bridge *br, const struct port *port, struct iface *iface)
4388 struct eth_addr ea, *mac = NULL;
4389 struct iface *hw_addr_iface;
4391 if (strcmp(iface->type, "internal")) {
4395 if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, &ea)) {
4397 } else if (port->cfg->fake_bridge) {
4398 /* Fake bridge and no MAC set in the configuration. Pick a local one. */
4399 find_local_hw_addr(br, &ea, port, &hw_addr_iface);
4404 if (iface->ofp_port == OFPP_LOCAL) {
4405 VLOG_ERR("interface %s: ignoring mac in Interface record "
4406 "(use Bridge record to set local port's mac)",
4408 } else if (eth_addr_is_multicast(*mac)) {
4409 VLOG_ERR("interface %s: cannot set MAC to multicast address",
4412 int error = netdev_set_etheraddr(iface->netdev, *mac);
4414 VLOG_ERR("interface %s: setting MAC failed (%s)",
4415 iface->name, ovs_strerror(error));
4421 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
4423 iface_set_ofport(const struct ovsrec_interface *if_cfg, ofp_port_t ofport)
4425 if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
4426 int64_t port = ofport == OFPP_NONE ? -1 : ofp_to_u16(ofport);
4427 ovsrec_interface_set_ofport(if_cfg, &port, 1);
4431 /* Clears all of the fields in 'if_cfg' that indicate interface status, and
4432 * sets the "ofport" field to -1.
4434 * This is appropriate when 'if_cfg''s interface cannot be created or is
4435 * otherwise invalid. */
4437 iface_clear_db_record(const struct ovsrec_interface *if_cfg, char *errp)
4439 if (!ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
4440 iface_set_ofport(if_cfg, OFPP_NONE);
4441 ovsrec_interface_set_error(if_cfg, errp);
4442 ovsrec_interface_set_status(if_cfg, NULL);
4443 ovsrec_interface_set_admin_state(if_cfg, NULL);
4444 ovsrec_interface_set_duplex(if_cfg, NULL);
4445 ovsrec_interface_set_link_speed(if_cfg, NULL, 0);
4446 ovsrec_interface_set_link_state(if_cfg, NULL);
4447 ovsrec_interface_set_mac_in_use(if_cfg, NULL);
4448 ovsrec_interface_set_mtu(if_cfg, NULL, 0);
4449 ovsrec_interface_set_cfm_fault(if_cfg, NULL, 0);
4450 ovsrec_interface_set_cfm_fault_status(if_cfg, NULL, 0);
4451 ovsrec_interface_set_cfm_remote_mpids(if_cfg, NULL, 0);
4452 ovsrec_interface_set_lacp_current(if_cfg, NULL, 0);
4453 ovsrec_interface_set_statistics(if_cfg, NULL, NULL, 0);
4454 ovsrec_interface_set_ifindex(if_cfg, NULL, 0);
4459 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
4461 union ovsdb_atom atom;
4463 atom.integer = target;
4464 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
4468 iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
4470 struct ofpbuf queues_buf;
4472 ofpbuf_init(&queues_buf, 0);
4474 if (!qos || qos->type[0] == '\0') {
4475 netdev_set_qos(iface->netdev, NULL, NULL);
4477 const struct ovsdb_datum *queues;
4478 struct netdev_queue_dump dump;
4479 unsigned int queue_id;
4480 struct smap details;
4484 /* Configure top-level Qos for 'iface'. */
4485 netdev_set_qos(iface->netdev, qos->type, &qos->other_config);
4487 /* Deconfigure queues that were deleted. */
4488 queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
4490 smap_init(&details);
4491 NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
4492 if (!queue_ids_include(queues, queue_id)) {
4493 netdev_delete_queue(iface->netdev, queue_id);
4496 smap_destroy(&details);
4498 /* Configure queues for 'iface'. */
4500 for (i = 0; i < qos->n_queues; i++) {
4501 const struct ovsrec_queue *queue = qos->value_queues[i];
4502 unsigned int queue_id = qos->key_queues[i];
4504 if (queue_id == 0) {
4508 if (queue->n_dscp == 1) {
4509 struct ofproto_port_queue *port_queue;
4511 port_queue = ofpbuf_put_uninit(&queues_buf,
4512 sizeof *port_queue);
4513 port_queue->queue = queue_id;
4514 port_queue->dscp = queue->dscp[0];
4517 netdev_set_queue(iface->netdev, queue_id, &queue->other_config);
4520 struct smap details;
4522 smap_init(&details);
4523 netdev_set_queue(iface->netdev, 0, &details);
4524 smap_destroy(&details);
4528 if (iface->ofp_port != OFPP_NONE) {
4529 const struct ofproto_port_queue *port_queues = queues_buf.data;
4530 size_t n_queues = queues_buf.size / sizeof *port_queues;
4532 ofproto_port_set_queues(iface->port->bridge->ofproto, iface->ofp_port,
4533 port_queues, n_queues);
4536 netdev_set_policing(iface->netdev,
4537 MIN(UINT32_MAX, iface->cfg->ingress_policing_rate),
4538 MIN(UINT32_MAX, iface->cfg->ingress_policing_burst));
4540 ofpbuf_uninit(&queues_buf);
4544 iface_configure_cfm(struct iface *iface)
4546 const struct ovsrec_interface *cfg = iface->cfg;
4547 const char *opstate_str;
4548 const char *cfm_ccm_vlan;
4549 struct cfm_settings s;
4550 struct smap netdev_args;
4552 if (!cfg->n_cfm_mpid) {
4553 ofproto_port_clear_cfm(iface->port->bridge->ofproto, iface->ofp_port);
4557 s.check_tnl_key = false;
4558 smap_init(&netdev_args);
4559 if (!netdev_get_config(iface->netdev, &netdev_args)) {
4560 const char *key = smap_get(&netdev_args, "key");
4561 const char *in_key = smap_get(&netdev_args, "in_key");
4563 s.check_tnl_key = (key && !strcmp(key, "flow"))
4564 || (in_key && !strcmp(in_key, "flow"));
4566 smap_destroy(&netdev_args);
4568 s.mpid = *cfg->cfm_mpid;
4569 s.interval = smap_get_int(&iface->cfg->other_config, "cfm_interval", 0);
4570 cfm_ccm_vlan = smap_get(&iface->cfg->other_config, "cfm_ccm_vlan");
4571 s.ccm_pcp = smap_get_int(&iface->cfg->other_config, "cfm_ccm_pcp", 0);
4573 if (s.interval <= 0) {
4577 if (!cfm_ccm_vlan) {
4579 } else if (!strcasecmp("random", cfm_ccm_vlan)) {
4580 s.ccm_vlan = CFM_RANDOM_VLAN;
4582 s.ccm_vlan = atoi(cfm_ccm_vlan);
4583 if (s.ccm_vlan == CFM_RANDOM_VLAN) {
4588 s.extended = smap_get_bool(&iface->cfg->other_config, "cfm_extended",
4590 s.demand = smap_get_bool(&iface->cfg->other_config, "cfm_demand", false);
4592 opstate_str = smap_get(&iface->cfg->other_config, "cfm_opstate");
4593 s.opup = !opstate_str || !strcasecmp("up", opstate_str);
4595 ofproto_port_set_cfm(iface->port->bridge->ofproto, iface->ofp_port, &s);
4598 /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
4599 * instead of obtaining it from the database. */
4601 iface_is_synthetic(const struct iface *iface)
4603 return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
4607 iface_validate_ofport__(size_t n, int64_t *ofport)
4609 return (n && *ofport >= 1 && *ofport < ofp_to_u16(OFPP_MAX)
4610 ? u16_to_ofp(*ofport)
4615 iface_get_requested_ofp_port(const struct ovsrec_interface *cfg)
4617 return iface_validate_ofport__(cfg->n_ofport_request, cfg->ofport_request);
4621 iface_pick_ofport(const struct ovsrec_interface *cfg)
4623 ofp_port_t requested_ofport = iface_get_requested_ofp_port(cfg);
4624 return (requested_ofport != OFPP_NONE
4626 : iface_validate_ofport__(cfg->n_ofport, cfg->ofport));
4629 /* Port mirroring. */
4631 static struct mirror *
4632 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
4636 HMAP_FOR_EACH_IN_BUCKET (m, hmap_node, uuid_hash(uuid), &br->mirrors) {
4637 if (uuid_equals(uuid, &m->uuid)) {
4645 bridge_configure_mirrors(struct bridge *br)
4647 const struct ovsdb_datum *mc;
4648 unsigned long *flood_vlans;
4649 struct mirror *m, *next;
4652 /* Get rid of deleted mirrors. */
4653 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
4654 HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mirrors) {
4655 union ovsdb_atom atom;
4657 atom.uuid = m->uuid;
4658 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
4663 /* Add new mirrors and reconfigure existing ones. */
4664 for (i = 0; i < br->cfg->n_mirrors; i++) {
4665 const struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
4666 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
4668 m = mirror_create(br, cfg);
4671 if (!mirror_configure(m)) {
4676 /* Update flooded vlans (for RSPAN). */
4677 flood_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
4678 br->cfg->n_flood_vlans);
4679 ofproto_set_flood_vlans(br->ofproto, flood_vlans);
4680 bitmap_free(flood_vlans);
4683 static struct mirror *
4684 mirror_create(struct bridge *br, const struct ovsrec_mirror *cfg)
4688 m = xzalloc(sizeof *m);
4689 m->uuid = cfg->header_.uuid;
4690 hmap_insert(&br->mirrors, &m->hmap_node, uuid_hash(&m->uuid));
4692 m->name = xstrdup(cfg->name);
4698 mirror_destroy(struct mirror *m)
4701 struct bridge *br = m->bridge;
4704 ofproto_mirror_unregister(br->ofproto, m);
4707 hmap_remove(&br->mirrors, &m->hmap_node);
4714 mirror_collect_ports(struct mirror *m,
4715 struct ovsrec_port **in_ports, int n_in_ports,
4716 void ***out_portsp, size_t *n_out_portsp)
4718 void **out_ports = xmalloc(n_in_ports * sizeof *out_ports);
4719 size_t n_out_ports = 0;
4722 for (i = 0; i < n_in_ports; i++) {
4723 const char *name = in_ports[i]->name;
4724 struct port *port = port_lookup(m->bridge, name);
4726 out_ports[n_out_ports++] = port;
4728 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
4729 "port %s", m->bridge->name, m->name, name);
4732 *out_portsp = out_ports;
4733 *n_out_portsp = n_out_ports;
4737 mirror_configure(struct mirror *m)
4739 const struct ovsrec_mirror *cfg = m->cfg;
4740 struct ofproto_mirror_settings s;
4743 if (strcmp(cfg->name, m->name)) {
4745 m->name = xstrdup(cfg->name);
4749 /* Get output port or VLAN. */
4750 if (cfg->output_port) {
4751 s.out_bundle = port_lookup(m->bridge, cfg->output_port->name);
4752 if (!s.out_bundle) {
4753 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
4754 m->bridge->name, m->name);
4757 s.out_vlan = UINT16_MAX;
4759 if (cfg->output_vlan) {
4760 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
4761 "output vlan; ignoring output vlan",
4762 m->bridge->name, m->name);
4764 } else if (cfg->output_vlan) {
4765 /* The database should prevent invalid VLAN values. */
4766 s.out_bundle = NULL;
4767 s.out_vlan = *cfg->output_vlan;
4769 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
4770 m->bridge->name, m->name);
4775 s.snaplen = *cfg->snaplen;
4780 /* Get port selection. */
4781 if (cfg->select_all) {
4782 size_t n_ports = hmap_count(&m->bridge->ports);
4783 void **ports = xmalloc(n_ports * sizeof *ports);
4788 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
4798 /* Get ports, dropping ports that don't exist.
4799 * The IDL ensures that there are no duplicates. */
4800 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
4801 &s.srcs, &s.n_srcs);
4802 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
4803 &s.dsts, &s.n_dsts);
4806 /* Get VLAN selection. */
4807 s.src_vlans = vlan_bitmap_from_array(cfg->select_vlan, cfg->n_select_vlan);
4810 ofproto_mirror_register(m->bridge->ofproto, m, &s);
4813 if (s.srcs != s.dsts) {
4824 mirror_refresh_stats(struct mirror *m)
4826 struct ofproto *ofproto = m->bridge->ofproto;
4827 uint64_t tx_packets, tx_bytes;
4828 const char *keys[2];
4830 size_t stat_cnt = 0;
4832 if (ofproto_mirror_get_stats(ofproto, m, &tx_packets, &tx_bytes)) {
4833 ovsrec_mirror_set_statistics(m->cfg, NULL, NULL, 0);
4837 if (tx_packets != UINT64_MAX) {
4838 keys[stat_cnt] = "tx_packets";
4839 values[stat_cnt] = tx_packets;
4842 if (tx_bytes != UINT64_MAX) {
4843 keys[stat_cnt] = "tx_bytes";
4844 values[stat_cnt] = tx_bytes;
4848 ovsrec_mirror_set_statistics(m->cfg, keys, values, stat_cnt);
4852 * Add registered netdev and dpif types to ovsdb to allow external
4853 * applications to query the capabilities of the Open vSwitch instance
4854 * running on the node.
4857 discover_types(const struct ovsrec_open_vswitch *cfg)
4861 /* Datapath types. */
4863 dp_enumerate_types(&types);
4864 const char **datapath_types = sset_array(&types);
4865 ovsrec_open_vswitch_set_datapath_types(cfg, datapath_types,
4866 sset_count(&types));
4867 free(datapath_types);
4868 sset_destroy(&types);
4872 netdev_enumerate_types(&types);
4873 const char **iface_types = sset_array(&types);
4874 ovsrec_open_vswitch_set_iface_types(cfg, iface_types, sset_count(&types));
4876 sset_destroy(&types);