* derivatives without specific, written prior permission.
*/
+#include <config.h>
#include "learning-switch.h"
#include <errno.h>
#include "openflow.h"
#include "queue.h"
#include "rconn.h"
+#include "timeval.h"
#include "vconn.h"
#include "xtoxll.h"
int max_idle;
uint64_t datapath_id;
+ uint32_t capabilities;
time_t last_features_request;
struct mac_learning *ml; /* NULL to act as hub instead of switch. */
+
+ /* Number of outgoing queued packets on the rconn. */
+ int n_queued;
};
+/* The log messages here could actually be useful in debugging, so keep the
+ * rate limit relatively high. */
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
+
static void queue_tx(struct lswitch *, struct rconn *, struct buffer *);
static void send_features_request(struct lswitch *, struct rconn *);
+static void process_switch_features(struct lswitch *, struct rconn *,
+ struct ofp_switch_features *);
static void process_packet_in(struct lswitch *, struct rconn *,
struct ofp_packet_in *);
+static void process_echo_request(struct lswitch *, struct rconn *,
+ struct ofp_header *);
+static void process_port_status(struct lswitch *, struct rconn *,
+ struct ofp_port_status *);
+static void process_phy_port(struct lswitch *, struct rconn *,
+ const struct ofp_phy_port *);
/* Creates and returns a new learning switch.
*
struct lswitch *
lswitch_create(struct rconn *rconn, bool learn_macs, int max_idle)
{
- struct lswitch *sw = xmalloc(sizeof *sw);
- memset(sw, 0, sizeof *sw);
+ struct lswitch *sw = xcalloc(1, sizeof *sw);
sw->max_idle = max_idle;
sw->datapath_id = 0;
- sw->last_features_request = 0;
+ sw->last_features_request = time_now() - 1;
sw->ml = learn_macs ? mac_learning_create() : NULL;
send_features_request(sw, rconn);
return sw;
[0 ... UINT8_MAX] = sizeof (struct ofp_header),
[OFPT_FEATURES_REPLY] = sizeof (struct ofp_switch_features),
[OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
+ [OFPT_PORT_STATUS] = sizeof(struct ofp_port_status),
};
struct ofp_header *oh;
oh = msg->data;
if (msg->size < min_size[oh->type]) {
- VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
- rconn_get_name(rconn),
- msg->size, oh->type, min_size[oh->type]);
+ VLOG_WARN_RL(&rl,
+ "%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
+ rconn_get_name(rconn),
+ msg->size, oh->type, min_size[oh->type]);
return;
}
- if (oh->type == OFPT_FEATURES_REPLY) {
- struct ofp_switch_features *osf = msg->data;
- sw->datapath_id = osf->datapath_id;
+ if (oh->type == OFPT_ECHO_REQUEST) {
+ process_echo_request(sw, rconn, msg->data);
+ } else if (oh->type == OFPT_FEATURES_REPLY) {
+ process_switch_features(sw, rconn, msg->data);
} else if (sw->datapath_id == 0) {
send_features_request(sw, rconn);
} else if (oh->type == OFPT_PACKET_IN) {
process_packet_in(sw, rconn, msg->data);
+ } else if (oh->type == OFPT_PORT_STATUS) {
+ process_port_status(sw, rconn, msg->data);
} else {
if (VLOG_IS_DBG_ENABLED()) {
char *p = ofp_to_string(msg->data, msg->size, 2);
- VLOG_DBG("OpenFlow packet ignored: %s", p);
+ VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", p);
free(p);
}
}
static void
send_features_request(struct lswitch *sw, struct rconn *rconn)
{
- time_t now = time(0);
+ time_t now = time_now();
if (now >= sw->last_features_request + 1) {
struct buffer *b;
- struct ofp_header *ofr;
struct ofp_switch_config *osc;
/* Send OFPT_FEATURES_REQUEST. */
- b = buffer_new(0);
- ofr = buffer_put_uninit(b, sizeof *ofr);
- memset(ofr, 0, sizeof *ofr);
- ofr->type = OFPT_FEATURES_REQUEST;
- ofr->version = OFP_VERSION;
- ofr->length = htons(sizeof *ofr);
+ make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
queue_tx(sw, rconn, b);
/* Send OFPT_SET_CONFIG. */
- b = buffer_new(0);
- osc = buffer_put_uninit(b, sizeof *osc);
- memset(osc, 0, sizeof *osc);
- osc->header.type = OFPT_SET_CONFIG;
- osc->header.version = OFP_VERSION;
- osc->header.length = htons(sizeof *osc);
+ osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
osc->flags = htons(OFPC_SEND_FLOW_EXP);
osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
queue_tx(sw, rconn, b);
static void
queue_tx(struct lswitch *sw, struct rconn *rconn, struct buffer *b)
{
- int retval = rconn_send(rconn, b);
- if (retval) {
+ int retval = rconn_send_with_limit(rconn, b, &sw->n_queued, 10);
+ if (retval && retval != ENOTCONN) {
if (retval == EAGAIN) {
- /* FIXME: ratelimit. */
- VLOG_WARN("%s: tx queue overflow", rconn_get_name(rconn));
- } else if (retval == ENOTCONN) {
- /* Ignore. */
+ VLOG_WARN_RL(&rl, "%s: tx queue overflow", rconn_get_name(rconn));
} else {
- /* FIXME: ratelimit. */
- VLOG_WARN("%s: send: %s", rconn_get_name(rconn), strerror(retval));
+ VLOG_WARN_RL(&rl, "%s: send: %s",
+ rconn_get_name(rconn), strerror(retval));
}
- buffer_delete(b);
+ }
+}
+
+static void
+process_switch_features(struct lswitch *sw, struct rconn *rconn,
+ struct ofp_switch_features *osf)
+{
+ size_t n_ports = ((ntohs(osf->header.length)
+ - offsetof(struct ofp_switch_features, ports))
+ / sizeof *osf->ports);
+ size_t i;
+
+ sw->datapath_id = osf->datapath_id;
+ sw->capabilities = ntohl(osf->capabilities);
+ for (i = 0; i < n_ports; i++) {
+ process_phy_port(sw, rconn, &osf->ports[i]);
}
}
if (sw->ml) {
if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) {
- VLOG_DBG("learned that "ETH_ADDR_FMT" is on datapath %"
- PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src),
- ntohll(sw->datapath_id), in_port);
+ VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on datapath %"
+ PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src),
+ ntohll(sw->datapath_id), in_port);
}
out_port = mac_learning_lookup(sw->ml, flow.dl_dst);
}
- if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
+ if (in_port == out_port) {
+ /* The input and output port match. Set up a flow to drop packets. */
+ queue_tx(sw, rconn, make_add_flow(&flow, ntohl(opi->buffer_id),
+ sw->max_idle, 0));
+ } else if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
/* The output port is known, or we always flood everything, so add a
* new flow. */
queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
queue_tx(sw, rconn, b);
}
}
+
+static void
+process_echo_request(struct lswitch *sw, struct rconn *rconn,
+ struct ofp_header *rq)
+{
+ queue_tx(sw, rconn, make_echo_reply(rq));
+}
+
+static void
+process_port_status(struct lswitch *sw, struct rconn *rconn,
+ struct ofp_port_status *ops)
+{
+ process_phy_port(sw, rconn, &ops->desc);
+}
+
+static void
+process_phy_port(struct lswitch *sw, struct rconn *rconn,
+ const struct ofp_phy_port *opp)
+{
+ if (sw->capabilities & OFPC_STP && opp->features & ntohl(OFPPF_STP)) {
+ uint32_t flags = ntohl(opp->flags);
+ uint32_t new_flags = flags & ~(OFPPFL_NO_RECV | OFPPFL_NO_RECV_STP
+ | OFPPFL_NO_FWD | OFPPFL_NO_PACKET_IN);
+ if (!(flags & (OFPPFL_NO_STP | OFPPFL_PORT_DOWN | OFPPFL_LINK_DOWN))) {
+ bool forward = false;
+ bool learn = false;
+ switch (flags & OFPPFL_STP_MASK) {
+ case OFPPFL_STP_LISTEN:
+ case OFPPFL_STP_BLOCK:
+ break;
+ case OFPPFL_STP_LEARN:
+ learn = true;
+ break;
+ case OFPPFL_STP_FORWARD:
+ forward = learn = true;
+ break;
+ }
+ if (!forward) {
+ new_flags |= OFPPFL_NO_RECV | OFPPFL_NO_FWD;
+ }
+ if (!learn) {
+ new_flags |= OFPPFL_NO_PACKET_IN;
+ }
+ }
+ if (flags != new_flags) {
+ struct ofp_port_mod *opm;
+ struct buffer *b;
+ int retval;
+
+ VLOG_WARN("port %d: flags=%x new_flags=%x",
+ ntohs(opp->port_no), flags, new_flags);
+ opm = make_openflow(sizeof *opm, OFPT_PORT_MOD, &b);
+ opm->mask = htonl(flags ^ new_flags);
+ opm->desc = *opp;
+ opm->desc.flags = htonl(new_flags);
+ retval = rconn_send(rconn, b, NULL);
+ if (retval) {
+ if (retval != ENOTCONN) {
+ VLOG_WARN_RL(&rl, "%s: send: %s",
+ rconn_get_name(rconn), strerror(retval));
+ }
+ buffer_delete(b);
+ }
+ }
+ }
+}