Coverage Report

Created: 2026-09-02 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/meta-flow.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2011-2017 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 "openvswitch/meta-flow.h"
20
21
#include <errno.h>
22
#include <limits.h>
23
#include <netinet/icmp6.h>
24
#include <netinet/ip6.h>
25
26
#include "classifier.h"
27
#include "openvswitch/dynamic-string.h"
28
#include "nx-match.h"
29
#include "ovs-atomic.h"
30
#include "ovs-rcu.h"
31
#include "ovs-thread.h"
32
#include "packets.h"
33
#include "random.h"
34
#include "openvswitch/shash.h"
35
#include "socket-util.h"
36
#include "tun-metadata.h"
37
#include "unaligned.h"
38
#include "util.h"
39
#include "openvswitch/ofp-errors.h"
40
#include "openvswitch/ofp-match.h"
41
#include "openvswitch/ofp-port.h"
42
#include "openvswitch/vlog.h"
43
#include "vl-mff-map.h"
44
#include "openvswitch/nsh.h"
45
46
VLOG_DEFINE_THIS_MODULE(meta_flow);
47
48
#define FLOW_U32OFS(FIELD)                                              \
49
    offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
50
51
#define MF_FIELD_SIZES(MEMBER)                  \
52
    sizeof ((union mf_value *)0)->MEMBER,       \
53
    8 * sizeof ((union mf_value *)0)->MEMBER
54
55
extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
56
57
const struct mf_field mf_fields[MFF_N_IDS] = {
58
#include "meta-flow.inc"
59
};
60
61
/* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
62
static struct shash mf_by_name;
63
64
/* Rate limit for parse errors.  These always indicate a bug in an OpenFlow
65
 * controller and so there's not much point in showing a lot of them. */
66
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
67
68
#define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff,  0xff, 0xff, 0xff, 0xff
69
#define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
70
#define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
71
#define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
72
#define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
73
#define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
74
#define MF_SUBVALUE_EXACT_INITIALIZER { .u8 = { MF_VALUE_EXACT_128 } }
75
76
const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
77
const union mf_subvalue exact_sub_match_mask = MF_SUBVALUE_EXACT_INITIALIZER;
78
79
static void nxm_init(void);
80
81
/* Returns the field with the given 'name', or a null pointer if no field has
82
 * that name. */
83
const struct mf_field *
84
mf_from_name(const char *name)
85
197k
{
86
197k
    nxm_init();
87
197k
    return shash_find_data(&mf_by_name, name);
88
197k
}
89
90
/* Returns the field with the given 'name' (which is 'len' bytes long), or a
91
 * null pointer if no field has that name. */
92
const struct mf_field *
93
mf_from_name_len(const char *name, size_t len)
94
167k
{
95
167k
    nxm_init();
96
97
167k
    struct shash_node *node = shash_find_len(&mf_by_name, name, len);
98
167k
    return node ? node->data : NULL;
99
167k
}
100
101
static void
102
nxm_do_init(void)
103
1
{
104
1
    int i;
105
106
1
    shash_init(&mf_by_name);
107
212
    for (i = 0; i < MFF_N_IDS; i++) {
108
211
        const struct mf_field *mf = &mf_fields[i];
109
110
211
        ovs_assert(mf->id == i); /* Fields must be in the enum order. */
111
112
211
        shash_add_once(&mf_by_name, mf->name, mf);
113
211
        if (mf->extra_name) {
114
17
            shash_add_once(&mf_by_name, mf->extra_name, mf);
115
17
        }
116
211
    }
117
1
}
118
119
static void
120
nxm_init(void)
121
364k
{
122
364k
    static pthread_once_t once = PTHREAD_ONCE_INIT;
123
364k
    pthread_once(&once, nxm_do_init);
124
364k
}
125
126
/* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
127
 * restrictions on a field's value.  Then, this function initializes
128
 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
129
 * This is not always possible, i.e. if one pair insists on a value of 0 in
130
 * some bit and the other pair insists on a value of 1 in that bit.  This
131
 * function returns false in a case where the combined restriction is
132
 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
133
 * true otherwise.
134
 *
135
 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
136
 * a corresponding 1-bit in its mask.) */
137
bool
138
mf_subvalue_intersect(const union mf_subvalue *a_value,
139
                      const union mf_subvalue *a_mask,
140
                      const union mf_subvalue *b_value,
141
                      const union mf_subvalue *b_mask,
142
                      union mf_subvalue *dst_value,
143
                      union mf_subvalue *dst_mask)
144
0
{
145
0
    for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
146
0
        ovs_be64 av = a_value->be64[i];
147
0
        ovs_be64 am = a_mask->be64[i];
148
0
        ovs_be64 bv = b_value->be64[i];
149
0
        ovs_be64 bm = b_mask->be64[i];
150
0
        ovs_be64 *dv = &dst_value->be64[i];
151
0
        ovs_be64 *dm = &dst_mask->be64[i];
152
153
0
        if ((av ^ bv) & (am & bm)) {
154
0
            return false;
155
0
        }
156
0
        *dv = av | bv;
157
0
        *dm = am | bm;
158
0
    }
159
0
    return true;
160
0
}
161
162
/* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
163
 * set, 2 if the second-lowest-order bit is set, and so on. */
164
int
165
mf_subvalue_width(const union mf_subvalue *v)
166
0
{
167
0
    return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
168
0
}
169
170
/* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
171
 * negative 'n', shifts the bits '-n' bits to the right. */
172
void
173
mf_subvalue_shift(union mf_subvalue *value, int n)
174
0
{
175
0
    if (n) {
176
0
        union mf_subvalue tmp;
177
0
        memset(&tmp, 0, sizeof tmp);
178
179
0
        if (n > 0 && n < 8 * sizeof tmp) {
180
0
            bitwise_copy(value, sizeof *value, 0,
181
0
                         &tmp, sizeof tmp, n,
182
0
                         8 * sizeof tmp - n);
183
0
        } else if (n < 0 && n > -8 * sizeof tmp) {
184
0
            bitwise_copy(value, sizeof *value, -n,
185
0
                         &tmp, sizeof tmp, 0,
186
0
                         8 * sizeof tmp + n);
187
0
        }
188
0
        *value = tmp;
189
0
    }
190
0
}
191
192
/* Appends a formatted representation of 'sv' to 's'. */
193
void
194
mf_subvalue_format(const union mf_subvalue *sv, struct ds *s)
195
2.87k
{
196
2.87k
    ds_put_hex(s, sv, sizeof *sv);
197
2.87k
}
198
199
/* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
200
 * specifies at least one bit in the field.
201
 *
202
 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
203
 * meets 'mf''s prerequisites. */
204
bool
205
mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
206
169k
{
207
169k
    switch (mf->id) {
208
718
    case MFF_DP_HASH:
209
718
        return !wc->masks.dp_hash;
210
510
    case MFF_RECIRC_ID:
211
510
        return !wc->masks.recirc_id;
212
13.9k
    case MFF_PACKET_TYPE:
213
13.9k
        return !wc->masks.packet_type;
214
243
    case MFF_CONJ_ID:
215
243
        return !wc->masks.conj_id;
216
471
    case MFF_TUN_SRC:
217
471
        return !wc->masks.tunnel.ip_src;
218
229
    case MFF_TUN_DST:
219
229
        return !wc->masks.tunnel.ip_dst;
220
1.05k
    case MFF_TUN_IPV6_SRC:
221
1.05k
        return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_src);
222
273
    case MFF_TUN_IPV6_DST:
223
273
        return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_dst);
224
299
    case MFF_TUN_ID:
225
299
        return !wc->masks.tunnel.tun_id;
226
0
    case MFF_TUN_TOS:
227
0
        return !wc->masks.tunnel.ip_tos;
228
0
    case MFF_TUN_TTL:
229
0
        return !wc->masks.tunnel.ip_ttl;
230
343
    case MFF_TUN_FLAGS:
231
343
        return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
232
576
    case MFF_TUN_GBP_ID:
233
576
        return !wc->masks.tunnel.gbp_id;
234
398
    case MFF_TUN_GBP_FLAGS:
235
398
        return !wc->masks.tunnel.gbp_flags;
236
511
    case MFF_TUN_ERSPAN_VER:
237
511
        return !wc->masks.tunnel.erspan_ver;
238
236
    case MFF_TUN_ERSPAN_IDX:
239
236
        return !wc->masks.tunnel.erspan_idx;
240
352
    case MFF_TUN_ERSPAN_DIR:
241
352
        return !wc->masks.tunnel.erspan_dir;
242
716
    case MFF_TUN_ERSPAN_HWID:
243
716
        return !wc->masks.tunnel.erspan_hwid;
244
78.3k
    CASE_MFF_TUN_METADATA:
245
78.3k
        return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
246
2.91M
                           mf->id - MFF_TUN_METADATA0);
247
479
    case MFF_METADATA:
248
479
        return !wc->masks.metadata;
249
1.74k
    case MFF_IN_PORT:
250
2.23k
    case MFF_IN_PORT_OXM:
251
2.23k
        return !wc->masks.in_port.ofp_port;
252
0
    case MFF_SKB_PRIORITY:
253
0
        return !wc->masks.skb_priority;
254
197
    case MFF_PKT_MARK:
255
197
        return !wc->masks.pkt_mark;
256
684
    case MFF_CT_STATE:
257
684
        return !wc->masks.ct_state;
258
288
    case MFF_CT_ZONE:
259
288
        return !wc->masks.ct_zone;
260
287
    case MFF_CT_MARK:
261
287
        return !wc->masks.ct_mark;
262
782
    case MFF_CT_LABEL:
263
782
        return ovs_u128_is_zero(wc->masks.ct_label);
264
317
    case MFF_CT_NW_PROTO:
265
317
        return !wc->masks.ct_nw_proto;
266
461
    case MFF_CT_NW_SRC:
267
461
        return !wc->masks.ct_nw_src;
268
117
    case MFF_CT_NW_DST:
269
117
        return !wc->masks.ct_nw_dst;
270
286
    case MFF_CT_TP_SRC:
271
286
        return !wc->masks.ct_tp_src;
272
133
    case MFF_CT_TP_DST:
273
133
        return !wc->masks.ct_tp_dst;
274
594
    case MFF_CT_IPV6_SRC:
275
594
        return ipv6_mask_is_any(&wc->masks.ct_ipv6_src);
276
1.23k
    case MFF_CT_IPV6_DST:
277
1.23k
        return ipv6_mask_is_any(&wc->masks.ct_ipv6_dst);
278
13.8k
    CASE_MFF_REGS:
279
13.8k
        return !wc->masks.regs[mf->id - MFF_REG0];
280
5.53k
    CASE_MFF_XREGS:
281
5.53k
        return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
282
4.69k
    CASE_MFF_XXREGS: {
283
4.69k
        ovs_u128 value = flow_get_xxreg(&wc->masks, mf->id - MFF_XXREG0);
284
4.69k
        return ovs_u128_is_zero(value);
285
22.8k
    }
286
697
    case MFF_ACTSET_OUTPUT:
287
697
        return !wc->masks.actset_output;
288
289
306
    case MFF_ETH_SRC:
290
306
        return eth_addr_is_zero(wc->masks.dl_src);
291
194
    case MFF_ETH_DST:
292
194
        return eth_addr_is_zero(wc->masks.dl_dst);
293
377
    case MFF_ETH_TYPE:
294
377
        return !wc->masks.dl_type;
295
296
98
    case MFF_ARP_SHA:
297
544
    case MFF_ND_SLL:
298
544
        return eth_addr_is_zero(wc->masks.arp_sha);
299
300
422
    case MFF_ARP_THA:
301
2.57k
    case MFF_ND_TLL:
302
2.57k
        return eth_addr_is_zero(wc->masks.arp_tha);
303
304
658
    case MFF_VLAN_TCI:
305
658
        return !wc->masks.vlans[0].tci;
306
0
    case MFF_DL_VLAN:
307
0
        return !(wc->masks.vlans[0].tci & htons(VLAN_VID_MASK));
308
478
    case MFF_VLAN_VID:
309
478
        return !(wc->masks.vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI));
310
0
    case MFF_DL_VLAN_PCP:
311
500
    case MFF_VLAN_PCP:
312
500
        return !(wc->masks.vlans[0].tci & htons(VLAN_PCP_MASK));
313
314
862
    case MFF_MPLS_LABEL:
315
862
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
316
219
    case MFF_MPLS_TC:
317
219
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
318
330
    case MFF_MPLS_BOS:
319
330
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
320
1.43k
    case MFF_MPLS_TTL:
321
1.43k
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
322
323
188
    case MFF_IPV4_SRC:
324
188
        return !wc->masks.nw_src;
325
887
    case MFF_IPV4_DST:
326
887
        return !wc->masks.nw_dst;
327
328
801
    case MFF_IPV6_SRC:
329
801
        return ipv6_mask_is_any(&wc->masks.ipv6_src);
330
1.69k
    case MFF_IPV6_DST:
331
1.69k
        return ipv6_mask_is_any(&wc->masks.ipv6_dst);
332
333
463
    case MFF_IPV6_LABEL:
334
463
        return !wc->masks.ipv6_label;
335
336
2.47k
    case MFF_IP_PROTO:
337
2.47k
        return !wc->masks.nw_proto;
338
71
    case MFF_IP_DSCP:
339
1.20k
    case MFF_IP_DSCP_SHIFTED:
340
1.20k
        return !(wc->masks.nw_tos & IP_DSCP_MASK);
341
466
    case MFF_IP_ECN:
342
466
        return !(wc->masks.nw_tos & IP_ECN_MASK);
343
477
    case MFF_IP_TTL:
344
477
        return !wc->masks.nw_ttl;
345
346
1.38k
    case MFF_ND_TARGET:
347
1.38k
        return ipv6_mask_is_any(&wc->masks.nd_target);
348
349
1.19k
    case MFF_ND_RESERVED:
350
1.19k
        return !wc->masks.igmp_group_ip4;
351
532
    case MFF_ND_OPTIONS_TYPE:
352
532
        return !wc->masks.tcp_flags;
353
354
917
    case MFF_IP_FRAG:
355
917
        return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
356
357
636
    case MFF_ARP_OP:
358
636
        return !wc->masks.nw_proto;
359
375
    case MFF_ARP_SPA:
360
375
        return !wc->masks.nw_src;
361
1.58k
    case MFF_ARP_TPA:
362
1.58k
        return !wc->masks.nw_dst;
363
364
834
    case MFF_TCP_SRC:
365
1.21k
    case MFF_UDP_SRC:
366
1.55k
    case MFF_SCTP_SRC:
367
1.72k
    case MFF_ICMPV4_TYPE:
368
2.47k
    case MFF_ICMPV6_TYPE:
369
2.47k
        return !wc->masks.tp_src;
370
117
    case MFF_TCP_DST:
371
923
    case MFF_UDP_DST:
372
1.37k
    case MFF_SCTP_DST:
373
1.68k
    case MFF_ICMPV4_CODE:
374
1.83k
    case MFF_ICMPV6_CODE:
375
1.83k
        return !wc->masks.tp_dst;
376
660
    case MFF_TCP_FLAGS:
377
660
        return !wc->masks.tcp_flags;
378
379
767
    case MFF_NSH_FLAGS:
380
767
        return !wc->masks.nsh.flags;
381
545
    case MFF_NSH_TTL:
382
545
        return !wc->masks.nsh.ttl;
383
212
    case MFF_NSH_MDTYPE:
384
212
        return !wc->masks.nsh.mdtype;
385
1.63k
    case MFF_NSH_NP:
386
1.63k
        return !wc->masks.nsh.np;
387
353
    case MFF_NSH_SPI:
388
353
        return !(wc->masks.nsh.path_hdr & htonl(NSH_SPI_MASK));
389
483
    case MFF_NSH_SI:
390
483
        return !(wc->masks.nsh.path_hdr & htonl(NSH_SI_MASK));
391
539
    case MFF_NSH_C1:
392
2.66k
    case MFF_NSH_C2:
393
2.83k
    case MFF_NSH_C3:
394
3.05k
    case MFF_NSH_C4:
395
3.05k
        return !wc->masks.nsh.context[mf->id - MFF_NSH_C1];
396
687
    case MFF_TUN_GTPU_FLAGS:
397
687
        return !wc->masks.tunnel.gtpu_flags;
398
590
    case MFF_TUN_GTPU_MSGTYPE:
399
590
        return !wc->masks.tunnel.gtpu_msgtype;
400
401
0
    case MFF_N_IDS:
402
0
    default:
403
0
        OVS_NOT_REACHED();
404
169k
    }
405
169k
}
406
407
/* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
408
 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
409
 * purposes, or to 0 if it is wildcarded.
410
 *
411
 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
412
 * meets 'mf''s prerequisites. */
413
void
414
mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
415
            union mf_value *mask)
416
130k
{
417
130k
    mf_get_value(mf, &wc->masks, mask);
418
130k
}
419
420
/* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'.  Returns true
421
 * if the mask is valid, false otherwise. */
422
bool
423
mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
424
309k
{
425
309k
    switch (mf->maskable) {
426
54.2k
    case MFM_NONE:
427
54.2k
        return (is_all_zeros(mask, mf->n_bytes) ||
428
41.2k
                is_all_ones(mask, mf->n_bytes));
429
430
255k
    case MFM_FULLY:
431
255k
        return true;
432
309k
    }
433
434
309k
    OVS_NOT_REACHED();
435
309k
}
436
437
/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
438
 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
439
 * Sets inspected bits in 'wc', if non-NULL. */
440
static bool
441
mf_are_prereqs_ok__(const struct mf_field *mf, const struct flow *flow,
442
                    const struct flow_wildcards *mask,
443
                    struct flow_wildcards *wc)
444
140k
{
445
140k
    ovs_be16 dl_type = get_dl_type(flow);
446
447
140k
    switch (mf->prereqs) {
448
113k
    case MFP_NONE:
449
113k
        return true;
450
3.74k
    case MFP_ETHERNET:
451
3.74k
        return is_ethernet(flow, wc);
452
1.55k
    case MFP_ARP:
453
1.55k
        return (dl_type == htons(ETH_TYPE_ARP) ||
454
992
                dl_type == htons(ETH_TYPE_RARP));
455
512
    case MFP_IPV4:
456
512
        return dl_type == htons(ETH_TYPE_IP);
457
448
    case MFP_IPV6:
458
448
        return dl_type == htons(ETH_TYPE_IPV6);
459
947
    case MFP_VLAN_VID:
460
947
        return is_vlan(flow, wc);
461
671
    case MFP_MPLS:
462
671
        return eth_type_mpls(dl_type);
463
5.01k
    case MFP_IP_ANY:
464
5.01k
        return is_ip_any(flow);
465
681
    case MFP_NSH:
466
681
        return dl_type == htons(ETH_TYPE_NSH);
467
2.80k
    case MFP_CT_VALID:
468
2.80k
        return is_ct_valid(flow, mask, wc);
469
2.05k
    case MFP_TCP:
470
        /* Matching !FRAG_LATER is not enforced (mask is not checked). */
471
2.05k
        return is_tcp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
472
1.48k
    case MFP_UDP:
473
1.48k
        return is_udp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
474
781
    case MFP_SCTP:
475
781
        return is_sctp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
476
1.12k
    case MFP_ICMPV4:
477
1.12k
        return is_icmpv4(flow, wc);
478
1.42k
    case MFP_ICMPV6:
479
1.42k
        return is_icmpv6(flow, wc);
480
630
    case MFP_ND:
481
630
        return is_nd(flow, wc);
482
1.89k
    case MFP_ND_SOLICIT:
483
1.89k
        return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT);
484
1.42k
    case MFP_ND_ADVERT:
485
1.42k
        return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_ADVERT);
486
140k
    }
487
488
140k
    OVS_NOT_REACHED();
489
140k
}
490
491
/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
492
 * Sets inspected bits in 'wc', if non-NULL. */
493
bool
494
mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow,
495
                  struct flow_wildcards *wc)
496
32.2k
{
497
32.2k
    return mf_are_prereqs_ok__(mf, flow, NULL, wc);
498
32.2k
}
499
500
/* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
501
 */
