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"
41 #include "netdev-offload.h"
43 #include "ofproto/bond.h"
44 #include "ofproto/ofproto.h"
45 #include "openvswitch/dynamic-string.h"
46 #include "openvswitch/list.h"
47 #include "openvswitch/meta-flow.h"
48 #include "openvswitch/ofp-print.h"
49 #include "openvswitch/ofpbuf.h"
50 #include "openvswitch/vconn.h"
51 #include "openvswitch/vlog.h"
55 #include "openvswitch/poll-loop.h"
57 #include "sflow_api.h"
59 #include "openvswitch/shash.h"
61 #include "socket-util.h"
63 #include "stream-ssl.h"
65 #include "system-stats.h"
67 #include "tnl-ports.h"
70 #include "lib/vswitch-idl.h"
71 #include "xenserver.h"
72 #include "vlan-bitmap.h"
74 VLOG_DEFINE_THIS_MODULE(bridge
);
76 COVERAGE_DEFINE(bridge_reconfigure
);
79 /* These members are always valid.
81 * They are immutable: they never change between iface_create() and
83 struct ovs_list port_elem
; /* Element in struct port's "ifaces" list. */
84 struct hmap_node name_node
; /* In struct bridge's "iface_by_name" hmap. */
85 struct hmap_node ofp_port_node
; /* In struct bridge's "ifaces" hmap. */
86 struct port
*port
; /* Containing port. */
87 char *name
; /* Host network device name. */
88 struct netdev
*netdev
; /* Network device. */
89 ofp_port_t ofp_port
; /* OpenFlow port number. */
92 /* These members are valid only within bridge_reconfigure(). */
93 const char *type
; /* Usually same as cfg->type. */
94 const struct ovsrec_interface
*cfg
;
98 struct uuid uuid
; /* UUID of this "mirror" record in database. */
99 struct hmap_node hmap_node
; /* In struct bridge's "mirrors" hmap. */
100 struct bridge
*bridge
;
102 const struct ovsrec_mirror
*cfg
;
106 struct hmap_node hmap_node
; /* Element in struct bridge's "ports" hmap. */
107 struct bridge
*bridge
;
110 const struct ovsrec_port
*cfg
;
112 /* An ordinary bridge port has 1 interface.
113 * A bridge port for bonding has at least 2 interfaces. */
114 struct ovs_list ifaces
; /* List of "struct iface"s. */
118 struct hmap_node node
; /* In 'all_bridges'. */
119 char *name
; /* User-specified arbitrary name. */
120 char *type
; /* Datapath type. */
121 struct eth_addr ea
; /* Bridge Ethernet Address. */
122 struct eth_addr default_ea
; /* Default MAC. */
123 const struct ovsrec_bridge
*cfg
;
125 /* OpenFlow switch processing. */
126 struct ofproto
*ofproto
; /* OpenFlow switch. */
129 struct hmap ports
; /* "struct port"s indexed by name. */
130 struct hmap ifaces
; /* "struct iface"s indexed by ofp_port. */
131 struct hmap iface_by_name
; /* "struct iface"s indexed by name. */
133 /* Port mirroring. */
134 struct hmap mirrors
; /* "struct mirror" indexed by UUID. */
137 struct hmap mappings
; /* "struct" indexed by UUID */
139 /* Used during reconfiguration. */
140 struct shash wanted_ports
;
142 /* Synthetic local port if necessary. */
143 struct ovsrec_port synth_local_port
;
144 struct ovsrec_interface synth_local_iface
;
145 struct ovsrec_interface
*synth_local_ifacep
;
149 struct hmap_node hmap_node
; /* In struct bridge's "mappings" hmap. */
150 struct bridge
*bridge
;
156 /* Internal representation of conntrack zone configuration table in OVSDB. */
159 struct simap tp
; /* A map from timeout policy attribute to
161 struct hmap_node node
; /* Node in 'struct datapath' 'ct_zones'
163 unsigned int last_used
; /* The last idl_seqno that this 'ct_zone' used
164 * in OVSDB. This number is used for garbage
168 /* Internal representation of datapath configuration table in OVSDB. */
170 char *type
; /* Datapath type. */
171 struct hmap ct_zones
; /* Map of 'struct ct_zone' elements, indexed
173 struct hmap_node node
; /* Node in 'all_datapaths' hmap. */
174 struct smap caps
; /* Capabilities. */
175 unsigned int last_used
; /* The last idl_seqno that this 'datapath'
176 * used in OVSDB. This number is used for
177 * garbage collection. */
180 /* All bridges, indexed by name. */
181 static struct hmap all_bridges
= HMAP_INITIALIZER(&all_bridges
);
183 /* All datapath configuartions, indexed by type. */
184 static struct hmap all_datapaths
= HMAP_INITIALIZER(&all_datapaths
);
186 /* OVSDB IDL used to obtain configuration. */
187 static struct ovsdb_idl
*idl
;
189 /* We want to complete daemonization, fully detaching from our parent process,
190 * only after we have completed our initial configuration, committed our state
191 * to the database, and received confirmation back from the database server
192 * that it applied the commit. This allows our parent process to know that,
193 * post-detach, ephemeral fields such as datapath-id and ofport are very likely
194 * to have already been filled in. (It is only "very likely" rather than
195 * certain because there is always a slim possibility that the transaction will
196 * fail or that some other client has added new bridges, ports, etc. while
197 * ovs-vswitchd was configuring using an old configuration.)
199 * We only need to do this once for our initial configuration at startup, so
200 * 'initial_config_done' tracks whether we've already done it. While we are
201 * waiting for a response to our commit, 'daemonize_txn' tracks the transaction
202 * itself and is otherwise NULL. */
203 static bool initial_config_done
;
204 static struct ovsdb_idl_txn
*daemonize_txn
;
206 /* Most recently processed IDL sequence number. */
207 static unsigned int idl_seqno
;
209 /* Track changes to port connectivity. */
210 static uint64_t connectivity_seqno
= LLONG_MIN
;
212 /* Status update to database.
214 * Some information in the database must be kept as up-to-date as possible to
215 * allow controllers to respond rapidly to network outages. Those status are
216 * updated via the 'status_txn'.
218 * We use the global connectivity sequence number to detect the status change.
219 * Also, to prevent the status update from sending too much to the database,
220 * we check the return status of each update transaction and do not start new
221 * update if the previous transaction status is 'TXN_INCOMPLETE'.
223 * 'statux_txn' is NULL if there is no ongoing status update.
225 * If the previous database transaction was failed (is not 'TXN_SUCCESS',
226 * 'TXN_UNCHANGED' or 'TXN_INCOMPLETE'), 'status_txn_try_again' is set to true,
227 * which will cause the main thread wake up soon and retry the status update.
229 static struct ovsdb_idl_txn
*status_txn
;
230 static bool status_txn_try_again
;
232 /* When the status update transaction returns 'TXN_INCOMPLETE', should register a
233 * timeout in 'STATUS_CHECK_AGAIN_MSEC' to check again. */
234 #define STATUS_CHECK_AGAIN_MSEC 100
236 /* Statistics update to database. */
237 static struct ovsdb_idl_txn
*stats_txn
;
239 /* Each time this timer expires, the bridge fetches interface and mirror
240 * statistics and pushes them into the database. */
241 static int stats_timer_interval
;
242 static long long int stats_timer
= LLONG_MIN
;
244 /* Each time this timer expires, the bridge fetches the list of port/VLAN
245 * membership that has been modified by the AA.
247 #define AA_REFRESH_INTERVAL (1000) /* In milliseconds. */
248 static long long int aa_refresh_timer
= LLONG_MIN
;
250 /* Whenever system interfaces are added, removed or change state, the bridge
251 * will be reconfigured.
253 static struct if_notifier
*ifnotifier
;
254 static struct seq
*ifaces_changed
;
255 static uint64_t last_ifaces_changed
;
257 /* Default/min/max packet-in queue sizes towards the controllers. */
258 #define BRIDGE_CONTROLLER_PACKET_QUEUE_DEFAULT_SIZE 100
259 #define BRIDGE_CONTROLLER_PACKET_QUEUE_MIN_SIZE 1
260 #define BRIDGE_CONTROLLER_PACKET_QUEUE_MAX_SIZE 512
262 static void add_del_bridges(const struct ovsrec_open_vswitch
*);
263 static void bridge_run__(void);
264 static void bridge_create(const struct ovsrec_bridge
*);
265 static void bridge_destroy(struct bridge
*, bool del
);
266 static struct bridge
*bridge_lookup(const char *name
);
267 static unixctl_cb_func bridge_unixctl_dump_flows
;
268 static unixctl_cb_func bridge_unixctl_reconnect
;
269 static size_t bridge_get_controllers(const struct bridge
*br
,
270 struct ovsrec_controller
***controllersp
);
271 static void bridge_collect_wanted_ports(struct bridge
*,
272 struct shash
*wanted_ports
);
273 static void bridge_delete_ofprotos(void);
274 static void bridge_delete_or_reconfigure_ports(struct bridge
*);
275 static void bridge_del_ports(struct bridge
*,
276 const struct shash
*wanted_ports
);
277 static void bridge_add_ports(struct bridge
*,
278 const struct shash
*wanted_ports
);
280 static void bridge_configure_datapath_id(struct bridge
*);
281 static void bridge_configure_netflow(struct bridge
*);
282 static void bridge_configure_forward_bpdu(struct bridge
*);
283 static void bridge_configure_mac_table(struct bridge
*);
284 static void bridge_configure_mcast_snooping(struct bridge
*);
285 static void bridge_configure_sflow(struct bridge
*, int *sflow_bridge_number
);
286 static void bridge_configure_ipfix(struct bridge
*);
287 static void bridge_configure_spanning_tree(struct bridge
*);
288 static void bridge_configure_tables(struct bridge
*);
289 static void bridge_configure_dp_desc(struct bridge
*);
290 static void bridge_configure_aa(struct bridge
*);
291 static void bridge_aa_refresh_queued(struct bridge
*);
292 static bool bridge_aa_need_refresh(struct bridge
*);
293 static void bridge_configure_remotes(struct bridge
*,
294 const struct sockaddr_in
*managers
,
296 static void bridge_pick_local_hw_addr(struct bridge
*, struct eth_addr
*ea
,
297 struct iface
**hw_addr_iface
);
298 static uint64_t bridge_pick_datapath_id(struct bridge
*,
299 const struct eth_addr bridge_ea
,
300 struct iface
*hw_addr_iface
);
301 static uint64_t dpid_from_hash(const void *, size_t nbytes
);
302 static bool bridge_has_bond_fake_iface(const struct bridge
*,
304 static bool port_is_bond_fake_iface(const struct port
*);
306 static void datapath_destroy(struct datapath
*dp
);
308 static unixctl_cb_func qos_unixctl_show_types
;
309 static unixctl_cb_func qos_unixctl_show
;
311 static struct port
*port_create(struct bridge
*, const struct ovsrec_port
*);
312 static void port_del_ifaces(struct port
*);
313 static void port_destroy(struct port
*);
314 static struct port
*port_lookup(const struct bridge
*, const char *name
);
315 static void port_configure(struct port
*);
316 static struct lacp_settings
*port_configure_lacp(struct port
*,
317 struct lacp_settings
*);
318 static void port_configure_bond(struct port
*, struct bond_settings
*);
319 static bool port_is_synthetic(const struct port
*);
321 static void reconfigure_system_stats(const struct ovsrec_open_vswitch
*);
322 static void run_system_stats(void);
324 static void bridge_configure_mirrors(struct bridge
*);
325 static struct mirror
*mirror_create(struct bridge
*,
326 const struct ovsrec_mirror
*);
327 static void mirror_destroy(struct mirror
*);
328 static bool mirror_configure(struct mirror
*);
329 static void mirror_refresh_stats(struct mirror
*);
331 static void iface_configure_lacp(struct iface
*, struct lacp_slave_settings
*);
332 static bool iface_create(struct bridge
*, const struct ovsrec_interface
*,
333 const struct ovsrec_port
*);
334 static bool iface_is_internal(const struct ovsrec_interface
*iface
,
335 const struct ovsrec_bridge
*br
);
336 static const char *iface_get_type(const struct ovsrec_interface
*,
337 const struct ovsrec_bridge
*);
338 static void iface_destroy(struct iface
*);
339 static void iface_destroy__(struct iface
*);
340 static struct iface
*iface_lookup(const struct bridge
*, const char *name
);
341 static struct iface
*iface_find(const char *name
);
342 static struct iface
*iface_from_ofp_port(const struct bridge
*,
343 ofp_port_t ofp_port
);
344 static void iface_set_mac(const struct bridge
*, const struct port
*, struct iface
*);
345 static void iface_set_ofport(const struct ovsrec_interface
*, ofp_port_t ofport
);
346 static void iface_clear_db_record(const struct ovsrec_interface
*if_cfg
, char *errp
);
347 static void iface_configure_qos(struct iface
*, const struct ovsrec_qos
*);
348 static void iface_configure_cfm(struct iface
*);
349 static void iface_refresh_cfm_stats(struct iface
*);
350 static void iface_refresh_stats(struct iface
*);
351 static void iface_refresh_netdev_status(struct iface
*);
352 static void iface_refresh_ofproto_status(struct iface
*);
353 static bool iface_is_synthetic(const struct iface
*);
354 static ofp_port_t
iface_get_requested_ofp_port(
355 const struct ovsrec_interface
*);
356 static ofp_port_t
iface_pick_ofport(const struct ovsrec_interface
*);
359 static void discover_types(const struct ovsrec_open_vswitch
*cfg
);
362 bridge_init_ofproto(const struct ovsrec_open_vswitch
*cfg
)
364 struct shash iface_hints
;
365 static bool initialized
= false;
372 shash_init(&iface_hints
);
375 for (i
= 0; i
< cfg
->n_bridges
; i
++) {
376 const struct ovsrec_bridge
*br_cfg
= cfg
->bridges
[i
];
379 for (j
= 0; j
< br_cfg
->n_ports
; j
++) {
380 struct ovsrec_port
*port_cfg
= br_cfg
->ports
[j
];
383 for (k
= 0; k
< port_cfg
->n_interfaces
; k
++) {
384 struct ovsrec_interface
*if_cfg
= port_cfg
->interfaces
[k
];
385 struct iface_hint
*iface_hint
;
387 iface_hint
= xmalloc(sizeof *iface_hint
);
388 iface_hint
->br_name
= br_cfg
->name
;
389 iface_hint
->br_type
= br_cfg
->datapath_type
;
390 iface_hint
->ofp_port
= iface_pick_ofport(if_cfg
);
392 shash_add(&iface_hints
, if_cfg
->name
, iface_hint
);
398 ofproto_init(&iface_hints
);
400 shash_destroy_free_data(&iface_hints
);
405 if_change_cb(void *aux OVS_UNUSED
)
407 seq_change(ifaces_changed
);
411 if_notifier_changed(struct if_notifier
*notifier OVS_UNUSED
)
414 bool changed
= false;
415 new_seq
= seq_read(ifaces_changed
);
416 if (new_seq
!= last_ifaces_changed
) {
418 last_ifaces_changed
= new_seq
;
420 seq_wait(ifaces_changed
, last_ifaces_changed
);
424 /* Public functions. */
426 /* Initializes the bridge module, configuring it to obtain its configuration
427 * from an OVSDB server accessed over 'remote', which should be a string in a
428 * form acceptable to ovsdb_idl_create(). */
430 bridge_init(const char *remote
)
432 /* Create connection to database. */
433 idl
= ovsdb_idl_create(remote
, &ovsrec_idl_class
, true, true);
434 idl_seqno
= ovsdb_idl_get_seqno(idl
);
435 ovsdb_idl_set_lock(idl
, "ovs_vswitchd");
436 ovsdb_idl_verify_write_only(idl
);
438 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_cur_cfg
);
439 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_statistics
);
440 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_datapath_types
);
441 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_iface_types
);
442 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_external_ids
);
443 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_ovs_version
);
444 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_db_version
);
445 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_system_type
);
446 ovsdb_idl_omit(idl
, &ovsrec_open_vswitch_col_system_version
);
447 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_dpdk_version
);
448 ovsdb_idl_omit_alert(idl
, &ovsrec_open_vswitch_col_dpdk_initialized
);
450 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_datapath_id
);
451 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_datapath_version
);
452 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_status
);
453 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_rstp_status
);
454 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_stp_enable
);
455 ovsdb_idl_omit_alert(idl
, &ovsrec_bridge_col_rstp_enable
);
456 ovsdb_idl_omit(idl
, &ovsrec_bridge_col_external_ids
);
458 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_status
);
459 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_rstp_status
);
460 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_rstp_statistics
);
461 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_statistics
);
462 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_bond_active_slave
);
463 ovsdb_idl_omit(idl
, &ovsrec_port_col_external_ids
);
464 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_trunks
);
465 ovsdb_idl_omit_alert(idl
, &ovsrec_port_col_vlan_mode
);
466 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_admin_state
);
467 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_duplex
);
468 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_link_speed
);
469 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_link_state
);
470 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_link_resets
);
471 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_mac_in_use
);
472 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_ifindex
);
473 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_mtu
);
474 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_ofport
);
475 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_statistics
);
476 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_status
);
477 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_fault
);
478 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_fault_status
);
479 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_remote_mpids
);
480 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_flap_count
);
481 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_health
);
482 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_cfm_remote_opstate
);
483 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_bfd_status
);
484 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_lacp_current
);
485 ovsdb_idl_omit_alert(idl
, &ovsrec_interface_col_error
);
486 ovsdb_idl_omit(idl
, &ovsrec_interface_col_external_ids
);
488 ovsdb_idl_omit_alert(idl
, &ovsrec_controller_col_is_connected
);
489 ovsdb_idl_omit_alert(idl
, &ovsrec_controller_col_role
);
490 ovsdb_idl_omit_alert(idl
, &ovsrec_controller_col_status
);
491 ovsdb_idl_omit(idl
, &ovsrec_controller_col_external_ids
);
493 ovsdb_idl_omit(idl
, &ovsrec_qos_col_external_ids
);
495 ovsdb_idl_omit(idl
, &ovsrec_queue_col_external_ids
);
497 ovsdb_idl_omit(idl
, &ovsrec_mirror_col_external_ids
);
498 ovsdb_idl_omit_alert(idl
, &ovsrec_mirror_col_statistics
);
500 ovsdb_idl_omit(idl
, &ovsrec_netflow_col_external_ids
);
501 ovsdb_idl_omit(idl
, &ovsrec_sflow_col_external_ids
);
502 ovsdb_idl_omit(idl
, &ovsrec_ipfix_col_external_ids
);
503 ovsdb_idl_omit(idl
, &ovsrec_flow_sample_collector_set_col_external_ids
);
505 ovsdb_idl_omit(idl
, &ovsrec_manager_col_external_ids
);
506 ovsdb_idl_omit(idl
, &ovsrec_manager_col_inactivity_probe
);
507 ovsdb_idl_omit(idl
, &ovsrec_manager_col_is_connected
);
508 ovsdb_idl_omit(idl
, &ovsrec_manager_col_max_backoff
);
509 ovsdb_idl_omit(idl
, &ovsrec_manager_col_status
);
511 ovsdb_idl_omit(idl
, &ovsrec_ssl_col_external_ids
);
513 /* Register unixctl commands. */
514 unixctl_command_register("qos/show-types", "interface", 1, 1,
515 qos_unixctl_show_types
, NULL
);
516 unixctl_command_register("qos/show", "interface", 1, 1,
517 qos_unixctl_show
, NULL
);
518 unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
519 bridge_unixctl_dump_flows
, NULL
);
520 unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
521 bridge_unixctl_reconnect
, NULL
);
530 ifaces_changed
= seq_create();
531 last_ifaces_changed
= seq_read(ifaces_changed
);
532 ifnotifier
= if_notifier_create(if_change_cb
, NULL
);
536 bridge_exit(bool delete_datapath
)
538 if_notifier_destroy(ifnotifier
);
539 seq_destroy(ifaces_changed
);
541 struct datapath
*dp
, *next
;
542 HMAP_FOR_EACH_SAFE (dp
, next
, node
, &all_datapaths
) {
543 datapath_destroy(dp
);
546 struct bridge
*br
, *next_br
;
547 HMAP_FOR_EACH_SAFE (br
, next_br
, node
, &all_bridges
) {
548 bridge_destroy(br
, delete_datapath
);
551 ovsdb_idl_destroy(idl
);
554 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
555 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
556 * responsible for freeing '*managersp' (with free()).
558 * You may be asking yourself "why does ovs-vswitchd care?", because
559 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
560 * should not be and in fact is not directly involved in that. But
561 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
562 * it has to tell in-band control where the managers are to enable that.
563 * (Thus, only managers connected in-band and with non-loopback addresses
567 collect_in_band_managers(const struct ovsrec_open_vswitch
*ovs_cfg
,
568 struct sockaddr_in
**managersp
, size_t *n_managersp
)
570 struct sockaddr_in
*managers
= NULL
;
571 size_t n_managers
= 0;
575 /* Collect all of the potential targets from the "targets" columns of the
576 * rows pointed to by "manager_options", excluding any that are
579 for (i
= 0; i
< ovs_cfg
->n_manager_options
; i
++) {
580 struct ovsrec_manager
*m
= ovs_cfg
->manager_options
[i
];
582 if (m
->connection_mode
&& !strcmp(m
->connection_mode
, "out-of-band")) {
583 sset_find_and_delete(&targets
, m
->target
);
585 sset_add(&targets
, m
->target
);
589 /* Now extract the targets' IP addresses. */
590 if (!sset_is_empty(&targets
)) {
593 managers
= xmalloc(sset_count(&targets
) * sizeof *managers
);
594 SSET_FOR_EACH (target
, &targets
) {
596 struct sockaddr_storage ss
;
597 struct sockaddr_in in
;
600 /* Ignore loopback. */
601 if (stream_parse_target_with_default_port(target
, OVSDB_PORT
,
603 && sa
.ss
.ss_family
== AF_INET
604 && sa
.in
.sin_addr
.s_addr
!= htonl(INADDR_LOOPBACK
)) {
605 managers
[n_managers
++] = sa
.in
;
609 sset_destroy(&targets
);
611 *managersp
= managers
;
612 *n_managersp
= n_managers
;
616 config_ofproto_types(const struct smap
*other_config
)
621 /* Pass custom configuration to datapath types. */
623 ofproto_enumerate_types(&types
);
624 SSET_FOR_EACH (type
, &types
) {
625 ofproto_type_set_config(type
, other_config
);
627 sset_destroy(&types
);
631 get_timeout_policy_from_ovsrec(struct simap
*tp
,
632 const struct ovsrec_ct_timeout_policy
*tp_cfg
)
634 for (size_t i
= 0; i
< tp_cfg
->n_timeouts
; i
++) {
635 simap_put(tp
, tp_cfg
->key_timeouts
[i
], tp_cfg
->value_timeouts
[i
]);
639 static struct ct_zone
*
640 ct_zone_lookup(struct hmap
*ct_zones
, uint16_t zone_id
)
642 struct ct_zone
*ct_zone
;
644 HMAP_FOR_EACH_WITH_HASH (ct_zone
, node
, hash_int(zone_id
, 0), ct_zones
) {
645 if (ct_zone
->zone_id
== zone_id
) {
652 static struct ct_zone
*
653 ct_zone_alloc(uint16_t zone_id
, struct ovsrec_ct_timeout_policy
*tp_cfg
)
655 struct ct_zone
*ct_zone
= xzalloc(sizeof *ct_zone
);
657 ct_zone
->zone_id
= zone_id
;
658 simap_init(&ct_zone
->tp
);
659 get_timeout_policy_from_ovsrec(&ct_zone
->tp
, tp_cfg
);
664 ct_zone_remove_and_destroy(struct datapath
*dp
, struct ct_zone
*ct_zone
)
666 hmap_remove(&dp
->ct_zones
, &ct_zone
->node
);
667 simap_destroy(&ct_zone
->tp
);
671 /* Replace 'old_tp' by 'new_tp' (destroyed 'new_tp'). Returns true if 'old_tp'
672 * and 'new_tp' contains different data, false if they are the same. */
674 update_timeout_policy(struct simap
*old_tp
, struct simap
*new_tp
)
676 bool changed
= !simap_equal(old_tp
, new_tp
);
678 simap_swap(old_tp
, new_tp
);
680 simap_destroy(new_tp
);
684 static struct datapath
*
685 datapath_lookup(const char *type
)
689 HMAP_FOR_EACH_WITH_HASH (dp
, node
, hash_string(type
, 0), &all_datapaths
) {
690 if (!strcmp(dp
->type
, type
)) {
697 static struct datapath
*
698 datapath_create(const char *type
)
700 struct datapath
*dp
= xzalloc(sizeof *dp
);
701 dp
->type
= xstrdup(type
);
702 hmap_init(&dp
->ct_zones
);
703 hmap_insert(&all_datapaths
, &dp
->node
, hash_string(type
, 0));
704 smap_init(&dp
->caps
);
709 datapath_destroy(struct datapath
*dp
)
712 struct ct_zone
*ct_zone
, *next
;
713 HMAP_FOR_EACH_SAFE (ct_zone
, next
, node
, &dp
->ct_zones
) {
714 ofproto_ct_del_zone_timeout_policy(dp
->type
, ct_zone
->zone_id
);
715 ct_zone_remove_and_destroy(dp
, ct_zone
);
718 hmap_remove(&all_datapaths
, &dp
->node
);
719 hmap_destroy(&dp
->ct_zones
);
721 smap_destroy(&dp
->caps
);
727 ct_zones_reconfigure(struct datapath
*dp
, struct ovsrec_datapath
*dp_cfg
)
729 struct ct_zone
*ct_zone
, *next
;
731 /* Add new 'ct_zone's or update existing 'ct_zone's based on the database
733 for (size_t i
= 0; i
< dp_cfg
->n_ct_zones
; i
++) {
734 uint16_t zone_id
= dp_cfg
->key_ct_zones
[i
];
735 struct ovsrec_ct_zone
*zone_cfg
= dp_cfg
->value_ct_zones
[i
];
736 struct ovsrec_ct_timeout_policy
*tp_cfg
= zone_cfg
->timeout_policy
;
738 ct_zone
= ct_zone_lookup(&dp
->ct_zones
, zone_id
);
740 struct simap new_tp
= SIMAP_INITIALIZER(&new_tp
);
741 get_timeout_policy_from_ovsrec(&new_tp
, tp_cfg
);
742 if (update_timeout_policy(&ct_zone
->tp
, &new_tp
)) {
743 ofproto_ct_set_zone_timeout_policy(dp
->type
, ct_zone
->zone_id
,
747 ct_zone
= ct_zone_alloc(zone_id
, tp_cfg
);
748 hmap_insert(&dp
->ct_zones
, &ct_zone
->node
, hash_int(zone_id
, 0));
749 ofproto_ct_set_zone_timeout_policy(dp
->type
, ct_zone
->zone_id
,
752 ct_zone
->last_used
= idl_seqno
;
755 /* Purge 'ct_zone's no longer found in the database. */
756 HMAP_FOR_EACH_SAFE (ct_zone
, next
, node
, &dp
->ct_zones
) {
757 if (ct_zone
->last_used
!= idl_seqno
) {
758 ofproto_ct_del_zone_timeout_policy(dp
->type
, ct_zone
->zone_id
);
759 ct_zone_remove_and_destroy(dp
, ct_zone
);
765 dp_capability_reconfigure(struct datapath
*dp
,
766 struct ovsrec_datapath
*dp_cfg
)
768 struct smap_node
*node
;
772 ofproto_get_datapath_cap(dp
->type
, &cap
);
774 SMAP_FOR_EACH (node
, &cap
) {
775 ovsrec_datapath_update_capabilities_setkey(dp_cfg
, node
->key
,
782 datapath_reconfigure(const struct ovsrec_open_vswitch
*cfg
)
784 struct datapath
*dp
, *next
;
786 /* Add new 'datapath's or update existing ones. */
787 for (size_t i
= 0; i
< cfg
->n_datapaths
; i
++) {
788 struct ovsrec_datapath
*dp_cfg
= cfg
->value_datapaths
[i
];
789 char *dp_name
= cfg
->key_datapaths
[i
];
791 dp
= datapath_lookup(dp_name
);
793 dp
= datapath_create(dp_name
);
794 dp_capability_reconfigure(dp
, dp_cfg
);
796 dp
->last_used
= idl_seqno
;
797 ct_zones_reconfigure(dp
, dp_cfg
);
800 /* Purge deleted 'datapath's. */
801 HMAP_FOR_EACH_SAFE (dp
, next
, node
, &all_datapaths
) {
802 if (dp
->last_used
!= idl_seqno
) {
803 datapath_destroy(dp
);
809 bridge_reconfigure(const struct ovsrec_open_vswitch
*ovs_cfg
)
811 struct sockaddr_in
*managers
;
812 struct bridge
*br
, *next
;
813 int sflow_bridge_number
;
816 COVERAGE_INC(bridge_reconfigure
);
818 ofproto_set_flow_limit(smap_get_int(&ovs_cfg
->other_config
, "flow-limit",
819 OFPROTO_FLOW_LIMIT_DEFAULT
));
820 ofproto_set_max_idle(smap_get_int(&ovs_cfg
->other_config
, "max-idle",
821 OFPROTO_MAX_IDLE_DEFAULT
));
822 ofproto_set_max_revalidator(smap_get_int(&ovs_cfg
->other_config
,
824 OFPROTO_MAX_REVALIDATOR_DEFAULT
));
825 ofproto_set_min_revalidate_pps(
826 smap_get_int(&ovs_cfg
->other_config
, "min-revalidate-pps",
827 OFPROTO_MIN_REVALIDATE_PPS_DEFAULT
));
828 ofproto_set_vlan_limit(smap_get_int(&ovs_cfg
->other_config
, "vlan-limit",
829 LEGACY_MAX_VLAN_HEADERS
));
830 ofproto_set_bundle_idle_timeout(smap_get_int(&ovs_cfg
->other_config
,
831 "bundle-idle-timeout", 0));
833 smap_get_int(&ovs_cfg
->other_config
, "n-handler-threads", 0),
834 smap_get_int(&ovs_cfg
->other_config
, "n-revalidator-threads", 0));
836 /* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
837 * to 'ovs_cfg', with only very minimal configuration otherwise.
839 * This is mostly an update to bridge data structures. Nothing is pushed
840 * down to ofproto or lower layers. */
841 add_del_bridges(ovs_cfg
);
842 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
843 bridge_collect_wanted_ports(br
, &br
->wanted_ports
);
844 bridge_del_ports(br
, &br
->wanted_ports
);
847 /* Start pushing configuration changes down to the ofproto layer:
849 * - Delete ofprotos that are no longer configured.
851 * - Delete ports that are no longer configured.
853 * - Reconfigure existing ports to their desired configurations, or
854 * delete them if not possible.
856 * We have to do all the deletions before we can do any additions, because
857 * the ports to be added might require resources that will be freed up by
858 * deletions (they might especially overlap in name). */
859 bridge_delete_ofprotos();
860 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
862 bridge_delete_or_reconfigure_ports(br
);
866 /* Finish pushing configuration changes to the ofproto layer:
868 * - Create ofprotos that are missing.
870 * - Add ports that are missing. */
871 HMAP_FOR_EACH_SAFE (br
, next
, node
, &all_bridges
) {
875 error
= ofproto_create(br
->name
, br
->type
, &br
->ofproto
);
877 VLOG_ERR("failed to create bridge %s: %s", br
->name
,
878 ovs_strerror(error
));
879 shash_destroy(&br
->wanted_ports
);
880 bridge_destroy(br
, true);
882 /* Trigger storing datapath version. */
883 seq_change(connectivity_seq_get());
888 config_ofproto_types(&ovs_cfg
->other_config
);
890 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
891 bridge_add_ports(br
, &br
->wanted_ports
);
892 shash_destroy(&br
->wanted_ports
);
895 reconfigure_system_stats(ovs_cfg
);
896 datapath_reconfigure(ovs_cfg
);
898 /* Complete the configuration. */
899 sflow_bridge_number
= 0;
900 collect_in_band_managers(ovs_cfg
, &managers
, &n_managers
);
901 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
904 /* We need the datapath ID early to allow LACP ports to use it as the
905 * default system ID. */
906 bridge_configure_datapath_id(br
);
908 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
911 port_configure(port
);
913 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
914 iface_set_ofport(iface
->cfg
, iface
->ofp_port
);
915 /* Clear eventual previous errors */
916 ovsrec_interface_set_error(iface
->cfg
, NULL
);
917 iface_configure_cfm(iface
);
918 iface_configure_qos(iface
, port
->cfg
->qos
);
919 iface_set_mac(br
, port
, iface
);
920 ofproto_port_set_bfd(br
->ofproto
, iface
->ofp_port
,
922 ofproto_port_set_lldp(br
->ofproto
, iface
->ofp_port
,
924 ofproto_port_set_config(br
->ofproto
, iface
->ofp_port
,
925 &iface
->cfg
->other_config
);
928 bridge_configure_mirrors(br
);
929 bridge_configure_forward_bpdu(br
);
930 bridge_configure_mac_table(br
);
931 bridge_configure_mcast_snooping(br
);
932 bridge_configure_remotes(br
, managers
, n_managers
);
933 bridge_configure_netflow(br
);
934 bridge_configure_sflow(br
, &sflow_bridge_number
);
935 bridge_configure_ipfix(br
);
936 bridge_configure_spanning_tree(br
);
937 bridge_configure_tables(br
);
938 bridge_configure_dp_desc(br
);
939 bridge_configure_aa(br
);
943 /* The ofproto-dpif provider does some final reconfiguration in its
944 * ->type_run() function. We have to call it before notifying the database
945 * client that reconfiguration is complete, otherwise there is a very
946 * narrow race window in which e.g. ofproto/trace will not recognize the
947 * new configuration (sometimes this causes unit test failures). */
951 /* Delete ofprotos which aren't configured or have the wrong type. Create
952 * ofprotos which don't exist but need to. */
954 bridge_delete_ofprotos(void)
961 /* Delete ofprotos with no bridge or with the wrong type. */
964 ofproto_enumerate_types(&types
);
965 SSET_FOR_EACH (type
, &types
) {
968 ofproto_enumerate_names(type
, &names
);
969 SSET_FOR_EACH (name
, &names
) {
970 br
= bridge_lookup(name
);
971 if (!br
|| strcmp(type
, br
->type
)) {
972 ofproto_delete(name
, type
);
976 sset_destroy(&names
);
977 sset_destroy(&types
);
981 add_ofp_port(ofp_port_t port
, ofp_port_t
*ports
, size_t *n
, size_t *allocated
)
983 if (*n
>= *allocated
) {
984 ports
= x2nrealloc(ports
, allocated
, sizeof *ports
);
986 ports
[(*n
)++] = port
;
990 /* Configures the MTU of 'netdev' based on the "mtu_request" column
993 iface_set_netdev_mtu(const struct ovsrec_interface
*iface_cfg
,
994 struct netdev
*netdev
)
996 if (iface_cfg
->n_mtu_request
== 1) {
997 /* The user explicitly asked for this MTU. */
998 netdev_mtu_user_config(netdev
, true);
999 /* Try to set the MTU to the requested value. */
1000 return netdev_set_mtu(netdev
, *iface_cfg
->mtu_request
);
1003 /* The user didn't explicitly asked for any MTU. */
1004 netdev_mtu_user_config(netdev
, false);
1009 bridge_delete_or_reconfigure_ports(struct bridge
*br
)
1011 struct ofproto_port ofproto_port
;
1012 struct ofproto_port_dump dump
;
1014 struct sset ofproto_ports
;
1015 struct port
*port
, *port_next
;
1017 /* List of "ofp_port"s to delete. We make a list instead of deleting them
1018 * right away because ofproto implementations aren't necessarily able to
1019 * iterate through a changing list of ports in an entirely robust way. */
1021 size_t n
, allocated
;
1026 sset_init(&ofproto_ports
);
1028 /* Main task: Iterate over the ports in 'br->ofproto' and remove the ports
1029 * that are not configured in the database. (This commonly happens when
1030 * ports have been deleted, e.g. with "ovs-vsctl del-port".)
1032 * Side tasks: Reconfigure the ports that are still in 'br'. Delete ports
1033 * that have the wrong OpenFlow port number (and arrange to add them back
1034 * with the correct OpenFlow port number). */
1035 OFPROTO_PORT_FOR_EACH (&ofproto_port
, &dump
, br
->ofproto
) {
1036 ofp_port_t requested_ofp_port
;
1037 struct iface
*iface
;
1039 sset_add(&ofproto_ports
, ofproto_port
.name
);
1041 iface
= iface_lookup(br
, ofproto_port
.name
);
1043 /* No such iface is configured, so we should delete this
1046 * As a corner case exception, keep the port if it's a bond fake
1048 if (bridge_has_bond_fake_iface(br
, ofproto_port
.name
)
1049 && !strcmp(ofproto_port
.type
, "internal")) {
1055 const char *netdev_type
= ofproto_port_open_type(br
->ofproto
,
1057 if (strcmp(ofproto_port
.type
, netdev_type
)
1058 || netdev_set_config(iface
->netdev
, &iface
->cfg
->options
, NULL
)) {
1059 /* The interface is the wrong type or can't be configured.
1064 iface_set_netdev_mtu(iface
->cfg
, iface
->netdev
);
1066 /* If the requested OpenFlow port for 'iface' changed, and it's not
1067 * already the correct port, then we might want to temporarily delete
1068 * this interface, so we can add it back again with the new OpenFlow
1070 requested_ofp_port
= iface_get_requested_ofp_port(iface
->cfg
);
1071 if (iface
->ofp_port
!= OFPP_LOCAL
&&
1072 requested_ofp_port
!= OFPP_NONE
&&
1073 requested_ofp_port
!= iface
->ofp_port
) {
1074 ofp_port_t victim_request
;
1075 struct iface
*victim
;
1077 /* Check for an existing OpenFlow port currently occupying
1078 * 'iface''s requested port number. If there isn't one, then
1079 * delete this port. Otherwise we need to consider further. */
1080 victim
= iface_from_ofp_port(br
, requested_ofp_port
);
1085 /* 'victim' is a port currently using 'iface''s requested port
1086 * number. Unless 'victim' specifically requested that port
1087 * number, too, then we can delete both 'iface' and 'victim'
1088 * temporarily. (We'll add both of them back again later with new
1089 * OpenFlow port numbers.)
1091 * If 'victim' did request port number 'requested_ofp_port', just
1092 * like 'iface', then that's a configuration inconsistency that we
1093 * can't resolve. We might as well let it keep its current port
1095 victim_request
= iface_get_requested_ofp_port(victim
->cfg
);
1096 if (victim_request
!= requested_ofp_port
) {
1097 del
= add_ofp_port(victim
->ofp_port
, del
, &n
, &allocated
);
1098 iface_destroy(victim
);
1107 iface_destroy(iface
);
1108 del
= add_ofp_port(ofproto_port
.ofp_port
, del
, &n
, &allocated
);
1110 for (i
= 0; i
< n
; i
++) {
1111 ofproto_port_del(br
->ofproto
, del
[i
]);
1115 /* Iterate over this module's idea of interfaces in 'br'. Remove any ports
1116 * that we didn't see when we iterated through the datapath, i.e. ports
1117 * that disappeared underneath use. This is an unusual situation, but it
1118 * can happen in some cases:
1120 * - An admin runs a command like "ovs-dpctl del-port" (which is a bad
1121 * idea but could happen).
1123 * - The port represented a device that disappeared, e.g. a tuntap
1124 * device destroyed via "tunctl -d", a physical Ethernet device
1125 * whose module was just unloaded via "rmmod", or a virtual NIC for a
1126 * VM whose VM was just terminated. */
1127 HMAP_FOR_EACH_SAFE (port
, port_next
, hmap_node
, &br
->ports
) {
1128 struct iface
*iface
, *iface_next
;
1130 LIST_FOR_EACH_SAFE (iface
, iface_next
, port_elem
, &port
->ifaces
) {
1131 if (!sset_contains(&ofproto_ports
, iface
->name
)) {
1132 iface_destroy__(iface
);
1136 if (ovs_list_is_empty(&port
->ifaces
)) {
1140 sset_destroy(&ofproto_ports
);
1144 bridge_add_ports__(struct bridge
*br
, const struct shash
*wanted_ports
,
1145 bool with_requested_port
)
1147 struct shash_node
*port_node
;
1149 SHASH_FOR_EACH (port_node
, wanted_ports
) {
1150 const struct ovsrec_port
*port_cfg
= port_node
->data
;
1153 for (i
= 0; i
< port_cfg
->n_interfaces
; i
++) {
1154 const struct ovsrec_interface
*iface_cfg
= port_cfg
->interfaces
[i
];
1155 ofp_port_t requested_ofp_port
;
1157 requested_ofp_port
= iface_get_requested_ofp_port(iface_cfg
);
1158 if ((requested_ofp_port
!= OFPP_NONE
) == with_requested_port
) {
1159 struct iface
*iface
= iface_lookup(br
, iface_cfg
->name
);
1162 iface_create(br
, iface_cfg
, port_cfg
);
1170 bridge_add_ports(struct bridge
*br
, const struct shash
*wanted_ports
)
1172 /* First add interfaces that request a particular port number. */
1173 bridge_add_ports__(br
, wanted_ports
, true);
1175 /* Then add interfaces that want automatic port number assignment.
1176 * We add these afterward to avoid accidentally taking a specifically
1177 * requested port number. */
1178 bridge_add_ports__(br
, wanted_ports
, false);
1182 port_configure(struct port
*port
)
1184 const struct ovsrec_port
*cfg
= port
->cfg
;
1185 struct bond_settings bond_settings
;
1186 struct lacp_settings lacp_settings
;
1187 struct ofproto_bundle_settings s
;
1188 struct iface
*iface
;
1191 s
.name
= port
->name
;
1195 s
.slaves
= xmalloc(ovs_list_size(&port
->ifaces
) * sizeof *s
.slaves
);
1196 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1197 s
.slaves
[s
.n_slaves
++] = iface
->ofp_port
;
1202 if (cfg
->tag
&& *cfg
->tag
>= 0 && *cfg
->tag
<= 4095) {
1206 /* Get VLAN trunks. */
1208 if (cfg
->n_trunks
) {
1209 s
.trunks
= vlan_bitmap_from_array(cfg
->trunks
, cfg
->n_trunks
);
1213 if (cfg
->n_cvlans
) {
1214 s
.cvlans
= vlan_bitmap_from_array(cfg
->cvlans
, cfg
->n_cvlans
);
1217 /* Get VLAN mode. */
1218 if (cfg
->vlan_mode
) {
1219 if (!strcmp(cfg
->vlan_mode
, "access")) {
1220 s
.vlan_mode
= PORT_VLAN_ACCESS
;
1221 } else if (!strcmp(cfg
->vlan_mode
, "trunk")) {
1222 s
.vlan_mode
= PORT_VLAN_TRUNK
;
1223 } else if (!strcmp(cfg
->vlan_mode
, "native-tagged")) {
1224 s
.vlan_mode
= PORT_VLAN_NATIVE_TAGGED
;
1225 } else if (!strcmp(cfg
->vlan_mode
, "native-untagged")) {
1226 s
.vlan_mode
= PORT_VLAN_NATIVE_UNTAGGED
;
1227 } else if (!strcmp(cfg
->vlan_mode
, "dot1q-tunnel")) {
1228 s
.vlan_mode
= PORT_VLAN_DOT1Q_TUNNEL
;
1230 /* This "can't happen" because ovsdb-server should prevent it. */
1231 VLOG_WARN("port %s: unknown VLAN mode %s, falling "
1232 "back to trunk mode", port
->name
, cfg
->vlan_mode
);
1233 s
.vlan_mode
= PORT_VLAN_TRUNK
;
1237 s
.vlan_mode
= PORT_VLAN_ACCESS
;
1238 if (cfg
->n_trunks
|| cfg
->n_cvlans
) {
1239 VLOG_WARN("port %s: ignoring trunks in favor of implicit vlan",
1243 s
.vlan_mode
= PORT_VLAN_TRUNK
;
1247 const char *qe
= smap_get_def(&cfg
->other_config
, "qinq-ethtype", "");
1248 s
.qinq_ethtype
= (!strcmp(qe
, "802.1q")
1249 ? ETH_TYPE_VLAN_8021Q
1250 : ETH_TYPE_VLAN_8021AD
);
1252 const char *pt
= smap_get_def(&cfg
->other_config
, "priority-tags", "");
1253 if (!strcmp(pt
, "if-nonzero") || !strcmp(pt
, "true")) {
1254 s
.use_priority_tags
= PORT_PRIORITY_TAGS_IF_NONZERO
;
1255 } else if (!strcmp(pt
, "always")) {
1256 s
.use_priority_tags
= PORT_PRIORITY_TAGS_ALWAYS
;
1258 s
.use_priority_tags
= PORT_PRIORITY_TAGS_NEVER
;
1261 /* Get LACP settings. */
1262 s
.lacp
= port_configure_lacp(port
, &lacp_settings
);
1266 s
.lacp_slaves
= xmalloc(s
.n_slaves
* sizeof *s
.lacp_slaves
);
1267 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1268 iface_configure_lacp(iface
, &s
.lacp_slaves
[i
++]);
1271 s
.lacp_slaves
= NULL
;
1274 /* Get bond settings. */
1275 if (s
.n_slaves
> 1) {
1276 s
.bond
= &bond_settings
;
1277 port_configure_bond(port
, &bond_settings
);
1280 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1281 netdev_set_miimon_interval(iface
->netdev
, 0);
1285 /* Protected port mode */
1286 s
.protected = cfg
->protected_
;
1289 ofproto_bundle_register(port
->bridge
->ofproto
, port
, &s
);
1295 free(s
.lacp_slaves
);
1298 /* Pick local port hardware address and datapath ID for 'br'. */
1300 bridge_configure_datapath_id(struct bridge
*br
)
1304 struct iface
*local_iface
;
1305 struct iface
*hw_addr_iface
;
1308 bridge_pick_local_hw_addr(br
, &ea
, &hw_addr_iface
);
1309 local_iface
= iface_from_ofp_port(br
, OFPP_LOCAL
);
1311 int error
= netdev_set_etheraddr(local_iface
->netdev
, ea
);
1313 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1314 VLOG_ERR_RL(&rl
, "bridge %s: failed to set bridge "
1315 "Ethernet address: %s",
1316 br
->name
, ovs_strerror(error
));
1321 dpid
= bridge_pick_datapath_id(br
, ea
, hw_addr_iface
);
1322 if (dpid
!= ofproto_get_datapath_id(br
->ofproto
)) {
1323 VLOG_INFO("bridge %s: using datapath ID %016"PRIx64
, br
->name
, dpid
);
1324 ofproto_set_datapath_id(br
->ofproto
, dpid
);
1327 dpid_string
= xasprintf("%016"PRIx64
, dpid
);
1328 ovsrec_bridge_set_datapath_id(br
->cfg
, dpid_string
);
1332 /* Returns a bitmap of "enum ofputil_protocol"s that are allowed for use with
1335 bridge_get_allowed_versions(struct bridge
*br
)
1337 if (!br
->cfg
->n_protocols
) {
1341 return ofputil_versions_from_strings(br
->cfg
->protocols
,
1342 br
->cfg
->n_protocols
);
1346 bridge_get_controller_queue_size(struct bridge
*br
,
1347 struct ovsrec_controller
*c
)
1349 if (c
&& c
->controller_queue_size
) {
1350 return *c
->controller_queue_size
;
1353 int queue_size
= smap_get_int(&br
->cfg
->other_config
,
1354 "controller-queue-size",
1355 BRIDGE_CONTROLLER_PACKET_QUEUE_DEFAULT_SIZE
);
1356 if (queue_size
< BRIDGE_CONTROLLER_PACKET_QUEUE_MIN_SIZE
||
1357 queue_size
> BRIDGE_CONTROLLER_PACKET_QUEUE_MAX_SIZE
) {
1358 return BRIDGE_CONTROLLER_PACKET_QUEUE_DEFAULT_SIZE
;
1364 /* Set NetFlow configuration on 'br'. */
1366 bridge_configure_netflow(struct bridge
*br
)
1368 struct ovsrec_netflow
*cfg
= br
->cfg
->netflow
;
1369 struct netflow_options opts
;
1372 ofproto_set_netflow(br
->ofproto
, NULL
);
1376 memset(&opts
, 0, sizeof opts
);
1378 /* Get default NetFlow configuration from datapath.
1379 * Apply overrides from 'cfg'. */
1380 ofproto_get_netflow_ids(br
->ofproto
, &opts
.engine_type
, &opts
.engine_id
);
1381 if (cfg
->engine_type
) {
1382 opts
.engine_type
= *cfg
->engine_type
;
1384 if (cfg
->engine_id
) {
1385 opts
.engine_id
= *cfg
->engine_id
;
1388 /* Configure active timeout interval. */
1389 opts
.active_timeout
= cfg
->active_timeout
;
1390 if (!opts
.active_timeout
) {
1391 opts
.active_timeout
= -1;
1392 } else if (opts
.active_timeout
< 0) {
1393 VLOG_WARN("bridge %s: active timeout interval set to negative "
1394 "value, using default instead (%d seconds)", br
->name
,
1395 NF_ACTIVE_TIMEOUT_DEFAULT
);
1396 opts
.active_timeout
= -1;
1399 /* Add engine ID to interface number to disambiguate bridgs? */
1400 opts
.add_id_to_iface
= cfg
->add_id_to_interface
;
1401 if (opts
.add_id_to_iface
) {
1402 if (opts
.engine_id
> 0x7f) {
1403 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
1404 "another vswitch, choose an engine id less than 128",
1407 if (hmap_count(&br
->ports
) > 508) {
1408 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
1409 "another port when more than 508 ports are used",
1415 sset_init(&opts
.collectors
);
1416 sset_add_array(&opts
.collectors
, cfg
->targets
, cfg
->n_targets
);
1419 if (ofproto_set_netflow(br
->ofproto
, &opts
)) {
1420 VLOG_ERR("bridge %s: problem setting netflow collectors", br
->name
);
1422 sset_destroy(&opts
.collectors
);
1425 /* Set sFlow configuration on 'br'. */
1427 bridge_configure_sflow(struct bridge
*br
, int *sflow_bridge_number
)
1429 const struct ovsrec_sflow
*cfg
= br
->cfg
->sflow
;
1430 struct ovsrec_controller
**controllers
;
1431 struct ofproto_sflow_options oso
;
1432 size_t n_controllers
;
1436 ofproto_set_sflow(br
->ofproto
, NULL
);
1440 memset(&oso
, 0, sizeof oso
);
1442 sset_init(&oso
.targets
);
1443 sset_add_array(&oso
.targets
, cfg
->targets
, cfg
->n_targets
);
1445 oso
.sampling_rate
= SFL_DEFAULT_SAMPLING_RATE
;
1446 if (cfg
->sampling
) {
1447 oso
.sampling_rate
= *cfg
->sampling
;
1450 oso
.polling_interval
= SFL_DEFAULT_POLLING_INTERVAL
;
1452 oso
.polling_interval
= *cfg
->polling
;
1455 oso
.header_len
= SFL_DEFAULT_HEADER_SIZE
;
1457 oso
.header_len
= *cfg
->header
;
1460 oso
.sub_id
= (*sflow_bridge_number
)++;
1461 oso
.agent_device
= cfg
->agent
;
1463 oso
.control_ip
= NULL
;
1464 n_controllers
= bridge_get_controllers(br
, &controllers
);
1465 for (i
= 0; i
< n_controllers
; i
++) {
1466 if (controllers
[i
]->local_ip
) {
1467 oso
.control_ip
= controllers
[i
]->local_ip
;
1471 ofproto_set_sflow(br
->ofproto
, &oso
);
1473 sset_destroy(&oso
.targets
);
1476 /* Returns whether a IPFIX row is valid. */
1478 ovsrec_ipfix_is_valid(const struct ovsrec_ipfix
*ipfix
)
1480 return ipfix
&& ipfix
->n_targets
> 0;
1483 /* Returns whether a Flow_Sample_Collector_Set row is valid. */
1485 ovsrec_fscs_is_valid(const struct ovsrec_flow_sample_collector_set
*fscs
,
1486 const struct bridge
*br
)
1488 return ovsrec_ipfix_is_valid(fscs
->ipfix
) && fscs
->bridge
== br
->cfg
;
1491 /* Set IPFIX configuration on 'br'. */
1493 bridge_configure_ipfix(struct bridge
*br
)
1495 const struct ovsrec_ipfix
*be_cfg
= br
->cfg
->ipfix
;
1496 bool valid_be_cfg
= ovsrec_ipfix_is_valid(be_cfg
);
1497 const struct ovsrec_flow_sample_collector_set
*fe_cfg
;
1498 struct ofproto_ipfix_bridge_exporter_options be_opts
;
1499 struct ofproto_ipfix_flow_exporter_options
*fe_opts
= NULL
;
1500 size_t n_fe_opts
= 0;
1501 const char *virtual_obs_id
;
1503 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg
, idl
) {
1504 if (ovsrec_fscs_is_valid(fe_cfg
, br
)) {
1509 if (!valid_be_cfg
&& n_fe_opts
== 0) {
1510 ofproto_set_ipfix(br
->ofproto
, NULL
, NULL
, 0);
1515 memset(&be_opts
, 0, sizeof be_opts
);
1517 sset_init(&be_opts
.targets
);
1518 sset_add_array(&be_opts
.targets
, be_cfg
->targets
, be_cfg
->n_targets
);
1520 if (be_cfg
->sampling
) {
1521 be_opts
.sampling_rate
= *be_cfg
->sampling
;
1523 be_opts
.sampling_rate
= SFL_DEFAULT_SAMPLING_RATE
;
1525 if (be_cfg
->obs_domain_id
) {
1526 be_opts
.obs_domain_id
= *be_cfg
->obs_domain_id
;
1528 if (be_cfg
->obs_point_id
) {
1529 be_opts
.obs_point_id
= *be_cfg
->obs_point_id
;
1531 if (be_cfg
->cache_active_timeout
) {
1532 be_opts
.cache_active_timeout
= *be_cfg
->cache_active_timeout
;
1534 if (be_cfg
->cache_max_flows
) {
1535 be_opts
.cache_max_flows
= *be_cfg
->cache_max_flows
;
1538 be_opts
.enable_tunnel_sampling
= smap_get_bool(&be_cfg
->other_config
,
1539 "enable-tunnel-sampling", true);
1541 be_opts
.enable_input_sampling
= !smap_get_bool(&be_cfg
->other_config
,
1542 "enable-input-sampling", false);
1544 be_opts
.enable_output_sampling
= !smap_get_bool(&be_cfg
->other_config
,
1545 "enable-output-sampling", false);
1547 virtual_obs_id
= smap_get(&be_cfg
->other_config
, "virtual_obs_id");
1548 be_opts
.virtual_obs_id
= nullable_xstrdup(virtual_obs_id
);
1551 if (n_fe_opts
> 0) {
1552 struct ofproto_ipfix_flow_exporter_options
*opts
;
1553 fe_opts
= xcalloc(n_fe_opts
, sizeof *fe_opts
);
1555 OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg
, idl
) {
1556 if (ovsrec_fscs_is_valid(fe_cfg
, br
)) {
1557 opts
->collector_set_id
= fe_cfg
->id
;
1558 sset_init(&opts
->targets
);
1559 sset_add_array(&opts
->targets
, fe_cfg
->ipfix
->targets
,
1560 fe_cfg
->ipfix
->n_targets
);
1561 opts
->cache_active_timeout
= fe_cfg
->ipfix
->cache_active_timeout
1562 ? *fe_cfg
->ipfix
->cache_active_timeout
: 0;
1563 opts
->cache_max_flows
= fe_cfg
->ipfix
->cache_max_flows
1564 ? *fe_cfg
->ipfix
->cache_max_flows
: 0;
1565 opts
->enable_tunnel_sampling
= smap_get_bool(
1566 &fe_cfg
->ipfix
->other_config
,
1567 "enable-tunnel-sampling", true);
1568 virtual_obs_id
= smap_get(&fe_cfg
->ipfix
->other_config
,
1570 opts
->virtual_obs_id
= nullable_xstrdup(virtual_obs_id
);
1576 ofproto_set_ipfix(br
->ofproto
, valid_be_cfg
? &be_opts
: NULL
, fe_opts
,
1580 sset_destroy(&be_opts
.targets
);
1581 free(be_opts
.virtual_obs_id
);
1584 if (n_fe_opts
> 0) {
1585 struct ofproto_ipfix_flow_exporter_options
*opts
= fe_opts
;
1587 for (i
= 0; i
< n_fe_opts
; i
++) {
1588 sset_destroy(&opts
->targets
);
1589 free(opts
->virtual_obs_id
);
1597 port_configure_stp(const struct ofproto
*ofproto
, struct port
*port
,
1598 struct ofproto_port_stp_settings
*port_s
,
1599 int *port_num_counter
, unsigned long *port_num_bitmap
)
1601 const char *config_str
;
1602 struct iface
*iface
;
1604 if (!smap_get_bool(&port
->cfg
->other_config
, "stp-enable", true)) {
1605 port_s
->enable
= false;
1608 port_s
->enable
= true;
1611 /* STP over bonds is not supported. */
1612 if (!ovs_list_is_singleton(&port
->ifaces
)) {
1613 VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
1615 port_s
->enable
= false;
1619 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
1621 /* Internal ports shouldn't participate in spanning tree, so
1623 if (!strcmp(iface
->type
, "internal")) {
1624 VLOG_DBG("port %s: disable STP on internal ports", port
->name
);
1625 port_s
->enable
= false;
1629 /* STP on mirror output ports is not supported. */
1630 if (ofproto_is_mirror_output_bundle(ofproto
, port
)) {
1631 VLOG_DBG("port %s: disable STP on mirror ports", port
->name
);
1632 port_s
->enable
= false;
1636 config_str
= smap_get(&port
->cfg
->other_config
, "stp-port-num");
1638 unsigned long int port_num
= strtoul(config_str
, NULL
, 0);
1639 int port_idx
= port_num
- 1;
1641 if (port_num
< 1 || port_num
> STP_MAX_PORTS
) {
1642 VLOG_ERR("port %s: invalid stp-port-num", port
->name
);
1643 port_s
->enable
= false;
1647 if (bitmap_is_set(port_num_bitmap
, port_idx
)) {
1648 VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
1649 port
->name
, port_num
);
1650 port_s
->enable
= false;
1653 bitmap_set1(port_num_bitmap
, port_idx
);
1654 port_s
->port_num
= port_idx
;
1656 if (*port_num_counter
>= STP_MAX_PORTS
) {
1657 VLOG_ERR("port %s: too many STP ports, disabling", port
->name
);
1658 port_s
->enable
= false;
1662 port_s
->port_num
= (*port_num_counter
)++;
1665 config_str
= smap_get(&port
->cfg
->other_config
, "stp-path-cost");
1667 port_s
->path_cost
= strtoul(config_str
, NULL
, 10);
1669 enum netdev_features current
;
1672 netdev_get_features(iface
->netdev
, ¤t
, NULL
, NULL
, NULL
);
1673 mbps
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 1000000;
1674 port_s
->path_cost
= stp_convert_speed_to_cost(mbps
);
1677 config_str
= smap_get(&port
->cfg
->other_config
, "stp-port-priority");
1679 port_s
->priority
= strtoul(config_str
, NULL
, 0);
1681 port_s
->priority
= STP_DEFAULT_PORT_PRIORITY
;
1686 port_configure_rstp(const struct ofproto
*ofproto
, struct port
*port
,
1687 struct ofproto_port_rstp_settings
*port_s
, int *port_num_counter
)
1689 const char *config_str
;
1690 struct iface
*iface
;
1692 if (!smap_get_bool(&port
->cfg
->other_config
, "rstp-enable", true)) {
1693 port_s
->enable
= false;
1696 port_s
->enable
= true;
1699 /* RSTP over bonds is not supported. */
1700 if (!ovs_list_is_singleton(&port
->ifaces
)) {
1701 VLOG_ERR("port %s: cannot enable RSTP on bonds, disabling",
1703 port_s
->enable
= false;
1707 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
1709 /* Internal ports shouldn't participate in spanning tree, so
1711 if (!strcmp(iface
->type
, "internal")) {
1712 VLOG_DBG("port %s: disable RSTP on internal ports", port
->name
);
1713 port_s
->enable
= false;
1717 /* RSTP on mirror output ports is not supported. */
1718 if (ofproto_is_mirror_output_bundle(ofproto
, port
)) {
1719 VLOG_DBG("port %s: disable RSTP on mirror ports", port
->name
);
1720 port_s
->enable
= false;
1724 config_str
= smap_get(&port
->cfg
->other_config
, "rstp-port-num");
1726 unsigned long int port_num
= strtoul(config_str
, NULL
, 0);
1727 if (port_num
< 1 || port_num
> RSTP_MAX_PORTS
) {
1728 VLOG_ERR("port %s: invalid rstp-port-num", port
->name
);
1729 port_s
->enable
= false;
1732 port_s
->port_num
= port_num
;
1734 if (*port_num_counter
>= RSTP_MAX_PORTS
) {
1735 VLOG_ERR("port %s: too many RSTP ports, disabling", port
->name
);
1736 port_s
->enable
= false;
1739 /* If rstp-port-num is not specified, use 0.
1740 * rstp_port_set_port_number() will look for the first free one. */
1741 port_s
->port_num
= 0;
1744 /* Increment the port num counter, because we only support
1745 * RSTP_MAX_PORTS rstp ports. */
1746 (*port_num_counter
)++;
1748 config_str
= smap_get(&port
->cfg
->other_config
, "rstp-path-cost");
1750 port_s
->path_cost
= strtoul(config_str
, NULL
, 10);
1752 enum netdev_features current
;
1755 netdev_get_features(iface
->netdev
, ¤t
, NULL
, NULL
, NULL
);
1756 mbps
= netdev_features_to_bps(current
, 100 * 1000 * 1000) / 1000000;
1757 port_s
->path_cost
= rstp_convert_speed_to_cost(mbps
);
1760 config_str
= smap_get(&port
->cfg
->other_config
, "rstp-port-priority");
1762 port_s
->priority
= strtoul(config_str
, NULL
, 0);
1764 port_s
->priority
= RSTP_DEFAULT_PORT_PRIORITY
;
1767 port_s
->admin_p2p_mac_state
= smap_get_ullong(
1768 &port
->cfg
->other_config
, "rstp-admin-p2p-mac",
1769 RSTP_ADMIN_P2P_MAC_FORCE_TRUE
);
1771 port_s
->admin_port_state
= smap_get_bool(&port
->cfg
->other_config
,
1772 "rstp-admin-port-state", true);
1774 port_s
->admin_edge_port
= smap_get_bool(&port
->cfg
->other_config
,
1775 "rstp-port-admin-edge", false);
1776 port_s
->auto_edge
= smap_get_bool(&port
->cfg
->other_config
,
1777 "rstp-port-auto-edge", true);
1778 port_s
->mcheck
= smap_get_bool(&port
->cfg
->other_config
,
1779 "rstp-port-mcheck", false);
1782 /* Set spanning tree configuration on 'br'. */
1784 bridge_configure_stp(struct bridge
*br
, bool enable_stp
)
1787 ofproto_set_stp(br
->ofproto
, NULL
);
1789 struct ofproto_stp_settings br_s
;
1790 const char *config_str
;
1792 int port_num_counter
;
1793 unsigned long *port_num_bitmap
;
1795 config_str
= smap_get(&br
->cfg
->other_config
, "stp-system-id");
1799 if (eth_addr_from_string(config_str
, &ea
)) {
1800 br_s
.system_id
= eth_addr_to_uint64(ea
);
1802 br_s
.system_id
= eth_addr_to_uint64(br
->ea
);
1803 VLOG_ERR("bridge %s: invalid stp-system-id, defaulting "
1804 "to "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(br
->ea
));
1807 br_s
.system_id
= eth_addr_to_uint64(br
->ea
);
1810 br_s
.priority
= smap_get_ullong(&br
->cfg
->other_config
, "stp-priority",
1811 STP_DEFAULT_BRIDGE_PRIORITY
);
1812 br_s
.hello_time
= smap_get_ullong(&br
->cfg
->other_config
,
1814 STP_DEFAULT_HELLO_TIME
);
1816 br_s
.max_age
= smap_get_ullong(&br
->cfg
->other_config
, "stp-max-age",
1817 STP_DEFAULT_MAX_AGE
/ 1000) * 1000;
1818 br_s
.fwd_delay
= smap_get_ullong(&br
->cfg
->other_config
,
1819 "stp-forward-delay",
1820 STP_DEFAULT_FWD_DELAY
/ 1000) * 1000;
1822 /* Configure STP on the bridge. */
1823 if (ofproto_set_stp(br
->ofproto
, &br_s
)) {
1824 VLOG_ERR("bridge %s: could not enable STP", br
->name
);
1828 /* Users must either set the port number with the "stp-port-num"
1829 * configuration on all ports or none. If manual configuration
1830 * is not done, then we allocate them sequentially. */
1831 port_num_counter
= 0;
1832 port_num_bitmap
= bitmap_allocate(STP_MAX_PORTS
);
1833 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
1834 struct ofproto_port_stp_settings port_s
;
1835 struct iface
*iface
;
1837 port_configure_stp(br
->ofproto
, port
, &port_s
,
1838 &port_num_counter
, port_num_bitmap
);
1840 /* As bonds are not supported, just apply configuration to
1841 * all interfaces. */
1842 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1843 if (ofproto_port_set_stp(br
->ofproto
, iface
->ofp_port
,
1845 VLOG_ERR("port %s: could not enable STP", port
->name
);
1851 if (bitmap_scan(port_num_bitmap
, 1, 0, STP_MAX_PORTS
) != STP_MAX_PORTS
1852 && port_num_counter
) {
1853 VLOG_ERR("bridge %s: must manually configure all STP port "
1854 "IDs or none, disabling", br
->name
);
1855 ofproto_set_stp(br
->ofproto
, NULL
);
1857 bitmap_free(port_num_bitmap
);
1862 bridge_configure_rstp(struct bridge
*br
, bool enable_rstp
)
1865 ofproto_set_rstp(br
->ofproto
, NULL
);
1867 struct ofproto_rstp_settings br_s
;
1868 const char *config_str
;
1870 int port_num_counter
;
1872 config_str
= smap_get(&br
->cfg
->other_config
, "rstp-address");
1876 if (eth_addr_from_string(config_str
, &ea
)) {
1877 br_s
.address
= eth_addr_to_uint64(ea
);
1880 br_s
.address
= eth_addr_to_uint64(br
->ea
);
1881 VLOG_ERR("bridge %s: invalid rstp-address, defaulting "
1882 "to "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(br
->ea
));
1886 br_s
.address
= eth_addr_to_uint64(br
->ea
);
1889 const struct smap
*oc
= &br
->cfg
->other_config
;
1890 br_s
.priority
= smap_get_ullong(oc
, "rstp-priority",
1891 RSTP_DEFAULT_PRIORITY
);
1892 br_s
.ageing_time
= smap_get_ullong(oc
, "rstp-ageing-time",
1893 RSTP_DEFAULT_AGEING_TIME
);
1894 br_s
.force_protocol_version
= smap_get_ullong(
1895 oc
, "rstp-force-protocol-version", FPV_DEFAULT
);
1896 br_s
.bridge_max_age
= smap_get_ullong(oc
, "rstp-max-age",
1897 RSTP_DEFAULT_BRIDGE_MAX_AGE
);
1898 br_s
.bridge_forward_delay
= smap_get_ullong(
1899 oc
, "rstp-forward-delay", RSTP_DEFAULT_BRIDGE_FORWARD_DELAY
);
1900 br_s
.transmit_hold_count
= smap_get_ullong(
1901 oc
, "rstp-transmit-hold-count", RSTP_DEFAULT_TRANSMIT_HOLD_COUNT
);
1903 /* Configure RSTP on the bridge. */
1904 if (ofproto_set_rstp(br
->ofproto
, &br_s
)) {
1905 VLOG_ERR("bridge %s: could not enable RSTP", br
->name
);
1909 port_num_counter
= 0;
1910 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
1911 struct ofproto_port_rstp_settings port_s
;
1912 struct iface
*iface
;
1914 port_configure_rstp(br
->ofproto
, port
, &port_s
,
1917 /* As bonds are not supported, just apply configuration to
1918 * all interfaces. */
1919 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
1920 if (ofproto_port_set_rstp(br
->ofproto
, iface
->ofp_port
,
1922 VLOG_ERR("port %s: could not enable RSTP", port
->name
);
1931 bridge_configure_spanning_tree(struct bridge
*br
)
1933 bool enable_rstp
= br
->cfg
->rstp_enable
;
1934 bool enable_stp
= br
->cfg
->stp_enable
;
1936 if (enable_rstp
&& enable_stp
) {
1937 VLOG_WARN("%s: RSTP and STP are mutually exclusive but both are "
1938 "configured; enabling RSTP", br
->name
);
1942 bridge_configure_stp(br
, enable_stp
);
1943 bridge_configure_rstp(br
, enable_rstp
);
1947 bridge_has_bond_fake_iface(const struct bridge
*br
, const char *name
)
1949 const struct port
*port
= port_lookup(br
, name
);
1950 return port
&& port_is_bond_fake_iface(port
);
1954 port_is_bond_fake_iface(const struct port
*port
)
1956 return port
->cfg
->bond_fake_iface
&& !ovs_list_is_short(&port
->ifaces
);
1960 add_del_bridges(const struct ovsrec_open_vswitch
*cfg
)
1962 struct bridge
*br
, *next
;
1963 struct shash_node
*node
;
1964 struct shash new_br
;
1967 /* Collect new bridges' names and types. */
1968 shash_init(&new_br
);
1969 for (i
= 0; i
< cfg
->n_bridges
; i
++) {
1970 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1971 const struct ovsrec_bridge
*br_cfg
= cfg
->bridges
[i
];
1973 if (strchr(br_cfg
->name
, '/') || strchr(br_cfg
->name
, '\\')) {
1974 /* Prevent remote ovsdb-server users from accessing arbitrary
1975 * directories, e.g. consider a bridge named "../../../etc/".
1977 * Prohibiting "\" is only necessary on Windows but it's no great
1978 * loss elsewhere. */
1979 VLOG_WARN_RL(&rl
, "ignoring bridge with invalid name \"%s\"",
1981 } else if (!shash_add_once(&new_br
, br_cfg
->name
, br_cfg
)) {
1982 VLOG_WARN_RL(&rl
, "bridge %s specified twice", br_cfg
->name
);
1986 /* Get rid of deleted bridges or those whose types have changed.
1987 * Update 'cfg' of bridges that still exist. */
1988 HMAP_FOR_EACH_SAFE (br
, next
, node
, &all_bridges
) {
1989 br
->cfg
= shash_find_data(&new_br
, br
->name
);
1990 if (!br
->cfg
|| strcmp(br
->type
, ofproto_normalize_type(
1991 br
->cfg
->datapath_type
))) {
1992 bridge_destroy(br
, true);
1996 /* Add new bridges. */
1997 SHASH_FOR_EACH(node
, &new_br
) {
1998 const struct ovsrec_bridge
*br_cfg
= node
->data
;
1999 if (!bridge_lookup(br_cfg
->name
)) {
2000 bridge_create(br_cfg
);
2004 shash_destroy(&new_br
);
2007 /* Configures 'netdev' based on the "options" column in 'iface_cfg'.
2008 * Returns 0 if successful, otherwise a positive errno value. */
2010 iface_set_netdev_config(const struct ovsrec_interface
*iface_cfg
,
2011 struct netdev
*netdev
, char **errp
)
2013 return netdev_set_config(netdev
, &iface_cfg
->options
, errp
);
2016 /* Opens a network device for 'if_cfg' and configures it. Adds the network
2017 * device to br->ofproto and stores the OpenFlow port number in '*ofp_portp'.
2019 * If successful, returns 0 and stores the network device in '*netdevp'. On
2020 * failure, returns a positive errno value and stores NULL in '*netdevp'. */
2022 iface_do_create(const struct bridge
*br
,
2023 const struct ovsrec_interface
*iface_cfg
,
2024 ofp_port_t
*ofp_portp
, struct netdev
**netdevp
,
2027 struct netdev
*netdev
= NULL
;
2031 if (netdev_is_reserved_name(iface_cfg
->name
)) {
2032 VLOG_WARN("could not create interface %s, name is reserved",
2038 type
= ofproto_port_open_type(br
->ofproto
,
2039 iface_get_type(iface_cfg
, br
->cfg
));
2040 error
= netdev_open(iface_cfg
->name
, type
, &netdev
);
2042 VLOG_WARN_BUF(errp
, "could not open network device %s (%s)",
2043 iface_cfg
->name
, ovs_strerror(error
));
2047 error
= iface_set_netdev_config(iface_cfg
, netdev
, errp
);
2052 iface_set_netdev_mtu(iface_cfg
, netdev
);
2054 *ofp_portp
= iface_pick_ofport(iface_cfg
);
2055 error
= ofproto_port_add(br
->ofproto
, netdev
, ofp_portp
);
2057 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2059 *errp
= xasprintf("could not add network device %s to ofproto (%s)",
2060 iface_cfg
->name
, ovs_strerror(error
));
2061 if (!VLOG_DROP_WARN(&rl
)) {
2062 VLOG_WARN("%s", *errp
);
2067 VLOG_INFO("bridge %s: added interface %s on port %d",
2068 br
->name
, iface_cfg
->name
, *ofp_portp
);
2075 netdev_close(netdev
);
2079 /* Creates a new iface on 'br' based on 'if_cfg'. The new iface has OpenFlow
2080 * port number 'ofp_port'. If ofp_port is OFPP_NONE, an OpenFlow port is
2081 * automatically allocated for the iface. Takes ownership of and
2082 * deallocates 'if_cfg'.
2084 * Return true if an iface is successfully created, false otherwise. */
2086 iface_create(struct bridge
*br
, const struct ovsrec_interface
*iface_cfg
,
2087 const struct ovsrec_port
*port_cfg
)
2089 struct netdev
*netdev
;
2090 struct iface
*iface
;
2091 ofp_port_t ofp_port
;
2096 /* Do the bits that can fail up front. */
2097 ovs_assert(!iface_lookup(br
, iface_cfg
->name
));
2098 error
= iface_do_create(br
, iface_cfg
, &ofp_port
, &netdev
, &errp
);
2100 iface_clear_db_record(iface_cfg
, errp
);
2105 /* Get or create the port structure. */
2106 port
= port_lookup(br
, port_cfg
->name
);
2108 port
= port_create(br
, port_cfg
);
2111 /* Create the iface structure. */
2112 iface
= xzalloc(sizeof *iface
);
2113 ovs_list_push_back(&port
->ifaces
, &iface
->port_elem
);
2114 hmap_insert(&br
->iface_by_name
, &iface
->name_node
,
2115 hash_string(iface_cfg
->name
, 0));
2117 iface
->name
= xstrdup(iface_cfg
->name
);
2118 iface
->ofp_port
= ofp_port
;
2119 iface
->netdev
= netdev
;
2120 iface
->type
= iface_get_type(iface_cfg
, br
->cfg
);
2121 iface
->cfg
= iface_cfg
;
2122 hmap_insert(&br
->ifaces
, &iface
->ofp_port_node
,
2123 hash_ofp_port(ofp_port
));
2125 /* Populate initial status in database. */
2126 iface_refresh_stats(iface
);
2127 iface_refresh_netdev_status(iface
);
2129 /* Add bond fake iface if necessary. */
2130 if (port_is_bond_fake_iface(port
)) {
2131 struct ofproto_port ofproto_port
;
2133 if (ofproto_port_query_by_name(br
->ofproto
, port
->name
,
2135 error
= netdev_open(port
->name
, "internal", &netdev
);
2137 ofp_port_t fake_ofp_port
= OFPP_NONE
;
2138 ofproto_port_add(br
->ofproto
, netdev
, &fake_ofp_port
);
2139 netdev_close(netdev
);
2141 VLOG_WARN("could not open network device %s (%s)",
2142 port
->name
, ovs_strerror(error
));
2145 /* Already exists, nothing to do. */
2146 ofproto_port_destroy(&ofproto_port
);
2153 /* Set forward BPDU option. */
2155 bridge_configure_forward_bpdu(struct bridge
*br
)
2157 ofproto_set_forward_bpdu(br
->ofproto
,
2158 smap_get_bool(&br
->cfg
->other_config
,
2163 /* Set MAC learning table configuration for 'br'. */
2165 bridge_configure_mac_table(struct bridge
*br
)
2167 const struct smap
*oc
= &br
->cfg
->other_config
;
2168 int idle_time
= smap_get_int(oc
, "mac-aging-time", 0);
2170 idle_time
= MAC_ENTRY_DEFAULT_IDLE_TIME
;
2173 int mac_table_size
= smap_get_int(oc
, "mac-table-size", 0);
2174 if (!mac_table_size
) {
2175 mac_table_size
= MAC_DEFAULT_MAX
;
2178 ofproto_set_mac_table_config(br
->ofproto
, idle_time
, mac_table_size
);
2181 /* Set multicast snooping table configuration for 'br'. */
2183 bridge_configure_mcast_snooping(struct bridge
*br
)
2185 if (!br
->cfg
->mcast_snooping_enable
) {
2186 ofproto_set_mcast_snooping(br
->ofproto
, NULL
);
2189 struct ofproto_mcast_snooping_settings br_s
;
2191 const struct smap
*oc
= &br
->cfg
->other_config
;
2192 int idle_time
= smap_get_int(oc
, "mcast-snooping-aging-time", 0);
2193 br_s
.idle_time
= idle_time
? idle_time
: MCAST_ENTRY_DEFAULT_IDLE_TIME
;
2194 int max_entries
= smap_get_int(oc
, "mcast-snooping-table-size", 0);
2195 br_s
.max_entries
= (max_entries
2197 : MCAST_DEFAULT_MAX_ENTRIES
);
2199 br_s
.flood_unreg
= !smap_get_bool(
2200 oc
, "mcast-snooping-disable-flood-unregistered", false);
2202 /* Configure multicast snooping on the bridge */
2203 if (ofproto_set_mcast_snooping(br
->ofproto
, &br_s
)) {
2204 VLOG_ERR("bridge %s: could not enable multicast snooping",
2209 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
2210 struct ofproto_mcast_snooping_port_settings port_s
;
2211 port_s
.flood
= smap_get_bool(&port
->cfg
->other_config
,
2212 "mcast-snooping-flood", false);
2213 port_s
.flood_reports
= smap_get_bool(&port
->cfg
->other_config
,
2214 "mcast-snooping-flood-reports", false);
2215 if (ofproto_port_set_mcast_snooping(br
->ofproto
, port
, &port_s
)) {
2216 VLOG_ERR("port %s: could not configure mcast snooping",
2224 find_local_hw_addr(const struct bridge
*br
, struct eth_addr
*ea
,
2225 const struct port
*fake_br
, struct iface
**hw_addr_iface
)
2227 struct hmapx mirror_output_ports
;
2229 bool found_addr
= false;
2233 /* Mirror output ports don't participate in picking the local hardware
2234 * address. ofproto can't help us find out whether a given port is a
2235 * mirror output because we haven't configured mirrors yet, so we need to
2236 * accumulate them ourselves. */
2237 hmapx_init(&mirror_output_ports
);
2238 for (i
= 0; i
< br
->cfg
->n_mirrors
; i
++) {
2239 struct ovsrec_mirror
*m
= br
->cfg
->mirrors
[i
];
2240 if (m
->output_port
) {
2241 hmapx_add(&mirror_output_ports
, m
->output_port
);
2245 /* Otherwise choose the minimum non-local MAC address among all of the
2247 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
2248 struct eth_addr iface_ea
;
2249 struct iface
*candidate
;
2250 struct iface
*iface
;
2252 /* Mirror output ports don't participate. */
2253 if (hmapx_contains(&mirror_output_ports
, port
->cfg
)) {
2257 /* Choose the MAC address to represent the port. */
2259 if (port
->cfg
->mac
&& eth_addr_from_string(port
->cfg
->mac
,
2261 /* Find the interface with this Ethernet address (if any) so that
2262 * we can provide the correct devname to the caller. */
2263 LIST_FOR_EACH (candidate
, port_elem
, &port
->ifaces
) {
2264 struct eth_addr candidate_ea
;
2265 if (!netdev_get_etheraddr(candidate
->netdev
, &candidate_ea
)
2266 && eth_addr_equals(iface_ea
, candidate_ea
)) {
2271 /* Choose the interface whose MAC address will represent the port.
2272 * The Linux kernel bonding code always chooses the MAC address of
2273 * the first slave added to a bond, and the Fedora networking
2274 * scripts always add slaves to a bond in alphabetical order, so
2275 * for compatibility we choose the interface with the name that is
2276 * first in alphabetical order. */
2277 LIST_FOR_EACH (candidate
, port_elem
, &port
->ifaces
) {
2278 if (!iface
|| strcmp(candidate
->name
, iface
->name
) < 0) {
2283 /* A port always has at least one interface. */
2284 ovs_assert(iface
!= NULL
);
2286 /* The local port doesn't count (since we're trying to choose its
2287 * MAC address anyway). */
2288 if (iface
->ofp_port
== OFPP_LOCAL
) {
2292 /* For fake bridges we only choose from ports with the same tag */
2293 if (fake_br
&& fake_br
->cfg
&& fake_br
->cfg
->tag
) {
2294 if (!port
->cfg
->tag
) {
2297 if (*port
->cfg
->tag
!= *fake_br
->cfg
->tag
) {
2303 error
= netdev_get_etheraddr(iface
->netdev
, &iface_ea
);
2309 /* Compare against our current choice. */
2310 if (!eth_addr_is_multicast(iface_ea
) &&
2311 !eth_addr_is_local(iface_ea
) &&
2312 !eth_addr_is_reserved(iface_ea
) &&
2313 !eth_addr_is_zero(iface_ea
) &&
2314 (!found_addr
|| eth_addr_compare_3way(iface_ea
, *ea
) < 0))
2317 *hw_addr_iface
= iface
;
2323 *ea
= br
->default_ea
;
2324 *hw_addr_iface
= NULL
;
2327 hmapx_destroy(&mirror_output_ports
);
2331 bridge_pick_local_hw_addr(struct bridge
*br
, struct eth_addr
*ea
,
2332 struct iface
**hw_addr_iface
)
2334 *hw_addr_iface
= NULL
;
2336 /* Did the user request a particular MAC? */
2337 const char *hwaddr
= smap_get_def(&br
->cfg
->other_config
, "hwaddr", "");
2338 if (eth_addr_from_string(hwaddr
, ea
)) {
2339 if (eth_addr_is_multicast(*ea
)) {
2340 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
2341 "address "ETH_ADDR_FMT
, br
->name
, ETH_ADDR_ARGS(*ea
));
2342 } else if (eth_addr_is_zero(*ea
)) {
2343 VLOG_ERR("bridge %s: cannot set MAC address to zero", br
->name
);
2349 /* Find a local hw address */
2350 find_local_hw_addr(br
, ea
, NULL
, hw_addr_iface
);
2353 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
2354 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
2355 * an interface on 'br', then that interface must be passed in as
2356 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
2357 * 'hw_addr_iface' must be passed in as a null pointer. */
2359 bridge_pick_datapath_id(struct bridge
*br
,
2360 const struct eth_addr bridge_ea
,
2361 struct iface
*hw_addr_iface
)
2364 * The procedure for choosing a bridge MAC address will, in the most
2365 * ordinary case, also choose a unique MAC that we can use as a datapath
2366 * ID. In some special cases, though, multiple bridges will end up with
2367 * the same MAC address. This is OK for the bridges, but it will confuse
2368 * the OpenFlow controller, because each datapath needs a unique datapath
2371 * Datapath IDs must be unique. It is also very desirable that they be
2372 * stable from one run to the next, so that policy set on a datapath
2375 const char *datapath_id
;
2378 datapath_id
= smap_get_def(&br
->cfg
->other_config
, "datapath-id", "");
2379 if (dpid_from_string(datapath_id
, &dpid
)) {
2383 if (!hw_addr_iface
) {
2385 * A purely internal bridge, that is, one that has no non-virtual
2386 * network devices on it at all, is difficult because it has no
2387 * natural unique identifier at all.
2389 * When the host is a XenServer, we handle this case by hashing the
2390 * host's UUID with the name of the bridge. Names of bridges are
2391 * persistent across XenServer reboots, although they can be reused if
2392 * an internal network is destroyed and then a new one is later
2393 * created, so this is fairly effective.
2395 * When the host is not a XenServer, we punt by using a random MAC
2396 * address on each run.
2398 const char *host_uuid
= xenserver_get_host_uuid();
2400 char *combined
= xasprintf("%s,%s", host_uuid
, br
->name
);
2401 dpid
= dpid_from_hash(combined
, strlen(combined
));
2407 return eth_addr_to_uint64(bridge_ea
);
2411 dpid_from_hash(const void *data
, size_t n
)
2414 uint8_t bytes
[SHA1_DIGEST_SIZE
];
2418 sha1_bytes(data
, n
, hash
.bytes
);
2419 eth_addr_mark_random(&hash
.ea
);
2420 return eth_addr_to_uint64(hash
.ea
);
2424 iface_refresh_netdev_status(struct iface
*iface
)
2428 enum netdev_features current
;
2429 enum netdev_flags flags
;
2430 const char *link_state
;
2431 struct eth_addr mac
;
2432 int64_t bps
, mtu_64
, ifindex64
, link_resets
;
2435 if (iface_is_synthetic(iface
)) {
2439 if (iface
->change_seq
== netdev_get_change_seq(iface
->netdev
)
2440 && !status_txn_try_again
) {
2444 iface
->change_seq
= netdev_get_change_seq(iface
->netdev
);
2448 if (!netdev_get_status(iface
->netdev
, &smap
)) {
2449 ovsrec_interface_set_status(iface
->cfg
, &smap
);
2451 ovsrec_interface_set_status(iface
->cfg
, NULL
);
2454 smap_destroy(&smap
);
2456 error
= netdev_get_flags(iface
->netdev
, &flags
);
2458 const char *state
= flags
& NETDEV_UP
? "up" : "down";
2460 ovsrec_interface_set_admin_state(iface
->cfg
, state
);
2462 ovsrec_interface_set_admin_state(iface
->cfg
, NULL
);
2465 link_state
= netdev_get_carrier(iface
->netdev
) ? "up" : "down";
2466 ovsrec_interface_set_link_state(iface
->cfg
, link_state
);
2468 link_resets
= netdev_get_carrier_resets(iface
->netdev
);
2469 ovsrec_interface_set_link_resets(iface
->cfg
, &link_resets
, 1);
2471 error
= netdev_get_features(iface
->netdev
, ¤t
, NULL
, NULL
, NULL
);
2472 bps
= !error
? netdev_features_to_bps(current
, 0) : 0;
2474 ovsrec_interface_set_duplex(iface
->cfg
,
2475 netdev_features_is_full_duplex(current
)
2477 ovsrec_interface_set_link_speed(iface
->cfg
, &bps
, 1);
2479 ovsrec_interface_set_duplex(iface
->cfg
, NULL
);
2480 ovsrec_interface_set_link_speed(iface
->cfg
, NULL
, 0);
2483 error
= netdev_get_mtu(iface
->netdev
, &mtu
);
2486 ovsrec_interface_set_mtu(iface
->cfg
, &mtu_64
, 1);
2488 ovsrec_interface_set_mtu(iface
->cfg
, NULL
, 0);
2491 error
= netdev_get_etheraddr(iface
->netdev
, &mac
);
2493 char mac_string
[ETH_ADDR_STRLEN
+ 1];
2495 snprintf(mac_string
, sizeof mac_string
,
2496 ETH_ADDR_FMT
, ETH_ADDR_ARGS(mac
));
2497 ovsrec_interface_set_mac_in_use(iface
->cfg
, mac_string
);
2499 ovsrec_interface_set_mac_in_use(iface
->cfg
, NULL
);
2502 /* The netdev may return a negative number (such as -EOPNOTSUPP)
2503 * if there is no valid ifindex number. */
2504 ifindex64
= netdev_get_ifindex(iface
->netdev
);
2505 if (ifindex64
< 0) {
2508 ovsrec_interface_set_ifindex(iface
->cfg
, &ifindex64
, 1);
2512 iface_refresh_ofproto_status(struct iface
*iface
)
2518 if (iface_is_synthetic(iface
)) {
2522 error
= ofproto_vport_get_status(iface
->port
->bridge
->ofproto
,
2523 iface
->ofp_port
, &errp
);
2524 if (error
&& error
!= EOPNOTSUPP
) {
2525 /* Need to verify to avoid race with transaction from
2526 * 'bridge_reconfigure' that clears errors explicitly. */
2527 ovsrec_interface_verify_error(iface
->cfg
);
2528 ovsrec_interface_set_error(iface
->cfg
,
2529 errp
? errp
: ovs_strerror(error
));
2533 current
= ofproto_port_is_lacp_current(iface
->port
->bridge
->ofproto
,
2537 ovsrec_interface_set_lacp_current(iface
->cfg
, &bl
, 1);
2539 ovsrec_interface_set_lacp_current(iface
->cfg
, NULL
, 0);
2542 if (ofproto_port_cfm_status_changed(iface
->port
->bridge
->ofproto
,
2544 || status_txn_try_again
) {
2545 iface_refresh_cfm_stats(iface
);
2548 if (ofproto_port_bfd_status_changed(iface
->port
->bridge
->ofproto
,
2550 || status_txn_try_again
) {
2554 ofproto_port_get_bfd_status(iface
->port
->bridge
->ofproto
,
2555 iface
->ofp_port
, &smap
);
2556 ovsrec_interface_set_bfd_status(iface
->cfg
, &smap
);
2557 smap_destroy(&smap
);
2561 /* Writes 'iface''s CFM statistics to the database. 'iface' must not be
2564 iface_refresh_cfm_stats(struct iface
*iface
)
2566 const struct ovsrec_interface
*cfg
= iface
->cfg
;
2567 struct cfm_status status
;
2570 error
= ofproto_port_get_cfm_status(iface
->port
->bridge
->ofproto
,
2571 iface
->ofp_port
, &status
);
2573 ovsrec_interface_set_cfm_fault(cfg
, NULL
, 0);
2574 ovsrec_interface_set_cfm_fault_status(cfg
, NULL
, 0);
2575 ovsrec_interface_set_cfm_remote_opstate(cfg
, NULL
);
2576 ovsrec_interface_set_cfm_flap_count(cfg
, NULL
, 0);
2577 ovsrec_interface_set_cfm_health(cfg
, NULL
, 0);
2578 ovsrec_interface_set_cfm_remote_mpids(cfg
, NULL
, 0);
2580 const char *reasons
[CFM_FAULT_N_REASONS
];
2581 int64_t cfm_health
= status
.health
;
2582 int64_t cfm_flap_count
= status
.flap_count
;
2583 bool faulted
= status
.faults
!= 0;
2586 ovsrec_interface_set_cfm_fault(cfg
, &faulted
, 1);
2589 for (i
= 0; i
< CFM_FAULT_N_REASONS
; i
++) {
2590 int reason
= 1 << i
;
2591 if (status
.faults
& reason
) {
2592 reasons
[j
++] = cfm_fault_reason_to_str(reason
);
2595 ovsrec_interface_set_cfm_fault_status(cfg
, reasons
, j
);
2597 ovsrec_interface_set_cfm_flap_count(cfg
, &cfm_flap_count
, 1);
2599 if (status
.remote_opstate
>= 0) {
2600 const char *remote_opstate
= status
.remote_opstate
? "up" : "down";
2601 ovsrec_interface_set_cfm_remote_opstate(cfg
, remote_opstate
);
2603 ovsrec_interface_set_cfm_remote_opstate(cfg
, NULL
);
2606 ovsrec_interface_set_cfm_remote_mpids(cfg
,
2607 (const int64_t *)status
.rmps
,
2609 if (cfm_health
>= 0) {
2610 ovsrec_interface_set_cfm_health(cfg
, &cfm_health
, 1);
2612 ovsrec_interface_set_cfm_health(cfg
, NULL
, 0);
2620 iface_refresh_stats(struct iface
*iface
)
2622 struct netdev_custom_stats custom_stats
;
2623 struct netdev_stats stats
;
2625 uint32_t i
, counters_size
;
2627 #define IFACE_STATS \
2628 IFACE_STAT(rx_packets, "rx_packets") \
2629 IFACE_STAT(tx_packets, "tx_packets") \
2630 IFACE_STAT(rx_bytes, "rx_bytes") \
2631 IFACE_STAT(tx_bytes, "tx_bytes") \
2632 IFACE_STAT(rx_dropped, "rx_dropped") \
2633 IFACE_STAT(tx_dropped, "tx_dropped") \
2634 IFACE_STAT(rx_errors, "rx_errors") \
2635 IFACE_STAT(tx_errors, "tx_errors") \
2636 IFACE_STAT(rx_frame_errors, "rx_frame_err") \
2637 IFACE_STAT(rx_over_errors, "rx_over_err") \
2638 IFACE_STAT(rx_crc_errors, "rx_crc_err") \
2639 IFACE_STAT(rx_missed_errors, "rx_missed_errors") \
2640 IFACE_STAT(collisions, "collisions") \
2641 IFACE_STAT(rx_1_to_64_packets, "rx_1_to_64_packets") \
2642 IFACE_STAT(rx_65_to_127_packets, "rx_65_to_127_packets") \
2643 IFACE_STAT(rx_128_to_255_packets, "rx_128_to_255_packets") \
2644 IFACE_STAT(rx_256_to_511_packets, "rx_256_to_511_packets") \
2645 IFACE_STAT(rx_512_to_1023_packets, "rx_512_to_1023_packets") \
2646 IFACE_STAT(rx_1024_to_1522_packets, "rx_1024_to_1522_packets") \
2647 IFACE_STAT(rx_1523_to_max_packets, "rx_1523_to_max_packets") \
2648 IFACE_STAT(tx_1_to_64_packets, "tx_1_to_64_packets") \
2649 IFACE_STAT(tx_65_to_127_packets, "tx_65_to_127_packets") \
2650 IFACE_STAT(tx_128_to_255_packets, "tx_128_to_255_packets") \
2651 IFACE_STAT(tx_256_to_511_packets, "tx_256_to_511_packets") \
2652 IFACE_STAT(tx_512_to_1023_packets, "tx_512_to_1023_packets") \
2653 IFACE_STAT(tx_1024_to_1522_packets, "tx_1024_to_1522_packets") \
2654 IFACE_STAT(tx_1523_to_max_packets, "tx_1523_to_max_packets") \
2655 IFACE_STAT(tx_multicast_packets, "tx_multicast_packets") \
2656 IFACE_STAT(rx_broadcast_packets, "rx_broadcast_packets") \
2657 IFACE_STAT(tx_broadcast_packets, "tx_broadcast_packets") \
2658 IFACE_STAT(rx_undersized_errors, "rx_undersized_errors") \
2659 IFACE_STAT(rx_oversize_errors, "rx_oversize_errors") \
2660 IFACE_STAT(rx_fragmented_errors, "rx_fragmented_errors") \
2661 IFACE_STAT(rx_jabber_errors, "rx_jabber_errors")
2663 #define IFACE_STAT(MEMBER, NAME) + 1
2664 enum { N_IFACE_STATS
= IFACE_STATS
};
2667 if (iface_is_synthetic(iface
)) {
2671 netdev_get_custom_stats(iface
->netdev
, &custom_stats
);
2673 counters_size
= custom_stats
.size
+ N_IFACE_STATS
;
2674 int64_t *values
= xmalloc(counters_size
* sizeof(int64_t));
2675 const char **keys
= xmalloc(counters_size
* sizeof(char *));
2677 /* Intentionally ignore return value, since errors will set 'stats' to
2678 * all-1s, and we will deal with that correctly below. */
2679 netdev_get_stats(iface
->netdev
, &stats
);
2681 /* Copy statistics into keys[] and values[]. */
2683 #define IFACE_STAT(MEMBER, NAME) \
2684 if (stats.MEMBER != UINT64_MAX) { \
2686 values[n] = stats.MEMBER; \
2692 /* Copy custom statistics into keys[] and values[]. */
2693 if (custom_stats
.size
&& custom_stats
.counters
) {
2694 for (i
= 0 ; i
< custom_stats
.size
; i
++) {
2695 values
[n
] = custom_stats
.counters
[i
].value
;
2696 keys
[n
] = custom_stats
.counters
[i
].name
;
2701 ovs_assert(n
<= counters_size
);
2703 ovsrec_interface_set_statistics(iface
->cfg
, keys
, values
, n
);
2708 netdev_free_custom_stats_counters(&custom_stats
);
2712 br_refresh_datapath_info(struct bridge
*br
)
2714 const char *version
;
2716 version
= (br
->ofproto
&& br
->ofproto
->ofproto_class
->get_datapath_version
2717 ? br
->ofproto
->ofproto_class
->get_datapath_version(br
->ofproto
)
2720 ovsrec_bridge_set_datapath_version(br
->cfg
,
2721 version
? version
: "<unknown>");
2725 br_refresh_stp_status(struct bridge
*br
)
2727 struct smap smap
= SMAP_INITIALIZER(&smap
);
2728 struct ofproto
*ofproto
= br
->ofproto
;
2729 struct ofproto_stp_status status
;
2731 if (ofproto_get_stp_status(ofproto
, &status
)) {
2735 if (!status
.enabled
) {
2736 ovsrec_bridge_set_status(br
->cfg
, NULL
);
2740 smap_add_format(&smap
, "stp_bridge_id", STP_ID_FMT
,
2741 STP_ID_ARGS(status
.bridge_id
));
2742 smap_add_format(&smap
, "stp_designated_root", STP_ID_FMT
,
2743 STP_ID_ARGS(status
.designated_root
));
2744 smap_add_format(&smap
, "stp_root_path_cost", "%d", status
.root_path_cost
);
2746 ovsrec_bridge_set_status(br
->cfg
, &smap
);
2747 smap_destroy(&smap
);
2751 port_refresh_stp_status(struct port
*port
)
2753 struct ofproto
*ofproto
= port
->bridge
->ofproto
;
2754 struct iface
*iface
;
2755 struct ofproto_port_stp_status status
;
2758 if (port_is_synthetic(port
)) {
2762 /* STP doesn't currently support bonds. */
2763 if (!ovs_list_is_singleton(&port
->ifaces
)) {
2764 ovsrec_port_set_status(port
->cfg
, NULL
);
2768 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
2769 if (ofproto_port_get_stp_status(ofproto
, iface
->ofp_port
, &status
)) {
2773 if (!status
.enabled
) {
2774 ovsrec_port_set_status(port
->cfg
, NULL
);
2778 /* Set Status column. */
2780 smap_add_format(&smap
, "stp_port_id", "%d", status
.port_id
);
2781 smap_add(&smap
, "stp_state", stp_state_name(status
.state
));
2782 smap_add_format(&smap
, "stp_sec_in_state", "%u", status
.sec_in_state
);
2783 smap_add(&smap
, "stp_role", stp_role_name(status
.role
));
2784 ovsrec_port_set_status(port
->cfg
, &smap
);
2785 smap_destroy(&smap
);
2789 port_refresh_stp_stats(struct port
*port
)
2791 struct ofproto
*ofproto
= port
->bridge
->ofproto
;
2792 struct iface
*iface
;
2793 struct ofproto_port_stp_stats stats
;
2794 const char *keys
[3];
2795 int64_t int_values
[3];
2797 if (port_is_synthetic(port
)) {
2801 /* STP doesn't currently support bonds. */
2802 if (!ovs_list_is_singleton(&port
->ifaces
)) {
2806 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
2807 if (ofproto_port_get_stp_stats(ofproto
, iface
->ofp_port
, &stats
)) {
2811 if (!stats
.enabled
) {
2812 ovsrec_port_set_statistics(port
->cfg
, NULL
, NULL
, 0);
2816 /* Set Statistics column. */
2817 keys
[0] = "stp_tx_count";
2818 int_values
[0] = stats
.tx_count
;
2819 keys
[1] = "stp_rx_count";
2820 int_values
[1] = stats
.rx_count
;
2821 keys
[2] = "stp_error_count";
2822 int_values
[2] = stats
.error_count
;
2824 ovsrec_port_set_statistics(port
->cfg
, keys
, int_values
,
2825 ARRAY_SIZE(int_values
));
2829 br_refresh_rstp_status(struct bridge
*br
)
2831 struct smap smap
= SMAP_INITIALIZER(&smap
);
2832 struct ofproto
*ofproto
= br
->ofproto
;
2833 struct ofproto_rstp_status status
;
2835 if (ofproto_get_rstp_status(ofproto
, &status
)) {
2838 if (!status
.enabled
) {
2839 ovsrec_bridge_set_rstp_status(br
->cfg
, NULL
);
2842 smap_add_format(&smap
, "rstp_bridge_id", RSTP_ID_FMT
,
2843 RSTP_ID_ARGS(status
.bridge_id
));
2844 smap_add_format(&smap
, "rstp_root_path_cost", "%"PRIu32
,
2845 status
.root_path_cost
);
2846 smap_add_format(&smap
, "rstp_root_id", RSTP_ID_FMT
,
2847 RSTP_ID_ARGS(status
.root_id
));
2848 smap_add_format(&smap
, "rstp_designated_id", RSTP_ID_FMT
,
2849 RSTP_ID_ARGS(status
.designated_id
));
2850 smap_add_format(&smap
, "rstp_designated_port_id", RSTP_PORT_ID_FMT
,
2851 status
.designated_port_id
);
2852 smap_add_format(&smap
, "rstp_bridge_port_id", RSTP_PORT_ID_FMT
,
2853 status
.bridge_port_id
);
2854 ovsrec_bridge_set_rstp_status(br
->cfg
, &smap
);
2855 smap_destroy(&smap
);
2859 port_refresh_rstp_status(struct port
*port
)
2861 struct ofproto
*ofproto
= port
->bridge
->ofproto
;
2862 struct iface
*iface
;
2863 struct ofproto_port_rstp_status status
;
2864 const char *keys
[4];
2865 int64_t int_values
[4];
2868 if (port_is_synthetic(port
)) {
2872 /* RSTP doesn't currently support bonds. */
2873 if (!ovs_list_is_singleton(&port
->ifaces
)) {
2874 ovsrec_port_set_rstp_status(port
->cfg
, NULL
);
2878 iface
= CONTAINER_OF(ovs_list_front(&port
->ifaces
), struct iface
, port_elem
);
2879 if (ofproto_port_get_rstp_status(ofproto
, iface
->ofp_port
, &status
)) {
2883 if (!status
.enabled
) {
2884 ovsrec_port_set_rstp_status(port
->cfg
, NULL
);
2885 ovsrec_port_set_rstp_statistics(port
->cfg
, NULL
, NULL
, 0);
2888 /* Set Status column. */
2891 smap_add_format(&smap
, "rstp_port_id", RSTP_PORT_ID_FMT
,
2893 smap_add_format(&smap
, "rstp_port_role", "%s",
2894 rstp_port_role_name(status
.role
));
2895 smap_add_format(&smap
, "rstp_port_state", "%s",
2896 rstp_state_name(status
.state
));
2897 smap_add_format(&smap
, "rstp_designated_bridge_id", RSTP_ID_FMT
,
2898 RSTP_ID_ARGS(status
.designated_bridge_id
));
2899 smap_add_format(&smap
, "rstp_designated_port_id", RSTP_PORT_ID_FMT
,
2900 status
.designated_port_id
);
2901 smap_add_format(&smap
, "rstp_designated_path_cost", "%"PRIu32
,
2902 status
.designated_path_cost
);
2904 ovsrec_port_set_rstp_status(port
->cfg
, &smap
);
2905 smap_destroy(&smap
);
2907 /* Set Statistics column. */
2908 keys
[0] = "rstp_tx_count";
2909 int_values
[0] = status
.tx_count
;
2910 keys
[1] = "rstp_rx_count";
2911 int_values
[1] = status
.rx_count
;
2912 keys
[2] = "rstp_uptime";
2913 int_values
[2] = status
.uptime
;
2914 keys
[3] = "rstp_error_count";
2915 int_values
[3] = status
.error_count
;
2916 ovsrec_port_set_rstp_statistics(port
->cfg
, keys
, int_values
,
2917 ARRAY_SIZE(int_values
));
2921 port_refresh_bond_status(struct port
*port
, bool force_update
)
2923 struct eth_addr mac
;
2925 /* Return if port is not a bond */
2926 if (ovs_list_is_singleton(&port
->ifaces
)) {
2930 if (bond_get_changed_active_slave(port
->name
, &mac
, force_update
)) {
2934 ds_put_format(&mac_s
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(mac
));
2935 ovsrec_port_set_bond_active_slave(port
->cfg
, ds_cstr(&mac_s
));
2941 enable_system_stats(const struct ovsrec_open_vswitch
*cfg
)
2943 return smap_get_bool(&cfg
->other_config
, "enable-statistics", false);
2947 reconfigure_system_stats(const struct ovsrec_open_vswitch
*cfg
)
2949 bool enable
= enable_system_stats(cfg
);
2951 system_stats_enable(enable
);
2953 ovsrec_open_vswitch_set_statistics(cfg
, NULL
);
2958 run_system_stats(void)
2960 const struct ovsrec_open_vswitch
*cfg
= ovsrec_open_vswitch_first(idl
);
2963 stats
= system_stats_run();
2965 struct ovsdb_idl_txn
*txn
;
2966 struct ovsdb_datum datum
;
2968 txn
= ovsdb_idl_txn_create(idl
);
2969 ovsdb_datum_from_smap(&datum
, stats
);
2970 smap_destroy(stats
);
2971 ovsdb_idl_txn_write(&cfg
->header_
, &ovsrec_open_vswitch_col_statistics
,
2973 ovsdb_idl_txn_commit(txn
);
2974 ovsdb_idl_txn_destroy(txn
);
2981 ofp12_controller_role_to_str(enum ofp12_controller_role role
)
2984 case OFPCR12_ROLE_EQUAL
:
2986 case OFPCR12_ROLE_MASTER
:
2988 case OFPCR12_ROLE_SLAVE
:
2990 case OFPCR12_ROLE_NOCHANGE
:
2997 refresh_controller_status(void)
3001 /* Accumulate status for controllers on all bridges. */
3002 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3003 struct shash info
= SHASH_INITIALIZER(&info
);
3004 ofproto_get_ofproto_controller_info(br
->ofproto
, &info
);
3006 /* Update each controller of the bridge in the database with
3007 * current status. */
3008 struct ovsrec_controller
**controllers
;
3009 size_t n_controllers
= bridge_get_controllers(br
, &controllers
);
3011 for (i
= 0; i
< n_controllers
; i
++) {
3012 struct ovsrec_controller
*cfg
= controllers
[i
];
3013 struct ofproto_controller_info
*cinfo
=
3014 shash_find_data(&info
, cfg
->target
);
3016 /* cinfo is NULL when 'cfg->target' is a passive connection. */
3018 ovsrec_controller_set_is_connected(cfg
, cinfo
->is_connected
);
3019 const char *role
= ofp12_controller_role_to_str(cinfo
->role
);
3020 ovsrec_controller_set_role(cfg
, role
);
3021 ovsrec_controller_set_status(cfg
, &cinfo
->pairs
);
3023 ovsrec_controller_set_is_connected(cfg
, false);
3024 ovsrec_controller_set_role(cfg
, NULL
);
3025 ovsrec_controller_set_status(cfg
, NULL
);
3029 ofproto_free_ofproto_controller_info(&info
);
3033 /* Update interface and mirror statistics if necessary. */
3035 run_stats_update(void)
3037 const struct ovsrec_open_vswitch
*cfg
= ovsrec_open_vswitch_first(idl
);
3044 /* Statistics update interval should always be greater than or equal to
3046 stats_interval
= MAX(smap_get_int(&cfg
->other_config
,
3047 "stats-update-interval",
3049 if (stats_timer_interval
!= stats_interval
) {
3050 stats_timer_interval
= stats_interval
;
3051 stats_timer
= LLONG_MIN
;
3054 if (time_msec() >= stats_timer
) {
3055 enum ovsdb_idl_txn_status status
;
3057 /* Rate limit the update. Do not start a new update if the
3058 * previous one is not done. */
3062 stats_txn
= ovsdb_idl_txn_create(idl
);
3063 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3067 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
3068 struct iface
*iface
;
3070 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
3071 iface_refresh_stats(iface
);
3073 port_refresh_stp_stats(port
);
3075 HMAP_FOR_EACH (m
, hmap_node
, &br
->mirrors
) {
3076 mirror_refresh_stats(m
);
3079 refresh_controller_status();
3082 status
= ovsdb_idl_txn_commit(stats_txn
);
3083 if (status
!= TXN_INCOMPLETE
) {
3084 stats_timer
= time_msec() + stats_timer_interval
;
3085 ovsdb_idl_txn_destroy(stats_txn
);
3092 stats_update_wait(void)
3094 /* If the 'stats_txn' is non-null (transaction incomplete), waits for the
3095 * transaction to complete. Otherwise, waits for the 'stats_timer'. */
3097 ovsdb_idl_txn_wait(stats_txn
);
3099 poll_timer_wait_until(stats_timer
);
3103 /* Update bridge/port/interface status if necessary. */
3105 run_status_update(void)
3110 /* Rate limit the update. Do not start a new update if the
3111 * previous one is not done. */
3112 seq
= seq_read(connectivity_seq_get());
3113 if (seq
!= connectivity_seqno
|| status_txn_try_again
) {
3114 const struct ovsrec_open_vswitch
*cfg
=
3115 ovsrec_open_vswitch_first(idl
);
3118 connectivity_seqno
= seq
;
3119 status_txn
= ovsdb_idl_txn_create(idl
);
3121 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3124 br_refresh_stp_status(br
);
3125 br_refresh_rstp_status(br
);
3126 br_refresh_datapath_info(br
);
3127 HMAP_FOR_EACH (port
, hmap_node
, &br
->ports
) {
3128 struct iface
*iface
;
3130 port_refresh_stp_status(port
);
3131 port_refresh_rstp_status(port
);
3132 port_refresh_bond_status(port
, status_txn_try_again
);
3133 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
3134 iface_refresh_netdev_status(iface
);
3135 iface_refresh_ofproto_status(iface
);
3142 /* Commit the transaction and get the status. If the transaction finishes,
3143 * then destroy the transaction. Otherwise, keep it so that we can check
3144 * progress the next time that this function is called. */
3146 enum ovsdb_idl_txn_status status
;
3148 status
= ovsdb_idl_txn_commit(status_txn
);
3149 if (status
!= TXN_INCOMPLETE
) {
3150 ovsdb_idl_txn_destroy(status_txn
);
3153 /* Sets the 'status_txn_try_again' if the transaction fails. */
3154 if (status
== TXN_SUCCESS
|| status
== TXN_UNCHANGED
) {
3155 status_txn_try_again
= false;
3157 status_txn_try_again
= true;
3162 /* Refresh AA port status if necessary. */
3163 if (time_msec() >= aa_refresh_timer
) {
3166 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3167 if (bridge_aa_need_refresh(br
)) {
3168 struct ovsdb_idl_txn
*txn
;
3170 txn
= ovsdb_idl_txn_create(idl
);
3171 bridge_aa_refresh_queued(br
);
3172 ovsdb_idl_txn_commit(txn
);
3173 ovsdb_idl_txn_destroy(txn
);
3177 aa_refresh_timer
= time_msec() + AA_REFRESH_INTERVAL
;
3182 status_update_wait(void)
3184 /* If the 'status_txn' is non-null (transaction incomplete), waits for the
3185 * transaction to complete. If the status update to database needs to be
3186 * run again (transaction fails), registers a timeout in
3187 * 'STATUS_CHECK_AGAIN_MSEC'. Otherwise, waits on the global connectivity
3188 * sequence number. */
3190 ovsdb_idl_txn_wait(status_txn
);
3191 } else if (status_txn_try_again
) {
3192 poll_timer_wait_until(time_msec() + STATUS_CHECK_AGAIN_MSEC
);
3194 seq_wait(connectivity_seq_get(), connectivity_seqno
);
3205 /* Let each datapath type do the work that it needs to do. */
3207 ofproto_enumerate_types(&types
);
3208 SSET_FOR_EACH (type
, &types
) {
3209 ofproto_type_run(type
);
3211 sset_destroy(&types
);
3213 /* Let each bridge do the work that it needs to do. */
3214 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3215 ofproto_run(br
->ofproto
);
3222 static struct ovsrec_open_vswitch null_cfg
;
3223 const struct ovsrec_open_vswitch
*cfg
;
3225 ovsrec_open_vswitch_init(&null_cfg
);
3231 if (ovsdb_idl_is_lock_contended(idl
)) {
3232 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
3233 struct bridge
*br
, *next_br
;
3235 VLOG_ERR_RL(&rl
, "another ovs-vswitchd process is running, "
3236 "disabling this process (pid %ld) until it goes away",
3237 (long int) getpid());
3239 HMAP_FOR_EACH_SAFE (br
, next_br
, node
, &all_bridges
) {
3240 bridge_destroy(br
, false);
3242 /* Since we will not be running system_stats_run() in this process
3243 * with the current situation of multiple ovs-vswitchd daemons,
3244 * disable system stats collection. */
3245 system_stats_enable(false);
3247 } else if (!ovsdb_idl_has_lock(idl
)
3248 || !ovsdb_idl_has_ever_connected(idl
)) {
3249 /* Returns if not holding the lock or not done retrieving db
3253 cfg
= ovsrec_open_vswitch_first(idl
);
3256 netdev_set_flow_api_enabled(&cfg
->other_config
);
3257 dpdk_init(&cfg
->other_config
);
3260 /* Initialize the ofproto library. This only needs to run once, but
3261 * it must be done after the configuration is set. If the
3262 * initialization has already occurred, bridge_init_ofproto()
3263 * returns immediately. */
3264 bridge_init_ofproto(cfg
);
3266 /* Once the value of flow-restore-wait is false, we no longer should
3267 * check its value from the database. */
3268 if (cfg
&& ofproto_get_flow_restore_wait()) {
3269 ofproto_set_flow_restore_wait(smap_get_bool(&cfg
->other_config
,
3270 "flow-restore-wait", false));
3275 /* Re-configure SSL. We do this on every trip through the main loop,
3276 * instead of just when the database changes, because the contents of the
3277 * key and certificate files can change without the database changing.
3279 * We do this before bridge_reconfigure() because that function might
3280 * initiate SSL connections and thus requires SSL to be configured. */
3281 if (cfg
&& cfg
->ssl
) {
3282 const struct ovsrec_ssl
*ssl
= cfg
->ssl
;
3284 stream_ssl_set_key_and_cert(ssl
->private_key
, ssl
->certificate
);
3285 stream_ssl_set_ca_cert_file(ssl
->ca_cert
, ssl
->bootstrap_ca_cert
);
3288 if (ovsdb_idl_get_seqno(idl
) != idl_seqno
||
3289 if_notifier_changed(ifnotifier
)) {
3290 struct ovsdb_idl_txn
*txn
;
3292 idl_seqno
= ovsdb_idl_get_seqno(idl
);
3293 txn
= ovsdb_idl_txn_create(idl
);
3294 bridge_reconfigure(cfg
? cfg
: &null_cfg
);
3297 ovsrec_open_vswitch_set_cur_cfg(cfg
, cfg
->next_cfg
);
3298 discover_types(cfg
);
3301 /* If we are completing our initial configuration for this run
3302 * of ovs-vswitchd, then keep the transaction around to monitor
3303 * it for completion. */
3304 if (initial_config_done
) {
3305 /* Always sets the 'status_txn_try_again' to check again,
3306 * in case that this transaction fails. */
3307 status_txn_try_again
= true;
3308 ovsdb_idl_txn_commit(txn
);
3309 ovsdb_idl_txn_destroy(txn
);
3311 initial_config_done
= true;
3312 daemonize_txn
= txn
;
3316 if (daemonize_txn
) {
3317 enum ovsdb_idl_txn_status status
= ovsdb_idl_txn_commit(daemonize_txn
);
3318 if (status
!= TXN_INCOMPLETE
) {
3319 ovsdb_idl_txn_destroy(daemonize_txn
);
3320 daemonize_txn
= NULL
;
3322 /* ovs-vswitchd has completed initialization, so allow the
3323 * process that forked us to exit successfully. */
3324 daemonize_complete();
3326 vlog_enable_async();
3328 VLOG_INFO_ONCE("%s (Open vSwitch) %s", program_name
, VERSION
);
3333 run_status_update();
3343 ovsdb_idl_wait(idl
);
3344 if (daemonize_txn
) {
3345 ovsdb_idl_txn_wait(daemonize_txn
);
3351 ofproto_enumerate_types(&types
);
3352 SSET_FOR_EACH (type
, &types
) {
3353 ofproto_type_wait(type
);
3355 sset_destroy(&types
);
3357 if (!hmap_is_empty(&all_bridges
)) {
3360 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3361 ofproto_wait(br
->ofproto
);
3363 stats_update_wait();
3364 status_update_wait();
3367 system_stats_wait();
3370 /* Adds some memory usage statistics for bridges into 'usage', for use with
3371 * memory_report(). */
3373 bridge_get_memory_usage(struct simap
*usage
)
3380 ofproto_enumerate_types(&types
);
3381 SSET_FOR_EACH (type
, &types
) {
3382 ofproto_type_get_memory_usage(type
, usage
);
3384 sset_destroy(&types
);
3386 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3387 ofproto_get_memory_usage(br
->ofproto
, usage
);
3391 /* QoS unixctl user interface functions. */
3393 struct qos_unixctl_show_cbdata
{
3395 struct iface
*iface
;
3399 qos_unixctl_show_queue(unsigned int queue_id
,
3400 const struct smap
*details
,
3401 struct iface
*iface
,
3404 struct netdev_queue_stats stats
;
3405 struct smap_node
*node
;
3408 ds_put_cstr(ds
, "\n");
3410 ds_put_format(ds
, "Queue %u:\n", queue_id
);
3412 ds_put_cstr(ds
, "Default:\n");
3415 SMAP_FOR_EACH (node
, details
) {
3416 ds_put_format(ds
, " %s: %s\n", node
->key
, node
->value
);
3419 error
= netdev_get_queue_stats(iface
->netdev
, queue_id
, &stats
);
3421 if (stats
.tx_packets
!= UINT64_MAX
) {
3422 ds_put_format(ds
, " tx_packets: %"PRIu64
"\n", stats
.tx_packets
);
3425 if (stats
.tx_bytes
!= UINT64_MAX
) {
3426 ds_put_format(ds
, " tx_bytes: %"PRIu64
"\n", stats
.tx_bytes
);
3429 if (stats
.tx_errors
!= UINT64_MAX
) {
3430 ds_put_format(ds
, " tx_errors: %"PRIu64
"\n", stats
.tx_errors
);
3433 ds_put_format(ds
, " Failed to get statistics for queue %u: %s",
3434 queue_id
, ovs_strerror(error
));
3439 qos_unixctl_show_types(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
3440 const char *argv
[], void *aux OVS_UNUSED
)
3442 struct ds ds
= DS_EMPTY_INITIALIZER
;
3443 struct sset types
= SSET_INITIALIZER(&types
);
3444 struct iface
*iface
;
3445 const char * types_name
;
3448 iface
= iface_find(argv
[1]);
3450 unixctl_command_reply_error(conn
, "no such interface");
3454 error
= netdev_get_qos_types(iface
->netdev
, &types
);
3456 if (!sset_is_empty(&types
)) {
3457 SSET_FOR_EACH (types_name
, &types
) {
3458 ds_put_format(&ds
, "QoS type: %s\n", types_name
);
3460 unixctl_command_reply(conn
, ds_cstr(&ds
));
3462 ds_put_format(&ds
, "No QoS types supported for interface: %s\n",
3464 unixctl_command_reply(conn
, ds_cstr(&ds
));
3467 ds_put_format(&ds
, "%s: failed to retrieve supported QoS types (%s)",
3468 iface
->name
, ovs_strerror(error
));
3469 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
3472 sset_destroy(&types
);
3477 qos_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
3478 const char *argv
[], void *aux OVS_UNUSED
)
3480 struct ds ds
= DS_EMPTY_INITIALIZER
;
3481 struct smap smap
= SMAP_INITIALIZER(&smap
);
3482 struct iface
*iface
;
3484 struct smap_node
*node
;
3487 iface
= iface_find(argv
[1]);
3489 unixctl_command_reply_error(conn
, "no such interface");
3493 error
= netdev_get_qos(iface
->netdev
, &type
, &smap
);
3495 if (*type
!= '\0') {
3496 struct netdev_queue_dump dump
;
3497 struct smap details
;
3498 unsigned int queue_id
;
3500 ds_put_format(&ds
, "QoS: %s %s\n", iface
->name
, type
);
3502 SMAP_FOR_EACH (node
, &smap
) {
3503 ds_put_format(&ds
, "%s: %s\n", node
->key
, node
->value
);
3506 smap_init(&details
);
3507 NETDEV_QUEUE_FOR_EACH (&queue_id
, &details
, &dump
, iface
->netdev
) {
3508 qos_unixctl_show_queue(queue_id
, &details
, iface
, &ds
);
3510 smap_destroy(&details
);
3512 unixctl_command_reply(conn
, ds_cstr(&ds
));
3514 ds_put_format(&ds
, "QoS not configured on %s\n", iface
->name
);
3515 unixctl_command_reply(conn
, ds_cstr(&ds
));
3518 ds_put_format(&ds
, "%s: failed to retrieve QOS configuration (%s)\n",
3519 iface
->name
, ovs_strerror(error
));
3520 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
3523 smap_destroy(&smap
);
3527 /* Bridge reconfiguration functions. */
3529 bridge_create(const struct ovsrec_bridge
*br_cfg
)
3533 ovs_assert(!bridge_lookup(br_cfg
->name
));
3534 br
= xzalloc(sizeof *br
);
3536 br
->name
= xstrdup(br_cfg
->name
);
3537 br
->type
= xstrdup(ofproto_normalize_type(br_cfg
->datapath_type
));
3540 /* Derive the default Ethernet address from the bridge's UUID. This should
3541 * be unique and it will be stable between ovs-vswitchd runs. */
3542 memcpy(&br
->default_ea
, &br_cfg
->header_
.uuid
, ETH_ADDR_LEN
);
3543 eth_addr_mark_random(&br
->default_ea
);
3545 hmap_init(&br
->ports
);
3546 hmap_init(&br
->ifaces
);
3547 hmap_init(&br
->iface_by_name
);
3548 hmap_init(&br
->mirrors
);
3550 hmap_init(&br
->mappings
);
3551 hmap_insert(&all_bridges
, &br
->node
, hash_string(br
->name
, 0));
3555 bridge_destroy(struct bridge
*br
, bool del
)
3558 struct mirror
*mirror
, *next_mirror
;
3559 struct port
*port
, *next_port
;
3561 HMAP_FOR_EACH_SAFE (port
, next_port
, hmap_node
, &br
->ports
) {
3564 HMAP_FOR_EACH_SAFE (mirror
, next_mirror
, hmap_node
, &br
->mirrors
) {
3565 mirror_destroy(mirror
);
3568 hmap_remove(&all_bridges
, &br
->node
);
3569 ofproto_destroy(br
->ofproto
, del
);
3570 hmap_destroy(&br
->ifaces
);
3571 hmap_destroy(&br
->ports
);
3572 hmap_destroy(&br
->iface_by_name
);
3573 hmap_destroy(&br
->mirrors
);
3574 hmap_destroy(&br
->mappings
);
3581 static struct bridge
*
3582 bridge_lookup(const char *name
)
3586 HMAP_FOR_EACH_WITH_HASH (br
, node
, hash_string(name
, 0), &all_bridges
) {
3587 if (!strcmp(br
->name
, name
)) {
3594 /* Handle requests for a listing of all flows known by the OpenFlow
3595 * stack, including those normally hidden. */
3597 bridge_unixctl_dump_flows(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
3598 const char *argv
[], void *aux OVS_UNUSED
)
3603 br
= bridge_lookup(argv
[1]);
3605 unixctl_command_reply_error(conn
, "Unknown bridge");
3610 ofproto_get_all_flows(br
->ofproto
, &results
);
3612 unixctl_command_reply(conn
, ds_cstr(&results
));
3613 ds_destroy(&results
);
3616 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
3617 * connections and reconnect. If BRIDGE is not specified, then all bridges
3618 * drop their controller connections and reconnect. */
3620 bridge_unixctl_reconnect(struct unixctl_conn
*conn
, int argc
,
3621 const char *argv
[], void *aux OVS_UNUSED
)
3625 br
= bridge_lookup(argv
[1]);
3627 unixctl_command_reply_error(conn
, "Unknown bridge");
3630 ofproto_reconnect_controllers(br
->ofproto
);
3632 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
3633 ofproto_reconnect_controllers(br
->ofproto
);
3636 unixctl_command_reply(conn
, NULL
);
3640 bridge_get_controllers(const struct bridge
*br
,
3641 struct ovsrec_controller
***controllersp
)
3643 struct ovsrec_controller
**controllers
;
3644 size_t n_controllers
;
3646 controllers
= br
->cfg
->controller
;
3647 n_controllers
= br
->cfg
->n_controller
;
3649 if (n_controllers
== 1 && !strcmp(controllers
[0]->target
, "none")) {
3655 *controllersp
= controllers
;
3657 return n_controllers
;
3661 bridge_collect_wanted_ports(struct bridge
*br
,
3662 struct shash
*wanted_ports
)
3666 shash_init(wanted_ports
);
3668 for (i
= 0; i
< br
->cfg
->n_ports
; i
++) {
3669 const char *name
= br
->cfg
->ports
[i
]->name
;
3670 if (!shash_add_once(wanted_ports
, name
, br
->cfg
->ports
[i
])) {
3671 VLOG_WARN("bridge %s: %s specified twice as bridge port",
3675 if (bridge_get_controllers(br
, NULL
)
3676 && !shash_find(wanted_ports
, br
->name
)) {
3677 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
3678 br
->name
, br
->name
);
3680 ovsrec_interface_init(&br
->synth_local_iface
);
3681 ovsrec_port_init(&br
->synth_local_port
);
3683 br
->synth_local_port
.interfaces
= &br
->synth_local_ifacep
;
3684 br
->synth_local_port
.n_interfaces
= 1;
3685 br
->synth_local_port
.name
= br
->name
;
3687 br
->synth_local_iface
.name
= br
->name
;
3688 br
->synth_local_iface
.type
= "internal";
3690 br
->synth_local_ifacep
= &br
->synth_local_iface
;
3692 shash_add(wanted_ports
, br
->name
, &br
->synth_local_port
);
3696 /* Deletes "struct port"s and "struct iface"s under 'br' which aren't
3697 * consistent with 'br->cfg'. Updates 'br->if_cfg_queue' with interfaces which
3698 * 'br' needs to complete its configuration. */
3700 bridge_del_ports(struct bridge
*br
, const struct shash
*wanted_ports
)
3702 struct shash_node
*port_node
;
3703 struct port
*port
, *next
;
3705 /* Get rid of deleted ports.
3706 * Get rid of deleted interfaces on ports that still exist. */
3707 HMAP_FOR_EACH_SAFE (port
, next
, hmap_node
, &br
->ports
) {
3708 port
->cfg
= shash_find_data(wanted_ports
, port
->name
);
3712 port_del_ifaces(port
);
3716 /* Update iface->cfg and iface->type in interfaces that still exist. */
3717 SHASH_FOR_EACH (port_node
, wanted_ports
) {
3718 const struct ovsrec_port
*port_rec
= port_node
->data
;
3721 for (i
= 0; i
< port_rec
->n_interfaces
; i
++) {
3722 const struct ovsrec_interface
*cfg
= port_rec
->interfaces
[i
];
3723 struct iface
*iface
= iface_lookup(br
, cfg
->name
);
3724 const char *type
= iface_get_type(cfg
, br
->cfg
);
3730 /* We will add new interfaces later. */
3736 /* Configures the IP stack for 'br''s local interface properly according to the
3737 * configuration in 'c'. */
3739 bridge_configure_local_iface_netdev(struct bridge
*br
,
3740 struct ovsrec_controller
*c
)
3742 struct netdev
*netdev
;
3743 struct in_addr mask
, gateway
;
3745 struct iface
*local_iface
;
3748 /* If there's no local interface or no IP address, give up. */
3749 local_iface
= iface_from_ofp_port(br
, OFPP_LOCAL
);
3750 if (!local_iface
|| !c
->local_ip
|| !ip_parse(c
->local_ip
, &ip
.s_addr
)) {
3754 /* Bring up the local interface. */
3755 netdev
= local_iface
->netdev
;
3756 netdev_turn_flags_on(netdev
, NETDEV_UP
, NULL
);
3758 /* Configure the IP address and netmask. */
3759 if (!c
->local_netmask
3760 || !ip_parse(c
->local_netmask
, &mask
.s_addr
)
3762 mask
.s_addr
= guess_netmask(ip
.s_addr
);
3764 if (!netdev_set_in4(netdev
, ip
, mask
)) {
3765 VLOG_INFO("bridge %s: configured IP address "IP_FMT
", netmask "IP_FMT
,
3766 br
->name
, IP_ARGS(ip
.s_addr
), IP_ARGS(mask
.s_addr
));
3769 /* Configure the default gateway. */
3770 if (c
->local_gateway
3771 && ip_parse(c
->local_gateway
, &gateway
.s_addr
)
3772 && gateway
.s_addr
) {
3773 if (!netdev_add_router(netdev
, gateway
)) {
3774 VLOG_INFO("bridge %s: configured gateway "IP_FMT
,
3775 br
->name
, IP_ARGS(gateway
.s_addr
));
3780 /* Returns true if 'a' and 'b' are the same except that any number of slashes
3781 * in either string are treated as equal to any number of slashes in the other,
3782 * e.g. "x///y" is equal to "x/y".
3784 * Also, if 'b_stoplen' bytes from 'b' are found to be equal to corresponding
3785 * bytes from 'a', the function considers this success. Specify 'b_stoplen' as
3786 * SIZE_MAX to compare all of 'a' to all of 'b' rather than just a prefix of
3787 * 'b' against a prefix of 'a'.
3790 equal_pathnames(const char *a
, const char *b
, size_t b_stoplen
)
3792 const char *b_start
= b
;
3794 if (b
- b_start
>= b_stoplen
) {
3796 } else if (*a
!= *b
) {
3798 } else if (*a
== '/') {
3799 a
+= strspn(a
, "/");
3800 b
+= strspn(b
, "/");
3801 } else if (*a
== '\0') {
3810 static enum ofconn_type
3811 get_controller_ofconn_type(const char *target
, const char *type
)
3814 ? (!strcmp(type
, "primary") ? OFCONN_PRIMARY
: OFCONN_SERVICE
)
3815 : (!vconn_verify_name(target
) ? OFCONN_PRIMARY
: OFCONN_SERVICE
));
3819 bridge_configure_remotes(struct bridge
*br
,
3820 const struct sockaddr_in
*managers
, size_t n_managers
)
3822 bool disable_in_band
;
3824 struct ovsrec_controller
**controllers
;
3825 size_t n_controllers
;
3827 enum ofproto_fail_mode fail_mode
;
3829 /* Check if we should disable in-band control on this bridge. */
3830 disable_in_band
= smap_get_bool(&br
->cfg
->other_config
, "disable-in-band",
3833 /* Set OpenFlow queue ID for in-band control. */
3834 ofproto_set_in_band_queue(br
->ofproto
,
3835 smap_get_int(&br
->cfg
->other_config
,
3836 "in-band-queue", -1));
3838 if (disable_in_band
) {
3839 ofproto_set_extra_in_band_remotes(br
->ofproto
, NULL
, 0);
3841 ofproto_set_extra_in_band_remotes(br
->ofproto
, managers
, n_managers
);
3844 n_controllers
= (ofproto_get_flow_restore_wait() ? 0
3845 : bridge_get_controllers(br
, &controllers
));
3847 /* The set of controllers to pass down to ofproto. */
3848 struct shash ocs
= SHASH_INITIALIZER(&ocs
);
3850 /* Add managment controller. */
3851 struct ofproto_controller
*oc
= xmalloc(sizeof *oc
);
3852 *oc
= (struct ofproto_controller
) {
3853 .type
= OFCONN_SERVICE
,
3854 .probe_interval
= 60,
3855 .band
= OFPROTO_OUT_OF_BAND
,
3856 .enable_async_msgs
= true,
3857 .allowed_versions
= bridge_get_allowed_versions(br
),
3858 .max_pktq_size
= bridge_get_controller_queue_size(br
, NULL
),
3861 &ocs
, xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br
->name
), oc
);
3863 for (size_t i
= 0; i
< n_controllers
; i
++) {
3864 struct ovsrec_controller
*c
= controllers
[i
];
3865 if (daemon_should_self_confine()
3866 && (!strncmp(c
->target
, "punix:", 6)
3867 || !strncmp(c
->target
, "unix:", 5))) {
3868 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3871 if (!strncmp(c
->target
, "unix:", 5)) {
3872 /* Connect to a listening socket */
3873 whitelist
= xasprintf("unix:%s/", ovs_rundir());
3874 if (strchr(c
->target
, '/') &&
3875 !equal_pathnames(c
->target
, whitelist
,
3876 strlen(whitelist
))) {
3877 /* Absolute path specified, but not in ovs_rundir */
3878 VLOG_ERR_RL(&rl
, "bridge %s: Not connecting to socket "
3879 "controller \"%s\" due to possibility for "
3880 "remote exploit. Instead, specify socket "
3881 "in whitelisted \"%s\" or connect to "
3882 "\"unix:%s/%s.mgmt\" (which is always "
3883 "available without special configuration).",
3884 br
->name
, c
->target
, whitelist
,
3885 ovs_rundir(), br
->name
);
3890 whitelist
= xasprintf("punix:%s/%s.",
3891 ovs_rundir(), br
->name
);
3892 if (!equal_pathnames(c
->target
, whitelist
, strlen(whitelist
))
3893 || strchr(c
->target
+ strlen(whitelist
), '/')) {
3894 /* Prevent remote ovsdb-server users from accessing
3895 * arbitrary Unix domain sockets and overwriting arbitrary
3897 VLOG_ERR_RL(&rl
, "bridge %s: Not adding Unix domain socket "
3898 "controller \"%s\" due to possibility of "
3899 "overwriting local files. Instead, specify "
3900 "path in whitelisted format \"%s*\" or "
3901 "connect to \"unix:%s/%s.mgmt\" (which is "
3902 "always available without special "
3904 br
->name
, c
->target
, whitelist
,
3905 ovs_rundir(), br
->name
);
3914 bridge_configure_local_iface_netdev(br
, c
);
3916 int dscp
= smap_get_int(&c
->other_config
, "dscp", DSCP_DEFAULT
);
3917 if (dscp
< 0 || dscp
> 63) {
3918 dscp
= DSCP_DEFAULT
;
3921 oc
= xmalloc(sizeof *oc
);
3922 *oc
= (struct ofproto_controller
) {
3923 .type
= get_controller_ofconn_type(c
->target
, c
->type
),
3924 .max_backoff
= c
->max_backoff
? *c
->max_backoff
/ 1000 : 8,
3925 .probe_interval
= (c
->inactivity_probe
3926 ? *c
->inactivity_probe
/ 1000 : 5),
3927 .band
= ((!c
->connection_mode
3928 || !strcmp(c
->connection_mode
, "in-band"))
3930 ? OFPROTO_IN_BAND
: OFPROTO_OUT_OF_BAND
),
3931 .enable_async_msgs
= (!c
->enable_async_messages
3932 || *c
->enable_async_messages
),
3933 .allowed_versions
= bridge_get_allowed_versions(br
),
3934 .max_pktq_size
= bridge_get_controller_queue_size(br
, c
),
3935 .rate_limit
= (c
->controller_rate_limit
3936 ? *c
->controller_rate_limit
: 0),
3937 .burst_limit
= (c
->controller_burst_limit
3938 ? *c
->controller_burst_limit
: 0),
3941 shash_add(&ocs
, c
->target
, oc
);
3943 ofproto_set_controllers(br
->ofproto
, &ocs
);
3944 shash_destroy_free_data(&ocs
);
3946 /* Set the fail-mode. */
3947 fail_mode
= !br
->cfg
->fail_mode
3948 || !strcmp(br
->cfg
->fail_mode
, "standalone")
3949 ? OFPROTO_FAIL_STANDALONE
3950 : OFPROTO_FAIL_SECURE
;
3951 ofproto_set_fail_mode(br
->ofproto
, fail_mode
);
3953 /* Configure OpenFlow controller connection snooping. */
3954 if (!ofproto_has_snoops(br
->ofproto
)) {
3958 sset_add_and_free(&snoops
, xasprintf("punix:%s/%s.snoop",
3959 ovs_rundir(), br
->name
));
3960 ofproto_set_snoops(br
->ofproto
, &snoops
);
3961 sset_destroy(&snoops
);
3966 bridge_configure_tables(struct bridge
*br
)
3968 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
3972 n_tables
= ofproto_get_n_tables(br
->ofproto
);
3974 for (i
= 0; i
< n_tables
; i
++) {
3975 struct ofproto_table_settings s
;
3976 bool use_default_prefixes
= true;
3979 s
.max_flows
= UINT_MAX
;
3981 s
.enable_eviction
= false;
3983 s
.n_prefix_fields
= 0;
3984 memset(s
.prefix_fields
, ~0, sizeof(s
.prefix_fields
));
3986 if (j
< br
->cfg
->n_flow_tables
&& i
== br
->cfg
->key_flow_tables
[j
]) {
3987 struct ovsrec_flow_table
*cfg
= br
->cfg
->value_flow_tables
[j
++];
3990 if (cfg
->n_flow_limit
&& *cfg
->flow_limit
< UINT_MAX
) {
3991 s
.max_flows
= *cfg
->flow_limit
;
3994 s
.enable_eviction
= (cfg
->overflow_policy
3995 && !strcmp(cfg
->overflow_policy
, "evict"));
3996 if (cfg
->n_groups
) {
3997 s
.groups
= xmalloc(cfg
->n_groups
* sizeof *s
.groups
);
3998 for (int k
= 0; k
< cfg
->n_groups
; k
++) {
3999 const char *string
= cfg
->groups
[k
];
4002 msg
= mf_parse_subfield__(&s
.groups
[k
], &string
);
4004 VLOG_WARN_RL(&rl
, "bridge %s table %d: error parsing "
4005 "'groups' (%s)", br
->name
, i
, msg
);
4007 } else if (*string
) {
4008 VLOG_WARN_RL(&rl
, "bridge %s table %d: 'groups' "
4009 "element '%s' contains trailing garbage",
4010 br
->name
, i
, cfg
->groups
[k
]);
4017 /* Prefix lookup fields. */
4018 s
.n_prefix_fields
= 0;
4019 for (int k
= 0; k
< cfg
->n_prefixes
; k
++) {
4020 const char *name
= cfg
->prefixes
[k
];
4021 const struct mf_field
*mf
;
4023 if (strcmp(name
, "none") == 0) {
4024 use_default_prefixes
= false;
4025 s
.n_prefix_fields
= 0;
4028 mf
= mf_from_name(name
);
4030 VLOG_WARN("bridge %s: 'prefixes' with unknown field: %s",
4034 if (mf
->flow_be32ofs
< 0 || mf
->n_bits
% 32) {
4035 VLOG_WARN("bridge %s: 'prefixes' with incompatible field: "
4036 "%s", br
->name
, name
);
4039 if (s
.n_prefix_fields
>= ARRAY_SIZE(s
.prefix_fields
)) {
4040 VLOG_WARN("bridge %s: 'prefixes' with too many fields, "
4041 "field not used: %s", br
->name
, name
);
4044 use_default_prefixes
= false;
4045 s
.prefix_fields
[s
.n_prefix_fields
++] = mf
->id
;
4048 if (use_default_prefixes
) {
4049 /* Use default values. */
4050 s
.n_prefix_fields
= ARRAY_SIZE(default_prefix_fields
);
4051 memcpy(s
.prefix_fields
, default_prefix_fields
,
4052 sizeof default_prefix_fields
);
4054 struct ds ds
= DS_EMPTY_INITIALIZER
;
4055 for (int k
= 0; k
< s
.n_prefix_fields
; k
++) {
4057 ds_put_char(&ds
, ',');
4059 ds_put_cstr(&ds
, mf_from_id(s
.prefix_fields
[k
])->name
);
4061 if (s
.n_prefix_fields
== 0) {
4062 ds_put_cstr(&ds
, "none");
4064 VLOG_INFO("bridge %s table %d: Prefix lookup with: %s.",
4065 br
->name
, i
, ds_cstr(&ds
));
4069 ofproto_configure_table(br
->ofproto
, i
, &s
);
4073 for (; j
< br
->cfg
->n_flow_tables
; j
++) {
4074 VLOG_WARN_RL(&rl
, "bridge %s: ignoring configuration for flow table "
4075 "%"PRId64
" not supported by this datapath", br
->name
,
4076 br
->cfg
->key_flow_tables
[j
]);
4081 bridge_configure_dp_desc(struct bridge
*br
)
4083 ofproto_set_dp_desc(br
->ofproto
,
4084 smap_get(&br
->cfg
->other_config
, "dp-desc"));
4087 static struct aa_mapping
*
4088 bridge_aa_mapping_find(struct bridge
*br
, const int64_t isid
)
4090 struct aa_mapping
*m
;
4092 HMAP_FOR_EACH_IN_BUCKET (m
,
4094 hash_bytes(&isid
, sizeof isid
, 0),
4096 if (isid
== m
->isid
) {
4103 static struct aa_mapping
*
4104 bridge_aa_mapping_create(struct bridge
*br
,
4108 struct aa_mapping
*m
;
4110 m
= xzalloc(sizeof *m
);
4114 m
->br_name
= xstrdup(br
->name
);
4115 hmap_insert(&br
->mappings
,
4117 hash_bytes(&isid
, sizeof isid
, 0));
4123 bridge_aa_mapping_destroy(struct aa_mapping
*m
)
4126 struct bridge
*br
= m
->bridge
;
4129 ofproto_aa_mapping_unregister(br
->ofproto
, m
);
4132 hmap_remove(&br
->mappings
, &m
->hmap_node
);
4141 bridge_aa_mapping_configure(struct aa_mapping
*m
)
4143 struct aa_mapping_settings s
;
4149 ofproto_aa_mapping_register(m
->bridge
->ofproto
, m
, &s
);
4155 bridge_configure_aa(struct bridge
*br
)
4157 const struct ovsdb_datum
*mc
;
4158 struct ovsrec_autoattach
*auto_attach
= br
->cfg
->auto_attach
;
4159 struct aa_settings aa_s
;
4160 struct aa_mapping
*m
, *next
;
4164 ofproto_set_aa(br
->ofproto
, NULL
, NULL
);
4168 memset(&aa_s
, 0, sizeof aa_s
);
4169 aa_s
.system_description
= auto_attach
->system_description
;
4170 aa_s
.system_name
= auto_attach
->system_name
;
4171 ofproto_set_aa(br
->ofproto
, NULL
, &aa_s
);
4173 mc
= ovsrec_autoattach_get_mappings(auto_attach
,
4175 OVSDB_TYPE_INTEGER
);
4176 HMAP_FOR_EACH_SAFE (m
, next
, hmap_node
, &br
->mappings
) {
4177 union ovsdb_atom atom
;
4179 atom
.integer
= m
->isid
;
4180 if (ovsdb_datum_find_key(mc
, &atom
, OVSDB_TYPE_INTEGER
) == UINT_MAX
) {
4181 VLOG_INFO("Deleting isid=%"PRIu32
", vlan=%"PRIu16
,
4183 bridge_aa_mapping_destroy(m
);
4187 /* Add new mappings and reconfigure existing ones. */
4188 for (i
= 0; i
< auto_attach
->n_mappings
; ++i
) {
4189 m
= bridge_aa_mapping_find(br
, auto_attach
->key_mappings
[i
]);
4192 VLOG_INFO("Adding isid=%"PRId64
", vlan=%"PRId64
,
4193 auto_attach
->key_mappings
[i
],
4194 auto_attach
->value_mappings
[i
]);
4195 m
= bridge_aa_mapping_create(br
,
4196 auto_attach
->key_mappings
[i
],
4197 auto_attach
->value_mappings
[i
]);
4199 if (!bridge_aa_mapping_configure(m
)) {
4200 bridge_aa_mapping_destroy(m
);
4207 bridge_aa_need_refresh(struct bridge
*br
)
4209 return ofproto_aa_vlan_get_queue_size(br
->ofproto
) > 0;
4213 bridge_aa_update_trunks(struct port
*port
, struct bridge_aa_vlan
*m
)
4215 int64_t *trunks
= NULL
;
4217 bool found
= false, reconfigure
= false;
4219 for (i
= 0; i
< port
->cfg
->n_trunks
; i
++) {
4220 if (port
->cfg
->trunks
[i
] == m
->vlan
) {
4227 case BRIDGE_AA_VLAN_OPER_ADD
:
4229 trunks
= xmalloc(sizeof *trunks
* (port
->cfg
->n_trunks
+ 1));
4231 for (i
= 0; i
< port
->cfg
->n_trunks
; i
++) {
4232 trunks
[i
] = port
->cfg
->trunks
[i
];
4234 trunks
[i
++] = m
->vlan
;
4240 case BRIDGE_AA_VLAN_OPER_REMOVE
:
4244 trunks
= xmalloc(sizeof *trunks
* (port
->cfg
->n_trunks
- 1));
4246 for (i
= 0; i
< port
->cfg
->n_trunks
; i
++) {
4247 if (port
->cfg
->trunks
[i
] != m
->vlan
) {
4248 trunks
[j
++] = port
->cfg
->trunks
[i
];
4257 case BRIDGE_AA_VLAN_OPER_UNDEF
:
4259 VLOG_WARN("unrecognized operation %u", m
->oper
);
4264 /* VLAN switching under trunk mode cause the trunk port to switch all
4265 * VLANs, see ovs-vswitchd.conf.db
4268 static char *vlan_mode_access
= "access";
4269 ovsrec_port_set_vlan_mode(port
->cfg
, vlan_mode_access
);
4273 static char *vlan_mode_trunk
= "trunk";
4274 ovsrec_port_set_vlan_mode(port
->cfg
, vlan_mode_trunk
);
4277 ovsrec_port_set_trunks(port
->cfg
, trunks
, i
);
4279 /* Force reconfigure of the port. */
4280 port_configure(port
);
4287 bridge_aa_refresh_queued(struct bridge
*br
)
4289 struct ovs_list
*list
= xmalloc(sizeof *list
);
4290 struct bridge_aa_vlan
*node
, *next
;
4292 ovs_list_init(list
);
4293 ofproto_aa_vlan_get_queued(br
->ofproto
, list
);
4295 LIST_FOR_EACH_SAFE (node
, next
, list_node
, list
) {
4298 VLOG_INFO("ifname=%s, vlan=%u, oper=%u", node
->port_name
, node
->vlan
,
4301 port
= port_lookup(br
, node
->port_name
);
4303 bridge_aa_update_trunks(port
, node
);
4306 ovs_list_remove(&node
->list_node
);
4307 free(node
->port_name
);
4315 /* Port functions. */
4317 static struct port
*
4318 port_create(struct bridge
*br
, const struct ovsrec_port
*cfg
)
4322 port
= xzalloc(sizeof *port
);
4324 port
->name
= xstrdup(cfg
->name
);
4326 ovs_list_init(&port
->ifaces
);
4328 hmap_insert(&br
->ports
, &port
->hmap_node
, hash_string(port
->name
, 0));
4332 /* Deletes interfaces from 'port' that are no longer configured for it. */
4334 port_del_ifaces(struct port
*port
)
4336 struct iface
*iface
, *next
;
4337 struct sset new_ifaces
;
4340 /* Collect list of new interfaces. */
4341 sset_init(&new_ifaces
);
4342 for (i
= 0; i
< port
->cfg
->n_interfaces
; i
++) {
4343 sset_add(&new_ifaces
, port
->cfg
->interfaces
[i
]->name
);
4346 /* Get rid of deleted interfaces. */
4347 LIST_FOR_EACH_SAFE (iface
, next
, port_elem
, &port
->ifaces
) {
4348 if (!sset_contains(&new_ifaces
, iface
->name
)) {
4349 iface_destroy(iface
);
4353 sset_destroy(&new_ifaces
);
4357 port_destroy(struct port
*port
)
4360 struct bridge
*br
= port
->bridge
;
4361 struct iface
*iface
, *next
;
4364 ofproto_bundle_unregister(br
->ofproto
, port
);
4367 LIST_FOR_EACH_SAFE (iface
, next
, port_elem
, &port
->ifaces
) {
4368 iface_destroy__(iface
);
4371 hmap_remove(&br
->ports
, &port
->hmap_node
);
4377 static struct port
*
4378 port_lookup(const struct bridge
*br
, const char *name
)
4382 HMAP_FOR_EACH_WITH_HASH (port
, hmap_node
, hash_string(name
, 0),
4384 if (!strcmp(port
->name
, name
)) {
4392 enable_lacp(struct port
*port
, bool *activep
)
4394 if (!port
->cfg
->lacp
) {
4395 /* XXX when LACP implementation has been sufficiently tested, enable by
4396 * default and make active on bonded ports. */
4398 } else if (!strcmp(port
->cfg
->lacp
, "off")) {
4400 } else if (!strcmp(port
->cfg
->lacp
, "active")) {
4403 } else if (!strcmp(port
->cfg
->lacp
, "passive")) {
4407 VLOG_WARN("port %s: unknown LACP mode %s",
4408 port
->name
, port
->cfg
->lacp
);
4413 static struct lacp_settings
*
4414 port_configure_lacp(struct port
*port
, struct lacp_settings
*s
)
4416 const char *lacp_time
, *system_id
;
4419 if (!enable_lacp(port
, &s
->active
)) {
4423 s
->name
= port
->name
;
4425 system_id
= smap_get(&port
->cfg
->other_config
, "lacp-system-id");
4427 if (!ovs_scan(system_id
, ETH_ADDR_SCAN_FMT
,
4428 ETH_ADDR_SCAN_ARGS(s
->id
))) {
4429 VLOG_WARN("port %s: LACP system ID (%s) must be an Ethernet"
4430 " address.", port
->name
, system_id
);
4434 s
->id
= port
->bridge
->ea
;
4437 if (eth_addr_is_zero(s
->id
)) {
4438 VLOG_WARN("port %s: Invalid zero LACP system ID.", port
->name
);
4442 /* Prefer bondable links if unspecified. */
4443 priority
= smap_get_int(&port
->cfg
->other_config
, "lacp-system-priority",
4445 s
->priority
= (priority
> 0 && priority
<= UINT16_MAX
4447 : UINT16_MAX
- !ovs_list_is_short(&port
->ifaces
));
4449 lacp_time
= smap_get_def(&port
->cfg
->other_config
, "lacp-time", "");
4450 s
->fast
= !strcasecmp(lacp_time
, "fast");
4452 s
->fallback_ab_cfg
= smap_get_bool(&port
->cfg
->other_config
,
4453 "lacp-fallback-ab", false);
4459 iface_configure_lacp(struct iface
*iface
, struct lacp_slave_settings
*s
)
4461 int priority
, portid
, key
;
4463 portid
= smap_get_int(&iface
->cfg
->other_config
, "lacp-port-id", 0);
4464 priority
= smap_get_int(&iface
->cfg
->other_config
, "lacp-port-priority",
4466 key
= smap_get_int(&iface
->cfg
->other_config
, "lacp-aggregation-key", 0);
4468 if (portid
<= 0 || portid
> UINT16_MAX
) {
4469 portid
= ofp_to_u16(iface
->ofp_port
);
4472 if (priority
<= 0 || priority
> UINT16_MAX
) {
4473 priority
= UINT16_MAX
;
4476 if (key
< 0 || key
> UINT16_MAX
) {
4480 s
->name
= iface
->name
;
4482 s
->priority
= priority
;
4487 port_configure_bond(struct port
*port
, struct bond_settings
*s
)
4489 const char *detect_s
;
4490 struct iface
*iface
;
4492 int miimon_interval
;
4494 s
->name
= port
->name
;
4496 if (port
->cfg
->bond_mode
) {
4497 if (!bond_mode_from_string(&s
->balance
, port
->cfg
->bond_mode
)) {
4498 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
4499 port
->name
, port
->cfg
->bond_mode
,
4500 bond_mode_to_string(s
->balance
));
4503 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 1);
4505 /* XXX: Post version 1.5.*, the default bond_mode changed from SLB to
4506 * active-backup. At some point we should remove this warning. */
4507 VLOG_WARN_RL(&rl
, "port %s: Using the default bond_mode %s. Note that"
4508 " in previous versions, the default bond_mode was"
4509 " balance-slb", port
->name
,
4510 bond_mode_to_string(s
->balance
));
4512 if (s
->balance
== BM_SLB
&& port
->bridge
->cfg
->n_flood_vlans
) {
4513 VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
4514 "please use another bond type or disable flood_vlans",
4518 miimon_interval
= smap_get_int(&port
->cfg
->other_config
,
4519 "bond-miimon-interval", 0);
4520 if (miimon_interval
<= 0) {
4521 miimon_interval
= 200;
4524 detect_s
= smap_get(&port
->cfg
->other_config
, "bond-detect-mode");
4525 if (!detect_s
|| !strcmp(detect_s
, "carrier")) {
4526 miimon_interval
= 0;
4527 } else if (strcmp(detect_s
, "miimon")) {
4528 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
4529 "defaulting to carrier", port
->name
, detect_s
);
4530 miimon_interval
= 0;
4533 s
->up_delay
= MAX(0, port
->cfg
->bond_updelay
);
4534 s
->down_delay
= MAX(0, port
->cfg
->bond_downdelay
);
4535 s
->basis
= smap_get_int(&port
->cfg
->other_config
, "bond-hash-basis", 0);
4536 s
->rebalance_interval
= smap_get_int(&port
->cfg
->other_config
,
4537 "bond-rebalance-interval", 10000);
4538 if (s
->rebalance_interval
&& s
->rebalance_interval
< 1000) {
4539 s
->rebalance_interval
= 1000;
4542 s
->lacp_fallback_ab_cfg
= smap_get_bool(&port
->cfg
->other_config
,
4543 "lacp-fallback-ab", false);
4545 LIST_FOR_EACH (iface
, port_elem
, &port
->ifaces
) {
4546 netdev_set_miimon_interval(iface
->netdev
, miimon_interval
);
4549 mac_s
= port
->cfg
->bond_active_slave
;
4550 if (!mac_s
|| !ovs_scan(mac_s
, ETH_ADDR_SCAN_FMT
,
4551 ETH_ADDR_SCAN_ARGS(s
->active_slave_mac
))) {
4552 /* OVSDB did not store the last active interface */
4553 s
->active_slave_mac
= eth_addr_zero
;
4557 /* Returns true if 'port' is synthetic, that is, if we constructed it locally
4558 * instead of obtaining it from the database. */
4560 port_is_synthetic(const struct port
*port
)
4562 return ovsdb_idl_row_is_synthetic(&port
->cfg
->header_
);
4565 /* Interface functions. */
4568 iface_is_internal(const struct ovsrec_interface
*iface
,
4569 const struct ovsrec_bridge
*br
)
4571 /* The local port and "internal" ports are always "internal". */
4572 return !strcmp(iface
->type
, "internal") || !strcmp(iface
->name
, br
->name
);
4575 /* Returns the correct network device type for interface 'iface' in bridge
4578 iface_get_type(const struct ovsrec_interface
*iface
,
4579 const struct ovsrec_bridge
*br
)
4583 /* The local port always has type "internal". Other ports take
4584 * their type from the database and default to "system" if none is
4586 if (iface_is_internal(iface
, br
)) {
4589 type
= iface
->type
[0] ? iface
->type
: "system";
4596 iface_destroy__(struct iface
*iface
)
4599 struct port
*port
= iface
->port
;
4600 struct bridge
*br
= port
->bridge
;
4602 VLOG_INFO("bridge %s: deleted interface %s on port %d",
4603 br
->name
, iface
->name
, iface
->ofp_port
);
4605 if (br
->ofproto
&& iface
->ofp_port
!= OFPP_NONE
) {
4606 ofproto_port_unregister(br
->ofproto
, iface
->ofp_port
);
4609 if (iface
->ofp_port
!= OFPP_NONE
) {
4610 hmap_remove(&br
->ifaces
, &iface
->ofp_port_node
);
4613 ovs_list_remove(&iface
->port_elem
);
4614 hmap_remove(&br
->iface_by_name
, &iface
->name_node
);
4616 tnl_port_map_delete_ipdev(netdev_get_name(iface
->netdev
));
4618 /* The user is changing configuration here, so netdev_remove needs to be
4619 * used as opposed to netdev_close */
4620 netdev_remove(iface
->netdev
);
4628 iface_destroy(struct iface
*iface
)
4631 struct port
*port
= iface
->port
;
4633 iface_destroy__(iface
);
4634 if (ovs_list_is_empty(&port
->ifaces
)) {
4640 static struct iface
*
4641 iface_lookup(const struct bridge
*br
, const char *name
)
4643 struct iface
*iface
;
4645 HMAP_FOR_EACH_WITH_HASH (iface
, name_node
, hash_string(name
, 0),
4646 &br
->iface_by_name
) {
4647 if (!strcmp(iface
->name
, name
)) {
4655 static struct iface
*
4656 iface_find(const char *name
)
4658 const struct bridge
*br
;
4660 HMAP_FOR_EACH (br
, node
, &all_bridges
) {
4661 struct iface
*iface
= iface_lookup(br
, name
);
4670 static struct iface
*
4671 iface_from_ofp_port(const struct bridge
*br
, ofp_port_t ofp_port
)
4673 struct iface
*iface
;
4675 HMAP_FOR_EACH_IN_BUCKET (iface
, ofp_port_node
, hash_ofp_port(ofp_port
),
4677 if (iface
->ofp_port
== ofp_port
) {
4684 /* Set Ethernet address of 'iface', if one is specified in the configuration
4687 iface_set_mac(const struct bridge
*br
, const struct port
*port
, struct iface
*iface
)
4689 struct eth_addr ea
, *mac
= NULL
;
4690 struct iface
*hw_addr_iface
;
4692 if (strcmp(iface
->type
, "internal")) {
4696 if (iface
->cfg
->mac
&& eth_addr_from_string(iface
->cfg
->mac
, &ea
)) {
4698 } else if (port
->cfg
->fake_bridge
) {
4699 /* Fake bridge and no MAC set in the configuration. Pick a local one. */
4700 find_local_hw_addr(br
, &ea
, port
, &hw_addr_iface
);
4705 if (iface
->ofp_port
== OFPP_LOCAL
) {
4706 VLOG_ERR("interface %s: ignoring mac in Interface record "
4707 "(use Bridge record to set local port's mac)",
4709 } else if (eth_addr_is_multicast(*mac
)) {
4710 VLOG_ERR("interface %s: cannot set MAC to multicast address",
4712 } else if (eth_addr_is_zero(*mac
)) {
4713 VLOG_ERR("interface %s: cannot set MAC to all zero address",
4716 int error
= netdev_set_etheraddr(iface
->netdev
, *mac
);
4718 VLOG_ERR("interface %s: setting MAC failed (%s)",
4719 iface
->name
, ovs_strerror(error
));
4725 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
4727 iface_set_ofport(const struct ovsrec_interface
*if_cfg
, ofp_port_t ofport
)
4729 if (if_cfg
&& !ovsdb_idl_row_is_synthetic(&if_cfg
->header_
)) {
4730 int64_t port
= ofport
== OFPP_NONE
? -1 : ofp_to_u16(ofport
);
4731 ovsrec_interface_set_ofport(if_cfg
, &port
, 1);
4735 /* Clears all of the fields in 'if_cfg' that indicate interface status, and
4736 * sets the "ofport" field to -1.
4738 * This is appropriate when 'if_cfg''s interface cannot be created or is
4739 * otherwise invalid. */
4741 iface_clear_db_record(const struct ovsrec_interface
*if_cfg
, char *errp
)
4743 if (!ovsdb_idl_row_is_synthetic(&if_cfg
->header_
)) {
4744 iface_set_ofport(if_cfg
, OFPP_NONE
);
4745 ovsrec_interface_set_error(if_cfg
, errp
);
4746 ovsrec_interface_set_status(if_cfg
, NULL
);
4747 ovsrec_interface_set_admin_state(if_cfg
, NULL
);
4748 ovsrec_interface_set_duplex(if_cfg
, NULL
);
4749 ovsrec_interface_set_link_speed(if_cfg
, NULL
, 0);
4750 ovsrec_interface_set_link_state(if_cfg
, NULL
);
4751 ovsrec_interface_set_mac_in_use(if_cfg
, NULL
);
4752 ovsrec_interface_set_mtu(if_cfg
, NULL
, 0);
4753 ovsrec_interface_set_cfm_fault(if_cfg
, NULL
, 0);
4754 ovsrec_interface_set_cfm_fault_status(if_cfg
, NULL
, 0);
4755 ovsrec_interface_set_cfm_remote_mpids(if_cfg
, NULL
, 0);
4756 ovsrec_interface_set_lacp_current(if_cfg
, NULL
, 0);
4757 ovsrec_interface_set_statistics(if_cfg
, NULL
, NULL
, 0);
4758 ovsrec_interface_set_ifindex(if_cfg
, NULL
, 0);
4763 queue_ids_include(const struct ovsdb_datum
*queues
, int64_t target
)
4765 union ovsdb_atom atom
;
4767 atom
.integer
= target
;
4768 return ovsdb_datum_find_key(queues
, &atom
, OVSDB_TYPE_INTEGER
) != UINT_MAX
;
4772 iface_configure_qos(struct iface
*iface
, const struct ovsrec_qos
*qos
)
4774 struct ofpbuf queues_buf
;
4776 ofpbuf_init(&queues_buf
, 0);
4778 if (!qos
|| qos
->type
[0] == '\0') {
4779 netdev_set_qos(iface
->netdev
, NULL
, NULL
);
4781 const struct ovsdb_datum
*queues
;
4782 struct netdev_queue_dump dump
;
4783 unsigned int queue_id
;
4784 struct smap details
;
4788 /* Configure top-level Qos for 'iface'. */
4789 netdev_set_qos(iface
->netdev
, qos
->type
, &qos
->other_config
);
4791 /* Deconfigure queues that were deleted. */
4792 queues
= ovsrec_qos_get_queues(qos
, OVSDB_TYPE_INTEGER
,
4794 smap_init(&details
);
4795 NETDEV_QUEUE_FOR_EACH (&queue_id
, &details
, &dump
, iface
->netdev
) {
4796 if (!queue_ids_include(queues
, queue_id
)) {
4797 netdev_delete_queue(iface
->netdev
, queue_id
);
4800 smap_destroy(&details
);
4802 /* Configure queues for 'iface'. */
4804 for (i
= 0; i
< qos
->n_queues
; i
++) {
4805 const struct ovsrec_queue
*queue
= qos
->value_queues
[i
];
4806 queue_id
= qos
->key_queues
[i
];
4808 if (queue_id
== 0) {
4812 if (queue
->n_dscp
== 1) {
4813 struct ofproto_port_queue
*port_queue
;
4815 port_queue
= ofpbuf_put_uninit(&queues_buf
,
4816 sizeof *port_queue
);
4817 port_queue
->queue
= queue_id
;
4818 port_queue
->dscp
= queue
->dscp
[0];
4821 netdev_set_queue(iface
->netdev
, queue_id
, &queue
->other_config
);
4824 smap_init(&details
);
4825 netdev_set_queue(iface
->netdev
, 0, &details
);
4826 smap_destroy(&details
);
4830 if (iface
->ofp_port
!= OFPP_NONE
) {
4831 const struct ofproto_port_queue
*port_queues
= queues_buf
.data
;
4832 size_t n_queues
= queues_buf
.size
/ sizeof *port_queues
;
4834 ofproto_port_set_queues(iface
->port
->bridge
->ofproto
, iface
->ofp_port
,
4835 port_queues
, n_queues
);
4838 netdev_set_policing(iface
->netdev
,
4839 MIN(UINT32_MAX
, iface
->cfg
->ingress_policing_rate
),
4840 MIN(UINT32_MAX
, iface
->cfg
->ingress_policing_burst
));
4842 ofpbuf_uninit(&queues_buf
);
4846 iface_configure_cfm(struct iface
*iface
)
4848 const struct ovsrec_interface
*cfg
= iface
->cfg
;
4849 const char *opstate_str
;
4850 const char *cfm_ccm_vlan
;
4851 struct cfm_settings s
;
4852 struct smap netdev_args
;
4854 if (!cfg
->n_cfm_mpid
) {
4855 ofproto_port_clear_cfm(iface
->port
->bridge
->ofproto
, iface
->ofp_port
);
4859 s
.check_tnl_key
= false;
4860 smap_init(&netdev_args
);
4861 if (!netdev_get_config(iface
->netdev
, &netdev_args
)) {
4862 const char *key
= smap_get(&netdev_args
, "key");
4863 const char *in_key
= smap_get(&netdev_args
, "in_key");
4865 s
.check_tnl_key
= (key
&& !strcmp(key
, "flow"))
4866 || (in_key
&& !strcmp(in_key
, "flow"));
4868 smap_destroy(&netdev_args
);
4870 s
.mpid
= *cfg
->cfm_mpid
;
4871 s
.interval
= smap_get_int(&iface
->cfg
->other_config
, "cfm_interval", 0);
4872 cfm_ccm_vlan
= smap_get(&iface
->cfg
->other_config
, "cfm_ccm_vlan");
4873 s
.ccm_pcp
= smap_get_int(&iface
->cfg
->other_config
, "cfm_ccm_pcp", 0);
4875 if (s
.interval
<= 0) {
4879 if (!cfm_ccm_vlan
) {
4881 } else if (!strcasecmp("random", cfm_ccm_vlan
)) {
4882 s
.ccm_vlan
= CFM_RANDOM_VLAN
;
4884 s
.ccm_vlan
= atoi(cfm_ccm_vlan
);
4885 if (s
.ccm_vlan
== CFM_RANDOM_VLAN
) {
4890 s
.extended
= smap_get_bool(&iface
->cfg
->other_config
, "cfm_extended",
4892 s
.demand
= smap_get_bool(&iface
->cfg
->other_config
, "cfm_demand", false);
4894 opstate_str
= smap_get(&iface
->cfg
->other_config
, "cfm_opstate");
4895 s
.opup
= !opstate_str
|| !strcasecmp("up", opstate_str
);
4897 ofproto_port_set_cfm(iface
->port
->bridge
->ofproto
, iface
->ofp_port
, &s
);
4900 /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
4901 * instead of obtaining it from the database. */
4903 iface_is_synthetic(const struct iface
*iface
)
4905 return ovsdb_idl_row_is_synthetic(&iface
->cfg
->header_
);
4909 iface_validate_ofport__(size_t n
, int64_t *ofport
)
4911 return (n
&& *ofport
>= 1 && *ofport
< ofp_to_u16(OFPP_MAX
)
4912 ? u16_to_ofp(*ofport
)
4917 iface_get_requested_ofp_port(const struct ovsrec_interface
*cfg
)
4919 return iface_validate_ofport__(cfg
->n_ofport_request
, cfg
->ofport_request
);
4923 iface_pick_ofport(const struct ovsrec_interface
*cfg
)
4925 ofp_port_t requested_ofport
= iface_get_requested_ofp_port(cfg
);
4926 return (requested_ofport
!= OFPP_NONE
4928 : iface_validate_ofport__(cfg
->n_ofport
, cfg
->ofport
));
4931 /* Port mirroring. */
4933 static struct mirror
*
4934 mirror_find_by_uuid(struct bridge
*br
, const struct uuid
*uuid
)
4938 HMAP_FOR_EACH_IN_BUCKET (m
, hmap_node
, uuid_hash(uuid
), &br
->mirrors
) {
4939 if (uuid_equals(uuid
, &m
->uuid
)) {
4947 bridge_configure_mirrors(struct bridge
*br
)
4949 const struct ovsdb_datum
*mc
;
4950 unsigned long *flood_vlans
;
4951 struct mirror
*m
, *next
;
4954 /* Get rid of deleted mirrors. */
4955 mc
= ovsrec_bridge_get_mirrors(br
->cfg
, OVSDB_TYPE_UUID
);
4956 HMAP_FOR_EACH_SAFE (m
, next
, hmap_node
, &br
->mirrors
) {
4957 union ovsdb_atom atom
;
4959 atom
.uuid
= m
->uuid
;
4960 if (ovsdb_datum_find_key(mc
, &atom
, OVSDB_TYPE_UUID
) == UINT_MAX
) {
4965 /* Add new mirrors and reconfigure existing ones. */
4966 for (i
= 0; i
< br
->cfg
->n_mirrors
; i
++) {
4967 const struct ovsrec_mirror
*cfg
= br
->cfg
->mirrors
[i
];
4968 m
= mirror_find_by_uuid(br
, &cfg
->header_
.uuid
);
4970 m
= mirror_create(br
, cfg
);
4973 if (!mirror_configure(m
)) {
4978 /* Update flooded vlans (for RSPAN). */
4979 flood_vlans
= vlan_bitmap_from_array(br
->cfg
->flood_vlans
,
4980 br
->cfg
->n_flood_vlans
);
4981 ofproto_set_flood_vlans(br
->ofproto
, flood_vlans
);
4982 bitmap_free(flood_vlans
);
4985 static struct mirror
*
4986 mirror_create(struct bridge
*br
, const struct ovsrec_mirror
*cfg
)
4990 m
= xzalloc(sizeof *m
);
4991 m
->uuid
= cfg
->header_
.uuid
;
4992 hmap_insert(&br
->mirrors
, &m
->hmap_node
, uuid_hash(&m
->uuid
));
4994 m
->name
= xstrdup(cfg
->name
);
5000 mirror_destroy(struct mirror
*m
)
5003 struct bridge
*br
= m
->bridge
;
5006 ofproto_mirror_unregister(br
->ofproto
, m
);
5009 hmap_remove(&br
->mirrors
, &m
->hmap_node
);
5016 mirror_collect_ports(struct mirror
*m
,
5017 struct ovsrec_port
**in_ports
, int n_in_ports
,
5018 void ***out_portsp
, size_t *n_out_portsp
)
5020 void **out_ports
= xmalloc(n_in_ports
* sizeof *out_ports
);
5021 size_t n_out_ports
= 0;
5024 for (i
= 0; i
< n_in_ports
; i
++) {
5025 const char *name
= in_ports
[i
]->name
;
5026 struct port
*port
= port_lookup(m
->bridge
, name
);
5028 out_ports
[n_out_ports
++] = port
;
5030 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
5031 "port %s", m
->bridge
->name
, m
->name
, name
);
5034 *out_portsp
= out_ports
;
5035 *n_out_portsp
= n_out_ports
;
5039 mirror_configure(struct mirror
*m
)
5041 const struct ovsrec_mirror
*cfg
= m
->cfg
;
5042 struct ofproto_mirror_settings s
;
5045 if (strcmp(cfg
->name
, m
->name
)) {
5047 m
->name
= xstrdup(cfg
->name
);
5051 /* Get output port or VLAN. */
5052 if (cfg
->output_port
) {
5053 s
.out_bundle
= port_lookup(m
->bridge
, cfg
->output_port
->name
);
5054 if (!s
.out_bundle
) {
5055 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
5056 m
->bridge
->name
, m
->name
);
5059 s
.out_vlan
= UINT16_MAX
;
5061 if (cfg
->output_vlan
) {
5062 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
5063 "output vlan; ignoring output vlan",
5064 m
->bridge
->name
, m
->name
);
5066 } else if (cfg
->output_vlan
) {
5067 /* The database should prevent invalid VLAN values. */
5068 s
.out_bundle
= NULL
;
5069 s
.out_vlan
= *cfg
->output_vlan
;
5071 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
5072 m
->bridge
->name
, m
->name
);
5077 s
.snaplen
= *cfg
->snaplen
;
5082 /* Get port selection. */
5083 if (cfg
->select_all
) {
5084 size_t n_ports
= hmap_count(&m
->bridge
->ports
);
5085 void **ports
= xmalloc(n_ports
* sizeof *ports
);
5090 HMAP_FOR_EACH (port
, hmap_node
, &m
->bridge
->ports
) {
5100 /* Get ports, dropping ports that don't exist.
5101 * The IDL ensures that there are no duplicates. */
5102 mirror_collect_ports(m
, cfg
->select_src_port
, cfg
->n_select_src_port
,
5103 &s
.srcs
, &s
.n_srcs
);
5104 mirror_collect_ports(m
, cfg
->select_dst_port
, cfg
->n_select_dst_port
,
5105 &s
.dsts
, &s
.n_dsts
);
5108 /* Get VLAN selection. */
5109 s
.src_vlans
= vlan_bitmap_from_array(cfg
->select_vlan
, cfg
->n_select_vlan
);
5112 ofproto_mirror_register(m
->bridge
->ofproto
, m
, &s
);
5115 if (s
.srcs
!= s
.dsts
) {
5126 mirror_refresh_stats(struct mirror
*m
)
5128 struct ofproto
*ofproto
= m
->bridge
->ofproto
;
5129 uint64_t tx_packets
, tx_bytes
;
5130 const char *keys
[2];
5132 size_t stat_cnt
= 0;
5134 if (ofproto_mirror_get_stats(ofproto
, m
, &tx_packets
, &tx_bytes
)) {
5135 ovsrec_mirror_set_statistics(m
->cfg
, NULL
, NULL
, 0);
5139 if (tx_packets
!= UINT64_MAX
) {
5140 keys
[stat_cnt
] = "tx_packets";
5141 values
[stat_cnt
] = tx_packets
;
5144 if (tx_bytes
!= UINT64_MAX
) {
5145 keys
[stat_cnt
] = "tx_bytes";
5146 values
[stat_cnt
] = tx_bytes
;
5150 ovsrec_mirror_set_statistics(m
->cfg
, keys
, values
, stat_cnt
);
5154 * Add registered netdev and dpif types to ovsdb to allow external
5155 * applications to query the capabilities of the Open vSwitch instance
5156 * running on the node.
5159 discover_types(const struct ovsrec_open_vswitch
*cfg
)
5163 /* Datapath types. */
5165 dp_enumerate_types(&types
);
5166 const char **datapath_types
= sset_array(&types
);
5167 ovsrec_open_vswitch_set_datapath_types(cfg
, datapath_types
,
5168 sset_count(&types
));
5169 free(datapath_types
);
5170 sset_destroy(&types
);
5174 netdev_enumerate_types(&types
);
5175 const char **iface_types
= sset_array(&types
);
5176 ovsrec_open_vswitch_set_iface_types(cfg
, iface_types
, sset_count(&types
));
5178 sset_destroy(&types
);