#include "ofpbuf.h"
#include "ofp-actions.h"
#include "ofp-errors.h"
+#include "ofp-msgs.h"
#include "ofp-parse.h"
#include "ofp-print.h"
#include "ofp-util.h"
};
struct lswitch {
+ struct rconn *rconn;
+
/* If nonnegative, the switch sets up flows that expire after the given
* number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
* Otherwise, the switch processes every packet. */
/* Number of outgoing queued packets on the rconn. */
struct rconn_packet_counter *queued;
+
+ /* If true, do not reply to any messages from the switch (for debugging
+ * fail-open mode). */
+ bool mute;
};
/* 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 ofpbuf *);
-static void send_features_request(struct lswitch *, struct rconn *);
+static void queue_tx(struct lswitch *, struct ofpbuf *);
+static void send_features_request(struct lswitch *);
+static void lswitch_process_packet(struct lswitch *, const struct ofpbuf *);
static enum ofperr process_switch_features(struct lswitch *,
- struct ofp_switch_features *);
-static void process_packet_in(struct lswitch *, struct rconn *,
- const struct ofp_header *);
-static void process_echo_request(struct lswitch *, struct rconn *,
- const struct ofp_header *);
+ struct ofp_header *);
+static void process_packet_in(struct lswitch *, const struct ofp_header *);
+static void process_echo_request(struct lswitch *, const struct ofp_header *);
/* Creates and returns a new learning switch whose configuration is given by
* 'cfg'.
struct lswitch *sw;
sw = xzalloc(sizeof *sw);
+ sw->rconn = rconn;
sw->max_idle = cfg->max_idle;
sw->datapath_id = 0;
sw->last_features_request = time_now() - 1;
}
sw->queued = rconn_packet_counter_create();
- send_features_request(sw, rconn);
+ send_features_request(sw);
protocol = ofputil_protocol_from_ofp_version(rconn_get_version(rconn));
if (cfg->default_flows) {
return sw;
}
+bool
+lswitch_is_alive(const struct lswitch *sw)
+{
+ return rconn_is_alive(sw->rconn);
+}
+
/* Destroys 'sw'. */
void
lswitch_destroy(struct lswitch *sw)
if (sw) {
struct lswitch_port *node, *next;
+ rconn_destroy(sw->rconn);
HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
hmap_remove(&sw->queue_numbers, &node->hmap_node);
free(node);
void
lswitch_run(struct lswitch *sw)
{
+ int i;
+
if (sw->ml) {
mac_learning_run(sw->ml, NULL);
}
+
+ rconn_run(sw->rconn);
+
+ for (i = 0; i < 50; i++) {
+ struct ofpbuf *msg;
+
+ msg = rconn_recv(sw->rconn);
+ if (!msg) {
+ break;
+ }
+
+ if (!sw->mute) {
+ lswitch_process_packet(sw, msg);
+ }
+ ofpbuf_delete(msg);
+ }
}
void
if (sw->ml) {
mac_learning_wait(sw->ml);
}
+ rconn_run(sw->rconn);
+ rconn_recv_wait(sw->rconn);
}
/* Processes 'msg', which should be an OpenFlow received on 'rconn', according
* to the learning switch state in 'sw'. The most likely result of processing
* is that flow-setup and packet-out OpenFlow messages will be sent out on
* 'rconn'. */
-void
-lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
- const struct ofpbuf *msg)
+static void
+lswitch_process_packet(struct lswitch *sw, const struct ofpbuf *msg)
{
- const struct ofp_header *oh = msg->data;
- const struct ofputil_msg_type *type;
+ enum ofptype type;
+ struct ofpbuf b;
+
+ b = *msg;
+ if (ofptype_pull(&type, &b)) {
+ return;
+ }
if (sw->datapath_id == 0
- && oh->type != OFPT_ECHO_REQUEST
- && oh->type != OFPT_FEATURES_REPLY) {
- send_features_request(sw, rconn);
+ && type != OFPTYPE_ECHO_REQUEST
+ && type != OFPTYPE_FEATURES_REPLY) {
+ send_features_request(sw);
return;
}
- ofputil_decode_msg_type(oh, &type);
- switch (ofputil_msg_type_code(type)) {
- case OFPUTIL_OFPT_ECHO_REQUEST:
- process_echo_request(sw, rconn, msg->data);
+ switch (type) {
+ case OFPTYPE_ECHO_REQUEST:
+ process_echo_request(sw, msg->data);
break;
- case OFPUTIL_OFPT_FEATURES_REPLY:
+ case OFPTYPE_FEATURES_REPLY:
process_switch_features(sw, msg->data);
break;
- case OFPUTIL_OFPT_PACKET_IN:
- case OFPUTIL_NXT_PACKET_IN:
- process_packet_in(sw, rconn, msg->data);
+ case OFPTYPE_PACKET_IN:
+ process_packet_in(sw, msg->data);
break;
- case OFPUTIL_OFPT_FLOW_REMOVED:
+ case OFPTYPE_FLOW_REMOVED:
/* Nothing to do. */
break;
- case OFPUTIL_MSG_INVALID:
- case OFPUTIL_OFPT_HELLO:
- case OFPUTIL_OFPT_ERROR:
- case OFPUTIL_OFPT_ECHO_REPLY:
- case OFPUTIL_OFPT_FEATURES_REQUEST:
- case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
- case OFPUTIL_OFPT_GET_CONFIG_REPLY:
- case OFPUTIL_OFPT_SET_CONFIG:
- case OFPUTIL_OFPT_PORT_STATUS:
- case OFPUTIL_OFPT_PACKET_OUT:
- case OFPUTIL_OFPT_FLOW_MOD:
- case OFPUTIL_OFPT_PORT_MOD:
- case OFPUTIL_OFPT_BARRIER_REQUEST:
- case OFPUTIL_OFPT_BARRIER_REPLY:
- case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
- case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
- case OFPUTIL_OFPST_DESC_REQUEST:
- case OFPUTIL_OFPST_FLOW_REQUEST:
- case OFPUTIL_OFPST_AGGREGATE_REQUEST:
- case OFPUTIL_OFPST_TABLE_REQUEST:
- case OFPUTIL_OFPST_PORT_REQUEST:
- case OFPUTIL_OFPST_QUEUE_REQUEST:
- case OFPUTIL_OFPST_PORT_DESC_REQUEST:
- case OFPUTIL_OFPST_DESC_REPLY:
- case OFPUTIL_OFPST_FLOW_REPLY:
- case OFPUTIL_OFPST_QUEUE_REPLY:
- case OFPUTIL_OFPST_PORT_REPLY:
- case OFPUTIL_OFPST_TABLE_REPLY:
- case OFPUTIL_OFPST_AGGREGATE_REPLY:
- case OFPUTIL_OFPST_PORT_DESC_REPLY:
- case OFPUTIL_NXT_ROLE_REQUEST:
- case OFPUTIL_NXT_ROLE_REPLY:
- case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
- case OFPUTIL_NXT_SET_FLOW_FORMAT:
- case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
- case OFPUTIL_NXT_FLOW_MOD:
- case OFPUTIL_NXT_FLOW_REMOVED:
- case OFPUTIL_NXT_FLOW_AGE:
- case OFPUTIL_NXT_SET_ASYNC_CONFIG:
- case OFPUTIL_NXT_SET_CONTROLLER_ID:
- case OFPUTIL_NXST_FLOW_REQUEST:
- case OFPUTIL_NXST_AGGREGATE_REQUEST:
- case OFPUTIL_NXST_FLOW_REPLY:
- case OFPUTIL_NXST_AGGREGATE_REPLY:
+ case OFPTYPE_HELLO:
+ case OFPTYPE_ERROR:
+ case OFPTYPE_ECHO_REPLY:
+ case OFPTYPE_FEATURES_REQUEST:
+ case OFPTYPE_GET_CONFIG_REQUEST:
+ case OFPTYPE_GET_CONFIG_REPLY:
+ case OFPTYPE_SET_CONFIG:
+ case OFPTYPE_PORT_STATUS:
+ case OFPTYPE_PACKET_OUT:
+ case OFPTYPE_FLOW_MOD:
+ case OFPTYPE_PORT_MOD:
+ case OFPTYPE_BARRIER_REQUEST:
+ case OFPTYPE_BARRIER_REPLY:
+ case OFPTYPE_DESC_STATS_REQUEST:
+ case OFPTYPE_DESC_STATS_REPLY:
+ case OFPTYPE_FLOW_STATS_REQUEST:
+ case OFPTYPE_FLOW_STATS_REPLY:
+ case OFPTYPE_AGGREGATE_STATS_REQUEST:
+ case OFPTYPE_AGGREGATE_STATS_REPLY:
+ case OFPTYPE_TABLE_STATS_REQUEST:
+ case OFPTYPE_TABLE_STATS_REPLY:
+ case OFPTYPE_PORT_STATS_REQUEST:
+ case OFPTYPE_PORT_STATS_REPLY:
+ case OFPTYPE_QUEUE_STATS_REQUEST:
+ case OFPTYPE_QUEUE_STATS_REPLY:
+ case OFPTYPE_PORT_DESC_STATS_REQUEST:
+ case OFPTYPE_PORT_DESC_STATS_REPLY:
+ case OFPTYPE_ROLE_REQUEST:
+ case OFPTYPE_ROLE_REPLY:
+ case OFPTYPE_SET_FLOW_FORMAT:
+ case OFPTYPE_FLOW_MOD_TABLE_ID:
+ case OFPTYPE_SET_PACKET_IN_FORMAT:
+ case OFPTYPE_FLOW_AGE:
+ case OFPTYPE_SET_ASYNC_CONFIG:
+ case OFPTYPE_SET_CONTROLLER_ID:
+ case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
+ case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
+ case OFPTYPE_FLOW_MONITOR_CANCEL:
+ case OFPTYPE_FLOW_MONITOR_PAUSED:
+ case OFPTYPE_FLOW_MONITOR_RESUMED:
default:
if (VLOG_IS_DBG_ENABLED()) {
char *s = ofp_to_string(msg->data, msg->size, 2);
}
\f
static void
-send_features_request(struct lswitch *sw, struct rconn *rconn)
+send_features_request(struct lswitch *sw)
{
time_t now = time_now();
if (now >= sw->last_features_request + 1) {
struct ofpbuf *b;
struct ofp_switch_config *osc;
+ int ofp_version = rconn_get_version(sw->rconn);
+
+ assert(ofp_version > 0 && ofp_version < 0xff);
/* Send OFPT_FEATURES_REQUEST. */
- make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
- queue_tx(sw, rconn, b);
+ b = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, ofp_version, 0);
+ queue_tx(sw, b);
/* Send OFPT_SET_CONFIG. */
- osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
+ b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, ofp_version, sizeof *osc);
+ osc = ofpbuf_put_zeros(b, sizeof *osc);
osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
- queue_tx(sw, rconn, b);
+ queue_tx(sw, b);
sw->last_features_request = now;
}
}
static void
-queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
+queue_tx(struct lswitch *sw, struct ofpbuf *b)
{
- int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
+ int retval = rconn_send_with_limit(sw->rconn, b, sw->queued, 10);
if (retval && retval != ENOTCONN) {
if (retval == EAGAIN) {
VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
- sw->datapath_id, rconn_get_name(rconn));
+ sw->datapath_id, rconn_get_name(sw->rconn));
} else {
VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
- sw->datapath_id, rconn_get_name(rconn),
+ sw->datapath_id, rconn_get_name(sw->rconn),
strerror(retval));
}
}
}
static enum ofperr
-process_switch_features(struct lswitch *sw, struct ofp_switch_features *osf)
+process_switch_features(struct lswitch *sw, struct ofp_header *oh)
{
struct ofputil_switch_features features;
struct ofputil_phy_port port;
enum ofperr error;
struct ofpbuf b;
- error = ofputil_decode_switch_features(osf, &features, &b);
+ error = ofputil_decode_switch_features(oh, &features, &b);
if (error) {
VLOG_ERR("received invalid switch feature reply (%s)",
ofperr_to_string(error));
sw->datapath_id = features.datapath_id;
- while (!ofputil_pull_phy_port(osf->header.version, &b, &port)) {
+ while (!ofputil_pull_phy_port(oh->version, &b, &port)) {
struct lswitch_port *lp = shash_find_data(&sw->queue_names, port.name);
if (lp && hmap_node_is_null(&lp->hmap_node)) {
lp->port_no = port.port_no;
}
static void
-process_packet_in(struct lswitch *sw, struct rconn *rconn,
- const struct ofp_header *oh)
+process_packet_in(struct lswitch *sw, const struct ofp_header *oh)
{
struct ofputil_packet_in pi;
uint32_t queue_id;
fm.ofpacts_len = ofpacts.size;
buffer = ofputil_encode_flow_mod(&fm, sw->protocol);
- queue_tx(sw, rconn, buffer);
+ queue_tx(sw, buffer);
/* If the switch didn't buffer the packet, we need to send a copy. */
if (pi.buffer_id == UINT32_MAX && out_port != OFPP_NONE) {
- queue_tx(sw, rconn, ofputil_encode_packet_out(&po));
+ queue_tx(sw, ofputil_encode_packet_out(&po));
}
} else {
/* We don't know that MAC, or we don't set up flows. Send along the
* packet without setting up a flow. */
if (pi.buffer_id != UINT32_MAX || out_port != OFPP_NONE) {
- queue_tx(sw, rconn, ofputil_encode_packet_out(&po));
+ queue_tx(sw, ofputil_encode_packet_out(&po));
}
}
}
static void
-process_echo_request(struct lswitch *sw, struct rconn *rconn,
- const struct ofp_header *rq)
+process_echo_request(struct lswitch *sw, const struct ofp_header *rq)
{
- queue_tx(sw, rconn, make_echo_reply(rq));
+ queue_tx(sw, make_echo_reply(rq));
}