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