502
bool
503
mf_are_match_prereqs_ok(const struct mf_field *mf, const struct match *match)
504
108k
{
505
108k
    return mf_are_prereqs_ok__(mf, &match->flow, &match->wc, NULL);
506
108k
}
507
508
/* Returns true if 'value' may be a valid value *as part of a masked match*,
509
 * false otherwise.
510
 *
511
 * A value is not rejected just because it is not valid for the field in
512
 * question, but only if it doesn't make sense to test the bits in question at
513
 * all.  For example, the MFF_VLAN_TCI field will never have a nonzero value
514
 * without the VLAN_CFI bit being set, but we can't reject those values because
515
 * it is still legitimate to test just for those bits (see the documentation
516
 * for NXM_OF_VLAN_TCI in meta-flow.h).  On the other hand, there is never a
517
 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
518
bool
519
mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
520
242k
{
521
242k
    switch (mf->id) {
522
718
    case MFF_DP_HASH:
523
1.22k
    case MFF_RECIRC_ID:
524
15.1k
    case MFF_PACKET_TYPE:
525
15.4k
    case MFF_CONJ_ID:
526
15.8k
    case MFF_TUN_ID:
527
16.4k
    case MFF_TUN_SRC:
528
16.7k
    case MFF_TUN_DST:
529
18.0k
    case MFF_TUN_IPV6_SRC:
530
18.4k
    case MFF_TUN_IPV6_DST:
531
18.4k
    case MFF_TUN_TOS:
532
18.4k
    case MFF_TUN_TTL:
533
19.2k
    case MFF_TUN_GBP_ID:
534
19.8k
    case MFF_TUN_GBP_FLAGS:
535
20.0k
    case MFF_TUN_ERSPAN_IDX:
536
20.5k
    case MFF_TUN_ERSPAN_VER:
537
20.9k
    case MFF_TUN_ERSPAN_DIR:
538
21.6k
    case MFF_TUN_ERSPAN_HWID:
539
22.3k
    case MFF_TUN_GTPU_FLAGS:
540
22.9k
    case MFF_TUN_GTPU_MSGTYPE:
541
5.41M
    CASE_MFF_TUN_METADATA:
542
5.41M
    case MFF_METADATA:
543
135k
    case MFF_IN_PORT:
544
135k
    case MFF_SKB_PRIORITY:
545
135k
    case MFF_PKT_MARK:
546
135k
    case MFF_CT_ZONE:
547
136k
    case MFF_CT_MARK:
548
137k
    case MFF_CT_LABEL:
549
138k
    case MFF_CT_NW_PROTO:
550
138k
    case MFF_CT_NW_SRC:
551
138k
    case MFF_CT_NW_DST:
552
139k
    case MFF_CT_IPV6_SRC:
553
140k
    case MFF_CT_IPV6_DST:
554
140k
    case MFF_CT_TP_SRC:
555
141k
    case MFF_CT_TP_DST:
556
4.96M
    CASE_MFF_REGS:
557
4.96M
    CASE_MFF_XREGS:
558
2.72M
    CASE_MFF_XXREGS:
559
1.45M
    case MFF_ETH_SRC:
560
185k
    case MFF_ETH_DST:
561
185k
    case MFF_ETH_TYPE:
562
188k
    case MFF_VLAN_TCI:
563
189k
    case MFF_MPLS_TTL:
564
190k
    case MFF_IPV4_SRC:
565
191k
    case MFF_IPV4_DST:
566
192k
    case MFF_IPV6_SRC:
567
194k
    case MFF_IPV6_DST:
568
197k
    case MFF_IP_PROTO:
569
198k
    case MFF_IP_TTL:
570
199k
    case MFF_ARP_SPA:
571
201k
    case MFF_ARP_TPA:
572
201k
    case MFF_ARP_SHA:
573
202k
    case MFF_ARP_THA:
574
203k
    case MFF_TCP_SRC:
575
204k
    case MFF_TCP_DST:
576
204k
    case MFF_UDP_SRC:
577
205k
    case MFF_UDP_DST:
578
206k
    case MFF_SCTP_SRC:
579
207k
    case MFF_SCTP_DST:
580
207k
    case MFF_ICMPV4_TYPE:
581
209k
    case MFF_ICMPV4_CODE:
582
210k
    case MFF_ICMPV6_TYPE:
583
210k
    case MFF_ICMPV6_CODE:
584
212k
    case MFF_ND_TARGET:
585
214k
    case MFF_ND_SLL:
586
217k
    case MFF_ND_TLL:
587
218k
    case MFF_ND_RESERVED:
588
219k
    case MFF_ND_OPTIONS_TYPE:
589
219k
        return true;
590
591
502
    case MFF_IN_PORT_OXM:
592
1.20k
    case MFF_ACTSET_OUTPUT: {
593
1.20k
        ofp_port_t port;
594
1.20k
        return !ofputil_port_from_ofp11(value->be32, &port);
595
502
    }
596
597
278
    case MFF_IP_DSCP:
598
278
        return !(value->u8 & ~IP_DSCP_MASK);
599
1.64k
    case MFF_IP_DSCP_SHIFTED:
600
1.64k
        return !(value->u8 & (~IP_DSCP_MASK >> 2));
601
987
    case MFF_IP_ECN:
602
987
        return !(value->u8 & ~IP_ECN_MASK);
603
917
    case MFF_IP_FRAG:
604
917
        return !(value->u8 & ~FLOW_NW_FRAG_MASK);
605
663
    case MFF_TCP_FLAGS:
606
663
        return !(value->be16 & ~htons(0x0fff));
607
608
844
    case MFF_ARP_OP:
609
844
        return !(value->be16 & htons(0xff00));
610
611
194
    case MFF_DL_VLAN:
612
194
        return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
613
1.56k
    case MFF_VLAN_VID:
614
1.56k
        return !(value->be16 & htons(VLAN_PCP_MASK));
615
616
194
    case MFF_DL_VLAN_PCP:
617
1.15k
    case MFF_VLAN_PCP:
618
1.15k
        return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
619
620
547
    case MFF_IPV6_LABEL:
621
547
        return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
622
623
1.06k
    case MFF_MPLS_LABEL:
624
1.06k
        return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
625
626
680
    case MFF_MPLS_TC:
627
680
        return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
628
629
334
    case MFF_MPLS_BOS:
630
334
        return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
631
632
449
    case MFF_TUN_FLAGS:
633
449
        return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
634
635
697
    case MFF_CT_STATE:
636
697
        return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
637
638
1.26k
    case MFF_NSH_FLAGS:
639
1.26k
        return true;
640
712
    case MFF_NSH_TTL:
641
712
        return (value->u8 <= 63);
642
288
    case MFF_NSH_MDTYPE:
643
288
        return (value->u8 == 1 || value->u8 == 2);
644
1.63k
    case MFF_NSH_NP:
645
1.63k
        return true;
646
633
    case MFF_NSH_SPI:
647
633
        return !(value->be32 & htonl(0xFF000000));
648
687
    case MFF_NSH_SI:
649
1.48k
    case MFF_NSH_C1:
650
3.90k
    case MFF_NSH_C2:
651
4.68k
    case MFF_NSH_C3:
652
5.12k
    case MFF_NSH_C4:
653
5.12k
        return true;
654
655
0
    case MFF_N_IDS:
656
0
    default:
657
0
        OVS_NOT_REACHED();
658
242k
    }
659
242k
}
660
661
/* Copies the value of field 'mf' from 'flow' into 'value'.  The caller is
662
 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
663
void
664
mf_get_value(const struct mf_field *mf, const struct flow *flow,
665
             union mf_value *value)
666
261k
{
667
261k
    switch (mf->id) {
668
1.20k
    case MFF_DP_HASH:
669
1.20k
        value->be32 = htonl(flow->dp_hash);
670
1.20k
        break;
671
1.46k
    case MFF_RECIRC_ID:
672
1.46k
        value->be32 = htonl(flow->recirc_id);
673
1.46k
        break;
674
5.81k
    case MFF_PACKET_TYPE:
675
5.81k
        value->be32 = flow->packet_type;
676
5.81k
        break;
677
1.18k
    case MFF_CONJ_ID:
678
1.18k
        value->be32 = htonl(flow->conj_id);
679
1.18k
        break;
680
2.96k
    case MFF_TUN_ID:
681
2.96k
        value->be64 = flow->tunnel.tun_id;
682
2.96k
        break;
683
940
    case MFF_TUN_SRC:
684
940
        value->be32 = flow->tunnel.ip_src;
685
940
        break;
686
996
    case MFF_TUN_DST:
687
996
        value->be32 = flow->tunnel.ip_dst;
688
996
        break;
689
604
    case MFF_TUN_IPV6_SRC:
690
604
        value->ipv6 = flow->tunnel.ipv6_src;
691
604
        break;
692
1.39k
    case MFF_TUN_IPV6_DST:
693
1.39k
        value->ipv6 = flow->tunnel.ipv6_dst;
694
1.39k
        break;
695
870
    case MFF_TUN_FLAGS:
696
870
        value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
697
870
        break;
698
892
    case MFF_TUN_GBP_ID:
699
892
        value->be16 = flow->tunnel.gbp_id;
700
892
        break;
701
2.30k
    case MFF_TUN_GBP_FLAGS:
702
2.30k
        value->u8 = flow->tunnel.gbp_flags;
703
2.30k
        break;
704
1.27k
    case MFF_TUN_TTL:
705
1.27k
        value->u8 = flow->tunnel.ip_ttl;
706
1.27k
        break;
707
4.03k
    case MFF_TUN_TOS:
708
4.03k
        value->u8 = flow->tunnel.ip_tos;
709
4.03k
        break;
710
516
    case MFF_TUN_ERSPAN_VER:
711
516
        value->u8 = flow->tunnel.erspan_ver;
712
516
        break;
713
516
    case MFF_TUN_ERSPAN_IDX:
714
516
        value->be32 = htonl(flow->tunnel.erspan_idx);
715
516
        break;
716
260
    case MFF_TUN_ERSPAN_DIR:
717
260
        value->u8 = flow->tunnel.erspan_dir;
718
260
        break;
719
0
    case MFF_TUN_ERSPAN_HWID:
720
0
        value->u8 = flow->tunnel.erspan_hwid;
721
0
        break;
722
810
    case MFF_TUN_GTPU_FLAGS:
723
810
        value->u8 = flow->tunnel.gtpu_flags;
724
810
        break;
725
0
    case MFF_TUN_GTPU_MSGTYPE:
726
0
        value->u8 = flow->tunnel.gtpu_msgtype;
727
0
        break;
728
101k
    CASE_MFF_TUN_METADATA:
729
101k
        tun_metadata_read(&flow->tunnel, mf, value);
730
101k
        break;
731
732
650
    case MFF_METADATA:
733
650
        value->be64 = flow->metadata;
734
650
        break;
735
736
894
    case MFF_IN_PORT:
737
894
        value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
738
894
        break;
739
4.13k
    case MFF_IN_PORT_OXM:
740
4.13k
        value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
741
4.13k
        break;
742
2.86k
    case MFF_ACTSET_OUTPUT:
743
2.86k
        value->be32 = ofputil_port_to_ofp11(flow->actset_output);
744
2.86k
        break;
745
746
0
    case MFF_SKB_PRIORITY:
747
0
        value->be32 = htonl(flow->skb_priority);
748
0
        break;
749
750
1.44k
    case MFF_PKT_MARK:
751
1.44k
        value->be32 = htonl(flow->pkt_mark);
752
1.44k
        break;
753
754
4.52k
    case MFF_CT_STATE:
755
4.52k
        value->be32 = htonl(flow->ct_state);
756
4.52k
        break;
757
758
668
    case MFF_CT_ZONE:
759
668
        value->be16 = htons(flow->ct_zone);
760
668
        break;
761
762
3.50k
    case MFF_CT_MARK:
763
3.50k
        value->be32 = htonl(flow->ct_mark);
764
3.50k
        break;
765
766
902
    case MFF_CT_LABEL:
767
902
        value->be128 = hton128(flow->ct_label);
768
902
        break;
769
770
3.82k
    case MFF_CT_NW_PROTO:
771
3.82k
        value->u8 = flow->ct_nw_proto;
772
3.82k
        break;
773
774
912
    case MFF_CT_NW_SRC:
775
912
        value->be32 = flow->ct_nw_src;
776
912
        break;
777
778
1.74k
    case MFF_CT_NW_DST:
779
1.74k
        value->be32 = flow->ct_nw_dst;
780
1.74k
        break;
781
782
588
    case MFF_CT_IPV6_SRC:
783
588
        value->ipv6 = flow->ct_ipv6_src;
784
588
        break;
785
786
1.26k
    case MFF_CT_IPV6_DST:
787
1.26k
        value->ipv6 = flow->ct_ipv6_dst;
788
1.26k
        break;
789
790
1.42k
    case MFF_CT_TP_SRC:
791
1.42k
        value->be16 = flow->ct_tp_src;
792
1.42k
        break;
793
794
1.43k
    case MFF_CT_TP_DST:
795
1.43k
        value->be16 = flow->ct_tp_dst;
796
1.43k
        break;
797
798
30.2k
    CASE_MFF_REGS:
799
30.2k
        value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
800
30.2k
        break;
801
802
14.8k
    CASE_MFF_XREGS:
803
14.8k
        value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
804
14.8k
        break;
805
806
8.77k
    CASE_MFF_XXREGS:
807
8.77k
        value->be128 = hton128(flow_get_xxreg(flow, mf->id - MFF_XXREG0));
808
8.77k
        break;
809
810
688
    case MFF_ETH_SRC:
811
688
        value->mac = flow->dl_src;
812
688
        break;
813
814
1.05k
    case MFF_ETH_DST:
815
1.05k
        value->mac = flow->dl_dst;
816
1.05k
        break;
817
818
1.36k
    case MFF_ETH_TYPE:
819
1.36k
        value->be16 = flow->dl_type;
820
1.36k
        break;
821
822
614
    case MFF_VLAN_TCI:
823
614
        value->be16 = flow->vlans[0].tci;
824
614
        break;
825
826
516
    case MFF_DL_VLAN:
827
516
        value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK);
828
516
        break;
829
522
    case MFF_VLAN_VID:
830
522
        value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI);
831
522
        break;
832
833
646
    case MFF_DL_VLAN_PCP:
834
1.43k
    case MFF_VLAN_PCP:
835
1.43k
        value->u8 = vlan_tci_to_pcp(flow->vlans[0].tci);
836
1.43k
        break;
837
838
580
    case MFF_MPLS_LABEL:
839
580
        value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
840
580
        break;
841
842
518
    case MFF_MPLS_TC:
843
518
        value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
844
518
        break;
845
846
516
    case MFF_MPLS_BOS:
847
516
        value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
848
516
        break;
849
850
322
    case MFF_MPLS_TTL:
851
322
        value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
852
322
        break;
853
854
1.66k
    case MFF_IPV4_SRC:
855
1.66k
        value->be32 = flow->nw_src;
856
1.66k
        break;
857
858
1.06k
    case MFF_IPV4_DST:
859
1.06k
        value->be32 = flow->nw_dst;
860
1.06k
        break;
861
862
848
    case MFF_IPV6_SRC:
863
848
        value->ipv6 = flow->ipv6_src;
864
848
        break;
865
866
1.07k
    case MFF_IPV6_DST:
867
1.07k
        value->ipv6 = flow->ipv6_dst;
868
1.07k
        break;
869
870
1.43k
    case MFF_IPV6_LABEL:
871
1.43k
        value->be32 = flow->ipv6_label;
872
1.43k
        break;
873
874
856
    case MFF_IP_PROTO:
875
856
        value->u8 = flow->nw_proto;
876
856
        break;
877
878
604
    case MFF_IP_DSCP:
879
604
        value->u8 = flow->nw_tos & IP_DSCP_MASK;
880
604
        break;
881
882
518
    case MFF_IP_DSCP_SHIFTED:
883
518
        value->u8 = flow->nw_tos >> 2;
884
518
        break;
885
886
516
    case MFF_IP_ECN:
887
516
        value->u8 = flow->nw_tos & IP_ECN_MASK;
888
516
        break;
889
890
928
    case MFF_IP_TTL:
891
928
        value->u8 = flow->nw_ttl;
892
928
        break;
893
894
548
    case MFF_IP_FRAG:
895
548
        value->u8 = flow->nw_frag;
896
548
        break;
897
898
658
    case MFF_ARP_OP:
899
658
        value->be16 = htons(flow->nw_proto);
900
658
        break;
901
902
960
    case MFF_ARP_SPA:
903
960
        value->be32 = flow->nw_src;
904
960
        break;
905
906
906
    case MFF_ARP_TPA:
907
906
        value->be32 = flow->nw_dst;
908
906
        break;
909
910
1.41k
    case MFF_ARP_SHA:
911
2.32k
    case MFF_ND_SLL:
912
2.32k
        value->mac = flow->arp_sha;
913
2.32k
        break;
914
915
1.02k
    case MFF_ARP_THA:
916
1.92k
    case MFF_ND_TLL:
917
1.92k
        value->mac = flow->arp_tha;
918
1.92k
        break;
919
920
782
    case MFF_TCP_SRC:
921
1.34k
    case MFF_UDP_SRC:
922
2.07k
    case MFF_SCTP_SRC:
923
2.07k
        value->be16 = flow->tp_src;
924
2.07k
        break;
925
926
1.16k
    case MFF_TCP_DST:
927
2.09k
    case MFF_UDP_DST:
928
3.00k
    case MFF_SCTP_DST:
929
3.00k
        value->be16 = flow->tp_dst;
930
3.00k
        break;
931
932
666
    case MFF_TCP_FLAGS:
933
666
    case MFF_ND_OPTIONS_TYPE:
934
666
        value->be16 = flow->tcp_flags;
935
666
        break;
936
937
0
    case MFF_ND_RESERVED:
938
0
        value->be32 = flow->igmp_group_ip4;
939
0
        break;
940
941
918
    case MFF_ICMPV4_TYPE:
942
1.43k
    case MFF_ICMPV6_TYPE:
943
1.43k
        value->u8 = ntohs(flow->tp_src);
944
1.43k
        break;
945
946
910
    case MFF_ICMPV4_CODE:
947
1.84k
    case MFF_ICMPV6_CODE:
948
1.84k
        value->u8 = ntohs(flow->tp_dst);
949
1.84k
        break;
950
951
392
    case MFF_ND_TARGET:
952
392
        value->ipv6 = flow->nd_target;
953
392
        break;
954
955
862
    case MFF_NSH_FLAGS:
956
862
        value->u8 = flow->nsh.flags;
957
862
        break;
958
792
    case MFF_NSH_TTL:
959
792
        value->u8 = flow->nsh.ttl;
960
792
        break;
961
1.58k
    case MFF_NSH_MDTYPE:
962
1.58k
        value->u8 = flow->nsh.mdtype;
963
1.58k
        break;
964
826
    case MFF_NSH_NP:
965
826
        value->u8 = flow->nsh.np;
966
826
        break;
967
2.33k
    case MFF_NSH_SPI:
968
2.33k
        value->be32 = nsh_path_hdr_to_spi(flow->nsh.path_hdr);
969
2.33k
        if (value->be32 == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
970
342
            value->be32 = OVS_BE32_MAX;
971
342
        }
972
2.33k
        break;
973
1.11k
    case MFF_NSH_SI:
974
1.11k
        value->u8 = nsh_path_hdr_to_si(flow->nsh.path_hdr);
975
1.11k
        break;
976
2.74k
    case MFF_NSH_C1:
977
3.83k
    case MFF_NSH_C2:
978
4.86k
    case MFF_NSH_C3:
979
5.66k
    case MFF_NSH_C4:
980
5.66k
        value->be32 = flow->nsh.context[mf->id - MFF_NSH_C1];
981
5.66k
        break;
982
983
0
    case MFF_N_IDS:
984
0
    default:
985
0
        OVS_NOT_REACHED();
986
261k
    }
987
261k
}
988
989
/* Makes 'match' match field 'mf' exactly, with the value matched taken from
990
 * 'value'.  The caller is responsible for ensuring that 'match' meets 'mf''s
991
 * prerequisites.
992
 *
993
 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
994
 * with the request or NULL if there is no error. The caller is reponsible
995
 * for freeing the string. */
