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