Coverage Report

Created: 2025-07-01 06:51

/src/openvswitch/lib/nx-match.c
Line
Count
Source (jump to first uncovered line)
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
176k
static uint32_t nxm_vendor(uint64_t header) { return header; }
100
10.7M
static int nxm_class(uint64_t header) { return header >> 48; }
101
7.23M
static int nxm_field(uint64_t header) { return (header >> 41) & 0x7f; }
102
9.17M
static bool nxm_hasmask(uint64_t header) { return (header >> 40) & 1; }
103
18.2M
static int nxm_length(uint64_t header) { return (header >> 32) & 0xff; }
104
22.4M
static uint64_t nxm_no_len(uint64_t header) { return header & 0xffffff80ffffffffULL; }
105
106
static bool
107
is_experimenter_oxm(uint64_t header)
108
3.53M
{
109
3.53M
    return nxm_class(header) == OFPXMC12_EXPERIMENTER;
110
3.53M
}
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
3.07M
{
129
3.07M
    return is_experimenter_oxm(header) ? 4 : 0;
130
3.07M
}
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
998k
{
137
998k
    ovs_assert(nxm_length(header) >= nxm_experimenter_len(header));
138
998k
    return nxm_length(header) - nxm_experimenter_len(header);
139
998k
}
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
452k
{
146
452k
    return 4 + nxm_experimenter_len(header);
147
452k
}
148
149
#define NXM_HEADER(VENDOR, CLASS, FIELD, HASMASK, LENGTH)       \
150
177k
    (((uint64_t) (CLASS) << 48) |                               \
151
177k
     ((uint64_t) (FIELD) << 41) |                               \
152
177k
     ((uint64_t) (HASMASK) << 40) |                             \
153
177k
     ((uint64_t) (LENGTH) << 32) |                              \
154
177k
     (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
176k
{
166
176k
    unsigned int new_len = nxm_payload_len(header)
167
176k
                           / 2 + nxm_experimenter_len(header);
168
176k
    return NXM_HEADER(nxm_vendor(header), nxm_class(header),
169
176k
                      nxm_field(header), 0, new_len);
170
176k
}
171
static uint64_t
172
nxm_make_wild_header(uint64_t header)
173
273
{
174
273
    unsigned int new_len = nxm_payload_len(header) * 2
175
273
                           + nxm_experimenter_len(header);
176
273
    return NXM_HEADER(nxm_vendor(header), nxm_class(header),
177
273
                      nxm_field(header), 1, new_len);
178
273
}
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
560
#define NXM_NX_COOKIE     NXM_HEADER  (0, 0x0001, 30, 0, 8)
188
560
#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
7.74M
{
222
7.74M
    const struct nxm_field *f = nxm_field_by_mf_id(id, version);
223
7.74M
    return f ? f->header : 0;
224
7.74M
}
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
1.57k
{
235
1.57k
    uint64_t oxm = mf_oxm_header(id, 0);
236
1.57k
    return is_experimenter_oxm(oxm) ? 0 : oxm >> 32;
237
1.57k
}
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
0
{
246
0
    uint64_t oxm = mf_oxm_header(mff->id, 0);
247
248
0
    if (mff->mapped) {
249
0
        oxm = nxm_no_len(oxm) | ((uint64_t) mff->n_bytes << 32);
250
0
    }
251
252
0
    return is_experimenter_oxm(oxm) ? 0 : oxm >> 32;
253
0
}
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
504k
{
259
504k
    const struct nxm_field *f = nxm_field_by_header(header, is_action, h_error);
260
261
504k
    if (f) {
262
256k
        const struct mf_field *mff = mf_from_id(f->id);
263
256k
        const struct mf_field *vl_mff = mf_get_vl_mff(mff, vl_mff_map);
264
256k
        return vl_mff ? vl_mff : mff;
265
256k
    } else {
266
247k
        return NULL;
267
247k
    }
268
504k
}
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
52.1k
{
275
52.1k
    return mf_from_oxm_header((uint64_t) header << 32, vl_mff_map, false, NULL);
276
52.1k
}
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
465k
{
283
465k
    unsigned int length = nxm_payload_len(header);
284
465k
    return nxm_hasmask(header) ? length / 2 : length;
285
465k
}
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
129k
{
295
129k
    unsigned int width = nxm_field_bytes(header);
296
129k
    unsigned int i;
297
298
499k
    for (i = 0; i < width; i++) {
299
375k
        if (value[i] & ~mask[i]) {
300
5.52k
            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
5.52k
            return false;
306
5.52k
        }
307
375k
    }
308
124k
    return true;
309
129k
}
310
311
static bool
312
is_cookie_pseudoheader(uint64_t header)
313
287
{
314
287
    return header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W;
315
287
}
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
460k
{
322
460k
    if (b->size < 4) {
323
6.80k
        goto bad_len;
324
6.80k
    }
325
326
453k
    *header = ((uint64_t) ntohl(get_unaligned_be32(b->data))) << 32;
327
453k
    if (is_experimenter_oxm(*header)) {
328
19.2k
        if (b->size < 8) {
329
576
            goto bad_len;
330
576
        }
331
18.7k
        *header = ntohll(get_unaligned_be64(b->data));
332
18.7k
    }
333
452k
    if (nxm_length(*header) < nxm_experimenter_len(*header)) {
334
914
        VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has invalid length %d "
335
914
                     "(minimum is %d)",
336
914
                     NXM_HEADER_ARGS(*header), nxm_length(*header),
337
914
                     nxm_header_len(*header));
338
914
        goto error;
339
914
    }
340
452k
    ofpbuf_pull(b, nxm_header_len(*header));
341
342
452k
    if (field) {
343
452k
        enum ofperr h_error = 0;
344
452k
        *field = mf_from_oxm_header(*header, vl_mff_map, is_action, &h_error);
345
452k
        if (!*field && !(allow_cookie && is_cookie_pseudoheader(*header))) {
346
241k
            VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" is unknown",
347
241k
                        NXM_HEADER_ARGS(*header));
348
241k
            if (is_action) {
349
2.02k
                if (h_error) {
350
1.44k
                     *field = NULL;
351
1.44k
                     return h_error;
352
1.44k
                }
353
589
                return OFPERR_OFPBAC_BAD_SET_TYPE;
354
239k
            } else {
355
239k
                return OFPERR_OFPBMC_BAD_FIELD;
356
239k
            }
357
241k
        } else if (mf_vl_mff_invalid(*field, vl_mff_map)) {
358
0
            return OFPERR_NXFMFC_INVALID_TLV_FIELD;
359
0
        }
360
452k
    }
361
362
210k
    return 0;
363
364
7.37k
bad_len:
365
7.37k
    VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry",
366
7.37k
                b->size);
367
8.29k
error:
368
8.29k
    *header = 0;
369
8.29k
    if (field) {
370
8.29k
        *field = NULL;
371
8.29k
    }
372
8.29k
    return OFPERR_OFPBMC_BAD_LEN;
373
7.37k
}
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
453k
{
379
453k
    int copy_len;
380
453k
    void *copy_dst;
381
382
453k
    copy_dst = value;
383
453k
    copy_len = MIN(width, field ? field->n_bytes : sizeof *value);
384
385
453k
    if (field && field->variable_len) {
386
138k
        memset(value, 0, field->n_bytes);
387
138k
        copy_dst = &value->u8 + field->n_bytes - copy_len;
388
138k
    }
389
390
453k
    memcpy(copy_dst, payload, copy_len);
391
453k
}
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
366k
{
399
366k
    const struct mf_field *field;
400
366k
    enum ofperr header_error;
401
366k
    unsigned int payload_len;
402
366k
    const uint8_t *payload;
403
366k
    int width;
404
405
366k
    header_error = nx_pull_header__(b, allow_cookie, vl_mff_map, header,
406
366k
                                    &field, is_action);
407
366k
    if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) {
408
9.81k
        return header_error;
409
9.81k
    }
410
411
356k
    payload_len = nxm_payload_len(*header);
412
356k
    payload = ofpbuf_try_pull(b, payload_len);
413
356k
    if (!payload) {
414
21.0k
        VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte "
415
21.0k
                    "payload but only %"PRIu32" bytes follow OXM header",
416
21.0k
                    NXM_HEADER_ARGS(*header), payload_len, b->size);
417
21.0k
        return OFPERR_OFPBMC_BAD_LEN;
418
21.0k
    }
419
420
335k
    width = nxm_field_bytes(*header);
