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"
77 VLOG_DEFINE_THIS_MODULE(bridge);
79 COVERAGE_DEFINE(bridge_flush);
80 COVERAGE_DEFINE(bridge_process_flow);
81 COVERAGE_DEFINE(bridge_reconfigure);
89 struct dst builtin[32];
94 static void dst_set_init(struct dst_set *);
95 static void dst_set_add(struct dst_set *, const struct dst *);
96 static void dst_set_free(struct dst_set *);
99 /* These members are always valid. */
100 struct list port_elem; /* Element in struct port's "ifaces" list. */
101 struct port *port; /* Containing port. */
102 char *name; /* Host network device name. */
103 tag_type tag; /* Tag associated with this interface. */
105 /* These members are valid only after bridge_reconfigure() causes them to
107 struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
108 int dp_ifidx; /* Index within kernel datapath. */
109 struct netdev *netdev; /* Network device. */
110 const char *type; /* Usually same as cfg->type. */
111 const struct ovsrec_interface *cfg;
114 #define MAX_MIRRORS 32
115 typedef uint32_t mirror_mask_t;
116 #define MIRROR_MASK_C(X) UINT32_C(X)
117 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
119 struct bridge *bridge;
122 struct uuid uuid; /* UUID of this "mirror" record in database. */
124 /* Selection criteria. */
125 struct sset src_ports; /* Source port names. */
126 struct sset dst_ports; /* Destination port names. */
131 struct port *out_port;
135 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
137 struct bridge *bridge;
138 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
141 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
142 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
143 * NULL if all VLANs are trunked. */
144 const struct ovsrec_port *cfg;
146 /* An ordinary bridge port has 1 interface.
147 * A bridge port for bonding has at least 2 interfaces. */
148 struct list ifaces; /* List of "struct iface"s. */
150 struct lacp *lacp; /* NULL if LACP is not enabled. */
155 /* Port mirroring info. */
156 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
157 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
158 bool is_mirror_output_port; /* Does port mirroring send frames here? */
162 struct list node; /* Node in global list of bridges. */
163 char *name; /* User-specified arbitrary name. */
164 struct mac_learning *ml; /* MAC learning table. */
165 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
166 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
167 const struct ovsrec_bridge *cfg;
169 /* OpenFlow switch processing. */
170 struct ofproto *ofproto; /* OpenFlow switch. */
172 /* Kernel datapath information. */
173 struct dpif *dpif; /* Datapath. */
174 struct hmap ifaces; /* "struct iface"s indexed by dp_ifidx. */
177 struct hmap ports; /* "struct port"s indexed by name. */
178 struct shash iface_by_name; /* "struct iface"s indexed by name. */
181 bool has_bonded_ports;
186 /* Port mirroring. */
187 struct mirror *mirrors[MAX_MIRRORS];
190 /* List of all bridges. */
191 static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
193 /* OVSDB IDL used to obtain configuration. */
194 static struct ovsdb_idl *idl;
196 /* Each time this timer expires, the bridge fetches systems and interface
197 * statistics and pushes them into the database. */
198 #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
199 static long long int stats_timer = LLONG_MIN;
201 /* Stores the time after which rate limited statistics may be written to the
202 * database. Only updated when changes to the database require rate limiting.
204 #define DB_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
205 static long long int db_limiter = LLONG_MIN;
207 static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
208 static void bridge_destroy(struct bridge *);
209 static struct bridge *bridge_lookup(const char *name);
210 static unixctl_cb_func bridge_unixctl_dump_flows;
211 static unixctl_cb_func bridge_unixctl_reconnect;
212 static int bridge_run_one(struct bridge *);
213 static size_t bridge_get_controllers(const struct bridge *br,
214 struct ovsrec_controller ***controllersp);
215 static void bridge_reconfigure_one(struct bridge *);
216 static void bridge_reconfigure_remotes(struct bridge *,
217 const struct sockaddr_in *managers,
219 static void bridge_get_all_ifaces(const struct bridge *, struct shash *ifaces);
220 static void bridge_fetch_dp_ifaces(struct bridge *);
221 static void bridge_flush(struct bridge *);
222 static void bridge_pick_local_hw_addr(struct bridge *,
223 uint8_t ea[ETH_ADDR_LEN],
224 struct iface **hw_addr_iface);
225 static uint64_t bridge_pick_datapath_id(struct bridge *,
226 const uint8_t bridge_ea[ETH_ADDR_LEN],
227 struct iface *hw_addr_iface);
228 static uint64_t dpid_from_hash(const void *, size_t nbytes);
230 static unixctl_cb_func bridge_unixctl_fdb_show;
231 static unixctl_cb_func cfm_unixctl_show;
232 static unixctl_cb_func qos_unixctl_show;
234 static void port_run(struct port *);
235 static void port_wait(struct port *);
236 static struct port *port_create(struct bridge *, const char *name);
237 static void port_reconfigure(struct port *, const struct ovsrec_port *);
238 static void port_del_ifaces(struct port *, const struct ovsrec_port *);
239 static void port_destroy(struct port *);
240 static struct port *port_lookup(const struct bridge *, const char *name);
241 static struct iface *port_get_an_iface(const struct port *);
242 static struct port *port_from_dp_ifidx(const struct bridge *,
244 static void port_reconfigure_lacp(struct port *);
245 static void port_reconfigure_bond(struct port *);
246 static void port_send_learning_packets(struct port *);
248 static void mirror_create(struct bridge *, struct ovsrec_mirror *);
249 static void mirror_destroy(struct mirror *);
250 static void mirror_reconfigure(struct bridge *);
251 static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
252 static bool vlan_is_mirrored(const struct mirror *, int vlan);
254 static struct iface *iface_create(struct port *port,
255 const struct ovsrec_interface *if_cfg);
256 static void iface_destroy(struct iface *);
257 static struct iface *iface_lookup(const struct bridge *, const char *name);
258 static struct iface *iface_find(const char *name);
259 static struct iface *iface_from_dp_ifidx(const struct bridge *,
261 static void iface_set_mac(struct iface *);
262 static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
263 static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
264 static void iface_update_cfm(struct iface *);
265 static bool iface_refresh_cfm_stats(struct iface *iface);
266 static bool iface_get_carrier(const struct iface *);
268 static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
270 static void shash_to_ovs_idl_map(struct shash *,
271 char ***keys, char ***values, size_t *n);
273 /* Hooks into ofproto processing. */
274 static struct ofhooks bridge_ofhooks;
276 /* Public functions. */
278 /* Initializes the bridge module, configuring it to obtain its configuration
279 * from an OVSDB server accessed over 'remote', which should be a string in a
280 * form acceptable to ovsdb_idl_create(). */
282 bridge_init(const char *remote)
284 /* Create connection to database. */
285 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
287 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
288 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
289 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
290 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
291 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
292 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
293 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
295 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
296 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
298 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
299 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
301 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
302 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
303 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
304 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
305 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
306 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
307 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
308 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
309 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
311 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
312 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
313 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
314 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
316 ovsdb_idl_omit_alert(idl, &ovsrec_maintenance_point_col_fault);
318 ovsdb_idl_omit_alert(idl, &ovsrec_monitor_col_fault);
320 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
322 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
324 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
326 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
328 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
330 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
331 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
332 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
333 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
334 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
336 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
338 /* Register unixctl commands. */
339 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
340 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
341 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
342 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
344 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
353 struct bridge *br, *next_br;
355 LIST_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
358 ovsdb_idl_destroy(idl);
361 /* Performs configuration that is only necessary once at ovs-vswitchd startup,
362 * but for which the ovs-vswitchd configuration 'cfg' is required. */
364 bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
366 static bool already_configured_once;
367 struct sset bridge_names;
368 struct sset dpif_names, dpif_types;
372 /* Only do this once per ovs-vswitchd run. */
373 if (already_configured_once) {
376 already_configured_once = true;
378 stats_timer = time_msec() + STATS_INTERVAL;
380 /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
381 sset_init(&bridge_names);
382 for (i = 0; i < cfg->n_bridges; i++) {
383 sset_add(&bridge_names, cfg->bridges[i]->name);
386 /* Iterate over all system dpifs and delete any of them that do not appear
388 sset_init(&dpif_names);
389 sset_init(&dpif_types);
390 dp_enumerate_types(&dpif_types);
391 SSET_FOR_EACH (type, &dpif_types) {
394 dp_enumerate_names(type, &dpif_names);
396 /* Delete each dpif whose name is not in 'bridge_names'. */
397 SSET_FOR_EACH (name, &dpif_names) {
398 if (!sset_contains(&bridge_names, name)) {
402 retval = dpif_open(name, type, &dpif);
410 sset_destroy(&bridge_names);
411 sset_destroy(&dpif_names);
412 sset_destroy(&dpif_types);
415 /* Callback for iterate_and_prune_ifaces(). */
417 check_iface(struct bridge *br, struct iface *iface, void *aux OVS_UNUSED)
419 if (!iface->netdev) {
420 /* We already reported a related error, don't bother duplicating it. */
424 if (iface->dp_ifidx < 0) {
425 VLOG_ERR("%s interface not in %s, dropping",
426 iface->name, dpif_name(br->dpif));
430 VLOG_DBG("%s has interface %s on port %d", dpif_name(br->dpif),
431 iface->name, iface->dp_ifidx);
435 /* Callback for iterate_and_prune_ifaces(). */
437 set_iface_properties(struct bridge *br OVS_UNUSED, struct iface *iface,
438 void *aux OVS_UNUSED)
440 /* Set policing attributes. */
441 netdev_set_policing(iface->netdev,
442 iface->cfg->ingress_policing_rate,
443 iface->cfg->ingress_policing_burst);
445 /* Set MAC address of internal interfaces other than the local
447 if (iface->dp_ifidx != ODPP_LOCAL && !strcmp(iface->type, "internal")) {
448 iface_set_mac(iface);
454 /* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
455 * Deletes from 'br' all the interfaces for which 'cb' returns false, and then
456 * deletes from 'br' any ports that no longer have any interfaces. */
458 iterate_and_prune_ifaces(struct bridge *br,
459 bool (*cb)(struct bridge *, struct iface *,
463 struct port *port, *next_port;
465 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
466 struct iface *iface, *next_iface;
468 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
469 if (!cb(br, iface, aux)) {
470 iface_set_ofport(iface->cfg, -1);
471 iface_destroy(iface);
475 if (list_is_empty(&port->ifaces)) {
476 VLOG_WARN("%s port has no interfaces, dropping", port->name);
482 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
483 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
484 * responsible for freeing '*managersp' (with free()).
486 * You may be asking yourself "why does ovs-vswitchd care?", because
487 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
488 * should not be and in fact is not directly involved in that. But
489 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
490 * it has to tell in-band control where the managers are to enable that.
491 * (Thus, only managers connected in-band are collected.)
494 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
495 struct sockaddr_in **managersp, size_t *n_managersp)
497 struct sockaddr_in *managers = NULL;
498 size_t n_managers = 0;
502 /* Collect all of the potential targets from the "targets" columns of the
503 * rows pointed to by "manager_options", excluding any that are
506 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
507 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
509 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
510 sset_find_and_delete(&targets, m->target);
512 sset_add(&targets, m->target);
516 /* Now extract the targets' IP addresses. */
517 if (!sset_is_empty(&targets)) {
520 managers = xmalloc(sset_count(&targets) * sizeof *managers);
521 SSET_FOR_EACH (target, &targets) {
522 struct sockaddr_in *sin = &managers[n_managers];
524 if ((!strncmp(target, "tcp:", 4)
525 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
526 (!strncmp(target, "ssl:", 4)
527 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
532 sset_destroy(&targets);
534 *managersp = managers;
535 *n_managersp = n_managers;
539 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
541 struct shash old_br, new_br;
542 struct shash_node *node;
543 struct bridge *br, *next;
544 struct sockaddr_in *managers;
547 int sflow_bridge_number;
549 COVERAGE_INC(bridge_reconfigure);
551 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
553 /* Collect old and new bridges. */
556 LIST_FOR_EACH (br, node, &all_bridges) {
557 shash_add(&old_br, br->name, br);
559 for (i = 0; i < ovs_cfg->n_bridges; i++) {
560 const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
561 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
562 VLOG_WARN("more than one bridge named %s", br_cfg->name);
566 /* Get rid of deleted bridges and add new bridges. */
567 LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
568 struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
575 SHASH_FOR_EACH (node, &new_br) {
576 const char *br_name = node->name;
577 const struct ovsrec_bridge *br_cfg = node->data;
578 br = shash_find_data(&old_br, br_name);
580 /* If the bridge datapath type has changed, we need to tear it
581 * down and recreate. */
582 if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
584 bridge_create(br_cfg);
587 bridge_create(br_cfg);
590 shash_destroy(&old_br);
591 shash_destroy(&new_br);
593 /* Reconfigure all bridges. */
594 LIST_FOR_EACH (br, node, &all_bridges) {
595 bridge_reconfigure_one(br);
598 /* Add and delete ports on all datapaths.
600 * The kernel will reject any attempt to add a given port to a datapath if
601 * that port already belongs to a different datapath, so we must do all
602 * port deletions before any port additions. */
603 LIST_FOR_EACH (br, node, &all_bridges) {
604 struct dpif_port_dump dump;
605 struct shash want_ifaces;
606 struct dpif_port dpif_port;
608 bridge_get_all_ifaces(br, &want_ifaces);
609 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
610 if (!shash_find(&want_ifaces, dpif_port.name)
611 && strcmp(dpif_port.name, br->name)) {
612 int retval = dpif_port_del(br->dpif, dpif_port.port_no);
614 VLOG_WARN("failed to remove %s interface from %s: %s",
615 dpif_port.name, dpif_name(br->dpif),
620 shash_destroy(&want_ifaces);
622 LIST_FOR_EACH (br, node, &all_bridges) {
623 struct shash cur_ifaces, want_ifaces;
624 struct dpif_port_dump dump;
625 struct dpif_port dpif_port;
627 /* Get the set of interfaces currently in this datapath. */
628 shash_init(&cur_ifaces);
629 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
630 struct dpif_port *port_info = xmalloc(sizeof *port_info);
631 dpif_port_clone(port_info, &dpif_port);
632 shash_add(&cur_ifaces, dpif_port.name, port_info);
635 /* Get the set of interfaces we want on this datapath. */
636 bridge_get_all_ifaces(br, &want_ifaces);
638 hmap_clear(&br->ifaces);
639 SHASH_FOR_EACH (node, &want_ifaces) {
640 const char *if_name = node->name;
641 struct iface *iface = node->data;
642 struct dpif_port *dpif_port;
646 type = iface ? iface->type : "internal";
647 dpif_port = shash_find_data(&cur_ifaces, if_name);
649 /* If we have a port or a netdev already, and it's not the type we
650 * want, then delete the port (if any) and close the netdev (if
652 if ((dpif_port && strcmp(dpif_port->type, type))
653 || (iface && iface->netdev
654 && strcmp(type, netdev_get_type(iface->netdev)))) {
656 error = ofproto_port_del(br->ofproto, dpif_port->port_no);
663 netdev_close(iface->netdev);
664 iface->netdev = NULL;
668 /* If the port doesn't exist or we don't have the netdev open,
669 * we need to do more work. */
670 if (!dpif_port || (iface && !iface->netdev)) {
671 struct netdev_options options;
672 struct netdev *netdev;
675 /* First open the network device. */
676 options.name = if_name;
678 options.args = &args;
679 options.ethertype = NETDEV_ETH_TYPE_NONE;
683 shash_from_ovs_idl_map(iface->cfg->key_options,
684 iface->cfg->value_options,
685 iface->cfg->n_options, &args);
687 error = netdev_open(&options, &netdev);
688 shash_destroy(&args);
691 VLOG_WARN("could not open network device %s (%s)",
692 if_name, strerror(error));
696 /* Then add the port if we haven't already. */
698 error = dpif_port_add(br->dpif, netdev, NULL);
700 netdev_close(netdev);
701 if (error == EFBIG) {
702 VLOG_ERR("ran out of valid port numbers on %s",
703 dpif_name(br->dpif));
706 VLOG_WARN("failed to add %s interface to %s: %s",
707 if_name, dpif_name(br->dpif),
714 /* Update 'iface'. */
716 iface->netdev = netdev;
718 } else if (iface && iface->netdev) {
722 shash_from_ovs_idl_map(iface->cfg->key_options,
723 iface->cfg->value_options,
724 iface->cfg->n_options, &args);
725 netdev_set_config(iface->netdev, &args);
726 shash_destroy(&args);
729 shash_destroy(&want_ifaces);
731 SHASH_FOR_EACH (node, &cur_ifaces) {
732 struct dpif_port *port_info = node->data;
733 dpif_port_destroy(port_info);
736 shash_destroy(&cur_ifaces);
738 sflow_bridge_number = 0;
739 LIST_FOR_EACH (br, node, &all_bridges) {
740 uint8_t ea[ETH_ADDR_LEN];
742 struct iface *local_iface;
743 struct iface *hw_addr_iface;
746 bridge_fetch_dp_ifaces(br);
748 /* Delete interfaces that cannot be opened.
750 * From this point forward we are guaranteed that every "struct iface"
751 * has nonnull 'netdev' and correct 'dp_ifidx'. */
752 iterate_and_prune_ifaces(br, check_iface, NULL);
754 /* Pick local port hardware address, datapath ID. */
755 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
756 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
758 int error = netdev_set_etheraddr(local_iface->netdev, ea);
760 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
761 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
762 "Ethernet address: %s",
763 br->name, strerror(error));
766 memcpy(br->ea, ea, ETH_ADDR_LEN);
768 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
769 ofproto_set_datapath_id(br->ofproto, dpid);
771 dpid_string = xasprintf("%016"PRIx64, dpid);
772 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
775 /* Set NetFlow configuration on this bridge. */
776 if (br->cfg->netflow) {
777 struct ovsrec_netflow *nf_cfg = br->cfg->netflow;
778 struct netflow_options opts;
780 memset(&opts, 0, sizeof opts);
782 dpif_get_netflow_ids(br->dpif, &opts.engine_type, &opts.engine_id);
783 if (nf_cfg->engine_type) {
784 opts.engine_type = *nf_cfg->engine_type;
786 if (nf_cfg->engine_id) {
787 opts.engine_id = *nf_cfg->engine_id;
790 opts.active_timeout = nf_cfg->active_timeout;
791 if (!opts.active_timeout) {
792 opts.active_timeout = -1;
793 } else if (opts.active_timeout < 0) {
794 VLOG_WARN("bridge %s: active timeout interval set to negative "
795 "value, using default instead (%d seconds)", br->name,
796 NF_ACTIVE_TIMEOUT_DEFAULT);
797 opts.active_timeout = -1;
800 opts.add_id_to_iface = nf_cfg->add_id_to_interface;
801 if (opts.add_id_to_iface) {
802 if (opts.engine_id > 0x7f) {
803 VLOG_WARN("bridge %s: netflow port mangling may conflict "
804 "with another vswitch, choose an engine id less "
805 "than 128", br->name);
807 if (hmap_count(&br->ports) > 508) {
808 VLOG_WARN("bridge %s: netflow port mangling will conflict "
809 "with another port when more than 508 ports are "
814 sset_init(&opts.collectors);
815 sset_add_array(&opts.collectors,
816 nf_cfg->targets, nf_cfg->n_targets);
817 if (ofproto_set_netflow(br->ofproto, &opts)) {
818 VLOG_ERR("bridge %s: problem setting netflow collectors",
821 sset_destroy(&opts.collectors);
823 ofproto_set_netflow(br->ofproto, NULL);
826 /* Set sFlow configuration on this bridge. */
827 if (br->cfg->sflow) {
828 const struct ovsrec_sflow *sflow_cfg = br->cfg->sflow;
829 struct ovsrec_controller **controllers;
830 struct ofproto_sflow_options oso;
831 size_t n_controllers;
833 memset(&oso, 0, sizeof oso);
835 sset_init(&oso.targets);
836 sset_add_array(&oso.targets,
837 sflow_cfg->targets, sflow_cfg->n_targets);
839 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
840 if (sflow_cfg->sampling) {
841 oso.sampling_rate = *sflow_cfg->sampling;
844 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
845 if (sflow_cfg->polling) {
846 oso.polling_interval = *sflow_cfg->polling;
849 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
850 if (sflow_cfg->header) {
851 oso.header_len = *sflow_cfg->header;
854 oso.sub_id = sflow_bridge_number++;
855 oso.agent_device = sflow_cfg->agent;
857 oso.control_ip = NULL;
858 n_controllers = bridge_get_controllers(br, &controllers);
859 for (i = 0; i < n_controllers; i++) {
860 if (controllers[i]->local_ip) {
861 oso.control_ip = controllers[i]->local_ip;
865 ofproto_set_sflow(br->ofproto, &oso);
867 sset_destroy(&oso.targets);
869 ofproto_set_sflow(br->ofproto, NULL);
872 /* Update the controller and related settings. It would be more
873 * straightforward to call this from bridge_reconfigure_one(), but we
874 * can't do it there for two reasons. First, and most importantly, at
875 * that point we don't know the dp_ifidx of any interfaces that have
876 * been added to the bridge (because we haven't actually added them to
877 * the datapath). Second, at that point we haven't set the datapath ID
878 * yet; when a controller is configured, resetting the datapath ID will
879 * immediately disconnect from the controller, so it's better to set
880 * the datapath ID before the controller. */
881 bridge_reconfigure_remotes(br, managers, n_managers);
883 LIST_FOR_EACH (br, node, &all_bridges) {
886 br->has_bonded_ports = false;
887 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
890 port_reconfigure_lacp(port);
891 port_reconfigure_bond(port);
893 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
894 iface_update_qos(iface, port->cfg->qos);
898 LIST_FOR_EACH (br, node, &all_bridges) {
899 iterate_and_prune_ifaces(br, set_iface_properties, NULL);
902 /* Some reconfiguration operations require the bridge to have been run at
904 LIST_FOR_EACH (br, node, &all_bridges) {
909 HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
910 iface_update_cfm(iface);
916 /* ovs-vswitchd has completed initialization, so allow the process that
917 * forked us to exit successfully. */
918 daemonize_complete();
922 get_ovsrec_key_value(const struct ovsdb_idl_row *row,
923 const struct ovsdb_idl_column *column,
926 const struct ovsdb_datum *datum;
927 union ovsdb_atom atom;
930 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
931 atom.string = (char *) key;
932 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
933 return idx == UINT_MAX ? NULL : datum->values[idx].string;
937 bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
939 return get_ovsrec_key_value(&br_cfg->header_,
940 &ovsrec_bridge_col_other_config, key);
944 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
945 struct iface **hw_addr_iface)
951 *hw_addr_iface = NULL;
953 /* Did the user request a particular MAC? */
954 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
955 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
956 if (eth_addr_is_multicast(ea)) {
957 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
958 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
959 } else if (eth_addr_is_zero(ea)) {
960 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
966 /* Otherwise choose the minimum non-local MAC address among all of the
968 memset(ea, 0xff, ETH_ADDR_LEN);
969 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
970 uint8_t iface_ea[ETH_ADDR_LEN];
971 struct iface *candidate;
974 /* Mirror output ports don't participate. */
975 if (port->is_mirror_output_port) {
979 /* Choose the MAC address to represent the port. */
981 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
982 /* Find the interface with this Ethernet address (if any) so that
983 * we can provide the correct devname to the caller. */
984 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
985 uint8_t candidate_ea[ETH_ADDR_LEN];
986 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
987 && eth_addr_equals(iface_ea, candidate_ea)) {
992 /* Choose the interface whose MAC address will represent the port.
993 * The Linux kernel bonding code always chooses the MAC address of
994 * the first slave added to a bond, and the Fedora networking
995 * scripts always add slaves to a bond in alphabetical order, so
996 * for compatibility we choose the interface with the name that is
997 * first in alphabetical order. */
998 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
999 if (!iface || strcmp(candidate->name, iface->name) < 0) {
1004 /* The local port doesn't count (since we're trying to choose its
1005 * MAC address anyway). */
1006 if (iface->dp_ifidx == ODPP_LOCAL) {
1011 error = netdev_get_etheraddr(iface->netdev, iface_ea);
1013 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1014 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
1015 iface->name, strerror(error));
1020 /* Compare against our current choice. */
1021 if (!eth_addr_is_multicast(iface_ea) &&
1022 !eth_addr_is_local(iface_ea) &&
1023 !eth_addr_is_reserved(iface_ea) &&
1024 !eth_addr_is_zero(iface_ea) &&
1025 eth_addr_compare_3way(iface_ea, ea) < 0)
1027 memcpy(ea, iface_ea, ETH_ADDR_LEN);
1028 *hw_addr_iface = iface;
1031 if (eth_addr_is_multicast(ea)) {
1032 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
1033 *hw_addr_iface = NULL;
1034 VLOG_WARN("bridge %s: using default bridge Ethernet "
1035 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1037 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1038 br->name, ETH_ADDR_ARGS(ea));
1042 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
1043 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
1044 * an interface on 'br', then that interface must be passed in as
1045 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1046 * 'hw_addr_iface' must be passed in as a null pointer. */
1048 bridge_pick_datapath_id(struct bridge *br,
1049 const uint8_t bridge_ea[ETH_ADDR_LEN],
1050 struct iface *hw_addr_iface)
1053 * The procedure for choosing a bridge MAC address will, in the most
1054 * ordinary case, also choose a unique MAC that we can use as a datapath
1055 * ID. In some special cases, though, multiple bridges will end up with
1056 * the same MAC address. This is OK for the bridges, but it will confuse
1057 * the OpenFlow controller, because each datapath needs a unique datapath
1060 * Datapath IDs must be unique. It is also very desirable that they be
1061 * stable from one run to the next, so that policy set on a datapath
1064 const char *datapath_id;
1067 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1068 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
1072 if (hw_addr_iface) {
1074 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
1076 * A bridge whose MAC address is taken from a VLAN network device
1077 * (that is, a network device created with vconfig(8) or similar
1078 * tool) will have the same MAC address as a bridge on the VLAN
1079 * device's physical network device.
1081 * Handle this case by hashing the physical network device MAC
1082 * along with the VLAN identifier.
1084 uint8_t buf[ETH_ADDR_LEN + 2];
1085 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1086 buf[ETH_ADDR_LEN] = vlan >> 8;
1087 buf[ETH_ADDR_LEN + 1] = vlan;
1088 return dpid_from_hash(buf, sizeof buf);
1091 * Assume that this bridge's MAC address is unique, since it
1092 * doesn't fit any of the cases we handle specially.
1097 * A purely internal bridge, that is, one that has no non-virtual
1098 * network devices on it at all, is more difficult because it has no
1099 * natural unique identifier at all.
1101 * When the host is a XenServer, we handle this case by hashing the
1102 * host's UUID with the name of the bridge. Names of bridges are
1103 * persistent across XenServer reboots, although they can be reused if
1104 * an internal network is destroyed and then a new one is later
1105 * created, so this is fairly effective.
1107 * When the host is not a XenServer, we punt by using a random MAC
1108 * address on each run.
1110 const char *host_uuid = xenserver_get_host_uuid();
1112 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1113 dpid = dpid_from_hash(combined, strlen(combined));
1119 return eth_addr_to_uint64(bridge_ea);
1123 dpid_from_hash(const void *data, size_t n)
1125 uint8_t hash[SHA1_DIGEST_SIZE];
1127 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
1128 sha1_bytes(data, n, hash);
1129 eth_addr_mark_random(hash);
1130 return eth_addr_to_uint64(hash);
1134 iface_refresh_status(struct iface *iface)
1138 enum netdev_flags flags;
1147 if (!netdev_get_status(iface->netdev, &sh)) {
1149 char **keys, **values;
1151 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1152 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1157 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1160 shash_destroy_free_data(&sh);
1162 error = netdev_get_flags(iface->netdev, &flags);
1164 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1167 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1170 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1172 ovsrec_interface_set_duplex(iface->cfg,
1173 netdev_features_is_full_duplex(current)
1175 /* warning: uint64_t -> int64_t conversion */
1176 bps = netdev_features_to_bps(current);
1177 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1180 ovsrec_interface_set_duplex(iface->cfg, NULL);
1181 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1185 ovsrec_interface_set_link_state(iface->cfg,
1186 iface_get_carrier(iface) ? "up" : "down");
1188 error = netdev_get_mtu(iface->netdev, &mtu);
1189 if (!error && mtu != INT_MAX) {
1191 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1194 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1198 /* Writes 'iface''s CFM statistics to the database. Returns true if anything
1199 * changed, false otherwise. */
1201 iface_refresh_cfm_stats(struct iface *iface)
1203 const struct ovsrec_monitor *mon;
1204 const struct cfm *cfm;
1205 bool changed = false;
1208 mon = iface->cfg->monitor;
1209 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1215 for (i = 0; i < mon->n_remote_mps; i++) {
1216 const struct ovsrec_maintenance_point *mp;
1217 const struct remote_mp *rmp;
1219 mp = mon->remote_mps[i];
1220 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1222 if (mp->n_fault != 1 || mp->fault[0] != rmp->fault) {
1223 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1228 if (mon->n_fault != 1 || mon->fault[0] != cfm->fault) {
1229 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1237 iface_refresh_lacp_stats(struct iface *iface)
1239 bool *db_current = iface->cfg->lacp_current;
1240 bool changed = false;
1242 if (iface->port->lacp) {
1243 bool current = lacp_slave_is_current(iface->port->lacp, iface);
1245 if (!db_current || *db_current != current) {
1247 ovsrec_interface_set_lacp_current(iface->cfg, ¤t, 1);
1249 } else if (db_current) {
1251 ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
1258 iface_refresh_stats(struct iface *iface)
1264 static const struct iface_stat iface_stats[] = {
1265 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1266 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1267 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1268 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1269 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1270 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1271 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1272 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1273 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1274 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1275 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1276 { "collisions", offsetof(struct netdev_stats, collisions) },
1278 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1279 const struct iface_stat *s;
1281 char *keys[N_STATS];
1282 int64_t values[N_STATS];
1285 struct netdev_stats stats;
1287 /* Intentionally ignore return value, since errors will set 'stats' to
1288 * all-1s, and we will deal with that correctly below. */
1289 netdev_get_stats(iface->netdev, &stats);
1292 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1293 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1294 if (value != UINT64_MAX) {
1301 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1305 refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1307 struct ovsdb_datum datum;
1311 get_system_stats(&stats);
1313 ovsdb_datum_from_shash(&datum, &stats);
1314 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1318 static inline const char *
1319 nx_role_to_str(enum nx_role role)
1324 case NX_ROLE_MASTER:
1329 return "*** INVALID ROLE ***";
1334 bridge_refresh_controller_status(const struct bridge *br)
1337 const struct ovsrec_controller *cfg;
1339 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1341 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
1342 struct ofproto_controller_info *cinfo =
1343 shash_find_data(&info, cfg->target);
1346 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1347 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1348 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1349 (char **) cinfo->pairs.values,
1352 ovsrec_controller_set_is_connected(cfg, false);
1353 ovsrec_controller_set_role(cfg, NULL);
1354 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1358 ofproto_free_ofproto_controller_info(&info);
1364 const struct ovsrec_open_vswitch *cfg;
1366 bool datapath_destroyed;
1367 bool database_changed;
1370 /* Let each bridge do the work that it needs to do. */
1371 datapath_destroyed = false;
1372 LIST_FOR_EACH (br, node, &all_bridges) {
1373 int error = bridge_run_one(br);
1375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1376 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1377 "forcing reconfiguration", br->name);
1378 datapath_destroyed = true;
1382 /* (Re)configure if necessary. */
1383 database_changed = ovsdb_idl_run(idl);
1384 cfg = ovsrec_open_vswitch_first(idl);
1386 /* Re-configure SSL. We do this on every trip through the main loop,
1387 * instead of just when the database changes, because the contents of the
1388 * key and certificate files can change without the database changing.
1390 * We do this before bridge_reconfigure() because that function might
1391 * initiate SSL connections and thus requires SSL to be configured. */
1392 if (cfg && cfg->ssl) {
1393 const struct ovsrec_ssl *ssl = cfg->ssl;
1395 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1396 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1399 if (database_changed || datapath_destroyed) {
1401 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1403 bridge_configure_once(cfg);
1404 bridge_reconfigure(cfg);
1406 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1407 ovsdb_idl_txn_commit(txn);
1408 ovsdb_idl_txn_destroy(txn); /* XXX */
1410 /* We still need to reconfigure to avoid dangling pointers to
1411 * now-destroyed ovsrec structures inside bridge data. */
1412 static const struct ovsrec_open_vswitch null_cfg;
1414 bridge_reconfigure(&null_cfg);
1418 /* Refresh system and interface stats if necessary. */
1419 if (time_msec() >= stats_timer) {
1421 struct ovsdb_idl_txn *txn;
1423 txn = ovsdb_idl_txn_create(idl);
1424 LIST_FOR_EACH (br, node, &all_bridges) {
1427 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1428 struct iface *iface;
1430 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1431 iface_refresh_stats(iface);
1432 iface_refresh_status(iface);
1435 bridge_refresh_controller_status(br);
1437 refresh_system_stats(cfg);
1438 ovsdb_idl_txn_commit(txn);
1439 ovsdb_idl_txn_destroy(txn); /* XXX */
1442 stats_timer = time_msec() + STATS_INTERVAL;
1445 if (time_msec() >= db_limiter) {
1446 struct ovsdb_idl_txn *txn;
1447 bool changed = false;
1449 txn = ovsdb_idl_txn_create(idl);
1450 LIST_FOR_EACH (br, node, &all_bridges) {
1453 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1454 struct iface *iface;
1456 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1457 changed = iface_refresh_cfm_stats(iface) || changed;
1458 changed = iface_refresh_lacp_stats(iface) || changed;
1464 db_limiter = time_msec() + DB_LIMIT_INTERVAL;
1467 ovsdb_idl_txn_commit(txn);
1468 ovsdb_idl_txn_destroy(txn);
1477 LIST_FOR_EACH (br, node, &all_bridges) {
1480 ofproto_wait(br->ofproto);
1481 mac_learning_wait(br->ml);
1482 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1486 ovsdb_idl_wait(idl);
1487 poll_timer_wait_until(stats_timer);
1489 if (db_limiter > time_msec()) {
1490 poll_timer_wait_until(db_limiter);
1494 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1495 * configuration changes. */
1497 bridge_flush(struct bridge *br)
1499 COVERAGE_INC(bridge_flush);
1503 /* Bridge unixctl user interface functions. */
1505 bridge_unixctl_fdb_show(struct unixctl_conn *conn,
1506 const char *args, void *aux OVS_UNUSED)
1508 struct ds ds = DS_EMPTY_INITIALIZER;
1509 const struct bridge *br;
1510 const struct mac_entry *e;
1512 br = bridge_lookup(args);
1514 unixctl_command_reply(conn, 501, "no such bridge");
1518 ds_put_cstr(&ds, " port VLAN MAC Age\n");
1519 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
1520 struct port *port = e->port.p;
1521 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1522 port_get_an_iface(port)->dp_ifidx,
1523 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
1525 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1529 /* CFM unixctl user interface functions. */
1531 cfm_unixctl_show(struct unixctl_conn *conn,
1532 const char *args, void *aux OVS_UNUSED)
1534 struct ds ds = DS_EMPTY_INITIALIZER;
1535 struct iface *iface;
1536 const struct cfm *cfm;
1538 iface = iface_find(args);
1540 unixctl_command_reply(conn, 501, "no such interface");
1544 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1547 unixctl_command_reply(conn, 501, "CFM not enabled");
1551 cfm_dump_ds(cfm, &ds);
1552 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1556 /* QoS unixctl user interface functions. */
1558 struct qos_unixctl_show_cbdata {
1560 struct iface *iface;
1564 qos_unixctl_show_cb(unsigned int queue_id,
1565 const struct shash *details,
1568 struct qos_unixctl_show_cbdata *data = aux;
1569 struct ds *ds = data->ds;
1570 struct iface *iface = data->iface;
1571 struct netdev_queue_stats stats;
1572 struct shash_node *node;
1575 ds_put_cstr(ds, "\n");
1577 ds_put_format(ds, "Queue %u:\n", queue_id);
1579 ds_put_cstr(ds, "Default:\n");
1582 SHASH_FOR_EACH (node, details) {
1583 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1586 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1588 if (stats.tx_packets != UINT64_MAX) {
1589 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1592 if (stats.tx_bytes != UINT64_MAX) {
1593 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1596 if (stats.tx_errors != UINT64_MAX) {
1597 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1600 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1601 queue_id, strerror(error));
1606 qos_unixctl_show(struct unixctl_conn *conn,
1607 const char *args, void *aux OVS_UNUSED)
1609 struct ds ds = DS_EMPTY_INITIALIZER;
1610 struct shash sh = SHASH_INITIALIZER(&sh);
1611 struct iface *iface;
1613 struct shash_node *node;
1614 struct qos_unixctl_show_cbdata data;
1617 iface = iface_find(args);
1619 unixctl_command_reply(conn, 501, "no such interface");
1623 netdev_get_qos(iface->netdev, &type, &sh);
1625 if (*type != '\0') {
1626 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1628 SHASH_FOR_EACH (node, &sh) {
1629 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1634 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1637 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1639 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1641 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1642 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1645 shash_destroy_free_data(&sh);
1649 /* Bridge reconfiguration functions. */
1650 static struct bridge *
1651 bridge_create(const struct ovsrec_bridge *br_cfg)
1656 assert(!bridge_lookup(br_cfg->name));
1657 br = xzalloc(sizeof *br);
1659 error = dpif_create_and_open(br_cfg->name, br_cfg->datapath_type,
1666 error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
1669 VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
1671 dpif_delete(br->dpif);
1672 dpif_close(br->dpif);
1677 br->name = xstrdup(br_cfg->name);
1679 br->ml = mac_learning_create();
1680 eth_addr_nicira_random(br->default_ea);
1682 hmap_init(&br->ports);
1683 hmap_init(&br->ifaces);
1684 shash_init(&br->iface_by_name);
1688 list_push_back(&all_bridges, &br->node);
1690 VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
1696 bridge_destroy(struct bridge *br)
1699 struct port *port, *next;
1702 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1705 list_remove(&br->node);
1706 ofproto_destroy(br->ofproto);
1707 error = dpif_delete(br->dpif);
1708 if (error && error != ENOENT) {
1709 VLOG_ERR("failed to delete %s: %s",
1710 dpif_name(br->dpif), strerror(error));
1712 dpif_close(br->dpif);
1713 mac_learning_destroy(br->ml);
1714 hmap_destroy(&br->ifaces);
1715 hmap_destroy(&br->ports);
1716 shash_destroy(&br->iface_by_name);
1722 static struct bridge *
1723 bridge_lookup(const char *name)
1727 LIST_FOR_EACH (br, node, &all_bridges) {
1728 if (!strcmp(br->name, name)) {
1735 /* Handle requests for a listing of all flows known by the OpenFlow
1736 * stack, including those normally hidden. */
1738 bridge_unixctl_dump_flows(struct unixctl_conn *conn,
1739 const char *args, void *aux OVS_UNUSED)
1744 br = bridge_lookup(args);
1746 unixctl_command_reply(conn, 501, "Unknown bridge");
1751 ofproto_get_all_flows(br->ofproto, &results);
1753 unixctl_command_reply(conn, 200, ds_cstr(&results));
1754 ds_destroy(&results);
1757 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1758 * connections and reconnect. If BRIDGE is not specified, then all bridges
1759 * drop their controller connections and reconnect. */
1761 bridge_unixctl_reconnect(struct unixctl_conn *conn,
1762 const char *args, void *aux OVS_UNUSED)
1765 if (args[0] != '\0') {
1766 br = bridge_lookup(args);
1768 unixctl_command_reply(conn, 501, "Unknown bridge");
1771 ofproto_reconnect_controllers(br->ofproto);
1773 LIST_FOR_EACH (br, node, &all_bridges) {
1774 ofproto_reconnect_controllers(br->ofproto);
1777 unixctl_command_reply(conn, 200, NULL);
1781 bridge_run_one(struct bridge *br)
1786 error = ofproto_run1(br->ofproto);
1791 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
1793 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1797 error = ofproto_run2(br->ofproto, br->flush);
1804 bridge_get_controllers(const struct bridge *br,
1805 struct ovsrec_controller ***controllersp)
1807 struct ovsrec_controller **controllers;
1808 size_t n_controllers;
1810 controllers = br->cfg->controller;
1811 n_controllers = br->cfg->n_controller;
1813 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1819 *controllersp = controllers;
1821 return n_controllers;
1825 bridge_reconfigure_one(struct bridge *br)
1827 enum ofproto_fail_mode fail_mode;
1828 struct port *port, *next;
1829 struct shash_node *node;
1830 struct shash new_ports;
1833 /* Collect new ports. */
1834 shash_init(&new_ports);
1835 for (i = 0; i < br->cfg->n_ports; i++) {
1836 const char *name = br->cfg->ports[i]->name;
1837 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1838 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1843 /* If we have a controller, then we need a local port. Complain if the
1844 * user didn't specify one.
1846 * XXX perhaps we should synthesize a port ourselves in this case. */
1847 if (bridge_get_controllers(br, NULL)) {
1848 char local_name[IF_NAMESIZE];
1851 error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
1852 local_name, sizeof local_name);
1853 if (!error && !shash_find(&new_ports, local_name)) {
1854 VLOG_WARN("bridge %s: controller specified but no local port "
1855 "(port named %s) defined",
1856 br->name, local_name);
1860 /* Get rid of deleted ports.
1861 * Get rid of deleted interfaces on ports that still exist. */
1862 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1863 const struct ovsrec_port *port_cfg;
1865 port_cfg = shash_find_data(&new_ports, port->name);
1869 port_del_ifaces(port, port_cfg);
1873 /* Create new ports.
1874 * Add new interfaces to existing ports.
1875 * Reconfigure existing ports. */
1876 SHASH_FOR_EACH (node, &new_ports) {
1877 struct port *port = port_lookup(br, node->name);
1879 port = port_create(br, node->name);
1882 port_reconfigure(port, node->data);
1883 if (list_is_empty(&port->ifaces)) {
1884 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1885 br->name, port->name);
1889 shash_destroy(&new_ports);
1891 /* Set the fail-mode */
1892 fail_mode = !br->cfg->fail_mode
1893 || !strcmp(br->cfg->fail_mode, "standalone")
1894 ? OFPROTO_FAIL_STANDALONE
1895 : OFPROTO_FAIL_SECURE;
1896 if (ofproto_get_fail_mode(br->ofproto) != fail_mode
1897 && !ofproto_has_primary_controller(br->ofproto)) {
1898 ofproto_flush_flows(br->ofproto);
1900 ofproto_set_fail_mode(br->ofproto, fail_mode);
1902 /* Delete all flows if we're switching from connected to standalone or vice
1903 * versa. (XXX Should we delete all flows if we are switching from one
1904 * controller to another?) */
1906 /* Configure OpenFlow controller connection snooping. */
1907 if (!ofproto_has_snoops(br->ofproto)) {
1911 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
1912 ovs_rundir(), br->name));
1913 ofproto_set_snoops(br->ofproto, &snoops);
1914 sset_destroy(&snoops);
1917 mirror_reconfigure(br);
1920 /* Initializes 'oc' appropriately as a management service controller for
1923 * The caller must free oc->target when it is no longer needed. */
1925 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1926 struct ofproto_controller *oc)
1928 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
1929 oc->max_backoff = 0;
1930 oc->probe_interval = 60;
1931 oc->band = OFPROTO_OUT_OF_BAND;
1933 oc->burst_limit = 0;
1936 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1938 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1939 struct ofproto_controller *oc)
1941 oc->target = c->target;
1942 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1943 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1944 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1945 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
1946 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1947 oc->burst_limit = (c->controller_burst_limit
1948 ? *c->controller_burst_limit : 0);
1951 /* Configures the IP stack for 'br''s local interface properly according to the
1952 * configuration in 'c'. */
1954 bridge_configure_local_iface_netdev(struct bridge *br,
1955 struct ovsrec_controller *c)
1957 struct netdev *netdev;
1958 struct in_addr mask, gateway;
1960 struct iface *local_iface;
1963 /* If there's no local interface or no IP address, give up. */
1964 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
1965 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1969 /* Bring up the local interface. */
1970 netdev = local_iface->netdev;
1971 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1973 /* Configure the IP address and netmask. */
1974 if (!c->local_netmask
1975 || !inet_aton(c->local_netmask, &mask)
1977 mask.s_addr = guess_netmask(ip.s_addr);
1979 if (!netdev_set_in4(netdev, ip, mask)) {
1980 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1981 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1984 /* Configure the default gateway. */
1985 if (c->local_gateway
1986 && inet_aton(c->local_gateway, &gateway)
1987 && gateway.s_addr) {
1988 if (!netdev_add_router(netdev, gateway)) {
1989 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1990 br->name, IP_ARGS(&gateway.s_addr));
1996 bridge_reconfigure_remotes(struct bridge *br,
1997 const struct sockaddr_in *managers,
2000 const char *disable_ib_str, *queue_id_str;
2001 bool disable_in_band = false;
2004 struct ovsrec_controller **controllers;
2005 size_t n_controllers;
2008 struct ofproto_controller *ocs;
2012 /* Check if we should disable in-band control on this bridge. */
2013 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
2014 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
2015 disable_in_band = true;
2018 /* Set OpenFlow queue ID for in-band control. */
2019 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
2020 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
2021 ofproto_set_in_band_queue(br->ofproto, queue_id);
2023 if (disable_in_band) {
2024 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
2026 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
2028 had_primary = ofproto_has_primary_controller(br->ofproto);
2030 n_controllers = bridge_get_controllers(br, &controllers);
2032 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
2035 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
2036 for (i = 0; i < n_controllers; i++) {
2037 struct ovsrec_controller *c = controllers[i];
2039 if (!strncmp(c->target, "punix:", 6)
2040 || !strncmp(c->target, "unix:", 5)) {
2041 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2043 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
2044 * domain sockets and overwriting arbitrary local files. */
2045 VLOG_ERR_RL(&rl, "%s: not adding Unix domain socket controller "
2046 "\"%s\" due to possibility for remote exploit",
2047 dpif_name(br->dpif), c->target);
2051 bridge_configure_local_iface_netdev(br, c);
2052 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
2053 if (disable_in_band) {
2054 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
2059 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
2060 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
2063 if (had_primary != ofproto_has_primary_controller(br->ofproto)) {
2064 ofproto_flush_flows(br->ofproto);
2067 /* If there are no controllers and the bridge is in standalone
2068 * mode, set up a flow that matches every packet and directs
2069 * them to OFPP_NORMAL (which goes to us). Otherwise, the
2070 * switch is in secure mode and we won't pass any traffic until
2071 * a controller has been defined and it tells us to do so. */
2073 && ofproto_get_fail_mode(br->ofproto) == OFPROTO_FAIL_STANDALONE) {
2074 union ofp_action action;
2075 struct cls_rule rule;
2077 memset(&action, 0, sizeof action);
2078 action.type = htons(OFPAT_OUTPUT);
2079 action.output.len = htons(sizeof action);
2080 action.output.port = htons(OFPP_NORMAL);
2081 cls_rule_init_catchall(&rule, 0);
2082 ofproto_add_flow(br->ofproto, &rule, &action, 1);
2087 bridge_get_all_ifaces(const struct bridge *br, struct shash *ifaces)
2092 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2093 struct iface *iface;
2095 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2096 shash_add_once(ifaces, iface->name, iface);
2098 if (!list_is_short(&port->ifaces) && port->cfg->bond_fake_iface) {
2099 shash_add_once(ifaces, port->name, NULL);
2104 /* For robustness, in case the administrator moves around datapath ports behind
2105 * our back, we re-check all the datapath port numbers here.
2107 * This function will set the 'dp_ifidx' members of interfaces that have
2108 * disappeared to -1, so only call this function from a context where those
2109 * 'struct iface's will be removed from the bridge. Otherwise, the -1
2110 * 'dp_ifidx'es will cause trouble later when we try to send them to the
2111 * datapath, which doesn't support UINT16_MAX+1 ports. */
2113 bridge_fetch_dp_ifaces(struct bridge *br)
2115 struct dpif_port_dump dump;
2116 struct dpif_port dpif_port;
2119 /* Reset all interface numbers. */
2120 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2121 struct iface *iface;
2123 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2124 iface->dp_ifidx = -1;
2127 hmap_clear(&br->ifaces);
2129 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
2130 struct iface *iface = iface_lookup(br, dpif_port.name);
2132 if (iface->dp_ifidx >= 0) {
2133 VLOG_WARN("%s reported interface %s twice",
2134 dpif_name(br->dpif), dpif_port.name);
2135 } else if (iface_from_dp_ifidx(br, dpif_port.port_no)) {
2136 VLOG_WARN("%s reported interface %"PRIu16" twice",
2137 dpif_name(br->dpif), dpif_port.port_no);
2139 iface->dp_ifidx = dpif_port.port_no;
2140 hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
2141 hash_int(iface->dp_ifidx, 0));
2144 iface_set_ofport(iface->cfg,
2145 (iface->dp_ifidx >= 0
2146 ? odp_port_to_ofp_port(iface->dp_ifidx)
2152 /* Bridge packet processing functions. */
2155 set_dst(struct dst *dst, const struct flow *flow,
2156 const struct port *in_port, const struct port *out_port,
2159 dst->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
2160 : in_port->vlan >= 0 ? in_port->vlan
2161 : flow->vlan_tci == 0 ? OFP_VLAN_NONE
2162 : vlan_tci_to_vid(flow->vlan_tci));
2164 dst->iface = (!out_port->bond
2165 ? port_get_an_iface(out_port)
2166 : bond_choose_output_slave(out_port->bond, flow,
2169 return dst->iface != NULL;
2173 mirror_mask_ffs(mirror_mask_t mask)
2175 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2180 dst_set_init(struct dst_set *set)
2182 set->dsts = set->builtin;
2184 set->allocated = ARRAY_SIZE(set->builtin);
2188 dst_set_add(struct dst_set *set, const struct dst *dst)
2190 if (set->n >= set->allocated) {
2191 size_t new_allocated;
2192 struct dst *new_dsts;
2194 new_allocated = set->allocated * 2;
2195 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2196 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2200 set->dsts = new_dsts;
2201 set->allocated = new_allocated;
2203 set->dsts[set->n++] = *dst;
2207 dst_set_free(struct dst_set *set)
2209 if (set->dsts != set->builtin) {
2215 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
2218 for (i = 0; i < set->n; i++) {
2219 if (set->dsts[i].vlan == test->vlan
2220 && set->dsts[i].iface == test->iface) {
2228 port_trunks_vlan(const struct port *port, uint16_t vlan)
2230 return (port->vlan < 0
2231 && (!port->trunks || bitmap_is_set(port->trunks, vlan)));
2235 port_includes_vlan(const struct port *port, uint16_t vlan)
2237 return vlan == port->vlan || port_trunks_vlan(port, vlan);
2241 port_is_floodable(const struct port *port)
2243 struct iface *iface;
2245 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2246 if (!ofproto_port_is_floodable(port->bridge->ofproto,
2254 /* Returns an arbitrary interface within 'port'. */
2255 static struct iface *
2256 port_get_an_iface(const struct port *port)
2258 return CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
2262 compose_dsts(const struct bridge *br, const struct flow *flow, uint16_t vlan,
2263 const struct port *in_port, const struct port *out_port,
2264 struct dst_set *set, tag_type *tags, uint16_t *nf_output_iface)
2268 if (out_port == FLOOD_PORT) {
2271 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2273 && port_is_floodable(port)
2274 && port_includes_vlan(port, vlan)
2275 && !port->is_mirror_output_port
2276 && set_dst(&dst, flow, in_port, port, tags)) {
2277 dst_set_add(set, &dst);
2280 *nf_output_iface = NF_OUT_FLOOD;
2281 } else if (out_port && set_dst(&dst, flow, in_port, out_port, tags)) {
2282 dst_set_add(set, &dst);
2283 *nf_output_iface = dst.iface->dp_ifidx;
2288 compose_mirror_dsts(const struct bridge *br, const struct flow *flow,
2289 uint16_t vlan, const struct port *in_port,
2290 struct dst_set *set, tag_type *tags)
2292 mirror_mask_t mirrors;
2296 mirrors = in_port->src_mirrors;
2297 for (i = 0; i < set->n; i++) {
2298 mirrors |= set->dsts[i].iface->port->dst_mirrors;
2305 flow_vlan = vlan_tci_to_vid(flow->vlan_tci);
2306 if (flow_vlan == 0) {
2307 flow_vlan = OFP_VLAN_NONE;
2311 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
2312 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
2316 if (set_dst(&dst, flow, in_port, m->out_port, tags)
2317 && !dst_is_duplicate(set, &dst)) {
2318 dst_set_add(set, &dst);
2323 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2324 if (port_includes_vlan(port, m->out_vlan)
2325 && set_dst(&dst, flow, in_port, port, tags))
2327 if (port->vlan < 0) {
2328 dst.vlan = m->out_vlan;
2330 if (dst_is_duplicate(set, &dst)) {
2334 /* Use the vlan tag on the original flow instead of
2335 * the one passed in the vlan parameter. This ensures
2336 * that we compare the vlan from before any implicit
2337 * tagging tags place. This is necessary because
2338 * dst->vlan is the final vlan, after removing implicit
2340 if (port == in_port && dst.vlan == flow_vlan) {
2341 /* Don't send out input port on same VLAN. */
2344 dst_set_add(set, &dst);
2349 mirrors &= mirrors - 1;
2354 compose_actions(struct bridge *br, const struct flow *flow, uint16_t vlan,
2355 const struct port *in_port, const struct port *out_port,
2356 tag_type *tags, struct ofpbuf *actions,
2357 uint16_t *nf_output_iface)
2359 uint16_t initial_vlan, cur_vlan;
2360 const struct dst *dst;
2364 compose_dsts(br, flow, vlan, in_port, out_port, &set, tags,
2366 compose_mirror_dsts(br, flow, vlan, in_port, &set, tags);
2368 /* Output all the packets we can without having to change the VLAN. */
2369 initial_vlan = vlan_tci_to_vid(flow->vlan_tci);
2370 if (initial_vlan == 0) {
2371 initial_vlan = OFP_VLAN_NONE;
2373 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2374 if (dst->vlan != initial_vlan) {
2377 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
2380 /* Then output the rest. */
2381 cur_vlan = initial_vlan;
2382 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2383 if (dst->vlan == initial_vlan) {
2386 if (dst->vlan != cur_vlan) {
2387 if (dst->vlan == OFP_VLAN_NONE) {
2388 nl_msg_put_flag(actions, ODP_ACTION_ATTR_STRIP_VLAN);
2391 tci = htons(dst->vlan & VLAN_VID_MASK);
2392 tci |= flow->vlan_tci & htons(VLAN_PCP_MASK);
2393 nl_msg_put_be16(actions, ODP_ACTION_ATTR_SET_DL_TCI, tci);
2395 cur_vlan = dst->vlan;
2397 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
2403 /* Returns the effective vlan of a packet, taking into account both the
2404 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2405 * the packet is untagged and -1 indicates it has an invalid header and
2406 * should be dropped. */
2407 static int flow_get_vlan(struct bridge *br, const struct flow *flow,
2408 struct port *in_port, bool have_packet)
2410 int vlan = vlan_tci_to_vid(flow->vlan_tci);
2411 if (in_port->vlan >= 0) {
2414 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2415 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2416 "packet received on port %s configured with "
2417 "implicit VLAN %"PRIu16,
2418 br->name, vlan, in_port->name, in_port->vlan);
2422 vlan = in_port->vlan;
2424 if (!port_includes_vlan(in_port, vlan)) {
2426 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2427 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2428 "packet received on port %s not configured for "
2430 br->name, vlan, in_port->name, vlan);
2439 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2440 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2441 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2443 is_gratuitous_arp(const struct flow *flow)
2445 return (flow->dl_type == htons(ETH_TYPE_ARP)
2446 && eth_addr_is_broadcast(flow->dl_dst)
2447 && (flow->nw_proto == ARP_OP_REPLY
2448 || (flow->nw_proto == ARP_OP_REQUEST
2449 && flow->nw_src == flow->nw_dst)));
2453 update_learning_table(struct bridge *br, const struct flow *flow, int vlan,
2454 struct port *in_port)
2456 struct mac_entry *mac;
2458 if (!mac_learning_may_learn(br->ml, flow->dl_src, vlan)) {
2462 mac = mac_learning_insert(br->ml, flow->dl_src, vlan);
2463 if (is_gratuitous_arp(flow)) {
2464 /* We don't want to learn from gratuitous ARP packets that are
2465 * reflected back over bond slaves so we lock the learning table. */
2466 if (!in_port->bond) {
2467 mac_entry_set_grat_arp_lock(mac);
2468 } else if (mac_entry_is_grat_arp_locked(mac)) {
2473 if (mac_entry_is_new(mac) || mac->port.p != in_port) {
2474 /* The log messages here could actually be useful in debugging,
2475 * so keep the rate limit relatively high. */
2476 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2477 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2478 "on port %s in VLAN %d",
2479 br->name, ETH_ADDR_ARGS(flow->dl_src),
2480 in_port->name, vlan);
2482 mac->port.p = in_port;
2483 ofproto_revalidate(br->ofproto, mac_learning_changed(br->ml, mac));
2487 /* Determines whether packets in 'flow' within 'br' should be forwarded or
2488 * dropped. Returns true if they may be forwarded, false if they should be
2491 * If 'have_packet' is true, it indicates that the caller is processing a
2492 * received packet. If 'have_packet' is false, then the caller is just
2493 * revalidating an existing flow because configuration has changed. Either
2494 * way, 'have_packet' only affects logging (there is no point in logging errors
2495 * during revalidation).
2497 * Sets '*in_portp' to the input port. This will be a null pointer if
2498 * flow->in_port does not designate a known input port (in which case
2499 * is_admissible() returns false).
2501 * When returning true, sets '*vlanp' to the effective VLAN of the input
2502 * packet, as returned by flow_get_vlan().
2504 * May also add tags to '*tags', although the current implementation only does
2505 * so in one special case.
2508 is_admissible(struct bridge *br, const struct flow *flow, bool have_packet,
2509 tag_type *tags, int *vlanp, struct port **in_portp)
2511 struct iface *in_iface;
2512 struct port *in_port;
2515 /* Find the interface and port structure for the received packet. */
2516 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2518 /* No interface? Something fishy... */
2520 /* Odd. A few possible reasons here:
2522 * - We deleted an interface but there are still a few packets
2523 * queued up from it.
2525 * - Someone externally added an interface (e.g. with "ovs-dpctl
2526 * add-if") that we don't know about.
2528 * - Packet arrived on the local port but the local port is not
2529 * one of our bridge ports.
2531 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2533 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
2534 "interface %"PRIu16, br->name, flow->in_port);
2540 *in_portp = in_port = in_iface->port;
2541 *vlanp = vlan = flow_get_vlan(br, flow, in_port, have_packet);
2546 /* Drop frames for reserved multicast addresses. */
2547 if (eth_addr_is_reserved(flow->dl_dst)) {
2551 /* Drop frames on ports reserved for mirroring. */
2552 if (in_port->is_mirror_output_port) {
2554 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2555 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2556 "%s, which is reserved exclusively for mirroring",
2557 br->name, in_port->name);
2562 if (in_port->bond) {
2563 struct mac_entry *mac;
2565 switch (bond_check_admissibility(in_port->bond, in_iface,
2566 flow->dl_dst, tags)) {
2573 case BV_DROP_IF_MOVED:
2574 mac = mac_learning_lookup(br->ml, flow->dl_src, vlan, NULL);
2575 if (mac && mac->port.p != in_port &&
2576 (!is_gratuitous_arp(flow)
2577 || mac_entry_is_grat_arp_locked(mac))) {
2587 /* If the composed actions may be applied to any packet in the given 'flow',
2588 * returns true. Otherwise, the actions should only be applied to 'packet', or
2589 * not at all, if 'packet' was NULL. */
2591 process_flow(struct bridge *br, const struct flow *flow,
2592 const struct ofpbuf *packet, struct ofpbuf *actions,
2593 tag_type *tags, uint16_t *nf_output_iface)
2595 struct port *in_port;
2596 struct port *out_port;
2597 struct mac_entry *mac;
2600 /* Check whether we should drop packets in this flow. */
2601 if (!is_admissible(br, flow, packet != NULL, tags, &vlan, &in_port)) {
2606 /* Learn source MAC (but don't try to learn from revalidation). */
2608 update_learning_table(br, flow, vlan, in_port);
2611 /* Determine output port. */
2612 mac = mac_learning_lookup(br->ml, flow->dl_dst, vlan, tags);
2614 out_port = mac->port.p;
2615 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
2616 /* If we are revalidating but don't have a learning entry then
2617 * eject the flow. Installing a flow that floods packets opens
2618 * up a window of time where we could learn from a packet reflected
2619 * on a bond and blackhole packets before the learning table is
2620 * updated to reflect the correct port. */
2623 out_port = FLOOD_PORT;
2626 /* Don't send packets out their input ports. */
2627 if (in_port == out_port) {
2633 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2641 bridge_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
2642 struct ofpbuf *actions, tag_type *tags,
2643 uint16_t *nf_output_iface, void *br_)
2645 struct bridge *br = br_;
2647 COVERAGE_INC(bridge_process_flow);
2648 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2652 bridge_special_ofhook_cb(const struct flow *flow,
2653 const struct ofpbuf *packet, void *br_)
2655 struct iface *iface;
2656 struct bridge *br = br_;
2658 iface = iface_from_dp_ifidx(br, flow->in_port);
2660 if (flow->dl_type == htons(ETH_TYPE_LACP)) {
2661 if (iface && iface->port->lacp && packet) {
2662 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
2664 lacp_process_pdu(iface->port->lacp, iface, pdu);
2674 bridge_account_flow_ofhook_cb(const struct flow *flow, tag_type tags,
2675 const struct nlattr *actions,
2677 uint64_t n_bytes, void *br_)
2679 struct bridge *br = br_;
2680 const struct nlattr *a;
2681 struct port *in_port;
2686 /* Feed information from the active flows back into the learning table to
2687 * ensure that table is always in sync with what is actually flowing
2688 * through the datapath.
2690 * We test that 'tags' is nonzero to ensure that only flows that include an
2691 * OFPP_NORMAL action are used for learning. This works because
2692 * bridge_normal_ofhook_cb() always sets a nonzero tag value. */
2693 if (tags && is_admissible(br, flow, false, &dummy, &vlan, &in_port)) {
2694 update_learning_table(br, flow, vlan, in_port);
2697 /* Account for bond slave utilization. */
2698 if (!br->has_bonded_ports) {
2701 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
2702 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
2703 struct port *out_port = port_from_dp_ifidx(br, nl_attr_get_u32(a));
2704 if (out_port && out_port->bond) {
2705 uint16_t vlan = (flow->vlan_tci
2706 ? vlan_tci_to_vid(flow->vlan_tci)
2708 bond_account(out_port->bond, flow, vlan, n_bytes);
2715 bridge_account_checkpoint_ofhook_cb(void *br_)
2717 struct bridge *br = br_;
2720 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2722 bond_rebalance(port->bond,
2723 ofproto_get_revalidate_set(br->ofproto));
2729 bridge_autopath_ofhook_cb(const struct flow *flow, uint32_t ofp_port,
2730 tag_type *tags, void *br_)
2732 struct bridge *br = br_;
2733 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2734 struct port *port = port_from_dp_ifidx(br, odp_port);
2739 } else if (list_is_short(&port->ifaces)) {
2742 struct iface *iface;
2744 /* Autopath does not support VLAN hashing. */
2745 iface = bond_choose_output_slave(port->bond, flow,
2746 OFP_VLAN_NONE, tags);
2747 ret = iface ? iface->dp_ifidx : ODPP_NONE;
2750 return odp_port_to_ofp_port(ret);
2753 static struct ofhooks bridge_ofhooks = {
2754 bridge_normal_ofhook_cb,
2755 bridge_special_ofhook_cb,
2756 bridge_account_flow_ofhook_cb,
2757 bridge_account_checkpoint_ofhook_cb,
2758 bridge_autopath_ofhook_cb,
2761 /* Port functions. */
2764 lacp_send_pdu_cb(void *iface_, const struct lacp_pdu *pdu)
2766 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2767 struct iface *iface = iface_;
2768 uint8_t ea[ETH_ADDR_LEN];
2771 error = netdev_get_etheraddr(iface->netdev, ea);
2773 struct lacp_pdu *packet_pdu;
2774 struct ofpbuf packet;
2776 ofpbuf_init(&packet, 0);
2777 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2778 sizeof *packet_pdu);
2780 error = netdev_send(iface->netdev, &packet);
2782 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
2783 "(%s)", iface->port->name, iface->name,
2786 ofpbuf_uninit(&packet);
2788 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2789 "%s (%s)", iface->port->name, iface->name,
2795 port_run(struct port *port)
2798 lacp_run(port->lacp, lacp_send_pdu_cb);
2802 struct iface *iface;
2804 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2805 bool may_enable = lacp_slave_may_enable(port->lacp, iface);
2806 bond_slave_set_lacp_may_enable(port->bond, iface, may_enable);
2809 bond_run(port->bond,
2810 ofproto_get_revalidate_set(port->bridge->ofproto),
2811 lacp_negotiated(port->lacp));
2812 if (bond_should_send_learning_packets(port->bond)) {
2813 port_send_learning_packets(port);
2819 port_wait(struct port *port)
2822 lacp_wait(port->lacp);
2826 bond_wait(port->bond);
2830 static struct port *
2831 port_create(struct bridge *br, const char *name)
2835 port = xzalloc(sizeof *port);
2838 port->trunks = NULL;
2839 port->name = xstrdup(name);
2840 list_init(&port->ifaces);
2842 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
2844 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2851 get_port_other_config(const struct ovsrec_port *port, const char *key,
2852 const char *default_value)
2856 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
2858 return value ? value : default_value;
2862 get_interface_other_config(const struct ovsrec_interface *iface,
2863 const char *key, const char *default_value)
2867 value = get_ovsrec_key_value(&iface->header_,
2868 &ovsrec_interface_col_other_config, key);
2869 return value ? value : default_value;
2873 port_del_ifaces(struct port *port, const struct ovsrec_port *cfg)
2875 struct iface *iface, *next;
2876 struct sset new_ifaces;
2879 /* Collect list of new interfaces. */
2880 sset_init(&new_ifaces);
2881 for (i = 0; i < cfg->n_interfaces; i++) {
2882 const char *name = cfg->interfaces[i]->name;
2883 sset_add(&new_ifaces, name);
2886 /* Get rid of deleted interfaces. */
2887 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2888 if (!sset_contains(&new_ifaces, iface->name)) {
2889 iface_destroy(iface);
2893 sset_destroy(&new_ifaces);
2896 /* Expires all MAC learning entries associated with 'port' and forces ofproto
2897 * to revalidate every flow. */
2899 port_flush_macs(struct port *port)
2901 struct bridge *br = port->bridge;
2902 struct mac_learning *ml = br->ml;
2903 struct mac_entry *mac, *next_mac;
2906 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2907 if (mac->port.p == port) {
2908 mac_learning_expire(ml, mac);
2914 port_reconfigure(struct port *port, const struct ovsrec_port *cfg)
2916 struct sset new_ifaces;
2917 bool need_flush = false;
2918 unsigned long *trunks;
2925 /* Add new interfaces and update 'cfg' member of existing ones. */
2926 sset_init(&new_ifaces);
2927 for (i = 0; i < cfg->n_interfaces; i++) {
2928 const struct ovsrec_interface *if_cfg = cfg->interfaces[i];
2929 struct iface *iface;
2931 if (!sset_add(&new_ifaces, if_cfg->name)) {
2932 VLOG_WARN("port %s: %s specified twice as port interface",
2933 port->name, if_cfg->name);
2934 iface_set_ofport(if_cfg, -1);
2938 iface = iface_lookup(port->bridge, if_cfg->name);
2940 if (iface->port != port) {
2941 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
2943 port->bridge->name, if_cfg->name, iface->port->name);
2946 iface->cfg = if_cfg;
2948 iface = iface_create(port, if_cfg);
2951 /* Determine interface type. The local port always has type
2952 * "internal". Other ports take their type from the database and
2953 * default to "system" if none is specified. */
2954 iface->type = (!strcmp(if_cfg->name, port->bridge->name) ? "internal"
2955 : if_cfg->type[0] ? if_cfg->type
2958 sset_destroy(&new_ifaces);
2963 if (list_is_short(&port->ifaces)) {
2965 if (vlan >= 0 && vlan <= 4095) {
2966 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
2971 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
2972 * they even work as-is. But they have not been tested. */
2973 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
2977 if (port->vlan != vlan) {
2982 /* Get trunked VLANs. */
2984 if (vlan < 0 && cfg->n_trunks) {
2987 trunks = bitmap_allocate(4096);
2989 for (i = 0; i < cfg->n_trunks; i++) {
2990 int trunk = cfg->trunks[i];
2992 bitmap_set1(trunks, trunk);
2998 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
2999 port->name, cfg->n_trunks);
3001 if (n_errors == cfg->n_trunks) {
3002 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
3004 bitmap_free(trunks);
3007 } else if (vlan >= 0 && cfg->n_trunks) {
3008 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
3012 ? port->trunks != NULL
3013 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
3016 bitmap_free(port->trunks);
3017 port->trunks = trunks;
3020 port_flush_macs(port);
3025 port_destroy(struct port *port)
3028 struct bridge *br = port->bridge;
3029 struct iface *iface, *next;
3032 for (i = 0; i < MAX_MIRRORS; i++) {
3033 struct mirror *m = br->mirrors[i];
3034 if (m && m->out_port == port) {
3039 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
3040 iface_destroy(iface);
3043 hmap_remove(&br->ports, &port->hmap_node);
3045 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
3047 bond_destroy(port->bond);
3048 lacp_destroy(port->lacp);
3049 port_flush_macs(port);
3051 bitmap_free(port->trunks);
3057 static struct port *
3058 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3060 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
3061 return iface ? iface->port : NULL;
3064 static struct port *
3065 port_lookup(const struct bridge *br, const char *name)
3069 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
3071 if (!strcmp(port->name, name)) {
3079 enable_lacp(struct port *port, bool *activep)
3081 if (!port->cfg->lacp) {
3082 /* XXX when LACP implementation has been sufficiently tested, enable by
3083 * default and make active on bonded ports. */
3085 } else if (!strcmp(port->cfg->lacp, "off")) {
3087 } else if (!strcmp(port->cfg->lacp, "active")) {
3090 } else if (!strcmp(port->cfg->lacp, "passive")) {
3094 VLOG_WARN("port %s: unknown LACP mode %s",
3095 port->name, port->cfg->lacp);
3101 iface_reconfigure_lacp(struct iface *iface)
3103 struct lacp_slave_settings s;
3106 s.name = iface->name;
3107 s.id = iface->dp_ifidx;
3108 priority = atoi(get_interface_other_config(
3109 iface->cfg, "lacp-port-priority", "0"));
3110 s.priority = (priority >= 0 && priority <= UINT16_MAX
3111 ? priority : UINT16_MAX);
3112 lacp_slave_register(iface->port->lacp, iface, &s);
3116 port_reconfigure_lacp(struct port *port)
3118 static struct lacp_settings s;
3119 struct iface *iface;
3122 if (!enable_lacp(port, &s.active)) {
3123 lacp_destroy(port->lacp);
3128 s.name = port->name;
3129 memcpy(s.id, port->bridge->ea, ETH_ADDR_LEN);
3131 /* Prefer bondable links if unspecified. */
3132 priority = atoi(get_port_other_config(port->cfg, "lacp-system-priority",
3134 s.priority = (priority > 0 && priority <= UINT16_MAX
3136 : UINT16_MAX - !list_is_short(&port->ifaces));
3138 s.fast = !strcmp(get_port_other_config(port->cfg, "lacp-time", "slow"),
3142 port->lacp = lacp_create();
3145 lacp_configure(port->lacp, &s);
3147 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3148 iface_reconfigure_lacp(iface);
3153 port_reconfigure_bond(struct port *port)
3155 struct bond_settings s;
3156 const char *detect_s;
3157 struct iface *iface;
3159 if (list_is_short(&port->ifaces)) {
3160 /* Not a bonded port. */
3161 bond_destroy(port->bond);
3166 port->bridge->has_bonded_ports = true;
3168 s.name = port->name;
3170 if (port->cfg->bond_mode
3171 && !bond_mode_from_string(&s.balance, port->cfg->bond_mode)) {
3172 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
3173 port->name, port->cfg->bond_mode,
3174 bond_mode_to_string(s.balance));
3177 s.detect = BLSM_CARRIER;
3178 detect_s = get_port_other_config(port->cfg, "bond-detect-mode", NULL);
3179 if (detect_s && !bond_detect_mode_from_string(&s.detect, detect_s)) {
3180 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
3182 port->name, detect_s, bond_detect_mode_to_string(s.detect));
3185 s.miimon_interval = atoi(
3186 get_port_other_config(port->cfg, "bond-miimon-interval", "200"));
3187 if (s.miimon_interval < 100) {
3188 s.miimon_interval = 100;
3191 s.up_delay = MAX(0, port->cfg->bond_updelay);
3192 s.down_delay = MAX(0, port->cfg->bond_downdelay);
3193 s.rebalance_interval = atoi(
3194 get_port_other_config(port->cfg, "bond-rebalance-interval", "10000"));
3195 if (s.rebalance_interval < 1000) {
3196 s.rebalance_interval = 1000;
3199 s.fake_iface = port->cfg->bond_fake_iface;
3202 port->bond = bond_create(&s);
3204 if (bond_reconfigure(port->bond, &s)) {
3205 bridge_flush(port->bridge);
3209 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3210 uint16_t stable_id = (port->lacp
3211 ? lacp_slave_get_port_id(port->lacp, iface)
3213 bond_slave_register(iface->port->bond, iface, stable_id,
3219 port_send_learning_packets(struct port *port)
3221 struct bridge *br = port->bridge;
3222 int error, n_packets, n_errors;
3223 struct mac_entry *e;
3225 error = n_packets = n_errors = 0;
3226 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
3227 if (e->port.p != port) {
3228 int ret = bond_send_learning_packet(port->bond, e->mac, e->vlan);
3238 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3239 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3240 "packets, last error was: %s",
3241 port->name, n_errors, n_packets, strerror(error));
3243 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3244 port->name, n_packets);
3248 /* Interface functions. */
3250 static struct iface *
3251 iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
3253 struct bridge *br = port->bridge;
3254 struct iface *iface;
3255 char *name = if_cfg->name;
3257 iface = xzalloc(sizeof *iface);
3259 iface->name = xstrdup(name);
3260 iface->dp_ifidx = -1;
3261 iface->tag = tag_create_random();
3262 iface->netdev = NULL;
3263 iface->cfg = if_cfg;
3265 shash_add_assert(&br->iface_by_name, iface->name, iface);
3267 list_push_back(&port->ifaces, &iface->port_elem);
3269 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3277 iface_destroy(struct iface *iface)
3280 struct port *port = iface->port;
3281 struct bridge *br = port->bridge;
3284 bond_slave_unregister(port->bond, iface);
3288 lacp_slave_unregister(port->lacp, iface);
3291 shash_find_and_delete_assert(&br->iface_by_name, iface->name);
3293 if (iface->dp_ifidx >= 0) {
3294 hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
3297 list_remove(&iface->port_elem);
3299 netdev_close(iface->netdev);
3304 bridge_flush(port->bridge);
3308 static struct iface *
3309 iface_lookup(const struct bridge *br, const char *name)
3311 return shash_find_data(&br->iface_by_name, name);
3314 static struct iface *
3315 iface_find(const char *name)
3317 const struct bridge *br;
3319 LIST_FOR_EACH (br, node, &all_bridges) {
3320 struct iface *iface = iface_lookup(br, name);
3329 static struct iface *
3330 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3332 struct iface *iface;
3334 HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
3335 hash_int(dp_ifidx, 0), &br->ifaces) {
3336 if (iface->dp_ifidx == dp_ifidx) {
3343 /* Set Ethernet address of 'iface', if one is specified in the configuration
3346 iface_set_mac(struct iface *iface)
3348 uint8_t ea[ETH_ADDR_LEN];
3350 if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
3351 if (eth_addr_is_multicast(ea)) {
3352 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3354 } else if (iface->dp_ifidx == ODPP_LOCAL) {
3355 VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
3356 iface->name, iface->name);
3358 int error = netdev_set_etheraddr(iface->netdev, ea);
3360 VLOG_ERR("interface %s: setting MAC failed (%s)",
3361 iface->name, strerror(error));
3367 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
3369 iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
3372 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
3376 /* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
3378 * The value strings in '*shash' are taken directly from values[], not copied,
3379 * so the caller should not modify or free them. */
3381 shash_from_ovs_idl_map(char **keys, char **values, size_t n,
3382 struct shash *shash)
3387 for (i = 0; i < n; i++) {
3388 shash_add(shash, keys[i], values[i]);
3392 /* Creates 'keys' and 'values' arrays from 'shash'.
3394 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
3395 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
3396 * are populated with with strings taken directly from 'shash' and thus have
3397 * the same ownership of the key-value pairs in shash.
3400 shash_to_ovs_idl_map(struct shash *shash,
3401 char ***keys, char ***values, size_t *n)
3405 struct shash_node *sn;
3407 count = shash_count(shash);
3409 k = xmalloc(count * sizeof *k);
3410 v = xmalloc(count * sizeof *v);
3413 SHASH_FOR_EACH(sn, shash) {
3424 struct iface_delete_queues_cbdata {
3425 struct netdev *netdev;
3426 const struct ovsdb_datum *queues;
3430 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
3432 union ovsdb_atom atom;
3434 atom.integer = target;
3435 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
3439 iface_delete_queues(unsigned int queue_id,
3440 const struct shash *details OVS_UNUSED, void *cbdata_)
3442 struct iface_delete_queues_cbdata *cbdata = cbdata_;
3444 if (!queue_ids_include(cbdata->queues, queue_id)) {
3445 netdev_delete_queue(cbdata->netdev, queue_id);
3450 iface_update_qos(struct iface *iface, const struct ovsrec_qos *qos)
3452 if (!qos || qos->type[0] == '\0') {
3453 netdev_set_qos(iface->netdev, NULL, NULL);
3455 struct iface_delete_queues_cbdata cbdata;
3456 struct shash details;
3459 /* Configure top-level Qos for 'iface'. */
3460 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
3461 qos->n_other_config, &details);
3462 netdev_set_qos(iface->netdev, qos->type, &details);
3463 shash_destroy(&details);
3465 /* Deconfigure queues that were deleted. */
3466 cbdata.netdev = iface->netdev;
3467 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
3469 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
3471 /* Configure queues for 'iface'. */
3472 for (i = 0; i < qos->n_queues; i++) {
3473 const struct ovsrec_queue *queue = qos->value_queues[i];
3474 unsigned int queue_id = qos->key_queues[i];
3476 shash_from_ovs_idl_map(queue->key_other_config,
3477 queue->value_other_config,
3478 queue->n_other_config, &details);
3479 netdev_set_queue(iface->netdev, queue_id, &details);
3480 shash_destroy(&details);
3486 iface_update_cfm(struct iface *iface)
3490 uint16_t *remote_mps;
3491 struct ovsrec_monitor *mon;
3492 uint8_t maid[CCM_MAID_LEN];
3494 mon = iface->cfg->monitor;
3497 ofproto_iface_clear_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
3501 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
3502 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
3506 cfm.mpid = mon->mpid;
3507 cfm.interval = mon->interval ? *mon->interval : 1000;
3509 memcpy(cfm.maid, maid, sizeof cfm.maid);
3511 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
3512 for(i = 0; i < mon->n_remote_mps; i++) {
3513 remote_mps[i] = mon->remote_mps[i]->mpid;
3516 ofproto_iface_set_cfm(iface->port->bridge->ofproto, iface->dp_ifidx,
3517 &cfm, remote_mps, mon->n_remote_mps);
3521 /* Read carrier or miimon status directly from 'iface''s netdev, according to
3522 * how 'iface''s port is configured.
3524 * Returns true if 'iface' is up, false otherwise. */
3526 iface_get_carrier(const struct iface *iface)
3529 return netdev_get_carrier(iface->netdev);
3532 /* Port mirroring. */
3534 static struct mirror *
3535 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
3539 for (i = 0; i < MAX_MIRRORS; i++) {
3540 struct mirror *m = br->mirrors[i];
3541 if (m && uuid_equals(uuid, &m->uuid)) {
3549 mirror_reconfigure(struct bridge *br)
3551 unsigned long *rspan_vlans;
3555 /* Get rid of deleted mirrors. */
3556 for (i = 0; i < MAX_MIRRORS; i++) {
3557 struct mirror *m = br->mirrors[i];
3559 const struct ovsdb_datum *mc;
3560 union ovsdb_atom atom;
3562 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
3563 atom.uuid = br->mirrors[i]->uuid;
3564 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3570 /* Add new mirrors and reconfigure existing ones. */
3571 for (i = 0; i < br->cfg->n_mirrors; i++) {
3572 struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
3573 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
3575 mirror_reconfigure_one(m, cfg);
3577 mirror_create(br, cfg);
3581 /* Update port reserved status. */
3582 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3583 port->is_mirror_output_port = false;
3585 for (i = 0; i < MAX_MIRRORS; i++) {
3586 struct mirror *m = br->mirrors[i];
3587 if (m && m->out_port) {
3588 m->out_port->is_mirror_output_port = true;
3592 /* Update flooded vlans (for RSPAN). */
3594 if (br->cfg->n_flood_vlans) {
3595 rspan_vlans = bitmap_allocate(4096);
3597 for (i = 0; i < br->cfg->n_flood_vlans; i++) {
3598 int64_t vlan = br->cfg->flood_vlans[i];
3599 if (vlan >= 0 && vlan < 4096) {
3600 bitmap_set1(rspan_vlans, vlan);
3601 VLOG_INFO("bridge %s: disabling learning on vlan %"PRId64,
3604 VLOG_ERR("bridge %s: invalid value %"PRId64 "for flood VLAN",
3609 if (mac_learning_set_flood_vlans(br->ml, rspan_vlans)) {
3611 mac_learning_flush(br->ml);
3616 mirror_create(struct bridge *br, struct ovsrec_mirror *cfg)
3621 for (i = 0; ; i++) {
3622 if (i >= MAX_MIRRORS) {
3623 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
3624 "cannot create %s", br->name, MAX_MIRRORS, cfg->name);
3627 if (!br->mirrors[i]) {
3632 VLOG_INFO("created port mirror %s on bridge %s", cfg->name, br->name);
3634 mac_learning_flush(br->ml);
3636 br->mirrors[i] = m = xzalloc(sizeof *m);
3639 m->name = xstrdup(cfg->name);
3640 sset_init(&m->src_ports);
3641 sset_init(&m->dst_ports);
3647 mirror_reconfigure_one(m, cfg);
3651 mirror_destroy(struct mirror *m)
3654 struct bridge *br = m->bridge;
3657 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3658 port->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3659 port->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3662 sset_destroy(&m->src_ports);
3663 sset_destroy(&m->dst_ports);
3666 m->bridge->mirrors[m->idx] = NULL;
3671 mac_learning_flush(br->ml);
3676 mirror_collect_ports(struct mirror *m, struct ovsrec_port **ports, int n_ports,
3681 for (i = 0; i < n_ports; i++) {
3682 const char *name = ports[i]->name;
3683 if (port_lookup(m->bridge, name)) {
3684 sset_add(names, name);
3686 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
3687 "port %s", m->bridge->name, m->name, name);
3693 mirror_collect_vlans(struct mirror *m, const struct ovsrec_mirror *cfg,
3699 *vlans = xmalloc(sizeof **vlans * cfg->n_select_vlan);
3701 for (i = 0; i < cfg->n_select_vlan; i++) {
3702 int64_t vlan = cfg->select_vlan[i];
3703 if (vlan < 0 || vlan > 4095) {
3704 VLOG_WARN("bridge %s: mirror %s selects invalid VLAN %"PRId64,
3705 m->bridge->name, m->name, vlan);
3707 (*vlans)[n_vlans++] = vlan;
3714 vlan_is_mirrored(const struct mirror *m, int vlan)
3718 for (i = 0; i < m->n_vlans; i++) {
3719 if (m->vlans[i] == vlan) {
3727 port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
3731 for (i = 0; i < m->n_vlans; i++) {
3732 if (port_trunks_vlan(p, m->vlans[i])) {
3740 mirror_reconfigure_one(struct mirror *m, struct ovsrec_mirror *cfg)
3742 struct sset src_ports, dst_ports;
3743 mirror_mask_t mirror_bit;
3744 struct port *out_port;
3751 if (strcmp(cfg->name, m->name)) {
3753 m->name = xstrdup(cfg->name);
3756 /* Get output port. */
3757 if (cfg->output_port) {
3758 out_port = port_lookup(m->bridge, cfg->output_port->name);
3760 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
3761 m->bridge->name, m->name);
3767 if (cfg->output_vlan) {
3768 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
3769 "output vlan; ignoring output vlan",
3770 m->bridge->name, m->name);
3772 } else if (cfg->output_vlan) {
3774 out_vlan = *cfg->output_vlan;
3776 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
3777 m->bridge->name, m->name);
3782 sset_init(&src_ports);
3783 sset_init(&dst_ports);
3784 if (cfg->select_all) {
3785 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3786 sset_add(&src_ports, port->name);
3787 sset_add(&dst_ports, port->name);
3792 /* Get ports, and drop duplicates and ports that don't exist. */
3793 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
3795 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
3798 /* Get all the vlans, and drop duplicate and invalid vlans. */
3799 n_vlans = mirror_collect_vlans(m, cfg, &vlans);
3802 /* Update mirror data. */
3803 if (!sset_equals(&m->src_ports, &src_ports)
3804 || !sset_equals(&m->dst_ports, &dst_ports)
3805 || m->n_vlans != n_vlans
3806 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
3807 || m->out_port != out_port
3808 || m->out_vlan != out_vlan) {
3809 bridge_flush(m->bridge);
3810 mac_learning_flush(m->bridge->ml);
3812 sset_swap(&m->src_ports, &src_ports);
3813 sset_swap(&m->dst_ports, &dst_ports);
3816 m->n_vlans = n_vlans;
3817 m->out_port = out_port;
3818 m->out_vlan = out_vlan;
3821 mirror_bit = MIRROR_MASK_C(1) << m->idx;
3822 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3823 if (sset_contains(&m->src_ports, port->name)
3826 ? port_trunks_any_mirrored_vlan(m, port)
3827 : vlan_is_mirrored(m, port->vlan)))) {
3828 port->src_mirrors |= mirror_bit;
3830 port->src_mirrors &= ~mirror_bit;
3833 if (sset_contains(&m->dst_ports, port->name)) {
3834 port->dst_mirrors |= mirror_bit;
3836 port->dst_mirrors &= ~mirror_bit;
3841 sset_destroy(&src_ports);
3842 sset_destroy(&dst_ports);