2 * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <netinet/icmp6.h>
23 #include "classifier.h"
24 #include "dynamic-string.h"
26 #include "meta-flow.h"
27 #include "ofp-actions.h"
28 #include "ofp-errors.h"
31 #include "openflow/nicira-ext.h"
34 #include "tun-metadata.h"
35 #include "unaligned.h"
37 #include "openvswitch/vlog.h"
39 VLOG_DEFINE_THIS_MODULE(nx_match);
47 * The header is 32 bits long. It looks like this:
50 * +----------------------------------+---------------+--+------------------+
51 * | oxm_class | oxm_field |hm| oxm_length |
52 * +----------------------------------+---------------+--+------------------+
54 * where hm stands for oxm_hasmask. It is followed by oxm_length bytes of
55 * payload. When oxm_hasmask is 0, the payload is the value of the field
56 * identified by the header; when oxm_hasmask is 1, the payload is a value for
57 * the field followed by a mask of equal length.
59 * Internally, we represent a standard OXM header as a 64-bit integer with the
60 * above information in the most-significant bits.
66 * The header is 64 bits long. It looks like the diagram above except that a
67 * 32-bit experimenter ID, which we call oxm_vendor and which identifies a
68 * vendor, is inserted just before the payload. Experimenter OXMs are
69 * identified by an all-1-bits oxm_class (OFPXMC12_EXPERIMENTER). The
70 * oxm_length value *includes* the experimenter ID, so that the real payload is
71 * only oxm_length - 4 bytes long.
73 * Internally, we represent an experimenter OXM header as a 64-bit integer with
74 * the standard header in the upper 32 bits and the experimenter ID in the
75 * lower 32 bits. (It would be more convenient to swap the positions of the
76 * two 32-bit words, but this would be more error-prone because experimenter
77 * OXMs are very rarely used, so accidentally passing one through a 32-bit type
78 * somewhere in the OVS code would be hard to find.)
83 * The high order bit differentiate reserved classes from member classes.
84 * Classes 0x0000 to 0x7FFF are member classes, allocated by ONF.
85 * Classes 0x8000 to 0xFFFE are reserved classes, reserved for standardisation.
87 enum ofp12_oxm_class {
88 OFPXMC12_NXM_0 = 0x0000, /* Backward compatibility with NXM */
89 OFPXMC12_NXM_1 = 0x0001, /* Backward compatibility with NXM */
90 OFPXMC12_OPENFLOW_BASIC = 0x8000, /* Basic class for OpenFlow */
91 OFPXMC15_PACKET_REGS = 0x8001, /* Packet registers (pipeline fields). */
92 OFPXMC12_EXPERIMENTER = 0xffff, /* Experimenter class */
95 /* Functions for extracting raw field values from OXM/NXM headers. */
96 static uint32_t nxm_vendor(uint64_t header) { return header; }
97 static int nxm_class(uint64_t header) { return header >> 48; }
98 static int nxm_field(uint64_t header) { return (header >> 41) & 0x7f; }
99 static bool nxm_hasmask(uint64_t header) { return (header >> 40) & 1; }
100 static int nxm_length(uint64_t header) { return (header >> 32) & 0xff; }
101 static uint64_t nxm_no_len(uint64_t header) { return header & 0xffffff80ffffffffULL; }
104 is_experimenter_oxm(uint64_t header)
106 return nxm_class(header) == OFPXMC12_EXPERIMENTER;
109 /* The OXM header "length" field is somewhat tricky:
111 * - For a standard OXM header, the length is the number of bytes of the
112 * payload, and the payload consists of just the value (and mask, if
115 * - For an experimenter OXM header, the length is the number of bytes in
116 * the payload plus 4 (the length of the experimenter ID). That is, the
117 * experimenter ID is included in oxm_length.
119 * This function returns the length of the experimenter ID field in 'header'.
120 * That is, for an experimenter OXM (when an experimenter ID is present), it
121 * returns 4, and for a standard OXM (when no experimenter ID is present), it
124 nxm_experimenter_len(uint64_t header)
126 return is_experimenter_oxm(header) ? 4 : 0;
129 /* Returns the number of bytes that follow the header for an NXM/OXM entry
130 * with the given 'header'. */
132 nxm_payload_len(uint64_t header)
134 return nxm_length(header) - nxm_experimenter_len(header);
137 /* Returns the number of bytes in the header for an NXM/OXM entry with the
140 nxm_header_len(uint64_t header)
142 return 4 + nxm_experimenter_len(header);
145 #define NXM_HEADER(VENDOR, CLASS, FIELD, HASMASK, LENGTH) \
146 (((uint64_t) (CLASS) << 48) | \
147 ((uint64_t) (FIELD) << 41) | \
148 ((uint64_t) (HASMASK) << 40) | \
149 ((uint64_t) (LENGTH) << 32) | \
152 #define NXM_HEADER_FMT "%#"PRIx32":%d:%d:%d:%d"
153 #define NXM_HEADER_ARGS(HEADER) \
154 nxm_vendor(HEADER), nxm_class(HEADER), nxm_field(HEADER), \
155 nxm_hasmask(HEADER), nxm_length(HEADER)
157 /* Functions for turning the "hasmask" bit on or off. (This also requires
158 * adjusting the length.) */
160 nxm_make_exact_header(uint64_t header)
162 int new_len = nxm_payload_len(header) / 2 + nxm_experimenter_len(header);
163 return NXM_HEADER(nxm_vendor(header), nxm_class(header),
164 nxm_field(header), 0, new_len);
167 nxm_make_wild_header(uint64_t header)
169 int new_len = nxm_payload_len(header) * 2 + nxm_experimenter_len(header);
170 return NXM_HEADER(nxm_vendor(header), nxm_class(header),
171 nxm_field(header), 1, new_len);
176 * This may be used to gain the OpenFlow 1.1-like ability to restrict
177 * certain NXM-based Flow Mod and Flow Stats Request messages to flows
178 * with specific cookies. See the "nx_flow_mod" and "nx_flow_stats_request"
179 * structure definitions for more details. This match is otherwise not
181 #define NXM_NX_COOKIE NXM_HEADER (0, 0x0001, 30, 0, 8)
182 #define NXM_NX_COOKIE_W nxm_make_wild_header(NXM_NX_COOKIE)
186 enum ofp_version version;
187 const char *name; /* e.g. "NXM_OF_IN_PORT". */
192 static const struct nxm_field *nxm_field_by_header(uint64_t header);
193 static const struct nxm_field *nxm_field_by_name(const char *name, size_t len);
194 static const struct nxm_field *nxm_field_by_mf_id(enum mf_field_id,
197 static void nx_put_header__(struct ofpbuf *, uint64_t header, bool masked);
198 static void nx_put_header_len(struct ofpbuf *, enum mf_field_id field,
199 enum ofp_version version, bool masked,
202 /* Rate limit for nx_match parse errors. These always indicate a bug in the
203 * peer and so there's not much point in showing a lot of them. */
204 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
206 static const struct nxm_field *
207 mf_parse_subfield_name(const char *name, int name_len, bool *wild);
209 /* Returns the preferred OXM header to use for field 'id' in OpenFlow version
210 * 'version'. Specify 0 for 'version' if an NXM legacy header should be
211 * preferred over any standardized OXM header. Returns 0 if field 'id' cannot
212 * be expressed in NXM or OXM. */
214 mf_oxm_header(enum mf_field_id id, enum ofp_version version)
216 const struct nxm_field *f = nxm_field_by_mf_id(id, version);
217 return f ? f->header : 0;
220 /* Returns the 32-bit OXM or NXM header to use for field 'id', preferring an
221 * NXM legacy header over any standardized OXM header. Returns 0 if field 'id'
222 * cannot be expressed with a 32-bit NXM or OXM header.
224 * Whenever possible, use nx_pull_header() instead of this function, because
225 * this function cannot support 64-bit experimenter OXM headers. */
227 mf_nxm_header(enum mf_field_id id)
229 uint64_t oxm = mf_oxm_header(id, 0);
230 return is_experimenter_oxm(oxm) ? 0 : oxm >> 32;
233 static const struct mf_field *
234 mf_from_oxm_header(uint64_t header)
236 const struct nxm_field *f = nxm_field_by_header(header);
237 return f ? mf_from_id(f->id) : NULL;
240 /* Returns the "struct mf_field" that corresponds to NXM or OXM header
241 * 'header', or NULL if 'header' doesn't correspond to any known field. */
242 const struct mf_field *
243 mf_from_nxm_header(uint32_t header)
245 return mf_from_oxm_header((uint64_t) header << 32);
248 /* Returns the width of the data for a field with the given 'header', in
251 nxm_field_bytes(uint64_t header)
253 unsigned int length = nxm_payload_len(header);
254 return nxm_hasmask(header) ? length / 2 : length;
257 /* nx_pull_match() and helpers. */
259 /* Given NXM/OXM value 'value' and mask 'mask' associated with 'header', checks
260 * for any 1-bit in the value where there is a 0-bit in the mask. Returns 0 if
261 * none, otherwise an error code. */
263 is_mask_consistent(uint64_t header, const uint8_t *value, const uint8_t *mask)
265 unsigned int width = nxm_field_bytes(header);
268 for (i = 0; i < width; i++) {
269 if (value[i] & ~mask[i]) {
270 if (!VLOG_DROP_WARN(&rl)) {
271 VLOG_WARN_RL(&rl, "Rejecting NXM/OXM entry "NXM_HEADER_FMT " "
272 "with 1-bits in value for bits wildcarded by the "
273 "mask.", NXM_HEADER_ARGS(header));
282 is_cookie_pseudoheader(uint64_t header)
284 return header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W;
288 nx_pull_header__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
289 const struct mf_field **field)
295 *header = ((uint64_t) ntohl(get_unaligned_be32(b->data))) << 32;
296 if (is_experimenter_oxm(*header)) {
300 *header = ntohll(get_unaligned_be64(b->data));
302 if (nxm_length(*header) < nxm_experimenter_len(*header)) {
303 VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has invalid length %d "
305 NXM_HEADER_ARGS(*header), nxm_length(*header),
306 nxm_header_len(*header));
309 ofpbuf_pull(b, nxm_header_len(*header));
312 *field = mf_from_oxm_header(*header);
313 if (!*field && !(allow_cookie && is_cookie_pseudoheader(*header))) {
314 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" is unknown",
315 NXM_HEADER_ARGS(*header));
316 return OFPERR_OFPBMC_BAD_FIELD;
323 VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry",
330 return OFPERR_OFPBMC_BAD_LEN;
334 copy_entry_value(const struct mf_field *field, union mf_value *value,
335 const uint8_t *payload, int width)
341 copy_len = MIN(width, field ? field->n_bytes : sizeof *value);
343 if (field && field->variable_len) {
344 memset(value, 0, field->n_bytes);
345 copy_dst = &value->u8 + field->n_bytes - copy_len;
348 memcpy(copy_dst, payload, copy_len);
352 nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
353 const struct mf_field **field_,
354 union mf_value *value, union mf_value *mask)
356 const struct mf_field *field;
357 enum ofperr header_error;
358 unsigned int payload_len;
359 const uint8_t *payload;
362 header_error = nx_pull_header__(b, allow_cookie, header, &field);
363 if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) {
367 payload_len = nxm_payload_len(*header);
368 payload = ofpbuf_try_pull(b, payload_len);
370 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte "
371 "payload but only %"PRIu32" bytes follow OXM header",
372 NXM_HEADER_ARGS(*header), payload_len, b->size);
373 return OFPERR_OFPBMC_BAD_LEN;
376 width = nxm_field_bytes(*header);
377 if (nxm_hasmask(*header)
378 && !is_mask_consistent(*header, payload, payload + width)) {
379 return OFPERR_OFPBMC_BAD_WILDCARDS;
382 copy_entry_value(field, value, payload, width);
385 if (nxm_hasmask(*header)) {
386 copy_entry_value(field, mask, payload + width, width);
388 memset(mask, 0xff, sizeof *mask);
390 } else if (nxm_hasmask(*header)) {
391 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but "
392 "masked OXMs are not allowed here",
393 NXM_HEADER_ARGS(*header));
394 return OFPERR_OFPBMC_BAD_MASK;
405 /* Attempts to pull an NXM or OXM header, value, and mask (if present) from the
406 * beginning of 'b'. If successful, stores a pointer to the "struct mf_field"
407 * corresponding to the pulled header in '*field', the value into '*value',
408 * and the mask into '*mask', and returns 0. On error, returns an OpenFlow
409 * error; in this case, some bytes might have been pulled off 'b' anyhow, and
410 * the output parameters might have been modified.
412 * If a NULL 'mask' is supplied, masked OXM or NXM entries are treated as
413 * errors (with OFPERR_OFPBMC_BAD_MASK).
416 nx_pull_entry(struct ofpbuf *b, const struct mf_field **field,
417 union mf_value *value, union mf_value *mask)
421 return nx_pull_entry__(b, false, &header, field, value, mask);
424 /* Attempts to pull an NXM or OXM header from the beginning of 'b'. If
425 * successful, stores a pointer to the "struct mf_field" corresponding to the
426 * pulled header in '*field', stores the header's hasmask bit in '*masked'
427 * (true if hasmask=1, false if hasmask=0), and returns 0. On error, returns
428 * an OpenFlow error; in this case, some bytes might have been pulled off 'b'
429 * anyhow, and the output parameters might have been modified.
431 * If NULL 'masked' is supplied, masked OXM or NXM headers are treated as
432 * errors (with OFPERR_OFPBMC_BAD_MASK).
435 nx_pull_header(struct ofpbuf *b, const struct mf_field **field, bool *masked)
440 error = nx_pull_header__(b, false, &header, field);
442 *masked = !error && nxm_hasmask(header);
443 } else if (!error && nxm_hasmask(header)) {
444 error = OFPERR_OFPBMC_BAD_MASK;
450 nx_pull_match_entry(struct ofpbuf *b, bool allow_cookie,
451 const struct mf_field **field,
452 union mf_value *value, union mf_value *mask)
457 error = nx_pull_entry__(b, allow_cookie, &header, field, value, mask);
461 if (field && *field) {
462 if (!mf_is_mask_valid(*field, mask)) {
463 VLOG_DBG_RL(&rl, "bad mask for field %s", (*field)->name);
464 return OFPERR_OFPBMC_BAD_MASK;
466 if (!mf_is_value_valid(*field, value)) {
467 VLOG_DBG_RL(&rl, "bad value for field %s", (*field)->name);
468 return OFPERR_OFPBMC_BAD_VALUE;
475 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
476 struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask)
480 ovs_assert((cookie != NULL) == (cookie_mask != NULL));
482 match_init_catchall(match);
484 *cookie = *cookie_mask = htonll(0);
487 ofpbuf_use_const(&b, p, match_len);
489 const uint8_t *pos = b.data;
490 const struct mf_field *field;
491 union mf_value value;
495 error = nx_pull_match_entry(&b, cookie != NULL, &field, &value, &mask);
497 if (error == OFPERR_OFPBMC_BAD_FIELD && !strict) {
502 error = OFPERR_OFPBMC_BAD_FIELD;
503 } else if (*cookie_mask) {
504 error = OFPERR_OFPBMC_DUP_FIELD;
506 *cookie = value.be64;
507 *cookie_mask = mask.be64;
509 } else if (!mf_are_prereqs_ok(field, &match->flow)) {
510 error = OFPERR_OFPBMC_BAD_PREREQ;
511 } else if (!mf_is_all_wild(field, &match->wc)) {
512 error = OFPERR_OFPBMC_DUP_FIELD;
516 mf_set(field, &value, &mask, match, &err_str);
518 VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
519 "within match (%s)", pos - p, err_str);
521 return OFPERR_OFPBMC_BAD_VALUE;
526 VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
527 "within match (%s)", pos -
528 p, ofperr_to_string(error));
537 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
539 ovs_be64 *cookie, ovs_be64 *cookie_mask)
544 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
546 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
547 "multiple of 8, is longer than space in message (max "
548 "length %"PRIu32")", match_len, b->size);
549 return OFPERR_OFPBMC_BAD_LEN;
553 return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask);
556 /* Parses the nx_match formatted match description in 'b' with length
557 * 'match_len'. Stores the results in 'match'. If 'cookie' and 'cookie_mask'
558 * are valid pointers, then stores the cookie and mask in them if 'b' contains
559 * a "NXM_NX_COOKIE*" match. Otherwise, stores 0 in both.
561 * Fails with an error upon encountering an unknown NXM header.
563 * Returns 0 if successful, otherwise an OpenFlow error code. */
565 nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match,
566 ovs_be64 *cookie, ovs_be64 *cookie_mask)
568 return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask);
571 /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers,
572 * instead of failing with an error. */
574 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
576 ovs_be64 *cookie, ovs_be64 *cookie_mask)
578 return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask);
582 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
584 struct ofp11_match_header *omh = b->data;
588 if (b->size < sizeof *omh) {
589 return OFPERR_OFPBMC_BAD_LEN;
592 match_len = ntohs(omh->length);
593 if (match_len < sizeof *omh) {
594 return OFPERR_OFPBMC_BAD_LEN;
597 if (omh->type != htons(OFPMT_OXM)) {
598 return OFPERR_OFPBMC_BAD_TYPE;
601 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
603 VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
604 "multiple of 8, is longer than space in message (max "
605 "length %"PRIu32")", match_len, b->size);
606 return OFPERR_OFPBMC_BAD_LEN;
609 return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
610 strict, match, NULL, NULL);
613 /* Parses the oxm formatted match description preceded by a struct
614 * ofp11_match_header in 'b'. Stores the result in 'match'.
616 * Fails with an error when encountering unknown OXM headers.
618 * Returns 0 if successful, otherwise an OpenFlow error code. */
620 oxm_pull_match(struct ofpbuf *b, struct match *match)
622 return oxm_pull_match__(b, true, match);
625 /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown
626 * OXM headers instead of failing with an error when they are encountered. */
628 oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
630 return oxm_pull_match__(b, false, match);
633 /* Verify an array of OXM TLVs treating value of each TLV as a mask,
634 * disallowing masks in each TLV and ignoring pre-requisites. */
636 oxm_pull_field_array(const void *fields_data, size_t fields_len,
637 struct field_array *fa)
641 ofpbuf_use_const(&b, fields_data, fields_len);
643 const uint8_t *pos = b.data;
644 const struct mf_field *field;
645 union mf_value value;
649 error = nx_pull_entry__(&b, false, &header, &field, &value, NULL);
651 VLOG_DBG_RL(&rl, "error pulling field array field");
654 VLOG_DBG_RL(&rl, "unknown field array field");
655 error = OFPERR_OFPBMC_BAD_FIELD;
656 } else if (bitmap_is_set(fa->used.bm, field->id)) {
657 VLOG_DBG_RL(&rl, "duplicate field array field '%s'", field->name);
658 error = OFPERR_OFPBMC_DUP_FIELD;
659 } else if (!mf_is_mask_valid(field, &value)) {
660 VLOG_DBG_RL(&rl, "bad mask in field array field '%s'", field->name);
661 return OFPERR_OFPBMC_BAD_MASK;
663 field_array_set(field->id, &value, fa);
667 const uint8_t *start = fields_data;
669 VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
670 "within field array (%s)", pos - start,
671 ofperr_to_string(error));
679 /* nx_put_match() and helpers.
681 * 'put' functions whose names end in 'w' add a wildcarded field.
682 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
683 * Other 'put' functions add exact-match fields.
686 nxm_put__(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
687 const void *value, const void *mask, size_t n_bytes)
689 nx_put_header_len(b, field, version, !!mask, n_bytes);
690 ofpbuf_put(b, value, n_bytes);
692 ofpbuf_put(b, mask, n_bytes);
698 nxm_put(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
699 const void *value, const void *mask, size_t n_bytes)
701 if (!is_all_zeros(mask, n_bytes)) {
702 bool masked = !is_all_ones(mask, n_bytes);
703 nxm_put__(b, field, version, value, masked ? mask : NULL, n_bytes);
708 nxm_put_8m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
709 uint8_t value, uint8_t mask)
711 nxm_put(b, field, version, &value, &mask, sizeof value);
715 nxm_put_8(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
718 nxm_put__(b, field, version, &value, NULL, sizeof value);
722 nxm_put_16m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
723 ovs_be16 value, ovs_be16 mask)
725 nxm_put(b, field, version, &value, &mask, sizeof value);
729 nxm_put_16(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
732 nxm_put__(b, field, version, &value, NULL, sizeof value);
736 nxm_put_32m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
737 ovs_be32 value, ovs_be32 mask)
739 nxm_put(b, field, version, &value, &mask, sizeof value);
743 nxm_put_32(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
746 nxm_put__(b, field, version, &value, NULL, sizeof value);
750 nxm_put_64m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
751 ovs_be64 value, ovs_be64 mask)
753 nxm_put(b, field, version, &value, &mask, sizeof value);
757 nxm_put_eth_masked(struct ofpbuf *b,
758 enum mf_field_id field, enum ofp_version version,
759 const struct eth_addr value, const struct eth_addr mask)
761 nxm_put(b, field, version, value.ea, mask.ea, ETH_ADDR_LEN);
765 nxm_put_ipv6(struct ofpbuf *b,
766 enum mf_field_id field, enum ofp_version version,
767 const struct in6_addr *value, const struct in6_addr *mask)
769 nxm_put(b, field, version, value->s6_addr, mask->s6_addr,
770 sizeof value->s6_addr);
774 nxm_put_frag(struct ofpbuf *b, const struct match *match,
775 enum ofp_version version)
777 uint8_t nw_frag = match->flow.nw_frag & FLOW_NW_FRAG_MASK;
778 uint8_t nw_frag_mask = match->wc.masks.nw_frag & FLOW_NW_FRAG_MASK;
780 nxm_put_8m(b, MFF_IP_FRAG, version, nw_frag,
781 nw_frag_mask == FLOW_NW_FRAG_MASK ? UINT8_MAX : nw_frag_mask);
784 /* Appends to 'b' a set of OXM or NXM matches for the IPv4 or IPv6 fields in
787 nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm)
789 const struct flow *flow = &match->flow;
791 if (flow->dl_type == htons(ETH_TYPE_IP)) {
792 nxm_put_32m(b, MFF_IPV4_SRC, oxm,
793 flow->nw_src, match->wc.masks.nw_src);
794 nxm_put_32m(b, MFF_IPV4_DST, oxm,
795 flow->nw_dst, match->wc.masks.nw_dst);
797 nxm_put_ipv6(b, MFF_IPV6_SRC, oxm,
798 &flow->ipv6_src, &match->wc.masks.ipv6_src);
799 nxm_put_ipv6(b, MFF_IPV6_DST, oxm,
800 &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
803 nxm_put_frag(b, match, oxm);
805 if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
807 nxm_put_8(b, MFF_IP_DSCP_SHIFTED, oxm,
810 nxm_put_8(b, MFF_IP_DSCP, oxm,
811 flow->nw_tos & IP_DSCP_MASK);
815 if (match->wc.masks.nw_tos & IP_ECN_MASK) {
816 nxm_put_8(b, MFF_IP_ECN, oxm,
817 flow->nw_tos & IP_ECN_MASK);
820 if (!oxm && match->wc.masks.nw_ttl) {
821 nxm_put_8(b, MFF_IP_TTL, oxm, flow->nw_ttl);
824 nxm_put_32m(b, MFF_IPV6_LABEL, oxm,
825 flow->ipv6_label, match->wc.masks.ipv6_label);
827 if (match->wc.masks.nw_proto) {
828 nxm_put_8(b, MFF_IP_PROTO, oxm, flow->nw_proto);
830 if (flow->nw_proto == IPPROTO_TCP) {
831 nxm_put_16m(b, MFF_TCP_SRC, oxm,
832 flow->tp_src, match->wc.masks.tp_src);
833 nxm_put_16m(b, MFF_TCP_DST, oxm,
834 flow->tp_dst, match->wc.masks.tp_dst);
835 nxm_put_16m(b, MFF_TCP_FLAGS, oxm,
836 flow->tcp_flags, match->wc.masks.tcp_flags);
837 } else if (flow->nw_proto == IPPROTO_UDP) {
838 nxm_put_16m(b, MFF_UDP_SRC, oxm,
839 flow->tp_src, match->wc.masks.tp_src);
840 nxm_put_16m(b, MFF_UDP_DST, oxm,
841 flow->tp_dst, match->wc.masks.tp_dst);
842 } else if (flow->nw_proto == IPPROTO_SCTP) {
843 nxm_put_16m(b, MFF_SCTP_SRC, oxm, flow->tp_src,
844 match->wc.masks.tp_src);
845 nxm_put_16m(b, MFF_SCTP_DST, oxm, flow->tp_dst,
846 match->wc.masks.tp_dst);
847 } else if (is_icmpv4(flow)) {
848 if (match->wc.masks.tp_src) {
849 nxm_put_8(b, MFF_ICMPV4_TYPE, oxm,
850 ntohs(flow->tp_src));
852 if (match->wc.masks.tp_dst) {
853 nxm_put_8(b, MFF_ICMPV4_CODE, oxm,
854 ntohs(flow->tp_dst));
856 } else if (is_icmpv6(flow)) {
857 if (match->wc.masks.tp_src) {
858 nxm_put_8(b, MFF_ICMPV6_TYPE, oxm,
859 ntohs(flow->tp_src));
861 if (match->wc.masks.tp_dst) {
862 nxm_put_8(b, MFF_ICMPV6_CODE, oxm,
863 ntohs(flow->tp_dst));
865 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
866 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
867 nxm_put_ipv6(b, MFF_ND_TARGET, oxm,
868 &flow->nd_target, &match->wc.masks.nd_target);
869 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
870 nxm_put_eth_masked(b, MFF_ND_SLL, oxm,
871 flow->arp_sha, match->wc.masks.arp_sha);
873 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
874 nxm_put_eth_masked(b, MFF_ND_TLL, oxm,
875 flow->arp_tha, match->wc.masks.arp_tha);
882 /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and
883 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
884 * Otherwise, 'cookie_mask' should be zero.
886 * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify
887 * 'oxm' as the OpenFlow version number for the OXM format to use.
889 * This function can cause 'b''s data to be reallocated.
891 * Returns the number of bytes appended to 'b', excluding padding.
893 * If 'match' is a catch-all rule that matches every packet, then this function
894 * appends nothing to 'b' and returns 0. */
896 nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
897 ovs_be64 cookie, ovs_be64 cookie_mask)
899 const struct flow *flow = &match->flow;
900 const size_t start_len = b->size;
904 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 34);
907 if (match->wc.masks.dp_hash) {
908 nxm_put_32m(b, MFF_DP_HASH, oxm,
909 htonl(flow->dp_hash), htonl(match->wc.masks.dp_hash));
912 if (match->wc.masks.recirc_id) {
913 nxm_put_32(b, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id));
916 if (match->wc.masks.conj_id) {
917 nxm_put_32(b, MFF_CONJ_ID, oxm, htonl(flow->conj_id));
920 if (match->wc.masks.in_port.ofp_port) {
921 ofp_port_t in_port = flow->in_port.ofp_port;
923 nxm_put_32(b, MFF_IN_PORT_OXM, oxm,
924 ofputil_port_to_ofp11(in_port));
926 nxm_put_16(b, MFF_IN_PORT, oxm,
927 htons(ofp_to_u16(in_port)));
930 if (match->wc.masks.actset_output) {
931 nxm_put_32(b, MFF_ACTSET_OUTPUT, oxm,
932 ofputil_port_to_ofp11(flow->actset_output));
936 nxm_put_eth_masked(b, MFF_ETH_SRC, oxm,
937 flow->dl_src, match->wc.masks.dl_src);
938 nxm_put_eth_masked(b, MFF_ETH_DST, oxm,
939 flow->dl_dst, match->wc.masks.dl_dst);
940 nxm_put_16m(b, MFF_ETH_TYPE, oxm,
941 ofputil_dl_type_to_openflow(flow->dl_type),
942 match->wc.masks.dl_type);
946 ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
947 ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
948 ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
950 if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
951 nxm_put_16(b, MFF_VLAN_VID, oxm, vid);
953 nxm_put_16m(b, MFF_VLAN_VID, oxm, vid, mask);
956 if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
957 nxm_put_8(b, MFF_VLAN_PCP, oxm,
958 vlan_tci_to_pcp(flow->vlan_tci));
962 nxm_put_16m(b, MFF_VLAN_TCI, oxm, flow->vlan_tci,
963 match->wc.masks.vlan_tci);
967 if (eth_type_mpls(flow->dl_type)) {
968 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
969 nxm_put_8(b, MFF_MPLS_TC, oxm,
970 mpls_lse_to_tc(flow->mpls_lse[0]));
973 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
974 nxm_put_8(b, MFF_MPLS_BOS, oxm,
975 mpls_lse_to_bos(flow->mpls_lse[0]));
978 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
979 nxm_put_32(b, MFF_MPLS_LABEL, oxm,
980 htonl(mpls_lse_to_label(flow->mpls_lse[0])));
985 if (is_ip_any(flow)) {
986 nxm_put_ip(b, match, oxm);
987 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
988 flow->dl_type == htons(ETH_TYPE_RARP)) {
990 if (match->wc.masks.nw_proto) {
991 nxm_put_16(b, MFF_ARP_OP, oxm,
992 htons(flow->nw_proto));
994 nxm_put_32m(b, MFF_ARP_SPA, oxm,
995 flow->nw_src, match->wc.masks.nw_src);
996 nxm_put_32m(b, MFF_ARP_TPA, oxm,
997 flow->nw_dst, match->wc.masks.nw_dst);
998 nxm_put_eth_masked(b, MFF_ARP_SHA, oxm,
999 flow->arp_sha, match->wc.masks.arp_sha);
1000 nxm_put_eth_masked(b, MFF_ARP_THA, oxm,
1001 flow->arp_tha, match->wc.masks.arp_tha);
1005 nxm_put_64m(b, MFF_TUN_ID, oxm,
1006 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
1008 /* Other tunnel metadata. */
1009 nxm_put_16m(b, MFF_TUN_FLAGS, oxm,
1010 htons(flow->tunnel.flags), htons(match->wc.masks.tunnel.flags));
1011 nxm_put_32m(b, MFF_TUN_SRC, oxm,
1012 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
1013 nxm_put_32m(b, MFF_TUN_DST, oxm,
1014 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
1015 nxm_put_16m(b, MFF_TUN_GBP_ID, oxm,
1016 flow->tunnel.gbp_id, match->wc.masks.tunnel.gbp_id);
1017 nxm_put_8m(b, MFF_TUN_GBP_FLAGS, oxm,
1018 flow->tunnel.gbp_flags, match->wc.masks.tunnel.gbp_flags);
1019 tun_metadata_to_nx_match(b, oxm, match);
1022 if (oxm < OFP15_VERSION) {
1023 for (i = 0; i < FLOW_N_REGS; i++) {
1024 nxm_put_32m(b, MFF_REG0 + i, oxm,
1025 htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
1028 for (i = 0; i < FLOW_N_XREGS; i++) {
1029 nxm_put_64m(b, MFF_XREG0 + i, oxm,
1030 htonll(flow_get_xreg(flow, i)),
1031 htonll(flow_get_xreg(&match->wc.masks, i)));
1036 nxm_put_32m(b, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark),
1037 htonl(match->wc.masks.pkt_mark));
1039 /* Connection tracking. */
1040 nxm_put_32m(b, MFF_CT_STATE, oxm, htonl(flow->ct_state),
1041 htonl(match->wc.masks.ct_state));
1042 nxm_put_16m(b, MFF_CT_ZONE, oxm, htons(flow->ct_zone),
1043 htons(match->wc.masks.ct_zone));
1044 nxm_put_32m(b, MFF_CT_MARK, oxm, htonl(flow->ct_mark),
1045 htonl(match->wc.masks.ct_mark));
1047 /* OpenFlow 1.1+ Metadata. */
1048 nxm_put_64m(b, MFF_METADATA, oxm,
1049 flow->metadata, match->wc.masks.metadata);
1053 bool masked = cookie_mask != OVS_BE64_MAX;
1055 cookie &= cookie_mask;
1056 nx_put_header__(b, NXM_NX_COOKIE, masked);
1057 ofpbuf_put(b, &cookie, sizeof cookie);
1059 ofpbuf_put(b, &cookie_mask, sizeof cookie_mask);
1063 match_len = b->size - start_len;
1067 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
1068 * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow
1069 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
1070 * Otherwise, 'cookie_mask' should be zero.
1072 * This function can cause 'b''s data to be reallocated.
1074 * Returns the number of bytes appended to 'b', excluding padding. The return
1075 * value can be zero if it appended nothing at all to 'b' (which happens if
1076 * 'cr' is a catch-all rule that matches every packet). */
1078 nx_put_match(struct ofpbuf *b, const struct match *match,
1079 ovs_be64 cookie, ovs_be64 cookie_mask)
1081 int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask);
1083 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1087 /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that
1088 * expresses 'cr', plus enough zero bytes to pad the data appended out to a
1091 * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow
1092 * version in use as 'version'.
1094 * This function can cause 'b''s data to be reallocated.
1096 * Returns the number of bytes appended to 'b', excluding the padding. Never
1099 oxm_put_match(struct ofpbuf *b, const struct match *match,
1100 enum ofp_version version)
1103 struct ofp11_match_header *omh;
1104 size_t start_len = b->size;
1105 ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
1107 ofpbuf_put_uninit(b, sizeof *omh);
1108 match_len = (nx_put_raw(b, version, match, cookie, cookie_mask)
1110 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1112 omh = ofpbuf_at(b, start_len, sizeof *omh);
1113 omh->type = htons(OFPMT_OXM);
1114 omh->length = htons(match_len);
1119 /* Appends to 'b' the nx_match format that expresses the tlv corresponding
1120 * to 'id'. If mask is not all-ones then it is also formated as the value
1123 nx_format_mask_tlv(struct ds *ds, enum mf_field_id id,
1124 const union mf_value *mask)
1126 const struct mf_field *mf = mf_from_id(id);
1128 ds_put_format(ds, "%s", mf->name);
1130 if (!is_all_ones(mask, mf->n_bytes)) {
1131 ds_put_char(ds, '=');
1132 mf_format(mf, mask, NULL, ds);
1135 ds_put_char(ds, ',');
1138 /* Appends a string representation of 'fa_' to 'ds'.
1139 * The TLVS value of 'fa_' is treated as a mask and
1140 * only the name of fields is formated if it is all ones. */
1142 oxm_format_field_array(struct ds *ds, const struct field_array *fa)
1144 size_t start_len = ds->length;
1147 for (i = 0; i < MFF_N_IDS; i++) {
1148 if (bitmap_is_set(fa->used.bm, i)) {
1149 nx_format_mask_tlv(ds, i, &fa->value[i]);
1153 if (ds->length > start_len) {
1158 /* Appends to 'b' a series of OXM TLVs corresponding to the series
1159 * of enum mf_field_id and value tuples in 'fa_'.
1161 * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow
1162 * version in use as 'version'.
1164 * This function can cause 'b''s data to be reallocated.
1166 * Returns the number of bytes appended to 'b'. May return zero. */
1168 oxm_put_field_array(struct ofpbuf *b, const struct field_array *fa,
1169 enum ofp_version version)
1171 size_t start_len = b->size;
1174 /* Field arrays are only used with the group selection method
1175 * property and group properties are only available in OpenFlow 1.5+.
1176 * So the following assertion should never fail.
1178 * If support for older OpenFlow versions is desired then some care
1179 * will need to be taken of different TLVs that handle the same
1180 * flow fields. In particular:
1181 * - VLAN_TCI, VLAN_VID and MFF_VLAN_PCP
1182 * - IP_DSCP_MASK and DSCP_SHIFTED
1185 ovs_assert(version >= OFP15_VERSION);
1187 for (i = 0; i < MFF_N_IDS; i++) {
1188 if (bitmap_is_set(fa->used.bm, i)) {
1189 int len = mf_field_len(mf_from_id(i), &fa->value[i], NULL, NULL);
1190 nxm_put__(b, i, version,
1191 &fa->value[i].u8 + mf_from_id(i)->n_bytes - len, NULL,
1196 return b->size - start_len;
1200 nx_put_header__(struct ofpbuf *b, uint64_t header, bool masked)
1202 uint64_t masked_header = masked ? nxm_make_wild_header(header) : header;
1203 ovs_be64 network_header = htonll(masked_header);
1205 ofpbuf_put(b, &network_header, nxm_header_len(header));
1209 nx_put_header(struct ofpbuf *b, enum mf_field_id field,
1210 enum ofp_version version, bool masked)
1212 nx_put_header__(b, mf_oxm_header(field, version), masked);
1216 nx_put_header_len(struct ofpbuf *b, enum mf_field_id field,
1217 enum ofp_version version, bool masked, size_t n_bytes)
1219 uint64_t header = mf_oxm_header(field, version);
1221 header = NXM_HEADER(nxm_vendor(header), nxm_class(header),
1222 nxm_field(header), false,
1223 nxm_experimenter_len(header) + n_bytes);
1225 nx_put_header__(b, header, masked);
1229 nx_put_entry(struct ofpbuf *b,
1230 enum mf_field_id field, enum ofp_version version,
1231 const union mf_value *value, const union mf_value *mask)
1233 const struct mf_field *mf = mf_from_id(field);
1237 len = mf_field_len(mf, value, mask, &masked);
1238 offset = mf->n_bytes - len;
1240 nx_put_header_len(b, field, version, masked, len);
1241 ofpbuf_put(b, &value->u8 + offset, len);
1243 ofpbuf_put(b, &mask->u8 + offset, len);
1247 /* nx_match_to_string() and helpers. */
1249 static void format_nxm_field_name(struct ds *, uint64_t header);
1252 nx_match_to_string(const uint8_t *p, unsigned int match_len)
1258 return xstrdup("<any>");
1261 ofpbuf_use_const(&b, p, match_len);
1264 union mf_value value;
1265 union mf_value mask;
1270 error = nx_pull_entry__(&b, true, &header, NULL, &value, &mask);
1274 value_len = MIN(sizeof value, nxm_field_bytes(header));
1277 ds_put_cstr(&s, ", ");
1280 format_nxm_field_name(&s, header);
1281 ds_put_char(&s, '(');
1283 for (int i = 0; i < value_len; i++) {
1284 ds_put_format(&s, "%02x", ((const uint8_t *) &value)[i]);
1286 if (nxm_hasmask(header)) {
1287 ds_put_char(&s, '/');
1288 for (int i = 0; i < value_len; i++) {
1289 ds_put_format(&s, "%02x", ((const uint8_t *) &mask)[i]);
1292 ds_put_char(&s, ')');
1297 ds_put_cstr(&s, ", ");
1300 ds_put_format(&s, "<%u invalid bytes>", b.size);
1303 return ds_steal_cstr(&s);
1307 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
1309 const struct ofp11_match_header *omh = p->data;
1310 uint16_t match_len_;
1315 if (match_len < sizeof *omh) {
1316 ds_put_format(&s, "<match too short: %u>", match_len);
1320 if (omh->type != htons(OFPMT_OXM)) {
1321 ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
1325 match_len_ = ntohs(omh->length);
1326 if (match_len_ < sizeof *omh) {
1327 ds_put_format(&s, "<match length field too short: %u>", match_len_);
1331 if (match_len_ != match_len) {
1332 ds_put_format(&s, "<match length field incorrect: %u != %u>",
1333 match_len_, match_len);
1337 return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
1338 match_len - sizeof *omh);
1341 return ds_steal_cstr(&s);
1345 nx_format_field_name(enum mf_field_id id, enum ofp_version version,
1348 format_nxm_field_name(s, mf_oxm_header(id, version));
1352 format_nxm_field_name(struct ds *s, uint64_t header)
1354 const struct nxm_field *f = nxm_field_by_header(header);
1356 ds_put_cstr(s, f->name);
1357 if (nxm_hasmask(header)) {
1358 ds_put_cstr(s, "_W");
1360 } else if (header == NXM_NX_COOKIE) {
1361 ds_put_cstr(s, "NXM_NX_COOKIE");
1362 } else if (header == NXM_NX_COOKIE_W) {
1363 ds_put_cstr(s, "NXM_NX_COOKIE_W");
1365 ds_put_format(s, "%d:%d", nxm_class(header), nxm_field(header));
1370 streq_len(const char *a, size_t a_len, const char *b)
1372 return strlen(b) == a_len && !memcmp(a, b, a_len);
1376 parse_nxm_field_name(const char *name, int name_len)
1378 const struct nxm_field *f;
1381 f = mf_parse_subfield_name(name, name_len, &wild);
1385 } else if (mf_from_id(f->id)->maskable != MFM_NONE) {
1386 return nxm_make_wild_header(f->header);
1390 if (streq_len(name, name_len, "NXM_NX_COOKIE")) {
1391 return NXM_NX_COOKIE;
1392 } else if (streq_len(name, name_len, "NXM_NX_COOKIE_W")) {
1393 return NXM_NX_COOKIE_W;
1396 /* Check whether it's a field header value as hex.
1397 * (This isn't ordinarily useful except for testing error behavior.) */
1398 if (name_len == 8) {
1402 header = hexits_value(name, name_len, &ok) << 32;
1406 } else if (name_len == 16) {
1410 header = hexits_value(name, name_len, &ok);
1411 if (ok && is_experimenter_oxm(header)) {
1419 /* nx_match_from_string(). */
1422 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
1424 const char *full_s = s;
1425 const size_t start_len = b->size;
1427 if (!strcmp(s, "<any>")) {
1428 /* Ensure that 'b->data' isn't actually null. */
1429 ofpbuf_prealloc_tailroom(b, 1);
1433 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
1437 ovs_be64 *header_ptr;
1442 name_len = strcspn(s, "(");
1443 if (s[name_len] != '(') {
1444 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
1447 header = parse_nxm_field_name(name, name_len);
1449 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
1454 header_ptr = ofpbuf_put_uninit(b, nxm_header_len(header));
1455 s = ofpbuf_put_hex(b, s, &n);
1456 if (n != nxm_field_bytes(header)) {
1457 const struct mf_field *field = mf_from_oxm_header(header);
1459 if (field && field->variable_len) {
1460 if (n <= field->n_bytes) {
1461 int len = (nxm_hasmask(header) ? n * 2 : n) +
1462 nxm_experimenter_len(header);
1464 header = NXM_HEADER(nxm_vendor(header), nxm_class(header),
1466 nxm_hasmask(header) ? 1 : 0, len);
1468 ovs_fatal(0, "expected to read at most %d bytes but got "
1469 "%"PRIuSIZE, field->n_bytes, n);
1472 ovs_fatal(0, "expected to read %d bytes but got %"PRIuSIZE,
1473 nxm_field_bytes(header), n);
1476 nw_header = htonll(header);
1477 memcpy(header_ptr, &nw_header, nxm_header_len(header));
1479 if (nxm_hasmask(header)) {
1480 s += strspn(s, " ");
1482 ovs_fatal(0, "%s: missing / in masked field %.*s",
1483 full_s, name_len, name);
1485 s = ofpbuf_put_hex(b, s + 1, &n);
1486 if (n != nxm_field_bytes(header)) {
1487 ovs_fatal(0, "%.2s: hex digits expected", s);
1491 s += strspn(s, " ");
1493 ovs_fatal(0, "%s: missing ) following field %.*s",
1494 full_s, name_len, name);
1499 return b->size - start_len;
1503 nx_match_from_string(const char *s, struct ofpbuf *b)
1505 int match_len = nx_match_from_string_raw(s, b);
1506 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1511 oxm_match_from_string(const char *s, struct ofpbuf *b)
1514 struct ofp11_match_header *omh;
1515 size_t start_len = b->size;
1517 ofpbuf_put_uninit(b, sizeof *omh);
1518 match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
1519 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1521 omh = ofpbuf_at(b, start_len, sizeof *omh);
1522 omh->type = htons(OFPMT_OXM);
1523 omh->length = htons(match_len);
1528 /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into
1531 * Returns NULL if successful, otherwise a malloc()'d string describing the
1532 * error. The caller is responsible for freeing the returned string. */
1533 char * OVS_WARN_UNUSED_RESULT
1534 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1536 const char *full_s = s;
1539 error = mf_parse_subfield__(&move->src, &s);
1543 if (strncmp(s, "->", 2)) {
1544 return xasprintf("%s: missing `->' following source", full_s);
1547 error = mf_parse_subfield(&move->dst, s);
1552 if (move->src.n_bits != move->dst.n_bits) {
1553 return xasprintf("%s: source field is %d bits wide but destination is "
1554 "%d bits wide", full_s,
1555 move->src.n_bits, move->dst.n_bits);
1560 /* nxm_format_reg_move(). */
1563 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1565 ds_put_format(s, "move:");
1566 mf_format_subfield(&move->src, s);
1567 ds_put_cstr(s, "->");
1568 mf_format_subfield(&move->dst, s);
1573 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1577 error = mf_check_src(&move->src, flow);
1582 return mf_check_dst(&move->dst, flow);
1585 /* nxm_execute_reg_move(). */
1588 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1589 struct flow *flow, struct flow_wildcards *wc)
1591 union mf_value src_value;
1592 union mf_value dst_value;
1594 mf_mask_field_and_prereqs(move->dst.field, wc);
1595 mf_mask_field_and_prereqs(move->src.field, wc);
1597 /* A flow may wildcard nw_frag. Do nothing if setting a transport
1598 * header field on a packet that does not have them. */
1599 if (mf_are_prereqs_ok(move->dst.field, flow)
1600 && mf_are_prereqs_ok(move->src.field, flow)) {
1602 mf_get_value(move->dst.field, flow, &dst_value);
1603 mf_get_value(move->src.field, flow, &src_value);
1604 bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1605 &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1607 mf_set_flow_value(move->dst.field, &dst_value, flow);
1612 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1613 struct flow *flow, struct flow_wildcards *wc)
1615 union mf_subvalue src_subvalue;
1616 union mf_subvalue mask_value;
1617 ovs_be64 src_data_be = htonll(src_data);
1619 memset(&mask_value, 0xff, sizeof mask_value);
1620 mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1622 bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1623 &src_subvalue, sizeof src_subvalue, 0,
1624 sizeof src_data_be * 8);
1625 mf_write_subfield_flow(dst, &src_subvalue, flow);
1628 /* nxm_parse_stack_action, works for both push() and pop(). */
1630 /* Parses 's' as a "push" or "pop" action, in the form described in
1631 * ovs-ofctl(8), into '*stack_action'.
1633 * Returns NULL if successful, otherwise a malloc()'d string describing the
1634 * error. The caller is responsible for freeing the returned string. */
1635 char * OVS_WARN_UNUSED_RESULT
1636 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1640 error = mf_parse_subfield__(&stack_action->subfield, &s);
1646 return xasprintf("%s: trailing garbage following push or pop", s);
1653 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1655 ds_put_cstr(s, "push:");
1656 mf_format_subfield(&push->subfield, s);
1660 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1662 ds_put_cstr(s, "pop:");
1663 mf_format_subfield(&pop->subfield, s);
1667 nxm_stack_push_check(const struct ofpact_stack *push,
1668 const struct flow *flow)
1670 return mf_check_src(&push->subfield, flow);
1674 nxm_stack_pop_check(const struct ofpact_stack *pop,
1675 const struct flow *flow)
1677 return mf_check_dst(&pop->subfield, flow);
1680 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1682 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1684 ofpbuf_put(stack, v, sizeof *v);
1687 static union mf_subvalue *
1688 nx_stack_pop(struct ofpbuf *stack)
1690 union mf_subvalue *v = NULL;
1694 stack->size -= sizeof *v;
1695 v = (union mf_subvalue *) ofpbuf_tail(stack);
1702 nxm_execute_stack_push(const struct ofpact_stack *push,
1703 const struct flow *flow, struct flow_wildcards *wc,
1704 struct ofpbuf *stack)
1706 union mf_subvalue mask_value;
1707 union mf_subvalue dst_value;
1709 memset(&mask_value, 0xff, sizeof mask_value);
1710 mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1712 mf_read_subfield(&push->subfield, flow, &dst_value);
1713 nx_stack_push(stack, &dst_value);
1717 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1718 struct flow *flow, struct flow_wildcards *wc,
1719 struct ofpbuf *stack)
1721 union mf_subvalue *src_value;
1723 src_value = nx_stack_pop(stack);
1725 /* Only pop if stack is not empty. Otherwise, give warning. */
1727 union mf_subvalue mask_value;
1729 memset(&mask_value, 0xff, sizeof mask_value);
1730 mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks);
1731 mf_write_subfield_flow(&pop->subfield, src_value, flow);
1733 if (!VLOG_DROP_WARN(&rl)) {
1734 char *flow_str = flow_to_string(flow);
1735 VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow\n"
1742 /* Formats 'sf' into 's' in a format normally acceptable to
1743 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
1744 * sf->field has no NXM name.) */
1746 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
1749 ds_put_cstr(s, "<unknown>");
1751 const struct nxm_field *f = nxm_field_by_mf_id(sf->field->id, 0);
1752 ds_put_cstr(s, f ? f->name : sf->field->name);
1755 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
1756 ds_put_cstr(s, "[]");
1757 } else if (sf->n_bits == 1) {
1758 ds_put_format(s, "[%d]", sf->ofs);
1760 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
1764 static const struct nxm_field *
1765 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
1767 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
1772 return nxm_field_by_name(name, name_len);
1775 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
1776 * returns NULL and advances '*sp' to the first byte following the parsed
1777 * string. On failure, returns a malloc()'d error message, does not modify
1778 * '*sp', and does not properly initialize 'sf'.
1780 * The syntax parsed from '*sp' takes the form "header[start..end]" where
1781 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
1782 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
1783 * may both be omitted (the [] are still required) to indicate an entire
1785 char * OVS_WARN_UNUSED_RESULT
1786 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
1788 const struct mf_field *field;
1789 const struct nxm_field *f;
1798 name_len = strcspn(s, "[");
1799 if (s[name_len] != '[') {
1800 return xasprintf("%s: missing [ looking for field name", *sp);
1803 f = mf_parse_subfield_name(name, name_len, &wild);
1805 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
1807 field = mf_from_id(f->id);
1810 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
1811 /* Nothing to do. */
1812 } else if (ovs_scan(s, "[%d]", &start)) {
1814 } else if (!strncmp(s, "[]", 2)) {
1816 end = field->n_bits - 1;
1818 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
1819 "[<start>..<end>]", *sp);
1821 s = strchr(s, ']') + 1;
1824 return xasprintf("%s: starting bit %d is after ending bit %d",
1826 } else if (start >= field->n_bits) {
1827 return xasprintf("%s: starting bit %d is not valid because field is "
1828 "only %d bits wide", *sp, start, field->n_bits);
1829 } else if (end >= field->n_bits){
1830 return xasprintf("%s: ending bit %d is not valid because field is "
1831 "only %d bits wide", *sp, end, field->n_bits);
1836 sf->n_bits = end - start + 1;
1842 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
1843 * successful, otherwise a malloc()'d string describing the error. The caller
1844 * is responsible for freeing the returned string.
1846 * The syntax parsed from 's' takes the form "header[start..end]" where
1847 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
1848 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
1849 * may both be omitted (the [] are still required) to indicate an entire
1851 char * OVS_WARN_UNUSED_RESULT
1852 mf_parse_subfield(struct mf_subfield *sf, const char *s)
1854 char *error = mf_parse_subfield__(sf, &s);
1855 if (!error && s[0]) {
1856 error = xstrdup("unexpected input following field syntax");
1861 /* Returns an bitmap in which each bit corresponds to the like-numbered field
1862 * in the OFPXMC12_OPENFLOW_BASIC OXM class, in which the bit values are taken
1863 * from the 'fields' bitmap. Only fields defined in OpenFlow 'version' are
1866 * This is useful for encoding OpenFlow 1.2 table stats messages. */
1868 oxm_bitmap_from_mf_bitmap(const struct mf_bitmap *fields,
1869 enum ofp_version version)
1871 uint64_t oxm_bitmap = 0;
1874 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fields->bm) {
1875 uint64_t oxm = mf_oxm_header(i, version);
1876 uint32_t class = nxm_class(oxm);
1877 int field = nxm_field(oxm);
1879 if (class == OFPXMC12_OPENFLOW_BASIC && field < 64) {
1880 oxm_bitmap |= UINT64_C(1) << field;
1883 return htonll(oxm_bitmap);
1886 /* Opposite conversion from oxm_bitmap_from_mf_bitmap().
1888 * This is useful for decoding OpenFlow 1.2 table stats messages. */
1890 oxm_bitmap_to_mf_bitmap(ovs_be64 oxm_bitmap, enum ofp_version version)
1892 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
1894 for (enum mf_field_id id = 0; id < MFF_N_IDS; id++) {
1895 uint64_t oxm = mf_oxm_header(id, version);
1896 if (oxm && version >= nxm_field_by_header(oxm)->version) {
1897 uint32_t class = nxm_class(oxm);
1898 int field = nxm_field(oxm);
1900 if (class == OFPXMC12_OPENFLOW_BASIC
1902 && oxm_bitmap & htonll(UINT64_C(1) << field)) {
1903 bitmap_set1(fields.bm, id);
1910 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1911 * modified with a "set_field" action. */
1913 oxm_writable_fields(void)
1915 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1918 for (i = 0; i < MFF_N_IDS; i++) {
1919 if (mf_oxm_header(i, 0) && mf_from_id(i)->writable) {
1920 bitmap_set1(b.bm, i);
1926 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1927 * matched in a flow table. */
1929 oxm_matchable_fields(void)
1931 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1934 for (i = 0; i < MFF_N_IDS; i++) {
1935 if (mf_oxm_header(i, 0)) {
1936 bitmap_set1(b.bm, i);
1942 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1943 * matched in a flow table with an arbitrary bitmask. */
1945 oxm_maskable_fields(void)
1947 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1950 for (i = 0; i < MFF_N_IDS; i++) {
1951 if (mf_oxm_header(i, 0) && mf_from_id(i)->maskable == MFM_FULLY) {
1952 bitmap_set1(b.bm, i);
1958 struct nxm_field_index {
1959 struct hmap_node header_node; /* In nxm_header_map. */
1960 struct hmap_node name_node; /* In nxm_name_map. */
1961 struct ovs_list mf_node; /* In mf_mf_map[nf.id]. */
1962 const struct nxm_field nf;
1965 #include "nx-match.inc"
1967 static struct hmap nxm_header_map;
1968 static struct hmap nxm_name_map;
1969 static struct ovs_list nxm_mf_map[MFF_N_IDS];
1974 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1975 if (ovsthread_once_start(&once)) {
1976 hmap_init(&nxm_header_map);
1977 hmap_init(&nxm_name_map);
1978 for (int i = 0; i < MFF_N_IDS; i++) {
1979 list_init(&nxm_mf_map[i]);
1981 for (struct nxm_field_index *nfi = all_nxm_fields;
1982 nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) {
1983 hmap_insert(&nxm_header_map, &nfi->header_node,
1984 hash_uint64(nxm_no_len(nfi->nf.header)));
1985 hmap_insert(&nxm_name_map, &nfi->name_node,
1986 hash_string(nfi->nf.name, 0));
1987 list_push_back(&nxm_mf_map[nfi->nf.id], &nfi->mf_node);
1989 ovsthread_once_done(&once);
1993 static const struct nxm_field *
1994 nxm_field_by_header(uint64_t header)
1996 const struct nxm_field_index *nfi;
1997 uint64_t header_no_len;
2000 if (nxm_hasmask(header)) {
2001 header = nxm_make_exact_header(header);
2004 header_no_len = nxm_no_len(header);
2006 HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_uint64(header_no_len),
2008 if (header_no_len == nxm_no_len(nfi->nf.header)) {
2009 if (nxm_length(header) == nxm_length(nfi->nf.header) ||
2010 mf_from_id(nfi->nf.id)->variable_len) {
2020 static const struct nxm_field *
2021 nxm_field_by_name(const char *name, size_t len)
2023 const struct nxm_field_index *nfi;
2026 HMAP_FOR_EACH_WITH_HASH (nfi, name_node, hash_bytes(name, len, 0),
2028 if (strlen(nfi->nf.name) == len && !memcmp(nfi->nf.name, name, len)) {
2035 static const struct nxm_field *
2036 nxm_field_by_mf_id(enum mf_field_id id, enum ofp_version version)
2038 const struct nxm_field_index *nfi;
2039 const struct nxm_field *f;
2044 LIST_FOR_EACH (nfi, mf_node, &nxm_mf_map[id]) {
2045 if (!f || version >= nfi->nf.version) {