+
+static void
+encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
+{
+ struct ofputil_tlv_map *map;
+
+ LIST_FOR_EACH (map, list_node, mappings) {
+ struct nx_tlv_map *nx_map;
+
+ nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
+ nx_map->option_class = htons(map->option_class);
+ nx_map->option_type = map->option_type;
+ nx_map->option_len = map->option_len;
+ nx_map->index = htons(map->index);
+ }
+}
+
+struct ofpbuf *
+ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
+ struct ofputil_tlv_table_mod *ttm)
+{
+ struct ofpbuf *b;
+ struct nx_tlv_table_mod *nx_ttm;
+
+ b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
+ nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
+ nx_ttm->command = htons(ttm->command);
+ encode_tlv_table_mappings(b, &ttm->mappings);
+
+ return b;
+}
+
+static enum ofperr
+decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
+ struct ovs_list *mappings)
+{
+ list_init(mappings);
+
+ while (msg->size) {
+ struct nx_tlv_map *nx_map;
+ struct ofputil_tlv_map *map;
+
+ nx_map = ofpbuf_pull(msg, sizeof *nx_map);
+ map = xmalloc(sizeof *map);
+ list_push_back(mappings, &map->list_node);
+
+ map->option_class = ntohs(nx_map->option_class);
+ map->option_type = nx_map->option_type;
+
+ map->option_len = nx_map->option_len;
+ if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
+ VLOG_WARN_RL(&bad_ofmsg_rl,
+ "tlv table option length (%u) is not a valid option size",
+ map->option_len);
+ ofputil_uninit_tlv_table(mappings);
+ return OFPERR_NXTTMFC_BAD_OPT_LEN;
+ }
+
+ map->index = ntohs(nx_map->index);
+ if (map->index >= max_fields) {
+ VLOG_WARN_RL(&bad_ofmsg_rl,
+ "tlv table field index (%u) is too large (max %u)",
+ map->index, max_fields - 1);
+ ofputil_uninit_tlv_table(mappings);
+ return OFPERR_NXTTMFC_BAD_FIELD_IDX;
+ }
+ }
+
+ return 0;
+}
+
+enum ofperr
+ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
+ struct ofputil_tlv_table_mod *ttm)
+{
+ struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
+ ofpraw_pull_assert(&msg);
+
+ struct nx_tlv_table_mod *nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
+ ttm->command = ntohs(nx_ttm->command);
+ if (ttm->command > NXTTMC_CLEAR) {
+ VLOG_WARN_RL(&bad_ofmsg_rl,
+ "tlv table mod command (%u) is out of range",
+ ttm->command);
+ return OFPERR_NXTTMFC_BAD_COMMAND;
+ }
+
+ return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
+ &ttm->mappings);
+}
+
+struct ofpbuf *
+ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
+ struct ofputil_tlv_table_reply *ttr)
+{
+ struct ofpbuf *b;
+ struct nx_tlv_table_reply *nx_ttr;
+
+ b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
+ nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
+ nx_ttr->max_option_space = htonl(ttr->max_option_space);
+ nx_ttr->max_fields = htons(ttr->max_fields);
+
+ encode_tlv_table_mappings(b, &ttr->mappings);
+
+ return b;
+}
+
+/* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
+ * if successful, otherwise an ofperr.
+ *
+ * The decoder verifies that the indexes in 'ttr->mappings' are less than
+ * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
+ * less than TUN_METADATA_NUM_OPTS. */
+enum ofperr
+ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
+ struct ofputil_tlv_table_reply *ttr)
+{
+ struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
+ ofpraw_pull_assert(&msg);
+
+ struct nx_tlv_table_reply *nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
+ ttr->max_option_space = ntohl(nx_ttr->max_option_space);
+ ttr->max_fields = ntohs(nx_ttr->max_fields);
+
+ return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
+}
+
+void
+ofputil_uninit_tlv_table(struct ovs_list *mappings)
+{
+ struct ofputil_tlv_map *map;
+
+ LIST_FOR_EACH_POP (map, list_node, mappings) {
+ free(map);
+ }
+}
+
+const char *
+ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type)
+{
+ switch (type) {
+ case OAM_PACKET_IN: return "PACKET_IN";
+ case OAM_PORT_STATUS: return "PORT_STATUS";
+ case OAM_FLOW_REMOVED: return "FLOW_REMOVED";
+ case OAM_ROLE_STATUS: return "ROLE_STATUS";
+ case OAM_TABLE_STATUS: return "TABLE_STATUS";
+ case OAM_REQUESTFORWARD: return "REQUESTFORWARD";
+
+ case OAM_N_TYPES:
+ default:
+ OVS_NOT_REACHED();
+ }
+}
+
+struct ofp14_async_prop {
+ uint64_t prop_type;
+ enum ofputil_async_msg_type oam;
+ bool master;
+ uint32_t allowed10, allowed14;
+};
+
+#define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
+ { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
+ { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
+
+static const struct ofp14_async_prop async_props[] = {
+ AP_PAIR( 0, OAM_PACKET_IN, OFPR10_BITS, OFPR14_BITS),
+ AP_PAIR( 2, OAM_PORT_STATUS, (1 << OFPPR_N_REASONS) - 1, 0),
+ AP_PAIR( 4, OAM_FLOW_REMOVED, (1 << OVS_OFPRR_NONE) - 1, 0),
+ AP_PAIR( 6, OAM_ROLE_STATUS, (1 << OFPCRR_N_REASONS) - 1, 0),
+ AP_PAIR( 8, OAM_TABLE_STATUS, OFPTR_BITS, 0),
+ AP_PAIR(10, OAM_REQUESTFORWARD, (1 << OFPRFR_N_REASONS) - 1, 0),
+};
+
+#define FOR_EACH_ASYNC_PROP(VAR) \
+ for (const struct ofp14_async_prop *VAR = async_props; \
+ VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
+
+static const struct ofp14_async_prop *
+get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type)
+{
+ FOR_EACH_ASYNC_PROP (ap) {
+ if (prop_type == ap->prop_type) {
+ return ap;
+ }
+ }
+ return NULL;
+}
+
+static const struct ofp14_async_prop *
+get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam,
+ bool master)
+{
+ FOR_EACH_ASYNC_PROP (ap) {
+ if (ap->oam == oam && ap->master == master) {
+ return ap;
+ }
+ }
+ return NULL;
+}
+
+static uint32_t
+ofp14_async_prop_allowed(const struct ofp14_async_prop *prop,
+ enum ofp_version version)
+{
+ return version >= OFP14_VERSION ? prop->allowed14 : prop->allowed10;
+}
+
+static ovs_be32
+encode_async_mask(const struct ofputil_async_cfg *src,
+ const struct ofp14_async_prop *ap,
+ enum ofp_version version)
+{
+ uint32_t mask = ap->master ? src->master[ap->oam] : src->slave[ap->oam];
+ return htonl(mask & ofp14_async_prop_allowed(ap, version));
+}
+
+static enum ofperr
+decode_async_mask(ovs_be32 src,
+ const struct ofp14_async_prop *ap, enum ofp_version version,
+ bool loose, struct ofputil_async_cfg *dst)
+{
+ uint32_t mask = ntohl(src);
+ uint32_t allowed = ofp14_async_prop_allowed(ap, version);
+ if (mask & ~allowed) {
+ OFPPROP_LOG(&bad_ofmsg_rl, loose,
+ "bad value %#x for %s (allowed mask %#x)",
+ mask, ofputil_async_msg_type_to_string(ap->oam),
+ allowed);
+ mask &= allowed;
+ if (!loose) {
+ return OFPERR_OFPACFC_INVALID;
+ }
+ }
+
+ if (ap->oam == OAM_PACKET_IN) {
+ if (mask & (1u << OFPR_NO_MATCH)) {
+ mask |= 1u << OFPR_EXPLICIT_MISS;
+ if (version < OFP13_VERSION) {
+ mask |= 1u << OFPR_IMPLICIT_MISS;
+ }
+ }
+ }
+
+ uint32_t *array = ap->master ? dst->master : dst->slave;
+ array[ap->oam] = mask;
+ return 0;
+}
+
+static enum ofperr
+parse_async_tlv(const struct ofpbuf *property,
+ const struct ofp14_async_prop *ap,
+ struct ofputil_async_cfg *ac,
+ enum ofp_version version, bool loose)
+{
+ enum ofperr error;
+ ovs_be32 mask;
+
+ error = ofpprop_parse_be32(property, &mask);
+ if (error) {
+ return error;
+ }
+
+ if (ofpprop_is_experimenter(ap->prop_type)) {
+ /* For experimenter properties, whether a property is for the master or
+ * slave role is indicated by both 'type' and 'exp_type' in struct
+ * ofp_prop_experimenter. Check that these are consistent. */
+ const struct ofp_prop_experimenter *ope = property->data;
+ bool should_be_master = ope->type == htons(0xffff);
+ if (should_be_master != ap->master) {
+ VLOG_WARN_RL(&bad_ofmsg_rl, "async property type %#"PRIx16" "
+ "indicates %s role but exp_type %"PRIu32" indicates "
+ "%s role",
+ ntohs(ope->type),
+ should_be_master ? "master" : "slave",
+ ntohl(ope->exp_type),
+ ap->master ? "master" : "slave");
+ return OFPERR_OFPBPC_BAD_EXP_TYPE;
+ }
+ }
+
+ return decode_async_mask(mask, ap, version, loose, ac);
+}
+
+static void
+decode_legacy_async_masks(const ovs_be32 masks[2],
+ enum ofputil_async_msg_type oam,
+ enum ofp_version version,
+ struct ofputil_async_cfg *dst)
+{
+ for (int i = 0; i < 2; i++) {
+ bool master = i == 0;
+ const struct ofp14_async_prop *ap
+ = get_ofp14_async_config_prop_by_oam(oam, master);
+ decode_async_mask(masks[i], ap, version, true, dst);
+ }
+}
+
+/* Decodes the OpenFlow "set async config" request and "get async config
+ * reply" message in '*oh' into an abstract form in 'ac'.
+ *
+ * Some versions of the "set async config" request change only some of the
+ * settings and leave the others alone. This function uses 'basis' as the
+ * initial state for decoding these. Other versions of the request change all
+ * the settings; this function ignores 'basis' when decoding these.
+ *
+ * If 'loose' is true, this function ignores properties and values that it does
+ * not understand, as a controller would want to do when interpreting
+ * capabilities provided by a switch. If 'loose' is false, this function
+ * treats unknown properties and values as an error, as a switch would want to
+ * do when interpreting a configuration request made by a controller.
+ *
+ * Returns 0 if successful, otherwise an OFPERR_* value.
+ *
+ * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
+ * the valid range of mask.
+ *
+ * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
+ * supported.*/
+enum ofperr
+ofputil_decode_set_async_config(const struct ofp_header *oh, bool loose,
+ const struct ofputil_async_cfg *basis,
+ struct ofputil_async_cfg *ac)
+{
+ struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
+ enum ofpraw raw = ofpraw_pull_assert(&b);
+
+ if (raw == OFPRAW_OFPT13_SET_ASYNC ||
+ raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
+ raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
+ const struct nx_async_config *msg = ofpmsg_body(oh);
+
+ *ac = OFPUTIL_ASYNC_CFG_INIT;
+ decode_legacy_async_masks(msg->packet_in_mask, OAM_PACKET_IN,
+ oh->version, ac);
+ decode_legacy_async_masks(msg->port_status_mask, OAM_PORT_STATUS,
+ oh->version, ac);
+ decode_legacy_async_masks(msg->flow_removed_mask, OAM_FLOW_REMOVED,
+ oh->version, ac);
+ } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
+ raw == OFPRAW_OFPT14_GET_ASYNC_REPLY ||
+ raw == OFPRAW_NXT_SET_ASYNC_CONFIG2) {
+ *ac = *basis;
+ while (b.size > 0) {
+ struct ofpbuf property;
+ enum ofperr error;
+ uint64_t type;
+
+ error = ofpprop_pull__(&b, &property, 8, 0xfffe, &type);
+ if (error) {
+ return error;
+ }
+
+ const struct ofp14_async_prop *ap
+ = get_ofp14_async_config_prop_by_prop_type(type);
+ error = (ap
+ ? parse_async_tlv(&property, ap, ac, oh->version, loose)
+ : OFPPROP_UNKNOWN(loose, "async config", type));
+ if (error) {
+ /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
+ * knows why, it's OpenFlow. */
+ if (error == OFPERR_OFPBPC_BAD_TYPE) {
+ error = OFPERR_OFPACFC_UNSUPPORTED;
+ }
+ return error;
+ }
+ }
+ } else {
+ return OFPERR_OFPBRC_BAD_VERSION;
+ }
+ return 0;
+}
+
+static void
+encode_legacy_async_masks(const struct ofputil_async_cfg *ac,
+ enum ofputil_async_msg_type oam,
+ enum ofp_version version,
+ ovs_be32 masks[2])
+{
+ for (int i = 0; i < 2; i++) {
+ bool master = i == 0;
+ const struct ofp14_async_prop *ap
+ = get_ofp14_async_config_prop_by_oam(oam, master);
+ masks[i] = encode_async_mask(ac, ap, version);
+ }
+}
+
+static void
+ofputil_put_async_config__(const struct ofputil_async_cfg *ac,
+ struct ofpbuf *buf, bool tlv,
+ enum ofp_version version, uint32_t oams)
+{
+ if (!tlv) {
+ struct nx_async_config *msg = ofpbuf_put_zeros(buf, sizeof *msg);
+ encode_legacy_async_masks(ac, OAM_PACKET_IN, version,
+ msg->packet_in_mask);
+ encode_legacy_async_masks(ac, OAM_PORT_STATUS, version,
+ msg->port_status_mask);
+ encode_legacy_async_masks(ac, OAM_FLOW_REMOVED, version,
+ msg->flow_removed_mask);
+ } else {
+ FOR_EACH_ASYNC_PROP (ap) {
+ if (oams & (1u << ap->oam)) {
+ size_t ofs = buf->size;
+ ofpprop_put_be32(buf, ap->prop_type,
+ encode_async_mask(ac, ap, version));
+
+ /* For experimenter properties, we need to use type 0xfffe for
+ * master and 0xffff for slaves. */
+ if (ofpprop_is_experimenter(ap->prop_type)) {
+ struct ofp_prop_experimenter *ope
+ = ofpbuf_at_assert(buf, ofs, sizeof *ope);
+ ope->type = ap->master ? htons(0xffff) : htons(0xfffe);
+ }
+ }
+ }
+ }
+}
+
+/* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
+ * states that the asynchronous message configuration is 'ac'. */
+struct ofpbuf *
+ofputil_encode_get_async_reply(const struct ofp_header *oh,
+ const struct ofputil_async_cfg *ac)
+{
+ struct ofpbuf *buf;
+
+ enum ofpraw raw = (oh->version < OFP14_VERSION
+ ? OFPRAW_OFPT13_GET_ASYNC_REPLY
+ : OFPRAW_OFPT14_GET_ASYNC_REPLY);
+ struct ofpbuf *reply = ofpraw_alloc_reply(raw, oh, 0);
+ ofputil_put_async_config__(ac, reply,
+ raw == OFPRAW_OFPT14_GET_ASYNC_REPLY,
+ oh->version, UINT32_MAX);
+ return reply;
+
+ return buf;
+}
+
+/* Encodes and returns a message, in a format appropriate for OpenFlow version
+ * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
+ *
+ * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
+ * to configure. OF1.0 through OF1.3 can't natively configure a subset of
+ * messages, so more messages than requested may be configured. OF1.0 through
+ * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
+ * any extensions then this function encodes an Open vSwitch extension message
+ * that does support configuring OVS extension OAM_*. */
+struct ofpbuf *
+ofputil_encode_set_async_config(const struct ofputil_async_cfg *ac,
+ uint32_t oams, enum ofp_version ofp_version)
+{
+ enum ofpraw raw = (ofp_version >= OFP14_VERSION ? OFPRAW_OFPT14_SET_ASYNC
+ : oams & OAM_EXTENSIONS ? OFPRAW_NXT_SET_ASYNC_CONFIG2
+ : ofp_version >= OFP13_VERSION ? OFPRAW_OFPT13_SET_ASYNC
+ : OFPRAW_NXT_SET_ASYNC_CONFIG);
+ struct ofpbuf *request = ofpraw_alloc(raw, ofp_version, 0);
+ ofputil_put_async_config__(ac, request,
+ (raw == OFPRAW_OFPT14_SET_ASYNC ||
+ raw == OFPRAW_NXT_SET_ASYNC_CONFIG2),
+ ofp_version, oams);
+ return request;
+}
+
+struct ofputil_async_cfg
+ofputil_async_cfg_default(enum ofp_version version)
+{
+ /* We enable all of the OF1.4 reasons regardless of 'version' because the
+ * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
+ * introduced in OF1.0, breaking it into more specific categories. When we
+ * encode these for earlier OpenFlow versions, we translate them into
+ * OFPR_ACTION. */
+ uint32_t pin = OFPR14_BITS & ~(1u << OFPR_INVALID_TTL);
+ pin |= 1u << OFPR_EXPLICIT_MISS;
+ if (version <= OFP12_VERSION) {
+ pin |= 1u << OFPR_IMPLICIT_MISS;
+ }
+
+ return (struct ofputil_async_cfg) {
+ .master[OAM_PACKET_IN] = pin,
+
+ .master[OAM_FLOW_REMOVED]
+ = (version >= OFP14_VERSION ? OFPRR14_BITS : OFPRR10_BITS),
+
+ .master[OAM_PORT_STATUS] = OFPPR_BITS,
+ .slave[OAM_PORT_STATUS] = OFPPR_BITS,
+ };
+}
+
+static void
+ofputil_put_ofp14_table_desc(const struct ofputil_table_desc *td,
+ struct ofpbuf *b, enum ofp_version version)
+{
+ struct ofp14_table_desc *otd;
+ struct ofp14_table_mod_prop_vacancy *otv;
+ size_t start_otd;
+
+ start_otd = b->size;
+ ofpbuf_put_zeros(b, sizeof *otd);
+
+ ofpprop_put_u32(b, OFPTMPT14_EVICTION, td->eviction_flags);
+
+ otv = ofpbuf_put_zeros(b, sizeof *otv);
+ otv->type = htons(OFPTMPT14_VACANCY);
+ otv->length = htons(sizeof *otv);
+ otv->vacancy_down = td->table_vacancy.vacancy_down;
+ otv->vacancy_up = td->table_vacancy.vacancy_up;
+ otv->vacancy = td->table_vacancy.vacancy;
+
+ otd = ofpbuf_at_assert(b, start_otd, sizeof *otd);
+ otd->length = htons(b->size - start_otd);
+ otd->table_id = td->table_id;
+ otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
+ td->eviction, td->vacancy,
+ version);
+}
+
+/* Converts the abstract form of a "table status" message in '*ts' into an
+ * OpenFlow message suitable for 'protocol', and returns that encoded form in
+ * a buffer owned by the caller. */
+struct ofpbuf *
+ofputil_encode_table_status(const struct ofputil_table_status *ts,
+ enum ofputil_protocol protocol)
+{
+ enum ofp_version version;
+ struct ofpbuf *b;
+
+ version = ofputil_protocol_to_ofp_version(protocol);
+ if (version >= OFP14_VERSION) {
+ enum ofpraw raw;
+ struct ofp14_table_status *ots;
+
+ raw = OFPRAW_OFPT14_TABLE_STATUS;
+ b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
+ ots = ofpbuf_put_zeros(b, sizeof *ots);
+ ots->reason = ts->reason;
+ ofputil_put_ofp14_table_desc(&ts->desc, b, version);
+ ofpmsg_update_length(b);
+ return b;
+ } else {
+ return NULL;
+ }
+}
+
+/* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
+ * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
+enum ofperr
+ofputil_decode_table_status(const struct ofp_header *oh,
+ struct ofputil_table_status *ts)
+{
+ const struct ofp14_table_status *ots;
+ struct ofpbuf b;
+ enum ofperr error;
+ enum ofpraw raw;
+
+ ofpbuf_use_const(&b, oh, ntohs(oh->length));
+ raw = ofpraw_pull_assert(&b);
+ ots = ofpbuf_pull(&b, sizeof *ots);
+
+ if (raw == OFPRAW_OFPT14_TABLE_STATUS) {
+ if (ots->reason != OFPTR_VACANCY_DOWN
+ && ots->reason != OFPTR_VACANCY_UP) {
+ return OFPERR_OFPBPC_BAD_VALUE;
+ }
+ ts->reason = ots->reason;
+
+ error = ofputil_decode_table_desc(&b, &ts->desc, oh->version);
+ return error;
+ } else {
+ return OFPERR_OFPBRC_BAD_VERSION;
+ }
+
+ return 0;
+}