996
void
997
mf_set_value(const struct mf_field *mf,
998
             const union mf_value *value, struct match *match, char **err_str)
999
157k
{
1000
157k
    if (err_str) {
1001
123k
        *err_str = NULL;
1002
123k
    }
1003
1004
157k
    switch (mf->id) {
1005
386
    case MFF_DP_HASH:
1006
386
        match_set_dp_hash(match, ntohl(value->be32));
1007
386
        break;
1008
367
    case MFF_RECIRC_ID:
1009
367
        match_set_recirc_id(match, ntohl(value->be32));
1010
367
        break;
1011
16.1k
    case MFF_PACKET_TYPE:
1012
16.1k
        match_set_packet_type(match, value->be32);
1013
16.1k
        break;
1014
242
    case MFF_CONJ_ID:
1015
242
        match_set_conj_id(match, ntohl(value->be32));
1016
242
        break;
1017
671
    case MFF_TUN_ID:
1018
671
        match_set_tun_id(match, value->be64);
1019
671
        break;
1020
462
    case MFF_TUN_SRC:
1021
462
        match_set_tun_src(match, value->be32);
1022
462
        break;
1023
244
    case MFF_TUN_DST:
1024
244
        match_set_tun_dst(match, value->be32);
1025
244
        break;
1026
302
    case MFF_TUN_IPV6_SRC:
1027
302
        match_set_tun_ipv6_src(match, &value->ipv6);
1028
302
        break;
1029
234
    case MFF_TUN_IPV6_DST:
1030
234
        match_set_tun_ipv6_dst(match, &value->ipv6);
1031
234
        break;
1032
511
    case MFF_TUN_FLAGS:
1033
511
        match_set_tun_flags(match, ntohs(value->be16));
1034
511
        break;
1035
421
    case MFF_TUN_GBP_ID:
1036
421
         match_set_tun_gbp_id(match, value->be16);
1037
421
         break;
1038
225
    case MFF_TUN_GBP_FLAGS:
1039
225
         match_set_tun_gbp_flags(match, value->u8);
1040
225
         break;
1041
445
    case MFF_TUN_TOS:
1042
445
        match_set_tun_tos(match, value->u8);
1043
445
        break;
1044
327
    case MFF_TUN_TTL:
1045
327
        match_set_tun_ttl(match, value->u8);
1046
327
        break;
1047
176
    case MFF_TUN_ERSPAN_VER:
1048
176
        match_set_tun_erspan_ver(match, value->u8);
1049
176
        break;
1050
115
    case MFF_TUN_ERSPAN_IDX:
1051
115
        match_set_tun_erspan_idx(match, ntohl(value->be32));
1052
115
        break;
1053
258
    case MFF_TUN_ERSPAN_DIR:
1054
258
        match_set_tun_erspan_dir(match, value->u8);
1055
258
        break;
1056
196
    case MFF_TUN_ERSPAN_HWID:
1057
196
        match_set_tun_erspan_hwid(match, value->u8);
1058
196
        break;
1059
602
    case MFF_TUN_GTPU_FLAGS:
1060
602
        match_set_tun_gtpu_flags(match, value->u8);
1061
602
        break;
1062
34
    case MFF_TUN_GTPU_MSGTYPE:
1063
34
        match_set_tun_gtpu_msgtype(match, value->u8);
1064
34
        break;
1065
62.7k
    CASE_MFF_TUN_METADATA:
1066
62.7k
        tun_metadata_set_match(mf, value, NULL, match, err_str);
1067
62.7k
        break;
1068
1069
401
    case MFF_METADATA:
1070
401
        match_set_metadata(match, value->be64);
1071
401
        break;
1072
1073
1.85k
    case MFF_IN_PORT:
1074
1.85k
        match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1075
1.85k
        break;
1076
1077
2.09k
    case MFF_IN_PORT_OXM: {
1078
2.09k
        ofp_port_t port;
1079
2.09k
        ofputil_port_from_ofp11(value->be32, &port);
1080
2.09k
        match_set_in_port(match, port);
1081
2.09k
        break;
1082
2.11M
    }
1083
1.93k
    case MFF_ACTSET_OUTPUT: {
1084
1.93k
        ofp_port_t port;
1085
1.93k
        ofputil_port_from_ofp11(value->be32, &port);
1086
1.93k
        match_set_actset_output(match, port);
1087
1.93k
        break;
1088
2.11M
    }
1089
1090
0
    case MFF_SKB_PRIORITY:
1091
0
        match_set_skb_priority(match, ntohl(value->be32));
1092
0
        break;
1093
1094
371
    case MFF_PKT_MARK:
1095
371
        match_set_pkt_mark(match, ntohl(value->be32));
1096
371
        break;
1097
1098
2.89k
    case MFF_CT_STATE:
1099
2.89k
        match_set_ct_state(match, ntohl(value->be32));
1100
2.89k
        break;
1101
1102
320
    case MFF_CT_ZONE:
1103
320
        match_set_ct_zone(match, ntohs(value->be16));
1104
320
        break;
1105
1106
337
    case MFF_CT_MARK:
1107
337
        match_set_ct_mark(match, ntohl(value->be32));
1108
337
        break;
1109
1110
281
    case MFF_CT_LABEL:
1111
281
        match_set_ct_label(match, ntoh128(value->be128));
1112
281
        break;
1113
1114
352
    case MFF_CT_NW_PROTO:
1115
352
        match_set_ct_nw_proto(match, value->u8);
1116
352
        break;
1117
1118
492
    case MFF_CT_NW_SRC:
1119
492
        match_set_ct_nw_src(match, value->be32);
1120
492
        break;
1121
1122
225
    case MFF_CT_NW_DST:
1123
225
        match_set_ct_nw_dst(match, value->be32);
1124
225
        break;
1125
1126
307
    case MFF_CT_IPV6_SRC:
1127
307
        match_set_ct_ipv6_src(match, &value->ipv6);
1128
307
        break;
1129
1130
284
    case MFF_CT_IPV6_DST:
1131
284
        match_set_ct_ipv6_dst(match, &value->ipv6);
1132
284
        break;
1133
1134
541
    case MFF_CT_TP_SRC:
1135
541
        match_set_ct_tp_src(match, value->be16);
1136
541
        break;
1137
1138
213
    case MFF_CT_TP_DST:
1139
213
        match_set_ct_tp_dst(match, value->be16);
1140
213
        break;
1141
1142
16.8k
    CASE_MFF_REGS:
1143
16.8k
        match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1144
16.8k
        break;
1145
1146
8.48k
    CASE_MFF_XREGS:
1147
8.48k
        match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
1148
8.48k
        break;
1149
1150
5.03k
    CASE_MFF_XXREGS:
1151
5.03k
        match_set_xxreg(match, mf->id - MFF_XXREG0, ntoh128(value->be128));
1152
5.03k
        break;
1153
1154
459
    case MFF_ETH_SRC:
1155
459
        match_set_dl_src(match, value->mac);
1156
459
        break;
1157
1158
305
    case MFF_ETH_DST:
1159
305
        match_set_dl_dst(match, value->mac);
1160
305
        break;
1161
1162
683
    case MFF_ETH_TYPE:
1163
683
        match_set_dl_type(match, value->be16);
1164
683
        break;
1165
1166
342
    case MFF_VLAN_TCI:
1167
342
        match_set_dl_tci(match, value->be16);
1168
342
        break;
1169
1170
905
    case MFF_DL_VLAN:
1171
905
        match_set_dl_vlan(match, value->be16, 0);
1172
905
        break;
1173
365
    case MFF_VLAN_VID:
1174
365
        match_set_vlan_vid(match, value->be16);
1175
365
        break;
1176
1177
675
    case MFF_DL_VLAN_PCP:
1178
1.21k
    case MFF_VLAN_PCP:
1179
1.21k
        match_set_dl_vlan_pcp(match, value->u8, 0);
1180
1.21k
        break;
1181
1182
81
    case MFF_MPLS_LABEL:
1183
81
        match_set_mpls_label(match, 0, value->be32);
1184
81
        break;
1185
1186
460
    case MFF_MPLS_TC:
1187
460
        match_set_mpls_tc(match, 0, value->u8);
1188
460
        break;
1189
1190
381
    case MFF_MPLS_BOS:
1191
381
        match_set_mpls_bos(match, 0, value->u8);
1192
381
        break;
1193
1194
614
    case MFF_MPLS_TTL:
1195
614
        match_set_mpls_ttl(match, 0, value->u8);
1196
614
        break;
1197
1198
602
    case MFF_IPV4_SRC:
1199
602
        match_set_nw_src(match, value->be32);
1200
602
        break;
1201
1202
1.05k
    case MFF_IPV4_DST:
1203
1.05k
        match_set_nw_dst(match, value->be32);
1204
1.05k
        break;
1205
1206
323
    case MFF_IPV6_SRC:
1207
323
        match_set_ipv6_src(match, &value->ipv6);
1208
323
        break;
1209
1210
596
    case MFF_IPV6_DST:
1211
596
        match_set_ipv6_dst(match, &value->ipv6);
1212
596
        break;
1213
1214
810
    case MFF_IPV6_LABEL:
1215
810
        match_set_ipv6_label(match, value->be32);
1216
810
        break;
1217
1218
1.99k
    case MFF_IP_PROTO:
1219
1.99k
        match_set_nw_proto(match, value->u8);
1220
1.99k
        break;
1221
1222
503
    case MFF_IP_DSCP:
1223
503
        match_set_nw_dscp(match, value->u8);
1224
503
        break;
1225
1226
415
    case MFF_IP_DSCP_SHIFTED:
1227
415
        match_set_nw_dscp(match, value->u8 << 2);
1228
415
        break;
1229
1230
243
    case MFF_IP_ECN:
1231
243
        match_set_nw_ecn(match, value->u8);
1232
243
        break;
1233
1234
633
    case MFF_IP_TTL:
1235
633
        match_set_nw_ttl(match, value->u8);
1236
633
        break;
1237
1238
432
    case MFF_IP_FRAG:
1239
432
        match_set_nw_frag(match, value->u8);
1240
432
        break;
1241
1242
644
    case MFF_ARP_OP:
1243
644
        match_set_nw_proto(match, ntohs(value->be16));
1244
644
        break;
1245
1246
352
    case MFF_ARP_SPA:
1247
352
        match_set_nw_src(match, value->be32);
1248
352
        break;
1249
1250
622
    case MFF_ARP_TPA:
1251
622
        match_set_nw_dst(match, value->be32);
1252
622
        break;
1253
1254
253
    case MFF_ARP_SHA:
1255
571
    case MFF_ND_SLL:
1256
571
        match_set_arp_sha(match, value->mac);
1257
571
        break;
1258
1259
312
    case MFF_ARP_THA:
1260
642
    case MFF_ND_TLL:
1261
642
        match_set_arp_tha(match, value->mac);
1262
642
        break;
1263
1264
939
    case MFF_TCP_SRC:
1265
1.37k
    case MFF_UDP_SRC:
1266
2.08k
    case MFF_SCTP_SRC:
1267
2.08k
        match_set_tp_src(match, value->be16);
1268
2.08k
        break;
1269
1270
352
    case MFF_TCP_DST:
1271
845
    case MFF_UDP_DST:
1272
1.16k
    case MFF_SCTP_DST:
1273
1.16k
        match_set_tp_dst(match, value->be16);
1274
1.16k
        break;
1275
1276
369
    case MFF_TCP_FLAGS:
1277
369
        match_set_tcp_flags(match, value->be16);
1278
369
        break;
1279
1280
332
    case MFF_ICMPV4_TYPE:
1281
928
    case MFF_ICMPV6_TYPE:
1282
928
        match_set_icmp_type(match, value->u8);
1283
928
        break;
1284
1285
551
    case MFF_ICMPV4_CODE:
1286
911
    case MFF_ICMPV6_CODE:
1287
911
        match_set_icmp_code(match, value->u8);
1288
911
        break;
1289
1290
83
    case MFF_ND_TARGET:
1291
83
        match_set_nd_target(match, &value->ipv6);
1292
83
        break;
1293
1294
275
    case MFF_ND_RESERVED:
1295
275
        match_set_nd_reserved(match, value->be32);
1296
275
        break;
1297
1298
145
    case MFF_ND_OPTIONS_TYPE:
1299
145
        match_set_nd_options_type(match, value->u8);
1300
145
        break;
1301
1302
452
    case MFF_NSH_FLAGS:
1303
452
        MATCH_SET_FIELD_UINT8(match, nsh.flags, value->u8);
1304
452
        break;
1305
540
    case MFF_NSH_TTL:
1306
540
        MATCH_SET_FIELD_UINT8(match, nsh.ttl, value->u8);
1307
540
        break;
1308
544
    case MFF_NSH_MDTYPE:
1309
544
        MATCH_SET_FIELD_UINT8(match, nsh.mdtype, value->u8);
1310
544
        break;
1311
1.22k
    case MFF_NSH_NP:
1312
1.22k
        MATCH_SET_FIELD_UINT8(match, nsh.np, value->u8);
1313
1.22k
        break;
1314
770
    case MFF_NSH_SPI:
1315
770
        match->wc.masks.nsh.path_hdr |= htonl(NSH_SPI_MASK);
1316
770
        nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr, value->be32);
1317
770
        break;
1318
606
    case MFF_NSH_SI:
1319
606
        match->wc.masks.nsh.path_hdr |= htonl(NSH_SI_MASK);
1320
606
        nsh_path_hdr_set_si(&match->flow.nsh.path_hdr, value->u8);
1321
606
        break;
1322
1.71k
    case MFF_NSH_C1:
1323
2.42k
    case MFF_NSH_C2:
1324
2.80k
    case MFF_NSH_C3:
1325
3.14k
    case MFF_NSH_C4:
1326
3.14k
        MATCH_SET_FIELD_BE32(match, nsh.context[mf->id - MFF_NSH_C1],
1327
3.14k
                             value->be32);
1328
3.14k
        break;
1329
1330
0
    case MFF_N_IDS:
1331
0
    default:
1332
0
        OVS_NOT_REACHED();
1333
157k
    }
1334
157k
}
1335
1336
/* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1337
 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1338
 * prerequisites. */
1339
void
1340
mf_mask_field_masked(const struct mf_field *mf, const union mf_value *mask,
1341
                     struct flow_wildcards *wc)
1342
0
{
1343
0
    union mf_value temp_mask;
1344
    /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1345
     * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1346
     * valid bits in this case. */
1347
0
    if (mf->id == MFF_DL_VLAN) {
1348
0
        temp_mask.be16 = htons(VLAN_VID_MASK) & mask->be16;
1349
0
        mask = &temp_mask;
1350
0
    }
1351
1352
0
    union mf_value mask_value;
1353
1354
0
    mf_get_value(mf, &wc->masks, &mask_value);
1355
0
    for (size_t i = 0; i < mf->n_bytes; i++) {
1356
0
        mask_value.b[i] |= mask->b[i];
1357
0
    }
1358
0
    mf_set_flow_value(mf, &mask_value, &wc->masks);
1359
0
}
1360
1361
/* Unwildcard 'wc' member field described by 'mf'.  The caller is
1362
 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1363
void
1364
mf_mask_field(const struct mf_field *mf, struct flow_wildcards *wc)
1365
0
{
1366
0
    mf_mask_field_masked(mf, &exact_match_mask, wc);
1367
0
}
1368
1369
static int
1370
field_len(const struct mf_field *mf, const union mf_value *value_)
1371
213k
{
1372
213k
    const uint8_t *value = &value_->u8;
1373
213k
    int i;
1374
1375
213k
    if (!mf->variable_len) {
1376
12.6k
        return mf->n_bytes;
1377
12.6k
    }
1378
1379
201k
    if (!value) {
1380
0
        return 0;
1381
0
    }
1382
1383
24.1M
    for (i = 0; i < mf->n_bytes; i++) {
1384
23.9M
        if (value[i] != 0) {
1385
70.7k
            break;
1386
70.7k
        }
1387
23.9M
    }
1388
1389
201k
    return mf->n_bytes - i;
1390
201k
}
1391
1392
/* Returns the effective length of the field. For fixed length fields,
1393
 * this is just the defined length. For variable length fields, it is
1394
 * the minimum size encoding that retains the same meaning (i.e.
1395
 * discarding leading zeros).
1396
 *
1397
 * 'is_masked' returns (if non-NULL) whether the original contained
1398
 * a mask. Otherwise, a mask that is the same length as the value
1399
 * might be misinterpreted as an exact match. */
1400
int
1401
mf_field_len(const struct mf_field *mf, const union mf_value *value,
1402
             const union mf_value *mask, bool *is_masked_)
