Coverage Report

Created: 2026-03-02 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/nx-match.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2010-2017, 2020 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
19
#include "nx-match.h"
20
21
#include <netinet/icmp6.h>
22
23
#include "classifier.h"
24
#include "colors.h"
25
#include "openvswitch/hmap.h"
26
#include "openflow/nicira-ext.h"
27
#include "openvswitch/dynamic-string.h"
28
#include "openvswitch/meta-flow.h"
29
#include "openvswitch/ofp-actions.h"
30
#include "openvswitch/ofp-errors.h"
31
#include "openvswitch/ofp-match.h"
32
#include "openvswitch/ofp-port.h"
33
#include "openvswitch/ofpbuf.h"
34
#include "openvswitch/vlog.h"
35
#include "packets.h"
36
#include "openvswitch/shash.h"
37
#include "tun-metadata.h"
38
#include "unaligned.h"
39
#include "util.h"
40
#include "vl-mff-map.h"
41
42
VLOG_DEFINE_THIS_MODULE(nx_match);
43
44
/* OXM headers.
45
 *
46
 *
47
 * Standard OXM/NXM
48
 * ================
49
 *
50
 * The header is 32 bits long.  It looks like this:
51
 *
52
 * |31                              16 15            9| 8 7                0
53
 * +----------------------------------+---------------+--+------------------+
54
 * |            oxm_class             |   oxm_field   |hm|    oxm_length    |
55
 * +----------------------------------+---------------+--+------------------+
56
 *
57
 * where hm stands for oxm_hasmask.  It is followed by oxm_length bytes of
58
 * payload.  When oxm_hasmask is 0, the payload is the value of the field
59
 * identified by the header; when oxm_hasmask is 1, the payload is a value for
60
 * the field followed by a mask of equal length.
61
 *
62
 * Internally, we represent a standard OXM header as a 64-bit integer with the
63
 * above information in the most-significant bits.
64
 *
65
 *
66
 * Experimenter OXM
67
 * ================
68
 *
69
 * The header is 64 bits long.  It looks like the diagram above except that a
70
 * 32-bit experimenter ID, which we call oxm_vendor and which identifies a
71
 * vendor, is inserted just before the payload.  Experimenter OXMs are
72
 * identified by an all-1-bits oxm_class (OFPXMC12_EXPERIMENTER).  The
73
 * oxm_length value *includes* the experimenter ID, so that the real payload is
74
 * only oxm_length - 4 bytes long.
75
 *
76
 * Internally, we represent an experimenter OXM header as a 64-bit integer with
77
 * the standard header in the upper 32 bits and the experimenter ID in the
78
 * lower 32 bits.  (It would be more convenient to swap the positions of the
79
 * two 32-bit words, but this would be more error-prone because experimenter
80
 * OXMs are very rarely used, so accidentally passing one through a 32-bit type
81
 * somewhere in the OVS code would be hard to find.)
82
 */
83
84
/*
85
 * OXM Class IDs.
86
 * The high order bit differentiate reserved classes from member classes.
87
 * Classes 0x0000 to 0x7FFF are member classes, allocated by ONF.
88
 * Classes 0x8000 to 0xFFFE are reserved classes, reserved for standardisation.
89
 */
90
enum ofp12_oxm_class {
91
    OFPXMC12_NXM_0          = 0x0000, /* Backward compatibility with NXM */
92
    OFPXMC12_NXM_1          = 0x0001, /* Backward compatibility with NXM */
93
    OFPXMC12_OPENFLOW_BASIC = 0x8000, /* Basic class for OpenFlow */
94
    OFPXMC15_PACKET_REGS    = 0x8001, /* Packet registers (pipeline fields). */
95
    OFPXMC12_EXPERIMENTER   = 0xffff, /* Experimenter class */
96
};
97
98
/* Functions for extracting raw field values from OXM/NXM headers. */
99
163k
static uint32_t nxm_vendor(uint64_t header) { return header; }
100
15.3M
static int nxm_class(uint64_t header) { return header >> 48; }
101
12.2M
static int nxm_field(uint64_t header) { return (header >> 41) & 0x7f; }
102
14.6M
static bool nxm_hasmask(uint64_t header) { return (header >> 40) & 1; }
103
29.2M
static int nxm_length(uint64_t header) { return (header >> 32) & 0xff; }
104
39.9M
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.08M
{
109
3.08M
    return nxm_class(header) == OFPXMC12_EXPERIMENTER;
110
3.08M
}
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
2.53M
{
129
2.53M
    return is_experimenter_oxm(header) ? 4 : 0;
130
2.53M
}
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
837k
{
137
837k
    ovs_assert(nxm_length(header) >= nxm_experimenter_len(header));
138
837k
    return nxm_length(header) - nxm_experimenter_len(header);
139
837k
}
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
360k
{
146
360k
    return 4 + nxm_experimenter_len(header);
147
360k
}
148
149
#define NXM_HEADER(VENDOR, CLASS, FIELD, HASMASK, LENGTH)       \
150
164k
    (((uint64_t) (CLASS) << 48) |                               \
151
164k
     ((uint64_t) (FIELD) << 41) |                               \
152
164k
     ((uint64_t) (HASMASK) << 40) |                             \
153
164k
     ((uint64_t) (LENGTH) << 32) |                              \
154
164k
     (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
136k
{
166
136k
    unsigned int new_len = nxm_payload_len(header)
167
136k
                           / 2 + nxm_experimenter_len(header);
168
136k
    return NXM_HEADER(nxm_vendor(header), nxm_class(header),
169
136k
                      nxm_field(header), 0, new_len);
170
136k
}
171
static uint64_t
172
nxm_make_wild_header(uint64_t header)
173
9.08k
{
174
9.08k
    unsigned int new_len = nxm_payload_len(header) * 2
175
9.08k
                           + nxm_experimenter_len(header);
176
9.08k
    return NXM_HEADER(nxm_vendor(header), nxm_class(header),
177
9.08k
                      nxm_field(header), 1, new_len);
178
9.08k
}
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
685
#define NXM_NX_COOKIE     NXM_HEADER  (0, 0x0001, 30, 0, 8)
188
583
#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
13.8M
{
222
13.8M
    const struct nxm_field *f = nxm_field_by_mf_id(id, version);
223
13.8M
    return f ? f->header : 0;
224
13.8M
}
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
72.6k
{
235
72.6k
    uint64_t oxm = mf_oxm_header(id, 0);
236
72.6k
    return is_experimenter_oxm(oxm) ? 0 : oxm >> 32;
237
72.6k
}
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
134k
{
246
134k
    uint64_t oxm = mf_oxm_header(mff->id, 0);
247
248
134k
    if (mff->mapped) {
249
0
        oxm = nxm_no_len(oxm) | ((uint64_t) mff->n_bytes << 32);
250
0
    }
251
252
134k
    return is_experimenter_oxm(oxm) ? 0 : oxm >> 32;
253
134k
}
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
384k
{
259
384k
    const struct nxm_field *f = nxm_field_by_header(header, is_action, h_error);
260
261
384k
    if (f) {
262
236k
        const struct mf_field *mff = mf_from_id(f->id);
263
236k
        const struct mf_field *vl_mff = mf_get_vl_mff(mff, vl_mff_map);
264
236k
        return vl_mff ? vl_mff : mff;
265
236k
    } else {
266
148k
        return NULL;
267
148k
    }
268
384k
}
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
45.9k
{
275
45.9k
    return mf_from_oxm_header((uint64_t) header << 32, vl_mff_map, false, NULL);
276
45.9k
}
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
389k
{
283
389k
    unsigned int length = nxm_payload_len(header);
284
389k
    return nxm_hasmask(header) ? length / 2 : length;
285
389k
}
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
106k
{
295
106k
    unsigned int width = nxm_field_bytes(header);
296
106k
    unsigned int i;
297
298
469k
    for (i = 0; i < width; i++) {
299
366k
        if (value[i] & ~mask[i]) {
300
3.90k
            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
3.90k
            return false;
306
3.90k
        }
307
366k
    }
308
103k
    return true;
309
106k
}
310
311
static bool
312
is_cookie_pseudoheader(uint64_t header)
313
313
{
314
313
    return header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W;
315
313
}
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
348k
{
322
348k
    if (b->size < 4) {
323
8.29k
        goto bad_len;
324
8.29k
    }
325
326
340k
    *header = ((uint64_t) ntohl(get_unaligned_be32(b->data))) << 32;
327
340k
    if (is_experimenter_oxm(*header)) {
328
12.7k
        if (b->size < 8) {
329
1.13k
            goto bad_len;
330
1.13k
        }
331
11.6k
        *header = ntohll(get_unaligned_be64(b->data));
332
11.6k
    }
333
339k
    if (nxm_length(*header) < nxm_experimenter_len(*header)) {
334
692
        VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has invalid length %d "
335
692
                     "(minimum is %d)",
336
692
                     NXM_HEADER_ARGS(*header), nxm_length(*header),
337
692
                     nxm_header_len(*header));
338
692
        goto error;
339
692
    }
340
338k
    ofpbuf_pull(b, nxm_header_len(*header));
341
342
338k
    if (field) {
343
338k
        enum ofperr h_error = 0;
344
338k
        *field = mf_from_oxm_header(*header, vl_mff_map, is_action, &h_error);
345
338k
        if (!*field && !(allow_cookie && is_cookie_pseudoheader(*header))) {
346
144k
            VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" is unknown",
347
144k
                        NXM_HEADER_ARGS(*header));
348
144k
            if (is_action) {
349
867
                if (h_error) {
350
685
                     *field = NULL;
351
685
                     return h_error;
352
685
                }
353
182
                return OFPERR_OFPBAC_BAD_SET_TYPE;
354
143k
            } else {
355
143k
                return OFPERR_OFPBMC_BAD_FIELD;
356
143k
            }
357
194k
        } else if (mf_vl_mff_invalid(*field, vl_mff_map)) {
358
0
            return OFPERR_NXFMFC_INVALID_TLV_FIELD;
359
0
        }
360
338k
    }
361
362
194k
    return 0;
363
364
9.43k
bad_len:
365
9.43k
    VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry",
366
9.43k
                b->size);