421
335k
    if (nxm_hasmask(*header)
422
335k
        && !is_mask_consistent(*header, payload, payload + width)) {
423
5.52k
        return OFPERR_OFPBMC_BAD_WILDCARDS;
424
5.52k
    }
425
426
330k
    copy_entry_value(field, value, payload, width);
427
428
330k
    if (mask) {
429
316k
        if (nxm_hasmask(*header)) {
430
123k
            copy_entry_value(field, mask, payload + width, width);
431
192k
        } else {
432
192k
            memset(mask, 0xff, sizeof *mask);
433
192k
        }
434
316k
    } else if (nxm_hasmask(*header)) {
435
898
        VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but "
436
898
                    "masked OXMs are not allowed here",
437
898
                    NXM_HEADER_ARGS(*header));
438
898
        return OFPERR_OFPBMC_BAD_MASK;
439
898
    }
440
441
329k
    if (field_) {
442
329k
        *field_ = field;
443
329k
        return header_error;
444
329k
    }
445
446
0
    return 0;
447
329k
}
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
16.4k
{
469
16.4k
    uint64_t header;
470
471
16.4k
    return nx_pull_entry__(b, false, vl_mff_map, &header, field, value, mask, is_action);
472
16.4k
}
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
93.9k
{
488
93.9k
    enum ofperr error;
489
93.9k
    uint64_t header;
490
491
93.9k
    error = nx_pull_header__(b, false, vl_mff_map,  &header, field, false);
492
93.9k
    if (masked) {
493
88.3k
        *masked = !error && nxm_hasmask(header);
494
88.3k
    } else if (!error && nxm_hasmask(header)) {
495
448
        error = OFPERR_OFPBMC_BAD_MASK;
496
448
    }
497
93.9k
    return error;
498
93.9k
}
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
341k
{
506
341k
    enum ofperr error;
507
341k
    uint64_t header;
508
509
341k
    error = nx_pull_entry__(b, allow_cookie, vl_mff_map, &header, field, value,
510
341k
                            mask, false);
511
341k
    if (error) {
512
164k
        return error;
513
164k
    }
514
176k
    if (field && *field) {
515
176k
        if (!mf_is_mask_valid(*field, mask)) {
516
218
            VLOG_DBG_RL(&rl, "bad mask for field %s", (*field)->name);
517
218
            return OFPERR_OFPBMC_BAD_MASK;
518
218
        }
519
175k
        if (!mf_is_value_valid(*field, value)) {
520
171
            VLOG_DBG_RL(&rl, "bad value for field %s", (*field)->name);
521
171
            return OFPERR_OFPBMC_BAD_VALUE;
522
171
        }
523
175k
    }
524
175k
    return 0;
525
176k
}
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
159k
{
536
159k
    ovs_assert((cookie != NULL) == (cookie_mask != NULL));
537
538
159k
    match_init_catchall(match);
539
159k
    match->flow.tunnel.metadata.tab = tun_table;
540
159k
    if (cookie) {
541
2.10k
        *cookie = *cookie_mask = htonll(0);
542
2.10k
    }
543
544
159k
    struct ofpbuf b = ofpbuf_const_initializer(p, match_len);
545
452k
    while (b.size) {
546
341k
        const uint8_t *pos = b.data;
547
341k
        const struct mf_field *field;
548
341k
        union mf_value value;
549
341k
        union mf_value mask;
550
341k
        enum ofperr error;
551
552
341k
        error = nx_pull_match_entry(&b, cookie != NULL, vl_mff_map, &field,
553
341k
                                    &value, &mask);
554
341k
        if (error) {
555
165k
            if (error == OFPERR_OFPBMC_BAD_FIELD && !strict) {
556
130k
                continue;
557
130k
            }
558
175k
        } else if (!field) {
559
77
            if (!cookie) {
560
0
                error = OFPERR_OFPBMC_BAD_FIELD;
561
77
            } else if (*cookie_mask) {
562
6
                error = OFPERR_OFPBMC_DUP_FIELD;
563
71
            } else {
564
71
                *cookie = value.be64;
565
71
                *cookie_mask = mask.be64;
566
71
            }
567
175k
        } else if (strict && !mf_are_match_prereqs_ok(field, match)) {
568
2.73k
            error = OFPERR_OFPBMC_BAD_PREREQ;
569
172k
        } else if (!mf_is_all_wild(field, &match->wc)) {
570
5.95k
            error = OFPERR_OFPBMC_DUP_FIELD;
571
167k
        } else if (pipeline_fields_only && !mf_is_pipeline_field(field)) {
572
5.20k
            error = OFPERR_OFPBRC_PIPELINE_FIELDS_ONLY;
573
161k
        } else {
574
161k
            char *err_str;
575
576
161k
            mf_set(field, &value, &mask, match, &err_str);
577
161k
            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
161k
            match_add_ethernet_prereq(match, field);
585
161k
        }
586
587
210k
        if (error) {
588
48.6k
            VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
589
48.6k
                        "within match (%s)", pos -
590
48.6k
                        p, ofperr_to_string(error));
591
48.6k
            return error;
592
48.6k
        }
593
210k
    }
594
595
110k
    match->flow.tunnel.metadata.tab = NULL;
596
110k
    return 0;
597
159k
}
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
13.7k
{
606
13.7k
    uint8_t *p = NULL;
607
608
13.7k
    if (match_len) {
609
4.61k
        p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
610
4.61k
        if (!p) {
611
2.40k
            VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
612
2.40k
                        "multiple of 8, is longer than space in message (max "
613
2.40k
                        "length %"PRIu32")", match_len, b->size);
614
2.40k
            return OFPERR_OFPBMC_BAD_LEN;
615
2.40k
        }
616
4.61k
    }
617
618
11.3k
    return nx_pull_raw(p, match_len, strict, pipeline_fields_only, match,
619
11.3k
                       cookie, cookie_mask, tun_table, vl_mff_map);
620
13.7k
}
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
9.61k
{
643
9.61k
    return nx_pull_match__(b, match_len, true, pipeline_fields_only, match,
644
9.61k
                           cookie, cookie_mask, tun_table, vl_mff_map);
645
9.61k
}
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
4.18k
{
656
4.18k
    return nx_pull_match__(b, match_len, false, pipeline_fields_only, match,
657
4.18k
                           cookie, cookie_mask, tun_table, NULL);
658
4.18k
}
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
162k
{
665
162k
    struct ofp11_match_header *omh = b->data;
666
162k
    uint8_t *p;
667
162k
    uint16_t match_len;
668
669
162k
    if (b->size < sizeof *omh) {
670
1.82k
        return OFPERR_OFPBMC_BAD_LEN;
671
1.82k
    }
672
673
160k
    match_len = ntohs(omh->length);
674
160k
    if (match_len < sizeof *omh) {
675
4.52k
        return OFPERR_OFPBMC_BAD_LEN;
676
4.52k
    }
677
678
155k
    if (omh->type != htons(OFPMT_OXM)) {
679
12.3k
        return OFPERR_OFPBMC_BAD_TYPE;
680
12.3k
    }
681
682
143k
    p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
683
143k
    if (!p) {
684
4.95k
        VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
685
4.95k
                    "multiple of 8, is longer than space in message (max "
686
4.95k
                    "length %"PRIu32")", match_len, b->size);
687
4.95k
        return OFPERR_OFPBMC_BAD_LEN;
688
4.95k
    }
689
690
138k
    return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
691
138k
                       strict, pipeline_fields_only, match, NULL, NULL,
692
138k
                       tun_table, vl_mff_map);
