1 /* Copyright (c) 2008, 2009, 2010, 2011 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.
18 #include "byte-order.h"
21 #include <arpa/inet.h>
24 #include <sys/socket.h>
26 #include <openflow/openflow.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
37 #include "classifier.h"
42 #include "dynamic-string.h"
49 #include "mac-learning.h"
53 #include "ofp-print.h"
55 #include "ofproto/netflow.h"
56 #include "ofproto/ofproto.h"
57 #include "ovsdb-data.h"
59 #include "poll-loop.h"
63 #include "socket-util.h"
64 #include "stream-ssl.h"
67 #include "system-stats.h"
72 #include "vswitchd/vswitch-idl.h"
73 #include "xenserver.h"
75 #include "sflow_api.h"
76 #include "vlan-bitmap.h"
78 VLOG_DEFINE_THIS_MODULE(bridge);
80 COVERAGE_DEFINE(bridge_reconfigure);
83 /* These members are always valid. */
84 struct list port_elem; /* Element in struct port's "ifaces" list. */
85 struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
86 struct port *port; /* Containing port. */
87 char *name; /* Host network device name. */
88 tag_type tag; /* Tag associated with this interface. */
90 /* These members are valid only after bridge_reconfigure() causes them to
92 struct hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
93 int ofp_port; /* OpenFlow port number, -1 if unknown. */
94 struct netdev *netdev; /* Network device. */
95 const char *type; /* Usually same as cfg->type. */
96 const struct ovsrec_interface *cfg;
100 struct uuid uuid; /* UUID of this "mirror" record in database. */
101 struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
102 struct bridge *bridge;
107 struct bridge *bridge;
108 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
111 const struct ovsrec_port *cfg;
113 /* An ordinary bridge port has 1 interface.
114 * A bridge port for bonding has at least 2 interfaces. */
115 struct list ifaces; /* List of "struct iface"s. */
119 struct hmap_node node; /* In 'all_bridges'. */
120 char *name; /* User-specified arbitrary name. */
121 char *type; /* Datapath type. */
122 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
123 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
124 const struct ovsrec_bridge *cfg;
126 /* OpenFlow switch processing. */
127 struct ofproto *ofproto; /* OpenFlow switch. */
130 struct hmap ports; /* "struct port"s indexed by name. */
131 struct hmap ifaces; /* "struct iface"s indexed by ofp_port. */
132 struct hmap iface_by_name; /* "struct iface"s indexed by name. */
134 /* Port mirroring. */
135 struct hmap mirrors; /* "struct mirror" indexed by UUID. */
137 /* Synthetic local port if necessary. */
138 struct ovsrec_port synth_local_port;
139 struct ovsrec_interface synth_local_iface;
140 struct ovsrec_interface *synth_local_ifacep;
143 /* All bridges, indexed by name. */
144 static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
146 /* OVSDB IDL used to obtain configuration. */
147 static struct ovsdb_idl *idl;
149 /* Each time this timer expires, the bridge fetches systems and interface
150 * statistics and pushes them into the database. */
151 #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
152 static long long int stats_timer = LLONG_MIN;
154 /* Stores the time after which rate limited statistics may be written to the
155 * database. Only updated when changes to the database require rate limiting.
157 #define DB_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
158 static long long int db_limiter = LLONG_MIN;
160 static void add_del_bridges(const struct ovsrec_open_vswitch *);
161 static void bridge_del_dps(void);
162 static bool bridge_add_dp(struct bridge *);
163 static void bridge_create(const struct ovsrec_bridge *);
164 static void bridge_destroy(struct bridge *);
165 static struct bridge *bridge_lookup(const char *name);
166 static unixctl_cb_func bridge_unixctl_dump_flows;
167 static unixctl_cb_func bridge_unixctl_reconnect;
168 static size_t bridge_get_controllers(const struct bridge *br,
169 struct ovsrec_controller ***controllersp);
170 static void bridge_add_del_ports(struct bridge *);
171 static void bridge_add_ofproto_ports(struct bridge *);
172 static void bridge_del_ofproto_ports(struct bridge *);
173 static void bridge_refresh_ofp_port(struct bridge *);
174 static void bridge_configure_datapath_id(struct bridge *);
175 static void bridge_configure_netflow(struct bridge *);
176 static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
177 static void bridge_configure_remotes(struct bridge *,
178 const struct sockaddr_in *managers,
180 static void bridge_pick_local_hw_addr(struct bridge *,
181 uint8_t ea[ETH_ADDR_LEN],
182 struct iface **hw_addr_iface);
183 static uint64_t bridge_pick_datapath_id(struct bridge *,
184 const uint8_t bridge_ea[ETH_ADDR_LEN],
185 struct iface *hw_addr_iface);
186 static uint64_t dpid_from_hash(const void *, size_t nbytes);
187 static bool bridge_has_bond_fake_iface(const struct bridge *,
189 static bool port_is_bond_fake_iface(const struct port *);
191 static unixctl_cb_func cfm_unixctl_show;
192 static unixctl_cb_func qos_unixctl_show;
194 static struct port *port_create(struct bridge *, const struct ovsrec_port *);
195 static void port_add_ifaces(struct port *);
196 static void port_del_ifaces(struct port *);
197 static void port_destroy(struct port *);
198 static struct port *port_lookup(const struct bridge *, const char *name);
199 static void port_configure(struct port *);
200 static struct lacp_settings *port_configure_lacp(struct port *,
201 struct lacp_settings *);
202 static void port_configure_bond(struct port *, struct bond_settings *);
204 static void bridge_configure_mirrors(struct bridge *);
205 static struct mirror *mirror_create(struct bridge *,
206 const struct ovsrec_mirror *);
207 static void mirror_destroy(struct mirror *);
208 static bool mirror_configure(struct mirror *, const struct ovsrec_mirror *);
210 static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
211 static struct iface *iface_create(struct port *port,
212 const struct ovsrec_interface *if_cfg);
213 static void iface_destroy(struct iface *);
214 static struct iface *iface_lookup(const struct bridge *, const char *name);
215 static struct iface *iface_find(const char *name);
216 static struct iface *iface_from_ofp_port(const struct bridge *,
218 static void iface_set_mac(struct iface *);
219 static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
220 static void iface_configure_qos(struct iface *, const struct ovsrec_qos *);
221 static void iface_configure_cfm(struct iface *);
222 static bool iface_refresh_cfm_stats(struct iface *iface);
223 static bool iface_get_carrier(const struct iface *);
224 static bool iface_is_synthetic(const struct iface *);
226 static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
228 static void shash_to_ovs_idl_map(struct shash *,
229 char ***keys, char ***values, size_t *n);
231 /* Public functions. */
233 /* Initializes the bridge module, configuring it to obtain its configuration
234 * from an OVSDB server accessed over 'remote', which should be a string in a
235 * form acceptable to ovsdb_idl_create(). */
237 bridge_init(const char *remote)
239 /* Create connection to database. */
240 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
242 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
243 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
244 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
245 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
246 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
247 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
248 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
250 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
251 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
253 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
254 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
256 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
257 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
258 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
259 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
260 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
261 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
262 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
263 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
264 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
266 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
267 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
268 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
269 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
271 ovsdb_idl_omit_alert(idl, &ovsrec_maintenance_point_col_fault);
273 ovsdb_idl_omit_alert(idl, &ovsrec_monitor_col_fault);
275 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
277 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
279 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
281 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
283 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
285 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
286 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
287 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
288 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
289 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
291 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
293 /* Register unixctl commands. */
294 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
295 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
296 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
298 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
307 struct bridge *br, *next_br;
309 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
312 ovsdb_idl_destroy(idl);
315 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
316 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
317 * responsible for freeing '*managersp' (with free()).
319 * You may be asking yourself "why does ovs-vswitchd care?", because
320 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
321 * should not be and in fact is not directly involved in that. But
322 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
323 * it has to tell in-band control where the managers are to enable that.
324 * (Thus, only managers connected in-band are collected.)
327 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
328 struct sockaddr_in **managersp, size_t *n_managersp)
330 struct sockaddr_in *managers = NULL;
331 size_t n_managers = 0;
335 /* Collect all of the potential targets from the "targets" columns of the
336 * rows pointed to by "manager_options", excluding any that are
339 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
340 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
342 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
343 sset_find_and_delete(&targets, m->target);
345 sset_add(&targets, m->target);
349 /* Now extract the targets' IP addresses. */
350 if (!sset_is_empty(&targets)) {
353 managers = xmalloc(sset_count(&targets) * sizeof *managers);
354 SSET_FOR_EACH (target, &targets) {
355 struct sockaddr_in *sin = &managers[n_managers];
357 if ((!strncmp(target, "tcp:", 4)
358 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
359 (!strncmp(target, "ssl:", 4)
360 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
365 sset_destroy(&targets);
367 *managersp = managers;
368 *n_managersp = n_managers;
372 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
374 struct sockaddr_in *managers;
375 struct bridge *br, *next;
376 int sflow_bridge_number;
379 COVERAGE_INC(bridge_reconfigure);
381 /* Create and destroy "struct bridge"s, "struct port"s, and "struct
382 * iface"s according to 'ovs_cfg', with only very minimal configuration
385 * This is purely an update to bridge data structures. Nothing is pushed
386 * down to ofproto or lower layers. */
387 add_del_bridges(ovs_cfg);
388 HMAP_FOR_EACH (br, node, &all_bridges) {
389 bridge_add_del_ports(br);
392 /* Delete all datapaths and datapath ports that are no longer configured.
394 * The kernel will reject any attempt to add a given port to a datapath if
395 * that port already belongs to a different datapath, so we must do all
396 * port deletions before any port additions. A datapath always has a
397 * "local port" so we must delete not-configured datapaths too. */
399 HMAP_FOR_EACH (br, node, &all_bridges) {
401 bridge_del_ofproto_ports(br);
405 /* Create datapaths and datapath ports that are missing.
407 * After this is done, we have our final set of bridges, ports, and
408 * interfaces. Every "struct bridge" has an ofproto, every "struct port"
409 * has at least one iface, every "struct iface" has a valid ofp_port and
411 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
412 if (!br->ofproto && !bridge_add_dp(br)) {
416 HMAP_FOR_EACH (br, node, &all_bridges) {
417 bridge_refresh_ofp_port(br);
418 bridge_add_ofproto_ports(br);
421 /* Complete the configuration. */
422 sflow_bridge_number = 0;
423 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
424 HMAP_FOR_EACH (br, node, &all_bridges) {
427 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
430 port_configure(port);
432 HMAP_FOR_EACH (iface, ofp_port_node, &br->ifaces) {
433 iface_configure_cfm(iface);
434 iface_configure_qos(iface, port->cfg->qos);
435 iface_set_mac(iface);
438 bridge_configure_mirrors(br);
439 bridge_configure_datapath_id(br);
440 bridge_configure_remotes(br, managers, n_managers);
441 bridge_configure_netflow(br);
442 bridge_configure_sflow(br, &sflow_bridge_number);
446 /* ovs-vswitchd has completed initialization, so allow the process that
447 * forked us to exit successfully. */
448 daemonize_complete();
451 /* Iterate over all system dpifs and delete any of them that do not have a
452 * configured bridge or that are the wrong type. */
456 struct sset dpif_names;
457 struct sset dpif_types;
460 sset_init(&dpif_names);
461 sset_init(&dpif_types);
462 dp_enumerate_types(&dpif_types);
463 SSET_FOR_EACH (type, &dpif_types) {
466 dp_enumerate_names(type, &dpif_names);
467 SSET_FOR_EACH (name, &dpif_names) {
468 struct bridge *br = bridge_lookup(name);
469 if (!br || strcmp(type, br->type)) {
472 if (!dpif_open(name, type, &dpif)) {
479 sset_destroy(&dpif_names);
480 sset_destroy(&dpif_types);
484 bridge_add_dp(struct bridge *br)
486 int error = ofproto_create(br->name, br->type, &br->ofproto);
488 VLOG_ERR("failed to create bridge %s: %s", br->name, strerror(error));
495 port_configure(struct port *port)
497 const struct ovsrec_port *cfg = port->cfg;
498 struct bond_settings bond_settings;
499 struct lacp_settings lacp_settings;
500 struct ofproto_bundle_settings s;
508 s.slaves = xmalloc(list_size(&port->ifaces) * sizeof *s.slaves);
509 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
510 s.slaves[s.n_slaves++] = iface->ofp_port;
516 if (list_is_short(&port->ifaces)) {
517 if (*cfg->tag >= 0 && *cfg->tag <= 4095) {
519 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, s.vlan);
522 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
523 * they even work as-is. But they have not been tested. */
524 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
529 /* Get VLAN trunks. */
531 if (s.vlan < 0 && cfg->n_trunks) {
532 s.trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
533 } else if (s.vlan >= 0 && cfg->n_trunks) {
534 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
538 /* Get LACP settings. */
539 s.lacp = port_configure_lacp(port, &lacp_settings);
543 s.lacp_slaves = xmalloc(s.n_slaves * sizeof *s.lacp_slaves);
544 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
545 iface_configure_lacp(iface, &s.lacp_slaves[i++]);
548 s.lacp_slaves = NULL;
551 /* Get bond settings. */
552 if (s.n_slaves > 1) {
553 port_configure_bond(port, &bond_settings);
554 s.bond = &bond_settings;
560 ofproto_bundle_register(port->bridge->ofproto, port, &s);
567 /* Pick local port hardware address and datapath ID for 'br'. */
569 bridge_configure_datapath_id(struct bridge *br)
571 uint8_t ea[ETH_ADDR_LEN];
573 struct iface *local_iface;
574 struct iface *hw_addr_iface;
577 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
578 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
580 int error = netdev_set_etheraddr(local_iface->netdev, ea);
582 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
583 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
584 "Ethernet address: %s",
585 br->name, strerror(error));
588 memcpy(br->ea, ea, ETH_ADDR_LEN);
590 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
591 ofproto_set_datapath_id(br->ofproto, dpid);
593 dpid_string = xasprintf("%016"PRIx64, dpid);
594 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
598 /* Set NetFlow configuration on 'br'. */
600 bridge_configure_netflow(struct bridge *br)
602 struct ovsrec_netflow *cfg = br->cfg->netflow;
603 struct netflow_options opts;
606 ofproto_set_netflow(br->ofproto, NULL);
610 memset(&opts, 0, sizeof opts);
612 /* Get default NetFlow configuration from datapath.
613 * Apply overrides from 'cfg'. */
614 ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
615 if (cfg->engine_type) {
616 opts.engine_type = *cfg->engine_type;
618 if (cfg->engine_id) {
619 opts.engine_id = *cfg->engine_id;
622 /* Configure active timeout interval. */
623 opts.active_timeout = cfg->active_timeout;
624 if (!opts.active_timeout) {
625 opts.active_timeout = -1;
626 } else if (opts.active_timeout < 0) {
627 VLOG_WARN("bridge %s: active timeout interval set to negative "
628 "value, using default instead (%d seconds)", br->name,
629 NF_ACTIVE_TIMEOUT_DEFAULT);
630 opts.active_timeout = -1;
633 /* Add engine ID to interface number to disambiguate bridgs? */
634 opts.add_id_to_iface = cfg->add_id_to_interface;
635 if (opts.add_id_to_iface) {
636 if (opts.engine_id > 0x7f) {
637 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
638 "another vswitch, choose an engine id less than 128",
641 if (hmap_count(&br->ports) > 508) {
642 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
643 "another port when more than 508 ports are used",
649 sset_init(&opts.collectors);
650 sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
653 if (ofproto_set_netflow(br->ofproto, &opts)) {
654 VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
656 sset_destroy(&opts.collectors);
659 /* Set sFlow configuration on 'br'. */
661 bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
663 const struct ovsrec_sflow *cfg = br->cfg->sflow;
664 struct ovsrec_controller **controllers;
665 struct ofproto_sflow_options oso;
666 size_t n_controllers;
670 ofproto_set_sflow(br->ofproto, NULL);
674 memset(&oso, 0, sizeof oso);
676 sset_init(&oso.targets);
677 sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
679 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
681 oso.sampling_rate = *cfg->sampling;
684 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
686 oso.polling_interval = *cfg->polling;
689 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
691 oso.header_len = *cfg->header;
694 oso.sub_id = (*sflow_bridge_number)++;
695 oso.agent_device = cfg->agent;
697 oso.control_ip = NULL;
698 n_controllers = bridge_get_controllers(br, &controllers);
699 for (i = 0; i < n_controllers; i++) {
700 if (controllers[i]->local_ip) {
701 oso.control_ip = controllers[i]->local_ip;
705 ofproto_set_sflow(br->ofproto, &oso);
707 sset_destroy(&oso.targets);
711 bridge_has_bond_fake_iface(const struct bridge *br, const char *name)
713 const struct port *port = port_lookup(br, name);
714 return port && port_is_bond_fake_iface(port);
718 port_is_bond_fake_iface(const struct port *port)
720 return port->cfg->bond_fake_iface && !list_is_short(&port->ifaces);
724 add_del_bridges(const struct ovsrec_open_vswitch *cfg)
726 struct bridge *br, *next;
730 /* Collect new bridges' names and types. */
732 for (i = 0; i < cfg->n_bridges; i++) {
733 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
734 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
735 VLOG_WARN("bridge %s specified twice", br_cfg->name);
739 /* Get rid of deleted bridges or those whose types have changed.
740 * Update 'cfg' of bridges that still exist. */
741 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
742 br->cfg = shash_find_data(&new_br, br->name);
743 if (!br->cfg || strcmp(br->type,
744 dpif_normalize_type(br->cfg->datapath_type))) {
749 /* Add new bridges. */
750 for (i = 0; i < cfg->n_bridges; i++) {
751 const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
752 struct bridge *br = bridge_lookup(br_cfg->name);
754 bridge_create(br_cfg);
758 shash_destroy(&new_br);
761 /* Delete each ofproto port on 'br' that doesn't have a corresponding "struct
764 * The kernel will reject any attempt to add a given port to a datapath if that
765 * port already belongs to a different datapath, so we must do all port
766 * deletions before any port additions. */
768 bridge_del_ofproto_ports(struct bridge *br)
770 struct ofproto_port_dump dump;
771 struct ofproto_port ofproto_port;
773 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
774 const char *name = ofproto_port.name;
779 /* Ignore the local port. We can't change it anyhow. */
780 if (!strcmp(name, br->name)) {
784 /* Get the type that 'ofproto_port' should have (ordinarily the
785 * type of its corresponding iface) or NULL if it should be
787 iface = iface_lookup(br, name);
788 type = (iface ? iface->type
789 : bridge_has_bond_fake_iface(br, name) ? "internal"
792 /* If it's the wrong type then delete the ofproto port. */
794 && !strcmp(ofproto_port.type, type)
795 && (!iface || !iface->netdev
796 || !strcmp(netdev_get_type(iface->netdev), type))) {
799 error = ofproto_port_del(br->ofproto, ofproto_port.ofp_port);
801 VLOG_WARN("bridge %s: failed to remove %s interface (%s)",
802 br->name, name, strerror(error));
805 ofproto_port_unregister(br->ofproto, ofproto_port.ofp_port);
806 netdev_close(iface->netdev);
807 iface->netdev = NULL;
813 iface_set_ofp_port(struct iface *iface, int ofp_port)
815 struct bridge *br = iface->port->bridge;
817 assert(iface->ofp_port < 0 && ofp_port >= 0);
818 iface->ofp_port = ofp_port;
819 hmap_insert(&br->ifaces, &iface->ofp_port_node, hash_int(ofp_port, 0));
824 bridge_refresh_ofp_port(struct bridge *br)
826 struct ofproto_port_dump dump;
827 struct ofproto_port ofproto_port;
830 /* Clear all the "ofp_port"es. */
831 hmap_clear(&br->ifaces);
832 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
835 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
836 iface->ofp_port = -1;
840 /* Obtain the correct "ofp_port"s from ofproto. */
841 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
842 struct iface *iface = iface_lookup(br, ofproto_port.name);
844 if (iface->ofp_port >= 0) {
845 VLOG_WARN("bridge %s: interface %s reported twice",
846 br->name, ofproto_port.name);
847 } else if (iface_from_ofp_port(br, ofproto_port.ofp_port)) {
848 VLOG_WARN("bridge %s: interface %"PRIu16" reported twice",
849 br->name, ofproto_port.ofp_port);
851 iface_set_ofp_port(iface, ofproto_port.ofp_port);
857 /* Add a dpif port for any "struct iface" that doesn't have one.
858 * Delete any "struct iface" for which this fails.
859 * Delete any "struct port" that thereby ends up with no ifaces. */
861 bridge_add_ofproto_ports(struct bridge *br)
863 struct port *port, *next_port;
865 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
866 struct iface *iface, *next_iface;
867 struct ofproto_port ofproto_port;
869 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
873 /* Open the netdev or reconfigure it. */
875 shash_from_ovs_idl_map(iface->cfg->key_options,
876 iface->cfg->value_options,
877 iface->cfg->n_options, &args);
878 if (!iface->netdev) {
879 struct netdev_options options;
880 options.name = iface->name;
881 options.type = iface->type;
882 options.args = &args;
883 options.ethertype = NETDEV_ETH_TYPE_NONE;
884 error = netdev_open(&options, &iface->netdev);
886 error = netdev_set_config(iface->netdev, &args);
888 shash_destroy(&args);
890 VLOG_WARN("could not %s network device %s (%s)",
891 iface->netdev ? "reconfigure" : "open",
892 iface->name, strerror(error));
895 /* Add the port, if necessary. */
896 if (iface->netdev && iface->ofp_port < 0) {
900 error = ofproto_port_add(br->ofproto, iface->netdev,
903 iface_set_ofp_port(iface, ofp_port);
905 netdev_close(iface->netdev);
906 iface->netdev = NULL;
910 /* Delete the iface if */
911 if (iface->netdev && iface->ofp_port >= 0) {
912 VLOG_DBG("bridge %s: interface %s is on port %d",
913 br->name, iface->name, iface->ofp_port);
916 VLOG_ERR("bridge %s: missing %s interface, dropping",
917 br->name, iface->name);
919 /* We already reported a related error, don't bother
922 iface_set_ofport(iface->cfg, -1);
923 iface_destroy(iface);
926 if (list_is_empty(&port->ifaces)) {
927 VLOG_WARN("%s port has no interfaces, dropping", port->name);
932 /* Add bond fake iface if necessary. */
933 if (port_is_bond_fake_iface(port)) {
934 if (ofproto_port_query_by_name(br->ofproto, port->name,
936 struct netdev_options options;
937 struct netdev *netdev;
940 options.name = port->name;
941 options.type = "internal";
943 options.ethertype = NETDEV_ETH_TYPE_NONE;
944 error = netdev_open(&options, &netdev);
946 ofproto_port_add(br->ofproto, netdev, NULL);
947 netdev_close(netdev);
949 VLOG_WARN("could not open network device %s (%s)",
950 port->name, strerror(error));
953 /* Already exists, nothing to do. */
954 ofproto_port_destroy(&ofproto_port);
956 ofproto_port_destroy(&ofproto_port);
962 get_ovsrec_key_value(const struct ovsdb_idl_row *row,
963 const struct ovsdb_idl_column *column,
966 const struct ovsdb_datum *datum;
967 union ovsdb_atom atom;
970 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
971 atom.string = (char *) key;
972 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
973 return idx == UINT_MAX ? NULL : datum->values[idx].string;
977 bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
979 return get_ovsrec_key_value(&br_cfg->header_,
980 &ovsrec_bridge_col_other_config, key);
984 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
985 struct iface **hw_addr_iface)
991 *hw_addr_iface = NULL;
993 /* Did the user request a particular MAC? */
994 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
995 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
996 if (eth_addr_is_multicast(ea)) {
997 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
998 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
999 } else if (eth_addr_is_zero(ea)) {
1000 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
1006 /* Otherwise choose the minimum non-local MAC address among all of the
1008 memset(ea, 0xff, ETH_ADDR_LEN);
1009 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1010 uint8_t iface_ea[ETH_ADDR_LEN];
1011 struct iface *candidate;
1012 struct iface *iface;
1014 /* Mirror output ports don't participate. */
1015 if (ofproto_is_mirror_output_bundle(br->ofproto, port)) {
1019 /* Choose the MAC address to represent the port. */
1021 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
1022 /* Find the interface with this Ethernet address (if any) so that
1023 * we can provide the correct devname to the caller. */
1024 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1025 uint8_t candidate_ea[ETH_ADDR_LEN];
1026 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
1027 && eth_addr_equals(iface_ea, candidate_ea)) {
1032 /* Choose the interface whose MAC address will represent the port.
1033 * The Linux kernel bonding code always chooses the MAC address of
1034 * the first slave added to a bond, and the Fedora networking
1035 * scripts always add slaves to a bond in alphabetical order, so
1036 * for compatibility we choose the interface with the name that is
1037 * first in alphabetical order. */
1038 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1039 if (!iface || strcmp(candidate->name, iface->name) < 0) {
1044 /* The local port doesn't count (since we're trying to choose its
1045 * MAC address anyway). */
1046 if (iface->ofp_port == OFPP_LOCAL) {
1051 error = netdev_get_etheraddr(iface->netdev, iface_ea);
1053 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1054 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
1055 iface->name, strerror(error));
1060 /* Compare against our current choice. */
1061 if (!eth_addr_is_multicast(iface_ea) &&
1062 !eth_addr_is_local(iface_ea) &&
1063 !eth_addr_is_reserved(iface_ea) &&
1064 !eth_addr_is_zero(iface_ea) &&
1065 eth_addr_compare_3way(iface_ea, ea) < 0)
1067 memcpy(ea, iface_ea, ETH_ADDR_LEN);
1068 *hw_addr_iface = iface;
1071 if (eth_addr_is_multicast(ea)) {
1072 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
1073 *hw_addr_iface = NULL;
1074 VLOG_WARN("bridge %s: using default bridge Ethernet "
1075 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1077 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1078 br->name, ETH_ADDR_ARGS(ea));
1082 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
1083 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
1084 * an interface on 'br', then that interface must be passed in as
1085 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1086 * 'hw_addr_iface' must be passed in as a null pointer. */
1088 bridge_pick_datapath_id(struct bridge *br,
1089 const uint8_t bridge_ea[ETH_ADDR_LEN],
1090 struct iface *hw_addr_iface)
1093 * The procedure for choosing a bridge MAC address will, in the most
1094 * ordinary case, also choose a unique MAC that we can use as a datapath
1095 * ID. In some special cases, though, multiple bridges will end up with
1096 * the same MAC address. This is OK for the bridges, but it will confuse
1097 * the OpenFlow controller, because each datapath needs a unique datapath
1100 * Datapath IDs must be unique. It is also very desirable that they be
1101 * stable from one run to the next, so that policy set on a datapath
1104 const char *datapath_id;
1107 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1108 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
1112 if (hw_addr_iface) {
1114 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
1116 * A bridge whose MAC address is taken from a VLAN network device
1117 * (that is, a network device created with vconfig(8) or similar
1118 * tool) will have the same MAC address as a bridge on the VLAN
1119 * device's physical network device.
1121 * Handle this case by hashing the physical network device MAC
1122 * along with the VLAN identifier.
1124 uint8_t buf[ETH_ADDR_LEN + 2];
1125 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1126 buf[ETH_ADDR_LEN] = vlan >> 8;
1127 buf[ETH_ADDR_LEN + 1] = vlan;
1128 return dpid_from_hash(buf, sizeof buf);
1131 * Assume that this bridge's MAC address is unique, since it
1132 * doesn't fit any of the cases we handle specially.
1137 * A purely internal bridge, that is, one that has no non-virtual
1138 * network devices on it at all, is more difficult because it has no
1139 * natural unique identifier at all.
1141 * When the host is a XenServer, we handle this case by hashing the
1142 * host's UUID with the name of the bridge. Names of bridges are
1143 * persistent across XenServer reboots, although they can be reused if
1144 * an internal network is destroyed and then a new one is later
1145 * created, so this is fairly effective.
1147 * When the host is not a XenServer, we punt by using a random MAC
1148 * address on each run.
1150 const char *host_uuid = xenserver_get_host_uuid();
1152 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1153 dpid = dpid_from_hash(combined, strlen(combined));
1159 return eth_addr_to_uint64(bridge_ea);
1163 dpid_from_hash(const void *data, size_t n)
1165 uint8_t hash[SHA1_DIGEST_SIZE];
1167 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
1168 sha1_bytes(data, n, hash);
1169 eth_addr_mark_random(hash);
1170 return eth_addr_to_uint64(hash);
1174 iface_refresh_status(struct iface *iface)
1178 enum netdev_flags flags;
1185 if (iface_is_synthetic(iface)) {
1191 if (!netdev_get_status(iface->netdev, &sh)) {
1193 char **keys, **values;
1195 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1196 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1201 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1204 shash_destroy_free_data(&sh);
1206 error = netdev_get_flags(iface->netdev, &flags);
1208 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1211 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1214 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1216 ovsrec_interface_set_duplex(iface->cfg,
1217 netdev_features_is_full_duplex(current)
1219 /* warning: uint64_t -> int64_t conversion */
1220 bps = netdev_features_to_bps(current);
1221 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1224 ovsrec_interface_set_duplex(iface->cfg, NULL);
1225 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1228 ovsrec_interface_set_link_state(iface->cfg,
1229 iface_get_carrier(iface) ? "up" : "down");
1231 error = netdev_get_mtu(iface->netdev, &mtu);
1232 if (!error && mtu != INT_MAX) {
1234 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1237 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1241 /* Writes 'iface''s CFM statistics to the database. Returns true if anything
1242 * changed, false otherwise. */
1244 iface_refresh_cfm_stats(struct iface *iface)
1246 const struct ovsrec_monitor *mon;
1247 const struct cfm *cfm;
1248 bool changed = false;
1251 mon = iface->cfg->monitor;
1252 cfm = ofproto_port_get_cfm(iface->port->bridge->ofproto, iface->ofp_port);
1258 for (i = 0; i < mon->n_remote_mps; i++) {
1259 const struct ovsrec_maintenance_point *mp;
1260 const struct remote_mp *rmp;
1262 mp = mon->remote_mps[i];
1263 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1265 if (mp->n_fault != 1 || mp->fault[0] != rmp->fault) {
1266 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1271 if (mon->n_fault != 1 || mon->fault[0] != cfm->fault) {
1272 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1280 iface_refresh_lacp_stats(struct iface *iface)
1282 struct ofproto *ofproto = iface->port->bridge->ofproto;
1283 int old = iface->cfg->lacp_current ? *iface->cfg->lacp_current : -1;
1284 int new = ofproto_port_is_lacp_current(ofproto, iface->ofp_port);
1288 ovsrec_interface_set_lacp_current(iface->cfg, ¤t, new >= 0);
1294 iface_refresh_stats(struct iface *iface)
1300 static const struct iface_stat iface_stats[] = {
1301 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1302 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1303 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1304 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1305 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1306 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1307 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1308 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1309 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1310 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1311 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1312 { "collisions", offsetof(struct netdev_stats, collisions) },
1314 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1315 const struct iface_stat *s;
1317 char *keys[N_STATS];
1318 int64_t values[N_STATS];
1321 struct netdev_stats stats;
1323 if (iface_is_synthetic(iface)) {
1327 /* Intentionally ignore return value, since errors will set 'stats' to
1328 * all-1s, and we will deal with that correctly below. */
1329 netdev_get_stats(iface->netdev, &stats);
1332 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1333 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1334 if (value != UINT64_MAX) {
1341 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1345 refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1347 struct ovsdb_datum datum;
1351 get_system_stats(&stats);
1353 ovsdb_datum_from_shash(&datum, &stats);
1354 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1358 static inline const char *
1359 nx_role_to_str(enum nx_role role)
1364 case NX_ROLE_MASTER:
1369 return "*** INVALID ROLE ***";
1374 bridge_refresh_controller_status(const struct bridge *br)
1377 const struct ovsrec_controller *cfg;
1379 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1381 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
1382 struct ofproto_controller_info *cinfo =
1383 shash_find_data(&info, cfg->target);
1386 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1387 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1388 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1389 (char **) cinfo->pairs.values,
1392 ovsrec_controller_set_is_connected(cfg, false);
1393 ovsrec_controller_set_role(cfg, NULL);
1394 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1398 ofproto_free_ofproto_controller_info(&info);
1404 const struct ovsrec_open_vswitch *cfg;
1406 bool datapath_destroyed;
1407 bool database_changed;
1410 /* Let each bridge do the work that it needs to do. */
1411 datapath_destroyed = false;
1412 HMAP_FOR_EACH (br, node, &all_bridges) {
1413 int error = ofproto_run(br->ofproto);
1415 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1416 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1417 "forcing reconfiguration", br->name);
1418 datapath_destroyed = true;
1422 /* (Re)configure if necessary. */
1423 database_changed = ovsdb_idl_run(idl);
1424 cfg = ovsrec_open_vswitch_first(idl);
1426 /* Re-configure SSL. We do this on every trip through the main loop,
1427 * instead of just when the database changes, because the contents of the
1428 * key and certificate files can change without the database changing.
1430 * We do this before bridge_reconfigure() because that function might
1431 * initiate SSL connections and thus requires SSL to be configured. */
1432 if (cfg && cfg->ssl) {
1433 const struct ovsrec_ssl *ssl = cfg->ssl;
1435 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1436 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1439 if (database_changed || datapath_destroyed) {
1441 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1443 bridge_reconfigure(cfg);
1445 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1446 ovsdb_idl_txn_commit(txn);
1447 ovsdb_idl_txn_destroy(txn); /* XXX */
1449 /* We still need to reconfigure to avoid dangling pointers to
1450 * now-destroyed ovsrec structures inside bridge data. */
1451 static const struct ovsrec_open_vswitch null_cfg;
1453 bridge_reconfigure(&null_cfg);
1457 /* Refresh system and interface stats if necessary. */
1458 if (time_msec() >= stats_timer) {
1460 struct ovsdb_idl_txn *txn;
1462 txn = ovsdb_idl_txn_create(idl);
1463 HMAP_FOR_EACH (br, node, &all_bridges) {
1466 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1467 struct iface *iface;
1469 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1470 iface_refresh_stats(iface);
1471 iface_refresh_status(iface);
1474 bridge_refresh_controller_status(br);
1476 refresh_system_stats(cfg);
1477 ovsdb_idl_txn_commit(txn);
1478 ovsdb_idl_txn_destroy(txn); /* XXX */
1481 stats_timer = time_msec() + STATS_INTERVAL;
1484 if (time_msec() >= db_limiter) {
1485 struct ovsdb_idl_txn *txn;
1486 bool changed = false;
1488 txn = ovsdb_idl_txn_create(idl);
1489 HMAP_FOR_EACH (br, node, &all_bridges) {
1492 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1493 struct iface *iface;
1495 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1496 changed = iface_refresh_cfm_stats(iface) || changed;
1497 changed = iface_refresh_lacp_stats(iface) || changed;
1503 db_limiter = time_msec() + DB_LIMIT_INTERVAL;
1506 ovsdb_idl_txn_commit(txn);
1507 ovsdb_idl_txn_destroy(txn);
1516 HMAP_FOR_EACH (br, node, &all_bridges) {
1517 ofproto_wait(br->ofproto);
1519 ovsdb_idl_wait(idl);
1520 poll_timer_wait_until(stats_timer);
1522 if (db_limiter > time_msec()) {
1523 poll_timer_wait_until(db_limiter);
1527 /* CFM unixctl user interface functions. */
1529 cfm_unixctl_show(struct unixctl_conn *conn,
1530 const char *args, void *aux OVS_UNUSED)
1532 struct ds ds = DS_EMPTY_INITIALIZER;
1533 struct iface *iface;
1534 const struct cfm *cfm;
1536 iface = iface_find(args);
1538 unixctl_command_reply(conn, 501, "no such interface");
1542 cfm = ofproto_port_get_cfm(iface->port->bridge->ofproto, iface->ofp_port);
1545 unixctl_command_reply(conn, 501, "CFM not enabled");
1549 cfm_dump_ds(cfm, &ds);
1550 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1554 /* QoS unixctl user interface functions. */
1556 struct qos_unixctl_show_cbdata {
1558 struct iface *iface;
1562 qos_unixctl_show_cb(unsigned int queue_id,
1563 const struct shash *details,
1566 struct qos_unixctl_show_cbdata *data = aux;
1567 struct ds *ds = data->ds;
1568 struct iface *iface = data->iface;
1569 struct netdev_queue_stats stats;
1570 struct shash_node *node;
1573 ds_put_cstr(ds, "\n");
1575 ds_put_format(ds, "Queue %u:\n", queue_id);
1577 ds_put_cstr(ds, "Default:\n");
1580 SHASH_FOR_EACH (node, details) {
1581 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1584 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1586 if (stats.tx_packets != UINT64_MAX) {
1587 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1590 if (stats.tx_bytes != UINT64_MAX) {
1591 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1594 if (stats.tx_errors != UINT64_MAX) {
1595 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1598 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1599 queue_id, strerror(error));
1604 qos_unixctl_show(struct unixctl_conn *conn,
1605 const char *args, void *aux OVS_UNUSED)
1607 struct ds ds = DS_EMPTY_INITIALIZER;
1608 struct shash sh = SHASH_INITIALIZER(&sh);
1609 struct iface *iface;
1611 struct shash_node *node;
1612 struct qos_unixctl_show_cbdata data;
1615 iface = iface_find(args);
1617 unixctl_command_reply(conn, 501, "no such interface");
1621 netdev_get_qos(iface->netdev, &type, &sh);
1623 if (*type != '\0') {
1624 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1626 SHASH_FOR_EACH (node, &sh) {
1627 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1632 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1635 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1637 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1639 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1640 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1643 shash_destroy_free_data(&sh);
1647 /* Bridge reconfiguration functions. */
1649 bridge_create(const struct ovsrec_bridge *br_cfg)
1653 assert(!bridge_lookup(br_cfg->name));
1654 br = xzalloc(sizeof *br);
1656 br->name = xstrdup(br_cfg->name);
1657 br->type = xstrdup(dpif_normalize_type(br_cfg->datapath_type));
1659 eth_addr_nicira_random(br->default_ea);
1661 hmap_init(&br->ports);
1662 hmap_init(&br->ifaces);
1663 hmap_init(&br->iface_by_name);
1664 hmap_init(&br->mirrors);
1666 hmap_insert(&all_bridges, &br->node, hash_string(br->name, 0));
1670 bridge_destroy(struct bridge *br)
1673 struct mirror *mirror, *next_mirror;
1674 struct port *port, *next_port;
1676 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
1679 HMAP_FOR_EACH_SAFE (mirror, next_mirror, hmap_node, &br->mirrors) {
1680 mirror_destroy(mirror);
1682 hmap_remove(&all_bridges, &br->node);
1683 ofproto_destroy(br->ofproto);
1684 hmap_destroy(&br->ifaces);
1685 hmap_destroy(&br->ports);
1686 hmap_destroy(&br->iface_by_name);
1687 hmap_destroy(&br->mirrors);
1694 static struct bridge *
1695 bridge_lookup(const char *name)
1699 HMAP_FOR_EACH_WITH_HASH (br, node, hash_string(name, 0), &all_bridges) {
1700 if (!strcmp(br->name, name)) {
1707 /* Handle requests for a listing of all flows known by the OpenFlow
1708 * stack, including those normally hidden. */
1710 bridge_unixctl_dump_flows(struct unixctl_conn *conn,
1711 const char *args, void *aux OVS_UNUSED)
1716 br = bridge_lookup(args);
1718 unixctl_command_reply(conn, 501, "Unknown bridge");
1723 ofproto_get_all_flows(br->ofproto, &results);
1725 unixctl_command_reply(conn, 200, ds_cstr(&results));
1726 ds_destroy(&results);
1729 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1730 * connections and reconnect. If BRIDGE is not specified, then all bridges
1731 * drop their controller connections and reconnect. */
1733 bridge_unixctl_reconnect(struct unixctl_conn *conn,
1734 const char *args, void *aux OVS_UNUSED)
1737 if (args[0] != '\0') {
1738 br = bridge_lookup(args);
1740 unixctl_command_reply(conn, 501, "Unknown bridge");
1743 ofproto_reconnect_controllers(br->ofproto);
1745 HMAP_FOR_EACH (br, node, &all_bridges) {
1746 ofproto_reconnect_controllers(br->ofproto);
1749 unixctl_command_reply(conn, 200, NULL);
1753 bridge_get_controllers(const struct bridge *br,
1754 struct ovsrec_controller ***controllersp)
1756 struct ovsrec_controller **controllers;
1757 size_t n_controllers;
1759 controllers = br->cfg->controller;
1760 n_controllers = br->cfg->n_controller;
1762 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1768 *controllersp = controllers;
1770 return n_controllers;
1773 /* Adds and deletes "struct port"s and "struct iface"s under 'br' to match
1774 * those configured in 'br->cfg'. */
1776 bridge_add_del_ports(struct bridge *br)
1778 struct port *port, *next;
1779 struct shash_node *node;
1780 struct shash new_ports;
1783 /* Collect new ports. */
1784 shash_init(&new_ports);
1785 for (i = 0; i < br->cfg->n_ports; i++) {
1786 const char *name = br->cfg->ports[i]->name;
1787 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1788 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1792 if (bridge_get_controllers(br, NULL)
1793 && !shash_find(&new_ports, br->name)) {
1794 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
1795 br->name, br->name);
1797 br->synth_local_port.interfaces = &br->synth_local_ifacep;
1798 br->synth_local_port.n_interfaces = 1;
1799 br->synth_local_port.name = br->name;
1801 br->synth_local_iface.name = br->name;
1802 br->synth_local_iface.type = "internal";
1804 br->synth_local_ifacep = &br->synth_local_iface;
1806 shash_add(&new_ports, br->name, &br->synth_local_port);
1809 /* Get rid of deleted ports.
1810 * Get rid of deleted interfaces on ports that still exist.
1811 * Update 'cfg' of ports that still exist. */
1812 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1813 port->cfg = shash_find_data(&new_ports, port->name);
1817 port_del_ifaces(port);
1821 /* Create new ports.
1822 * Add new interfaces to existing ports. */
1823 SHASH_FOR_EACH (node, &new_ports) {
1824 struct port *port = port_lookup(br, node->name);
1826 struct ovsrec_port *cfg = node->data;
1827 port = port_create(br, cfg);
1829 port_add_ifaces(port);
1830 if (list_is_empty(&port->ifaces)) {
1831 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1832 br->name, port->name);
1836 shash_destroy(&new_ports);
1839 /* Initializes 'oc' appropriately as a management service controller for
1842 * The caller must free oc->target when it is no longer needed. */
1844 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1845 struct ofproto_controller *oc)
1847 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
1848 oc->max_backoff = 0;
1849 oc->probe_interval = 60;
1850 oc->band = OFPROTO_OUT_OF_BAND;
1852 oc->burst_limit = 0;
1855 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1857 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1858 struct ofproto_controller *oc)
1860 oc->target = c->target;
1861 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1862 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1863 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1864 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
1865 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1866 oc->burst_limit = (c->controller_burst_limit
1867 ? *c->controller_burst_limit : 0);
1870 /* Configures the IP stack for 'br''s local interface properly according to the
1871 * configuration in 'c'. */
1873 bridge_configure_local_iface_netdev(struct bridge *br,
1874 struct ovsrec_controller *c)
1876 struct netdev *netdev;
1877 struct in_addr mask, gateway;
1879 struct iface *local_iface;
1882 /* If there's no local interface or no IP address, give up. */
1883 local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
1884 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1888 /* Bring up the local interface. */
1889 netdev = local_iface->netdev;
1890 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1892 /* Configure the IP address and netmask. */
1893 if (!c->local_netmask
1894 || !inet_aton(c->local_netmask, &mask)
1896 mask.s_addr = guess_netmask(ip.s_addr);
1898 if (!netdev_set_in4(netdev, ip, mask)) {
1899 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1900 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1903 /* Configure the default gateway. */
1904 if (c->local_gateway
1905 && inet_aton(c->local_gateway, &gateway)
1906 && gateway.s_addr) {
1907 if (!netdev_add_router(netdev, gateway)) {
1908 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1909 br->name, IP_ARGS(&gateway.s_addr));
1915 bridge_configure_remotes(struct bridge *br,
1916 const struct sockaddr_in *managers, size_t n_managers)
1918 const char *disable_ib_str, *queue_id_str;
1919 bool disable_in_band = false;
1922 struct ovsrec_controller **controllers;
1923 size_t n_controllers;
1925 enum ofproto_fail_mode fail_mode;
1927 struct ofproto_controller *ocs;
1931 /* Check if we should disable in-band control on this bridge. */
1932 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
1933 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
1934 disable_in_band = true;
1937 /* Set OpenFlow queue ID for in-band control. */
1938 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
1939 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
1940 ofproto_set_in_band_queue(br->ofproto, queue_id);
1942 if (disable_in_band) {
1943 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
1945 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
1948 n_controllers = bridge_get_controllers(br, &controllers);
1950 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
1953 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
1954 for (i = 0; i < n_controllers; i++) {
1955 struct ovsrec_controller *c = controllers[i];
1957 if (!strncmp(c->target, "punix:", 6)
1958 || !strncmp(c->target, "unix:", 5)) {
1959 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1961 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
1962 * domain sockets and overwriting arbitrary local files. */
1963 VLOG_ERR_RL(&rl, "bridge %s: not adding Unix domain socket "
1964 "controller \"%s\" due to possibility for remote "
1965 "exploit", br->name, c->target);
1969 bridge_configure_local_iface_netdev(br, c);
1970 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
1971 if (disable_in_band) {
1972 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
1977 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
1978 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
1981 /* Set the fail-mode. */
1982 fail_mode = !br->cfg->fail_mode
1983 || !strcmp(br->cfg->fail_mode, "standalone")
1984 ? OFPROTO_FAIL_STANDALONE
1985 : OFPROTO_FAIL_SECURE;
1986 ofproto_set_fail_mode(br->ofproto, fail_mode);
1988 /* Configure OpenFlow controller connection snooping. */
1989 if (!ofproto_has_snoops(br->ofproto)) {
1993 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
1994 ovs_rundir(), br->name));
1995 ofproto_set_snoops(br->ofproto, &snoops);
1996 sset_destroy(&snoops);
2000 /* Port functions. */
2002 static struct port *
2003 port_create(struct bridge *br, const struct ovsrec_port *cfg)
2007 port = xzalloc(sizeof *port);
2009 port->name = xstrdup(cfg->name);
2011 list_init(&port->ifaces);
2013 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
2015 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2021 get_port_other_config(const struct ovsrec_port *port, const char *key,
2022 const char *default_value)
2026 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
2028 return value ? value : default_value;
2032 get_interface_other_config(const struct ovsrec_interface *iface,
2033 const char *key, const char *default_value)
2037 value = get_ovsrec_key_value(&iface->header_,
2038 &ovsrec_interface_col_other_config, key);
2039 return value ? value : default_value;
2042 /* Deletes interfaces from 'port' that are no longer configured for it. */
2044 port_del_ifaces(struct port *port)
2046 struct iface *iface, *next;
2047 struct sset new_ifaces;
2050 /* Collect list of new interfaces. */
2051 sset_init(&new_ifaces);
2052 for (i = 0; i < port->cfg->n_interfaces; i++) {
2053 const char *name = port->cfg->interfaces[i]->name;
2054 sset_add(&new_ifaces, name);
2057 /* Get rid of deleted interfaces. */
2058 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2059 if (!sset_contains(&new_ifaces, iface->name)) {
2060 iface_destroy(iface);
2064 sset_destroy(&new_ifaces);
2067 /* Adds new interfaces to 'port' and updates 'type' and 'cfg' members of
2070 port_add_ifaces(struct port *port)
2072 struct shash new_ifaces;
2073 struct shash_node *node;
2076 /* Collect new ifaces. */
2077 shash_init(&new_ifaces);
2078 for (i = 0; i < port->cfg->n_interfaces; i++) {
2079 const struct ovsrec_interface *cfg = port->cfg->interfaces[i];
2080 if (!shash_add_once(&new_ifaces, cfg->name, cfg)) {
2081 VLOG_WARN("port %s: %s specified twice as port interface",
2082 port->name, cfg->name);
2083 iface_set_ofport(cfg, -1);
2087 /* Create new interfaces.
2088 * Update interface types and 'cfg' members. */
2089 SHASH_FOR_EACH (node, &new_ifaces) {
2090 const struct ovsrec_interface *cfg = node->data;
2091 const char *iface_name = node->name;
2092 struct iface *iface;
2094 iface = iface_lookup(port->bridge, iface_name);
2096 iface = iface_create(port, cfg);
2101 /* Determine interface type. The local port always has type
2102 * "internal". Other ports take their type from the database and
2103 * default to "system" if none is specified. */
2104 iface->type = (!strcmp(iface_name, port->bridge->name) ? "internal"
2105 : cfg->type[0] ? cfg->type
2108 shash_destroy(&new_ifaces);
2112 port_destroy(struct port *port)
2115 struct bridge *br = port->bridge;
2116 struct iface *iface, *next;
2119 ofproto_bundle_unregister(br->ofproto, port);
2122 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2123 iface_destroy(iface);
2126 hmap_remove(&br->ports, &port->hmap_node);
2128 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
2135 static struct port *
2136 port_lookup(const struct bridge *br, const char *name)
2140 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
2142 if (!strcmp(port->name, name)) {
2150 enable_lacp(struct port *port, bool *activep)
2152 if (!port->cfg->lacp) {
2153 /* XXX when LACP implementation has been sufficiently tested, enable by
2154 * default and make active on bonded ports. */
2156 } else if (!strcmp(port->cfg->lacp, "off")) {
2158 } else if (!strcmp(port->cfg->lacp, "active")) {
2161 } else if (!strcmp(port->cfg->lacp, "passive")) {
2165 VLOG_WARN("port %s: unknown LACP mode %s",
2166 port->name, port->cfg->lacp);
2171 static struct lacp_settings *
2172 port_configure_lacp(struct port *port, struct lacp_settings *s)
2174 const char *lacp_time;
2175 long long int custom_time;
2178 if (!enable_lacp(port, &s->active)) {
2182 s->name = port->name;
2183 memcpy(s->id, port->bridge->ea, ETH_ADDR_LEN);
2185 /* Prefer bondable links if unspecified. */
2186 priority = atoi(get_port_other_config(port->cfg, "lacp-system-priority",
2188 s->priority = (priority > 0 && priority <= UINT16_MAX
2190 : UINT16_MAX - !list_is_short(&port->ifaces));
2192 s->strict = !strcmp(get_port_other_config(port->cfg, "lacp-strict",
2196 lacp_time = get_port_other_config(port->cfg, "lacp-time", "slow");
2197 custom_time = atoi(lacp_time);
2198 if (!strcmp(lacp_time, "fast")) {
2199 s->lacp_time = LACP_TIME_FAST;
2200 } else if (!strcmp(lacp_time, "slow")) {
2201 s->lacp_time = LACP_TIME_SLOW;
2202 } else if (custom_time > 0) {
2203 s->lacp_time = LACP_TIME_CUSTOM;
2204 s->custom_time = custom_time;
2206 s->lacp_time = LACP_TIME_SLOW;
2213 iface_configure_lacp(struct iface *iface, struct lacp_slave_settings *s)
2215 int priority, portid;
2217 portid = atoi(get_interface_other_config(iface->cfg, "lacp-port-id", "0"));
2218 priority = atoi(get_interface_other_config(iface->cfg,
2219 "lacp-port-priority", "0"));
2221 if (portid <= 0 || portid > UINT16_MAX) {
2222 portid = iface->ofp_port;
2225 if (priority <= 0 || priority > UINT16_MAX) {
2226 priority = UINT16_MAX;
2229 s->name = iface->name;
2231 s->priority = priority;
2235 port_configure_bond(struct port *port, struct bond_settings *s)
2237 const char *detect_s;
2239 s->name = port->name;
2240 s->balance = BM_SLB;
2241 if (port->cfg->bond_mode
2242 && !bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
2243 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
2244 port->name, port->cfg->bond_mode,
2245 bond_mode_to_string(s->balance));
2248 s->detect = BLSM_CARRIER;
2249 detect_s = get_port_other_config(port->cfg, "bond-detect-mode", NULL);
2250 if (detect_s && !bond_detect_mode_from_string(&s->detect, detect_s)) {
2251 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
2253 port->name, detect_s, bond_detect_mode_to_string(s->detect));
2256 s->miimon_interval = atoi(
2257 get_port_other_config(port->cfg, "bond-miimon-interval", "200"));
2258 if (s->miimon_interval < 100) {
2259 s->miimon_interval = 100;
2262 s->up_delay = MAX(0, port->cfg->bond_updelay);
2263 s->down_delay = MAX(0, port->cfg->bond_downdelay);
2264 s->rebalance_interval = atoi(
2265 get_port_other_config(port->cfg, "bond-rebalance-interval", "10000"));
2266 if (s->rebalance_interval < 1000) {
2267 s->rebalance_interval = 1000;
2270 s->fake_iface = port->cfg->bond_fake_iface;
2273 /* Interface functions. */
2275 static struct iface *
2276 iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
2278 struct bridge *br = port->bridge;
2279 struct iface *iface;
2280 char *name = if_cfg->name;
2282 iface = xzalloc(sizeof *iface);
2284 iface->name = xstrdup(name);
2285 iface->ofp_port = -1;
2286 iface->tag = tag_create_random();
2287 iface->netdev = NULL;
2288 iface->cfg = if_cfg;
2290 hmap_insert(&br->iface_by_name, &iface->name_node, hash_string(name, 0));
2292 list_push_back(&port->ifaces, &iface->port_elem);
2294 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
2300 iface_destroy(struct iface *iface)
2303 struct port *port = iface->port;
2304 struct bridge *br = port->bridge;
2306 if (br->ofproto && iface->ofp_port >= 0) {
2307 ofproto_port_unregister(br->ofproto, iface->ofp_port);
2310 if (iface->ofp_port >= 0) {
2311 hmap_remove(&br->ifaces, &iface->ofp_port_node);
2314 list_remove(&iface->port_elem);
2315 hmap_remove(&br->iface_by_name, &iface->name_node);
2317 netdev_close(iface->netdev);
2324 static struct iface *
2325 iface_lookup(const struct bridge *br, const char *name)
2327 struct iface *iface;
2329 HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
2330 &br->iface_by_name) {
2331 if (!strcmp(iface->name, name)) {
2339 static struct iface *
2340 iface_find(const char *name)
2342 const struct bridge *br;
2344 HMAP_FOR_EACH (br, node, &all_bridges) {
2345 struct iface *iface = iface_lookup(br, name);
2354 static struct iface *
2355 iface_from_ofp_port(const struct bridge *br, uint16_t ofp_port)
2357 struct iface *iface;
2359 HMAP_FOR_EACH_IN_BUCKET (iface, ofp_port_node,
2360 hash_int(ofp_port, 0), &br->ifaces) {
2361 if (iface->ofp_port == ofp_port) {
2368 /* Set Ethernet address of 'iface', if one is specified in the configuration
2371 iface_set_mac(struct iface *iface)
2373 uint8_t ea[ETH_ADDR_LEN];
2375 if (!strcmp(iface->type, "internal")
2376 && iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
2377 if (iface->ofp_port == OFPP_LOCAL) {
2378 VLOG_ERR("interface %s: ignoring mac in Interface record "
2379 "(use Bridge record to set local port's mac)",
2381 } else if (eth_addr_is_multicast(ea)) {
2382 VLOG_ERR("interface %s: cannot set MAC to multicast address",
2385 int error = netdev_set_etheraddr(iface->netdev, ea);
2387 VLOG_ERR("interface %s: setting MAC failed (%s)",
2388 iface->name, strerror(error));
2394 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
2396 iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
2398 if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
2399 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
2403 /* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
2405 * The value strings in '*shash' are taken directly from values[], not copied,
2406 * so the caller should not modify or free them. */
2408 shash_from_ovs_idl_map(char **keys, char **values, size_t n,
2409 struct shash *shash)
2414 for (i = 0; i < n; i++) {
2415 shash_add(shash, keys[i], values[i]);
2419 /* Creates 'keys' and 'values' arrays from 'shash'.
2421 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
2422 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
2423 * are populated with with strings taken directly from 'shash' and thus have
2424 * the same ownership of the key-value pairs in shash.
2427 shash_to_ovs_idl_map(struct shash *shash,
2428 char ***keys, char ***values, size_t *n)
2432 struct shash_node *sn;
2434 count = shash_count(shash);
2436 k = xmalloc(count * sizeof *k);
2437 v = xmalloc(count * sizeof *v);
2440 SHASH_FOR_EACH(sn, shash) {
2451 struct iface_delete_queues_cbdata {
2452 struct netdev *netdev;
2453 const struct ovsdb_datum *queues;
2457 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
2459 union ovsdb_atom atom;
2461 atom.integer = target;
2462 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
2466 iface_delete_queues(unsigned int queue_id,
2467 const struct shash *details OVS_UNUSED, void *cbdata_)
2469 struct iface_delete_queues_cbdata *cbdata = cbdata_;
2471 if (!queue_ids_include(cbdata->queues, queue_id)) {
2472 netdev_delete_queue(cbdata->netdev, queue_id);
2477 iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
2479 if (!qos || qos->type[0] == '\0') {
2480 netdev_set_qos(iface->netdev, NULL, NULL);
2482 struct iface_delete_queues_cbdata cbdata;
2483 struct shash details;
2486 /* Configure top-level Qos for 'iface'. */
2487 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
2488 qos->n_other_config, &details);
2489 netdev_set_qos(iface->netdev, qos->type, &details);
2490 shash_destroy(&details);
2492 /* Deconfigure queues that were deleted. */
2493 cbdata.netdev = iface->netdev;
2494 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
2496 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
2498 /* Configure queues for 'iface'. */
2499 for (i = 0; i < qos->n_queues; i++) {
2500 const struct ovsrec_queue *queue = qos->value_queues[i];
2501 unsigned int queue_id = qos->key_queues[i];
2503 shash_from_ovs_idl_map(queue->key_other_config,
2504 queue->value_other_config,
2505 queue->n_other_config, &details);
2506 netdev_set_queue(iface->netdev, queue_id, &details);
2507 shash_destroy(&details);
2511 netdev_set_policing(iface->netdev,
2512 iface->cfg->ingress_policing_rate,
2513 iface->cfg->ingress_policing_burst);
2517 iface_configure_cfm(struct iface *iface)
2521 uint16_t *remote_mps;
2522 struct ovsrec_monitor *mon;
2523 uint8_t maid[CCM_MAID_LEN];
2525 mon = iface->cfg->monitor;
2528 ofproto_port_clear_cfm(iface->port->bridge->ofproto, iface->ofp_port);
2532 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
2533 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
2537 cfm.mpid = mon->mpid;
2538 cfm.interval = mon->interval ? *mon->interval : 1000;
2540 memcpy(cfm.maid, maid, sizeof cfm.maid);
2542 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
2543 for(i = 0; i < mon->n_remote_mps; i++) {
2544 remote_mps[i] = mon->remote_mps[i]->mpid;
2547 ofproto_port_set_cfm(iface->port->bridge->ofproto, iface->ofp_port,
2548 &cfm, remote_mps, mon->n_remote_mps);
2552 /* Read carrier or miimon status directly from 'iface''s netdev, according to
2553 * how 'iface''s port is configured.
2555 * Returns true if 'iface' is up, false otherwise. */
2557 iface_get_carrier(const struct iface *iface)
2560 return netdev_get_carrier(iface->netdev);
2563 /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
2564 * instead of obtaining it from the database. */
2566 iface_is_synthetic(const struct iface *iface)
2568 return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
2571 /* Port mirroring. */
2573 static struct mirror *
2574 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
2578 HMAP_FOR_EACH_IN_BUCKET (m, hmap_node, uuid_hash(uuid), &br->mirrors) {
2579 if (uuid_equals(uuid, &m->uuid)) {
2587 bridge_configure_mirrors(struct bridge *br)
2589 const struct ovsdb_datum *mc;
2590 unsigned long *flood_vlans;
2591 struct mirror *m, *next;
2594 /* Get rid of deleted mirrors. */
2595 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
2596 HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mirrors) {
2597 union ovsdb_atom atom;
2599 atom.uuid = m->uuid;
2600 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
2605 /* Add new mirrors and reconfigure existing ones. */
2606 for (i = 0; i < br->cfg->n_mirrors; i++) {
2607 const struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
2608 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
2610 m = mirror_create(br, cfg);
2612 if (!mirror_configure(m, cfg)) {
2617 /* Update flooded vlans (for RSPAN). */
2618 flood_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
2619 br->cfg->n_flood_vlans);
2620 ofproto_set_flood_vlans(br->ofproto, flood_vlans);
2621 bitmap_free(flood_vlans);
2624 static struct mirror *
2625 mirror_create(struct bridge *br, const struct ovsrec_mirror *cfg)
2629 m = xzalloc(sizeof *m);
2630 m->uuid = cfg->header_.uuid;
2631 hmap_insert(&br->mirrors, &m->hmap_node, uuid_hash(&m->uuid));
2633 m->name = xstrdup(cfg->name);
2639 mirror_destroy(struct mirror *m)
2642 struct bridge *br = m->bridge;
2645 ofproto_mirror_unregister(br->ofproto, m);
2648 hmap_remove(&br->mirrors, &m->hmap_node);
2655 mirror_collect_ports(struct mirror *m,
2656 struct ovsrec_port **in_ports, int n_in_ports,
2657 void ***out_portsp, size_t *n_out_portsp)
2659 void **out_ports = xmalloc(n_in_ports * sizeof *out_ports);
2660 size_t n_out_ports = 0;
2663 for (i = 0; i < n_in_ports; i++) {
2664 const char *name = in_ports[i]->name;
2665 struct port *port = port_lookup(m->bridge, name);
2667 out_ports[n_out_ports++] = port;
2669 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
2670 "port %s", m->bridge->name, m->name, name);
2673 *out_portsp = out_ports;
2674 *n_out_portsp = n_out_ports;
2678 mirror_configure(struct mirror *m, const struct ovsrec_mirror *cfg)
2680 struct ofproto_mirror_settings s;
2681 struct port *out_port;
2685 if (strcmp(cfg->name, m->name)) {
2687 m->name = xstrdup(cfg->name);
2691 /* Get output port or VLAN. */
2692 if (cfg->output_port) {
2693 s.out_bundle = port_lookup(m->bridge, cfg->output_port->name);
2695 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
2696 m->bridge->name, m->name);
2699 s.out_vlan = UINT16_MAX;
2701 if (cfg->output_vlan) {
2702 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
2703 "output vlan; ignoring output vlan",
2704 m->bridge->name, m->name);
2706 } else if (cfg->output_vlan) {
2707 /* The database should prevent invalid VLAN values. */
2708 s.out_bundle = NULL;
2709 s.out_vlan = *cfg->output_vlan;
2711 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
2712 m->bridge->name, m->name);
2716 /* Get port selection. */
2717 if (cfg->select_all) {
2718 size_t n_ports = hmap_count(&m->bridge->ports);
2719 void **ports = xmalloc(n_ports * sizeof *ports);
2723 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
2733 /* Get ports, dropping ports that don't exist.
2734 * The IDL ensures that there are no duplicates. */
2735 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
2736 &s.srcs, &s.n_srcs);
2737 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
2738 &s.dsts, &s.n_dsts);
2742 /* Get VLAN selection. */
2743 s.src_vlans = vlan_bitmap_from_array(cfg->select_vlan, cfg->n_select_vlan);
2746 ofproto_mirror_register(m->bridge->ofproto, m, &s);
2749 if (s.srcs != s.dsts) {