367
10.1k
error:
368
10.1k
    *header = 0;
369
10.1k
    if (field) {
370
10.1k
        *field = NULL;
371
10.1k
    }
372
10.1k
    return OFPERR_OFPBMC_BAD_LEN;
373
9.43k
}
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
381k
{
379
381k
    int copy_len;
380
381k
    void *copy_dst;
381
382
381k
    copy_dst = value;
383
381k
    copy_len = MIN(width, field ? field->n_bytes : sizeof *value);
384
385
381k
    if (field && field->variable_len) {
386
108k
        memset(value, 0, field->n_bytes);
387
108k
        copy_dst = &value->u8 + field->n_bytes - copy_len;
388
108k
    }
389
390
381k
    memcpy(copy_dst, payload, copy_len);
391
381k
}
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
313k
{
399
313k
    const struct mf_field *field;
400
313k
    enum ofperr header_error;
401
313k
    unsigned int payload_len;
402
313k
    const uint8_t *payload;
403
313k
    int width;
404
405
313k
    header_error = nx_pull_header__(b, allow_cookie, vl_mff_map, header,
406
313k
                                    &field, is_action);
407
313k
    if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) {
408
10.8k
        return header_error;
409
10.8k
    }
410
411
302k
    payload_len = nxm_payload_len(*header);
412
302k
    payload = ofpbuf_try_pull(b, payload_len);
413
302k
    if (!payload) {
414
20.7k
        VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte "
415
20.7k
                    "payload but only %"PRIu32" bytes follow OXM header",
416
20.7k
                    NXM_HEADER_ARGS(*header), payload_len, b->size);
417
20.7k
        return OFPERR_OFPBMC_BAD_LEN;
418
20.7k
    }
419
420
282k
    width = nxm_field_bytes(*header);
421
282k
    if (nxm_hasmask(*header)
422
106k
        && !is_mask_consistent(*header, payload, payload + width)) {
423
3.90k
        return OFPERR_OFPBMC_BAD_WILDCARDS;
424
3.90k
    }
425
426
278k
    copy_entry_value(field, value, payload, width);
427
428
278k
    if (mask) {
429
266k
        if (nxm_hasmask(*header)) {
430
102k
            copy_entry_value(field, mask, payload + width, width);
431
163k
        } else {
432
163k
            memset(mask, 0xff, sizeof *mask);
433
163k
        }
434
266k
    } else if (nxm_hasmask(*header)) {
435
128
        VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but "
436
128
                    "masked OXMs are not allowed here",
437
128
                    NXM_HEADER_ARGS(*header));
438
128
        return OFPERR_OFPBMC_BAD_MASK;
439
128
    }
440
441
278k
    if (field_) {
442
278k
        *field_ = field;
443
278k
        return header_error;
444
278k
    }
445
446
0
    return 0;
447
278k
}
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
6.42k
{
469
6.42k
    uint64_t header;
470
471
6.42k
    return nx_pull_entry__(b, false, vl_mff_map, &header, field, value, mask, is_action);
472
6.42k
}
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
34.9k
{
488
34.9k
    enum ofperr error;
489
34.9k
    uint64_t header;
490
491
34.9k
    error = nx_pull_header__(b, false, vl_mff_map,  &header, field, false);
492
34.9k
    if (masked) {
493
24.6k
        *masked = !error && nxm_hasmask(header);
494
24.6k
    } else if (!error && nxm_hasmask(header)) {
495
72
        error = OFPERR_OFPBMC_BAD_MASK;
496
72
    }
497
34.9k
    return error;
498
34.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
297k
{
506
297k
    enum ofperr error;
507
297k
    uint64_t header;
508
509
297k
    error = nx_pull_entry__(b, allow_cookie, vl_mff_map, &header, field, value,
510
297k
                            mask, false);
511
297k
    if (error) {
512
130k
        return error;
513
130k
    }
514
166k
    if (field && *field) {
515
166k
        if (!mf_is_mask_valid(*field, mask)) {
516
1.57k
            VLOG_DBG_RL(&rl, "bad mask for field %s", (*field)->name);
517
1.57k
            return OFPERR_OFPBMC_BAD_MASK;
518
1.57k
        }
519
165k
        if (!mf_is_value_valid(*field, value)) {
520
485
            VLOG_DBG_RL(&rl, "bad value for field %s", (*field)->name);
521
485
            return OFPERR_OFPBMC_BAD_VALUE;
522
485
        }
523
165k
    }
524
164k
    return 0;
525
166k
}
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
150k
{
536
150k
    ovs_assert((cookie != NULL) == (cookie_mask != NULL));
537
538
150k
    match_init_catchall(match);
539
150k
    match->flow.tunnel.metadata.tab = tun_table;
540
150k
    if (cookie) {
541
2.48k
        *cookie = *cookie_mask = htonll(0);
542
2.48k
    }
543
544
150k
    struct ofpbuf b = ofpbuf_const_initializer(p, match_len);
545
389k
    while (b.size) {
546
297k
        const uint8_t *pos = b.data;
547
297k
        const struct mf_field *field;
548
297k
        union mf_value value;
549
297k
        union mf_value mask;
550
297k
        enum ofperr error;
551
552
297k
        error = nx_pull_match_entry(&b, cookie != NULL, vl_mff_map, &field,
553
297k
                                    &value, &mask);
554
297k
        if (error) {
555
132k
            if (error == OFPERR_OFPBMC_BAD_FIELD && !strict) {
556
95.8k
                continue;
557
95.8k
            }
558
164k
        } else if (!field) {
559
65
            if (!cookie) {
560
0
                error = OFPERR_OFPBMC_BAD_FIELD;
561
65
            } else if (*cookie_mask) {
562
0
                error = OFPERR_OFPBMC_DUP_FIELD;
563
65
            } else {
564
65
                *cookie = value.be64;
565
65
                *cookie_mask = mask.be64;
566
65
            }
567
164k
        } else if (strict && !mf_are_match_prereqs_ok(field, match)) {
568
4.88k
            error = OFPERR_OFPBMC_BAD_PREREQ;
569
159k
        } else if (!mf_is_all_wild(field, &match->wc)) {
570
9.29k
            error = OFPERR_OFPBMC_DUP_FIELD;
571
150k
        } else if (pipeline_fields_only && !mf_is_pipeline_field(field)) {
572
6.78k
            error = OFPERR_OFPBRC_PIPELINE_FIELDS_ONLY;
573
143k
        } else {
574
143k
            char *err_str;
575
576
143k
            mf_set(field, &value, &mask, match, &err_str);
577
143k
            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
143k
            match_add_ethernet_prereq(match, field);
585
143k
        }
586
587
201k
        if (error) {
588
57.6k
            VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
589
57.6k
                        "within match (%s)", pos -
590
57.6k
                        p, ofperr_to_string(error));
591
57.6k
            return error;
592
57.6k
        }
593
201k
    }
594
595
92.6k
    match->flow.tunnel.metadata.tab = NULL;
596
92.6k
    return 0;
597
150k
}
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.4k
{
606
13.4k
    uint8_t *p = NULL;
607
608
13.4k
    if (match_len) {
609
3.34k
        p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
610
3.34k
        if (!p) {
611
1.29k
            VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
612
1.29k
                        "multiple of 8, is longer than space in message (max "
613
1.29k
                        "length %"PRIu32")", match_len, b->size);
614
1.29k
            return OFPERR_OFPBMC_BAD_LEN;
615
1.29k
        }
616
3.34k
    }
617
618
12.1k
    return nx_pull_raw(p, match_len, strict, pipeline_fields_only, match,
619
12.1k
                       cookie, cookie_mask, tun_table, vl_mff_map);
620
13.4k
}
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.40k
{
643
9.40k
    return nx_pull_match__(b, match_len, true, pipeline_fields_only, match,
644
9.40k
                           cookie, cookie_mask, tun_table, vl_mff_map);
645
9.40k
}
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.00k
{
656
4.00k
    return nx_pull_match__(b, match_len, false, pipeline_fields_only, match,
657
4.00k
                           cookie, cookie_mask, tun_table, NULL);
658
4.00k
}
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
159k
{
665
159k
    struct ofp11_match_header *omh = b->data;
666
159k
    uint8_t *p;
667
159k
    uint16_t match_len;
668
669
159k
    if (b->size < sizeof *omh) {
670
2.93k
        return OFPERR_OFPBMC_BAD_LEN;
671
2.93k
    }
672
673
156k
    match_len = ntohs(omh->length);
674
156k
    if (match_len < sizeof *omh) {
675
2.38k
        return OFPERR_OFPBMC_BAD_LEN;
676
2.38k
    }
677
678
153k
    if (omh->type != htons(OFPMT_OXM)) {
679
15.6k
        return OFPERR_OFPBMC_BAD_TYPE;
680
15.6k
    }
681
682
138k
    p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
683
138k
    if (!p) {
684
3.19k
        VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
685
3.19k
                    "multiple of 8, is longer than space in message (max "
686
3.19k
                    "length %"PRIu32")", match_len, b->size);
687
3.19k
        return OFPERR_OFPBMC_BAD_LEN;
688
3.19k
    }
689
690
135k
    return nx_pull_raw(p + sizeof *omh, match_len - sizeof *omh,
691
135k
                       strict, pipeline_fields_only, match, NULL, NULL,
692
135k
                       tun_table, vl_mff_map);