693
143k
}
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
47.3k
{
713
47.3k
    return oxm_pull_match__(b, true, pipeline_fields_only, tun_table,
714
47.3k
                            vl_mff_map, match);
715
47.3k
}
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
114k
{
724
114k
    return oxm_pull_match__(b, false, pipeline_fields_only, tun_table, NULL,
725
114k
                            match);
726
114k
}
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
9.72k
{
741
9.72k
    return nx_pull_raw(oxm, oxm_len, !loose, false, match, NULL, NULL,
742
9.72k
                       tun_table, vl_mff_map);
743
9.72k
}
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
4.47k
{
751
4.47k
    struct ofpbuf b = ofpbuf_const_initializer(fields_data, fields_len);
752
11.3k
    while (b.size) {
753
8.78k
        const uint8_t *pos = b.data;
754
8.78k
        const struct mf_field *field;
755
8.78k
        union mf_value value;
756
8.78k
        enum ofperr error;
757
8.78k
        uint64_t header;
758
759
8.78k
        error = nx_pull_entry__(&b, false, NULL, &header, &field, &value,
760
8.78k
                                NULL, false);
761
8.78k
        if (error) {
762
1.68k
            VLOG_DBG_RL(&rl, "error pulling field array field");
763
7.09k
        } else if (!field) {
764
0
            VLOG_DBG_RL(&rl, "unknown field array field");
765
0
            error = OFPERR_OFPBMC_BAD_FIELD;
766
7.09k
        } else if (bitmap_is_set(fa->used.bm, field->id)) {
767
24
            VLOG_DBG_RL(&rl, "duplicate field array field '%s'", field->name);
768
24
            error = OFPERR_OFPBMC_DUP_FIELD;
769
7.07k
        } else if (!mf_is_mask_valid(field, &value)) {
770
167
            VLOG_DBG_RL(&rl, "bad mask in field array field '%s'", field->name);
771
167
            error = OFPERR_OFPBMC_BAD_MASK;
772
6.90k
        } else {
773
6.90k
            field_array_set(field->id, &value, fa);
774
6.90k
        }
775
776
8.78k
        if (error) {
777
1.87k
            const uint8_t *start = fields_data;
778
779
1.87k
            VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
780
1.87k
                        "within field array (%s)", pos - start,
781
1.87k
                        ofperr_to_string(error));
782
783
1.87k
            free(fa->values);
784
1.87k
            fa->values = NULL;
785
1.87k
            return error;
786
1.87k
        }
787
8.78k
    }
788
789
2.59k
    return 0;
790
4.47k
}
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
0
{
809
0
    nx_put_header_len(b, field, version, !!mask, n_bytes);
810
0
    ofpbuf_put(b, value, n_bytes);
811
0
    if (mask) {
812
0
        ofpbuf_put(b, mask, n_bytes);
813
0
    }
814
0
}
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
0
{
821
0
    nxm_put_entry_raw(ctx->output, field, version, value, mask, n_bytes);
822
0
    if (!ctx->implied_ethernet && mf_from_id(field)->prereqs != MFP_NONE) {
823
0
        ctx->implied_ethernet = true;
824
0
    }
825
0
}
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
0
{
832
0
    if (!is_all_zeros(mask, n_bytes)) {
833
0
        bool masked = !is_all_ones(mask, n_bytes);
834
0
        nxm_put__(ctx, field, version, value, masked ? mask : NULL, n_bytes);
835
0
    }
836
0
}
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
0
{
843
0
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
844
0
}
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
0
{
850
0
    nxm_put__(ctx, field, version, &value, NULL, sizeof value);
851
0
}
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
0
{
858
0
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
859
0
}
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
0
{
865
0
    nxm_put__(ctx, field, version, &value, NULL, sizeof value);
866
0
}
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
0
{
873
0
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
874
0
}
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
0
{
880
0
    nxm_put__(ctx, field, version, &value, NULL, sizeof value);
881
0
}
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
0
{
888
0
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
889
0
}
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
0
{
896
0
    nxm_put(ctx, field, version, &value, &mask, sizeof(value));
897
0
}
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
0
{
904
0
    nxm_put(ctx, field, version, value.ea, mask.ea, ETH_ADDR_LEN);
905
0
}
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
0
{
912
0
    nxm_put(ctx, field, version, value->s6_addr, mask->s6_addr,
913
0
            sizeof value->s6_addr);
914
0
}
915
916
static void
917
nxm_put_frag(struct nxm_put_ctx *ctx, const struct match *match,
918
             enum ofp_version version)
919
0
{
920
0
    uint8_t nw_frag = match->flow.nw_frag & FLOW_NW_FRAG_MASK;
921
0
    uint8_t nw_frag_mask = match->wc.masks.nw_frag & FLOW_NW_FRAG_MASK;
922
923
0
    nxm_put_8m(ctx, MFF_IP_FRAG, version, nw_frag,
924
0
               nw_frag_mask == FLOW_NW_FRAG_MASK ? UINT8_MAX : nw_frag_mask);
925
0
}
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
0
{
933
0
    const struct flow *flow = &match->flow;
934
0
    ovs_be16 dl_type = get_dl_type(flow);
935
936
0
    if (dl_type == htons(ETH_TYPE_IP)) {
937
0
        nxm_put_32m(ctx, MFF_IPV4_SRC, oxm,
938
0
                    flow->nw_src, match->wc.masks.nw_src);
939
0
        nxm_put_32m(ctx, MFF_IPV4_DST, oxm,
940
0
                    flow->nw_dst, match->wc.masks.nw_dst);
941
0
    } else {
942
0
        nxm_put_ipv6(ctx, MFF_IPV6_SRC, oxm,
943
0
                     &flow->ipv6_src, &match->wc.masks.ipv6_src);
944
0
        nxm_put_ipv6(ctx, MFF_IPV6_DST, oxm,
945
0
                     &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
946
0
    }
947
948
0
    nxm_put_frag(ctx, match, oxm);
949
950
0
    if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
951
0
        if (oxm) {
952
0
            nxm_put_8(ctx, MFF_IP_DSCP_SHIFTED, oxm,
953
0
                      flow->nw_tos >> 2);
954
0
        } else {
955
0
            nxm_put_8(ctx, MFF_IP_DSCP, oxm,
956
0
                      flow->nw_tos & IP_DSCP_MASK);
957
0
        }
958
0
    }
959
960
0
    if (match->wc.masks.nw_tos & IP_ECN_MASK) {
961
0
        nxm_put_8(ctx, MFF_IP_ECN, oxm,
962
0
                  flow->nw_tos & IP_ECN_MASK);
963
0
    }
964
965
0
    if (match->wc.masks.nw_ttl) {
966
0
        nxm_put_8(ctx, MFF_IP_TTL, oxm, flow->nw_ttl);
967
0
    }
968
969
0
    nxm_put_32m(ctx, MFF_IPV6_LABEL, oxm,
970
0
                flow->ipv6_label, match->wc.masks.ipv6_label);
971
972
0
    if (match->wc.masks.nw_proto) {
973
0
        nxm_put_8(ctx, MFF_IP_PROTO, oxm, flow->nw_proto);
974
975
0
        if (flow->nw_proto == IPPROTO_TCP) {
976
0
            nxm_put_16m(ctx, MFF_TCP_SRC, oxm,
977
0
                        flow->tp_src, match->wc.masks.tp_src);
978
0
            nxm_put_16m(ctx, MFF_TCP_DST, oxm,
979
0
                        flow->tp_dst, match->wc.masks.tp_dst);
980
0
            nxm_put_16m(ctx, MFF_TCP_FLAGS, oxm,
981
0
                        flow->tcp_flags, match->wc.masks.tcp_flags);
982
0
        } else if (flow->nw_proto == IPPROTO_UDP) {
983
0
            nxm_put_16m(ctx, MFF_UDP_SRC, oxm,
984
0
                        flow->tp_src, match->wc.masks.tp_src);
985
0
            nxm_put_16m(ctx, MFF_UDP_DST, oxm,
986
0
                        flow->tp_dst, match->wc.masks.tp_dst);
987
0
        } else if (flow->nw_proto == IPPROTO_SCTP) {
988
0
            nxm_put_16m(ctx, MFF_SCTP_SRC, oxm, flow->tp_src,
989
0
                        match->wc.masks.tp_src);
990
0
            nxm_put_16m(ctx, MFF_SCTP_DST, oxm, flow->tp_dst,
991
0
                        match->wc.masks.tp_dst);
992
0
        } else if (is_icmpv4(flow, NULL)) {
993
0
            if (match->wc.masks.tp_src) {
994
0
                nxm_put_8(ctx, MFF_ICMPV4_TYPE, oxm,
995
0
                          ntohs(flow->tp_src));
996
0
            }
997
0
            if (match->wc.masks.tp_dst) {
998
0
                nxm_put_8(ctx, MFF_ICMPV4_CODE, oxm,
999
0
                          ntohs(flow->tp_dst));
1000
0
            }
1001
0
        } else if (is_icmpv6(flow, NULL)) {
1002
0
            if (match->wc.masks.tp_src) {
1003
0
                nxm_put_8(ctx, MFF_ICMPV6_TYPE, oxm,
1004
0
                          ntohs(flow->tp_src));
1005
0
            }
1006
0
            if (match->wc.masks.tp_dst) {
1007
0
                nxm_put_8(ctx, MFF_ICMPV6_CODE, oxm,
1008
0
                          ntohs(flow->tp_dst));
1009
0
            }
1010
0
            if (is_nd(flow, NULL)) {
1011
0
                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
0
                nxm_put_ipv6(ctx, MFF_ND_TARGET, oxm,
1016
0
                             &flow->nd_target, &match->wc.masks.nd_target);
1017
0
                if (match->wc.masks.tcp_flags) {
1018
0
                   nxm_put_8(ctx, MFF_ND_OPTIONS_TYPE, oxm,
1019
0
                             ntohs(flow->tcp_flags));
1020
0
                }
1021
0
                if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
1022
0
                    nxm_put_eth_masked(ctx, MFF_ND_SLL, oxm,
1023
0
                                       flow->arp_sha, match->wc.masks.arp_sha);
1024
0
                }
1025
0
                if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1026
0
                    nxm_put_eth_masked(ctx, MFF_ND_TLL, oxm,
1027
0
                                       flow->arp_tha, match->wc.masks.arp_tha);
1028
0
                }
1029
0
            }
1030
0
        }