1403
143k
{
1404
143k
    int len, mask_len;
1405
143k
    bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1406
1407
143k
    len = field_len(mf, value);
1408
143k
    if (is_masked) {
1409
70.8k
        mask_len = field_len(mf, mask);
1410
70.8k
        len = MAX(len, mask_len);
1411
70.8k
    }
1412
1413
143k
    if (is_masked_) {
1414
143k
        *is_masked_ = is_masked;
1415
143k
    }
1416
1417
143k
    return len;
1418
143k
}
1419
1420
/* Sets 'flow' member field described by 'mf' to 'value'.  The caller is
1421
 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1422
void
1423
mf_set_flow_value(const struct mf_field *mf,
1424
                  const union mf_value *value, struct flow *flow)
1425
0
{
1426
0
    switch (mf->id) {
1427
0
    case MFF_DP_HASH:
1428
0
        flow->dp_hash = ntohl(value->be32);
1429
0
        break;
1430
0
    case MFF_RECIRC_ID:
1431
0
        flow->recirc_id = ntohl(value->be32);
1432
0
        break;
1433
0
    case MFF_PACKET_TYPE:
1434
0
        flow->packet_type = value->be32;
1435
0
        break;
1436
0
    case MFF_CONJ_ID:
1437
0
        flow->conj_id = ntohl(value->be32);
1438
0
        break;
1439
0
    case MFF_TUN_ID:
1440
0
        flow->tunnel.tun_id = value->be64;
1441
0
        break;
1442
0
    case MFF_TUN_SRC:
1443
0
        flow->tunnel.ip_src = value->be32;
1444
0
        break;
1445
0
    case MFF_TUN_DST:
1446
0
        flow->tunnel.ip_dst = value->be32;
1447
0
        break;
1448
0
    case MFF_TUN_IPV6_SRC:
1449
0
        flow->tunnel.ipv6_src = value->ipv6;
1450
0
        break;
1451
0
    case MFF_TUN_IPV6_DST:
1452
0
        flow->tunnel.ipv6_dst = value->ipv6;
1453
0
        break;
1454
0
    case MFF_TUN_FLAGS:
1455
0
        flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1456
0
                             ntohs(value->be16);
1457
0
        break;
1458
0
    case MFF_TUN_GBP_ID:
1459
0
        flow->tunnel.gbp_id = value->be16;
1460
0
        break;
1461
0
    case MFF_TUN_GBP_FLAGS:
1462
0
        flow->tunnel.gbp_flags = value->u8;
1463
0
        break;
1464
0
    case MFF_TUN_TOS:
1465
0
        flow->tunnel.ip_tos = value->u8;
1466
0
        break;
1467
0
    case MFF_TUN_TTL:
1468
0
        flow->tunnel.ip_ttl = value->u8;
1469
0
        break;
1470
0
    case MFF_TUN_ERSPAN_VER:
1471
0
        flow->tunnel.erspan_ver = value->u8;
1472
0
        break;
1473
0
    case MFF_TUN_ERSPAN_IDX:
1474
0
        flow->tunnel.erspan_idx = ntohl(value->be32);
1475
0
        break;
1476
0
    case MFF_TUN_ERSPAN_DIR:
1477
0
        flow->tunnel.erspan_dir = value->u8;
1478
0
        break;
1479
0
    case MFF_TUN_ERSPAN_HWID:
1480
0
        flow->tunnel.erspan_hwid = value->u8;
1481
0
        break;
1482
0
    case MFF_TUN_GTPU_FLAGS:
1483
0
        flow->tunnel.gtpu_flags = value->u8;
1484
0
        break;
1485
0
    case MFF_TUN_GTPU_MSGTYPE:
1486
0
        flow->tunnel.gtpu_msgtype = value->u8;
1487
0
        break;
1488
0
    CASE_MFF_TUN_METADATA:
1489
0
        tun_metadata_write(&flow->tunnel, mf, value);
1490
0
        break;
1491
0
    case MFF_METADATA:
1492
0
        flow->metadata = value->be64;
1493
0
        break;
1494
1495
0
    case MFF_IN_PORT:
1496
0
        flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1497
0
        break;
1498
0
    case MFF_IN_PORT_OXM:
1499
0
        ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1500
0
        break;
1501
0
    case MFF_ACTSET_OUTPUT:
1502
0
        ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1503
0
        break;
1504
1505
0
    case MFF_SKB_PRIORITY:
1506
0
        flow->skb_priority = ntohl(value->be32);
1507
0
        break;
1508
1509
0
    case MFF_PKT_MARK:
1510
0
        flow->pkt_mark = ntohl(value->be32);
1511
0
        break;
1512
1513
0
    case MFF_CT_STATE:
1514
0
        flow->ct_state = ntohl(value->be32);
1515
0
        break;
1516
1517
0
    case MFF_CT_ZONE:
1518
0
        flow->ct_zone = ntohs(value->be16);
1519
0
        break;
1520
1521
0
    case MFF_CT_MARK:
1522
0
        flow->ct_mark = ntohl(value->be32);
1523
0
        break;
1524
1525
0
    case MFF_CT_LABEL:
1526
0
        flow->ct_label = ntoh128(value->be128);
1527
0
        break;
1528
1529
0
    case MFF_CT_NW_PROTO:
1530
0
        flow->ct_nw_proto = value->u8;
1531
0
        break;
1532
1533
0
    case MFF_CT_NW_SRC:
1534
0
        flow->ct_nw_src = value->be32;
1535
0
        break;
1536
1537
0
    case MFF_CT_NW_DST:
1538
0
        flow->ct_nw_dst = value->be32;
1539
0
        break;
1540
1541
0
    case MFF_CT_IPV6_SRC:
1542
0
        flow->ct_ipv6_src = value->ipv6;
1543
0
        break;
1544
1545
0
    case MFF_CT_IPV6_DST:
1546
0
        flow->ct_ipv6_dst = value->ipv6;
1547
0
        break;
1548
1549
0
    case MFF_CT_TP_SRC:
1550
0
        flow->ct_tp_src = value->be16;
1551
0
        break;
1552
1553
0
    case MFF_CT_TP_DST:
1554
0
        flow->ct_tp_dst = value->be16;
1555
0
        break;
1556
1557
0
    CASE_MFF_REGS:
1558
0
        flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1559
0
        break;
1560
1561
0
    CASE_MFF_XREGS:
1562
0
        flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1563
0
        break;
1564
1565
0
    CASE_MFF_XXREGS:
1566
0
        flow_set_xxreg(flow, mf->id - MFF_XXREG0, ntoh128(value->be128));
1567
0
        break;
1568
1569
0
    case MFF_ETH_SRC:
1570
0
        flow->dl_src = value->mac;
1571
0
        break;
1572
1573
0
    case MFF_ETH_DST:
1574
0
        flow->dl_dst = value->mac;
1575
0
        break;
1576
1577
0
    case MFF_ETH_TYPE:
1578
0
        flow->dl_type = value->be16;
1579
0
        break;
1580
1581
0
    case MFF_VLAN_TCI:
1582
0
        flow->vlans[0].tci = value->be16;
1583
0
        flow_fix_vlan_tpid(flow);
1584
0
        break;
1585
1586
0
    case MFF_DL_VLAN:
1587
0
        flow_set_dl_vlan(flow, value->be16, 0);
1588
0
        flow_fix_vlan_tpid(flow);
1589
0
        break;
1590
1591
0
    case MFF_VLAN_VID:
1592
0
        flow_set_vlan_vid(flow, value->be16);
1593
0
        flow_fix_vlan_tpid(flow);
1594
0
        break;
1595
1596
0
    case MFF_DL_VLAN_PCP:
1597
0
    case MFF_VLAN_PCP:
1598
0
        flow_set_vlan_pcp(flow, value->u8, 0);
1599
0
        flow_fix_vlan_tpid(flow);
1600
0
        break;
1601
1602
0
    case MFF_MPLS_LABEL:
1603
0
        flow_set_mpls_label(flow, 0, value->be32);
1604
0
        break;
1605
1606
0
    case MFF_MPLS_TC:
1607
0
        flow_set_mpls_tc(flow, 0, value->u8);
1608
0
        break;
1609
1610
0
    case MFF_MPLS_BOS:
1611
0
        flow_set_mpls_bos(flow, 0, value->u8);
1612
0
        break;
1613
1614
0
    case MFF_MPLS_TTL:
1615
0
        flow_set_mpls_ttl(flow, 0, value->u8);
1616
0
        break;
1617
1618
0
    case MFF_IPV4_SRC:
1619
0
        flow->nw_src = value->be32;
1620
0
        break;
1621
1622
0
    case MFF_IPV4_DST:
1623
0
        flow->nw_dst = value->be32;
1624
0
        break;
1625
1626
0
    case MFF_IPV6_SRC:
1627
0
        flow->ipv6_src = value->ipv6;
1628
0
        break;
1629
1630
0
    case MFF_IPV6_DST:
1631
0
        flow->ipv6_dst = value->ipv6;
1632
0
        break;
1633
1634
0
    case MFF_IPV6_LABEL:
1635
0
        flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1636
0
        break;
1637
1638
0
    case MFF_IP_PROTO:
1639
0
        flow->nw_proto = value->u8;
1640
0
        break;
1641
1642
0
    case MFF_IP_DSCP:
1643
0
        flow->nw_tos &= ~IP_DSCP_MASK;
1644
0
        flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1645
0
        break;
1646
1647
0
    case MFF_IP_DSCP_SHIFTED:
1648
0
        flow->nw_tos &= ~IP_DSCP_MASK;
1649
0
        flow->nw_tos |= value->u8 << 2;
1650
0
        break;
1651
1652
0
    case MFF_IP_ECN:
1653
0
        flow->nw_tos &= ~IP_ECN_MASK;
1654
0
        flow->nw_tos |= value->u8 & IP_ECN_MASK;
1655
0
        break;
1656
1657
0
    case MFF_IP_TTL:
1658
0
        flow->nw_ttl = value->u8;
1659
0
        break;
1660
1661
0
    case MFF_IP_FRAG:
1662
0
        flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1663
0
        break;
1664
1665
0
    case MFF_ARP_OP:
1666
0
        flow->nw_proto = ntohs(value->be16);
1667
0
        break;
1668
1669
0
    case MFF_ARP_SPA:
1670
0
        flow->nw_src = value->be32;
1671
0
        break;
1672
1673
0
    case MFF_ARP_TPA:
1674
0
        flow->nw_dst = value->be32;
1675
0
        break;
1676
1677
0
    case MFF_ARP_SHA:
1678
0
    case MFF_ND_SLL:
1679
0
        flow->arp_sha = value->mac;
1680
0
        break;
1681
1682
0
    case MFF_ARP_THA:
1683
0
    case MFF_ND_TLL:
1684
0
        flow->arp_tha = value->mac;
1685
0
        break;
1686
1687
0
    case MFF_TCP_SRC:
1688
0
    case MFF_UDP_SRC:
1689
0
    case MFF_SCTP_SRC:
1690
0
        flow->tp_src = value->be16;
1691
0
        break;
1692
1693
0
    case MFF_TCP_DST:
1694
0
    case MFF_UDP_DST:
1695
0
    case MFF_SCTP_DST:
1696
0
        flow->tp_dst = value->be16;
1697
0
        break;
1698
1699
0
    case MFF_TCP_FLAGS:
1700
0
        flow->tcp_flags = value->be16;
1701
0
        break;
1702
1703
0
    case MFF_ICMPV4_TYPE:
1704
0
    case MFF_ICMPV6_TYPE:
1705
0
        flow->tp_src = htons(value->u8);
1706
0
        break;
1707
1708
0
    case MFF_ICMPV4_CODE:
1709
0
    case MFF_ICMPV6_CODE:
1710
0
        flow->tp_dst = htons(value->u8);
1711
0
        break;
1712
1713
0
    case MFF_ND_TARGET:
1714
0
        flow->nd_target = value->ipv6;
1715
0
        break;
1716
1717
0
    case MFF_ND_RESERVED:
1718
0
        flow->igmp_group_ip4 = value->be32;
1719
0
        break;
1720
1721
0
    case MFF_ND_OPTIONS_TYPE:
1722
0
        flow->tcp_flags = htons(value->u8);
1723
0
        break;
1724
1725
0
    case MFF_NSH_FLAGS:
1726
0
        flow->nsh.flags = value->u8;
1727
0
        break;
1728
0
    case MFF_NSH_TTL:
1729
0
        flow->nsh.ttl = value->u8;
1730
0
        break;
1731
0
    case MFF_NSH_MDTYPE:
1732
0
        flow->nsh.mdtype = value->u8;
1733
0
        break;
1734
0
    case MFF_NSH_NP:
1735
0
        flow->nsh.np = value->u8;
1736
0
        break;
1737
0
    case MFF_NSH_SPI:
1738
0
        nsh_path_hdr_set_spi(&flow->nsh.path_hdr, value->be32);
1739
0
        break;
1740
0
    case MFF_NSH_SI:
1741
0
        nsh_path_hdr_set_si(&flow->nsh.path_hdr, value->u8);
1742
0
        break;
1743
0
    case MFF_NSH_C1:
1744
0
    case MFF_NSH_C2:
1745
0
    case MFF_NSH_C3:
1746
0
    case MFF_NSH_C4:
1747
0
        flow->nsh.context[mf->id - MFF_NSH_C1] = value->be32;
1748
0
        break;
1749
1750
0
    case MFF_N_IDS:
1751
0
    default:
1752
0
        OVS_NOT_REACHED();
1753
0
    }
1754
0
}
1755
1756
/* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1757
 * bits.  Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1758
 * src[i].  */
1759
static void
1760
apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1761
0
{
1762
0
    size_t i;
1763
1764
0
    for (i = 0; i < n; i++) {
1765
0
        dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1766
0
    }
1767
0
}
1768
1769
/* Sets 'flow' member field described by 'field' to 'value', except that bits
1770
 * for which 'mask' has a 0-bit keep their existing values.  The caller is
1771
 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1772
void
1773
mf_set_flow_value_masked(const struct mf_field *field,
1774
                         const union mf_value *value,
1775
                         const union mf_value *mask,
1776
                         struct flow *flow)
1777
0
{
1778
0
    union mf_value tmp;
1779
1780
0
    mf_get_value(field, flow, &tmp);
1781
0
    apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1782
0
               (uint8_t *) &tmp, field->n_bytes);
1783
0
    mf_set_flow_value(field, &tmp, flow);
1784
0
}
1785
1786
bool
1787
mf_is_tunnel_field(const struct mf_field *mf)
1788
0
{
1789
0
    switch (mf->id) {
1790
0
    case MFF_TUN_ID:
1791
0
    case MFF_TUN_SRC:
1792
0
    case MFF_TUN_DST:
1793
0
    case MFF_TUN_IPV6_SRC:
1794
0
    case MFF_TUN_IPV6_DST:
1795
0
    case MFF_TUN_FLAGS:
1796
0
    case MFF_TUN_TTL:
1797
0
    case MFF_TUN_TOS:
1798
0
    case MFF_TUN_GBP_ID:
1799
0
    case MFF_TUN_GBP_FLAGS:
1800
0
    case MFF_TUN_ERSPAN_VER:
1801
0
    case MFF_TUN_ERSPAN_IDX:
1802
0
    case MFF_TUN_ERSPAN_DIR:
1803
0
    case MFF_TUN_ERSPAN_HWID:
1804
0
    case MFF_TUN_GTPU_FLAGS:
1805
0
    case MFF_TUN_GTPU_MSGTYPE:
1806
0
    CASE_MFF_TUN_METADATA:
1807
0
        return true;
1808
1809
0
    case MFF_DP_HASH:
1810
0
    case MFF_RECIRC_ID:
1811
0
    case MFF_PACKET_TYPE:
1812
0
    case MFF_CONJ_ID:
1813
0
    case MFF_METADATA:
1814
0
    case MFF_IN_PORT:
1815
0
    case MFF_IN_PORT_OXM:
1816
0
    case MFF_ACTSET_OUTPUT:
1817
0
    case MFF_SKB_PRIORITY:
1818
0
    case MFF_PKT_MARK:
1819
0
    case MFF_CT_STATE:
1820
0
    case MFF_CT_ZONE:
1821
0
    case MFF_CT_MARK:
1822
0
    case MFF_CT_LABEL:
1823
0
    case MFF_CT_NW_PROTO:
1824
0
    case MFF_CT_NW_SRC:
1825
0
    case MFF_CT_NW_DST:
1826
0
    case MFF_CT_IPV6_SRC:
1827
0
    case MFF_CT_IPV6_DST:
1828
0
    case MFF_CT_TP_SRC:
1829
0
    case MFF_CT_TP_DST:
1830
0
    CASE_MFF_REGS:
1831
0
    CASE_MFF_XREGS:
1832
0
    CASE_MFF_XXREGS:
1833
0
    case MFF_ETH_SRC:
1834
0
    case MFF_ETH_DST:
1835
0
    case MFF_ETH_TYPE:
1836
0
    case MFF_VLAN_TCI:
1837
0
    case MFF_DL_VLAN:
1838
0
    case MFF_VLAN_VID:
1839
0
    case MFF_DL_VLAN_PCP:
1840
0
    case MFF_VLAN_PCP:
1841
0
    case MFF_MPLS_LABEL:
1842
0
    case MFF_MPLS_TC:
1843
0
    case MFF_MPLS_BOS:
1844
0
    case MFF_MPLS_TTL:
1845
0
    case MFF_IPV4_SRC:
1846
0
    case MFF_IPV4_DST:
1847
0
    case MFF_IPV6_SRC:
1848
0
    case MFF_IPV6_DST:
1849
0
    case MFF_IPV6_LABEL:
1850
0
    case MFF_IP_PROTO:
1851
0
    case MFF_IP_DSCP:
1852
0
    case MFF_IP_DSCP_SHIFTED:
1853
0
    case MFF_IP_ECN:
1854
0
    case MFF_IP_TTL:
1855
0
    case MFF_IP_FRAG:
1856
0
    case MFF_ARP_OP:
1857
0
    case MFF_ARP_SPA:
1858
0
    case MFF_ARP_TPA:
1859
0
    case MFF_ARP_SHA:
1860
0
    case MFF_ARP_THA:
1861
0
    case MFF_TCP_SRC:
1862
0
    case MFF_TCP_DST:
1863
0
    case MFF_TCP_FLAGS:
1864
0
    case MFF_UDP_SRC:
1865
0
    case MFF_UDP_DST:
1866
0
    case MFF_SCTP_SRC:
1867
0
    case MFF_SCTP_DST:
1868
0
    case MFF_ICMPV4_TYPE:
1869
0
    case MFF_ICMPV4_CODE:
1870
0
    case MFF_ICMPV6_TYPE:
1871
0
    case MFF_ICMPV6_CODE:
1872
0
    case MFF_ND_TARGET:
1873
0
    case MFF_ND_SLL:
1874
0
    case MFF_ND_TLL:
1875
0
    case MFF_ND_RESERVED:
1876
0
    case MFF_ND_OPTIONS_TYPE:
1877
0
    case MFF_NSH_FLAGS:
1878
0
    case MFF_NSH_MDTYPE:
1879
0
    case MFF_NSH_NP:
1880
0
    case MFF_NSH_SPI:
1881
0
    case MFF_NSH_SI:
1882
0
    case MFF_NSH_C1:
1883
0
    case MFF_NSH_C2:
1884
0
    case MFF_NSH_C3:
1885
0
    case MFF_NSH_C4:
1886
0
    case MFF_NSH_TTL:
1887
0
        return false;
1888
1889
0
    case MFF_N_IDS:
1890
0
    default:
1891
0
        OVS_NOT_REACHED();
1892
0
    }
1893
0
}
1894
1895
bool
1896
mf_is_tun_metadata(const struct mf_field *mf)
1897
95.2k
{
1898
95.2k
    return mf->id >= MFF_TUN_METADATA0 &&
1899
89.6k
           mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1900
95.2k
}
1901
1902
bool
1903
mf_is_any_metadata(const struct mf_field *mf)
1904
0
{
1905
0
    switch (mf->id) {
1906
0
    case MFF_DP_HASH:
1907
0
    case MFF_RECIRC_ID:
1908
0
    case MFF_PACKET_TYPE:
1909
0
    case MFF_CONJ_ID:
1910
0
    case MFF_TUN_ERSPAN_DIR:
1911
0
    CASE_MFF_TUN_METADATA:
1912
0
    case MFF_METADATA:
1913
0
    case MFF_IN_PORT:
1914
0
    case MFF_IN_PORT_OXM:
1915
0
    case MFF_ACTSET_OUTPUT:
1916
0
    case MFF_SKB_PRIORITY:
1917
0
    case MFF_PKT_MARK:
1918
0
    case MFF_CT_STATE:
1919
0
    case MFF_CT_ZONE:
1920
0
    case MFF_CT_MARK:
1921
0
    case MFF_CT_LABEL:
1922
0
    case MFF_CT_NW_PROTO:
1923
0
    case MFF_CT_NW_SRC:
1924
0
    case MFF_CT_NW_DST:
1925
0
    case MFF_CT_IPV6_SRC:
1926
0
    case MFF_CT_IPV6_DST:
1927
0
    case MFF_CT_TP_SRC:
1928
0
    case MFF_CT_TP_DST:
1929
0
    CASE_MFF_REGS:
1930
0
    CASE_MFF_XREGS:
1931
0
    CASE_MFF_XXREGS:
1932
0
        return true;
1933
1934
0
    case MFF_TUN_ID:
1935
0
    case MFF_TUN_SRC:
1936
0
    case MFF_TUN_DST:
1937
0
    case MFF_TUN_IPV6_SRC:
1938
0
    case MFF_TUN_IPV6_DST:
1939
0
    case MFF_TUN_FLAGS:
1940
0
    case MFF_TUN_TTL:
1941
0
    case MFF_TUN_TOS:
1942
0
    case MFF_TUN_GBP_ID:
1943
0
    case MFF_TUN_GBP_FLAGS:
1944
0
    case MFF_TUN_ERSPAN_IDX:
1945
0
    case MFF_TUN_ERSPAN_VER:
1946
0
    case MFF_TUN_ERSPAN_HWID:
1947
0
    case MFF_TUN_GTPU_FLAGS:
1948
0
    case MFF_TUN_GTPU_MSGTYPE:
1949
0
    case MFF_ETH_SRC:
1950
0
    case MFF_ETH_DST:
1951
0
    case MFF_ETH_TYPE:
1952
0
    case MFF_VLAN_TCI:
1953
0
    case MFF_DL_VLAN:
1954
0
    case MFF_VLAN_VID:
1955
0
    case MFF_DL_VLAN_PCP:
1956
0
    case MFF_VLAN_PCP:
1957
0
    case MFF_MPLS_LABEL:
1958
0
    case MFF_MPLS_TC:
1959
0
    case MFF_MPLS_BOS:
1960
0
    case MFF_MPLS_TTL:
1961
0
    case MFF_IPV4_SRC:
1962
0
    case MFF_IPV4_DST:
1963
0
    case MFF_IPV6_SRC:
1964
0
    case MFF_IPV6_DST:
1965
0
    case MFF_IPV6_LABEL:
1966
0
    case MFF_IP_PROTO:
1967
0
    case MFF_IP_DSCP:
1968
0
    case MFF_IP_DSCP_SHIFTED:
1969
0
    case MFF_IP_ECN:
1970
0
    case MFF_IP_TTL:
1971
0
    case MFF_IP_FRAG:
1972
0
    case MFF_ARP_OP:
1973
0
    case MFF_ARP_SPA:
1974
0
    case MFF_ARP_TPA:
1975
0
    case MFF_ARP_SHA:
1976
0
    case MFF_ARP_THA:
1977
0
    case MFF_TCP_SRC:
1978
0
    case MFF_TCP_DST:
1979
0
    case MFF_TCP_FLAGS:
1980
0
    case MFF_UDP_SRC:
1981
0
    case MFF_UDP_DST:
1982
0
    case MFF_SCTP_SRC:
1983
0
    case MFF_SCTP_DST:
1984
0
    case MFF_ICMPV4_TYPE:
1985
0
    case MFF_ICMPV4_CODE:
1986
0
    case MFF_ICMPV6_TYPE:
1987
0
    case MFF_ICMPV6_CODE:
1988
0
    case MFF_ND_TARGET:
1989
0
    case MFF_ND_SLL:
1990
0
    case MFF_ND_TLL:
1991
0
    case MFF_ND_RESERVED:
1992
0
    case MFF_ND_OPTIONS_TYPE:
1993
0
    case MFF_NSH_FLAGS:
1994
0
    case MFF_NSH_MDTYPE:
1995
0
    case MFF_NSH_NP:
1996
0
    case MFF_NSH_SPI:
1997
0
    case MFF_NSH_SI:
1998
0
    case MFF_NSH_C1:
1999
0
    case MFF_NSH_C2:
2000
0
    case MFF_NSH_C3:
2001
0
    case MFF_NSH_C4:
2002
0
    case MFF_NSH_TTL:
2003
0
        return false;
2004
2005
0
    case MFF_N_IDS:
2006
0
    default:
2007
0
        OVS_NOT_REACHED();
2008
0
    }
2009
0
}
2010
2011
bool
2012
mf_is_frozen_metadata(const struct mf_field *mf)
2013
0
{
2014
0
    if (mf->id >= MFF_TUN_ID && mf->id <= MFF_IN_PORT_OXM) {
2015
0
        return true;
2016
0
    }
2017
2018
0
    if (mf->id >= MFF_REG0 && mf->id < MFF_ETH_SRC) {
2019
0
        return true;
2020
0
    }
2021
0
    return false;
2022
0
}
2023
2024
bool
2025
mf_is_pipeline_field(const struct mf_field *mf)
2026
88.7k
{
2027
88.7k
    switch (mf->id) {
2028
76
    case MFF_TUN_ID:
2029
230
    case MFF_TUN_SRC:
2030
275
    case MFF_TUN_DST:
2031
859
    case MFF_TUN_IPV6_SRC:
2032
967
    case MFF_TUN_IPV6_DST:
2033
1.12k
    case MFF_TUN_FLAGS:
2034
1.65k
    case MFF_TUN_GBP_ID:
2035
1.82k
    case MFF_TUN_GBP_FLAGS:
2036
2.15k
    case MFF_TUN_ERSPAN_VER:
2037
2.16k
    case MFF_TUN_ERSPAN_IDX:
2038
2.24k
    case MFF_TUN_ERSPAN_DIR:
2039
2.37k
    case MFF_TUN_ERSPAN_HWID:
2040
2.68k
    case MFF_TUN_GTPU_FLAGS:
2041
2.73k
    case MFF_TUN_GTPU_MSGTYPE:
2042
2.46M
    CASE_MFF_TUN_METADATA:
2043
2.46M
    case MFF_METADATA:
2044
62.6k
    case MFF_IN_PORT:
2045
62.8k
    case MFF_IN_PORT_OXM:
2046
2.19M
    CASE_MFF_REGS:
2047
2.19M
    CASE_MFF_XREGS:
2048
1.18M
    CASE_MFF_XXREGS:
2049
624k
    case MFF_PACKET_TYPE:
2050
79.2k
        return true;
2051
2052
10
    case MFF_DP_HASH:
2053
342
    case MFF_RECIRC_ID:
2054
537
    case MFF_CONJ_ID:
2055
537
    case MFF_TUN_TTL:
2056
537
    case MFF_TUN_TOS:
2057
603
    case MFF_ACTSET_OUTPUT:
2058
603
    case MFF_SKB_PRIORITY:
2059
637
    case MFF_PKT_MARK:
2060
733
    case MFF_CT_STATE:
2061
768
    case MFF_CT_ZONE:
2062
786
    case MFF_CT_MARK:
2063
894
    case MFF_CT_LABEL:
2064
1.09k
    case MFF_CT_NW_PROTO:
2065
1.15k
    case MFF_CT_NW_SRC:
2066
1.19k
    case MFF_CT_NW_DST:
2067
1.20k
    case MFF_CT_IPV6_SRC:
2068
1.22k
    case MFF_CT_IPV6_DST:
2069
1.24k
    case MFF_CT_TP_SRC:
2070
1.27k
    case MFF_CT_TP_DST:
2071
1.31k
    case MFF_ETH_SRC:
2072
1.38k
    case MFF_ETH_DST:
2073
1.46k
    case MFF_ETH_TYPE:
2074
1.67k
    case MFF_VLAN_TCI:
2075
1.67k
    case MFF_DL_VLAN:
2076
1.69k
    case MFF_VLAN_VID:
2077
1.69k
    case MFF_DL_VLAN_PCP:
2078
1.71k
    case MFF_VLAN_PCP:
2079
1.77k
    case MFF_MPLS_LABEL:
2080
1.81k
    case MFF_MPLS_TC:
2081
1.96k
    case MFF_MPLS_BOS:
2082
3.05k
    case MFF_MPLS_TTL:
2083
3.14k
    case MFF_IPV4_SRC:
2084
3.15k
    case MFF_IPV4_DST:
2085
3.34k
    case MFF_IPV6_SRC:
2086
3.48k
    case MFF_IPV6_DST:
2087
3.50k
    case MFF_IPV6_LABEL:
2088
3.70k
    case MFF_IP_PROTO:
2089
3.73k
    case MFF_IP_DSCP:
2090
3.76k
    case MFF_IP_DSCP_SHIFTED:
2091
4.14k
    case MFF_IP_ECN:
2092
4.20k
    case MFF_IP_TTL:
2093
4.29k
    case MFF_IP_FRAG:
2094
4.31k
    case MFF_ARP_OP:
2095
4.51k
    case MFF_ARP_SPA:
2096
5.09k
    case MFF_ARP_TPA:
2097
5.10k
    case MFF_ARP_SHA:
2098
5.14k
    case MFF_ARP_THA:
2099
5.21k
    case MFF_TCP_SRC:
2100
5.27k
    case MFF_TCP_DST:
2101
5.28k
    case MFF_TCP_FLAGS:
2102
5.48k
    case MFF_UDP_SRC:
2103
5.81k
    case MFF_UDP_DST:
2104
5.85k
    case MFF_SCTP_SRC:
2105
5.88k
    case MFF_SCTP_DST:
2106
5.95k
    case MFF_ICMPV4_TYPE:
2107
5.99k
    case MFF_ICMPV4_CODE:
2108
6.02k
    case MFF_ICMPV6_TYPE:
2109
6.03k
    case MFF_ICMPV6_CODE:
2110
6.11k
    case MFF_ND_TARGET:
2111
6.17k
    case MFF_ND_SLL:
2112
6.24k
    case MFF_ND_TLL:
2113
6.62k
    case MFF_ND_RESERVED:
2114
6.71k
    case MFF_ND_OPTIONS_TYPE:
2115
6.74k
    case MFF_NSH_FLAGS:
2116
6.94k
    case MFF_NSH_TTL:
2117
6.96k
    case MFF_NSH_MDTYPE:
2118
7.02k
    case MFF_NSH_NP:
2119
7.29k
    case MFF_NSH_SPI:
2120
7.35k
    case MFF_NSH_SI:
2121
7.55k
    case MFF_NSH_C1:
2122
9.38k
    case MFF_NSH_C2:
2123
9.45k
    case MFF_NSH_C3:
2124
9.48k
    case MFF_NSH_C4:
2125
9.48k
        return false;
2126
2127
0
    case MFF_N_IDS:
2128
0
    default:
2129
0
        OVS_NOT_REACHED();
2130
88.7k
    }
2131
88.7k
}
2132
2133
/* Returns true if 'mf' has previously been set in 'flow', false if
2134
 * it contains a non-default value.
2135
 *
2136
 * The caller is responsible for ensuring that 'flow' meets 'mf''s
2137
 * prerequisites. */
