Coverage Report

Created: 2023-03-26 07:41

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