693
138k
}
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
40.7k
{
713
40.7k
    return oxm_pull_match__(b, true, pipeline_fields_only, tun_table,
714
40.7k
                            vl_mff_map, match);
715
40.7k
}
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
118k
{
724
118k
    return oxm_pull_match__(b, false, pipeline_fields_only, tun_table, NULL,
725
118k
                            match);
726
118k
}
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
3.12k
{
741
3.12k
    return nx_pull_raw(oxm, oxm_len, !loose, false, match, NULL, NULL,
742
3.12k
                       tun_table, vl_mff_map);
743
3.12k
}
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.59k
{
751
4.59k
    struct ofpbuf b = ofpbuf_const_initializer(fields_data, fields_len);
752
12.7k
    while (b.size) {
753
9.98k
        const uint8_t *pos = b.data;
754
9.98k
        const struct mf_field *field;
755
9.98k
        union mf_value value;
756
9.98k
        enum ofperr error;
757
9.98k
        uint64_t header;
758
759
9.98k
        error = nx_pull_entry__(&b, false, NULL, &header, &field, &value,
760
9.98k
                                NULL, false);
761
9.98k
        if (error) {
762
1.66k
            VLOG_DBG_RL(&rl, "error pulling field array field");
763
8.32k
        } else if (!field) {
764
0
            VLOG_DBG_RL(&rl, "unknown field array field");
765
0
            error = OFPERR_OFPBMC_BAD_FIELD;
766
8.32k
        } else if (bitmap_is_set(fa->used.bm, field->id)) {
767
13
            VLOG_DBG_RL(&rl, "duplicate field array field '%s'", field->name);
768
13
            error = OFPERR_OFPBMC_DUP_FIELD;
769
8.30k
        } else if (!mf_is_mask_valid(field, &value)) {
770
128
            VLOG_DBG_RL(&rl, "bad mask in field array field '%s'", field->name);
771
128
            error = OFPERR_OFPBMC_BAD_MASK;
772
8.17k
        } else {
773
8.17k
            field_array_set(field->id, &value, fa);
774
8.17k
        }
775
776
9.98k
        if (error) {
777
1.80k
            const uint8_t *start = fields_data;
778
779
1.80k
            VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
780
1.80k
                        "within field array (%s)", pos - start,
781
1.80k
                        ofperr_to_string(error));
782
783
1.80k
            free(fa->values);
784
1.80k
            fa->values = NULL;
785
1.80k
            return error;
786
1.80k
        }
787
9.98k
    }
788
789
2.78k
    return 0;
790
4.59k
}
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
18.4k
{
809
18.4k
    nx_put_header_len(b, field, version, !!mask, n_bytes);
810
18.4k
    ofpbuf_put(b, value, n_bytes);
811
18.4k
    if (mask) {
812
8.71k
        ofpbuf_put(b, mask, n_bytes);
813
8.71k
    }
814
18.4k
}
815
816
static void
817
nxm_put__(struct nxm_put_ctx *ctx,
818
          enum mf_field_id field, enum ofp_version version,
819
          const void *value, const void *mask, size_t n_bytes)
820
3.93k
{
821
3.93k
    nxm_put_entry_raw(ctx->output, field, version, value, mask, n_bytes);
822
3.93k
    if (!ctx->implied_ethernet && mf_from_id(field)->prereqs != MFP_NONE) {
823
889
        ctx->implied_ethernet = true;
824
889
    }
825
3.93k
}
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
305k
{
832
305k
    if (!is_all_zeros(mask, n_bytes)) {
833
3.33k
        bool masked = !is_all_ones(mask, n_bytes);
834
3.33k
        nxm_put__(ctx, field, version, value, masked ? mask : NULL, n_bytes);
835
3.33k
    }
836
305k
}
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
48.1k
{
843
48.1k
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
844
48.1k
}
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
498
{
850
498
    nxm_put__(ctx, field, version, &value, NULL, sizeof value);
851
498
}
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
21.6k
{
858
21.6k
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
859
21.6k
}
860
861
static void
862
nxm_put_16(struct nxm_put_ctx *ctx,
863
           enum mf_field_id field, enum ofp_version version, ovs_be16 value)
864
35
{
865
35
    nxm_put__(ctx, field, version, &value, NULL, sizeof value);
866
35
}
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
196k
{
873
196k
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
874
196k
}
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
69
{
880
69
    nxm_put__(ctx, field, version, &value, NULL, sizeof value);
881
69
}
882
883
static void
884
nxm_put_64m(struct nxm_put_ctx *ctx,
885
            enum mf_field_id field, enum ofp_version version,
886
            ovs_be64 value, ovs_be64 mask)
887
8.65k
{
888
8.65k
    nxm_put(ctx, field, version, &value, &mask, sizeof value);
889
8.65k
}
890
891
static void
892
nxm_put_128m(struct nxm_put_ctx *ctx,
893
             enum mf_field_id field, enum ofp_version version,
894
             const ovs_be128 value, const ovs_be128 mask)
895
4.32k
{
896
4.32k
    nxm_put(ctx, field, version, &value, &mask, sizeof(value));
897
4.32k
}
898
899
static void
900
nxm_put_eth_masked(struct nxm_put_ctx *ctx,
901
                   enum mf_field_id field, enum ofp_version version,
902
                   const struct eth_addr value, const struct eth_addr mask)
903
8.75k
{
904
8.75k
    nxm_put(ctx, field, version, value.ea, mask.ea, ETH_ADDR_LEN);
905
8.75k
}
906
907
static void
908
nxm_put_ipv6(struct nxm_put_ctx *ctx,
909
             enum mf_field_id field, enum ofp_version version,
910
             const struct in6_addr *value, const struct in6_addr *mask)
911
17.9k
{
912
17.9k
    nxm_put(ctx, field, version, value->s6_addr, mask->s6_addr,
913
17.9k
            sizeof value->s6_addr);
914
17.9k
}
915
916
static void
917
nxm_put_frag(struct nxm_put_ctx *ctx, const struct match *match,
918
             enum ofp_version version)
919
511
{
920
511
    uint8_t nw_frag = match->flow.nw_frag & FLOW_NW_FRAG_MASK;
921
511
    uint8_t nw_frag_mask = match->wc.masks.nw_frag & FLOW_NW_FRAG_MASK;
922
923
511
    nxm_put_8m(ctx, MFF_IP_FRAG, version, nw_frag,
924
511
               nw_frag_mask == FLOW_NW_FRAG_MASK ? UINT8_MAX : nw_frag_mask);
925
511
}
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
511
{
933
511
    const struct flow *flow = &match->flow;
934
511
    ovs_be16 dl_type = get_dl_type(flow);
935
936
511
    if (dl_type == htons(ETH_TYPE_IP)) {
937
227
        nxm_put_32m(ctx, MFF_IPV4_SRC, oxm,
938
227
                    flow->nw_src, match->wc.masks.nw_src);
939
227
        nxm_put_32m(ctx, MFF_IPV4_DST, oxm,
940
227
                    flow->nw_dst, match->wc.masks.nw_dst);
941
284
    } else {
942
284
        nxm_put_ipv6(ctx, MFF_IPV6_SRC, oxm,
943
284
                     &flow->ipv6_src, &match->wc.masks.ipv6_src);
944
284
        nxm_put_ipv6(ctx, MFF_IPV6_DST, oxm,
945
284
                     &flow->ipv6_dst, &match->wc.masks.ipv6_dst);
946
284
    }
947
948
511
    nxm_put_frag(ctx, match, oxm);
949
950
511
    if (match->wc.masks.nw_tos & IP_DSCP_MASK) {
951
2
        if (oxm) {
952
1
            nxm_put_8(ctx, MFF_IP_DSCP_SHIFTED, oxm,
953
1
                      flow->nw_tos >> 2);
954
1
        } else {
955
1
            nxm_put_8(ctx, MFF_IP_DSCP, oxm,
956
1
                      flow->nw_tos & IP_DSCP_MASK);
957
1
        }
958
2
    }
959
960
511
    if (match->wc.masks.nw_tos & IP_ECN_MASK) {
961
4
        nxm_put_8(ctx, MFF_IP_ECN, oxm,
962
4
                  flow->nw_tos & IP_ECN_MASK);
963
4
    }
964
965
511
    if (match->wc.masks.nw_ttl) {
966
4
        nxm_put_8(ctx, MFF_IP_TTL, oxm, flow->nw_ttl);
967
4
    }
968
969
511
    nxm_put_32m(ctx, MFF_IPV6_LABEL, oxm,
970
511
                flow->ipv6_label, match->wc.masks.ipv6_label);
971
972
511
    if (match->wc.masks.nw_proto) {
973
364
        nxm_put_8(ctx, MFF_IP_PROTO, oxm, flow->nw_proto);
974
975
364
        if (flow->nw_proto == IPPROTO_TCP) {
976
73
            nxm_put_16m(ctx, MFF_TCP_SRC, oxm,
977
73
                        flow->tp_src, match->wc.masks.tp_src);
978
73
            nxm_put_16m(ctx, MFF_TCP_DST, oxm,
979
73
                        flow->tp_dst, match->wc.masks.tp_dst);
980
73
            nxm_put_16m(ctx, MFF_TCP_FLAGS, oxm,
981
73
                        flow->tcp_flags, match->wc.masks.tcp_flags);
982
291
        } else if (flow->nw_proto == IPPROTO_UDP) {
983
67
            nxm_put_16m(ctx, MFF_UDP_SRC, oxm,
984
67
                        flow->tp_src, match->wc.masks.tp_src);
985
67
            nxm_put_16m(ctx, MFF_UDP_DST, oxm,
986
67
                        flow->tp_dst, match->wc.masks.tp_dst);
987
224
        } else if (flow->nw_proto == IPPROTO_SCTP) {
988
37
            nxm_put_16m(ctx, MFF_SCTP_SRC, oxm, flow->tp_src,
989
37
                        match->wc.masks.tp_src);
990
37
            nxm_put_16m(ctx, MFF_SCTP_DST, oxm, flow->tp_dst,
991
37
                        match->wc.masks.tp_dst);
992
187
        } else if (is_icmpv4(flow, NULL)) {
993
34
            if (match->wc.masks.tp_src) {
994
10
                nxm_put_8(ctx, MFF_ICMPV4_TYPE, oxm,
995
10
                          ntohs(flow->tp_src));
996
10
            }
997
34
            if (match->wc.masks.tp_dst) {
998
11
                nxm_put_8(ctx, MFF_ICMPV4_CODE, oxm,
999
11
                          ntohs(flow->tp_dst));
1000
11
            }
1001
153
        } else if (is_icmpv6(flow, NULL)) {
1002
134
            if (match->wc.masks.tp_src) {
1003
81
                nxm_put_8(ctx, MFF_ICMPV6_TYPE, oxm,
1004
81
                          ntohs(flow->tp_src));
1005
81
            }
1006
134
            if (match->wc.masks.tp_dst) {
1007
16
                nxm_put_8(ctx, MFF_ICMPV6_CODE, oxm,
1008
16
                          ntohs(flow->tp_dst));
1009
16
            }
1010
134
            if (is_nd(flow, NULL)) {
1011
47
                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
47
                nxm_put_ipv6(ctx, MFF_ND_TARGET, oxm,
1016
47
                             &flow->nd_target, &match->wc.masks.nd_target);
1017
47
                if (match->wc.masks.tcp_flags) {
1018
1
                   nxm_put_8(ctx, MFF_ND_OPTIONS_TYPE, oxm,
1019
1
                             ntohs(flow->tcp_flags));
1020
1
                }
1021
47
                if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
1022
25
                    nxm_put_eth_masked(ctx, MFF_ND_SLL, oxm,
1023
25
                                       flow->arp_sha, match->wc.masks.arp_sha);
1024
25
                }
1025
47
                if (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1026
22
                    nxm_put_eth_masked(ctx, MFF_ND_TLL, oxm,
1027
22
                                       flow->arp_tha, match->wc.masks.arp_tha);
1028
22
                }
1029
47
            }
1030
134
        }
1031
364
    }
