+ if (ob_len < sizeof *ob) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
+ "%"PRIuSIZE" is not valid", ob_len);
+ goto err;
+ } else if (ob_len > buckets_length) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
+ "%"PRIuSIZE" exceeds remaining buckets data size %"
+ PRIuSIZE, ob_len, buckets_length);
+ goto err;
+ } else if (actions_len > ob_len - sizeof *ob) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
+ "length %"PRIuSIZE" exceeds remaining bucket "
+ "data size %"PRIuSIZE, actions_len,
+ ob_len - sizeof *ob);
+ goto err;
+ }
+ buckets_length -= ob_len;
+
+ err = ofpacts_pull_openflow_actions(msg, actions_len, version,
+ &ofpacts);
+ if (err) {
+ goto err;
+ }
+
+ properties_len = ob_len - sizeof *ob - actions_len;
+ struct ofpbuf properties = ofpbuf_const_initializer(
+ ofpbuf_pull(msg, properties_len), properties_len);
+ while (properties.size > 0) {
+ struct ofpbuf payload;
+ uint64_t type;
+
+ err = ofpprop_pull(&properties, &payload, &type);
+ if (err) {
+ goto err;
+ }
+
+ switch (type) {
+ case OFPGBPT15_WEIGHT:
+ err = ofpprop_parse_be16(&payload, &weight);
+ break;
+
+ case OFPGBPT15_WATCH_PORT:
+ err = ofpprop_parse_be32(&payload, &watch_port);
+ break;
+
+ case OFPGBPT15_WATCH_GROUP:
+ err = ofpprop_parse_be32(&payload, &watch_group);
+ break;
+
+ default:
+ err = OFPPROP_UNKNOWN(false, "group bucket", type);
+ break;
+ }
+
+ if (err) {
+ goto err;
+ }
+ }
+
+ bucket = xzalloc(sizeof *bucket);
+
+ bucket->weight = ntohs(weight);
+ err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
+ if (err) {
+ err = OFPERR_OFPGMFC_BAD_WATCH;
+ goto err;
+ }
+ bucket->watch_group = ntohl(watch_group);
+ bucket->bucket_id = ntohl(ob->bucket_id);
+ if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
+ bucket->bucket_id);
+ err = OFPERR_OFPGMFC_BAD_BUCKET;
+ goto err;
+ }
+
+ bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
+ bucket->ofpacts_len = ofpacts.size;
+ list_push_back(buckets, &bucket->list_node);
+
+ continue;
+
+ err:
+ free(bucket);
+ ofpbuf_uninit(&ofpacts);
+ ofputil_bucket_list_destroy(buckets);
+ return err;
+ }
+
+ if (ofputil_bucket_check_duplicate_id(buckets)) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
+ ofputil_bucket_list_destroy(buckets);
+ return OFPERR_OFPGMFC_BAD_BUCKET;
+ }
+
+ return 0;
+}
+
+static void
+ofputil_init_group_properties(struct ofputil_group_props *gp)
+{
+ memset(gp, 0, sizeof *gp);
+}
+
+static enum ofperr
+parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
+ enum ofp11_group_type group_type,
+ enum ofp15_group_mod_command group_cmd,
+ struct ofputil_group_props *gp)
+{
+ struct ntr_group_prop_selection_method *prop = payload->data;
+ size_t fields_len, method_len;
+ enum ofperr error;
+
+ switch (group_type) {
+ case OFPGT11_SELECT:
+ break;
+ case OFPGT11_ALL:
+ case OFPGT11_INDIRECT:
+ case OFPGT11_FF:
+ OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
+ "only allowed for select groups");
+ return OFPERR_OFPBPC_BAD_VALUE;
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ switch (group_cmd) {
+ case OFPGC15_ADD:
+ case OFPGC15_MODIFY:
+ break;
+ case OFPGC15_DELETE:
+ case OFPGC15_INSERT_BUCKET:
+ case OFPGC15_REMOVE_BUCKET:
+ OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
+ "only allowed for add and delete group modifications");
+ return OFPERR_OFPBPC_BAD_VALUE;
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ if (payload->size < sizeof *prop) {
+ OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property "
+ "length %u is not valid", payload->size);
+ return OFPERR_OFPBPC_BAD_LEN;
+ }
+
+ method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
+
+ if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
+ OFPPROP_LOG(&bad_ofmsg_rl, false,
+ "ntr selection method is not null terminated");
+ return OFPERR_OFPBPC_BAD_VALUE;
+ }
+
+ if (strcmp("hash", prop->selection_method)) {
+ OFPPROP_LOG(&bad_ofmsg_rl, false,
+ "ntr selection method '%s' is not supported",
+ prop->selection_method);
+ return OFPERR_OFPBPC_BAD_VALUE;
+ }
+
+ strcpy(gp->selection_method, prop->selection_method);
+ gp->selection_method_param = ntohll(prop->selection_method_param);
+
+ if (!method_len && gp->selection_method_param) {
+ OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
+ "non-zero but selection method is empty");
+ return OFPERR_OFPBPC_BAD_VALUE;
+ }
+
+ ofpbuf_pull(payload, sizeof *prop);
+
+ fields_len = ntohs(prop->length) - sizeof *prop;
+ if (!method_len && fields_len) {
+ OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
+ "zero but fields are provided");
+ return OFPERR_OFPBPC_BAD_VALUE;
+ }
+
+ error = oxm_pull_field_array(payload->data, fields_len,
+ &gp->fields);
+ if (error) {
+ OFPPROP_LOG(&bad_ofmsg_rl, false,
+ "ntr selection method fields are invalid");
+ return error;
+ }
+
+ return 0;
+}
+
+static enum ofperr
+parse_ofp15_group_properties(struct ofpbuf *msg,
+ enum ofp11_group_type group_type,
+ enum ofp15_group_mod_command group_cmd,
+ struct ofputil_group_props *gp,
+ size_t properties_len)
+{
+ struct ofpbuf properties = ofpbuf_const_initializer(
+ ofpbuf_pull(msg, properties_len), properties_len);
+ while (properties.size > 0) {
+ struct ofpbuf payload;
+ enum ofperr error;
+ uint64_t type;
+
+ error = ofpprop_pull(&properties, &payload, &type);
+ if (error) {
+ return error;
+ }
+
+ switch (type) {
+ case OFPPROP_EXP(NTR_VENDOR_ID, NTRT_SELECTION_METHOD):
+ case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID, NTRT_SELECTION_METHOD):
+ error = parse_group_prop_ntr_selection_method(&payload, group_type,
+ group_cmd, gp);
+ break;
+
+ default:
+ error = OFPPROP_UNKNOWN(false, "group", type);
+ break;
+ }
+
+ if (error) {
+ return error;
+ }
+ }
+
+ return 0;
+}
+
+static int
+ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
+ struct ofpbuf *msg,
+ enum ofp_version version)
+{
+ struct ofp11_group_desc_stats *ogds;
+ size_t length;
+
+ if (!msg->header) {
+ ofpraw_pull_assert(msg);
+ }
+
+ if (!msg->size) {
+ return EOF;
+ }
+
+ ogds = ofpbuf_try_pull(msg, sizeof *ogds);
+ if (!ogds) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
+ "leftover bytes at end", msg->size);
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+ gd->type = ogds->type;
+ gd->group_id = ntohl(ogds->group_id);
+
+ length = ntohs(ogds->length);
+ if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
+ "length %"PRIuSIZE, length);
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+
+ return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
+ &gd->buckets);
+}
+
+static int
+ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
+ struct ofpbuf *msg,
+ enum ofp_version version)
+{
+ struct ofp15_group_desc_stats *ogds;
+ uint16_t length, bucket_list_len;
+ int error;
+
+ if (!msg->header) {
+ ofpraw_pull_assert(msg);
+ }
+
+ if (!msg->size) {
+ return EOF;
+ }
+
+ ogds = ofpbuf_try_pull(msg, sizeof *ogds);
+ if (!ogds) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
+ "leftover bytes at end", msg->size);
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+ gd->type = ogds->type;
+ gd->group_id = ntohl(ogds->group_id);
+
+ length = ntohs(ogds->length);
+ if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
+ "length %u", length);
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+
+ bucket_list_len = ntohs(ogds->bucket_list_len);
+ if (length < bucket_list_len + sizeof *ogds) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
+ "bucket list length %u", bucket_list_len);
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+ error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
+ &gd->buckets);
+ if (error) {
+ return error;
+ }
+
+ /* By definition group desc messages don't have a group mod command.
+ * However, parse_group_prop_ntr_selection_method() checks to make sure
+ * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
+ * against group mod messages with other commands supplying
+ * a NTR selection method group experimenter property.
+ * Such properties are valid for group desc replies so
+ * claim that the group mod command is OFPGC15_ADD to
+ * satisfy the check in parse_group_prop_ntr_selection_method() */
+ return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
+ length - sizeof *ogds - bucket_list_len);
+}
+
+/* Converts a group description reply in 'msg' into an abstract
+ * ofputil_group_desc in 'gd'.
+ *
+ * Multiple group description 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_group_desc_reply(struct ofputil_group_desc *gd,
+ struct ofpbuf *msg, enum ofp_version version)
+{
+ ofputil_init_group_properties(&gd->props);
+
+ switch (version)
+ {
+ case OFP11_VERSION:
+ case OFP12_VERSION:
+ case OFP13_VERSION:
+ case OFP14_VERSION:
+ return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
+
+ case OFP15_VERSION:
+ return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
+
+ case OFP10_VERSION:
+ default:
+ OVS_NOT_REACHED();
+ }
+}
+
+void
+ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
+{
+ ofputil_bucket_list_destroy(&gm->buckets);
+}
+
+static struct ofpbuf *
+ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
+ const struct ofputil_group_mod *gm)
+{
+ struct ofpbuf *b;
+ struct ofp11_group_mod *ogm;
+ size_t start_ogm;
+ struct ofputil_bucket *bucket;
+
+ b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
+ start_ogm = b->size;
+ ofpbuf_put_zeros(b, sizeof *ogm);
+
+ LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
+ ofputil_put_ofp11_bucket(bucket, b, ofp_version);
+ }
+ ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
+ ogm->command = htons(gm->command);
+ ogm->type = gm->type;
+ ogm->group_id = htonl(gm->group_id);
+
+ return b;
+}
+
+static struct ofpbuf *
+ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
+ const struct ofputil_group_mod *gm)
+{
+ struct ofpbuf *b;
+ struct ofp15_group_mod *ogm;
+ size_t start_ogm;
+ struct ofputil_bucket *bucket;
+ struct id_pool *bucket_ids = NULL;
+
+ b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
+ start_ogm = b->size;
+ ofpbuf_put_zeros(b, sizeof *ogm);
+
+ LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
+ uint32_t bucket_id;
+
+ /* Generate a bucket id if none was supplied */
+ if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
+ if (!bucket_ids) {
+ const struct ofputil_bucket *bkt;
+
+ bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
+
+ /* Mark all bucket_ids that are present in gm
+ * as used in the pool. */
+ LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
+ if (bkt == bucket) {
+ break;
+ }
+ if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
+ id_pool_add(bucket_ids, bkt->bucket_id);
+ }
+ }
+ }
+
+ if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
+ OVS_NOT_REACHED();
+ }
+ } else {
+ bucket_id = bucket->bucket_id;
+ }
+
+ ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
+ }
+ ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
+ ogm->command = htons(gm->command);
+ ogm->type = gm->type;
+ ogm->group_id = htonl(gm->group_id);
+ ogm->command_bucket_id = htonl(gm->command_bucket_id);
+ ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
+
+ /* Add group properties */
+ if (gm->props.selection_method[0]) {
+ ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
+ }
+
+ id_pool_destroy(bucket_ids);
+ return b;
+}
+
+static void
+bad_group_cmd(enum ofp15_group_mod_command cmd)
+{
+ const char *opt_version;
+ const char *version;
+ const char *cmd_str;
+
+ switch (cmd) {
+ case OFPGC15_ADD:
+ case OFPGC15_MODIFY:
+ case OFPGC15_DELETE:
+ version = "1.1";
+ opt_version = "11";
+ break;
+
+ case OFPGC15_INSERT_BUCKET:
+ case OFPGC15_REMOVE_BUCKET:
+ version = "1.5";
+ opt_version = "15";
+ break;
+
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ switch (cmd) {
+ case OFPGC15_ADD:
+ cmd_str = "add-group";
+ break;
+
+ case OFPGC15_MODIFY:
+ cmd_str = "mod-group";
+ break;
+
+ case OFPGC15_DELETE:
+ cmd_str = "del-group";
+ break;
+
+ case OFPGC15_INSERT_BUCKET:
+ cmd_str = "insert-bucket";
+ break;
+
+ case OFPGC15_REMOVE_BUCKET:
+ cmd_str = "remove-bucket";
+ break;
+
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
+ cmd_str, version, opt_version);
+
+}
+
+/* Converts abstract group mod 'gm' into a message for OpenFlow version
+ * 'ofp_version' and returns the message. */
+struct ofpbuf *
+ofputil_encode_group_mod(enum ofp_version ofp_version,
+ const struct ofputil_group_mod *gm)
+{
+
+ switch (ofp_version) {
+ case OFP10_VERSION:
+ bad_group_cmd(gm->command);
+
+ case OFP11_VERSION:
+ case OFP12_VERSION:
+ case OFP13_VERSION:
+ case OFP14_VERSION:
+ if (gm->command > OFPGC11_DELETE) {
+ bad_group_cmd(gm->command);
+ }
+ return ofputil_encode_ofp11_group_mod(ofp_version, gm);
+
+ case OFP15_VERSION:
+ return ofputil_encode_ofp15_group_mod(ofp_version, gm);
+
+ default:
+ OVS_NOT_REACHED();
+ }
+}
+
+static enum ofperr
+ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
+ struct ofputil_group_mod *gm)
+{
+ const struct ofp11_group_mod *ogm;
+ enum ofperr error;
+
+ ogm = ofpbuf_pull(msg, sizeof *ogm);
+ gm->command = ntohs(ogm->command);
+ gm->type = ogm->type;
+ gm->group_id = ntohl(ogm->group_id);
+ gm->command_bucket_id = OFPG15_BUCKET_ALL;
+
+ error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
+ &gm->buckets);
+
+ /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
+ if (!error
+ && ofp_version >= OFP13_VERSION
+ && gm->command == OFPGC11_DELETE
+ && !list_is_empty(&gm->buckets)) {
+ error = OFPERR_OFPGMFC_INVALID_GROUP;
+ }
+
+ return error;
+}
+
+static enum ofperr
+ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
+ struct ofputil_group_mod *gm)
+{
+ const struct ofp15_group_mod *ogm;
+ uint16_t bucket_list_len;
+ enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
+
+ ogm = ofpbuf_pull(msg, sizeof *ogm);
+ gm->command = ntohs(ogm->command);
+ gm->type = ogm->type;
+ gm->group_id = ntohl(ogm->group_id);
+
+ gm->command_bucket_id = ntohl(ogm->command_bucket_id);
+ switch (gm->command) {
+ case OFPGC15_REMOVE_BUCKET:
+ if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
+ error = 0;
+ }
+ /* Fall through */
+ case OFPGC15_INSERT_BUCKET:
+ if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
+ gm->command_bucket_id == OFPG15_BUCKET_FIRST
+ || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
+ error = 0;
+ }
+ break;
+
+ case OFPGC11_ADD:
+ case OFPGC11_MODIFY:
+ case OFPGC11_DELETE:
+ default:
+ if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
+ error = 0;
+ }
+ break;
+ }
+ if (error) {
+ VLOG_WARN_RL(&bad_ofmsg_rl,
+ "group command bucket id (%u) is out of range",
+ gm->command_bucket_id);
+ return OFPERR_OFPGMFC_BAD_BUCKET;
+ }
+
+ bucket_list_len = ntohs(ogm->bucket_array_len);
+ error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
+ gm->type, &gm->buckets);
+ if (error) {
+ return error;
+ }
+
+ return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
+ msg->size);
+}
+
+/* Converts OpenFlow group mod message 'oh' into an abstract group mod in
+ * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
+enum ofperr
+ofputil_decode_group_mod(const struct ofp_header *oh,
+ struct ofputil_group_mod *gm)
+{
+ ofputil_init_group_properties(&gm->props);
+
+ enum ofp_version ofp_version = oh->version;
+ struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
+ ofpraw_pull_assert(&msg);
+
+ enum ofperr err;
+ switch (ofp_version)
+ {
+ case OFP11_VERSION:
+ case OFP12_VERSION:
+ case OFP13_VERSION:
+ case OFP14_VERSION:
+ err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
+ break;
+
+ case OFP15_VERSION:
+ err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
+ break;
+
+ case OFP10_VERSION:
+ default:
+ OVS_NOT_REACHED();
+ }
+ if (err) {
+ return err;
+ }
+
+ switch (gm->type) {
+ case OFPGT11_INDIRECT:
+ if (!list_is_singleton(&gm->buckets)) {
+ return OFPERR_OFPGMFC_INVALID_GROUP;
+ }
+ break;
+ case OFPGT11_ALL:
+ case OFPGT11_SELECT:
+ case OFPGT11_FF:
+ break;
+ default:
+ return OFPERR_OFPGMFC_BAD_TYPE;
+ }
+
+ switch (gm->command) {
+ case OFPGC11_ADD:
+ case OFPGC11_MODIFY:
+ case OFPGC11_DELETE:
+ case OFPGC15_INSERT_BUCKET:
+ break;
+ case OFPGC15_REMOVE_BUCKET:
+ if (!list_is_empty(&gm->buckets)) {
+ return OFPERR_OFPGMFC_BAD_BUCKET;
+ }
+ break;
+ default:
+ return OFPERR_OFPGMFC_BAD_COMMAND;
+ }
+
+ struct ofputil_bucket *bucket;
+ LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
+ if (bucket->weight && gm->type != OFPGT11_SELECT) {
+ return OFPERR_OFPGMFC_INVALID_GROUP;
+ }
+
+ switch (gm->type) {
+ case OFPGT11_ALL:
+ case OFPGT11_INDIRECT:
+ if (ofputil_bucket_has_liveness(bucket)) {
+ return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
+ }
+ break;
+ case OFPGT11_SELECT:
+ break;
+ case OFPGT11_FF:
+ if (!ofputil_bucket_has_liveness(bucket)) {
+ return OFPERR_OFPGMFC_INVALID_GROUP;
+ }
+ break;
+ default:
+ /* Returning BAD TYPE to be consistent
+ * though gm->type has been checked already. */
+ return OFPERR_OFPGMFC_BAD_TYPE;
+ }
+ }
+
+ return 0;
+}
+
+/* Parse a queue status request message into 'oqsr'.
+ * Returns 0 if successful, otherwise an OFPERR_* number. */
+enum ofperr
+ofputil_decode_queue_stats_request(const struct ofp_header *request,
+ struct ofputil_queue_stats_request *oqsr)
+{
+ switch ((enum ofp_version)request->version) {
+ case OFP15_VERSION:
+ case OFP14_VERSION:
+ case OFP13_VERSION:
+ case OFP12_VERSION:
+ case OFP11_VERSION: {
+ const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
+ oqsr->queue_id = ntohl(qsr11->queue_id);
+ return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
+ }
+
+ case OFP10_VERSION: {
+ const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
+ oqsr->queue_id = ntohl(qsr10->queue_id);
+ oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
+ /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
+ if (oqsr->port_no == OFPP_ALL) {
+ oqsr->port_no = OFPP_ANY;
+ }
+ return 0;
+ }
+
+ default:
+ OVS_NOT_REACHED();
+ }
+}
+
+/* Encode a queue stats request for 'oqsr', the encoded message
+ * will be for OpenFlow version 'ofp_version'. Returns message
+ * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
+struct ofpbuf *
+ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
+ const struct ofputil_queue_stats_request *oqsr)
+{
+ struct ofpbuf *request;
+
+ switch (ofp_version) {
+ case OFP11_VERSION:
+ case OFP12_VERSION:
+ case OFP13_VERSION:
+ case OFP14_VERSION:
+ case OFP15_VERSION: {
+ struct ofp11_queue_stats_request *req;
+ request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
+ req = ofpbuf_put_zeros(request, sizeof *req);
+ req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
+ req->queue_id = htonl(oqsr->queue_id);
+ break;
+ }
+ case OFP10_VERSION: {
+ struct ofp10_queue_stats_request *req;
+ request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
+ req = ofpbuf_put_zeros(request, sizeof *req);
+ /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
+ req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
+ ? OFPP_ALL : oqsr->port_no));
+ req->queue_id = htonl(oqsr->queue_id);
+ break;
+ }
+ default:
+ OVS_NOT_REACHED();
+ }
+
+ return request;
+}
+
+/* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
+ * message 'oh'. */
+size_t
+ofputil_count_queue_stats(const struct ofp_header *oh)
+{
+ struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
+ ofpraw_pull_assert(&b);
+
+ for (size_t n = 0; ; n++) {
+ struct ofputil_queue_stats qs;
+ if (ofputil_decode_queue_stats(&qs, &b)) {
+ return n;
+ }
+ }
+}
+
+static enum ofperr
+ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
+ const struct ofp10_queue_stats *qs10)
+{
+ oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
+ oqs->queue_id = ntohl(qs10->queue_id);
+ oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
+ oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
+ oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
+ oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
+
+ return 0;
+}
+
+static enum ofperr
+ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
+ const struct ofp11_queue_stats *qs11)
+{
+ enum ofperr error;
+
+ error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
+ if (error) {
+ return error;
+ }
+
+ oqs->queue_id = ntohl(qs11->queue_id);
+ oqs->tx_bytes = ntohll(qs11->tx_bytes);
+ oqs->tx_packets = ntohll(qs11->tx_packets);
+ oqs->tx_errors = ntohll(qs11->tx_errors);
+ oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
+
+ return 0;
+}
+
+static enum ofperr
+ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
+ const struct ofp13_queue_stats *qs13)
+{
+ enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
+ if (!error) {
+ oqs->duration_sec = ntohl(qs13->duration_sec);
+ oqs->duration_nsec = ntohl(qs13->duration_nsec);
+ }
+
+ return error;
+}
+
+static enum ofperr
+ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
+ struct ofpbuf *msg)
+{
+ const struct ofp14_queue_stats *qs14;
+ size_t len;
+
+ qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
+ if (!qs14) {
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+
+ len = ntohs(qs14->length);
+ if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
+ return OFPERR_OFPBRC_BAD_LEN;
+ }
+ ofpbuf_pull(msg, len - sizeof *qs14);
+
+ /* No properties yet defined, so ignore them for now. */
+
+ return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
+}
+
+/* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
+ * ofputil_queue_stats in 'qs'.
+ *
+ * Multiple OFPST_QUEUE_STATS 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_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
+{
+ enum ofperr error;
+ enum ofpraw raw;
+
+ error = (msg->header ? ofpraw_decode(&raw, msg->header)
+ : ofpraw_pull(&raw, msg));
+ if (error) {
+ return error;
+ }
+
+ if (!msg->size) {
+ return EOF;
+ } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
+ return ofputil_pull_ofp14_queue_stats(qs, msg);
+ } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
+ const struct ofp13_queue_stats *qs13;
+
+ qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
+ if (!qs13) {
+ goto bad_len;
+ }
+ return ofputil_queue_stats_from_ofp13(qs, qs13);
+ } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
+ const struct ofp11_queue_stats *qs11;
+
+ qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
+ if (!qs11) {
+ goto bad_len;
+ }
+ return ofputil_queue_stats_from_ofp11(qs, qs11);
+ } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
+ const struct ofp10_queue_stats *qs10;
+
+ qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
+ if (!qs10) {
+ goto bad_len;
+ }
+ return ofputil_queue_stats_from_ofp10(qs, qs10);
+ } else {
+ OVS_NOT_REACHED();
+ }
+
+ bad_len:
+ VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
+ "bytes at end", msg->size);
+ return OFPERR_OFPBRC_BAD_LEN;
+}
+
+static void
+ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
+ struct ofp10_queue_stats *qs10)
+{
+ qs10->port_no = htons(ofp_to_u16(oqs->port_no));
+ memset(qs10->pad, 0, sizeof qs10->pad);
+ qs10->queue_id = htonl(oqs->queue_id);
+ put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
+ put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
+ put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
+}
+
+static void
+ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
+ struct ofp11_queue_stats *qs11)
+{
+ qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
+ qs11->queue_id = htonl(oqs->queue_id);
+ qs11->tx_bytes = htonll(oqs->tx_bytes);
+ qs11->tx_packets = htonll(oqs->tx_packets);
+ qs11->tx_errors = htonll(oqs->tx_errors);
+}
+
+static void
+ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
+ struct ofp13_queue_stats *qs13)
+{
+ ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
+ if (oqs->duration_sec != UINT32_MAX) {
+ qs13->duration_sec = htonl(oqs->duration_sec);
+ qs13->duration_nsec = htonl(oqs->duration_nsec);
+ } else {
+ qs13->duration_sec = OVS_BE32_MAX;
+ qs13->duration_nsec = OVS_BE32_MAX;
+ }
+}
+
+static void
+ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
+ struct ofp14_queue_stats *qs14)
+{
+ qs14->length = htons(sizeof *qs14);
+ memset(qs14->pad, 0, sizeof qs14->pad);
+ ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
+}
+
+
+/* Encode a queue stat for 'oqs' and append it to 'replies'. */
+void
+ofputil_append_queue_stat(struct ovs_list *replies,
+ const struct ofputil_queue_stats *oqs)
+{
+ switch (ofpmp_version(replies)) {
+ case OFP13_VERSION: {
+ struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
+ ofputil_queue_stats_to_ofp13(oqs, reply);
+ break;
+ }
+
+ case OFP12_VERSION:
+ case OFP11_VERSION: {
+ struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
+ ofputil_queue_stats_to_ofp11(oqs, reply);
+ break;
+ }
+
+ case OFP10_VERSION: {
+ struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
+ ofputil_queue_stats_to_ofp10(oqs, reply);
+ break;
+ }
+
+ case OFP14_VERSION:
+ case OFP15_VERSION: {
+ struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
+ ofputil_queue_stats_to_ofp14(oqs, reply);
+ break;
+ }
+
+ default:
+ OVS_NOT_REACHED();
+ }
+}
+
+enum ofperr
+ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
+ struct ofputil_bundle_ctrl_msg *msg)
+{
+ struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
+ enum ofpraw raw = ofpraw_pull_assert(&b);
+ ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL
+ || raw == OFPRAW_ONFT13_BUNDLE_CONTROL);
+
+ const struct ofp14_bundle_ctrl_msg *m = b.msg;
+ msg->bundle_id = ntohl(m->bundle_id);
+ msg->type = ntohs(m->type);
+ msg->flags = ntohs(m->flags);
+
+ return 0;
+}