1031
0
    }
1032
0
}
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
0
{
1051
0
    const struct flow *flow = &match->flow;
1052
0
    const size_t start_len = b->size;
1053
0
    ovs_be16 dl_type = get_dl_type(flow);
1054
0
    ovs_be32 spi_mask;
1055
0
    int match_len;
1056
1057
0
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 42);
1058
1059
0
    struct nxm_put_ctx ctx = { .output = b, .implied_ethernet = false };
1060
1061
    /* OpenFlow Packet Type. Must be first. */
1062
0
    if (match->wc.masks.packet_type && !match_has_default_packet_type(match)) {
1063
0
        nxm_put_32m(&ctx, MFF_PACKET_TYPE, oxm, flow->packet_type,
1064
0
                    match->wc.masks.packet_type);
1065
0
    }
1066
1067
    /* Metadata. */
1068
0
    if (match->wc.masks.dp_hash) {
1069
0
        nxm_put_32m(&ctx, MFF_DP_HASH, oxm,
1070
0
                    htonl(flow->dp_hash), htonl(match->wc.masks.dp_hash));
1071
0
    }
1072
1073
0
    if (match->wc.masks.recirc_id) {
1074
0
        nxm_put_32(&ctx, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id));
1075
0
    }
1076
1077
0
    if (match->wc.masks.conj_id) {
1078
0
        nxm_put_32(&ctx, MFF_CONJ_ID, oxm, htonl(flow->conj_id));
1079
0
    }
1080
1081
0
    if (match->wc.masks.in_port.ofp_port) {
1082
0
        ofp_port_t in_port = flow->in_port.ofp_port;
1083
0
        if (oxm) {
1084
0
            nxm_put_32(&ctx, MFF_IN_PORT_OXM, oxm,
1085
0
                       ofputil_port_to_ofp11(in_port));
1086
0
        } else {
1087
0
            nxm_put_16(&ctx, MFF_IN_PORT, oxm,
1088
0
                       htons(ofp_to_u16(in_port)));
1089
0
        }
1090
0
    }
1091
0
    if (match->wc.masks.actset_output) {
1092
0
        nxm_put_32(&ctx, MFF_ACTSET_OUTPUT, oxm,
1093
0
                   ofputil_port_to_ofp11(flow->actset_output));
1094
0
    }
1095
1096
    /* Ethernet. */
1097
0
    nxm_put_eth_masked(&ctx, MFF_ETH_SRC, oxm,
1098
0
                       flow->dl_src, match->wc.masks.dl_src);
1099
0
    nxm_put_eth_masked(&ctx, MFF_ETH_DST, oxm,
1100
0
                       flow->dl_dst, match->wc.masks.dl_dst);
1101
0
    nxm_put_16m(&ctx, MFF_ETH_TYPE, oxm,
1102
0
                ofputil_dl_type_to_openflow(flow->dl_type),
1103
0
                match->wc.masks.dl_type);
1104
1105
    /* 802.1Q. */
1106
0
    if (oxm) {
1107
0
        ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
1108
0
        ovs_be16 vid = flow->vlans[0].tci & VID_CFI_MASK;
1109
0
        ovs_be16 mask = match->wc.masks.vlans[0].tci & VID_CFI_MASK;
1110
1111
0
        if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
1112
0
            nxm_put_16(&ctx, MFF_VLAN_VID, oxm, vid);
1113
0
        } else if (mask) {
1114
0
            nxm_put_16m(&ctx, MFF_VLAN_VID, oxm, vid, mask);
1115
0
        }
1116
1117
0
        if (vid && vlan_tci_to_pcp(match->wc.masks.vlans[0].tci)) {
1118
0
            nxm_put_8(&ctx, MFF_VLAN_PCP, oxm,
1119
0
                      vlan_tci_to_pcp(flow->vlans[0].tci));
1120
0
        }
1121
1122
0
    } else {
1123
0
        nxm_put_16m(&ctx, MFF_VLAN_TCI, oxm, flow->vlans[0].tci,
1124
0
                    match->wc.masks.vlans[0].tci);
1125
0
    }
1126
1127
    /* MPLS. */
1128
0
    if (eth_type_mpls(dl_type)) {
1129
0
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1130
0
            nxm_put_8(&ctx, MFF_MPLS_TC, oxm,
1131
0
                      mpls_lse_to_tc(flow->mpls_lse[0]));
1132
0
        }
1133
1134
0
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1135
0
            nxm_put_8(&ctx, MFF_MPLS_BOS, oxm,
1136
0
                      mpls_lse_to_bos(flow->mpls_lse[0]));
1137
0
        }
1138
1139
0
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1140
0
            nxm_put_8(&ctx, MFF_MPLS_TTL, oxm,
1141
0
                      mpls_lse_to_ttl(flow->mpls_lse[0]));
1142
0
        }
1143
1144
0
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1145
0
            nxm_put_32(&ctx, MFF_MPLS_LABEL, oxm,
1146
0
                       htonl(mpls_lse_to_label(flow->mpls_lse[0])));
1147
0
        }
1148
0
    }
1149
1150
    /* L3. */
1151
0
    if (is_ip_any(flow)) {
1152
0
        nxm_put_ip(&ctx, match, oxm);
1153
0
    } else if (dl_type == htons(ETH_TYPE_ARP) ||
1154
0
               dl_type == htons(ETH_TYPE_RARP)) {
1155
        /* ARP. */
1156
0
        if (match->wc.masks.nw_proto) {
1157
0
            nxm_put_16(&ctx, MFF_ARP_OP, oxm,
1158
0
                       htons(flow->nw_proto));
1159
0
        }
1160
0
        nxm_put_32m(&ctx, MFF_ARP_SPA, oxm,
1161
0
                    flow->nw_src, match->wc.masks.nw_src);
1162
0
        nxm_put_32m(&ctx, MFF_ARP_TPA, oxm,
1163
0
                    flow->nw_dst, match->wc.masks.nw_dst);
1164
0
        nxm_put_eth_masked(&ctx, MFF_ARP_SHA, oxm,
1165
0
                           flow->arp_sha, match->wc.masks.arp_sha);
1166
0
        nxm_put_eth_masked(&ctx, MFF_ARP_THA, oxm,
1167
0
                           flow->arp_tha, match->wc.masks.arp_tha);
1168
0
    }
1169
1170
    /* Tunnel ID. */
