1 /* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include "byte-order.h"
21 #include <arpa/inet.h>
24 #include <sys/socket.h>
26 #include <openflow/openflow.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
37 #include "classifier.h"
42 #include "dynamic-string.h"
49 #include "mac-learning.h"
53 #include "ofp-print.h"
55 #include "ofproto/netflow.h"
56 #include "ofproto/ofproto.h"
57 #include "ovsdb-data.h"
59 #include "poll-loop.h"
63 #include "socket-util.h"
64 #include "stream-ssl.h"
67 #include "system-stats.h"
72 #include "vswitchd/vswitch-idl.h"
73 #include "xenserver.h"
75 #include "sflow_api.h"
76 #include "vlan-bitmap.h"
78 VLOG_DEFINE_THIS_MODULE(bridge);
80 COVERAGE_DEFINE(bridge_flush);
81 COVERAGE_DEFINE(bridge_process_flow);
82 COVERAGE_DEFINE(bridge_reconfigure);
90 struct dst builtin[32];
95 static void dst_set_init(struct dst_set *);
96 static void dst_set_add(struct dst_set *, const struct dst *);
97 static void dst_set_free(struct dst_set *);
100 /* These members are always valid. */
101 struct list port_elem; /* Element in struct port's "ifaces" list. */
102 struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
103 struct port *port; /* Containing port. */
104 char *name; /* Host network device name. */
105 tag_type tag; /* Tag associated with this interface. */
107 /* These members are valid only after bridge_reconfigure() causes them to
109 struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
110 int dp_ifidx; /* Index within kernel datapath. */
111 struct netdev *netdev; /* Network device. */
112 const char *type; /* Usually same as cfg->type. */
113 const struct ovsrec_interface *cfg;
116 #define MAX_MIRRORS 32
117 typedef uint32_t mirror_mask_t;
118 #define MIRROR_MASK_C(X) UINT32_C(X)
119 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
121 struct bridge *bridge;
124 struct uuid uuid; /* UUID of this "mirror" record in database. */
126 /* Selection criteria. */
127 struct sset src_ports; /* Source port names. */
128 struct sset dst_ports; /* Destination port names. */
133 struct port *out_port;
137 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
139 struct bridge *bridge;
140 struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
143 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
144 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
145 * NULL if all VLANs are trunked. */
146 const struct ovsrec_port *cfg;
148 /* An ordinary bridge port has 1 interface.
149 * A bridge port for bonding has at least 2 interfaces. */
150 struct list ifaces; /* List of "struct iface"s. */
152 struct lacp *lacp; /* NULL if LACP is not enabled. */
157 /* Port mirroring info. */
158 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
159 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
160 bool is_mirror_output_port; /* Does port mirroring send frames here? */
164 struct list node; /* Node in global list of bridges. */
165 char *name; /* User-specified arbitrary name. */
166 struct mac_learning *ml; /* MAC learning table. */
167 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
168 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
169 const struct ovsrec_bridge *cfg;
171 /* OpenFlow switch processing. */
172 struct ofproto *ofproto; /* OpenFlow switch. */
175 struct hmap ports; /* "struct port"s indexed by name. */
176 struct hmap ifaces; /* "struct iface"s indexed by dp_ifidx. */
177 struct hmap iface_by_name; /* "struct iface"s indexed by name. */
180 bool has_bonded_ports;
185 /* Port mirroring. */
186 struct mirror *mirrors[MAX_MIRRORS];
188 /* Synthetic local port if necessary. */
189 struct ovsrec_port synth_local_port;
190 struct ovsrec_interface synth_local_iface;
191 struct ovsrec_interface *synth_local_ifacep;
194 /* List of all bridges. */
195 static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
197 /* OVSDB IDL used to obtain configuration. */
198 static struct ovsdb_idl *idl;
200 /* Each time this timer expires, the bridge fetches systems and interface
201 * statistics and pushes them into the database. */
202 #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
203 static long long int stats_timer = LLONG_MIN;
205 /* Stores the time after which rate limited statistics may be written to the
206 * database. Only updated when changes to the database require rate limiting.
208 #define DB_LIMIT_INTERVAL (1 * 1000) /* In milliseconds. */
209 static long long int db_limiter = LLONG_MIN;
211 static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
212 static void bridge_destroy(struct bridge *);
213 static struct bridge *bridge_lookup(const char *name);
214 static unixctl_cb_func bridge_unixctl_dump_flows;
215 static unixctl_cb_func bridge_unixctl_reconnect;
216 static int bridge_run_one(struct bridge *);
217 static size_t bridge_get_controllers(const struct bridge *br,
218 struct ovsrec_controller ***controllersp);
219 static void bridge_reconfigure_one(struct bridge *);
220 static void bridge_reconfigure_remotes(struct bridge *,
221 const struct sockaddr_in *managers,
223 static void bridge_get_all_ifaces(const struct bridge *, struct shash *ifaces);
224 static void bridge_fetch_dp_ifaces(struct bridge *);
225 static void bridge_flush(struct bridge *);
226 static void bridge_pick_local_hw_addr(struct bridge *,
227 uint8_t ea[ETH_ADDR_LEN],
228 struct iface **hw_addr_iface);
229 static uint64_t bridge_pick_datapath_id(struct bridge *,
230 const uint8_t bridge_ea[ETH_ADDR_LEN],
231 struct iface *hw_addr_iface);
232 static uint64_t dpid_from_hash(const void *, size_t nbytes);
234 static unixctl_cb_func bridge_unixctl_fdb_show;
235 static unixctl_cb_func cfm_unixctl_show;
236 static unixctl_cb_func qos_unixctl_show;
238 static void port_run(struct port *);
239 static void port_wait(struct port *);
240 static struct port *port_create(struct bridge *, const char *name);
241 static void port_reconfigure(struct port *, const struct ovsrec_port *);
242 static void port_del_ifaces(struct port *, const struct ovsrec_port *);
243 static void port_destroy(struct port *);
244 static struct port *port_lookup(const struct bridge *, const char *name);
245 static struct iface *port_get_an_iface(const struct port *);
246 static struct port *port_from_dp_ifidx(const struct bridge *,
248 static void port_reconfigure_lacp(struct port *);
249 static void port_reconfigure_bond(struct port *);
250 static void port_send_learning_packets(struct port *);
252 static void mirror_create(struct bridge *, struct ovsrec_mirror *);
253 static void mirror_destroy(struct mirror *);
254 static void mirror_reconfigure(struct bridge *);
255 static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
256 static bool vlan_is_mirrored(const struct mirror *, int vlan);
258 static struct iface *iface_create(struct port *port,
259 const struct ovsrec_interface *if_cfg);
260 static void iface_destroy(struct iface *);
261 static struct iface *iface_lookup(const struct bridge *, const char *name);
262 static struct iface *iface_find(const char *name);
263 static struct iface *iface_from_dp_ifidx(const struct bridge *,
265 static void iface_set_mac(struct iface *);
266 static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
267 static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
268 static void iface_update_cfm(struct iface *);
269 static bool iface_refresh_cfm_stats(struct iface *iface);
270 static bool iface_get_carrier(const struct iface *);
271 static bool iface_is_synthetic(const struct iface *);
273 static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
275 static void shash_to_ovs_idl_map(struct shash *,
276 char ***keys, char ***values, size_t *n);
278 /* Hooks into ofproto processing. */
279 static struct ofhooks bridge_ofhooks;
281 /* Public functions. */
283 /* Initializes the bridge module, configuring it to obtain its configuration
284 * from an OVSDB server accessed over 'remote', which should be a string in a
285 * form acceptable to ovsdb_idl_create(). */
287 bridge_init(const char *remote)
289 /* Create connection to database. */
290 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
292 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
293 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
294 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
295 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
296 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
297 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
298 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
300 ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
301 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
303 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
304 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
306 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
307 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
308 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
309 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
310 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
311 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
312 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
313 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
314 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
316 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
317 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
318 ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
319 ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
321 ovsdb_idl_omit_alert(idl, &ovsrec_maintenance_point_col_fault);
323 ovsdb_idl_omit_alert(idl, &ovsrec_monitor_col_fault);
325 ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
327 ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
329 ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
331 ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
333 ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
335 ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
336 ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
337 ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
338 ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
339 ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
341 ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
343 /* Register unixctl commands. */
344 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
345 unixctl_command_register("cfm/show", cfm_unixctl_show, NULL);
346 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
347 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
349 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
358 struct bridge *br, *next_br;
360 LIST_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
363 ovsdb_idl_destroy(idl);
366 /* Performs configuration that is only necessary once at ovs-vswitchd startup,
367 * but for which the ovs-vswitchd configuration 'cfg' is required. */
369 bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
371 static bool already_configured_once;
372 struct sset bridge_names;
373 struct sset dpif_names, dpif_types;
377 /* Only do this once per ovs-vswitchd run. */
378 if (already_configured_once) {
381 already_configured_once = true;
383 stats_timer = time_msec() + STATS_INTERVAL;
385 /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
386 sset_init(&bridge_names);
387 for (i = 0; i < cfg->n_bridges; i++) {
388 sset_add(&bridge_names, cfg->bridges[i]->name);
391 /* Iterate over all system dpifs and delete any of them that do not appear
393 sset_init(&dpif_names);
394 sset_init(&dpif_types);
395 dp_enumerate_types(&dpif_types);
396 SSET_FOR_EACH (type, &dpif_types) {
399 dp_enumerate_names(type, &dpif_names);
401 /* Delete each dpif whose name is not in 'bridge_names'. */
402 SSET_FOR_EACH (name, &dpif_names) {
403 if (!sset_contains(&bridge_names, name)) {
407 retval = dpif_open(name, type, &dpif);
415 sset_destroy(&bridge_names);
416 sset_destroy(&dpif_names);
417 sset_destroy(&dpif_types);
420 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
421 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
422 * responsible for freeing '*managersp' (with free()).
424 * You may be asking yourself "why does ovs-vswitchd care?", because
425 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
426 * should not be and in fact is not directly involved in that. But
427 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
428 * it has to tell in-band control where the managers are to enable that.
429 * (Thus, only managers connected in-band are collected.)
432 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
433 struct sockaddr_in **managersp, size_t *n_managersp)
435 struct sockaddr_in *managers = NULL;
436 size_t n_managers = 0;
440 /* Collect all of the potential targets from the "targets" columns of the
441 * rows pointed to by "manager_options", excluding any that are
444 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
445 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
447 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
448 sset_find_and_delete(&targets, m->target);
450 sset_add(&targets, m->target);
454 /* Now extract the targets' IP addresses. */
455 if (!sset_is_empty(&targets)) {
458 managers = xmalloc(sset_count(&targets) * sizeof *managers);
459 SSET_FOR_EACH (target, &targets) {
460 struct sockaddr_in *sin = &managers[n_managers];
462 if ((!strncmp(target, "tcp:", 4)
463 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
464 (!strncmp(target, "ssl:", 4)
465 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
470 sset_destroy(&targets);
472 *managersp = managers;
473 *n_managersp = n_managers;
477 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
479 struct shash old_br, new_br;
480 struct shash_node *node;
481 struct bridge *br, *next;
482 struct sockaddr_in *managers;
485 int sflow_bridge_number;
487 COVERAGE_INC(bridge_reconfigure);
489 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
491 /* Collect old and new bridges. */
494 LIST_FOR_EACH (br, node, &all_bridges) {
495 shash_add(&old_br, br->name, br);
497 for (i = 0; i < ovs_cfg->n_bridges; i++) {
498 const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
499 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
500 VLOG_WARN("more than one bridge named %s", br_cfg->name);
504 /* Get rid of deleted bridges and add new bridges. */
505 LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
506 struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
513 SHASH_FOR_EACH (node, &new_br) {
514 const char *br_name = node->name;
515 const struct ovsrec_bridge *br_cfg = node->data;
516 br = shash_find_data(&old_br, br_name);
518 /* If the bridge datapath type has changed, we need to tear it
519 * down and recreate. */
520 if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
522 bridge_create(br_cfg);
525 bridge_create(br_cfg);
528 shash_destroy(&old_br);
529 shash_destroy(&new_br);
531 /* Reconfigure all bridges. */
532 LIST_FOR_EACH (br, node, &all_bridges) {
533 bridge_reconfigure_one(br);
536 /* Add and delete ports on all datapaths.
538 * The kernel will reject any attempt to add a given port to a datapath if
539 * that port already belongs to a different datapath, so we must do all
540 * port deletions before any port additions. */
541 LIST_FOR_EACH (br, node, &all_bridges) {
542 struct ofproto_port ofproto_port;
543 struct ofproto_port_dump dump;
544 struct shash want_ifaces;
546 bridge_get_all_ifaces(br, &want_ifaces);
547 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
548 if (!shash_find(&want_ifaces, ofproto_port.name)
549 && strcmp(ofproto_port.name, br->name)) {
550 int retval = ofproto_port_del(br->ofproto,
551 ofproto_port.ofp_port);
553 VLOG_WARN("bridge %s: failed to remove %s interface (%s)",
554 br->name, ofproto_port.name, strerror(retval));
558 shash_destroy(&want_ifaces);
560 LIST_FOR_EACH (br, node, &all_bridges) {
561 struct shash cur_ifaces, want_ifaces;
562 struct ofproto_port ofproto_port;
563 struct ofproto_port_dump dump;
565 /* Get the set of interfaces currently in this datapath. */
566 shash_init(&cur_ifaces);
567 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
568 struct ofproto_port *port_info = xmalloc(sizeof *port_info);
569 ofproto_port_clone(port_info, &ofproto_port);
570 shash_add(&cur_ifaces, ofproto_port.name, port_info);
573 /* Get the set of interfaces we want on this datapath. */
574 bridge_get_all_ifaces(br, &want_ifaces);
576 hmap_clear(&br->ifaces);
577 SHASH_FOR_EACH (node, &want_ifaces) {
578 const char *if_name = node->name;
579 struct iface *iface = node->data;
580 struct ofproto_port *ofproto_port;
584 type = iface ? iface->type : "internal";
585 ofproto_port = shash_find_data(&cur_ifaces, if_name);
587 /* If we have a port or a netdev already, and it's not the type we
588 * want, then delete the port (if any) and close the netdev (if
590 if ((ofproto_port && strcmp(ofproto_port->type, type))
591 || (iface && iface->netdev
592 && strcmp(type, netdev_get_type(iface->netdev)))) {
594 error = ofproto_port_del(br->ofproto,
595 ofproto_port->ofp_port);
602 if (iface->port->bond) {
603 /* The bond has a pointer to the netdev, so remove it
604 * from the bond before closing the netdev. The slave
605 * will get added back to the bond later, after a new
606 * netdev is available. */
607 bond_slave_unregister(iface->port->bond, iface);
609 netdev_close(iface->netdev);
610 iface->netdev = NULL;
614 /* If the port doesn't exist or we don't have the netdev open,
615 * we need to do more work. */
616 if (!ofproto_port || (iface && !iface->netdev)) {
617 struct netdev_options options;
618 struct netdev *netdev;
621 /* First open the network device. */
622 options.name = if_name;
624 options.args = &args;
625 options.ethertype = NETDEV_ETH_TYPE_NONE;
629 shash_from_ovs_idl_map(iface->cfg->key_options,
630 iface->cfg->value_options,
631 iface->cfg->n_options, &args);
633 error = netdev_open(&options, &netdev);
634 shash_destroy(&args);
637 VLOG_WARN("could not open network device %s (%s)",
638 if_name, strerror(error));
642 /* Then add the port if we haven't already. */
644 error = ofproto_port_add(br->ofproto, netdev, NULL);
646 netdev_close(netdev);
647 if (error == EFBIG) {
648 VLOG_ERR("bridge %s: out of valid port numbers",
652 VLOG_WARN("bridge %s: failed to add %s interface "
654 br->name, if_name, strerror(error));
660 /* Update 'iface'. */
662 iface->netdev = netdev;
664 } else if (iface && iface->netdev) {
668 shash_from_ovs_idl_map(iface->cfg->key_options,
669 iface->cfg->value_options,
670 iface->cfg->n_options, &args);
671 netdev_set_config(iface->netdev, &args);
672 shash_destroy(&args);
675 shash_destroy(&want_ifaces);
677 SHASH_FOR_EACH (node, &cur_ifaces) {
678 struct ofproto_port *port_info = node->data;
679 ofproto_port_destroy(port_info);
682 shash_destroy(&cur_ifaces);
684 sflow_bridge_number = 0;
685 LIST_FOR_EACH (br, node, &all_bridges) {
686 uint8_t ea[ETH_ADDR_LEN];
688 struct iface *local_iface;
689 struct port *port, *next_port;
690 struct iface *hw_addr_iface;
693 bridge_fetch_dp_ifaces(br);
695 /* Delete interfaces that cannot be opened.
697 * Following this loop, every remaining "struct iface" has nonnull
698 * 'netdev' and correct 'dp_ifidx'. */
699 HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
700 struct iface *iface, *next_iface;
702 LIST_FOR_EACH_SAFE (iface, next_iface, port_elem, &port->ifaces) {
703 if (iface->netdev && iface->dp_ifidx >= 0) {
704 VLOG_DBG("bridge %s: interface %s is on port %d",
705 br->name, iface->name, iface->dp_ifidx);
708 VLOG_ERR("bridge %s: missing %s interface, dropping",
709 br->name, iface->name);
711 /* We already reported a related error, don't bother
715 iface_set_ofport(iface->cfg, -1);
716 iface_destroy(iface);
720 if (list_is_empty(&port->ifaces)) {
721 VLOG_WARN("%s port has no interfaces, dropping", port->name);
726 /* Pick local port hardware address, datapath ID. */
727 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
728 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
730 int error = netdev_set_etheraddr(local_iface->netdev, ea);
732 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
733 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
734 "Ethernet address: %s",
735 br->name, strerror(error));
738 memcpy(br->ea, ea, ETH_ADDR_LEN);
740 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
741 ofproto_set_datapath_id(br->ofproto, dpid);
743 dpid_string = xasprintf("%016"PRIx64, dpid);
744 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
747 /* Set NetFlow configuration on this bridge. */
748 if (br->cfg->netflow) {
749 struct ovsrec_netflow *nf_cfg = br->cfg->netflow;
750 struct netflow_options opts;
752 memset(&opts, 0, sizeof opts);
754 ofproto_get_netflow_ids(br->ofproto,
755 &opts.engine_type, &opts.engine_id);
756 if (nf_cfg->engine_type) {
757 opts.engine_type = *nf_cfg->engine_type;
759 if (nf_cfg->engine_id) {
760 opts.engine_id = *nf_cfg->engine_id;
763 opts.active_timeout = nf_cfg->active_timeout;
764 if (!opts.active_timeout) {
765 opts.active_timeout = -1;
766 } else if (opts.active_timeout < 0) {
767 VLOG_WARN("bridge %s: active timeout interval set to negative "
768 "value, using default instead (%d seconds)", br->name,
769 NF_ACTIVE_TIMEOUT_DEFAULT);
770 opts.active_timeout = -1;
773 opts.add_id_to_iface = nf_cfg->add_id_to_interface;
774 if (opts.add_id_to_iface) {
775 if (opts.engine_id > 0x7f) {
776 VLOG_WARN("bridge %s: netflow port mangling may conflict "
777 "with another vswitch, choose an engine id less "
778 "than 128", br->name);
780 if (hmap_count(&br->ports) > 508) {
781 VLOG_WARN("bridge %s: netflow port mangling will conflict "
782 "with another port when more than 508 ports are "
787 sset_init(&opts.collectors);
788 sset_add_array(&opts.collectors,
789 nf_cfg->targets, nf_cfg->n_targets);
790 if (ofproto_set_netflow(br->ofproto, &opts)) {
791 VLOG_ERR("bridge %s: problem setting netflow collectors",
794 sset_destroy(&opts.collectors);
796 ofproto_set_netflow(br->ofproto, NULL);
799 /* Set sFlow configuration on this bridge. */
800 if (br->cfg->sflow) {
801 const struct ovsrec_sflow *sflow_cfg = br->cfg->sflow;
802 struct ovsrec_controller **controllers;
803 struct ofproto_sflow_options oso;
804 size_t n_controllers;
806 memset(&oso, 0, sizeof oso);
808 sset_init(&oso.targets);
809 sset_add_array(&oso.targets,
810 sflow_cfg->targets, sflow_cfg->n_targets);
812 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
813 if (sflow_cfg->sampling) {
814 oso.sampling_rate = *sflow_cfg->sampling;
817 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
818 if (sflow_cfg->polling) {
819 oso.polling_interval = *sflow_cfg->polling;
822 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
823 if (sflow_cfg->header) {
824 oso.header_len = *sflow_cfg->header;
827 oso.sub_id = sflow_bridge_number++;
828 oso.agent_device = sflow_cfg->agent;
830 oso.control_ip = NULL;
831 n_controllers = bridge_get_controllers(br, &controllers);
832 for (i = 0; i < n_controllers; i++) {
833 if (controllers[i]->local_ip) {
834 oso.control_ip = controllers[i]->local_ip;
838 ofproto_set_sflow(br->ofproto, &oso);
840 sset_destroy(&oso.targets);
842 ofproto_set_sflow(br->ofproto, NULL);
845 /* Update the controller and related settings. It would be more
846 * straightforward to call this from bridge_reconfigure_one(), but we
847 * can't do it there for two reasons. First, and most importantly, at
848 * that point we don't know the dp_ifidx of any interfaces that have
849 * been added to the bridge (because we haven't actually added them to
850 * the datapath). Second, at that point we haven't set the datapath ID
851 * yet; when a controller is configured, resetting the datapath ID will
852 * immediately disconnect from the controller, so it's better to set
853 * the datapath ID before the controller. */
854 bridge_reconfigure_remotes(br, managers, n_managers);
856 LIST_FOR_EACH (br, node, &all_bridges) {
859 br->has_bonded_ports = false;
860 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
863 port_reconfigure_lacp(port);
864 port_reconfigure_bond(port);
866 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
867 iface_update_qos(iface, port->cfg->qos);
868 netdev_set_policing(iface->netdev,
869 iface->cfg->ingress_policing_rate,
870 iface->cfg->ingress_policing_burst);
871 iface_set_mac(iface);
876 /* Some reconfiguration operations require the bridge to have been run at
878 LIST_FOR_EACH (br, node, &all_bridges) {
883 HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
884 iface_update_cfm(iface);
890 /* ovs-vswitchd has completed initialization, so allow the process that
891 * forked us to exit successfully. */
892 daemonize_complete();
896 get_ovsrec_key_value(const struct ovsdb_idl_row *row,
897 const struct ovsdb_idl_column *column,
900 const struct ovsdb_datum *datum;
901 union ovsdb_atom atom;
904 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
905 atom.string = (char *) key;
906 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
907 return idx == UINT_MAX ? NULL : datum->values[idx].string;
911 bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
913 return get_ovsrec_key_value(&br_cfg->header_,
914 &ovsrec_bridge_col_other_config, key);
918 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
919 struct iface **hw_addr_iface)
925 *hw_addr_iface = NULL;
927 /* Did the user request a particular MAC? */
928 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
929 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
930 if (eth_addr_is_multicast(ea)) {
931 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
932 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
933 } else if (eth_addr_is_zero(ea)) {
934 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
940 /* Otherwise choose the minimum non-local MAC address among all of the
942 memset(ea, 0xff, ETH_ADDR_LEN);
943 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
944 uint8_t iface_ea[ETH_ADDR_LEN];
945 struct iface *candidate;
948 /* Mirror output ports don't participate. */
949 if (port->is_mirror_output_port) {
953 /* Choose the MAC address to represent the port. */
955 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
956 /* Find the interface with this Ethernet address (if any) so that
957 * we can provide the correct devname to the caller. */
958 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
959 uint8_t candidate_ea[ETH_ADDR_LEN];
960 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
961 && eth_addr_equals(iface_ea, candidate_ea)) {
966 /* Choose the interface whose MAC address will represent the port.
967 * The Linux kernel bonding code always chooses the MAC address of
968 * the first slave added to a bond, and the Fedora networking
969 * scripts always add slaves to a bond in alphabetical order, so
970 * for compatibility we choose the interface with the name that is
971 * first in alphabetical order. */
972 LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
973 if (!iface || strcmp(candidate->name, iface->name) < 0) {
978 /* The local port doesn't count (since we're trying to choose its
979 * MAC address anyway). */
980 if (iface->dp_ifidx == ODPP_LOCAL) {
985 error = netdev_get_etheraddr(iface->netdev, iface_ea);
987 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
988 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
989 iface->name, strerror(error));
994 /* Compare against our current choice. */
995 if (!eth_addr_is_multicast(iface_ea) &&
996 !eth_addr_is_local(iface_ea) &&
997 !eth_addr_is_reserved(iface_ea) &&
998 !eth_addr_is_zero(iface_ea) &&
999 eth_addr_compare_3way(iface_ea, ea) < 0)
1001 memcpy(ea, iface_ea, ETH_ADDR_LEN);
1002 *hw_addr_iface = iface;
1005 if (eth_addr_is_multicast(ea)) {
1006 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
1007 *hw_addr_iface = NULL;
1008 VLOG_WARN("bridge %s: using default bridge Ethernet "
1009 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1011 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1012 br->name, ETH_ADDR_ARGS(ea));
1016 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
1017 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
1018 * an interface on 'br', then that interface must be passed in as
1019 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1020 * 'hw_addr_iface' must be passed in as a null pointer. */
1022 bridge_pick_datapath_id(struct bridge *br,
1023 const uint8_t bridge_ea[ETH_ADDR_LEN],
1024 struct iface *hw_addr_iface)
1027 * The procedure for choosing a bridge MAC address will, in the most
1028 * ordinary case, also choose a unique MAC that we can use as a datapath
1029 * ID. In some special cases, though, multiple bridges will end up with
1030 * the same MAC address. This is OK for the bridges, but it will confuse
1031 * the OpenFlow controller, because each datapath needs a unique datapath
1034 * Datapath IDs must be unique. It is also very desirable that they be
1035 * stable from one run to the next, so that policy set on a datapath
1038 const char *datapath_id;
1041 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1042 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
1046 if (hw_addr_iface) {
1048 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
1050 * A bridge whose MAC address is taken from a VLAN network device
1051 * (that is, a network device created with vconfig(8) or similar
1052 * tool) will have the same MAC address as a bridge on the VLAN
1053 * device's physical network device.
1055 * Handle this case by hashing the physical network device MAC
1056 * along with the VLAN identifier.
1058 uint8_t buf[ETH_ADDR_LEN + 2];
1059 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1060 buf[ETH_ADDR_LEN] = vlan >> 8;
1061 buf[ETH_ADDR_LEN + 1] = vlan;
1062 return dpid_from_hash(buf, sizeof buf);
1065 * Assume that this bridge's MAC address is unique, since it
1066 * doesn't fit any of the cases we handle specially.
1071 * A purely internal bridge, that is, one that has no non-virtual
1072 * network devices on it at all, is more difficult because it has no
1073 * natural unique identifier at all.
1075 * When the host is a XenServer, we handle this case by hashing the
1076 * host's UUID with the name of the bridge. Names of bridges are
1077 * persistent across XenServer reboots, although they can be reused if
1078 * an internal network is destroyed and then a new one is later
1079 * created, so this is fairly effective.
1081 * When the host is not a XenServer, we punt by using a random MAC
1082 * address on each run.
1084 const char *host_uuid = xenserver_get_host_uuid();
1086 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1087 dpid = dpid_from_hash(combined, strlen(combined));
1093 return eth_addr_to_uint64(bridge_ea);
1097 dpid_from_hash(const void *data, size_t n)
1099 uint8_t hash[SHA1_DIGEST_SIZE];
1101 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
1102 sha1_bytes(data, n, hash);
1103 eth_addr_mark_random(hash);
1104 return eth_addr_to_uint64(hash);
1108 iface_refresh_status(struct iface *iface)
1112 enum netdev_flags flags;
1119 if (iface_is_synthetic(iface)) {
1125 if (!netdev_get_status(iface->netdev, &sh)) {
1127 char **keys, **values;
1129 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1130 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1135 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1138 shash_destroy_free_data(&sh);
1140 error = netdev_get_flags(iface->netdev, &flags);
1142 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1145 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1148 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1150 ovsrec_interface_set_duplex(iface->cfg,
1151 netdev_features_is_full_duplex(current)
1153 /* warning: uint64_t -> int64_t conversion */
1154 bps = netdev_features_to_bps(current);
1155 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1158 ovsrec_interface_set_duplex(iface->cfg, NULL);
1159 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1163 ovsrec_interface_set_link_state(iface->cfg,
1164 iface_get_carrier(iface) ? "up" : "down");
1166 error = netdev_get_mtu(iface->netdev, &mtu);
1167 if (!error && mtu != INT_MAX) {
1169 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1172 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1176 /* Writes 'iface''s CFM statistics to the database. Returns true if anything
1177 * changed, false otherwise. */
1179 iface_refresh_cfm_stats(struct iface *iface)
1181 const struct ovsrec_monitor *mon;
1182 const struct cfm *cfm;
1183 bool changed = false;
1186 mon = iface->cfg->monitor;
1187 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1193 for (i = 0; i < mon->n_remote_mps; i++) {
1194 const struct ovsrec_maintenance_point *mp;
1195 const struct remote_mp *rmp;
1197 mp = mon->remote_mps[i];
1198 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1200 if (mp->n_fault != 1 || mp->fault[0] != rmp->fault) {
1201 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1206 if (mon->n_fault != 1 || mon->fault[0] != cfm->fault) {
1207 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1215 iface_refresh_lacp_stats(struct iface *iface)
1217 bool *db_current = iface->cfg->lacp_current;
1218 bool changed = false;
1220 if (iface->port->lacp) {
1221 bool current = lacp_slave_is_current(iface->port->lacp, iface);
1223 if (!db_current || *db_current != current) {
1225 ovsrec_interface_set_lacp_current(iface->cfg, ¤t, 1);
1227 } else if (db_current) {
1229 ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
1236 iface_refresh_stats(struct iface *iface)
1242 static const struct iface_stat iface_stats[] = {
1243 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1244 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1245 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1246 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1247 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1248 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1249 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1250 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1251 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1252 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1253 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1254 { "collisions", offsetof(struct netdev_stats, collisions) },
1256 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1257 const struct iface_stat *s;
1259 char *keys[N_STATS];
1260 int64_t values[N_STATS];
1263 struct netdev_stats stats;
1265 if (iface_is_synthetic(iface)) {
1269 /* Intentionally ignore return value, since errors will set 'stats' to
1270 * all-1s, and we will deal with that correctly below. */
1271 netdev_get_stats(iface->netdev, &stats);
1274 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1275 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1276 if (value != UINT64_MAX) {
1283 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1287 refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1289 struct ovsdb_datum datum;
1293 get_system_stats(&stats);
1295 ovsdb_datum_from_shash(&datum, &stats);
1296 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1300 static inline const char *
1301 nx_role_to_str(enum nx_role role)
1306 case NX_ROLE_MASTER:
1311 return "*** INVALID ROLE ***";
1316 bridge_refresh_controller_status(const struct bridge *br)
1319 const struct ovsrec_controller *cfg;
1321 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1323 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
1324 struct ofproto_controller_info *cinfo =
1325 shash_find_data(&info, cfg->target);
1328 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1329 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1330 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1331 (char **) cinfo->pairs.values,
1334 ovsrec_controller_set_is_connected(cfg, false);
1335 ovsrec_controller_set_role(cfg, NULL);
1336 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1340 ofproto_free_ofproto_controller_info(&info);
1346 const struct ovsrec_open_vswitch *cfg;
1348 bool datapath_destroyed;
1349 bool database_changed;
1352 /* Let each bridge do the work that it needs to do. */
1353 datapath_destroyed = false;
1354 LIST_FOR_EACH (br, node, &all_bridges) {
1355 int error = bridge_run_one(br);
1357 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1358 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1359 "forcing reconfiguration", br->name);
1360 datapath_destroyed = true;
1364 /* (Re)configure if necessary. */
1365 database_changed = ovsdb_idl_run(idl);
1366 cfg = ovsrec_open_vswitch_first(idl);
1368 /* Re-configure SSL. We do this on every trip through the main loop,
1369 * instead of just when the database changes, because the contents of the
1370 * key and certificate files can change without the database changing.
1372 * We do this before bridge_reconfigure() because that function might
1373 * initiate SSL connections and thus requires SSL to be configured. */
1374 if (cfg && cfg->ssl) {
1375 const struct ovsrec_ssl *ssl = cfg->ssl;
1377 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1378 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1381 if (database_changed || datapath_destroyed) {
1383 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1385 bridge_configure_once(cfg);
1386 bridge_reconfigure(cfg);
1388 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1389 ovsdb_idl_txn_commit(txn);
1390 ovsdb_idl_txn_destroy(txn); /* XXX */
1392 /* We still need to reconfigure to avoid dangling pointers to
1393 * now-destroyed ovsrec structures inside bridge data. */
1394 static const struct ovsrec_open_vswitch null_cfg;
1396 bridge_reconfigure(&null_cfg);
1400 /* Refresh system and interface stats if necessary. */
1401 if (time_msec() >= stats_timer) {
1403 struct ovsdb_idl_txn *txn;
1405 txn = ovsdb_idl_txn_create(idl);
1406 LIST_FOR_EACH (br, node, &all_bridges) {
1409 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1410 struct iface *iface;
1412 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1413 iface_refresh_stats(iface);
1414 iface_refresh_status(iface);
1417 bridge_refresh_controller_status(br);
1419 refresh_system_stats(cfg);
1420 ovsdb_idl_txn_commit(txn);
1421 ovsdb_idl_txn_destroy(txn); /* XXX */
1424 stats_timer = time_msec() + STATS_INTERVAL;
1427 if (time_msec() >= db_limiter) {
1428 struct ovsdb_idl_txn *txn;
1429 bool changed = false;
1431 txn = ovsdb_idl_txn_create(idl);
1432 LIST_FOR_EACH (br, node, &all_bridges) {
1435 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1436 struct iface *iface;
1438 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
1439 changed = iface_refresh_cfm_stats(iface) || changed;
1440 changed = iface_refresh_lacp_stats(iface) || changed;
1446 db_limiter = time_msec() + DB_LIMIT_INTERVAL;
1449 ovsdb_idl_txn_commit(txn);
1450 ovsdb_idl_txn_destroy(txn);
1459 LIST_FOR_EACH (br, node, &all_bridges) {
1462 ofproto_wait(br->ofproto);
1463 mac_learning_wait(br->ml);
1464 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1468 ovsdb_idl_wait(idl);
1469 poll_timer_wait_until(stats_timer);
1471 if (db_limiter > time_msec()) {
1472 poll_timer_wait_until(db_limiter);
1476 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1477 * configuration changes. */
1479 bridge_flush(struct bridge *br)
1481 COVERAGE_INC(bridge_flush);
1485 /* Bridge unixctl user interface functions. */
1487 bridge_unixctl_fdb_show(struct unixctl_conn *conn,
1488 const char *args, void *aux OVS_UNUSED)
1490 struct ds ds = DS_EMPTY_INITIALIZER;
1491 const struct bridge *br;
1492 const struct mac_entry *e;
1494 br = bridge_lookup(args);
1496 unixctl_command_reply(conn, 501, "no such bridge");
1500 ds_put_cstr(&ds, " port VLAN MAC Age\n");
1501 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
1502 struct port *port = e->port.p;
1503 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1504 port_get_an_iface(port)->dp_ifidx,
1505 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
1507 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1511 /* CFM unixctl user interface functions. */
1513 cfm_unixctl_show(struct unixctl_conn *conn,
1514 const char *args, void *aux OVS_UNUSED)
1516 struct ds ds = DS_EMPTY_INITIALIZER;
1517 struct iface *iface;
1518 const struct cfm *cfm;
1520 iface = iface_find(args);
1522 unixctl_command_reply(conn, 501, "no such interface");
1526 cfm = ofproto_iface_get_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
1529 unixctl_command_reply(conn, 501, "CFM not enabled");
1533 cfm_dump_ds(cfm, &ds);
1534 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1538 /* QoS unixctl user interface functions. */
1540 struct qos_unixctl_show_cbdata {
1542 struct iface *iface;
1546 qos_unixctl_show_cb(unsigned int queue_id,
1547 const struct shash *details,
1550 struct qos_unixctl_show_cbdata *data = aux;
1551 struct ds *ds = data->ds;
1552 struct iface *iface = data->iface;
1553 struct netdev_queue_stats stats;
1554 struct shash_node *node;
1557 ds_put_cstr(ds, "\n");
1559 ds_put_format(ds, "Queue %u:\n", queue_id);
1561 ds_put_cstr(ds, "Default:\n");
1564 SHASH_FOR_EACH (node, details) {
1565 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1568 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1570 if (stats.tx_packets != UINT64_MAX) {
1571 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1574 if (stats.tx_bytes != UINT64_MAX) {
1575 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1578 if (stats.tx_errors != UINT64_MAX) {
1579 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1582 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1583 queue_id, strerror(error));
1588 qos_unixctl_show(struct unixctl_conn *conn,
1589 const char *args, void *aux OVS_UNUSED)
1591 struct ds ds = DS_EMPTY_INITIALIZER;
1592 struct shash sh = SHASH_INITIALIZER(&sh);
1593 struct iface *iface;
1595 struct shash_node *node;
1596 struct qos_unixctl_show_cbdata data;
1599 iface = iface_find(args);
1601 unixctl_command_reply(conn, 501, "no such interface");
1605 netdev_get_qos(iface->netdev, &type, &sh);
1607 if (*type != '\0') {
1608 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1610 SHASH_FOR_EACH (node, &sh) {
1611 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1616 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1619 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1621 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1623 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1624 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1627 shash_destroy_free_data(&sh);
1631 /* Bridge reconfiguration functions. */
1632 static struct bridge *
1633 bridge_create(const struct ovsrec_bridge *br_cfg)
1638 assert(!bridge_lookup(br_cfg->name));
1639 br = xzalloc(sizeof *br);
1641 error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
1644 VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
1650 br->name = xstrdup(br_cfg->name);
1652 br->ml = mac_learning_create();
1653 eth_addr_nicira_random(br->default_ea);
1655 hmap_init(&br->ports);
1656 hmap_init(&br->ifaces);
1657 hmap_init(&br->iface_by_name);
1661 list_push_back(&all_bridges, &br->node);
1663 VLOG_INFO("bridge %s: created", br->name);
1669 bridge_destroy(struct bridge *br)
1672 struct port *port, *next;
1675 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1678 for (i = 0; i < MAX_MIRRORS; i++) {
1679 mirror_destroy(br->mirrors[i]);
1681 list_remove(&br->node);
1682 ofproto_destroy_and_delete(br->ofproto);
1683 mac_learning_destroy(br->ml);
1684 hmap_destroy(&br->ifaces);
1685 hmap_destroy(&br->ports);
1686 hmap_destroy(&br->iface_by_name);
1687 free(br->synth_local_iface.type);
1693 static struct bridge *
1694 bridge_lookup(const char *name)
1698 LIST_FOR_EACH (br, node, &all_bridges) {
1699 if (!strcmp(br->name, name)) {
1706 /* Handle requests for a listing of all flows known by the OpenFlow
1707 * stack, including those normally hidden. */
1709 bridge_unixctl_dump_flows(struct unixctl_conn *conn,
1710 const char *args, void *aux OVS_UNUSED)
1715 br = bridge_lookup(args);
1717 unixctl_command_reply(conn, 501, "Unknown bridge");
1722 ofproto_get_all_flows(br->ofproto, &results);
1724 unixctl_command_reply(conn, 200, ds_cstr(&results));
1725 ds_destroy(&results);
1728 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1729 * connections and reconnect. If BRIDGE is not specified, then all bridges
1730 * drop their controller connections and reconnect. */
1732 bridge_unixctl_reconnect(struct unixctl_conn *conn,
1733 const char *args, void *aux OVS_UNUSED)
1736 if (args[0] != '\0') {
1737 br = bridge_lookup(args);
1739 unixctl_command_reply(conn, 501, "Unknown bridge");
1742 ofproto_reconnect_controllers(br->ofproto);
1744 LIST_FOR_EACH (br, node, &all_bridges) {
1745 ofproto_reconnect_controllers(br->ofproto);
1748 unixctl_command_reply(conn, 200, NULL);
1752 bridge_run_one(struct bridge *br)
1757 error = ofproto_run1(br->ofproto);
1762 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
1764 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
1768 error = ofproto_run2(br->ofproto, br->flush);
1775 bridge_get_controllers(const struct bridge *br,
1776 struct ovsrec_controller ***controllersp)
1778 struct ovsrec_controller **controllers;
1779 size_t n_controllers;
1781 controllers = br->cfg->controller;
1782 n_controllers = br->cfg->n_controller;
1784 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1790 *controllersp = controllers;
1792 return n_controllers;
1796 bridge_reconfigure_one(struct bridge *br)
1798 enum ofproto_fail_mode fail_mode;
1799 struct port *port, *next;
1800 struct shash_node *node;
1801 struct shash new_ports;
1804 /* Collect new ports. */
1805 shash_init(&new_ports);
1806 for (i = 0; i < br->cfg->n_ports; i++) {
1807 const char *name = br->cfg->ports[i]->name;
1808 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1809 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1813 if (bridge_get_controllers(br, NULL)
1814 && !shash_find(&new_ports, br->name)) {
1815 struct ofproto_port local_port;
1819 VLOG_WARN("bridge %s: no port named %s, synthesizing one",
1820 br->name, br->name);
1822 error = ofproto_port_query_by_name(br->ofproto, br->name, &local_port);
1823 type = xstrdup(error ? "internal" : local_port.type);
1824 ofproto_port_destroy(&local_port);
1826 br->synth_local_port.interfaces = &br->synth_local_ifacep;
1827 br->synth_local_port.n_interfaces = 1;
1828 br->synth_local_port.name = br->name;
1830 br->synth_local_iface.name = br->name;
1831 free(br->synth_local_iface.type);
1832 br->synth_local_iface.type = type;
1834 br->synth_local_ifacep = &br->synth_local_iface;
1836 shash_add(&new_ports, br->name, &br->synth_local_port);
1839 /* Get rid of deleted ports.
1840 * Get rid of deleted interfaces on ports that still exist. */
1841 HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
1842 const struct ovsrec_port *port_cfg;
1844 port_cfg = shash_find_data(&new_ports, port->name);
1848 port_del_ifaces(port, port_cfg);
1852 /* Create new ports.
1853 * Add new interfaces to existing ports.
1854 * Reconfigure existing ports. */
1855 SHASH_FOR_EACH (node, &new_ports) {
1856 struct port *port = port_lookup(br, node->name);
1858 port = port_create(br, node->name);
1861 port_reconfigure(port, node->data);
1862 if (list_is_empty(&port->ifaces)) {
1863 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1864 br->name, port->name);
1868 shash_destroy(&new_ports);
1870 /* Set the fail-mode */
1871 fail_mode = !br->cfg->fail_mode
1872 || !strcmp(br->cfg->fail_mode, "standalone")
1873 ? OFPROTO_FAIL_STANDALONE
1874 : OFPROTO_FAIL_SECURE;
1875 ofproto_set_fail_mode(br->ofproto, fail_mode);
1877 /* Configure OpenFlow controller connection snooping. */
1878 if (!ofproto_has_snoops(br->ofproto)) {
1882 sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
1883 ovs_rundir(), br->name));
1884 ofproto_set_snoops(br->ofproto, &snoops);
1885 sset_destroy(&snoops);
1888 mirror_reconfigure(br);
1891 /* Initializes 'oc' appropriately as a management service controller for
1894 * The caller must free oc->target when it is no longer needed. */
1896 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1897 struct ofproto_controller *oc)
1899 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
1900 oc->max_backoff = 0;
1901 oc->probe_interval = 60;
1902 oc->band = OFPROTO_OUT_OF_BAND;
1904 oc->burst_limit = 0;
1907 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1909 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1910 struct ofproto_controller *oc)
1912 oc->target = c->target;
1913 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1914 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1915 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1916 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
1917 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1918 oc->burst_limit = (c->controller_burst_limit
1919 ? *c->controller_burst_limit : 0);
1922 /* Configures the IP stack for 'br''s local interface properly according to the
1923 * configuration in 'c'. */
1925 bridge_configure_local_iface_netdev(struct bridge *br,
1926 struct ovsrec_controller *c)
1928 struct netdev *netdev;
1929 struct in_addr mask, gateway;
1931 struct iface *local_iface;
1934 /* If there's no local interface or no IP address, give up. */
1935 local_iface = iface_from_dp_ifidx(br, ODPP_LOCAL);
1936 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1940 /* Bring up the local interface. */
1941 netdev = local_iface->netdev;
1942 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1944 /* Configure the IP address and netmask. */
1945 if (!c->local_netmask
1946 || !inet_aton(c->local_netmask, &mask)
1948 mask.s_addr = guess_netmask(ip.s_addr);
1950 if (!netdev_set_in4(netdev, ip, mask)) {
1951 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1952 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1955 /* Configure the default gateway. */
1956 if (c->local_gateway
1957 && inet_aton(c->local_gateway, &gateway)
1958 && gateway.s_addr) {
1959 if (!netdev_add_router(netdev, gateway)) {
1960 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
1961 br->name, IP_ARGS(&gateway.s_addr));
1967 bridge_reconfigure_remotes(struct bridge *br,
1968 const struct sockaddr_in *managers,
1971 const char *disable_ib_str, *queue_id_str;
1972 bool disable_in_band = false;
1975 struct ovsrec_controller **controllers;
1976 size_t n_controllers;
1978 struct ofproto_controller *ocs;
1982 /* Check if we should disable in-band control on this bridge. */
1983 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
1984 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
1985 disable_in_band = true;
1988 /* Set OpenFlow queue ID for in-band control. */
1989 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
1990 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
1991 ofproto_set_in_band_queue(br->ofproto, queue_id);
1993 if (disable_in_band) {
1994 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
1996 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
1999 n_controllers = bridge_get_controllers(br, &controllers);
2001 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
2004 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
2005 for (i = 0; i < n_controllers; i++) {
2006 struct ovsrec_controller *c = controllers[i];
2008 if (!strncmp(c->target, "punix:", 6)
2009 || !strncmp(c->target, "unix:", 5)) {
2010 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2012 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
2013 * domain sockets and overwriting arbitrary local files. */
2014 VLOG_ERR_RL(&rl, "bridge %s: not adding Unix domain socket "
2015 "controller \"%s\" due to possibility for remote "
2016 "exploit", br->name, c->target);
2020 bridge_configure_local_iface_netdev(br, c);
2021 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
2022 if (disable_in_band) {
2023 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
2028 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
2029 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
2034 bridge_get_all_ifaces(const struct bridge *br, struct shash *ifaces)
2039 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2040 struct iface *iface;
2042 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2043 shash_add_once(ifaces, iface->name, iface);
2045 if (!list_is_short(&port->ifaces) && port->cfg->bond_fake_iface) {
2046 shash_add_once(ifaces, port->name, NULL);
2051 /* For robustness, in case the administrator moves around datapath ports behind
2052 * our back, we re-check all the datapath port numbers here.
2054 * This function will set the 'dp_ifidx' members of interfaces that have
2055 * disappeared to -1, so only call this function from a context where those
2056 * 'struct iface's will be removed from the bridge. Otherwise, the -1
2057 * 'dp_ifidx'es will cause trouble later when we try to send them to the
2058 * datapath, which doesn't support UINT16_MAX+1 ports. */
2060 bridge_fetch_dp_ifaces(struct bridge *br)
2062 struct ofproto_port ofproto_port;
2063 struct ofproto_port_dump dump;
2066 /* Reset all interface numbers. */
2067 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2068 struct iface *iface;
2070 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2071 iface->dp_ifidx = -1;
2074 hmap_clear(&br->ifaces);
2076 OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
2077 struct iface *iface = iface_lookup(br, ofproto_port.name);
2079 uint32_t odp_port = ofp_port_to_odp_port(ofproto_port.ofp_port);
2081 if (iface->dp_ifidx >= 0) {
2082 VLOG_WARN("bridge %s: interface %s reported twice",
2083 br->name, ofproto_port.name);
2084 } else if (iface_from_dp_ifidx(br, odp_port)) {
2085 VLOG_WARN("bridge %s: interface %"PRIu16" reported twice",
2086 br->name, odp_port);
2088 iface->dp_ifidx = odp_port;
2089 hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
2090 hash_int(iface->dp_ifidx, 0));
2093 iface_set_ofport(iface->cfg,
2094 (iface->dp_ifidx >= 0
2095 ? odp_port_to_ofp_port(iface->dp_ifidx)
2101 /* Bridge packet processing functions. */
2104 set_dst(struct dst *dst, const struct flow *flow,
2105 const struct port *in_port, const struct port *out_port,
2108 dst->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
2109 : in_port->vlan >= 0 ? in_port->vlan
2110 : flow->vlan_tci == 0 ? OFP_VLAN_NONE
2111 : vlan_tci_to_vid(flow->vlan_tci));
2113 dst->iface = (!out_port->bond
2114 ? port_get_an_iface(out_port)
2115 : bond_choose_output_slave(out_port->bond, flow,
2118 return dst->iface != NULL;
2122 mirror_mask_ffs(mirror_mask_t mask)
2124 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2129 dst_set_init(struct dst_set *set)
2131 set->dsts = set->builtin;
2133 set->allocated = ARRAY_SIZE(set->builtin);
2137 dst_set_add(struct dst_set *set, const struct dst *dst)
2139 if (set->n >= set->allocated) {
2140 size_t new_allocated;
2141 struct dst *new_dsts;
2143 new_allocated = set->allocated * 2;
2144 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2145 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2149 set->dsts = new_dsts;
2150 set->allocated = new_allocated;
2152 set->dsts[set->n++] = *dst;
2156 dst_set_free(struct dst_set *set)
2158 if (set->dsts != set->builtin) {
2164 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
2167 for (i = 0; i < set->n; i++) {
2168 if (set->dsts[i].vlan == test->vlan
2169 && set->dsts[i].iface == test->iface) {
2177 port_trunks_vlan(const struct port *port, uint16_t vlan)
2179 return (port->vlan < 0 || vlan_bitmap_contains(port->trunks, vlan));
2183 port_includes_vlan(const struct port *port, uint16_t vlan)
2185 return vlan == port->vlan || port_trunks_vlan(port, vlan);
2189 port_is_floodable(const struct port *port)
2191 struct iface *iface;
2193 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2194 if (!ofproto_port_is_floodable(port->bridge->ofproto,
2202 /* Returns an arbitrary interface within 'port'. */
2203 static struct iface *
2204 port_get_an_iface(const struct port *port)
2206 return CONTAINER_OF(list_front(&port->ifaces), struct iface, port_elem);
2210 compose_dsts(const struct bridge *br, const struct flow *flow, uint16_t vlan,
2211 const struct port *in_port, const struct port *out_port,
2212 struct dst_set *set, tag_type *tags, uint16_t *nf_output_iface)
2216 if (out_port == FLOOD_PORT) {
2219 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2221 && port_is_floodable(port)
2222 && port_includes_vlan(port, vlan)
2223 && !port->is_mirror_output_port
2224 && set_dst(&dst, flow, in_port, port, tags)) {
2225 dst_set_add(set, &dst);
2228 *nf_output_iface = NF_OUT_FLOOD;
2229 } else if (out_port && set_dst(&dst, flow, in_port, out_port, tags)) {
2230 dst_set_add(set, &dst);
2231 *nf_output_iface = dst.iface->dp_ifidx;
2236 compose_mirror_dsts(const struct bridge *br, const struct flow *flow,
2237 uint16_t vlan, const struct port *in_port,
2238 struct dst_set *set, tag_type *tags)
2240 mirror_mask_t mirrors;
2244 mirrors = in_port->src_mirrors;
2245 for (i = 0; i < set->n; i++) {
2246 mirrors |= set->dsts[i].iface->port->dst_mirrors;
2253 flow_vlan = vlan_tci_to_vid(flow->vlan_tci);
2254 if (flow_vlan == 0) {
2255 flow_vlan = OFP_VLAN_NONE;
2259 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
2260 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
2264 if (set_dst(&dst, flow, in_port, m->out_port, tags)
2265 && !dst_is_duplicate(set, &dst)) {
2266 dst_set_add(set, &dst);
2271 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2272 if (port_includes_vlan(port, m->out_vlan)
2273 && set_dst(&dst, flow, in_port, port, tags))
2275 if (port->vlan < 0) {
2276 dst.vlan = m->out_vlan;
2278 if (dst_is_duplicate(set, &dst)) {
2282 /* Use the vlan tag on the original flow instead of
2283 * the one passed in the vlan parameter. This ensures
2284 * that we compare the vlan from before any implicit
2285 * tagging tags place. This is necessary because
2286 * dst->vlan is the final vlan, after removing implicit
2288 if (port == in_port && dst.vlan == flow_vlan) {
2289 /* Don't send out input port on same VLAN. */
2292 dst_set_add(set, &dst);
2297 mirrors &= mirrors - 1;
2302 compose_actions(struct bridge *br, const struct flow *flow, uint16_t vlan,
2303 const struct port *in_port, const struct port *out_port,
2304 tag_type *tags, struct ofpbuf *actions,
2305 uint16_t *nf_output_iface)
2307 uint16_t initial_vlan, cur_vlan;
2308 const struct dst *dst;
2312 compose_dsts(br, flow, vlan, in_port, out_port, &set, tags,
2314 compose_mirror_dsts(br, flow, vlan, in_port, &set, tags);
2316 /* Output all the packets we can without having to change the VLAN. */
2317 initial_vlan = vlan_tci_to_vid(flow->vlan_tci);
2318 if (initial_vlan == 0) {
2319 initial_vlan = OFP_VLAN_NONE;
2321 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2322 if (dst->vlan != initial_vlan) {
2325 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
2328 /* Then output the rest. */
2329 cur_vlan = initial_vlan;
2330 for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
2331 if (dst->vlan == initial_vlan) {
2334 if (dst->vlan != cur_vlan) {
2335 if (dst->vlan == OFP_VLAN_NONE) {
2336 nl_msg_put_flag(actions, ODP_ACTION_ATTR_STRIP_VLAN);
2339 tci = htons(dst->vlan & VLAN_VID_MASK);
2340 tci |= flow->vlan_tci & htons(VLAN_PCP_MASK);
2341 nl_msg_put_be16(actions, ODP_ACTION_ATTR_SET_DL_TCI, tci);
2343 cur_vlan = dst->vlan;
2345 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->iface->dp_ifidx);
2351 /* Returns the effective vlan of a packet, taking into account both the
2352 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2353 * the packet is untagged and -1 indicates it has an invalid header and
2354 * should be dropped. */
2355 static int flow_get_vlan(struct bridge *br, const struct flow *flow,
2356 struct port *in_port, bool have_packet)
2358 int vlan = vlan_tci_to_vid(flow->vlan_tci);
2359 if (in_port->vlan >= 0) {
2362 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2363 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2364 "packet received on port %s configured with "
2365 "implicit VLAN %"PRIu16,
2366 br->name, vlan, in_port->name, in_port->vlan);
2370 vlan = in_port->vlan;
2372 if (!port_includes_vlan(in_port, vlan)) {
2374 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2375 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2376 "packet received on port %s not configured for "
2378 br->name, vlan, in_port->name, vlan);
2387 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2388 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2389 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2391 is_gratuitous_arp(const struct flow *flow)
2393 return (flow->dl_type == htons(ETH_TYPE_ARP)
2394 && eth_addr_is_broadcast(flow->dl_dst)
2395 && (flow->nw_proto == ARP_OP_REPLY
2396 || (flow->nw_proto == ARP_OP_REQUEST
2397 && flow->nw_src == flow->nw_dst)));
2401 update_learning_table(struct bridge *br, const struct flow *flow, int vlan,
2402 struct port *in_port)
2404 struct mac_entry *mac;
2406 if (!mac_learning_may_learn(br->ml, flow->dl_src, vlan)) {
2410 mac = mac_learning_insert(br->ml, flow->dl_src, vlan);
2411 if (is_gratuitous_arp(flow)) {
2412 /* We don't want to learn from gratuitous ARP packets that are
2413 * reflected back over bond slaves so we lock the learning table. */
2414 if (!in_port->bond) {
2415 mac_entry_set_grat_arp_lock(mac);
2416 } else if (mac_entry_is_grat_arp_locked(mac)) {
2421 if (mac_entry_is_new(mac) || mac->port.p != in_port) {
2422 /* The log messages here could actually be useful in debugging,
2423 * so keep the rate limit relatively high. */
2424 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2425 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2426 "on port %s in VLAN %d",
2427 br->name, ETH_ADDR_ARGS(flow->dl_src),
2428 in_port->name, vlan);
2430 mac->port.p = in_port;
2431 ofproto_revalidate(br->ofproto, mac_learning_changed(br->ml, mac));
2435 /* Determines whether packets in 'flow' within 'br' should be forwarded or
2436 * dropped. Returns true if they may be forwarded, false if they should be
2439 * If 'have_packet' is true, it indicates that the caller is processing a
2440 * received packet. If 'have_packet' is false, then the caller is just
2441 * revalidating an existing flow because configuration has changed. Either
2442 * way, 'have_packet' only affects logging (there is no point in logging errors
2443 * during revalidation).
2445 * Sets '*in_portp' to the input port. This will be a null pointer if
2446 * flow->in_port does not designate a known input port (in which case
2447 * is_admissible() returns false).
2449 * When returning true, sets '*vlanp' to the effective VLAN of the input
2450 * packet, as returned by flow_get_vlan().
2452 * May also add tags to '*tags', although the current implementation only does
2453 * so in one special case.
2456 is_admissible(struct bridge *br, const struct flow *flow, bool have_packet,
2457 tag_type *tags, int *vlanp, struct port **in_portp)
2459 struct iface *in_iface;
2460 struct port *in_port;
2463 /* Find the interface and port structure for the received packet. */
2464 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2466 /* No interface? Something fishy... */
2468 /* Odd. A few possible reasons here:
2470 * - We deleted an interface but there are still a few packets
2471 * queued up from it.
2473 * - Someone externally added an interface (e.g. with "ovs-dpctl
2474 * add-if") that we don't know about.
2476 * - Packet arrived on the local port but the local port is not
2477 * one of our bridge ports.
2479 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2481 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
2482 "interface %"PRIu16, br->name, flow->in_port);
2488 *in_portp = in_port = in_iface->port;
2489 *vlanp = vlan = flow_get_vlan(br, flow, in_port, have_packet);
2494 /* Drop frames for reserved multicast addresses. */
2495 if (eth_addr_is_reserved(flow->dl_dst)) {
2499 /* Drop frames on ports reserved for mirroring. */
2500 if (in_port->is_mirror_output_port) {
2502 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2503 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2504 "%s, which is reserved exclusively for mirroring",
2505 br->name, in_port->name);
2510 if (in_port->bond) {
2511 struct mac_entry *mac;
2513 switch (bond_check_admissibility(in_port->bond, in_iface,
2514 flow->dl_dst, tags)) {
2521 case BV_DROP_IF_MOVED:
2522 mac = mac_learning_lookup(br->ml, flow->dl_src, vlan, NULL);
2523 if (mac && mac->port.p != in_port &&
2524 (!is_gratuitous_arp(flow)
2525 || mac_entry_is_grat_arp_locked(mac))) {
2535 /* If the composed actions may be applied to any packet in the given 'flow',
2536 * returns true. Otherwise, the actions should only be applied to 'packet', or
2537 * not at all, if 'packet' was NULL. */
2539 process_flow(struct bridge *br, const struct flow *flow,
2540 const struct ofpbuf *packet, struct ofpbuf *actions,
2541 tag_type *tags, uint16_t *nf_output_iface)
2543 struct port *in_port;
2544 struct port *out_port;
2545 struct mac_entry *mac;
2548 /* Check whether we should drop packets in this flow. */
2549 if (!is_admissible(br, flow, packet != NULL, tags, &vlan, &in_port)) {
2554 /* Learn source MAC (but don't try to learn from revalidation). */
2556 update_learning_table(br, flow, vlan, in_port);
2559 /* Determine output port. */
2560 mac = mac_learning_lookup(br->ml, flow->dl_dst, vlan, tags);
2562 out_port = mac->port.p;
2563 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
2564 /* If we are revalidating but don't have a learning entry then
2565 * eject the flow. Installing a flow that floods packets opens
2566 * up a window of time where we could learn from a packet reflected
2567 * on a bond and blackhole packets before the learning table is
2568 * updated to reflect the correct port. */
2571 out_port = FLOOD_PORT;
2574 /* Don't send packets out their input ports. */
2575 if (in_port == out_port) {
2581 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2589 bridge_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
2590 struct ofpbuf *actions, tag_type *tags,
2591 uint16_t *nf_output_iface, void *br_)
2593 struct bridge *br = br_;
2595 COVERAGE_INC(bridge_process_flow);
2596 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2600 bridge_special_ofhook_cb(const struct flow *flow,
2601 const struct ofpbuf *packet, void *br_)
2603 struct iface *iface;
2604 struct bridge *br = br_;
2606 iface = iface_from_dp_ifidx(br, flow->in_port);
2608 if (flow->dl_type == htons(ETH_TYPE_LACP)) {
2609 if (iface && iface->port->lacp && packet) {
2610 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
2612 lacp_process_pdu(iface->port->lacp, iface, pdu);
2622 bridge_account_flow_ofhook_cb(const struct flow *flow, tag_type tags,
2623 const struct nlattr *actions,
2625 uint64_t n_bytes, void *br_)
2627 struct bridge *br = br_;
2628 const struct nlattr *a;
2629 struct port *in_port;
2634 /* Feed information from the active flows back into the learning table to
2635 * ensure that table is always in sync with what is actually flowing
2636 * through the datapath.
2638 * We test that 'tags' is nonzero to ensure that only flows that include an
2639 * OFPP_NORMAL action are used for learning. This works because
2640 * bridge_normal_ofhook_cb() always sets a nonzero tag value. */
2641 if (tags && is_admissible(br, flow, false, &dummy, &vlan, &in_port)) {
2642 update_learning_table(br, flow, vlan, in_port);
2645 /* Account for bond slave utilization. */
2646 if (!br->has_bonded_ports) {
2649 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
2650 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
2651 struct port *out_port = port_from_dp_ifidx(br, nl_attr_get_u32(a));
2652 if (out_port && out_port->bond) {
2653 uint16_t vlan = (flow->vlan_tci
2654 ? vlan_tci_to_vid(flow->vlan_tci)
2656 bond_account(out_port->bond, flow, vlan, n_bytes);
2663 bridge_account_checkpoint_ofhook_cb(void *br_)
2665 struct bridge *br = br_;
2668 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
2670 bond_rebalance(port->bond,
2671 ofproto_get_revalidate_set(br->ofproto));
2677 bridge_autopath_ofhook_cb(const struct flow *flow, uint32_t ofp_port,
2678 tag_type *tags, void *br_)
2680 struct bridge *br = br_;
2681 uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2682 struct port *port = port_from_dp_ifidx(br, odp_port);
2687 } else if (list_is_short(&port->ifaces)) {
2690 struct iface *iface;
2692 /* Autopath does not support VLAN hashing. */
2693 iface = bond_choose_output_slave(port->bond, flow,
2694 OFP_VLAN_NONE, tags);
2695 ret = iface ? iface->dp_ifidx : ODPP_NONE;
2698 return odp_port_to_ofp_port(ret);
2701 static struct ofhooks bridge_ofhooks = {
2702 bridge_normal_ofhook_cb,
2703 bridge_special_ofhook_cb,
2704 bridge_account_flow_ofhook_cb,
2705 bridge_account_checkpoint_ofhook_cb,
2706 bridge_autopath_ofhook_cb,
2709 /* Port functions. */
2712 lacp_send_pdu_cb(void *iface_, const struct lacp_pdu *pdu)
2714 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2715 struct iface *iface = iface_;
2716 uint8_t ea[ETH_ADDR_LEN];
2719 error = netdev_get_etheraddr(iface->netdev, ea);
2721 struct lacp_pdu *packet_pdu;
2722 struct ofpbuf packet;
2724 ofpbuf_init(&packet, 0);
2725 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2726 sizeof *packet_pdu);
2728 error = netdev_send(iface->netdev, &packet);
2730 VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
2731 "(%s)", iface->port->name, iface->name,
2734 ofpbuf_uninit(&packet);
2736 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2737 "%s (%s)", iface->port->name, iface->name,
2743 port_run(struct port *port)
2746 lacp_run(port->lacp, lacp_send_pdu_cb);
2750 struct iface *iface;
2752 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
2753 bool may_enable = lacp_slave_may_enable(port->lacp, iface);
2754 bond_slave_set_lacp_may_enable(port->bond, iface, may_enable);
2757 bond_run(port->bond,
2758 ofproto_get_revalidate_set(port->bridge->ofproto),
2759 lacp_negotiated(port->lacp));
2760 if (bond_should_send_learning_packets(port->bond)) {
2761 port_send_learning_packets(port);
2767 port_wait(struct port *port)
2770 lacp_wait(port->lacp);
2774 bond_wait(port->bond);
2778 static struct port *
2779 port_create(struct bridge *br, const char *name)
2783 port = xzalloc(sizeof *port);
2786 port->trunks = NULL;
2787 port->name = xstrdup(name);
2788 list_init(&port->ifaces);
2790 hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
2792 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
2799 get_port_other_config(const struct ovsrec_port *port, const char *key,
2800 const char *default_value)
2804 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
2806 return value ? value : default_value;
2810 get_interface_other_config(const struct ovsrec_interface *iface,
2811 const char *key, const char *default_value)
2815 value = get_ovsrec_key_value(&iface->header_,
2816 &ovsrec_interface_col_other_config, key);
2817 return value ? value : default_value;
2821 port_del_ifaces(struct port *port, const struct ovsrec_port *cfg)
2823 struct iface *iface, *next;
2824 struct sset new_ifaces;
2827 /* Collect list of new interfaces. */
2828 sset_init(&new_ifaces);
2829 for (i = 0; i < cfg->n_interfaces; i++) {
2830 const char *name = cfg->interfaces[i]->name;
2831 sset_add(&new_ifaces, name);
2834 /* Get rid of deleted interfaces. */
2835 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2836 if (!sset_contains(&new_ifaces, iface->name)) {
2837 iface_destroy(iface);
2841 sset_destroy(&new_ifaces);
2844 /* Expires all MAC learning entries associated with 'port' and forces ofproto
2845 * to revalidate every flow. */
2847 port_flush_macs(struct port *port)
2849 struct bridge *br = port->bridge;
2850 struct mac_learning *ml = br->ml;
2851 struct mac_entry *mac, *next_mac;
2854 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2855 if (mac->port.p == port) {
2856 mac_learning_expire(ml, mac);
2862 port_reconfigure(struct port *port, const struct ovsrec_port *cfg)
2864 struct sset new_ifaces;
2865 bool need_flush = false;
2866 unsigned long *trunks;
2873 /* Add new interfaces and update 'cfg' member of existing ones. */
2874 sset_init(&new_ifaces);
2875 for (i = 0; i < cfg->n_interfaces; i++) {
2876 const struct ovsrec_interface *if_cfg = cfg->interfaces[i];
2877 struct iface *iface;
2879 if (!sset_add(&new_ifaces, if_cfg->name)) {
2880 VLOG_WARN("port %s: %s specified twice as port interface",
2881 port->name, if_cfg->name);
2882 iface_set_ofport(if_cfg, -1);
2886 iface = iface_lookup(port->bridge, if_cfg->name);
2888 if (iface->port != port) {
2889 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
2891 port->bridge->name, if_cfg->name, iface->port->name);
2894 iface->cfg = if_cfg;
2896 iface = iface_create(port, if_cfg);
2899 /* Determine interface type. The local port always has type
2900 * "internal". Other ports take their type from the database and
2901 * default to "system" if none is specified. */
2902 iface->type = (!strcmp(if_cfg->name, port->bridge->name) ? "internal"
2903 : if_cfg->type[0] ? if_cfg->type
2906 sset_destroy(&new_ifaces);
2911 if (list_is_short(&port->ifaces)) {
2913 if (vlan >= 0 && vlan <= 4095) {
2914 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
2919 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
2920 * they even work as-is. But they have not been tested. */
2921 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
2925 if (port->vlan != vlan) {
2930 /* Get trunked VLANs. */
2932 if (vlan < 0 && cfg->n_trunks) {
2933 trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
2935 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
2938 } else if (vlan >= 0 && cfg->n_trunks) {
2939 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
2942 if (!vlan_bitmap_equal(trunks, port->trunks)) {
2945 bitmap_free(port->trunks);
2946 port->trunks = trunks;
2949 port_flush_macs(port);
2954 port_destroy(struct port *port)
2957 struct bridge *br = port->bridge;
2958 struct iface *iface, *next;
2961 for (i = 0; i < MAX_MIRRORS; i++) {
2962 struct mirror *m = br->mirrors[i];
2963 if (m && m->out_port == port) {
2968 LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
2969 iface_destroy(iface);
2972 hmap_remove(&br->ports, &port->hmap_node);
2974 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
2976 bond_destroy(port->bond);
2977 lacp_destroy(port->lacp);
2978 port_flush_macs(port);
2980 bitmap_free(port->trunks);
2986 static struct port *
2987 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
2989 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
2990 return iface ? iface->port : NULL;
2993 static struct port *
2994 port_lookup(const struct bridge *br, const char *name)
2998 HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
3000 if (!strcmp(port->name, name)) {
3008 enable_lacp(struct port *port, bool *activep)
3010 if (!port->cfg->lacp) {
3011 /* XXX when LACP implementation has been sufficiently tested, enable by
3012 * default and make active on bonded ports. */
3014 } else if (!strcmp(port->cfg->lacp, "off")) {
3016 } else if (!strcmp(port->cfg->lacp, "active")) {
3019 } else if (!strcmp(port->cfg->lacp, "passive")) {
3023 VLOG_WARN("port %s: unknown LACP mode %s",
3024 port->name, port->cfg->lacp);
3030 iface_reconfigure_lacp(struct iface *iface)
3032 struct lacp_slave_settings s;
3033 int priority, portid;
3035 portid = atoi(get_interface_other_config(iface->cfg, "lacp-port-id", "0"));
3036 priority = atoi(get_interface_other_config(iface->cfg,
3037 "lacp-port-priority", "0"));
3039 if (portid <= 0 || portid > UINT16_MAX) {
3040 portid = iface->dp_ifidx;
3043 if (priority <= 0 || priority > UINT16_MAX) {
3044 priority = UINT16_MAX;
3047 s.name = iface->name;
3049 s.priority = priority;
3050 lacp_slave_register(iface->port->lacp, iface, &s);
3054 port_reconfigure_lacp(struct port *port)
3056 static struct lacp_settings s;
3057 struct iface *iface;
3058 uint8_t sysid[ETH_ADDR_LEN];
3059 const char *sysid_str;
3060 const char *lacp_time;
3061 long long int custom_time;
3064 if (!enable_lacp(port, &s.active)) {
3065 lacp_destroy(port->lacp);
3070 sysid_str = get_port_other_config(port->cfg, "lacp-system-id", NULL);
3071 if (sysid_str && eth_addr_from_string(sysid_str, sysid)) {
3072 memcpy(s.id, sysid, ETH_ADDR_LEN);
3074 memcpy(s.id, port->bridge->ea, ETH_ADDR_LEN);
3077 s.name = port->name;
3079 /* Prefer bondable links if unspecified. */
3080 priority = atoi(get_port_other_config(port->cfg, "lacp-system-priority",
3082 s.priority = (priority > 0 && priority <= UINT16_MAX
3084 : UINT16_MAX - !list_is_short(&port->ifaces));
3086 s.strict = !strcmp(get_port_other_config(port->cfg, "lacp-strict",
3090 lacp_time = get_port_other_config(port->cfg, "lacp-time", "slow");
3091 custom_time = atoi(lacp_time);
3092 if (!strcmp(lacp_time, "fast")) {
3093 s.lacp_time = LACP_TIME_FAST;
3094 } else if (!strcmp(lacp_time, "slow")) {
3095 s.lacp_time = LACP_TIME_SLOW;
3096 } else if (custom_time > 0) {
3097 s.lacp_time = LACP_TIME_CUSTOM;
3098 s.custom_time = custom_time;
3100 s.lacp_time = LACP_TIME_SLOW;
3104 port->lacp = lacp_create();
3107 lacp_configure(port->lacp, &s);
3109 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3110 iface_reconfigure_lacp(iface);
3115 port_reconfigure_bond(struct port *port)
3117 struct bond_settings s;
3118 const char *detect_s;
3119 struct iface *iface;
3121 if (list_is_short(&port->ifaces)) {
3122 /* Not a bonded port. */
3123 bond_destroy(port->bond);
3128 port->bridge->has_bonded_ports = true;
3130 s.name = port->name;
3132 if (port->cfg->bond_mode
3133 && !bond_mode_from_string(&s.balance, port->cfg->bond_mode)) {
3134 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
3135 port->name, port->cfg->bond_mode,
3136 bond_mode_to_string(s.balance));
3139 s.detect = BLSM_CARRIER;
3140 detect_s = get_port_other_config(port->cfg, "bond-detect-mode", NULL);
3141 if (detect_s && !bond_detect_mode_from_string(&s.detect, detect_s)) {
3142 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
3144 port->name, detect_s, bond_detect_mode_to_string(s.detect));
3147 s.miimon_interval = atoi(
3148 get_port_other_config(port->cfg, "bond-miimon-interval", "200"));
3149 if (s.miimon_interval < 100) {
3150 s.miimon_interval = 100;
3153 s.up_delay = MAX(0, port->cfg->bond_updelay);
3154 s.down_delay = MAX(0, port->cfg->bond_downdelay);
3155 s.rebalance_interval = atoi(
3156 get_port_other_config(port->cfg, "bond-rebalance-interval", "10000"));
3157 if (s.rebalance_interval < 1000) {
3158 s.rebalance_interval = 1000;
3161 s.fake_iface = port->cfg->bond_fake_iface;
3164 port->bond = bond_create(&s);
3166 if (bond_reconfigure(port->bond, &s)) {
3167 bridge_flush(port->bridge);
3171 LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
3172 uint16_t stable_id = (port->lacp
3173 ? lacp_slave_get_port_id(port->lacp, iface)
3175 bond_slave_register(iface->port->bond, iface, stable_id,
3181 port_send_learning_packets(struct port *port)
3183 struct bridge *br = port->bridge;
3184 int error, n_packets, n_errors;
3185 struct mac_entry *e;
3187 error = n_packets = n_errors = 0;
3188 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
3189 if (e->port.p != port) {
3190 int ret = bond_send_learning_packet(port->bond, e->mac, e->vlan);
3200 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3201 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3202 "packets, last error was: %s",
3203 port->name, n_errors, n_packets, strerror(error));
3205 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3206 port->name, n_packets);
3210 /* Interface functions. */
3212 static struct iface *
3213 iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
3215 struct bridge *br = port->bridge;
3216 struct iface *iface;
3217 char *name = if_cfg->name;
3219 iface = xzalloc(sizeof *iface);
3221 iface->name = xstrdup(name);
3222 iface->dp_ifidx = -1;
3223 iface->tag = tag_create_random();
3224 iface->netdev = NULL;
3225 iface->cfg = if_cfg;
3227 hmap_insert(&br->iface_by_name, &iface->name_node, hash_string(name, 0));
3229 list_push_back(&port->ifaces, &iface->port_elem);
3231 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
3239 iface_destroy(struct iface *iface)
3242 struct port *port = iface->port;
3243 struct bridge *br = port->bridge;
3246 bond_slave_unregister(port->bond, iface);
3250 lacp_slave_unregister(port->lacp, iface);
3253 if (iface->dp_ifidx >= 0) {
3254 hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
3257 list_remove(&iface->port_elem);
3258 hmap_remove(&br->iface_by_name, &iface->name_node);
3260 netdev_close(iface->netdev);
3265 bridge_flush(port->bridge);
3269 static struct iface *
3270 iface_lookup(const struct bridge *br, const char *name)
3272 struct iface *iface;
3274 HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
3275 &br->iface_by_name) {
3276 if (!strcmp(iface->name, name)) {
3284 static struct iface *
3285 iface_find(const char *name)
3287 const struct bridge *br;
3289 LIST_FOR_EACH (br, node, &all_bridges) {
3290 struct iface *iface = iface_lookup(br, name);
3299 static struct iface *
3300 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
3302 struct iface *iface;
3304 HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
3305 hash_int(dp_ifidx, 0), &br->ifaces) {
3306 if (iface->dp_ifidx == dp_ifidx) {
3313 /* Set Ethernet address of 'iface', if one is specified in the configuration
3316 iface_set_mac(struct iface *iface)
3318 uint8_t ea[ETH_ADDR_LEN];
3320 if (!strcmp(iface->type, "internal")
3321 && iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
3322 if (iface->dp_ifidx == ODPP_LOCAL) {
3323 VLOG_ERR("interface %s: ignoring mac in Interface record "
3324 "(use Bridge record to set local port's mac)",
3326 } else if (eth_addr_is_multicast(ea)) {
3327 VLOG_ERR("interface %s: cannot set MAC to multicast address",
3330 int error = netdev_set_etheraddr(iface->netdev, ea);
3332 VLOG_ERR("interface %s: setting MAC failed (%s)",
3333 iface->name, strerror(error));
3339 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
3341 iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
3343 if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
3344 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
3348 /* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
3350 * The value strings in '*shash' are taken directly from values[], not copied,
3351 * so the caller should not modify or free them. */
3353 shash_from_ovs_idl_map(char **keys, char **values, size_t n,
3354 struct shash *shash)
3359 for (i = 0; i < n; i++) {
3360 shash_add(shash, keys[i], values[i]);
3364 /* Creates 'keys' and 'values' arrays from 'shash'.
3366 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
3367 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
3368 * are populated with with strings taken directly from 'shash' and thus have
3369 * the same ownership of the key-value pairs in shash.
3372 shash_to_ovs_idl_map(struct shash *shash,
3373 char ***keys, char ***values, size_t *n)
3377 struct shash_node *sn;
3379 count = shash_count(shash);
3381 k = xmalloc(count * sizeof *k);
3382 v = xmalloc(count * sizeof *v);
3385 SHASH_FOR_EACH(sn, shash) {
3396 struct iface_delete_queues_cbdata {
3397 struct netdev *netdev;
3398 const struct ovsdb_datum *queues;
3402 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
3404 union ovsdb_atom atom;
3406 atom.integer = target;
3407 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
3411 iface_delete_queues(unsigned int queue_id,
3412 const struct shash *details OVS_UNUSED, void *cbdata_)
3414 struct iface_delete_queues_cbdata *cbdata = cbdata_;
3416 if (!queue_ids_include(cbdata->queues, queue_id)) {
3417 netdev_delete_queue(cbdata->netdev, queue_id);
3422 iface_update_qos(struct iface *iface, const struct ovsrec_qos *qos)
3424 if (!qos || qos->type[0] == '\0') {
3425 netdev_set_qos(iface->netdev, NULL, NULL);
3427 struct iface_delete_queues_cbdata cbdata;
3428 struct shash details;
3431 /* Configure top-level Qos for 'iface'. */
3432 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
3433 qos->n_other_config, &details);
3434 netdev_set_qos(iface->netdev, qos->type, &details);
3435 shash_destroy(&details);
3437 /* Deconfigure queues that were deleted. */
3438 cbdata.netdev = iface->netdev;
3439 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
3441 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
3443 /* Configure queues for 'iface'. */
3444 for (i = 0; i < qos->n_queues; i++) {
3445 const struct ovsrec_queue *queue = qos->value_queues[i];
3446 unsigned int queue_id = qos->key_queues[i];
3448 shash_from_ovs_idl_map(queue->key_other_config,
3449 queue->value_other_config,
3450 queue->n_other_config, &details);
3451 netdev_set_queue(iface->netdev, queue_id, &details);
3452 shash_destroy(&details);
3458 iface_update_cfm(struct iface *iface)
3462 uint16_t *remote_mps;
3463 struct ovsrec_monitor *mon;
3464 uint8_t maid[CCM_MAID_LEN];
3466 mon = iface->cfg->monitor;
3469 ofproto_iface_clear_cfm(iface->port->bridge->ofproto, iface->dp_ifidx);
3473 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
3474 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
3478 cfm.mpid = mon->mpid;
3479 cfm.interval = mon->interval ? *mon->interval : 1000;
3481 memcpy(cfm.maid, maid, sizeof cfm.maid);
3483 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
3484 for(i = 0; i < mon->n_remote_mps; i++) {
3485 remote_mps[i] = mon->remote_mps[i]->mpid;
3488 ofproto_iface_set_cfm(iface->port->bridge->ofproto, iface->dp_ifidx,
3489 &cfm, remote_mps, mon->n_remote_mps);
3493 /* Read carrier or miimon status directly from 'iface''s netdev, according to
3494 * how 'iface''s port is configured.
3496 * Returns true if 'iface' is up, false otherwise. */
3498 iface_get_carrier(const struct iface *iface)
3501 return netdev_get_carrier(iface->netdev);
3504 /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
3505 * instead of obtaining it from the database. */
3507 iface_is_synthetic(const struct iface *iface)
3509 return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
3512 /* Port mirroring. */
3514 static struct mirror *
3515 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
3519 for (i = 0; i < MAX_MIRRORS; i++) {
3520 struct mirror *m = br->mirrors[i];
3521 if (m && uuid_equals(uuid, &m->uuid)) {
3529 mirror_reconfigure(struct bridge *br)
3531 unsigned long *rspan_vlans;
3535 /* Get rid of deleted mirrors. */
3536 for (i = 0; i < MAX_MIRRORS; i++) {
3537 struct mirror *m = br->mirrors[i];
3539 const struct ovsdb_datum *mc;
3540 union ovsdb_atom atom;
3542 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
3543 atom.uuid = br->mirrors[i]->uuid;
3544 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
3550 /* Add new mirrors and reconfigure existing ones. */
3551 for (i = 0; i < br->cfg->n_mirrors; i++) {
3552 struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
3553 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
3555 mirror_reconfigure_one(m, cfg);
3557 mirror_create(br, cfg);
3561 /* Update port reserved status. */
3562 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3563 port->is_mirror_output_port = false;
3565 for (i = 0; i < MAX_MIRRORS; i++) {
3566 struct mirror *m = br->mirrors[i];
3567 if (m && m->out_port) {
3568 m->out_port->is_mirror_output_port = true;
3572 /* Update flooded vlans (for RSPAN). */
3574 if (br->cfg->n_flood_vlans) {
3575 rspan_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
3576 br->cfg->n_flood_vlans);
3578 if (mac_learning_set_flood_vlans(br->ml, rspan_vlans)) {
3580 mac_learning_flush(br->ml);
3586 mirror_create(struct bridge *br, struct ovsrec_mirror *cfg)
3591 for (i = 0; ; i++) {
3592 if (i >= MAX_MIRRORS) {
3593 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
3594 "cannot create %s", br->name, MAX_MIRRORS, cfg->name);
3597 if (!br->mirrors[i]) {
3602 VLOG_INFO("created port mirror %s on bridge %s", cfg->name, br->name);
3604 mac_learning_flush(br->ml);
3606 br->mirrors[i] = m = xzalloc(sizeof *m);
3607 m->uuid = cfg->header_.uuid;
3610 m->name = xstrdup(cfg->name);
3611 sset_init(&m->src_ports);
3612 sset_init(&m->dst_ports);
3618 mirror_reconfigure_one(m, cfg);
3622 mirror_destroy(struct mirror *m)
3625 struct bridge *br = m->bridge;
3628 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
3629 port->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3630 port->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
3633 sset_destroy(&m->src_ports);
3634 sset_destroy(&m->dst_ports);
3637 m->bridge->mirrors[m->idx] = NULL;
3642 mac_learning_flush(br->ml);
3647 mirror_collect_ports(struct mirror *m, struct ovsrec_port **ports, int n_ports,
3652 for (i = 0; i < n_ports; i++) {
3653 const char *name = ports[i]->name;
3654 if (port_lookup(m->bridge, name)) {
3655 sset_add(names, name);
3657 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
3658 "port %s", m->bridge->name, m->name, name);
3664 mirror_collect_vlans(struct mirror *m, const struct ovsrec_mirror *cfg,
3670 *vlans = xmalloc(sizeof **vlans * cfg->n_select_vlan);
3672 for (i = 0; i < cfg->n_select_vlan; i++) {
3673 int64_t vlan = cfg->select_vlan[i];
3674 if (vlan < 0 || vlan > 4095) {
3675 VLOG_WARN("bridge %s: mirror %s selects invalid VLAN %"PRId64,
3676 m->bridge->name, m->name, vlan);
3678 (*vlans)[n_vlans++] = vlan;
3685 vlan_is_mirrored(const struct mirror *m, int vlan)
3689 for (i = 0; i < m->n_vlans; i++) {
3690 if (m->vlans[i] == vlan) {
3698 mirror_reconfigure_one(struct mirror *m, struct ovsrec_mirror *cfg)
3700 struct sset src_ports, dst_ports;
3701 mirror_mask_t mirror_bit;
3702 struct port *out_port;
3709 if (strcmp(cfg->name, m->name)) {
3711 m->name = xstrdup(cfg->name);
3714 /* Get output port. */
3715 if (cfg->output_port) {
3716 out_port = port_lookup(m->bridge, cfg->output_port->name);
3718 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
3719 m->bridge->name, m->name);
3725 if (cfg->output_vlan) {
3726 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
3727 "output vlan; ignoring output vlan",
3728 m->bridge->name, m->name);
3730 } else if (cfg->output_vlan) {
3732 out_vlan = *cfg->output_vlan;
3734 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
3735 m->bridge->name, m->name);
3740 sset_init(&src_ports);
3741 sset_init(&dst_ports);
3742 if (cfg->select_all) {
3743 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3744 sset_add(&src_ports, port->name);
3745 sset_add(&dst_ports, port->name);
3750 /* Get ports, and drop duplicates and ports that don't exist. */
3751 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
3753 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
3756 /* Get all the vlans, and drop duplicate and invalid vlans. */
3757 n_vlans = mirror_collect_vlans(m, cfg, &vlans);
3760 /* Update mirror data. */
3761 if (!sset_equals(&m->src_ports, &src_ports)
3762 || !sset_equals(&m->dst_ports, &dst_ports)
3763 || m->n_vlans != n_vlans
3764 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
3765 || m->out_port != out_port
3766 || m->out_vlan != out_vlan) {
3767 bridge_flush(m->bridge);
3768 mac_learning_flush(m->bridge->ml);
3770 sset_swap(&m->src_ports, &src_ports);
3771 sset_swap(&m->dst_ports, &dst_ports);
3774 m->n_vlans = n_vlans;
3775 m->out_port = out_port;
3776 m->out_vlan = out_vlan;
3779 mirror_bit = MIRROR_MASK_C(1) << m->idx;
3780 HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
3781 if (sset_contains(&m->src_ports, port->name)) {
3782 port->src_mirrors |= mirror_bit;
3784 port->src_mirrors &= ~mirror_bit;
3787 if (sset_contains(&m->dst_ports, port->name)) {
3788 port->dst_mirrors |= mirror_bit;
3790 port->dst_mirrors &= ~mirror_bit;
3795 sset_destroy(&src_ports);
3796 sset_destroy(&dst_ports);