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. */
153 /* Port mirroring info. */
154 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
155 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
156 bool is_mirror_output_port; /* Does port mirroring send frames here? */
160 struct list node; /* Node in global list of bridges. */
161 char *name; /* User-specified arbitrary name. */
162 struct mac_learning *ml; /* MAC learning table. */
163 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
164 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
165 const struct ovsrec_bridge *cfg;
167 /* OpenFlow switch processing. */
168 struct ofproto *ofproto; /* OpenFlow switch. */
170 /* Kernel datapath information. */
171 struct dpif *dpif; /* Datapath. */
172 struct hmap ifaces; /* "struct iface"s indexed by dp_ifidx. */
175 struct hmap ports; /* "struct port"s indexed by name. */
176 struct shash iface_by_name; /* "struct iface"s indexed by name. */
179 bool has_bonded_ports;
184 /* Port mirroring. */
185 struct mirror *mirrors[MAX_MIRRORS];
188 /* List of all bridges. */
189 static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
191 /* OVSDB IDL used to obtain configuration. */
192 static struct ovsdb_idl *idl;
194 /* Each time this timer expires, the bridge fetches systems and interface
195 * statistics and pushes them into the database. */
196 #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
197 static long long int stats_timer = LLONG_MIN;
199 /* Stores the time after which CFM statistics may be written to the database.
200 * Only updated when changes to the database require rate limiting. */
201 #define CFM_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
202 static long long int cfm_limiter = LLONG_MIN;
204 static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
205 static void bridge_destroy(struct bridge *);
206 static struct bridge *bridge_lookup(const char *name);
207 static unixctl_cb_func bridge_unixctl_dump_flows;
208 static unixctl_cb_func bridge_unixctl_reconnect;
209 static int bridge_run_one(struct bridge *);
210 static size_t bridge_get_controllers(const struct bridge *br,
211 struct ovsrec_controller ***controllersp);
212 static void bridge_reconfigure_one(struct bridge *);
213 static void bridge_reconfigure_remotes(struct bridge *,
214 const struct sockaddr_in *managers,
216 static void bridge_get_all_ifaces(const struct bridge *, struct shash *ifaces);
217 static void bridge_fetch_dp_ifaces(struct bridge *);
218 static void bridge_flush(struct bridge *);
219 static void bridge_pick_local_hw_addr(struct bridge *,
220 uint8_t ea[ETH_ADDR_LEN],
221 struct iface **hw_addr_iface);
222 static uint64_t bridge_pick_datapath_id(struct bridge *,
223 const uint8_t bridge_ea[ETH_ADDR_LEN],
224 struct iface *hw_addr_iface);
225 static uint64_t dpid_from_hash(const void *, size_t nbytes);
227 static unixctl_cb_func bridge_unixctl_fdb_show;
228 static unixctl_cb_func cfm_unixctl_show;
229 static unixctl_cb_func qos_unixctl_show;
231 static void port_run(struct port *);
232 static void port_wait(struct port *);
233 static struct port *port_create(struct bridge *, const char *name);
234 static void port_reconfigure(struct port *, const struct ovsrec_port *);
235 static void port_del_ifaces(struct port *, const struct ovsrec_port *);
236 static void port_destroy(struct port *);
237 static struct port *port_lookup(const struct bridge *, const char *name);
238 static struct iface *port_get_an_iface(const struct port *);
239 static struct port *port_from_dp_ifidx(const struct bridge *,
241 static void port_reconfigure_bond(struct port *);
242 static void port_send_learning_packets(struct port *);
244 static void mirror_create(struct bridge *, struct ovsrec_mirror *);
245 static void mirror_destroy(struct mirror *);
246 static void mirror_reconfigure(struct bridge *);
247 static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
248 static bool vlan_is_mirrored(const struct mirror *, int vlan);
250 static struct iface *iface_create(struct port *port,
251 const struct ovsrec_interface *if_cfg);
252 static void iface_destroy(struct iface *);
253 static struct iface *iface_lookup(const struct bridge *, const char *name);
254 static struct iface *iface_find(const char *name);
255 static struct iface *iface_from_dp_ifidx(const struct bridge *,
257 static void iface_set_mac(struct iface *);
258 static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
259 static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
260 static void iface_update_cfm(struct iface *);
261 static bool iface_refresh_cfm_stats(struct iface *iface);
262 static bool iface_get_carrier(const struct iface *);
264 static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
266 static void shash_to_ovs_idl_map(struct shash *,
267 char ***keys, char ***values, size_t *n);
269 /* Hooks into ofproto processing. */
270 static struct ofhooks bridge_ofhooks;
272 /* Public functions. */
274 /* Initializes the bridge module, configuring it to obtain its configuration
275 * from an OVSDB server accessed over 'remote', which should be a string in a
276 * form acceptable to ovsdb_idl_create(). */
278 bridge_init(const char *remote)
280 /* Create connection to database. */
281 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
283 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
284 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
285 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
287 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
289 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
290 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
292 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
293 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
294 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
296 /* Register unixctl commands. */
297 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
298 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
299 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
300 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
302 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
310 struct bridge *br, *next_br;
312 LIST_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
315 ovsdb_idl_destroy(idl);
318 /* Performs configuration that is only necessary once at ovs-vswitchd startup,
319 * but for which the ovs-vswitchd configuration 'cfg' is required. */
321 bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
323 static bool already_configured_once;
324 struct sset bridge_names;
325 struct sset dpif_names, dpif_types;
329 /* Only do this once per ovs-vswitchd run. */
330 if (already_configured_once) {
333 already_configured_once = true;
335 stats_timer = time_msec() + STATS_INTERVAL;
337 /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
338 sset_init(&bridge_names);
339 for (i = 0; i < cfg->n_bridges; i++) {
340 sset_add(&bridge_names, cfg->bridges[i]->name);
343 /* Iterate over all system dpifs and delete any of them that do not appear
345 sset_init(&dpif_names);
346 sset_init(&dpif_types);
347 dp_enumerate_types(&dpif_types);
348 SSET_FOR_EACH (type, &dpif_types) {
351 dp_enumerate_names(type, &dpif_names);
353 /* Delete each dpif whose name is not in 'bridge_names'. */
354 SSET_FOR_EACH (name, &dpif_names) {
355 if (!sset_contains(&bridge_names, name)) {
359 retval = dpif_open(name, type, &dpif);
367 sset_destroy(&bridge_names);
368 sset_destroy(&dpif_names);
369 sset_destroy(&dpif_types);
372 /* Callback for iterate_and_prune_ifaces(). */
374 check_iface(struct bridge *br, struct iface *iface, void *aux OVS_UNUSED)
376 if (!iface->netdev) {
377 /* We already reported a related error, don't bother duplicating it. */
381 if (iface->dp_ifidx < 0) {
382 VLOG_ERR("%s interface not in %s, dropping",
383 iface->name, dpif_name(br->dpif));
387 VLOG_DBG("%s has interface %s on port %d", dpif_name(br->dpif),
388 iface->name, iface->dp_ifidx);
392 /* Callback for iterate_and_prune_ifaces(). */
394 set_iface_properties(struct bridge *br OVS_UNUSED, struct iface *iface,
395 void *aux OVS_UNUSED)
397 /* Set policing attributes. */
398 netdev_set_policing(iface->netdev,
399 iface->cfg->ingress_policing_rate,
400 iface->cfg->ingress_policing_burst);
402 /* Set MAC address of internal interfaces other than the local
404 if (iface->dp_ifidx != ODPP_LOCAL && !strcmp(iface->type, "internal")) {
405 iface_set_mac(iface);
411 /* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
412 * Deletes from 'br' all the interfaces for which 'cb' returns false, and then
413 * deletes from 'br' any ports that no longer have any interfaces. */
415 iterate_and_prune_ifaces(struct bridge *br,
416 bool (*cb)(struct bridge *, struct iface *,
420 struct port *port, *next_port;
422 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
423 struct iface *iface, *next_iface;
425 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
426 if (!cb(br, iface, aux)) {
427 iface_set_ofport(iface->cfg, -1);
428 iface_destroy(iface);
432 if (list_is_empty(&port->ifaces)) {
433 VLOG_WARN("%s port has no interfaces, dropping", port->name);
439 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
440 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
441 * responsible for freeing '*managersp' (with free()).
443 * You may be asking yourself "why does ovs-vswitchd care?", because
444 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
445 * should not be and in fact is not directly involved in that. But
446 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
447 * it has to tell in-band control where the managers are to enable that.
448 * (Thus, only managers connected in-band are collected.)
451 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
452 struct sockaddr_in **managersp, size_t *n_managersp)
454 struct sockaddr_in *managers = NULL;
455 size_t n_managers = 0;
459 /* Collect all of the potential targets from the "targets" columns of the
460 * rows pointed to by "manager_options", excluding any that are
463 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
464 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
466 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
467 sset_find_and_delete(&targets, m->target);
469 sset_add(&targets, m->target);
473 /* Now extract the targets' IP addresses. */
474 if (!sset_is_empty(&targets)) {
477 managers = xmalloc(sset_count(&targets) * sizeof *managers);
478 SSET_FOR_EACH (target, &targets) {
479 struct sockaddr_in *sin = &managers[n_managers];
481 if ((!strncmp(target, "tcp:", 4)
482 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
483 (!strncmp(target, "ssl:", 4)
484 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
489 sset_destroy(&targets);
491 *managersp = managers;
492 *n_managersp = n_managers;
496 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
498 struct shash old_br, new_br;
499 struct shash_node *node;
500 struct bridge *br, *next;
501 struct sockaddr_in *managers;
504 int sflow_bridge_number;
506 COVERAGE_INC(bridge_reconfigure);
508 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
510 /* Collect old and new bridges. */
513 LIST_FOR_EACH (br, node, &all_bridges) {
514 shash_add(&old_br, br->name, br);
516 for (i = 0; i < ovs_cfg->n_bridges; i++) {
517 const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
518 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
519 VLOG_WARN("more than one bridge named %s", br_cfg->name);
523 /* Get rid of deleted bridges and add new bridges. */
524 LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
525 struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
532 SHASH_FOR_EACH (node, &new_br) {
533 const char *br_name = node->name;
534 const struct ovsrec_bridge *br_cfg = node->data;
535 br = shash_find_data(&old_br, br_name);
537 /* If the bridge datapath type has changed, we need to tear it
538 * down and recreate. */
539 if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
541 bridge_create(br_cfg);
544 bridge_create(br_cfg);
547 shash_destroy(&old_br);
548 shash_destroy(&new_br);
550 /* Reconfigure all bridges. */
551 LIST_FOR_EACH (br, node, &all_bridges) {
552 bridge_reconfigure_one(br);
555 /* Add and delete ports on all datapaths.
557 * The kernel will reject any attempt to add a given port to a datapath if
558 * that port already belongs to a different datapath, so we must do all
559 * port deletions before any port additions. */
560 LIST_FOR_EACH (br, node, &all_bridges) {
561 struct dpif_port_dump dump;
562 struct shash want_ifaces;
563 struct dpif_port dpif_port;
565 bridge_get_all_ifaces(br, &want_ifaces);
566 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
567 if (!shash_find(&want_ifaces, dpif_port.name)
568 && strcmp(dpif_port.name, br->name)) {
569 int retval = dpif_port_del(br->dpif, dpif_port.port_no);
571 VLOG_WARN("failed to remove %s interface from %s: %s",
572 dpif_port.name, dpif_name(br->dpif),
577 shash_destroy(&want_ifaces);
579 LIST_FOR_EACH (br, node, &all_bridges) {
580 struct shash cur_ifaces, want_ifaces;
581 struct dpif_port_dump dump;
582 struct dpif_port dpif_port;
584 /* Get the set of interfaces currently in this datapath. */
585 shash_init(&cur_ifaces);
586 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
587 struct dpif_port *port_info = xmalloc(sizeof *port_info);
588 dpif_port_clone(port_info, &dpif_port);
589 shash_add(&cur_ifaces, dpif_port.name, port_info);
592 /* Get the set of interfaces we want on this datapath. */
593 bridge_get_all_ifaces(br, &want_ifaces);
595 hmap_clear(&br->ifaces);
596 SHASH_FOR_EACH (node, &want_ifaces) {
597 const char *if_name = node->name;
598 struct iface *iface = node->data;
599 struct dpif_port *dpif_port;
603 type = iface ? iface->type : "internal";
604 dpif_port = shash_find_data(&cur_ifaces, if_name);
606 /* If we have a port or a netdev already, and it's not the type we
607 * want, then delete the port (if any) and close the netdev (if
609 if ((dpif_port && strcmp(dpif_port->type, type))
610 || (iface && iface->netdev
611 && strcmp(type, netdev_get_type(iface->netdev)))) {
613 error = ofproto_port_del(br->ofproto, dpif_port->port_no);
620 netdev_close(iface->netdev);
621 iface->netdev = NULL;
625 /* If the port doesn't exist or we don't have the netdev open,
626 * we need to do more work. */
627 if (!dpif_port || (iface && !iface->netdev)) {
628 struct netdev_options options;
629 struct netdev *netdev;
632 /* First open the network device. */
633 options.name = if_name;
635 options.args = &args;
636 options.ethertype = NETDEV_ETH_TYPE_NONE;
640 shash_from_ovs_idl_map(iface->cfg->key_options,
641 iface->cfg->value_options,
642 iface->cfg->n_options, &args);
644 error = netdev_open(&options, &netdev);
645 shash_destroy(&args);
648 VLOG_WARN("could not open network device %s (%s)",
649 if_name, strerror(error));
653 /* Then add the port if we haven't already. */
655 error = dpif_port_add(br->dpif, netdev, NULL);
657 netdev_close(netdev);
658 if (error == EFBIG) {
659 VLOG_ERR("ran out of valid port numbers on %s",
660 dpif_name(br->dpif));
663 VLOG_WARN("failed to add %s interface to %s: %s",
664 if_name, dpif_name(br->dpif),
671 /* Update 'iface'. */
673 iface->netdev = netdev;
675 } else if (iface && iface->netdev) {
679 shash_from_ovs_idl_map(iface->cfg->key_options,
680 iface->cfg->value_options,
681 iface->cfg->n_options, &args);
682 netdev_set_config(iface->netdev, &args);
683 shash_destroy(&args);
686 shash_destroy(&want_ifaces);
688 SHASH_FOR_EACH (node, &cur_ifaces) {
689 struct dpif_port *port_info = node->data;
690 dpif_port_destroy(port_info);
693 shash_destroy(&cur_ifaces);
695 sflow_bridge_number = 0;
696 LIST_FOR_EACH (br, node, &all_bridges) {
697 uint8_t ea[ETH_ADDR_LEN];
699 struct iface *local_iface;
700 struct iface *hw_addr_iface;
703 bridge_fetch_dp_ifaces(br);
705 /* Delete interfaces that cannot be opened.
707 * From this point forward we are guaranteed that every "struct iface"
708 * has nonnull 'netdev' and correct 'dp_ifidx'. */
709 iterate_and_prune_ifaces(br, check_iface, NULL);
711 /* Pick local port hardware address, datapath ID. */
712 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
713 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
715 int error = netdev_set_etheraddr(local_iface->netdev, ea);
717 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
718 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
719 "Ethernet address: %s",
720 br->name, strerror(error));
723 memcpy(br->ea, ea, ETH_ADDR_LEN);
725 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
726 ofproto_set_datapath_id(br->ofproto, dpid);
728 dpid_string = xasprintf("%016"PRIx64, dpid);
729 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
732 /* Set NetFlow configuration on this bridge. */
733 if (br->cfg->netflow) {
734 struct ovsrec_netflow *nf_cfg = br->cfg->netflow;
735 struct netflow_options opts;
737 memset(&opts, 0, sizeof opts);
739 dpif_get_netflow_ids(br->dpif, &opts.engine_type, &opts.engine_id);
740 if (nf_cfg->engine_type) {
741 opts.engine_type = *nf_cfg->engine_type;
743 if (nf_cfg->engine_id) {
744 opts.engine_id = *nf_cfg->engine_id;
747 opts.active_timeout = nf_cfg->active_timeout;
748 if (!opts.active_timeout) {
749 opts.active_timeout = -1;
750 } else if (opts.active_timeout < 0) {
751 VLOG_WARN("bridge %s: active timeout interval set to negative "
752 "value, using default instead (%d seconds)", br->name,
753 NF_ACTIVE_TIMEOUT_DEFAULT);
754 opts.active_timeout = -1;
757 opts.add_id_to_iface = nf_cfg->add_id_to_interface;
758 if (opts.add_id_to_iface) {
759 if (opts.engine_id > 0x7f) {
760 VLOG_WARN("bridge %s: netflow port mangling may conflict "
761 "with another vswitch, choose an engine id less "
762 "than 128", br->name);
764 if (hmap_count(&br->ports) > 508) {
765 VLOG_WARN("bridge %s: netflow port mangling will conflict "
766 "with another port when more than 508 ports are "
771 sset_init(&opts.collectors);
772 sset_add_array(&opts.collectors,
773 nf_cfg->targets, nf_cfg->n_targets);
774 if (ofproto_set_netflow(br->ofproto, &opts)) {
775 VLOG_ERR("bridge %s: problem setting netflow collectors",
778 sset_destroy(&opts.collectors);
780 ofproto_set_netflow(br->ofproto, NULL);
783 /* Set sFlow configuration on this bridge. */
784 if (br->cfg->sflow) {
785 const struct ovsrec_sflow *sflow_cfg = br->cfg->sflow;
786 struct ovsrec_controller **controllers;
787 struct ofproto_sflow_options oso;
788 size_t n_controllers;
790 memset(&oso, 0, sizeof oso);
792 sset_init(&oso.targets);
793 sset_add_array(&oso.targets,
794 sflow_cfg->targets, sflow_cfg->n_targets);
796 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
797 if (sflow_cfg->sampling) {
798 oso.sampling_rate = *sflow_cfg->sampling;
801 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
802 if (sflow_cfg->polling) {
803 oso.polling_interval = *sflow_cfg->polling;
806 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
807 if (sflow_cfg->header) {
808 oso.header_len = *sflow_cfg->header;
811 oso.sub_id = sflow_bridge_number++;
812 oso.agent_device = sflow_cfg->agent;
814 oso.control_ip = NULL;
815 n_controllers = bridge_get_controllers(br, &controllers);
816 for (i = 0; i < n_controllers; i++) {
817 if (controllers[i]->local_ip) {
818 oso.control_ip = controllers[i]->local_ip;
822 ofproto_set_sflow(br->ofproto, &oso);
824 sset_destroy(&oso.targets);
826 ofproto_set_sflow(br->ofproto, NULL);
829 /* Update the controller and related settings. It would be more
830 * straightforward to call this from bridge_reconfigure_one(), but we
831 * can't do it there for two reasons. First, and most importantly, at
832 * that point we don't know the dp_ifidx of any interfaces that have
833 * been added to the bridge (because we haven't actually added them to
834 * the datapath). Second, at that point we haven't set the datapath ID
835 * yet; when a controller is configured, resetting the datapath ID will
836 * immediately disconnect from the controller, so it's better to set
837 * the datapath ID before the controller. */
838 bridge_reconfigure_remotes(br, managers, n_managers);
840 LIST_FOR_EACH (br, node, &all_bridges) {
843 br->has_bonded_ports = false;
844 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
847 port_reconfigure_bond(port);
849 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
850 iface_update_qos(iface, port->cfg->qos);
854 LIST_FOR_EACH (br, node, &all_bridges) {
855 iterate_and_prune_ifaces(br, set_iface_properties, NULL);
858 LIST_FOR_EACH (br, node, &all_bridges) {
860 HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
861 iface_update_cfm(iface);
867 /* ovs-vswitchd has completed initialization, so allow the process that
868 * forked us to exit successfully. */
869 daemonize_complete();
873 get_ovsrec_key_value(const struct ovsdb_idl_row *row,
874 const struct ovsdb_idl_column *column,
877 const struct ovsdb_datum *datum;
878 union ovsdb_atom atom;
881 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
882 atom.string = (char *) key;
883 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
884 return idx == UINT_MAX ? NULL : datum->values[idx].string;
888 bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
890 return get_ovsrec_key_value(&br_cfg->header_,
891 &ovsrec_bridge_col_other_config, key);
895 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
896 struct iface **hw_addr_iface)
902 *hw_addr_iface = NULL;
904 /* Did the user request a particular MAC? */
905 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
906 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
907 if (eth_addr_is_multicast(ea)) {
908 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
909 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
910 } else if (eth_addr_is_zero(ea)) {
911 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
917 /* Otherwise choose the minimum non-local MAC address among all of the
919 memset(ea, 0xff, ETH_ADDR_LEN);
920 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
921 uint8_t iface_ea[ETH_ADDR_LEN];
922 struct iface *candidate;
925 /* Mirror output ports don't participate. */
926 if (port->is_mirror_output_port) {
930 /* Choose the MAC address to represent the port. */
932 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
933 /* Find the interface with this Ethernet address (if any) so that
934 * we can provide the correct devname to the caller. */
935 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
936 uint8_t candidate_ea[ETH_ADDR_LEN];
937 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
938 && eth_addr_equals(iface_ea, candidate_ea)) {
943 /* Choose the interface whose MAC address will represent the port.
944 * The Linux kernel bonding code always chooses the MAC address of
945 * the first slave added to a bond, and the Fedora networking
946 * scripts always add slaves to a bond in alphabetical order, so
947 * for compatibility we choose the interface with the name that is
948 * first in alphabetical order. */
949 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
950 if (!iface || strcmp(candidate->name, iface->name) < 0) {
955 /* The local port doesn't count (since we're trying to choose its
956 * MAC address anyway). */
957 if (iface->dp_ifidx == ODPP_LOCAL) {
962 error = netdev_get_etheraddr(iface->netdev, iface_ea);
964 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
965 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
966 iface->name, strerror(error));
971 /* Compare against our current choice. */
972 if (!eth_addr_is_multicast(iface_ea) &&
973 !eth_addr_is_local(iface_ea) &&
974 !eth_addr_is_reserved(iface_ea) &&
975 !eth_addr_is_zero(iface_ea) &&
976 eth_addr_compare_3way(iface_ea, ea) < 0)
978 memcpy(ea, iface_ea, ETH_ADDR_LEN);
979 *hw_addr_iface = iface;
982 if (eth_addr_is_multicast(ea)) {
983 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
984 *hw_addr_iface = NULL;
985 VLOG_WARN("bridge %s: using default bridge Ethernet "
986 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
988 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
989 br->name, ETH_ADDR_ARGS(ea));
993 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
994 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
995 * an interface on 'br', then that interface must be passed in as
996 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
997 * 'hw_addr_iface' must be passed in as a null pointer. */
999 bridge_pick_datapath_id(struct bridge *br,
1000 const uint8_t bridge_ea[ETH_ADDR_LEN],
1001 struct iface *hw_addr_iface)
1004 * The procedure for choosing a bridge MAC address will, in the most
1005 * ordinary case, also choose a unique MAC that we can use as a datapath
1006 * ID. In some special cases, though, multiple bridges will end up with
1007 * the same MAC address. This is OK for the bridges, but it will confuse
1008 * the OpenFlow controller, because each datapath needs a unique datapath
1011 * Datapath IDs must be unique. It is also very desirable that they be
1012 * stable from one run to the next, so that policy set on a datapath
1015 const char *datapath_id;
1018 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1019 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
1023 if (hw_addr_iface) {
1025 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
1027 * A bridge whose MAC address is taken from a VLAN network device
1028 * (that is, a network device created with vconfig(8) or similar
1029 * tool) will have the same MAC address as a bridge on the VLAN
1030 * device's physical network device.
1032 * Handle this case by hashing the physical network device MAC
1033 * along with the VLAN identifier.
1035 uint8_t buf[ETH_ADDR_LEN + 2];
1036 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1037 buf[ETH_ADDR_LEN] = vlan >> 8;
1038 buf[ETH_ADDR_LEN + 1] = vlan;
1039 return dpid_from_hash(buf, sizeof buf);
1042 * Assume that this bridge's MAC address is unique, since it
1043 * doesn't fit any of the cases we handle specially.
1048 * A purely internal bridge, that is, one that has no non-virtual
1049 * network devices on it at all, is more difficult because it has no
1050 * natural unique identifier at all.
1052 * When the host is a XenServer, we handle this case by hashing the
1053 * host's UUID with the name of the bridge. Names of bridges are
1054 * persistent across XenServer reboots, although they can be reused if
1055 * an internal network is destroyed and then a new one is later
1056 * created, so this is fairly effective.
1058 * When the host is not a XenServer, we punt by using a random MAC
1059 * address on each run.
1061 const char *host_uuid = xenserver_get_host_uuid();
1063 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1064 dpid = dpid_from_hash(combined, strlen(combined));
1070 return eth_addr_to_uint64(bridge_ea);
1074 dpid_from_hash(const void *data, size_t n)
1076 uint8_t hash[SHA1_DIGEST_SIZE];
1078 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
1079 sha1_bytes(data, n, hash);
1080 eth_addr_mark_random(hash);
1081 return eth_addr_to_uint64(hash);
1085 iface_refresh_status(struct iface *iface)
1089 enum netdev_flags flags;
1098 if (!netdev_get_status(iface->netdev, &sh)) {
1100 char **keys, **values;
1102 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1103 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1108 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1111 shash_destroy_free_data(&sh);
1113 error = netdev_get_flags(iface->netdev, &flags);
1115 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1118 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1121 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1123 ovsrec_interface_set_duplex(iface->cfg,
1124 netdev_features_is_full_duplex(current)
1126 /* warning: uint64_t -> int64_t conversion */
1127 bps = netdev_features_to_bps(current);
1128 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1131 ovsrec_interface_set_duplex(iface->cfg, NULL);
1132 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1136 ovsrec_interface_set_link_state(iface->cfg,
1137 iface_get_carrier(iface) ? "up" : "down");
1139 error = netdev_get_mtu(iface->netdev, &mtu);
1140 if (!error && mtu != INT_MAX) {
1142 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1145 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1149 /* Writes 'iface''s CFM statistics to the database. Returns true if anything
1150 * changed, false otherwise. */
1152 iface_refresh_cfm_stats(struct iface *iface)
1154 const struct ovsrec_monitor *mon;
1155 const struct cfm *cfm;
1156 bool changed = false;
1159 mon = iface->cfg->monitor;
1160 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1166 for (i = 0; i < mon->n_remote_mps; i++) {
1167 const struct ovsrec_maintenance_point *mp;
1168 const struct remote_mp *rmp;
1170 mp = mon->remote_mps[i];
1171 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1173 if (mp->n_fault != 1 || mp->fault[0] != rmp->fault) {
1174 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1179 if (mon->n_fault != 1 || mon->fault[0] != cfm->fault) {
1180 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1188 iface_refresh_stats(struct iface *iface)
1194 static const struct iface_stat iface_stats[] = {
1195 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1196 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1197 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1198 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1199 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1200 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1201 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1202 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1203 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1204 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1205 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1206 { "collisions", offsetof(struct netdev_stats, collisions) },
1208 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1209 const struct iface_stat *s;
1211 char *keys[N_STATS];
1212 int64_t values[N_STATS];
1215 struct netdev_stats stats;
1217 /* Intentionally ignore return value, since errors will set 'stats' to
1218 * all-1s, and we will deal with that correctly below. */
1219 netdev_get_stats(iface->netdev, &stats);
1222 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1223 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1224 if (value != UINT64_MAX) {
1231 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1235 refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1237 struct ovsdb_datum datum;
1241 get_system_stats(&stats);
1243 ovsdb_datum_from_shash(&datum, &stats);
1244 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1248 static inline const char *
1249 nx_role_to_str(enum nx_role role)
1254 case NX_ROLE_MASTER:
1259 return "*** INVALID ROLE ***";
1264 bridge_refresh_controller_status(const struct bridge *br)
1267 const struct ovsrec_controller *cfg;
1269 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1271 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
1272 struct ofproto_controller_info *cinfo =
1273 shash_find_data(&info, cfg->target);
1276 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1277 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1278 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1279 (char **) cinfo->pairs.values,
1282 ovsrec_controller_set_is_connected(cfg, false);
1283 ovsrec_controller_set_role(cfg, NULL);
1284 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1288 ofproto_free_ofproto_controller_info(&info);
1294 const struct ovsrec_open_vswitch *cfg;
1296 bool datapath_destroyed;
1297 bool database_changed;
1300 /* Let each bridge do the work that it needs to do. */
1301 datapath_destroyed = false;
1302 LIST_FOR_EACH (br, node, &all_bridges) {
1303 int error = bridge_run_one(br);
1305 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1306 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1307 "forcing reconfiguration", br->name);
1308 datapath_destroyed = true;
1312 /* (Re)configure if necessary. */
1313 database_changed = ovsdb_idl_run(idl);
1314 cfg = ovsrec_open_vswitch_first(idl);
1316 /* Re-configure SSL. We do this on every trip through the main loop,
1317 * instead of just when the database changes, because the contents of the
1318 * key and certificate files can change without the database changing.
1320 * We do this before bridge_reconfigure() because that function might
1321 * initiate SSL connections and thus requires SSL to be configured. */
1322 if (cfg && cfg->ssl) {
1323 const struct ovsrec_ssl *ssl = cfg->ssl;
1325 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1326 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1329 if (database_changed || datapath_destroyed) {
1331 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1333 bridge_configure_once(cfg);
1334 bridge_reconfigure(cfg);
1336 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1337 ovsdb_idl_txn_commit(txn);
1338 ovsdb_idl_txn_destroy(txn); /* XXX */
1340 /* We still need to reconfigure to avoid dangling pointers to
1341 * now-destroyed ovsrec structures inside bridge data. */
1342 static const struct ovsrec_open_vswitch null_cfg;
1344 bridge_reconfigure(&null_cfg);
1348 /* Refresh system and interface stats if necessary. */
1349 if (time_msec() >= stats_timer) {
1351 struct ovsdb_idl_txn *txn;
1353 txn = ovsdb_idl_txn_create(idl);
1354 LIST_FOR_EACH (br, node, &all_bridges) {
1357 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1358 struct iface *iface;
1360 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1361 iface_refresh_stats(iface);
1362 iface_refresh_status(iface);
1365 bridge_refresh_controller_status(br);
1367 refresh_system_stats(cfg);
1368 ovsdb_idl_txn_commit(txn);
1369 ovsdb_idl_txn_destroy(txn); /* XXX */
1372 stats_timer = time_msec() + STATS_INTERVAL;
1375 if (time_msec() >= cfm_limiter) {
1376 struct ovsdb_idl_txn *txn;
1377 bool changed = false;
1379 txn = ovsdb_idl_txn_create(idl);
1380 LIST_FOR_EACH (br, node, &all_bridges) {
1383 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1384 struct iface *iface;
1386 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1387 changed = iface_refresh_cfm_stats(iface) || changed;
1393 cfm_limiter = time_msec() + CFM_LIMIT_INTERVAL;
1396 ovsdb_idl_txn_commit(txn);
1397 ovsdb_idl_txn_destroy(txn);
1406 LIST_FOR_EACH (br, node, &all_bridges) {
1409 ofproto_wait(br->ofproto);
1410 mac_learning_wait(br->ml);
1411 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1415 ovsdb_idl_wait(idl);
1416 poll_timer_wait_until(stats_timer);
1418 if (cfm_limiter > time_msec()) {
1419 poll_timer_wait_until(cfm_limiter);
1423 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1424 * configuration changes. */
1426 bridge_flush(struct bridge *br)
1428 COVERAGE_INC(bridge_flush);
1432 /* Bridge unixctl user interface functions. */
1434 bridge_unixctl_fdb_show(struct unixctl_conn *conn,
1435 const char *args, void *aux OVS_UNUSED)
1437 struct ds ds = DS_EMPTY_INITIALIZER;
1438 const struct bridge *br;
1439 const struct mac_entry *e;
1441 br = bridge_lookup(args);
1443 unixctl_command_reply(conn, 501, "no such bridge");
1447 ds_put_cstr(&ds, " port VLAN MAC Age\n");
1448 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
1449 struct port *port = e->port.p;
1450 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1451 port_get_an_iface(port)->dp_ifidx,
1452 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
1454 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1458 /* CFM unixctl user interface functions. */
1460 cfm_unixctl_show(struct unixctl_conn *conn,
1461 const char *args, void *aux OVS_UNUSED)
1463 struct ds ds = DS_EMPTY_INITIALIZER;
1464 struct iface *iface;
1465 const struct cfm *cfm;
1467 iface = iface_find(args);
1469 unixctl_command_reply(conn, 501, "no such interface");
1473 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1476 unixctl_command_reply(conn, 501, "CFM not enabled");
1480 cfm_dump_ds(cfm, &ds);
1481 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1485 /* QoS unixctl user interface functions. */
1487 struct qos_unixctl_show_cbdata {
1489 struct iface *iface;
1493 qos_unixctl_show_cb(unsigned int queue_id,
1494 const struct shash *details,
1497 struct qos_unixctl_show_cbdata *data = aux;
1498 struct ds *ds = data->ds;
1499 struct iface *iface = data->iface;
1500 struct netdev_queue_stats stats;
1501 struct shash_node *node;
1504 ds_put_cstr(ds, "\n");
1506 ds_put_format(ds, "Queue %u:\n", queue_id);
1508 ds_put_cstr(ds, "Default:\n");
1511 SHASH_FOR_EACH (node, details) {
1512 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1515 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1517 if (stats.tx_packets != UINT64_MAX) {
1518 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1521 if (stats.tx_bytes != UINT64_MAX) {
1522 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1525 if (stats.tx_errors != UINT64_MAX) {
1526 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1529 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1530 queue_id, strerror(error));
1535 qos_unixctl_show(struct unixctl_conn *conn,
1536 const char *args, void *aux OVS_UNUSED)
1538 struct ds ds = DS_EMPTY_INITIALIZER;
1539 struct shash sh = SHASH_INITIALIZER(&sh);
1540 struct iface *iface;
1542 struct shash_node *node;
1543 struct qos_unixctl_show_cbdata data;
1546 iface = iface_find(args);
1548 unixctl_command_reply(conn, 501, "no such interface");
1552 netdev_get_qos(iface->netdev, &type, &sh);
1554 if (*type != '\0') {
1555 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1557 SHASH_FOR_EACH (node, &sh) {
1558 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1563 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1566 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1568 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1570 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1571 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1574 shash_destroy_free_data(&sh);
1578 /* Bridge reconfiguration functions. */
1579 static struct bridge *
1580 bridge_create(const struct ovsrec_bridge *br_cfg)
1585 assert(!bridge_lookup(br_cfg->name));
1586 br = xzalloc(sizeof *br);
1588 error = dpif_create_and_open(br_cfg->name, br_cfg->datapath_type,
1595 error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
1598 VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
1600 dpif_delete(br->dpif);
1601 dpif_close(br->dpif);
1606 br->name = xstrdup(br_cfg->name);
1608 br->ml = mac_learning_create();
1609 eth_addr_nicira_random(br->default_ea);
1611 hmap_init(&br->ports);
1612 hmap_init(&br->ifaces);
1613 shash_init(&br->iface_by_name);
1617 list_push_back(&all_bridges, &br->node);
1619 VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
1625 bridge_destroy(struct bridge *br)
1628 struct port *port, *next;
1631 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1634 list_remove(&br->node);
1635 ofproto_destroy(br->ofproto);
1636 error = dpif_delete(br->dpif);
1637 if (error && error != ENOENT) {
1638 VLOG_ERR("failed to delete %s: %s",
1639 dpif_name(br->dpif), strerror(error));
1641 dpif_close(br->dpif);
1642 mac_learning_destroy(br->ml);
1643 hmap_destroy(&br->ifaces);
1644 hmap_destroy(&br->ports);
1645 shash_destroy(&br->iface_by_name);
1651 static struct bridge *
1652 bridge_lookup(const char *name)
1656 LIST_FOR_EACH (br, node, &all_bridges) {
1657 if (!strcmp(br->name, name)) {
1664 /* Handle requests for a listing of all flows known by the OpenFlow
1665 * stack, including those normally hidden. */
1667 bridge_unixctl_dump_flows(struct unixctl_conn *conn,
1668 const char *args, void *aux OVS_UNUSED)
1673 br = bridge_lookup(args);
1675 unixctl_command_reply(conn, 501, "Unknown bridge");
1680 ofproto_get_all_flows(br->ofproto, &results);
1682 unixctl_command_reply(conn, 200, ds_cstr(&results));
1683 ds_destroy(&results);
1686 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1687 * connections and reconnect. If BRIDGE is not specified, then all bridges
1688 * drop their controller connections and reconnect. */
1690 bridge_unixctl_reconnect(struct unixctl_conn *conn,
1691 const char *args, void *aux OVS_UNUSED)
1694 if (args[0] != '\0') {
1695 br = bridge_lookup(args);
1697 unixctl_command_reply(conn, 501, "Unknown bridge");
1700 ofproto_reconnect_controllers(br->ofproto);
1702 LIST_FOR_EACH (br, node, &all_bridges) {
1703 ofproto_reconnect_controllers(br->ofproto);
1706 unixctl_command_reply(conn, 200, NULL);
1710 bridge_run_one(struct bridge *br)
1715 error = ofproto_run1(br->ofproto);
1720 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
1722 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1726 error = ofproto_run2(br->ofproto, br->flush);
1733 bridge_get_controllers(const struct bridge *br,
1734 struct ovsrec_controller ***controllersp)
1736 struct ovsrec_controller **controllers;
1737 size_t n_controllers;
1739 controllers = br->cfg->controller;
1740 n_controllers = br->cfg->n_controller;
1742 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1748 *controllersp = controllers;
1750 return n_controllers;
1754 bridge_reconfigure_one(struct bridge *br)
1756 enum ofproto_fail_mode fail_mode;
1757 struct port *port, *next;
1758 struct shash_node *node;
1759 struct shash new_ports;
1762 /* Collect new ports. */
1763 shash_init(&new_ports);
1764 for (i = 0; i < br->cfg->n_ports; i++) {
1765 const char *name = br->cfg->ports[i]->name;
1766 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1767 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1772 /* If we have a controller, then we need a local port. Complain if the
1773 * user didn't specify one.
1775 * XXX perhaps we should synthesize a port ourselves in this case. */
1776 if (bridge_get_controllers(br, NULL)) {
1777 char local_name[IF_NAMESIZE];
1780 error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
1781 local_name, sizeof local_name);
1782 if (!error && !shash_find(&new_ports, local_name)) {
1783 VLOG_WARN("bridge %s: controller specified but no local port "
1784 "(port named %s) defined",
1785 br->name, local_name);
1789 /* Get rid of deleted ports.
1790 * Get rid of deleted interfaces on ports that still exist. */
1791 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1792 const struct ovsrec_port *port_cfg;
1794 port_cfg = shash_find_data(&new_ports, port->name);
1798 port_del_ifaces(port, port_cfg);
1802 /* Create new ports.
1803 * Add new interfaces to existing ports.
1804 * Reconfigure existing ports. */
1805 SHASH_FOR_EACH (node, &new_ports) {
1806 struct port *port = port_lookup(br, node->name);
1808 port = port_create(br, node->name);
1811 port_reconfigure(port, node->data);
1812 if (list_is_empty(&port->ifaces)) {
1813 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1814 br->name, port->name);
1818 shash_destroy(&new_ports);
1820 /* Set the fail-mode */
1821 fail_mode = !br->cfg->fail_mode
1822 || !strcmp(br->cfg->fail_mode, "standalone")
1823 ? OFPROTO_FAIL_STANDALONE
1824 : OFPROTO_FAIL_SECURE;
1825 if (ofproto_get_fail_mode(br->ofproto) != fail_mode
1826 && !ofproto_has_primary_controller(br->ofproto)) {
1827 ofproto_flush_flows(br->ofproto);
1829 ofproto_set_fail_mode(br->ofproto, fail_mode);
1831 /* Delete all flows if we're switching from connected to standalone or vice
1832 * versa. (XXX Should we delete all flows if we are switching from one
1833 * controller to another?) */
1835 /* Configure OpenFlow controller connection snooping. */
1836 if (!ofproto_has_snoops(br->ofproto)) {
1840 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
1841 ovs_rundir(), br->name));
1842 ofproto_set_snoops(br->ofproto, &snoops);
1843 sset_destroy(&snoops);
1846 mirror_reconfigure(br);
1849 /* Initializes 'oc' appropriately as a management service controller for
1852 * The caller must free oc->target when it is no longer needed. */
1854 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1855 struct ofproto_controller *oc)
1857 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
1858 oc->max_backoff = 0;
1859 oc->probe_interval = 60;
1860 oc->band = OFPROTO_OUT_OF_BAND;
1862 oc->burst_limit = 0;
1865 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1867 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1868 struct ofproto_controller *oc)
1870 oc->target = c->target;
1871 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1872 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1873 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1874 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
1875 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1876 oc->burst_limit = (c->controller_burst_limit
1877 ? *c->controller_burst_limit : 0);
1880 /* Configures the IP stack for 'br''s local interface properly according to the
1881 * configuration in 'c'. */
1883 bridge_configure_local_iface_netdev(struct bridge *br,
1884 struct ovsrec_controller *c)
1886 struct netdev *netdev;
1887 struct in_addr mask, gateway;
1889 struct iface *local_iface;
1892 /* If there's no local interface or no IP address, give up. */
1893 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
1894 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1898 /* Bring up the local interface. */
1899 netdev = local_iface->netdev;
1900 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1902 /* Configure the IP address and netmask. */
1903 if (!c->local_netmask
1904 || !inet_aton(c->local_netmask, &mask)
1906 mask.s_addr = guess_netmask(ip.s_addr);
1908 if (!netdev_set_in4(netdev, ip, mask)) {
1909 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1910 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1913 /* Configure the default gateway. */
1914 if (c->local_gateway
1915 && inet_aton(c->local_gateway, &gateway)
1916 && gateway.s_addr) {
1917 if (!netdev_add_router(netdev, gateway)) {
1918 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1919 br->name, IP_ARGS(&gateway.s_addr));
1925 bridge_reconfigure_remotes(struct bridge *br,
1926 const struct sockaddr_in *managers,
1929 const char *disable_ib_str, *queue_id_str;
1930 bool disable_in_band = false;
1933 struct ovsrec_controller **controllers;
1934 size_t n_controllers;
1937 struct ofproto_controller *ocs;
1941 /* Check if we should disable in-band control on this bridge. */
1942 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
1943 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
1944 disable_in_band = true;
1947 /* Set OpenFlow queue ID for in-band control. */
1948 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
1949 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
1950 ofproto_set_in_band_queue(br->ofproto, queue_id);
1952 if (disable_in_band) {
1953 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
1955 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
1957 had_primary = ofproto_has_primary_controller(br->ofproto);
1959 n_controllers = bridge_get_controllers(br, &controllers);
1961 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
1964 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
1965 for (i = 0; i < n_controllers; i++) {
1966 struct ovsrec_controller *c = controllers[i];
1968 if (!strncmp(c->target, "punix:", 6)
1969 || !strncmp(c->target, "unix:", 5)) {
1970 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1972 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
1973 * domain sockets and overwriting arbitrary local files. */
1974 VLOG_ERR_RL(&rl, "%s: not adding Unix domain socket controller "
1975 "\"%s\" due to possibility for remote exploit",
1976 dpif_name(br->dpif), c->target);
1980 bridge_configure_local_iface_netdev(br, c);
1981 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
1982 if (disable_in_band) {
1983 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
1988 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
1989 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
1992 if (had_primary != ofproto_has_primary_controller(br->ofproto)) {
1993 ofproto_flush_flows(br->ofproto);
1996 /* If there are no controllers and the bridge is in standalone
1997 * mode, set up a flow that matches every packet and directs
1998 * them to OFPP_NORMAL (which goes to us). Otherwise, the
1999 * switch is in secure mode and we won't pass any traffic until
2000 * a controller has been defined and it tells us to do so. */
2002 && ofproto_get_fail_mode(br->ofproto) == OFPROTO_FAIL_STANDALONE) {
2003 union ofp_action action;
2004 struct cls_rule rule;
2006 memset(&action, 0, sizeof action);
2007 action.type = htons(OFPAT_OUTPUT);
2008 action.output.len = htons(sizeof action);
2009 action.output.port = htons(OFPP_NORMAL);
2010 cls_rule_init_catchall(&rule, 0);
2011 ofproto_add_flow(br->ofproto, &rule, &action, 1);
2016 bridge_get_all_ifaces(const struct bridge *br, struct shash *ifaces)
2021 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2022 struct iface *iface;
2024 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2025 shash_add_once(ifaces, iface->name, iface);
2027 if (!list_is_short(&port->ifaces) && port->cfg->bond_fake_iface) {
2028 shash_add_once(ifaces, port->name, NULL);
2033 /* For robustness, in case the administrator moves around datapath ports behind
2034 * our back, we re-check all the datapath port numbers here.
2036 * This function will set the 'dp_ifidx' members of interfaces that have
2037 * disappeared to -1, so only call this function from a context where those
2038 * 'struct iface's will be removed from the bridge. Otherwise, the -1
2039 * 'dp_ifidx'es will cause trouble later when we try to send them to the
2040 * datapath, which doesn't support UINT16_MAX+1 ports. */
2042 bridge_fetch_dp_ifaces(struct bridge *br)
2044 struct dpif_port_dump dump;
2045 struct dpif_port dpif_port;
2048 /* Reset all interface numbers. */
2049 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2050 struct iface *iface;
2052 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2053 iface->dp_ifidx = -1;
2056 hmap_clear(&br->ifaces);
2058 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
2059 struct iface *iface = iface_lookup(br, dpif_port.name);
2061 if (iface->dp_ifidx >= 0) {
2062 VLOG_WARN("%s reported interface %s twice",
2063 dpif_name(br->dpif), dpif_port.name);
2064 } else if (iface_from_dp_ifidx(br, dpif_port.port_no)) {
2065 VLOG_WARN("%s reported interface %"PRIu16" twice",
2066 dpif_name(br->dpif), dpif_port.port_no);
2068 iface->dp_ifidx = dpif_port.port_no;
2069 hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
2070 hash_int(iface->dp_ifidx, 0));
2073 iface_set_ofport(iface->cfg,
2074 (iface->dp_ifidx >= 0
2075 ? odp_port_to_ofp_port(iface->dp_ifidx)
2081 /* Bridge packet processing functions. */
2084 set_dst(struct dst *dst, const struct flow *flow,
2085 const struct port *in_port, const struct port *out_port,
2088 dst->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
2089 : in_port->vlan >= 0 ? in_port->vlan
2090 : flow->vlan_tci == 0 ? OFP_VLAN_NONE
2091 : vlan_tci_to_vid(flow->vlan_tci));
2093 dst->iface = (!out_port->bond
2094 ? port_get_an_iface(out_port)
2095 : bond_choose_output_slave(out_port->bond, flow,
2098 return dst->iface != NULL;
2102 swap_dst(struct dst *p, struct dst *q)
2104 struct dst tmp = *p;
2109 /* Moves all the dsts with vlan == 'vlan' to the front of the 'n_dsts' in
2110 * 'dsts'. (This may help performance by reducing the number of VLAN changes
2111 * that we push to the datapath. We could in fact fully sort the array by
2112 * vlan, but in most cases there are at most two different vlan tags so that's
2113 * possibly overkill.) */
2115 partition_dsts(struct dst_set *set, int vlan)
2117 struct dst *first = set->dsts;
2118 struct dst *last = set->dsts + set->n;
2120 while (first != last) {
2122 * - All dsts < first have vlan == 'vlan'.
2123 * - All dsts >= last have vlan != 'vlan'.
2124 * - first < last. */
2125 while (first->vlan == vlan) {
2126 if (++first == last) {
2131 /* Same invariants, plus one additional:
2132 * - first->vlan != vlan.
2134 while (last[-1].vlan != vlan) {
2135 if (--last == first) {
2140 /* Same invariants, plus one additional:
2141 * - last[-1].vlan == vlan.*/
2142 swap_dst(first++, --last);
2147 mirror_mask_ffs(mirror_mask_t mask)
2149 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2154 dst_set_init(struct dst_set *set)
2156 set->dsts = set->builtin;
2158 set->allocated = ARRAY_SIZE(set->builtin);
2162 dst_set_add(struct dst_set *set, const struct dst *dst)
2164 if (set->n >= set->allocated) {
2165 size_t new_allocated;
2166 struct dst *new_dsts;
2168 new_allocated = set->allocated * 2;
2169 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2170 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2174 set->dsts = new_dsts;
2175 set->allocated = new_allocated;
2177 set->dsts[set->n++] = *dst;
2181 dst_set_free(struct dst_set *set)
2183 if (set->dsts != set->builtin) {
2189 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
2192 for (i = 0; i < set->n; i++) {
2193 if (set->dsts[i].vlan == test->vlan
2194 && set->dsts[i].iface == test->iface) {
2202 port_trunks_vlan(const struct port *port, uint16_t vlan)
2204 return (port->vlan < 0
2205 && (!port->trunks || bitmap_is_set(port->trunks, vlan)));
2209 port_includes_vlan(const struct port *port, uint16_t vlan)
2211 return vlan == port->vlan || port_trunks_vlan(port, vlan);
2215 port_is_floodable(const struct port *port)
2217 struct iface *iface;
2219 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2220 if (!ofproto_port_is_floodable(port->bridge->ofproto,
2228 /* Returns an arbitrary interface within 'port'. */
2229 static struct iface *
2230 port_get_an_iface(const struct port *port)
2232 return CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
2236 compose_dsts(const struct bridge *br, const struct flow *flow, uint16_t vlan,
2237 const struct port *in_port, const struct port *out_port,
2238 struct dst_set *set, tag_type *tags, uint16_t *nf_output_iface)
2240 mirror_mask_t mirrors = in_port->src_mirrors;
2244 flow_vlan = vlan_tci_to_vid(flow->vlan_tci);
2245 if (flow_vlan == 0) {
2246 flow_vlan = OFP_VLAN_NONE;
2249 if (out_port == FLOOD_PORT) {
2252 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2254 && port_is_floodable(port)
2255 && port_includes_vlan(port, vlan)
2256 && !port->is_mirror_output_port
2257 && set_dst(&dst, flow, in_port, port, tags)) {
2258 mirrors |= port->dst_mirrors;
2259 dst_set_add(set, &dst);
2262 *nf_output_iface = NF_OUT_FLOOD;
2263 } else if (out_port && set_dst(&dst, flow, in_port, out_port, tags)) {
2264 dst_set_add(set, &dst);
2265 *nf_output_iface = dst.iface->dp_ifidx;
2266 mirrors |= out_port->dst_mirrors;
2270 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
2271 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
2273 if (set_dst(&dst, flow, in_port, m->out_port, tags)
2274 && !dst_is_duplicate(set, &dst)) {
2275 dst_set_add(set, &dst);
2280 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2281 if (port_includes_vlan(port, m->out_vlan)
2282 && set_dst(&dst, flow, in_port, port, tags))
2284 if (port->vlan < 0) {
2285 dst.vlan = m->out_vlan;
2287 if (dst_is_duplicate(set, &dst)) {
2291 /* Use the vlan tag on the original flow instead of
2292 * the one passed in the vlan parameter. This ensures
2293 * that we compare the vlan from before any implicit
2294 * tagging tags place. This is necessary because
2295 * dst->vlan is the final vlan, after removing implicit
2297 if (port == in_port && dst.vlan == flow_vlan) {
2298 /* Don't send out input port on same VLAN. */
2301 dst_set_add(set, &dst);
2306 mirrors &= mirrors - 1;
2309 partition_dsts(set, flow_vlan);
2313 compose_actions(struct bridge *br, const struct flow *flow, uint16_t vlan,
2314 const struct port *in_port, const struct port *out_port,
2315 tag_type *tags, struct ofpbuf *actions,
2316 uint16_t *nf_output_iface)
2323 compose_dsts(br, flow, vlan, in_port, out_port, &set, tags,
2326 cur_vlan = vlan_tci_to_vid(flow->vlan_tci);
2327 if (cur_vlan == 0) {
2328 cur_vlan = OFP_VLAN_NONE;
2330 for (i = 0; i < set.n; i++) {
2331 const struct dst *dst = &set.dsts[i];
2332 if (dst->vlan != cur_vlan) {
2333 if (dst->vlan == OFP_VLAN_NONE) {
2334 nl_msg_put_flag(actions, ODP_ACTION_ATTR_STRIP_VLAN);
2337 tci = htons(dst->vlan & VLAN_VID_MASK);
2338 tci |= flow->vlan_tci & htons(VLAN_PCP_MASK);
2339 nl_msg_put_be16(actions, ODP_ACTION_ATTR_SET_DL_TCI, tci);
2341 cur_vlan = dst->vlan;
2343 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
2348 /* Returns the effective vlan of a packet, taking into account both the
2349 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2350 * the packet is untagged and -1 indicates it has an invalid header and
2351 * should be dropped. */
2352 static int flow_get_vlan(struct bridge *br, const struct flow *flow,
2353 struct port *in_port, bool have_packet)
2355 int vlan = vlan_tci_to_vid(flow->vlan_tci);
2356 if (in_port->vlan >= 0) {
2359 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2360 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2361 "packet received on port %s configured with "
2362 "implicit VLAN %"PRIu16,
2363 br->name, vlan, in_port->name, in_port->vlan);
2367 vlan = in_port->vlan;
2369 if (!port_includes_vlan(in_port, vlan)) {
2371 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2372 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2373 "packet received on port %s not configured for "
2375 br->name, vlan, in_port->name, vlan);
2384 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2385 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2386 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2388 is_gratuitous_arp(const struct flow *flow)
2390 return (flow->dl_type == htons(ETH_TYPE_ARP)
2391 && eth_addr_is_broadcast(flow->dl_dst)
2392 && (flow->nw_proto == ARP_OP_REPLY
2393 || (flow->nw_proto == ARP_OP_REQUEST
2394 && flow->nw_src == flow->nw_dst)));
2398 update_learning_table(struct bridge *br, const struct flow *flow, int vlan,
2399 struct port *in_port)
2401 struct mac_entry *mac;
2403 if (!mac_learning_may_learn(br->ml, flow->dl_src, vlan)) {
2407 mac = mac_learning_insert(br->ml, flow->dl_src, vlan);
2408 if (is_gratuitous_arp(flow)) {
2409 /* We don't want to learn from gratuitous ARP packets that are
2410 * reflected back over bond slaves so we lock the learning table. */
2411 if (!in_port->bond) {
2412 mac_entry_set_grat_arp_lock(mac);
2413 } else if (mac_entry_is_grat_arp_locked(mac)) {
2418 if (mac_entry_is_new(mac) || mac->port.p != in_port) {
2419 /* The log messages here could actually be useful in debugging,
2420 * so keep the rate limit relatively high. */
2421 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2422 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2423 "on port %s in VLAN %d",
2424 br->name, ETH_ADDR_ARGS(flow->dl_src),
2425 in_port->name, vlan);
2427 mac->port.p = in_port;
2428 ofproto_revalidate(br->ofproto, mac_learning_changed(br->ml, mac));
2432 /* Determines whether packets in 'flow' within 'br' should be forwarded or
2433 * dropped. Returns true if they may be forwarded, false if they should be
2436 * If 'have_packet' is true, it indicates that the caller is processing a
2437 * received packet. If 'have_packet' is false, then the caller is just
2438 * revalidating an existing flow because configuration has changed. Either
2439 * way, 'have_packet' only affects logging (there is no point in logging errors
2440 * during revalidation).
2442 * Sets '*in_portp' to the input port. This will be a null pointer if
2443 * flow->in_port does not designate a known input port (in which case
2444 * is_admissible() returns false).
2446 * When returning true, sets '*vlanp' to the effective VLAN of the input
2447 * packet, as returned by flow_get_vlan().
2449 * May also add tags to '*tags', although the current implementation only does
2450 * so in one special case.
2453 is_admissible(struct bridge *br, const struct flow *flow, bool have_packet,
2454 tag_type *tags, int *vlanp, struct port **in_portp)
2456 struct iface *in_iface;
2457 struct port *in_port;
2460 /* Find the interface and port structure for the received packet. */
2461 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2463 /* No interface? Something fishy... */
2465 /* Odd. A few possible reasons here:
2467 * - We deleted an interface but there are still a few packets
2468 * queued up from it.
2470 * - Someone externally added an interface (e.g. with "ovs-dpctl
2471 * add-if") that we don't know about.
2473 * - Packet arrived on the local port but the local port is not
2474 * one of our bridge ports.
2476 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2478 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
2479 "interface %"PRIu16, br->name, flow->in_port);
2485 *in_portp = in_port = in_iface->port;
2486 *vlanp = vlan = flow_get_vlan(br, flow, in_port, have_packet);
2491 /* Drop frames for reserved multicast addresses. */
2492 if (eth_addr_is_reserved(flow->dl_dst)) {
2496 /* Drop frames on ports reserved for mirroring. */
2497 if (in_port->is_mirror_output_port) {
2499 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2500 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2501 "%s, which is reserved exclusively for mirroring",
2502 br->name, in_port->name);
2507 if (in_port->bond) {
2508 struct mac_entry *mac;
2510 switch (bond_check_admissibility(in_port->bond, in_iface,
2511 flow->dl_dst, tags)) {
2518 case BV_DROP_IF_MOVED:
2519 mac = mac_learning_lookup(br->ml, flow->dl_src, vlan, NULL);
2520 if (mac && mac->port.p != in_port &&
2521 (!is_gratuitous_arp(flow)
2522 || mac_entry_is_grat_arp_locked(mac))) {
2532 /* If the composed actions may be applied to any packet in the given 'flow',
2533 * returns true. Otherwise, the actions should only be applied to 'packet', or
2534 * not at all, if 'packet' was NULL. */
2536 process_flow(struct bridge *br, const struct flow *flow,
2537 const struct ofpbuf *packet, struct ofpbuf *actions,
2538 tag_type *tags, uint16_t *nf_output_iface)
2540 struct port *in_port;
2541 struct port *out_port;
2542 struct mac_entry *mac;
2545 /* Check whether we should drop packets in this flow. */
2546 if (!is_admissible(br, flow, packet != NULL, tags, &vlan, &in_port)) {
2551 /* Learn source MAC (but don't try to learn from revalidation). */
2553 update_learning_table(br, flow, vlan, in_port);
2556 /* Determine output port. */
2557 mac = mac_learning_lookup(br->ml, flow->dl_dst, vlan, tags);
2559 out_port = mac->port.p;
2560 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
2561 /* If we are revalidating but don't have a learning entry then
2562 * eject the flow. Installing a flow that floods packets opens
2563 * up a window of time where we could learn from a packet reflected
2564 * on a bond and blackhole packets before the learning table is
2565 * updated to reflect the correct port. */
2568 out_port = FLOOD_PORT;
2571 /* Don't send packets out their input ports. */
2572 if (in_port == out_port) {
2578 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2586 bridge_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
2587 struct ofpbuf *actions, tag_type *tags,
2588 uint16_t *nf_output_iface, void *br_)
2590 struct bridge *br = br_;
2592 COVERAGE_INC(bridge_process_flow);
2593 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2597 bridge_special_ofhook_cb(const struct flow *flow,
2598 const struct ofpbuf *packet, void *br_)
2600 struct iface *iface;
2601 struct bridge *br = br_;
2603 iface = iface_from_dp_ifidx(br, flow->in_port);
2605 if (flow->dl_type == htons(ETH_TYPE_LACP)) {
2606 if (iface && iface->port->bond && packet) {
2607 bond_process_lacp(iface->port->bond, iface, packet);
2616 bridge_account_flow_ofhook_cb(const struct flow *flow, tag_type tags,
2617 const struct nlattr *actions,
2619 uint64_t n_bytes, void *br_)
2621 struct bridge *br = br_;
2622 const struct nlattr *a;
2623 struct port *in_port;
2628 /* Feed information from the active flows back into the learning table to
2629 * ensure that table is always in sync with what is actually flowing
2630 * through the datapath.
2632 * We test that 'tags' is nonzero to ensure that only flows that include an
2633 * OFPP_NORMAL action are used for learning. This works because
2634 * bridge_normal_ofhook_cb() always sets a nonzero tag value. */
2635 if (tags && is_admissible(br, flow, false, &dummy, &vlan, &in_port)) {
2636 update_learning_table(br, flow, vlan, in_port);
2639 /* Account for bond slave utilization. */
2640 if (!br->has_bonded_ports) {
2643 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
2644 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
2645 struct port *out_port = port_from_dp_ifidx(br, nl_attr_get_u32(a));
2646 if (out_port && out_port->bond) {
2647 uint16_t vlan = (flow->vlan_tci
2648 ? vlan_tci_to_vid(flow->vlan_tci)
2650 bond_account(out_port->bond, flow, vlan, n_bytes);
2657 bridge_account_checkpoint_ofhook_cb(void *br_)
2659 struct bridge *br = br_;
2662 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2664 bond_rebalance(port->bond,
2665 ofproto_get_revalidate_set(br->ofproto));
2670 static struct ofhooks bridge_ofhooks = {
2671 bridge_normal_ofhook_cb,
2672 bridge_special_ofhook_cb,
2673 bridge_account_flow_ofhook_cb,
2674 bridge_account_checkpoint_ofhook_cb,
2677 /* Port functions. */
2680 port_run(struct port *port)
2683 bond_run(port->bond,
2684 ofproto_get_revalidate_set(port->bridge->ofproto));
2685 if (bond_should_send_learning_packets(port->bond)) {
2686 port_send_learning_packets(port);
2692 port_wait(struct port *port)
2695 bond_wait(port->bond);
2699 static struct port *
2700 port_create(struct bridge *br, const char *name)
2704 port = xzalloc(sizeof *port);
2707 port->trunks = NULL;
2708 port->name = xstrdup(name);
2709 list_init(&port->ifaces);
2711 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
2713 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2720 get_port_other_config(const struct ovsrec_port *port, const char *key,
2721 const char *default_value)
2725 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
2727 return value ? value : default_value;
2731 get_interface_other_config(const struct ovsrec_interface *iface,
2732 const char *key, const char *default_value)
2736 value = get_ovsrec_key_value(&iface->header_,
2737 &ovsrec_interface_col_other_config, key);
2738 return value ? value : default_value;
2742 port_del_ifaces(struct port *port, const struct ovsrec_port *cfg)
2744 struct iface *iface, *next;
2745 struct sset new_ifaces;
2748 /* Collect list of new interfaces. */
2749 sset_init(&new_ifaces);
2750 for (i = 0; i < cfg->n_interfaces; i++) {
2751 const char *name = cfg->interfaces[i]->name;
2752 sset_add(&new_ifaces, name);
2755 /* Get rid of deleted interfaces. */
2756 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2757 if (!sset_contains(&new_ifaces, iface->name)) {
2758 iface_destroy(iface);
2762 sset_destroy(&new_ifaces);
2765 /* Expires all MAC learning entries associated with 'port' and forces ofproto
2766 * to revalidate every flow. */
2768 port_flush_macs(struct port *port)
2770 struct bridge *br = port->bridge;
2771 struct mac_learning *ml = br->ml;
2772 struct mac_entry *mac, *next_mac;
2775 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2776 if (mac->port.p == port) {
2777 mac_learning_expire(ml, mac);
2783 port_reconfigure(struct port *port, const struct ovsrec_port *cfg)
2785 struct sset new_ifaces;
2786 bool need_flush = false;
2787 unsigned long *trunks;
2794 /* Add new interfaces and update 'cfg' member of existing ones. */
2795 sset_init(&new_ifaces);
2796 for (i = 0; i < cfg->n_interfaces; i++) {
2797 const struct ovsrec_interface *if_cfg = cfg->interfaces[i];
2798 struct iface *iface;
2800 if (!sset_add(&new_ifaces, if_cfg->name)) {
2801 VLOG_WARN("port %s: %s specified twice as port interface",
2802 port->name, if_cfg->name);
2803 iface_set_ofport(if_cfg, -1);
2807 iface = iface_lookup(port->bridge, if_cfg->name);
2809 if (iface->port != port) {
2810 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
2812 port->bridge->name, if_cfg->name, iface->port->name);
2815 iface->cfg = if_cfg;
2817 iface = iface_create(port, if_cfg);
2820 /* Determine interface type. The local port always has type
2821 * "internal". Other ports take their type from the database and
2822 * default to "system" if none is specified. */
2823 iface->type = (!strcmp(if_cfg->name, port->bridge->name) ? "internal"
2824 : if_cfg->type[0] ? if_cfg->type
2827 sset_destroy(&new_ifaces);
2832 if (list_is_short(&port->ifaces)) {
2834 if (vlan >= 0 && vlan <= 4095) {
2835 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
2840 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
2841 * they even work as-is. But they have not been tested. */
2842 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
2846 if (port->vlan != vlan) {
2851 /* Get trunked VLANs. */
2853 if (vlan < 0 && cfg->n_trunks) {
2856 trunks = bitmap_allocate(4096);
2858 for (i = 0; i < cfg->n_trunks; i++) {
2859 int trunk = cfg->trunks[i];
2861 bitmap_set1(trunks, trunk);
2867 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
2868 port->name, cfg->n_trunks);
2870 if (n_errors == cfg->n_trunks) {
2871 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
2873 bitmap_free(trunks);
2876 } else if (vlan >= 0 && cfg->n_trunks) {
2877 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
2881 ? port->trunks != NULL
2882 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
2885 bitmap_free(port->trunks);
2886 port->trunks = trunks;
2889 port_flush_macs(port);
2894 port_destroy(struct port *port)
2897 struct bridge *br = port->bridge;
2898 struct iface *iface, *next;
2901 for (i = 0; i < MAX_MIRRORS; i++) {
2902 struct mirror *m = br->mirrors[i];
2903 if (m && m->out_port == port) {
2908 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2909 iface_destroy(iface);
2912 hmap_remove(&br->ports, &port->hmap_node);
2914 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
2916 port_flush_macs(port);
2918 bitmap_free(port->trunks);
2924 static struct port *
2925 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
2927 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
2928 return iface ? iface->port : NULL;
2931 static struct port *
2932 port_lookup(const struct bridge *br, const char *name)
2936 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
2938 if (!strcmp(port->name, name)) {
2946 enable_lacp(struct port *port, bool *activep)
2948 if (!port->cfg->lacp) {
2949 /* XXX when LACP implementation has been sufficiently tested, enable by
2950 * default and make active on bonded ports. */
2952 } else if (!strcmp(port->cfg->lacp, "off")) {
2954 } else if (!strcmp(port->cfg->lacp, "active")) {
2957 } else if (!strcmp(port->cfg->lacp, "passive")) {
2961 VLOG_WARN("port %s: unknown LACP mode %s",
2962 port->name, port->cfg->lacp);
2967 static struct lacp_settings *
2968 port_reconfigure_bond_lacp(struct port *port, struct lacp_settings *s)
2970 if (!enable_lacp(port, &s->active)) {
2974 s->name = port->name;
2975 memcpy(s->id, port->bridge->ea, ETH_ADDR_LEN);
2976 s->priority = atoi(get_port_other_config(port->cfg, "lacp-system-priority",
2978 s->fast = !strcmp(get_port_other_config(port->cfg, "lacp-time", "slow"),
2981 if (s->priority <= 0 || s->priority > UINT16_MAX) {
2982 /* Prefer bondable links if unspecified. */
2983 s->priority = UINT16_MAX - !list_is_short(&port->ifaces);
2989 iface_reconfigure_bond(struct iface *iface)
2991 struct lacp_slave_settings s;
2994 s.name = iface->name;
2995 s.id = iface->dp_ifidx;
2996 priority = atoi(get_interface_other_config(
2997 iface->cfg, "lacp-port-priority", "0"));
2998 s.priority = (priority >= 0 && priority <= UINT16_MAX
2999 ? priority : UINT16_MAX);
3000 bond_slave_register(iface->port->bond, iface, iface->netdev, &s);
3004 port_reconfigure_bond(struct port *port)
3006 struct lacp_settings lacp_settings;
3007 struct bond_settings s;
3008 const char *detect_s;
3009 struct iface *iface;
3011 if (list_is_short(&port->ifaces)) {
3012 /* Not a bonded port. */
3013 bond_destroy(port->bond);
3018 port->bridge->has_bonded_ports = true;
3020 s.name = port->name;
3022 if (port->cfg->bond_mode
3023 && !bond_mode_from_string(&s.balance, port->cfg->bond_mode)) {
3024 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
3025 port->name, port->cfg->bond_mode,
3026 bond_mode_to_string(s.balance));
3029 s.detect = BLSM_CARRIER;
3030 detect_s = get_port_other_config(port->cfg, "bond-detect-mode", NULL);
3031 if (detect_s && !bond_detect_mode_from_string(&s.detect, detect_s)) {
3032 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
3034 port->name, detect_s, bond_detect_mode_to_string(s.detect));
3037 s.miimon_interval = atoi(
3038 get_port_other_config(port->cfg, "bond-miimon-interval", "200"));
3039 if (s.miimon_interval < 100) {
3040 s.miimon_interval = 100;
3043 s.up_delay = MAX(0, port->cfg->bond_updelay);
3044 s.down_delay = MAX(0, port->cfg->bond_downdelay);
3045 s.rebalance_interval = atoi(
3046 get_port_other_config(port->cfg, "bond-rebalance-interval", "10000"));
3047 if (s.rebalance_interval < 1000) {
3048 s.rebalance_interval = 1000;
3051 s.fake_iface = port->cfg->bond_fake_iface;
3052 s.lacp = port_reconfigure_bond_lacp(port, &lacp_settings);
3055 port->bond = bond_create(&s);
3057 bond_reconfigure(port->bond, &s);
3060 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3061 iface_reconfigure_bond(iface);
3066 port_send_learning_packets(struct port *port)
3068 struct bridge *br = port->bridge;
3069 int error, n_packets, n_errors;
3070 struct mac_entry *e;
3072 error = n_packets = n_errors = 0;
3073 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
3074 if (e->port.p != port) {
3075 int ret = bond_send_learning_packet(port->bond, e->mac, e->vlan);
3085 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3086 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3087 "packets, last error was: %s",
3088 port->name, n_errors, n_packets, strerror(error));
3090 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3091 port->name, n_packets);
3095 /* Interface functions. */
3097 static struct iface *
3098 iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
3100 struct bridge *br = port->bridge;
3101 struct iface *iface;
3102 char *name = if_cfg->name;
3104 iface = xzalloc(sizeof *iface);
3106 iface->name = xstrdup(name);
3107 iface->dp_ifidx = -1;
3108 iface->tag = tag_create_random();
3109 iface->netdev = NULL;
3110 iface->cfg = if_cfg;
3112 shash_add_assert(&br->iface_by_name, iface->name, iface);
3114 list_push_back(&port->ifaces, &iface->port_elem);
3116 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3124 iface_destroy(struct iface *iface)
3127 struct port *port = iface->port;
3128 struct bridge *br = port->bridge;
3131 bond_slave_unregister(port->bond, iface);
3134 shash_find_and_delete_assert(&br->iface_by_name, iface->name);
3136 if (iface->dp_ifidx >= 0) {
3137 hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
3140 list_remove(&iface->port_elem);
3142 netdev_close(iface->netdev);
3147 bridge_flush(port->bridge);
3151 static struct iface *
3152 iface_lookup(const struct bridge *br, const char *name)
3154 return shash_find_data(&br->iface_by_name, name);
3157 static struct iface *
3158 iface_find(const char *name)
3160 const struct bridge *br;
3162 LIST_FOR_EACH (br, node, &all_bridges) {
3163 struct iface *iface = iface_lookup(br, name);
3172 static struct iface *
3173 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3175 struct iface *iface;
3177 HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
3178 hash_int(dp_ifidx, 0), &br->ifaces) {
3179 if (iface->dp_ifidx == dp_ifidx) {
3186 /* Set Ethernet address of 'iface', if one is specified in the configuration
3189 iface_set_mac(struct iface *iface)
3191 uint8_t ea[ETH_ADDR_LEN];
3193 if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
3194 if (eth_addr_is_multicast(ea)) {
3195 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3197 } else if (iface->dp_ifidx == ODPP_LOCAL) {
3198 VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
3199 iface->name, iface->name);
3201 int error = netdev_set_etheraddr(iface->netdev, ea);
3203 VLOG_ERR("interface %s: setting MAC failed (%s)",
3204 iface->name, strerror(error));
3210 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
3212 iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
3215 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
3219 /* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
3221 * The value strings in '*shash' are taken directly from values[], not copied,
3222 * so the caller should not modify or free them. */
3224 shash_from_ovs_idl_map(char **keys, char **values, size_t n,
3225 struct shash *shash)
3230 for (i = 0; i < n; i++) {
3231 shash_add(shash, keys[i], values[i]);
3235 /* Creates 'keys' and 'values' arrays from 'shash'.
3237 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
3238 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
3239 * are populated with with strings taken directly from 'shash' and thus have
3240 * the same ownership of the key-value pairs in shash.
3243 shash_to_ovs_idl_map(struct shash *shash,
3244 char ***keys, char ***values, size_t *n)
3248 struct shash_node *sn;
3250 count = shash_count(shash);
3252 k = xmalloc(count * sizeof *k);
3253 v = xmalloc(count * sizeof *v);
3256 SHASH_FOR_EACH(sn, shash) {
3267 struct iface_delete_queues_cbdata {
3268 struct netdev *netdev;
3269 const struct ovsdb_datum *queues;
3273 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
3275 union ovsdb_atom atom;
3277 atom.integer = target;
3278 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
3282 iface_delete_queues(unsigned int queue_id,
3283 const struct shash *details OVS_UNUSED, void *cbdata_)
3285 struct iface_delete_queues_cbdata *cbdata = cbdata_;
3287 if (!queue_ids_include(cbdata->queues, queue_id)) {
3288 netdev_delete_queue(cbdata->netdev, queue_id);
3293 iface_update_qos(struct iface *iface, const struct ovsrec_qos *qos)
3295 if (!qos || qos->type[0] == '\0') {
3296 netdev_set_qos(iface->netdev, NULL, NULL);
3298 struct iface_delete_queues_cbdata cbdata;
3299 struct shash details;
3302 /* Configure top-level Qos for 'iface'. */
3303 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
3304 qos->n_other_config, &details);
3305 netdev_set_qos(iface->netdev, qos->type, &details);
3306 shash_destroy(&details);
3308 /* Deconfigure queues that were deleted. */
3309 cbdata.netdev = iface->netdev;
3310 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
3312 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
3314 /* Configure queues for 'iface'. */
3315 for (i = 0; i < qos->n_queues; i++) {
3316 const struct ovsrec_queue *queue = qos->value_queues[i];
3317 unsigned int queue_id = qos->key_queues[i];
3319 shash_from_ovs_idl_map(queue->key_other_config,
3320 queue->value_other_config,
3321 queue->n_other_config, &details);
3322 netdev_set_queue(iface->netdev, queue_id, &details);
3323 shash_destroy(&details);
3329 iface_update_cfm(struct iface *iface)
3333 uint16_t *remote_mps;
3334 struct ovsrec_monitor *mon;
3335 uint8_t maid[CCM_MAID_LEN];
3337 mon = iface->cfg->monitor;
3340 ofproto_iface_clear_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
3344 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
3345 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
3349 cfm.mpid = mon->mpid;
3350 cfm.interval = mon->interval ? *mon->interval : 1000;
3352 memcpy(cfm.maid, maid, sizeof cfm.maid);
3354 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
3355 for(i = 0; i < mon->n_remote_mps; i++) {
3356 remote_mps[i] = mon->remote_mps[i]->mpid;
3359 ofproto_iface_set_cfm(iface->port->bridge->ofproto, iface->dp_ifidx,
3360 &cfm, remote_mps, mon->n_remote_mps);
3364 /* Read carrier or miimon status directly from 'iface''s netdev, according to
3365 * how 'iface''s port is configured.
3367 * Returns true if 'iface' is up, false otherwise. */
3369 iface_get_carrier(const struct iface *iface)
3372 return netdev_get_carrier(iface->netdev);
3375 /* Port mirroring. */
3377 static struct mirror *
3378 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
3382 for (i = 0; i < MAX_MIRRORS; i++) {
3383 struct mirror *m = br->mirrors[i];
3384 if (m && uuid_equals(uuid, &m->uuid)) {
3392 mirror_reconfigure(struct bridge *br)
3394 unsigned long *rspan_vlans;
3398 /* Get rid of deleted mirrors. */
3399 for (i = 0; i < MAX_MIRRORS; i++) {
3400 struct mirror *m = br->mirrors[i];
3402 const struct ovsdb_datum *mc;
3403 union ovsdb_atom atom;
3405 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
3406 atom.uuid = br->mirrors[i]->uuid;
3407 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3413 /* Add new mirrors and reconfigure existing ones. */
3414 for (i = 0; i < br->cfg->n_mirrors; i++) {
3415 struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
3416 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
3418 mirror_reconfigure_one(m, cfg);
3420 mirror_create(br, cfg);
3424 /* Update port reserved status. */
3425 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3426 port->is_mirror_output_port = false;
3428 for (i = 0; i < MAX_MIRRORS; i++) {
3429 struct mirror *m = br->mirrors[i];
3430 if (m && m->out_port) {
3431 m->out_port->is_mirror_output_port = true;
3435 /* Update flooded vlans (for RSPAN). */
3437 if (br->cfg->n_flood_vlans) {
3438 rspan_vlans = bitmap_allocate(4096);
3440 for (i = 0; i < br->cfg->n_flood_vlans; i++) {
3441 int64_t vlan = br->cfg->flood_vlans[i];
3442 if (vlan >= 0 && vlan < 4096) {
3443 bitmap_set1(rspan_vlans, vlan);
3444 VLOG_INFO("bridge %s: disabling learning on vlan %"PRId64,
3447 VLOG_ERR("bridge %s: invalid value %"PRId64 "for flood VLAN",
3452 if (mac_learning_set_flood_vlans(br->ml, rspan_vlans)) {
3454 mac_learning_flush(br->ml);
3459 mirror_create(struct bridge *br, struct ovsrec_mirror *cfg)
3464 for (i = 0; ; i++) {
3465 if (i >= MAX_MIRRORS) {
3466 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
3467 "cannot create %s", br->name, MAX_MIRRORS, cfg->name);
3470 if (!br->mirrors[i]) {
3475 VLOG_INFO("created port mirror %s on bridge %s", cfg->name, br->name);
3477 mac_learning_flush(br->ml);
3479 br->mirrors[i] = m = xzalloc(sizeof *m);
3482 m->name = xstrdup(cfg->name);
3483 sset_init(&m->src_ports);
3484 sset_init(&m->dst_ports);
3490 mirror_reconfigure_one(m, cfg);
3494 mirror_destroy(struct mirror *m)
3497 struct bridge *br = m->bridge;
3500 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3501 port->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3502 port->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3505 sset_destroy(&m->src_ports);
3506 sset_destroy(&m->dst_ports);
3509 m->bridge->mirrors[m->idx] = NULL;
3514 mac_learning_flush(br->ml);
3519 mirror_collect_ports(struct mirror *m, struct ovsrec_port **ports, int n_ports,
3524 for (i = 0; i < n_ports; i++) {
3525 const char *name = ports[i]->name;
3526 if (port_lookup(m->bridge, name)) {
3527 sset_add(names, name);
3529 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
3530 "port %s", m->bridge->name, m->name, name);
3536 mirror_collect_vlans(struct mirror *m, const struct ovsrec_mirror *cfg,
3542 *vlans = xmalloc(sizeof **vlans * cfg->n_select_vlan);
3544 for (i = 0; i < cfg->n_select_vlan; i++) {
3545 int64_t vlan = cfg->select_vlan[i];
3546 if (vlan < 0 || vlan > 4095) {
3547 VLOG_WARN("bridge %s: mirror %s selects invalid VLAN %"PRId64,
3548 m->bridge->name, m->name, vlan);
3550 (*vlans)[n_vlans++] = vlan;
3557 vlan_is_mirrored(const struct mirror *m, int vlan)
3561 for (i = 0; i < m->n_vlans; i++) {
3562 if (m->vlans[i] == vlan) {
3570 port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
3574 for (i = 0; i < m->n_vlans; i++) {
3575 if (port_trunks_vlan(p, m->vlans[i])) {
3583 mirror_reconfigure_one(struct mirror *m, struct ovsrec_mirror *cfg)
3585 struct sset src_ports, dst_ports;
3586 mirror_mask_t mirror_bit;
3587 struct port *out_port;
3594 if (strcmp(cfg->name, m->name)) {
3596 m->name = xstrdup(cfg->name);
3599 /* Get output port. */
3600 if (cfg->output_port) {
3601 out_port = port_lookup(m->bridge, cfg->output_port->name);
3603 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
3604 m->bridge->name, m->name);
3610 if (cfg->output_vlan) {
3611 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
3612 "output vlan; ignoring output vlan",
3613 m->bridge->name, m->name);
3615 } else if (cfg->output_vlan) {
3617 out_vlan = *cfg->output_vlan;
3619 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
3620 m->bridge->name, m->name);
3625 sset_init(&src_ports);
3626 sset_init(&dst_ports);
3627 if (cfg->select_all) {
3628 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3629 sset_add(&src_ports, port->name);
3630 sset_add(&dst_ports, port->name);
3635 /* Get ports, and drop duplicates and ports that don't exist. */
3636 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
3638 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
3641 /* Get all the vlans, and drop duplicate and invalid vlans. */
3642 n_vlans = mirror_collect_vlans(m, cfg, &vlans);
3645 /* Update mirror data. */
3646 if (!sset_equals(&m->src_ports, &src_ports)
3647 || !sset_equals(&m->dst_ports, &dst_ports)
3648 || m->n_vlans != n_vlans
3649 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
3650 || m->out_port != out_port
3651 || m->out_vlan != out_vlan) {
3652 bridge_flush(m->bridge);
3653 mac_learning_flush(m->bridge->ml);
3655 sset_swap(&m->src_ports, &src_ports);
3656 sset_swap(&m->dst_ports, &dst_ports);
3659 m->n_vlans = n_vlans;
3660 m->out_port = out_port;
3661 m->out_vlan = out_vlan;
3664 mirror_bit = MIRROR_MASK_C(1) << m->idx;
3665 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3666 if (sset_contains(&m->src_ports, port->name)
3669 ? port_trunks_any_mirrored_vlan(m, port)
3670 : vlan_is_mirrored(m, port->vlan)))) {
3671 port->src_mirrors |= mirror_bit;
3673 port->src_mirrors &= ~mirror_bit;
3676 if (sset_contains(&m->dst_ports, port->name)) {
3677 port->dst_mirrors |= mirror_bit;
3679 port->dst_mirrors &= ~mirror_bit;
3684 sset_destroy(&src_ports);
3685 sset_destroy(&dst_ports);