Coverage Report

Created: 2026-06-22 06:39

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