2138
bool
2139
mf_is_set(const struct mf_field *mf, const struct flow *flow)
2140
0
{
2141
0
    if (!mf_is_tun_metadata(mf)) {
2142
0
        union mf_value value;
2143
2144
0
        mf_get_value(mf, flow, &value);
2145
0
        return !is_all_zeros(&value, mf->n_bytes);
2146
0
    } else {
2147
0
        return ULLONG_GET(flow->tunnel.metadata.present.map,
2148
0
                          mf->id - MFF_TUN_METADATA0);
2149
0
    }
2150
0
}
2151
2152
/* Makes 'match' wildcard field 'mf'.
2153
 *
2154
 * The caller is responsible for ensuring that 'match' meets 'mf''s
2155
 * prerequisites.
2156
 *
2157
 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2158
 * with the request or NULL if there is no error. The caller is reponsible
2159
 * for freeing the string. */
2160
void
2161
mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
2162
63.0k
{
2163
63.0k
    if (err_str) {
2164
63.0k
        *err_str = NULL;
2165
63.0k
    }
2166
2167
63.0k
    switch (mf->id) {
2168
786
    case MFF_DP_HASH:
2169
786
        match->flow.dp_hash = 0;
2170
786
        match->wc.masks.dp_hash = 0;
2171
786
        break;
2172
282
    case MFF_RECIRC_ID:
2173
282
        match->flow.recirc_id = 0;
2174
282
        match->wc.masks.recirc_id = 0;
2175
282
        break;
2176
187
    case MFF_PACKET_TYPE:
2177
187
        match->flow.packet_type = 0;
2178
187
        match->wc.masks.packet_type = 0;
2179
187
        break;
2180
76
    case MFF_CONJ_ID:
2181
76
        match->flow.conj_id = 0;
2182
76
        match->wc.masks.conj_id = 0;
2183
76
        break;
2184
334
    case MFF_TUN_ID:
2185
334
        match_set_tun_id_masked(match, htonll(0), htonll(0));
2186
334
        break;
2187
221
    case MFF_TUN_SRC:
2188
221
        match_set_tun_src_masked(match, htonl(0), htonl(0));
2189
221
        break;
2190
232
    case MFF_TUN_DST:
2191
232
        match_set_tun_dst_masked(match, htonl(0), htonl(0));
2192
232
        break;
2193
250
    case MFF_TUN_IPV6_SRC:
2194
250
        memset(&match->wc.masks.tunnel.ipv6_src, 0,
2195
250
               sizeof match->wc.masks.tunnel.ipv6_src);
2196
250
        memset(&match->flow.tunnel.ipv6_src, 0,
2197
250
               sizeof match->flow.tunnel.ipv6_src);
2198
250
        break;
2199
220
    case MFF_TUN_IPV6_DST:
2200
220
        memset(&match->wc.masks.tunnel.ipv6_dst, 0,
2201
220
               sizeof match->wc.masks.tunnel.ipv6_dst);
2202
220
        memset(&match->flow.tunnel.ipv6_dst, 0,
2203
220
               sizeof match->flow.tunnel.ipv6_dst);
2204
220
        break;
2205
257
    case MFF_TUN_FLAGS:
2206
257
        match_set_tun_flags_masked(match, 0, 0);
2207
257
        break;
2208
500
    case MFF_TUN_GBP_ID:
2209
500
        match_set_tun_gbp_id_masked(match, 0, 0);
2210
500
        break;
2211
312
    case MFF_TUN_GBP_FLAGS:
2212
312
        match_set_tun_gbp_flags_masked(match, 0, 0);
2213
312
        break;
2214
307
    case MFF_TUN_TOS:
2215
307
        match_set_tun_tos_masked(match, 0, 0);
2216
307
        break;
2217
413
    case MFF_TUN_TTL:
2218
413
        match_set_tun_ttl_masked(match, 0, 0);
2219
413
        break;
2220
232
    case MFF_TUN_ERSPAN_VER:
2221
232
        match_set_tun_erspan_ver_masked(match, 0, 0);
2222
232
        break;
2223
23
    case MFF_TUN_ERSPAN_IDX:
2224
23
        match_set_tun_erspan_idx_masked(match, 0, 0);
2225
23
        break;
2226
70
    case MFF_TUN_ERSPAN_DIR:
2227
70
        match_set_tun_erspan_dir_masked(match, 0, 0);
2228
70
        break;
2229
66
    case MFF_TUN_ERSPAN_HWID:
2230
66
        match_set_tun_erspan_hwid_masked(match, 0, 0);
2231
66
        break;
2232
361
    case MFF_TUN_GTPU_FLAGS:
2233
361
        match_set_tun_gtpu_flags_masked(match, 0, 0);
2234
361
        break;
2235
510
    case MFF_TUN_GTPU_MSGTYPE:
2236
510
        match_set_tun_gtpu_msgtype_masked(match, 0, 0);
2237
510
        break;
2238
0
    CASE_MFF_TUN_METADATA:
2239
0
        tun_metadata_set_match(mf, NULL, NULL, match, err_str);
2240
0
        break;
2241
2242
401
    case MFF_METADATA:
2243
401
        match_set_metadata_masked(match, htonll(0), htonll(0));
2244
401
        break;
2245
2246
278
    case MFF_IN_PORT:
2247
338
    case MFF_IN_PORT_OXM:
2248
338
        match->flow.in_port.ofp_port = 0;
2249
338
        match->wc.masks.in_port.ofp_port = 0;
2250
338
        break;
2251
106
    case MFF_ACTSET_OUTPUT:
2252
106
        match->flow.actset_output = 0;
2253
106
        match->wc.masks.actset_output = 0;
2254
106
        break;
2255
2256
0
    case MFF_SKB_PRIORITY:
2257
0
        match->flow.skb_priority = 0;
2258
0
        match->wc.masks.skb_priority = 0;
2259
0
        break;
2260
2261
209
    case MFF_PKT_MARK:
2262
209
        match->flow.pkt_mark = 0;
2263
209
        match->wc.masks.pkt_mark = 0;
2264
209
        break;
2265
2266
438
    case MFF_CT_STATE:
2267
438
        match->flow.ct_state = 0;
2268
438
        match->wc.masks.ct_state = 0;
2269
438
        break;
2270
2271
248
    case MFF_CT_ZONE:
2272
248
        match->flow.ct_zone = 0;
2273
248
        match->wc.masks.ct_zone = 0;
2274
248
        break;
2275
2276
80
    case MFF_CT_MARK:
2277
80
        match->flow.ct_mark = 0;
2278
80
        match->wc.masks.ct_mark = 0;
2279
80
        break;
2280
2281
78
    case MFF_CT_LABEL:
2282
78
        memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
2283
78
        memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
2284
78
        break;
2285
2286
229
    case MFF_CT_NW_PROTO:
2287
229
        match->flow.ct_nw_proto = 0;
2288
229
        match->wc.masks.ct_nw_proto = 0;
2289
229
        break;
2290
2291
276
    case MFF_CT_NW_SRC:
2292
276
        match->flow.ct_nw_src = 0;
2293
276
        match->wc.masks.ct_nw_src = 0;
2294
276
        break;
2295
2296
226
    case MFF_CT_NW_DST:
2297
226
        match->flow.ct_nw_dst = 0;
2298
226
        match->wc.masks.ct_nw_dst = 0;
2299
226
        break;
2300
2301
328
    case MFF_CT_IPV6_SRC:
2302
328
        memset(&match->flow.ct_ipv6_src, 0, sizeof(match->flow.ct_ipv6_src));
2303
328
        WC_UNMASK_FIELD(&match->wc, ct_ipv6_src);
2304
328
        break;
2305
2306
133
    case MFF_CT_IPV6_DST:
2307
133
        memset(&match->flow.ct_ipv6_dst, 0, sizeof(match->flow.ct_ipv6_dst));
2308
133
        WC_UNMASK_FIELD(&match->wc, ct_ipv6_dst);
2309
133
        break;
2310
2311
390
    case MFF_CT_TP_SRC:
2312
390
        match->flow.ct_tp_src = 0;
2313
390
        match->wc.masks.ct_tp_src = 0;
2314
390
        break;
2315
2316
213
    case MFF_CT_TP_DST:
2317
213
        match->flow.ct_tp_dst = 0;
2318
213
        match->wc.masks.ct_tp_dst = 0;
2319
213
        break;
2320
2321
16.8k
    CASE_MFF_REGS:
2322
16.8k
        match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
2323
16.8k
        break;
2324
2325
7.03k
    CASE_MFF_XREGS:
2326
7.03k
        match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
2327
7.03k
        break;
2328
2329
6.22k
    CASE_MFF_XXREGS: {
2330
6.22k
        match_set_xxreg_masked(match, mf->id - MFF_XXREG0, OVS_U128_ZERO,
2331
6.22k
                               OVS_U128_ZERO);
2332
6.22k
        break;
2333
24.9k
    }
2334
2335
317
    case MFF_ETH_SRC:
2336
317
        match->flow.dl_src = eth_addr_zero;
2337
317
        match->wc.masks.dl_src = eth_addr_zero;
2338
317
        break;
2339
2340
132
    case MFF_ETH_DST:
2341
132
        match->flow.dl_dst = eth_addr_zero;
2342
132
        match->wc.masks.dl_dst = eth_addr_zero;
2343
132
        break;
2344
2345
319
    case MFF_ETH_TYPE:
2346
319
        match->flow.dl_type = htons(0);
2347
319
        match->wc.masks.dl_type = htons(0);
2348
319
        break;
2349
2350
207
    case MFF_VLAN_TCI:
2351
207
        match_set_dl_tci_masked(match, htons(0), htons(0));
2352
207
        break;
2353
2354
234
    case MFF_DL_VLAN:
2355
659
    case MFF_VLAN_VID:
2356
659
        match_set_any_vid(match);
2357
659
        break;
2358
2359
480
    case MFF_DL_VLAN_PCP:
2360
1.07k
    case MFF_VLAN_PCP:
2361
1.07k
        match_set_any_pcp(match);
2362
1.07k
        break;
2363
2364
975
    case MFF_MPLS_LABEL:
2365
975
        match_set_any_mpls_label(match, 0);
2366
975
        break;
2367
2368
141
    case MFF_MPLS_TC:
2369
141
        match_set_any_mpls_tc(match, 0);
2370
141
        break;
2371
2372
385
    case MFF_MPLS_BOS:
2373
385
        match_set_any_mpls_bos(match, 0);
2374
385
        break;
2375
2376
408
    case MFF_MPLS_TTL:
2377
408
        match_set_any_mpls_ttl(match, 0);
2378
408
        break;
2379
2380
215
    case MFF_IPV4_SRC:
2381
458
    case MFF_ARP_SPA:
2382
458
        match_set_nw_src_masked(match, htonl(0), htonl(0));
2383
458
        break;
2384
2385
715
    case MFF_IPV4_DST:
2386
1.25k
    case MFF_ARP_TPA:
2387
1.25k
        match_set_nw_dst_masked(match, htonl(0), htonl(0));
2388
1.25k
        break;
2389
2390
314
    case MFF_IPV6_SRC:
2391
314
        memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
2392
314
        memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
2393
314
        break;
2394
2395
386
    case MFF_IPV6_DST:
2396
386
        memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
2397
386
        memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
2398
386
        break;
2399
2400
600
    case MFF_IPV6_LABEL:
2401
600
        match->wc.masks.ipv6_label = htonl(0);
2402
600
        match->flow.ipv6_label = htonl(0);
2403
600
        break;
2404
2405
492
    case MFF_IP_PROTO:
2406
492
        match->wc.masks.nw_proto = 0;
2407
492
        match->flow.nw_proto = 0;
2408
492
        break;
2409
2410
287
    case MFF_IP_DSCP:
2411
1.61k
    case MFF_IP_DSCP_SHIFTED:
2412
1.61k
        match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
2413
1.61k
        match->flow.nw_tos &= ~IP_DSCP_MASK;
2414
1.61k
        break;
2415
2416
223
    case MFF_IP_ECN:
2417
223
        match->wc.masks.nw_tos &= ~IP_ECN_MASK;
2418
223
        match->flow.nw_tos &= ~IP_ECN_MASK;
2419
223
        break;
2420
2421
298
    case MFF_IP_TTL:
2422
298
        match->wc.masks.nw_ttl = 0;
2423
298
        match->flow.nw_ttl = 0;
2424
298
        break;
2425
2426
111
    case MFF_IP_FRAG:
2427
111
        match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
2428
111
        match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
2429
111
        break;
2430
2431
283
    case MFF_ARP_OP:
2432
283
        match->wc.masks.nw_proto = 0;
2433
283
        match->flow.nw_proto = 0;
2434
283
        break;
2435
2436
318
    case MFF_ARP_SHA:
2437
645
    case MFF_ND_SLL:
2438
645
        match->flow.arp_sha = eth_addr_zero;
2439
645
        match->wc.masks.arp_sha = eth_addr_zero;
2440
645
        break;
2441
2442
388
    case MFF_ARP_THA:
2443
2.15k
    case MFF_ND_TLL:
2444
2.15k
        match->flow.arp_tha = eth_addr_zero;
2445
2.15k
        match->wc.masks.arp_tha = eth_addr_zero;
2446
2.15k
        break;
2447
2448
533
    case MFF_ND_RESERVED:
2449
533
        match->wc.masks.igmp_group_ip4 = htonl(0);
2450
533
        match->flow.igmp_group_ip4 = htonl(0);
2451
533
        break;
2452
2453
318
    case MFF_TCP_SRC:
2454
668
    case MFF_UDP_SRC:
2455
910
    case MFF_SCTP_SRC:
2456
1.13k
    case MFF_ICMPV4_TYPE:
2457
1.66k
    case MFF_ICMPV6_TYPE:
2458
1.66k
        match->wc.masks.tp_src = htons(0);
2459
1.66k
        match->flow.tp_src = htons(0);
2460
1.66k
        break;
2461
2462
389
    case MFF_TCP_DST:
2463
701
    case MFF_UDP_DST:
2464
1.14k
    case MFF_SCTP_DST:
2465
1.27k
    case MFF_ICMPV4_CODE:
2466
1.50k
    case MFF_ICMPV6_CODE:
2467
1.50k
        match->wc.masks.tp_dst = htons(0);
2468
1.50k
        match->flow.tp_dst = htons(0);
2469
1.50k
        break;
2470
2471
794
    case MFF_TCP_FLAGS:
2472
994
    case MFF_ND_OPTIONS_TYPE:
2473
994
        match->wc.masks.tcp_flags = htons(0);
2474
994
        match->flow.tcp_flags = htons(0);
2475
994
        break;
2476
2477
462
    case MFF_ND_TARGET:
2478
462
        memset(&match->wc.masks.nd_target, 0,
2479
462
               sizeof match->wc.masks.nd_target);
2480
462
        memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
2481
462
        break;
2482
2483
675
    case MFF_NSH_FLAGS:
2484
675
        MATCH_SET_FIELD_MASKED(match, nsh.flags, 0, 0);
2485
675
        break;
2486
464
    case MFF_NSH_TTL:
2487
464
        MATCH_SET_FIELD_MASKED(match, nsh.ttl, 0, 0);
2488
464
        break;
2489
270
    case MFF_NSH_MDTYPE:
2490
270
        MATCH_SET_FIELD_MASKED(match, nsh.mdtype, 0, 0);
2491
270
        break;
2492
554
    case MFF_NSH_NP:
2493
554
        MATCH_SET_FIELD_MASKED(match, nsh.np, 0, 0);
2494
554
        break;
2495
517
    case MFF_NSH_SPI:
2496
517
        match->wc.masks.nsh.path_hdr &= ~htonl(NSH_SPI_MASK);
2497
517
        nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr, htonl(0));
2498
517
        break;
2499
728
    case MFF_NSH_SI:
2500
728
        match->wc.masks.nsh.path_hdr &= ~htonl(NSH_SI_MASK);
2501
728
        nsh_path_hdr_set_si(&match->flow.nsh.path_hdr, 0);
2502
728
        break;
2503
474
    case MFF_NSH_C1:
2504
912
    case MFF_NSH_C2:
2505
1.34k
    case MFF_NSH_C3:
2506
1.75k
    case MFF_NSH_C4:
2507
1.75k
        MATCH_SET_FIELD_MASKED(match, nsh.context[mf->id - MFF_NSH_C1],
2508
1.75k
                               htonl(0), htonl(0));
2509
1.75k
        break;
2510
2511
0
    case MFF_N_IDS:
2512
0
    default:
2513
0
        OVS_NOT_REACHED();
2514
63.0k
    }