1171
0
    nxm_put_64m(&ctx, MFF_TUN_ID, oxm,
1172
0
                flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
1173
1174
    /* Other tunnel metadata. */
1175
0
    nxm_put_16m(&ctx, MFF_TUN_FLAGS, oxm,
1176
0
                htons(flow->tunnel.flags), htons(match->wc.masks.tunnel.flags));
1177
0
    nxm_put_32m(&ctx, MFF_TUN_SRC, oxm,
1178
0
                flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
1179
0
    nxm_put_32m(&ctx, MFF_TUN_DST, oxm,
1180
0
                flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
1181
0
    nxm_put_ipv6(&ctx, MFF_TUN_IPV6_SRC, oxm,
1182
0
                 &flow->tunnel.ipv6_src, &match->wc.masks.tunnel.ipv6_src);
1183
0
    nxm_put_ipv6(&ctx, MFF_TUN_IPV6_DST, oxm,
1184
0
                 &flow->tunnel.ipv6_dst, &match->wc.masks.tunnel.ipv6_dst);
1185
0
    nxm_put_16m(&ctx, MFF_TUN_GBP_ID, oxm,
1186
0
                flow->tunnel.gbp_id, match->wc.masks.tunnel.gbp_id);
1187
0
    nxm_put_8m(&ctx, MFF_TUN_GBP_FLAGS, oxm,
1188
0
               flow->tunnel.gbp_flags, match->wc.masks.tunnel.gbp_flags);
1189
0
    tun_metadata_to_nx_match(b, oxm, match);
1190
1191
    /* ERSPAN */
1192
0
    nxm_put_32m(&ctx, MFF_TUN_ERSPAN_IDX, oxm,
1193
0
                htonl(flow->tunnel.erspan_idx),
1194
0
                htonl(match->wc.masks.tunnel.erspan_idx));
1195
0
    nxm_put_8m(&ctx, MFF_TUN_ERSPAN_VER, oxm,
1196
0
                flow->tunnel.erspan_ver, match->wc.masks.tunnel.erspan_ver);
1197
0
    nxm_put_8m(&ctx, MFF_TUN_ERSPAN_DIR, oxm,
1198
0
                flow->tunnel.erspan_dir, match->wc.masks.tunnel.erspan_dir);
1199
0
    nxm_put_8m(&ctx, MFF_TUN_ERSPAN_HWID, oxm,
1200
0
                flow->tunnel.erspan_hwid, match->wc.masks.tunnel.erspan_hwid);
1201
1202
    /* GTP-U */
1203
0
    nxm_put_8m(&ctx, MFF_TUN_GTPU_FLAGS, oxm, flow->tunnel.gtpu_flags,
1204
0
               match->wc.masks.tunnel.gtpu_flags);
1205
0
    nxm_put_8m(&ctx, MFF_TUN_GTPU_MSGTYPE, oxm, flow->tunnel.gtpu_msgtype,
1206
0
               match->wc.masks.tunnel.gtpu_msgtype);
1207
1208
    /* Network Service Header */
1209
0
    nxm_put_8m(&ctx, MFF_NSH_FLAGS, oxm, flow->nsh.flags,
1210
0
            match->wc.masks.nsh.flags);
1211
0
    nxm_put_8m(&ctx, MFF_NSH_TTL, oxm, flow->nsh.ttl,
1212
0
            match->wc.masks.nsh.ttl);
1213
0
    nxm_put_8m(&ctx, MFF_NSH_MDTYPE, oxm, flow->nsh.mdtype,
1214
0
            match->wc.masks.nsh.mdtype);
1215
0
    nxm_put_8m(&ctx, MFF_NSH_NP, oxm, flow->nsh.np,
1216
0
            match->wc.masks.nsh.np);
1217
0
    spi_mask = nsh_path_hdr_to_spi(match->wc.masks.nsh.path_hdr);
1218
0
    if (spi_mask == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
1219
0
        spi_mask = OVS_BE32_MAX;
1220
0
    }
1221
0
    nxm_put_32m(&ctx, MFF_NSH_SPI, oxm,
1222
0
                nsh_path_hdr_to_spi(flow->nsh.path_hdr),
1223
0
                spi_mask);
1224
0
    nxm_put_8m(&ctx, MFF_NSH_SI, oxm,
1225
0
                nsh_path_hdr_to_si(flow->nsh.path_hdr),
1226
0
                nsh_path_hdr_to_si(match->wc.masks.nsh.path_hdr));
1227
0
    for (int i = 0; i < 4; i++) {
1228
0
        nxm_put_32m(&ctx, MFF_NSH_C1 + i, oxm, flow->nsh.context[i],
1229
0
                    match->wc.masks.nsh.context[i]);
1230
0
    }
1231
1232
    /* Registers. */
1233
0
    if (oxm < OFP15_VERSION) {
1234
0
        for (int i = 0; i < FLOW_N_REGS; i++) {
1235
0
            nxm_put_32m(&ctx, MFF_REG0 + i, oxm,
1236
0
                        htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
1237
0
        }
1238
0
    } 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
0
    nxm_put_32m(&ctx, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark),
1248
0
                htonl(match->wc.masks.pkt_mark));
1249
1250
    /* Connection tracking. */
1251
0
    nxm_put_32m(&ctx, MFF_CT_STATE, oxm, htonl(flow->ct_state),
1252
0
                htonl(match->wc.masks.ct_state));
1253
0
    nxm_put_16m(&ctx, MFF_CT_ZONE, oxm, htons(flow->ct_zone),
1254
0
                htons(match->wc.masks.ct_zone));
1255
0
    nxm_put_32m(&ctx, MFF_CT_MARK, oxm, htonl(flow->ct_mark),
1256
0
                htonl(match->wc.masks.ct_mark));
1257
0
    nxm_put_128m(&ctx, MFF_CT_LABEL, oxm, hton128(flow->ct_label),
1258
0
                 hton128(match->wc.masks.ct_label));
1259
0
    nxm_put_32m(&ctx, MFF_CT_NW_SRC, oxm,
1260
0
                flow->ct_nw_src, match->wc.masks.ct_nw_src);
1261
0
    nxm_put_ipv6(&ctx, MFF_CT_IPV6_SRC, oxm,
1262
0
                 &flow->ct_ipv6_src, &match->wc.masks.ct_ipv6_src);
1263
0
    nxm_put_32m(&ctx, MFF_CT_NW_DST, oxm,
1264
0
                flow->ct_nw_dst, match->wc.masks.ct_nw_dst);
1265
0
    nxm_put_ipv6(&ctx, MFF_CT_IPV6_DST, oxm,
1266
0
                 &flow->ct_ipv6_dst, &match->wc.masks.ct_ipv6_dst);
1267
0
    if (flow->ct_nw_proto) {
1268
0
        nxm_put_8m(&ctx, MFF_CT_NW_PROTO, oxm, flow->ct_nw_proto,
1269
0
                   match->wc.masks.ct_nw_proto);
1270
0
        nxm_put_16m(&ctx, MFF_CT_TP_SRC, oxm,
1271
0
                    flow->ct_tp_src, match->wc.masks.ct_tp_src);
1272
0
        nxm_put_16m(&ctx, MFF_CT_TP_DST, oxm,
1273
0
                    flow->ct_tp_dst, match->wc.masks.ct_tp_dst);
1274
0
    }
1275
    /* OpenFlow 1.1+ Metadata. */
1276
0
    nxm_put_64m(&ctx, MFF_METADATA, oxm,
1277
0
                flow->metadata, match->wc.masks.metadata);
1278
1279
    /* Cookie. */
1280
0
    if (cookie_mask) {
1281
0
        bool masked = cookie_mask != OVS_BE64_MAX;
1282
1283
0
        cookie &= cookie_mask;
1284
0
        nx_put_header__(b, NXM_NX_COOKIE, masked);
1285
0
        ofpbuf_put(b, &cookie, sizeof cookie);
1286
0
        if (masked) {
1287
0
            ofpbuf_put(b, &cookie_mask, sizeof cookie_mask);
1288
0
        }
1289
0
    }
1290
1291
0
    if (match_has_default_packet_type(match) && !ctx.implied_ethernet) {
1292
0
        uint64_t pt_stub[16 / 8];
1293
0
        struct ofpbuf pt;
1294
0
        ofpbuf_use_stack(&pt, pt_stub, sizeof pt_stub);
1295
0
        nxm_put_entry_raw(&pt, MFF_PACKET_TYPE, oxm, &flow->packet_type,
1296
0
                          NULL, sizeof flow->packet_type);
1297
1298
0
        ofpbuf_insert(b, start_len, pt.data, pt.size);
1299
0
    }
1300
1301
0
    match_len = b->size - start_len;
1302
0
    return match_len;
1303
0
}
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
0
{
1319
0
    int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask);
1320
1321
0
    ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1322
0
    return match_len;
1323
0
}
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
0
{
1340
0
    int match_len;
1341
0
    struct ofp11_match_header *omh;
1342
0
    size_t start_len = b->size;
1343
0
    ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
1344
1345
0
    ofpbuf_put_uninit(b, sizeof *omh);
1346
0
    match_len = (nx_put_raw(b, version, match, cookie, cookie_mask)
1347
0
                 + sizeof *omh);
1348
0
    ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1349
1350
0
    omh = ofpbuf_at(b, start_len, sizeof *omh);
1351
0
    omh->type = htons(OFPMT_OXM);
1352
0
    omh->length = htons(match_len);
1353
1354
0
    return match_len;
1355
0
}
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
5.13k
{
1378
5.13k
    const struct mf_field *mf = mf_from_id(id);
1379
1380
5.13k
    ds_put_format(ds, "%s", mf->name);
1381
1382
5.13k
    if (!is_all_ones(mask, mf->n_bytes)) {
1383
5.04k
        ds_put_char(ds, '=');
1384
5.04k
        mf_format(mf, mask, NULL, NULL, ds);
1385
5.04k
    }
1386
1387
5.13k
    ds_put_char(ds, ',');
1388
5.13k
}
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
2.40k
{
1396
2.40k
    size_t start_len = ds->length;
1397
2.40k
    size_t i, offset = 0;
1398
1399
5.13k
    BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fa->used.bm) {
1400
5.13k
        const struct mf_field *mf = mf_from_id(i);
1401
5.13k
        union mf_value value;
1402
1403
5.13k
        memcpy(&value, fa->values + offset, mf->n_bytes);
1404
5.13k
        nx_format_mask_tlv(ds, i, &value);
1405
5.13k
        offset += mf->n_bytes;
1406
5.13k
    }
1407
1408
2.40k
    if (ds->length > start_len) {
1409
2.40k
        ds_chomp(ds, ',');
1410
2.40k
    }
1411
2.40k
}
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
0
{
1456
0
    uint64_t masked_header = masked ? nxm_make_wild_header(header) : header;
1457
0
    ovs_be64 network_header = htonll(masked_header);
1458
1459
0
    ofpbuf_put(b, &network_header, nxm_header_len(header));
1460
0
}
1461
1462
void
1463
nx_put_header(struct ofpbuf *b, enum mf_field_id field,
1464
              enum ofp_version version, bool masked)
