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_flush);
81 COVERAGE_DEFINE(bridge_process_flow);
82 COVERAGE_DEFINE(bridge_reconfigure);
90 struct dst builtin[32];
95 static void dst_set_init(struct dst_set *);
96 static void dst_set_add(struct dst_set *, const struct dst *);
97 static void dst_set_free(struct dst_set *);
100 /* These members are always valid. */
101 struct list port_elem; /* Element in struct port's "ifaces" list. */
102 struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
103 struct port *port; /* Containing port. */
104 char *name; /* Host network device name. */
105 tag_type tag; /* Tag associated with this interface. */
107 /* These members are valid only after bridge_reconfigure() causes them to
109 struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
110 int dp_ifidx; /* Index within kernel datapath. */
111 struct netdev *netdev; /* Network device. */
112 const char *type; /* Usually same as cfg->type. */
113 const struct ovsrec_interface *cfg;
116 #define MAX_MIRRORS 32
117 typedef uint32_t mirror_mask_t;
118 #define MIRROR_MASK_C(X) UINT32_C(X)
119 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
121 struct bridge *bridge;
124 struct uuid uuid; /* UUID of this "mirror" record in database. */
126 /* Selection criteria. */
127 struct sset src_ports; /* Source port names. */
128 struct sset dst_ports; /* Destination port names. */
133 struct port *out_port;
137 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
139 struct bridge *bridge;
140 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
143 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
144 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
145 * NULL if all VLANs are trunked. */
146 const struct ovsrec_port *cfg;
148 /* An ordinary bridge port has 1 interface.
149 * A bridge port for bonding has at least 2 interfaces. */
150 struct list ifaces; /* List of "struct iface"s. */
152 struct lacp *lacp; /* NULL if LACP is not enabled. */
157 /* Port mirroring info. */
158 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
159 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
160 bool is_mirror_output_port; /* Does port mirroring send frames here? */
164 struct hmap_node node; /* In 'all_bridges'. */
165 char *name; /* User-specified arbitrary name. */
166 struct mac_learning *ml; /* MAC learning table. */
167 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
168 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
169 const struct ovsrec_bridge *cfg;
171 /* OpenFlow switch processing. */
172 struct ofproto *ofproto; /* OpenFlow switch. */
175 struct hmap ports; /* "struct port"s indexed by name. */
176 struct hmap ifaces; /* "struct iface"s indexed by dp_ifidx. */
177 struct hmap iface_by_name; /* "struct iface"s indexed by name. */
180 bool has_bonded_ports;
185 /* Port mirroring. */
186 struct mirror *mirrors[MAX_MIRRORS];
188 /* Synthetic local port if necessary. */
189 struct ovsrec_port synth_local_port;
190 struct ovsrec_interface synth_local_iface;
191 struct ovsrec_interface *synth_local_ifacep;
194 /* All bridges, indexed by name. */
195 static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
197 /* OVSDB IDL used to obtain configuration. */
198 static struct ovsdb_idl *idl;
200 /* Each time this timer expires, the bridge fetches systems and interface
201 * statistics and pushes them into the database. */
202 #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
203 static long long int stats_timer = LLONG_MIN;
205 /* Stores the time after which rate limited statistics may be written to the
206 * database. Only updated when changes to the database require rate limiting.
208 #define DB_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
209 static long long int db_limiter = LLONG_MIN;
211 static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
212 static void bridge_destroy(struct bridge *);
213 static struct bridge *bridge_lookup(const char *name);
214 static unixctl_cb_func bridge_unixctl_dump_flows;
215 static unixctl_cb_func bridge_unixctl_reconnect;
216 static int bridge_run_one(struct bridge *);
217 static size_t bridge_get_controllers(const struct bridge *br,
218 struct ovsrec_controller ***controllersp);
219 static void bridge_reconfigure_one(struct bridge *);
220 static void bridge_configure_datapath_id(struct bridge *);
221 static void bridge_configure_netflow(struct bridge *);
222 static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
223 static void bridge_reconfigure_remotes(struct bridge *,
224 const struct sockaddr_in *managers,
226 static void bridge_flush(struct bridge *);
227 static void bridge_pick_local_hw_addr(struct bridge *,
228 uint8_t ea[ETH_ADDR_LEN],
229 struct iface **hw_addr_iface);
230 static uint64_t bridge_pick_datapath_id(struct bridge *,
231 const uint8_t bridge_ea[ETH_ADDR_LEN],
232 struct iface *hw_addr_iface);
233 static uint64_t dpid_from_hash(const void *, size_t nbytes);
234 static bool bridge_has_bond_fake_iface(const struct bridge *,
236 static bool port_is_bond_fake_iface(const struct port *);
238 static unixctl_cb_func bridge_unixctl_fdb_show;
239 static unixctl_cb_func cfm_unixctl_show;
240 static unixctl_cb_func qos_unixctl_show;
242 static void port_run(struct port *);
243 static void port_wait(struct port *);
244 static struct port *port_create(struct bridge *, const char *name);
245 static void port_reconfigure(struct port *, const struct ovsrec_port *);
246 static void port_del_ifaces(struct port *, const struct ovsrec_port *);
247 static void port_destroy(struct port *);
248 static struct port *port_lookup(const struct bridge *, const char *name);
249 static struct iface *port_get_an_iface(const struct port *);
250 static struct port *port_from_dp_ifidx(const struct bridge *,
252 static void port_reconfigure_lacp(struct port *);
253 static void port_reconfigure_bond(struct port *);
254 static void port_send_learning_packets(struct port *);
256 static void mirror_create(struct bridge *, struct ovsrec_mirror *);
257 static void mirror_destroy(struct mirror *);
258 static void mirror_reconfigure(struct bridge *);
259 static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
260 static bool vlan_is_mirrored(const struct mirror *, int vlan);
262 static struct iface *iface_create(struct port *port,
263 const struct ovsrec_interface *if_cfg);
264 static void iface_destroy(struct iface *);
265 static struct iface *iface_lookup(const struct bridge *, const char *name);
266 static struct iface *iface_find(const char *name);
267 static struct iface *iface_from_dp_ifidx(const struct bridge *,
269 static void iface_set_mac(struct iface *);
270 static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
271 static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
272 static void iface_update_cfm(struct iface *);
273 static bool iface_refresh_cfm_stats(struct iface *iface);
274 static bool iface_get_carrier(const struct iface *);
275 static bool iface_is_synthetic(const struct iface *);
277 static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
279 static void shash_to_ovs_idl_map(struct shash *,
280 char ***keys, char ***values, size_t *n);
282 /* Hooks into ofproto processing. */
283 static struct ofhooks bridge_ofhooks;
285 /* Public functions. */
287 /* Initializes the bridge module, configuring it to obtain its configuration
288 * from an OVSDB server accessed over 'remote', which should be a string in a
289 * form acceptable to ovsdb_idl_create(). */
291 bridge_init(const char *remote)
293 /* Create connection to database. */
294 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
296 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
297 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
298 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
299 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
300 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
301 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
302 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
304 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
305 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
307 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
308 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
310 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
311 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
312 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
313 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
314 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
315 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
316 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
317 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
318 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
320 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
321 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
322 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
323 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
325 ovsdb_idl_omit_alert(idl, &ovsrec_maintenance_point_col_fault);
327 ovsdb_idl_omit_alert(idl, &ovsrec_monitor_col_fault);
329 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
331 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
333 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
335 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
337 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
339 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
340 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
341 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
342 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
343 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
345 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
347 /* Register unixctl commands. */
348 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
349 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
350 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
351 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
353 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
362 struct bridge *br, *next_br;
364 HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
367 ovsdb_idl_destroy(idl);
370 /* Performs configuration that is only necessary once at ovs-vswitchd startup,
371 * but for which the ovs-vswitchd configuration 'cfg' is required. */
373 bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
375 static bool already_configured_once;
376 struct sset bridge_names;
377 struct sset dpif_names, dpif_types;
381 /* Only do this once per ovs-vswitchd run. */
382 if (already_configured_once) {
385 already_configured_once = true;
387 stats_timer = time_msec() + STATS_INTERVAL;
389 /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
390 sset_init(&bridge_names);
391 for (i = 0; i < cfg->n_bridges; i++) {
392 sset_add(&bridge_names, cfg->bridges[i]->name);
395 /* Iterate over all system dpifs and delete any of them that do not appear
397 sset_init(&dpif_names);
398 sset_init(&dpif_types);
399 dp_enumerate_types(&dpif_types);
400 SSET_FOR_EACH (type, &dpif_types) {
403 dp_enumerate_names(type, &dpif_names);
405 /* Delete each dpif whose name is not in 'bridge_names'. */
406 SSET_FOR_EACH (name, &dpif_names) {
407 if (!sset_contains(&bridge_names, name)) {
411 retval = dpif_open(name, type, &dpif);
419 sset_destroy(&bridge_names);
420 sset_destroy(&dpif_names);
421 sset_destroy(&dpif_types);
424 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
425 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
426 * responsible for freeing '*managersp' (with free()).
428 * You may be asking yourself "why does ovs-vswitchd care?", because
429 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
430 * should not be and in fact is not directly involved in that. But
431 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
432 * it has to tell in-band control where the managers are to enable that.
433 * (Thus, only managers connected in-band are collected.)
436 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
437 struct sockaddr_in **managersp, size_t *n_managersp)
439 struct sockaddr_in *managers = NULL;
440 size_t n_managers = 0;
444 /* Collect all of the potential targets from the "targets" columns of the
445 * rows pointed to by "manager_options", excluding any that are
448 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
449 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
451 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
452 sset_find_and_delete(&targets, m->target);
454 sset_add(&targets, m->target);
458 /* Now extract the targets' IP addresses. */
459 if (!sset_is_empty(&targets)) {
462 managers = xmalloc(sset_count(&targets) * sizeof *managers);
463 SSET_FOR_EACH (target, &targets) {
464 struct sockaddr_in *sin = &managers[n_managers];
466 if ((!strncmp(target, "tcp:", 4)
467 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
468 (!strncmp(target, "ssl:", 4)
469 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
474 sset_destroy(&targets);
476 *managersp = managers;
477 *n_managersp = n_managers;
481 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
483 struct shash_node *node;
484 struct bridge *br, *next;
485 struct sockaddr_in *managers;
489 int sflow_bridge_number;
491 COVERAGE_INC(bridge_reconfigure);
493 /* Collect old and new bridges. */
495 for (i = 0; i < ovs_cfg->n_bridges; i++) {
496 const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
497 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
498 VLOG_WARN("more than one bridge named %s", br_cfg->name);
502 /* Get rid of deleted bridges and add new bridges. */
503 HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
504 br->cfg = shash_find_data(&new_br, br->name);
509 SHASH_FOR_EACH (node, &new_br) {
510 const struct ovsrec_bridge *br_cfg = node->data;
511 struct bridge *br = bridge_lookup(node->name);
513 /* If the bridge datapath type has changed, we need to tear it
514 * down and recreate. */
515 if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
517 bridge_create(br_cfg);
520 bridge_create(br_cfg);
523 shash_destroy(&new_br);
525 /* Reconfigure all bridges. */
526 HMAP_FOR_EACH (br, node, &all_bridges) {
527 bridge_reconfigure_one(br);
530 /* Add and delete ports on all datapaths.
532 * The kernel will reject any attempt to add a given port to a datapath if
533 * that port already belongs to a different datapath, so we must do all
534 * port deletions before any port additions. */
535 HMAP_FOR_EACH (br, node, &all_bridges) {
536 struct ofproto_port_dump dump;
537 struct ofproto_port ofproto_port;
539 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
540 const char *name = ofproto_port.name;
545 /* Ignore the local port. We can't change it anyhow. */
546 if (!strcmp(name, br->name)) {
550 /* Get the type that 'ofproto_port' should have (ordinarily the
551 * type of its corresponding iface) or NULL if it should be
553 iface = iface_lookup(br, name);
554 type = (iface ? iface->type
555 : bridge_has_bond_fake_iface(br, name) ? "internal"
558 /* If it's the wrong type then delete the ofproto port. */
560 && !strcmp(ofproto_port.type, type)
561 && (!iface || !iface->netdev
562 || !strcmp(netdev_get_type(iface->netdev), type))) {
565 error = ofproto_port_del(br->ofproto, ofproto_port.ofp_port);
567 VLOG_WARN("bridge %s: failed to remove %s interface (%s)",
568 br->name, name, strerror(error));
571 if (iface->port->bond) {
572 /* The bond has a pointer to the netdev, so remove it from
573 * the bond before closing the netdev. The slave will get
574 * added back to the bond later, after a new netdev is
576 bond_slave_unregister(iface->port->bond, iface);
578 netdev_close(iface->netdev);
579 iface->netdev = NULL;
583 HMAP_FOR_EACH (br, node, &all_bridges) {
584 struct ofproto_port ofproto_port;
585 struct ofproto_port_dump dump;
586 struct port *port, *next_port;
588 /* Clear all the "dp_ifidx"es. */
589 hmap_clear(&br->ifaces);
590 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
593 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
594 iface->dp_ifidx = -1;
598 /* Obtain the correct "dp_ifidx"es from ofproto. */
599 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
600 struct iface *iface = iface_lookup(br, ofproto_port.name);
604 odp_port = ofp_port_to_odp_port(ofproto_port.ofp_port);
605 if (iface->dp_ifidx >= 0) {
606 VLOG_WARN("bridge %s: interface %s reported twice",
607 br->name, ofproto_port.name);
608 } else if (iface_from_dp_ifidx(br, odp_port)) {
609 VLOG_WARN("bridge %s: interface %"PRIu16" reported twice",
612 iface->dp_ifidx = odp_port;
613 hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
614 hash_int(iface->dp_ifidx, 0));
619 /* Add a dpif port for any "struct iface" that doesn't have one.
620 * Delete any "struct iface" for which this fails.
621 * Delete any "struct port" that thereby ends up with no ifaces. */
622 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
623 struct iface *iface, *next_iface;
625 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
629 /* Open the netdev or reconfigure it. */
631 shash_from_ovs_idl_map(iface->cfg->key_options,
632 iface->cfg->value_options,
633 iface->cfg->n_options, &args);
634 if (!iface->netdev) {
635 struct netdev_options options;
636 options.name = iface->name;
637 options.type = iface->type;
638 options.args = &args;
639 options.ethertype = NETDEV_ETH_TYPE_NONE;
640 error = netdev_open(&options, &iface->netdev);
642 error = netdev_set_config(iface->netdev, &args);
644 shash_destroy(&args);
646 VLOG_WARN("could not %s network device %s (%s)",
647 iface->netdev ? "reconfigure" : "open",
648 iface->name, strerror(error));
651 /* Add the port, if necessary. */
652 if (iface->netdev && iface->dp_ifidx < 0) {
656 error = ofproto_port_add(br->ofproto, iface->netdev,
659 iface->dp_ifidx = ofp_port_to_odp_port(ofp_port);
661 netdev_close(iface->netdev);
662 iface->netdev = NULL;
666 /* Delete the iface if */
667 if (iface->netdev && iface->dp_ifidx >= 0) {
668 VLOG_DBG("bridge %s: interface %s is on port %d",
669 br->name, iface->name, iface->dp_ifidx);
672 VLOG_ERR("bridge %s: missing %s interface, dropping",
673 br->name, iface->name);
675 /* We already reported a related error, don't bother
678 iface_set_ofport(iface->cfg, -1);
679 iface_destroy(iface);
682 if (list_is_empty(&port->ifaces)) {
683 VLOG_WARN("%s port has no interfaces, dropping", port->name);
688 /* Add bond fake iface if necessary. */
689 if (port_is_bond_fake_iface(port)) {
690 if (ofproto_port_query_by_name(br->ofproto, port->name,
692 struct netdev_options options;
693 struct netdev *netdev;
696 options.name = port->name;
697 options.type = "internal";
699 options.ethertype = NETDEV_ETH_TYPE_NONE;
700 error = netdev_open(&options, &netdev);
702 ofproto_port_add(br->ofproto, netdev, NULL);
703 netdev_close(netdev);
705 VLOG_WARN("could not open network device %s (%s)",
706 port->name, strerror(error));
709 /* Already exists, nothing to do. */
710 ofproto_port_destroy(&ofproto_port);
716 sflow_bridge_number = 0;
717 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
718 HMAP_FOR_EACH (br, node, &all_bridges) {
719 bridge_configure_datapath_id(br);
720 bridge_configure_netflow(br);
721 bridge_configure_sflow(br, &sflow_bridge_number);
723 /* Update the controller and related settings. It would be more
724 * straightforward to call this from bridge_reconfigure_one(), but we
725 * can't do it there for two reasons. First, and most importantly, at
726 * that point we don't know the dp_ifidx of any interfaces that have
727 * been added to the bridge (because we haven't actually added them to
728 * the datapath). Second, at that point we haven't set the datapath ID
729 * yet; when a controller is configured, resetting the datapath ID will
730 * immediately disconnect from the controller, so it's better to set
731 * the datapath ID before the controller. */
732 bridge_reconfigure_remotes(br, managers, n_managers);
736 HMAP_FOR_EACH (br, node, &all_bridges) {
739 br->has_bonded_ports = false;
740 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
743 port_reconfigure_lacp(port);
744 port_reconfigure_bond(port);
746 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
747 iface_update_qos(iface, port->cfg->qos);
748 netdev_set_policing(iface->netdev,
749 iface->cfg->ingress_policing_rate,
750 iface->cfg->ingress_policing_burst);
751 iface_set_mac(iface);
756 /* Some reconfiguration operations require the bridge to have been run at
758 HMAP_FOR_EACH (br, node, &all_bridges) {
763 HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
764 iface_update_cfm(iface);
768 /* ovs-vswitchd has completed initialization, so allow the process that
769 * forked us to exit successfully. */
770 daemonize_complete();
773 /* Pick local port hardware address and datapath ID for 'br'. */
775 bridge_configure_datapath_id(struct bridge *br)
777 uint8_t ea[ETH_ADDR_LEN];
779 struct iface *local_iface;
780 struct iface *hw_addr_iface;
783 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
784 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
786 int error = netdev_set_etheraddr(local_iface->netdev, ea);
788 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
789 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
790 "Ethernet address: %s",
791 br->name, strerror(error));
794 memcpy(br->ea, ea, ETH_ADDR_LEN);
796 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
797 ofproto_set_datapath_id(br->ofproto, dpid);
799 dpid_string = xasprintf("%016"PRIx64, dpid);
800 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
804 /* Set NetFlow configuration on 'br'. */
806 bridge_configure_netflow(struct bridge *br)
808 struct ovsrec_netflow *cfg = br->cfg->netflow;
809 struct netflow_options opts;
812 ofproto_set_netflow(br->ofproto, NULL);
816 memset(&opts, 0, sizeof opts);
818 /* Get default NetFlow configuration from datapath.
819 * Apply overrides from 'cfg'. */
820 ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
821 if (cfg->engine_type) {
822 opts.engine_type = *cfg->engine_type;
824 if (cfg->engine_id) {
825 opts.engine_id = *cfg->engine_id;
828 /* Configure active timeout interval. */
829 opts.active_timeout = cfg->active_timeout;
830 if (!opts.active_timeout) {
831 opts.active_timeout = -1;
832 } else if (opts.active_timeout < 0) {
833 VLOG_WARN("bridge %s: active timeout interval set to negative "
834 "value, using default instead (%d seconds)", br->name,
835 NF_ACTIVE_TIMEOUT_DEFAULT);
836 opts.active_timeout = -1;
839 /* Add engine ID to interface number to disambiguate bridgs? */
840 opts.add_id_to_iface = cfg->add_id_to_interface;
841 if (opts.add_id_to_iface) {
842 if (opts.engine_id > 0x7f) {
843 VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
844 "another vswitch, choose an engine id less than 128",
847 if (hmap_count(&br->ports) > 508) {
848 VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
849 "another port when more than 508 ports are used",
855 sset_init(&opts.collectors);
856 sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
859 if (ofproto_set_netflow(br->ofproto, &opts)) {
860 VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
862 sset_destroy(&opts.collectors);
865 /* Set sFlow configuration on 'br'. */
867 bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
869 const struct ovsrec_sflow *cfg = br->cfg->sflow;
870 struct ovsrec_controller **controllers;
871 struct ofproto_sflow_options oso;
872 size_t n_controllers;
876 ofproto_set_sflow(br->ofproto, NULL);
880 memset(&oso, 0, sizeof oso);
882 sset_init(&oso.targets);
883 sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
885 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
887 oso.sampling_rate = *cfg->sampling;
890 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
892 oso.polling_interval = *cfg->polling;
895 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
897 oso.header_len = *cfg->header;
900 oso.sub_id = (*sflow_bridge_number)++;
901 oso.agent_device = cfg->agent;
903 oso.control_ip = NULL;
904 n_controllers = bridge_get_controllers(br, &controllers);
905 for (i = 0; i < n_controllers; i++) {
906 if (controllers[i]->local_ip) {
907 oso.control_ip = controllers[i]->local_ip;
911 ofproto_set_sflow(br->ofproto, &oso);
913 sset_destroy(&oso.targets);
917 bridge_has_bond_fake_iface(const struct bridge *br, const char *name)
919 const struct port *port = port_lookup(br, name);
920 return port && port_is_bond_fake_iface(port);
924 port_is_bond_fake_iface(const struct port *port)
926 return port->cfg->bond_fake_iface && !list_is_short(&port->ifaces);
930 get_ovsrec_key_value(const struct ovsdb_idl_row *row,
931 const struct ovsdb_idl_column *column,
934 const struct ovsdb_datum *datum;
935 union ovsdb_atom atom;
938 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
939 atom.string = (char *) key;
940 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
941 return idx == UINT_MAX ? NULL : datum->values[idx].string;
945 bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
947 return get_ovsrec_key_value(&br_cfg->header_,
948 &ovsrec_bridge_col_other_config, key);
952 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
953 struct iface **hw_addr_iface)
959 *hw_addr_iface = NULL;
961 /* Did the user request a particular MAC? */
962 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
963 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
964 if (eth_addr_is_multicast(ea)) {
965 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
966 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
967 } else if (eth_addr_is_zero(ea)) {
968 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
974 /* Otherwise choose the minimum non-local MAC address among all of the
976 memset(ea, 0xff, ETH_ADDR_LEN);
977 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
978 uint8_t iface_ea[ETH_ADDR_LEN];
979 struct iface *candidate;
982 /* Mirror output ports don't participate. */
983 if (port->is_mirror_output_port) {
987 /* Choose the MAC address to represent the port. */
989 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
990 /* Find the interface with this Ethernet address (if any) so that
991 * we can provide the correct devname to the caller. */
992 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
993 uint8_t candidate_ea[ETH_ADDR_LEN];
994 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
995 && eth_addr_equals(iface_ea, candidate_ea)) {
1000 /* Choose the interface whose MAC address will represent the port.
1001 * The Linux kernel bonding code always chooses the MAC address of
1002 * the first slave added to a bond, and the Fedora networking
1003 * scripts always add slaves to a bond in alphabetical order, so
1004 * for compatibility we choose the interface with the name that is
1005 * first in alphabetical order. */
1006 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
1007 if (!iface || strcmp(candidate->name, iface->name) < 0) {
1012 /* The local port doesn't count (since we're trying to choose its
1013 * MAC address anyway). */
1014 if (iface->dp_ifidx == ODPP_LOCAL) {
1019 error = netdev_get_etheraddr(iface->netdev, iface_ea);
1021 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1022 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
1023 iface->name, strerror(error));
1028 /* Compare against our current choice. */
1029 if (!eth_addr_is_multicast(iface_ea) &&
1030 !eth_addr_is_local(iface_ea) &&
1031 !eth_addr_is_reserved(iface_ea) &&
1032 !eth_addr_is_zero(iface_ea) &&
1033 eth_addr_compare_3way(iface_ea, ea) < 0)
1035 memcpy(ea, iface_ea, ETH_ADDR_LEN);
1036 *hw_addr_iface = iface;
1039 if (eth_addr_is_multicast(ea)) {
1040 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
1041 *hw_addr_iface = NULL;
1042 VLOG_WARN("bridge %s: using default bridge Ethernet "
1043 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1045 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1046 br->name, ETH_ADDR_ARGS(ea));
1050 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
1051 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
1052 * an interface on 'br', then that interface must be passed in as
1053 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1054 * 'hw_addr_iface' must be passed in as a null pointer. */
1056 bridge_pick_datapath_id(struct bridge *br,
1057 const uint8_t bridge_ea[ETH_ADDR_LEN],
1058 struct iface *hw_addr_iface)
1061 * The procedure for choosing a bridge MAC address will, in the most
1062 * ordinary case, also choose a unique MAC that we can use as a datapath
1063 * ID. In some special cases, though, multiple bridges will end up with
1064 * the same MAC address. This is OK for the bridges, but it will confuse
1065 * the OpenFlow controller, because each datapath needs a unique datapath
1068 * Datapath IDs must be unique. It is also very desirable that they be
1069 * stable from one run to the next, so that policy set on a datapath
1072 const char *datapath_id;
1075 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1076 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
1080 if (hw_addr_iface) {
1082 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
1084 * A bridge whose MAC address is taken from a VLAN network device
1085 * (that is, a network device created with vconfig(8) or similar
1086 * tool) will have the same MAC address as a bridge on the VLAN
1087 * device's physical network device.
1089 * Handle this case by hashing the physical network device MAC
1090 * along with the VLAN identifier.
1092 uint8_t buf[ETH_ADDR_LEN + 2];
1093 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1094 buf[ETH_ADDR_LEN] = vlan >> 8;
1095 buf[ETH_ADDR_LEN + 1] = vlan;
1096 return dpid_from_hash(buf, sizeof buf);
1099 * Assume that this bridge's MAC address is unique, since it
1100 * doesn't fit any of the cases we handle specially.
1105 * A purely internal bridge, that is, one that has no non-virtual
1106 * network devices on it at all, is more difficult because it has no
1107 * natural unique identifier at all.
1109 * When the host is a XenServer, we handle this case by hashing the
1110 * host's UUID with the name of the bridge. Names of bridges are
1111 * persistent across XenServer reboots, although they can be reused if
1112 * an internal network is destroyed and then a new one is later
1113 * created, so this is fairly effective.
1115 * When the host is not a XenServer, we punt by using a random MAC
1116 * address on each run.
1118 const char *host_uuid = xenserver_get_host_uuid();
1120 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1121 dpid = dpid_from_hash(combined, strlen(combined));
1127 return eth_addr_to_uint64(bridge_ea);
1131 dpid_from_hash(const void *data, size_t n)
1133 uint8_t hash[SHA1_DIGEST_SIZE];
1135 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
1136 sha1_bytes(data, n, hash);
1137 eth_addr_mark_random(hash);
1138 return eth_addr_to_uint64(hash);
1142 iface_refresh_status(struct iface *iface)
1146 enum netdev_flags flags;
1153 if (iface_is_synthetic(iface)) {
1159 if (!netdev_get_status(iface->netdev, &sh)) {
1161 char **keys, **values;
1163 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1164 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1169 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1172 shash_destroy_free_data(&sh);
1174 error = netdev_get_flags(iface->netdev, &flags);
1176 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1179 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1182 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1184 ovsrec_interface_set_duplex(iface->cfg,
1185 netdev_features_is_full_duplex(current)
1187 /* warning: uint64_t -> int64_t conversion */
1188 bps = netdev_features_to_bps(current);
1189 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1192 ovsrec_interface_set_duplex(iface->cfg, NULL);
1193 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1197 ovsrec_interface_set_link_state(iface->cfg,
1198 iface_get_carrier(iface) ? "up" : "down");
1200 error = netdev_get_mtu(iface->netdev, &mtu);
1201 if (!error && mtu != INT_MAX) {
1203 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1206 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1210 /* Writes 'iface''s CFM statistics to the database. Returns true if anything
1211 * changed, false otherwise. */
1213 iface_refresh_cfm_stats(struct iface *iface)
1215 const struct ovsrec_monitor *mon;
1216 const struct cfm *cfm;
1217 bool changed = false;
1220 mon = iface->cfg->monitor;
1221 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1227 for (i = 0; i < mon->n_remote_mps; i++) {
1228 const struct ovsrec_maintenance_point *mp;
1229 const struct remote_mp *rmp;
1231 mp = mon->remote_mps[i];
1232 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1234 if (mp->n_fault != 1 || mp->fault[0] != rmp->fault) {
1235 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1240 if (mon->n_fault != 1 || mon->fault[0] != cfm->fault) {
1241 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1249 iface_refresh_lacp_stats(struct iface *iface)
1251 bool *db_current = iface->cfg->lacp_current;
1252 bool changed = false;
1254 if (iface->port->lacp) {
1255 bool current = lacp_slave_is_current(iface->port->lacp, iface);
1257 if (!db_current || *db_current != current) {
1259 ovsrec_interface_set_lacp_current(iface->cfg, ¤t, 1);
1261 } else if (db_current) {
1263 ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
1270 iface_refresh_stats(struct iface *iface)
1276 static const struct iface_stat iface_stats[] = {
1277 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1278 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1279 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1280 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1281 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1282 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1283 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1284 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1285 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1286 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1287 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1288 { "collisions", offsetof(struct netdev_stats, collisions) },
1290 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1291 const struct iface_stat *s;
1293 char *keys[N_STATS];
1294 int64_t values[N_STATS];
1297 struct netdev_stats stats;
1299 if (iface_is_synthetic(iface)) {
1303 /* Intentionally ignore return value, since errors will set 'stats' to
1304 * all-1s, and we will deal with that correctly below. */
1305 netdev_get_stats(iface->netdev, &stats);
1308 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1309 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1310 if (value != UINT64_MAX) {
1317 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1321 refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1323 struct ovsdb_datum datum;
1327 get_system_stats(&stats);
1329 ovsdb_datum_from_shash(&datum, &stats);
1330 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1334 static inline const char *
1335 nx_role_to_str(enum nx_role role)
1340 case NX_ROLE_MASTER:
1345 return "*** INVALID ROLE ***";
1350 bridge_refresh_controller_status(const struct bridge *br)
1353 const struct ovsrec_controller *cfg;
1355 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1357 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
1358 struct ofproto_controller_info *cinfo =
1359 shash_find_data(&info, cfg->target);
1362 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1363 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1364 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1365 (char **) cinfo->pairs.values,
1368 ovsrec_controller_set_is_connected(cfg, false);
1369 ovsrec_controller_set_role(cfg, NULL);
1370 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1374 ofproto_free_ofproto_controller_info(&info);
1380 const struct ovsrec_open_vswitch *cfg;
1382 bool datapath_destroyed;
1383 bool database_changed;
1386 /* Let each bridge do the work that it needs to do. */
1387 datapath_destroyed = false;
1388 HMAP_FOR_EACH (br, node, &all_bridges) {
1389 int error = bridge_run_one(br);
1391 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1392 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1393 "forcing reconfiguration", br->name);
1394 datapath_destroyed = true;
1398 /* (Re)configure if necessary. */
1399 database_changed = ovsdb_idl_run(idl);
1400 cfg = ovsrec_open_vswitch_first(idl);
1402 /* Re-configure SSL. We do this on every trip through the main loop,
1403 * instead of just when the database changes, because the contents of the
1404 * key and certificate files can change without the database changing.
1406 * We do this before bridge_reconfigure() because that function might
1407 * initiate SSL connections and thus requires SSL to be configured. */
1408 if (cfg && cfg->ssl) {
1409 const struct ovsrec_ssl *ssl = cfg->ssl;
1411 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1412 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1415 if (database_changed || datapath_destroyed) {
1417 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1419 bridge_configure_once(cfg);
1420 bridge_reconfigure(cfg);
1422 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1423 ovsdb_idl_txn_commit(txn);
1424 ovsdb_idl_txn_destroy(txn); /* XXX */
1426 /* We still need to reconfigure to avoid dangling pointers to
1427 * now-destroyed ovsrec structures inside bridge data. */
1428 static const struct ovsrec_open_vswitch null_cfg;
1430 bridge_reconfigure(&null_cfg);
1434 /* Refresh system and interface stats if necessary. */
1435 if (time_msec() >= stats_timer) {
1437 struct ovsdb_idl_txn *txn;
1439 txn = ovsdb_idl_txn_create(idl);
1440 HMAP_FOR_EACH (br, node, &all_bridges) {
1443 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1444 struct iface *iface;
1446 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1447 iface_refresh_stats(iface);
1448 iface_refresh_status(iface);
1451 bridge_refresh_controller_status(br);
1453 refresh_system_stats(cfg);
1454 ovsdb_idl_txn_commit(txn);
1455 ovsdb_idl_txn_destroy(txn); /* XXX */
1458 stats_timer = time_msec() + STATS_INTERVAL;
1461 if (time_msec() >= db_limiter) {
1462 struct ovsdb_idl_txn *txn;
1463 bool changed = false;
1465 txn = ovsdb_idl_txn_create(idl);
1466 HMAP_FOR_EACH (br, node, &all_bridges) {
1469 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1470 struct iface *iface;
1472 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1473 changed = iface_refresh_cfm_stats(iface) || changed;
1474 changed = iface_refresh_lacp_stats(iface) || changed;
1480 db_limiter = time_msec() + DB_LIMIT_INTERVAL;
1483 ovsdb_idl_txn_commit(txn);
1484 ovsdb_idl_txn_destroy(txn);
1493 HMAP_FOR_EACH (br, node, &all_bridges) {
1496 ofproto_wait(br->ofproto);
1497 mac_learning_wait(br->ml);
1498 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1502 ovsdb_idl_wait(idl);
1503 poll_timer_wait_until(stats_timer);
1505 if (db_limiter > time_msec()) {
1506 poll_timer_wait_until(db_limiter);
1510 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1511 * configuration changes. */
1513 bridge_flush(struct bridge *br)
1515 COVERAGE_INC(bridge_flush);
1519 /* Bridge unixctl user interface functions. */
1521 bridge_unixctl_fdb_show(struct unixctl_conn *conn,
1522 const char *args, void *aux OVS_UNUSED)
1524 struct ds ds = DS_EMPTY_INITIALIZER;
1525 const struct bridge *br;
1526 const struct mac_entry *e;
1528 br = bridge_lookup(args);
1530 unixctl_command_reply(conn, 501, "no such bridge");
1534 ds_put_cstr(&ds, " port VLAN MAC Age\n");
1535 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
1536 struct port *port = e->port.p;
1537 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1538 port_get_an_iface(port)->dp_ifidx,
1539 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
1541 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1545 /* CFM unixctl user interface functions. */
1547 cfm_unixctl_show(struct unixctl_conn *conn,
1548 const char *args, void *aux OVS_UNUSED)
1550 struct ds ds = DS_EMPTY_INITIALIZER;
1551 struct iface *iface;
1552 const struct cfm *cfm;
1554 iface = iface_find(args);
1556 unixctl_command_reply(conn, 501, "no such interface");
1560 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1563 unixctl_command_reply(conn, 501, "CFM not enabled");
1567 cfm_dump_ds(cfm, &ds);
1568 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1572 /* QoS unixctl user interface functions. */
1574 struct qos_unixctl_show_cbdata {
1576 struct iface *iface;
1580 qos_unixctl_show_cb(unsigned int queue_id,
1581 const struct shash *details,
1584 struct qos_unixctl_show_cbdata *data = aux;
1585 struct ds *ds = data->ds;
1586 struct iface *iface = data->iface;
1587 struct netdev_queue_stats stats;
1588 struct shash_node *node;
1591 ds_put_cstr(ds, "\n");
1593 ds_put_format(ds, "Queue %u:\n", queue_id);
1595 ds_put_cstr(ds, "Default:\n");
1598 SHASH_FOR_EACH (node, details) {
1599 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1602 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1604 if (stats.tx_packets != UINT64_MAX) {
1605 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1608 if (stats.tx_bytes != UINT64_MAX) {
1609 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1612 if (stats.tx_errors != UINT64_MAX) {
1613 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1616 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1617 queue_id, strerror(error));
1622 qos_unixctl_show(struct unixctl_conn *conn,
1623 const char *args, void *aux OVS_UNUSED)
1625 struct ds ds = DS_EMPTY_INITIALIZER;
1626 struct shash sh = SHASH_INITIALIZER(&sh);
1627 struct iface *iface;
1629 struct shash_node *node;
1630 struct qos_unixctl_show_cbdata data;
1633 iface = iface_find(args);
1635 unixctl_command_reply(conn, 501, "no such interface");
1639 netdev_get_qos(iface->netdev, &type, &sh);
1641 if (*type != '\0') {
1642 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1644 SHASH_FOR_EACH (node, &sh) {
1645 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1650 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1653 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1655 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1657 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1658 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1661 shash_destroy_free_data(&sh);
1665 /* Bridge reconfiguration functions. */
1666 static struct bridge *
1667 bridge_create(const struct ovsrec_bridge *br_cfg)
1672 assert(!bridge_lookup(br_cfg->name));
1673 br = xzalloc(sizeof *br);
1675 error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
1678 VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
1684 br->name = xstrdup(br_cfg->name);
1686 br->ml = mac_learning_create();
1687 eth_addr_nicira_random(br->default_ea);
1689 hmap_init(&br->ports);
1690 hmap_init(&br->ifaces);
1691 hmap_init(&br->iface_by_name);
1695 hmap_insert(&all_bridges, &br->node, hash_string(br->name, 0));
1697 VLOG_INFO("bridge %s: created", br->name);
1703 bridge_destroy(struct bridge *br)
1706 struct port *port, *next;
1709 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1712 for (i = 0; i < MAX_MIRRORS; i++) {
1713 mirror_destroy(br->mirrors[i]);
1715 hmap_remove(&all_bridges, &br->node);
1716 ofproto_destroy_and_delete(br->ofproto);
1717 mac_learning_destroy(br->ml);
1718 hmap_destroy(&br->ifaces);
1719 hmap_destroy(&br->ports);
1720 hmap_destroy(&br->iface_by_name);
1721 free(br->synth_local_iface.type);
1727 static struct bridge *
1728 bridge_lookup(const char *name)
1732 HMAP_FOR_EACH_WITH_HASH (br, node, hash_string(name, 0), &all_bridges) {
1733 if (!strcmp(br->name, name)) {
1740 /* Handle requests for a listing of all flows known by the OpenFlow
1741 * stack, including those normally hidden. */
1743 bridge_unixctl_dump_flows(struct unixctl_conn *conn,
1744 const char *args, void *aux OVS_UNUSED)
1749 br = bridge_lookup(args);
1751 unixctl_command_reply(conn, 501, "Unknown bridge");
1756 ofproto_get_all_flows(br->ofproto, &results);
1758 unixctl_command_reply(conn, 200, ds_cstr(&results));
1759 ds_destroy(&results);
1762 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1763 * connections and reconnect. If BRIDGE is not specified, then all bridges
1764 * drop their controller connections and reconnect. */
1766 bridge_unixctl_reconnect(struct unixctl_conn *conn,
1767 const char *args, void *aux OVS_UNUSED)
1770 if (args[0] != '\0') {
1771 br = bridge_lookup(args);
1773 unixctl_command_reply(conn, 501, "Unknown bridge");
1776 ofproto_reconnect_controllers(br->ofproto);
1778 HMAP_FOR_EACH (br, node, &all_bridges) {
1779 ofproto_reconnect_controllers(br->ofproto);
1782 unixctl_command_reply(conn, 200, NULL);
1786 bridge_run_one(struct bridge *br)
1791 error = ofproto_run1(br->ofproto);
1796 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
1798 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1802 error = ofproto_run2(br->ofproto, br->flush);
1809 bridge_get_controllers(const struct bridge *br,
1810 struct ovsrec_controller ***controllersp)
1812 struct ovsrec_controller **controllers;
1813 size_t n_controllers;
1815 controllers = br->cfg->controller;
1816 n_controllers = br->cfg->n_controller;
1818 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1824 *controllersp = controllers;
1826 return n_controllers;
1830 bridge_reconfigure_one(struct bridge *br)
1832 enum ofproto_fail_mode fail_mode;
1833 struct port *port, *next;
1834 struct shash_node *node;
1835 struct shash new_ports;
1838 /* Collect new ports. */
1839 shash_init(&new_ports);
1840 for (i = 0; i < br->cfg->n_ports; i++) {
1841 const char *name = br->cfg->ports[i]->name;
1842 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1843 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1847 if (bridge_get_controllers(br, NULL)
1848 && !shash_find(&new_ports, br->name)) {
1849 struct ofproto_port local_port;
1853 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
1854 br->name, br->name);
1856 error = ofproto_port_query_by_name(br->ofproto, br->name, &local_port);
1857 type = xstrdup(error ? "internal" : local_port.type);
1858 ofproto_port_destroy(&local_port);
1860 br->synth_local_port.interfaces = &br->synth_local_ifacep;
1861 br->synth_local_port.n_interfaces = 1;
1862 br->synth_local_port.name = br->name;
1864 br->synth_local_iface.name = br->name;
1865 free(br->synth_local_iface.type);
1866 br->synth_local_iface.type = type;
1868 br->synth_local_ifacep = &br->synth_local_iface;
1870 shash_add(&new_ports, br->name, &br->synth_local_port);
1873 /* Get rid of deleted ports.
1874 * Get rid of deleted interfaces on ports that still exist. */
1875 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1876 const struct ovsrec_port *port_cfg;
1878 port_cfg = shash_find_data(&new_ports, port->name);
1882 port_del_ifaces(port, port_cfg);
1886 /* Create new ports.
1887 * Add new interfaces to existing ports.
1888 * Reconfigure existing ports. */
1889 SHASH_FOR_EACH (node, &new_ports) {
1890 struct port *port = port_lookup(br, node->name);
1892 port = port_create(br, node->name);
1895 port_reconfigure(port, node->data);
1896 if (list_is_empty(&port->ifaces)) {
1897 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1898 br->name, port->name);
1902 shash_destroy(&new_ports);
1904 /* Set the fail-mode */
1905 fail_mode = !br->cfg->fail_mode
1906 || !strcmp(br->cfg->fail_mode, "standalone")
1907 ? OFPROTO_FAIL_STANDALONE
1908 : OFPROTO_FAIL_SECURE;
1909 ofproto_set_fail_mode(br->ofproto, fail_mode);
1911 /* Configure OpenFlow controller connection snooping. */
1912 if (!ofproto_has_snoops(br->ofproto)) {
1916 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
1917 ovs_rundir(), br->name));
1918 ofproto_set_snoops(br->ofproto, &snoops);
1919 sset_destroy(&snoops);
1922 mirror_reconfigure(br);
1925 /* Initializes 'oc' appropriately as a management service controller for
1928 * The caller must free oc->target when it is no longer needed. */
1930 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1931 struct ofproto_controller *oc)
1933 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
1934 oc->max_backoff = 0;
1935 oc->probe_interval = 60;
1936 oc->band = OFPROTO_OUT_OF_BAND;
1938 oc->burst_limit = 0;
1941 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1943 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1944 struct ofproto_controller *oc)
1946 oc->target = c->target;
1947 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1948 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1949 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1950 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
1951 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1952 oc->burst_limit = (c->controller_burst_limit
1953 ? *c->controller_burst_limit : 0);
1956 /* Configures the IP stack for 'br''s local interface properly according to the
1957 * configuration in 'c'. */
1959 bridge_configure_local_iface_netdev(struct bridge *br,
1960 struct ovsrec_controller *c)
1962 struct netdev *netdev;
1963 struct in_addr mask, gateway;
1965 struct iface *local_iface;
1968 /* If there's no local interface or no IP address, give up. */
1969 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
1970 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1974 /* Bring up the local interface. */
1975 netdev = local_iface->netdev;
1976 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1978 /* Configure the IP address and netmask. */
1979 if (!c->local_netmask
1980 || !inet_aton(c->local_netmask, &mask)
1982 mask.s_addr = guess_netmask(ip.s_addr);
1984 if (!netdev_set_in4(netdev, ip, mask)) {
1985 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1986 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1989 /* Configure the default gateway. */
1990 if (c->local_gateway
1991 && inet_aton(c->local_gateway, &gateway)
1992 && gateway.s_addr) {
1993 if (!netdev_add_router(netdev, gateway)) {
1994 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1995 br->name, IP_ARGS(&gateway.s_addr));
2001 bridge_reconfigure_remotes(struct bridge *br,
2002 const struct sockaddr_in *managers,
2005 const char *disable_ib_str, *queue_id_str;
2006 bool disable_in_band = false;
2009 struct ovsrec_controller **controllers;
2010 size_t n_controllers;
2012 struct ofproto_controller *ocs;
2016 /* Check if we should disable in-band control on this bridge. */
2017 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
2018 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
2019 disable_in_band = true;
2022 /* Set OpenFlow queue ID for in-band control. */
2023 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
2024 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
2025 ofproto_set_in_band_queue(br->ofproto, queue_id);
2027 if (disable_in_band) {
2028 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
2030 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
2033 n_controllers = bridge_get_controllers(br, &controllers);
2035 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
2038 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
2039 for (i = 0; i < n_controllers; i++) {
2040 struct ovsrec_controller *c = controllers[i];
2042 if (!strncmp(c->target, "punix:", 6)
2043 || !strncmp(c->target, "unix:", 5)) {
2044 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2046 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
2047 * domain sockets and overwriting arbitrary local files. */
2048 VLOG_ERR_RL(&rl, "bridge %s: not adding Unix domain socket "
2049 "controller \"%s\" due to possibility for remote "
2050 "exploit", br->name, c->target);
2054 bridge_configure_local_iface_netdev(br, c);
2055 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
2056 if (disable_in_band) {
2057 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
2062 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
2063 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
2067 /* Bridge packet processing functions. */
2070 set_dst(struct dst *dst, const struct flow *flow,
2071 const struct port *in_port, const struct port *out_port,
2074 dst->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
2075 : in_port->vlan >= 0 ? in_port->vlan
2076 : flow->vlan_tci == 0 ? OFP_VLAN_NONE
2077 : vlan_tci_to_vid(flow->vlan_tci));
2079 dst->iface = (!out_port->bond
2080 ? port_get_an_iface(out_port)
2081 : bond_choose_output_slave(out_port->bond, flow,
2084 return dst->iface != NULL;
2088 mirror_mask_ffs(mirror_mask_t mask)
2090 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2095 dst_set_init(struct dst_set *set)
2097 set->dsts = set->builtin;
2099 set->allocated = ARRAY_SIZE(set->builtin);
2103 dst_set_add(struct dst_set *set, const struct dst *dst)
2105 if (set->n >= set->allocated) {
2106 size_t new_allocated;
2107 struct dst *new_dsts;
2109 new_allocated = set->allocated * 2;
2110 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2111 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2115 set->dsts = new_dsts;
2116 set->allocated = new_allocated;
2118 set->dsts[set->n++] = *dst;
2122 dst_set_free(struct dst_set *set)
2124 if (set->dsts != set->builtin) {
2130 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
2133 for (i = 0; i < set->n; i++) {
2134 if (set->dsts[i].vlan == test->vlan
2135 && set->dsts[i].iface == test->iface) {
2143 port_trunks_vlan(const struct port *port, uint16_t vlan)
2145 return (port->vlan < 0 || vlan_bitmap_contains(port->trunks, vlan));
2149 port_includes_vlan(const struct port *port, uint16_t vlan)
2151 return vlan == port->vlan || port_trunks_vlan(port, vlan);
2155 port_is_floodable(const struct port *port)
2157 struct iface *iface;
2159 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2160 if (!ofproto_port_is_floodable(port->bridge->ofproto,
2168 /* Returns an arbitrary interface within 'port'. */
2169 static struct iface *
2170 port_get_an_iface(const struct port *port)
2172 return CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
2176 compose_dsts(const struct bridge *br, const struct flow *flow, uint16_t vlan,
2177 const struct port *in_port, const struct port *out_port,
2178 struct dst_set *set, tag_type *tags, uint16_t *nf_output_iface)
2182 if (out_port == FLOOD_PORT) {
2185 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2187 && port_is_floodable(port)
2188 && port_includes_vlan(port, vlan)
2189 && !port->is_mirror_output_port
2190 && set_dst(&dst, flow, in_port, port, tags)) {
2191 dst_set_add(set, &dst);
2194 *nf_output_iface = NF_OUT_FLOOD;
2195 } else if (out_port && set_dst(&dst, flow, in_port, out_port, tags)) {
2196 dst_set_add(set, &dst);
2197 *nf_output_iface = dst.iface->dp_ifidx;
2202 compose_mirror_dsts(const struct bridge *br, const struct flow *flow,
2203 uint16_t vlan, const struct port *in_port,
2204 struct dst_set *set, tag_type *tags)
2206 mirror_mask_t mirrors;
2210 mirrors = in_port->src_mirrors;
2211 for (i = 0; i < set->n; i++) {
2212 mirrors |= set->dsts[i].iface->port->dst_mirrors;
2219 flow_vlan = vlan_tci_to_vid(flow->vlan_tci);
2220 if (flow_vlan == 0) {
2221 flow_vlan = OFP_VLAN_NONE;
2225 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
2226 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
2230 if (set_dst(&dst, flow, in_port, m->out_port, tags)
2231 && !dst_is_duplicate(set, &dst)) {
2232 dst_set_add(set, &dst);
2237 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2238 if (port_includes_vlan(port, m->out_vlan)
2239 && set_dst(&dst, flow, in_port, port, tags))
2241 if (port->vlan < 0) {
2242 dst.vlan = m->out_vlan;
2244 if (dst_is_duplicate(set, &dst)) {
2248 /* Use the vlan tag on the original flow instead of
2249 * the one passed in the vlan parameter. This ensures
2250 * that we compare the vlan from before any implicit
2251 * tagging tags place. This is necessary because
2252 * dst->vlan is the final vlan, after removing implicit
2254 if (port == in_port && dst.vlan == flow_vlan) {
2255 /* Don't send out input port on same VLAN. */
2258 dst_set_add(set, &dst);
2263 mirrors &= mirrors - 1;
2268 compose_actions(struct bridge *br, const struct flow *flow, uint16_t vlan,
2269 const struct port *in_port, const struct port *out_port,
2270 tag_type *tags, struct ofpbuf *actions,
2271 uint16_t *nf_output_iface)
2273 uint16_t initial_vlan, cur_vlan;
2274 const struct dst *dst;
2278 compose_dsts(br, flow, vlan, in_port, out_port, &set, tags,
2280 compose_mirror_dsts(br, flow, vlan, in_port, &set, tags);
2282 /* Output all the packets we can without having to change the VLAN. */
2283 initial_vlan = vlan_tci_to_vid(flow->vlan_tci);
2284 if (initial_vlan == 0) {
2285 initial_vlan = OFP_VLAN_NONE;
2287 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2288 if (dst->vlan != initial_vlan) {
2291 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
2294 /* Then output the rest. */
2295 cur_vlan = initial_vlan;
2296 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2297 if (dst->vlan == initial_vlan) {
2300 if (dst->vlan != cur_vlan) {
2301 if (dst->vlan == OFP_VLAN_NONE) {
2302 nl_msg_put_flag(actions, ODP_ACTION_ATTR_STRIP_VLAN);
2305 tci = htons(dst->vlan & VLAN_VID_MASK);
2306 tci |= flow->vlan_tci & htons(VLAN_PCP_MASK);
2307 nl_msg_put_be16(actions, ODP_ACTION_ATTR_SET_DL_TCI, tci);
2309 cur_vlan = dst->vlan;
2311 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
2317 /* Returns the effective vlan of a packet, taking into account both the
2318 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2319 * the packet is untagged and -1 indicates it has an invalid header and
2320 * should be dropped. */
2321 static int flow_get_vlan(struct bridge *br, const struct flow *flow,
2322 struct port *in_port, bool have_packet)
2324 int vlan = vlan_tci_to_vid(flow->vlan_tci);
2325 if (in_port->vlan >= 0) {
2328 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2329 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2330 "packet received on port %s configured with "
2331 "implicit VLAN %"PRIu16,
2332 br->name, vlan, in_port->name, in_port->vlan);
2336 vlan = in_port->vlan;
2338 if (!port_includes_vlan(in_port, vlan)) {
2340 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2341 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2342 "packet received on port %s not configured for "
2344 br->name, vlan, in_port->name, vlan);
2353 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2354 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2355 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2357 is_gratuitous_arp(const struct flow *flow)
2359 return (flow->dl_type == htons(ETH_TYPE_ARP)
2360 && eth_addr_is_broadcast(flow->dl_dst)
2361 && (flow->nw_proto == ARP_OP_REPLY
2362 || (flow->nw_proto == ARP_OP_REQUEST
2363 && flow->nw_src == flow->nw_dst)));
2367 update_learning_table(struct bridge *br, const struct flow *flow, int vlan,
2368 struct port *in_port)
2370 struct mac_entry *mac;
2372 if (!mac_learning_may_learn(br->ml, flow->dl_src, vlan)) {
2376 mac = mac_learning_insert(br->ml, flow->dl_src, vlan);
2377 if (is_gratuitous_arp(flow)) {
2378 /* We don't want to learn from gratuitous ARP packets that are
2379 * reflected back over bond slaves so we lock the learning table. */
2380 if (!in_port->bond) {
2381 mac_entry_set_grat_arp_lock(mac);
2382 } else if (mac_entry_is_grat_arp_locked(mac)) {
2387 if (mac_entry_is_new(mac) || mac->port.p != in_port) {
2388 /* The log messages here could actually be useful in debugging,
2389 * so keep the rate limit relatively high. */
2390 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2391 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2392 "on port %s in VLAN %d",
2393 br->name, ETH_ADDR_ARGS(flow->dl_src),
2394 in_port->name, vlan);
2396 mac->port.p = in_port;
2397 ofproto_revalidate(br->ofproto, mac_learning_changed(br->ml, mac));
2401 /* Determines whether packets in 'flow' within 'br' should be forwarded or
2402 * dropped. Returns true if they may be forwarded, false if they should be
2405 * If 'have_packet' is true, it indicates that the caller is processing a
2406 * received packet. If 'have_packet' is false, then the caller is just
2407 * revalidating an existing flow because configuration has changed. Either
2408 * way, 'have_packet' only affects logging (there is no point in logging errors
2409 * during revalidation).
2411 * Sets '*in_portp' to the input port. This will be a null pointer if
2412 * flow->in_port does not designate a known input port (in which case
2413 * is_admissible() returns false).
2415 * When returning true, sets '*vlanp' to the effective VLAN of the input
2416 * packet, as returned by flow_get_vlan().
2418 * May also add tags to '*tags', although the current implementation only does
2419 * so in one special case.
2422 is_admissible(struct bridge *br, const struct flow *flow, bool have_packet,
2423 tag_type *tags, int *vlanp, struct port **in_portp)
2425 struct iface *in_iface;
2426 struct port *in_port;
2429 /* Find the interface and port structure for the received packet. */
2430 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2432 /* No interface? Something fishy... */
2434 /* Odd. A few possible reasons here:
2436 * - We deleted an interface but there are still a few packets
2437 * queued up from it.
2439 * - Someone externally added an interface (e.g. with "ovs-dpctl
2440 * add-if") that we don't know about.
2442 * - Packet arrived on the local port but the local port is not
2443 * one of our bridge ports.
2445 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2447 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
2448 "interface %"PRIu16, br->name, flow->in_port);
2454 *in_portp = in_port = in_iface->port;
2455 *vlanp = vlan = flow_get_vlan(br, flow, in_port, have_packet);
2460 /* Drop frames for reserved multicast addresses. */
2461 if (eth_addr_is_reserved(flow->dl_dst)) {
2465 /* Drop frames on ports reserved for mirroring. */
2466 if (in_port->is_mirror_output_port) {
2468 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2469 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2470 "%s, which is reserved exclusively for mirroring",
2471 br->name, in_port->name);
2476 if (in_port->bond) {
2477 struct mac_entry *mac;
2479 switch (bond_check_admissibility(in_port->bond, in_iface,
2480 flow->dl_dst, tags)) {
2487 case BV_DROP_IF_MOVED:
2488 mac = mac_learning_lookup(br->ml, flow->dl_src, vlan, NULL);
2489 if (mac && mac->port.p != in_port &&
2490 (!is_gratuitous_arp(flow)
2491 || mac_entry_is_grat_arp_locked(mac))) {
2501 /* If the composed actions may be applied to any packet in the given 'flow',
2502 * returns true. Otherwise, the actions should only be applied to 'packet', or
2503 * not at all, if 'packet' was NULL. */
2505 process_flow(struct bridge *br, const struct flow *flow,
2506 const struct ofpbuf *packet, struct ofpbuf *actions,
2507 tag_type *tags, uint16_t *nf_output_iface)
2509 struct port *in_port;
2510 struct port *out_port;
2511 struct mac_entry *mac;
2514 /* Check whether we should drop packets in this flow. */
2515 if (!is_admissible(br, flow, packet != NULL, tags, &vlan, &in_port)) {
2520 /* Learn source MAC (but don't try to learn from revalidation). */
2522 update_learning_table(br, flow, vlan, in_port);
2525 /* Determine output port. */
2526 mac = mac_learning_lookup(br->ml, flow->dl_dst, vlan, tags);
2528 out_port = mac->port.p;
2529 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
2530 /* If we are revalidating but don't have a learning entry then
2531 * eject the flow. Installing a flow that floods packets opens
2532 * up a window of time where we could learn from a packet reflected
2533 * on a bond and blackhole packets before the learning table is
2534 * updated to reflect the correct port. */
2537 out_port = FLOOD_PORT;
2540 /* Don't send packets out their input ports. */
2541 if (in_port == out_port) {
2547 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2555 bridge_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
2556 struct ofpbuf *actions, tag_type *tags,
2557 uint16_t *nf_output_iface, void *br_)
2559 struct bridge *br = br_;
2561 COVERAGE_INC(bridge_process_flow);
2562 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2566 bridge_special_ofhook_cb(const struct flow *flow,
2567 const struct ofpbuf *packet, void *br_)
2569 struct iface *iface;
2570 struct bridge *br = br_;
2572 iface = iface_from_dp_ifidx(br, flow->in_port);
2574 if (flow->dl_type == htons(ETH_TYPE_LACP)) {
2575 if (iface && iface->port->lacp && packet) {
2576 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
2578 lacp_process_pdu(iface->port->lacp, iface, pdu);
2588 bridge_account_flow_ofhook_cb(const struct flow *flow, tag_type tags,
2589 const struct nlattr *actions,
2591 uint64_t n_bytes, void *br_)
2593 struct bridge *br = br_;
2594 const struct nlattr *a;
2595 struct port *in_port;
2600 /* Feed information from the active flows back into the learning table to
2601 * ensure that table is always in sync with what is actually flowing
2602 * through the datapath.
2604 * We test that 'tags' is nonzero to ensure that only flows that include an
2605 * OFPP_NORMAL action are used for learning. This works because
2606 * bridge_normal_ofhook_cb() always sets a nonzero tag value. */
2607 if (tags && is_admissible(br, flow, false, &dummy, &vlan, &in_port)) {
2608 update_learning_table(br, flow, vlan, in_port);
2611 /* Account for bond slave utilization. */
2612 if (!br->has_bonded_ports) {
2615 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
2616 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
2617 struct port *out_port = port_from_dp_ifidx(br, nl_attr_get_u32(a));
2618 if (out_port && out_port->bond) {
2619 uint16_t vlan = (flow->vlan_tci
2620 ? vlan_tci_to_vid(flow->vlan_tci)
2622 bond_account(out_port->bond, flow, vlan, n_bytes);
2629 bridge_account_checkpoint_ofhook_cb(void *br_)
2631 struct bridge *br = br_;
2634 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2636 bond_rebalance(port->bond,
2637 ofproto_get_revalidate_set(br->ofproto));
2643 bridge_autopath_ofhook_cb(const struct flow *flow, uint32_t ofp_port,
2644 tag_type *tags, void *br_)
2646 struct bridge *br = br_;
2647 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2648 struct port *port = port_from_dp_ifidx(br, odp_port);
2653 } else if (list_is_short(&port->ifaces)) {
2656 struct iface *iface;
2658 /* Autopath does not support VLAN hashing. */
2659 iface = bond_choose_output_slave(port->bond, flow,
2660 OFP_VLAN_NONE, tags);
2661 ret = iface ? iface->dp_ifidx : ODPP_NONE;
2664 return odp_port_to_ofp_port(ret);
2667 static struct ofhooks bridge_ofhooks = {
2668 bridge_normal_ofhook_cb,
2669 bridge_special_ofhook_cb,
2670 bridge_account_flow_ofhook_cb,
2671 bridge_account_checkpoint_ofhook_cb,
2672 bridge_autopath_ofhook_cb,
2675 /* Port functions. */
2678 lacp_send_pdu_cb(void *iface_, const struct lacp_pdu *pdu)
2680 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2681 struct iface *iface = iface_;
2682 uint8_t ea[ETH_ADDR_LEN];
2685 error = netdev_get_etheraddr(iface->netdev, ea);
2687 struct lacp_pdu *packet_pdu;
2688 struct ofpbuf packet;
2690 ofpbuf_init(&packet, 0);
2691 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2692 sizeof *packet_pdu);
2694 error = netdev_send(iface->netdev, &packet);
2696 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
2697 "(%s)", iface->port->name, iface->name,
2700 ofpbuf_uninit(&packet);
2702 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2703 "%s (%s)", iface->port->name, iface->name,
2709 port_run(struct port *port)
2712 lacp_run(port->lacp, lacp_send_pdu_cb);
2716 struct iface *iface;
2718 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2719 bool may_enable = lacp_slave_may_enable(port->lacp, iface);
2720 bond_slave_set_lacp_may_enable(port->bond, iface, may_enable);
2723 bond_run(port->bond,
2724 ofproto_get_revalidate_set(port->bridge->ofproto),
2725 lacp_negotiated(port->lacp));
2726 if (bond_should_send_learning_packets(port->bond)) {
2727 port_send_learning_packets(port);
2733 port_wait(struct port *port)
2736 lacp_wait(port->lacp);
2740 bond_wait(port->bond);
2744 static struct port *
2745 port_create(struct bridge *br, const char *name)
2749 port = xzalloc(sizeof *port);
2752 port->trunks = NULL;
2753 port->name = xstrdup(name);
2754 list_init(&port->ifaces);
2756 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
2758 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2765 get_port_other_config(const struct ovsrec_port *port, const char *key,
2766 const char *default_value)
2770 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
2772 return value ? value : default_value;
2776 get_interface_other_config(const struct ovsrec_interface *iface,
2777 const char *key, const char *default_value)
2781 value = get_ovsrec_key_value(&iface->header_,
2782 &ovsrec_interface_col_other_config, key);
2783 return value ? value : default_value;
2787 port_del_ifaces(struct port *port, const struct ovsrec_port *cfg)
2789 struct iface *iface, *next;
2790 struct sset new_ifaces;
2793 /* Collect list of new interfaces. */
2794 sset_init(&new_ifaces);
2795 for (i = 0; i < cfg->n_interfaces; i++) {
2796 const char *name = cfg->interfaces[i]->name;
2797 sset_add(&new_ifaces, name);
2800 /* Get rid of deleted interfaces. */
2801 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2802 if (!sset_contains(&new_ifaces, iface->name)) {
2803 iface_destroy(iface);
2807 sset_destroy(&new_ifaces);
2810 /* Expires all MAC learning entries associated with 'port' and forces ofproto
2811 * to revalidate every flow. */
2813 port_flush_macs(struct port *port)
2815 struct bridge *br = port->bridge;
2816 struct mac_learning *ml = br->ml;
2817 struct mac_entry *mac, *next_mac;
2820 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2821 if (mac->port.p == port) {
2822 mac_learning_expire(ml, mac);
2828 port_reconfigure(struct port *port, const struct ovsrec_port *cfg)
2830 struct sset new_ifaces;
2831 bool need_flush = false;
2832 unsigned long *trunks;
2839 /* Add new interfaces and update 'cfg' member of existing ones. */
2840 sset_init(&new_ifaces);
2841 for (i = 0; i < cfg->n_interfaces; i++) {
2842 const struct ovsrec_interface *if_cfg = cfg->interfaces[i];
2843 struct iface *iface;
2845 if (!sset_add(&new_ifaces, if_cfg->name)) {
2846 VLOG_WARN("port %s: %s specified twice as port interface",
2847 port->name, if_cfg->name);
2848 iface_set_ofport(if_cfg, -1);
2852 iface = iface_lookup(port->bridge, if_cfg->name);
2854 if (iface->port != port) {
2855 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
2857 port->bridge->name, if_cfg->name, iface->port->name);
2860 iface->cfg = if_cfg;
2862 iface = iface_create(port, if_cfg);
2865 /* Determine interface type. The local port always has type
2866 * "internal". Other ports take their type from the database and
2867 * default to "system" if none is specified. */
2868 iface->type = (!strcmp(if_cfg->name, port->bridge->name) ? "internal"
2869 : if_cfg->type[0] ? if_cfg->type
2872 sset_destroy(&new_ifaces);
2877 if (list_is_short(&port->ifaces)) {
2879 if (vlan >= 0 && vlan <= 4095) {
2880 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
2885 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
2886 * they even work as-is. But they have not been tested. */
2887 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
2891 if (port->vlan != vlan) {
2896 /* Get trunked VLANs. */
2898 if (vlan < 0 && cfg->n_trunks) {
2899 trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
2901 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
2904 } else if (vlan >= 0 && cfg->n_trunks) {
2905 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
2908 if (!vlan_bitmap_equal(trunks, port->trunks)) {
2911 bitmap_free(port->trunks);
2912 port->trunks = trunks;
2915 port_flush_macs(port);
2920 port_destroy(struct port *port)
2923 struct bridge *br = port->bridge;
2924 struct iface *iface, *next;
2927 for (i = 0; i < MAX_MIRRORS; i++) {
2928 struct mirror *m = br->mirrors[i];
2929 if (m && m->out_port == port) {
2934 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2935 iface_destroy(iface);
2938 hmap_remove(&br->ports, &port->hmap_node);
2940 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
2942 bond_destroy(port->bond);
2943 lacp_destroy(port->lacp);
2944 port_flush_macs(port);
2946 bitmap_free(port->trunks);
2952 static struct port *
2953 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
2955 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
2956 return iface ? iface->port : NULL;
2959 static struct port *
2960 port_lookup(const struct bridge *br, const char *name)
2964 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
2966 if (!strcmp(port->name, name)) {
2974 enable_lacp(struct port *port, bool *activep)
2976 if (!port->cfg->lacp) {
2977 /* XXX when LACP implementation has been sufficiently tested, enable by
2978 * default and make active on bonded ports. */
2980 } else if (!strcmp(port->cfg->lacp, "off")) {
2982 } else if (!strcmp(port->cfg->lacp, "active")) {
2985 } else if (!strcmp(port->cfg->lacp, "passive")) {
2989 VLOG_WARN("port %s: unknown LACP mode %s",
2990 port->name, port->cfg->lacp);
2996 iface_reconfigure_lacp(struct iface *iface)
2998 struct lacp_slave_settings s;
2999 int priority, portid;
3001 portid = atoi(get_interface_other_config(iface->cfg, "lacp-port-id", "0"));
3002 priority = atoi(get_interface_other_config(iface->cfg,
3003 "lacp-port-priority", "0"));
3005 if (portid <= 0 || portid > UINT16_MAX) {
3006 portid = iface->dp_ifidx;
3009 if (priority <= 0 || priority > UINT16_MAX) {
3010 priority = UINT16_MAX;
3013 s.name = iface->name;
3015 s.priority = priority;
3016 lacp_slave_register(iface->port->lacp, iface, &s);
3020 port_reconfigure_lacp(struct port *port)
3022 static struct lacp_settings s;
3023 struct iface *iface;
3024 uint8_t sysid[ETH_ADDR_LEN];
3025 const char *sysid_str;
3026 const char *lacp_time;
3027 long long int custom_time;
3030 if (!enable_lacp(port, &s.active)) {
3031 lacp_destroy(port->lacp);
3036 sysid_str = get_port_other_config(port->cfg, "lacp-system-id", NULL);
3037 if (sysid_str && eth_addr_from_string(sysid_str, sysid)) {
3038 memcpy(s.id, sysid, ETH_ADDR_LEN);
3040 memcpy(s.id, port->bridge->ea, ETH_ADDR_LEN);
3043 s.name = port->name;
3045 /* Prefer bondable links if unspecified. */
3046 priority = atoi(get_port_other_config(port->cfg, "lacp-system-priority",
3048 s.priority = (priority > 0 && priority <= UINT16_MAX
3050 : UINT16_MAX - !list_is_short(&port->ifaces));
3052 s.strict = !strcmp(get_port_other_config(port->cfg, "lacp-strict",
3056 lacp_time = get_port_other_config(port->cfg, "lacp-time", "slow");
3057 custom_time = atoi(lacp_time);
3058 if (!strcmp(lacp_time, "fast")) {
3059 s.lacp_time = LACP_TIME_FAST;
3060 } else if (!strcmp(lacp_time, "slow")) {
3061 s.lacp_time = LACP_TIME_SLOW;
3062 } else if (custom_time > 0) {
3063 s.lacp_time = LACP_TIME_CUSTOM;
3064 s.custom_time = custom_time;
3066 s.lacp_time = LACP_TIME_SLOW;
3070 port->lacp = lacp_create();
3073 lacp_configure(port->lacp, &s);
3075 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3076 iface_reconfigure_lacp(iface);
3081 port_reconfigure_bond(struct port *port)
3083 struct bond_settings s;
3084 const char *detect_s;
3085 struct iface *iface;
3087 if (list_is_short(&port->ifaces)) {
3088 /* Not a bonded port. */
3089 bond_destroy(port->bond);
3094 port->bridge->has_bonded_ports = true;
3096 s.name = port->name;
3098 if (port->cfg->bond_mode
3099 && !bond_mode_from_string(&s.balance, port->cfg->bond_mode)) {
3100 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
3101 port->name, port->cfg->bond_mode,
3102 bond_mode_to_string(s.balance));
3105 s.detect = BLSM_CARRIER;
3106 detect_s = get_port_other_config(port->cfg, "bond-detect-mode", NULL);
3107 if (detect_s && !bond_detect_mode_from_string(&s.detect, detect_s)) {
3108 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
3110 port->name, detect_s, bond_detect_mode_to_string(s.detect));
3113 s.miimon_interval = atoi(
3114 get_port_other_config(port->cfg, "bond-miimon-interval", "200"));
3115 if (s.miimon_interval < 100) {
3116 s.miimon_interval = 100;
3119 s.up_delay = MAX(0, port->cfg->bond_updelay);
3120 s.down_delay = MAX(0, port->cfg->bond_downdelay);
3121 s.rebalance_interval = atoi(
3122 get_port_other_config(port->cfg, "bond-rebalance-interval", "10000"));
3123 if (s.rebalance_interval < 1000) {
3124 s.rebalance_interval = 1000;
3127 s.fake_iface = port->cfg->bond_fake_iface;
3130 port->bond = bond_create(&s);
3132 if (bond_reconfigure(port->bond, &s)) {
3133 bridge_flush(port->bridge);
3137 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3138 uint16_t stable_id = (port->lacp
3139 ? lacp_slave_get_port_id(port->lacp, iface)
3141 bond_slave_register(iface->port->bond, iface, stable_id,
3147 port_send_learning_packets(struct port *port)
3149 struct bridge *br = port->bridge;
3150 int error, n_packets, n_errors;
3151 struct mac_entry *e;
3153 error = n_packets = n_errors = 0;
3154 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
3155 if (e->port.p != port) {
3156 int ret = bond_send_learning_packet(port->bond, e->mac, e->vlan);
3166 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3167 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3168 "packets, last error was: %s",
3169 port->name, n_errors, n_packets, strerror(error));
3171 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3172 port->name, n_packets);
3176 /* Interface functions. */
3178 static struct iface *
3179 iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
3181 struct bridge *br = port->bridge;
3182 struct iface *iface;
3183 char *name = if_cfg->name;
3185 iface = xzalloc(sizeof *iface);
3187 iface->name = xstrdup(name);
3188 iface->dp_ifidx = -1;
3189 iface->tag = tag_create_random();
3190 iface->netdev = NULL;
3191 iface->cfg = if_cfg;
3193 hmap_insert(&br->iface_by_name, &iface->name_node, hash_string(name, 0));
3195 list_push_back(&port->ifaces, &iface->port_elem);
3197 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3205 iface_destroy(struct iface *iface)
3208 struct port *port = iface->port;
3209 struct bridge *br = port->bridge;
3212 bond_slave_unregister(port->bond, iface);
3216 lacp_slave_unregister(port->lacp, iface);
3219 if (iface->dp_ifidx >= 0) {
3220 hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
3223 list_remove(&iface->port_elem);
3224 hmap_remove(&br->iface_by_name, &iface->name_node);
3226 netdev_close(iface->netdev);
3231 bridge_flush(port->bridge);
3235 static struct iface *
3236 iface_lookup(const struct bridge *br, const char *name)
3238 struct iface *iface;
3240 HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
3241 &br->iface_by_name) {
3242 if (!strcmp(iface->name, name)) {
3250 static struct iface *
3251 iface_find(const char *name)
3253 const struct bridge *br;
3255 HMAP_FOR_EACH (br, node, &all_bridges) {
3256 struct iface *iface = iface_lookup(br, name);
3265 static struct iface *
3266 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3268 struct iface *iface;
3270 HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
3271 hash_int(dp_ifidx, 0), &br->ifaces) {
3272 if (iface->dp_ifidx == dp_ifidx) {
3279 /* Set Ethernet address of 'iface', if one is specified in the configuration
3282 iface_set_mac(struct iface *iface)
3284 uint8_t ea[ETH_ADDR_LEN];
3286 if (!strcmp(iface->type, "internal")
3287 && iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
3288 if (iface->dp_ifidx == ODPP_LOCAL) {
3289 VLOG_ERR("interface %s: ignoring mac in Interface record "
3290 "(use Bridge record to set local port's mac)",
3292 } else if (eth_addr_is_multicast(ea)) {
3293 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3296 int error = netdev_set_etheraddr(iface->netdev, ea);
3298 VLOG_ERR("interface %s: setting MAC failed (%s)",
3299 iface->name, strerror(error));
3305 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
3307 iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
3309 if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
3310 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
3314 /* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
3316 * The value strings in '*shash' are taken directly from values[], not copied,
3317 * so the caller should not modify or free them. */
3319 shash_from_ovs_idl_map(char **keys, char **values, size_t n,
3320 struct shash *shash)
3325 for (i = 0; i < n; i++) {
3326 shash_add(shash, keys[i], values[i]);
3330 /* Creates 'keys' and 'values' arrays from 'shash'.
3332 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
3333 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
3334 * are populated with with strings taken directly from 'shash' and thus have
3335 * the same ownership of the key-value pairs in shash.
3338 shash_to_ovs_idl_map(struct shash *shash,
3339 char ***keys, char ***values, size_t *n)
3343 struct shash_node *sn;
3345 count = shash_count(shash);
3347 k = xmalloc(count * sizeof *k);
3348 v = xmalloc(count * sizeof *v);
3351 SHASH_FOR_EACH(sn, shash) {
3362 struct iface_delete_queues_cbdata {
3363 struct netdev *netdev;
3364 const struct ovsdb_datum *queues;
3368 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
3370 union ovsdb_atom atom;
3372 atom.integer = target;
3373 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
3377 iface_delete_queues(unsigned int queue_id,
3378 const struct shash *details OVS_UNUSED, void *cbdata_)
3380 struct iface_delete_queues_cbdata *cbdata = cbdata_;
3382 if (!queue_ids_include(cbdata->queues, queue_id)) {
3383 netdev_delete_queue(cbdata->netdev, queue_id);
3388 iface_update_qos(struct iface *iface, const struct ovsrec_qos *qos)
3390 if (!qos || qos->type[0] == '\0') {
3391 netdev_set_qos(iface->netdev, NULL, NULL);
3393 struct iface_delete_queues_cbdata cbdata;
3394 struct shash details;
3397 /* Configure top-level Qos for 'iface'. */
3398 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
3399 qos->n_other_config, &details);
3400 netdev_set_qos(iface->netdev, qos->type, &details);
3401 shash_destroy(&details);
3403 /* Deconfigure queues that were deleted. */
3404 cbdata.netdev = iface->netdev;
3405 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
3407 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
3409 /* Configure queues for 'iface'. */
3410 for (i = 0; i < qos->n_queues; i++) {
3411 const struct ovsrec_queue *queue = qos->value_queues[i];
3412 unsigned int queue_id = qos->key_queues[i];
3414 shash_from_ovs_idl_map(queue->key_other_config,
3415 queue->value_other_config,
3416 queue->n_other_config, &details);
3417 netdev_set_queue(iface->netdev, queue_id, &details);
3418 shash_destroy(&details);
3424 iface_update_cfm(struct iface *iface)
3428 uint16_t *remote_mps;
3429 struct ovsrec_monitor *mon;
3430 uint8_t maid[CCM_MAID_LEN];
3432 mon = iface->cfg->monitor;
3435 ofproto_iface_clear_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
3439 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
3440 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
3444 cfm.mpid = mon->mpid;
3445 cfm.interval = mon->interval ? *mon->interval : 1000;
3447 memcpy(cfm.maid, maid, sizeof cfm.maid);
3449 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
3450 for(i = 0; i < mon->n_remote_mps; i++) {
3451 remote_mps[i] = mon->remote_mps[i]->mpid;
3454 ofproto_iface_set_cfm(iface->port->bridge->ofproto, iface->dp_ifidx,
3455 &cfm, remote_mps, mon->n_remote_mps);
3459 /* Read carrier or miimon status directly from 'iface''s netdev, according to
3460 * how 'iface''s port is configured.
3462 * Returns true if 'iface' is up, false otherwise. */
3464 iface_get_carrier(const struct iface *iface)
3467 return netdev_get_carrier(iface->netdev);
3470 /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
3471 * instead of obtaining it from the database. */
3473 iface_is_synthetic(const struct iface *iface)
3475 return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
3478 /* Port mirroring. */
3480 static struct mirror *
3481 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
3485 for (i = 0; i < MAX_MIRRORS; i++) {
3486 struct mirror *m = br->mirrors[i];
3487 if (m && uuid_equals(uuid, &m->uuid)) {
3495 mirror_reconfigure(struct bridge *br)
3497 unsigned long *rspan_vlans;
3501 /* Get rid of deleted mirrors. */
3502 for (i = 0; i < MAX_MIRRORS; i++) {
3503 struct mirror *m = br->mirrors[i];
3505 const struct ovsdb_datum *mc;
3506 union ovsdb_atom atom;
3508 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
3509 atom.uuid = br->mirrors[i]->uuid;
3510 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3516 /* Add new mirrors and reconfigure existing ones. */
3517 for (i = 0; i < br->cfg->n_mirrors; i++) {
3518 struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
3519 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
3521 mirror_reconfigure_one(m, cfg);
3523 mirror_create(br, cfg);
3527 /* Update port reserved status. */
3528 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3529 port->is_mirror_output_port = false;
3531 for (i = 0; i < MAX_MIRRORS; i++) {
3532 struct mirror *m = br->mirrors[i];
3533 if (m && m->out_port) {
3534 m->out_port->is_mirror_output_port = true;
3538 /* Update flooded vlans (for RSPAN). */
3540 if (br->cfg->n_flood_vlans) {
3541 rspan_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
3542 br->cfg->n_flood_vlans);
3544 if (mac_learning_set_flood_vlans(br->ml, rspan_vlans)) {
3546 mac_learning_flush(br->ml);
3552 mirror_create(struct bridge *br, struct ovsrec_mirror *cfg)
3557 for (i = 0; ; i++) {
3558 if (i >= MAX_MIRRORS) {
3559 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
3560 "cannot create %s", br->name, MAX_MIRRORS, cfg->name);
3563 if (!br->mirrors[i]) {
3568 VLOG_INFO("created port mirror %s on bridge %s", cfg->name, br->name);
3570 mac_learning_flush(br->ml);
3572 br->mirrors[i] = m = xzalloc(sizeof *m);
3573 m->uuid = cfg->header_.uuid;
3576 m->name = xstrdup(cfg->name);
3577 sset_init(&m->src_ports);
3578 sset_init(&m->dst_ports);
3584 mirror_reconfigure_one(m, cfg);
3588 mirror_destroy(struct mirror *m)
3591 struct bridge *br = m->bridge;
3594 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3595 port->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3596 port->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3599 sset_destroy(&m->src_ports);
3600 sset_destroy(&m->dst_ports);
3603 m->bridge->mirrors[m->idx] = NULL;
3608 mac_learning_flush(br->ml);
3613 mirror_collect_ports(struct mirror *m, struct ovsrec_port **ports, int n_ports,
3618 for (i = 0; i < n_ports; i++) {
3619 const char *name = ports[i]->name;
3620 if (port_lookup(m->bridge, name)) {
3621 sset_add(names, name);
3623 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
3624 "port %s", m->bridge->name, m->name, name);
3630 mirror_collect_vlans(struct mirror *m, const struct ovsrec_mirror *cfg,
3636 *vlans = xmalloc(sizeof **vlans * cfg->n_select_vlan);
3638 for (i = 0; i < cfg->n_select_vlan; i++) {
3639 int64_t vlan = cfg->select_vlan[i];
3640 if (vlan < 0 || vlan > 4095) {
3641 VLOG_WARN("bridge %s: mirror %s selects invalid VLAN %"PRId64,
3642 m->bridge->name, m->name, vlan);
3644 (*vlans)[n_vlans++] = vlan;
3651 vlan_is_mirrored(const struct mirror *m, int vlan)
3655 for (i = 0; i < m->n_vlans; i++) {
3656 if (m->vlans[i] == vlan) {
3664 mirror_reconfigure_one(struct mirror *m, struct ovsrec_mirror *cfg)
3666 struct sset src_ports, dst_ports;
3667 mirror_mask_t mirror_bit;
3668 struct port *out_port;
3675 if (strcmp(cfg->name, m->name)) {
3677 m->name = xstrdup(cfg->name);
3680 /* Get output port. */
3681 if (cfg->output_port) {
3682 out_port = port_lookup(m->bridge, cfg->output_port->name);
3684 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
3685 m->bridge->name, m->name);
3691 if (cfg->output_vlan) {
3692 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
3693 "output vlan; ignoring output vlan",
3694 m->bridge->name, m->name);
3696 } else if (cfg->output_vlan) {
3698 out_vlan = *cfg->output_vlan;
3700 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
3701 m->bridge->name, m->name);
3706 sset_init(&src_ports);
3707 sset_init(&dst_ports);
3708 if (cfg->select_all) {
3709 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3710 sset_add(&src_ports, port->name);
3711 sset_add(&dst_ports, port->name);
3716 /* Get ports, and drop duplicates and ports that don't exist. */
3717 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
3719 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
3722 /* Get all the vlans, and drop duplicate and invalid vlans. */
3723 n_vlans = mirror_collect_vlans(m, cfg, &vlans);
3726 /* Update mirror data. */
3727 if (!sset_equals(&m->src_ports, &src_ports)
3728 || !sset_equals(&m->dst_ports, &dst_ports)
3729 || m->n_vlans != n_vlans
3730 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
3731 || m->out_port != out_port
3732 || m->out_vlan != out_vlan) {
3733 bridge_flush(m->bridge);
3734 mac_learning_flush(m->bridge->ml);
3736 sset_swap(&m->src_ports, &src_ports);
3737 sset_swap(&m->dst_ports, &dst_ports);
3740 m->n_vlans = n_vlans;
3741 m->out_port = out_port;
3742 m->out_vlan = out_vlan;
3745 mirror_bit = MIRROR_MASK_C(1) << m->idx;
3746 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3747 if (sset_contains(&m->src_ports, port->name)) {
3748 port->src_mirrors |= mirror_bit;
3750 port->src_mirrors &= ~mirror_bit;
3753 if (sset_contains(&m->dst_ports, port->name)) {
3754 port->dst_mirrors |= mirror_bit;
3756 port->dst_mirrors &= ~mirror_bit;
3761 sset_destroy(&src_ports);
3762 sset_destroy(&dst_ports);