2515
63.0k
}
2516
2517
/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2518
 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2519
 * with a 1-bit indicating that the corresponding value bit must match and a
2520
 * 0-bit indicating a don't-care.
2521
 *
2522
 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2523
 * mf_set_value(mf, value, match).  If 'mask' points to all-0-bits, then this
2524
 * call is equivalent to mf_set_wild(mf, match).
2525
 *
2526
 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
2527
 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
2528
 *
2529
 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2530
 * with the request or NULL if there is no error. The caller is reponsible
2531
 * for freeing the string.
2532
 *
2533
 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
2534
 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
2535
 * protocol versions can support this functionality. */
2536
uint32_t
2537
mf_set(const struct mf_field *mf,
2538
       const union mf_value *value, const union mf_value *mask,
2539
       struct match *match, char **err_str)
2540
355k
{
2541
355k
    if (!mask || is_all_ones(mask, mf->n_bytes)) {
2542
157k
        mf_set_value(mf, value, match, err_str);
2543
157k
        return mf->usable_protocols_exact;
2544
198k
    } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
2545
        /* Tunnel metadata matches on the existence of the field itself, so
2546
         * it still needs to be encoded even if the value is wildcarded. */
2547
63.0k
        mf_set_wild(mf, match, err_str);
2548
63.0k
        return OFPUTIL_P_ANY;
2549
63.0k
    }
2550
2551
135k
    if (err_str) {
2552
95.7k
        *err_str = NULL;
2553
95.7k
    }
2554
2555
    /* The cases where 'mask' is all-1-bits or all-0-bits were already handled
2556
     * above[*], so the code below only needs to work for the remaining cases
2557
     * of a nontrivial mask.
2558
     *
2559
     * [*] Except where the field is a tunnel metadata field and 'mask' is
2560
     *     all-0-bits; see above. */
2561
135k
    switch (mf->id) {
2562
292
    case MFF_CT_ZONE:
2563
1.96k
    case MFF_CT_NW_PROTO:
2564
2.45k
    case MFF_RECIRC_ID:
2565
3.30k
    case MFF_PACKET_TYPE:
2566
3.70k
    case MFF_CONJ_ID:
2567
4.02k
    case MFF_IN_PORT:
2568
4.42k
    case MFF_IN_PORT_OXM:
2569
4.94k
    case MFF_ACTSET_OUTPUT:
2570
4.94k
    case MFF_SKB_PRIORITY:
2571
5.61k
    case MFF_ETH_TYPE:
2572
5.87k
    case MFF_DL_VLAN:
2573
6.19k
    case MFF_DL_VLAN_PCP:
2574
6.58k
    case MFF_VLAN_PCP:
2575
6.87k
    case MFF_MPLS_LABEL:
2576
7.13k
    case MFF_MPLS_TC:
2577
7.39k
    case MFF_MPLS_BOS:
2578
7.50k
    case MFF_MPLS_TTL:
2579
7.76k
    case MFF_IP_PROTO:
2580
7.96k
    case MFF_IP_TTL:
2581
8.23k
    case MFF_IP_DSCP:
2582
8.49k
    case MFF_IP_DSCP_SHIFTED:
2583
8.75k
    case MFF_IP_ECN:
2584
9.00k
    case MFF_ARP_OP:
2585
9.29k
    case MFF_ICMPV4_TYPE:
2586
9.48k
    case MFF_ICMPV4_CODE:
2587
9.73k
    case MFF_ICMPV6_TYPE:
2588
9.97k
    case MFF_ICMPV6_CODE:
2589
9.97k
    case MFF_ND_RESERVED:
2590
9.97k
    case MFF_ND_OPTIONS_TYPE:
2591
9.97k
        return OFPUTIL_P_NONE;
2592
2593
373
    case MFF_DP_HASH:
2594
373
        match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
2595
373
        break;
2596
1.15k
    case MFF_TUN_ID:
2597
1.15k
        match_set_tun_id_masked(match, value->be64, mask->be64);
2598
1.15k
        break;
2599
451
    case MFF_TUN_SRC:
2600
451
        match_set_tun_src_masked(match, value->be32, mask->be32);
2601
451
        break;
2602
443
    case MFF_TUN_DST:
2603
443
        match_set_tun_dst_masked(match, value->be32, mask->be32);
2604
443
        break;
2605
739
    case MFF_TUN_IPV6_SRC:
2606
739
        match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2607
739
        break;
2608
672
    case MFF_TUN_IPV6_DST:
2609
672
        match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2610
672
        break;
2611
683
    case MFF_TUN_FLAGS:
2612
683
        match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2613
683
        break;
2614
512
    case MFF_TUN_GBP_ID:
2615
512
        match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
2616
512
        break;
2617
1.08k
    case MFF_TUN_GBP_FLAGS:
2618
1.08k
        match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
2619
1.08k
        break;
2620
372
    case MFF_TUN_TTL:
2621
372
        match_set_tun_ttl_masked(match, value->u8, mask->u8);
2622
372
        break;
2623
1.85k
    case MFF_TUN_TOS:
2624
1.85k
        match_set_tun_tos_masked(match, value->u8, mask->u8);
2625
1.85k
        break;
2626
361
    case MFF_TUN_ERSPAN_VER:
2627
361
        match_set_tun_erspan_ver_masked(match, value->u8, mask->u8);
2628
361
        break;
2629
356
    case MFF_TUN_ERSPAN_IDX:
2630
356
        match_set_tun_erspan_idx_masked(match, ntohl(value->be32),
2631
356
                                        ntohl(mask->be32));
2632
356
        break;
2633
154
    case MFF_TUN_ERSPAN_DIR:
2634
154
        match_set_tun_erspan_dir_masked(match, value->u8, mask->u8);
2635
154
        break;
2636
454
    case MFF_TUN_ERSPAN_HWID:
2637
454
        match_set_tun_erspan_hwid_masked(match, value->u8, mask->u8);
2638
454
        break;
2639
406
    case MFF_TUN_GTPU_FLAGS:
2640
406
        match_set_tun_gtpu_flags_masked(match, value->u8, mask->u8);
2641
406
        break;
2642
37
    case MFF_TUN_GTPU_MSGTYPE:
2643
37
        match_set_tun_gtpu_msgtype_masked(match, value->u8, mask->u8);
2644
37
        break;
2645
62.8k
    CASE_MFF_TUN_METADATA:
2646
62.8k
        tun_metadata_set_match(mf, value, mask, match, err_str);
2647
62.8k
        break;
2648
2649
406
    case MFF_METADATA:
2650
406
        match_set_metadata_masked(match, value->be64, mask->be64);
2651
406
        break;
2652
2653
16.3k
    CASE_MFF_REGS:
2654
16.3k
        match_set_reg_masked(match, mf->id - MFF_REG0,
2655
16.3k
                             ntohl(value->be32), ntohl(mask->be32));
2656
16.3k
        break;
2657
2658
6.92k
    CASE_MFF_XREGS:
2659
6.92k
        match_set_xreg_masked(match, mf->id - MFF_XREG0,
2660
6.92k
                              ntohll(value->be64), ntohll(mask->be64));
2661
6.92k
        break;
2662
2663
3.29k
    CASE_MFF_XXREGS: {
2664
3.29k
        match_set_xxreg_masked(match, mf->id - MFF_XXREG0,
2665
3.29k
                ntoh128(value->be128), ntoh128(mask->be128));
2666
3.29k
        break;
2667
12.5k
    }
2668
2669
574
    case MFF_PKT_MARK:
2670
574
        match_set_pkt_mark_masked(match, ntohl(value->be32),
2671
574
                                  ntohl(mask->be32));
2672
574
        break;
2673
2674
796
    case MFF_CT_STATE:
2675
796
        match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
2676
796
        break;
2677
2678
1.85k
    case MFF_CT_MARK:
2679
1.85k
        match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
2680
1.85k
        break;
2681
2682
889
    case MFF_CT_LABEL:
2683
889
        match_set_ct_label_masked(match, ntoh128(value->be128),
2684
889
                                  ntoh128(mask->be128));
2685
889
        break;
2686
2687
274
    case MFF_CT_NW_SRC:
2688
274
        match_set_ct_nw_src_masked(match, value->be32, mask->be32);
2689
274
        break;
2690
2691
713
    case MFF_CT_NW_DST:
2692
713
        match_set_ct_nw_dst_masked(match, value->be32, mask->be32);
2693
713
        break;
2694
2695
503
    case MFF_CT_IPV6_SRC:
2696
503
        match_set_ct_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2697
503
        break;
2698
2699
1.36k
    case MFF_CT_IPV6_DST:
2700
1.36k
        match_set_ct_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2701
1.36k
        break;
2702
2703
263
    case MFF_CT_TP_SRC:
2704
263
        match_set_ct_tp_src_masked(match, value->be16, mask->be16);
2705
263
        break;
2706
2707
656
    case MFF_CT_TP_DST:
2708
656
        match_set_ct_tp_dst_masked(match, value->be16, mask->be16);
2709
656
        break;
2710
2711
290
    case MFF_ETH_DST:
2712
290
        match_set_dl_dst_masked(match, value->mac, mask->mac);
2713
290
        break;
2714
2715
529
    case MFF_ETH_SRC:
2716
529
        match_set_dl_src_masked(match, value->mac, mask->mac);
2717
529
        break;
2718
2719
529
    case MFF_ARP_SHA:
2720
857
    case MFF_ND_SLL:
2721
857
        match_set_arp_sha_masked(match, value->mac, mask->mac);
2722
857
        break;
2723
2724
351
    case MFF_ARP_THA:
2725
757
    case MFF_ND_TLL:
2726
757
        match_set_arp_tha_masked(match, value->mac, mask->mac);
2727
757
        break;
2728
2729
559
    case MFF_VLAN_TCI:
2730
559
        match_set_dl_tci_masked(match, value->be16, mask->be16);
2731
559
        break;
2732
2733
350
    case MFF_VLAN_VID:
2734
350
        match_set_vlan_vid_masked(match, value->be16, mask->be16);
2735
350
        break;
2736
2737
550
    case MFF_IPV4_SRC:
2738
550
        match_set_nw_src_masked(match, value->be32, mask->be32);
2739
550
        break;
2740
2741
654
    case MFF_IPV4_DST:
2742
654
        match_set_nw_dst_masked(match, value->be32, mask->be32);
2743
654
        break;
2744
2745
915
    case MFF_IPV6_SRC:
2746
915
        match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2747
915
        break;
2748
2749
1.53k
    case MFF_IPV6_DST:
2750
1.53k
        match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2751
1.53k
        break;
2752
2753
1.08k
    case MFF_IPV6_LABEL:
2754
1.08k
        if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2755
765
            mf_set_value(mf, value, match, err_str);
2756
765
        } else {
2757
323
            match_set_ipv6_label_masked(match, value->be32, mask->be32);
2758
323
        }
2759
1.08k
        break;
2760
2761
612
    case MFF_ND_TARGET:
2762
612
        match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2763
612
        break;
2764
2765
1.07k
    case MFF_IP_FRAG:
2766
1.07k
        match_set_nw_frag_masked(match, value->u8,
2767
1.07k
                                 mask->u8 & FLOW_NW_FRAG_MASK);
2768
1.07k
        break;
2769
2770
246
    case MFF_ARP_SPA:
2771
246
        match_set_nw_src_masked(match, value->be32, mask->be32);
2772
246
        break;
2773
2774
346
    case MFF_ARP_TPA:
2775
346
        match_set_nw_dst_masked(match, value->be32, mask->be32);
2776
346
        break;
2777
2778
636
    case MFF_TCP_SRC:
2779
1.04k
    case MFF_UDP_SRC:
2780
1.44k
    case MFF_SCTP_SRC:
2781
1.44k
        match_set_tp_src_masked(match, value->be16, mask->be16);
2782
1.44k
        break;
2783
2784
399
    case MFF_TCP_DST:
2785
663
    case MFF_UDP_DST:
2786
1.17k
    case MFF_SCTP_DST:
2787
1.17k
        match_set_tp_dst_masked(match, value->be16, mask->be16);
2788
1.17k
        break;
2789
2790
575
    case MFF_TCP_FLAGS:
2791
575
        match_set_tcp_flags_masked(match, value->be16, mask->be16);
2792
575
        break;
2793
2794
230
    case MFF_NSH_FLAGS:
2795
230
        MATCH_SET_FIELD_MASKED(match, nsh.flags, value->u8, mask->u8);
2796
230
        break;
2797
200
    case MFF_NSH_TTL:
2798
200
        MATCH_SET_FIELD_MASKED(match, nsh.ttl, value->u8, mask->u8);
2799
200
        break;
2800
568
    case MFF_NSH_MDTYPE:
2801
568
        MATCH_SET_FIELD_MASKED(match, nsh.mdtype, value->u8, mask->u8);
2802
568
        break;
2803
258
    case MFF_NSH_NP:
2804
258
        MATCH_SET_FIELD_MASKED(match, nsh.np, value->u8, mask->u8);
2805
258
        break;
2806
828
    case MFF_NSH_SPI:
2807
828
        match->wc.masks.nsh.path_hdr |= mask->be32;
2808
828
        nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr,
2809
828
                             value->be32 & mask->be32);
2810
828
        break;
2811
364
    case MFF_NSH_SI:
2812
364
        match->wc.masks.nsh.path_hdr |= htonl(mask->u8);
2813
364
        nsh_path_hdr_set_si(&match->flow.nsh.path_hdr,
2814
364
                             value->u8 & mask->u8);
2815
364
        break;
2816
648
    case MFF_NSH_C1:
2817
1.12k
    case MFF_NSH_C2:
2818
1.45k
    case MFF_NSH_C3:
2819
1.74k
    case MFF_NSH_C4:
2820
1.74k
        MATCH_SET_FIELD_MASKED(match, nsh.context[mf->id - MFF_NSH_C1],
2821
1.74k
                               value->be32, mask->be32);
2822
1.74k
        break;
2823
2824
0
    case MFF_N_IDS:
2825
0
    default:
2826
0
        OVS_NOT_REACHED();
2827
135k
    }
2828
2829
125k
    return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
2830
1.79k
             || ip_is_cidr(mask->be32))
2831
125k
            ? mf->usable_protocols_cidr
2832
125k
            : mf->usable_protocols_bitwise);
2833
135k
}
2834
2835
static enum ofperr
2836
mf_check__(const struct mf_subfield *sf, const struct match *match,
2837
           const char *type)
2838
110k
{
2839
110k
    if (!sf->field) {
2840
0
        VLOG_WARN_RL(&rl, "unknown %s field", type);
2841
0
        return OFPERR_OFPBAC_BAD_SET_TYPE;
2842
110k
    } else if (!sf->n_bits) {
2843
5.42k
        VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2844
5.42k
        return OFPERR_OFPBAC_BAD_SET_LEN;
2845
105k
    } else if (sf->ofs >= sf->field->n_bits) {
2846
2.79k
        VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2847
2.79k
                     sf->ofs, sf->field->n_bits, type, sf->field->name);
2848
2.79k
        return OFPERR_OFPBAC_BAD_SET_LEN;
2849
102k
    } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2850
