]> git.proxmox.com Git - ovs.git/blob - lib/nx-match.c
85b58bf642b1b9180acf83490ad9ce5fc63bbd86
[ovs.git] / lib / nx-match.c
1 /*
2 * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18
19 #include "nx-match.h"
20
21 #include <netinet/icmp6.h>
22
23 #include "classifier.h"
24 #include "dynamic-string.h"
25 #include "hmap.h"
26 #include "meta-flow.h"
27 #include "ofp-actions.h"
28 #include "ofp-errors.h"
29 #include "ofp-util.h"
30 #include "ofpbuf.h"
31 #include "openflow/nicira-ext.h"
32 #include "packets.h"
33 #include "shash.h"
34 #include "unaligned.h"
35 #include "util.h"
36 #include "openvswitch/vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(nx_match);
39
40 /* OXM headers.
41 *
42 *
43 * Standard OXM/NXM
44 * ================
45 *
46 * The header is 32 bits long. It looks like this:
47 *
48 * |31 16 15 9| 8 7 0
49 * +----------------------------------+---------------+--+------------------+
50 * | oxm_class | oxm_field |hm| oxm_length |
51 * +----------------------------------+---------------+--+------------------+
52 *
53 * where hm stands for oxm_hasmask. It is followed by oxm_length bytes of
54 * payload. When oxm_hasmask is 0, the payload is the value of the field
55 * identified by the header; when oxm_hasmask is 1, the payload is a value for
56 * the field followed by a mask of equal length.
57 *
58 * Internally, we represent a standard OXM header as a 64-bit integer with the
59 * above information in the most-significant bits.
60 *
61 *
62 * Experimenter OXM
63 * ================
64 *
65 * The header is 64 bits long. It looks like the diagram above except that a
66 * 32-bit experimenter ID, which we call oxm_vendor and which identifies a
67 * vendor, is inserted just before the payload. Experimenter OXMs are
68 * identified by an all-1-bits oxm_class (OFPXMC12_EXPERIMENTER). The
69 * oxm_length value *includes* the experimenter ID, so that the real payload is
70 * only oxm_length - 4 bytes long.
71 *
72 * Internally, we represent an experimenter OXM header as a 64-bit integer with
73 * the standard header in the upper 32 bits and the experimenter ID in the
74 * lower 32 bits. (It would be more convenient to swap the positions of the
75 * two 32-bit words, but this would be more error-prone because experimenter
76 * OXMs are very rarely used, so accidentally passing one through a 32-bit type
77 * somewhere in the OVS code would be hard to find.)
78 */
79
80 /*
81 * OXM Class IDs.
82 * The high order bit differentiate reserved classes from member classes.
83 * Classes 0x0000 to 0x7FFF are member classes, allocated by ONF.
84 * Classes 0x8000 to 0xFFFE are reserved classes, reserved for standardisation.
85 */
86 enum ofp12_oxm_class {
87 OFPXMC12_NXM_0 = 0x0000, /* Backward compatibility with NXM */
88 OFPXMC12_NXM_1 = 0x0001, /* Backward compatibility with NXM */
89 OFPXMC12_OPENFLOW_BASIC = 0x8000, /* Basic class for OpenFlow */
90 OFPXMC15_PACKET_REGS = 0x8001, /* Packet registers (pipeline fields). */
91 OFPXMC12_EXPERIMENTER = 0xffff, /* Experimenter class */
92 };
93
94 /* Functions for extracting raw field values from OXM/NXM headers. */
95 static uint32_t nxm_vendor(uint64_t header) { return header; }
96 static int nxm_class(uint64_t header) { return header >> 48; }
97 static int nxm_field(uint64_t header) { return (header >> 41) & 0x7f; }
98 static bool nxm_hasmask(uint64_t header) { return (header >> 40) & 1; }
99 static int nxm_length(uint64_t header) { return (header >> 32) & 0xff; }
100
101 static bool
102 is_experimenter_oxm(uint64_t header)
103 {
104 return nxm_class(header) == OFPXMC12_EXPERIMENTER;
105 }
106
107 /* The OXM header "length" field is somewhat tricky:
108 *
109 * - For a standard OXM header, the length is the number of bytes of the
110 * payload, and the payload consists of just the value (and mask, if
111 * present).
112 *
113 * - For an experimenter OXM header, the length is the number of bytes in
114 * the payload plus 4 (the length of the experimenter ID). That is, the
115 * experimenter ID is included in oxm_length.
116 *
117 * This function returns the length of the experimenter ID field in 'header'.
118 * That is, for an experimenter OXM (when an experimenter ID is present), it
119 * returns 4, and for a standard OXM (when no experimenter ID is present), it
120 * returns 0. */
121 static int
122 nxm_experimenter_len(uint64_t header)
123 {
124 return is_experimenter_oxm(header) ? 4 : 0;
125 }
126
127 /* Returns the number of bytes that follow the header for an NXM/OXM entry
128 * with the given 'header'. */
129 static int
130 nxm_payload_len(uint64_t header)
131 {
132 return nxm_length(header) - nxm_experimenter_len(header);
133 }
134
135 /* Returns the number of bytes in the header for an NXM/OXM entry with the
136 * given 'header'. */
137 static int
138 nxm_header_len(uint64_t header)
139 {
140 return 4 + nxm_experimenter_len(header);
141 }
142
143 #define NXM_HEADER(VENDOR, CLASS, FIELD, HASMASK, LENGTH) \
144 (((uint64_t) (CLASS) << 48) | \
145 ((uint64_t) (FIELD) << 41) | \
146 ((uint64_t) (HASMASK) << 40) | \
147 ((uint64_t) (LENGTH) << 32) | \
148 (VENDOR))
149
150 #define NXM_HEADER_FMT "%#"PRIx32":%d:%d:%d:%d"
151 #define NXM_HEADER_ARGS(HEADER) \
152 nxm_vendor(HEADER), nxm_class(HEADER), nxm_field(HEADER), \
153 nxm_hasmask(HEADER), nxm_length(HEADER)
154
155 /* Functions for turning the "hasmask" bit on or off. (This also requires
156 * adjusting the length.) */
157 static uint64_t
158 nxm_make_exact_header(uint64_t header)
159 {
160 int new_len = nxm_payload_len(header) / 2 + nxm_experimenter_len(header);
161 return NXM_HEADER(nxm_vendor(header), nxm_class(header),
162 nxm_field(header), 0, new_len);
163 }
164 static uint64_t
165 nxm_make_wild_header(uint64_t header)
166 {
167 int new_len = nxm_payload_len(header) * 2 + nxm_experimenter_len(header);
168 return NXM_HEADER(nxm_vendor(header), nxm_class(header),
169 nxm_field(header), 1, new_len);
170 }
171
172 /* Flow cookie.
173 *
174 * This may be used to gain the OpenFlow 1.1-like ability to restrict
175 * certain NXM-based Flow Mod and Flow Stats Request messages to flows
176 * with specific cookies. See the "nx_flow_mod" and "nx_flow_stats_request"
177 * structure definitions for more details. This match is otherwise not
178 * allowed. */
179 #define NXM_NX_COOKIE NXM_HEADER (0, 0x0001, 30, 0, 8)
180 #define NXM_NX_COOKIE_W nxm_make_wild_header(NXM_NX_COOKIE)
181
182 struct nxm_field {
183 uint64_t header;
184 enum ofp_version version;
185 const char *name; /* e.g. "NXM_OF_IN_PORT". */
186
187 enum mf_field_id id;
188 };
189
190 static const struct nxm_field *nxm_field_by_header(uint64_t header);
191 static const struct nxm_field *nxm_field_by_name(const char *name, size_t len);
192 static const struct nxm_field *nxm_field_by_mf_id(enum mf_field_id,
193 enum ofp_version);
194
195 static void nx_put_header__(struct ofpbuf *, uint64_t header, bool masked);
196
197 /* Rate limit for nx_match parse errors. These always indicate a bug in the
198 * peer and so there's not much point in showing a lot of them. */
199 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
200
201 static const struct nxm_field *
202 mf_parse_subfield_name(const char *name, int name_len, bool *wild);
203
204 /* Returns the preferred OXM header to use for field 'id' in OpenFlow version
205 * 'version'. Specify 0 for 'version' if an NXM legacy header should be
206 * preferred over any standardized OXM header. Returns 0 if field 'id' cannot
207 * be expressed in NXM or OXM. */
208 static uint64_t
209 mf_oxm_header(enum mf_field_id id, enum ofp_version version)
210 {
211 const struct nxm_field *f = nxm_field_by_mf_id(id, version);
212 return f ? f->header : 0;
213 }
214
215 /* Returns the 32-bit OXM or NXM header to use for field 'id', preferring an
216 * NXM legacy header over any standardized OXM header. Returns 0 if field 'id'
217 * cannot be expressed with a 32-bit NXM or OXM header.
218 *
219 * Whenever possible, use nx_pull_header() instead of this function, because
220 * this function cannot support 64-bit experimenter OXM headers. */
221 uint32_t
222 mf_nxm_header(enum mf_field_id id)
223 {
224 uint64_t oxm = mf_oxm_header(id, 0);
225 return is_experimenter_oxm(oxm) ? 0 : oxm >> 32;
226 }
227
228 static const struct mf_field *
229 mf_from_oxm_header(uint64_t header)
230 {
231 const struct nxm_field *f = nxm_field_by_header(header);
232 return f ? mf_from_id(f->id) : NULL;
233 }
234
235 /* Returns the "struct mf_field" that corresponds to NXM or OXM header
236 * 'header', or NULL if 'header' doesn't correspond to any known field. */
237 const struct mf_field *
238 mf_from_nxm_header(uint32_t header)
239 {
240 return mf_from_oxm_header((uint64_t) header << 32);
241 }
242
243 /* Returns the width of the data for a field with the given 'header', in
244 * bytes. */
245 static int
246 nxm_field_bytes(uint64_t header)
247 {
248 unsigned int length = nxm_payload_len(header);
249 return nxm_hasmask(header) ? length / 2 : length;
250 }
251 \f
252 /* nx_pull_match() and helpers. */
253
254 /* Given NXM/OXM value 'value' and mask 'mask' associated with 'header', checks
255 * for any 1-bit in the value where there is a 0-bit in the mask. Returns 0 if
256 * none, otherwise an error code. */
257 static bool
258 is_mask_consistent(uint64_t header, const uint8_t *value, const uint8_t *mask)
259 {
260 unsigned int width = nxm_field_bytes(header);
261 unsigned int i;
262
263 for (i = 0; i < width; i++) {
264 if (value[i] & ~mask[i]) {
265 if (!VLOG_DROP_WARN(&rl)) {
266 VLOG_WARN_RL(&rl, "Rejecting NXM/OXM entry "NXM_HEADER_FMT " "
267 "with 1-bits in value for bits wildcarded by the "
268 "mask.", NXM_HEADER_ARGS(header));
269 }
270 return false;
271 }
272 }
273 return true;
274 }
275
276 static bool
277 is_cookie_pseudoheader(uint64_t header)
278 {
279 return header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W;
280 }
281
282 static enum ofperr
283 nx_pull_header__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
284 const struct mf_field **field)
285 {
286 if (b->size < 4) {
287 goto bad_len;
288 }
289
290 *header = ((uint64_t) ntohl(get_unaligned_be32(b->data))) << 32;
291 if (is_experimenter_oxm(*header)) {
292 if (b->size < 8) {
293 goto bad_len;
294 }
295 *header = ntohll(get_unaligned_be64(b->data));
296 }
297 if (nxm_length(*header) <= nxm_experimenter_len(*header)) {
298 VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has invalid length %d "
299 "(minimum is %d)",
300 NXM_HEADER_ARGS(*header), nxm_length(*header),
301 nxm_header_len(*header) + 1);
302 goto error;
303 }
304 ofpbuf_pull(b, nxm_header_len(*header));
305
306 if (field) {
307 *field = mf_from_oxm_header(*header);
308 if (!*field && !(allow_cookie && is_cookie_pseudoheader(*header))) {
309 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" is unknown",
310 NXM_HEADER_ARGS(*header));
311 return OFPERR_OFPBMC_BAD_FIELD;
312 }
313 }
314
315 return 0;
316
317 bad_len:
318 VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry",
319 b->size);
320 error:
321 *header = 0;
322 if (field) {
323 *field = NULL;
324 }
325 return OFPERR_OFPBMC_BAD_LEN;
326 }
327
328 static enum ofperr
329 nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
330 const struct mf_field **field,
331 union mf_value *value, union mf_value *mask)
332 {
333 enum ofperr header_error;
334 unsigned int payload_len;
335 const uint8_t *payload;
336 int width;
337
338 header_error = nx_pull_header__(b, allow_cookie, header, field);
339 if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) {
340 return header_error;
341 }
342
343 payload_len = nxm_payload_len(*header);
344 payload = ofpbuf_try_pull(b, payload_len);
345 if (!payload) {
346 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte "
347 "payload but only %"PRIu32" bytes follow OXM header",
348 NXM_HEADER_ARGS(*header), payload_len, b->size);
349 return OFPERR_OFPBMC_BAD_LEN;
350 }
351
352 width = nxm_field_bytes(*header);
353 if (nxm_hasmask(*header)
354 && !is_mask_consistent(*header, payload, payload + width)) {
355 return OFPERR_OFPBMC_BAD_WILDCARDS;
356 }
357
358 memcpy(value, payload, MIN(width, sizeof *value));
359 if (mask) {
360 if (nxm_hasmask(*header)) {
361 memcpy(mask, payload + width, MIN(width, sizeof *mask));
362 } else {
363 memset(mask, 0xff, MIN(width, sizeof *mask));
364 }
365 } else if (nxm_hasmask(*header)) {
366 VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but "
367 "masked OXMs are not allowed here",
368 NXM_HEADER_ARGS(*header));
369 return OFPERR_OFPBMC_BAD_MASK;
370 }
371
372 return header_error;
373 }
374
375 /* Attempts to pull an NXM or OXM header, value, and mask (if present) from the
376 * beginning of 'b'. If successful, stores a pointer to the "struct mf_field"
377 * corresponding to the pulled header in '*field', the value into '*value',
378 * and the mask into '*mask', and returns 0. On error, returns an OpenFlow
379 * error; in this case, some bytes might have been pulled off 'b' anyhow, and
380 * the output parameters might have been modified.
381 *
382 * If a NULL 'mask' is supplied, masked OXM or NXM entries are treated as
383 * errors (with OFPERR_OFPBMC_BAD_MASK).
384 */
385 enum ofperr
386 nx_pull_entry(struct ofpbuf *b, const struct mf_field **field,
387 union mf_value *value, union mf_value *mask)
388 {
389 uint64_t header;
390
391 return nx_pull_entry__(b, false, &header, field, value, mask);
392 }
393
394 /* Attempts to pull an NXM or OXM header from the beginning of 'b'. If
395 * successful, stores a pointer to the "struct mf_field" corresponding to the
396 * pulled header in '*field', stores the header's hasmask bit in '*masked'
397 * (true if hasmask=1, false if hasmask=0), and returns 0. On error, returns
398 * an OpenFlow error; in this case, some bytes might have been pulled off 'b'
399 * anyhow, and the output parameters might have been modified.
400 *
401 * If NULL 'masked' is supplied, masked OXM or NXM headers are treated as
402 * errors (with OFPERR_OFPBMC_BAD_MASK).
403 */
404 enum ofperr
405 nx_pull_header(struct ofpbuf *b, const struct mf_field **field, bool *masked)
406 {
407 enum ofperr error;
408 uint64_t header;
409
410 error = nx_pull_header__(b, false, &header, field);
411 if (masked) {
412 *masked = !error && nxm_hasmask(header);
413 } else if (!error && nxm_hasmask(header)) {
414 error = OFPERR_OFPBMC_BAD_MASK;
415 }
416 return error;
417 }
418
419 static enum ofperr
420 nx_pull_match_entry(struct ofpbuf *b, bool allow_cookie,
421 const struct mf_field **field,
422 union mf_value *value, union mf_value *mask)
423 {
424 enum ofperr error;
425 uint64_t header;
426
427 error = nx_pull_entry__(b, allow_cookie, &header, field, value, mask);
428 if (error) {
429 return error;
430 }
431 if (field && *field) {
432 if (!mf_is_mask_valid(*field, mask)) {
433 VLOG_DBG_RL(&rl, "bad mask for field %s", (*field)->name);
434 return OFPERR_OFPBMC_BAD_MASK;
435 }
436 if (!mf_is_value_valid(*field, value)) {
437 VLOG_DBG_RL(&rl, "bad value for field %s", (*field)->name);
438 return OFPERR_OFPBMC_BAD_VALUE;
439 }
440 }
441 return 0;
442 }
443
444 static enum ofperr
445 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
446 struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask)
447 {
448 struct ofpbuf b;
449
450 ovs_assert((cookie != NULL) == (cookie_mask != NULL));
451
452 match_init_catchall(match);
453 if (cookie) {
454 *cookie = *cookie_mask = htonll(0);
455 }
456
457 ofpbuf_use_const(&b, p, match_len);
458 while (b.size) {
459 const uint8_t *pos = b.data;
460 const struct mf_field *field;
461 union mf_value value;
462 union mf_value mask;
463 enum ofperr error;
464
465 error = nx_pull_match_entry(&b, cookie != NULL, &field, &value, &mask);
466 if (error) {
467 if (error == OFPERR_OFPBMC_BAD_FIELD && !strict) {
468 continue;
469 }
470 } else if (!field) {
471 if (!cookie) {
472 error = OFPERR_OFPBMC_BAD_FIELD;
473 } else if (*cookie_mask) {
474 error = OFPERR_OFPBMC_DUP_FIELD;
475 } else {
476 *cookie = value.be64;
477 *cookie_mask = mask.be64;
478 }
479 } else if (!mf_are_prereqs_ok(field, &match->flow)) {
480 error = OFPERR_OFPBMC_BAD_PREREQ;
481 } else if (!mf_is_all_wild(field, &match->wc)) {
482 error = OFPERR_OFPBMC_DUP_FIELD;
483 } else {
484 mf_set(field, &value, &mask, match);
485 }
486
487 if (error) {
488 VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
489 "within match (%s)", pos -
490 p, ofperr_to_string(error));
491 return error;
492 }
493 }
494
495 return 0;
496 }
497
498 static enum ofperr
499 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
500 struct match *match,
501 ovs_be64 *cookie, ovs_be64 *cookie_mask)
502 {
503 uint8_t *p = NULL;
504
505 if (match_len) {
506 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
507 if (!p) {
508 VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
509 "multiple of 8, is longer than space in message (max "
510 "length %"PRIu32")", match_len, b->size);
511 return OFPERR_OFPBMC_BAD_LEN;
512 }
513 }
514
515 return nx_pull_raw(p, match_len, strict, match, cookie, cookie_mask);
516 }
517
518 /* Parses the nx_match formatted match description in 'b' with length
519 * 'match_len'. Stores the results in 'match'. If 'cookie' and 'cookie_mask'
520 * are valid pointers, then stores the cookie and mask in them if 'b' contains
521 * a "NXM_NX_COOKIE*" match. Otherwise, stores 0 in both.
522 *
523 * Fails with an error upon encountering an unknown NXM header.
524 *
525 * Returns 0 if successful, otherwise an OpenFlow error code. */
526 enum ofperr
527 nx_pull_match(struct ofpbuf *b, unsigned int match_len, struct match *match,
528 ovs_be64 *cookie, ovs_be64 *cookie_mask)
529 {
530 return nx_pull_match__(b, match_len, true, match, cookie, cookie_mask);
531 }
532
533 /* Behaves the same as nx_pull_match(), but skips over unknown NXM headers,
534 * instead of failing with an error. */
535 enum ofperr
536 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
537 struct match *match,
538 ovs_be64 *cookie, ovs_be64 *cookie_mask)
539 {
540 return nx_pull_match__(b, match_len, false, match, cookie, cookie_mask);
541 }
542
543 static enum ofperr
544 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
545 {
546 struct ofp11_match_header *omh = b->data;
547 uint8_t *p;
548 uint16_t match_len;
549
550 if (b->size < sizeof *omh) {
551 return OFPERR_OFPBMC_BAD_LEN;
552 }
553
554 match_len = ntohs(omh->length);
555 if (match_len < sizeof *omh) {
556 return OFPERR_OFPBMC_BAD_LEN;
557 }
558
559 if (omh->type != htons(OFPMT_OXM)) {
560 return OFPERR_OFPBMC_BAD_TYPE;
561 }
562
563 p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
564 if (!p) {
565 VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
566 "multiple of 8, is longer than space in message (max "
567 "length %"PRIu32")", match_len, b->size);
568 return OFPERR_OFPBMC_BAD_LEN;
569 }
570
571 return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
572 strict, match, NULL, NULL);
573 }
574
575 /* Parses the oxm formatted match description preceded by a struct
576 * ofp11_match_header in 'b'. Stores the result in 'match'.
577 *
578 * Fails with an error when encountering unknown OXM headers.
579 *
580 * Returns 0 if successful, otherwise an OpenFlow error code. */
581 enum ofperr
582 oxm_pull_match(struct ofpbuf *b, struct match *match)
583 {
584 return oxm_pull_match__(b, true, match);
585 }
586
587 /* Behaves the same as oxm_pull_match() with one exception. Skips over unknown
588 * OXM headers instead of failing with an error when they are encountered. */
589 enum ofperr
590 oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
591 {
592 return oxm_pull_match__(b, false, match);
593 }
594
595 /* Verify an array of OXM TLVs treating value of each TLV as a mask,
596 * disallowing masks in each TLV and ignoring pre-requisites. */
597 enum ofperr
598 oxm_pull_field_array(const void *fields_data, size_t fields_len,
599 struct field_array *fa)
600 {
601 struct ofpbuf b;
602
603 ofpbuf_use_const(&b, fields_data, fields_len);
604 while (b.size) {
605 const uint8_t *pos = b.data;
606 const struct mf_field *field;
607 union mf_value value;
608 enum ofperr error;
609 uint64_t header;
610
611 error = nx_pull_entry__(&b, false, &header, &field, &value, NULL);
612 if (error) {
613 VLOG_DBG_RL(&rl, "error pulling field array field");
614 return error;
615 } else if (!field) {
616 VLOG_DBG_RL(&rl, "unknown field array field");
617 error = OFPERR_OFPBMC_BAD_FIELD;
618 } else if (bitmap_is_set(fa->used.bm, field->id)) {
619 VLOG_DBG_RL(&rl, "duplicate field array field '%s'", field->name);
620 error = OFPERR_OFPBMC_DUP_FIELD;
621 } else if (!mf_is_mask_valid(field, &value)) {
622 VLOG_DBG_RL(&rl, "bad mask in field array field '%s'", field->name);
623 return OFPERR_OFPBMC_BAD_MASK;
624 } else {
625 field_array_set(field->id, &value, fa);
626 }
627
628 if (error) {
629 const uint8_t *start = fields_data;
630
631 VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
632 "within field array (%s)", pos - start,
633 ofperr_to_string(error));
634 return error;
635 }
636 }
637
638 return 0;
639 }
640 \f
641 /* nx_put_match() and helpers.
642 *
643 * 'put' functions whose names end in 'w' add a wildcarded field.
644 * 'put' functions whose names end in 'm' add a field that might be wildcarded.
645 * Other 'put' functions add exact-match fields.
646 */
647
648 static void
649 nxm_put_unmasked(struct ofpbuf *b, enum mf_field_id field,
650 enum ofp_version version, const void *value, size_t n_bytes)
651 {
652 nx_put_header(b, field, version, false);
653 ofpbuf_put(b, value, n_bytes);
654 }
655
656 static void
657 nxm_put(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
658 const void *value, const void *mask, size_t n_bytes)
659 {
660 if (!is_all_zeros(mask, n_bytes)) {
661 bool masked = !is_all_ones(mask, n_bytes);
662 nx_put_header(b, field, version, masked);
663 ofpbuf_put(b, value, n_bytes);
664 if (masked) {
665 ofpbuf_put(b, mask, n_bytes);
666 }
667 }
668 }
669
670 static void
671 nxm_put_8m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
672 uint8_t value, uint8_t mask)
673 {
674 nxm_put(b, field, version, &value, &mask, sizeof value);
675 }
676
677 static void
678 nxm_put_8(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
679 uint8_t value)
680 {
681 nxm_put_unmasked(b, field, version, &value, sizeof value);
682 }
683
684 static void
685 nxm_put_16m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
686 ovs_be16 value, ovs_be16 mask)
687 {
688 nxm_put(b, field, version, &value, &mask, sizeof value);
689 }
690
691 static void
692 nxm_put_16(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
693 ovs_be16 value)
694 {
695 nxm_put_unmasked(b, field, version, &value, sizeof value);
696 }
697
698 static void
699 nxm_put_32m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
700 ovs_be32 value, ovs_be32 mask)
701 {
702 nxm_put(b, field, version, &value, &mask, sizeof value);
703 }
704
705 static void
706 nxm_put_32(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
707 ovs_be32 value)
708 {
709 nxm_put_unmasked(b, field, version, &value, sizeof value);
710 }
711
712 static void
713 nxm_put_64m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
714 ovs_be64 value, ovs_be64 mask)
715 {
716 nxm_put(b, field, version, &value, &mask, sizeof value);
717 }
718
719 static void
720 nxm_put_eth_masked(struct ofpbuf *b,
721 enum mf_field_id field, enum ofp_version version,
722 const uint8_t value[ETH_ADDR_LEN],
723 const uint8_t mask[ETH_ADDR_LEN])
724 {
725 nxm_put(b, field, version, value, mask, ETH_ADDR_LEN);
726 }
727
728 static void
729 nxm_put_ipv6(struct ofpbuf *b,
730 enum mf_field_id field, enum ofp_version version,
731 const struct in6_addr *value, const struct in6_addr *mask)
732 {
733 nxm_put(b, field, version, value->s6_addr, mask->s6_addr,
734 sizeof value->s6_addr);
735 }
736
737 static void
738 nxm_put_frag(struct ofpbuf *b, const struct match *match,
739 enum ofp_version version)
740 {
741 uint8_t nw_frag = match->flow.nw_frag & FLOW_NW_FRAG_MASK;
742 uint8_t nw_frag_mask = match->wc.masks.nw_frag & FLOW_NW_FRAG_MASK;
743
744 nxm_put_8m(b, MFF_IP_FRAG, version, nw_frag,
745 nw_frag_mask == FLOW_NW_FRAG_MASK ? UINT8_MAX : nw_frag_mask);
746 }
747
748 /* Appends to 'b' a set of OXM or NXM matches for the IPv4 or IPv6 fields in
749 * 'match'. */
750 static void
751 nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm)
752 {
753 const struct flow *flow = &match->flow;
754
755 if (flow->dl_type == htons(ETH_TYPE_IP)) {
756 nxm_put_32m(b, MFF_IPV4_SRC, oxm,
757 flow->nw_src, match->wc.masks.nw_src);
758 nxm_put_32m(b, MFF_IPV4_DST, oxm,
759 flow->nw_dst, match->wc.masks.nw_dst);
760 } else {
761 nxm_put_ipv6(b, MFF_IPV6_SRC, oxm,
762 &flow->ipv6_src, &match->wc.masks.ipv6_src);
763 nxm_put_ipv6(b, MFF_IPV6_DST, oxm,
764 &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
765 }
766
767 nxm_put_frag(b, match, oxm);
768
769 if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
770 if (oxm) {
771 nxm_put_8(b, MFF_IP_DSCP_SHIFTED, oxm,
772 flow->nw_tos >> 2);
773 } else {
774 nxm_put_8(b, MFF_IP_DSCP, oxm,
775 flow->nw_tos & IP_DSCP_MASK);
776 }
777 }
778
779 if (match->wc.masks.nw_tos & IP_ECN_MASK) {
780 nxm_put_8(b, MFF_IP_ECN, oxm,
781 flow->nw_tos & IP_ECN_MASK);
782 }
783
784 if (!oxm && match->wc.masks.nw_ttl) {
785 nxm_put_8(b, MFF_IP_TTL, oxm, flow->nw_ttl);
786 }
787
788 nxm_put_32m(b, MFF_IPV6_LABEL, oxm,
789 flow->ipv6_label, match->wc.masks.ipv6_label);
790
791 if (match->wc.masks.nw_proto) {
792 nxm_put_8(b, MFF_IP_PROTO, oxm, flow->nw_proto);
793
794 if (flow->nw_proto == IPPROTO_TCP) {
795 nxm_put_16m(b, MFF_TCP_SRC, oxm,
796 flow->tp_src, match->wc.masks.tp_src);
797 nxm_put_16m(b, MFF_TCP_DST, oxm,
798 flow->tp_dst, match->wc.masks.tp_dst);
799 nxm_put_16m(b, MFF_TCP_FLAGS, oxm,
800 flow->tcp_flags, match->wc.masks.tcp_flags);
801 } else if (flow->nw_proto == IPPROTO_UDP) {
802 nxm_put_16m(b, MFF_UDP_SRC, oxm,
803 flow->tp_src, match->wc.masks.tp_src);
804 nxm_put_16m(b, MFF_UDP_DST, oxm,
805 flow->tp_dst, match->wc.masks.tp_dst);
806 } else if (flow->nw_proto == IPPROTO_SCTP) {
807 nxm_put_16m(b, MFF_SCTP_SRC, oxm, flow->tp_src,
808 match->wc.masks.tp_src);
809 nxm_put_16m(b, MFF_SCTP_DST, oxm, flow->tp_dst,
810 match->wc.masks.tp_dst);
811 } else if (is_icmpv4(flow)) {
812 if (match->wc.masks.tp_src) {
813 nxm_put_8(b, MFF_ICMPV4_TYPE, oxm,
814 ntohs(flow->tp_src));
815 }
816 if (match->wc.masks.tp_dst) {
817 nxm_put_8(b, MFF_ICMPV4_CODE, oxm,
818 ntohs(flow->tp_dst));
819 }
820 } else if (is_icmpv6(flow)) {
821 if (match->wc.masks.tp_src) {
822 nxm_put_8(b, MFF_ICMPV6_TYPE, oxm,
823 ntohs(flow->tp_src));
824 }
825 if (match->wc.masks.tp_dst) {
826 nxm_put_8(b, MFF_ICMPV6_CODE, oxm,
827 ntohs(flow->tp_dst));
828 }
829 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
830 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
831 nxm_put_ipv6(b, MFF_ND_TARGET, oxm,
832 &flow->nd_target, &match->wc.masks.nd_target);
833 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
834 nxm_put_eth_masked(b, MFF_ND_SLL, oxm,
835 flow->arp_sha, match->wc.masks.arp_sha);
836 }
837 if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
838 nxm_put_eth_masked(b, MFF_ND_TLL, oxm,
839 flow->arp_tha, match->wc.masks.arp_tha);
840 }
841 }
842 }
843 }
844 }
845
846 /* Appends to 'b' the nx_match format that expresses 'match'. For Flow Mod and
847 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
848 * Otherwise, 'cookie_mask' should be zero.
849 *
850 * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify
851 * 'oxm' as the OpenFlow version number for the OXM format to use.
852 *
853 * This function can cause 'b''s data to be reallocated.
854 *
855 * Returns the number of bytes appended to 'b', excluding padding.
856 *
857 * If 'match' is a catch-all rule that matches every packet, then this function
858 * appends nothing to 'b' and returns 0. */
859 static int
860 nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
861 ovs_be64 cookie, ovs_be64 cookie_mask)
862 {
863 const struct flow *flow = &match->flow;
864 const size_t start_len = b->size;
865 int match_len;
866 int i;
867
868 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
869
870 /* Metadata. */
871 if (match->wc.masks.dp_hash) {
872 nxm_put_32m(b, MFF_DP_HASH, oxm,
873 htonl(flow->dp_hash), htonl(match->wc.masks.dp_hash));
874 }
875
876 if (match->wc.masks.recirc_id) {
877 nxm_put_32(b, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id));
878 }
879
880 if (match->wc.masks.conj_id) {
881 nxm_put_32(b, MFF_CONJ_ID, oxm, htonl(flow->conj_id));
882 }
883
884 if (match->wc.masks.in_port.ofp_port) {
885 ofp_port_t in_port = flow->in_port.ofp_port;
886 if (oxm) {
887 nxm_put_32(b, MFF_IN_PORT_OXM, oxm,
888 ofputil_port_to_ofp11(in_port));
889 } else {
890 nxm_put_16(b, MFF_IN_PORT, oxm,
891 htons(ofp_to_u16(in_port)));
892 }
893 }
894 if (match->wc.masks.actset_output) {
895 nxm_put_32(b, MFF_ACTSET_OUTPUT, oxm,
896 ofputil_port_to_ofp11(flow->actset_output));
897 }
898
899 /* Ethernet. */
900 nxm_put_eth_masked(b, MFF_ETH_SRC, oxm,
901 flow->dl_src, match->wc.masks.dl_src);
902 nxm_put_eth_masked(b, MFF_ETH_DST, oxm,
903 flow->dl_dst, match->wc.masks.dl_dst);
904 nxm_put_16m(b, MFF_ETH_TYPE, oxm,
905 ofputil_dl_type_to_openflow(flow->dl_type),
906 match->wc.masks.dl_type);
907
908 /* 802.1Q. */
909 if (oxm) {
910 ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
911 ovs_be16 vid = flow->vlan_tci & VID_CFI_MASK;
912 ovs_be16 mask = match->wc.masks.vlan_tci & VID_CFI_MASK;
913
914 if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
915 nxm_put_16(b, MFF_VLAN_VID, oxm, vid);
916 } else if (mask) {
917 nxm_put_16m(b, MFF_VLAN_VID, oxm, vid, mask);
918 }
919
920 if (vid && vlan_tci_to_pcp(match->wc.masks.vlan_tci)) {
921 nxm_put_8(b, MFF_VLAN_PCP, oxm,
922 vlan_tci_to_pcp(flow->vlan_tci));
923 }
924
925 } else {
926 nxm_put_16m(b, MFF_VLAN_TCI, oxm, flow->vlan_tci,
927 match->wc.masks.vlan_tci);
928 }
929
930 /* MPLS. */
931 if (eth_type_mpls(flow->dl_type)) {
932 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
933 nxm_put_8(b, MFF_MPLS_TC, oxm,
934 mpls_lse_to_tc(flow->mpls_lse[0]));
935 }
936
937 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
938 nxm_put_8(b, MFF_MPLS_BOS, oxm,
939 mpls_lse_to_bos(flow->mpls_lse[0]));
940 }
941
942 if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
943 nxm_put_32(b, MFF_MPLS_LABEL, oxm,
944 htonl(mpls_lse_to_label(flow->mpls_lse[0])));
945 }
946 }
947
948 /* L3. */
949 if (is_ip_any(flow)) {
950 nxm_put_ip(b, match, oxm);
951 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
952 flow->dl_type == htons(ETH_TYPE_RARP)) {
953 /* ARP. */
954 if (match->wc.masks.nw_proto) {
955 nxm_put_16(b, MFF_ARP_OP, oxm,
956 htons(flow->nw_proto));
957 }
958 nxm_put_32m(b, MFF_ARP_SPA, oxm,
959 flow->nw_src, match->wc.masks.nw_src);
960 nxm_put_32m(b, MFF_ARP_TPA, oxm,
961 flow->nw_dst, match->wc.masks.nw_dst);
962 nxm_put_eth_masked(b, MFF_ARP_SHA, oxm,
963 flow->arp_sha, match->wc.masks.arp_sha);
964 nxm_put_eth_masked(b, MFF_ARP_THA, oxm,
965 flow->arp_tha, match->wc.masks.arp_tha);
966 }
967
968 /* Tunnel ID. */
969 nxm_put_64m(b, MFF_TUN_ID, oxm,
970 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
971
972 /* Other tunnel metadata. */
973 nxm_put_32m(b, MFF_TUN_SRC, oxm,
974 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
975 nxm_put_32m(b, MFF_TUN_DST, oxm,
976 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
977 nxm_put_16m(b, MFF_TUN_GBP_ID, oxm,
978 flow->tunnel.gbp_id, match->wc.masks.tunnel.gbp_id);
979 nxm_put_8m(b, MFF_TUN_GBP_FLAGS, oxm,
980 flow->tunnel.gbp_flags, match->wc.masks.tunnel.gbp_flags);
981
982 /* Registers. */
983 if (oxm < OFP15_VERSION) {
984 for (i = 0; i < FLOW_N_REGS; i++) {
985 nxm_put_32m(b, MFF_REG0 + i, oxm,
986 htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
987 }
988 } else {
989 for (i = 0; i < FLOW_N_XREGS; i++) {
990 nxm_put_64m(b, MFF_XREG0 + i, oxm,
991 htonll(flow_get_xreg(flow, i)),
992 htonll(flow_get_xreg(&match->wc.masks, i)));
993 }
994 }
995
996 /* Mark. */
997 nxm_put_32m(b, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark),
998 htonl(match->wc.masks.pkt_mark));
999
1000 /* OpenFlow 1.1+ Metadata. */
1001 nxm_put_64m(b, MFF_METADATA, oxm,
1002 flow->metadata, match->wc.masks.metadata);
1003
1004 /* Cookie. */
1005 if (cookie_mask) {
1006 bool masked = cookie_mask != OVS_BE64_MAX;
1007
1008 cookie &= cookie_mask;
1009 nx_put_header__(b, NXM_NX_COOKIE, masked);
1010 ofpbuf_put(b, &cookie, sizeof cookie);
1011 if (masked) {
1012 ofpbuf_put(b, &cookie_mask, sizeof cookie_mask);
1013 }
1014 }
1015
1016 match_len = b->size - start_len;
1017 return match_len;
1018 }
1019
1020 /* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
1021 * bytes to pad the nx_match out to a multiple of 8. For Flow Mod and Flow
1022 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
1023 * Otherwise, 'cookie_mask' should be zero.
1024 *
1025 * This function can cause 'b''s data to be reallocated.
1026 *
1027 * Returns the number of bytes appended to 'b', excluding padding. The return
1028 * value can be zero if it appended nothing at all to 'b' (which happens if
1029 * 'cr' is a catch-all rule that matches every packet). */
1030 int
1031 nx_put_match(struct ofpbuf *b, const struct match *match,
1032 ovs_be64 cookie, ovs_be64 cookie_mask)
1033 {
1034 int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask);
1035
1036 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1037 return match_len;
1038 }
1039
1040 /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that
1041 * expresses 'cr', plus enough zero bytes to pad the data appended out to a
1042 * multiple of 8.
1043 *
1044 * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow
1045 * version in use as 'version'.
1046 *
1047 * This function can cause 'b''s data to be reallocated.
1048 *
1049 * Returns the number of bytes appended to 'b', excluding the padding. Never
1050 * returns zero. */
1051 int
1052 oxm_put_match(struct ofpbuf *b, const struct match *match,
1053 enum ofp_version version)
1054 {
1055 int match_len;
1056 struct ofp11_match_header *omh;
1057 size_t start_len = b->size;
1058 ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
1059
1060 ofpbuf_put_uninit(b, sizeof *omh);
1061 match_len = (nx_put_raw(b, version, match, cookie, cookie_mask)
1062 + sizeof *omh);
1063 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1064
1065 omh = ofpbuf_at(b, start_len, sizeof *omh);
1066 omh->type = htons(OFPMT_OXM);
1067 omh->length = htons(match_len);
1068
1069 return match_len;
1070 }
1071
1072 /* Appends to 'b' the nx_match format that expresses the tlv corresponding
1073 * to 'id'. If mask is not all-ones then it is also formated as the value
1074 * of the tlv. */
1075 static void
1076 nx_format_mask_tlv(struct ds *ds, enum mf_field_id id,
1077 const union mf_value *mask)
1078 {
1079 const struct mf_field *mf = mf_from_id(id);
1080
1081 ds_put_format(ds, "%s", mf->name);
1082
1083 if (!is_all_ones(mask, mf->n_bytes)) {
1084 ds_put_char(ds, '=');
1085 mf_format(mf, mask, NULL, ds);
1086 }
1087
1088 ds_put_char(ds, ',');
1089 }
1090
1091 /* Appends a string representation of 'fa_' to 'ds'.
1092 * The TLVS value of 'fa_' is treated as a mask and
1093 * only the name of fields is formated if it is all ones. */
1094 void
1095 oxm_format_field_array(struct ds *ds, const struct field_array *fa)
1096 {
1097 size_t start_len = ds->length;
1098 int i;
1099
1100 for (i = 0; i < MFF_N_IDS; i++) {
1101 if (bitmap_is_set(fa->used.bm, i)) {
1102 nx_format_mask_tlv(ds, i, &fa->value[i]);
1103 }
1104 }
1105
1106 if (ds->length > start_len) {
1107 ds_chomp(ds, ',');
1108 }
1109 }
1110
1111 /* Appends to 'b' a series of OXM TLVs corresponding to the series
1112 * of enum mf_field_id and value tuples in 'fa_'.
1113 *
1114 * OXM differs slightly among versions of OpenFlow. Specify the OpenFlow
1115 * version in use as 'version'.
1116 *
1117 * This function can cause 'b''s data to be reallocated.
1118 *
1119 * Returns the number of bytes appended to 'b'. May return zero. */
1120 int
1121 oxm_put_field_array(struct ofpbuf *b, const struct field_array *fa,
1122 enum ofp_version version)
1123 {
1124 size_t start_len = b->size;
1125 int i;
1126
1127 /* Field arrays are only used with the group selection method
1128 * property and group properties are only available in OpenFlow 1.5+.
1129 * So the following assertion should never fail.
1130 *
1131 * If support for older OpenFlow versions is desired then some care
1132 * will need to be taken of different TLVs that handle the same
1133 * flow fields. In particular:
1134 * - VLAN_TCI, VLAN_VID and MFF_VLAN_PCP
1135 * - IP_DSCP_MASK and DSCP_SHIFTED
1136 * - REGS and XREGS
1137 */
1138 ovs_assert(version >= OFP15_VERSION);
1139
1140 for (i = 0; i < MFF_N_IDS; i++) {
1141 if (bitmap_is_set(fa->used.bm, i)) {
1142 nxm_put_unmasked(b, i, version, &fa->value[i],
1143 mf_from_id(i)->n_bytes);
1144 }
1145 }
1146
1147 return b->size - start_len;
1148 }
1149
1150 static void
1151 nx_put_header__(struct ofpbuf *b, uint64_t header, bool masked)
1152 {
1153 uint64_t masked_header = masked ? nxm_make_wild_header(header) : header;
1154 ovs_be64 network_header = htonll(masked_header);
1155
1156 ofpbuf_put(b, &network_header, nxm_header_len(header));
1157 }
1158
1159 void
1160 nx_put_header(struct ofpbuf *b, enum mf_field_id field,
1161 enum ofp_version version, bool masked)
1162 {
1163 nx_put_header__(b, mf_oxm_header(field, version), masked);
1164 }
1165
1166 void
1167 nx_put_entry(struct ofpbuf *b,
1168 enum mf_field_id field, enum ofp_version version,
1169 const union mf_value *value, const union mf_value *mask)
1170 {
1171 int n_bytes = mf_from_id(field)->n_bytes;
1172 bool masked = mask && !is_all_ones(mask, n_bytes);
1173
1174 nx_put_header(b, field, version, masked);
1175 ofpbuf_put(b, value, n_bytes);
1176 if (masked) {
1177 ofpbuf_put(b, mask, n_bytes);
1178 }
1179 }
1180 \f
1181 /* nx_match_to_string() and helpers. */
1182
1183 static void format_nxm_field_name(struct ds *, uint64_t header);
1184
1185 char *
1186 nx_match_to_string(const uint8_t *p, unsigned int match_len)
1187 {
1188 struct ofpbuf b;
1189 struct ds s;
1190
1191 if (!match_len) {
1192 return xstrdup("<any>");
1193 }
1194
1195 ofpbuf_use_const(&b, p, match_len);
1196 ds_init(&s);
1197 while (b.size) {
1198 union mf_value value;
1199 union mf_value mask;
1200 enum ofperr error;
1201 uint64_t header;
1202 int value_len;
1203
1204 error = nx_pull_entry__(&b, true, &header, NULL, &value, &mask);
1205 if (error) {
1206 break;
1207 }
1208 value_len = MIN(sizeof value, nxm_field_bytes(header));
1209
1210 if (s.length) {
1211 ds_put_cstr(&s, ", ");
1212 }
1213
1214 format_nxm_field_name(&s, header);
1215 ds_put_char(&s, '(');
1216
1217 for (int i = 0; i < value_len; i++) {
1218 ds_put_format(&s, "%02x", ((const uint8_t *) &value)[i]);
1219 }
1220 if (nxm_hasmask(header)) {
1221 ds_put_char(&s, '/');
1222 for (int i = 0; i < value_len; i++) {
1223 ds_put_format(&s, "%02x", ((const uint8_t *) &mask)[i]);
1224 }
1225 }
1226 ds_put_char(&s, ')');
1227 }
1228
1229 if (b.size) {
1230 if (s.length) {
1231 ds_put_cstr(&s, ", ");
1232 }
1233
1234 ds_put_format(&s, "<%u invalid bytes>", b.size);
1235 }
1236
1237 return ds_steal_cstr(&s);
1238 }
1239
1240 char *
1241 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
1242 {
1243 const struct ofp11_match_header *omh = p->data;
1244 uint16_t match_len_;
1245 struct ds s;
1246
1247 ds_init(&s);
1248
1249 if (match_len < sizeof *omh) {
1250 ds_put_format(&s, "<match too short: %u>", match_len);
1251 goto err;
1252 }
1253
1254 if (omh->type != htons(OFPMT_OXM)) {
1255 ds_put_format(&s, "<bad match type field: %u>", ntohs(omh->type));
1256 goto err;
1257 }
1258
1259 match_len_ = ntohs(omh->length);
1260 if (match_len_ < sizeof *omh) {
1261 ds_put_format(&s, "<match length field too short: %u>", match_len_);
1262 goto err;
1263 }
1264
1265 if (match_len_ != match_len) {
1266 ds_put_format(&s, "<match length field incorrect: %u != %u>",
1267 match_len_, match_len);
1268 goto err;
1269 }
1270
1271 return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
1272 match_len - sizeof *omh);
1273
1274 err:
1275 return ds_steal_cstr(&s);
1276 }
1277
1278 void
1279 nx_format_field_name(enum mf_field_id id, enum ofp_version version,
1280 struct ds *s)
1281 {
1282 format_nxm_field_name(s, mf_oxm_header(id, version));
1283 }
1284
1285 static void
1286 format_nxm_field_name(struct ds *s, uint64_t header)
1287 {
1288 const struct nxm_field *f = nxm_field_by_header(header);
1289 if (f) {
1290 ds_put_cstr(s, f->name);
1291 if (nxm_hasmask(header)) {
1292 ds_put_cstr(s, "_W");
1293 }
1294 } else if (header == NXM_NX_COOKIE) {
1295 ds_put_cstr(s, "NXM_NX_COOKIE");
1296 } else if (header == NXM_NX_COOKIE_W) {
1297 ds_put_cstr(s, "NXM_NX_COOKIE_W");
1298 } else {
1299 ds_put_format(s, "%d:%d", nxm_class(header), nxm_field(header));
1300 }
1301 }
1302
1303 static bool
1304 streq_len(const char *a, size_t a_len, const char *b)
1305 {
1306 return strlen(b) == a_len && !memcmp(a, b, a_len);
1307 }
1308
1309 static uint64_t
1310 parse_nxm_field_name(const char *name, int name_len)
1311 {
1312 const struct nxm_field *f;
1313 bool wild;
1314
1315 f = mf_parse_subfield_name(name, name_len, &wild);
1316 if (f) {
1317 if (!wild) {
1318 return f->header;
1319 } else if (mf_from_id(f->id)->maskable != MFM_NONE) {
1320 return nxm_make_wild_header(f->header);
1321 }
1322 }
1323
1324 if (streq_len(name, name_len, "NXM_NX_COOKIE")) {
1325 return NXM_NX_COOKIE;
1326 } else if (streq_len(name, name_len, "NXM_NX_COOKIE_W")) {
1327 return NXM_NX_COOKIE_W;
1328 }
1329
1330 /* Check whether it's a field header value as hex.
1331 * (This isn't ordinarily useful except for testing error behavior.) */
1332 if (name_len == 8) {
1333 uint64_t header;
1334 bool ok;
1335
1336 header = hexits_value(name, name_len, &ok) << 32;
1337 if (ok) {
1338 return header;
1339 }
1340 } else if (name_len == 16) {
1341 uint64_t header;
1342 bool ok;
1343
1344 header = hexits_value(name, name_len, &ok);
1345 if (ok && is_experimenter_oxm(header)) {
1346 return header;
1347 }
1348 }
1349
1350 return 0;
1351 }
1352 \f
1353 /* nx_match_from_string(). */
1354
1355 static int
1356 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
1357 {
1358 const char *full_s = s;
1359 const size_t start_len = b->size;
1360
1361 if (!strcmp(s, "<any>")) {
1362 /* Ensure that 'b->data' isn't actually null. */
1363 ofpbuf_prealloc_tailroom(b, 1);
1364 return 0;
1365 }
1366
1367 for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
1368 const char *name;
1369 uint64_t header;
1370 int name_len;
1371 size_t n;
1372
1373 name = s;
1374 name_len = strcspn(s, "(");
1375 if (s[name_len] != '(') {
1376 ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
1377 }
1378
1379 header = parse_nxm_field_name(name, name_len);
1380 if (!header) {
1381 ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
1382 }
1383
1384 s += name_len + 1;
1385
1386 nx_put_header__(b, header, false);
1387 s = ofpbuf_put_hex(b, s, &n);
1388 if (n != nxm_field_bytes(header)) {
1389 ovs_fatal(0, "%.2s: hex digits expected", s);
1390 }
1391 if (nxm_hasmask(header)) {
1392 s += strspn(s, " ");
1393 if (*s != '/') {
1394 ovs_fatal(0, "%s: missing / in masked field %.*s",
1395 full_s, name_len, name);
1396 }
1397 s = ofpbuf_put_hex(b, s + 1, &n);
1398 if (n != nxm_field_bytes(header)) {
1399 ovs_fatal(0, "%.2s: hex digits expected", s);
1400 }
1401 }
1402
1403 s += strspn(s, " ");
1404 if (*s != ')') {
1405 ovs_fatal(0, "%s: missing ) following field %.*s",
1406 full_s, name_len, name);
1407 }
1408 s++;
1409 }
1410
1411 return b->size - start_len;
1412 }
1413
1414 int
1415 nx_match_from_string(const char *s, struct ofpbuf *b)
1416 {
1417 int match_len = nx_match_from_string_raw(s, b);
1418 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1419 return match_len;
1420 }
1421
1422 int
1423 oxm_match_from_string(const char *s, struct ofpbuf *b)
1424 {
1425 int match_len;
1426 struct ofp11_match_header *omh;
1427 size_t start_len = b->size;
1428
1429 ofpbuf_put_uninit(b, sizeof *omh);
1430 match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
1431 ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1432
1433 omh = ofpbuf_at(b, start_len, sizeof *omh);
1434 omh->type = htons(OFPMT_OXM);
1435 omh->length = htons(match_len);
1436
1437 return match_len;
1438 }
1439 \f
1440 /* Parses 's' as a "move" action, in the form described in ovs-ofctl(8), into
1441 * '*move'.
1442 *
1443 * Returns NULL if successful, otherwise a malloc()'d string describing the
1444 * error. The caller is responsible for freeing the returned string. */
1445 char * OVS_WARN_UNUSED_RESULT
1446 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
1447 {
1448 const char *full_s = s;
1449 char *error;
1450
1451 error = mf_parse_subfield__(&move->src, &s);
1452 if (error) {
1453 return error;
1454 }
1455 if (strncmp(s, "->", 2)) {
1456 return xasprintf("%s: missing `->' following source", full_s);
1457 }
1458 s += 2;
1459 error = mf_parse_subfield(&move->dst, s);
1460 if (error) {
1461 return error;
1462 }
1463
1464 if (move->src.n_bits != move->dst.n_bits) {
1465 return xasprintf("%s: source field is %d bits wide but destination is "
1466 "%d bits wide", full_s,
1467 move->src.n_bits, move->dst.n_bits);
1468 }
1469 return NULL;
1470 }
1471 \f
1472 /* nxm_format_reg_move(). */
1473
1474 void
1475 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1476 {
1477 ds_put_format(s, "move:");
1478 mf_format_subfield(&move->src, s);
1479 ds_put_cstr(s, "->");
1480 mf_format_subfield(&move->dst, s);
1481 }
1482
1483 \f
1484 enum ofperr
1485 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
1486 {
1487 enum ofperr error;
1488
1489 error = mf_check_src(&move->src, flow);
1490 if (error) {
1491 return error;
1492 }
1493
1494 return mf_check_dst(&move->dst, flow);
1495 }
1496 \f
1497 /* nxm_execute_reg_move(). */
1498
1499 void
1500 nxm_execute_reg_move(const struct ofpact_reg_move *move,
1501 struct flow *flow, struct flow_wildcards *wc)
1502 {
1503 union mf_value src_value;
1504 union mf_value dst_value;
1505
1506 mf_mask_field_and_prereqs(move->dst.field, &wc->masks);
1507 mf_mask_field_and_prereqs(move->src.field, &wc->masks);
1508
1509 /* A flow may wildcard nw_frag. Do nothing if setting a transport
1510 * header field on a packet that does not have them. */
1511 if (mf_are_prereqs_ok(move->dst.field, flow)
1512 && mf_are_prereqs_ok(move->src.field, flow)) {
1513
1514 mf_get_value(move->dst.field, flow, &dst_value);
1515 mf_get_value(move->src.field, flow, &src_value);
1516 bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
1517 &dst_value, move->dst.field->n_bytes, move->dst.ofs,
1518 move->src.n_bits);
1519 mf_set_flow_value(move->dst.field, &dst_value, flow);
1520 }
1521 }
1522
1523 void
1524 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1525 struct flow *flow, struct flow_wildcards *wc)
1526 {
1527 union mf_subvalue src_subvalue;
1528 union mf_subvalue mask_value;
1529 ovs_be64 src_data_be = htonll(src_data);
1530
1531 memset(&mask_value, 0xff, sizeof mask_value);
1532 mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1533
1534 bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1535 &src_subvalue, sizeof src_subvalue, 0,
1536 sizeof src_data_be * 8);
1537 mf_write_subfield_flow(dst, &src_subvalue, flow);
1538 }
1539 \f
1540 /* nxm_parse_stack_action, works for both push() and pop(). */
1541
1542 /* Parses 's' as a "push" or "pop" action, in the form described in
1543 * ovs-ofctl(8), into '*stack_action'.
1544 *
1545 * Returns NULL if successful, otherwise a malloc()'d string describing the
1546 * error. The caller is responsible for freeing the returned string. */
1547 char * OVS_WARN_UNUSED_RESULT
1548 nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1549 {
1550 char *error;
1551
1552 error = mf_parse_subfield__(&stack_action->subfield, &s);
1553 if (error) {
1554 return error;
1555 }
1556
1557 if (*s != '\0') {
1558 return xasprintf("%s: trailing garbage following push or pop", s);
1559 }
1560
1561 return NULL;
1562 }
1563
1564 void
1565 nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1566 {
1567 ds_put_cstr(s, "push:");
1568 mf_format_subfield(&push->subfield, s);
1569 }
1570
1571 void
1572 nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1573 {
1574 ds_put_cstr(s, "pop:");
1575 mf_format_subfield(&pop->subfield, s);
1576 }
1577
1578 enum ofperr
1579 nxm_stack_push_check(const struct ofpact_stack *push,
1580 const struct flow *flow)
1581 {
1582 return mf_check_src(&push->subfield, flow);
1583 }
1584
1585 enum ofperr
1586 nxm_stack_pop_check(const struct ofpact_stack *pop,
1587 const struct flow *flow)
1588 {
1589 return mf_check_dst(&pop->subfield, flow);
1590 }
1591
1592 /* nxm_execute_stack_push(), nxm_execute_stack_pop(). */
1593 static void
1594 nx_stack_push(struct ofpbuf *stack, union mf_subvalue *v)
1595 {
1596 ofpbuf_put(stack, v, sizeof *v);
1597 }
1598
1599 static union mf_subvalue *
1600 nx_stack_pop(struct ofpbuf *stack)
1601 {
1602 union mf_subvalue *v = NULL;
1603
1604 if (stack->size) {
1605
1606 stack->size -= sizeof *v;
1607 v = (union mf_subvalue *) ofpbuf_tail(stack);
1608 }
1609
1610 return v;
1611 }
1612
1613 void
1614 nxm_execute_stack_push(const struct ofpact_stack *push,
1615 const struct flow *flow, struct flow_wildcards *wc,
1616 struct ofpbuf *stack)
1617 {
1618 union mf_subvalue mask_value;
1619 union mf_subvalue dst_value;
1620
1621 memset(&mask_value, 0xff, sizeof mask_value);
1622 mf_write_subfield_flow(&push->subfield, &mask_value, &wc->masks);
1623
1624 mf_read_subfield(&push->subfield, flow, &dst_value);
1625 nx_stack_push(stack, &dst_value);
1626 }
1627
1628 void
1629 nxm_execute_stack_pop(const struct ofpact_stack *pop,
1630 struct flow *flow, struct flow_wildcards *wc,
1631 struct ofpbuf *stack)
1632 {
1633 union mf_subvalue *src_value;
1634
1635 src_value = nx_stack_pop(stack);
1636
1637 /* Only pop if stack is not empty. Otherwise, give warning. */
1638 if (src_value) {
1639 union mf_subvalue mask_value;
1640
1641 memset(&mask_value, 0xff, sizeof mask_value);
1642 mf_write_subfield_flow(&pop->subfield, &mask_value, &wc->masks);
1643 mf_write_subfield_flow(&pop->subfield, src_value, flow);
1644 } else {
1645 if (!VLOG_DROP_WARN(&rl)) {
1646 char *flow_str = flow_to_string(flow);
1647 VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow\n"
1648 " %s", flow_str);
1649 free(flow_str);
1650 }
1651 }
1652 }
1653 \f
1654 /* Formats 'sf' into 's' in a format normally acceptable to
1655 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
1656 * sf->field has no NXM name.) */
1657 void
1658 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
1659 {
1660 if (!sf->field) {
1661 ds_put_cstr(s, "<unknown>");
1662 } else {
1663 const struct nxm_field *f = nxm_field_by_mf_id(sf->field->id, 0);
1664 ds_put_cstr(s, f ? f->name : sf->field->name);
1665 }
1666
1667 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
1668 ds_put_cstr(s, "[]");
1669 } else if (sf->n_bits == 1) {
1670 ds_put_format(s, "[%d]", sf->ofs);
1671 } else {
1672 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
1673 }
1674 }
1675
1676 static const struct nxm_field *
1677 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
1678 {
1679 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
1680 if (*wild) {
1681 name_len -= 2;
1682 }
1683
1684 return nxm_field_by_name(name, name_len);
1685 }
1686
1687 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
1688 * returns NULL and advances '*sp' to the first byte following the parsed
1689 * string. On failure, returns a malloc()'d error message, does not modify
1690 * '*sp', and does not properly initialize 'sf'.
1691 *
1692 * The syntax parsed from '*sp' takes the form "header[start..end]" where
1693 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
1694 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
1695 * may both be omitted (the [] are still required) to indicate an entire
1696 * field. */
1697 char * OVS_WARN_UNUSED_RESULT
1698 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
1699 {
1700 const struct mf_field *field;
1701 const struct nxm_field *f;
1702 const char *name;
1703 int start, end;
1704 const char *s;
1705 int name_len;
1706 bool wild;
1707
1708 s = *sp;
1709 name = s;
1710 name_len = strcspn(s, "[");
1711 if (s[name_len] != '[') {
1712 return xasprintf("%s: missing [ looking for field name", *sp);
1713 }
1714
1715 f = mf_parse_subfield_name(name, name_len, &wild);
1716 if (!f) {
1717 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
1718 }
1719 field = mf_from_id(f->id);
1720
1721 s += name_len;
1722 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
1723 /* Nothing to do. */
1724 } else if (ovs_scan(s, "[%d]", &start)) {
1725 end = start;
1726 } else if (!strncmp(s, "[]", 2)) {
1727 start = 0;
1728 end = field->n_bits - 1;
1729 } else {
1730 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
1731 "[<start>..<end>]", *sp);
1732 }
1733 s = strchr(s, ']') + 1;
1734
1735 if (start > end) {
1736 return xasprintf("%s: starting bit %d is after ending bit %d",
1737 *sp, start, end);
1738 } else if (start >= field->n_bits) {
1739 return xasprintf("%s: starting bit %d is not valid because field is "
1740 "only %d bits wide", *sp, start, field->n_bits);
1741 } else if (end >= field->n_bits){
1742 return xasprintf("%s: ending bit %d is not valid because field is "
1743 "only %d bits wide", *sp, end, field->n_bits);
1744 }
1745
1746 sf->field = field;
1747 sf->ofs = start;
1748 sf->n_bits = end - start + 1;
1749
1750 *sp = s;
1751 return NULL;
1752 }
1753
1754 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
1755 * successful, otherwise a malloc()'d string describing the error. The caller
1756 * is responsible for freeing the returned string.
1757 *
1758 * The syntax parsed from 's' takes the form "header[start..end]" where
1759 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
1760 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
1761 * may both be omitted (the [] are still required) to indicate an entire
1762 * field. */
1763 char * OVS_WARN_UNUSED_RESULT
1764 mf_parse_subfield(struct mf_subfield *sf, const char *s)
1765 {
1766 char *error = mf_parse_subfield__(sf, &s);
1767 if (!error && s[0]) {
1768 error = xstrdup("unexpected input following field syntax");
1769 }
1770 return error;
1771 }
1772 \f
1773 /* Returns an bitmap in which each bit corresponds to the like-numbered field
1774 * in the OFPXMC12_OPENFLOW_BASIC OXM class, in which the bit values are taken
1775 * from the 'fields' bitmap. Only fields defined in OpenFlow 'version' are
1776 * considered.
1777 *
1778 * This is useful for encoding OpenFlow 1.2 table stats messages. */
1779 ovs_be64
1780 oxm_bitmap_from_mf_bitmap(const struct mf_bitmap *fields,
1781 enum ofp_version version)
1782 {
1783 uint64_t oxm_bitmap = 0;
1784 int i;
1785
1786 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fields->bm) {
1787 uint64_t oxm = mf_oxm_header(i, version);
1788 uint32_t class = nxm_class(oxm);
1789 int field = nxm_field(oxm);
1790
1791 if (class == OFPXMC12_OPENFLOW_BASIC && field < 64) {
1792 oxm_bitmap |= UINT64_C(1) << field;
1793 }
1794 }
1795 return htonll(oxm_bitmap);
1796 }
1797
1798 /* Opposite conversion from oxm_bitmap_from_mf_bitmap().
1799 *
1800 * This is useful for decoding OpenFlow 1.2 table stats messages. */
1801 struct mf_bitmap
1802 oxm_bitmap_to_mf_bitmap(ovs_be64 oxm_bitmap, enum ofp_version version)
1803 {
1804 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
1805
1806 for (enum mf_field_id id = 0; id < MFF_N_IDS; id++) {
1807 uint64_t oxm = mf_oxm_header(id, version);
1808 if (oxm && version >= nxm_field_by_header(oxm)->version) {
1809 uint32_t class = nxm_class(oxm);
1810 int field = nxm_field(oxm);
1811
1812 if (class == OFPXMC12_OPENFLOW_BASIC
1813 && field < 64
1814 && oxm_bitmap & htonll(UINT64_C(1) << field)) {
1815 bitmap_set1(fields.bm, id);
1816 }
1817 }
1818 }
1819 return fields;
1820 }
1821
1822 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1823 * modified with a "set_field" action. */
1824 struct mf_bitmap
1825 oxm_writable_fields(void)
1826 {
1827 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1828 int i;
1829
1830 for (i = 0; i < MFF_N_IDS; i++) {
1831 if (mf_oxm_header(i, 0) && mf_from_id(i)->writable) {
1832 bitmap_set1(b.bm, i);
1833 }
1834 }
1835 return b;
1836 }
1837
1838 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1839 * matched in a flow table. */
1840 struct mf_bitmap
1841 oxm_matchable_fields(void)
1842 {
1843 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1844 int i;
1845
1846 for (i = 0; i < MFF_N_IDS; i++) {
1847 if (mf_oxm_header(i, 0)) {
1848 bitmap_set1(b.bm, i);
1849 }
1850 }
1851 return b;
1852 }
1853
1854 /* Returns a bitmap of fields that can be encoded in OXM and that can be
1855 * matched in a flow table with an arbitrary bitmask. */
1856 struct mf_bitmap
1857 oxm_maskable_fields(void)
1858 {
1859 struct mf_bitmap b = MF_BITMAP_INITIALIZER;
1860 int i;
1861
1862 for (i = 0; i < MFF_N_IDS; i++) {
1863 if (mf_oxm_header(i, 0) && mf_from_id(i)->maskable == MFM_FULLY) {
1864 bitmap_set1(b.bm, i);
1865 }
1866 }
1867 return b;
1868 }
1869 \f
1870 struct nxm_field_index {
1871 struct hmap_node header_node; /* In nxm_header_map. */
1872 struct hmap_node name_node; /* In nxm_name_map. */
1873 struct ovs_list mf_node; /* In mf_mf_map[nf.id]. */
1874 const struct nxm_field nf;
1875 };
1876
1877 #include "nx-match.inc"
1878
1879 static struct hmap nxm_header_map;
1880 static struct hmap nxm_name_map;
1881 static struct ovs_list nxm_mf_map[MFF_N_IDS];
1882
1883 static void
1884 nxm_init(void)
1885 {
1886 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1887 if (ovsthread_once_start(&once)) {
1888 hmap_init(&nxm_header_map);
1889 hmap_init(&nxm_name_map);
1890 for (int i = 0; i < MFF_N_IDS; i++) {
1891 list_init(&nxm_mf_map[i]);
1892 }
1893 for (struct nxm_field_index *nfi = all_nxm_fields;
1894 nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) {
1895 hmap_insert(&nxm_header_map, &nfi->header_node,
1896 hash_uint64(nfi->nf.header));
1897 hmap_insert(&nxm_name_map, &nfi->name_node,
1898 hash_string(nfi->nf.name, 0));
1899 list_push_back(&nxm_mf_map[nfi->nf.id], &nfi->mf_node);
1900 }
1901 ovsthread_once_done(&once);
1902 }
1903 }
1904
1905 static const struct nxm_field *
1906 nxm_field_by_header(uint64_t header)
1907 {
1908 const struct nxm_field_index *nfi;
1909
1910 nxm_init();
1911 if (nxm_hasmask(header)) {
1912 header = nxm_make_exact_header(header);
1913 }
1914
1915 HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_uint64(header),
1916 &nxm_header_map) {
1917 if (header == nfi->nf.header) {
1918 return &nfi->nf;
1919 }
1920 }
1921 return NULL;
1922 }
1923
1924 static const struct nxm_field *
1925 nxm_field_by_name(const char *name, size_t len)
1926 {
1927 const struct nxm_field_index *nfi;
1928
1929 nxm_init();
1930 HMAP_FOR_EACH_WITH_HASH (nfi, name_node, hash_bytes(name, len, 0),
1931 &nxm_name_map) {
1932 if (strlen(nfi->nf.name) == len && !memcmp(nfi->nf.name, name, len)) {
1933 return &nfi->nf;
1934 }
1935 }
1936 return NULL;
1937 }
1938
1939 static const struct nxm_field *
1940 nxm_field_by_mf_id(enum mf_field_id id, enum ofp_version version)
1941 {
1942 const struct nxm_field_index *nfi;
1943 const struct nxm_field *f;
1944
1945 nxm_init();
1946
1947 f = NULL;
1948 LIST_FOR_EACH (nfi, mf_node, &nxm_mf_map[id]) {
1949 if (!f || version >= nfi->nf.version) {
1950 f = &nfi->nf;
1951 }
1952 }
1953 return f;
1954 }