1032
511
}
1033
1034
/* Appends to 'b' the nx_match format that expresses 'match'.  For Flow Mod and
1035
 * Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
1036
 * Otherwise, 'cookie_mask' should be zero.
1037
 *
1038
 * Specify 'oxm' as 0 to express the match in NXM format; otherwise, specify
1039
 * 'oxm' as the OpenFlow version number for the OXM format to use.
1040
 *
1041
 * This function can cause 'b''s data to be reallocated.
1042
 *
1043
 * Returns the number of bytes appended to 'b', excluding padding.
1044
 *
1045
 * If 'match' is a catch-all rule that matches every packet, then this function
1046
 * appends nothing to 'b' and returns 0. */
1047
static int
1048
nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
1049
           ovs_be64 cookie, ovs_be64 cookie_mask)
1050
4.32k
{
1051
4.32k
    const struct flow *flow = &match->flow;
1052
4.32k
    const size_t start_len = b->size;
1053
4.32k
    ovs_be16 dl_type = get_dl_type(flow);
1054
4.32k
    ovs_be32 spi_mask;
1055
4.32k
    int match_len;
1056
1057
4.32k
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
1058
1059
4.32k
    struct nxm_put_ctx ctx = { .output = b, .implied_ethernet = false };
1060
1061
    /* OpenFlow Packet Type. Must be first. */
1062
4.32k
    if (match->wc.masks.packet_type && !match_has_default_packet_type(match)) {
1063
562
        nxm_put_32m(&ctx, MFF_PACKET_TYPE, oxm, flow->packet_type,
1064
562
                    match->wc.masks.packet_type);
1065
562
    }
1066
1067
    /* Metadata. */
1068
4.32k
    if (match->wc.masks.dp_hash) {
1069
39
        nxm_put_32m(&ctx, MFF_DP_HASH, oxm,
1070
39
                    htonl(flow->dp_hash), htonl(match->wc.masks.dp_hash));
1071
39
    }
1072
1073
4.32k
    if (match->wc.masks.recirc_id) {
1074
9
        nxm_put_32(&ctx, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id));
1075
9
    }
1076
1077
4.32k
    if (match->wc.masks.conj_id) {
1078
7
        nxm_put_32(&ctx, MFF_CONJ_ID, oxm, htonl(flow->conj_id));
1079
7
    }
1080
1081
4.32k
    if (match->wc.masks.in_port.ofp_port) {
1082
28
        ofp_port_t in_port = flow->in_port.ofp_port;
1083
28
        if (oxm) {
1084
1
            nxm_put_32(&ctx, MFF_IN_PORT_OXM, oxm,
1085
1
                       ofputil_port_to_ofp11(in_port));
1086
27
        } else {
1087
27
            nxm_put_16(&ctx, MFF_IN_PORT, oxm,
1088
27
                       htons(ofp_to_u16(in_port)));
1089
27
        }
1090
28
    }
1091
4.32k
    if (match->wc.masks.actset_output) {
1092
51
        nxm_put_32(&ctx, MFF_ACTSET_OUTPUT, oxm,
1093
51
                   ofputil_port_to_ofp11(flow->actset_output));
1094
51
    }
1095
1096
    /* Ethernet. */
1097
4.32k
    nxm_put_eth_masked(&ctx, MFF_ETH_SRC, oxm,
1098
4.32k
                       flow->dl_src, match->wc.masks.dl_src);
1099
4.32k
    nxm_put_eth_masked(&ctx, MFF_ETH_DST, oxm,
1100
4.32k
                       flow->dl_dst, match->wc.masks.dl_dst);
1101
4.32k
    nxm_put_16m(&ctx, MFF_ETH_TYPE, oxm,
1102
4.32k
                ofputil_dl_type_to_openflow(flow->dl_type),
1103
4.32k
                match->wc.masks.dl_type);
1104
1105
    /* 802.1Q. */
1106
4.32k
    if (oxm) {
1107
436
        ovs_be16 VID_CFI_MASK = htons(VLAN_VID_MASK | VLAN_CFI);
1108
436
        ovs_be16 vid = flow->vlans[0].tci & VID_CFI_MASK;
1109
436
        ovs_be16 mask = match->wc.masks.vlans[0].tci & VID_CFI_MASK;
1110
1111
436
        if (mask == htons(VLAN_VID_MASK | VLAN_CFI)) {
1112
3
            nxm_put_16(&ctx, MFF_VLAN_VID, oxm, vid);
1113
433
        } else if (mask) {
1114
12
            nxm_put_16m(&ctx, MFF_VLAN_VID, oxm, vid, mask);
1115
12
        }
1116
1117
436
        if (vid && vlan_tci_to_pcp(match->wc.masks.vlans[0].tci)) {
1118
1
            nxm_put_8(&ctx, MFF_VLAN_PCP, oxm,
1119
1
                      vlan_tci_to_pcp(flow->vlans[0].tci));
1120
1
        }
1121
1122
3.89k
    } else {
1123
3.89k
        nxm_put_16m(&ctx, MFF_VLAN_TCI, oxm, flow->vlans[0].tci,
1124
3.89k
                    match->wc.masks.vlans[0].tci);
1125
3.89k
    }
1126
1127
    /* MPLS. */
1128
4.32k
    if (eth_type_mpls(dl_type)) {
1129
55
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1130
1
            nxm_put_8(&ctx, MFF_MPLS_TC, oxm,
1131
1
                      mpls_lse_to_tc(flow->mpls_lse[0]));
1132
1
        }
1133
1134
55
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1135
2
            nxm_put_8(&ctx, MFF_MPLS_BOS, oxm,
1136
2
                      mpls_lse_to_bos(flow->mpls_lse[0]));
1137
2
        }
1138
1139
55
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1140
1
            nxm_put_8(&ctx, MFF_MPLS_TTL, oxm,
1141
1
                      mpls_lse_to_ttl(flow->mpls_lse[0]));
1142
1
        }
1143
1144
55
        if (match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1145
1
            nxm_put_32(&ctx, MFF_MPLS_LABEL, oxm,
1146
1
                       htonl(mpls_lse_to_label(flow->mpls_lse[0])));
1147
1
        }
1148
55
    }
1149
1150
    /* L3. */
1151
4.32k
    if (is_ip_any(flow)) {
1152
511
        nxm_put_ip(&ctx, match, oxm);
1153
3.81k
    } else if (dl_type == htons(ETH_TYPE_ARP) ||
1154
3.80k
               dl_type == htons(ETH_TYPE_RARP)) {
1155
        /* ARP. */
1156
27
        if (match->wc.masks.nw_proto) {
1157
5
            nxm_put_16(&ctx, MFF_ARP_OP, oxm,
1158
5
                       htons(flow->nw_proto));
1159
5
        }
1160
27
        nxm_put_32m(&ctx, MFF_ARP_SPA, oxm,
1161
27
                    flow->nw_src, match->wc.masks.nw_src);
1162
27
        nxm_put_32m(&ctx, MFF_ARP_TPA, oxm,
1163
27
                    flow->nw_dst, match->wc.masks.nw_dst);
1164
27
        nxm_put_eth_masked(&ctx, MFF_ARP_SHA, oxm,
1165
27
                           flow->arp_sha, match->wc.masks.arp_sha);
1166
27
        nxm_put_eth_masked(&ctx, MFF_ARP_THA, oxm,
1167
27
                           flow->arp_tha, match->wc.masks.arp_tha);
1168
27
    }
1169
1170
    /* Tunnel ID. */