1465
0
{
1466
0
    nx_put_header__(b, mf_oxm_header(field, version), masked);
1467
0
}
1468
1469
void nx_put_mff_header(struct ofpbuf *b, const struct mf_field *mff,
1470
                       enum ofp_version version, bool masked)
1471
0
{
1472
0
    if (mff->mapped) {
1473
0
        nx_put_header_len(b, mff->id, version, masked, mff->n_bytes);
1474
0
    } else {
1475
0
        nx_put_header(b, mff->id, version, masked);
1476
0
    }
1477
0
}
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
0
{
1483
0
    uint64_t header = mf_oxm_header(field, version);
1484
1485
0
    header = NXM_HEADER(nxm_vendor(header), nxm_class(header),
1486
0
                        nxm_field(header), false,
1487
0
                        nxm_experimenter_len(header) + n_bytes);
1488
1489
0
    nx_put_header__(b, header, masked);
1490
0
}
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
0
{
1497
0
    bool masked;
1498
0
    int len, offset;
1499
1500
0
    len = mf_field_len(mff, value, mask, &masked);
1501
0
    offset = mff->n_bytes - len;
1502
1503
0
    nxm_put_entry_raw(b, mff->id, version,
1504
0
                      &value->u8 + offset, masked ? &mask->u8 + offset : NULL,
1505
0
                      len);
1506
0
}
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
0
{
1793
0
    const char *full_s = s;
1794
0
    char *error;
1795
1796
0
    error = mf_parse_subfield__(&move->src, &s);
1797
0
    if (error) {
1798
0
        return error;
1799
0
    }
1800
0
    if (strncmp(s, "->", 2)) {
1801
0
        return xasprintf("%s: missing `->' following source", full_s);
1802
0
    }
1803
0
    s += 2;
1804
0
    error = mf_parse_subfield(&move->dst, s);
1805
0
    if (error) {
1806
0
        return error;
1807
0
    }
1808
1809
0
    if (move->src.n_bits != move->dst.n_bits) {
1810
0
        return xasprintf("%s: source field is %d bits wide but destination is "
1811
0
                         "%d bits wide", full_s,
1812
0
                         move->src.n_bits, move->dst.n_bits);
1813
0
    }
1814
0
    return NULL;
1815
0
}
1816

1817
/* nxm_format_reg_move(). */
1818
1819
void
1820
nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1821
123
{
1822
123
    ds_put_format(s, "%smove:%s", colors.special, colors.end);
1823
123
    mf_format_subfield(&move->src, s);
1824
123
    ds_put_format(s, "%s->%s", colors.special, colors.end);
1825
123
    mf_format_subfield(&move->dst, s);
1826
123
}
1827
1828

1829
enum ofperr
1830
nxm_reg_move_check(const struct ofpact_reg_move *move,
1831
                   const struct match *match)
1832
268
{
1833
268
    enum ofperr error;
1834
1835
268
    error = mf_check_src(&move->src, match);
1836
268
    if (error) {
1837
109
        return error;
1838
109
    }
1839
1840
159
    return mf_check_dst(&move->dst, match);
1841
268
}
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
0
    memset(&mask_value, 0xff, sizeof mask_value);
1854
0
    mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1855
1856
0
    bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1857
0
                 &src_subvalue, sizeof src_subvalue, 0,
1858
0
                 sizeof src_data_be * 8);
1859
0
    mf_write_subfield_flow(dst, &src_subvalue, flow);
1860
0
}
1861

1862
/* nxm_parse_stack_action, works for both push() and pop(). */
1863
1864
/* Parses 's' as a "push" or "pop" action, in the form described in
1865
 * ovs-actions(7), into '*stack_action'.
1866
 *
1867
 * Returns NULL if successful, otherwise a malloc()'d string describing the
1868
 * error.  The caller is responsible for freeing the returned string. */
1869
char * OVS_WARN_UNUSED_RESULT
1870
nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1871
0
{
1872
0
    char *error;
1873
1874
0
    error = mf_parse_subfield__(&stack_action->subfield, &s);
1875
0
    if (error) {
1876
0
        return error;
1877
0
    }
1878
1879
0
    if (*s != '\0') {
1880
0
        return xasprintf("%s: trailing garbage following push or pop", s);
1881
0
    }
1882
1883
0
    return NULL;
1884
0
}
1885
1886
void
1887
nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1888
15
{
1889
15
    ds_put_format(s, "%spush:%s", colors.param, colors.end);
1890
15
    mf_format_subfield(&push->subfield, s);
1891
15
}
1892
1893
void
1894
nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1895
420
{
1896
420
    ds_put_format(s, "%spop:%s", colors.param, colors.end);
1897
420
    mf_format_subfield(&pop->subfield, s);
1898
420
}
1899
1900
enum ofperr
1901
nxm_stack_push_check(const struct ofpact_stack *push,
1902
                     const struct match *match)
1903
585
{
1904
585
    return mf_check_src(&push->subfield, match);
1905
585
}
1906
1907
enum ofperr
1908
nxm_stack_pop_check(const struct ofpact_stack *pop,
1909
                    const struct match *match)
1910
1.87k
{
1911
1.87k
    return mf_check_dst(&pop->subfield, match);
1912
1.87k
}
1913
1914
/* nxm_execute_stack_push(), nxm_execute_stack_pop().
1915
 *
1916
 * A stack is an ofpbuf with 'data' pointing to the bottom of the stack and
1917
 * 'size' indexing the top of the stack.  Each value of some byte length is
1918
 * stored to the stack immediately followed by the length of the value as an
1919
 * unsigned byte.  This way a POP operation can first read the length byte, and
1920
 * then the appropriate number of bytes from the stack.  This also means that
1921
 * it is only possible to traverse the stack from top to bottom.  It is
1922
 * possible, however, to push values also to the bottom of the stack, which is
1923
 * useful when a stack has been serialized to a wire format in reverse order
1924
 * (topmost value first).
1925
 */
1926
1927
/* Push value 'v' of length 'bytes' to the top of 'stack'. */
1928
void
1929
nx_stack_push(struct ofpbuf *stack, const void *v, uint8_t bytes)
1930
0
{
1931
0
    ofpbuf_put(stack, v, bytes);
1932
0
    ofpbuf_put(stack, &bytes, sizeof bytes);
1933
0
}
1934
1935
/* Push value 'v' of length 'bytes' to the bottom of 'stack'. */
1936
void
1937
nx_stack_push_bottom(struct ofpbuf *stack, const void *v, uint8_t bytes)
1938
3.42k
{
1939
3.42k
    ofpbuf_push(stack, &bytes, sizeof bytes);
1940
3.42k
    ofpbuf_push(stack, v, bytes);
1941
3.42k
}
1942
1943
/* Pop the topmost value from 'stack', returning a pointer to the value in the
1944
 * stack and the length of the value in '*bytes'.  In case of underflow a NULL
1945
 * is returned and length is returned as zero via '*bytes'. */
