#define VCONN_H 1
#include <stdbool.h>
+#include <stddef.h>
#include <stdint.h>
struct buffer;
void vconn_recv_wait(struct vconn *);
void vconn_send_wait(struct vconn *);
+void *make_openflow(size_t openflow_len, uint8_t type, struct buffer **);
+void *make_openflow_xid(size_t openflow_len, uint8_t type,
+ uint32_t xid, struct buffer **);
+void update_openflow_length(struct buffer *);
struct buffer *make_add_simple_flow(const struct flow *,
uint32_t buffer_id, uint16_t out_port,
uint16_t max_idle);
#include "ofp-print.h"
#include "openflow.h"
#include "poll-loop.h"
+#include "random.h"
#include "util.h"
#define THIS_MODULE VLM_vconn
vconn_wait(vconn, WAIT_SEND);
}
+/* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
+ * containing an OpenFlow header with the given 'type' and a random transaction
+ * id. Stores the new buffer in '*bufferp'. The caller must free the buffer
+ * when it is no longer needed. */
+void *
+make_openflow(size_t openflow_len, uint8_t type, struct buffer **bufferp)
+{
+ return make_openflow_xid(openflow_len, type, random_uint32(), bufferp);
+}
+
+/* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
+ * containing an OpenFlow header with the given 'type' and transaction id
+ * 'xid'. Stores the new buffer in '*bufferp'. The caller must free the
+ * buffer when it is no longer needed. */
+void *
+make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
+ struct buffer **bufferp)
+{
+ struct buffer *buffer;
+ struct ofp_header *oh;
+
+ assert(openflow_len >= sizeof *oh);
+ assert(openflow_len <= UINT16_MAX);
+ buffer = *bufferp = buffer_new(openflow_len);
+ oh = buffer_put_uninit(buffer, openflow_len);
+ memset(oh, 0, openflow_len);
+ oh->version = OFP_VERSION;
+ oh->type = type;
+ oh->length = htons(openflow_len);
+ oh->xid = xid;
+ return oh;
+}
+
+/* Updates the 'length' field of the OpenFlow message in 'buffer' to
+ * 'buffer->size'. */
+void
+update_openflow_length(struct buffer *buffer)
+{
+ struct ofp_header *oh = buffer_at_assert(buffer, 0, sizeof *oh);
+ oh->length = htons(buffer->size);
+}
+
struct buffer *
make_add_simple_flow(const struct flow *flow,
uint32_t buffer_id, uint16_t out_port, uint16_t max_idle)
}
static void *
-alloc_openflow_buffer(struct datapath *dp, size_t openflow_len, uint8_t type,
- const struct sender *sender, struct buffer **bufferp)
+make_openflow_reply(size_t openflow_len, uint8_t type,
+ const struct sender *sender, struct buffer **bufferp)
{
- struct buffer *buffer;
- struct ofp_header *oh;
-
- buffer = *bufferp = buffer_new(openflow_len);
- oh = buffer_put_uninit(buffer, openflow_len);
- oh->version = OFP_VERSION;
- oh->type = type;
- oh->length = 0; /* Filled in by send_openflow_buffer(). */
- oh->xid = sender ? sender->xid : 0;
- return oh;
+ return make_openflow_xid(openflow_len, type, sender ? sender->xid : 0,
+ bufferp);
}
static int
{
struct remote *remote = sender ? sender->remote : dp->controller;
struct rconn *rconn = remote->rconn;
- struct ofp_header *oh;
int retval;
- oh = buffer_at_assert(buffer, 0, sizeof *oh);
- oh->length = htons(buffer->size);
-
+ update_openflow_length(buffer);
retval = rconn_send(rconn, buffer);
if (retval) {
VLOG_WARN("send to %s failed: %s",
struct ofp_switch_features *ofr;
struct sw_port *p;
- ofr = alloc_openflow_buffer(dp, sizeof *ofr, OFPT_FEATURES_REPLY,
- sender, &buffer);
+ ofr = make_openflow_reply(sizeof *ofr, OFPT_FEATURES_REPLY,
+ sender, &buffer);
ofr->datapath_id = htonll(dp->id);
ofr->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
ofr->n_compression = 0; /* Not supported */
{
struct buffer *buffer;
struct ofp_port_status *ops;
- ops = alloc_openflow_buffer(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
- &buffer);
+ ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &buffer);
ops->reason = status;
memset(ops->pad, 0, sizeof ops->pad);
fill_port_desc(p->dp, p, &ops->desc);
{
struct buffer *buffer;
struct ofp_flow_expired *ofe;
- ofe = alloc_openflow_buffer(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, NULL,
- &buffer);
+ ofe = make_openflow_xid(sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &buffer);
flow_fill_match(&ofe->match, &flow->key);
memset(ofe->pad, 0, sizeof ofe->pad);
{
struct buffer *buffer;
struct ofp_error_msg *oem;
- oem = alloc_openflow_buffer(dp, sizeof(*oem)+len, OFPT_ERROR_MSG,
- sender, &buffer);
+ oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR_MSG,
+ sender, &buffer);
oem->type = htons(type);
oem->code = htons(code);
memcpy(oem->data, data, len);
struct buffer *buffer;
struct ofp_switch_config *osc;
- osc = alloc_openflow_buffer(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY,
- sender, &buffer);
+ osc = make_openflow_reply(sizeof *osc, OFPT_GET_CONFIG_REPLY,
+ sender, &buffer);
assert(sizeof *osc == sizeof dp->config);
memcpy(((char *)osc) + sizeof osc->header,
return 0;
}
- osr = alloc_openflow_buffer(dp, sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
- &buffer);
+ osr = make_openflow_reply(sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
+ &buffer);
osr->type = htons(cb->s - stats);
osr->flags = 0;
\f
/* Generic commands. */
-static void *
-alloc_openflow_buffer(size_t openflow_len, uint8_t type,
- struct buffer **bufferp)
-{
- struct buffer *buffer;
- struct ofp_header *oh;
-
- buffer = *bufferp = buffer_new(openflow_len);
- oh = buffer_put_uninit(buffer, openflow_len);
- memset(oh, 0, openflow_len);
- oh->version = OFP_VERSION;
- oh->type = type;
- oh->length = 0;
- oh->xid = random_uint32();
- return oh;
-}
-
static void *
alloc_stats_request(size_t body_len, uint16_t type, struct buffer **bufferp)
{
struct ofp_stats_request *rq;
- rq = alloc_openflow_buffer((offsetof(struct ofp_stats_request, body)
- + body_len), OFPT_STATS_REQUEST, bufferp);
+ rq = make_openflow((offsetof(struct ofp_stats_request, body)
+ + body_len), OFPT_STATS_REQUEST, bufferp);
rq->type = htons(type);
rq->flags = htons(0);
return rq->body;
static void
send_openflow_buffer(struct vconn *vconn, struct buffer *buffer)
{
- struct ofp_header *oh;
-
- oh = buffer_at_assert(buffer, 0, sizeof *oh);
- oh->length = htons(buffer->size);
-
+ update_openflow_length(buffer);
run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
}
struct vconn *vconn;
struct buffer *reply;
+ update_openflow_length(request);
run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
reply = transact_openflow(vconn, request);
ofp_print(stdout, reply->data, reply->size, 1);
dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
{
struct buffer *request;
- alloc_openflow_buffer(sizeof(struct ofp_header), request_type, &request);
+ make_openflow(sizeof(struct ofp_header), request_type, &request);
dump_transaction(vconn_name, request);
}
/* Parse and send. */
size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
- ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
+ ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &n_actions,
NULL, &priority, &max_idle);
ofm->command = htons(OFPFC_ADD);
/* Parse and send. */
size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
- ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
+ ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
str_to_flow(line, &ofm->match, &ofm->actions[0], &n_actions,
- NULL, &priority, &max_idle);
+ NULL, &priority, &max_idle);
ofm->command = htons(OFPFC_ADD);
ofm->max_idle = htons(max_idle);
ofm->buffer_id = htonl(UINT32_MAX);
/* Parse and send. */
size = sizeof *ofm;
- ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
+ ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, 0, NULL,
- &priority, NULL);
+ &priority, NULL);
ofm->command = htons(OFPFC_DELETE);
ofm->max_idle = htons(0);
ofm->buffer_id = htonl(UINT32_MAX);
struct vconn *vconn;
struct buffer *reply;
- alloc_openflow_buffer(sizeof(struct ofp_header), OFPT_ECHO_REQUEST,
- &request);
+ make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
reply = transact_openflow(vconn, request);
if (reply->size != request->size) {
struct buffer *request, *reply;
struct ofp_header *rq_hdr, *rpy_hdr;
- rq_hdr = alloc_openflow_buffer(sizeof(struct ofp_header) + payload,
- OFPT_ECHO_REQUEST, &request);
+ rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
+ OFPT_ECHO_REQUEST, &request);
random_bytes(rq_hdr + 1, payload);
gettimeofday(&start, NULL);
struct buffer *request;
struct ofp_header *rq_hdr;
- rq_hdr = alloc_openflow_buffer(message_size, OFPT_ECHO_REQUEST,
- &request);
+ rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
memset(rq_hdr + 1, 0, payload_size);
buffer_delete(transact_openflow(vconn, request));
}