1171
4.32k
    nxm_put_64m(&ctx, MFF_TUN_ID, oxm,
1172
4.32k
                flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
1173
1174
    /* Other tunnel metadata. */
1175
4.32k
    nxm_put_16m(&ctx, MFF_TUN_FLAGS, oxm,
1176
4.32k
                htons(flow->tunnel.flags), htons(match->wc.masks.tunnel.flags));
1177
4.32k
    nxm_put_32m(&ctx, MFF_TUN_SRC, oxm,
1178
4.32k
                flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
1179
4.32k
    nxm_put_32m(&ctx, MFF_TUN_DST, oxm,
1180
4.32k
                flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
1181
4.32k
    nxm_put_ipv6(&ctx, MFF_TUN_IPV6_SRC, oxm,
1182
4.32k
                 &flow->tunnel.ipv6_src, &match->wc.masks.tunnel.ipv6_src);
1183
4.32k
    nxm_put_ipv6(&ctx, MFF_TUN_IPV6_DST, oxm,
1184
4.32k
                 &flow->tunnel.ipv6_dst, &match->wc.masks.tunnel.ipv6_dst);
1185
4.32k
    nxm_put_16m(&ctx, MFF_TUN_GBP_ID, oxm,
1186
4.32k
                flow->tunnel.gbp_id, match->wc.masks.tunnel.gbp_id);
1187
4.32k
    nxm_put_8m(&ctx, MFF_TUN_GBP_FLAGS, oxm,
1188
4.32k
               flow->tunnel.gbp_flags, match->wc.masks.tunnel.gbp_flags);
1189
4.32k
    tun_metadata_to_nx_match(b, oxm, match);
1190
1191
    /* ERSPAN */
1192
4.32k
    nxm_put_32m(&ctx, MFF_TUN_ERSPAN_IDX, oxm,
1193
4.32k
                htonl(flow->tunnel.erspan_idx),
1194
4.32k
                htonl(match->wc.masks.tunnel.erspan_idx));
1195
4.32k
    nxm_put_8m(&ctx, MFF_TUN_ERSPAN_VER, oxm,
1196
4.32k
                flow->tunnel.erspan_ver, match->wc.masks.tunnel.erspan_ver);
1197
4.32k
    nxm_put_8m(&ctx, MFF_TUN_ERSPAN_DIR, oxm,
1198
4.32k
                flow->tunnel.erspan_dir, match->wc.masks.tunnel.erspan_dir);
1199
4.32k
    nxm_put_8m(&ctx, MFF_TUN_ERSPAN_HWID, oxm,
1200
4.32k
                flow->tunnel.erspan_hwid, match->wc.masks.tunnel.erspan_hwid);
1201
1202
    /* GTP-U */
1203
4.32k
    nxm_put_8m(&ctx, MFF_TUN_GTPU_FLAGS, oxm, flow->tunnel.gtpu_flags,
1204
4.32k
               match->wc.masks.tunnel.gtpu_flags);
1205
4.32k
    nxm_put_8m(&ctx, MFF_TUN_GTPU_MSGTYPE, oxm, flow->tunnel.gtpu_msgtype,
1206
4.32k
               match->wc.masks.tunnel.gtpu_msgtype);
1207
1208
    /* Network Service Header */
1209
4.32k
    nxm_put_8m(&ctx, MFF_NSH_FLAGS, oxm, flow->nsh.flags,
1210
4.32k
            match->wc.masks.nsh.flags);
1211
4.32k
    nxm_put_8m(&ctx, MFF_NSH_TTL, oxm, flow->nsh.ttl,
1212
4.32k
            match->wc.masks.nsh.ttl);
1213
4.32k
    nxm_put_8m(&ctx, MFF_NSH_MDTYPE, oxm, flow->nsh.mdtype,
1214
4.32k
            match->wc.masks.nsh.mdtype);
1215
4.32k
    nxm_put_8m(&ctx, MFF_NSH_NP, oxm, flow->nsh.np,
1216
4.32k
            match->wc.masks.nsh.np);
1217
4.32k
    spi_mask = nsh_path_hdr_to_spi(match->wc.masks.nsh.path_hdr);
1218
4.32k
    if (spi_mask == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
1219
83
        spi_mask = OVS_BE32_MAX;
1220
83
    }
1221
4.32k
    nxm_put_32m(&ctx, MFF_NSH_SPI, oxm,
1222
4.32k
                nsh_path_hdr_to_spi(flow->nsh.path_hdr),
1223
4.32k
                spi_mask);
1224
4.32k
    nxm_put_8m(&ctx, MFF_NSH_SI, oxm,
1225
4.32k
                nsh_path_hdr_to_si(flow->nsh.path_hdr),
1226
4.32k
                nsh_path_hdr_to_si(match->wc.masks.nsh.path_hdr));
1227
21.6k
    for (int i = 0; i < 4; i++) {
1228
17.3k
        nxm_put_32m(&ctx, MFF_NSH_C1 + i, oxm, flow->nsh.context[i],
1229
17.3k
                    match->wc.masks.nsh.context[i]);
1230
17.3k
    }
1231
1232
    /* Registers. */
1233
4.32k
    if (oxm < OFP15_VERSION) {
1234
142k
        for (int i = 0; i < FLOW_N_REGS; i++) {
1235
138k
            nxm_put_32m(&ctx, MFF_REG0 + i, oxm,
1236
138k
                        htonl(flow->regs[i]), htonl(match->wc.masks.regs[i]));
1237
138k
        }
1238
4.32k
    } else {
1239
0
        for (int i = 0; i < FLOW_N_XREGS; i++) {
1240
0
            nxm_put_64m(&ctx, MFF_XREG0 + i, oxm,
1241
0
                        htonll(flow_get_xreg(flow, i)),
1242
0
                        htonll(flow_get_xreg(&match->wc.masks, i)));
1243
0
        }
1244
0
    }
1245
1246
    /* Packet mark. */
1247
4.32k
    nxm_put_32m(&ctx, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark),
1248
4.32k
                htonl(match->wc.masks.pkt_mark));
1249
1250
    /* Connection tracking. */
1251
4.32k
    nxm_put_32m(&ctx, MFF_CT_STATE, oxm, htonl(flow->ct_state),
1252
4.32k
                htonl(match->wc.masks.ct_state));
1253
4.32k
    nxm_put_16m(&ctx, MFF_CT_ZONE, oxm, htons(flow->ct_zone),
1254
4.32k
                htons(match->wc.masks.ct_zone));
1255
4.32k
    nxm_put_32m(&ctx, MFF_CT_MARK, oxm, htonl(flow->ct_mark),
1256
4.32k
                htonl(match->wc.masks.ct_mark));
1257
4.32k
    nxm_put_128m(&ctx, MFF_CT_LABEL, oxm, hton128(flow->ct_label),
1258
4.32k
                 hton128(match->wc.masks.ct_label));
1259
4.32k
    nxm_put_32m(&ctx, MFF_CT_NW_SRC, oxm,
1260
4.32k
                flow->ct_nw_src, match->wc.masks.ct_nw_src);
1261
4.32k
    nxm_put_ipv6(&ctx, MFF_CT_IPV6_SRC, oxm,
1262
4.32k
                 &flow->ct_ipv6_src, &match->wc.masks.ct_ipv6_src);
1263
4.32k
    nxm_put_32m(&ctx, MFF_CT_NW_DST, oxm,
1264
4.32k
                flow->ct_nw_dst, match->wc.masks.ct_nw_dst);
1265
4.32k
    nxm_put_ipv6(&ctx, MFF_CT_IPV6_DST, oxm,
1266
4.32k
                 &flow->ct_ipv6_dst, &match->wc.masks.ct_ipv6_dst);
1267
4.32k
    if (flow->ct_nw_proto) {
1268
7
        nxm_put_8m(&ctx, MFF_CT_NW_PROTO, oxm, flow->ct_nw_proto,
1269
7
                   match->wc.masks.ct_nw_proto);
1270
7
        nxm_put_16m(&ctx, MFF_CT_TP_SRC, oxm,
1271
7
                    flow->ct_tp_src, match->wc.masks.ct_tp_src);
1272
7
        nxm_put_16m(&ctx, MFF_CT_TP_DST, oxm,
1273
7
                    flow->ct_tp_dst, match->wc.masks.ct_tp_dst);
1274
7
    }
1275
    /* OpenFlow 1.1+ Metadata. */
1276
4.32k
    nxm_put_64m(&ctx, MFF_METADATA, oxm,
1277
4.32k
                flow->metadata, match->wc.masks.metadata);
1278
1279
    /* Cookie. */
1280
4.32k
    if (cookie_mask) {
1281
102
        bool masked = cookie_mask != OVS_BE64_MAX;
1282
1283
102
        cookie &= cookie_mask;
1284
102
        nx_put_header__(b, NXM_NX_COOKIE, masked);
1285
102
        ofpbuf_put(b, &cookie, sizeof cookie);
1286
102
        if (masked) {
1287
101
            ofpbuf_put(b, &cookie_mask, sizeof cookie_mask);
1288
101
        }
1289
102
    }
1290
1291
4.32k
    if (match_has_default_packet_type(match) && !ctx.implied_ethernet) {
1292
359
        uint64_t pt_stub[16 / 8];
1293
359
        struct ofpbuf pt;
1294
359
        ofpbuf_use_stack(&pt, pt_stub, sizeof pt_stub);
1295
359
        nxm_put_entry_raw(&pt, MFF_PACKET_TYPE, oxm, &flow->packet_type,
1296
359
                          NULL, sizeof flow->packet_type);
1297
1298
359
        ofpbuf_insert(b, start_len, pt.data, pt.size);
1299
359
    }
1300
1301
4.32k
    match_len = b->size - start_len;
1302
4.32k
    return match_len;
1303
4.32k
}
1304
1305
/* Appends to 'b' the nx_match format that expresses 'match', plus enough zero
1306
 * bytes to pad the nx_match out to a multiple of 8.  For Flow Mod and Flow
1307
 * Stats Requests messages, a 'cookie' and 'cookie_mask' may be supplied.
1308
 * Otherwise, 'cookie_mask' should be zero.
1309
 *
1310
 * This function can cause 'b''s data to be reallocated.
1311
 *
1312
 * Returns the number of bytes appended to 'b', excluding padding.  The return
1313
 * value can be zero if it appended nothing at all to 'b' (which happens if
1314
 * 'cr' is a catch-all rule that matches every packet). */
1315
int
1316
nx_put_match(struct ofpbuf *b, const struct match *match,
1317
             ovs_be64 cookie, ovs_be64 cookie_mask)
1318
3.89k
{
1319
3.89k
    int match_len = nx_put_raw(b, 0, match, cookie, cookie_mask);
1320
1321
3.89k
    ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1322
3.89k
    return match_len;
1323
3.89k
}
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
436
{
1340
436
    int match_len;
1341
436
    struct ofp11_match_header *omh;
1342
436
    size_t start_len = b->size;
1343
436
    ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
1344
1345
436
    ofpbuf_put_uninit(b, sizeof *omh);
1346
436
    match_len = (nx_put_raw(b, version, match, cookie, cookie_mask)
1347
436
                 + sizeof *omh);
1348
436
    ofpbuf_put_zeros(b, PAD_SIZE(match_len, 8));
1349
1350
436
    omh = ofpbuf_at(b, start_len, sizeof *omh);
1351
436
    omh->type = htons(OFPMT_OXM);
1352
436
    omh->length = htons(match_len);
1353
1354
436
    return match_len;
1355
436
}
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
6.40k
{
1378
6.40k
    const struct mf_field *mf = mf_from_id(id);
1379
1380
6.40k
    ds_put_format(ds, "%s", mf->name);
1381
1382
6.40k
    if (!is_all_ones(mask, mf->n_bytes)) {
1383
6.33k
        ds_put_char(ds, '=');
1384
6.33k
        mf_format(mf, mask, NULL, NULL, ds);
1385
6.33k
    }
1386
1387
6.40k
    ds_put_char(ds, ',');
1388
6.40k
}
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.47k
{
1396
2.47k
    size_t start_len = ds->length;
1397
2.47k
    size_t i, offset = 0;
1398
1399
6.40k
    BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fa->used.bm) {
1400
6.40k
        const struct mf_field *mf = mf_from_id(i);
1401
6.40k
        union mf_value value;
1402
1403
6.40k
        memcpy(&value, fa->values + offset, mf->n_bytes);
1404
6.40k
        nx_format_mask_tlv(ds, i, &value);
1405
6.40k
        offset += mf->n_bytes;
1406
6.40k
    }
1407
1408
2.47k
    if (ds->length > start_len) {
1409
2.47k
        ds_chomp(ds, ',');
1410
2.47k
    }
1411
2.47k
}
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
21.5k
{
1456
21.5k
    uint64_t masked_header = masked ? nxm_make_wild_header(header) : header;
1457
21.5k
    ovs_be64 network_header = htonll(masked_header);
1458
1459
21.5k
    ofpbuf_put(b, &network_header, nxm_header_len(header));
1460
21.5k
}
1461
1462
void
1463
nx_put_header(struct ofpbuf *b, enum mf_field_id field,
1464
              enum ofp_version version, bool masked)
