+/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
+ * ofputil_flow_stats in 'fs'. For OFPST_FLOW messages, 'flow_format' should
+ * be the current flow format at the time when the request corresponding to the
+ * reply in 'msg' was sent. Otherwise 'flow_format' is ignored.
+ *
+ * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
+ * OpenFlow message. Calling this function multiple times for a single 'msg'
+ * iterates through the replies. The caller must initially leave 'msg''s layer
+ * pointers null and not modify them between calls.
+ *
+ * Returns 0 if successful, EOF if no replies were left in this 'msg',
+ * otherwise a positive errno value. */
+int
+ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
+ struct ofpbuf *msg,
+ enum nx_flow_format flow_format)
+{
+ const struct ofputil_msg_type *type;
+ int code;
+
+ ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
+ code = ofputil_msg_type_code(type);
+ if (!msg->l2) {
+ msg->l2 = msg->data;
+ if (code == OFPUTIL_OFPST_FLOW_REPLY) {
+ ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
+ } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
+ ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
+ } else {
+ NOT_REACHED();
+ }
+ }
+
+ if (!msg->size) {
+ return EOF;
+ } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
+ const struct ofp_flow_stats *ofs;
+ size_t length;
+
+ ofs = ofpbuf_try_pull(msg, sizeof *ofs);
+ if (!ofs) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
+ "bytes at end", msg->size);
+ return EINVAL;
+ }
+
+ length = ntohs(ofs->length);
+ if (length < sizeof *ofs) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
+ "length %zu", length);
+ return EINVAL;
+ }
+
+ if (ofputil_pull_actions(msg, length - sizeof *ofs,
+ &fs->actions, &fs->n_actions)) {
+ return EINVAL;
+ }
+
+ fs->cookie = get_32aligned_be64(&ofs->cookie);
+ ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
+ flow_format, fs->cookie, &fs->rule);
+ fs->table_id = ofs->table_id;
+ fs->duration_sec = ntohl(ofs->duration_sec);
+ fs->duration_nsec = ntohl(ofs->duration_nsec);
+ fs->idle_timeout = ntohs(ofs->idle_timeout);
+ fs->hard_timeout = ntohs(ofs->hard_timeout);
+ fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
+ fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
+ } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
+ const struct nx_flow_stats *nfs;
+ size_t match_len, length;
+
+ nfs = ofpbuf_try_pull(msg, sizeof *nfs);
+ if (!nfs) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
+ "bytes at end", msg->size);
+ return EINVAL;
+ }
+
+ length = ntohs(nfs->length);
+ match_len = ntohs(nfs->match_len);
+ if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
+ "claims invalid length %zu", match_len, length);
+ return EINVAL;
+ }
+ if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
+ return EINVAL;
+ }
+
+ if (ofputil_pull_actions(msg,
+ length - sizeof *nfs - ROUND_UP(match_len, 8),
+ &fs->actions, &fs->n_actions)) {
+ return EINVAL;
+ }
+
+ fs->cookie = nfs->cookie;
+ fs->table_id = nfs->table_id;
+ fs->duration_sec = ntohl(nfs->duration_sec);
+ fs->duration_nsec = ntohl(nfs->duration_nsec);
+ fs->idle_timeout = ntohs(nfs->idle_timeout);
+ fs->hard_timeout = ntohs(nfs->hard_timeout);
+ fs->packet_count = ntohll(nfs->packet_count);
+ fs->byte_count = ntohll(nfs->byte_count);
+ } else {
+ NOT_REACHED();
+ }
+
+ return 0;
+}
+
+/* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh', received
+ * when the current flow format was 'flow_format', into an abstract
+ * ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise an
+ * OpenFlow error code.
+ *
+ * For OFPT_FLOW_REMOVED messages, 'flow_format' should be the current flow
+ * format at the time when the message was received. Otherwise 'flow_format'
+ * is ignored. */
+int
+ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
+ const struct ofp_header *oh,
+ enum nx_flow_format flow_format)
+{
+ const struct ofputil_msg_type *type;
+ enum ofputil_msg_code code;
+
+ ofputil_decode_msg_type(oh, &type);
+ code = ofputil_msg_type_code(type);
+ if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
+ const struct ofp_flow_removed *ofr;
+
+ ofr = (const struct ofp_flow_removed *) oh;
+ ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
+ flow_format, ofr->cookie, &fr->rule);
+ fr->cookie = ofr->cookie;
+ fr->reason = ofr->reason;
+ fr->duration_sec = ntohl(ofr->duration_sec);
+ fr->duration_nsec = ntohl(ofr->duration_nsec);
+ fr->idle_timeout = ntohs(ofr->idle_timeout);
+ fr->packet_count = ntohll(ofr->packet_count);
+ fr->byte_count = ntohll(ofr->byte_count);
+ } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
+ struct nx_flow_removed *nfr;
+ struct ofpbuf b;
+ int error;
+
+ ofpbuf_use_const(&b, oh, ntohs(oh->length));
+
+ nfr = ofpbuf_pull(&b, sizeof *nfr);
+ error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
+ &fr->rule);
+ if (error) {
+ return error;
+ }
+ if (b.size) {
+ return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
+ }
+
+ fr->cookie = nfr->cookie;
+ fr->reason = nfr->reason;
+ fr->duration_sec = ntohl(nfr->duration_sec);
+ fr->duration_nsec = ntohl(nfr->duration_nsec);
+ fr->idle_timeout = ntohs(nfr->idle_timeout);
+ fr->packet_count = ntohll(nfr->packet_count);
+ fr->byte_count = ntohll(nfr->byte_count);
+ } else {
+ NOT_REACHED();
+ }
+
+ return 0;
+}
+
+/* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
+ * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
+ * message. */
+struct ofpbuf *
+ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
+ enum nx_flow_format flow_format)
+{
+ struct ofpbuf *msg;
+
+ if (flow_format == NXFF_OPENFLOW10
+ || flow_format == NXFF_TUN_ID_FROM_COOKIE) {
+ struct ofp_flow_removed *ofr;
+
+ ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
+ &msg);
+ ofputil_cls_rule_to_match(&fr->rule, flow_format, &ofr->match,
+ fr->cookie, &ofr->cookie);
+ ofr->priority = htons(fr->rule.priority);
+ ofr->reason = fr->reason;
+ ofr->duration_sec = htonl(fr->duration_sec);
+ ofr->duration_nsec = htonl(fr->duration_nsec);
+ ofr->idle_timeout = htons(fr->idle_timeout);
+ ofr->packet_count = htonll(fr->packet_count);
+ ofr->byte_count = htonll(fr->byte_count);
+ } else if (flow_format == NXFF_NXM) {
+ struct nx_flow_removed *nfr;
+ int match_len;
+
+ make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
+ match_len = nx_put_match(msg, &fr->rule);
+
+ nfr = msg->data;
+ nfr->cookie = fr->cookie;
+ nfr->priority = htons(fr->rule.priority);
+ nfr->reason = fr->reason;
+ nfr->duration_sec = htonl(fr->duration_sec);
+ nfr->duration_nsec = htonl(fr->duration_nsec);
+ nfr->idle_timeout = htons(fr->idle_timeout);
+ nfr->match_len = htons(match_len);
+ nfr->packet_count = htonll(fr->packet_count);
+ nfr->byte_count = htonll(fr->byte_count);
+ } else {
+ NOT_REACHED();
+ }
+
+ return msg;
+}
+
+/* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
+ * and returns the message.
+ *
+ * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
+ * returned ofpbuf.
+ *
+ * If 'rw_packet' is nonnull, then it must contain the same data as
+ * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
+ * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
+ * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
+ * enough headroom to insert a "struct ofp_packet_in", this is more efficient
+ * than ofputil_encode_packet_in() because it does not copy the packet
+ * payload. */
+struct ofpbuf *
+ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
+ struct ofpbuf *rw_packet)
+{
+ int total_len = pin->packet->size;
+ struct ofp_packet_in *opi;
+
+ if (rw_packet) {
+ if (pin->send_len < rw_packet->size) {
+ rw_packet->size = pin->send_len;
+ }
+ } else {
+ rw_packet = ofpbuf_clone_data_with_headroom(
+ pin->packet->data, MIN(pin->send_len, pin->packet->size),
+ offsetof(struct ofp_packet_in, data));
+ }
+
+ /* Add OFPT_PACKET_IN. */
+ opi = ofpbuf_push_zeros(rw_packet, offsetof(struct ofp_packet_in, data));
+ opi->header.version = OFP_VERSION;
+ opi->header.type = OFPT_PACKET_IN;
+ opi->total_len = htons(total_len);
+ opi->in_port = htons(pin->in_port);
+ opi->reason = pin->reason;
+ opi->buffer_id = htonl(pin->buffer_id);
+ update_openflow_length(rw_packet);
+
+ return rw_packet;
+}
+