1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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"
33 #include "openvswitch/hmap.h"
35 #include "if-notifier.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 struct seq
*ifaces_changed
;
227 static uint64_t last_ifaces_changed
;
229 static void add_del_bridges(const struct ovsrec_open_vswitch
*);
230 static void bridge_run__(void);
231 static void bridge_create(const struct ovsrec_bridge
*);
232 static void bridge_destroy(struct bridge
*, bool del
);
233 static struct bridge
*bridge_lookup(const char *name
);
234 static unixctl_cb_func bridge_unixctl_dump_flows
;
235 static unixctl_cb_func bridge_unixctl_reconnect
;
236 static size_t bridge_get_controllers(const struct bridge
*br
,
237 struct ovsrec_controller
***controllersp
);
238 static void bridge_collect_wanted_ports(struct bridge
*,
239 struct shash
*wanted_ports
);
240 static void bridge_delete_ofprotos(void);
241 static void bridge_delete_or_reconfigure_ports(struct bridge
*);
242 static void bridge_del_ports(struct bridge
*,
243 const struct shash
*wanted_ports
);
244 static void bridge_add_ports(struct bridge
*,
245 const struct shash
*wanted_ports
);
247 static void bridge_configure_datapath_id(struct bridge
*);
248 static void bridge_configure_netflow(struct bridge
*);
249 static void bridge_configure_forward_bpdu(struct bridge
*);
250 static void bridge_configure_mac_table(struct bridge
*);
251 static void bridge_configure_mcast_snooping(struct bridge
*);
252 static void bridge_configure_sflow(struct bridge
*, int *sflow_bridge_number
);
253 static void bridge_configure_ipfix(struct bridge
*);
254 static void bridge_configure_spanning_tree(struct bridge
*);
255 static void bridge_configure_tables(struct bridge
*);
256 static void bridge_configure_dp_desc(struct bridge
*);
257 static void bridge_configure_aa(struct bridge
*);
258 static void bridge_aa_refresh_queued(struct bridge
*);
259 static bool bridge_aa_need_refresh(struct bridge
*);
260 static void bridge_configure_remotes(struct bridge
*,
261 const struct sockaddr_in
*managers
,
263 static void bridge_pick_local_hw_addr(struct bridge
*, struct eth_addr
*ea
,
264 struct iface
**hw_addr_iface
);
265 static uint64_t bridge_pick_datapath_id(struct bridge
*,
266 const struct eth_addr bridge_ea
,
267 struct iface
*hw_addr_iface
);
268 static uint64_t dpid_from_hash(const void *, size_t nbytes
);
269 static bool bridge_has_bond_fake_iface(const struct bridge
*,
271 static bool port_is_bond_fake_iface(const struct port
*);
273 static unixctl_cb_func qos_unixctl_show_types
;
274 static unixctl_cb_func qos_unixctl_show
;
276 static struct port
*port_create(struct bridge
*, const struct ovsrec_port
*);
277 static void port_del_ifaces(struct port
*);
278 static void port_destroy(struct port
*);
279 static struct port
*port_lookup(const struct bridge
*, const char *name
);
280 static void port_configure(struct port
*);
281 static struct lacp_settings
*port_configure_lacp(struct port
*,
282 struct lacp_settings
*);
283 static void port_configure_bond(struct port
*, struct bond_settings
*);
284 static bool port_is_synthetic(const struct port
*);
286 static void reconfigure_system_stats(const struct ovsrec_open_vswitch
*);
287 static void run_system_stats(void);
289 static void bridge_configure_mirrors(struct bridge
*);
290 static struct mirror
*mirror_create(struct bridge
*,
291 const struct ovsrec_mirror
*);
292 static void mirror_destroy(struct mirror
*);
293 static bool mirror_configure(struct mirror
*);
294 static void mirror_refresh_stats(struct mirror
*);
296 static void iface_configure_lacp(struct iface
*, struct lacp_slave_settings
*);
297 static bool iface_create(struct bridge
*, const struct ovsrec_interface
*,
298 const struct ovsrec_port
*);
299 static bool iface_is_internal(const struct ovsrec_interface
*iface
,
300 const struct ovsrec_bridge
*br
);
301 static const char *iface_get_type(const struct ovsrec_interface
*,
302 const struct ovsrec_bridge
*);
303 static void iface_destroy(struct iface
*);
304 static void iface_destroy__(struct iface
*);
305 static struct iface
*iface_lookup(const struct bridge
*, const char *name
);
306 static struct iface
*iface_find(const char *name
);
307 static struct iface
*iface_from_ofp_port(const struct bridge
*,
308 ofp_port_t ofp_port
);
309 static void iface_set_mac(const struct bridge
*, const struct port
*, struct iface
*);
310 static void iface_set_ofport(const struct ovsrec_interface
*, ofp_port_t ofport
);
311 static void iface_clear_db_record(const struct ovsrec_interface
*if_cfg
, char *errp
);
312 static void iface_configure_qos(struct iface
*, const struct ovsrec_qos
*);
313 static void iface_configure_cfm(struct iface
*);
314 static void iface_refresh_cfm_stats(struct iface
*);
315 static void iface_refresh_stats(struct iface
*);
316 static void iface_refresh_netdev_status(struct iface
*);
317 static void iface_refresh_ofproto_status(struct iface
*);
318 static bool iface_is_synthetic(const struct iface
*);
319 static ofp_port_t
iface_get_requested_ofp_port(
320 const struct ovsrec_interface
*);
321 static ofp_port_t
iface_pick_ofport(const struct ovsrec_interface
*);
324 static void discover_types(const struct ovsrec_open_vswitch
*cfg
);
327 bridge_init_ofproto(const struct ovsrec_open_vswitch
*cfg
)
329 struct shash iface_hints
;
330 static bool initialized
= false;
337 shash_init(&iface_hints
);
340 for (i
= 0; i
< cfg
->n_bridges
; i
++) {
341 const struct ovsrec_bridge
*br_cfg
= cfg
->bridges
[i
];
344 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
345 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
348 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
349 struct ovsrec_interface
*if_cfg
= port_cfg
->interfaces
[k
];
350 struct iface_hint
*iface_hint
;
352 iface_hint
= xmalloc(sizeof *iface_hint
);
353 iface_hint
->br_name
= br_cfg
->name
;
354 iface_hint
->br_type
= br_cfg
->datapath_type
;
355 iface_hint
->ofp_port
= iface_pick_ofport(if_cfg
);
357 shash_add(&iface_hints
, if_cfg
->name
, iface_hint
);
363 ofproto_init(&iface_hints
);
365 shash_destroy_free_data(&iface_hints
);
370 if_change_cb(void *aux OVS_UNUSED
)
372 seq_change(ifaces_changed
);
376 if_notifier_changed(struct if_notifier
*notifier OVS_UNUSED
)
379 bool changed
= false;
380 new_seq
= seq_read(ifaces_changed
);
381 if (new_seq
!= last_ifaces_changed
) {
383 last_ifaces_changed
= new_seq
;
385 seq_wait(ifaces_changed
, last_ifaces_changed
);
389 /* Public functions. */
391 /* Initializes the bridge module, configuring it to obtain its configuration
392 * from an OVSDB server accessed over 'remote', which should be a string in a
393 * form acceptable to ovsdb_idl_create(). */
395 bridge_init(const char *remote
)
397 /* Create connection to database. */
398 idl
= ovsdb_idl_create(remote
, &ovsrec_idl_class
, true, true);
399 idl_seqno
= ovsdb_idl_get_seqno(idl
);
400 ovsdb_idl_set_lock(idl
, "ovs_vswitchd");
401 ovsdb_idl_verify_write_only(idl
);
403 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
404 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_statistics
);
405 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_datapath_types
);
406 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_iface_types
);
407 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_external_ids
);
408 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_ovs_version
);
409 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_db_version
);
410 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_system_type
);
411 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_system_version
);
413 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_datapath_id
);
414 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_datapath_version
);
415 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_status
);
416 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_rstp_status
);
417 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_stp_enable
);
418 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_rstp_enable
);
419 ovsdb_idl_omit(idl
, &ovsrec_bridge_col_external_ids
);
421 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_status
);
422 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_rstp_status
);
423 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_rstp_statistics
);
424 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_statistics
);
425 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_bond_active_slave
);
426 ovsdb_idl_omit(idl
, &ovsrec_port_col_external_ids
);
427 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_trunks
);
428 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_vlan_mode
);
429 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_admin_state
);
430 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_duplex
);
431 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_link_speed
);
432 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_link_state
);
433 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_link_resets
);
434 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_mac_in_use
);
435 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_ifindex
);
436 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_mtu
);
437 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_ofport
);
438 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_statistics
);
439 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_status
);
440 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_fault
);
441 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_fault_status
);
442 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_remote_mpids
);
443 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_flap_count
);
444 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_health
);
445 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_remote_opstate
);
446 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_bfd_status
);
447 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_lacp_current
);
448 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_error
);
449 ovsdb_idl_omit(idl
, &ovsrec_interface_col_external_ids
);
451 ovsdb_idl_omit_alert(idl
, &ovsrec_controller_col_is_connected
);
452 ovsdb_idl_omit_alert(idl
, &ovsrec_controller_col_role
);
453 ovsdb_idl_omit_alert(idl
, &ovsrec_controller_col_status
);
454 ovsdb_idl_omit(idl
, &ovsrec_controller_col_external_ids
);
456 ovsdb_idl_omit(idl
, &ovsrec_qos_col_external_ids
);
458 ovsdb_idl_omit(idl
, &ovsrec_queue_col_external_ids
);
460 ovsdb_idl_omit(idl
, &ovsrec_mirror_col_external_ids
);
461 ovsdb_idl_omit_alert(idl
, &ovsrec_mirror_col_statistics
);
463 ovsdb_idl_omit(idl
, &ovsrec_netflow_col_external_ids
);
464 ovsdb_idl_omit(idl
, &ovsrec_sflow_col_external_ids
);
465 ovsdb_idl_omit(idl
, &ovsrec_ipfix_col_external_ids
);
466 ovsdb_idl_omit(idl
, &ovsrec_flow_sample_collector_set_col_external_ids
);
468 ovsdb_idl_omit(idl
, &ovsrec_manager_col_external_ids
);
469 ovsdb_idl_omit(idl
, &ovsrec_manager_col_inactivity_probe
);
470 ovsdb_idl_omit(idl
, &ovsrec_manager_col_is_connected
);
471 ovsdb_idl_omit(idl
, &ovsrec_manager_col_max_backoff
);
472 ovsdb_idl_omit(idl
, &ovsrec_manager_col_status
);
474 ovsdb_idl_omit(idl
, &ovsrec_ssl_col_external_ids
);
476 /* Register unixctl commands. */
477 unixctl_command_register("qos/show-types", "interface", 1, 1,
478 qos_unixctl_show_types
, NULL
);
479 unixctl_command_register("qos/show", "interface", 1, 1,
480 qos_unixctl_show
, NULL
);
481 unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
482 bridge_unixctl_dump_flows
, NULL
);
483 unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
484 bridge_unixctl_reconnect
, NULL
);
493 ifaces_changed
= seq_create();
494 last_ifaces_changed
= seq_read(ifaces_changed
);
495 ifnotifier
= if_notifier_create(if_change_cb
, NULL
);
499 bridge_exit(bool delete_datapath
)
501 struct bridge
*br
, *next_br
;
503 if_notifier_destroy(ifnotifier
);
504 seq_destroy(ifaces_changed
);
505 HMAP_FOR_EACH_SAFE (br
, next_br
, node
, &all_bridges
) {
506 bridge_destroy(br
, delete_datapath
);
508 ovsdb_idl_destroy(idl
);
511 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
512 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
513 * responsible for freeing '*managersp' (with free()).
515 * You may be asking yourself "why does ovs-vswitchd care?", because
516 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
517 * should not be and in fact is not directly involved in that. But
518 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
519 * it has to tell in-band control where the managers are to enable that.
520 * (Thus, only managers connected in-band and with non-loopback addresses
524 collect_in_band_managers(const struct ovsrec_open_vswitch
*ovs_cfg
,
525 struct sockaddr_in
**managersp
, size_t *n_managersp
)
527 struct sockaddr_in
*managers
= NULL
;
528 size_t n_managers
= 0;
532 /* Collect all of the potential targets from the "targets" columns of the
533 * rows pointed to by "manager_options", excluding any that are
536 for (i
= 0; i
< ovs_cfg
->n_manager_options
; i
++) {
537 struct ovsrec_manager
*m
= ovs_cfg
->manager_options
[i
];
539 if (m
->connection_mode
&& !strcmp(m
->connection_mode
, "out-of-band")) {
540 sset_find_and_delete(&targets
, m
->target
);
542 sset_add(&targets
, m
->target
);
546 /* Now extract the targets' IP addresses. */
547 if (!sset_is_empty(&targets
)) {
550 managers
= xmalloc(sset_count(&targets
) * sizeof *managers
);
551 SSET_FOR_EACH (target
, &targets
) {
553 struct sockaddr_storage ss
;
554 struct sockaddr_in in
;
557 /* Ignore loopback. */
558 if (stream_parse_target_with_default_port(target
, OVSDB_PORT
,
560 && sa
.ss
.ss_family
== AF_INET
561 && sa
.in
.sin_addr
.s_addr
!= htonl(INADDR_LOOPBACK
)) {
562 managers
[n_managers
++] = sa
.in
;
566 sset_destroy(&targets
);
568 *managersp
= managers
;
569 *n_managersp
= n_managers
;
573 config_ofproto_types(const struct smap
*other_config
)
578 /* Pass custom configuration to datapath types. */
580 ofproto_enumerate_types(&types
);
581 SSET_FOR_EACH (type
, &types
) {
582 ofproto_type_set_config(type
, other_config
);
584 sset_destroy(&types
);
588 bridge_reconfigure(const struct ovsrec_open_vswitch
*ovs_cfg
)
590 struct sockaddr_in
*managers
;
591 struct bridge
*br
, *next
;
592 int sflow_bridge_number
;
595 COVERAGE_INC(bridge_reconfigure
);
597 ofproto_set_flow_limit(smap_get_int(&ovs_cfg
->other_config
, "flow-limit",
598 OFPROTO_FLOW_LIMIT_DEFAULT
));
599 ofproto_set_max_idle(smap_get_int(&ovs_cfg
->other_config
, "max-idle",
600 OFPROTO_MAX_IDLE_DEFAULT
));
601 ofproto_set_vlan_limit(smap_get_int(&ovs_cfg
->other_config
, "vlan-limit",
602 LEGACY_MAX_VLAN_HEADERS
));
605 smap_get_int(&ovs_cfg
->other_config
, "n-handler-threads", 0),
606 smap_get_int(&ovs_cfg
->other_config
, "n-revalidator-threads", 0));
608 /* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
609 * to 'ovs_cfg', with only very minimal configuration otherwise.
611 * This is mostly an update to bridge data structures. Nothing is pushed
612 * down to ofproto or lower layers. */
613 add_del_bridges(ovs_cfg
);
614 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
615 bridge_collect_wanted_ports(br
, &br
->wanted_ports
);
616 bridge_del_ports(br
, &br
->wanted_ports
);
619 /* Start pushing configuration changes down to the ofproto layer:
621 * - Delete ofprotos that are no longer configured.
623 * - Delete ports that are no longer configured.
625 * - Reconfigure existing ports to their desired configurations, or
626 * delete them if not possible.
628 * We have to do all the deletions before we can do any additions, because
629 * the ports to be added might require resources that will be freed up by
630 * deletions (they might especially overlap in name). */
631 bridge_delete_ofprotos();
632 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
634 bridge_delete_or_reconfigure_ports(br
);
638 /* Finish pushing configuration changes to the ofproto layer:
640 * - Create ofprotos that are missing.
642 * - Add ports that are missing. */
643 HMAP_FOR_EACH_SAFE (br
, next
, node
, &all_bridges
) {
647 error
= ofproto_create(br
->name
, br
->type
, &br
->ofproto
);
649 VLOG_ERR("failed to create bridge %s: %s", br
->name
,
650 ovs_strerror(error
));
651 shash_destroy(&br
->wanted_ports
);
652 bridge_destroy(br
, true);
654 /* Trigger storing datapath version. */
655 seq_change(connectivity_seq_get());
660 config_ofproto_types(&ovs_cfg
->other_config
);
662 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
663 bridge_add_ports(br
, &br
->wanted_ports
);
664 shash_destroy(&br
->wanted_ports
);
667 reconfigure_system_stats(ovs_cfg
);
669 /* Complete the configuration. */
670 sflow_bridge_number
= 0;
671 collect_in_band_managers(ovs_cfg
, &managers
, &n_managers
);
672 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
675 /* We need the datapath ID early to allow LACP ports to use it as the
676 * default system ID. */
677 bridge_configure_datapath_id(br
);
679 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
682 port_configure(port
);
684 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
685 iface_set_ofport(iface
->cfg
, iface
->ofp_port
);
686 /* Clear eventual previous errors */
687 ovsrec_interface_set_error(iface
->cfg
, NULL
);
688 iface_configure_cfm(iface
);
689 iface_configure_qos(iface
, port
->cfg
->qos
);
690 iface_set_mac(br
, port
, iface
);
691 ofproto_port_set_bfd(br
->ofproto
, iface
->ofp_port
,
693 ofproto_port_set_lldp(br
->ofproto
, iface
->ofp_port
,
695 ofproto_port_set_config(br
->ofproto
, iface
->ofp_port
,
696 &iface
->cfg
->other_config
);
699 bridge_configure_mirrors(br
);
700 bridge_configure_forward_bpdu(br
);
701 bridge_configure_mac_table(br
);
702 bridge_configure_mcast_snooping(br
);
703 bridge_configure_remotes(br
, managers
, n_managers
);
704 bridge_configure_netflow(br
);
705 bridge_configure_sflow(br
, &sflow_bridge_number
);
706 bridge_configure_ipfix(br
);
707 bridge_configure_spanning_tree(br
);
708 bridge_configure_tables(br
);
709 bridge_configure_dp_desc(br
);
710 bridge_configure_aa(br
);
714 /* The ofproto-dpif provider does some final reconfiguration in its
715 * ->type_run() function. We have to call it before notifying the database
716 * client that reconfiguration is complete, otherwise there is a very
717 * narrow race window in which e.g. ofproto/trace will not recognize the
718 * new configuration (sometimes this causes unit test failures). */
722 /* Delete ofprotos which aren't configured or have the wrong type. Create
723 * ofprotos which don't exist but need to. */
725 bridge_delete_ofprotos(void)
732 /* Delete ofprotos with no bridge or with the wrong type. */
735 ofproto_enumerate_types(&types
);
736 SSET_FOR_EACH (type
, &types
) {
739 ofproto_enumerate_names(type
, &names
);
740 SSET_FOR_EACH (name
, &names
) {
741 br
= bridge_lookup(name
);
742 if (!br
|| strcmp(type
, br
->type
)) {
743 ofproto_delete(name
, type
);
747 sset_destroy(&names
);
748 sset_destroy(&types
);
752 add_ofp_port(ofp_port_t port
, ofp_port_t
*ports
, size_t *n
, size_t *allocated
)
754 if (*n
>= *allocated
) {
755 ports
= x2nrealloc(ports
, allocated
, sizeof *ports
);
757 ports
[(*n
)++] = port
;
761 /* Configures the MTU of 'netdev' based on the "mtu_request" column
764 iface_set_netdev_mtu(const struct ovsrec_interface
*iface_cfg
,
765 struct netdev
*netdev
)
767 if (iface_cfg
->n_mtu_request
== 1) {
768 /* The user explicitly asked for this MTU. */
769 netdev_mtu_user_config(netdev
, true);
770 /* Try to set the MTU to the requested value. */
771 return netdev_set_mtu(netdev
, *iface_cfg
->mtu_request
);
774 /* The user didn't explicitly asked for any MTU. */
775 netdev_mtu_user_config(netdev
, false);
780 bridge_delete_or_reconfigure_ports(struct bridge
*br
)
782 struct ofproto_port ofproto_port
;
783 struct ofproto_port_dump dump
;
785 struct sset ofproto_ports
;
786 struct port
*port
, *port_next
;
788 /* List of "ofp_port"s to delete. We make a list instead of deleting them
789 * right away because ofproto implementations aren't necessarily able to
790 * iterate through a changing list of ports in an entirely robust way. */
797 sset_init(&ofproto_ports
);
799 /* Main task: Iterate over the ports in 'br->ofproto' and remove the ports
800 * that are not configured in the database. (This commonly happens when
801 * ports have been deleted, e.g. with "ovs-vsctl del-port".)
803 * Side tasks: Reconfigure the ports that are still in 'br'. Delete ports
804 * that have the wrong OpenFlow port number (and arrange to add them back
805 * with the correct OpenFlow port number). */
806 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, br
->ofproto
) {
807 ofp_port_t requested_ofp_port
;
810 sset_add(&ofproto_ports
, ofproto_port
.name
);
812 iface
= iface_lookup(br
, ofproto_port
.name
);
814 /* No such iface is configured, so we should delete this
817 * As a corner case exception, keep the port if it's a bond fake
819 if (bridge_has_bond_fake_iface(br
, ofproto_port
.name
)
820 && !strcmp(ofproto_port
.type
, "internal")) {
826 if (strcmp(ofproto_port
.type
, iface
->netdev_type
)
827 || netdev_set_config(iface
->netdev
, &iface
->cfg
->options
, NULL
)) {
828 /* The interface is the wrong type or can't be configured.
833 iface_set_netdev_mtu(iface
->cfg
, iface
->netdev
);
835 /* If the requested OpenFlow port for 'iface' changed, and it's not
836 * already the correct port, then we might want to temporarily delete
837 * this interface, so we can add it back again with the new OpenFlow
839 requested_ofp_port
= iface_get_requested_ofp_port(iface
->cfg
);
840 if (iface
->ofp_port
!= OFPP_LOCAL
&&
841 requested_ofp_port
!= OFPP_NONE
&&
842 requested_ofp_port
!= iface
->ofp_port
) {
843 ofp_port_t victim_request
;
844 struct iface
*victim
;
846 /* Check for an existing OpenFlow port currently occupying
847 * 'iface''s requested port number. If there isn't one, then
848 * delete this port. Otherwise we need to consider further. */
849 victim
= iface_from_ofp_port(br
, requested_ofp_port
);
854 /* 'victim' is a port currently using 'iface''s requested port
855 * number. Unless 'victim' specifically requested that port
856 * number, too, then we can delete both 'iface' and 'victim'
857 * temporarily. (We'll add both of them back again later with new
858 * OpenFlow port numbers.)
860 * If 'victim' did request port number 'requested_ofp_port', just
861 * like 'iface', then that's a configuration inconsistency that we
862 * can't resolve. We might as well let it keep its current port
864 victim_request
= iface_get_requested_ofp_port(victim
->cfg
);
865 if (victim_request
!= requested_ofp_port
) {
866 del
= add_ofp_port(victim
->ofp_port
, del
, &n
, &allocated
);
867 iface_destroy(victim
);
876 iface_destroy(iface
);
877 del
= add_ofp_port(ofproto_port
.ofp_port
, del
, &n
, &allocated
);
879 for (i
= 0; i
< n
; i
++) {
880 ofproto_port_del(br
->ofproto
, del
[i
]);
884 /* Iterate over this module's idea of interfaces in 'br'. Remove any ports
885 * that we didn't see when we iterated through the datapath, i.e. ports
886 * that disappeared underneath use. This is an unusual situation, but it
887 * can happen in some cases:
889 * - An admin runs a command like "ovs-dpctl del-port" (which is a bad
890 * idea but could happen).
892 * - The port represented a device that disappeared, e.g. a tuntap
893 * device destroyed via "tunctl -d", a physical Ethernet device
894 * whose module was just unloaded via "rmmod", or a virtual NIC for a
895 * VM whose VM was just terminated. */
896 HMAP_FOR_EACH_SAFE (port
, port_next
, hmap_node
, &br
->ports
) {
897 struct iface
*iface
, *iface_next
;
899 LIST_FOR_EACH_SAFE (iface
, iface_next
, port_elem
, &port
->ifaces
) {
900 if (!sset_contains(&ofproto_ports
, iface
->name
)) {
901 iface_destroy__(iface
);
905 if (ovs_list_is_empty(&port
->ifaces
)) {
909 sset_destroy(&ofproto_ports
);
913 bridge_add_ports__(struct bridge
*br
, const struct shash
*wanted_ports
,
914 bool with_requested_port
)
916 struct shash_node
*port_node
;
918 SHASH_FOR_EACH (port_node
, wanted_ports
) {
919 const struct ovsrec_port
*port_cfg
= port_node
->data
;
922 for (i
= 0; i
< port_cfg
->n_interfaces
; i
++) {
923 const struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[i
];
924 ofp_port_t requested_ofp_port
;
926 requested_ofp_port
= iface_get_requested_ofp_port(iface_cfg
);
927 if ((requested_ofp_port
!= OFPP_NONE
) == with_requested_port
) {
928 struct iface
*iface
= iface_lookup(br
, iface_cfg
->name
);
931 iface_create(br
, iface_cfg
, port_cfg
);
939 bridge_add_ports(struct bridge
*br
, const struct shash
*wanted_ports
)
941 /* First add interfaces that request a particular port number. */
942 bridge_add_ports__(br
, wanted_ports
, true);
944 /* Then add interfaces that want automatic port number assignment.
945 * We add these afterward to avoid accidentally taking a specifically
946 * requested port number. */
947 bridge_add_ports__(br
, wanted_ports
, false);
951 port_configure(struct port
*port
)
953 const struct ovsrec_port
*cfg
= port
->cfg
;
954 struct bond_settings bond_settings
;
955 struct lacp_settings lacp_settings
;
956 struct ofproto_bundle_settings s
;
964 s
.slaves
= xmalloc(ovs_list_size(&port
->ifaces
) * sizeof *s
.slaves
);
965 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
966 s
.slaves
[s
.n_slaves
++] = iface
->ofp_port
;
971 if (cfg
->tag
&& *cfg
->tag
>= 0 && *cfg
->tag
<= 4095) {
975 /* Get VLAN trunks. */
978 s
.trunks
= vlan_bitmap_from_array(cfg
->trunks
, cfg
->n_trunks
);
983 s
.cvlans
= vlan_bitmap_from_array(cfg
->cvlans
, cfg
->n_cvlans
);
987 if (cfg
->vlan_mode
) {
988 if (!strcmp(cfg
->vlan_mode
, "access")) {
989 s
.vlan_mode
= PORT_VLAN_ACCESS
;
990 } else if (!strcmp(cfg
->vlan_mode
, "trunk")) {
991 s
.vlan_mode
= PORT_VLAN_TRUNK
;
992 } else if (!strcmp(cfg
->vlan_mode
, "native-tagged")) {
993 s
.vlan_mode
= PORT_VLAN_NATIVE_TAGGED
;
994 } else if (!strcmp(cfg
->vlan_mode
, "native-untagged")) {
995 s
.vlan_mode
= PORT_VLAN_NATIVE_UNTAGGED
;
996 } else if (!strcmp(cfg
->vlan_mode
, "dot1q-tunnel")) {
997 s
.vlan_mode
= PORT_VLAN_DOT1Q_TUNNEL
;
999 /* This "can't happen" because ovsdb-server should prevent it. */
1000 VLOG_WARN("port %s: unknown VLAN mode %s, falling "
1001 "back to trunk mode", port
->name
, cfg
->vlan_mode
);
1002 s
.vlan_mode
= PORT_VLAN_TRUNK
;
1006 s
.vlan_mode
= PORT_VLAN_ACCESS
;
1007 if (cfg
->n_trunks
|| cfg
->n_cvlans
) {
1008 VLOG_WARN("port %s: ignoring trunks in favor of implicit vlan",
1012 s
.vlan_mode
= PORT_VLAN_TRUNK
;
1016 const char *qe
= smap_get_def(&cfg
->other_config
, "qinq-ethtype", "");
1017 s
.qinq_ethtype
= (!strcmp(qe
, "802.1q")
1018 ? ETH_TYPE_VLAN_8021Q
1019 : ETH_TYPE_VLAN_8021AD
);
1021 s
.use_priority_tags
= smap_get_bool(&cfg
->other_config
, "priority-tags",
1024 /* Get LACP settings. */
1025 s
.lacp
= port_configure_lacp(port
, &lacp_settings
);
1029 s
.lacp_slaves
= xmalloc(s
.n_slaves
* sizeof *s
.lacp_slaves
);
1030 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1031 iface_configure_lacp(iface
, &s
.lacp_slaves
[i
++]);
1034 s
.lacp_slaves
= NULL
;
1037 /* Get bond settings. */
1038 if (s
.n_slaves
> 1) {
1039 s
.bond
= &bond_settings
;
1040 port_configure_bond(port
, &bond_settings
);
1043 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1044 netdev_set_miimon_interval(iface
->netdev
, 0);
1048 /* Protected port mode */
1049 s
.protected = cfg
->protected;
1052 ofproto_bundle_register(port
->bridge
->ofproto
, port
, &s
);
1058 free(s
.lacp_slaves
);
1061 /* Pick local port hardware address and datapath ID for 'br'. */
1063 bridge_configure_datapath_id(struct bridge
*br
)
1067 struct iface
*local_iface
;
1068 struct iface
*hw_addr_iface
;
1071 bridge_pick_local_hw_addr(br
, &ea
, &hw_addr_iface
);
1072 local_iface
= iface_from_ofp_port(br
, OFPP_LOCAL
);
1074 int error
= netdev_set_etheraddr(local_iface
->netdev
, ea
);
1076 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1077 VLOG_ERR_RL(&rl
, "bridge %s: failed to set bridge "
1078 "Ethernet address: %s",
1079 br
->name
, ovs_strerror(error
));
1084 dpid
= bridge_pick_datapath_id(br
, ea
, hw_addr_iface
);
1085 if (dpid
!= ofproto_get_datapath_id(br
->ofproto
)) {
1086 VLOG_INFO("bridge %s: using datapath ID %016"PRIx64
, br
->name
, dpid
);
1087 ofproto_set_datapath_id(br
->ofproto
, dpid
);
1090 dpid_string
= xasprintf("%016"PRIx64
, dpid
);
1091 ovsrec_bridge_set_datapath_id(br
->cfg
, dpid_string
);
1095 /* Returns a bitmap of "enum ofputil_protocol"s that are allowed for use with
1098 bridge_get_allowed_versions(struct bridge
*br
)
1100 if (!br
->cfg
->n_protocols
) {
1104 return ofputil_versions_from_strings(br
->cfg
->protocols
,
1105 br
->cfg
->n_protocols
);
1108 /* Set NetFlow configuration on 'br'. */
1110 bridge_configure_netflow(struct bridge
*br
)
1112 struct ovsrec_netflow
*cfg
= br
->cfg
->netflow
;
1113 struct netflow_options opts
;
1116 ofproto_set_netflow(br
->ofproto
, NULL
);
1120 memset(&opts
, 0, sizeof opts
);
1122 /* Get default NetFlow configuration from datapath.
1123 * Apply overrides from 'cfg'. */
1124 ofproto_get_netflow_ids(br
->ofproto
, &opts
.engine_type
, &opts
.engine_id
);
1125 if (cfg
->engine_type
) {
1126 opts
.engine_type
= *cfg
->engine_type
;
1128 if (cfg
->engine_id
) {
1129 opts
.engine_id
= *cfg
->engine_id
;
1132 /* Configure active timeout interval. */
1133 opts
.active_timeout
= cfg
->active_timeout
;
1134 if (!opts
.active_timeout
) {
1135 opts
.active_timeout
= -1;
1136 } else if (opts
.active_timeout
< 0) {
1137 VLOG_WARN("bridge %s: active timeout interval set to negative "
1138 "value, using default instead (%d seconds)", br
->name
,
1139 NF_ACTIVE_TIMEOUT_DEFAULT
);
1140 opts
.active_timeout
= -1;
1143 /* Add engine ID to interface number to disambiguate bridgs? */
1144 opts
.add_id_to_iface
= cfg
->add_id_to_interface
;
1145 if (opts
.add_id_to_iface
) {
1146 if (opts
.engine_id
> 0x7f) {
1147 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
1148 "another vswitch, choose an engine id less than 128",
1151 if (hmap_count(&br
->ports
) > 508) {
1152 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
1153 "another port when more than 508 ports are used",
1159 sset_init(&opts
.collectors
);
1160 sset_add_array(&opts
.collectors
, cfg
->targets
, cfg
->n_targets
);
1163 if (ofproto_set_netflow(br
->ofproto
, &opts
)) {
1164 VLOG_ERR("bridge %s: problem setting netflow collectors", br
->name
);
1166 sset_destroy(&opts
.collectors
);
1169 /* Set sFlow configuration on 'br'. */
1171 bridge_configure_sflow(struct bridge
*br
, int *sflow_bridge_number
)
1173 const struct ovsrec_sflow
*cfg
= br
->cfg
->sflow
;
1174 struct ovsrec_controller
**controllers
;
1175 struct ofproto_sflow_options oso
;
1176 size_t n_controllers
;
1180 ofproto_set_sflow(br
->ofproto
, NULL
);
1184 memset(&oso
, 0, sizeof oso
);
1186 sset_init(&oso
.targets
);
1187 sset_add_array(&oso
.targets
, cfg
->targets
, cfg
->n_targets
);
1189 oso
.sampling_rate
= SFL_DEFAULT_SAMPLING_RATE
;
1190 if (cfg
->sampling
) {
1191 oso
.sampling_rate
= *cfg
->sampling
;
1194 oso
.polling_interval
= SFL_DEFAULT_POLLING_INTERVAL
;
1196 oso
.polling_interval
= *cfg
->polling
;
1199 oso
.header_len
= SFL_DEFAULT_HEADER_SIZE
;
1201 oso
.header_len
= *cfg
->header
;
1204 oso
.sub_id
= (*sflow_bridge_number
)++;
1205 oso
.agent_device
= cfg
->agent
;
1207 oso
.control_ip
= NULL
;
1208 n_controllers
= bridge_get_controllers(br
, &controllers
);
1209 for (i
= 0; i
< n_controllers
; i
++) {
1210 if (controllers
[i
]->local_ip
) {
1211 oso
.control_ip
= controllers
[i
]->local_ip
;
1215 ofproto_set_sflow(br
->ofproto
, &oso
);
1217 sset_destroy(&oso
.targets
);
1220 /* Returns whether a IPFIX row is valid. */
1222 ovsrec_ipfix_is_valid(const struct ovsrec_ipfix
*ipfix
)
1224 return ipfix
&& ipfix
->n_targets
> 0;
1227 /* Returns whether a Flow_Sample_Collector_Set row is valid. */
1229 ovsrec_fscs_is_valid(const struct ovsrec_flow_sample_collector_set
*fscs
,
1230 const struct bridge
*br
)
1232 return ovsrec_ipfix_is_valid(fscs
->ipfix
) && fscs
->bridge
== br
->cfg
;
1235 /* Set IPFIX configuration on 'br'. */
1237 bridge_configure_ipfix(struct bridge
*br
)
1239 const struct ovsrec_ipfix
*be_cfg
= br
->cfg
->ipfix
;
1240 bool valid_be_cfg
= ovsrec_ipfix_is_valid(be_cfg
);
1241 const struct ovsrec_flow_sample_collector_set
*fe_cfg
;
1242 struct ofproto_ipfix_bridge_exporter_options be_opts
;
1243 struct ofproto_ipfix_flow_exporter_options
*fe_opts
= NULL
;
1244 size_t n_fe_opts
= 0;
1245 const char *virtual_obs_id
;
1247 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg
, idl
) {
1248 if (ovsrec_fscs_is_valid(fe_cfg
, br
)) {
1253 if (!valid_be_cfg
&& n_fe_opts
== 0) {
1254 ofproto_set_ipfix(br
->ofproto
, NULL
, NULL
, 0);
1259 memset(&be_opts
, 0, sizeof be_opts
);
1261 sset_init(&be_opts
.targets
);
1262 sset_add_array(&be_opts
.targets
, be_cfg
->targets
, be_cfg
->n_targets
);
1264 if (be_cfg
->sampling
) {
1265 be_opts
.sampling_rate
= *be_cfg
->sampling
;
1267 be_opts
.sampling_rate
= SFL_DEFAULT_SAMPLING_RATE
;
1269 if (be_cfg
->obs_domain_id
) {
1270 be_opts
.obs_domain_id
= *be_cfg
->obs_domain_id
;
1272 if (be_cfg
->obs_point_id
) {
1273 be_opts
.obs_point_id
= *be_cfg
->obs_point_id
;
1275 if (be_cfg
->cache_active_timeout
) {
1276 be_opts
.cache_active_timeout
= *be_cfg
->cache_active_timeout
;
1278 if (be_cfg
->cache_max_flows
) {
1279 be_opts
.cache_max_flows
= *be_cfg
->cache_max_flows
;
1282 be_opts
.enable_tunnel_sampling
= smap_get_bool(&be_cfg
->other_config
,
1283 "enable-tunnel-sampling", true);
1285 be_opts
.enable_input_sampling
= !smap_get_bool(&be_cfg
->other_config
,
1286 "enable-input-sampling", false);
1288 be_opts
.enable_output_sampling
= !smap_get_bool(&be_cfg
->other_config
,
1289 "enable-output-sampling", false);
1291 virtual_obs_id
= smap_get(&be_cfg
->other_config
, "virtual_obs_id");
1292 be_opts
.virtual_obs_id
= nullable_xstrdup(virtual_obs_id
);
1295 if (n_fe_opts
> 0) {
1296 struct ofproto_ipfix_flow_exporter_options
*opts
;
1297 fe_opts
= xcalloc(n_fe_opts
, sizeof *fe_opts
);
1299 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg
, idl
) {
1300 if (ovsrec_fscs_is_valid(fe_cfg
, br
)) {
1301 opts
->collector_set_id
= fe_cfg
->id
;
1302 sset_init(&opts
->targets
);
1303 sset_add_array(&opts
->targets
, fe_cfg
->ipfix
->targets
,
1304 fe_cfg
->ipfix
->n_targets
);
1305 opts
->cache_active_timeout
= fe_cfg
->ipfix
->cache_active_timeout
1306 ? *fe_cfg
->ipfix
->cache_active_timeout
: 0;
1307 opts
->cache_max_flows
= fe_cfg
->ipfix
->cache_max_flows
1308 ? *fe_cfg
->ipfix
->cache_max_flows
: 0;
1309 opts
->enable_tunnel_sampling
= smap_get_bool(
1310 &fe_cfg
->ipfix
->other_config
,
1311 "enable-tunnel-sampling", true);
1312 virtual_obs_id
= smap_get(&fe_cfg
->ipfix
->other_config
,
1314 opts
->virtual_obs_id
= nullable_xstrdup(virtual_obs_id
);
1320 ofproto_set_ipfix(br
->ofproto
, valid_be_cfg
? &be_opts
: NULL
, fe_opts
,
1324 sset_destroy(&be_opts
.targets
);
1325 free(be_opts
.virtual_obs_id
);
1328 if (n_fe_opts
> 0) {
1329 struct ofproto_ipfix_flow_exporter_options
*opts
= fe_opts
;
1331 for (i
= 0; i
< n_fe_opts
; i
++) {
1332 sset_destroy(&opts
->targets
);
1333 free(opts
->virtual_obs_id
);
1341 port_configure_stp(const struct ofproto
*ofproto
, struct port
*port
,
1342 struct ofproto_port_stp_settings
*port_s
,
1343 int *port_num_counter
, unsigned long *port_num_bitmap
)
1345 const char *config_str
;
1346 struct iface
*iface
;
1348 if (!smap_get_bool(&port
->cfg
->other_config
, "stp-enable", true)) {
1349 port_s
->enable
= false;
1352 port_s
->enable
= true;
1355 /* STP over bonds is not supported. */
1356 if (!ovs_list_is_singleton(&port
->ifaces
)) {
1357 VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
1359 port_s
->enable
= false;
1363 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
1365 /* Internal ports shouldn't participate in spanning tree, so
1367 if (!strcmp(iface
->type
, "internal")) {
1368 VLOG_DBG("port %s: disable STP on internal ports", port
->name
);
1369 port_s
->enable
= false;
1373 /* STP on mirror output ports is not supported. */
1374 if (ofproto_is_mirror_output_bundle(ofproto
, port
)) {
1375 VLOG_DBG("port %s: disable STP on mirror ports", port
->name
);
1376 port_s
->enable
= false;
1380 config_str
= smap_get(&port
->cfg
->other_config
, "stp-port-num");
1382 unsigned long int port_num
= strtoul(config_str
, NULL
, 0);
1383 int port_idx
= port_num
- 1;
1385 if (port_num
< 1 || port_num
> STP_MAX_PORTS
) {
1386 VLOG_ERR("port %s: invalid stp-port-num", port
->name
);
1387 port_s
->enable
= false;
1391 if (bitmap_is_set(port_num_bitmap
, port_idx
)) {
1392 VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
1393 port
->name
, port_num
);
1394 port_s
->enable
= false;
1397 bitmap_set1(port_num_bitmap
, port_idx
);
1398 port_s
->port_num
= port_idx
;
1400 if (*port_num_counter
>= STP_MAX_PORTS
) {
1401 VLOG_ERR("port %s: too many STP ports, disabling", port
->name
);
1402 port_s
->enable
= false;
1406 port_s
->port_num
= (*port_num_counter
)++;
1409 config_str
= smap_get(&port
->cfg
->other_config
, "stp-path-cost");
1411 port_s
->path_cost
= strtoul(config_str
, NULL
, 10);
1413 enum netdev_features current
;
1416 netdev_get_features(iface
->netdev
, ¤t
, NULL
, NULL
, NULL
);
1417 mbps
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 1000000;
1418 port_s
->path_cost
= stp_convert_speed_to_cost(mbps
);
1421 config_str
= smap_get(&port
->cfg
->other_config
, "stp-port-priority");
1423 port_s
->priority
= strtoul(config_str
, NULL
, 0);
1425 port_s
->priority
= STP_DEFAULT_PORT_PRIORITY
;
1430 port_configure_rstp(const struct ofproto
*ofproto
, struct port
*port
,
1431 struct ofproto_port_rstp_settings
*port_s
, int *port_num_counter
)
1433 const char *config_str
;
1434 struct iface
*iface
;
1436 if (!smap_get_bool(&port
->cfg
->other_config
, "rstp-enable", true)) {
1437 port_s
->enable
= false;
1440 port_s
->enable
= true;
1443 /* RSTP over bonds is not supported. */
1444 if (!ovs_list_is_singleton(&port
->ifaces
)) {
1445 VLOG_ERR("port %s: cannot enable RSTP on bonds, disabling",
1447 port_s
->enable
= false;
1451 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
1453 /* Internal ports shouldn't participate in spanning tree, so
1455 if (!strcmp(iface
->type
, "internal")) {
1456 VLOG_DBG("port %s: disable RSTP on internal ports", port
->name
);
1457 port_s
->enable
= false;
1461 /* RSTP on mirror output ports is not supported. */
1462 if (ofproto_is_mirror_output_bundle(ofproto
, port
)) {
1463 VLOG_DBG("port %s: disable RSTP on mirror ports", port
->name
);
1464 port_s
->enable
= false;
1468 config_str
= smap_get(&port
->cfg
->other_config
, "rstp-port-num");
1470 unsigned long int port_num
= strtoul(config_str
, NULL
, 0);
1471 if (port_num
< 1 || port_num
> RSTP_MAX_PORTS
) {
1472 VLOG_ERR("port %s: invalid rstp-port-num", port
->name
);
1473 port_s
->enable
= false;
1476 port_s
->port_num
= port_num
;
1478 if (*port_num_counter
>= RSTP_MAX_PORTS
) {
1479 VLOG_ERR("port %s: too many RSTP ports, disabling", port
->name
);
1480 port_s
->enable
= false;
1483 /* If rstp-port-num is not specified, use 0.
1484 * rstp_port_set_port_number() will look for the first free one. */
1485 port_s
->port_num
= 0;
1488 /* Increment the port num counter, because we only support
1489 * RSTP_MAX_PORTS rstp ports. */
1490 (*port_num_counter
)++;
1492 config_str
= smap_get(&port
->cfg
->other_config
, "rstp-path-cost");
1494 port_s
->path_cost
= strtoul(config_str
, NULL
, 10);
1496 enum netdev_features current
;
1499 netdev_get_features(iface
->netdev
, ¤t
, NULL
, NULL
, NULL
);
1500 mbps
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 1000000;
1501 port_s
->path_cost
= rstp_convert_speed_to_cost(mbps
);
1504 config_str
= smap_get(&port
->cfg
->other_config
, "rstp-port-priority");
1506 port_s
->priority
= strtoul(config_str
, NULL
, 0);
1508 port_s
->priority
= RSTP_DEFAULT_PORT_PRIORITY
;
1511 port_s
->admin_p2p_mac_state
= smap_get_ullong(
1512 &port
->cfg
->other_config
, "rstp-admin-p2p-mac",
1513 RSTP_ADMIN_P2P_MAC_FORCE_TRUE
);
1515 port_s
->admin_port_state
= smap_get_bool(&port
->cfg
->other_config
,
1516 "rstp-admin-port-state", true);
1518 port_s
->admin_edge_port
= smap_get_bool(&port
->cfg
->other_config
,
1519 "rstp-port-admin-edge", false);
1520 port_s
->auto_edge
= smap_get_bool(&port
->cfg
->other_config
,
1521 "rstp-port-auto-edge", true);
1522 port_s
->mcheck
= smap_get_bool(&port
->cfg
->other_config
,
1523 "rstp-port-mcheck", false);
1526 /* Set spanning tree configuration on 'br'. */
1528 bridge_configure_stp(struct bridge
*br
, bool enable_stp
)
1531 ofproto_set_stp(br
->ofproto
, NULL
);
1533 struct ofproto_stp_settings br_s
;
1534 const char *config_str
;
1536 int port_num_counter
;
1537 unsigned long *port_num_bitmap
;
1539 config_str
= smap_get(&br
->cfg
->other_config
, "stp-system-id");
1543 if (eth_addr_from_string(config_str
, &ea
)) {
1544 br_s
.system_id
= eth_addr_to_uint64(ea
);
1546 br_s
.system_id
= eth_addr_to_uint64(br
->ea
);
1547 VLOG_ERR("bridge %s: invalid stp-system-id, defaulting "
1548 "to "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(br
->ea
));
1551 br_s
.system_id
= eth_addr_to_uint64(br
->ea
);
1554 br_s
.priority
= smap_get_ullong(&br
->cfg
->other_config
, "stp-priority",
1555 STP_DEFAULT_BRIDGE_PRIORITY
);
1556 br_s
.hello_time
= smap_get_ullong(&br
->cfg
->other_config
,
1558 STP_DEFAULT_HELLO_TIME
);
1560 br_s
.max_age
= smap_get_ullong(&br
->cfg
->other_config
, "stp-max-age",
1561 STP_DEFAULT_MAX_AGE
/ 1000) * 1000;
1562 br_s
.fwd_delay
= smap_get_ullong(&br
->cfg
->other_config
,
1563 "stp-forward-delay",
1564 STP_DEFAULT_FWD_DELAY
/ 1000) * 1000;
1566 /* Configure STP on the bridge. */
1567 if (ofproto_set_stp(br
->ofproto
, &br_s
)) {
1568 VLOG_ERR("bridge %s: could not enable STP", br
->name
);
1572 /* Users must either set the port number with the "stp-port-num"
1573 * configuration on all ports or none. If manual configuration
1574 * is not done, then we allocate them sequentially. */
1575 port_num_counter
= 0;
1576 port_num_bitmap
= bitmap_allocate(STP_MAX_PORTS
);
1577 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
1578 struct ofproto_port_stp_settings port_s
;
1579 struct iface
*iface
;
1581 port_configure_stp(br
->ofproto
, port
, &port_s
,
1582 &port_num_counter
, port_num_bitmap
);
1584 /* As bonds are not supported, just apply configuration to
1585 * all interfaces. */
1586 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1587 if (ofproto_port_set_stp(br
->ofproto
, iface
->ofp_port
,
1589 VLOG_ERR("port %s: could not enable STP", port
->name
);
1595 if (bitmap_scan(port_num_bitmap
, 1, 0, STP_MAX_PORTS
) != STP_MAX_PORTS
1596 && port_num_counter
) {
1597 VLOG_ERR("bridge %s: must manually configure all STP port "
1598 "IDs or none, disabling", br
->name
);
1599 ofproto_set_stp(br
->ofproto
, NULL
);
1601 bitmap_free(port_num_bitmap
);
1606 bridge_configure_rstp(struct bridge
*br
, bool enable_rstp
)
1609 ofproto_set_rstp(br
->ofproto
, NULL
);
1611 struct ofproto_rstp_settings br_s
;
1612 const char *config_str
;
1614 int port_num_counter
;
1616 config_str
= smap_get(&br
->cfg
->other_config
, "rstp-address");
1620 if (eth_addr_from_string(config_str
, &ea
)) {
1621 br_s
.address
= eth_addr_to_uint64(ea
);
1624 br_s
.address
= eth_addr_to_uint64(br
->ea
);
1625 VLOG_ERR("bridge %s: invalid rstp-address, defaulting "
1626 "to "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(br
->ea
));
1630 br_s
.address
= eth_addr_to_uint64(br
->ea
);
1633 const struct smap
*oc
= &br
->cfg
->other_config
;
1634 br_s
.priority
= smap_get_ullong(oc
, "rstp-priority",
1635 RSTP_DEFAULT_PRIORITY
);
1636 br_s
.ageing_time
= smap_get_ullong(oc
, "rstp-ageing-time",
1637 RSTP_DEFAULT_AGEING_TIME
);
1638 br_s
.force_protocol_version
= smap_get_ullong(
1639 oc
, "rstp-force-protocol-version", FPV_DEFAULT
);
1640 br_s
.bridge_max_age
= smap_get_ullong(oc
, "rstp-max-age",
1641 RSTP_DEFAULT_BRIDGE_MAX_AGE
);
1642 br_s
.bridge_forward_delay
= smap_get_ullong(
1643 oc
, "rstp-forward-delay", RSTP_DEFAULT_BRIDGE_FORWARD_DELAY
);
1644 br_s
.transmit_hold_count
= smap_get_ullong(
1645 oc
, "rstp-transmit-hold-count", RSTP_DEFAULT_TRANSMIT_HOLD_COUNT
);
1647 /* Configure RSTP on the bridge. */
1648 if (ofproto_set_rstp(br
->ofproto
, &br_s
)) {
1649 VLOG_ERR("bridge %s: could not enable RSTP", br
->name
);
1653 port_num_counter
= 0;
1654 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
1655 struct ofproto_port_rstp_settings port_s
;
1656 struct iface
*iface
;
1658 port_configure_rstp(br
->ofproto
, port
, &port_s
,
1661 /* As bonds are not supported, just apply configuration to
1662 * all interfaces. */
1663 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1664 if (ofproto_port_set_rstp(br
->ofproto
, iface
->ofp_port
,
1666 VLOG_ERR("port %s: could not enable RSTP", port
->name
);
1675 bridge_configure_spanning_tree(struct bridge
*br
)
1677 bool enable_rstp
= br
->cfg
->rstp_enable
;
1678 bool enable_stp
= br
->cfg
->stp_enable
;
1680 if (enable_rstp
&& enable_stp
) {
1681 VLOG_WARN("%s: RSTP and STP are mutually exclusive but both are "
1682 "configured; enabling RSTP", br
->name
);
1686 bridge_configure_stp(br
, enable_stp
);
1687 bridge_configure_rstp(br
, enable_rstp
);
1691 bridge_has_bond_fake_iface(const struct bridge
*br
, const char *name
)
1693 const struct port
*port
= port_lookup(br
, name
);
1694 return port
&& port_is_bond_fake_iface(port
);
1698 port_is_bond_fake_iface(const struct port
*port
)
1700 return port
->cfg
->bond_fake_iface
&& !ovs_list_is_short(&port
->ifaces
);
1704 add_del_bridges(const struct ovsrec_open_vswitch
*cfg
)
1706 struct bridge
*br
, *next
;
1707 struct shash_node
*node
;
1708 struct shash new_br
;
1711 /* Collect new bridges' names and types. */
1712 shash_init(&new_br
);
1713 for (i
= 0; i
< cfg
->n_bridges
; i
++) {
1714 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1715 const struct ovsrec_bridge
*br_cfg
= cfg
->bridges
[i
];
1717 if (strchr(br_cfg
->name
, '/') || strchr(br_cfg
->name
, '\\')) {
1718 /* Prevent remote ovsdb-server users from accessing arbitrary
1719 * directories, e.g. consider a bridge named "../../../etc/".
1721 * Prohibiting "\" is only necessary on Windows but it's no great
1722 * loss elsewhere. */
1723 VLOG_WARN_RL(&rl
, "ignoring bridge with invalid name \"%s\"",
1725 } else if (!shash_add_once(&new_br
, br_cfg
->name
, br_cfg
)) {
1726 VLOG_WARN_RL(&rl
, "bridge %s specified twice", br_cfg
->name
);
1730 /* Get rid of deleted bridges or those whose types have changed.
1731 * Update 'cfg' of bridges that still exist. */
1732 HMAP_FOR_EACH_SAFE (br
, next
, node
, &all_bridges
) {
1733 br
->cfg
= shash_find_data(&new_br
, br
->name
);
1734 if (!br
->cfg
|| strcmp(br
->type
, ofproto_normalize_type(
1735 br
->cfg
->datapath_type
))) {
1736 bridge_destroy(br
, true);
1740 /* Add new bridges. */
1741 SHASH_FOR_EACH(node
, &new_br
) {
1742 const struct ovsrec_bridge
*br_cfg
= node
->data
;
1743 if (!bridge_lookup(br_cfg
->name
)) {
1744 bridge_create(br_cfg
);
1748 shash_destroy(&new_br
);
1751 /* Configures 'netdev' based on the "options" column in 'iface_cfg'.
1752 * Returns 0 if successful, otherwise a positive errno value. */
1754 iface_set_netdev_config(const struct ovsrec_interface
*iface_cfg
,
1755 struct netdev
*netdev
, char **errp
)
1757 return netdev_set_config(netdev
, &iface_cfg
->options
, errp
);
1760 /* Opens a network device for 'if_cfg' and configures it. Adds the network
1761 * device to br->ofproto and stores the OpenFlow port number in '*ofp_portp'.
1763 * If successful, returns 0 and stores the network device in '*netdevp'. On
1764 * failure, returns a positive errno value and stores NULL in '*netdevp'. */
1766 iface_do_create(const struct bridge
*br
,
1767 const struct ovsrec_interface
*iface_cfg
,
1768 ofp_port_t
*ofp_portp
, struct netdev
**netdevp
,
1771 struct netdev
*netdev
= NULL
;
1775 if (netdev_is_reserved_name(iface_cfg
->name
)) {
1776 VLOG_WARN("could not create interface %s, name is reserved",
1782 type
= ofproto_port_open_type(br
->cfg
->datapath_type
,
1783 iface_get_type(iface_cfg
, br
->cfg
));
1784 error
= netdev_open(iface_cfg
->name
, type
, &netdev
);
1786 VLOG_WARN_BUF(errp
, "could not open network device %s (%s)",
1787 iface_cfg
->name
, ovs_strerror(error
));
1791 error
= iface_set_netdev_config(iface_cfg
, netdev
, errp
);
1796 iface_set_netdev_mtu(iface_cfg
, netdev
);
1798 *ofp_portp
= iface_pick_ofport(iface_cfg
);
1799 error
= ofproto_port_add(br
->ofproto
, netdev
, ofp_portp
);
1801 VLOG_WARN_BUF(errp
, "could not add network device %s to ofproto (%s)",
1802 iface_cfg
->name
, ovs_strerror(error
));
1806 VLOG_INFO("bridge %s: added interface %s on port %d",
1807 br
->name
, iface_cfg
->name
, *ofp_portp
);
1814 netdev_close(netdev
);
1818 /* Creates a new iface on 'br' based on 'if_cfg'. The new iface has OpenFlow
1819 * port number 'ofp_port'. If ofp_port is OFPP_NONE, an OpenFlow port is
1820 * automatically allocated for the iface. Takes ownership of and
1821 * deallocates 'if_cfg'.
1823 * Return true if an iface is successfully created, false otherwise. */
1825 iface_create(struct bridge
*br
, const struct ovsrec_interface
*iface_cfg
,
1826 const struct ovsrec_port
*port_cfg
)
1828 struct netdev
*netdev
;
1829 struct iface
*iface
;
1830 ofp_port_t ofp_port
;
1835 /* Do the bits that can fail up front. */
1836 ovs_assert(!iface_lookup(br
, iface_cfg
->name
));
1837 error
= iface_do_create(br
, iface_cfg
, &ofp_port
, &netdev
, &errp
);
1839 iface_clear_db_record(iface_cfg
, errp
);
1844 /* Get or create the port structure. */
1845 port
= port_lookup(br
, port_cfg
->name
);
1847 port
= port_create(br
, port_cfg
);
1850 /* Create the iface structure. */
1851 iface
= xzalloc(sizeof *iface
);
1852 ovs_list_push_back(&port
->ifaces
, &iface
->port_elem
);
1853 hmap_insert(&br
->iface_by_name
, &iface
->name_node
,
1854 hash_string(iface_cfg
->name
, 0));
1856 iface
->name
= xstrdup(iface_cfg
->name
);
1857 iface
->ofp_port
= ofp_port
;
1858 iface
->netdev
= netdev
;
1859 iface
->type
= iface_get_type(iface_cfg
, br
->cfg
);
1860 iface
->netdev_type
= ofproto_port_open_type(br
->cfg
->datapath_type
,
1862 iface
->cfg
= iface_cfg
;
1863 hmap_insert(&br
->ifaces
, &iface
->ofp_port_node
,
1864 hash_ofp_port(ofp_port
));
1866 /* Populate initial status in database. */
1867 iface_refresh_stats(iface
);
1868 iface_refresh_netdev_status(iface
);
1870 /* Add bond fake iface if necessary. */
1871 if (port_is_bond_fake_iface(port
)) {
1872 struct ofproto_port ofproto_port
;
1874 if (ofproto_port_query_by_name(br
->ofproto
, port
->name
,
1876 error
= netdev_open(port
->name
, "internal", &netdev
);
1878 ofp_port_t fake_ofp_port
= OFPP_NONE
;
1879 ofproto_port_add(br
->ofproto
, netdev
, &fake_ofp_port
);
1880 netdev_close(netdev
);
1882 VLOG_WARN("could not open network device %s (%s)",
1883 port
->name
, ovs_strerror(error
));
1886 /* Already exists, nothing to do. */
1887 ofproto_port_destroy(&ofproto_port
);
1894 /* Set forward BPDU option. */
1896 bridge_configure_forward_bpdu(struct bridge
*br
)
1898 ofproto_set_forward_bpdu(br
->ofproto
,
1899 smap_get_bool(&br
->cfg
->other_config
,
1904 /* Set MAC learning table configuration for 'br'. */
1906 bridge_configure_mac_table(struct bridge
*br
)
1908 const struct smap
*oc
= &br
->cfg
->other_config
;
1909 int idle_time
= smap_get_int(oc
, "mac-aging-time", 0);
1911 idle_time
= MAC_ENTRY_DEFAULT_IDLE_TIME
;
1914 int mac_table_size
= smap_get_int(oc
, "mac-table-size", 0);
1915 if (!mac_table_size
) {
1916 mac_table_size
= MAC_DEFAULT_MAX
;
1919 ofproto_set_mac_table_config(br
->ofproto
, idle_time
, mac_table_size
);
1922 /* Set multicast snooping table configuration for 'br'. */
1924 bridge_configure_mcast_snooping(struct bridge
*br
)
1926 if (!br
->cfg
->mcast_snooping_enable
) {
1927 ofproto_set_mcast_snooping(br
->ofproto
, NULL
);
1930 struct ofproto_mcast_snooping_settings br_s
;
1932 const struct smap
*oc
= &br
->cfg
->other_config
;
1933 int idle_time
= smap_get_int(oc
, "mcast-snooping-aging-time", 0);
1934 br_s
.idle_time
= idle_time
? idle_time
: MCAST_ENTRY_DEFAULT_IDLE_TIME
;
1935 int max_entries
= smap_get_int(oc
, "mcast-snooping-table-size", 0);
1936 br_s
.max_entries
= (max_entries
1938 : MCAST_DEFAULT_MAX_ENTRIES
);
1940 br_s
.flood_unreg
= !smap_get_bool(
1941 oc
, "mcast-snooping-disable-flood-unregistered", false);
1943 /* Configure multicast snooping on the bridge */
1944 if (ofproto_set_mcast_snooping(br
->ofproto
, &br_s
)) {
1945 VLOG_ERR("bridge %s: could not enable multicast snooping",
1950 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
1951 struct ofproto_mcast_snooping_port_settings port_s
;
1952 port_s
.flood
= smap_get_bool(&port
->cfg
->other_config
,
1953 "mcast-snooping-flood", false);
1954 port_s
.flood_reports
= smap_get_bool(&port
->cfg
->other_config
,
1955 "mcast-snooping-flood-reports", false);
1956 if (ofproto_port_set_mcast_snooping(br
->ofproto
, port
, &port_s
)) {
1957 VLOG_ERR("port %s: could not configure mcast snooping",
1965 find_local_hw_addr(const struct bridge
*br
, struct eth_addr
*ea
,
1966 const struct port
*fake_br
, struct iface
**hw_addr_iface
)
1968 struct hmapx mirror_output_ports
;
1970 bool found_addr
= false;
1974 /* Mirror output ports don't participate in picking the local hardware
1975 * address. ofproto can't help us find out whether a given port is a
1976 * mirror output because we haven't configured mirrors yet, so we need to
1977 * accumulate them ourselves. */
1978 hmapx_init(&mirror_output_ports
);
1979 for (i
= 0; i
< br
->cfg
->n_mirrors
; i
++) {
1980 struct ovsrec_mirror
*m
= br
->cfg
->mirrors
[i
];
1981 if (m
->output_port
) {
1982 hmapx_add(&mirror_output_ports
, m
->output_port
);
1986 /* Otherwise choose the minimum non-local MAC address among all of the
1988 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
1989 struct eth_addr iface_ea
;
1990 struct iface
*candidate
;
1991 struct iface
*iface
;
1993 /* Mirror output ports don't participate. */
1994 if (hmapx_contains(&mirror_output_ports
, port
->cfg
)) {
1998 /* Choose the MAC address to represent the port. */
2000 if (port
->cfg
->mac
&& eth_addr_from_string(port
->cfg
->mac
,
2002 /* Find the interface with this Ethernet address (if any) so that
2003 * we can provide the correct devname to the caller. */
2004 LIST_FOR_EACH (candidate
, port_elem
, &port
->ifaces
) {
2005 struct eth_addr candidate_ea
;
2006 if (!netdev_get_etheraddr(candidate
->netdev
, &candidate_ea
)
2007 && eth_addr_equals(iface_ea
, candidate_ea
)) {
2012 /* Choose the interface whose MAC address will represent the port.
2013 * The Linux kernel bonding code always chooses the MAC address of
2014 * the first slave added to a bond, and the Fedora networking
2015 * scripts always add slaves to a bond in alphabetical order, so
2016 * for compatibility we choose the interface with the name that is
2017 * first in alphabetical order. */
2018 LIST_FOR_EACH (candidate
, port_elem
, &port
->ifaces
) {
2019 if (!iface
|| strcmp(candidate
->name
, iface
->name
) < 0) {
2024 /* A port always has at least one interface. */
2025 ovs_assert(iface
!= NULL
);
2027 /* The local port doesn't count (since we're trying to choose its
2028 * MAC address anyway). */
2029 if (iface
->ofp_port
== OFPP_LOCAL
) {
2033 /* For fake bridges we only choose from ports with the same tag */
2034 if (fake_br
&& fake_br
->cfg
&& fake_br
->cfg
->tag
) {
2035 if (!port
->cfg
->tag
) {
2038 if (*port
->cfg
->tag
!= *fake_br
->cfg
->tag
) {
2044 error
= netdev_get_etheraddr(iface
->netdev
, &iface_ea
);
2050 /* Compare against our current choice. */
2051 if (!eth_addr_is_multicast(iface_ea
) &&
2052 !eth_addr_is_local(iface_ea
) &&
2053 !eth_addr_is_reserved(iface_ea
) &&
2054 !eth_addr_is_zero(iface_ea
) &&
2055 (!found_addr
|| eth_addr_compare_3way(iface_ea
, *ea
) < 0))
2058 *hw_addr_iface
= iface
;
2064 *ea
= br
->default_ea
;
2065 *hw_addr_iface
= NULL
;
2068 hmapx_destroy(&mirror_output_ports
);
2072 bridge_pick_local_hw_addr(struct bridge
*br
, struct eth_addr
*ea
,
2073 struct iface
**hw_addr_iface
)
2075 *hw_addr_iface
= NULL
;
2077 /* Did the user request a particular MAC? */
2078 const char *hwaddr
= smap_get_def(&br
->cfg
->other_config
, "hwaddr", "");
2079 if (eth_addr_from_string(hwaddr
, ea
)) {
2080 if (eth_addr_is_multicast(*ea
)) {
2081 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
2082 "address "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(*ea
));
2083 } else if (eth_addr_is_zero(*ea
)) {
2084 VLOG_ERR("bridge %s: cannot set MAC address to zero", br
->name
);
2090 /* Find a local hw address */
2091 find_local_hw_addr(br
, ea
, NULL
, hw_addr_iface
);
2094 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
2095 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
2096 * an interface on 'br', then that interface must be passed in as
2097 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
2098 * 'hw_addr_iface' must be passed in as a null pointer. */
2100 bridge_pick_datapath_id(struct bridge
*br
,
2101 const struct eth_addr bridge_ea
,
2102 struct iface
*hw_addr_iface
)
2105 * The procedure for choosing a bridge MAC address will, in the most
2106 * ordinary case, also choose a unique MAC that we can use as a datapath
2107 * ID. In some special cases, though, multiple bridges will end up with
2108 * the same MAC address. This is OK for the bridges, but it will confuse
2109 * the OpenFlow controller, because each datapath needs a unique datapath
2112 * Datapath IDs must be unique. It is also very desirable that they be
2113 * stable from one run to the next, so that policy set on a datapath
2116 const char *datapath_id
;
2119 datapath_id
= smap_get_def(&br
->cfg
->other_config
, "datapath-id", "");
2120 if (dpid_from_string(datapath_id
, &dpid
)) {
2124 if (!hw_addr_iface
) {
2126 * A purely internal bridge, that is, one that has no non-virtual
2127 * network devices on it at all, is difficult because it has no
2128 * natural unique identifier at all.
2130 * When the host is a XenServer, we handle this case by hashing the
2131 * host's UUID with the name of the bridge. Names of bridges are
2132 * persistent across XenServer reboots, although they can be reused if
2133 * an internal network is destroyed and then a new one is later
2134 * created, so this is fairly effective.
2136 * When the host is not a XenServer, we punt by using a random MAC
2137 * address on each run.
2139 const char *host_uuid
= xenserver_get_host_uuid();
2141 char *combined
= xasprintf("%s,%s", host_uuid
, br
->name
);
2142 dpid
= dpid_from_hash(combined
, strlen(combined
));
2148 return eth_addr_to_uint64(bridge_ea
);
2152 dpid_from_hash(const void *data
, size_t n
)
2155 uint8_t bytes
[SHA1_DIGEST_SIZE
];
2159 sha1_bytes(data
, n
, hash
.bytes
);
2160 eth_addr_mark_random(&hash
.ea
);
2161 return eth_addr_to_uint64(hash
.ea
);
2165 iface_refresh_netdev_status(struct iface
*iface
)
2169 enum netdev_features current
;
2170 enum netdev_flags flags
;
2171 const char *link_state
;
2172 struct eth_addr mac
;
2173 int64_t bps
, mtu_64
, ifindex64
, link_resets
;
2176 if (iface_is_synthetic(iface
)) {
2180 if (iface
->change_seq
== netdev_get_change_seq(iface
->netdev
)
2181 && !status_txn_try_again
) {
2185 iface
->change_seq
= netdev_get_change_seq(iface
->netdev
);
2189 if (!netdev_get_status(iface
->netdev
, &smap
)) {
2190 ovsrec_interface_set_status(iface
->cfg
, &smap
);
2192 ovsrec_interface_set_status(iface
->cfg
, NULL
);
2195 smap_destroy(&smap
);
2197 error
= netdev_get_flags(iface
->netdev
, &flags
);
2199 const char *state
= flags
& NETDEV_UP
? "up" : "down";
2201 ovsrec_interface_set_admin_state(iface
->cfg
, state
);
2203 ovsrec_interface_set_admin_state(iface
->cfg
, NULL
);
2206 link_state
= netdev_get_carrier(iface
->netdev
) ? "up" : "down";
2207 ovsrec_interface_set_link_state(iface
->cfg
, link_state
);
2209 link_resets
= netdev_get_carrier_resets(iface
->netdev
);
2210 ovsrec_interface_set_link_resets(iface
->cfg
, &link_resets
, 1);
2212 error
= netdev_get_features(iface
->netdev
, ¤t
, NULL
, NULL
, NULL
);
2213 bps
= !error
? netdev_features_to_bps(current
, 0) : 0;
2215 ovsrec_interface_set_duplex(iface
->cfg
,
2216 netdev_features_is_full_duplex(current
)
2218 ovsrec_interface_set_link_speed(iface
->cfg
, &bps
, 1);
2220 ovsrec_interface_set_duplex(iface
->cfg
, NULL
);
2221 ovsrec_interface_set_link_speed(iface
->cfg
, NULL
, 0);
2224 error
= netdev_get_mtu(iface
->netdev
, &mtu
);
2227 ovsrec_interface_set_mtu(iface
->cfg
, &mtu_64
, 1);
2229 ovsrec_interface_set_mtu(iface
->cfg
, NULL
, 0);
2232 error
= netdev_get_etheraddr(iface
->netdev
, &mac
);
2234 char mac_string
[ETH_ADDR_STRLEN
+ 1];
2236 snprintf(mac_string
, sizeof mac_string
,
2237 ETH_ADDR_FMT
, ETH_ADDR_ARGS(mac
));
2238 ovsrec_interface_set_mac_in_use(iface
->cfg
, mac_string
);
2240 ovsrec_interface_set_mac_in_use(iface
->cfg
, NULL
);
2243 /* The netdev may return a negative number (such as -EOPNOTSUPP)
2244 * if there is no valid ifindex number. */
2245 ifindex64
= netdev_get_ifindex(iface
->netdev
);
2246 if (ifindex64
< 0) {
2249 ovsrec_interface_set_ifindex(iface
->cfg
, &ifindex64
, 1);
2253 iface_refresh_ofproto_status(struct iface
*iface
)
2257 if (iface_is_synthetic(iface
)) {
2261 current
= ofproto_port_is_lacp_current(iface
->port
->bridge
->ofproto
,
2265 ovsrec_interface_set_lacp_current(iface
->cfg
, &bl
, 1);
2267 ovsrec_interface_set_lacp_current(iface
->cfg
, NULL
, 0);
2270 if (ofproto_port_cfm_status_changed(iface
->port
->bridge
->ofproto
,
2272 || status_txn_try_again
) {
2273 iface_refresh_cfm_stats(iface
);
2276 if (ofproto_port_bfd_status_changed(iface
->port
->bridge
->ofproto
,
2278 || status_txn_try_again
) {
2282 ofproto_port_get_bfd_status(iface
->port
->bridge
->ofproto
,
2283 iface
->ofp_port
, &smap
);
2284 ovsrec_interface_set_bfd_status(iface
->cfg
, &smap
);
2285 smap_destroy(&smap
);
2289 /* Writes 'iface''s CFM statistics to the database. 'iface' must not be
2292 iface_refresh_cfm_stats(struct iface
*iface
)
2294 const struct ovsrec_interface
*cfg
= iface
->cfg
;
2295 struct cfm_status status
;
2298 error
= ofproto_port_get_cfm_status(iface
->port
->bridge
->ofproto
,
2299 iface
->ofp_port
, &status
);
2301 ovsrec_interface_set_cfm_fault(cfg
, NULL
, 0);
2302 ovsrec_interface_set_cfm_fault_status(cfg
, NULL
, 0);
2303 ovsrec_interface_set_cfm_remote_opstate(cfg
, NULL
);
2304 ovsrec_interface_set_cfm_flap_count(cfg
, NULL
, 0);
2305 ovsrec_interface_set_cfm_health(cfg
, NULL
, 0);
2306 ovsrec_interface_set_cfm_remote_mpids(cfg
, NULL
, 0);
2308 const char *reasons
[CFM_FAULT_N_REASONS
];
2309 int64_t cfm_health
= status
.health
;
2310 int64_t cfm_flap_count
= status
.flap_count
;
2311 bool faulted
= status
.faults
!= 0;
2314 ovsrec_interface_set_cfm_fault(cfg
, &faulted
, 1);
2317 for (i
= 0; i
< CFM_FAULT_N_REASONS
; i
++) {
2318 int reason
= 1 << i
;
2319 if (status
.faults
& reason
) {
2320 reasons
[j
++] = cfm_fault_reason_to_str(reason
);
2323 ovsrec_interface_set_cfm_fault_status(cfg
, reasons
, j
);
2325 ovsrec_interface_set_cfm_flap_count(cfg
, &cfm_flap_count
, 1);
2327 if (status
.remote_opstate
>= 0) {
2328 const char *remote_opstate
= status
.remote_opstate
? "up" : "down";
2329 ovsrec_interface_set_cfm_remote_opstate(cfg
, remote_opstate
);
2331 ovsrec_interface_set_cfm_remote_opstate(cfg
, NULL
);
2334 ovsrec_interface_set_cfm_remote_mpids(cfg
,
2335 (const int64_t *)status
.rmps
,
2337 if (cfm_health
>= 0) {
2338 ovsrec_interface_set_cfm_health(cfg
, &cfm_health
, 1);
2340 ovsrec_interface_set_cfm_health(cfg
, NULL
, 0);
2348 iface_refresh_stats(struct iface
*iface
)
2350 #define IFACE_STATS \
2351 IFACE_STAT(rx_packets, "rx_packets") \
2352 IFACE_STAT(tx_packets, "tx_packets") \
2353 IFACE_STAT(rx_bytes, "rx_bytes") \
2354 IFACE_STAT(tx_bytes, "tx_bytes") \
2355 IFACE_STAT(rx_dropped, "rx_dropped") \
2356 IFACE_STAT(tx_dropped, "tx_dropped") \
2357 IFACE_STAT(rx_errors, "rx_errors") \
2358 IFACE_STAT(tx_errors, "tx_errors") \
2359 IFACE_STAT(rx_frame_errors, "rx_frame_err") \
2360 IFACE_STAT(rx_over_errors, "rx_over_err") \
2361 IFACE_STAT(rx_crc_errors, "rx_crc_err") \
2362 IFACE_STAT(collisions, "collisions") \
2363 IFACE_STAT(rx_1_to_64_packets, "rx_1_to_64_packets") \
2364 IFACE_STAT(rx_65_to_127_packets, "rx_65_to_127_packets") \
2365 IFACE_STAT(rx_128_to_255_packets, "rx_128_to_255_packets") \
2366 IFACE_STAT(rx_256_to_511_packets, "rx_256_to_511_packets") \
2367 IFACE_STAT(rx_512_to_1023_packets, "rx_512_to_1023_packets") \
2368 IFACE_STAT(rx_1024_to_1522_packets, "rx_1024_to_1522_packets") \
2369 IFACE_STAT(rx_1523_to_max_packets, "rx_1523_to_max_packets") \
2370 IFACE_STAT(tx_1_to_64_packets, "tx_1_to_64_packets") \
2371 IFACE_STAT(tx_65_to_127_packets, "tx_65_to_127_packets") \
2372 IFACE_STAT(tx_128_to_255_packets, "tx_128_to_255_packets") \
2373 IFACE_STAT(tx_256_to_511_packets, "tx_256_to_511_packets") \
2374 IFACE_STAT(tx_512_to_1023_packets, "tx_512_to_1023_packets") \
2375 IFACE_STAT(tx_1024_to_1522_packets, "tx_1024_to_1522_packets") \
2376 IFACE_STAT(tx_1523_to_max_packets, "tx_1523_to_max_packets") \
2377 IFACE_STAT(tx_multicast_packets, "tx_multicast_packets") \
2378 IFACE_STAT(rx_broadcast_packets, "rx_broadcast_packets") \
2379 IFACE_STAT(tx_broadcast_packets, "tx_broadcast_packets") \
2380 IFACE_STAT(rx_undersized_errors, "rx_undersized_errors") \
2381 IFACE_STAT(rx_oversize_errors, "rx_oversize_errors") \
2382 IFACE_STAT(rx_fragmented_errors, "rx_fragmented_errors") \
2383 IFACE_STAT(rx_jabber_errors, "rx_jabber_errors")
2385 #define IFACE_STAT(MEMBER, NAME) + 1
2386 enum { N_IFACE_STATS
= IFACE_STATS
};
2388 int64_t values
[N_IFACE_STATS
];
2389 const char *keys
[N_IFACE_STATS
];
2392 struct netdev_stats stats
;
2394 if (iface_is_synthetic(iface
)) {
2398 /* Intentionally ignore return value, since errors will set 'stats' to
2399 * all-1s, and we will deal with that correctly below. */
2400 netdev_get_stats(iface
->netdev
, &stats
);
2402 /* Copy statistics into keys[] and values[]. */
2404 #define IFACE_STAT(MEMBER, NAME) \
2405 if (stats.MEMBER != UINT64_MAX) { \
2407 values[n] = stats.MEMBER; \
2412 ovs_assert(n
<= N_IFACE_STATS
);
2414 ovsrec_interface_set_statistics(iface
->cfg
, keys
, values
, n
);
2419 br_refresh_datapath_info(struct bridge
*br
)
2421 const char *version
;
2423 version
= (br
->ofproto
&& br
->ofproto
->ofproto_class
->get_datapath_version
2424 ? br
->ofproto
->ofproto_class
->get_datapath_version(br
->ofproto
)
2427 ovsrec_bridge_set_datapath_version(br
->cfg
,
2428 version
? version
: "<unknown>");
2432 br_refresh_stp_status(struct bridge
*br
)
2434 struct smap smap
= SMAP_INITIALIZER(&smap
);
2435 struct ofproto
*ofproto
= br
->ofproto
;
2436 struct ofproto_stp_status status
;
2438 if (ofproto_get_stp_status(ofproto
, &status
)) {
2442 if (!status
.enabled
) {
2443 ovsrec_bridge_set_status(br
->cfg
, NULL
);
2447 smap_add_format(&smap
, "stp_bridge_id", STP_ID_FMT
,
2448 STP_ID_ARGS(status
.bridge_id
));
2449 smap_add_format(&smap
, "stp_designated_root", STP_ID_FMT
,
2450 STP_ID_ARGS(status
.designated_root
));
2451 smap_add_format(&smap
, "stp_root_path_cost", "%d", status
.root_path_cost
);
2453 ovsrec_bridge_set_status(br
->cfg
, &smap
);
2454 smap_destroy(&smap
);
2458 port_refresh_stp_status(struct port
*port
)
2460 struct ofproto
*ofproto
= port
->bridge
->ofproto
;
2461 struct iface
*iface
;
2462 struct ofproto_port_stp_status status
;
2465 if (port_is_synthetic(port
)) {
2469 /* STP doesn't currently support bonds. */
2470 if (!ovs_list_is_singleton(&port
->ifaces
)) {
2471 ovsrec_port_set_status(port
->cfg
, NULL
);
2475 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
2476 if (ofproto_port_get_stp_status(ofproto
, iface
->ofp_port
, &status
)) {
2480 if (!status
.enabled
) {
2481 ovsrec_port_set_status(port
->cfg
, NULL
);
2485 /* Set Status column. */
2487 smap_add_format(&smap
, "stp_port_id", "%d", status
.port_id
);
2488 smap_add(&smap
, "stp_state", stp_state_name(status
.state
));
2489 smap_add_format(&smap
, "stp_sec_in_state", "%u", status
.sec_in_state
);
2490 smap_add(&smap
, "stp_role", stp_role_name(status
.role
));
2491 ovsrec_port_set_status(port
->cfg
, &smap
);
2492 smap_destroy(&smap
);
2496 port_refresh_stp_stats(struct port
*port
)
2498 struct ofproto
*ofproto
= port
->bridge
->ofproto
;
2499 struct iface
*iface
;
2500 struct ofproto_port_stp_stats stats
;
2501 const char *keys
[3];
2502 int64_t int_values
[3];
2504 if (port_is_synthetic(port
)) {
2508 /* STP doesn't currently support bonds. */
2509 if (!ovs_list_is_singleton(&port
->ifaces
)) {
2513 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
2514 if (ofproto_port_get_stp_stats(ofproto
, iface
->ofp_port
, &stats
)) {
2518 if (!stats
.enabled
) {
2519 ovsrec_port_set_statistics(port
->cfg
, NULL
, NULL
, 0);
2523 /* Set Statistics column. */
2524 keys
[0] = "stp_tx_count";
2525 int_values
[0] = stats
.tx_count
;
2526 keys
[1] = "stp_rx_count";
2527 int_values
[1] = stats
.rx_count
;
2528 keys
[2] = "stp_error_count";
2529 int_values
[2] = stats
.error_count
;
2531 ovsrec_port_set_statistics(port
->cfg
, keys
, int_values
,
2532 ARRAY_SIZE(int_values
));
2536 br_refresh_rstp_status(struct bridge
*br
)
2538 struct smap smap
= SMAP_INITIALIZER(&smap
);
2539 struct ofproto
*ofproto
= br
->ofproto
;
2540 struct ofproto_rstp_status status
;
2542 if (ofproto_get_rstp_status(ofproto
, &status
)) {
2545 if (!status
.enabled
) {
2546 ovsrec_bridge_set_rstp_status(br
->cfg
, NULL
);
2549 smap_add_format(&smap
, "rstp_bridge_id", RSTP_ID_FMT
,
2550 RSTP_ID_ARGS(status
.bridge_id
));
2551 smap_add_format(&smap
, "rstp_root_path_cost", "%"PRIu32
,
2552 status
.root_path_cost
);
2553 smap_add_format(&smap
, "rstp_root_id", RSTP_ID_FMT
,
2554 RSTP_ID_ARGS(status
.root_id
));
2555 smap_add_format(&smap
, "rstp_designated_id", RSTP_ID_FMT
,
2556 RSTP_ID_ARGS(status
.designated_id
));
2557 smap_add_format(&smap
, "rstp_designated_port_id", RSTP_PORT_ID_FMT
,
2558 status
.designated_port_id
);
2559 smap_add_format(&smap
, "rstp_bridge_port_id", RSTP_PORT_ID_FMT
,
2560 status
.bridge_port_id
);
2561 ovsrec_bridge_set_rstp_status(br
->cfg
, &smap
);
2562 smap_destroy(&smap
);
2566 port_refresh_rstp_status(struct port
*port
)
2568 struct ofproto
*ofproto
= port
->bridge
->ofproto
;
2569 struct iface
*iface
;
2570 struct ofproto_port_rstp_status status
;
2571 const char *keys
[4];
2572 int64_t int_values
[4];
2575 if (port_is_synthetic(port
)) {
2579 /* RSTP doesn't currently support bonds. */
2580 if (!ovs_list_is_singleton(&port
->ifaces
)) {
2581 ovsrec_port_set_rstp_status(port
->cfg
, NULL
);
2585 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
2586 if (ofproto_port_get_rstp_status(ofproto
, iface
->ofp_port
, &status
)) {
2590 if (!status
.enabled
) {
2591 ovsrec_port_set_rstp_status(port
->cfg
, NULL
);
2592 ovsrec_port_set_rstp_statistics(port
->cfg
, NULL
, NULL
, 0);
2595 /* Set Status column. */
2598 smap_add_format(&smap
, "rstp_port_id", RSTP_PORT_ID_FMT
,
2600 smap_add_format(&smap
, "rstp_port_role", "%s",
2601 rstp_port_role_name(status
.role
));
2602 smap_add_format(&smap
, "rstp_port_state", "%s",
2603 rstp_state_name(status
.state
));
2604 smap_add_format(&smap
, "rstp_designated_bridge_id", RSTP_ID_FMT
,
2605 RSTP_ID_ARGS(status
.designated_bridge_id
));
2606 smap_add_format(&smap
, "rstp_designated_port_id", RSTP_PORT_ID_FMT
,
2607 status
.designated_port_id
);
2608 smap_add_format(&smap
, "rstp_designated_path_cost", "%"PRIu32
,
2609 status
.designated_path_cost
);
2611 ovsrec_port_set_rstp_status(port
->cfg
, &smap
);
2612 smap_destroy(&smap
);
2614 /* Set Statistics column. */
2615 keys
[0] = "rstp_tx_count";
2616 int_values
[0] = status
.tx_count
;
2617 keys
[1] = "rstp_rx_count";
2618 int_values
[1] = status
.rx_count
;
2619 keys
[2] = "rstp_uptime";
2620 int_values
[2] = status
.uptime
;
2621 keys
[3] = "rstp_error_count";
2622 int_values
[3] = status
.error_count
;
2623 ovsrec_port_set_rstp_statistics(port
->cfg
, keys
, int_values
,
2624 ARRAY_SIZE(int_values
));
2628 port_refresh_bond_status(struct port
*port
, bool force_update
)
2630 struct eth_addr mac
;
2632 /* Return if port is not a bond */
2633 if (ovs_list_is_singleton(&port
->ifaces
)) {
2637 if (bond_get_changed_active_slave(port
->name
, &mac
, force_update
)) {
2641 ds_put_format(&mac_s
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(mac
));
2642 ovsrec_port_set_bond_active_slave(port
->cfg
, ds_cstr(&mac_s
));
2648 enable_system_stats(const struct ovsrec_open_vswitch
*cfg
)
2650 return smap_get_bool(&cfg
->other_config
, "enable-statistics", false);
2654 reconfigure_system_stats(const struct ovsrec_open_vswitch
*cfg
)
2656 bool enable
= enable_system_stats(cfg
);
2658 system_stats_enable(enable
);
2660 ovsrec_open_vswitch_set_statistics(cfg
, NULL
);
2665 run_system_stats(void)
2667 const struct ovsrec_open_vswitch
*cfg
= ovsrec_open_vswitch_first(idl
);
2670 stats
= system_stats_run();
2672 struct ovsdb_idl_txn
*txn
;
2673 struct ovsdb_datum datum
;
2675 txn
= ovsdb_idl_txn_create(idl
);
2676 ovsdb_datum_from_smap(&datum
, stats
);
2677 smap_destroy(stats
);
2678 ovsdb_idl_txn_write(&cfg
->header_
, &ovsrec_open_vswitch_col_statistics
,
2680 ovsdb_idl_txn_commit(txn
);
2681 ovsdb_idl_txn_destroy(txn
);
2688 ofp12_controller_role_to_str(enum ofp12_controller_role role
)
2691 case OFPCR12_ROLE_EQUAL
:
2693 case OFPCR12_ROLE_MASTER
:
2695 case OFPCR12_ROLE_SLAVE
:
2697 case OFPCR12_ROLE_NOCHANGE
:
2699 return "*** INVALID ROLE ***";
2704 refresh_controller_status(void)
2708 /* Accumulate status for controllers on all bridges. */
2709 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
2710 struct shash info
= SHASH_INITIALIZER(&info
);
2711 ofproto_get_ofproto_controller_info(br
->ofproto
, &info
);
2713 /* Update each controller of the bridge in the database with
2714 * current status. */
2715 struct ovsrec_controller
**controllers
;
2716 size_t n_controllers
= bridge_get_controllers(br
, &controllers
);
2718 for (i
= 0; i
< n_controllers
; i
++) {
2719 struct ovsrec_controller
*cfg
= controllers
[i
];
2720 struct ofproto_controller_info
*cinfo
=
2721 shash_find_data(&info
, cfg
->target
);
2723 /* cinfo is NULL when 'cfg->target' is a passive connection. */
2725 ovsrec_controller_set_is_connected(cfg
, cinfo
->is_connected
);
2726 const char *role
= ofp12_controller_role_to_str(cinfo
->role
);
2727 ovsrec_controller_set_role(cfg
, role
);
2728 ovsrec_controller_set_status(cfg
, &cinfo
->pairs
);
2730 ovsrec_controller_set_is_connected(cfg
, false);
2731 ovsrec_controller_set_role(cfg
, NULL
);
2732 ovsrec_controller_set_status(cfg
, NULL
);
2736 ofproto_free_ofproto_controller_info(&info
);
2740 /* Update interface and mirror statistics if necessary. */
2742 run_stats_update(void)
2744 const struct ovsrec_open_vswitch
*cfg
= ovsrec_open_vswitch_first(idl
);
2751 /* Statistics update interval should always be greater than or equal to
2753 stats_interval
= MAX(smap_get_int(&cfg
->other_config
,
2754 "stats-update-interval",
2756 if (stats_timer_interval
!= stats_interval
) {
2757 stats_timer_interval
= stats_interval
;
2758 stats_timer
= LLONG_MIN
;
2761 if (time_msec() >= stats_timer
) {
2762 enum ovsdb_idl_txn_status status
;
2764 /* Rate limit the update. Do not start a new update if the
2765 * previous one is not done. */
2769 stats_txn
= ovsdb_idl_txn_create(idl
);
2770 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
2774 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
2775 struct iface
*iface
;
2777 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
2778 iface_refresh_stats(iface
);
2780 port_refresh_stp_stats(port
);
2782 HMAP_FOR_EACH (m
, hmap_node
, &br
->mirrors
) {
2783 mirror_refresh_stats(m
);
2786 refresh_controller_status();
2789 status
= ovsdb_idl_txn_commit(stats_txn
);
2790 if (status
!= TXN_INCOMPLETE
) {
2791 stats_timer
= time_msec() + stats_timer_interval
;
2792 ovsdb_idl_txn_destroy(stats_txn
);
2799 stats_update_wait(void)
2801 /* If the 'stats_txn' is non-null (transaction incomplete), waits for the
2802 * transaction to complete. Otherwise, waits for the 'stats_timer'. */
2804 ovsdb_idl_txn_wait(stats_txn
);
2806 poll_timer_wait_until(stats_timer
);
2810 /* Update bridge/port/interface status if necessary. */
2812 run_status_update(void)
2817 /* Rate limit the update. Do not start a new update if the
2818 * previous one is not done. */
2819 seq
= seq_read(connectivity_seq_get());
2820 if (seq
!= connectivity_seqno
|| status_txn_try_again
) {
2823 connectivity_seqno
= seq
;
2824 status_txn
= ovsdb_idl_txn_create(idl
);
2825 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
2828 br_refresh_stp_status(br
);
2829 br_refresh_rstp_status(br
);
2830 br_refresh_datapath_info(br
);
2831 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
2832 struct iface
*iface
;
2834 port_refresh_stp_status(port
);
2835 port_refresh_rstp_status(port
);
2836 port_refresh_bond_status(port
, status_txn_try_again
);
2837 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
2838 iface_refresh_netdev_status(iface
);
2839 iface_refresh_ofproto_status(iface
);
2846 /* Commit the transaction and get the status. If the transaction finishes,
2847 * then destroy the transaction. Otherwise, keep it so that we can check
2848 * progress the next time that this function is called. */
2850 enum ovsdb_idl_txn_status status
;
2852 status
= ovsdb_idl_txn_commit(status_txn
);
2853 if (status
!= TXN_INCOMPLETE
) {
2854 ovsdb_idl_txn_destroy(status_txn
);
2857 /* Sets the 'status_txn_try_again' if the transaction fails. */
2858 if (status
== TXN_SUCCESS
|| status
== TXN_UNCHANGED
) {
2859 status_txn_try_again
= false;
2861 status_txn_try_again
= true;
2866 /* Refresh AA port status if necessary. */
2867 if (time_msec() >= aa_refresh_timer
) {
2870 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
2871 if (bridge_aa_need_refresh(br
)) {
2872 struct ovsdb_idl_txn
*txn
;
2874 txn
= ovsdb_idl_txn_create(idl
);
2875 bridge_aa_refresh_queued(br
);
2876 ovsdb_idl_txn_commit(txn
);
2877 ovsdb_idl_txn_destroy(txn
);
2881 aa_refresh_timer
= time_msec() + AA_REFRESH_INTERVAL
;
2886 status_update_wait(void)
2888 /* If the 'status_txn' is non-null (transaction incomplete), waits for the
2889 * transaction to complete. If the status update to database needs to be
2890 * run again (transaction fails), registers a timeout in
2891 * 'STATUS_CHECK_AGAIN_MSEC'. Otherwise, waits on the global connectivity
2892 * sequence number. */
2894 ovsdb_idl_txn_wait(status_txn
);
2895 } else if (status_txn_try_again
) {
2896 poll_timer_wait_until(time_msec() + STATUS_CHECK_AGAIN_MSEC
);
2898 seq_wait(connectivity_seq_get(), connectivity_seqno
);
2909 /* Let each datapath type do the work that it needs to do. */
2911 ofproto_enumerate_types(&types
);
2912 SSET_FOR_EACH (type
, &types
) {
2913 ofproto_type_run(type
);
2915 sset_destroy(&types
);
2917 /* Let each bridge do the work that it needs to do. */
2918 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
2919 ofproto_run(br
->ofproto
);
2926 static struct ovsrec_open_vswitch null_cfg
;
2927 const struct ovsrec_open_vswitch
*cfg
;
2929 ovsrec_open_vswitch_init(&null_cfg
);
2935 if (ovsdb_idl_is_lock_contended(idl
)) {
2936 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
2937 struct bridge
*br
, *next_br
;
2939 VLOG_ERR_RL(&rl
, "another ovs-vswitchd process is running, "
2940 "disabling this process (pid %ld) until it goes away",
2941 (long int) getpid());
2943 HMAP_FOR_EACH_SAFE (br
, next_br
, node
, &all_bridges
) {
2944 bridge_destroy(br
, false);
2946 /* Since we will not be running system_stats_run() in this process
2947 * with the current situation of multiple ovs-vswitchd daemons,
2948 * disable system stats collection. */
2949 system_stats_enable(false);
2951 } else if (!ovsdb_idl_has_lock(idl
)
2952 || !ovsdb_idl_has_ever_connected(idl
)) {
2953 /* Returns if not holding the lock or not done retrieving db
2957 cfg
= ovsrec_open_vswitch_first(idl
);
2960 netdev_set_flow_api_enabled(&cfg
->other_config
);
2961 dpdk_init(&cfg
->other_config
);
2964 /* Initialize the ofproto library. This only needs to run once, but
2965 * it must be done after the configuration is set. If the
2966 * initialization has already occurred, bridge_init_ofproto()
2967 * returns immediately. */
2968 bridge_init_ofproto(cfg
);
2970 /* Once the value of flow-restore-wait is false, we no longer should
2971 * check its value from the database. */
2972 if (cfg
&& ofproto_get_flow_restore_wait()) {
2973 ofproto_set_flow_restore_wait(smap_get_bool(&cfg
->other_config
,
2974 "flow-restore-wait", false));
2979 /* Re-configure SSL. We do this on every trip through the main loop,
2980 * instead of just when the database changes, because the contents of the
2981 * key and certificate files can change without the database changing.
2983 * We do this before bridge_reconfigure() because that function might
2984 * initiate SSL connections and thus requires SSL to be configured. */
2985 if (cfg
&& cfg
->ssl
) {
2986 const struct ovsrec_ssl
*ssl
= cfg
->ssl
;
2988 stream_ssl_set_key_and_cert(ssl
->private_key
, ssl
->certificate
);
2989 stream_ssl_set_ca_cert_file(ssl
->ca_cert
, ssl
->bootstrap_ca_cert
);
2992 if (ovsdb_idl_get_seqno(idl
) != idl_seqno
||
2993 if_notifier_changed(ifnotifier
)) {
2994 struct ovsdb_idl_txn
*txn
;
2996 idl_seqno
= ovsdb_idl_get_seqno(idl
);
2997 txn
= ovsdb_idl_txn_create(idl
);
2998 bridge_reconfigure(cfg
? cfg
: &null_cfg
);
3001 ovsrec_open_vswitch_set_cur_cfg(cfg
, cfg
->next_cfg
);
3002 discover_types(cfg
);
3005 /* If we are completing our initial configuration for this run
3006 * of ovs-vswitchd, then keep the transaction around to monitor
3007 * it for completion. */
3008 if (initial_config_done
) {
3009 /* Always sets the 'status_txn_try_again' to check again,
3010 * in case that this transaction fails. */
3011 status_txn_try_again
= true;
3012 ovsdb_idl_txn_commit(txn
);
3013 ovsdb_idl_txn_destroy(txn
);
3015 initial_config_done
= true;
3016 daemonize_txn
= txn
;
3020 if (daemonize_txn
) {
3021 enum ovsdb_idl_txn_status status
= ovsdb_idl_txn_commit(daemonize_txn
);
3022 if (status
!= TXN_INCOMPLETE
) {
3023 ovsdb_idl_txn_destroy(daemonize_txn
);
3024 daemonize_txn
= NULL
;
3026 /* ovs-vswitchd has completed initialization, so allow the
3027 * process that forked us to exit successfully. */
3028 daemonize_complete();
3030 vlog_enable_async();
3032 VLOG_INFO_ONCE("%s (Open vSwitch) %s", program_name
, VERSION
);
3037 run_status_update();
3047 ovsdb_idl_wait(idl
);
3048 if (daemonize_txn
) {
3049 ovsdb_idl_txn_wait(daemonize_txn
);
3055 ofproto_enumerate_types(&types
);
3056 SSET_FOR_EACH (type
, &types
) {
3057 ofproto_type_wait(type
);
3059 sset_destroy(&types
);
3061 if (!hmap_is_empty(&all_bridges
)) {
3064 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3065 ofproto_wait(br
->ofproto
);
3067 stats_update_wait();
3068 status_update_wait();
3071 system_stats_wait();
3074 /* Adds some memory usage statistics for bridges into 'usage', for use with
3075 * memory_report(). */
3077 bridge_get_memory_usage(struct simap
*usage
)
3084 ofproto_enumerate_types(&types
);
3085 SSET_FOR_EACH (type
, &types
) {
3086 ofproto_type_get_memory_usage(type
, usage
);
3088 sset_destroy(&types
);
3090 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3091 ofproto_get_memory_usage(br
->ofproto
, usage
);
3095 /* QoS unixctl user interface functions. */
3097 struct qos_unixctl_show_cbdata
{
3099 struct iface
*iface
;
3103 qos_unixctl_show_queue(unsigned int queue_id
,
3104 const struct smap
*details
,
3105 struct iface
*iface
,
3108 struct netdev_queue_stats stats
;
3109 struct smap_node
*node
;
3112 ds_put_cstr(ds
, "\n");
3114 ds_put_format(ds
, "Queue %u:\n", queue_id
);
3116 ds_put_cstr(ds
, "Default:\n");
3119 SMAP_FOR_EACH (node
, details
) {
3120 ds_put_format(ds
, "\t%s: %s\n", node
->key
, node
->value
);
3123 error
= netdev_get_queue_stats(iface
->netdev
, queue_id
, &stats
);
3125 if (stats
.tx_packets
!= UINT64_MAX
) {
3126 ds_put_format(ds
, "\ttx_packets: %"PRIu64
"\n", stats
.tx_packets
);
3129 if (stats
.tx_bytes
!= UINT64_MAX
) {
3130 ds_put_format(ds
, "\ttx_bytes: %"PRIu64
"\n", stats
.tx_bytes
);
3133 if (stats
.tx_errors
!= UINT64_MAX
) {
3134 ds_put_format(ds
, "\ttx_errors: %"PRIu64
"\n", stats
.tx_errors
);
3137 ds_put_format(ds
, "\tFailed to get statistics for queue %u: %s",
3138 queue_id
, ovs_strerror(error
));
3143 qos_unixctl_show_types(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
3144 const char *argv
[], void *aux OVS_UNUSED
)
3146 struct ds ds
= DS_EMPTY_INITIALIZER
;
3147 struct sset types
= SSET_INITIALIZER(&types
);
3148 struct iface
*iface
;
3149 const char * types_name
;
3152 iface
= iface_find(argv
[1]);
3154 unixctl_command_reply_error(conn
, "no such interface");
3158 error
= netdev_get_qos_types(iface
->netdev
, &types
);
3160 if (!sset_is_empty(&types
)) {
3161 SSET_FOR_EACH (types_name
, &types
) {
3162 ds_put_format(&ds
, "QoS type: %s\n", types_name
);
3164 unixctl_command_reply(conn
, ds_cstr(&ds
));
3166 ds_put_format(&ds
, "No QoS types supported for interface: %s\n",
3168 unixctl_command_reply(conn
, ds_cstr(&ds
));
3171 ds_put_format(&ds
, "%s: failed to retrieve supported QoS types (%s)",
3172 iface
->name
, ovs_strerror(error
));
3173 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
3176 sset_destroy(&types
);
3181 qos_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
3182 const char *argv
[], void *aux OVS_UNUSED
)
3184 struct ds ds
= DS_EMPTY_INITIALIZER
;
3185 struct smap smap
= SMAP_INITIALIZER(&smap
);
3186 struct iface
*iface
;
3188 struct smap_node
*node
;
3191 iface
= iface_find(argv
[1]);
3193 unixctl_command_reply_error(conn
, "no such interface");
3197 error
= netdev_get_qos(iface
->netdev
, &type
, &smap
);
3199 if (*type
!= '\0') {
3200 struct netdev_queue_dump dump
;
3201 struct smap details
;
3202 unsigned int queue_id
;
3204 ds_put_format(&ds
, "QoS: %s %s\n", iface
->name
, type
);
3206 SMAP_FOR_EACH (node
, &smap
) {
3207 ds_put_format(&ds
, "%s: %s\n", node
->key
, node
->value
);
3210 smap_init(&details
);
3211 NETDEV_QUEUE_FOR_EACH (&queue_id
, &details
, &dump
, iface
->netdev
) {
3212 qos_unixctl_show_queue(queue_id
, &details
, iface
, &ds
);
3214 smap_destroy(&details
);
3216 unixctl_command_reply(conn
, ds_cstr(&ds
));
3218 ds_put_format(&ds
, "QoS not configured on %s\n", iface
->name
);
3219 unixctl_command_reply(conn
, ds_cstr(&ds
));
3222 ds_put_format(&ds
, "%s: failed to retrieve QOS configuration (%s)\n",
3223 iface
->name
, ovs_strerror(error
));
3224 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
3227 smap_destroy(&smap
);
3231 /* Bridge reconfiguration functions. */
3233 bridge_create(const struct ovsrec_bridge
*br_cfg
)
3237 ovs_assert(!bridge_lookup(br_cfg
->name
));
3238 br
= xzalloc(sizeof *br
);
3240 br
->name
= xstrdup(br_cfg
->name
);
3241 br
->type
= xstrdup(ofproto_normalize_type(br_cfg
->datapath_type
));
3244 /* Derive the default Ethernet address from the bridge's UUID. This should
3245 * be unique and it will be stable between ovs-vswitchd runs. */
3246 memcpy(&br
->default_ea
, &br_cfg
->header_
.uuid
, ETH_ADDR_LEN
);
3247 eth_addr_mark_random(&br
->default_ea
);
3249 hmap_init(&br
->ports
);
3250 hmap_init(&br
->ifaces
);
3251 hmap_init(&br
->iface_by_name
);
3252 hmap_init(&br
->mirrors
);
3254 hmap_init(&br
->mappings
);
3255 hmap_insert(&all_bridges
, &br
->node
, hash_string(br
->name
, 0));
3259 bridge_destroy(struct bridge
*br
, bool del
)
3262 struct mirror
*mirror
, *next_mirror
;
3263 struct port
*port
, *next_port
;
3265 HMAP_FOR_EACH_SAFE (port
, next_port
, hmap_node
, &br
->ports
) {
3268 HMAP_FOR_EACH_SAFE (mirror
, next_mirror
, hmap_node
, &br
->mirrors
) {
3269 mirror_destroy(mirror
);
3272 hmap_remove(&all_bridges
, &br
->node
);
3273 ofproto_destroy(br
->ofproto
, del
);
3274 hmap_destroy(&br
->ifaces
);
3275 hmap_destroy(&br
->ports
);
3276 hmap_destroy(&br
->iface_by_name
);
3277 hmap_destroy(&br
->mirrors
);
3278 hmap_destroy(&br
->mappings
);
3285 static struct bridge
*
3286 bridge_lookup(const char *name
)
3290 HMAP_FOR_EACH_WITH_HASH (br
, node
, hash_string(name
, 0), &all_bridges
) {
3291 if (!strcmp(br
->name
, name
)) {
3298 /* Handle requests for a listing of all flows known by the OpenFlow
3299 * stack, including those normally hidden. */
3301 bridge_unixctl_dump_flows(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
3302 const char *argv
[], void *aux OVS_UNUSED
)
3307 br
= bridge_lookup(argv
[1]);
3309 unixctl_command_reply_error(conn
, "Unknown bridge");
3314 ofproto_get_all_flows(br
->ofproto
, &results
);
3316 unixctl_command_reply(conn
, ds_cstr(&results
));
3317 ds_destroy(&results
);
3320 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
3321 * connections and reconnect. If BRIDGE is not specified, then all bridges
3322 * drop their controller connections and reconnect. */
3324 bridge_unixctl_reconnect(struct unixctl_conn
*conn
, int argc
,
3325 const char *argv
[], void *aux OVS_UNUSED
)
3329 br
= bridge_lookup(argv
[1]);
3331 unixctl_command_reply_error(conn
, "Unknown bridge");
3334 ofproto_reconnect_controllers(br
->ofproto
);
3336 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3337 ofproto_reconnect_controllers(br
->ofproto
);
3340 unixctl_command_reply(conn
, NULL
);
3344 bridge_get_controllers(const struct bridge
*br
,
3345 struct ovsrec_controller
***controllersp
)
3347 struct ovsrec_controller
**controllers
;
3348 size_t n_controllers
;
3350 controllers
= br
->cfg
->controller
;
3351 n_controllers
= br
->cfg
->n_controller
;
3353 if (n_controllers
== 1 && !strcmp(controllers
[0]->target
, "none")) {
3359 *controllersp
= controllers
;
3361 return n_controllers
;
3365 bridge_collect_wanted_ports(struct bridge
*br
,
3366 struct shash
*wanted_ports
)
3370 shash_init(wanted_ports
);
3372 for (i
= 0; i
< br
->cfg
->n_ports
; i
++) {
3373 const char *name
= br
->cfg
->ports
[i
]->name
;
3374 if (!shash_add_once(wanted_ports
, name
, br
->cfg
->ports
[i
])) {
3375 VLOG_WARN("bridge %s: %s specified twice as bridge port",
3379 if (bridge_get_controllers(br
, NULL
)
3380 && !shash_find(wanted_ports
, br
->name
)) {
3381 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
3382 br
->name
, br
->name
);
3384 ovsrec_interface_init(&br
->synth_local_iface
);
3385 ovsrec_port_init(&br
->synth_local_port
);
3387 br
->synth_local_port
.interfaces
= &br
->synth_local_ifacep
;
3388 br
->synth_local_port
.n_interfaces
= 1;
3389 br
->synth_local_port
.name
= br
->name
;
3391 br
->synth_local_iface
.name
= br
->name
;
3392 br
->synth_local_iface
.type
= "internal";
3394 br
->synth_local_ifacep
= &br
->synth_local_iface
;
3396 shash_add(wanted_ports
, br
->name
, &br
->synth_local_port
);
3400 /* Deletes "struct port"s and "struct iface"s under 'br' which aren't
3401 * consistent with 'br->cfg'. Updates 'br->if_cfg_queue' with interfaces which
3402 * 'br' needs to complete its configuration. */
3404 bridge_del_ports(struct bridge
*br
, const struct shash
*wanted_ports
)
3406 struct shash_node
*port_node
;
3407 struct port
*port
, *next
;
3409 /* Get rid of deleted ports.
3410 * Get rid of deleted interfaces on ports that still exist. */
3411 HMAP_FOR_EACH_SAFE (port
, next
, hmap_node
, &br
->ports
) {
3412 port
->cfg
= shash_find_data(wanted_ports
, port
->name
);
3416 port_del_ifaces(port
);
3420 /* Update iface->cfg and iface->type in interfaces that still exist. */
3421 SHASH_FOR_EACH (port_node
, wanted_ports
) {
3422 const struct ovsrec_port
*port_rec
= port_node
->data
;
3425 for (i
= 0; i
< port_rec
->n_interfaces
; i
++) {
3426 const struct ovsrec_interface
*cfg
= port_rec
->interfaces
[i
];
3427 struct iface
*iface
= iface_lookup(br
, cfg
->name
);
3428 const char *type
= iface_get_type(cfg
, br
->cfg
);
3429 const char *dp_type
= br
->cfg
->datapath_type
;
3430 const char *netdev_type
= ofproto_port_open_type(dp_type
, type
);
3435 iface
->netdev_type
= netdev_type
;
3436 } else if (!strcmp(type
, "null")) {
3437 VLOG_WARN_ONCE("%s: The null interface type is deprecated and"
3438 " may be removed in February 2013. Please email"
3439 " dev@openvswitch.org with concerns.",
3442 /* We will add new interfaces later. */
3448 /* Initializes 'oc' appropriately as a management service controller for
3451 * The caller must free oc->target when it is no longer needed. */
3453 bridge_ofproto_controller_for_mgmt(const struct bridge
*br
,
3454 struct ofproto_controller
*oc
)
3456 oc
->target
= xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br
->name
);
3457 oc
->max_backoff
= 0;
3458 oc
->probe_interval
= 60;
3459 oc
->band
= OFPROTO_OUT_OF_BAND
;
3461 oc
->burst_limit
= 0;
3462 oc
->enable_async_msgs
= true;
3466 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
3468 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller
*c
,
3469 struct ofproto_controller
*oc
)
3473 oc
->target
= c
->target
;
3474 oc
->max_backoff
= c
->max_backoff
? *c
->max_backoff
/ 1000 : 8;
3475 oc
->probe_interval
= c
->inactivity_probe
? *c
->inactivity_probe
/ 1000 : 5;
3476 oc
->band
= (!c
->connection_mode
|| !strcmp(c
->connection_mode
, "in-band")
3477 ? OFPROTO_IN_BAND
: OFPROTO_OUT_OF_BAND
);
3478 oc
->rate_limit
= c
->controller_rate_limit
? *c
->controller_rate_limit
: 0;
3479 oc
->burst_limit
= (c
->controller_burst_limit
3480 ? *c
->controller_burst_limit
: 0);
3481 oc
->enable_async_msgs
= (!c
->enable_async_messages
3482 || *c
->enable_async_messages
);
3483 dscp
= smap_get_int(&c
->other_config
, "dscp", DSCP_DEFAULT
);
3484 if (dscp
< 0 || dscp
> 63) {
3485 dscp
= DSCP_DEFAULT
;
3490 /* Configures the IP stack for 'br''s local interface properly according to the
3491 * configuration in 'c'. */
3493 bridge_configure_local_iface_netdev(struct bridge
*br
,
3494 struct ovsrec_controller
*c
)
3496 struct netdev
*netdev
;
3497 struct in_addr mask
, gateway
;
3499 struct iface
*local_iface
;
3502 /* If there's no local interface or no IP address, give up. */
3503 local_iface
= iface_from_ofp_port(br
, OFPP_LOCAL
);
3504 if (!local_iface
|| !c
->local_ip
|| !ip_parse(c
->local_ip
, &ip
.s_addr
)) {
3508 /* Bring up the local interface. */
3509 netdev
= local_iface
->netdev
;
3510 netdev_turn_flags_on(netdev
, NETDEV_UP
, NULL
);
3512 /* Configure the IP address and netmask. */
3513 if (!c
->local_netmask
3514 || !ip_parse(c
->local_netmask
, &mask
.s_addr
)
3516 mask
.s_addr
= guess_netmask(ip
.s_addr
);
3518 if (!netdev_set_in4(netdev
, ip
, mask
)) {
3519 VLOG_INFO("bridge %s: configured IP address "IP_FMT
", netmask "IP_FMT
,
3520 br
->name
, IP_ARGS(ip
.s_addr
), IP_ARGS(mask
.s_addr
));
3523 /* Configure the default gateway. */
3524 if (c
->local_gateway
3525 && ip_parse(c
->local_gateway
, &gateway
.s_addr
)
3526 && gateway
.s_addr
) {
3527 if (!netdev_add_router(netdev
, gateway
)) {
3528 VLOG_INFO("bridge %s: configured gateway "IP_FMT
,
3529 br
->name
, IP_ARGS(gateway
.s_addr
));
3534 /* Returns true if 'a' and 'b' are the same except that any number of slashes
3535 * in either string are treated as equal to any number of slashes in the other,
3536 * e.g. "x///y" is equal to "x/y".
3538 * Also, if 'b_stoplen' bytes from 'b' are found to be equal to corresponding
3539 * bytes from 'a', the function considers this success. Specify 'b_stoplen' as
3540 * SIZE_MAX to compare all of 'a' to all of 'b' rather than just a prefix of
3541 * 'b' against a prefix of 'a'.
3544 equal_pathnames(const char *a
, const char *b
, size_t b_stoplen
)
3546 const char *b_start
= b
;
3548 if (b
- b_start
>= b_stoplen
) {
3550 } else if (*a
!= *b
) {
3552 } else if (*a
== '/') {
3553 a
+= strspn(a
, "/");
3554 b
+= strspn(b
, "/");
3555 } else if (*a
== '\0') {
3565 bridge_configure_remotes(struct bridge
*br
,
3566 const struct sockaddr_in
*managers
, size_t n_managers
)
3568 bool disable_in_band
;
3570 struct ovsrec_controller
**controllers
;
3571 size_t n_controllers
;
3573 enum ofproto_fail_mode fail_mode
;
3575 struct ofproto_controller
*ocs
;
3579 /* Check if we should disable in-band control on this bridge. */
3580 disable_in_band
= smap_get_bool(&br
->cfg
->other_config
, "disable-in-band",
3583 /* Set OpenFlow queue ID for in-band control. */
3584 ofproto_set_in_band_queue(br
->ofproto
,
3585 smap_get_int(&br
->cfg
->other_config
,
3586 "in-band-queue", -1));
3588 if (disable_in_band
) {
3589 ofproto_set_extra_in_band_remotes(br
->ofproto
, NULL
, 0);
3591 ofproto_set_extra_in_band_remotes(br
->ofproto
, managers
, n_managers
);
3594 n_controllers
= bridge_get_controllers(br
, &controllers
);
3596 ocs
= xmalloc((n_controllers
+ 1) * sizeof *ocs
);
3599 bridge_ofproto_controller_for_mgmt(br
, &ocs
[n_ocs
++]);
3600 for (i
= 0; i
< n_controllers
; i
++) {
3601 struct ovsrec_controller
*c
= controllers
[i
];
3603 if (daemon_should_self_confine()
3604 && (!strncmp(c
->target
, "punix:", 6)
3605 || !strncmp(c
->target
, "unix:", 5))) {
3606 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3609 if (!strncmp(c
->target
, "unix:", 5)) {
3610 /* Connect to a listening socket */
3611 whitelist
= xasprintf("unix:%s/", ovs_rundir());
3612 if (strchr(c
->target
, '/') &&
3613 !equal_pathnames(c
->target
, whitelist
,
3614 strlen(whitelist
))) {
3615 /* Absolute path specified, but not in ovs_rundir */
3616 VLOG_ERR_RL(&rl
, "bridge %s: Not connecting to socket "
3617 "controller \"%s\" due to possibility for "
3618 "remote exploit. Instead, specify socket "
3619 "in whitelisted \"%s\" or connect to "
3620 "\"unix:%s/%s.mgmt\" (which is always "
3621 "available without special configuration).",
3622 br
->name
, c
->target
, whitelist
,
3623 ovs_rundir(), br
->name
);
3628 whitelist
= xasprintf("punix:%s/%s.",
3629 ovs_rundir(), br
->name
);
3630 if (!equal_pathnames(c
->target
, whitelist
, strlen(whitelist
))
3631 || strchr(c
->target
+ strlen(whitelist
), '/')) {
3632 /* Prevent remote ovsdb-server users from accessing
3633 * arbitrary Unix domain sockets and overwriting arbitrary
3635 VLOG_ERR_RL(&rl
, "bridge %s: Not adding Unix domain socket "
3636 "controller \"%s\" due to possibility of "
3637 "overwriting local files. Instead, specify "
3638 "path in whitelisted format \"%s*\" or "
3639 "connect to \"unix:%s/%s.mgmt\" (which is "
3640 "always available without special "
3642 br
->name
, c
->target
, whitelist
,
3643 ovs_rundir(), br
->name
);
3652 bridge_configure_local_iface_netdev(br
, c
);
3653 bridge_ofproto_controller_from_ovsrec(c
, &ocs
[n_ocs
]);
3654 if (disable_in_band
) {
3655 ocs
[n_ocs
].band
= OFPROTO_OUT_OF_BAND
;
3660 ofproto_set_controllers(br
->ofproto
, ocs
, n_ocs
,
3661 bridge_get_allowed_versions(br
));
3662 free(ocs
[0].target
); /* From bridge_ofproto_controller_for_mgmt(). */
3665 /* Set the fail-mode. */
3666 fail_mode
= !br
->cfg
->fail_mode
3667 || !strcmp(br
->cfg
->fail_mode
, "standalone")
3668 ? OFPROTO_FAIL_STANDALONE
3669 : OFPROTO_FAIL_SECURE
;
3670 ofproto_set_fail_mode(br
->ofproto
, fail_mode
);
3672 /* Configure OpenFlow controller connection snooping. */
3673 if (!ofproto_has_snoops(br
->ofproto
)) {
3677 sset_add_and_free(&snoops
, xasprintf("punix:%s/%s.snoop",
3678 ovs_rundir(), br
->name
));
3679 ofproto_set_snoops(br
->ofproto
, &snoops
);
3680 sset_destroy(&snoops
);
3685 bridge_configure_tables(struct bridge
*br
)
3687 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3691 n_tables
= ofproto_get_n_tables(br
->ofproto
);
3693 for (i
= 0; i
< n_tables
; i
++) {
3694 struct ofproto_table_settings s
;
3695 bool use_default_prefixes
= true;
3698 s
.max_flows
= UINT_MAX
;
3700 s
.enable_eviction
= false;
3702 s
.n_prefix_fields
= 0;
3703 memset(s
.prefix_fields
, ~0, sizeof(s
.prefix_fields
));
3705 if (j
< br
->cfg
->n_flow_tables
&& i
== br
->cfg
->key_flow_tables
[j
]) {
3706 struct ovsrec_flow_table
*cfg
= br
->cfg
->value_flow_tables
[j
++];
3709 if (cfg
->n_flow_limit
&& *cfg
->flow_limit
< UINT_MAX
) {
3710 s
.max_flows
= *cfg
->flow_limit
;
3713 s
.enable_eviction
= (cfg
->overflow_policy
3714 && !strcmp(cfg
->overflow_policy
, "evict"));
3715 if (cfg
->n_groups
) {
3716 s
.groups
= xmalloc(cfg
->n_groups
* sizeof *s
.groups
);
3717 for (int k
= 0; k
< cfg
->n_groups
; k
++) {
3718 const char *string
= cfg
->groups
[k
];
3721 msg
= mf_parse_subfield__(&s
.groups
[k
], &string
);
3723 VLOG_WARN_RL(&rl
, "bridge %s table %d: error parsing "
3724 "'groups' (%s)", br
->name
, i
, msg
);
3726 } else if (*string
) {
3727 VLOG_WARN_RL(&rl
, "bridge %s table %d: 'groups' "
3728 "element '%s' contains trailing garbage",
3729 br
->name
, i
, cfg
->groups
[k
]);
3736 /* Prefix lookup fields. */
3737 s
.n_prefix_fields
= 0;
3738 for (int k
= 0; k
< cfg
->n_prefixes
; k
++) {
3739 const char *name
= cfg
->prefixes
[k
];
3740 const struct mf_field
*mf
;
3742 if (strcmp(name
, "none") == 0) {
3743 use_default_prefixes
= false;
3744 s
.n_prefix_fields
= 0;
3747 mf
= mf_from_name(name
);
3749 VLOG_WARN("bridge %s: 'prefixes' with unknown field: %s",
3753 if (mf
->flow_be32ofs
< 0 || mf
->n_bits
% 32) {
3754 VLOG_WARN("bridge %s: 'prefixes' with incompatible field: "
3755 "%s", br
->name
, name
);
3758 if (s
.n_prefix_fields
>= ARRAY_SIZE(s
.prefix_fields
)) {
3759 VLOG_WARN("bridge %s: 'prefixes' with too many fields, "
3760 "field not used: %s", br
->name
, name
);
3763 use_default_prefixes
= false;
3764 s
.prefix_fields
[s
.n_prefix_fields
++] = mf
->id
;
3767 if (use_default_prefixes
) {
3768 /* Use default values. */
3769 s
.n_prefix_fields
= ARRAY_SIZE(default_prefix_fields
);
3770 memcpy(s
.prefix_fields
, default_prefix_fields
,
3771 sizeof default_prefix_fields
);
3773 struct ds ds
= DS_EMPTY_INITIALIZER
;
3774 for (int k
= 0; k
< s
.n_prefix_fields
; k
++) {
3776 ds_put_char(&ds
, ',');
3778 ds_put_cstr(&ds
, mf_from_id(s
.prefix_fields
[k
])->name
);
3780 if (s
.n_prefix_fields
== 0) {
3781 ds_put_cstr(&ds
, "none");
3783 VLOG_INFO("bridge %s table %d: Prefix lookup with: %s.",
3784 br
->name
, i
, ds_cstr(&ds
));
3788 ofproto_configure_table(br
->ofproto
, i
, &s
);
3792 for (; j
< br
->cfg
->n_flow_tables
; j
++) {
3793 VLOG_WARN_RL(&rl
, "bridge %s: ignoring configuration for flow table "
3794 "%"PRId64
" not supported by this datapath", br
->name
,
3795 br
->cfg
->key_flow_tables
[j
]);
3800 bridge_configure_dp_desc(struct bridge
*br
)
3802 ofproto_set_dp_desc(br
->ofproto
,
3803 smap_get(&br
->cfg
->other_config
, "dp-desc"));
3806 static struct aa_mapping
*
3807 bridge_aa_mapping_find(struct bridge
*br
, const int64_t isid
)
3809 struct aa_mapping
*m
;
3811 HMAP_FOR_EACH_IN_BUCKET (m
,
3813 hash_bytes(&isid
, sizeof isid
, 0),
3815 if (isid
== m
->isid
) {
3822 static struct aa_mapping
*
3823 bridge_aa_mapping_create(struct bridge
*br
,
3827 struct aa_mapping
*m
;
3829 m
= xzalloc(sizeof *m
);
3833 m
->br_name
= xstrdup(br
->name
);
3834 hmap_insert(&br
->mappings
,
3836 hash_bytes(&isid
, sizeof isid
, 0));
3842 bridge_aa_mapping_destroy(struct aa_mapping
*m
)
3845 struct bridge
*br
= m
->bridge
;
3848 ofproto_aa_mapping_unregister(br
->ofproto
, m
);
3851 hmap_remove(&br
->mappings
, &m
->hmap_node
);
3860 bridge_aa_mapping_configure(struct aa_mapping
*m
)
3862 struct aa_mapping_settings s
;
3868 ofproto_aa_mapping_register(m
->bridge
->ofproto
, m
, &s
);
3874 bridge_configure_aa(struct bridge
*br
)
3876 const struct ovsdb_datum
*mc
;
3877 struct ovsrec_autoattach
*auto_attach
= br
->cfg
->auto_attach
;
3878 struct aa_settings aa_s
;
3879 struct aa_mapping
*m
, *next
;
3883 ofproto_set_aa(br
->ofproto
, NULL
, NULL
);
3887 memset(&aa_s
, 0, sizeof aa_s
);
3888 aa_s
.system_description
= auto_attach
->system_description
;
3889 aa_s
.system_name
= auto_attach
->system_name
;
3890 ofproto_set_aa(br
->ofproto
, NULL
, &aa_s
);
3892 mc
= ovsrec_autoattach_get_mappings(auto_attach
,
3894 OVSDB_TYPE_INTEGER
);
3895 HMAP_FOR_EACH_SAFE (m
, next
, hmap_node
, &br
->mappings
) {
3896 union ovsdb_atom atom
;
3898 atom
.integer
= m
->isid
;
3899 if (ovsdb_datum_find_key(mc
, &atom
, OVSDB_TYPE_INTEGER
) == UINT_MAX
) {
3900 VLOG_INFO("Deleting isid=%"PRIu32
", vlan=%"PRIu16
,
3902 bridge_aa_mapping_destroy(m
);
3906 /* Add new mappings and reconfigure existing ones. */
3907 for (i
= 0; i
< auto_attach
->n_mappings
; ++i
) {
3908 m
= bridge_aa_mapping_find(br
, auto_attach
->key_mappings
[i
]);
3911 VLOG_INFO("Adding isid=%"PRId64
", vlan=%"PRId64
,
3912 auto_attach
->key_mappings
[i
],
3913 auto_attach
->value_mappings
[i
]);
3914 m
= bridge_aa_mapping_create(br
,
3915 auto_attach
->key_mappings
[i
],
3916 auto_attach
->value_mappings
[i
]);
3918 if (!bridge_aa_mapping_configure(m
)) {
3919 bridge_aa_mapping_destroy(m
);
3926 bridge_aa_need_refresh(struct bridge
*br
)
3928 return ofproto_aa_vlan_get_queue_size(br
->ofproto
) > 0;
3932 bridge_aa_update_trunks(struct port
*port
, struct bridge_aa_vlan
*m
)
3934 int64_t *trunks
= NULL
;
3936 bool found
= false, reconfigure
= false;
3938 for (i
= 0; i
< port
->cfg
->n_trunks
; i
++) {
3939 if (port
->cfg
->trunks
[i
] == m
->vlan
) {
3946 case BRIDGE_AA_VLAN_OPER_ADD
:
3948 trunks
= xmalloc(sizeof *trunks
* (port
->cfg
->n_trunks
+ 1));
3950 for (i
= 0; i
< port
->cfg
->n_trunks
; i
++) {
3951 trunks
[i
] = port
->cfg
->trunks
[i
];
3953 trunks
[i
++] = m
->vlan
;
3959 case BRIDGE_AA_VLAN_OPER_REMOVE
:
3963 trunks
= xmalloc(sizeof *trunks
* (port
->cfg
->n_trunks
- 1));
3965 for (i
= 0; i
< port
->cfg
->n_trunks
; i
++) {
3966 if (port
->cfg
->trunks
[i
] != m
->vlan
) {
3967 trunks
[j
++] = port
->cfg
->trunks
[i
];
3976 case BRIDGE_AA_VLAN_OPER_UNDEF
:
3978 VLOG_WARN("unrecognized operation %u", m
->oper
);
3983 /* VLAN switching under trunk mode cause the trunk port to switch all
3984 * VLANs, see ovs-vswitchd.conf.db
3987 static char *vlan_mode_access
= "access";
3988 ovsrec_port_set_vlan_mode(port
->cfg
, vlan_mode_access
);
3992 static char *vlan_mode_trunk
= "trunk";
3993 ovsrec_port_set_vlan_mode(port
->cfg
, vlan_mode_trunk
);
3996 ovsrec_port_set_trunks(port
->cfg
, trunks
, i
);
3998 /* Force reconfigure of the port. */
3999 port_configure(port
);
4006 bridge_aa_refresh_queued(struct bridge
*br
)
4008 struct ovs_list
*list
= xmalloc(sizeof *list
);
4009 struct bridge_aa_vlan
*node
, *next
;
4011 ovs_list_init(list
);
4012 ofproto_aa_vlan_get_queued(br
->ofproto
, list
);
4014 LIST_FOR_EACH_SAFE (node
, next
, list_node
, list
) {
4017 VLOG_INFO("ifname=%s, vlan=%u, oper=%u", node
->port_name
, node
->vlan
,
4020 port
= port_lookup(br
, node
->port_name
);
4022 bridge_aa_update_trunks(port
, node
);
4025 ovs_list_remove(&node
->list_node
);
4026 free(node
->port_name
);
4034 /* Port functions. */
4036 static struct port
*
4037 port_create(struct bridge
*br
, const struct ovsrec_port
*cfg
)
4041 port
= xzalloc(sizeof *port
);
4043 port
->name
= xstrdup(cfg
->name
);
4045 ovs_list_init(&port
->ifaces
);
4047 hmap_insert(&br
->ports
, &port
->hmap_node
, hash_string(port
->name
, 0));
4051 /* Deletes interfaces from 'port' that are no longer configured for it. */
4053 port_del_ifaces(struct port
*port
)
4055 struct iface
*iface
, *next
;
4056 struct sset new_ifaces
;
4059 /* Collect list of new interfaces. */
4060 sset_init(&new_ifaces
);
4061 for (i
= 0; i
< port
->cfg
->n_interfaces
; i
++) {
4062 const char *name
= port
->cfg
->interfaces
[i
]->name
;
4063 const char *type
= port
->cfg
->interfaces
[i
]->type
;
4064 if (strcmp(type
, "null")) {
4065 sset_add(&new_ifaces
, name
);
4069 /* Get rid of deleted interfaces. */
4070 LIST_FOR_EACH_SAFE (iface
, next
, port_elem
, &port
->ifaces
) {
4071 if (!sset_contains(&new_ifaces
, iface
->name
)) {
4072 iface_destroy(iface
);
4076 sset_destroy(&new_ifaces
);
4080 port_destroy(struct port
*port
)
4083 struct bridge
*br
= port
->bridge
;
4084 struct iface
*iface
, *next
;
4087 ofproto_bundle_unregister(br
->ofproto
, port
);
4090 LIST_FOR_EACH_SAFE (iface
, next
, port_elem
, &port
->ifaces
) {
4091 iface_destroy__(iface
);
4094 hmap_remove(&br
->ports
, &port
->hmap_node
);
4100 static struct port
*
4101 port_lookup(const struct bridge
*br
, const char *name
)
4105 HMAP_FOR_EACH_WITH_HASH (port
, hmap_node
, hash_string(name
, 0),
4107 if (!strcmp(port
->name
, name
)) {
4115 enable_lacp(struct port
*port
, bool *activep
)
4117 if (!port
->cfg
->lacp
) {
4118 /* XXX when LACP implementation has been sufficiently tested, enable by
4119 * default and make active on bonded ports. */
4121 } else if (!strcmp(port
->cfg
->lacp
, "off")) {
4123 } else if (!strcmp(port
->cfg
->lacp
, "active")) {
4126 } else if (!strcmp(port
->cfg
->lacp
, "passive")) {
4130 VLOG_WARN("port %s: unknown LACP mode %s",
4131 port
->name
, port
->cfg
->lacp
);
4136 static struct lacp_settings
*
4137 port_configure_lacp(struct port
*port
, struct lacp_settings
*s
)
4139 const char *lacp_time
, *system_id
;
4142 if (!enable_lacp(port
, &s
->active
)) {
4146 s
->name
= port
->name
;
4148 system_id
= smap_get(&port
->cfg
->other_config
, "lacp-system-id");
4150 if (!ovs_scan(system_id
, ETH_ADDR_SCAN_FMT
,
4151 ETH_ADDR_SCAN_ARGS(s
->id
))) {
4152 VLOG_WARN("port %s: LACP system ID (%s) must be an Ethernet"
4153 " address.", port
->name
, system_id
);
4157 s
->id
= port
->bridge
->ea
;
4160 if (eth_addr_is_zero(s
->id
)) {
4161 VLOG_WARN("port %s: Invalid zero LACP system ID.", port
->name
);
4165 /* Prefer bondable links if unspecified. */
4166 priority
= smap_get_int(&port
->cfg
->other_config
, "lacp-system-priority",
4168 s
->priority
= (priority
> 0 && priority
<= UINT16_MAX
4170 : UINT16_MAX
- !ovs_list_is_short(&port
->ifaces
));
4172 lacp_time
= smap_get_def(&port
->cfg
->other_config
, "lacp-time", "");
4173 s
->fast
= !strcasecmp(lacp_time
, "fast");
4175 s
->fallback_ab_cfg
= smap_get_bool(&port
->cfg
->other_config
,
4176 "lacp-fallback-ab", false);
4182 iface_configure_lacp(struct iface
*iface
, struct lacp_slave_settings
*s
)
4184 int priority
, portid
, key
;
4186 portid
= smap_get_int(&iface
->cfg
->other_config
, "lacp-port-id", 0);
4187 priority
= smap_get_int(&iface
->cfg
->other_config
, "lacp-port-priority",
4189 key
= smap_get_int(&iface
->cfg
->other_config
, "lacp-aggregation-key", 0);
4191 if (portid
<= 0 || portid
> UINT16_MAX
) {
4192 portid
= ofp_to_u16(iface
->ofp_port
);
4195 if (priority
<= 0 || priority
> UINT16_MAX
) {
4196 priority
= UINT16_MAX
;
4199 if (key
< 0 || key
> UINT16_MAX
) {
4203 s
->name
= iface
->name
;
4205 s
->priority
= priority
;
4210 port_configure_bond(struct port
*port
, struct bond_settings
*s
)
4212 const char *detect_s
;
4213 struct iface
*iface
;
4215 int miimon_interval
;
4217 s
->name
= port
->name
;
4219 if (port
->cfg
->bond_mode
) {
4220 if (!bond_mode_from_string(&s
->balance
, port
->cfg
->bond_mode
)) {
4221 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
4222 port
->name
, port
->cfg
->bond_mode
,
4223 bond_mode_to_string(s
->balance
));
4226 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
4228 /* XXX: Post version 1.5.*, the default bond_mode changed from SLB to
4229 * active-backup. At some point we should remove this warning. */
4230 VLOG_WARN_RL(&rl
, "port %s: Using the default bond_mode %s. Note that"
4231 " in previous versions, the default bond_mode was"
4232 " balance-slb", port
->name
,
4233 bond_mode_to_string(s
->balance
));
4235 if (s
->balance
== BM_SLB
&& port
->bridge
->cfg
->n_flood_vlans
) {
4236 VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
4237 "please use another bond type or disable flood_vlans",
4241 miimon_interval
= smap_get_int(&port
->cfg
->other_config
,
4242 "bond-miimon-interval", 0);
4243 if (miimon_interval
<= 0) {
4244 miimon_interval
= 200;
4247 detect_s
= smap_get(&port
->cfg
->other_config
, "bond-detect-mode");
4248 if (!detect_s
|| !strcmp(detect_s
, "carrier")) {
4249 miimon_interval
= 0;
4250 } else if (strcmp(detect_s
, "miimon")) {
4251 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
4252 "defaulting to carrier", port
->name
, detect_s
);
4253 miimon_interval
= 0;
4256 s
->up_delay
= MAX(0, port
->cfg
->bond_updelay
);
4257 s
->down_delay
= MAX(0, port
->cfg
->bond_downdelay
);
4258 s
->basis
= smap_get_int(&port
->cfg
->other_config
, "bond-hash-basis", 0);
4259 s
->rebalance_interval
= smap_get_int(&port
->cfg
->other_config
,
4260 "bond-rebalance-interval", 10000);
4261 if (s
->rebalance_interval
&& s
->rebalance_interval
< 1000) {
4262 s
->rebalance_interval
= 1000;
4265 s
->lacp_fallback_ab_cfg
= smap_get_bool(&port
->cfg
->other_config
,
4266 "lacp-fallback-ab", false);
4268 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
4269 netdev_set_miimon_interval(iface
->netdev
, miimon_interval
);
4272 mac_s
= port
->cfg
->bond_active_slave
;
4273 if (!mac_s
|| !ovs_scan(mac_s
, ETH_ADDR_SCAN_FMT
,
4274 ETH_ADDR_SCAN_ARGS(s
->active_slave_mac
))) {
4275 /* OVSDB did not store the last active interface */
4276 s
->active_slave_mac
= eth_addr_zero
;
4280 /* Returns true if 'port' is synthetic, that is, if we constructed it locally
4281 * instead of obtaining it from the database. */
4283 port_is_synthetic(const struct port
*port
)
4285 return ovsdb_idl_row_is_synthetic(&port
->cfg
->header_
);
4288 /* Interface functions. */
4291 iface_is_internal(const struct ovsrec_interface
*iface
,
4292 const struct ovsrec_bridge
*br
)
4294 /* The local port and "internal" ports are always "internal". */
4295 return !strcmp(iface
->type
, "internal") || !strcmp(iface
->name
, br
->name
);
4298 /* Returns the correct network device type for interface 'iface' in bridge
4301 iface_get_type(const struct ovsrec_interface
*iface
,
4302 const struct ovsrec_bridge
*br
)
4306 /* The local port always has type "internal". Other ports take
4307 * their type from the database and default to "system" if none is
4309 if (iface_is_internal(iface
, br
)) {
4312 type
= iface
->type
[0] ? iface
->type
: "system";
4319 iface_destroy__(struct iface
*iface
)
4322 struct port
*port
= iface
->port
;
4323 struct bridge
*br
= port
->bridge
;
4325 VLOG_INFO("bridge %s: deleted interface %s on port %d",
4326 br
->name
, iface
->name
, iface
->ofp_port
);
4328 if (br
->ofproto
&& iface
->ofp_port
!= OFPP_NONE
) {
4329 ofproto_port_unregister(br
->ofproto
, iface
->ofp_port
);
4332 if (iface
->ofp_port
!= OFPP_NONE
) {
4333 hmap_remove(&br
->ifaces
, &iface
->ofp_port_node
);
4336 ovs_list_remove(&iface
->port_elem
);
4337 hmap_remove(&br
->iface_by_name
, &iface
->name_node
);
4339 /* The user is changing configuration here, so netdev_remove needs to be
4340 * used as opposed to netdev_close */
4341 netdev_remove(iface
->netdev
);
4349 iface_destroy(struct iface
*iface
)
4352 struct port
*port
= iface
->port
;
4354 iface_destroy__(iface
);
4355 if (ovs_list_is_empty(&port
->ifaces
)) {
4361 static struct iface
*
4362 iface_lookup(const struct bridge
*br
, const char *name
)
4364 struct iface
*iface
;
4366 HMAP_FOR_EACH_WITH_HASH (iface
, name_node
, hash_string(name
, 0),
4367 &br
->iface_by_name
) {
4368 if (!strcmp(iface
->name
, name
)) {
4376 static struct iface
*
4377 iface_find(const char *name
)
4379 const struct bridge
*br
;
4381 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
4382 struct iface
*iface
= iface_lookup(br
, name
);
4391 static struct iface
*
4392 iface_from_ofp_port(const struct bridge
*br
, ofp_port_t ofp_port
)
4394 struct iface
*iface
;
4396 HMAP_FOR_EACH_IN_BUCKET (iface
, ofp_port_node
, hash_ofp_port(ofp_port
),
4398 if (iface
->ofp_port
== ofp_port
) {
4405 /* Set Ethernet address of 'iface', if one is specified in the configuration
4408 iface_set_mac(const struct bridge
*br
, const struct port
*port
, struct iface
*iface
)
4410 struct eth_addr ea
, *mac
= NULL
;
4411 struct iface
*hw_addr_iface
;
4413 if (strcmp(iface
->type
, "internal")) {
4417 if (iface
->cfg
->mac
&& eth_addr_from_string(iface
->cfg
->mac
, &ea
)) {
4419 } else if (port
->cfg
->fake_bridge
) {
4420 /* Fake bridge and no MAC set in the configuration. Pick a local one. */
4421 find_local_hw_addr(br
, &ea
, port
, &hw_addr_iface
);
4426 if (iface
->ofp_port
== OFPP_LOCAL
) {
4427 VLOG_ERR("interface %s: ignoring mac in Interface record "
4428 "(use Bridge record to set local port's mac)",
4430 } else if (eth_addr_is_multicast(*mac
)) {
4431 VLOG_ERR("interface %s: cannot set MAC to multicast address",
4433 } else if (eth_addr_is_zero(*mac
)) {
4434 VLOG_ERR("interface %s: cannot set MAC to all zero address",
4437 int error
= netdev_set_etheraddr(iface
->netdev
, *mac
);
4439 VLOG_ERR("interface %s: setting MAC failed (%s)",
4440 iface
->name
, ovs_strerror(error
));
4446 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
4448 iface_set_ofport(const struct ovsrec_interface
*if_cfg
, ofp_port_t ofport
)
4450 if (if_cfg
&& !ovsdb_idl_row_is_synthetic(&if_cfg
->header_
)) {
4451 int64_t port
= ofport
== OFPP_NONE
? -1 : ofp_to_u16(ofport
);
4452 ovsrec_interface_set_ofport(if_cfg
, &port
, 1);
4456 /* Clears all of the fields in 'if_cfg' that indicate interface status, and
4457 * sets the "ofport" field to -1.
4459 * This is appropriate when 'if_cfg''s interface cannot be created or is
4460 * otherwise invalid. */
4462 iface_clear_db_record(const struct ovsrec_interface
*if_cfg
, char *errp
)
4464 if (!ovsdb_idl_row_is_synthetic(&if_cfg
->header_
)) {
4465 iface_set_ofport(if_cfg
, OFPP_NONE
);
4466 ovsrec_interface_set_error(if_cfg
, errp
);
4467 ovsrec_interface_set_status(if_cfg
, NULL
);
4468 ovsrec_interface_set_admin_state(if_cfg
, NULL
);
4469 ovsrec_interface_set_duplex(if_cfg
, NULL
);
4470 ovsrec_interface_set_link_speed(if_cfg
, NULL
, 0);
4471 ovsrec_interface_set_link_state(if_cfg
, NULL
);
4472 ovsrec_interface_set_mac_in_use(if_cfg
, NULL
);
4473 ovsrec_interface_set_mtu(if_cfg
, NULL
, 0);
4474 ovsrec_interface_set_cfm_fault(if_cfg
, NULL
, 0);
4475 ovsrec_interface_set_cfm_fault_status(if_cfg
, NULL
, 0);
4476 ovsrec_interface_set_cfm_remote_mpids(if_cfg
, NULL
, 0);
4477 ovsrec_interface_set_lacp_current(if_cfg
, NULL
, 0);
4478 ovsrec_interface_set_statistics(if_cfg
, NULL
, NULL
, 0);
4479 ovsrec_interface_set_ifindex(if_cfg
, NULL
, 0);
4484 queue_ids_include(const struct ovsdb_datum
*queues
, int64_t target
)
4486 union ovsdb_atom atom
;
4488 atom
.integer
= target
;
4489 return ovsdb_datum_find_key(queues
, &atom
, OVSDB_TYPE_INTEGER
) != UINT_MAX
;
4493 iface_configure_qos(struct iface
*iface
, const struct ovsrec_qos
*qos
)
4495 struct ofpbuf queues_buf
;
4497 ofpbuf_init(&queues_buf
, 0);
4499 if (!qos
|| qos
->type
[0] == '\0') {
4500 netdev_set_qos(iface
->netdev
, NULL
, NULL
);
4502 const struct ovsdb_datum
*queues
;
4503 struct netdev_queue_dump dump
;
4504 unsigned int queue_id
;
4505 struct smap details
;
4509 /* Configure top-level Qos for 'iface'. */
4510 netdev_set_qos(iface
->netdev
, qos
->type
, &qos
->other_config
);
4512 /* Deconfigure queues that were deleted. */
4513 queues
= ovsrec_qos_get_queues(qos
, OVSDB_TYPE_INTEGER
,
4515 smap_init(&details
);
4516 NETDEV_QUEUE_FOR_EACH (&queue_id
, &details
, &dump
, iface
->netdev
) {
4517 if (!queue_ids_include(queues
, queue_id
)) {
4518 netdev_delete_queue(iface
->netdev
, queue_id
);
4521 smap_destroy(&details
);
4523 /* Configure queues for 'iface'. */
4525 for (i
= 0; i
< qos
->n_queues
; i
++) {
4526 const struct ovsrec_queue
*queue
= qos
->value_queues
[i
];
4527 queue_id
= qos
->key_queues
[i
];
4529 if (queue_id
== 0) {
4533 if (queue
->n_dscp
== 1) {
4534 struct ofproto_port_queue
*port_queue
;
4536 port_queue
= ofpbuf_put_uninit(&queues_buf
,
4537 sizeof *port_queue
);
4538 port_queue
->queue
= queue_id
;
4539 port_queue
->dscp
= queue
->dscp
[0];
4542 netdev_set_queue(iface
->netdev
, queue_id
, &queue
->other_config
);
4545 smap_init(&details
);
4546 netdev_set_queue(iface
->netdev
, 0, &details
);
4547 smap_destroy(&details
);
4551 if (iface
->ofp_port
!= OFPP_NONE
) {
4552 const struct ofproto_port_queue
*port_queues
= queues_buf
.data
;
4553 size_t n_queues
= queues_buf
.size
/ sizeof *port_queues
;
4555 ofproto_port_set_queues(iface
->port
->bridge
->ofproto
, iface
->ofp_port
,
4556 port_queues
, n_queues
);
4559 netdev_set_policing(iface
->netdev
,
4560 MIN(UINT32_MAX
, iface
->cfg
->ingress_policing_rate
),
4561 MIN(UINT32_MAX
, iface
->cfg
->ingress_policing_burst
));
4563 ofpbuf_uninit(&queues_buf
);
4567 iface_configure_cfm(struct iface
*iface
)
4569 const struct ovsrec_interface
*cfg
= iface
->cfg
;
4570 const char *opstate_str
;
4571 const char *cfm_ccm_vlan
;
4572 struct cfm_settings s
;
4573 struct smap netdev_args
;
4575 if (!cfg
->n_cfm_mpid
) {
4576 ofproto_port_clear_cfm(iface
->port
->bridge
->ofproto
, iface
->ofp_port
);
4580 s
.check_tnl_key
= false;
4581 smap_init(&netdev_args
);
4582 if (!netdev_get_config(iface
->netdev
, &netdev_args
)) {
4583 const char *key
= smap_get(&netdev_args
, "key");
4584 const char *in_key
= smap_get(&netdev_args
, "in_key");
4586 s
.check_tnl_key
= (key
&& !strcmp(key
, "flow"))
4587 || (in_key
&& !strcmp(in_key
, "flow"));
4589 smap_destroy(&netdev_args
);
4591 s
.mpid
= *cfg
->cfm_mpid
;
4592 s
.interval
= smap_get_int(&iface
->cfg
->other_config
, "cfm_interval", 0);
4593 cfm_ccm_vlan
= smap_get(&iface
->cfg
->other_config
, "cfm_ccm_vlan");
4594 s
.ccm_pcp
= smap_get_int(&iface
->cfg
->other_config
, "cfm_ccm_pcp", 0);
4596 if (s
.interval
<= 0) {
4600 if (!cfm_ccm_vlan
) {
4602 } else if (!strcasecmp("random", cfm_ccm_vlan
)) {
4603 s
.ccm_vlan
= CFM_RANDOM_VLAN
;
4605 s
.ccm_vlan
= atoi(cfm_ccm_vlan
);
4606 if (s
.ccm_vlan
== CFM_RANDOM_VLAN
) {
4611 s
.extended
= smap_get_bool(&iface
->cfg
->other_config
, "cfm_extended",
4613 s
.demand
= smap_get_bool(&iface
->cfg
->other_config
, "cfm_demand", false);
4615 opstate_str
= smap_get(&iface
->cfg
->other_config
, "cfm_opstate");
4616 s
.opup
= !opstate_str
|| !strcasecmp("up", opstate_str
);
4618 ofproto_port_set_cfm(iface
->port
->bridge
->ofproto
, iface
->ofp_port
, &s
);
4621 /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
4622 * instead of obtaining it from the database. */
4624 iface_is_synthetic(const struct iface
*iface
)
4626 return ovsdb_idl_row_is_synthetic(&iface
->cfg
->header_
);
4630 iface_validate_ofport__(size_t n
, int64_t *ofport
)
4632 return (n
&& *ofport
>= 1 && *ofport
< ofp_to_u16(OFPP_MAX
)
4633 ? u16_to_ofp(*ofport
)
4638 iface_get_requested_ofp_port(const struct ovsrec_interface
*cfg
)
4640 return iface_validate_ofport__(cfg
->n_ofport_request
, cfg
->ofport_request
);
4644 iface_pick_ofport(const struct ovsrec_interface
*cfg
)
4646 ofp_port_t requested_ofport
= iface_get_requested_ofp_port(cfg
);
4647 return (requested_ofport
!= OFPP_NONE
4649 : iface_validate_ofport__(cfg
->n_ofport
, cfg
->ofport
));
4652 /* Port mirroring. */
4654 static struct mirror
*
4655 mirror_find_by_uuid(struct bridge
*br
, const struct uuid
*uuid
)
4659 HMAP_FOR_EACH_IN_BUCKET (m
, hmap_node
, uuid_hash(uuid
), &br
->mirrors
) {
4660 if (uuid_equals(uuid
, &m
->uuid
)) {
4668 bridge_configure_mirrors(struct bridge
*br
)
4670 const struct ovsdb_datum
*mc
;
4671 unsigned long *flood_vlans
;
4672 struct mirror
*m
, *next
;
4675 /* Get rid of deleted mirrors. */
4676 mc
= ovsrec_bridge_get_mirrors(br
->cfg
, OVSDB_TYPE_UUID
);
4677 HMAP_FOR_EACH_SAFE (m
, next
, hmap_node
, &br
->mirrors
) {
4678 union ovsdb_atom atom
;
4680 atom
.uuid
= m
->uuid
;
4681 if (ovsdb_datum_find_key(mc
, &atom
, OVSDB_TYPE_UUID
) == UINT_MAX
) {
4686 /* Add new mirrors and reconfigure existing ones. */
4687 for (i
= 0; i
< br
->cfg
->n_mirrors
; i
++) {
4688 const struct ovsrec_mirror
*cfg
= br
->cfg
->mirrors
[i
];
4689 m
= mirror_find_by_uuid(br
, &cfg
->header_
.uuid
);
4691 m
= mirror_create(br
, cfg
);
4694 if (!mirror_configure(m
)) {
4699 /* Update flooded vlans (for RSPAN). */
4700 flood_vlans
= vlan_bitmap_from_array(br
->cfg
->flood_vlans
,
4701 br
->cfg
->n_flood_vlans
);
4702 ofproto_set_flood_vlans(br
->ofproto
, flood_vlans
);
4703 bitmap_free(flood_vlans
);
4706 static struct mirror
*
4707 mirror_create(struct bridge
*br
, const struct ovsrec_mirror
*cfg
)
4711 m
= xzalloc(sizeof *m
);
4712 m
->uuid
= cfg
->header_
.uuid
;
4713 hmap_insert(&br
->mirrors
, &m
->hmap_node
, uuid_hash(&m
->uuid
));
4715 m
->name
= xstrdup(cfg
->name
);
4721 mirror_destroy(struct mirror
*m
)
4724 struct bridge
*br
= m
->bridge
;
4727 ofproto_mirror_unregister(br
->ofproto
, m
);
4730 hmap_remove(&br
->mirrors
, &m
->hmap_node
);
4737 mirror_collect_ports(struct mirror
*m
,
4738 struct ovsrec_port
**in_ports
, int n_in_ports
,
4739 void ***out_portsp
, size_t *n_out_portsp
)
4741 void **out_ports
= xmalloc(n_in_ports
* sizeof *out_ports
);
4742 size_t n_out_ports
= 0;
4745 for (i
= 0; i
< n_in_ports
; i
++) {
4746 const char *name
= in_ports
[i
]->name
;
4747 struct port
*port
= port_lookup(m
->bridge
, name
);
4749 out_ports
[n_out_ports
++] = port
;
4751 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
4752 "port %s", m
->bridge
->name
, m
->name
, name
);
4755 *out_portsp
= out_ports
;
4756 *n_out_portsp
= n_out_ports
;
4760 mirror_configure(struct mirror
*m
)
4762 const struct ovsrec_mirror
*cfg
= m
->cfg
;
4763 struct ofproto_mirror_settings s
;
4766 if (strcmp(cfg
->name
, m
->name
)) {
4768 m
->name
= xstrdup(cfg
->name
);
4772 /* Get output port or VLAN. */
4773 if (cfg
->output_port
) {
4774 s
.out_bundle
= port_lookup(m
->bridge
, cfg
->output_port
->name
);
4775 if (!s
.out_bundle
) {
4776 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
4777 m
->bridge
->name
, m
->name
);
4780 s
.out_vlan
= UINT16_MAX
;
4782 if (cfg
->output_vlan
) {
4783 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
4784 "output vlan; ignoring output vlan",
4785 m
->bridge
->name
, m
->name
);
4787 } else if (cfg
->output_vlan
) {
4788 /* The database should prevent invalid VLAN values. */
4789 s
.out_bundle
= NULL
;
4790 s
.out_vlan
= *cfg
->output_vlan
;
4792 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
4793 m
->bridge
->name
, m
->name
);
4798 s
.snaplen
= *cfg
->snaplen
;
4803 /* Get port selection. */
4804 if (cfg
->select_all
) {
4805 size_t n_ports
= hmap_count(&m
->bridge
->ports
);
4806 void **ports
= xmalloc(n_ports
* sizeof *ports
);
4811 HMAP_FOR_EACH (port
, hmap_node
, &m
->bridge
->ports
) {
4821 /* Get ports, dropping ports that don't exist.
4822 * The IDL ensures that there are no duplicates. */
4823 mirror_collect_ports(m
, cfg
->select_src_port
, cfg
->n_select_src_port
,
4824 &s
.srcs
, &s
.n_srcs
);
4825 mirror_collect_ports(m
, cfg
->select_dst_port
, cfg
->n_select_dst_port
,
4826 &s
.dsts
, &s
.n_dsts
);
4829 /* Get VLAN selection. */
4830 s
.src_vlans
= vlan_bitmap_from_array(cfg
->select_vlan
, cfg
->n_select_vlan
);
4833 ofproto_mirror_register(m
->bridge
->ofproto
, m
, &s
);
4836 if (s
.srcs
!= s
.dsts
) {
4847 mirror_refresh_stats(struct mirror
*m
)
4849 struct ofproto
*ofproto
= m
->bridge
->ofproto
;
4850 uint64_t tx_packets
, tx_bytes
;
4851 const char *keys
[2];
4853 size_t stat_cnt
= 0;
4855 if (ofproto_mirror_get_stats(ofproto
, m
, &tx_packets
, &tx_bytes
)) {
4856 ovsrec_mirror_set_statistics(m
->cfg
, NULL
, NULL
, 0);
4860 if (tx_packets
!= UINT64_MAX
) {
4861 keys
[stat_cnt
] = "tx_packets";
4862 values
[stat_cnt
] = tx_packets
;
4865 if (tx_bytes
!= UINT64_MAX
) {
4866 keys
[stat_cnt
] = "tx_bytes";
4867 values
[stat_cnt
] = tx_bytes
;
4871 ovsrec_mirror_set_statistics(m
->cfg
, keys
, values
, stat_cnt
);
4875 * Add registered netdev and dpif types to ovsdb to allow external
4876 * applications to query the capabilities of the Open vSwitch instance
4877 * running on the node.
4880 discover_types(const struct ovsrec_open_vswitch
*cfg
)
4884 /* Datapath types. */
4886 dp_enumerate_types(&types
);
4887 const char **datapath_types
= sset_array(&types
);
4888 ovsrec_open_vswitch_set_datapath_types(cfg
, datapath_types
,
4889 sset_count(&types
));
4890 free(datapath_types
);
4891 sset_destroy(&types
);
4895 netdev_enumerate_types(&types
);
4896 const char **iface_types
= sset_array(&types
);
4897 ovsrec_open_vswitch_set_iface_types(cfg
, iface_types
, sset_count(&types
));
4899 sset_destroy(&types
);