1465
3.00k
{
1466
3.00k
    nx_put_header__(b, mf_oxm_header(field, version), masked);
1467
3.00k
}
1468
1469
void nx_put_mff_header(struct ofpbuf *b, const struct mf_field *mff,
1470
                       enum ofp_version version, bool masked)
1471
2.75k
{
1472
2.75k
    if (mff->mapped) {
1473
0
        nx_put_header_len(b, mff->id, version, masked, mff->n_bytes);
1474
2.75k
    } else {
1475
2.75k
        nx_put_header(b, mff->id, version, masked);
1476
2.75k
    }
1477
2.75k
}
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
18.4k
{
1483
18.4k
    uint64_t header = mf_oxm_header(field, version);
1484
1485
18.4k
    header = NXM_HEADER(nxm_vendor(header), nxm_class(header),
1486
18.4k
                        nxm_field(header), false,
1487
18.4k
                        nxm_experimenter_len(header) + n_bytes);
1488
1489
18.4k
    nx_put_header__(b, header, masked);
1490
18.4k
}
1491
1492
void
1493
nx_put_entry(struct ofpbuf *b, const struct mf_field *mff,
1494
             enum ofp_version version, const union mf_value *value,
1495
             const union mf_value *mask)
1496
13.6k
{
1497
13.6k
    bool masked;
1498
13.6k
    int len, offset;
1499
1500
13.6k
    len = mf_field_len(mff, value, mask, &masked);
1501
13.6k
    offset = mff->n_bytes - len;
1502
1503
13.6k
    nxm_put_entry_raw(b, mff->id, version,
1504
13.6k
                      &value->u8 + offset, masked ? &mask->u8 + offset : NULL,
1505
13.6k
                      len);
1506
13.6k
}
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
631
{
1793
631
    const char *full_s = s;
1794
631
    char *error;
1795
1796
631
    error = mf_parse_subfield__(&move->src, &s);
1797
631
    if (error) {
1798
9
        return error;
1799
9
    }
1800
622
    if (strncmp(s, "->", 2)) {
1801
15
        return xasprintf("%s: missing `->' following source", full_s);
1802
15
    }
1803
607
    s += 2;
1804
607
    error = mf_parse_subfield(&move->dst, s);
1805
607
    if (error) {
1806
2
        return error;
1807
2
    }
1808
1809
605
    if (move->src.n_bits != move->dst.n_bits) {
1810
1
        return xasprintf("%s: source field is %d bits wide but destination is "
1811
1
                         "%d bits wide", full_s,
1812
1
                         move->src.n_bits, move->dst.n_bits);
1813
1
    }
1814
604
    return NULL;
1815
605
}
1816

1817
/* nxm_format_reg_move(). */
1818
1819
void
1820
nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
1821
33
{
1822
33
    ds_put_format(s, "%smove:%s", colors.special, colors.end);
1823
33
    mf_format_subfield(&move->src, s);
1824
33
    ds_put_format(s, "%s->%s", colors.special, colors.end);
1825
33
    mf_format_subfield(&move->dst, s);
1826
33
}
1827
1828

1829
enum ofperr
1830
nxm_reg_move_check(const struct ofpact_reg_move *move,
1831
                   const struct match *match)
1832
2.38k
{
1833
2.38k
    enum ofperr error;
1834
1835
2.38k
    error = mf_check_src(&move->src, match);
1836
2.38k
    if (error) {
1837
1.50k
        return error;
1838
1.50k
    }
1839
1840
880
    return mf_check_dst(&move->dst, match);
1841
2.38k
}
1842

1843
/* nxm_execute_reg_move(). */
1844
1845
void
1846
nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
1847
             struct flow *flow, struct flow_wildcards *wc)
1848
0
{
1849
0
    union mf_subvalue src_subvalue;
1850
0
    union mf_subvalue mask_value;
1851
0
    ovs_be64 src_data_be = htonll(src_data);
1852
1853
    /* ODP library doesn't rely on tunnel fields to be unwildcarded
1854
     * when written but not read.  No need to unwildcard them here. */
1855
0
    if (!mf_is_tunnel_field(dst->field)) {
1856
0
        memset(&mask_value, 0xff, sizeof mask_value);
1857
0
        mf_write_subfield_flow(dst, &mask_value, &wc->masks);
1858
0
    }
1859
1860
0
    bitwise_copy(&src_data_be, sizeof src_data_be, 0,
1861
0
                 &src_subvalue, sizeof src_subvalue, 0,
1862
0
                 sizeof src_data_be * 8);
1863
0
    mf_write_subfield_flow(dst, &src_subvalue, flow);
1864
0
}
1865

1866
/* nxm_parse_stack_action, works for both push() and pop(). */
1867
1868
/* Parses 's' as a "push" or "pop" action, in the form described in
1869
 * ovs-actions(7), into '*stack_action'.
1870
 *
1871
 * Returns NULL if successful, otherwise a malloc()'d string describing the
1872
 * error.  The caller is responsible for freeing the returned string. */
1873
char * OVS_WARN_UNUSED_RESULT
1874
nxm_parse_stack_action(struct ofpact_stack *stack_action, const char *s)
1875
1.73k
{
1876
1.73k
    char *error;
1877
1878
1.73k
    error = mf_parse_subfield__(&stack_action->subfield, &s);
1879
1.73k
    if (error) {
1880
9
        return error;
1881
9
    }
1882
1883
1.72k
    if (*s != '\0') {
1884
1
        return xasprintf("%s: trailing garbage following push or pop", s);
1885
1
    }
1886
1887
1.72k
    return NULL;
1888
1.72k
}
1889
1890
void
1891
nxm_format_stack_push(const struct ofpact_stack *push, struct ds *s)
1892
19
{
1893
19
    ds_put_format(s, "%spush:%s", colors.param, colors.end);
1894
19
    mf_format_subfield(&push->subfield, s);
1895
19
}
1896
1897
void
1898
nxm_format_stack_pop(const struct ofpact_stack *pop, struct ds *s)
1899
201
{
1900
201
    ds_put_format(s, "%spop:%s", colors.param, colors.end);
1901
201
    mf_format_subfield(&pop->subfield, s);
1902
201
}
1903
1904
enum ofperr
1905
nxm_stack_push_check(const struct ofpact_stack *push,
1906
                     const struct match *match)
