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