2 * Copyright (c) 2010, 2011, 2012, 2013, 2014 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 "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(nx_match);
42 * The high order bit differentiate reserved classes from member classes.
43 * Classes 0x0000 to 0x7FFF are member classes, allocated by ONF.
44 * Classes 0x8000 to 0xFFFE are reserved classes, reserved for standardisation.
46 enum ofp12_oxm_class {
47 OFPXMC12_NXM_0 = 0x0000, /* Backward compatibility with NXM */
48 OFPXMC12_NXM_1 = 0x0001, /* Backward compatibility with NXM */
49 OFPXMC12_OPENFLOW_BASIC = 0x8000, /* Basic class for OpenFlow */
50 OFPXMC15_PACKET_REGS = 0x8001, /* Packet registers (pipeline fields). */
51 OFPXMC12_EXPERIMENTER = 0xffff, /* Experimenter class */
54 /* Functions for extracting fields from OXM/NXM headers. */
55 static int nxm_vendor(uint32_t header) { return header >> 16; }
56 static int nxm_field(uint32_t header) { return (header >> 9) & 0x7f; }
57 static bool nxm_hasmask(uint32_t header) { return (header & 0x100) != 0; }
58 static int nxm_length(uint32_t header) { return header & 0xff; }
60 /* Returns true if 'header' is a legacy NXM header, false if it is an OXM
63 is_nxm_header(uint32_t header)
65 return nxm_vendor(header) <= 1;
68 #define NXM_HEADER(VENDOR, FIELD, HASMASK, LENGTH) \
69 (((VENDOR) << 16) | ((FIELD) << 9) | ((HASMASK) << 8) | (LENGTH))
71 #define NXM_HEADER_FMT "%d:%d:%d:%d"
72 #define NXM_HEADER_ARGS(HEADER) \
73 nxm_vendor(HEADER), nxm_field(HEADER), \
74 nxm_hasmask(HEADER), nxm_length(HEADER)
76 /* Functions for turning the "hasmask" bit on or off. (This also requires
77 * adjusting the length.) */
79 nxm_make_exact_header(uint32_t header)
81 return NXM_HEADER(nxm_vendor(header), nxm_field(header), 0,
82 nxm_length(header) / 2);
85 nxm_make_wild_header(uint32_t header)
87 return NXM_HEADER(nxm_vendor(header), nxm_field(header), 1,
88 nxm_length(header) * 2);
93 * This may be used to gain the OpenFlow 1.1-like ability to restrict
94 * certain NXM-based Flow Mod and Flow Stats Request messages to flows
95 * with specific cookies. See the "nx_flow_mod" and "nx_flow_stats_request"
96 * structure definitions for more details. This match is otherwise not
98 #define NXM_NX_COOKIE NXM_HEADER (0x0001, 30, 0, 8)
99 #define NXM_NX_COOKIE_W nxm_make_wild_header(NXM_NX_COOKIE)
103 enum ofp_version version;
104 const char *name; /* e.g. "NXM_OF_IN_PORT". */
109 static const struct nxm_field *nxm_field_by_header(uint32_t header);
110 static const struct nxm_field *nxm_field_by_name(const char *name, size_t len);
111 static const struct nxm_field *nxm_field_by_mf_id(enum mf_field_id);
112 static const struct nxm_field *oxm_field_by_mf_id(enum mf_field_id);
114 static void nx_put_header__(struct ofpbuf *, uint32_t header, bool masked);
116 /* Rate limit for nx_match parse errors. These always indicate a bug in the
117 * peer and so there's not much point in showing a lot of them. */
118 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
120 static const struct nxm_field *
121 mf_parse_subfield_name(const char *name, int name_len, bool *wild);
123 static const struct nxm_field *
124 nxm_field_from_mf_field(enum mf_field_id id, enum ofp_version version)
126 const struct nxm_field *oxm = oxm_field_by_mf_id(id);
127 const struct nxm_field *nxm = nxm_field_by_mf_id(id);
128 return oxm && (version >= oxm->version || !nxm) ? oxm : nxm;
131 /* Returns the preferred OXM header to use for field 'id' in OpenFlow version
132 * 'version'. Specify 0 for 'version' if an NXM legacy header should be
133 * preferred over any standardized OXM header. Returns 0 if field 'id' cannot
134 * be expressed in NXM or OXM. */
136 mf_oxm_header(enum mf_field_id id, enum ofp_version version)
138 const struct nxm_field *f = nxm_field_from_mf_field(id, version);
139 return f ? f->header : 0;
142 /* Returns the "struct mf_field" that corresponds to NXM or OXM header
143 * 'header', or NULL if 'header' doesn't correspond to any known field. */
144 const struct mf_field *
145 mf_from_nxm_header(uint32_t header)
147 const struct nxm_field *f = nxm_field_by_header(header);
148 return f ? mf_from_id(f->id) : NULL;
151 /* Returns the width of the data for a field with the given 'header', in
154 nxm_field_bytes(uint32_t header)
156 unsigned int length = nxm_length(header);
157 return nxm_hasmask(header) ? length / 2 : length;
160 /* Returns the earliest version of OpenFlow that standardized an OXM header for
161 * field 'id', or UINT8_MAX if no version of OpenFlow does. */
162 static enum ofp_version
163 mf_oxm_version(enum mf_field_id id)
165 const struct nxm_field *oxm = oxm_field_by_mf_id(id);
166 return oxm ? oxm->version : UINT8_MAX;
169 /* nx_pull_match() and helpers. */
171 /* Given NXM/OXM value 'value' and mask 'mask' associated with 'header', checks
172 * for any 1-bit in the value where there is a 0-bit in the mask. Returns 0 if
173 * none, otherwise an error code. */
175 is_mask_consistent(uint32_t header, const uint8_t *value, const uint8_t *mask)
177 unsigned int width = nxm_field_bytes(header);
180 for (i = 0; i < width; i++) {
181 if (value[i] & ~mask[i]) {
182 if (!VLOG_DROP_WARN(&rl)) {
183 VLOG_WARN_RL(&rl, "Rejecting NXM/OXM entry "NXM_HEADER_FMT " "
184 "with 1-bits in value for bits wildcarded by the "
185 "mask.", NXM_HEADER_ARGS(header));
194 is_cookie_pseudoheader(uint32_t header)
196 return header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W;
200 nx_pull_header__(struct ofpbuf *b, bool allow_cookie, uint32_t *header,
201 const struct mf_field **field)
203 if (ofpbuf_size(b) < 4) {
204 VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry",
208 *header = ntohl(get_unaligned_be32(ofpbuf_pull(b, 4)));
209 if (nxm_length(*header) == 0) {
210 VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has zero length",
211 NXM_HEADER_ARGS(*header));
215 *field = mf_from_nxm_header(*header);
216 if (!*field && !(allow_cookie && is_cookie_pseudoheader(*header))) {
217 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" is unknown",
218 NXM_HEADER_ARGS(*header));
219 return OFPERR_OFPBMC_BAD_FIELD;
228 return OFPERR_OFPBMC_BAD_LEN;
232 nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint32_t *header,
233 const struct mf_field **field,
234 union mf_value *value, union mf_value *mask)
236 enum ofperr header_error;
237 unsigned int payload_len;
238 const uint8_t *payload;
241 header_error = nx_pull_header__(b, allow_cookie, header, field);
242 if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) {
246 payload_len = nxm_length(*header);
247 payload = ofpbuf_try_pull(b, payload_len);
249 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte "
250 "payload but only %"PRIu32" bytes follow OXM header",
251 NXM_HEADER_ARGS(*header), payload_len, ofpbuf_size(b));
252 return OFPERR_OFPBMC_BAD_LEN;
255 width = nxm_field_bytes(*header);
256 if (nxm_hasmask(*header)
257 && !is_mask_consistent(*header, payload, payload + width)) {
258 return OFPERR_OFPBMC_BAD_WILDCARDS;
261 memcpy(value, payload, MIN(width, sizeof *value));
263 if (nxm_hasmask(*header)) {
264 memcpy(mask, payload + width, MIN(width, sizeof *mask));
266 memset(mask, 0xff, MIN(width, sizeof *mask));
268 } else if (nxm_hasmask(*header)) {
269 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but "
270 "masked OXMs are not allowed here",
271 NXM_HEADER_ARGS(*header));
272 return OFPERR_OFPBMC_BAD_MASK;
278 /* Attempts to pull an NXM or OXM header, value, and mask (if present) from the
279 * beginning of 'b'. If successful, stores a pointer to the "struct mf_field"
280 * corresponding to the pulled header in '*field', the value into '*value',
281 * and the mask into '*mask', and returns 0. On error, returns an OpenFlow
282 * error; in this case, some bytes might have been pulled off 'b' anyhow, and
283 * the output parameters might have been modified.
285 * If a NULL 'mask' is supplied, masked OXM or NXM entries are treated as
286 * errors (with OFPERR_OFPBMC_BAD_MASK).
289 nx_pull_entry(struct ofpbuf *b, const struct mf_field **field,
290 union mf_value *value, union mf_value *mask)
294 return nx_pull_entry__(b, false, &header, field, value, mask);
297 /* Attempts to pull an NXM or OXM header from the beginning of 'b'. If
298 * successful, stores a pointer to the "struct mf_field" corresponding to the
299 * pulled header in '*field', stores the header's hasmask bit in '*masked'
300 * (true if hasmask=1, false if hasmask=0), and returns 0. On error, returns
301 * an OpenFlow error; in this case, some bytes might have been pulled off 'b'
302 * anyhow, and the output parameters might have been modified.
304 * If NULL 'masked' is supplied, masked OXM or NXM headers are treated as
305 * errors (with OFPERR_OFPBMC_BAD_MASK).
308 nx_pull_header(struct ofpbuf *b, const struct mf_field **field, bool *masked)
313 error = nx_pull_header__(b, false, &header, field);
315 *masked = !error && nxm_hasmask(header);
316 } else if (!error && nxm_hasmask(header)) {
317 error = OFPERR_OFPBMC_BAD_MASK;
323 nx_pull_match_entry(struct ofpbuf *b, bool allow_cookie,
324 const struct mf_field **field,
325 union mf_value *value, union mf_value *mask)
330 error = nx_pull_entry__(b, allow_cookie, &header, field, value, mask);
334 if (field && *field) {
335 if (!mf_is_mask_valid(*field, mask)) {
336 VLOG_DBG_RL(&rl, "bad mask for field %s", (*field)->name);
337 return OFPERR_OFPBMC_BAD_MASK;
339 if (!mf_is_value_valid(*field, value)) {
340 VLOG_DBG_RL(&rl, "bad value for field %s", (*field)->name);
341 return OFPERR_OFPBMC_BAD_VALUE;
348 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
349 struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask)
353 ovs_assert((cookie != NULL) == (cookie_mask != NULL));
355 match_init_catchall(match);
357 *cookie = *cookie_mask = htonll(0);
360 ofpbuf_use_const(&b, p, match_len);
361 while (ofpbuf_size(&b)) {
362 const uint8_t *pos = ofpbuf_data(&b);
363 const struct mf_field *field;
364 union mf_value value;
368 error = nx_pull_match_entry(&b, cookie != NULL, &field, &value, &mask);
370 if (error == OFPERR_OFPBMC_BAD_FIELD && !strict) {
375 error = OFPERR_OFPBMC_BAD_FIELD;
376 } else if (*cookie_mask) {
377 error = OFPERR_OFPBMC_DUP_FIELD;
379 *cookie = value.be64;
380 *cookie_mask = mask.be64;
382 } else if (!mf_are_prereqs_ok(field, &match->flow)) {
383 error = OFPERR_OFPBMC_BAD_PREREQ;
384 } else if (!mf_is_all_wild(field, &match->wc)) {
385 error = OFPERR_OFPBMC_DUP_FIELD;
387 mf_set(field, &value, &mask, match);
391 VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
392 "within match (%s)", pos -
393 p, ofperr_to_string(error));
402 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
404 ovs_be64 *cookie, ovs_be64 *cookie_mask)
409 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
411 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
412 "multiple of 8, is longer than space in message (max "
413 "length %"PRIu32")", match_len, ofpbuf_size(b));
414 return OFPERR_OFPBMC_BAD_LEN;
418 return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask);
421 /* Parses the nx_match formatted match description in 'b' with length
422 * 'match_len'. Stores the results in 'match'. If 'cookie' and 'cookie_mask'
423 * are valid pointers, then stores the cookie and mask in them if 'b' contains
424 * a "NXM_NX_COOKIE*" match. Otherwise, stores 0 in both.
426 * Fails with an error upon encountering an unknown NXM header.
428 * Returns 0 if successful, otherwise an OpenFlow error code. */
430 nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match,
431 ovs_be64 *cookie, ovs_be64 *cookie_mask)
433 return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask);
436 /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers,
437 * instead of failing with an error. */
439 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
441 ovs_be64 *cookie, ovs_be64 *cookie_mask)
443 return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask);
447 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
449 struct ofp11_match_header *omh = ofpbuf_data(b);
453 if (ofpbuf_size(b) < sizeof *omh) {
454 return OFPERR_OFPBMC_BAD_LEN;
457 match_len = ntohs(omh->length);
458 if (match_len < sizeof *omh) {
459 return OFPERR_OFPBMC_BAD_LEN;
462 if (omh->type != htons(OFPMT_OXM)) {
463 return OFPERR_OFPBMC_BAD_TYPE;
466 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
468 VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
469 "multiple of 8, is longer than space in message (max "
470 "length %"PRIu32")", match_len, ofpbuf_size(b));
471 return OFPERR_OFPBMC_BAD_LEN;
474 return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
475 strict, match, NULL, NULL);
478 /* Parses the oxm formatted match description preceded by a struct
479 * ofp11_match_header in 'b'. Stores the result in 'match'.
481 * Fails with an error when encountering unknown OXM headers.
483 * Returns 0 if successful, otherwise an OpenFlow error code. */
485 oxm_pull_match(struct ofpbuf *b, struct match *match)
487 return oxm_pull_match__(b, true, match);
490 /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown
491 * OXM headers instead of failing with an error when they are encountered. */
493 oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
495 return oxm_pull_match__(b, false, match);
498 /* nx_put_match() and helpers.
500 * 'put' functions whose names end in 'w' add a wildcarded field.
501 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
502 * Other 'put' functions add exact-match fields.
506 nxm_put_unmasked(struct ofpbuf *b, enum mf_field_id field,
507 enum ofp_version version, const void *value, size_t n_bytes)
509 nx_put_header(b, field, version, false);
510 ofpbuf_put(b, value, n_bytes);
514 nxm_put(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
515 const void *value, const void *mask, size_t n_bytes)
517 if (!is_all_zeros(mask, n_bytes)) {
518 bool masked = !is_all_ones(mask, n_bytes);
519 nx_put_header(b, field, version, masked);
520 ofpbuf_put(b, value, n_bytes);
522 ofpbuf_put(b, mask, n_bytes);
528 nxm_put_8m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
529 uint8_t value, uint8_t mask)
531 nxm_put(b, field, version, &value, &mask, sizeof value);
535 nxm_put_8(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
538 nxm_put_unmasked(b, field, version, &value, sizeof value);
542 nxm_put_16m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
543 ovs_be16 value, ovs_be16 mask)
545 nxm_put(b, field, version, &value, &mask, sizeof value);
549 nxm_put_16(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
552 nxm_put_unmasked(b, field, version, &value, sizeof value);
556 nxm_put_32m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
557 ovs_be32 value, ovs_be32 mask)
559 nxm_put(b, field, version, &value, &mask, sizeof value);
563 nxm_put_32(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
566 nxm_put_unmasked(b, field, version, &value, sizeof value);
570 nxm_put_64m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
571 ovs_be64 value, ovs_be64 mask)
573 nxm_put(b, field, version, &value, &mask, sizeof value);
577 nxm_put_eth_masked(struct ofpbuf *b,
578 enum mf_field_id field, enum ofp_version version,
579 const uint8_t value[ETH_ADDR_LEN],
580 const uint8_t mask[ETH_ADDR_LEN])
582 nxm_put(b, field, version, value, mask, ETH_ADDR_LEN);
586 nxm_put_ipv6(struct ofpbuf *b,
587 enum mf_field_id field, enum ofp_version version,
588 const struct in6_addr *value, const struct in6_addr *mask)
590 nxm_put(b, field, version, value->s6_addr, mask->s6_addr,
591 sizeof value->s6_addr);
595 nxm_put_frag(struct ofpbuf *b, const struct match *match,
596 enum ofp_version version)
598 uint8_t nw_frag = match->flow.nw_frag & FLOW_NW_FRAG_MASK;
599 uint8_t nw_frag_mask = match->wc.masks.nw_frag & FLOW_NW_FRAG_MASK;
601 nxm_put_8m(b, MFF_IP_FRAG, version, nw_frag,
602 nw_frag_mask == FLOW_NW_FRAG_MASK ? UINT8_MAX : nw_frag_mask);
605 /* Appends to 'b' a set of OXM or NXM matches for the IPv4 or IPv6 fields in
608 nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm)
610 const struct flow *flow = &match->flow;
612 if (flow->dl_type == htons(ETH_TYPE_IP)) {
613 nxm_put_32m(b, MFF_IPV4_SRC, oxm,
614 flow->nw_src, match->wc.masks.nw_src);
615 nxm_put_32m(b, MFF_IPV4_DST, oxm,
616 flow->nw_dst, match->wc.masks.nw_dst);
618 nxm_put_ipv6(b, MFF_IPV6_SRC, oxm,
619 &flow->ipv6_src, &match->wc.masks.ipv6_src);
620 nxm_put_ipv6(b, MFF_IPV6_DST, oxm,
621 &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
624 nxm_put_frag(b, match, oxm);
626 if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
628 nxm_put_8(b, MFF_IP_DSCP_SHIFTED, oxm,
631 nxm_put_8(b, MFF_IP_DSCP, oxm,
632 flow->nw_tos & IP_DSCP_MASK);
636 if (match->wc.masks.nw_tos & IP_ECN_MASK) {
637 nxm_put_8(b, MFF_IP_ECN, oxm,
638 flow->nw_tos & IP_ECN_MASK);
641 if (!oxm && match->wc.masks.nw_ttl) {
642 nxm_put_8(b, MFF_IP_TTL, oxm, flow->nw_ttl);
645 nxm_put_32m(b, MFF_IPV6_LABEL, oxm,
646 flow->ipv6_label, match->wc.masks.ipv6_label);
648 if (match->wc.masks.nw_proto) {
649 nxm_put_8(b, MFF_IP_PROTO, oxm, flow->nw_proto);
651 if (flow->nw_proto == IPPROTO_TCP) {
652 nxm_put_16m(b, MFF_TCP_SRC, oxm,
653 flow->tp_src, match->wc.masks.tp_src);
654 nxm_put_16m(b, MFF_TCP_DST, oxm,
655 flow->tp_dst, match->wc.masks.tp_dst);
656 nxm_put_16m(b, MFF_TCP_FLAGS, oxm,
657 flow->tcp_flags, match->wc.masks.tcp_flags);
658 } else if (flow->nw_proto == IPPROTO_UDP) {
659 nxm_put_16m(b, MFF_UDP_SRC, oxm,
660 flow->tp_src, match->wc.masks.tp_src);
661 nxm_put_16m(b, MFF_UDP_DST, oxm,
662 flow->tp_dst, match->wc.masks.tp_dst);
663 } else if (flow->nw_proto == IPPROTO_SCTP) {
664 nxm_put_16m(b, MFF_SCTP_SRC, oxm, flow->tp_src,
665 match->wc.masks.tp_src);
666 nxm_put_16m(b, MFF_SCTP_DST, oxm, flow->tp_dst,
667 match->wc.masks.tp_dst);
668 } else if (is_icmpv4(flow)) {
669 if (match->wc.masks.tp_src) {
670 nxm_put_8(b, MFF_ICMPV4_TYPE, oxm,
671 ntohs(flow->tp_src));
673 if (match->wc.masks.tp_dst) {
674 nxm_put_8(b, MFF_ICMPV4_CODE, oxm,
675 ntohs(flow->tp_dst));
677 } else if (is_icmpv6(flow)) {
678 if (match->wc.masks.tp_src) {
679 nxm_put_8(b, MFF_ICMPV6_TYPE, oxm,
680 ntohs(flow->tp_src));
682 if (match->wc.masks.tp_dst) {
683 nxm_put_8(b, MFF_ICMPV6_CODE, oxm,
684 ntohs(flow->tp_dst));
686 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
687 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
688 nxm_put_ipv6(b, MFF_ND_TARGET, oxm,
689 &flow->nd_target, &match->wc.masks.nd_target);
690 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
691 nxm_put_eth_masked(b, MFF_ND_SLL, oxm,
692 flow->arp_sha, match->wc.masks.arp_sha);
694 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
695 nxm_put_eth_masked(b, MFF_ND_TLL, oxm,
696 flow->arp_tha, match->wc.masks.arp_tha);
703 /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and
704 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
705 * Otherwise, 'cookie_mask' should be zero.
707 * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify
708 * 'oxm' as the OpenFlow version number for the OXM format to use.
710 * This function can cause 'b''s data to be reallocated.
712 * Returns the number of bytes appended to 'b', excluding padding.
714 * If 'match' is a catch-all rule that matches every packet, then this function
715 * appends nothing to 'b' and returns 0. */
717 nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
718 ovs_be64 cookie, ovs_be64 cookie_mask)
720 const struct flow *flow = &match->flow;
721 const size_t start_len = ofpbuf_size(b);
725 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
728 if (match->wc.masks.dp_hash) {
729 nxm_put_32m(b, MFF_DP_HASH, oxm,
730 htonl(flow->dp_hash), htonl(match->wc.masks.dp_hash));
733 if (match->wc.masks.recirc_id) {
734 nxm_put_32(b, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id));
737 if (match->wc.masks.in_port.ofp_port) {
738 ofp_port_t in_port = flow->in_port.ofp_port;
740 nxm_put_32(b, MFF_IN_PORT_OXM, oxm,
741 ofputil_port_to_ofp11(in_port));
743 nxm_put_16(b, MFF_IN_PORT, oxm,
744 htons(ofp_to_u16(in_port)));
749 nxm_put_eth_masked(b, MFF_ETH_SRC, oxm,
750 flow->dl_src, match->wc.masks.dl_src);
751 nxm_put_eth_masked(b, MFF_ETH_DST, oxm,
752 flow->dl_dst, match->wc.masks.dl_dst);
753 nxm_put_16m(b, MFF_ETH_TYPE, oxm,
754 ofputil_dl_type_to_openflow(flow->dl_type),
755 match->wc.masks.dl_type);
759 ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
760 ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
761 ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
763 if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
764 nxm_put_16(b, MFF_VLAN_VID, oxm, vid);
766 nxm_put_16m(b, MFF_VLAN_VID, oxm, vid, mask);
769 if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
770 nxm_put_8(b, MFF_VLAN_PCP, oxm,
771 vlan_tci_to_pcp(flow->vlan_tci));
775 nxm_put_16m(b, MFF_VLAN_TCI, oxm, flow->vlan_tci,
776 match->wc.masks.vlan_tci);
780 if (eth_type_mpls(flow->dl_type)) {
781 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
782 nxm_put_8(b, MFF_MPLS_TC, oxm,
783 mpls_lse_to_tc(flow->mpls_lse[0]));
786 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
787 nxm_put_8(b, MFF_MPLS_BOS, oxm,
788 mpls_lse_to_bos(flow->mpls_lse[0]));
791 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
792 nxm_put_32(b, MFF_MPLS_LABEL, oxm,
793 htonl(mpls_lse_to_label(flow->mpls_lse[0])));
798 if (is_ip_any(flow)) {
799 nxm_put_ip(b, match, oxm);
800 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
801 flow->dl_type == htons(ETH_TYPE_RARP)) {
803 if (match->wc.masks.nw_proto) {
804 nxm_put_16(b, MFF_ARP_OP, oxm,
805 htons(flow->nw_proto));
807 nxm_put_32m(b, MFF_ARP_SPA, oxm,
808 flow->nw_src, match->wc.masks.nw_src);
809 nxm_put_32m(b, MFF_ARP_TPA, oxm,
810 flow->nw_dst, match->wc.masks.nw_dst);
811 nxm_put_eth_masked(b, MFF_ARP_SHA, oxm,
812 flow->arp_sha, match->wc.masks.arp_sha);
813 nxm_put_eth_masked(b, MFF_ARP_THA, oxm,
814 flow->arp_tha, match->wc.masks.arp_tha);
818 nxm_put_64m(b, MFF_TUN_ID, oxm,
819 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
821 /* Other tunnel metadata. */
822 nxm_put_32m(b, MFF_TUN_SRC, oxm,
823 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
824 nxm_put_32m(b, MFF_TUN_DST, oxm,
825 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
828 if (oxm < OFP15_VERSION) {
829 for (i = 0; i < FLOW_N_REGS; i++) {
830 nxm_put_32m(b, MFF_REG0 + i, oxm,
831 htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
834 for (i = 0; i < FLOW_N_XREGS; i++) {
835 nxm_put_64m(b, MFF_XREG0 + i, oxm,
836 htonll(flow_get_xreg(flow, i)),
837 htonll(flow_get_xreg(&match->wc.masks, i)));
842 nxm_put_32m(b, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark),
843 htonl(match->wc.masks.pkt_mark));
845 /* OpenFlow 1.1+ Metadata. */
846 nxm_put_64m(b, MFF_METADATA, oxm,
847 flow->metadata, match->wc.masks.metadata);
851 bool masked = cookie_mask != OVS_BE64_MAX;
853 cookie &= cookie_mask;
854 nx_put_header__(b, NXM_NX_COOKIE, masked);
855 ofpbuf_put(b, &cookie, sizeof cookie);
857 ofpbuf_put(b, &cookie_mask, sizeof cookie_mask);
861 match_len = ofpbuf_size(b) - start_len;
865 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
866 * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow
867 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
868 * Otherwise, 'cookie_mask' should be zero.
870 * This function can cause 'b''s data to be reallocated.
872 * Returns the number of bytes appended to 'b', excluding padding. The return
873 * value can be zero if it appended nothing at all to 'b' (which happens if
874 * 'cr' is a catch-all rule that matches every packet). */
876 nx_put_match(struct ofpbuf *b, const struct match *match,
877 ovs_be64 cookie, ovs_be64 cookie_mask)
879 int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask);
881 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
885 /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that
886 * expresses 'cr', plus enough zero bytes to pad the data appended out to a
889 * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow
890 * version in use as 'version'.
892 * This function can cause 'b''s data to be reallocated.
894 * Returns the number of bytes appended to 'b', excluding the padding. Never
897 oxm_put_match(struct ofpbuf *b, const struct match *match,
898 enum ofp_version version)
901 struct ofp11_match_header *omh;
902 size_t start_len = ofpbuf_size(b);
903 ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
905 ofpbuf_put_uninit(b, sizeof *omh);
906 match_len = (nx_put_raw(b, version, match, cookie, cookie_mask)
908 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
910 omh = ofpbuf_at(b, start_len, sizeof *omh);
911 omh->type = htons(OFPMT_OXM);
912 omh->length = htons(match_len);
918 nx_put_header__(struct ofpbuf *b, uint32_t header, bool masked)
920 uint32_t masked_header = masked ? nxm_make_wild_header(header) : header;
921 ovs_be32 network_header = htonl(masked_header);
923 ofpbuf_put(b, &network_header, sizeof network_header);
927 nx_put_header(struct ofpbuf *b, enum mf_field_id field,
928 enum ofp_version version, bool masked)
930 nx_put_header__(b, mf_oxm_header(field, version), masked);
934 nx_put_entry(struct ofpbuf *b,
935 enum mf_field_id field, enum ofp_version version,
936 const union mf_value *value, const union mf_value *mask)
938 int n_bytes = mf_from_id(field)->n_bytes;
939 bool masked = mask && !is_all_ones(mask, n_bytes);
941 nx_put_header(b, field, version, masked);
942 ofpbuf_put(b, value, n_bytes);
944 ofpbuf_put(b, mask, n_bytes);
948 /* nx_match_to_string() and helpers. */
950 static void format_nxm_field_name(struct ds *, uint32_t header);
953 nx_match_to_string(const uint8_t *p, unsigned int match_len)
959 return xstrdup("<any>");
962 ofpbuf_use_const(&b, p, match_len);
964 while (ofpbuf_size(&b)) {
965 union mf_value value;
971 error = nx_pull_entry__(&b, true, &header, NULL, &value, &mask);
975 value_len = MIN(sizeof value, nxm_field_bytes(header));
978 ds_put_cstr(&s, ", ");
981 format_nxm_field_name(&s, header);
982 ds_put_char(&s, '(');
984 for (int i = 0; i < value_len; i++) {
985 ds_put_format(&s, "%02x", ((const uint8_t *) &value)[i]);
987 if (nxm_hasmask(header)) {
988 ds_put_char(&s, '/');
989 for (int i = 0; i < value_len; i++) {
990 ds_put_format(&s, "%02x", ((const uint8_t *) &mask)[i]);
993 ds_put_char(&s, ')');
996 if (ofpbuf_size(&b)) {
998 ds_put_cstr(&s, ", ");
1001 ds_put_format(&s, "<%u invalid bytes>", ofpbuf_size(&b));
1004 return ds_steal_cstr(&s);
1008 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
1010 const struct ofp11_match_header *omh = ofpbuf_data(p);
1011 uint16_t match_len_;
1016 if (match_len < sizeof *omh) {
1017 ds_put_format(&s, "<match too short: %u>", match_len);
1021 if (omh->type != htons(OFPMT_OXM)) {
1022 ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
1026 match_len_ = ntohs(omh->length);
1027 if (match_len_ < sizeof *omh) {
1028 ds_put_format(&s, "<match length field too short: %u>", match_len_);
1032 if (match_len_ != match_len) {
1033 ds_put_format(&s, "<match length field incorrect: %u != %u>",
1034 match_len_, match_len);
1038 return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
1039 match_len - sizeof *omh);
1042 return ds_steal_cstr(&s);
1046 nx_format_field_name(enum mf_field_id id, enum ofp_version version,
1049 format_nxm_field_name(s, mf_oxm_header(id, version));
1053 format_nxm_field_name(struct ds *s, uint32_t header)
1055 const struct nxm_field *f = nxm_field_by_header(header);
1057 ds_put_cstr(s, f->name);
1058 if (nxm_hasmask(header)) {
1059 ds_put_cstr(s, "_W");
1061 } else if (header == NXM_NX_COOKIE) {
1062 ds_put_cstr(s, "NXM_NX_COOKIE");
1063 } else if (header == NXM_NX_COOKIE_W) {
1064 ds_put_cstr(s, "NXM_NX_COOKIE_W");
1066 ds_put_format(s, "%d:%d", nxm_vendor(header), nxm_field(header));
1071 streq_len(const char *a, size_t a_len, const char *b)
1073 return strlen(b) == a_len && !memcmp(a, b, a_len);
1077 parse_nxm_field_name(const char *name, int name_len)
1079 const struct nxm_field *f;
1082 f = mf_parse_subfield_name(name, name_len, &wild);
1086 } else if (mf_from_id(f->id)->maskable != MFM_NONE) {
1087 return nxm_make_wild_header(f->header);
1091 if (streq_len(name, name_len, "NXM_NX_COOKIE")) {
1092 return NXM_NX_COOKIE;
1093 } else if (streq_len(name, name_len, "NXM_NX_COOKIE_W")) {
1094 return NXM_NX_COOKIE_W;
1097 /* Check whether it's a 32-bit field header value as hex.
1098 * (This isn't ordinarily useful except for testing error behavior.) */
1099 if (name_len == 8) {
1100 uint32_t header = hexits_value(name, name_len, NULL);
1101 if (header != UINT_MAX) {
1109 /* nx_match_from_string(). */
1112 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
1114 const char *full_s = s;
1115 const size_t start_len = ofpbuf_size(b);
1117 if (!strcmp(s, "<any>")) {
1118 /* Ensure that 'ofpbuf_data(b)' isn't actually null. */
1119 ofpbuf_prealloc_tailroom(b, 1);
1123 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
1130 name_len = strcspn(s, "(");
1131 if (s[name_len] != '(') {
1132 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
1135 header = parse_nxm_field_name(name, name_len);
1137 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
1142 nx_put_header__(b, header, false);
1143 s = ofpbuf_put_hex(b, s, &n);
1144 if (n != nxm_field_bytes(header)) {
1145 ovs_fatal(0, "%.2s: hex digits expected", s);
1147 if (nxm_hasmask(header)) {
1148 s += strspn(s, " ");
1150 ovs_fatal(0, "%s: missing / in masked field %.*s",
1151 full_s, name_len, name);
1153 s = ofpbuf_put_hex(b, s + 1, &n);
1154 if (n != nxm_field_bytes(header)) {
1155 ovs_fatal(0, "%.2s: hex digits expected", s);
1159 s += strspn(s, " ");
1161 ovs_fatal(0, "%s: missing ) following field %.*s",
1162 full_s, name_len, name);
1167 return ofpbuf_size(b) - start_len;
1171 nx_match_from_string(const char *s, struct ofpbuf *b)
1173 int match_len = nx_match_from_string_raw(s, b);
1174 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1179 oxm_match_from_string(const char *s, struct ofpbuf *b)
1182 struct ofp11_match_header *omh;
1183 size_t start_len = ofpbuf_size(b);
1185 ofpbuf_put_uninit(b, sizeof *omh);
1186 match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
1187 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1189 omh = ofpbuf_at(b, start_len, sizeof *omh);
1190 omh->type = htons(OFPMT_OXM);
1191 omh->length = htons(match_len);
1196 /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into
1199 * Returns NULL if successful, otherwise a malloc()'d string describing the
1200 * error. The caller is responsible for freeing the returned string. */
1201 char * WARN_UNUSED_RESULT
1202 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1204 const char *full_s = s;
1207 error = mf_parse_subfield__(&move->src, &s);
1211 if (strncmp(s, "->", 2)) {
1212 return xasprintf("%s: missing `->' following source", full_s);
1215 error = mf_parse_subfield(&move->dst, s);
1220 if (move->src.n_bits != move->dst.n_bits) {
1221 return xasprintf("%s: source field is %d bits wide but destination is "
1222 "%d bits wide", full_s,
1223 move->src.n_bits, move->dst.n_bits);
1228 /* nxm_format_reg_move(). */
1231 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1233 ds_put_format(s, "move:");
1234 mf_format_subfield(&move->src, s);
1235 ds_put_cstr(s, "->");
1236 mf_format_subfield(&move->dst, s);
1241 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1245 error = mf_check_src(&move->src, flow);
1250 return mf_check_dst(&move->dst, NULL);
1253 /* nxm_execute_reg_move(). */
1256 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1257 struct flow *flow, struct flow_wildcards *wc)
1259 union mf_value src_value;
1260 union mf_value dst_value;
1262 mf_mask_field_and_prereqs(move->dst.field, &wc->masks);
1263 mf_mask_field_and_prereqs(move->src.field, &wc->masks);
1265 mf_get_value(move->dst.field, flow, &dst_value);
1266 mf_get_value(move->src.field, flow, &src_value);
1267 bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1268 &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1270 mf_set_flow_value(move->dst.field, &dst_value, flow);
1274 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1275 struct flow *flow, struct flow_wildcards *wc)
1277 union mf_subvalue src_subvalue;
1278 union mf_subvalue mask_value;
1279 ovs_be64 src_data_be = htonll(src_data);
1281 memset(&mask_value, 0xff, sizeof mask_value);
1282 mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1284 bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1285 &src_subvalue, sizeof src_subvalue, 0,
1286 sizeof src_data_be * 8);
1287 mf_write_subfield_flow(dst, &src_subvalue, flow);
1290 /* nxm_parse_stack_action, works for both push() and pop(). */
1292 /* Parses 's' as a "push" or "pop" action, in the form described in
1293 * ovs-ofctl(8), into '*stack_action'.
1295 * Returns NULL if successful, otherwise a malloc()'d string describing the
1296 * error. The caller is responsible for freeing the returned string. */
1297 char * WARN_UNUSED_RESULT
1298 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1302 error = mf_parse_subfield__(&stack_action->subfield, &s);
1308 return xasprintf("%s: trailing garbage following push or pop", s);
1315 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1317 ds_put_cstr(s, "push:");
1318 mf_format_subfield(&push->subfield, s);
1322 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1324 ds_put_cstr(s, "pop:");
1325 mf_format_subfield(&pop->subfield, s);
1329 nxm_stack_push_check(const struct ofpact_stack *push,
1330 const struct flow *flow)
1332 return mf_check_src(&push->subfield, flow);
1336 nxm_stack_pop_check(const struct ofpact_stack *pop,
1337 const struct flow *flow)
1339 return mf_check_dst(&pop->subfield, flow);
1342 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1344 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1346 ofpbuf_put(stack, v, sizeof *v);
1349 static union mf_subvalue *
1350 nx_stack_pop(struct ofpbuf *stack)
1352 union mf_subvalue *v = NULL;
1354 if (ofpbuf_size(stack)) {
1356 ofpbuf_set_size(stack, ofpbuf_size(stack) - sizeof *v);
1357 v = (union mf_subvalue *) ofpbuf_tail(stack);
1364 nxm_execute_stack_push(const struct ofpact_stack *push,
1365 const struct flow *flow, struct flow_wildcards *wc,
1366 struct ofpbuf *stack)
1368 union mf_subvalue mask_value;
1369 union mf_subvalue dst_value;
1371 memset(&mask_value, 0xff, sizeof mask_value);
1372 mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1374 mf_read_subfield(&push->subfield, flow, &dst_value);
1375 nx_stack_push(stack, &dst_value);
1379 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1380 struct flow *flow, struct flow_wildcards *wc,
1381 struct ofpbuf *stack)
1383 union mf_subvalue *src_value;
1385 src_value = nx_stack_pop(stack);
1387 /* Only pop if stack is not empty. Otherwise, give warning. */
1389 union mf_subvalue mask_value;
1391 memset(&mask_value, 0xff, sizeof mask_value);
1392 mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks);
1393 mf_write_subfield_flow(&pop->subfield, src_value, flow);
1395 if (!VLOG_DROP_WARN(&rl)) {
1396 char *flow_str = flow_to_string(flow);
1397 VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n"
1404 /* Formats 'sf' into 's' in a format normally acceptable to
1405 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
1406 * sf->field has no NXM name.) */
1408 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
1411 ds_put_cstr(s, "<unknown>");
1413 const struct nxm_field *f = nxm_field_from_mf_field(sf->field->id, 0);
1414 ds_put_cstr(s, f ? f->name : sf->field->name);
1417 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
1418 ds_put_cstr(s, "[]");
1419 } else if (sf->n_bits == 1) {
1420 ds_put_format(s, "[%d]", sf->ofs);
1422 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
1426 static const struct nxm_field *
1427 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
1429 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
1434 return nxm_field_by_name(name, name_len);
1437 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
1438 * returns NULL and advances '*sp' to the first byte following the parsed
1439 * string. On failure, returns a malloc()'d error message, does not modify
1440 * '*sp', and does not properly initialize 'sf'.
1442 * The syntax parsed from '*sp' takes the form "header[start..end]" where
1443 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
1444 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
1445 * may both be omitted (the [] are still required) to indicate an entire
1447 char * WARN_UNUSED_RESULT
1448 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
1450 const struct mf_field *field;
1451 const struct nxm_field *f;
1460 name_len = strcspn(s, "[");
1461 if (s[name_len] != '[') {
1462 return xasprintf("%s: missing [ looking for field name", *sp);
1465 f = mf_parse_subfield_name(name, name_len, &wild);
1467 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
1469 field = mf_from_id(f->id);
1472 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
1473 /* Nothing to do. */
1474 } else if (ovs_scan(s, "[%d]", &start)) {
1476 } else if (!strncmp(s, "[]", 2)) {
1478 end = field->n_bits - 1;
1480 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
1481 "[<start>..<end>]", *sp);
1483 s = strchr(s, ']') + 1;
1486 return xasprintf("%s: starting bit %d is after ending bit %d",
1488 } else if (start >= field->n_bits) {
1489 return xasprintf("%s: starting bit %d is not valid because field is "
1490 "only %d bits wide", *sp, start, field->n_bits);
1491 } else if (end >= field->n_bits){
1492 return xasprintf("%s: ending bit %d is not valid because field is "
1493 "only %d bits wide", *sp, end, field->n_bits);
1498 sf->n_bits = end - start + 1;
1504 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
1505 * successful, otherwise a malloc()'d string describing the error. The caller
1506 * is responsible for freeing the returned string.
1508 * The syntax parsed from 's' takes the form "header[start..end]" where
1509 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
1510 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
1511 * may both be omitted (the [] are still required) to indicate an entire
1513 char * WARN_UNUSED_RESULT
1514 mf_parse_subfield(struct mf_subfield *sf, const char *s)
1516 char *error = mf_parse_subfield__(sf, &s);
1517 if (!error && s[0]) {
1518 error = xstrdup("unexpected input following field syntax");
1523 /* Returns an bitmap in which each bit corresponds to the like-numbered field
1524 * in the OFPXMC12_OPENFLOW_BASIC OXM class, in which the bit values are taken
1525 * from the 'fields' bitmap. Only fields defined in OpenFlow 'version' are
1528 * This is useful for encoding OpenFlow 1.2 table stats messages. */
1530 oxm_bitmap_from_mf_bitmap(const struct mf_bitmap *fields,
1531 enum ofp_version version)
1533 uint64_t oxm_bitmap = 0;
1536 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fields->bm) {
1537 uint32_t oxm = mf_oxm_header(i, version);
1538 uint32_t vendor = nxm_vendor(oxm);
1539 int field = nxm_field(oxm);
1541 if (vendor == OFPXMC12_OPENFLOW_BASIC && field < 64) {
1542 oxm_bitmap |= UINT64_C(1) << field;
1545 return htonll(oxm_bitmap);
1548 /* Opposite conversion from oxm_bitmap_from_mf_bitmap().
1550 * This is useful for decoding OpenFlow 1.2 table stats messages. */
1552 oxm_bitmap_to_mf_bitmap(ovs_be64 oxm_bitmap, enum ofp_version version)
1554 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
1556 for (enum mf_field_id id = 0; id < MFF_N_IDS; id++) {
1557 if (version >= mf_oxm_version(id)) {
1558 uint32_t oxm = mf_oxm_header(id, version);
1559 uint32_t vendor = nxm_vendor(oxm);
1560 int field = nxm_field(oxm);
1562 if (vendor == OFPXMC12_OPENFLOW_BASIC
1564 && oxm_bitmap & htonll(UINT64_C(1) << field)) {
1565 bitmap_set1(fields.bm, id);
1572 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1573 * modified with a "set_field" action. */
1575 oxm_writable_fields(void)
1577 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1580 for (i = 0; i < MFF_N_IDS; i++) {
1581 if (mf_oxm_header(i, 0) && mf_from_id(i)->writable) {
1582 bitmap_set1(b.bm, i);
1588 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1589 * matched in a flow table. */
1591 oxm_matchable_fields(void)
1593 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1596 for (i = 0; i < MFF_N_IDS; i++) {
1597 if (mf_oxm_header(i, 0)) {
1598 bitmap_set1(b.bm, i);
1604 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1605 * matched in a flow table with an arbitrary bitmask. */
1607 oxm_maskable_fields(void)
1609 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1612 for (i = 0; i < MFF_N_IDS; i++) {
1613 if (mf_oxm_header(i, 0) && mf_from_id(i)->maskable == MFM_FULLY) {
1614 bitmap_set1(b.bm, i);
1620 struct nxm_field_index {
1621 struct hmap_node header_node;
1622 struct hmap_node name_node;
1623 struct nxm_field nf;
1626 #include "nx-match.inc"
1628 static struct hmap nxm_header_map;
1629 static struct hmap nxm_name_map;
1630 static struct nxm_field *nxm_fields[MFF_N_IDS];
1631 static struct nxm_field *oxm_fields[MFF_N_IDS];
1636 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1637 if (ovsthread_once_start(&once)) {
1638 hmap_init(&nxm_header_map);
1639 hmap_init(&nxm_name_map);
1640 for (struct nxm_field_index *nfi = all_nxm_fields;
1641 nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) {
1642 hmap_insert(&nxm_header_map, &nfi->header_node,
1643 hash_int(nfi->nf.header, 0));
1644 hmap_insert(&nxm_name_map, &nfi->name_node,
1645 hash_string(nfi->nf.name, 0));
1646 if (is_nxm_header(nfi->nf.header)) {
1647 nxm_fields[nfi->nf.id] = &nfi->nf;
1649 oxm_fields[nfi->nf.id] = &nfi->nf;
1652 ovsthread_once_done(&once);
1656 static const struct nxm_field *
1657 nxm_field_by_header(uint32_t header)
1659 const struct nxm_field_index *nfi;
1662 if (nxm_hasmask(header)) {
1663 header = nxm_make_exact_header(header);
1666 HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_int(header, 0),
1668 if (header == nfi->nf.header) {
1675 static const struct nxm_field *
1676 nxm_field_by_name(const char *name, size_t len)
1678 const struct nxm_field_index *nfi;
1681 HMAP_FOR_EACH_WITH_HASH (nfi, name_node, hash_bytes(name, len, 0),
1683 if (strlen(nfi->nf.name) == len && !memcmp(nfi->nf.name, name, len)) {
1690 static const struct nxm_field *
1691 nxm_field_by_mf_id(enum mf_field_id id)
1694 return nxm_fields[id];
1697 static const struct nxm_field *
1698 oxm_field_by_mf_id(enum mf_field_id id)
1701 return oxm_fields[id];