635
        VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2851
635
                     "of %s field %s", sf->ofs, sf->n_bits,
2852
635
                     sf->field->n_bits, type, sf->field->name);
2853
635
        return OFPERR_OFPBAC_BAD_SET_LEN;
2854
101k
    } else if (match && !mf_are_match_prereqs_ok(sf->field, match)) {
2855
1.30k
        VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2856
1.30k
                     type, sf->field->name);
2857
1.30k
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2858
100k
    } else {
2859
100k
        return 0;
2860
100k
    }
2861
110k
}
2862
2863
/* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2864
static void
2865
unwildcard_subfield(const struct mf_subfield *sf, struct flow_wildcards *wc)
2866
0
{
2867
0
    if (wc) {
2868
0
        union mf_value mask;
2869
2870
0
        memset(&mask, 0, sizeof mask);
2871
0
        bitwise_one(&mask, sf->field->n_bytes, sf->ofs, sf->n_bits);
2872
0
        mf_mask_field_masked(sf->field, &mask, wc);
2873
0
    }
2874
0
}
2875
2876
/* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2877
 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2878
 *
2879
 * Note: doesn't set 'dst' to 1s in case 'dst' is a tunnel field, because it
2880
 * is not necessary, as ODP library doesn't rely on matching these fields
2881
 * when they are written but not read.
2882
 *
2883
 * 'src' and 'dst' may overlap. */
2884
void
2885
mf_subfield_copy(const struct mf_subfield *src,
2886
                 const struct mf_subfield *dst,
2887
                 struct flow *flow, struct flow_wildcards *wc)
2888
0
{
2889
0
    ovs_assert(src->n_bits == dst->n_bits);
2890
0
    if (mf_are_prereqs_ok(dst->field, flow, wc)
2891
0
        && mf_are_prereqs_ok(src->field, flow, wc)) {
2892
0
        unwildcard_subfield(src, wc);
2893
0
        if (!mf_is_tunnel_field(dst->field)) {
2894
0
            unwildcard_subfield(dst, wc);
2895
0
        }
2896
2897
0
        union mf_value src_value;
2898
0
        union mf_value dst_value;
2899
0
        mf_get_value(dst->field, flow, &dst_value);
2900
0
        mf_get_value(src->field, flow, &src_value);
2901
0
        bitwise_copy(&src_value, src->field->n_bytes, src->ofs,
2902
0
                     &dst_value, dst->field->n_bytes, dst->ofs,
2903
0
                     src->n_bits);
2904
0
        mf_set_flow_value(dst->field, &dst_value, flow);
2905
0
    }
2906
0
}
2907
2908
/* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2909
 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2910
 *
2911
 * 'src' and 'dst' may overlap. */
2912
void
2913
mf_subfield_swap(const struct mf_subfield *a,
2914
                 const struct mf_subfield *b,
2915
                 struct flow *flow, struct flow_wildcards *wc)
2916
0
{
2917
0
    ovs_assert(a->n_bits == b->n_bits);
2918
0
    if (mf_are_prereqs_ok(a->field, flow, wc)
2919
0
        && mf_are_prereqs_ok(b->field, flow, wc)) {
2920
0
        unwildcard_subfield(a, wc);
2921
0
        unwildcard_subfield(b, wc);
2922
2923
0
        union mf_value a_value;
2924
0
        union mf_value b_value;
2925
0
        mf_get_value(a->field, flow, &a_value);
2926
0
        mf_get_value(b->field, flow, &b_value);
2927
0
        union mf_value b2_value = b_value;
2928
2929
        /* Copy 'a' into 'b'. */
2930
0
        bitwise_copy(&a_value, a->field->n_bytes, a->ofs,
2931
0
                     &b_value, b->field->n_bytes, b->ofs,
2932
0
                     a->n_bits);
2933
0
        mf_set_flow_value(b->field, &b_value, flow);
2934
2935
        /* Copy original 'b' into 'a'. */
2936
0
        bitwise_copy(&b2_value, b->field->n_bytes, b->ofs,
2937
0
                     &a_value, a->field->n_bytes, a->ofs,
2938
0
                     b->n_bits);
2939
0
        mf_set_flow_value(a->field, &a_value, flow);
2940
0
    }
2941
0
}
2942
2943
/* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
2944
 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2945
 * ofp_mkerr()).  */
2946
enum ofperr
2947
mf_check_src(const struct mf_subfield *sf, const struct match *match)
2948
96.1k
{
2949
96.1k
    return mf_check__(sf, match, "source");
2950
96.1k
}
2951
2952
/* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
2953
 * if so, otherwise an OpenFlow error code (e.g. as returned by
2954
 * ofp_mkerr()). */
2955
enum ofperr
2956
mf_check_dst(const struct mf_subfield *sf, const struct match *match)
2957
14.6k
{
2958
14.6k
    int error = mf_check__(sf, match, "destination");
2959
14.6k
    if (!error && !sf->field->writable) {
2960
50
        VLOG_WARN_RL(&rl, "destination field %s is not writable",
2961
50
                     sf->field->name);
2962
50
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2963
50
    }
2964
14.5k
    return error;
2965
14.6k
}
2966
2967
/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2968
 * 'value' and 'mask', respectively. */
2969
void
2970
mf_get(const struct mf_field *mf, const struct match *match,
2971
       union mf_value *value, union mf_value *mask)
2972
130k
{
2973
130k
    mf_get_value(mf, &match->flow, value);
2974
130k
    mf_get_mask(mf, &match->wc, mask);
2975
130k
}
2976
2977
static char *
2978
mf_from_integer_string(const struct mf_field *mf, const char *s,
2979
                       uint8_t *valuep, uint8_t *maskp)
2980
115k
{
2981
115k
    const char *err_str;
2982
115k
    char *tail;
2983
115k
    int err;
2984
2985
115k
    err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2986
115k
    if (err || (*tail != '\0' && *tail != '/')) {
2987
110
        err_str = "value";
2988
110
        goto syntax_error;
2989
110
    }
2990
2991
115k
    if (*tail == '/') {
2992
48.8k
        err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2993
48.8k
        if (err || *tail != '\0') {
2994
16
            err_str = "mask";
2995
16
            goto syntax_error;
2996
16
        }
2997
66.3k
    } else {
2998
66.3k
        memset(maskp, 0xff, mf->n_bytes);
2999
66.3k
    }
3000
3001
115k
    return NULL;
3002
3003
126
syntax_error:
3004
126
    if (err == ERANGE) {
3005
54
        return xasprintf("%s: %s too large for %u-byte field %s",
3006
54
                         s, err_str, mf->n_bytes, mf->name);
3007
72
    } else {
3008
72
        return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
3009
72
    }
3010
126
}
3011
3012
static char *
3013
mf_from_packet_type_string(const char *s, ovs_be32 *packet_type)
3014
569
{
3015
569
    const char *err_str;
3016
569
    char *tail;
3017
569
    int err;
3018
3019
569
    if (*s != '(') {
3020
3
        err_str = "missing '('";
3021
3
        goto syntax_error;
3022
3
    }
3023
566
    s++;
3024
566
    err = parse_int_string(s, (uint8_t *)packet_type, 2, &tail);
3025
566
    if (err) {
3026
1
        err_str = "ns";
3027
1
        goto syntax_error;
3028
1
    }
3029
565
    if (*tail != ',') {
3030
7
        err_str = "missing ','";
3031
7
        goto syntax_error;
3032
7
    }
3033
558
    s = tail + 1;
3034
558
    err = parse_int_string(s, ((uint8_t *)packet_type) + 2, 2, &tail);
3035
558
    if (err) {
3036
2
        err_str = "ns_type";
3037
2
        goto syntax_error;
3038
2
    }
3039
556
    if (*tail != ')') {
3040
1
        err_str = "missing ')'";
3041
1
        goto syntax_error;
3042
1
    }
3043
3044
555
    return NULL;
3045
3046
14
syntax_error:
3047
14
    return xasprintf("%s: bad syntax for packet type %s", s, err_str);
3048
556
}
3049
3050
static char *
3051
mf_from_ethernet_string(const struct mf_field *mf, const char *s,
3052
                        struct eth_addr *mac, struct eth_addr *mask)
3053
581
{
3054
581
    int n;
3055
3056
581
    ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
3057
3058
581
    n = -1;
3059
581
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
3060
548
        && n == strlen(s)) {
3061
208
        *mask = eth_addr_exact;
3062
208
        return NULL;
3063
208
    }
3064
3065
373
    n = -1;
3066
373
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
3067
373
                 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
3068
324
        && n == strlen(s)) {
3069
280
        return NULL;
3070
280
    }
3071
3072
93
    return xasprintf("%s: invalid Ethernet address", s);
3073
373
}
3074
3075
static char *
3076
mf_from_ipv4_string(const struct mf_field *mf, const char *s,
3077
                    ovs_be32 *ip, ovs_be32 *mask)
3078
1.74k
{
3079
1.74k
    ovs_assert(mf->n_bytes == sizeof *ip);
3080
1.74k
    return ip_parse_masked(s, ip, mask);
3081
1.74k
}
3082
3083
static char *
3084
mf_from_ipv6_string(const struct mf_field *mf, const char *s,
3085
                    struct in6_addr *ipv6, struct in6_addr *mask)
3086
3.27k
{
3087
3.27k
    ovs_assert(mf->n_bytes == sizeof *ipv6);
3088
3.27k
    return ipv6_parse_masked(s, ipv6, mask);
3089
3.27k
}
3090
3091
static char *
3092
mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
3093
                        const struct ofputil_port_map *port_map,
3094
                        ovs_be16 *valuep, ovs_be16 *maskp)
3095
366
{
3096
366
    ofp_port_t port;
3097
3098
366
    ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3099
3100
366
    if (ofputil_port_from_string(s, port_map, &port)) {
3101
364
        *valuep = htons(ofp_to_u16(port));
3102
364
        *maskp = OVS_BE16_MAX;
3103
364
        return NULL;
3104
364
    }
3105
2
    return xasprintf("%s: invalid or unknown port for %s", s, mf->name);
3106
366
}
3107
3108
static char *
3109
mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
3110
                          const struct ofputil_port_map *port_map,
3111
                          ovs_be32 *valuep, ovs_be32 *maskp)
3112
467
{
3113
467
    ofp_port_t port;
3114
3115
467
    ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3116
467
    if (ofputil_port_from_string(s, port_map, &port)) {
3117
464
        *valuep = ofputil_port_to_ofp11(port);
3118
464
        *maskp = OVS_BE32_MAX;
3119
464
        return NULL;
3120
464
    }
3121
3
    return xasprintf("%s: port value out of range for %s", s, mf->name);
3122
467
}
3123
3124
struct frag_handling {
3125
    const char *name;
3126
    uint8_t mask;
3127
    uint8_t value;
3128
};
3129
3130
static const struct frag_handling all_frags[] = {
3131
#define A FLOW_NW_FRAG_ANY
3132
#define L FLOW_NW_FRAG_LATER
3133
    /* name               mask  value */
3134
3135
    { "no",               A|L,  0     },
3136
    { "first",            A|L,  A     },
3137
    { "later",            A|L,  A|L   },
3138
3139
    { "no",               A,    0     },
3140
    { "yes",              A,    A     },
3141
3142
    { "not_later",        L,    0     },
3143
    { "later",            L,    L     },
3144
#undef A
3145
#undef L
3146
};
3147
3148
static char *
3149
mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
3150
506
{
3151
506
    const struct frag_handling *h;
3152
3153
1.73k
    for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
3154
1.68k
        if (!strcasecmp(s, h->name)) {
3155
            /* We force the upper bits of the mask on to make mf_parse_value()
3156
             * happy (otherwise it will never think it's an exact match.) */
3157
456
            *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
3158
456
            *valuep = h->value;
3159
456
            return NULL;
3160
456
        }
3161
1.68k
    }
3162
3163
50
    return xasprintf("%s: unknown fragment type (valid types are \"no\", "
3164
50
                     "\"yes\", \"first\", \"later\", \"not_later\"", s);
3165
506
}
3166
3167
static char *
3168
parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
3169
               const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
3170
               ovs_be16 *maskp)
3171
1.84k
{
3172
1.84k
    int err;
3173
1.84k
    char *err_str;
3174
1.84k
    uint32_t flags, mask;
3175
3176
1.84k
    err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
3177
1.84k
                      &flags, ntohs(allowed), maskp ? &mask : NULL);
3178
1.84k
    if (err < 0) {
3179
96
        return err_str;
3180
96
    }
3181
3182
1.75k
    *flagsp = htons(flags);
3183
1.75k
    if (maskp) {
3184
1.75k
        *maskp = htons(mask);
3185
1.75k
    }
3186
3187
1.75k
    return NULL;
3188
1.84k
}
3189
3190
static char *
3191
mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
3192
844
{
3193
844
    return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
3194
844
                          TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
3195
844
}
3196
3197
static char *
3198
mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
3199
1.00k
{
3200
1.00k
    return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
3201
1.00k
                          htons(FLOW_TNL_PUB_F_MASK), maskp);
3202
1.00k
}
3203
3204
static char *
3205
mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
3206
1.70k
{
3207
1.70k
    int err;
3208
1.70k
    char *err_str;
3209
1.70k
    uint32_t flags, mask;
3210
3211
1.70k
    err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
3212
1.70k
                      &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
3213
1.70k
    if (err < 0) {
3214
181
        return err_str;
3215
181
    }
3216
3217
1.51k
    *flagsp = htonl(flags);
3218
1.51k
    if (maskp) {
3219
1.51k
        *maskp = htonl(mask);
3220
1.51k
    }
3221
3222
1.51k
    return NULL;
3223
1.70k
}
3224
3225
/* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
3226
 * NULL if successful, otherwise a malloc()'d string describing the error. */
3227
char *
3228
mf_parse(const struct mf_field *mf, const char *s,
3229
         const struct ofputil_port_map *port_map,
3230
         union mf_value *value, union mf_value *mask)
3231
157k
{
3232
157k
    char *error;
3233
3234
157k
    if (!strcmp(s, "*")) {
3235
31.4k
        memset(value, 0, mf->n_bytes);
3236
31.4k
        memset(mask, 0, mf->n_bytes);
3237
31.4k
        return NULL;
3238
31.4k
    }
3239
3240
126k
    switch (mf->string) {
3241
20.3k
    case MFS_DECIMAL:
3242
115k
    case MFS_HEXADECIMAL:
3243
115k
        error = mf_from_integer_string(mf, s,
3244
115k
                                       (uint8_t *) value, (uint8_t *) mask);
3245
115k
        break;
3246
3247
1.70k
    case MFS_CT_STATE:
3248
1.70k
        ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3249
1.70k
        error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
3250
1.70k
        break;
3251
3252
581
    case MFS_ETHERNET:
3253
581
        error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
3254
581
        break;
3255
3256
1.74k
    case MFS_IPV4:
3257
1.74k
        error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
3258
1.74k
        break;
3259
3260
3.27k
    case MFS_IPV6:
3261
3.27k
        error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
3262
3.27k
        break;
3263
3264
366
    case MFS_OFP_PORT:
3265
366
        error = mf_from_ofp_port_string(mf, s, port_map,
3266
366
                                        &value->be16, &mask->be16);
3267
366
        break;
3268
3269
467
    case MFS_OFP_PORT_OXM:
3270
467
        error = mf_from_ofp_port_string32(mf, s, port_map,
3271
467
                                          &value->be32, &mask->be32);
3272
467
        break;
3273
3274
506
    case MFS_FRAG:
3275
506
        error = mf_from_frag_string(s, &value->u8, &mask->u8);
3276
506
        break;
3277
3278
1.00k
    case MFS_TNL_FLAGS:
3279
1.00k
        ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3280
1.00k
        error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
3281
1.00k
        break;
3282
3283
844
    case MFS_TCP_FLAGS:
3284
844
        ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3285
844
        error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
3286
844
        break;
3287
3288
569
    case MFS_PACKET_TYPE:
3289
569
        ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3290
569
        error = mf_from_packet_type_string(s, &value->be32);
3291
569
        mask->be32 = OVS_BE32_MAX;
3292
569
        break;
3293
3294
0
    default:
3295
0
        OVS_NOT_REACHED();
3296
126k
    }
3297
3298
126k
    if (!error && !mf_is_mask_valid(mf, mask)) {
3299
3
        error = xasprintf("%s: invalid mask for field %s", s, mf->name);
3300
3
    }
3301
126k
    return error;
3302
126k
}
3303
3304
/* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
3305
 * successful, otherwise a malloc()'d string describing the error. */
3306
char *
3307
mf_parse_value(const struct mf_field *mf, const char *s,
3308
               const struct ofputil_port_map *port_map, union mf_value *value)
3309
32.5k
{
3310
32.5k
    union mf_value mask;
3311
32.5k
    char *error;
3312
3313
32.5k
    error = mf_parse(mf, s, port_map, value, &mask);
3314
32.5k
    if (error) {
3315
14
        return error;
3316
14
    }
3317
3318
32.5k
    if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
3319
1
        return xasprintf("%s: wildcards not allowed here", s);
3320
1
    }
3321
32.5k
    return NULL;
3322
32.5k
}
3323
3324
static void
3325
mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
3326
                         const uint8_t *maskp, struct ds *s)
3327
10.4k
{
3328
10.4k
    if (mf->string == MFS_HEXADECIMAL) {
3329
8.52k
        ds_put_hex(s, valuep, mf->n_bytes);
3330
8.52k
    } else {
3331
1.97k
        unsigned long long int integer = 0;
3332
1.97k
        int i;
3333
3334
1.97k
        ovs_assert(mf->n_bytes <= 8);
3335
5.58k
        for (i = 0; i < mf->n_bytes; i++) {
3336
3.61k
            integer = (integer << 8) | valuep[i];
3337
3.61k
        }
3338
1.97k
        ds_put_format(s, "%lld", integer);
3339
1.97k
    }
3340
3341
10.4k
    if (maskp) {
3342
        /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
3343
         * not sure that that a bit-mask written in decimal is ever easier to
3344
         * understand than the same bit-mask written in hexadecimal. */
3345
510
        ds_put_char(s, '/');
3346
510
        ds_put_hex(s, maskp, mf->n_bytes);
3347
510
    }
3348
10.4k
}
3349
3350
static void
3351
mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
3352
276
{
3353
276
    const struct frag_handling *h;
3354
3355
276
    mask &= FLOW_NW_FRAG_MASK;
3356
276
    value &= mask;
3357
3358
1.90k
    for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
3359
1.68k
        if (value == h->value && mask == h->mask) {
3360
50
            ds_put_cstr(s, h->name);
3361
50
            return;
3362
50
        }
3363
1.68k
    }
3364
226
    ds_put_cstr(s, "<error>");
3365
226
}
3366
3367
static void
3368
mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
3369
302
{
3370
302
    format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
3371
302
                        ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
3372
302
}
3373
3374
static void
3375
mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
3376
1.81k
{
3377
1.81k
    format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
3378
1.81k
                        TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
3379
1.81k
}
3380
3381
static void
3382
mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
3383
2.09k
{
3384
2.09k
    format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
3385
2.09k
                        ntohl(mask), UINT16_MAX);
3386
2.09k
}
3387
3388
static void
3389
mf_format_packet_type_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
3390
1.15k
{
3391
1.15k
    format_packet_type_masked(s, value, mask);
3392
1.15k
}
3393
3394
/* Appends to 's' a string representation of field 'mf' whose value is in
3395
 * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
3396
void
3397
mf_format(const struct mf_field *mf,
3398
          const union mf_value *value, const union mf_value *mask,
3399
          const struct ofputil_port_map *port_map,
3400
          struct ds *s)
3401
20.5k
{
3402
20.5k
    if (mask) {
3403
7.39k
        if (is_all_zeros(mask, mf->n_bytes)) {
3404
2.72k
            ds_put_cstr(s, "ANY");
3405
2.72k
            return;
3406
4.66k
        } else if (is_all_ones(mask, mf->n_bytes)) {
3407
4.01k
            mask = NULL;
3408
4.01k
        }
3409
7.39k
    }
3410
3411
17.8k
    switch (mf->string) {
3412
1.49k
    case MFS_OFP_PORT_OXM:
3413
1.49k
        if (!mask) {
3414
1.37k
            ofp_port_t port;
3415
1.37k
            ofputil_port_from_ofp11(value->be32, &port);
3416
1.37k
            ofputil_format_port(port, port_map, s);
3417
1.37k
            break;
3418
1.37k
        }
3419
        /* fall through */
