- * The OpenFlow header length is initially set to 'openflow_len'; if the
- * message is later extended, the length should be updated with
- * update_openflow_length() before sending.
- *
- * Returns the header. */
-void *
-make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
- struct ofpbuf **bufferp)
-{
- *bufferp = ofpbuf_new(openflow_len);
- return put_openflow_xid(openflow_len, type, xid, *bufferp);
-}
-
-/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
- * with the given 'type' and an arbitrary transaction id. Allocated bytes
- * beyond the header, if any, are zeroed.
- *
- * The OpenFlow header length is initially set to 'openflow_len'; if the
- * message is later extended, the length should be updated with
- * update_openflow_length() before sending.
- *
- * Returns the header. */
-void *
-put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
-{
- return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
-}
-
-/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
- * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
- * the header, if any, are zeroed.
- *
- * The OpenFlow header length is initially set to 'openflow_len'; if the
- * message is later extended, the length should be updated with
- * update_openflow_length() before sending.
- *
- * Returns the header. */
-void *
-put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
- struct ofpbuf *buffer)
-{
- struct ofp_header *oh;
-
- assert(openflow_len >= sizeof *oh);
- assert(openflow_len <= UINT16_MAX);
-
- oh = ofpbuf_put_uninit(buffer, openflow_len);
- oh->version = OFP_VERSION;
- oh->type = type;
- oh->length = htons(openflow_len);
- oh->xid = xid;
- memset(oh + 1, 0, openflow_len - sizeof *oh);
- return oh;
-}
-
-/* Updates the 'length' field of the OpenFlow message in 'buffer' to
- * 'buffer->size'. */
-void
-update_openflow_length(struct ofpbuf *buffer)
-{
- struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
- oh->length = htons(buffer->size);
-}
-
-struct ofpbuf *
-make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len)
-{
- struct ofp_flow_mod *ofm;
- size_t size = sizeof *ofm + actions_len;
- struct ofpbuf *out = ofpbuf_new(size);
- ofm = ofpbuf_put_zeros(out, sizeof *ofm);
- ofm->header.version = OFP_VERSION;
- ofm->header.type = OFPT_FLOW_MOD;
- ofm->header.length = htons(size);
- ofm->match.wildcards = htonl(0);
- ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
- : flow->in_port);
- memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
- memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
- ofm->match.dl_vlan = flow->dl_vlan;
- ofm->match.dl_type = flow->dl_type;
- ofm->match.nw_src = flow->nw_src;
- ofm->match.nw_dst = flow->nw_dst;
- ofm->match.nw_proto = flow->nw_proto;
- ofm->match.tp_src = flow->tp_src;
- ofm->match.tp_dst = flow->tp_dst;
- ofm->command = htons(command);
- return out;
-}
-
-struct ofpbuf *
-make_add_flow(const flow_t *flow, uint32_t buffer_id,
- uint16_t idle_timeout, size_t actions_len)
-{
- struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
- struct ofp_flow_mod *ofm = out->data;
- ofm->idle_timeout = htons(idle_timeout);
- ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
- ofm->buffer_id = htonl(buffer_id);
- return out;
-}
-
-struct ofpbuf *
-make_del_flow(const flow_t *flow)
-{
- struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
- struct ofp_flow_mod *ofm = out->data;
- ofm->out_port = htons(OFPP_NONE);
- return out;
-}
-
-struct ofpbuf *
-make_add_simple_flow(const flow_t *flow,
- uint32_t buffer_id, uint16_t out_port,
- uint16_t idle_timeout)
-{
- struct ofp_action_output *oao;
- struct ofpbuf *buffer = make_add_flow(flow, buffer_id, idle_timeout,
- sizeof *oao);
- oao = ofpbuf_put_zeros(buffer, sizeof *oao);
- oao->type = htons(OFPAT_OUTPUT);
- oao->len = htons(sizeof *oao);
- oao->port = htons(out_port);
- return buffer;
-}
-
-struct ofpbuf *
-make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
- uint16_t in_port,
- const struct ofp_action_header *actions, size_t n_actions)
-{
- size_t actions_len = n_actions * sizeof *actions;
- struct ofp_packet_out *opo;
- size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
- struct ofpbuf *out = ofpbuf_new(size);
-
- opo = ofpbuf_put_uninit(out, sizeof *opo);
- opo->header.version = OFP_VERSION;
- opo->header.type = OFPT_PACKET_OUT;
- opo->header.length = htons(size);
- opo->header.xid = htonl(0);
- opo->buffer_id = htonl(buffer_id);
- opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
- opo->actions_len = htons(actions_len);
- ofpbuf_put(out, actions, actions_len);
- if (packet) {
- ofpbuf_put(out, packet->data, packet->size);
- }
- return out;
-}
-
-struct ofpbuf *
-make_unbuffered_packet_out(const struct ofpbuf *packet,
- uint16_t in_port, uint16_t out_port)
-{
- struct ofp_action_output action;
- action.type = htons(OFPAT_OUTPUT);
- action.len = htons(sizeof action);
- action.port = htons(out_port);
- return make_packet_out(packet, UINT32_MAX, in_port,
- (struct ofp_action_header *) &action, 1);
-}
-
-struct ofpbuf *
-make_buffered_packet_out(uint32_t buffer_id,
- uint16_t in_port, uint16_t out_port)
-{
- struct ofp_action_output action;
- action.type = htons(OFPAT_OUTPUT);
- action.len = htons(sizeof action);
- action.port = htons(out_port);
- return make_packet_out(NULL, buffer_id, in_port,
- (struct ofp_action_header *) &action, 1);
-}
-
-/* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
-struct ofpbuf *
-make_echo_request(void)
-{
- struct ofp_header *rq;
- struct ofpbuf *out = ofpbuf_new(sizeof *rq);
- rq = ofpbuf_put_uninit(out, sizeof *rq);
- rq->version = OFP_VERSION;
- rq->type = OFPT_ECHO_REQUEST;
- rq->length = htons(sizeof *rq);
- rq->xid = 0;
- return out;
-}
-
-/* Creates and returns an OFPT_ECHO_REPLY message matching the
- * OFPT_ECHO_REQUEST message in 'rq'. */
-struct ofpbuf *
-make_echo_reply(const struct ofp_header *rq)
-{
- size_t size = ntohs(rq->length);
- struct ofpbuf *out = ofpbuf_new(size);
- struct ofp_header *reply = ofpbuf_put(out, rq, size);
- reply->type = OFPT_ECHO_REPLY;
- return out;
-}
-
-static int
-check_message_type(uint8_t got_type, uint8_t want_type)
-{
- if (got_type != want_type) {
- char *want_type_name = ofp_message_type_to_string(want_type);
- char *got_type_name = ofp_message_type_to_string(got_type);
- VLOG_WARN_RL(&bad_ofmsg_rl,
- "received bad message type %s (expected %s)",
- got_type_name, want_type_name);
- free(want_type_name);
- free(got_type_name);
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
- }
- return 0;
-}
-
-/* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
- * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
- * with ofp_mkerr()). */
-int
-check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
-{
- size_t got_size;
- int error;
-
- error = check_message_type(msg->type, type);
- if (error) {
- return error;
- }
-
- got_size = ntohs(msg->length);
- if (got_size != size) {
- char *type_name = ofp_message_type_to_string(type);
- VLOG_WARN_RL(&bad_ofmsg_rl,
- "received %s message of length %"PRIu16" (expected %zu)",
- type_name, got_size, size);
- free(type_name);
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
- }
-
- return 0;
-}
-
-/* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
- * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if
- * the checks pass, otherwise an OpenFlow error code (produced with
- * ofp_mkerr()).