+/* Returns a string that describes 'match' in a very literal way, without
+ * interpreting its contents except in a very basic fashion. The returned
+ * string is intended to be fixed-length, so that it is easy to see differences
+ * between two such strings if one is put above another. This is useful for
+ * describing changes made by normalize_match().
+ *
+ * The caller must free the returned string (with free()). */
+char *
+ofp_match_to_literal_string(const struct ofp_match *match)
+{
+ return xasprintf("wildcards=%#10"PRIx32" "
+ " in_port=%5"PRId16" "
+ " dl_src="ETH_ADDR_FMT" "
+ " dl_dst="ETH_ADDR_FMT" "
+ " dl_vlan=%5"PRId16" "
+ " dl_vlan_pcp=%3"PRId8" "
+ " dl_type=%#6"PRIx16" "
+ " nw_tos=%#4"PRIx8" "
+ " nw_proto=%#4"PRIx16" "
+ " nw_src=%#10"PRIx32" "
+ " nw_dst=%#10"PRIx32" "
+ " tp_src=%5"PRId16" "
+ " tp_dst=%5"PRId16,
+ ntohl(match->wildcards),
+ ntohs(match->in_port),
+ ETH_ADDR_ARGS(match->dl_src),
+ ETH_ADDR_ARGS(match->dl_dst),
+ ntohs(match->dl_vlan),
+ match->dl_vlan_pcp,
+ ntohs(match->dl_type),
+ match->nw_tos,
+ match->nw_proto,
+ ntohl(match->nw_src),
+ ntohl(match->nw_dst),
+ ntohs(match->tp_src),
+ ntohs(match->tp_dst));
+}
+
+static uint32_t
+vendor_code_to_id(uint8_t code)
+{
+ switch (code) {
+#define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
+ OFPUTIL_VENDORS
+#undef OFPUTIL_VENDOR
+ default:
+ return UINT32_MAX;
+ }
+}
+
+/* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
+ * information taken from 'error', whose encoding must be as described in the
+ * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
+ * oh->xid as its transaction ID, and it will include up to the first 64 bytes
+ * of 'oh'.
+ *
+ * Returns NULL if 'error' is not an OpenFlow error code. */
+struct ofpbuf *
+make_ofp_error_msg(int error, const struct ofp_header *oh)
+{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+
+ struct ofpbuf *buf;
+ const void *data;
+ size_t len;
+ uint8_t vendor;
+ uint16_t type;
+ uint16_t code;
+ uint32_t xid;
+
+ if (!is_ofp_error(error)) {
+ /* We format 'error' with strerror() here since it seems likely to be
+ * a system errno value. */
+ VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
+ error, strerror(error));
+ return NULL;
+ }
+
+ if (oh) {
+ xid = oh->xid;
+ data = oh;
+ len = ntohs(oh->length);
+ if (len > 64) {
+ len = 64;
+ }
+ } else {
+ xid = 0;
+ data = NULL;
+ len = 0;
+ }
+
+ vendor = get_ofp_err_vendor(error);
+ type = get_ofp_err_type(error);
+ code = get_ofp_err_code(error);
+ if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
+ struct ofp_error_msg *oem;
+
+ oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
+ oem->type = htons(type);
+ oem->code = htons(code);
+ } else {
+ struct ofp_error_msg *oem;
+ struct nx_vendor_error *nve;
+ uint32_t vendor_id;
+
+ vendor_id = vendor_code_to_id(vendor);
+ if (vendor_id == UINT32_MAX) {
+ VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
+ error, vendor);
+ return NULL;
+ }
+
+ oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
+ OFPT_ERROR, xid, &buf);
+ oem->type = htons(NXET_VENDOR);
+ oem->code = htons(NXVC_VENDOR_ERROR);
+
+ nve = ofpbuf_put_uninit(buf, sizeof *nve);
+ nve->vendor = htonl(vendor_id);
+ nve->type = htons(type);
+ nve->code = htons(code);
+ }
+
+ if (len) {
+ ofpbuf_put(buf, data, len);
+ }
+
+ return buf;
+}
+
+/* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
+ * successful, otherwise an OpenFlow error.
+ *
+ * If successful, the first action is stored in '*actionsp' and the number of
+ * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
+ * are stored, respectively.
+ *
+ * This function does not check that the actions are valid (the caller should
+ * do so, with validate_actions()). The caller is also responsible for making
+ * sure that 'b->data' is initially aligned appropriately for "union
+ * ofp_action". */
+int
+ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
+ union ofp_action **actionsp, size_t *n_actionsp)
+{
+ if (actions_len % ACTION_ALIGNMENT != 0) {
+ VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
+ "is not a multiple of %d", actions_len, ACTION_ALIGNMENT);
+ goto error;
+ }
+
+ *actionsp = ofpbuf_try_pull(b, actions_len);
+ if (*actionsp == NULL) {
+ VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
+ "exceeds remaining message length (%zu)",
+ actions_len, b->size);
+ goto error;
+ }
+
+ *n_actionsp = actions_len / ACTION_ALIGNMENT;
+ return 0;
+
+error:
+ *actionsp = NULL;
+ *n_actionsp = 0;
+ return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
+}