1907
1.65k
{
1908
1.65k
    return mf_check_src(&push->subfield, match);
1909
1.65k
}
1910
1911
enum ofperr
1912
nxm_stack_pop_check(const struct ofpact_stack *pop,
1913
                    const struct match *match)
1914
1.96k
{
1915
1.96k
    return mf_check_dst(&pop->subfield, match);
1916
1.96k
}
1917
1918
/* nxm_execute_stack_push(), nxm_execute_stack_pop().
1919
 *
1920
 * A stack is an ofpbuf with 'data' pointing to the bottom of the stack and
1921
 * 'size' indexing the top of the stack.  Each value of some byte length is
1922
 * stored to the stack immediately followed by the length of the value as an
1923
 * unsigned byte.  This way a POP operation can first read the length byte, and
1924
 * then the appropriate number of bytes from the stack.  This also means that
1925
 * it is only possible to traverse the stack from top to bottom.  It is
1926
 * possible, however, to push values also to the bottom of the stack, which is
1927
 * useful when a stack has been serialized to a wire format in reverse order
1928
 * (topmost value first).
1929
 */
1930
1931
/* Push value 'v' of length 'bytes' to the top of 'stack'. */
1932
void
1933
nx_stack_push(struct ofpbuf *stack, const void *v, uint8_t bytes)
1934
0
{
1935
0
    ofpbuf_put(stack, v, bytes);
1936
0
    ofpbuf_put(stack, &bytes, sizeof bytes);
1937
0
}
1938
1939
/* Push value 'v' of length 'bytes' to the bottom of 'stack'. */
1940
void
1941
nx_stack_push_bottom(struct ofpbuf *stack, const void *v, uint8_t bytes)
1942
1.86k
{
1943
1.86k
    ofpbuf_push(stack, &bytes, sizeof bytes);
1944
1.86k
    ofpbuf_push(stack, v, bytes);
1945
1.86k
}
1946
1947
/* Pop the topmost value from 'stack', returning a pointer to the value in the
1948
 * stack and the length of the value in '*bytes'.  In case of underflow a NULL
1949
 * is returned and length is returned as zero via '*bytes'. */
1950
void *
1951
nx_stack_pop(struct ofpbuf *stack, uint8_t *bytes)
1952
1.42k
{
1953
1.42k
    if (!stack->size) {
1954
0
        *bytes = 0;
1955
0
        return NULL;
1956
0
    }
1957
1958
1.42k
    stack->size -= sizeof *bytes;
1959
1.42k
    memcpy(bytes, ofpbuf_tail(stack), sizeof *bytes);
1960
1961
1.42k
    ovs_assert(stack->size >= *bytes);
1962
1.42k
    stack->size -= *bytes;
1963
1.42k
    return ofpbuf_tail(stack);
1964
1.42k
}
1965
1966
void
1967
nxm_execute_stack_push(const struct ofpact_stack *push,
1968
                       const struct flow *flow, struct flow_wildcards *wc,
1969
                       struct ofpbuf *stack)
1970
0
{
1971
0
    union mf_subvalue dst_value;
1972
1973
0
    mf_write_subfield_flow(&push->subfield,
1974
0
                           (union mf_subvalue *) &exact_match_mask,
1975
0
                           &wc->masks);
1976
1977
0
    mf_read_subfield(&push->subfield, flow, &dst_value);
1978
0
    uint8_t bytes = DIV_ROUND_UP(push->subfield.n_bits, 8);
1979
0
    nx_stack_push(stack, &dst_value.u8[sizeof dst_value - bytes], bytes);
1980
0
}
1981
1982
bool
1983
nxm_execute_stack_pop(const struct ofpact_stack *pop,
1984
                      struct flow *flow, struct flow_wildcards *wc,
1985
                      struct ofpbuf *stack)
1986
0
{
1987
0
    uint8_t src_bytes;
1988
0
    const void *src = nx_stack_pop(stack, &src_bytes);
1989
0
    if (src) {
1990
0
        union mf_subvalue src_value;
1991
0
        uint8_t dst_bytes = DIV_ROUND_UP(pop->subfield.n_bits, 8);
1992
1993
0
        if (src_bytes < dst_bytes) {
1994
0
            memset(&src_value.u8[sizeof src_value - dst_bytes], 0,
1995
0
                   dst_bytes - src_bytes);
1996
0
        }
1997
0
        memcpy(&src_value.u8[sizeof src_value - src_bytes], src, src_bytes);
1998
        /* ODP library doesn't rely on tunnel fields to be unwildcarded
1999
         * when written but not read.  No need to unwildcard them here. */
2000
0
        if (!mf_is_tunnel_field(pop->subfield.field)) {
2001
0
            mf_write_subfield_flow(&pop->subfield,
2002
0
                                   (union mf_subvalue *) &exact_match_mask,
2003
0
                                   &wc->masks);
2004
0
        }
2005
0
        mf_write_subfield_flow(&pop->subfield, &src_value, flow);
2006
0
        return true;
2007
0
    } else {
2008
        /* Attempted to pop from an empty stack. */
2009
0
        return false;
2010
0
    }
2011
0
}
2012

2013
/* Parses a field from '*s' into '*field'.  If successful, stores the
2014
 * reference to the field in '*field', and returns NULL.  On failure,
2015
 * returns a malloc()'ed error message.
2016
 */
2017
char * OVS_WARN_UNUSED_RESULT
2018
mf_parse_field(const struct mf_field **field, const char *s)
2019
264
{
2020
264
    const struct nxm_field *f;
2021
264
    int s_len = strlen(s);
2022
2023
264
    f = nxm_field_by_name(s, s_len);
2024
264
    (*field) = f ? mf_from_id(f->id) : mf_from_name_len(s, s_len);
2025
264
    if (!*field) {
2026
4
        return xasprintf("unknown field `%s'", s);
2027
4
    }
2028
260
    return NULL;
2029
264
}
2030

2031
/* Formats 'sf' into 's' in a format normally acceptable to
2032
 * mf_parse_subfield().  (It won't be acceptable if sf->field is NULL or if
2033
 * sf->field has no NXM name.) */
2034
void
2035
mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2036
26.7k
{
2037
26.7k
    if (!sf->field) {
2038
0
        ds_put_cstr(s, "<unknown>");
2039
26.7k
    } else {
2040
26.7k
        const struct nxm_field *f = nxm_field_by_mf_id(sf->field->id, 0);
2041
26.7k
        ds_put_cstr(s, f ? f->name : sf->field->name);
2042
26.7k
    }
2043
2044
26.7k
    if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2045
1.52k
        ds_put_cstr(s, "[]");
2046
25.1k
    } else if (sf->n_bits == 1) {
2047
2.43k
        ds_put_format(s, "[%d]", sf->ofs);
2048
22.7k
    } else {
2049
22.7k
        ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2050
22.7k
    }
2051
26.7k
}
2052
2053
static const struct nxm_field *
2054
mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2055
156k
{
2056
156k
    *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2057
156k
    if (*wild) {
2058
355
        name_len -= 2;
2059
355
    }
2060
2061
156k
    return nxm_field_by_name(name, name_len);
2062
156k
}
2063
2064
/* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
2065
 * returns NULL and advances '*sp' to the first byte following the parsed
2066
 * string.  On failure, returns a malloc()'d error message, does not modify
2067
 * '*sp', and does not properly initialize 'sf'.
2068
 *
2069
 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2070
 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2071
 * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2072
 * may both be omitted (the [] are still required) to indicate an entire
2073
 * field. */
2074
char * OVS_WARN_UNUSED_RESULT
2075
mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2076
156k
{
2077
156k
    const struct mf_field *field = NULL;
2078
156k
    const struct nxm_field *f;
2079
156k
    const char *name;
2080
156k
    int start, end;
2081
156k
    const char *s;
2082
156k
    int name_len;
2083
156k
    bool wild;
2084
2085
156k
    s = *sp;
2086
156k
    name = s;
2087
156k
    name_len = strcspn(s, "[-");
2088
2089
156k
    f = mf_parse_subfield_name(name, name_len, &wild);
2090
156k
    field = f ? mf_from_id(f->id) : mf_from_name_len(name, name_len);
2091
156k
    if (!field) {
2092
47.6k
        return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2093
47.6k
    }
2094
2095
108k
    s += name_len;
2096
    /* Assume full field. */
2097
108k
    start = 0;
2098
108k
    end = field->n_bits - 1;
2099
108k
    if (*s == '[') {
2100
73.5k
        if (!strncmp(s, "[]", 2)) {
2101
            /* Nothing to do. */
2102
56.3k
        } else if (ovs_scan(s, "[%d..%d]", &start, &end)) {
2103
            /* Nothing to do. */
2104
55.7k
        } else if (ovs_scan(s, "[%d]", &start)) {
2105
55.6k
            end = start;
2106
55.6k
        } else {
2107
92
            return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2108
92
                             "[<start>..<end>]", *sp);
2109
92
        }
2110
73.4k
        s = strchr(s, ']') + 1;
2111
73.4k
    }
2112
2113
108k
    if (start > end) {
2114
32
        return xasprintf("%s: starting bit %d is after ending bit %d",
2115
32
                         *sp, start, end);
2116
108k
    } else if (start >= field->n_bits) {
2117
35
        return xasprintf("%s: starting bit %d is not valid because field is "
2118
35
                         "only %d bits wide", *sp, start, field->n_bits);
2119
108k
    } else if (end >= field->n_bits){
2120
28
        return xasprintf("%s: ending bit %d is not valid because field is "
2121
28
                         "only %d bits wide", *sp, end, field->n_bits);
2122
28
    }
2123
2124
108k
    sf->field = field;
2125
108k
    sf->ofs = start;
2126
108k
    sf->n_bits = end - start + 1;
2127
2128
108k
    *sp = s;
2129
108k
    return NULL;