1946
void *
1947
nx_stack_pop(struct ofpbuf *stack, uint8_t *bytes)
1948
3.06k
{
1949
3.06k
    if (!stack->size) {
1950
0
        *bytes = 0;
1951
0
        return NULL;
1952
0
    }
1953
1954
3.06k
    stack->size -= sizeof *bytes;
1955
3.06k
    memcpy(bytes, ofpbuf_tail(stack), sizeof *bytes);
1956
1957
3.06k
    ovs_assert(stack->size >= *bytes);
1958
3.06k
    stack->size -= *bytes;
1959
3.06k
    return ofpbuf_tail(stack);
1960
3.06k
}
1961
1962
void
1963
nxm_execute_stack_push(const struct ofpact_stack *push,
1964
                       const struct flow *flow, struct flow_wildcards *wc,
1965
                       struct ofpbuf *stack)
1966
0
{
1967
0
    union mf_subvalue dst_value;
1968
1969
0
    mf_write_subfield_flow(&push->subfield,
1970
0
                           (union mf_subvalue *)&exact_match_mask,
1971
0
                           &wc->masks);
1972
1973
0
    mf_read_subfield(&push->subfield, flow, &dst_value);
1974
0
    uint8_t bytes = DIV_ROUND_UP(push->subfield.n_bits, 8);
1975
0
    nx_stack_push(stack, &dst_value.u8[sizeof dst_value - bytes], bytes);
1976
0
}
1977
1978
bool
1979
nxm_execute_stack_pop(const struct ofpact_stack *pop,
1980
                      struct flow *flow, struct flow_wildcards *wc,
1981
                      struct ofpbuf *stack)
1982
0
{
1983
0
    uint8_t src_bytes;
1984
0
    const void *src = nx_stack_pop(stack, &src_bytes);
1985
0
    if (src) {
1986
0
        union mf_subvalue src_value;
1987
0
        uint8_t dst_bytes = DIV_ROUND_UP(pop->subfield.n_bits, 8);
1988
1989
0
        if (src_bytes < dst_bytes) {
1990
0
            memset(&src_value.u8[sizeof src_value - dst_bytes], 0,
1991
0
                   dst_bytes - src_bytes);
1992
0
        }
1993
0
        memcpy(&src_value.u8[sizeof src_value - src_bytes], src, src_bytes);
1994
0
        mf_write_subfield_flow(&pop->subfield,
1995
0
                               (union mf_subvalue *)&exact_match_mask,
1996
0
                               &wc->masks);
1997
0
        mf_write_subfield_flow(&pop->subfield, &src_value, flow);
1998
0
        return true;
1999
0
    } else {
2000
        /* Attempted to pop from an empty stack. */
2001
0
        return false;
2002
0
    }
2003
0
}
2004

2005
/* Parses a field from '*s' into '*field'.  If successful, stores the
2006
 * reference to the field in '*field', and returns NULL.  On failure,
2007
 * returns a malloc()'ed error message.
2008
 */
2009
char * OVS_WARN_UNUSED_RESULT
2010
mf_parse_field(const struct mf_field **field, const char *s)
2011
0
{
2012
0
    const struct nxm_field *f;
2013
0
    int s_len = strlen(s);
2014
2015
0
    f = nxm_field_by_name(s, s_len);
2016
0
    (*field) = f ? mf_from_id(f->id) : mf_from_name_len(s, s_len);
2017
0
    if (!*field) {
2018
0
        return xasprintf("unknown field `%s'", s);
2019
0
    }
2020
0
    return NULL;
2021
0
}
2022

2023
/* Formats 'sf' into 's' in a format normally acceptable to
2024
 * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2025
 * sf->field has no NXM name.) */
2026
void
2027
mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2028
30.8k
{
2029
30.8k
    if (!sf->field) {
2030
0
        ds_put_cstr(s, "<unknown>");
2031
30.8k
    } else {
2032
30.8k
        const struct nxm_field *f = nxm_field_by_mf_id(sf->field->id, 0);
2033
30.8k
        ds_put_cstr(s, f ? f->name : sf->field->name);
2034
30.8k
    }
2035
2036
30.8k
    if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2037
2.98k
        ds_put_cstr(s, "[]");
2038
27.9k
    } else if (sf->n_bits == 1) {
2039
2.54k
        ds_put_format(s, "[%d]", sf->ofs);
2040
25.3k
    } else {
2041
25.3k
        ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2042
25.3k
    }
2043
30.8k
}
2044
2045
static const struct nxm_field *
2046
mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2047
0
{
2048
0
    *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2049
0
    if (*wild) {
2050
0
        name_len -= 2;
2051
0
    }
2052
2053
0
    return nxm_field_by_name(name, name_len);
2054
0
}
2055
2056
/* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2057
 * returns NULL and advances '*sp' to the first byte following the parsed
2058
 * string.  On failure, returns a malloc()'d error message, does not modify
2059
 * '*sp', and does not properly initialize 'sf'.
2060
 *
2061
 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2062
 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2063
 * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2064
 * may both be omitted (the [] are still required) to indicate an entire
2065
 * field. */
2066
char * OVS_WARN_UNUSED_RESULT
2067
mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2068
0
{
2069
0
    const struct mf_field *field = NULL;
2070
0
    const struct nxm_field *f;
2071
0
    const char *name;
2072
0
    int start, end;
2073
0
    const char *s;
2074
0
    int name_len;
2075
0
    bool wild;
2076
2077
0
    s = *sp;
2078
0
    name = s;
2079
0
    name_len = strcspn(s, "[-");
2080
2081
0
    f = mf_parse_subfield_name(name, name_len, &wild);
2082
0
    field = f ? mf_from_id(f->id) : mf_from_name_len(name, name_len);
2083
0
    if (!field) {
2084
0
        return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2085
0
    }
2086
2087
0
    s += name_len;
2088
    /* Assume full field. */
2089
0
    start = 0;
2090
0
    end = field->n_bits - 1;
2091
0
    if (*s == '[') {
2092
0
        if (!strncmp(s, "[]", 2)) {
2093
            /* Nothing to do. */
2094
0
        } else if (ovs_scan(s, "[%d..%d]", &start, &end)) {
2095
            /* Nothing to do. */
2096
0
        } else if (ovs_scan(s, "[%d]", &start)) {
2097
0
            end = start;
2098
0
        } else {
2099
0
            return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2100
0
                             "[<start>..<end>]", *sp);
2101
0
        }
2102
0
        s = strchr(s, ']') + 1;
2103
0
    }
2104
2105
0
    if (start > end) {
2106
0
        return xasprintf("%s: starting bit %d is after ending bit %d",
2107
0
                         *sp, start, end);
2108
0
    } else if (start >= field->n_bits) {
2109
0
        return xasprintf("%s: starting bit %d is not valid because field is "
2110
0
                         "only %d bits wide", *sp, start, field->n_bits);
2111
0
    } else if (end >= field->n_bits){
2112
0
        return xasprintf("%s: ending bit %d is not valid because field is "
2113
0
                         "only %d bits wide", *sp, end, field->n_bits);
2114
0
    }
2115
2116
0
    sf->field = field;
2117
0
    sf->ofs = start;
2118
0
    sf->n_bits = end - start + 1;
2119
2120
0
    *sp = s;
2121
0
    return NULL;
2122
0
}
2123
2124
/* Parses a subfield from the entirety of 's' into 'sf'.  Returns NULL if
2125
 * successful, otherwise a malloc()'d string describing the error.  The caller
2126
 * is responsible for freeing the returned string.
2127
 *
2128
 * The syntax parsed from 's' takes the form "header[start..end]" where
2129
 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2130
 * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2131
 * may both be omitted (the [] are still required) to indicate an entire
2132
 * field.  */
2133
char * OVS_WARN_UNUSED_RESULT
2134
mf_parse_subfield(struct mf_subfield *sf, const char *s)
2135
0
{
2136
0
    char *error = mf_parse_subfield__(sf, &s);
2137
0
    if (!error && s[0]) {
2138
0
        error = xstrdup("unexpected input following field syntax");
2139
0
    }
2140
0
    return error;
2141
0
}
2142

2143
/* Returns an bitmap in which each bit corresponds to the like-numbered field
2144
 * in the OFPXMC12_OPENFLOW_BASIC OXM class, in which the bit values are taken
2145
 * from the 'fields' bitmap.  Only fields defined in OpenFlow 'version' are
2146
 * considered.
2147
 *
2148
 * This is useful for encoding OpenFlow 1.2 table stats messages. */
