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