2130
108k
}
2131
2132
/* Parses a subfield from the entirety of 's' into 'sf'.  Returns NULL if
2133
 * successful, otherwise a malloc()'d string describing the error.  The caller
2134
 * is responsible for freeing the returned string.
2135
 *
2136
 * The syntax parsed from 's' takes the form "header[start..end]" where
2137
 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2138
 * bit indexes.  "..end" may be omitted to indicate a single bit.  "start..end"
2139
 * may both be omitted (the [] are still required) to indicate an entire
2140
 * field.  */
2141
char * OVS_WARN_UNUSED_RESULT
2142
mf_parse_subfield(struct mf_subfield *sf, const char *s)
2143
153k
{
2144
153k
    char *error = mf_parse_subfield__(sf, &s);
2145
153k
    if (!error && s[0]) {
2146
17
        error = xstrdup("unexpected input following field syntax");
2147
17
    }
2148
153k
    return error;
2149
153k
}
2150

2151
/* Returns an bitmap in which each bit corresponds to the like-numbered field
2152
 * in the OFPXMC12_OPENFLOW_BASIC OXM class, in which the bit values are taken
2153
 * from the 'fields' bitmap.  Only fields defined in OpenFlow 'version' are
2154
 * considered.
2155
 *
2156
 * This is useful for encoding OpenFlow 1.2 table stats messages. */
2157
ovs_be64
2158
oxm_bitmap_from_mf_bitmap(const struct mf_bitmap *fields,
2159
                          enum ofp_version version)
2160
0
{
2161
0
    uint64_t oxm_bitmap = 0;
2162
0
    enum mf_field_id id;
2163
2164
0
    BITMAP_FOR_EACH_1 (id, MFF_N_IDS, fields->bm) {
2165
0
        uint64_t oxm = mf_oxm_header(id, version);
2166
0
        uint32_t class = nxm_class(oxm);
2167
0
        int field = nxm_field(oxm);
2168
2169
0
        if (class == OFPXMC12_OPENFLOW_BASIC && field < 64) {
2170
0
            oxm_bitmap |= UINT64_C(1) << field;
2171
0
        }
2172
0
    }
2173
0
    return htonll(oxm_bitmap);
2174
0
}
2175
2176
/* Opposite conversion from oxm_bitmap_from_mf_bitmap().
2177
 *
2178
 * This is useful for decoding OpenFlow 1.2 table stats messages. */
2179
struct mf_bitmap
2180
oxm_bitmap_to_mf_bitmap(ovs_be64 oxm_bitmap, enum ofp_version version)
2181
64.6k
{
2182
64.6k
    struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
2183
2184
13.7M
    for (enum mf_field_id id = 0; id < MFF_N_IDS; id++) {
2185
13.6M
        uint64_t oxm = mf_oxm_header(id, version);
2186
13.6M
        if (oxm && version >= nxm_field_by_header(oxm, false, NULL)->version) {
2187
12.0M
            uint32_t class = nxm_class(oxm);
2188
12.0M
            int field = nxm_field(oxm);
2189
2190
12.0M
            if (class == OFPXMC12_OPENFLOW_BASIC
2191
2.26M
                && field < 64
2192
2.26M
                && oxm_bitmap & htonll(UINT64_C(1) << field)) {
2193
558k
                bitmap_set1(fields.bm, id);
2194
558k
            }
2195
12.0M
        }
2196
13.6M
    }
2197
64.6k
    return fields;
2198
64.6k
}
2199
2200
/* Returns a bitmap of fields that can be encoded in OXM and that can be
2201
 * modified with a "set_field" action.  */
2202
struct mf_bitmap
2203
oxm_writable_fields(void)
2204
0
{
2205
0
    struct mf_bitmap b = MF_BITMAP_INITIALIZER;
2206
0
    int i;
2207
2208
0
    for (i = 0; i < MFF_N_IDS; i++) {
2209
0
        if (mf_oxm_header(i, 0) && mf_from_id(i)->writable) {
2210
0
            bitmap_set1(b.bm, i);
2211
0
        }
2212
0
    }
2213
0
    return b;
2214
0
}
2215
2216
/* Returns a bitmap of fields that can be encoded in OXM and that can be
2217
 * matched in a flow table.  */
2218
struct mf_bitmap
2219
oxm_matchable_fields(void)
2220
0
{
2221
0
    struct mf_bitmap b = MF_BITMAP_INITIALIZER;
2222
0
    int i;
2223
2224
0
    for (i = 0; i < MFF_N_IDS; i++) {
2225
0
        if (mf_oxm_header(i, 0)) {
2226
0
            bitmap_set1(b.bm, i);
2227
0
        }
2228
0
    }
2229
0
    return b;
2230
0
}
2231
2232
/* Returns a bitmap of fields that can be encoded in OXM and that can be
2233
 * matched in a flow table with an arbitrary bitmask.  */
2234
struct mf_bitmap
2235
oxm_maskable_fields(void)
2236
0
{
2237
0
    struct mf_bitmap b = MF_BITMAP_INITIALIZER;
2238
0
    int i;
2239
2240
0
    for (i = 0; i < MFF_N_IDS; i++) {
2241
0
        if (mf_oxm_header(i, 0) && mf_from_id(i)->maskable == MFM_FULLY) {
2242
0
            bitmap_set1(b.bm, i);
2243
0
        }
2244
0
    }
2245
0
    return b;
2246
0
}
2247

2248
struct nxm_field_index {
2249
    struct hmap_node header_node; /* In nxm_header_map. */
2250
    struct hmap_node name_node;   /* In nxm_name_map. */
2251
    struct ovs_list mf_node;      /* In mf_mf_map[nf.id]. */
2252
    const struct nxm_field nf;
2253
};
2254
2255
#include "nx-match.inc"
2256
2257
static struct hmap nxm_header_map;
2258
static struct hmap nxm_name_map;
2259
static struct ovs_list nxm_mf_map[MFF_N_IDS];
2260
2261
static void
2262
nxm_init(void)
2263
27.7M
{
2264
27.7M
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2265
27.7M
    if (ovsthread_once_start(&once)) {
2266
2
        hmap_init(&nxm_header_map);
2267
2
        hmap_init(&nxm_name_map);
2268
424
        for (int i = 0; i < MFF_N_IDS; i++) {
2269
422
            ovs_list_init(&nxm_mf_map[i]);
2270
422
        }
2271
2
        for (struct nxm_field_index *nfi = all_nxm_fields;
2272
476
             nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) {
2273
474
            hmap_insert(&nxm_header_map, &nfi->header_node,
2274
474
                        hash_uint64(nxm_no_len(nfi->nf.header)));
2275
474
            hmap_insert(&nxm_name_map, &nfi->name_node,
2276
474
                        hash_string(nfi->nf.name, 0));
2277
474
            ovs_list_push_back(&nxm_mf_map[nfi->nf.id], &nfi->mf_node);
2278
474
        }
2279
2
        ovsthread_once_done(&once);
2280
2
    }
2281
27.7M
}
2282
2283
2284
static const struct nxm_field *
2285
nxm_field_by_header(uint64_t header, bool is_action, enum ofperr *h_error)
2286
13.6M
{
2287
13.6M
    const struct nxm_field_index *nfi;
2288
13.6M
    uint64_t header_no_len;
2289
2290
13.6M
    nxm_init();
2291
13.6M
    if (nxm_hasmask(header)) {
2292
136k
        header = nxm_make_exact_header(header);
2293
136k
    }
2294
2295
13.6M
    header_no_len = nxm_no_len(header);
2296
2297
13.6M
    HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_uint64(header_no_len),
2298
26.2M
                             &nxm_header_map) {
2299
26.2M
        if (is_action && nxm_length(header) > 0) {
2300
10.8k
            if (nxm_length(header) != nxm_length(nfi->nf.header) && h_error ) {
2301
5.00k
               *h_error = OFPERR_OFPBAC_BAD_SET_LEN;
2302
5.00k
            }
2303
10.8k
        }
2304
26.2M
        if (header_no_len == nxm_no_len(nfi->nf.header)) {
2305
13.5M
            if (nxm_length(header) == nxm_length(nfi->nf.header) ||
2306
13.5M
                mf_from_id(nfi->nf.id)->variable_len) {
2307
13.5M
                return &nfi->nf;
2308
13.5M
            } else {
2309
34.3k
                return NULL;
2310
34.3k
            }
2311
13.5M
        }
2312
26.2M
    }
2313
113k
    return NULL;
2314
13.6M
}
2315
2316
static const struct nxm_field *
2317
nxm_field_by_name(const char *name, size_t len)
2318
156k
{
2319
156k
    const struct nxm_field_index *nfi;
2320
2321
156k
    nxm_init();
2322
156k
    HMAP_FOR_EACH_WITH_HASH (nfi, name_node, hash_bytes(name, len, 0),
2323
156k
                             &nxm_name_map) {
2324
2.93k
        if (strlen(nfi->nf.name) == len && !memcmp(nfi->nf.name, name, len)) {
2325
2.71k
            return &nfi->nf;
2326
2.71k
        }
2327
2.93k
    }
2328
153k
    return NULL;
2329
156k
}
2330
2331
static const struct nxm_field *
2332
nxm_field_by_mf_id(enum mf_field_id id, enum ofp_version version)
2333
13.8M
{
2334
13.8M
    const struct nxm_field_index *nfi;
2335
13.8M
    const struct nxm_field *f;
2336
2337
13.8M
    nxm_init();
2338
2339
13.8M
    f = NULL;
2340
15.5M
    LIST_FOR_EACH (nfi, mf_node, &nxm_mf_map[id]) {
2341
15.5M
        if (!f || version >= nfi->nf.version) {
2342
15.3M
            f = &nfi->nf;
2343
15.3M
        }
2344
15.5M
    }
2345
13.8M
    return f;
2346
13.8M
}