2149
ovs_be64
2150
oxm_bitmap_from_mf_bitmap(const struct mf_bitmap *fields,
2151
                          enum ofp_version version)
2152
0
{
2153
0
    uint64_t oxm_bitmap = 0;
2154
0
    enum mf_field_id id;
2155
2156
0
    BITMAP_FOR_EACH_1 (id, MFF_N_IDS, fields->bm) {
2157
0
        uint64_t oxm = mf_oxm_header(id, version);
2158
0
        uint32_t class = nxm_class(oxm);
2159
0
        int field = nxm_field(oxm);
2160
2161
0
        if (class == OFPXMC12_OPENFLOW_BASIC && field < 64) {
2162
0
            oxm_bitmap |= UINT64_C(1) << field;
2163
0
        }
2164
0
    }
2165
0
    return htonll(oxm_bitmap);
2166
0
}
2167
2168
/* Opposite conversion from oxm_bitmap_from_mf_bitmap().
2169
 *
2170
 * This is useful for decoding OpenFlow 1.2 table stats messages. */
2171
struct mf_bitmap
2172
oxm_bitmap_to_mf_bitmap(ovs_be64 oxm_bitmap, enum ofp_version version)
2173
42.2k
{
2174
42.2k
    struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
2175
2176
7.78M
    for (enum mf_field_id id = 0; id < MFF_N_IDS; id++) {
2177
7.73M
        uint64_t oxm = mf_oxm_header(id, version);
2178
7.73M
        if (oxm && version >= nxm_field_by_header(oxm, false, NULL)->version) {
2179
7.06M
            uint32_t class = nxm_class(oxm);
2180
7.06M
            int field = nxm_field(oxm);
2181
2182
7.06M
            if (class == OFPXMC12_OPENFLOW_BASIC
2183
7.06M
                && field < 64
2184
7.06M
                && oxm_bitmap & htonll(UINT64_C(1) << field)) {
2185
409k
                bitmap_set1(fields.bm, id);
2186
409k
            }
2187
7.06M
        }
2188
7.73M
    }
2189
42.2k
    return fields;
2190
42.2k
}
2191
2192
/* Returns a bitmap of fields that can be encoded in OXM and that can be
2193
 * modified with a "set_field" action.  */
2194
struct mf_bitmap
2195
oxm_writable_fields(void)
2196
0
{
2197
0
    struct mf_bitmap b = MF_BITMAP_INITIALIZER;
2198
0
    int i;
2199
2200
0
    for (i = 0; i < MFF_N_IDS; i++) {
2201
0
        if (mf_oxm_header(i, 0) && mf_from_id(i)->writable) {
2202
0
            bitmap_set1(b.bm, i);
2203
0
        }
2204
0
    }
2205
0
    return b;
2206
0
}
2207
2208
/* Returns a bitmap of fields that can be encoded in OXM and that can be
2209
 * matched in a flow table.  */
2210
struct mf_bitmap
2211
oxm_matchable_fields(void)
2212
0
{
2213
0
    struct mf_bitmap b = MF_BITMAP_INITIALIZER;
2214
0
    int i;
2215
2216
0
    for (i = 0; i < MFF_N_IDS; i++) {
2217
0
        if (mf_oxm_header(i, 0)) {
2218
0
            bitmap_set1(b.bm, i);
2219
0
        }
2220
0
    }
2221
0
    return b;
2222
0
}
2223
2224
/* Returns a bitmap of fields that can be encoded in OXM and that can be
2225
 * matched in a flow table with an arbitrary bitmask.  */
2226
struct mf_bitmap
2227
oxm_maskable_fields(void)
2228
0
{
2229
0
    struct mf_bitmap b = MF_BITMAP_INITIALIZER;
2230
0
    int i;
2231
2232
0
    for (i = 0; i < MFF_N_IDS; i++) {
2233
0
        if (mf_oxm_header(i, 0) && mf_from_id(i)->maskable == MFM_FULLY) {
2234
0
            bitmap_set1(b.bm, i);
2235
0
        }
2236
0
    }
2237
0
    return b;
2238
0
}
2239

2240
struct nxm_field_index {
2241
    struct hmap_node header_node; /* In nxm_header_map. */
2242
    struct hmap_node name_node;   /* In nxm_name_map. */
2243
    struct ovs_list mf_node;      /* In mf_mf_map[nf.id]. */
2244
    const struct nxm_field nf;
2245
};
2246
2247
#include "nx-match.inc"
2248
2249
static struct hmap nxm_header_map;
2250
static struct hmap nxm_name_map;
2251
static struct ovs_list nxm_mf_map[MFF_N_IDS];
2252
2253
static void
2254
nxm_init(void)
2255
15.8M
{
2256
15.8M
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2257
15.8M
    if (ovsthread_once_start(&once)) {
2258
1
        hmap_init(&nxm_header_map);
2259
1
        hmap_init(&nxm_name_map);
2260
184
        for (int i = 0; i < MFF_N_IDS; i++) {
2261
183
            ovs_list_init(&nxm_mf_map[i]);
2262
183
        }
2263
1
        for (struct nxm_field_index *nfi = all_nxm_fields;
2264
210
             nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) {
2265
209
            hmap_insert(&nxm_header_map, &nfi->header_node,
2266
209
                        hash_uint64(nxm_no_len(nfi->nf.header)));
2267
209
            hmap_insert(&nxm_name_map, &nfi->name_node,
2268
209
                        hash_string(nfi->nf.name, 0));
2269
209
            ovs_list_push_back(&nxm_mf_map[nfi->nf.id], &nfi->mf_node);
2270
209
        }
2271
1
        ovsthread_once_done(&once);
2272
1
    }
2273
15.8M
}
2274
2275
2276
static const struct nxm_field *
2277
nxm_field_by_header(uint64_t header, bool is_action, enum ofperr *h_error)
2278
8.03M
{
2279
8.03M
    const struct nxm_field_index *nfi;
2280
8.03M
    uint64_t header_no_len;
2281
2282
8.03M
    nxm_init();
2283
8.03M
    if (nxm_hasmask(header)) {
2284
176k
        header = nxm_make_exact_header(header);
2285
176k
    }
2286
2287
8.03M
    header_no_len = nxm_no_len(header);
2288
2289
8.03M
    HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_uint64(header_no_len),
2290
14.4M
                             &nxm_header_map) {
2291
14.4M
        if (is_action && nxm_length(header) > 0) {
2292
17.5k
            if (nxm_length(header) != nxm_length(nfi->nf.header) && h_error ) {
2293
7.21k
               *h_error = OFPERR_OFPBAC_BAD_SET_LEN;
2294
7.21k
            }
2295
17.5k
        }
2296
14.4M
        if (header_no_len == nxm_no_len(nfi->nf.header)) {
2297
7.85M
            if (nxm_length(header) == nxm_length(nfi->nf.header) ||
2298
7.85M
                mf_from_id(nfi->nf.id)->variable_len) {
2299
7.78M
                return &nfi->nf;
2300
7.78M
            } else {
2301
67.5k
                return NULL;
2302
67.5k
            }
2303
7.85M
        }
2304
14.4M
    }
2305
180k
    return NULL;
2306
8.03M
}
2307
2308
static const struct nxm_field *
2309
nxm_field_by_name(const char *name, size_t len)
2310
0
{
2311
0
    const struct nxm_field_index *nfi;
2312
2313
0
    nxm_init();
2314
0
    HMAP_FOR_EACH_WITH_HASH (nfi, name_node, hash_bytes(name, len, 0),
2315
0
                             &nxm_name_map) {
2316
0
        if (strlen(nfi->nf.name) == len && !memcmp(nfi->nf.name, name, len)) {
2317
0
            return &nfi->nf;
2318
0
        }
2319
0
    }
2320
0
    return NULL;
2321
0
}
2322
2323
static const struct nxm_field *
2324
nxm_field_by_mf_id(enum mf_field_id id, enum ofp_version version)
2325
7.77M
{
2326
7.77M
    const struct nxm_field_index *nfi;
2327
7.77M
    const struct nxm_field *f;
2328
2329
7.77M
    nxm_init();
2330
2331
7.77M
    f = NULL;
2332
8.87M
    LIST_FOR_EACH (nfi, mf_node, &nxm_mf_map[id]) {
2333
8.87M
        if (!f || version >= nfi->nf.version) {
2334
8.70M
            f = &nfi->nf;
2335
8.70M
        }
2336
8.87M
    }
2337
7.77M
    return f;
2338
7.77M
}