3420
210
    case MFS_OFP_PORT:
3421
210
        if (!mask) {
3422
97
            ofputil_format_port(u16_to_ofp(ntohs(value->be16)), port_map, s);
3423
97
            break;
3424
97
        }
3425
        /* fall through */
3426
1.97k
    case MFS_DECIMAL:
3427
10.4k
    case MFS_HEXADECIMAL:
3428
10.4k
        mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
3429
10.4k
        break;
3430
3431
2.09k
    case MFS_CT_STATE:
3432
2.09k
        mf_format_ct_state_string(value->be32,
3433
2.09k
                                  mask ? mask->be32 : OVS_BE32_MAX, s);
3434
2.09k
        break;
3435
3436
70
    case MFS_ETHERNET:
3437
70
        eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
3438
70
        break;
3439
3440
123
    case MFS_IPV4:
3441
123
        ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
3442
123
        break;
3443
3444
31
    case MFS_IPV6:
3445
31
        ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
3446
31
        break;
3447
3448
276
    case MFS_FRAG:
3449
276
        mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
3450
276
        break;
3451
3452
302
    case MFS_TNL_FLAGS:
3453
302
        mf_format_tnl_flags_string(value->be16,
3454
302
                                   mask ? mask->be16 : OVS_BE16_MAX, s);
3455
302
        break;
3456
3457
1.81k
    case MFS_TCP_FLAGS:
3458
1.81k
        mf_format_tcp_flags_string(value->be16,
3459
1.81k
                                   mask ? mask->be16 : OVS_BE16_MAX, s);
3460
1.81k
        break;
3461
3462
1.15k
    case MFS_PACKET_TYPE:
3463
1.15k
        mf_format_packet_type_string(value->be32,
3464
1.15k
                                     mask ? mask->be32 : OVS_BE32_MAX, s);
3465
1.15k
        break;
3466
3467
0
    default:
3468
0
        OVS_NOT_REACHED();
3469
17.8k
    }
3470
17.8k
}
3471

3472
/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3473
 * least-significant bits in 'x'.
3474
 */
3475
void
3476
mf_write_subfield_flow(const struct mf_subfield *sf,
3477
                       const union mf_subvalue *x, struct flow *flow)
3478
0
{
3479
0
    const struct mf_field *field = sf->field;
3480
0
    union mf_value value;
3481
3482
0
    mf_get_value(field, flow, &value);
3483
0
    bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
3484
0
                 sf->ofs, sf->n_bits);
3485
0
    mf_set_flow_value(field, &value, flow);
3486
0
}
3487
3488
/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
3489
 * least-significant bits in 'x'.
3490
 */
3491
void
3492
mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
3493
                  struct match *match)
3494
0
{
3495
0
    const struct mf_field *field = sf->field;
3496
0
    union mf_value value, mask;
3497
3498
0
    mf_get(field, match, &value, &mask);
3499
0
    bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3500
0
    bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
3501
0
    mf_set(field, &value, &mask, match, NULL);
3502
0
}
3503
3504
void
3505
mf_write_subfield_value(const struct mf_subfield *sf, const void *src,
3506
                        struct match *match)
3507
74.5k
{
3508
74.5k
    const struct mf_field *field = sf->field;
3509
74.5k
    union mf_value value, mask;
3510
74.5k
    unsigned int size = DIV_ROUND_UP(sf->n_bits, 8);
3511
3512
74.5k
    mf_get(field, match, &value, &mask);
3513
74.5k
    bitwise_copy(src, size, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3514
74.5k
    bitwise_one (              &mask,  field->n_bytes, sf->ofs, sf->n_bits);
3515
74.5k
    mf_set(field, &value, &mask, match, NULL);
3516
74.5k
}
3517
3518
/* 'v' and 'm' correspond to values of 'field'.  This function copies them into
3519
 * 'match' in the correspond positions. */
3520
void
3521
mf_mask_subfield(const struct mf_field *field,
3522
                 const union mf_subvalue *v,
3523
                 const union mf_subvalue *m,
3524
                 struct match *match)
3525
0
{
3526
0
    union mf_value value, mask;
3527
3528
0
    mf_get(field, match, &value, &mask);
3529
0
    bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
3530
0
    bitwise_copy(m, sizeof *m, 0, &mask,  field->n_bytes, 0, field->n_bits);
3531
0
    mf_set(field, &value, &mask, match, NULL);
3532
0
}
3533
3534
/* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
3535
 * reading 'flow', e.g. as checked by mf_check_src(). */
3536
void
3537
mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
3538
                 union mf_subvalue *x)
3539
0
{
3540
0
    union mf_value value;
3541
3542
0
    mf_get_value(sf->field, flow, &value);
3543
3544
0
    memset(x, 0, sizeof *x);
3545
0
    bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
3546
0
                 x, sizeof *x, 0,
3547
0
                 sf->n_bits);
3548
0
}
3549
3550
/* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
3551
 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
3552
 * less. */
3553
uint64_t
3554
mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
3555
0
{
3556
0
    union mf_value value;
3557
3558
0
    mf_get_value(sf->field, flow, &value);
3559
0
    return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
3560
0
}
3561
3562
void
3563
mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3564
0
{
3565
0
    ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
3566
0
}
3567
3568
void
3569
field_array_set(enum mf_field_id id, const union mf_value *value,
3570
                struct field_array *fa)
3571
8.74k
{
3572
8.74k
    size_t i, offset = 0;
3573
3574
8.74k
    ovs_assert(id < MFF_N_IDS);
3575
3576
    /* Find the spot for 'id'. */
3577
8.74k
    BITMAP_FOR_EACH_1 (i, id, fa->used.bm) {
3578
5.79k
        offset += mf_from_id(i)->n_bytes;
3579
5.79k
    }
3580
3581
8.74k
    size_t value_size = mf_from_id(id)->n_bytes;
3582
3583
    /* make room if necessary. */
3584
8.74k
    if (!bitmap_is_set(fa->used.bm, id)) {
3585
8.74k
        fa->values = xrealloc(fa->values, fa->values_size + value_size);
3586
        /* Move remainder forward, if any. */
3587
8.74k
        if (offset < fa->values_size) {
3588
2.10k
            memmove(fa->values + offset + value_size, fa->values + offset,
3589
2.10k
                    fa->values_size - offset);
3590
2.10k
        }
3591
8.74k
        fa->values_size += value_size;
3592
8.74k
    }
3593
8.74k
    bitmap_set1(fa->used.bm, id);
3594
3595
8.74k
    memcpy(fa->values + offset, value, value_size);
3596
8.74k
}
3597
3598
/* A wrapper for variable length mf_fields that is maintained by
3599
 * struct vl_mff_map.*/
3600
struct vl_mf_field {
3601
    struct mf_field mf;
3602
    struct ovs_refcount ref_cnt;
3603
    struct cmap_node cmap_node; /* In ofproto->vl_mff_map->cmap. */
3604
};
3605
3606
static inline uint32_t
3607
mf_field_hash(uint32_t key)
3608
0
{
3609
0
    return hash_int(key, 0);
3610
0
}
3611
3612
static void
3613
vmf_delete(struct vl_mf_field *vmf)
3614
0
{
3615
0
    if (ovs_refcount_unref(&vmf->ref_cnt) == 1) {
3616
        /* Postpone as this function is typically called immediately
3617
         * after removing from cmap. */
3618
0
        ovsrcu_postpone(free, vmf);
3619
0
    } else {
3620
0
        VLOG_WARN_RL(&rl,
3621
0
                     "Attempted to delete VMF %s but refcount is nonzero!",
3622
0
                     vmf->mf.name);
3623
0
    }
3624
0
}
3625
3626
enum ofperr
3627
mf_vl_mff_map_clear(struct vl_mff_map *vl_mff_map, bool force)
3628
    OVS_REQUIRES(vl_mff_map->mutex)
3629
0
{
3630
0
    struct vl_mf_field *vmf;
3631
3632
0
    if (!force) {
3633
0
        CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3634
0
            if (ovs_refcount_read(&vmf->ref_cnt) != 1) {
3635
0
                return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3636
0
            }
3637
0
        }
3638
0
    }
3639
3640
0
    CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3641
0
        cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
3642
0
                    mf_field_hash(vmf->mf.id));
3643
0
        vmf_delete(vmf);
3644
0
    }
3645
3646
0
    return 0;
3647
0
}
3648
3649
static struct vl_mf_field *
3650
mf_get_vl_mff__(uint32_t id, const struct vl_mff_map *vl_mff_map)
3651
0
{
3652
0
    struct vl_mf_field *vmf;
3653
3654
0
    CMAP_FOR_EACH_WITH_HASH (vmf, cmap_node, mf_field_hash(id),
3655
0
                             &vl_mff_map->cmap) {
3656
0
        if (vmf->mf.id == id) {
3657
0
            return vmf;
3658
0
        }
3659
0
    }
3660
3661
0
    return NULL;
3662
0
}
3663
3664
/* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
3665
 * pointer to the variable length meta-flow field corresponding to 'mff'.
3666
 * Returns NULL if no mapping is existed for 'mff'. */
3667
const struct mf_field *
3668
mf_get_vl_mff(const struct mf_field *mff,
3669
              const struct vl_mff_map *vl_mff_map)
3670
277k
{
3671
277k
    if (mff && mff->variable_len && vl_mff_map) {
3672
0
        struct vl_mf_field *vl_mff = mf_get_vl_mff__(mff->id, vl_mff_map);
3673
3674
0
        return vl_mff ? &vl_mff->mf : NULL;
3675
0
    }
3676
3677
277k
    return NULL;
3678
277k
}
3679
3680
static enum ofperr
3681
mf_vl_mff_map_del(struct vl_mff_map *vl_mff_map,
3682
                  const struct ofputil_tlv_table_mod *ttm, bool force)
3683
    OVS_REQUIRES(vl_mff_map->mutex)
3684
0
{
3685
0
    struct ofputil_tlv_map *tlv_map;
3686
0
    struct vl_mf_field *vmf;
3687
0
    unsigned int idx;
3688
3689
0
    if (!force) {
3690
0
        LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3691
0
            idx = MFF_TUN_METADATA0 + tlv_map->index;
3692
0
            if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3693
0
                return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3694
0
            }
3695
3696
0
            vmf = mf_get_vl_mff__(idx, vl_mff_map);
3697
0
            if (vmf && ovs_refcount_read(&vmf->ref_cnt) != 1) {
3698
0
                return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3699
0
            }
3700
0
        }
3701
0
    }
3702
3703
0
    LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3704
0
        idx = MFF_TUN_METADATA0 + tlv_map->index;
3705
0
        if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3706
0
            return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3707
0
        }
3708
3709
0
        vmf = mf_get_vl_mff__(idx, vl_mff_map);
3710
0
        if (vmf) {
3711
0
            cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
3712
0
                        mf_field_hash(idx));
3713
0
            vmf_delete(vmf);
3714
0
        }
3715
0
    }
3716
3717
0
    return 0;
3718
0
}
3719
3720
static enum ofperr
3721
mf_vl_mff_map_add(struct vl_mff_map *vl_mff_map,
3722
                  const struct ofputil_tlv_table_mod *ttm)
3723
    OVS_REQUIRES(vl_mff_map->mutex)
3724
0
{
3725
0
    struct ofputil_tlv_map *tlv_map;
3726
0
    struct vl_mf_field *vmf;
3727
0
    unsigned int idx;
3728
3729
0
    LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3730
0
        idx = MFF_TUN_METADATA0 + tlv_map->index;
3731
0
        if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3732
0
            return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3733
0
        }
3734
3735
0
        vmf = xmalloc(sizeof *vmf);
3736
0
        vmf->mf = mf_fields[idx];
3737
0
        vmf->mf.n_bytes = tlv_map->option_len;
3738
0
        vmf->mf.n_bits = tlv_map->option_len * 8;
3739
0
        vmf->mf.mapped = true;
3740
0
        ovs_refcount_init(&vmf->ref_cnt);
3741
3742
0
        cmap_insert(&vl_mff_map->cmap, &vmf->cmap_node,
3743
0
                    mf_field_hash(idx));
3744
0
    }
3745
3746
0
    return 0;
3747
0
}
3748
3749
/* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3750
 * This function must be invoked after tun_metadata_table_mod().
3751
 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3752
 * invalid.
3753
 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3754
 * vl_mf_field that is still used by any active flow.*/
3755
enum ofperr
3756
mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map *vl_mff_map,
3757
                                    const struct ofputil_tlv_table_mod *ttm)
3758
    OVS_REQUIRES(vl_mff_map->mutex)
3759
0
{
3760
0
    switch (ttm->command) {
3761
0
    case NXTTMC_ADD:
3762
0
        return mf_vl_mff_map_add(vl_mff_map, ttm);
3763
3764
0
    case NXTTMC_DELETE:
3765
0
        return mf_vl_mff_map_del(vl_mff_map, ttm, false);
3766
3767
0
    case NXTTMC_CLEAR:
3768
0
        return mf_vl_mff_map_clear(vl_mff_map, false);
3769
3770
0
    default:
3771
0
        OVS_NOT_REACHED();
3772
0
    }
3773
3774
0
    return 0;
3775
0
}
3776
3777
/* Returns true if a variable length meta-flow field 'mff' is not mapped in
3778
 * the 'vl_mff_map'. */
3779
bool
3780
mf_vl_mff_invalid(const struct mf_field *mff, const struct vl_mff_map *map)
3781
277k
{
3782
277k
    return map && mff && mff->variable_len && !mff->mapped;
3783
277k
}
3784
3785
void
3786
mf_vl_mff_set_tlv_bitmap(const struct mf_field *mff, uint64_t *tlv_bitmap)
3787
86.2k
{
3788
86.2k
    if (mff && mff->mapped) {
3789
0
        ovs_assert(mf_is_tun_metadata(mff));
3790
0
        ULLONG_SET1(*tlv_bitmap, mff->id - MFF_TUN_METADATA0);
3791
0
    }
3792
86.2k
}
3793
3794
static void
3795
mf_vl_mff_ref_cnt_mod(const struct vl_mff_map *map, uint64_t tlv_bitmap,
3796
                      bool ref)
3797
0
{
3798
0
    struct vl_mf_field *vmf;
3799
0
    int i;
3800
3801
0
    if (map) {
3802
0
        ULLONG_FOR_EACH_1 (i, tlv_bitmap) {
3803
0
            vmf = mf_get_vl_mff__(i + MFF_TUN_METADATA0, map);
3804
0
            if (vmf) {
3805
0
                if (ref) {
3806
0
                    ovs_refcount_ref(&vmf->ref_cnt);
3807
0
                } else {
3808
0
                    ovs_refcount_unref(&vmf->ref_cnt);
3809
0
                }
3810
0
            } else {
3811
0
                VLOG_WARN("Invalid TLV index %d.", i);
3812
0
            }
3813
0
        }
3814
0
    }
3815
0
}
3816
3817
void
3818
mf_vl_mff_ref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3819
0
{
3820
0
    mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, true);
3821
0
}
3822
3823
void
3824
mf_vl_mff_unref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3825
0
{
3826
0
    mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, false);
3827
0
}
3828
3829
enum ofperr
3830
mf_vl_mff_nx_pull_header(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3831
                         const struct mf_field **field, bool *masked,
3832
                         uint64_t *tlv_bitmap)
3833
22.0k
{
3834
22.0k
    enum ofperr error = nx_pull_header(b, vl_mff_map, field, masked);
3835
22.0k
    if (error) {
3836
2.69k
        return error;
3837
2.69k
    }
3838
3839
19.3k
    mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3840
19.3k
    return 0;
3841
22.0k
}
3842
3843
enum ofperr
3844
mf_vl_mff_nx_pull_entry(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3845
                        const struct mf_field **field, union mf_value *value,
3846
                        union mf_value *mask, uint64_t *tlv_bitmap)
3847
22.7k
{
3848
22.7k
    enum ofperr error = nx_pull_entry(b, vl_mff_map, field, value, mask, true);
3849
22.7k
    if (error) {
3850
1.65k
        return error;
3851
1.65k
    }
3852
3853
21.0k
    mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3854
21.0k
    return 0;
3855
22.7k
}
3856
3857
enum ofperr
3858
mf_vl_mff_mf_from_nxm_header(uint32_t header,
3859
                             const struct vl_mff_map *vl_mff_map,
3860
                             const struct mf_field **field,
3861
                             uint64_t *tlv_bitmap)
3862
50.2k
{
3863
50.2k
    *field = mf_from_nxm_header(header, vl_mff_map);
3864
50.2k
    if (!*field) {
3865
4.45k
        return OFPERR_OFPBAC_BAD_SET_TYPE;
3866
45.7k
    } else if (mf_vl_mff_invalid(*field, vl_mff_map)) {
3867
0
        return OFPERR_NXFMFC_INVALID_TLV_FIELD;
3868
0
    }
3869
3870
45.7k
    mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3871
45.7k
    return 0;
3872
50.2k
}
3873

3874
/* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
3875
 * false otherwise. */
3876
bool
3877
mf_bitmap_is_superset(const struct mf_bitmap *super,
3878
                      const struct mf_bitmap *sub)
3879
0
{
3880
0
    return bitmap_is_superset(super->bm, sub->bm, MFF_N_IDS);
3881
0
}
3882
3883
/* Returns the bitwise-and of 'a' and 'b'. */
3884
struct mf_bitmap
3885
mf_bitmap_and(struct mf_bitmap a, struct mf_bitmap b)
3886
144k
{
3887
144k
    bitmap_and(a.bm, b.bm, MFF_N_IDS);
3888
144k
    return a;
3889
144k
}
3890
3891
/* Returns the bitwise-or of 'a' and 'b'. */
3892
struct mf_bitmap
3893
mf_bitmap_or(struct mf_bitmap a, struct mf_bitmap b)
3894
0
{
3895
0
    bitmap_or(a.bm, b.bm, MFF_N_IDS);
3896
0
    return a;
3897
0
}
3898
3899
/* Returns the bitwise-not of 'x'. */
3900
struct mf_bitmap
3901
mf_bitmap_not(struct mf_bitmap x)
3902
72.2k
{
3903
72.2k
    bitmap_not(x.bm, MFF_N_IDS);
3904
72.2k
    return x;
3905
72.2k
}
3906
3907
void
3908
mf_set_mask_l3_prereqs(const struct mf_field *mf, const struct flow *fl,
3909
                       struct flow_wildcards *wc)
3910
0
{
3911
0
    if (is_ip_any(fl) &&
3912
0
        ((mf->id == MFF_IPV4_SRC) ||
3913
0
         (mf->id == MFF_IPV4_DST) ||
3914
0
         (mf->id == MFF_IPV6_SRC) ||
3915
0
         (mf->id == MFF_IPV6_DST) ||
3916
0
         (mf->id == MFF_IPV6_LABEL) ||
3917
0
         (mf->id == MFF_IP_DSCP) ||
3918
0
         (mf->id == MFF_IP_ECN) ||
3919
0
         (mf->id == MFF_IP_TTL))) {
3920
0
        WC_MASK_FIELD(wc, nw_proto);
3921
0
    } else if ((fl->dl_type == htons(ETH_TYPE_ARP)) &&
3922
0
               ((mf->id == MFF_ARP_OP) ||
3923
0
                (mf->id == MFF_ARP_SHA) ||
3924
0
                (mf->id == MFF_ARP_THA) ||
3925
0
                (mf->id == MFF_ARP_SPA) ||
3926
0
                (mf->id == MFF_ARP_TPA))) {
3927
        /* mask only the lower 8 bits. */
3928
0
        wc->masks.nw_proto = 0xff;
3929
0
    }
3930
0
}