+ /* Compose actions. */
+ memset(actions, 0, sizeof actions);
+ a = actions;
+ if (e->vlan) {
+ a->vlan_vid.type = htons(OFPAT_SET_VLAN_VID);
+ a->vlan_vid.len = htons(sizeof *a);
+ a->vlan_vid.vlan_vid = htons(e->vlan);
+ a++;
+ }
+ a->output.type = htons(OFPAT_OUTPUT);
+ a->output.len = htons(sizeof *a);
+ a->output.port = htons(odp_port_to_ofp_port(dp_ifidx));
+ a++;
+
+ /* Send packet. */
+ n_packets++;
+ compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
+ e->mac);
+ flow_extract(&packet, 0, ODPP_NONE, &flow);
+ retval = ofproto_send_packet(br->ofproto, &flow, actions, a - actions,
+ &packet);
+ if (retval) {
+ error = retval;
+ n_errors++;
+ }
+ }
+ ofpbuf_uninit(&packet);
+
+ if (n_errors) {
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+ VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
+ "packets, last error was: %s",
+ port->name, n_errors, n_packets, strerror(error));
+ } else {
+ VLOG_DBG("bond %s: sent %d gratuitous learning packets",
+ port->name, n_packets);
+ }
+}
+\f
+/* Bonding unixctl user interface functions. */
+
+static void
+bond_unixctl_list(struct unixctl_conn *conn,
+ const char *args OVS_UNUSED, void *aux OVS_UNUSED)
+{
+ struct ds ds = DS_EMPTY_INITIALIZER;
+ const struct bridge *br;
+
+ ds_put_cstr(&ds, "bridge\tbond\tslaves\n");
+
+ LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+ size_t i;
+
+ for (i = 0; i < br->n_ports; i++) {
+ const struct port *port = br->ports[i];
+ if (port->n_ifaces > 1) {
+ size_t j;
+
+ ds_put_format(&ds, "%s\t%s\t", br->name, port->name);
+ for (j = 0; j < port->n_ifaces; j++) {
+ const struct iface *iface = port->ifaces[j];
+ if (j) {
+ ds_put_cstr(&ds, ", ");
+ }
+ ds_put_cstr(&ds, iface->name);
+ }
+ ds_put_char(&ds, '\n');
+ }
+ }
+ }
+ unixctl_command_reply(conn, 200, ds_cstr(&ds));
+ ds_destroy(&ds);
+}
+
+static struct port *
+bond_find(const char *name)
+{
+ const struct bridge *br;
+
+ LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+ size_t i;
+
+ for (i = 0; i < br->n_ports; i++) {
+ struct port *port = br->ports[i];
+ if (!strcmp(port->name, name) && port->n_ifaces > 1) {
+ return port;
+ }
+ }
+ }
+ return NULL;
+}
+
+static void
+bond_unixctl_show(struct unixctl_conn *conn,
+ const char *args, void *aux OVS_UNUSED)
+{
+ struct ds ds = DS_EMPTY_INITIALIZER;
+ const struct port *port;
+ size_t j;
+
+ port = bond_find(args);
+ if (!port) {
+ unixctl_command_reply(conn, 501, "no such bond");
+ return;
+ }
+
+ ds_put_format(&ds, "updelay: %d ms\n", port->updelay);
+ ds_put_format(&ds, "downdelay: %d ms\n", port->downdelay);
+ ds_put_format(&ds, "next rebalance: %lld ms\n",
+ port->bond_next_rebalance - time_msec());
+ for (j = 0; j < port->n_ifaces; j++) {
+ const struct iface *iface = port->ifaces[j];
+ struct bond_entry *be;
+
+ /* Basic info. */
+ ds_put_format(&ds, "slave %s: %s\n",
+ iface->name, iface->enabled ? "enabled" : "disabled");
+ if (j == port->active_iface) {
+ ds_put_cstr(&ds, "\tactive slave\n");
+ }
+ if (iface->delay_expires != LLONG_MAX) {
+ ds_put_format(&ds, "\t%s expires in %lld ms\n",
+ iface->enabled ? "downdelay" : "updelay",
+ iface->delay_expires - time_msec());
+ }
+
+ /* Hashes. */
+ for (be = port->bond_hash; be <= &port->bond_hash[BOND_MASK]; be++) {
+ int hash = be - port->bond_hash;
+ struct mac_entry *me;
+
+ if (be->iface_idx != j) {
+ continue;
+ }
+
+ ds_put_format(&ds, "\thash %d: %"PRIu64" kB load\n",
+ hash, be->tx_bytes / 1024);
+
+ /* MACs. */
+ LIST_FOR_EACH (me, struct mac_entry, lru_node,
+ &port->bridge->ml->lrus) {
+ uint16_t dp_ifidx;
+ tag_type tags = 0;
+ if (bond_hash(me->mac) == hash
+ && me->port != port->port_idx
+ && choose_output_iface(port, me->mac, &dp_ifidx, &tags)
+ && dp_ifidx == iface->dp_ifidx)
+ {
+ ds_put_format(&ds, "\t\t"ETH_ADDR_FMT"\n",
+ ETH_ADDR_ARGS(me->mac));
+ }
+ }
+ }
+ }
+ unixctl_command_reply(conn, 200, ds_cstr(&ds));
+ ds_destroy(&ds);
+}
+
+static void
+bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_,
+ void *aux OVS_UNUSED)
+{
+ char *args = (char *) args_;
+ char *save_ptr = NULL;
+ char *bond_s, *hash_s, *slave_s;
+ uint8_t mac[ETH_ADDR_LEN];
+ struct port *port;
+ struct iface *iface;
+ struct bond_entry *entry;
+ int hash;
+
+ bond_s = strtok_r(args, " ", &save_ptr);
+ hash_s = strtok_r(NULL, " ", &save_ptr);
+ slave_s = strtok_r(NULL, " ", &save_ptr);
+ if (!slave_s) {
+ unixctl_command_reply(conn, 501,
+ "usage: bond/migrate BOND HASH SLAVE");
+ return;
+ }
+
+ port = bond_find(bond_s);
+ if (!port) {
+ unixctl_command_reply(conn, 501, "no such bond");
+ return;
+ }
+
+ if (sscanf(hash_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
+ == ETH_ADDR_SCAN_COUNT) {
+ hash = bond_hash(mac);
+ } else if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
+ hash = atoi(hash_s) & BOND_MASK;
+ } else {
+ unixctl_command_reply(conn, 501, "bad hash");
+ return;
+ }
+
+ iface = port_lookup_iface(port, slave_s);
+ if (!iface) {
+ unixctl_command_reply(conn, 501, "no such slave");
+ return;
+ }
+
+ if (!iface->enabled) {
+ unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
+ return;
+ }
+
+ entry = &port->bond_hash[hash];
+ ofproto_revalidate(port->bridge->ofproto, entry->iface_tag);
+ entry->iface_idx = iface->port_ifidx;
+ entry->iface_tag = tag_create_random();
+ port->bond_compat_is_stale = true;
+ unixctl_command_reply(conn, 200, "migrated");
+}
+
+static void
+bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_,
+ void *aux OVS_UNUSED)
+{
+ char *args = (char *) args_;
+ char *save_ptr = NULL;
+ char *bond_s, *slave_s;
+ struct port *port;
+ struct iface *iface;
+
+ bond_s = strtok_r(args, " ", &save_ptr);
+ slave_s = strtok_r(NULL, " ", &save_ptr);
+ if (!slave_s) {
+ unixctl_command_reply(conn, 501,
+ "usage: bond/set-active-slave BOND SLAVE");
+ return;
+ }
+
+ port = bond_find(bond_s);
+ if (!port) {
+ unixctl_command_reply(conn, 501, "no such bond");
+ return;
+ }
+
+ iface = port_lookup_iface(port, slave_s);
+ if (!iface) {
+ unixctl_command_reply(conn, 501, "no such slave");
+ return;
+ }
+
+ if (!iface->enabled) {
+ unixctl_command_reply(conn, 501, "cannot make disabled slave active");
+ return;
+ }
+
+ if (port->active_iface != iface->port_ifidx) {
+ ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
+ port->active_iface = iface->port_ifidx;
+ port->active_iface_tag = tag_create_random();
+ VLOG_INFO("port %s: active interface is now %s",
+ port->name, iface->name);
+ bond_send_learning_packets(port);
+ unixctl_command_reply(conn, 200, "done");
+ } else {
+ unixctl_command_reply(conn, 200, "no change");
+ }
+}
+
+static void
+enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
+{
+ char *args = (char *) args_;
+ char *save_ptr = NULL;
+ char *bond_s, *slave_s;
+ struct port *port;
+ struct iface *iface;
+
+ bond_s = strtok_r(args, " ", &save_ptr);
+ slave_s = strtok_r(NULL, " ", &save_ptr);
+ if (!slave_s) {
+ unixctl_command_reply(conn, 501,
+ "usage: bond/enable/disable-slave BOND SLAVE");
+ return;
+ }
+
+ port = bond_find(bond_s);
+ if (!port) {
+ unixctl_command_reply(conn, 501, "no such bond");
+ return;
+ }
+
+ iface = port_lookup_iface(port, slave_s);
+ if (!iface) {
+ unixctl_command_reply(conn, 501, "no such slave");
+ return;
+ }
+
+ bond_enable_slave(iface, enable);
+ unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
+}
+
+static void
+bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args,
+ void *aux OVS_UNUSED)
+{
+ enable_slave(conn, args, true);
+}