1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
28 #include "dynamic-string.h"
35 #include "mac-learning.h"
36 #include "meta-flow.h"
38 #include "ofp-print.h"
40 #include "ofproto/ofproto.h"
41 #include "poll-loop.h"
44 #include "socket-util.h"
46 #include "stream-ssl.h"
48 #include "system-stats.h"
53 #include "lib/vswitch-idl.h"
54 #include "xenserver.h"
56 #include "sflow_api.h"
57 #include "vlan-bitmap.h"
59 VLOG_DEFINE_THIS_MODULE(bridge);
61 COVERAGE_DEFINE(bridge_reconfigure);
64 /* These members are always valid. */
65 struct list port_elem; /* Element in struct port's "ifaces" list. */
66 struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
67 struct port *port; /* Containing port. */
68 char *name; /* Host network device name. */
69 tag_type tag; /* Tag associated with this interface. */
71 /* These members are valid only after bridge_reconfigure() causes them to
73 struct hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
74 int ofp_port; /* OpenFlow port number, -1 if unknown. */
75 struct netdev *netdev; /* Network device. */
76 const char *type; /* Usually same as cfg->type. */
77 const struct ovsrec_interface *cfg;
78 bool need_refresh; /* Refresh iface after create. */
82 struct uuid uuid; /* UUID of this "mirror" record in database. */
83 struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
84 struct bridge *bridge;
86 const struct ovsrec_mirror *cfg;
90 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
91 struct bridge *bridge;
94 const struct ovsrec_port *cfg;
96 /* An ordinary bridge port has 1 interface.
97 * A bridge port for bonding has at least 2 interfaces. */
98 struct list ifaces; /* List of "struct iface"s. */
102 struct hmap_node node; /* In 'all_bridges'. */
103 char *name; /* User-specified arbitrary name. */
104 char *type; /* Datapath type. */
105 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
106 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
107 const struct ovsrec_bridge *cfg;
109 /* OpenFlow switch processing. */
110 struct ofproto *ofproto; /* OpenFlow switch. */
113 struct hmap ports; /* "struct port"s indexed by name. */
114 struct hmap ifaces; /* "struct iface"s indexed by ofp_port. */
115 struct hmap iface_by_name; /* "struct iface"s indexed by name. */
117 /* Port mirroring. */
118 struct hmap mirrors; /* "struct mirror" indexed by UUID. */
120 /* Synthetic local port if necessary. */
121 struct ovsrec_port synth_local_port;
122 struct ovsrec_interface synth_local_iface;
123 struct ovsrec_interface *synth_local_ifacep;
126 /* All bridges, indexed by name. */
127 static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
129 /* OVSDB IDL used to obtain configuration. */
130 static struct ovsdb_idl *idl;
132 /* Most recently processed IDL sequence number. */
133 static unsigned int idl_seqno;
135 /* Each time this timer expires, the bridge fetches systems and interface
136 * statistics and pushes them into the database. */
137 #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
138 static long long int stats_timer = LLONG_MIN;
140 /* Stores the time after which rate limited statistics may be written to the
141 * database. Only updated when changes to the database require rate limiting.
143 #define DB_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
144 static long long int db_limiter = LLONG_MIN;
146 /* In some datapaths, creating and destroying OpenFlow ports can be extremely
147 * expensive. This can cause bridge_reconfigure() to take a long time during
148 * which no other work can be done. To deal with this problem, we limit port
149 * adds and deletions to a window of OFP_PORT_ACTION_WINDOW milliseconds per
150 * call to bridge_reconfigure(). If there is more work to do after the limit
151 * is reached, 'need_reconfigure', is flagged and it's done on the next loop.
152 * This allows the rest of the code to catch up on important things like
153 * forwarding packets. */
154 #define OFP_PORT_ACTION_WINDOW 10
155 static bool need_reconfigure = false;
157 static void add_del_bridges(const struct ovsrec_open_vswitch *);
158 static void bridge_del_ofprotos(void);
159 static bool bridge_add_ofprotos(struct bridge *);
160 static void bridge_create(const struct ovsrec_bridge *);
161 static void bridge_destroy(struct bridge *);
162 static struct bridge *bridge_lookup(const char *name);
163 static unixctl_cb_func bridge_unixctl_dump_flows;
164 static unixctl_cb_func bridge_unixctl_reconnect;
165 static size_t bridge_get_controllers(const struct bridge *br,
166 struct ovsrec_controller ***controllersp);
167 static void bridge_add_del_ports(struct bridge *,
168 const unsigned long int *splinter_vlans);
169 static void bridge_add_ofproto_ports(struct bridge *,
170 long long int *port_action_timer);
171 static void bridge_del_ofproto_ports(struct bridge *,
172 long long int *port_action_timer);
173 static void bridge_refresh_ofp_port(struct bridge *);
174 static void bridge_configure_datapath_id(struct bridge *);
175 static void bridge_configure_flow_eviction_threshold(struct bridge *);
176 static void bridge_configure_netflow(struct bridge *);
177 static void bridge_configure_forward_bpdu(struct bridge *);
178 static void bridge_configure_mac_idle_time(struct bridge *);
179 static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
180 static void bridge_configure_stp(struct bridge *);
181 static void bridge_configure_tables(struct bridge *);
182 static void bridge_configure_remotes(struct bridge *,
183 const struct sockaddr_in *managers,
185 static void bridge_pick_local_hw_addr(struct bridge *,
186 uint8_t ea[ETH_ADDR_LEN],
187 struct iface **hw_addr_iface);
188 static uint64_t bridge_pick_datapath_id(struct bridge *,
189 const uint8_t bridge_ea[ETH_ADDR_LEN],
190 struct iface *hw_addr_iface);
191 static uint64_t dpid_from_hash(const void *, size_t nbytes);
192 static bool bridge_has_bond_fake_iface(const struct bridge *,
194 static bool port_is_bond_fake_iface(const struct port *);
196 static unixctl_cb_func qos_unixctl_show;
198 static struct port *port_create(struct bridge *, const struct ovsrec_port *);
199 static void port_add_ifaces(struct port *);
200 static void port_del_ifaces(struct port *);
201 static void port_destroy(struct port *);
202 static struct port *port_lookup(const struct bridge *, const char *name);
203 static void port_configure(struct port *);
204 static struct lacp_settings *port_configure_lacp(struct port *,
205 struct lacp_settings *);
206 static void port_configure_bond(struct port *, struct bond_settings *,
207 uint32_t *bond_stable_ids);
208 static bool port_is_synthetic(const struct port *);
210 static void bridge_configure_mirrors(struct bridge *);
211 static struct mirror *mirror_create(struct bridge *,
212 const struct ovsrec_mirror *);
213 static void mirror_destroy(struct mirror *);
214 static bool mirror_configure(struct mirror *);
215 static void mirror_refresh_stats(struct mirror *);
217 static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
218 static struct iface *iface_create(struct port *port,
219 const struct ovsrec_interface *if_cfg);
220 static void iface_destroy(struct iface *);
221 static struct iface *iface_lookup(const struct bridge *, const char *name);
222 static struct iface *iface_find(const char *name);
223 static struct iface *iface_from_ofp_port(const struct bridge *,
225 static void iface_set_mac(struct iface *);
226 static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
227 static void iface_clear_db_record(const struct ovsrec_interface *if_cfg);
228 static void iface_configure_qos(struct iface *, const struct ovsrec_qos *);
229 static void iface_configure_cfm(struct iface *);
230 static void iface_refresh_cfm_stats(struct iface *);
231 static void iface_refresh_stats(struct iface *);
232 static void iface_refresh_status(struct iface *);
233 static bool iface_is_synthetic(const struct iface *);
234 static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
236 static void shash_to_ovs_idl_map(struct shash *,
237 char ***keys, char ***values, size_t *n);
239 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
241 * This is deprecated. It is only for compatibility with broken device drivers
242 * in old versions of Linux that do not properly support VLANs when VLAN
243 * devices are not used. When broken device drivers are no longer in
244 * widespread use, we will delete these interfaces. */
246 /* True if VLAN splinters are enabled on any interface, false otherwise.*/
247 static bool vlan_splinters_enabled_anywhere;
249 static bool vlan_splinters_is_enabled(const struct ovsrec_interface *);
250 static unsigned long int *collect_splinter_vlans(
251 const struct ovsrec_open_vswitch *);
252 static void configure_splinter_port(struct port *);
253 static void add_vlan_splinter_ports(struct bridge *,
254 const unsigned long int *splinter_vlans,
255 struct shash *ports);
257 /* Public functions. */
259 /* Initializes the bridge module, configuring it to obtain its configuration
260 * from an OVSDB server accessed over 'remote', which should be a string in a
261 * form acceptable to ovsdb_idl_create(). */
263 bridge_init(const char *remote)
265 /* Create connection to database. */
266 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
267 idl_seqno = ovsdb_idl_get_seqno(idl);
268 ovsdb_idl_set_lock(idl, "ovs_vswitchd");
270 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
271 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
272 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
273 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
274 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
275 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
276 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
278 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
279 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_status);
280 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
282 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_status);
283 ovsdb_idl_omit_alert(idl, &ovsrec_port_col_statistics);
284 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
285 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
287 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
288 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
289 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
290 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
291 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_resets);
292 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
293 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
294 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
295 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
296 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault);
297 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault_status);
298 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_mpids);
299 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_health);
300 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_lacp_current);
301 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
303 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
304 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
305 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
306 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
308 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
310 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
312 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
313 ovsdb_idl_omit_alert(idl, &ovsrec_mirror_col_statistics);
315 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
317 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
319 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
320 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
321 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
322 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
323 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
325 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
327 /* Register unixctl commands. */
328 unixctl_command_register("qos/show", "interface", 1, 1,
329 qos_unixctl_show, NULL);
330 unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
331 bridge_unixctl_dump_flows, NULL);
332 unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
333 bridge_unixctl_reconnect, NULL);
343 struct bridge *br, *next_br;
345 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
348 ovsdb_idl_destroy(idl);
351 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
352 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
353 * responsible for freeing '*managersp' (with free()).
355 * You may be asking yourself "why does ovs-vswitchd care?", because
356 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
357 * should not be and in fact is not directly involved in that. But
358 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
359 * it has to tell in-band control where the managers are to enable that.
360 * (Thus, only managers connected in-band are collected.)
363 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
364 struct sockaddr_in **managersp, size_t *n_managersp)
366 struct sockaddr_in *managers = NULL;
367 size_t n_managers = 0;
371 /* Collect all of the potential targets from the "targets" columns of the
372 * rows pointed to by "manager_options", excluding any that are
375 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
376 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
378 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
379 sset_find_and_delete(&targets, m->target);
381 sset_add(&targets, m->target);
385 /* Now extract the targets' IP addresses. */
386 if (!sset_is_empty(&targets)) {
389 managers = xmalloc(sset_count(&targets) * sizeof *managers);
390 SSET_FOR_EACH (target, &targets) {
391 struct sockaddr_in *sin = &managers[n_managers];
393 if (stream_parse_target_with_default_ports(target,
401 sset_destroy(&targets);
403 *managersp = managers;
404 *n_managersp = n_managers;
408 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
410 unsigned long int *splinter_vlans;
411 long long int port_action_timer;
412 struct sockaddr_in *managers;
413 struct bridge *br, *next;
414 int sflow_bridge_number;
417 COVERAGE_INC(bridge_reconfigure);
419 port_action_timer = LLONG_MAX;
420 need_reconfigure = false;
422 /* Create and destroy "struct bridge"s, "struct port"s, and "struct
423 * iface"s according to 'ovs_cfg', with only very minimal configuration
426 * This is mostly an update to bridge data structures. Very little is
427 * pushed down to ofproto or lower layers. */
428 add_del_bridges(ovs_cfg);
429 splinter_vlans = collect_splinter_vlans(ovs_cfg);
430 HMAP_FOR_EACH (br, node, &all_bridges) {
431 bridge_add_del_ports(br, splinter_vlans);
433 free(splinter_vlans);
435 /* Delete all datapaths and datapath ports that are no longer configured.
437 * The kernel will reject any attempt to add a given port to a datapath if
438 * that port already belongs to a different datapath, so we must do all
439 * port deletions before any port additions. A datapath always has a
440 * "local port" so we must delete not-configured datapaths too. */
441 bridge_del_ofprotos();
442 HMAP_FOR_EACH (br, node, &all_bridges) {
444 bridge_del_ofproto_ports(br, &port_action_timer);
448 /* Create datapaths and datapath ports that are missing.
450 * After this is done, we have our final set of bridges, ports, and
451 * interfaces. Every "struct bridge" has an ofproto, every "struct port"
452 * has at least one iface, every "struct iface" has a valid ofp_port and
454 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
456 if (bridge_add_ofprotos(br)) {
457 bridge_del_ofproto_ports(br, &port_action_timer);
463 HMAP_FOR_EACH (br, node, &all_bridges) {
464 bridge_refresh_ofp_port(br);
465 bridge_add_ofproto_ports(br, &port_action_timer);
468 /* Complete the configuration. */
469 sflow_bridge_number = 0;
470 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
471 HMAP_FOR_EACH (br, node, &all_bridges) {
474 /* We need the datapath ID early to allow LACP ports to use it as the
475 * default system ID. */
476 bridge_configure_datapath_id(br);
478 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
481 port_configure(port);
483 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
484 iface_configure_cfm(iface);
485 iface_configure_qos(iface, port->cfg->qos);
486 iface_set_mac(iface);
489 bridge_configure_mirrors(br);
490 bridge_configure_flow_eviction_threshold(br);
491 bridge_configure_forward_bpdu(br);
492 bridge_configure_mac_idle_time(br);
493 bridge_configure_remotes(br, managers, n_managers);
494 bridge_configure_netflow(br);
495 bridge_configure_sflow(br, &sflow_bridge_number);
496 bridge_configure_stp(br);
497 bridge_configure_tables(br);
501 if (!need_reconfigure) {
502 /* ovs-vswitchd has completed initialization, so allow the process that
503 * forked us to exit successfully. */
504 daemonize_complete();
508 /* Iterate over all ofprotos and delete any of them that do not have a
509 * configured bridge or that are the wrong type. */
511 bridge_del_ofprotos(void)
519 ofproto_enumerate_types(&types);
520 SSET_FOR_EACH (type, &types) {
523 ofproto_enumerate_names(type, &names);
524 SSET_FOR_EACH (name, &names) {
525 struct bridge *br = bridge_lookup(name);
526 if (!br || strcmp(type, br->type)) {
527 ofproto_delete(name, type);
531 sset_destroy(&names);
532 sset_destroy(&types);
536 bridge_add_ofprotos(struct bridge *br)
538 int error = ofproto_create(br->name, br->type, &br->ofproto);
540 VLOG_ERR("failed to create bridge %s: %s", br->name, strerror(error));
547 port_configure(struct port *port)
549 const struct ovsrec_port *cfg = port->cfg;
550 struct bond_settings bond_settings;
551 struct lacp_settings lacp_settings;
552 struct ofproto_bundle_settings s;
555 if (cfg->vlan_mode && !strcmp(cfg->vlan_mode, "splinter")) {
556 configure_splinter_port(port);
565 s.slaves = xmalloc(list_size(&port->ifaces) * sizeof *s.slaves);
566 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
567 s.slaves[s.n_slaves++] = iface->ofp_port;
572 if (cfg->tag && *cfg->tag >= 0 && *cfg->tag <= 4095) {
576 /* Get VLAN trunks. */
579 s.trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
583 if (cfg->vlan_mode) {
584 if (!strcmp(cfg->vlan_mode, "access")) {
585 s.vlan_mode = PORT_VLAN_ACCESS;
586 } else if (!strcmp(cfg->vlan_mode, "trunk")) {
587 s.vlan_mode = PORT_VLAN_TRUNK;
588 } else if (!strcmp(cfg->vlan_mode, "native-tagged")) {
589 s.vlan_mode = PORT_VLAN_NATIVE_TAGGED;
590 } else if (!strcmp(cfg->vlan_mode, "native-untagged")) {
591 s.vlan_mode = PORT_VLAN_NATIVE_UNTAGGED;
593 /* This "can't happen" because ovsdb-server should prevent it. */
594 VLOG_ERR("unknown VLAN mode %s", cfg->vlan_mode);
595 s.vlan_mode = PORT_VLAN_TRUNK;
599 s.vlan_mode = PORT_VLAN_ACCESS;
601 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
605 s.vlan_mode = PORT_VLAN_TRUNK;
608 s.use_priority_tags = !strcmp("true", ovsrec_port_get_other_config_value(
609 cfg, "priority-tags", ""));
611 /* Get LACP settings. */
612 s.lacp = port_configure_lacp(port, &lacp_settings);
616 s.lacp_slaves = xmalloc(s.n_slaves * sizeof *s.lacp_slaves);
617 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
618 iface_configure_lacp(iface, &s.lacp_slaves[i++]);
621 s.lacp_slaves = NULL;
624 /* Get bond settings. */
625 if (s.n_slaves > 1) {
626 s.bond = &bond_settings;
627 s.bond_stable_ids = xmalloc(s.n_slaves * sizeof *s.bond_stable_ids);
628 port_configure_bond(port, &bond_settings, s.bond_stable_ids);
631 s.bond_stable_ids = NULL;
633 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
634 netdev_set_miimon_interval(iface->netdev, 0);
639 ofproto_bundle_register(port->bridge->ofproto, port, &s);
645 free(s.bond_stable_ids);
648 /* Pick local port hardware address and datapath ID for 'br'. */
650 bridge_configure_datapath_id(struct bridge *br)
652 uint8_t ea[ETH_ADDR_LEN];
654 struct iface *local_iface;
655 struct iface *hw_addr_iface;
658 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
659 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
661 int error = netdev_set_etheraddr(local_iface->netdev, ea);
663 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
664 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
665 "Ethernet address: %s",
666 br->name, strerror(error));
669 memcpy(br->ea, ea, ETH_ADDR_LEN);
671 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
672 ofproto_set_datapath_id(br->ofproto, dpid);
674 dpid_string = xasprintf("%016"PRIx64, dpid);
675 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
679 /* Set NetFlow configuration on 'br'. */
681 bridge_configure_netflow(struct bridge *br)
683 struct ovsrec_netflow *cfg = br->cfg->netflow;
684 struct netflow_options opts;
687 ofproto_set_netflow(br->ofproto, NULL);
691 memset(&opts, 0, sizeof opts);
693 /* Get default NetFlow configuration from datapath.
694 * Apply overrides from 'cfg'. */
695 ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
696 if (cfg->engine_type) {
697 opts.engine_type = *cfg->engine_type;
699 if (cfg->engine_id) {
700 opts.engine_id = *cfg->engine_id;
703 /* Configure active timeout interval. */
704 opts.active_timeout = cfg->active_timeout;
705 if (!opts.active_timeout) {
706 opts.active_timeout = -1;
707 } else if (opts.active_timeout < 0) {
708 VLOG_WARN("bridge %s: active timeout interval set to negative "
709 "value, using default instead (%d seconds)", br->name,
710 NF_ACTIVE_TIMEOUT_DEFAULT);
711 opts.active_timeout = -1;
714 /* Add engine ID to interface number to disambiguate bridgs? */
715 opts.add_id_to_iface = cfg->add_id_to_interface;
716 if (opts.add_id_to_iface) {
717 if (opts.engine_id > 0x7f) {
718 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
719 "another vswitch, choose an engine id less than 128",
722 if (hmap_count(&br->ports) > 508) {
723 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
724 "another port when more than 508 ports are used",
730 sset_init(&opts.collectors);
731 sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
734 if (ofproto_set_netflow(br->ofproto, &opts)) {
735 VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
737 sset_destroy(&opts.collectors);
740 /* Set sFlow configuration on 'br'. */
742 bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
744 const struct ovsrec_sflow *cfg = br->cfg->sflow;
745 struct ovsrec_controller **controllers;
746 struct ofproto_sflow_options oso;
747 size_t n_controllers;
751 ofproto_set_sflow(br->ofproto, NULL);
755 memset(&oso, 0, sizeof oso);
757 sset_init(&oso.targets);
758 sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
760 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
762 oso.sampling_rate = *cfg->sampling;
765 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
767 oso.polling_interval = *cfg->polling;
770 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
772 oso.header_len = *cfg->header;
775 oso.sub_id = (*sflow_bridge_number)++;
776 oso.agent_device = cfg->agent;
778 oso.control_ip = NULL;
779 n_controllers = bridge_get_controllers(br, &controllers);
780 for (i = 0; i < n_controllers; i++) {
781 if (controllers[i]->local_ip) {
782 oso.control_ip = controllers[i]->local_ip;
786 ofproto_set_sflow(br->ofproto, &oso);
788 sset_destroy(&oso.targets);
792 port_configure_stp(const struct ofproto *ofproto, struct port *port,
793 struct ofproto_port_stp_settings *port_s,
794 int *port_num_counter, unsigned long *port_num_bitmap)
796 const char *config_str;
799 config_str = ovsrec_port_get_other_config_value(port->cfg, "stp-enable",
801 if (config_str && !strcmp(config_str, "false")) {
802 port_s->enable = false;
805 port_s->enable = true;
808 /* STP over bonds is not supported. */
809 if (!list_is_singleton(&port->ifaces)) {
810 VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
812 port_s->enable = false;
816 iface = CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
818 /* Internal ports shouldn't participate in spanning tree, so
820 if (!strcmp(iface->type, "internal")) {
821 VLOG_DBG("port %s: disable STP on internal ports", port->name);
822 port_s->enable = false;
826 /* STP on mirror output ports is not supported. */
827 if (ofproto_is_mirror_output_bundle(ofproto, port)) {
828 VLOG_DBG("port %s: disable STP on mirror ports", port->name);
829 port_s->enable = false;
833 config_str = ovsrec_port_get_other_config_value(port->cfg, "stp-port-num",
836 unsigned long int port_num = strtoul(config_str, NULL, 0);
837 int port_idx = port_num - 1;
839 if (port_num < 1 || port_num > STP_MAX_PORTS) {
840 VLOG_ERR("port %s: invalid stp-port-num", port->name);
841 port_s->enable = false;
845 if (bitmap_is_set(port_num_bitmap, port_idx)) {
846 VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
847 port->name, port_num);
848 port_s->enable = false;
851 bitmap_set1(port_num_bitmap, port_idx);
852 port_s->port_num = port_idx;
854 if (*port_num_counter > STP_MAX_PORTS) {
855 VLOG_ERR("port %s: too many STP ports, disabling", port->name);
856 port_s->enable = false;
860 port_s->port_num = (*port_num_counter)++;
863 config_str = ovsrec_port_get_other_config_value(port->cfg, "stp-path-cost",
866 port_s->path_cost = strtoul(config_str, NULL, 10);
868 enum netdev_features current;
870 if (netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL)) {
871 /* Couldn't get speed, so assume 100Mb/s. */
872 port_s->path_cost = 19;
876 mbps = netdev_features_to_bps(current) / 1000000;
877 port_s->path_cost = stp_convert_speed_to_cost(mbps);
881 config_str = ovsrec_port_get_other_config_value(port->cfg,
885 port_s->priority = strtoul(config_str, NULL, 0);
887 port_s->priority = STP_DEFAULT_PORT_PRIORITY;
891 /* Set spanning tree configuration on 'br'. */
893 bridge_configure_stp(struct bridge *br)
895 if (!br->cfg->stp_enable) {
896 ofproto_set_stp(br->ofproto, NULL);
898 struct ofproto_stp_settings br_s;
899 const char *config_str;
901 int port_num_counter;
902 unsigned long *port_num_bitmap;
904 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
908 uint8_t ea[ETH_ADDR_LEN];
910 if (eth_addr_from_string(config_str, ea)) {
911 br_s.system_id = eth_addr_to_uint64(ea);
913 br_s.system_id = eth_addr_to_uint64(br->ea);
914 VLOG_ERR("bridge %s: invalid stp-system-id, defaulting "
915 "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
918 br_s.system_id = eth_addr_to_uint64(br->ea);
921 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
925 br_s.priority = strtoul(config_str, NULL, 0);
927 br_s.priority = STP_DEFAULT_BRIDGE_PRIORITY;
930 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
934 br_s.hello_time = strtoul(config_str, NULL, 10) * 1000;
936 br_s.hello_time = STP_DEFAULT_HELLO_TIME;
939 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
943 br_s.max_age = strtoul(config_str, NULL, 10) * 1000;
945 br_s.max_age = STP_DEFAULT_MAX_AGE;
948 config_str = ovsrec_bridge_get_other_config_value(br->cfg,
952 br_s.fwd_delay = strtoul(config_str, NULL, 10) * 1000;
954 br_s.fwd_delay = STP_DEFAULT_FWD_DELAY;
957 /* Configure STP on the bridge. */
958 if (ofproto_set_stp(br->ofproto, &br_s)) {
959 VLOG_ERR("bridge %s: could not enable STP", br->name);
963 /* Users must either set the port number with the "stp-port-num"
964 * configuration on all ports or none. If manual configuration
965 * is not done, then we allocate them sequentially. */
966 port_num_counter = 0;
967 port_num_bitmap = bitmap_allocate(STP_MAX_PORTS);
968 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
969 struct ofproto_port_stp_settings port_s;
972 port_configure_stp(br->ofproto, port, &port_s,
973 &port_num_counter, port_num_bitmap);
975 /* As bonds are not supported, just apply configuration to
977 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
978 if (ofproto_port_set_stp(br->ofproto, iface->ofp_port,
980 VLOG_ERR("port %s: could not enable STP", port->name);
986 if (bitmap_scan(port_num_bitmap, 0, STP_MAX_PORTS) != STP_MAX_PORTS
987 && port_num_counter) {
988 VLOG_ERR("bridge %s: must manually configure all STP port "
989 "IDs or none, disabling", br->name);
990 ofproto_set_stp(br->ofproto, NULL);
992 bitmap_free(port_num_bitmap);
997 bridge_has_bond_fake_iface(const struct bridge *br, const char *name)
999 const struct port *port = port_lookup(br, name);
1000 return port && port_is_bond_fake_iface(port);
1004 port_is_bond_fake_iface(const struct port *port)
1006 return port->cfg->bond_fake_iface && !list_is_short(&port->ifaces);
1010 add_del_bridges(const struct ovsrec_open_vswitch *cfg)
1012 struct bridge *br, *next;
1013 struct shash new_br;
1016 /* Collect new bridges' names and types. */
1017 shash_init(&new_br);
1018 for (i = 0; i < cfg->n_bridges; i++) {
1019 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1020 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
1022 if (strchr(br_cfg->name, '/')) {
1023 /* Prevent remote ovsdb-server users from accessing arbitrary
1024 * directories, e.g. consider a bridge named "../../../etc/". */
1025 VLOG_WARN_RL(&rl, "ignoring bridge with invalid name \"%s\"",
1027 } else if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
1028 VLOG_WARN_RL(&rl, "bridge %s specified twice", br_cfg->name);
1032 /* Get rid of deleted bridges or those whose types have changed.
1033 * Update 'cfg' of bridges that still exist. */
1034 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
1035 br->cfg = shash_find_data(&new_br, br->name);
1036 if (!br->cfg || strcmp(br->type, ofproto_normalize_type(
1037 br->cfg->datapath_type))) {
1042 /* Add new bridges. */
1043 for (i = 0; i < cfg->n_bridges; i++) {
1044 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
1045 struct bridge *br = bridge_lookup(br_cfg->name);
1047 bridge_create(br_cfg);
1051 shash_destroy(&new_br);
1055 may_port_action(long long int *port_action_timer)
1057 if (need_reconfigure) {
1062 if (*port_action_timer == LLONG_MAX) {
1063 *port_action_timer = time_msec() + OFP_PORT_ACTION_WINDOW;
1064 } else if (time_msec() > *port_action_timer) {
1065 need_reconfigure = true;
1071 /* Delete each ofproto port on 'br' that doesn't have a corresponding "struct
1074 * The kernel will reject any attempt to add a given port to a datapath if that
1075 * port already belongs to a different datapath, so we must do all port
1076 * deletions before any port additions. */
1078 bridge_del_ofproto_ports(struct bridge *br,
1079 long long int *port_action_timer)
1081 struct ofproto_port_dump dump;
1082 struct ofproto_port ofproto_port;
1084 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
1085 const char *name = ofproto_port.name;
1086 struct iface *iface;
1090 /* Ignore the local port. We can't change it anyhow. */
1091 if (!strcmp(name, br->name)) {
1095 /* Get the type that 'ofproto_port' should have (ordinarily the
1096 * type of its corresponding iface) or NULL if it should be
1098 iface = iface_lookup(br, name);
1099 type = (iface ? iface->type
1100 : bridge_has_bond_fake_iface(br, name) ? "internal"
1103 /* If it's the wrong type then delete the ofproto port. */
1105 && !strcmp(ofproto_port.type, type)
1106 && (!iface || !iface->netdev
1107 || !strcmp(netdev_get_type(iface->netdev), type))) {
1111 if (may_port_action(port_action_timer)) {
1112 error = ofproto_port_del(br->ofproto, ofproto_port.ofp_port);
1114 VLOG_WARN("bridge %s: failed to remove %s interface (%s)",
1115 br->name, name, strerror(error));
1117 VLOG_INFO("bridge %s: removed interface %s (%d)", br->name,
1118 name, ofproto_port.ofp_port);
1122 netdev_close(iface->netdev);
1123 iface->netdev = NULL;
1129 iface_set_ofp_port(struct iface *iface, int ofp_port)
1131 struct bridge *br = iface->port->bridge;
1133 assert(iface->ofp_port < 0 && ofp_port >= 0);
1134 iface->ofp_port = ofp_port;
1135 hmap_insert(&br->ifaces, &iface->ofp_port_node, hash_int(ofp_port, 0));
1136 iface_set_ofport(iface->cfg, ofp_port);
1140 bridge_refresh_ofp_port(struct bridge *br)
1142 struct ofproto_port_dump dump;
1143 struct ofproto_port ofproto_port;
1146 /* Clear all the "ofp_port"es. */
1147 hmap_clear(&br->ifaces);
1148 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1149 struct iface *iface;
1151 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1152 iface->ofp_port = -1;
1156 /* Obtain the correct "ofp_port"s from ofproto. */
1157 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
1158 struct iface *iface = iface_lookup(br, ofproto_port.name);
1160 if (iface->ofp_port >= 0) {
1161 VLOG_WARN("bridge %s: interface %s reported twice",
1162 br->name, ofproto_port.name);
1163 } else if (iface_from_ofp_port(br, ofproto_port.ofp_port)) {
1164 VLOG_WARN("bridge %s: interface %"PRIu16" reported twice",
1165 br->name, ofproto_port.ofp_port);
1167 iface_set_ofp_port(iface, ofproto_port.ofp_port);
1173 /* Add an ofproto port for any "struct iface" that doesn't have one.
1174 * Delete any "struct iface" for which this fails.
1175 * Delete any "struct port" that thereby ends up with no ifaces. */
1177 bridge_add_ofproto_ports(struct bridge *br,
1178 long long int *port_action_timer)
1180 struct port *port, *next_port;
1182 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
1183 struct iface *iface, *next_iface;
1184 struct ofproto_port ofproto_port;
1186 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
1189 if (iface->ofp_port < 0 && !may_port_action(port_action_timer)) {
1190 iface_clear_db_record(iface->cfg);
1191 iface_destroy(iface);
1195 /* Open the netdev. */
1196 if (!iface->netdev) {
1197 error = netdev_open(iface->name, iface->type, &iface->netdev);
1199 VLOG_WARN("could not open network device %s (%s)",
1200 iface->name, strerror(error));
1204 && port->cfg->vlan_mode
1205 && !strcmp(port->cfg->vlan_mode, "splinter")) {
1206 netdev_turn_flags_on(iface->netdev, NETDEV_UP, true);
1212 /* Configure the netdev. */
1213 if (iface->netdev) {
1217 shash_from_ovs_idl_map(iface->cfg->key_options,
1218 iface->cfg->value_options,
1219 iface->cfg->n_options, &args);
1220 error = netdev_set_config(iface->netdev, &args);
1221 shash_destroy(&args);
1224 VLOG_WARN("could not configure network device %s (%s)",
1225 iface->name, strerror(error));
1226 netdev_close(iface->netdev);
1227 iface->netdev = NULL;
1231 /* Add the port, if necessary. */
1232 if (iface->netdev && iface->ofp_port < 0) {
1236 error = ofproto_port_add(br->ofproto, iface->netdev,
1239 VLOG_INFO("bridge %s: added interface %s (%d)", br->name,
1240 iface->name, ofp_port);
1241 iface_set_ofp_port(iface, ofp_port);
1243 netdev_close(iface->netdev);
1244 iface->netdev = NULL;
1248 /* Populate stats columns in new Interface rows. */
1249 if (iface->netdev && iface->need_refresh) {
1250 iface_refresh_stats(iface);
1251 iface_refresh_status(iface);
1252 iface->need_refresh = false;
1255 /* Delete the iface if we failed. */
1256 if (iface->netdev && iface->ofp_port >= 0) {
1257 VLOG_DBG("bridge %s: interface %s is on port %d",
1258 br->name, iface->name, iface->ofp_port);
1260 if (iface->netdev) {
1261 VLOG_ERR("bridge %s: missing %s interface, dropping",
1262 br->name, iface->name);
1264 /* We already reported a related error, don't bother
1265 * duplicating it. */
1267 if (!ofproto_port_query_by_name(br->ofproto, port->name,
1269 VLOG_INFO("bridge %s: removed interface %s (%d)",
1270 br->name, port->name, ofproto_port.ofp_port);
1271 ofproto_port_del(br->ofproto, ofproto_port.ofp_port);
1272 ofproto_port_destroy(&ofproto_port);
1274 iface_clear_db_record(iface->cfg);
1275 iface_destroy(iface);
1279 if (list_is_empty(&port->ifaces)) {
1280 if (!need_reconfigure) {
1281 VLOG_WARN("%s port has no interfaces, dropping", port->name);
1287 /* Add bond fake iface if necessary. */
1288 if (port_is_bond_fake_iface(port)) {
1289 if (ofproto_port_query_by_name(br->ofproto, port->name,
1291 struct netdev *netdev;
1294 error = netdev_open(port->name, "internal", &netdev);
1296 /* There are unlikely to be a great number of fake
1297 * interfaces so we don't bother rate limiting their
1299 ofproto_port_add(br->ofproto, netdev, NULL);
1300 netdev_close(netdev);
1302 VLOG_WARN("could not open network device %s (%s)",
1303 port->name, strerror(error));
1306 /* Already exists, nothing to do. */
1307 ofproto_port_destroy(&ofproto_port);
1313 /* Set Flow eviction threshold */
1315 bridge_configure_flow_eviction_threshold(struct bridge *br)
1317 const char *threshold_str;
1321 ovsrec_bridge_get_other_config_value(br->cfg,
1322 "flow-eviction-threshold",
1324 if (threshold_str) {
1325 threshold = strtoul(threshold_str, NULL, 10);
1327 threshold = OFPROTO_FLOW_EVICTON_THRESHOLD_DEFAULT;
1329 ofproto_set_flow_eviction_threshold(br->ofproto, threshold);
1332 /* Set forward BPDU option. */
1334 bridge_configure_forward_bpdu(struct bridge *br)
1336 const char *forward_bpdu_str;
1337 bool forward_bpdu = false;
1339 forward_bpdu_str = ovsrec_bridge_get_other_config_value(br->cfg,
1342 if (forward_bpdu_str && !strcmp(forward_bpdu_str, "true")) {
1343 forward_bpdu = true;
1345 ofproto_set_forward_bpdu(br->ofproto, forward_bpdu);
1348 /* Set MAC aging time for 'br'. */
1350 bridge_configure_mac_idle_time(struct bridge *br)
1352 const char *idle_time_str;
1355 idle_time_str = ovsrec_bridge_get_other_config_value(br->cfg,
1358 idle_time = (idle_time_str && atoi(idle_time_str)
1359 ? atoi(idle_time_str)
1360 : MAC_ENTRY_DEFAULT_IDLE_TIME);
1361 ofproto_set_mac_idle_time(br->ofproto, idle_time);
1365 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
1366 struct iface **hw_addr_iface)
1368 struct hmapx mirror_output_ports;
1371 bool found_addr = false;
1375 *hw_addr_iface = NULL;
1377 /* Did the user request a particular MAC? */
1378 hwaddr = ovsrec_bridge_get_other_config_value(br->cfg, "hwaddr", NULL);
1379 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
1380 if (eth_addr_is_multicast(ea)) {
1381 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
1382 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1383 } else if (eth_addr_is_zero(ea)) {
1384 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
1390 /* Mirror output ports don't participate in picking the local hardware
1391 * address. ofproto can't help us find out whether a given port is a
1392 * mirror output because we haven't configured mirrors yet, so we need to
1393 * accumulate them ourselves. */
1394 hmapx_init(&mirror_output_ports);
1395 for (i = 0; i < br->cfg->n_mirrors; i++) {
1396 struct ovsrec_mirror *m = br->cfg->mirrors[i];
1397 if (m->output_port) {
1398 hmapx_add(&mirror_output_ports, m->output_port);
1402 /* Otherwise choose the minimum non-local MAC address among all of the
1404 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1405 uint8_t iface_ea[ETH_ADDR_LEN];
1406 struct iface *candidate;
1407 struct iface *iface;
1409 /* Mirror output ports don't participate. */
1410 if (hmapx_contains(&mirror_output_ports, port->cfg)) {
1414 /* Choose the MAC address to represent the port. */
1416 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
1417 /* Find the interface with this Ethernet address (if any) so that
1418 * we can provide the correct devname to the caller. */
1419 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1420 uint8_t candidate_ea[ETH_ADDR_LEN];
1421 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
1422 && eth_addr_equals(iface_ea, candidate_ea)) {
1427 /* Choose the interface whose MAC address will represent the port.
1428 * The Linux kernel bonding code always chooses the MAC address of
1429 * the first slave added to a bond, and the Fedora networking
1430 * scripts always add slaves to a bond in alphabetical order, so
1431 * for compatibility we choose the interface with the name that is
1432 * first in alphabetical order. */
1433 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1434 if (!iface || strcmp(candidate->name, iface->name) < 0) {
1439 /* The local port doesn't count (since we're trying to choose its
1440 * MAC address anyway). */
1441 if (iface->ofp_port == OFPP_LOCAL) {
1446 error = netdev_get_etheraddr(iface->netdev, iface_ea);
1452 /* Compare against our current choice. */
1453 if (!eth_addr_is_multicast(iface_ea) &&
1454 !eth_addr_is_local(iface_ea) &&
1455 !eth_addr_is_reserved(iface_ea) &&
1456 !eth_addr_is_zero(iface_ea) &&
1457 (!found_addr || eth_addr_compare_3way(iface_ea, ea) < 0))
1459 memcpy(ea, iface_ea, ETH_ADDR_LEN);
1460 *hw_addr_iface = iface;
1465 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1466 br->name, ETH_ADDR_ARGS(ea));
1468 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1469 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
1470 *hw_addr_iface = NULL;
1471 VLOG_WARN_RL(&rl, "bridge %s: using default bridge Ethernet "
1472 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1475 hmapx_destroy(&mirror_output_ports);
1478 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
1479 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
1480 * an interface on 'br', then that interface must be passed in as
1481 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1482 * 'hw_addr_iface' must be passed in as a null pointer. */
1484 bridge_pick_datapath_id(struct bridge *br,
1485 const uint8_t bridge_ea[ETH_ADDR_LEN],
1486 struct iface *hw_addr_iface)
1489 * The procedure for choosing a bridge MAC address will, in the most
1490 * ordinary case, also choose a unique MAC that we can use as a datapath
1491 * ID. In some special cases, though, multiple bridges will end up with
1492 * the same MAC address. This is OK for the bridges, but it will confuse
1493 * the OpenFlow controller, because each datapath needs a unique datapath
1496 * Datapath IDs must be unique. It is also very desirable that they be
1497 * stable from one run to the next, so that policy set on a datapath
1500 const char *datapath_id;
1503 datapath_id = ovsrec_bridge_get_other_config_value(br->cfg, "datapath-id",
1505 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
1509 if (!hw_addr_iface) {
1511 * A purely internal bridge, that is, one that has no non-virtual
1512 * network devices on it at all, is difficult because it has no
1513 * natural unique identifier at all.
1515 * When the host is a XenServer, we handle this case by hashing the
1516 * host's UUID with the name of the bridge. Names of bridges are
1517 * persistent across XenServer reboots, although they can be reused if
1518 * an internal network is destroyed and then a new one is later
1519 * created, so this is fairly effective.
1521 * When the host is not a XenServer, we punt by using a random MAC
1522 * address on each run.
1524 const char *host_uuid = xenserver_get_host_uuid();
1526 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1527 dpid = dpid_from_hash(combined, strlen(combined));
1533 return eth_addr_to_uint64(bridge_ea);
1537 dpid_from_hash(const void *data, size_t n)
1539 uint8_t hash[SHA1_DIGEST_SIZE];
1541 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
1542 sha1_bytes(data, n, hash);
1543 eth_addr_mark_random(hash);
1544 return eth_addr_to_uint64(hash);
1548 iface_refresh_status(struct iface *iface)
1552 enum netdev_features current;
1553 enum netdev_flags flags;
1559 if (iface_is_synthetic(iface)) {
1565 if (!netdev_get_drv_info(iface->netdev, &sh)) {
1567 char **keys, **values;
1569 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1570 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1575 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1578 shash_destroy_free_data(&sh);
1580 error = netdev_get_flags(iface->netdev, &flags);
1582 ovsrec_interface_set_admin_state(iface->cfg,
1583 flags & NETDEV_UP ? "up" : "down");
1586 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1589 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1591 ovsrec_interface_set_duplex(iface->cfg,
1592 netdev_features_is_full_duplex(current)
1594 /* warning: uint64_t -> int64_t conversion */
1595 bps = netdev_features_to_bps(current);
1596 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1599 ovsrec_interface_set_duplex(iface->cfg, NULL);
1600 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1603 error = netdev_get_mtu(iface->netdev, &mtu);
1606 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1609 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1613 /* Writes 'iface''s CFM statistics to the database. */
1615 iface_refresh_cfm_stats(struct iface *iface)
1617 const struct ovsrec_interface *cfg = iface->cfg;
1619 const uint64_t *rmps;
1623 if (iface_is_synthetic(iface)) {
1627 fault = ofproto_port_get_cfm_fault(iface->port->bridge->ofproto,
1630 const char *reasons[CFM_FAULT_N_REASONS];
1631 bool fault_bool = fault;
1635 for (i = 0; i < CFM_FAULT_N_REASONS; i++) {
1636 int reason = 1 << i;
1637 if (fault & reason) {
1638 reasons[j++] = cfm_fault_reason_to_str(reason);
1642 ovsrec_interface_set_cfm_fault(cfg, &fault_bool, 1);
1643 ovsrec_interface_set_cfm_fault_status(cfg, (char **) reasons, j);
1645 ovsrec_interface_set_cfm_fault(cfg, NULL, 0);
1646 ovsrec_interface_set_cfm_fault_status(cfg, NULL, 0);
1649 error = ofproto_port_get_cfm_remote_mpids(iface->port->bridge->ofproto,
1650 iface->ofp_port, &rmps, &n_rmps);
1652 ovsrec_interface_set_cfm_remote_mpids(cfg, (const int64_t *)rmps,
1655 ovsrec_interface_set_cfm_remote_mpids(cfg, NULL, 0);
1658 health = ofproto_port_get_cfm_health(iface->port->bridge->ofproto,
1661 int64_t cfm_health = health;
1662 ovsrec_interface_set_cfm_health(cfg, &cfm_health, 1);
1664 ovsrec_interface_set_cfm_health(cfg, NULL, 0);
1669 iface_refresh_stats(struct iface *iface)
1671 #define IFACE_STATS \
1672 IFACE_STAT(rx_packets, "rx_packets") \
1673 IFACE_STAT(tx_packets, "tx_packets") \
1674 IFACE_STAT(rx_bytes, "rx_bytes") \
1675 IFACE_STAT(tx_bytes, "tx_bytes") \
1676 IFACE_STAT(rx_dropped, "rx_dropped") \
1677 IFACE_STAT(tx_dropped, "tx_dropped") \
1678 IFACE_STAT(rx_errors, "rx_errors") \
1679 IFACE_STAT(tx_errors, "tx_errors") \
1680 IFACE_STAT(rx_frame_errors, "rx_frame_err") \
1681 IFACE_STAT(rx_over_errors, "rx_over_err") \
1682 IFACE_STAT(rx_crc_errors, "rx_crc_err") \
1683 IFACE_STAT(collisions, "collisions")
1685 #define IFACE_STAT(MEMBER, NAME) NAME,
1686 static char *keys[] = { IFACE_STATS };
1688 int64_t values[ARRAY_SIZE(keys)];
1691 struct netdev_stats stats;
1693 if (iface_is_synthetic(iface)) {
1697 /* Intentionally ignore return value, since errors will set 'stats' to
1698 * all-1s, and we will deal with that correctly below. */
1699 netdev_get_stats(iface->netdev, &stats);
1701 /* Copy statistics into values[] array. */
1703 #define IFACE_STAT(MEMBER, NAME) values[i++] = stats.MEMBER;
1706 assert(i == ARRAY_SIZE(keys));
1708 ovsrec_interface_set_statistics(iface->cfg, keys, values,
1714 br_refresh_stp_status(struct bridge *br)
1716 struct ofproto *ofproto = br->ofproto;
1717 struct ofproto_stp_status status;
1718 char *keys[3], *values[3];
1721 if (ofproto_get_stp_status(ofproto, &status)) {
1725 if (!status.enabled) {
1726 ovsrec_bridge_set_status(br->cfg, NULL, NULL, 0);
1730 keys[0] = "stp_bridge_id",
1731 values[0] = xasprintf(STP_ID_FMT, STP_ID_ARGS(status.bridge_id));
1732 keys[1] = "stp_designated_root",
1733 values[1] = xasprintf(STP_ID_FMT, STP_ID_ARGS(status.designated_root));
1734 keys[2] = "stp_root_path_cost",
1735 values[2] = xasprintf("%d", status.root_path_cost);
1737 ovsrec_bridge_set_status(br->cfg, keys, values, ARRAY_SIZE(values));
1739 for (i = 0; i < ARRAY_SIZE(values); i++) {
1745 port_refresh_stp_status(struct port *port)
1747 struct ofproto *ofproto = port->bridge->ofproto;
1748 struct iface *iface;
1749 struct ofproto_port_stp_status status;
1751 char *str_values[4];
1752 int64_t int_values[3];
1755 if (port_is_synthetic(port)) {
1759 /* STP doesn't currently support bonds. */
1760 if (!list_is_singleton(&port->ifaces)) {
1761 ovsrec_port_set_status(port->cfg, NULL, NULL, 0);
1765 iface = CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
1767 if (ofproto_port_get_stp_status(ofproto, iface->ofp_port, &status)) {
1771 if (!status.enabled) {
1772 ovsrec_port_set_status(port->cfg, NULL, NULL, 0);
1773 ovsrec_port_set_statistics(port->cfg, NULL, NULL, 0);
1777 /* Set Status column. */
1778 keys[0] = "stp_port_id";
1779 str_values[0] = xasprintf(STP_PORT_ID_FMT, status.port_id);
1780 keys[1] = "stp_state";
1781 str_values[1] = xstrdup(stp_state_name(status.state));
1782 keys[2] = "stp_sec_in_state";
1783 str_values[2] = xasprintf("%u", status.sec_in_state);
1784 keys[3] = "stp_role";
1785 str_values[3] = xstrdup(stp_role_name(status.role));
1787 ovsrec_port_set_status(port->cfg, keys, str_values,
1788 ARRAY_SIZE(str_values));
1790 for (i = 0; i < ARRAY_SIZE(str_values); i++) {
1791 free(str_values[i]);
1794 /* Set Statistics column. */
1795 keys[0] = "stp_tx_count";
1796 int_values[0] = status.tx_count;
1797 keys[1] = "stp_rx_count";
1798 int_values[1] = status.rx_count;
1799 keys[2] = "stp_error_count";
1800 int_values[2] = status.error_count;
1802 ovsrec_port_set_statistics(port->cfg, keys, int_values,
1803 ARRAY_SIZE(int_values));
1807 enable_system_stats(const struct ovsrec_open_vswitch *cfg)
1811 /* Use other-config:enable-system-stats by preference. */
1812 enable = ovsrec_open_vswitch_get_other_config_value(cfg,
1813 "enable-statistics",
1816 return !strcmp(enable, "true");
1819 /* Disable by default. */
1824 refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1826 struct ovsdb_datum datum;
1830 if (enable_system_stats(cfg)) {
1831 get_system_stats(&stats);
1834 ovsdb_datum_from_shash(&datum, &stats);
1835 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1839 static inline const char *
1840 nx_role_to_str(enum nx_role role)
1845 case NX_ROLE_MASTER:
1850 return "*** INVALID ROLE ***";
1855 refresh_controller_status(void)
1859 const struct ovsrec_controller *cfg;
1863 /* Accumulate status for controllers on all bridges. */
1864 HMAP_FOR_EACH (br, node, &all_bridges) {
1865 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1868 /* Update each controller in the database with current status. */
1869 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
1870 struct ofproto_controller_info *cinfo =
1871 shash_find_data(&info, cfg->target);
1874 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1875 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1876 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1877 (char **) cinfo->pairs.values,
1880 ovsrec_controller_set_is_connected(cfg, false);
1881 ovsrec_controller_set_role(cfg, NULL);
1882 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1886 ofproto_free_ofproto_controller_info(&info);
1890 refresh_cfm_stats(void)
1892 static struct ovsdb_idl_txn *txn = NULL;
1897 txn = ovsdb_idl_txn_create(idl);
1899 HMAP_FOR_EACH (br, node, &all_bridges) {
1900 struct iface *iface;
1902 HMAP_FOR_EACH (iface, name_node, &br->iface_by_name) {
1903 iface_refresh_cfm_stats(iface);
1908 if (ovsdb_idl_txn_commit(txn) != TXN_INCOMPLETE) {
1909 ovsdb_idl_txn_destroy(txn);
1914 /* Performs periodic activity required by bridges that needs to be done with
1915 * the least possible latency.
1917 * It makes sense to call this function a couple of times per poll loop, to
1918 * provide a significant performance boost on some benchmarks with ofprotos
1919 * that use the ofproto-dpif implementation. */
1921 bridge_run_fast(void)
1925 HMAP_FOR_EACH (br, node, &all_bridges) {
1926 ofproto_run_fast(br->ofproto);
1933 const struct ovsrec_open_vswitch *cfg;
1935 bool vlan_splinters_changed;
1938 /* (Re)configure if necessary. */
1940 if (ovsdb_idl_is_lock_contended(idl)) {
1941 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1942 struct bridge *br, *next_br;
1944 VLOG_ERR_RL(&rl, "another ovs-vswitchd process is running, "
1945 "disabling this process until it goes away");
1947 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
1951 } else if (!ovsdb_idl_has_lock(idl)) {
1954 cfg = ovsrec_open_vswitch_first(idl);
1956 /* Let each bridge do the work that it needs to do. */
1957 HMAP_FOR_EACH (br, node, &all_bridges) {
1958 ofproto_run(br->ofproto);
1961 /* Re-configure SSL. We do this on every trip through the main loop,
1962 * instead of just when the database changes, because the contents of the
1963 * key and certificate files can change without the database changing.
1965 * We do this before bridge_reconfigure() because that function might
1966 * initiate SSL connections and thus requires SSL to be configured. */
1967 if (cfg && cfg->ssl) {
1968 const struct ovsrec_ssl *ssl = cfg->ssl;
1970 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1971 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1974 /* If VLAN splinters are in use, then we need to reconfigure if VLAN usage
1976 vlan_splinters_changed = false;
1977 if (vlan_splinters_enabled_anywhere) {
1978 HMAP_FOR_EACH (br, node, &all_bridges) {
1979 if (ofproto_has_vlan_usage_changed(br->ofproto)) {
1980 vlan_splinters_changed = true;
1986 if (need_reconfigure || ovsdb_idl_get_seqno(idl) != idl_seqno
1987 || vlan_splinters_changed) {
1988 idl_seqno = ovsdb_idl_get_seqno(idl);
1990 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1992 bridge_reconfigure(cfg);
1994 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1995 ovsdb_idl_txn_commit(txn);
1996 ovsdb_idl_txn_destroy(txn); /* XXX */
1998 /* We still need to reconfigure to avoid dangling pointers to
1999 * now-destroyed ovsrec structures inside bridge data. */
2000 static const struct ovsrec_open_vswitch null_cfg;
2002 bridge_reconfigure(&null_cfg);
2006 /* Refresh system and interface stats if necessary. */
2007 if (time_msec() >= stats_timer) {
2009 struct ovsdb_idl_txn *txn;
2011 txn = ovsdb_idl_txn_create(idl);
2012 HMAP_FOR_EACH (br, node, &all_bridges) {
2016 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2017 struct iface *iface;
2019 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2020 iface_refresh_stats(iface);
2021 iface_refresh_status(iface);
2025 HMAP_FOR_EACH (m, hmap_node, &br->mirrors) {
2026 mirror_refresh_stats(m);
2030 refresh_system_stats(cfg);
2031 refresh_controller_status();
2032 ovsdb_idl_txn_commit(txn);
2033 ovsdb_idl_txn_destroy(txn); /* XXX */
2036 stats_timer = time_msec() + STATS_INTERVAL;
2039 if (time_msec() >= db_limiter) {
2040 struct ovsdb_idl_txn *txn;
2042 txn = ovsdb_idl_txn_create(idl);
2043 HMAP_FOR_EACH (br, node, &all_bridges) {
2044 struct iface *iface;
2047 br_refresh_stp_status(br);
2049 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2050 port_refresh_stp_status(port);
2053 HMAP_FOR_EACH (iface, name_node, &br->iface_by_name) {
2054 const char *link_state;
2055 int64_t link_resets;
2058 if (iface_is_synthetic(iface)) {
2062 current = ofproto_port_is_lacp_current(br->ofproto,
2066 ovsrec_interface_set_lacp_current(iface->cfg, &bl, 1);
2068 ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
2071 link_state = netdev_get_carrier(iface->netdev) ? "up" : "down";
2072 ovsrec_interface_set_link_state(iface->cfg, link_state);
2074 link_resets = netdev_get_carrier_resets(iface->netdev);
2075 ovsrec_interface_set_link_resets(iface->cfg, &link_resets, 1);
2079 if (ovsdb_idl_txn_commit(txn) != TXN_UNCHANGED) {
2080 db_limiter = time_msec() + DB_LIMIT_INTERVAL;
2082 ovsdb_idl_txn_destroy(txn);
2085 refresh_cfm_stats();
2091 ovsdb_idl_wait(idl);
2093 if (need_reconfigure) {
2094 poll_immediate_wake();
2097 if (!hmap_is_empty(&all_bridges)) {
2100 HMAP_FOR_EACH (br, node, &all_bridges) {
2101 ofproto_wait(br->ofproto);
2103 poll_timer_wait_until(stats_timer);
2105 if (db_limiter > time_msec()) {
2106 poll_timer_wait_until(db_limiter);
2111 /* QoS unixctl user interface functions. */
2113 struct qos_unixctl_show_cbdata {
2115 struct iface *iface;
2119 qos_unixctl_show_cb(unsigned int queue_id,
2120 const struct shash *details,
2123 struct qos_unixctl_show_cbdata *data = aux;
2124 struct ds *ds = data->ds;
2125 struct iface *iface = data->iface;
2126 struct netdev_queue_stats stats;
2127 struct shash_node *node;
2130 ds_put_cstr(ds, "\n");
2132 ds_put_format(ds, "Queue %u:\n", queue_id);
2134 ds_put_cstr(ds, "Default:\n");
2137 SHASH_FOR_EACH (node, details) {
2138 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
2141 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
2143 if (stats.tx_packets != UINT64_MAX) {
2144 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
2147 if (stats.tx_bytes != UINT64_MAX) {
2148 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
2151 if (stats.tx_errors != UINT64_MAX) {
2152 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
2155 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
2156 queue_id, strerror(error));
2161 qos_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
2162 const char *argv[], void *aux OVS_UNUSED)
2164 struct ds ds = DS_EMPTY_INITIALIZER;
2165 struct shash sh = SHASH_INITIALIZER(&sh);
2166 struct iface *iface;
2168 struct shash_node *node;
2169 struct qos_unixctl_show_cbdata data;
2172 iface = iface_find(argv[1]);
2174 unixctl_command_reply_error(conn, "no such interface");
2178 netdev_get_qos(iface->netdev, &type, &sh);
2180 if (*type != '\0') {
2181 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
2183 SHASH_FOR_EACH (node, &sh) {
2184 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
2189 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
2192 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
2194 unixctl_command_reply(conn, ds_cstr(&ds));
2196 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
2197 unixctl_command_reply_error(conn, ds_cstr(&ds));
2200 shash_destroy_free_data(&sh);
2204 /* Bridge reconfiguration functions. */
2206 bridge_create(const struct ovsrec_bridge *br_cfg)
2210 assert(!bridge_lookup(br_cfg->name));
2211 br = xzalloc(sizeof *br);
2213 br->name = xstrdup(br_cfg->name);
2214 br->type = xstrdup(ofproto_normalize_type(br_cfg->datapath_type));
2217 /* Derive the default Ethernet address from the bridge's UUID. This should
2218 * be unique and it will be stable between ovs-vswitchd runs. */
2219 memcpy(br->default_ea, &br_cfg->header_.uuid, ETH_ADDR_LEN);
2220 eth_addr_mark_random(br->default_ea);
2222 hmap_init(&br->ports);
2223 hmap_init(&br->ifaces);
2224 hmap_init(&br->iface_by_name);
2225 hmap_init(&br->mirrors);
2227 hmap_insert(&all_bridges, &br->node, hash_string(br->name, 0));
2231 bridge_destroy(struct bridge *br)
2234 struct mirror *mirror, *next_mirror;
2235 struct port *port, *next_port;
2237 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
2240 HMAP_FOR_EACH_SAFE (mirror, next_mirror, hmap_node, &br->mirrors) {
2241 mirror_destroy(mirror);
2243 hmap_remove(&all_bridges, &br->node);
2244 ofproto_destroy(br->ofproto);
2245 hmap_destroy(&br->ifaces);
2246 hmap_destroy(&br->ports);
2247 hmap_destroy(&br->iface_by_name);
2248 hmap_destroy(&br->mirrors);
2255 static struct bridge *
2256 bridge_lookup(const char *name)
2260 HMAP_FOR_EACH_WITH_HASH (br, node, hash_string(name, 0), &all_bridges) {
2261 if (!strcmp(br->name, name)) {
2268 /* Handle requests for a listing of all flows known by the OpenFlow
2269 * stack, including those normally hidden. */
2271 bridge_unixctl_dump_flows(struct unixctl_conn *conn, int argc OVS_UNUSED,
2272 const char *argv[], void *aux OVS_UNUSED)
2277 br = bridge_lookup(argv[1]);
2279 unixctl_command_reply_error(conn, "Unknown bridge");
2284 ofproto_get_all_flows(br->ofproto, &results);
2286 unixctl_command_reply(conn, ds_cstr(&results));
2287 ds_destroy(&results);
2290 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
2291 * connections and reconnect. If BRIDGE is not specified, then all bridges
2292 * drop their controller connections and reconnect. */
2294 bridge_unixctl_reconnect(struct unixctl_conn *conn, int argc,
2295 const char *argv[], void *aux OVS_UNUSED)
2299 br = bridge_lookup(argv[1]);
2301 unixctl_command_reply_error(conn, "Unknown bridge");
2304 ofproto_reconnect_controllers(br->ofproto);
2306 HMAP_FOR_EACH (br, node, &all_bridges) {
2307 ofproto_reconnect_controllers(br->ofproto);
2310 unixctl_command_reply(conn, NULL);
2314 bridge_get_controllers(const struct bridge *br,
2315 struct ovsrec_controller ***controllersp)
2317 struct ovsrec_controller **controllers;
2318 size_t n_controllers;
2320 controllers = br->cfg->controller;
2321 n_controllers = br->cfg->n_controller;
2323 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
2329 *controllersp = controllers;
2331 return n_controllers;
2334 /* Adds and deletes "struct port"s and "struct iface"s under 'br' to match
2335 * those configured in 'br->cfg'. */
2337 bridge_add_del_ports(struct bridge *br,
2338 const unsigned long int *splinter_vlans)
2340 struct port *port, *next;
2341 struct shash_node *node;
2342 struct shash new_ports;
2345 /* Collect new ports. */
2346 shash_init(&new_ports);
2347 for (i = 0; i < br->cfg->n_ports; i++) {
2348 const char *name = br->cfg->ports[i]->name;
2349 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
2350 VLOG_WARN("bridge %s: %s specified twice as bridge port",
2354 if (bridge_get_controllers(br, NULL)
2355 && !shash_find(&new_ports, br->name)) {
2356 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
2357 br->name, br->name);
2359 br->synth_local_port.interfaces = &br->synth_local_ifacep;
2360 br->synth_local_port.n_interfaces = 1;
2361 br->synth_local_port.name = br->name;
2363 br->synth_local_iface.name = br->name;
2364 br->synth_local_iface.type = "internal";
2366 br->synth_local_ifacep = &br->synth_local_iface;
2368 shash_add(&new_ports, br->name, &br->synth_local_port);
2371 if (splinter_vlans) {
2372 add_vlan_splinter_ports(br, splinter_vlans, &new_ports);
2375 /* Get rid of deleted ports.
2376 * Get rid of deleted interfaces on ports that still exist. */
2377 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
2378 port->cfg = shash_find_data(&new_ports, port->name);
2382 port_del_ifaces(port);
2386 /* Create new ports.
2387 * Add new interfaces to existing ports. */
2388 SHASH_FOR_EACH (node, &new_ports) {
2389 struct port *port = port_lookup(br, node->name);
2391 struct ovsrec_port *cfg = node->data;
2392 port = port_create(br, cfg);
2394 port_add_ifaces(port);
2395 if (list_is_empty(&port->ifaces)) {
2396 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
2397 br->name, port->name);
2401 shash_destroy(&new_ports);
2404 /* Initializes 'oc' appropriately as a management service controller for
2407 * The caller must free oc->target when it is no longer needed. */
2409 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
2410 struct ofproto_controller *oc)
2412 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
2413 oc->max_backoff = 0;
2414 oc->probe_interval = 60;
2415 oc->band = OFPROTO_OUT_OF_BAND;
2417 oc->burst_limit = 0;
2418 oc->enable_async_msgs = true;
2421 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
2423 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
2424 struct ofproto_controller *oc)
2426 const char *config_str;
2428 oc->target = c->target;
2429 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
2430 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
2431 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
2432 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
2433 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
2434 oc->burst_limit = (c->controller_burst_limit
2435 ? *c->controller_burst_limit : 0);
2436 oc->enable_async_msgs = (!c->enable_async_messages
2437 || *c->enable_async_messages);
2438 config_str = ovsrec_controller_get_other_config_value(c, "dscp", NULL);
2440 oc->dscp = atoi(config_str);
2442 oc->dscp = DSCP_DEFAULT;
2446 /* Configures the IP stack for 'br''s local interface properly according to the
2447 * configuration in 'c'. */
2449 bridge_configure_local_iface_netdev(struct bridge *br,
2450 struct ovsrec_controller *c)
2452 struct netdev *netdev;
2453 struct in_addr mask, gateway;
2455 struct iface *local_iface;
2458 /* If there's no local interface or no IP address, give up. */
2459 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
2460 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
2464 /* Bring up the local interface. */
2465 netdev = local_iface->netdev;
2466 netdev_turn_flags_on(netdev, NETDEV_UP, true);
2468 /* Configure the IP address and netmask. */
2469 if (!c->local_netmask
2470 || !inet_aton(c->local_netmask, &mask)
2472 mask.s_addr = guess_netmask(ip.s_addr);
2474 if (!netdev_set_in4(netdev, ip, mask)) {
2475 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
2476 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
2479 /* Configure the default gateway. */
2480 if (c->local_gateway
2481 && inet_aton(c->local_gateway, &gateway)
2482 && gateway.s_addr) {
2483 if (!netdev_add_router(netdev, gateway)) {
2484 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
2485 br->name, IP_ARGS(&gateway.s_addr));
2490 /* Returns true if 'a' and 'b' are the same except that any number of slashes
2491 * in either string are treated as equal to any number of slashes in the other,
2492 * e.g. "x///y" is equal to "x/y". */
2494 equal_pathnames(const char *a, const char *b)
2498 a += strspn(a, "/");
2499 b += strspn(b, "/");
2500 } else if (*a == '\0') {
2511 bridge_configure_remotes(struct bridge *br,
2512 const struct sockaddr_in *managers, size_t n_managers)
2514 const char *disable_ib_str, *queue_id_str;
2515 bool disable_in_band = false;
2518 struct ovsrec_controller **controllers;
2519 size_t n_controllers;
2521 enum ofproto_fail_mode fail_mode;
2523 struct ofproto_controller *ocs;
2527 /* Check if we should disable in-band control on this bridge. */
2528 disable_ib_str = ovsrec_bridge_get_other_config_value(br->cfg,
2531 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
2532 disable_in_band = true;
2535 /* Set OpenFlow queue ID for in-band control. */
2536 queue_id_str = ovsrec_bridge_get_other_config_value(br->cfg,
2539 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
2540 ofproto_set_in_band_queue(br->ofproto, queue_id);
2542 if (disable_in_band) {
2543 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
2545 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
2548 n_controllers = bridge_get_controllers(br, &controllers);
2550 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
2553 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
2554 for (i = 0; i < n_controllers; i++) {
2555 struct ovsrec_controller *c = controllers[i];
2557 if (!strncmp(c->target, "punix:", 6)
2558 || !strncmp(c->target, "unix:", 5)) {
2559 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2562 whitelist = xasprintf("unix:%s/%s.controller",
2563 ovs_rundir(), br->name);
2564 if (!equal_pathnames(c->target, whitelist)) {
2565 /* Prevent remote ovsdb-server users from accessing arbitrary
2566 * Unix domain sockets and overwriting arbitrary local
2568 VLOG_ERR_RL(&rl, "bridge %s: Not adding Unix domain socket "
2569 "controller \"%s\" due to possibility for remote "
2570 "exploit. Instead, specify whitelisted \"%s\" or "
2571 "connect to \"unix:%s/%s.mgmt\" (which is always "
2572 "available without special configuration).",
2573 br->name, c->target, whitelist,
2574 ovs_rundir(), br->name);
2582 bridge_configure_local_iface_netdev(br, c);
2583 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
2584 if (disable_in_band) {
2585 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
2590 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
2591 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
2594 /* Set the fail-mode. */
2595 fail_mode = !br->cfg->fail_mode
2596 || !strcmp(br->cfg->fail_mode, "standalone")
2597 ? OFPROTO_FAIL_STANDALONE
2598 : OFPROTO_FAIL_SECURE;
2599 ofproto_set_fail_mode(br->ofproto, fail_mode);
2601 /* Configure OpenFlow controller connection snooping. */
2602 if (!ofproto_has_snoops(br->ofproto)) {
2606 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
2607 ovs_rundir(), br->name));
2608 ofproto_set_snoops(br->ofproto, &snoops);
2609 sset_destroy(&snoops);
2614 bridge_configure_tables(struct bridge *br)
2616 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2620 n_tables = ofproto_get_n_tables(br->ofproto);
2622 for (i = 0; i < n_tables; i++) {
2623 struct ofproto_table_settings s;
2626 s.max_flows = UINT_MAX;
2630 if (j < br->cfg->n_flow_tables && i == br->cfg->key_flow_tables[j]) {
2631 struct ovsrec_flow_table *cfg = br->cfg->value_flow_tables[j++];
2634 if (cfg->n_flow_limit && *cfg->flow_limit < UINT_MAX) {
2635 s.max_flows = *cfg->flow_limit;
2637 if (cfg->overflow_policy
2638 && !strcmp(cfg->overflow_policy, "evict")) {
2641 s.groups = xmalloc(cfg->n_groups * sizeof *s.groups);
2642 for (k = 0; k < cfg->n_groups; k++) {
2643 const char *string = cfg->groups[k];
2646 msg = mf_parse_subfield__(&s.groups[k], &string);
2648 VLOG_WARN_RL(&rl, "bridge %s table %d: error parsing "
2649 "'groups' (%s)", br->name, i, msg);
2651 } else if (*string) {
2652 VLOG_WARN_RL(&rl, "bridge %s table %d: 'groups' "
2653 "element '%s' contains trailing garbage",
2654 br->name, i, cfg->groups[k]);
2662 ofproto_configure_table(br->ofproto, i, &s);
2666 for (; j < br->cfg->n_flow_tables; j++) {
2667 VLOG_WARN_RL(&rl, "bridge %s: ignoring configuration for flow table "
2668 "%"PRId64" not supported by this datapath", br->name,
2669 br->cfg->key_flow_tables[j]);
2673 /* Port functions. */
2675 static struct port *
2676 port_create(struct bridge *br, const struct ovsrec_port *cfg)
2680 port = xzalloc(sizeof *port);
2682 port->name = xstrdup(cfg->name);
2684 list_init(&port->ifaces);
2686 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
2690 /* Deletes interfaces from 'port' that are no longer configured for it. */
2692 port_del_ifaces(struct port *port)
2694 struct iface *iface, *next;
2695 struct sset new_ifaces;
2698 /* Collect list of new interfaces. */
2699 sset_init(&new_ifaces);
2700 for (i = 0; i < port->cfg->n_interfaces; i++) {
2701 const char *name = port->cfg->interfaces[i]->name;
2702 const char *type = port->cfg->interfaces[i]->name;
2703 if (strcmp(type, "null")) {
2704 sset_add(&new_ifaces, name);
2708 /* Get rid of deleted interfaces. */
2709 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2710 if (!sset_contains(&new_ifaces, iface->name)) {
2711 iface_destroy(iface);
2715 sset_destroy(&new_ifaces);
2718 /* Adds new interfaces to 'port' and updates 'type' and 'cfg' members of
2721 port_add_ifaces(struct port *port)
2723 struct shash new_ifaces;
2724 struct shash_node *node;
2727 /* Collect new ifaces. */
2728 shash_init(&new_ifaces);
2729 for (i = 0; i < port->cfg->n_interfaces; i++) {
2730 const struct ovsrec_interface *cfg = port->cfg->interfaces[i];
2731 if (strcmp(cfg->type, "null")
2732 && !shash_add_once(&new_ifaces, cfg->name, cfg)) {
2733 VLOG_WARN("port %s: %s specified twice as port interface",
2734 port->name, cfg->name);
2735 iface_clear_db_record(cfg);
2739 /* Create new interfaces.
2740 * Update interface types and 'cfg' members. */
2741 SHASH_FOR_EACH (node, &new_ifaces) {
2742 const struct ovsrec_interface *cfg = node->data;
2743 const char *iface_name = node->name;
2744 struct iface *iface;
2746 iface = iface_lookup(port->bridge, iface_name);
2748 iface = iface_create(port, cfg);
2753 /* Determine interface type. The local port always has type
2754 * "internal". Other ports take their type from the database and
2755 * default to "system" if none is specified. */
2756 iface->type = (!strcmp(iface_name, port->bridge->name) ? "internal"
2757 : cfg->type[0] ? cfg->type
2760 shash_destroy(&new_ifaces);
2764 port_destroy(struct port *port)
2767 struct bridge *br = port->bridge;
2768 struct iface *iface, *next;
2771 ofproto_bundle_unregister(br->ofproto, port);
2774 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2775 iface_destroy(iface);
2778 hmap_remove(&br->ports, &port->hmap_node);
2784 static struct port *
2785 port_lookup(const struct bridge *br, const char *name)
2789 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
2791 if (!strcmp(port->name, name)) {
2799 enable_lacp(struct port *port, bool *activep)
2801 if (!port->cfg->lacp) {
2802 /* XXX when LACP implementation has been sufficiently tested, enable by
2803 * default and make active on bonded ports. */
2805 } else if (!strcmp(port->cfg->lacp, "off")) {
2807 } else if (!strcmp(port->cfg->lacp, "active")) {
2810 } else if (!strcmp(port->cfg->lacp, "passive")) {
2814 VLOG_WARN("port %s: unknown LACP mode %s",
2815 port->name, port->cfg->lacp);
2820 static struct lacp_settings *
2821 port_configure_lacp(struct port *port, struct lacp_settings *s)
2823 const char *lacp_time, *system_id;
2824 long long int custom_time;
2827 if (!enable_lacp(port, &s->active)) {
2831 s->name = port->name;
2833 system_id = ovsrec_port_get_other_config_value(port->cfg, "lacp-system-id",
2836 if (sscanf(system_id, ETH_ADDR_SCAN_FMT,
2837 ETH_ADDR_SCAN_ARGS(s->id)) != ETH_ADDR_SCAN_COUNT) {
2838 VLOG_WARN("port %s: LACP system ID (%s) must be an Ethernet"
2839 " address.", port->name, system_id);
2843 memcpy(s->id, port->bridge->ea, ETH_ADDR_LEN);
2846 if (eth_addr_is_zero(s->id)) {
2847 VLOG_WARN("port %s: Invalid zero LACP system ID.", port->name);
2851 /* Prefer bondable links if unspecified. */
2852 priority = atoi(ovsrec_port_get_other_config_value(port->cfg,
2853 "lacp-system-priority",
2855 s->priority = (priority > 0 && priority <= UINT16_MAX
2857 : UINT16_MAX - !list_is_short(&port->ifaces));
2859 s->heartbeat = !strcmp(ovsrec_port_get_other_config_value(port->cfg,
2864 lacp_time = ovsrec_port_get_other_config_value(port->cfg, "lacp-time",
2866 custom_time = atoi(lacp_time);
2867 if (!strcmp(lacp_time, "fast")) {
2868 s->lacp_time = LACP_TIME_FAST;
2869 } else if (!strcmp(lacp_time, "slow")) {
2870 s->lacp_time = LACP_TIME_SLOW;
2871 } else if (custom_time > 0) {
2872 s->lacp_time = LACP_TIME_CUSTOM;
2873 s->custom_time = custom_time;
2875 s->lacp_time = LACP_TIME_SLOW;
2882 iface_configure_lacp(struct iface *iface, struct lacp_slave_settings *s)
2884 int priority, portid, key;
2886 portid = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
2890 atoi(ovsrec_interface_get_other_config_value(iface->cfg,
2891 "lacp-port-priority",
2893 key = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
2894 "lacp-aggregation-key",
2897 if (portid <= 0 || portid > UINT16_MAX) {
2898 portid = iface->ofp_port;
2901 if (priority <= 0 || priority > UINT16_MAX) {
2902 priority = UINT16_MAX;
2905 if (key < 0 || key > UINT16_MAX) {
2909 s->name = iface->name;
2911 s->priority = priority;
2916 port_configure_bond(struct port *port, struct bond_settings *s,
2917 uint32_t *bond_stable_ids)
2919 const char *detect_s;
2920 struct iface *iface;
2921 int miimon_interval;
2924 s->name = port->name;
2926 if (port->cfg->bond_mode) {
2927 if (!bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
2928 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
2929 port->name, port->cfg->bond_mode,
2930 bond_mode_to_string(s->balance));
2933 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2935 /* XXX: Post version 1.5.*, the default bond_mode changed from SLB to
2936 * active-backup. At some point we should remove this warning. */
2937 VLOG_WARN_RL(&rl, "port %s: Using the default bond_mode %s. Note that"
2938 " in previous versions, the default bond_mode was"
2939 " balance-slb", port->name,
2940 bond_mode_to_string(s->balance));
2942 if (s->balance == BM_SLB && port->bridge->cfg->n_flood_vlans) {
2943 VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
2944 "please use another bond type or disable flood_vlans",
2949 atoi(ovsrec_port_get_other_config_value(port->cfg,
2950 "bond-miimon-interval", "0"));
2951 if (miimon_interval <= 0) {
2952 miimon_interval = 200;
2955 detect_s = ovsrec_port_get_other_config_value(port->cfg,
2958 if (!strcmp(detect_s, "carrier")) {
2959 miimon_interval = 0;
2960 } else if (strcmp(detect_s, "miimon")) {
2961 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
2962 "defaulting to carrier", port->name, detect_s);
2963 miimon_interval = 0;
2966 s->up_delay = MAX(0, port->cfg->bond_updelay);
2967 s->down_delay = MAX(0, port->cfg->bond_downdelay);
2968 s->basis = atoi(ovsrec_port_get_other_config_value(port->cfg,
2971 s->rebalance_interval = atoi(
2972 ovsrec_port_get_other_config_value(port->cfg,
2973 "bond-rebalance-interval",
2975 if (s->rebalance_interval && s->rebalance_interval < 1000) {
2976 s->rebalance_interval = 1000;
2979 s->fake_iface = port->cfg->bond_fake_iface;
2982 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2983 long long stable_id;
2986 atoll(ovsrec_interface_get_other_config_value(iface->cfg,
2989 if (stable_id <= 0 || stable_id >= UINT32_MAX) {
2990 stable_id = iface->ofp_port;
2992 bond_stable_ids[i++] = stable_id;
2994 netdev_set_miimon_interval(iface->netdev, miimon_interval);
2998 /* Returns true if 'port' is synthetic, that is, if we constructed it locally
2999 * instead of obtaining it from the database. */
3001 port_is_synthetic(const struct port *port)
3003 return ovsdb_idl_row_is_synthetic(&port->cfg->header_);
3006 /* Interface functions. */
3008 static struct iface *
3009 iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
3011 struct bridge *br = port->bridge;
3012 struct iface *iface;
3013 char *name = if_cfg->name;
3015 iface = xzalloc(sizeof *iface);
3017 iface->name = xstrdup(name);
3018 iface->ofp_port = -1;
3019 iface->tag = tag_create_random();
3020 iface->netdev = NULL;
3021 iface->cfg = if_cfg;
3022 iface->need_refresh = true;
3024 hmap_insert(&br->iface_by_name, &iface->name_node, hash_string(name, 0));
3026 list_push_back(&port->ifaces, &iface->port_elem);
3028 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3034 iface_destroy(struct iface *iface)
3037 struct port *port = iface->port;
3038 struct bridge *br = port->bridge;
3040 if (br->ofproto && iface->ofp_port >= 0) {
3041 ofproto_port_unregister(br->ofproto, iface->ofp_port);
3044 if (iface->ofp_port >= 0) {
3045 hmap_remove(&br->ifaces, &iface->ofp_port_node);
3048 list_remove(&iface->port_elem);
3049 hmap_remove(&br->iface_by_name, &iface->name_node);
3051 netdev_close(iface->netdev);
3058 static struct iface *
3059 iface_lookup(const struct bridge *br, const char *name)
3061 struct iface *iface;
3063 HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
3064 &br->iface_by_name) {
3065 if (!strcmp(iface->name, name)) {
3073 static struct iface *
3074 iface_find(const char *name)
3076 const struct bridge *br;
3078 HMAP_FOR_EACH (br, node, &all_bridges) {
3079 struct iface *iface = iface_lookup(br, name);
3088 static struct iface *
3089 iface_from_ofp_port(const struct bridge *br, uint16_t ofp_port)
3091 struct iface *iface;
3093 HMAP_FOR_EACH_IN_BUCKET (iface, ofp_port_node,
3094 hash_int(ofp_port, 0), &br->ifaces) {
3095 if (iface->ofp_port == ofp_port) {
3102 /* Set Ethernet address of 'iface', if one is specified in the configuration
3105 iface_set_mac(struct iface *iface)
3107 uint8_t ea[ETH_ADDR_LEN];
3109 if (!strcmp(iface->type, "internal")
3110 && iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
3111 if (iface->ofp_port == OFPP_LOCAL) {
3112 VLOG_ERR("interface %s: ignoring mac in Interface record "
3113 "(use Bridge record to set local port's mac)",
3115 } else if (eth_addr_is_multicast(ea)) {
3116 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3119 int error = netdev_set_etheraddr(iface->netdev, ea);
3121 VLOG_ERR("interface %s: setting MAC failed (%s)",
3122 iface->name, strerror(error));
3128 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
3130 iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
3132 if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
3133 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
3137 /* Clears all of the fields in 'if_cfg' that indicate interface status, and
3138 * sets the "ofport" field to -1.
3140 * This is appropriate when 'if_cfg''s interface cannot be created or is
3141 * otherwise invalid. */
3143 iface_clear_db_record(const struct ovsrec_interface *if_cfg)
3145 if (!ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
3146 iface_set_ofport(if_cfg, -1);
3147 ovsrec_interface_set_status(if_cfg, NULL, NULL, 0);
3148 ovsrec_interface_set_admin_state(if_cfg, NULL);
3149 ovsrec_interface_set_duplex(if_cfg, NULL);
3150 ovsrec_interface_set_link_speed(if_cfg, NULL, 0);
3151 ovsrec_interface_set_link_state(if_cfg, NULL);
3152 ovsrec_interface_set_mtu(if_cfg, NULL, 0);
3153 ovsrec_interface_set_cfm_fault(if_cfg, NULL, 0);
3154 ovsrec_interface_set_cfm_fault_status(if_cfg, NULL, 0);
3155 ovsrec_interface_set_cfm_remote_mpids(if_cfg, NULL, 0);
3156 ovsrec_interface_set_lacp_current(if_cfg, NULL, 0);
3157 ovsrec_interface_set_statistics(if_cfg, NULL, NULL, 0);
3161 /* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
3163 * The value strings in '*shash' are taken directly from values[], not copied,
3164 * so the caller should not modify or free them. */
3166 shash_from_ovs_idl_map(char **keys, char **values, size_t n,
3167 struct shash *shash)
3172 for (i = 0; i < n; i++) {
3173 shash_add(shash, keys[i], values[i]);
3177 /* Creates 'keys' and 'values' arrays from 'shash'.
3179 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
3180 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
3181 * are populated with with strings taken directly from 'shash' and thus have
3182 * the same ownership of the key-value pairs in shash.
3185 shash_to_ovs_idl_map(struct shash *shash,
3186 char ***keys, char ***values, size_t *n)
3190 struct shash_node *sn;
3192 count = shash_count(shash);
3194 k = xmalloc(count * sizeof *k);
3195 v = xmalloc(count * sizeof *v);
3198 SHASH_FOR_EACH(sn, shash) {
3209 struct iface_delete_queues_cbdata {
3210 struct netdev *netdev;
3211 const struct ovsdb_datum *queues;
3215 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
3217 union ovsdb_atom atom;
3219 atom.integer = target;
3220 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
3224 iface_delete_queues(unsigned int queue_id,
3225 const struct shash *details OVS_UNUSED, void *cbdata_)
3227 struct iface_delete_queues_cbdata *cbdata = cbdata_;
3229 if (!queue_ids_include(cbdata->queues, queue_id)) {
3230 netdev_delete_queue(cbdata->netdev, queue_id);
3235 iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
3237 struct ofpbuf queues_buf;
3239 ofpbuf_init(&queues_buf, 0);
3241 if (!qos || qos->type[0] == '\0' || qos->n_queues < 1) {
3242 netdev_set_qos(iface->netdev, NULL, NULL);
3244 struct iface_delete_queues_cbdata cbdata;
3245 struct shash details;
3249 /* Configure top-level Qos for 'iface'. */
3250 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
3251 qos->n_other_config, &details);
3252 netdev_set_qos(iface->netdev, qos->type, &details);
3253 shash_destroy(&details);
3255 /* Deconfigure queues that were deleted. */
3256 cbdata.netdev = iface->netdev;
3257 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
3259 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
3261 /* Configure queues for 'iface'. */
3263 for (i = 0; i < qos->n_queues; i++) {
3264 const struct ovsrec_queue *queue = qos->value_queues[i];
3265 unsigned int queue_id = qos->key_queues[i];
3267 if (queue_id == 0) {
3271 if (queue->n_dscp == 1) {
3272 struct ofproto_port_queue *port_queue;
3274 port_queue = ofpbuf_put_uninit(&queues_buf,
3275 sizeof *port_queue);
3276 port_queue->queue = queue_id;
3277 port_queue->dscp = queue->dscp[0];
3280 shash_from_ovs_idl_map(queue->key_other_config,
3281 queue->value_other_config,
3282 queue->n_other_config, &details);
3283 netdev_set_queue(iface->netdev, queue_id, &details);
3284 shash_destroy(&details);
3287 shash_init(&details);
3288 netdev_set_queue(iface->netdev, 0, &details);
3289 shash_destroy(&details);
3293 if (iface->ofp_port >= 0) {
3294 const struct ofproto_port_queue *port_queues = queues_buf.data;
3295 size_t n_queues = queues_buf.size / sizeof *port_queues;
3297 ofproto_port_set_queues(iface->port->bridge->ofproto, iface->ofp_port,
3298 port_queues, n_queues);
3301 netdev_set_policing(iface->netdev,
3302 iface->cfg->ingress_policing_rate,
3303 iface->cfg->ingress_policing_burst);
3305 ofpbuf_uninit(&queues_buf);
3309 iface_configure_cfm(struct iface *iface)
3311 const struct ovsrec_interface *cfg = iface->cfg;
3312 const char *extended_str, *opstate_str;
3313 const char *cfm_ccm_vlan;
3314 struct cfm_settings s;
3316 if (!cfg->n_cfm_mpid) {
3317 ofproto_port_clear_cfm(iface->port->bridge->ofproto, iface->ofp_port);
3321 s.mpid = *cfg->cfm_mpid;
3322 s.interval = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
3325 cfm_ccm_vlan = ovsrec_interface_get_other_config_value(iface->cfg,
3328 s.ccm_pcp = atoi(ovsrec_interface_get_other_config_value(iface->cfg,
3331 if (s.interval <= 0) {
3335 if (!strcasecmp("random", cfm_ccm_vlan)) {
3336 s.ccm_vlan = CFM_RANDOM_VLAN;
3338 s.ccm_vlan = atoi(cfm_ccm_vlan);
3339 if (s.ccm_vlan == CFM_RANDOM_VLAN) {
3344 extended_str = ovsrec_interface_get_other_config_value(iface->cfg,
3347 s.extended = !strcasecmp("true", extended_str);
3349 opstate_str = ovsrec_interface_get_other_config_value(iface->cfg,
3352 s.opup = !strcasecmp("up", opstate_str);
3354 ofproto_port_set_cfm(iface->port->bridge->ofproto, iface->ofp_port, &s);
3357 /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
3358 * instead of obtaining it from the database. */
3360 iface_is_synthetic(const struct iface *iface)
3362 return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
3366 /* Port mirroring. */
3368 static struct mirror *
3369 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
3373 HMAP_FOR_EACH_IN_BUCKET (m, hmap_node, uuid_hash(uuid), &br->mirrors) {
3374 if (uuid_equals(uuid, &m->uuid)) {
3382 bridge_configure_mirrors(struct bridge *br)
3384 const struct ovsdb_datum *mc;
3385 unsigned long *flood_vlans;
3386 struct mirror *m, *next;
3389 /* Get rid of deleted mirrors. */
3390 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
3391 HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mirrors) {
3392 union ovsdb_atom atom;
3394 atom.uuid = m->uuid;
3395 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3400 /* Add new mirrors and reconfigure existing ones. */
3401 for (i = 0; i < br->cfg->n_mirrors; i++) {
3402 const struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
3403 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
3405 m = mirror_create(br, cfg);
3408 if (!mirror_configure(m)) {
3413 /* Update flooded vlans (for RSPAN). */
3414 flood_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
3415 br->cfg->n_flood_vlans);
3416 ofproto_set_flood_vlans(br->ofproto, flood_vlans);
3417 bitmap_free(flood_vlans);
3420 static struct mirror *
3421 mirror_create(struct bridge *br, const struct ovsrec_mirror *cfg)
3425 m = xzalloc(sizeof *m);
3426 m->uuid = cfg->header_.uuid;
3427 hmap_insert(&br->mirrors, &m->hmap_node, uuid_hash(&m->uuid));
3429 m->name = xstrdup(cfg->name);
3435 mirror_destroy(struct mirror *m)
3438 struct bridge *br = m->bridge;
3441 ofproto_mirror_unregister(br->ofproto, m);
3444 hmap_remove(&br->mirrors, &m->hmap_node);
3451 mirror_collect_ports(struct mirror *m,
3452 struct ovsrec_port **in_ports, int n_in_ports,
3453 void ***out_portsp, size_t *n_out_portsp)
3455 void **out_ports = xmalloc(n_in_ports * sizeof *out_ports);
3456 size_t n_out_ports = 0;
3459 for (i = 0; i < n_in_ports; i++) {
3460 const char *name = in_ports[i]->name;
3461 struct port *port = port_lookup(m->bridge, name);
3463 out_ports[n_out_ports++] = port;
3465 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
3466 "port %s", m->bridge->name, m->name, name);
3469 *out_portsp = out_ports;
3470 *n_out_portsp = n_out_ports;
3474 mirror_configure(struct mirror *m)
3476 const struct ovsrec_mirror *cfg = m->cfg;
3477 struct ofproto_mirror_settings s;
3480 if (strcmp(cfg->name, m->name)) {
3482 m->name = xstrdup(cfg->name);
3486 /* Get output port or VLAN. */
3487 if (cfg->output_port) {
3488 s.out_bundle = port_lookup(m->bridge, cfg->output_port->name);
3489 if (!s.out_bundle) {
3490 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
3491 m->bridge->name, m->name);
3494 s.out_vlan = UINT16_MAX;
3496 if (cfg->output_vlan) {
3497 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
3498 "output vlan; ignoring output vlan",
3499 m->bridge->name, m->name);
3501 } else if (cfg->output_vlan) {
3502 /* The database should prevent invalid VLAN values. */
3503 s.out_bundle = NULL;
3504 s.out_vlan = *cfg->output_vlan;
3506 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
3507 m->bridge->name, m->name);
3511 /* Get port selection. */
3512 if (cfg->select_all) {
3513 size_t n_ports = hmap_count(&m->bridge->ports);
3514 void **ports = xmalloc(n_ports * sizeof *ports);
3519 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3529 /* Get ports, dropping ports that don't exist.
3530 * The IDL ensures that there are no duplicates. */
3531 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
3532 &s.srcs, &s.n_srcs);
3533 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
3534 &s.dsts, &s.n_dsts);
3537 /* Get VLAN selection. */
3538 s.src_vlans = vlan_bitmap_from_array(cfg->select_vlan, cfg->n_select_vlan);
3541 ofproto_mirror_register(m->bridge->ofproto, m, &s);
3544 if (s.srcs != s.dsts) {
3553 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
3555 * This is deprecated. It is only for compatibility with broken device drivers
3556 * in old versions of Linux that do not properly support VLANs when VLAN
3557 * devices are not used. When broken device drivers are no longer in
3558 * widespread use, we will delete these interfaces. */
3560 static void **blocks;
3561 static size_t n_blocks, allocated_blocks;
3563 /* Adds 'block' to a list of blocks that have to be freed with free() when the
3564 * VLAN splinters are reconfigured. */
3566 register_block(void *block)
3568 if (n_blocks >= allocated_blocks) {
3569 blocks = x2nrealloc(blocks, &allocated_blocks, sizeof *blocks);
3571 blocks[n_blocks++] = block;
3574 /* Frees all of the blocks registered with register_block(). */
3576 free_registered_blocks(void)
3580 for (i = 0; i < n_blocks; i++) {
3586 /* Returns true if VLAN splinters are enabled on 'iface_cfg', false
3589 vlan_splinters_is_enabled(const struct ovsrec_interface *iface_cfg)
3593 value = ovsrec_interface_get_other_config_value(iface_cfg,
3594 "enable-vlan-splinters",
3596 return !strcmp(value, "true");
3599 /* Figures out the set of VLANs that are in use for the purpose of VLAN
3602 * If VLAN splinters are enabled on at least one interface and any VLANs are in
3603 * use, returns a 4096-bit bitmap with a 1-bit for each in-use VLAN (bits 0 and
3604 * 4095 will not be set). The caller is responsible for freeing the bitmap,
3607 * If VLANs splinters are not enabled on any interface or if no VLANs are in
3608 * use, returns NULL.
3610 * Updates 'vlan_splinters_enabled_anywhere'. */
3611 static unsigned long int *
3612 collect_splinter_vlans(const struct ovsrec_open_vswitch *ovs_cfg)
3614 unsigned long int *splinter_vlans;
3615 struct sset splinter_ifaces;
3616 const char *real_dev_name;
3617 struct shash *real_devs;
3618 struct shash_node *node;
3622 /* Free space allocated for synthesized ports and interfaces, since we're
3623 * in the process of reconstructing all of them. */
3624 free_registered_blocks();
3626 splinter_vlans = bitmap_allocate(4096);
3627 sset_init(&splinter_ifaces);
3628 vlan_splinters_enabled_anywhere = false;
3629 for (i = 0; i < ovs_cfg->n_bridges; i++) {
3630 struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
3633 for (j = 0; j < br_cfg->n_ports; j++) {
3634 struct ovsrec_port *port_cfg = br_cfg->ports[j];
3637 for (k = 0; k < port_cfg->n_interfaces; k++) {
3638 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
3640 if (vlan_splinters_is_enabled(iface_cfg)) {
3641 vlan_splinters_enabled_anywhere = true;
3642 sset_add(&splinter_ifaces, iface_cfg->name);
3643 vlan_bitmap_from_array__(port_cfg->trunks,
3649 if (port_cfg->tag && *port_cfg->tag > 0 && *port_cfg->tag < 4095) {
3650 bitmap_set1(splinter_vlans, *port_cfg->tag);
3655 if (!vlan_splinters_enabled_anywhere) {
3656 free(splinter_vlans);
3657 sset_destroy(&splinter_ifaces);
3661 HMAP_FOR_EACH (br, node, &all_bridges) {
3663 ofproto_get_vlan_usage(br->ofproto, splinter_vlans);
3667 /* Don't allow VLANs 0 or 4095 to be splintered. VLAN 0 should appear on
3668 * the real device. VLAN 4095 is reserved and Linux doesn't allow a VLAN
3669 * device to be created for it. */
3670 bitmap_set0(splinter_vlans, 0);
3671 bitmap_set0(splinter_vlans, 4095);
3673 /* Delete all VLAN devices that we don't need. */
3675 real_devs = vlandev_get_real_devs();
3676 SHASH_FOR_EACH (node, real_devs) {
3677 const struct vlan_real_dev *real_dev = node->data;
3678 const struct vlan_dev *vlan_dev;
3679 bool real_dev_has_splinters;
3681 real_dev_has_splinters = sset_contains(&splinter_ifaces,
3683 HMAP_FOR_EACH (vlan_dev, hmap_node, &real_dev->vlan_devs) {
3684 if (!real_dev_has_splinters
3685 || !bitmap_is_set(splinter_vlans, vlan_dev->vid)) {
3686 struct netdev *netdev;
3688 if (!netdev_open(vlan_dev->name, "system", &netdev)) {
3689 if (!netdev_get_in4(netdev, NULL, NULL) ||
3690 !netdev_get_in6(netdev, NULL)) {
3691 vlandev_del(vlan_dev->name);
3693 /* It has an IP address configured, so we don't own
3694 * it. Don't delete it. */
3696 netdev_close(netdev);
3703 /* Add all VLAN devices that we need. */
3704 SSET_FOR_EACH (real_dev_name, &splinter_ifaces) {
3707 BITMAP_FOR_EACH_1 (vid, 4096, splinter_vlans) {
3708 if (!vlandev_get_name(real_dev_name, vid)) {
3709 vlandev_add(real_dev_name, vid);
3716 sset_destroy(&splinter_ifaces);
3718 if (bitmap_scan(splinter_vlans, 0, 4096) >= 4096) {
3719 free(splinter_vlans);
3722 return splinter_vlans;
3725 /* Pushes the configure of VLAN splinter port 'port' (e.g. eth0.9) down to
3728 configure_splinter_port(struct port *port)
3730 struct ofproto *ofproto = port->bridge->ofproto;
3731 uint16_t realdev_ofp_port;
3732 const char *realdev_name;
3733 struct iface *vlandev, *realdev;
3735 ofproto_bundle_unregister(port->bridge->ofproto, port);
3737 vlandev = CONTAINER_OF(list_front(&port->ifaces), struct iface,
3740 realdev_name = ovsrec_port_get_other_config_value(port->cfg,
3742 realdev = iface_lookup(port->bridge, realdev_name);
3743 realdev_ofp_port = realdev ? realdev->ofp_port : 0;
3745 ofproto_port_set_realdev(ofproto, vlandev->ofp_port, realdev_ofp_port,
3749 static struct ovsrec_port *
3750 synthesize_splinter_port(const char *real_dev_name,
3751 const char *vlan_dev_name, int vid)
3753 struct ovsrec_interface *iface;
3754 struct ovsrec_port *port;
3756 iface = xzalloc(sizeof *iface);
3757 iface->name = xstrdup(vlan_dev_name);
3758 iface->type = "system";
3760 port = xzalloc(sizeof *port);
3761 port->interfaces = xmemdup(&iface, sizeof iface);
3762 port->n_interfaces = 1;
3763 port->name = xstrdup(vlan_dev_name);
3764 port->vlan_mode = "splinter";
3765 port->tag = xmalloc(sizeof *port->tag);
3767 port->key_other_config = xmalloc(sizeof *port->key_other_config);
3768 port->key_other_config[0] = "realdev";
3769 port->value_other_config = xmalloc(sizeof *port->value_other_config);
3770 port->value_other_config[0] = xstrdup(real_dev_name);
3771 port->n_other_config = 1;
3773 register_block(iface);
3774 register_block(iface->name);
3775 register_block(port);
3776 register_block(port->interfaces);
3777 register_block(port->name);
3778 register_block(port->tag);
3779 register_block(port->key_other_config);
3780 register_block(port->value_other_config);
3781 register_block(port->value_other_config[0]);
3786 /* For each interface with 'br' that has VLAN splinters enabled, adds a
3787 * corresponding ovsrec_port to 'ports' for each splinter VLAN marked with a
3788 * 1-bit in the 'splinter_vlans' bitmap. */
3790 add_vlan_splinter_ports(struct bridge *br,
3791 const unsigned long int *splinter_vlans,
3792 struct shash *ports)
3796 /* We iterate through 'br->cfg->ports' instead of 'ports' here because
3797 * we're modifying 'ports'. */
3798 for (i = 0; i < br->cfg->n_ports; i++) {
3799 const char *name = br->cfg->ports[i]->name;
3800 struct ovsrec_port *port_cfg = shash_find_data(ports, name);
3803 for (j = 0; j < port_cfg->n_interfaces; j++) {
3804 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[j];
3806 if (vlan_splinters_is_enabled(iface_cfg)) {
3807 const char *real_dev_name;
3810 real_dev_name = iface_cfg->name;
3811 BITMAP_FOR_EACH_1 (vid, 4096, splinter_vlans) {
3812 const char *vlan_dev_name;
3814 vlan_dev_name = vlandev_get_name(real_dev_name, vid);
3816 && !shash_find(ports, vlan_dev_name)) {
3817 shash_add(ports, vlan_dev_name,
3818 synthesize_splinter_port(
3819 real_dev_name, vlan_dev_name, vid));
3828 mirror_refresh_stats(struct mirror *m)
3830 struct ofproto *ofproto = m->bridge->ofproto;
3831 uint64_t tx_packets, tx_bytes;
3834 size_t stat_cnt = 0;
3836 if (ofproto_mirror_get_stats(ofproto, m, &tx_packets, &tx_bytes)) {
3837 ovsrec_mirror_set_statistics(m->cfg, NULL, NULL, 0);
3841 if (tx_packets != UINT64_MAX) {
3842 keys[stat_cnt] = "tx_packets";
3843 values[stat_cnt] = tx_packets;
3846 if (tx_bytes != UINT64_MAX) {
3847 keys[stat_cnt] = "tx_bytes";
3848 values[stat_cnt] = tx_bytes;
3852 ovsrec_mirror_set_statistics(m->cfg, keys, values, stat_cnt);