+ if (type != OFPUTIL_METER_FEATURES) {
+ struct ofp13_meter_multipart_request *omr;
+ omr = ofpbuf_put_zeros(msg, sizeof *omr);
+ omr->meter_id = htonl(meter_id);
+ }
+ return msg;
+}
+
+static void
+ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
+ struct ofpbuf *msg)
+{
+ uint16_t n = 0;
+
+ for (n = 0; n < n_bands; ++n) {
+ /* Currently all band types have same size. */
+ struct ofp13_meter_band_dscp_remark *ombh;
+ size_t ombh_len = sizeof *ombh;
+
+ ombh = ofpbuf_put_zeros(msg, ombh_len);
+
+ ombh->type = htons(mb->type);
+ ombh->len = htons(ombh_len);
+ ombh->rate = htonl(mb->rate);
+ ombh->burst_size = htonl(mb->burst_size);
+ ombh->prec_level = mb->prec_level;
+
+ mb++;
+ }
+}
+
+/* Encode a meter stat for 'mc' and append it to 'replies'. */
+void
+ofputil_append_meter_config(struct list *replies,
+ const struct ofputil_meter_config *mc)
+{
+ struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
+ size_t start_ofs = ofpbuf_size(msg);
+ struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply);
+ reply->flags = htons(mc->flags);
+ reply->meter_id = htonl(mc->meter_id);
+
+ ofputil_put_bands(mc->n_bands, mc->bands, msg);
+
+ reply->length = htons(ofpbuf_size(msg) - start_ofs);
+
+ ofpmp_postappend(replies, start_ofs);
+}
+
+/* Encode a meter stat for 'ms' and append it to 'replies'. */
+void
+ofputil_append_meter_stats(struct list *replies,
+ const struct ofputil_meter_stats *ms)
+{
+ struct ofp13_meter_stats *reply;
+ uint16_t n = 0;
+ uint16_t len;
+
+ len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
+ reply = ofpmp_append(replies, len);
+
+ reply->meter_id = htonl(ms->meter_id);
+ reply->len = htons(len);
+ memset(reply->pad, 0, sizeof reply->pad);
+ reply->flow_count = htonl(ms->flow_count);
+ reply->packet_in_count = htonll(ms->packet_in_count);
+ reply->byte_in_count = htonll(ms->byte_in_count);
+ reply->duration_sec = htonl(ms->duration_sec);
+ reply->duration_nsec = htonl(ms->duration_nsec);
+
+ for (n = 0; n < ms->n_bands; ++n) {
+ const struct ofputil_meter_band_stats *src = &ms->bands[n];
+ struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
+
+ dst->packet_band_count = htonll(src->packet_count);
+ dst->byte_band_count = htonll(src->byte_count);
+ }
+}
+
+/* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
+ * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
+ * 'bands'. The caller must have initialized 'bands' and retains ownership of
+ * it across the call.
+ *
+ * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
+ * message. Calling this function multiple times for a single 'msg' iterates
+ * through the replies. 'bands' is cleared for each reply.
+ *
+ * Returns 0 if successful, EOF if no replies were left in this 'msg',
+ * otherwise a positive errno value. */
+int
+ofputil_decode_meter_config(struct ofpbuf *msg,
+ struct ofputil_meter_config *mc,
+ struct ofpbuf *bands)
+{
+ const struct ofp13_meter_config *omc;
+ enum ofperr err;
+
+ /* Pull OpenFlow headers for the first call. */
+ if (!msg->frame) {
+ ofpraw_pull_assert(msg);
+ }
+
+ if (!ofpbuf_size(msg)) {
+ return EOF;
+ }
+
+ omc = ofpbuf_try_pull(msg, sizeof *omc);
+ if (!omc) {
+ VLOG_WARN_RL(&bad_ofmsg_rl,
+ "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
+ ofpbuf_size(msg));
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+
+ ofpbuf_clear(bands);
+ err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
+ &mc->n_bands, bands);
+ if (err) {
+ return err;
+ }
+ mc->meter_id = ntohl(omc->meter_id);
+ mc->flags = ntohs(omc->flags);
+ mc->bands = ofpbuf_data(bands);
+
+ return 0;
+}
+
+static enum ofperr
+ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
+ struct ofpbuf *bands)
+{
+ const struct ofp13_meter_band_stats *ombs;
+ struct ofputil_meter_band_stats *mbs;
+ uint16_t n, i;
+
+ ombs = ofpbuf_try_pull(msg, len);
+ if (!ombs) {
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+
+ n = len / sizeof *ombs;
+ if (len != n * sizeof *ombs) {
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+
+ mbs = ofpbuf_put_uninit(bands, len);
+
+ for (i = 0; i < n; ++i) {
+ mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
+ mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
+ }
+ *n_bands = n;
+ return 0;
+}
+
+/* Converts an OFPMP_METER reply in 'msg' into an abstract
+ * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
+ * decoded into 'bands'.
+ *
+ * Multiple OFPMP_METER replies can be packed into a single OpenFlow
+ * message. Calling this function multiple times for a single 'msg' iterates
+ * through the replies. 'bands' is cleared for each reply.
+ *
+ * Returns 0 if successful, EOF if no replies were left in this 'msg',
+ * otherwise a positive errno value. */
+int
+ofputil_decode_meter_stats(struct ofpbuf *msg,
+ struct ofputil_meter_stats *ms,
+ struct ofpbuf *bands)
+{
+ const struct ofp13_meter_stats *oms;
+ enum ofperr err;
+
+ /* Pull OpenFlow headers for the first call. */
+ if (!msg->frame) {
+ ofpraw_pull_assert(msg);
+ }
+
+ if (!ofpbuf_size(msg)) {
+ return EOF;
+ }
+
+ oms = ofpbuf_try_pull(msg, sizeof *oms);
+ if (!oms) {
+ VLOG_WARN_RL(&bad_ofmsg_rl,
+ "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
+ ofpbuf_size(msg));
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+
+ ofpbuf_clear(bands);
+ err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
+ &ms->n_bands, bands);
+ if (err) {
+ return err;
+ }
+ ms->meter_id = ntohl(oms->meter_id);
+ ms->flow_count = ntohl(oms->flow_count);
+ ms->packet_in_count = ntohll(oms->packet_in_count);
+ ms->byte_in_count = ntohll(oms->byte_in_count);
+ ms->duration_sec = ntohl(oms->duration_sec);
+ ms->duration_nsec = ntohl(oms->duration_nsec);
+ ms->bands = ofpbuf_data(bands);
+
+ return 0;
+}
+
+void
+ofputil_decode_meter_features(const struct ofp_header *oh,
+ struct ofputil_meter_features *mf)
+{
+ const struct ofp13_meter_features *omf = ofpmsg_body(oh);
+
+ mf->max_meters = ntohl(omf->max_meter);
+ mf->band_types = ntohl(omf->band_types);
+ mf->capabilities = ntohl(omf->capabilities);
+ mf->max_bands = omf->max_bands;
+ mf->max_color = omf->max_color;
+}
+
+struct ofpbuf *
+ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
+ const struct ofp_header *request)
+{
+ struct ofpbuf *reply;
+ struct ofp13_meter_features *omf;
+
+ reply = ofpraw_alloc_stats_reply(request, 0);
+ omf = ofpbuf_put_zeros(reply, sizeof *omf);
+
+ omf->max_meter = htonl(mf->max_meters);
+ omf->band_types = htonl(mf->band_types);
+ omf->capabilities = htonl(mf->capabilities);
+ omf->max_bands = mf->max_bands;
+ omf->max_color = mf->max_color;
+
+ return reply;
+}
+
+struct ofpbuf *
+ofputil_encode_meter_mod(enum ofp_version ofp_version,
+ const struct ofputil_meter_mod *mm)
+{
+ struct ofpbuf *msg;
+
+ struct ofp13_meter_mod *omm;
+
+ msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
+ NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
+ omm = ofpbuf_put_zeros(msg, sizeof *omm);
+ omm->command = htons(mm->command);
+ if (mm->command != OFPMC13_DELETE) {
+ omm->flags = htons(mm->meter.flags);
+ }
+ omm->meter_id = htonl(mm->meter.meter_id);
+
+ ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
+
+ ofpmsg_update_length(msg);
+ return msg;
+}
+
+static ovs_be16
+ofputil_tid_command(const struct ofputil_flow_mod *fm,
+ enum ofputil_protocol protocol)
+{
+ return htons(protocol & OFPUTIL_P_TID
+ ? (fm->command & 0xff) | (fm->table_id << 8)
+ : fm->command);
+}
+
+/* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
+ * 'protocol' and returns the message. */
+struct ofpbuf *
+ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
+ enum ofputil_protocol protocol)
+{
+ enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
+ ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
+ struct ofpbuf *msg;
+
+ switch (protocol) {
+ case OFPUTIL_P_OF11_STD:
+ case OFPUTIL_P_OF12_OXM:
+ case OFPUTIL_P_OF13_OXM:
+ case OFPUTIL_P_OF14_OXM: {
+ struct ofp11_flow_mod *ofm;
+ int tailroom;
+
+ tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
+ msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
+ ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
+ if ((protocol == OFPUTIL_P_OF11_STD
+ && (fm->command == OFPFC_MODIFY ||
+ fm->command == OFPFC_MODIFY_STRICT)
+ && fm->cookie_mask == htonll(0))
+ || fm->command == OFPFC_ADD) {
+ ofm->cookie = fm->new_cookie;
+ } else {
+ ofm->cookie = fm->cookie;
+ }
+ ofm->cookie_mask = fm->cookie_mask;
+ if (fm->table_id != OFPTT_ALL
+ || (protocol != OFPUTIL_P_OF11_STD
+ && (fm->command == OFPFC_DELETE ||
+ fm->command == OFPFC_DELETE_STRICT))) {
+ ofm->table_id = fm->table_id;
+ } else {
+ ofm->table_id = 0;
+ }
+ ofm->command = fm->command;
+ ofm->idle_timeout = htons(fm->idle_timeout);
+ ofm->hard_timeout = htons(fm->hard_timeout);
+ ofm->priority = htons(fm->priority);
+ ofm->buffer_id = htonl(fm->buffer_id);
+ ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
+ ofm->out_group = htonl(fm->out_group);
+ ofm->flags = raw_flags;
+ ofputil_put_ofp11_match(msg, &fm->match, protocol);
+ ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
+ version);
+ break;
+ }
+
+ case OFPUTIL_P_OF10_STD:
+ case OFPUTIL_P_OF10_STD_TID: {
+ struct ofp10_flow_mod *ofm;
+
+ msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
+ fm->ofpacts_len);
+ ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
+ ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
+ ofm->cookie = fm->new_cookie;
+ ofm->command = ofputil_tid_command(fm, protocol);
+ ofm->idle_timeout = htons(fm->idle_timeout);
+ ofm->hard_timeout = htons(fm->hard_timeout);
+ ofm->priority = htons(fm->priority);
+ ofm->buffer_id = htonl(fm->buffer_id);
+ ofm->out_port = htons(ofp_to_u16(fm->out_port));
+ ofm->flags = raw_flags;
+ ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
+ version);
+ break;
+ }
+
+ case OFPUTIL_P_OF10_NXM:
+ case OFPUTIL_P_OF10_NXM_TID: {
+ struct nx_flow_mod *nfm;
+ int match_len;
+
+ msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
+ NXM_TYPICAL_LEN + fm->ofpacts_len);
+ nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
+ nfm->command = ofputil_tid_command(fm, protocol);
+ nfm->cookie = fm->new_cookie;
+ match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
+ nfm = ofpbuf_l3(msg);
+ nfm->idle_timeout = htons(fm->idle_timeout);
+ nfm->hard_timeout = htons(fm->hard_timeout);
+ nfm->priority = htons(fm->priority);
+ nfm->buffer_id = htonl(fm->buffer_id);
+ nfm->out_port = htons(ofp_to_u16(fm->out_port));
+ nfm->flags = raw_flags;
+ nfm->match_len = htons(match_len);
+ ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
+ version);
+ break;
+ }
+
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ ofpmsg_update_length(msg);
+ return msg;
+}
+
+static enum ofperr