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