Coverage Report

Created: 2025-10-10 06:39

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
162k
{
86
162k
    nxm_init();
87
162k
    return shash_find_data(&mf_by_name, name);
88
162k
}
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
155k
{
95
155k
    nxm_init();
96
97
155k
    struct shash_node *node = shash_find_len(&mf_by_name, name, len);
98
155k
    return node ? node->data : NULL;
99
155k
}
100
101
static void
102
nxm_do_init(void)
103
1
{
104
1
    int i;
105
106
1
    shash_init(&mf_by_name);
107
184
    for (i = 0; i < MFF_N_IDS; i++) {
108
183
        const struct mf_field *mf = &mf_fields[i];
109
110
183
        ovs_assert(mf->id == i); /* Fields must be in the enum order. */
111
112
183
        shash_add_once(&mf_by_name, mf->name, mf);
113
183
        if (mf->extra_name) {
114
17
            shash_add_once(&mf_by_name, mf->extra_name, mf);
115
17
        }
116
183
    }
117
1
}
118
119
static void
120
nxm_init(void)
121
317k
{
122
317k
    static pthread_once_t once = PTHREAD_ONCE_INIT;
123
317k
    pthread_once(&once, nxm_do_init);
124
317k
}
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
0
{
196
0
    ds_put_hex(s, sv, sizeof *sv);
197
0
}
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
0
{
207
0
    switch (mf->id) {
208
0
    case MFF_DP_HASH:
209
0
        return !wc->masks.dp_hash;
210
0
    case MFF_RECIRC_ID:
211
0
        return !wc->masks.recirc_id;
212
0
    case MFF_PACKET_TYPE:
213
0
        return !wc->masks.packet_type;
214
0
    case MFF_CONJ_ID:
215
0
        return !wc->masks.conj_id;
216
0
    case MFF_TUN_SRC:
217
0
        return !wc->masks.tunnel.ip_src;
218
0
    case MFF_TUN_DST:
219
0
        return !wc->masks.tunnel.ip_dst;
220
0
    case MFF_TUN_IPV6_SRC:
221
0
        return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_src);
222
0
    case MFF_TUN_IPV6_DST:
223
0
        return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_dst);
224
0
    case MFF_TUN_ID:
225
0
        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
0
    case MFF_TUN_FLAGS:
231
0
        return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
232
0
    case MFF_TUN_GBP_ID:
233
0
        return !wc->masks.tunnel.gbp_id;
234
0
    case MFF_TUN_GBP_FLAGS:
235
0
        return !wc->masks.tunnel.gbp_flags;
236
0
    case MFF_TUN_ERSPAN_VER:
237
0
        return !wc->masks.tunnel.erspan_ver;
238
0
    case MFF_TUN_ERSPAN_IDX:
239
0
        return !wc->masks.tunnel.erspan_idx;
240
0
    case MFF_TUN_ERSPAN_DIR:
241
0
        return !wc->masks.tunnel.erspan_dir;
242
0
    case MFF_TUN_ERSPAN_HWID:
243
0
        return !wc->masks.tunnel.erspan_hwid;
244
0
    CASE_MFF_TUN_METADATA:
245
0
        return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
246
0
                           mf->id - MFF_TUN_METADATA0);
247
0
    case MFF_METADATA:
248
0
        return !wc->masks.metadata;
249
0
    case MFF_IN_PORT:
250
0
    case MFF_IN_PORT_OXM:
251
0
        return !wc->masks.in_port.ofp_port;
252
0
    case MFF_SKB_PRIORITY:
253
0
        return !wc->masks.skb_priority;
254
0
    case MFF_PKT_MARK:
255
0
        return !wc->masks.pkt_mark;
256
0
    case MFF_CT_STATE:
257
0
        return !wc->masks.ct_state;
258
0
    case MFF_CT_ZONE:
259
0
        return !wc->masks.ct_zone;
260
0
    case MFF_CT_MARK:
261
0
        return !wc->masks.ct_mark;
262
0
    case MFF_CT_LABEL:
263
0
        return ovs_u128_is_zero(wc->masks.ct_label);
264
0
    case MFF_CT_NW_PROTO:
265
0
        return !wc->masks.ct_nw_proto;
266
0
    case MFF_CT_NW_SRC:
267
0
        return !wc->masks.ct_nw_src;
268
0
    case MFF_CT_NW_DST:
269
0
        return !wc->masks.ct_nw_dst;
270
0
    case MFF_CT_TP_SRC:
271
0
        return !wc->masks.ct_tp_src;
272
0
    case MFF_CT_TP_DST:
273
0
        return !wc->masks.ct_tp_dst;
274
0
    case MFF_CT_IPV6_SRC:
275
0
        return ipv6_mask_is_any(&wc->masks.ct_ipv6_src);
276
0
    case MFF_CT_IPV6_DST:
277
0
        return ipv6_mask_is_any(&wc->masks.ct_ipv6_dst);
278
0
    CASE_MFF_REGS:
279
0
        return !wc->masks.regs[mf->id - MFF_REG0];
280
0
    CASE_MFF_XREGS:
281
0
        return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
282
0
    CASE_MFF_XXREGS: {
283
0
        ovs_u128 value = flow_get_xxreg(&wc->masks, mf->id - MFF_XXREG0);
284
0
        return ovs_u128_is_zero(value);
285
0
    }
286
0
    case MFF_ACTSET_OUTPUT:
287
0
        return !wc->masks.actset_output;
288
289
0
    case MFF_ETH_SRC:
290
0
        return eth_addr_is_zero(wc->masks.dl_src);
291
0
    case MFF_ETH_DST:
292
0
        return eth_addr_is_zero(wc->masks.dl_dst);
293
0
    case MFF_ETH_TYPE:
294
0
        return !wc->masks.dl_type;
295
296
0
    case MFF_ARP_SHA:
297
0
    case MFF_ND_SLL:
298
0
        return eth_addr_is_zero(wc->masks.arp_sha);
299
300
0
    case MFF_ARP_THA:
301
0
    case MFF_ND_TLL:
302
0
        return eth_addr_is_zero(wc->masks.arp_tha);
303
304
0
    case MFF_VLAN_TCI:
305
0
        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
0
    case MFF_VLAN_VID:
309
0
        return !(wc->masks.vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI));
310
0
    case MFF_DL_VLAN_PCP:
311
0
    case MFF_VLAN_PCP:
312
0
        return !(wc->masks.vlans[0].tci & htons(VLAN_PCP_MASK));
313
314
0
    case MFF_MPLS_LABEL:
315
0
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
316
0
    case MFF_MPLS_TC:
317
0
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
318
0
    case MFF_MPLS_BOS:
319
0
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
320
0
    case MFF_MPLS_TTL:
321
0
        return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
322
323
0
    case MFF_IPV4_SRC:
324
0
        return !wc->masks.nw_src;
325
0
    case MFF_IPV4_DST:
326
0
        return !wc->masks.nw_dst;
327
328
0
    case MFF_IPV6_SRC:
329
0
        return ipv6_mask_is_any(&wc->masks.ipv6_src);
330
0
    case MFF_IPV6_DST:
331
0
        return ipv6_mask_is_any(&wc->masks.ipv6_dst);
332
333
0
    case MFF_IPV6_LABEL:
334
0
        return !wc->masks.ipv6_label;
335
336
0
    case MFF_IP_PROTO:
337
0
        return !wc->masks.nw_proto;
338
0
    case MFF_IP_DSCP:
339
0
    case MFF_IP_DSCP_SHIFTED:
340
0
        return !(wc->masks.nw_tos & IP_DSCP_MASK);
341
0
    case MFF_IP_ECN:
342
0
        return !(wc->masks.nw_tos & IP_ECN_MASK);
343
0
    case MFF_IP_TTL:
344
0
        return !wc->masks.nw_ttl;
345
346
0
    case MFF_ND_TARGET:
347
0
        return ipv6_mask_is_any(&wc->masks.nd_target);
348
349
0
    case MFF_ND_RESERVED:
350
0
        return !wc->masks.igmp_group_ip4;
351
0
    case MFF_ND_OPTIONS_TYPE:
352
0
        return !wc->masks.tcp_flags;
353
354
0
    case MFF_IP_FRAG:
355
0
        return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
356
357
0
    case MFF_ARP_OP:
358
0
        return !wc->masks.nw_proto;
359
0
    case MFF_ARP_SPA:
360
0
        return !wc->masks.nw_src;
361
0
    case MFF_ARP_TPA:
362
0
        return !wc->masks.nw_dst;
363
364
0
    case MFF_TCP_SRC:
365
0
    case MFF_UDP_SRC:
366
0
    case MFF_SCTP_SRC:
367
0
    case MFF_ICMPV4_TYPE:
368
0
    case MFF_ICMPV6_TYPE:
369
0
        return !wc->masks.tp_src;
370
0
    case MFF_TCP_DST:
371
0
    case MFF_UDP_DST:
372
0
    case MFF_SCTP_DST:
373
0
    case MFF_ICMPV4_CODE:
374
0
    case MFF_ICMPV6_CODE:
375
0
        return !wc->masks.tp_dst;
376
0
    case MFF_TCP_FLAGS:
377
0
        return !wc->masks.tcp_flags;
378
379
0
    case MFF_NSH_FLAGS:
380
0
        return !wc->masks.nsh.flags;
381
0
    case MFF_NSH_TTL:
382
0
        return !wc->masks.nsh.ttl;
383
0
    case MFF_NSH_MDTYPE:
384
0
        return !wc->masks.nsh.mdtype;
385
0
    case MFF_NSH_NP:
386
0
        return !wc->masks.nsh.np;
387
0
    case MFF_NSH_SPI:
388
0
        return !(wc->masks.nsh.path_hdr & htonl(NSH_SPI_MASK));
389
0
    case MFF_NSH_SI:
390
0
        return !(wc->masks.nsh.path_hdr & htonl(NSH_SI_MASK));
391
0
    case MFF_NSH_C1:
392
0
    case MFF_NSH_C2:
393
0
    case MFF_NSH_C3:
394
0
    case MFF_NSH_C4:
395
0
        return !wc->masks.nsh.context[mf->id - MFF_NSH_C1];
396
0
    case MFF_TUN_GTPU_FLAGS:
397
0
        return !wc->masks.tunnel.gtpu_flags;
398
0
    case MFF_TUN_GTPU_MSGTYPE:
399
0
        return !wc->masks.tunnel.gtpu_msgtype;
400
401
0
    case MFF_N_IDS:
402
0
    default:
403
0
        OVS_NOT_REACHED();
404
0
    }
405
0
}
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
91.0k
{
417
91.0k
    mf_get_value(mf, &wc->masks, mask);
418
91.0k
}
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
99.7k
{
425
99.7k
    switch (mf->maskable) {
426
17.3k
    case MFM_NONE:
427
17.3k
        return (is_all_zeros(mask, mf->n_bytes) ||
428
8.71k
                is_all_ones(mask, mf->n_bytes));
429
430
82.3k
    case MFM_FULLY:
431
82.3k
        return true;
432
99.7k
    }
433
434
99.7k
    OVS_NOT_REACHED();
435
99.7k
}
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
82.0k
{
445
82.0k
    ovs_be16 dl_type = get_dl_type(flow);
446
447
82.0k
    switch (mf->prereqs) {
448
68.7k
    case MFP_NONE:
449
68.7k
        return true;
450
3.40k
    case MFP_ETHERNET:
451
3.40k
        return is_ethernet(flow, wc);
452
560
    case MFP_ARP:
453
560
        return (dl_type == htons(ETH_TYPE_ARP) ||
454
234
                dl_type == htons(ETH_TYPE_RARP));
455
416
    case MFP_IPV4:
456
416
        return dl_type == htons(ETH_TYPE_IP);
457
271
    case MFP_IPV6:
458
271
        return dl_type == htons(ETH_TYPE_IPV6);
459
431
    case MFP_VLAN_VID:
460
431
        return is_vlan(flow, wc);
461
430
    case MFP_MPLS:
462
430
        return eth_type_mpls(dl_type);
463
971
    case MFP_IP_ANY:
464
971
        return is_ip_any(flow);
465
963
    case MFP_NSH:
466
963
        return dl_type == htons(ETH_TYPE_NSH);
467
135
    case MFP_CT_VALID:
468
135
        return is_ct_valid(flow, mask, wc);
469
1.41k
    case MFP_TCP:
470
        /* Matching !FRAG_LATER is not enforced (mask is not checked). */
471
1.41k
        return is_tcp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
472
1.46k
    case MFP_UDP:
473
1.46k
        return is_udp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
474
570
    case MFP_SCTP:
475
570
        return is_sctp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
476
474
    case MFP_ICMPV4:
477
474
        return is_icmpv4(flow, wc);
478
465
    case MFP_ICMPV6:
479
465
        return is_icmpv6(flow, wc);
480
557
    case MFP_ND:
481
557
        return is_nd(flow, wc);
482
303
    case MFP_ND_SOLICIT:
483
303
        return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT);
484
451
    case MFP_ND_ADVERT:
485
451
        return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_ADVERT);
486
82.0k
    }
487
488
82.0k
    OVS_NOT_REACHED();
489
82.0k
}
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
25.2k
{
497
25.2k
    return mf_are_prereqs_ok__(mf, flow, NULL, wc);
498
25.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
56.8k
{
505
56.8k
    return mf_are_prereqs_ok__(mf, &match->flow, &match->wc, NULL);
506
56.8k
}
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
40.6k
{
521
40.6k
    switch (mf->id) {
522
0
    case MFF_DP_HASH:
523
0
    case MFF_RECIRC_ID:
524
0
    case MFF_PACKET_TYPE:
525
0
    case MFF_CONJ_ID:
526
195
    case MFF_TUN_ID:
527
390
    case MFF_TUN_SRC:
528
598
    case MFF_TUN_DST:
529
792
    case MFF_TUN_IPV6_SRC:
530
860
    case MFF_TUN_IPV6_DST:
531
860
    case MFF_TUN_TOS:
532
860
    case MFF_TUN_TTL:
533
926
    case MFF_TUN_GBP_ID:
534
1.12k
    case MFF_TUN_GBP_FLAGS:
535
1.12k
    case MFF_TUN_ERSPAN_IDX:
536
1.12k
    case MFF_TUN_ERSPAN_VER:
537
1.12k
    case MFF_TUN_ERSPAN_DIR:
538
1.12k
    case MFF_TUN_ERSPAN_HWID:
539
1.12k
    case MFF_TUN_GTPU_FLAGS:
540
1.12k
    case MFF_TUN_GTPU_MSGTYPE:
541
580k
    CASE_MFF_TUN_METADATA:
542
580k
    case MFF_METADATA:
543
16.3k
    case MFF_IN_PORT:
544
16.3k
    case MFF_SKB_PRIORITY:
545
16.4k
    case MFF_PKT_MARK:
546
16.4k
    case MFF_CT_ZONE:
547
16.9k
    case MFF_CT_MARK:
548
17.0k
    case MFF_CT_LABEL:
549
17.0k
    case MFF_CT_NW_PROTO:
550
17.0k
    case MFF_CT_NW_SRC:
551
17.0k
    case MFF_CT_NW_DST:
552
17.0k
    case MFF_CT_IPV6_SRC:
553
17.0k
    case MFF_CT_IPV6_DST:
554
17.0k
    case MFF_CT_TP_SRC:
555
17.0k
    case MFF_CT_TP_DST:
556
324k
    CASE_MFF_REGS:
557
324k
    CASE_MFF_XREGS:
558
190k
    CASE_MFF_XXREGS:
559
104k
    case MFF_ETH_SRC:
560
27.8k
    case MFF_ETH_DST:
561
27.8k
    case MFF_ETH_TYPE:
562
29.5k
    case MFF_VLAN_TCI:
563
29.6k
    case MFF_MPLS_TTL:
564
29.8k
    case MFF_IPV4_SRC:
565
30.0k
    case MFF_IPV4_DST:
566
30.4k
    case MFF_IPV6_SRC:
567
30.4k
    case MFF_IPV6_DST:
568
30.4k
    case MFF_IP_PROTO:
569
30.6k
    case MFF_IP_TTL:
570
31.0k
    case MFF_ARP_SPA:
571
31.1k
    case MFF_ARP_TPA:
572
31.3k
    case MFF_ARP_SHA:
573
31.5k
    case MFF_ARP_THA:
574
31.6k
    case MFF_TCP_SRC:
575
32.0k
    case MFF_TCP_DST:
576
32.4k
    case MFF_UDP_SRC:
577
33.5k
    case MFF_UDP_DST:
578
33.7k
    case MFF_SCTP_SRC:
579
34.0k
    case MFF_SCTP_DST:
580
34.3k
    case MFF_ICMPV4_TYPE:
581
34.6k
    case MFF_ICMPV4_CODE:
582
34.9k
    case MFF_ICMPV6_TYPE:
583
35.2k
    case MFF_ICMPV6_CODE:
584
35.2k
    case MFF_ND_TARGET:
585
35.5k
    case MFF_ND_SLL:
586
35.9k
    case MFF_ND_TLL:
587
35.9k
    case MFF_ND_RESERVED:
588
35.9k
    case MFF_ND_OPTIONS_TYPE:
589
35.9k
        return true;
590
591
0
    case MFF_IN_PORT_OXM:
592
0
    case MFF_ACTSET_OUTPUT: {
593
0
        ofp_port_t port;
594
0
        return !ofputil_port_from_ofp11(value->be32, &port);
595
0
    }
596
597
215
    case MFF_IP_DSCP:
598
215
        return !(value->u8 & ~IP_DSCP_MASK);
599
375
    case MFF_IP_DSCP_SHIFTED:
600
375
        return !(value->u8 & (~IP_DSCP_MASK >> 2));
601
218
    case MFF_IP_ECN:
602
218
        return !(value->u8 & ~IP_ECN_MASK);
603
0
    case MFF_IP_FRAG:
604
0
        return !(value->u8 & ~FLOW_NW_FRAG_MASK);
605
0
    case MFF_TCP_FLAGS:
606
0
        return !(value->be16 & ~htons(0x0fff));
607
608
67
    case MFF_ARP_OP:
609
67
        return !(value->be16 & htons(0xff00));
610
611
99
    case MFF_DL_VLAN:
612
99
        return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
613
398
    case MFF_VLAN_VID:
614
398
        return !(value->be16 & htons(VLAN_PCP_MASK));
615
616
195
    case MFF_DL_VLAN_PCP:
617
746
    case MFF_VLAN_PCP:
618
746
        return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
619
620
203
    case MFF_IPV6_LABEL:
621
203
        return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
622
623
66
    case MFF_MPLS_LABEL:
624
66
        return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
625
626
326
    case MFF_MPLS_TC:
627
326
        return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
628
629
0
    case MFF_MPLS_BOS:
630
0
        return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
631
632
723
    case MFF_TUN_FLAGS:
633
723
        return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
634
635
0
    case MFF_CT_STATE:
636
0
        return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
637
638
35
    case MFF_NSH_FLAGS:
639
35
        return true;
640
68
    case MFF_NSH_TTL:
641
68
        return (value->u8 <= 63);
642
0
    case MFF_NSH_MDTYPE:
643
0
        return (value->u8 == 1 || value->u8 == 2);
644
0
    case MFF_NSH_NP:
645
0
        return true;
646
37
    case MFF_NSH_SPI:
647
37
        return !(value->be32 & htonl(0xFF000000));
648
69
    case MFF_NSH_SI:
649
267
    case MFF_NSH_C1:
650
504
    case MFF_NSH_C2:
651
914
    case MFF_NSH_C3:
652
1.16k
    case MFF_NSH_C4:
653
1.16k
        return true;
654
655
0
    case MFF_N_IDS:
656
0
    default:
657
0
        OVS_NOT_REACHED();
658
40.6k
    }
659
40.6k
}
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
182k
{
667
182k
    switch (mf->id) {
668
982
    case MFF_DP_HASH:
669
982
        value->be32 = htonl(flow->dp_hash);
670
982
        break;
671
604
    case MFF_RECIRC_ID:
672
604
        value->be32 = htonl(flow->recirc_id);
673
604
        break;
674
2.35k
    case MFF_PACKET_TYPE:
675
2.35k
        value->be32 = flow->packet_type;
676
2.35k
        break;
677
1.17k
    case MFF_CONJ_ID:
678
1.17k
        value->be32 = htonl(flow->conj_id);
679
1.17k
        break;
680
1.22k
    case MFF_TUN_ID:
681
1.22k
        value->be64 = flow->tunnel.tun_id;
682
1.22k
        break;
683
910
    case MFF_TUN_SRC:
684
910
        value->be32 = flow->tunnel.ip_src;
685
910
        break;
686
702
    case MFF_TUN_DST:
687
702
        value->be32 = flow->tunnel.ip_dst;
688
702
        break;
689
208
    case MFF_TUN_IPV6_SRC:
690
208
        value->ipv6 = flow->tunnel.ipv6_src;
691
208
        break;
692
574
    case MFF_TUN_IPV6_DST:
693
574
        value->ipv6 = flow->tunnel.ipv6_dst;
694
574
        break;
695
494
    case MFF_TUN_FLAGS:
696
494
        value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
697
494
        break;
698
1.65k
    case MFF_TUN_GBP_ID:
699
1.65k
        value->be16 = flow->tunnel.gbp_id;
700
1.65k
        break;
701
534
    case MFF_TUN_GBP_FLAGS:
702
534
        value->u8 = flow->tunnel.gbp_flags;
703
534
        break;
704
1.43k
    case MFF_TUN_TTL:
705
1.43k
        value->u8 = flow->tunnel.ip_ttl;
706
1.43k
        break;
707
502
    case MFF_TUN_TOS:
708
502
        value->u8 = flow->tunnel.ip_tos;
709
502
        break;
710
754
    case MFF_TUN_ERSPAN_VER:
711
754
        value->u8 = flow->tunnel.erspan_ver;
712
754
        break;
713
516
    case MFF_TUN_ERSPAN_IDX:
714
516
        value->be32 = htonl(flow->tunnel.erspan_idx);
715
516
        break;
716
38
    case MFF_TUN_ERSPAN_DIR:
717
38
        value->u8 = flow->tunnel.erspan_dir;
718
38
        break;
719
0
    case MFF_TUN_ERSPAN_HWID:
720
0
        value->u8 = flow->tunnel.erspan_hwid;
721
0
        break;
722
1.13k
    case MFF_TUN_GTPU_FLAGS:
723
1.13k
        value->u8 = flow->tunnel.gtpu_flags;
724
1.13k
        break;
725
0
    case MFF_TUN_GTPU_MSGTYPE:
726
0
        value->u8 = flow->tunnel.gtpu_msgtype;
727
0
        break;
728
78.2k
    CASE_MFF_TUN_METADATA:
729
78.2k
        tun_metadata_read(&flow->tunnel, mf, value);
730
78.2k
        break;
731
732
946
    case MFF_METADATA:
733
946
        value->be64 = flow->metadata;
734
946
        break;
735
736
1.47k
    case MFF_IN_PORT:
737
1.47k
        value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
738
1.47k
        break;
739
1.44k
    case MFF_IN_PORT_OXM:
740
1.44k
        value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
741
1.44k
        break;
742
1.36k
    case MFF_ACTSET_OUTPUT:
743
1.36k
        value->be32 = ofputil_port_to_ofp11(flow->actset_output);
744
1.36k
        break;
745
746
0
    case MFF_SKB_PRIORITY:
747
0
        value->be32 = htonl(flow->skb_priority);
748
0
        break;
749
750
510
    case MFF_PKT_MARK:
751
510
        value->be32 = htonl(flow->pkt_mark);
752
510
        break;
753
754
542
    case MFF_CT_STATE:
755
542
        value->be32 = htonl(flow->ct_state);
756
542
        break;
757
758
2.58k
    case MFF_CT_ZONE:
759
2.58k
        value->be16 = htons(flow->ct_zone);
760
2.58k
        break;
761
762
666
    case MFF_CT_MARK:
763
666
        value->be32 = htonl(flow->ct_mark);
764
666
        break;
765
766
864
    case MFF_CT_LABEL:
767
864
        value->be128 = hton128(flow->ct_label);
768
864
        break;
769
770
634
    case MFF_CT_NW_PROTO:
771
634
        value->u8 = flow->ct_nw_proto;
772
634
        break;
773
774
1.30k
    case MFF_CT_NW_SRC:
775
1.30k
        value->be32 = flow->ct_nw_src;
776
1.30k
        break;
777
778
962
    case MFF_CT_NW_DST:
779
962
        value->be32 = flow->ct_nw_dst;
780
962
        break;
781
782
984
    case MFF_CT_IPV6_SRC:
783
984
        value->ipv6 = flow->ct_ipv6_src;
784
984
        break;
785
786
350
    case MFF_CT_IPV6_DST:
787
350
        value->ipv6 = flow->ct_ipv6_dst;
788
350
        break;
789
790
1.64k
    case MFF_CT_TP_SRC:
791
1.64k
        value->be16 = flow->ct_tp_src;
792
1.64k
        break;
793
794
638
    case MFF_CT_TP_DST:
795
638
        value->be16 = flow->ct_tp_dst;
796
638
        break;
797
798
13.8k
    CASE_MFF_REGS:
799
13.8k
        value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
800
13.8k
        break;
801
802
6.82k
    CASE_MFF_XREGS:
803
6.82k
        value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
804
6.82k
        break;
805
806
2.57k
    CASE_MFF_XXREGS:
807
2.57k
        value->be128 = hton128(flow_get_xxreg(flow, mf->id - MFF_XXREG0));
808
2.57k
        break;
809
810
860
    case MFF_ETH_SRC:
811
860
        value->mac = flow->dl_src;
812
860
        break;
813
814
1.18k
    case MFF_ETH_DST:
815
1.18k
        value->mac = flow->dl_dst;
816
1.18k
        break;
817
818
560
    case MFF_ETH_TYPE:
819
560
        value->be16 = flow->dl_type;
820
560
        break;
821
822
1.07k
    case MFF_VLAN_TCI:
823
1.07k
        value->be16 = flow->vlans[0].tci;
824
1.07k
        break;
825
826
774
    case MFF_DL_VLAN:
827
774
        value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK);
828
774
        break;
829
706
    case MFF_VLAN_VID:
830
706
        value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI);
831
706
        break;
832
833
532
    case MFF_DL_VLAN_PCP:
834
1.04k
    case MFF_VLAN_PCP:
835
1.04k
        value->u8 = vlan_tci_to_pcp(flow->vlans[0].tci);
836
1.04k
        break;
837
838
516
    case MFF_MPLS_LABEL:
839
516
        value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
840
516
        break;
841
842
532
    case MFF_MPLS_TC:
843
532
        value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
844
532
        break;
845
846
260
    case MFF_MPLS_BOS:
847
260
        value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
848
260
        break;
849
850
1.99k
    case MFF_MPLS_TTL:
851
1.99k
        value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
852
1.99k
        break;
853
854
950
    case MFF_IPV4_SRC:
855
950
        value->be32 = flow->nw_src;
856
950
        break;
857
858
1.01k
    case MFF_IPV4_DST:
859
1.01k
        value->be32 = flow->nw_dst;
860
1.01k
        break;
861
862
1.15k
    case MFF_IPV6_SRC:
863
1.15k
        value->ipv6 = flow->ipv6_src;
864
1.15k
        break;
865
866
880
    case MFF_IPV6_DST:
867
880
        value->ipv6 = flow->ipv6_dst;
868
880
        break;
869
870
1.47k
    case MFF_IPV6_LABEL:
871
1.47k
        value->be32 = flow->ipv6_label;
872
1.47k
        break;
873
874
1.47k
    case MFF_IP_PROTO:
875
1.47k
        value->u8 = flow->nw_proto;
876
1.47k
        break;
877
878
1.33k
    case MFF_IP_DSCP:
879
1.33k
        value->u8 = flow->nw_tos & IP_DSCP_MASK;
880
1.33k
        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
776
    case MFF_IP_TTL:
891
776
        value->u8 = flow->nw_ttl;
892
776
        break;
893
894
570
    case MFF_IP_FRAG:
895
570
        value->u8 = flow->nw_frag;
896
570
        break;
897
898
562
    case MFF_ARP_OP:
899
562
        value->be16 = htons(flow->nw_proto);
900
562
        break;
901
902
654
    case MFF_ARP_SPA:
903
654
        value->be32 = flow->nw_src;
904
654
        break;
905
906
942
    case MFF_ARP_TPA:
907
942
        value->be32 = flow->nw_dst;
908
942
        break;
909
910
910
    case MFF_ARP_SHA:
911
1.81k
    case MFF_ND_SLL:
912
1.81k
        value->mac = flow->arp_sha;
913
1.81k
        break;
914
915
906
    case MFF_ARP_THA:
916
1.94k
    case MFF_ND_TLL:
917
1.94k
        value->mac = flow->arp_tha;
918
1.94k
        break;
919
920
454
    case MFF_TCP_SRC:
921
1.27k
    case MFF_UDP_SRC:
922
1.64k
    case MFF_SCTP_SRC:
923
1.64k
        value->be16 = flow->tp_src;
924
1.64k
        break;
925
926
1.06k
    case MFF_TCP_DST:
927
2.04k
    case MFF_UDP_DST:
928
2.64k
    case MFF_SCTP_DST:
929
2.64k
        value->be16 = flow->tp_dst;
930
2.64k
        break;
931
932
308
    case MFF_TCP_FLAGS:
933
308
    case MFF_ND_OPTIONS_TYPE:
934
308
        value->be16 = flow->tcp_flags;
935
308
        break;
936
937
0
    case MFF_ND_RESERVED:
938
0
        value->be32 = flow->igmp_group_ip4;
939
0
        break;
940
941
874
    case MFF_ICMPV4_TYPE:
942
1.47k
    case MFF_ICMPV6_TYPE:
943
1.47k
        value->u8 = ntohs(flow->tp_src);
944
1.47k
        break;
945
946
828
    case MFF_ICMPV4_CODE:
947
1.73k
    case MFF_ICMPV6_CODE:
948
1.73k
        value->u8 = ntohs(flow->tp_dst);
949
1.73k
        break;
950
951
392
    case MFF_ND_TARGET:
952
392
        value->ipv6 = flow->nd_target;
953
392
        break;
954
955
1.50k
    case MFF_NSH_FLAGS:
956
1.50k
        value->u8 = flow->nsh.flags;
957
1.50k
        break;
958
748
    case MFF_NSH_TTL:
959
748
        value->u8 = flow->nsh.ttl;
960
748
        break;
961
1.37k
    case MFF_NSH_MDTYPE:
962
1.37k
        value->u8 = flow->nsh.mdtype;
963
1.37k
        break;
964
844
    case MFF_NSH_NP:
965
844
        value->u8 = flow->nsh.np;
966
844
        break;
967
2.30k
    case MFF_NSH_SPI:
968
2.30k
        value->be32 = nsh_path_hdr_to_spi(flow->nsh.path_hdr);
969
2.30k
        if (value->be32 == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
970
247
            value->be32 = OVS_BE32_MAX;
971
247
        }
972
2.30k
        break;
973
706
    case MFF_NSH_SI:
974
706
        value->u8 = nsh_path_hdr_to_si(flow->nsh.path_hdr);
975
706
        break;
976
1.38k
    case MFF_NSH_C1:
977
2.56k
    case MFF_NSH_C2:
978
3.74k
    case MFF_NSH_C3:
979
6.19k
    case MFF_NSH_C4:
980
6.19k
        value->be32 = flow->nsh.context[mf->id - MFF_NSH_C1];
981
6.19k
        break;
982
983
0
    case MFF_N_IDS:
984
0
    default:
985
0
        OVS_NOT_REACHED();
986
182k
    }
987
182k
}
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
67.1k
{
1000
67.1k
    if (err_str) {
1001
37.2k
        *err_str = NULL;
1002
37.2k
    }
1003
1004
67.1k
    switch (mf->id) {
1005
285
    case MFF_DP_HASH:
1006
285
        match_set_dp_hash(match, ntohl(value->be32));
1007
285
        break;
1008
96
    case MFF_RECIRC_ID:
1009
96
        match_set_recirc_id(match, ntohl(value->be32));
1010
96
        break;
1011
1.08k
    case MFF_PACKET_TYPE:
1012
1.08k
        match_set_packet_type(match, value->be32);
1013
1.08k
        break;
1014
287
    case MFF_CONJ_ID:
1015
287
        match_set_conj_id(match, ntohl(value->be32));
1016
287
        break;
1017
361
    case MFF_TUN_ID:
1018
361
        match_set_tun_id(match, value->be64);
1019
361
        break;
1020
242
    case MFF_TUN_SRC:
1021
242
        match_set_tun_src(match, value->be32);
1022
242
        break;
1023
263
    case MFF_TUN_DST:
1024
263
        match_set_tun_dst(match, value->be32);
1025
263
        break;
1026
361
    case MFF_TUN_IPV6_SRC:
1027
361
        match_set_tun_ipv6_src(match, &value->ipv6);
1028
361
        break;
1029
286
    case MFF_TUN_IPV6_DST:
1030
286
        match_set_tun_ipv6_dst(match, &value->ipv6);
1031
286
        break;
1032
353
    case MFF_TUN_FLAGS:
1033
353
        match_set_tun_flags(match, ntohs(value->be16));
1034
353
        break;
1035
505
    case MFF_TUN_GBP_ID:
1036
505
         match_set_tun_gbp_id(match, value->be16);
1037
505
         break;
1038
198
    case MFF_TUN_GBP_FLAGS:
1039
198
         match_set_tun_gbp_flags(match, value->u8);
1040
198
         break;
1041
209
    case MFF_TUN_TOS:
1042
209
        match_set_tun_tos(match, value->u8);
1043
209
        break;
1044
278
    case MFF_TUN_TTL:
1045
278
        match_set_tun_ttl(match, value->u8);
1046
278
        break;
1047
0
    case MFF_TUN_ERSPAN_VER:
1048
0
        match_set_tun_erspan_ver(match, value->u8);
1049
0
        break;
1050
0
    case MFF_TUN_ERSPAN_IDX:
1051
0
        match_set_tun_erspan_idx(match, ntohl(value->be32));
1052
0
        break;
1053
0
    case MFF_TUN_ERSPAN_DIR:
1054
0
        match_set_tun_erspan_dir(match, value->u8);
1055
0
        break;
1056
0
    case MFF_TUN_ERSPAN_HWID:
1057
0
        match_set_tun_erspan_hwid(match, value->u8);
1058
0
        break;
1059
436
    case MFF_TUN_GTPU_FLAGS:
1060
436
        match_set_tun_gtpu_flags(match, value->u8);
1061
436
        break;
1062
0
    case MFF_TUN_GTPU_MSGTYPE:
1063
0
        match_set_tun_gtpu_msgtype(match, value->u8);
1064
0
        break;
1065
23.7k
    CASE_MFF_TUN_METADATA:
1066
23.7k
        tun_metadata_set_match(mf, value, NULL, match, err_str);
1067
23.7k
        break;
1068
1069
308
    case MFF_METADATA:
1070
308
        match_set_metadata(match, value->be64);
1071
308
        break;
1072
1073
622
    case MFF_IN_PORT:
1074
622
        match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1075
622
        break;
1076
1077
657
    case MFF_IN_PORT_OXM: {
1078
657
        ofp_port_t port;
1079
657
        ofputil_port_from_ofp11(value->be32, &port);
1080
657
        match_set_in_port(match, port);
1081
657
        break;
1082
752k
    }
1083
634
    case MFF_ACTSET_OUTPUT: {
1084
634
        ofp_port_t port;
1085
634
        ofputil_port_from_ofp11(value->be32, &port);
1086
634
        match_set_actset_output(match, port);
1087
634
        break;
1088
752k
    }
1089
1090
0
    case MFF_SKB_PRIORITY:
1091
0
        match_set_skb_priority(match, ntohl(value->be32));
1092
0
        break;
1093
1094
402
    case MFF_PKT_MARK:
1095
402
        match_set_pkt_mark(match, ntohl(value->be32));
1096
402
        break;
1097
1098
799
    case MFF_CT_STATE:
1099
799
        match_set_ct_state(match, ntohl(value->be32));
1100
799
        break;
1101
1102
849
    case MFF_CT_ZONE:
1103
849
        match_set_ct_zone(match, ntohs(value->be16));
1104
849
        break;
1105
1106
237
    case MFF_CT_MARK:
1107
237
        match_set_ct_mark(match, ntohl(value->be32));
1108
237
        break;
1109
1110
329
    case MFF_CT_LABEL:
1111
329
        match_set_ct_label(match, ntoh128(value->be128));
1112
329
        break;
1113
1114
270
    case MFF_CT_NW_PROTO:
1115
270
        match_set_ct_nw_proto(match, value->u8);
1116
270
        break;
1117
1118
258
    case MFF_CT_NW_SRC:
1119
258
        match_set_ct_nw_src(match, value->be32);
1120
258
        break;
1121
1122
194
    case MFF_CT_NW_DST:
1123
194
        match_set_ct_nw_dst(match, value->be32);
1124
194
        break;
1125
1126
273
    case MFF_CT_IPV6_SRC:
1127
273
        match_set_ct_ipv6_src(match, &value->ipv6);
1128
273
        break;
1129
1130
145
    case MFF_CT_IPV6_DST:
1131
145
        match_set_ct_ipv6_dst(match, &value->ipv6);
1132
145
        break;
1133
1134
284
    case MFF_CT_TP_SRC:
1135
284
        match_set_ct_tp_src(match, value->be16);
1136
284
        break;
1137
1138
274
    case MFF_CT_TP_DST:
1139
274
        match_set_ct_tp_dst(match, value->be16);
1140
274
        break;
1141
1142
7.35k
    CASE_MFF_REGS:
1143
7.35k
        match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1144
7.35k
        break;
1145
1146
3.56k
    CASE_MFF_XREGS:
1147
3.56k
        match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
1148
3.56k
        break;
1149
1150
1.55k
    CASE_MFF_XXREGS:
1151
1.55k
        match_set_xxreg(match, mf->id - MFF_XXREG0, ntoh128(value->be128));
1152
1.55k
        break;
1153
1154
255
    case MFF_ETH_SRC:
1155
255
        match_set_dl_src(match, value->mac);
1156
255
        break;
1157
1158
266
    case MFF_ETH_DST:
1159
266
        match_set_dl_dst(match, value->mac);
1160
266
        break;
1161
1162
475
    case MFF_ETH_TYPE:
1163
475
        match_set_dl_type(match, value->be16);
1164
475
        break;
1165
1166
240
    case MFF_VLAN_TCI:
1167
240
        match_set_dl_tci(match, value->be16);
1168
240
        break;
1169
1170
640
    case MFF_DL_VLAN:
1171
640
        match_set_dl_vlan(match, value->be16, 0);
1172
640
        break;
1173
273
    case MFF_VLAN_VID:
1174
273
        match_set_vlan_vid(match, value->be16);
1175
273
        break;
1176
1177
201
    case MFF_DL_VLAN_PCP:
1178
628
    case MFF_VLAN_PCP:
1179
628
        match_set_dl_vlan_pcp(match, value->u8, 0);
1180
628
        break;
1181
1182
262
    case MFF_MPLS_LABEL:
1183
262
        match_set_mpls_label(match, 0, value->be32);
1184
262
        break;
1185
1186
410
    case MFF_MPLS_TC:
1187
410
        match_set_mpls_tc(match, 0, value->u8);
1188
410
        break;
1189
1190
298
    case MFF_MPLS_BOS:
1191
298
        match_set_mpls_bos(match, 0, value->u8);
1192
298
        break;
1193
1194
1.14k
    case MFF_MPLS_TTL:
1195
1.14k
        match_set_mpls_ttl(match, 0, value->u8);
1196
1.14k
        break;
1197
1198
514
    case MFF_IPV4_SRC:
1199
514
        match_set_nw_src(match, value->be32);
1200
514
        break;
1201
1202
481
    case MFF_IPV4_DST:
1203
481
        match_set_nw_dst(match, value->be32);
1204
481
        break;
1205
1206
419
    case MFF_IPV6_SRC:
1207
419
        match_set_ipv6_src(match, &value->ipv6);
1208
419
        break;
1209
1210
389
    case MFF_IPV6_DST:
1211
389
        match_set_ipv6_dst(match, &value->ipv6);
1212
389
        break;
1213
1214
477
    case MFF_IPV6_LABEL:
1215
477
        match_set_ipv6_label(match, value->be32);
1216
477
        break;
1217
1218
485
    case MFF_IP_PROTO:
1219
485
        match_set_nw_proto(match, value->u8);
1220
485
        break;
1221
1222
326
    case MFF_IP_DSCP:
1223
326
        match_set_nw_dscp(match, value->u8);
1224
326
        break;
1225
1226
699
    case MFF_IP_DSCP_SHIFTED:
1227
699
        match_set_nw_dscp(match, value->u8 << 2);
1228
699
        break;
1229
1230
75
    case MFF_IP_ECN:
1231
75
        match_set_nw_ecn(match, value->u8);
1232
75
        break;
1233
1234
348
    case MFF_IP_TTL:
1235
348
        match_set_nw_ttl(match, value->u8);
1236
348
        break;
1237
1238
305
    case MFF_IP_FRAG:
1239
305
        match_set_nw_frag(match, value->u8);
1240
305
        break;
1241
1242
319
    case MFF_ARP_OP:
1243
319
        match_set_nw_proto(match, ntohs(value->be16));
1244
319
        break;
1245
1246
260
    case MFF_ARP_SPA:
1247
260
        match_set_nw_src(match, value->be32);
1248
260
        break;
1249
1250
194
    case MFF_ARP_TPA:
1251
194
        match_set_nw_dst(match, value->be32);
1252
194
        break;
1253
1254
260
    case MFF_ARP_SHA:
1255
454
    case MFF_ND_SLL:
1256
454
        match_set_arp_sha(match, value->mac);
1257
454
        break;
1258
1259
258
    case MFF_ARP_THA:
1260
516
    case MFF_ND_TLL:
1261
516
        match_set_arp_tha(match, value->mac);
1262
516
        break;
1263
1264
371
    case MFF_TCP_SRC:
1265
755
    case MFF_UDP_SRC:
1266
864
    case MFF_SCTP_SRC:
1267
864
        match_set_tp_src(match, value->be16);
1268
864
        break;
1269
1270
529
    case MFF_TCP_DST:
1271
888
    case MFF_UDP_DST:
1272
1.17k
    case MFF_SCTP_DST:
1273
1.17k
        match_set_tp_dst(match, value->be16);
1274
1.17k
        break;
1275
1276
519
    case MFF_TCP_FLAGS:
1277
519
        match_set_tcp_flags(match, value->be16);
1278
519
        break;
1279
1280
346
    case MFF_ICMPV4_TYPE:
1281
555
    case MFF_ICMPV6_TYPE:
1282
555
        match_set_icmp_type(match, value->u8);
1283
555
        break;
1284
1285
291
    case MFF_ICMPV4_CODE:
1286
548
    case MFF_ICMPV6_CODE:
1287
548
        match_set_icmp_code(match, value->u8);
1288
548
        break;
1289
1290
121
    case MFF_ND_TARGET:
1291
121
        match_set_nd_target(match, &value->ipv6);
1292
121
        break;
1293
1294
0
    case MFF_ND_RESERVED:
1295
0
        match_set_nd_reserved(match, value->be32);
1296
0
        break;
1297
1298
0
    case MFF_ND_OPTIONS_TYPE:
1299
0
        match_set_nd_options_type(match, value->u8);
1300
0
        break;
1301
1302
360
    case MFF_NSH_FLAGS:
1303
360
        MATCH_SET_FIELD_UINT8(match, nsh.flags, value->u8);
1304
360
        break;
1305
467
    case MFF_NSH_TTL:
1306
467
        MATCH_SET_FIELD_UINT8(match, nsh.ttl, value->u8);
1307
467
        break;
1308
377
    case MFF_NSH_MDTYPE:
1309
377
        MATCH_SET_FIELD_UINT8(match, nsh.mdtype, value->u8);
1310
377
        break;
1311
262
    case MFF_NSH_NP:
1312
262
        MATCH_SET_FIELD_UINT8(match, nsh.np, value->u8);
1313
262
        break;
1314
598
    case MFF_NSH_SPI:
1315
598
        match->wc.masks.nsh.path_hdr |= htonl(NSH_SPI_MASK);
1316
598
        nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr, value->be32);
1317
598
        break;
1318
347
    case MFF_NSH_SI:
1319
347
        match->wc.masks.nsh.path_hdr |= htonl(NSH_SI_MASK);
1320
347
        nsh_path_hdr_set_si(&match->flow.nsh.path_hdr, value->u8);
1321
347
        break;
1322
529
    case MFF_NSH_C1:
1323
1.15k
    case MFF_NSH_C2:
1324
1.55k
    case MFF_NSH_C3:
1325
1.81k
    case MFF_NSH_C4:
1326
1.81k
        MATCH_SET_FIELD_BE32(match, nsh.context[mf->id - MFF_NSH_C1],
1327
1.81k
                             value->be32);
1328
1.81k
        break;
1329
1330
0
    case MFF_N_IDS:
1331
0
    default:
1332
0
        OVS_NOT_REACHED();
1333
67.1k
    }
1334
67.1k
}
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
75.9k
{
1372
75.9k
    const uint8_t *value = &value_->u8;
1373
75.9k
    int i;
1374
1375
75.9k
    if (!mf->variable_len) {
1376
5.25k
        return mf->n_bytes;
1377
5.25k
    }
1378
1379
70.6k
    if (!value) {
1380
0
        return 0;
1381
0
    }
1382
1383
8.69M
    for (i = 0; i < mf->n_bytes; i++) {
1384
8.66M
        if (value[i] != 0) {
1385
36.1k
            break;
1386
36.1k
        }
1387
8.66M
    }
1388
1389
70.6k
    return mf->n_bytes - i;
1390
70.6k
}
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
52.0k
{
1404
52.0k
    int len, mask_len;
1405
52.0k
    bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1406
1407
52.0k
    len = field_len(mf, value);
1408
52.0k
    if (is_masked) {
1409
23.8k
        mask_len = field_len(mf, mask);
1410
23.8k
        len = MAX(len, mask_len);
1411
23.8k
    }
1412
1413
52.0k
    if (is_masked_) {
1414
52.0k
        *is_masked_ = is_masked;
1415
52.0k
    }
1416
1417
52.0k
    return len;
1418
52.0k
}
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_tun_metadata(const struct mf_field *mf)
1788
33.5k
{
1789
33.5k
    return mf->id >= MFF_TUN_METADATA0 &&
1790
31.0k
           mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1791
33.5k
}
1792
1793
bool
1794
mf_is_any_metadata(const struct mf_field *mf)
1795
0
{
1796
0
    switch (mf->id) {
1797
0
    case MFF_DP_HASH:
1798
0
    case MFF_RECIRC_ID:
1799
0
    case MFF_PACKET_TYPE:
1800
0
    case MFF_CONJ_ID:
1801
0
    case MFF_TUN_ERSPAN_DIR:
1802
0
    CASE_MFF_TUN_METADATA:
1803
0
    case MFF_METADATA:
1804
0
    case MFF_IN_PORT:
1805
0
    case MFF_IN_PORT_OXM:
1806
0
    case MFF_ACTSET_OUTPUT:
1807
0
    case MFF_SKB_PRIORITY:
1808
0
    case MFF_PKT_MARK:
1809
0
    case MFF_CT_STATE:
1810
0
    case MFF_CT_ZONE:
1811
0
    case MFF_CT_MARK:
1812
0
    case MFF_CT_LABEL:
1813
0
    case MFF_CT_NW_PROTO:
1814
0
    case MFF_CT_NW_SRC:
1815
0
    case MFF_CT_NW_DST:
1816
0
    case MFF_CT_IPV6_SRC:
1817
0
    case MFF_CT_IPV6_DST:
1818
0
    case MFF_CT_TP_SRC:
1819
0
    case MFF_CT_TP_DST:
1820
0
    CASE_MFF_REGS:
1821
0
    CASE_MFF_XREGS:
1822
0
    CASE_MFF_XXREGS:
1823
0
        return true;
1824
1825
0
    case MFF_TUN_ID:
1826
0
    case MFF_TUN_SRC:
1827
0
    case MFF_TUN_DST:
1828
0
    case MFF_TUN_IPV6_SRC:
1829
0
    case MFF_TUN_IPV6_DST:
1830
0
    case MFF_TUN_FLAGS:
1831
0
    case MFF_TUN_TTL:
1832
0
    case MFF_TUN_TOS:
1833
0
    case MFF_TUN_GBP_ID:
1834
0
    case MFF_TUN_GBP_FLAGS:
1835
0
    case MFF_TUN_ERSPAN_IDX:
1836
0
    case MFF_TUN_ERSPAN_VER:
1837
0
    case MFF_TUN_ERSPAN_HWID:
1838
0
    case MFF_TUN_GTPU_FLAGS:
1839
0
    case MFF_TUN_GTPU_MSGTYPE:
1840
0
    case MFF_ETH_SRC:
1841
0
    case MFF_ETH_DST:
1842
0
    case MFF_ETH_TYPE:
1843
0
    case MFF_VLAN_TCI:
1844
0
    case MFF_DL_VLAN:
1845
0
    case MFF_VLAN_VID:
1846
0
    case MFF_DL_VLAN_PCP:
1847
0
    case MFF_VLAN_PCP:
1848
0
    case MFF_MPLS_LABEL:
1849
0
    case MFF_MPLS_TC:
1850
0
    case MFF_MPLS_BOS:
1851
0
    case MFF_MPLS_TTL:
1852
0
    case MFF_IPV4_SRC:
1853
0
    case MFF_IPV4_DST:
1854
0
    case MFF_IPV6_SRC:
1855
0
    case MFF_IPV6_DST:
1856
0
    case MFF_IPV6_LABEL:
1857
0
    case MFF_IP_PROTO:
1858
0
    case MFF_IP_DSCP:
1859
0
    case MFF_IP_DSCP_SHIFTED:
1860
0
    case MFF_IP_ECN:
1861
0
    case MFF_IP_TTL:
1862
0
    case MFF_IP_FRAG:
1863
0
    case MFF_ARP_OP:
1864
0
    case MFF_ARP_SPA:
1865
0
    case MFF_ARP_TPA:
1866
0
    case MFF_ARP_SHA:
1867
0
    case MFF_ARP_THA:
1868
0
    case MFF_TCP_SRC:
1869
0
    case MFF_TCP_DST:
1870
0
    case MFF_TCP_FLAGS:
1871
0
    case MFF_UDP_SRC:
1872
0
    case MFF_UDP_DST:
1873
0
    case MFF_SCTP_SRC:
1874
0
    case MFF_SCTP_DST:
1875
0
    case MFF_ICMPV4_TYPE:
1876
0
    case MFF_ICMPV4_CODE:
1877
0
    case MFF_ICMPV6_TYPE:
1878
0
    case MFF_ICMPV6_CODE:
1879
0
    case MFF_ND_TARGET:
1880
0
    case MFF_ND_SLL:
1881
0
    case MFF_ND_TLL:
1882
0
    case MFF_ND_RESERVED:
1883
0
    case MFF_ND_OPTIONS_TYPE:
1884
0
    case MFF_NSH_FLAGS:
1885
0
    case MFF_NSH_MDTYPE:
1886
0
    case MFF_NSH_NP:
1887
0
    case MFF_NSH_SPI:
1888
0
    case MFF_NSH_SI:
1889
0
    case MFF_NSH_C1:
1890
0
    case MFF_NSH_C2:
1891
0
    case MFF_NSH_C3:
1892
0
    case MFF_NSH_C4:
1893
0
    case MFF_NSH_TTL:
1894
0
        return false;
1895
1896
0
    case MFF_N_IDS:
1897
0
    default:
1898
0
        OVS_NOT_REACHED();
1899
0
    }
1900
0
}
1901
1902
bool
1903
mf_is_frozen_metadata(const struct mf_field *mf)
1904
0
{
1905
0
    if (mf->id >= MFF_TUN_ID && mf->id <= MFF_IN_PORT_OXM) {
1906
0
        return true;
1907
0
    }
1908
1909
0
    if (mf->id >= MFF_REG0 && mf->id < MFF_ETH_SRC) {
1910
0
        return true;
1911
0
    }
1912
0
    return false;
1913
0
}
1914
1915
bool
1916
mf_is_pipeline_field(const struct mf_field *mf)
1917
0
{
1918
0
    switch (mf->id) {
1919
0
    case MFF_TUN_ID:
1920
0
    case MFF_TUN_SRC:
1921
0
    case MFF_TUN_DST:
1922
0
    case MFF_TUN_IPV6_SRC:
1923
0
    case MFF_TUN_IPV6_DST:
1924
0
    case MFF_TUN_FLAGS:
1925
0
    case MFF_TUN_GBP_ID:
1926
0
    case MFF_TUN_GBP_FLAGS:
1927
0
    case MFF_TUN_ERSPAN_VER:
1928
0
    case MFF_TUN_ERSPAN_IDX:
1929
0
    case MFF_TUN_ERSPAN_DIR:
1930
0
    case MFF_TUN_ERSPAN_HWID:
1931
0
    case MFF_TUN_GTPU_FLAGS:
1932
0
    case MFF_TUN_GTPU_MSGTYPE:
1933
0
    CASE_MFF_TUN_METADATA:
1934
0
    case MFF_METADATA:
1935
0
    case MFF_IN_PORT:
1936
0
    case MFF_IN_PORT_OXM:
1937
0
    CASE_MFF_REGS:
1938
0
    CASE_MFF_XREGS:
1939
0
    CASE_MFF_XXREGS:
1940
0
    case MFF_PACKET_TYPE:
1941
0
        return true;
1942
1943
0
    case MFF_DP_HASH:
1944
0
    case MFF_RECIRC_ID:
1945
0
    case MFF_CONJ_ID:
1946
0
    case MFF_TUN_TTL:
1947
0
    case MFF_TUN_TOS:
1948
0
    case MFF_ACTSET_OUTPUT:
1949
0
    case MFF_SKB_PRIORITY:
1950
0
    case MFF_PKT_MARK:
1951
0
    case MFF_CT_STATE:
1952
0
    case MFF_CT_ZONE:
1953
0
    case MFF_CT_MARK:
1954
0
    case MFF_CT_LABEL:
1955
0
    case MFF_CT_NW_PROTO:
1956
0
    case MFF_CT_NW_SRC:
1957
0
    case MFF_CT_NW_DST:
1958
0
    case MFF_CT_IPV6_SRC:
1959
0
    case MFF_CT_IPV6_DST:
1960
0
    case MFF_CT_TP_SRC:
1961
0
    case MFF_CT_TP_DST:
1962
0
    case MFF_ETH_SRC:
1963
0
    case MFF_ETH_DST:
1964
0
    case MFF_ETH_TYPE:
1965
0
    case MFF_VLAN_TCI:
1966
0
    case MFF_DL_VLAN:
1967
0
    case MFF_VLAN_VID:
1968
0
    case MFF_DL_VLAN_PCP:
1969
0
    case MFF_VLAN_PCP:
1970
0
    case MFF_MPLS_LABEL:
1971
0
    case MFF_MPLS_TC:
1972
0
    case MFF_MPLS_BOS:
1973
0
    case MFF_MPLS_TTL:
1974
0
    case MFF_IPV4_SRC:
1975
0
    case MFF_IPV4_DST:
1976
0
    case MFF_IPV6_SRC:
1977
0
    case MFF_IPV6_DST:
1978
0
    case MFF_IPV6_LABEL:
1979
0
    case MFF_IP_PROTO:
1980
0
    case MFF_IP_DSCP:
1981
0
    case MFF_IP_DSCP_SHIFTED:
1982
0
    case MFF_IP_ECN:
1983
0
    case MFF_IP_TTL:
1984
0
    case MFF_IP_FRAG:
1985
0
    case MFF_ARP_OP:
1986
0
    case MFF_ARP_SPA:
1987
0
    case MFF_ARP_TPA:
1988
0
    case MFF_ARP_SHA:
1989
0
    case MFF_ARP_THA:
1990
0
    case MFF_TCP_SRC:
1991
0
    case MFF_TCP_DST:
1992
0
    case MFF_TCP_FLAGS:
1993
0
    case MFF_UDP_SRC:
1994
0
    case MFF_UDP_DST:
1995
0
    case MFF_SCTP_SRC:
1996
0
    case MFF_SCTP_DST:
1997
0
    case MFF_ICMPV4_TYPE:
1998
0
    case MFF_ICMPV4_CODE:
1999
0
    case MFF_ICMPV6_TYPE:
2000
0
    case MFF_ICMPV6_CODE:
2001
0
    case MFF_ND_TARGET:
2002
0
    case MFF_ND_SLL:
2003
0
    case MFF_ND_TLL:
2004
0
    case MFF_ND_RESERVED:
2005
0
    case MFF_ND_OPTIONS_TYPE:
2006
0
    case MFF_NSH_FLAGS:
2007
0
    case MFF_NSH_TTL:
2008
0
    case MFF_NSH_MDTYPE:
2009
0
    case MFF_NSH_NP:
2010
0
    case MFF_NSH_SPI:
2011
0
    case MFF_NSH_SI:
2012
0
    case MFF_NSH_C1:
2013
0
    case MFF_NSH_C2:
2014
0
    case MFF_NSH_C3:
2015
0
    case MFF_NSH_C4:
2016
0
        return false;
2017
2018
0
    case MFF_N_IDS:
2019
0
    default:
2020
0
        OVS_NOT_REACHED();
2021
0
    }
2022
0
}
2023
2024
/* Returns true if 'mf' has previously been set in 'flow', false if
2025
 * it contains a non-default value.
2026
 *
2027
 * The caller is responsible for ensuring that 'flow' meets 'mf''s
2028
 * prerequisites. */
2029
bool
2030
mf_is_set(const struct mf_field *mf, const struct flow *flow)
2031
0
{
2032
0
    if (!mf_is_tun_metadata(mf)) {
2033
0
        union mf_value value;
2034
2035
0
        mf_get_value(mf, flow, &value);
2036
0
        return !is_all_zeros(&value, mf->n_bytes);
2037
0
    } else {
2038
0
        return ULLONG_GET(flow->tunnel.metadata.present.map,
2039
0
                          mf->id - MFF_TUN_METADATA0);
2040
0
    }
2041
0
}
2042
2043
/* Makes 'match' wildcard field 'mf'.
2044
 *
2045
 * The caller is responsible for ensuring that 'match' meets 'mf''s
2046
 * prerequisites.
2047
 *
2048
 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2049
 * with the request or NULL if there is no error. The caller is reponsible
2050
 * for freeing the string. */
2051
void
2052
mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
2053
33.2k
{
2054
33.2k
    if (err_str) {
2055
33.2k
        *err_str = NULL;
2056
33.2k
    }
2057
2058
33.2k
    switch (mf->id) {
2059
68
    case MFF_DP_HASH:
2060
68
        match->flow.dp_hash = 0;
2061
68
        match->wc.masks.dp_hash = 0;
2062
68
        break;
2063
66
    case MFF_RECIRC_ID:
2064
66
        match->flow.recirc_id = 0;
2065
66
        match->wc.masks.recirc_id = 0;
2066
66
        break;
2067
312
    case MFF_PACKET_TYPE:
2068
312
        match->flow.packet_type = 0;
2069
312
        match->wc.masks.packet_type = 0;
2070
312
        break;
2071
109
    case MFF_CONJ_ID:
2072
109
        match->flow.conj_id = 0;
2073
109
        match->wc.masks.conj_id = 0;
2074
109
        break;
2075
152
    case MFF_TUN_ID:
2076
152
        match_set_tun_id_masked(match, htonll(0), htonll(0));
2077
152
        break;
2078
66
    case MFF_TUN_SRC:
2079
66
        match_set_tun_src_masked(match, htonl(0), htonl(0));
2080
66
        break;
2081
66
    case MFF_TUN_DST:
2082
66
        match_set_tun_dst_masked(match, htonl(0), htonl(0));
2083
66
        break;
2084
69
    case MFF_TUN_IPV6_SRC:
2085
69
        memset(&match->wc.masks.tunnel.ipv6_src, 0,
2086
69
               sizeof match->wc.masks.tunnel.ipv6_src);
2087
69
        memset(&match->flow.tunnel.ipv6_src, 0,
2088
69
               sizeof match->flow.tunnel.ipv6_src);
2089
69
        break;
2090
66
    case MFF_TUN_IPV6_DST:
2091
66
        memset(&match->wc.masks.tunnel.ipv6_dst, 0,
2092
66
               sizeof match->wc.masks.tunnel.ipv6_dst);
2093
66
        memset(&match->flow.tunnel.ipv6_dst, 0,
2094
66
               sizeof match->flow.tunnel.ipv6_dst);
2095
66
        break;
2096
626
    case MFF_TUN_FLAGS:
2097
626
        match_set_tun_flags_masked(match, 0, 0);
2098
626
        break;
2099
23
    case MFF_TUN_GBP_ID:
2100
23
        match_set_tun_gbp_id_masked(match, 0, 0);
2101
23
        break;
2102
66
    case MFF_TUN_GBP_FLAGS:
2103
66
        match_set_tun_gbp_flags_masked(match, 0, 0);
2104
66
        break;
2105
196
    case MFF_TUN_TOS:
2106
196
        match_set_tun_tos_masked(match, 0, 0);
2107
196
        break;
2108
329
    case MFF_TUN_TTL:
2109
329
        match_set_tun_ttl_masked(match, 0, 0);
2110
329
        break;
2111
0
    case MFF_TUN_ERSPAN_VER:
2112
0
        match_set_tun_erspan_ver_masked(match, 0, 0);
2113
0
        break;
2114
0
    case MFF_TUN_ERSPAN_IDX:
2115
0
        match_set_tun_erspan_idx_masked(match, 0, 0);
2116
0
        break;
2117
0
    case MFF_TUN_ERSPAN_DIR:
2118
0
        match_set_tun_erspan_dir_masked(match, 0, 0);
2119
0
        break;
2120
0
    case MFF_TUN_ERSPAN_HWID:
2121
0
        match_set_tun_erspan_hwid_masked(match, 0, 0);
2122
0
        break;
2123
268
    case MFF_TUN_GTPU_FLAGS:
2124
268
        match_set_tun_gtpu_flags_masked(match, 0, 0);
2125
268
        break;
2126
0
    case MFF_TUN_GTPU_MSGTYPE:
2127
0
        match_set_tun_gtpu_msgtype_masked(match, 0, 0);
2128
0
        break;
2129
0
    CASE_MFF_TUN_METADATA:
2130
0
        tun_metadata_set_match(mf, NULL, NULL, match, err_str);
2131
0
        break;
2132
2133
311
    case MFF_METADATA:
2134
311
        match_set_metadata_masked(match, htonll(0), htonll(0));
2135
311
        break;
2136
2137
194
    case MFF_IN_PORT:
2138
194
    case MFF_IN_PORT_OXM:
2139
194
        match->flow.in_port.ofp_port = 0;
2140
194
        match->wc.masks.in_port.ofp_port = 0;
2141
194
        break;
2142
34
    case MFF_ACTSET_OUTPUT:
2143
34
        match->flow.actset_output = 0;
2144
34
        match->wc.masks.actset_output = 0;
2145
34
        break;
2146
2147
0
    case MFF_SKB_PRIORITY:
2148
0
        match->flow.skb_priority = 0;
2149
0
        match->wc.masks.skb_priority = 0;
2150
0
        break;
2151
2152
335
    case MFF_PKT_MARK:
2153
335
        match->flow.pkt_mark = 0;
2154
335
        match->wc.masks.pkt_mark = 0;
2155
335
        break;
2156
2157
440
    case MFF_CT_STATE:
2158
440
        match->flow.ct_state = 0;
2159
440
        match->wc.masks.ct_state = 0;
2160
440
        break;
2161
2162
346
    case MFF_CT_ZONE:
2163
346
        match->flow.ct_zone = 0;
2164
346
        match->wc.masks.ct_zone = 0;
2165
346
        break;
2166
2167
82
    case MFF_CT_MARK:
2168
82
        match->flow.ct_mark = 0;
2169
82
        match->wc.masks.ct_mark = 0;
2170
82
        break;
2171
2172
194
    case MFF_CT_LABEL:
2173
194
        memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
2174
194
        memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
2175
194
        break;
2176
2177
194
    case MFF_CT_NW_PROTO:
2178
194
        match->flow.ct_nw_proto = 0;
2179
194
        match->wc.masks.ct_nw_proto = 0;
2180
194
        break;
2181
2182
68
    case MFF_CT_NW_SRC:
2183
68
        match->flow.ct_nw_src = 0;
2184
68
        match->wc.masks.ct_nw_src = 0;
2185
68
        break;
2186
2187
1.35k
    case MFF_CT_NW_DST:
2188
1.35k
        match->flow.ct_nw_dst = 0;
2189
1.35k
        match->wc.masks.ct_nw_dst = 0;
2190
1.35k
        break;
2191
2192
221
    case MFF_CT_IPV6_SRC:
2193
221
        memset(&match->flow.ct_ipv6_src, 0, sizeof(match->flow.ct_ipv6_src));
2194
221
        WC_UNMASK_FIELD(&match->wc, ct_ipv6_src);
2195
221
        break;
2196
2197
521
    case MFF_CT_IPV6_DST:
2198
521
        memset(&match->flow.ct_ipv6_dst, 0, sizeof(match->flow.ct_ipv6_dst));
2199
521
        WC_UNMASK_FIELD(&match->wc, ct_ipv6_dst);
2200
521
        break;
2201
2202
404
    case MFF_CT_TP_SRC:
2203
404
        match->flow.ct_tp_src = 0;
2204
404
        match->wc.masks.ct_tp_src = 0;
2205
404
        break;
2206
2207
76
    case MFF_CT_TP_DST:
2208
76
        match->flow.ct_tp_dst = 0;
2209
76
        match->wc.masks.ct_tp_dst = 0;
2210
76
        break;
2211
2212
6.49k
    CASE_MFF_REGS:
2213
6.49k
        match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
2214
6.49k
        break;
2215
2216
3.44k
    CASE_MFF_XREGS:
2217
3.44k
        match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
2218
3.44k
        break;
2219
2220
1.60k
    CASE_MFF_XXREGS: {
2221
1.60k
        match_set_xxreg_masked(match, mf->id - MFF_XXREG0, OVS_U128_ZERO,
2222
1.60k
                               OVS_U128_ZERO);
2223
1.60k
        break;
2224
2.15k
    }
2225
2226
389
    case MFF_ETH_SRC:
2227
389
        match->flow.dl_src = eth_addr_zero;
2228
389
        match->wc.masks.dl_src = eth_addr_zero;
2229
389
        break;
2230
2231
194
    case MFF_ETH_DST:
2232
194
        match->flow.dl_dst = eth_addr_zero;
2233
194
        match->wc.masks.dl_dst = eth_addr_zero;
2234
194
        break;
2235
2236
226
    case MFF_ETH_TYPE:
2237
226
        match->flow.dl_type = htons(0);
2238
226
        match->wc.masks.dl_type = htons(0);
2239
226
        break;
2240
2241
350
    case MFF_VLAN_TCI:
2242
350
        match_set_dl_tci_masked(match, htons(0), htons(0));
2243
350
        break;
2244
2245
892
    case MFF_DL_VLAN:
2246
1.13k
    case MFF_VLAN_VID:
2247
1.13k
        match_set_any_vid(match);
2248
1.13k
        break;
2249
2250
599
    case MFF_DL_VLAN_PCP:
2251
1.01k
    case MFF_VLAN_PCP:
2252
1.01k
        match_set_any_pcp(match);
2253
1.01k
        break;
2254
2255
781
    case MFF_MPLS_LABEL:
2256
781
        match_set_any_mpls_label(match, 0);
2257
781
        break;
2258
2259
459
    case MFF_MPLS_TC:
2260
459
        match_set_any_mpls_tc(match, 0);
2261
459
        break;
2262
2263
106
    case MFF_MPLS_BOS:
2264
106
        match_set_any_mpls_bos(match, 0);
2265
106
        break;
2266
2267
298
    case MFF_MPLS_TTL:
2268
298
        match_set_any_mpls_ttl(match, 0);
2269
298
        break;
2270
2271
227
    case MFF_IPV4_SRC:
2272
293
    case MFF_ARP_SPA:
2273
293
        match_set_nw_src_masked(match, htonl(0), htonl(0));
2274
293
        break;
2275
2276
251
    case MFF_IPV4_DST:
2277
317
    case MFF_ARP_TPA:
2278
317
        match_set_nw_dst_masked(match, htonl(0), htonl(0));
2279
317
        break;
2280
2281
343
    case MFF_IPV6_SRC:
2282
343
        memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
2283
343
        memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
2284
343
        break;
2285
2286
201
    case MFF_IPV6_DST:
2287
201
        memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
2288
201
        memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
2289
201
        break;
2290
2291
437
    case MFF_IPV6_LABEL:
2292
437
        match->wc.masks.ipv6_label = htonl(0);
2293
437
        match->flow.ipv6_label = htonl(0);
2294
437
        break;
2295
2296
291
    case MFF_IP_PROTO:
2297
291
        match->wc.masks.nw_proto = 0;
2298
291
        match->flow.nw_proto = 0;
2299
291
        break;
2300
2301
316
    case MFF_IP_DSCP:
2302
555
    case MFF_IP_DSCP_SHIFTED:
2303
555
        match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
2304
555
        match->flow.nw_tos &= ~IP_DSCP_MASK;
2305
555
        break;
2306
2307
73
    case MFF_IP_ECN:
2308
73
        match->wc.masks.nw_tos &= ~IP_ECN_MASK;
2309
73
        match->flow.nw_tos &= ~IP_ECN_MASK;
2310
73
        break;
2311
2312
219
    case MFF_IP_TTL:
2313
219
        match->wc.masks.nw_ttl = 0;
2314
219
        match->flow.nw_ttl = 0;
2315
219
        break;
2316
2317
194
    case MFF_IP_FRAG:
2318
194
        match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
2319
194
        match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
2320
194
        break;
2321
2322
204
    case MFF_ARP_OP:
2323
204
        match->wc.masks.nw_proto = 0;
2324
204
        match->flow.nw_proto = 0;
2325
204
        break;
2326
2327
66
    case MFF_ARP_SHA:
2328
260
    case MFF_ND_SLL:
2329
260
        match->flow.arp_sha = eth_addr_zero;
2330
260
        match->wc.masks.arp_sha = eth_addr_zero;
2331
260
        break;
2332
2333
66
    case MFF_ARP_THA:
2334
100
    case MFF_ND_TLL:
2335
100
        match->flow.arp_tha = eth_addr_zero;
2336
100
        match->wc.masks.arp_tha = eth_addr_zero;
2337
100
        break;
2338
2339
0
    case MFF_ND_RESERVED:
2340
0
        match->wc.masks.igmp_group_ip4 = htonl(0);
2341
0
        match->flow.igmp_group_ip4 = htonl(0);
2342
0
        break;
2343
2344
256
    case MFF_TCP_SRC:
2345
594
    case MFF_UDP_SRC:
2346
661
    case MFF_SCTP_SRC:
2347
866
    case MFF_ICMPV4_TYPE:
2348
1.06k
    case MFF_ICMPV6_TYPE:
2349
1.06k
        match->wc.masks.tp_src = htons(0);
2350
1.06k
        match->flow.tp_src = htons(0);
2351
1.06k
        break;
2352
2353
272
    case MFF_TCP_DST:
2354
534
    case MFF_UDP_DST:
2355
777
    case MFF_SCTP_DST:
2356
845
    case MFF_ICMPV4_CODE:
2357
1.03k
    case MFF_ICMPV6_CODE:
2358
1.03k
        match->wc.masks.tp_dst = htons(0);
2359
1.03k
        match->flow.tp_dst = htons(0);
2360
1.03k
        break;
2361
2362
268
    case MFF_TCP_FLAGS:
2363
268
    case MFF_ND_OPTIONS_TYPE:
2364
268
        match->wc.masks.tcp_flags = htons(0);
2365
268
        match->flow.tcp_flags = htons(0);
2366
268
        break;
2367
2368
0
    case MFF_ND_TARGET:
2369
0
        memset(&match->wc.masks.nd_target, 0,
2370
0
               sizeof match->wc.masks.nd_target);
2371
0
        memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
2372
0
        break;
2373
2374
236
    case MFF_NSH_FLAGS:
2375
236
        MATCH_SET_FIELD_MASKED(match, nsh.flags, 0, 0);
2376
236
        break;
2377
215
    case MFF_NSH_TTL:
2378
215
        MATCH_SET_FIELD_MASKED(match, nsh.ttl, 0, 0);
2379
215
        break;
2380
322
    case MFF_NSH_MDTYPE:
2381
322
        MATCH_SET_FIELD_MASKED(match, nsh.mdtype, 0, 0);
2382
322
        break;
2383
66
    case MFF_NSH_NP:
2384
66
        MATCH_SET_FIELD_MASKED(match, nsh.np, 0, 0);
2385
66
        break;
2386
839
    case MFF_NSH_SPI:
2387
839
        match->wc.masks.nsh.path_hdr &= ~htonl(NSH_SPI_MASK);
2388
839
        nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr, htonl(0));
2389
839
        break;
2390
401
    case MFF_NSH_SI:
2391
401
        match->wc.masks.nsh.path_hdr &= ~htonl(NSH_SI_MASK);
2392
401
        nsh_path_hdr_set_si(&match->flow.nsh.path_hdr, 0);
2393
401
        break;
2394
295
    case MFF_NSH_C1:
2395
621
    case MFF_NSH_C2:
2396
895
    case MFF_NSH_C3:
2397
1.51k
    case MFF_NSH_C4:
2398
1.51k
        MATCH_SET_FIELD_MASKED(match, nsh.context[mf->id - MFF_NSH_C1],
2399
1.51k
                               htonl(0), htonl(0));
2400
1.51k
        break;
2401
2402
0
    case MFF_N_IDS:
2403
0
    default:
2404
0
        OVS_NOT_REACHED();
2405
33.2k
    }
2406
33.2k
}
2407
2408
/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2409
 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2410
 * with a 1-bit indicating that the corresponding value bit must match and a
2411
 * 0-bit indicating a don't-care.
2412
 *
2413
 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2414
 * mf_set_value(mf, value, match).  If 'mask' points to all-0-bits, then this
2415
 * call is equivalent to mf_set_wild(mf, match).
2416
 *
2417
 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()).  The caller
2418
 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
2419
 *
2420
 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2421
 * with the request or NULL if there is no error. The caller is reponsible
2422
 * for freeing the string.
2423
 *
2424
 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
2425
 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
2426
 * protocol versions can support this functionality. */
2427
uint32_t
2428
mf_set(const struct mf_field *mf,
2429
       const union mf_value *value, const union mf_value *mask,
2430
       struct match *match, char **err_str)
2431
151k
{
2432
151k
    if (!mask || is_all_ones(mask, mf->n_bytes)) {
2433
66.9k
        mf_set_value(mf, value, match, err_str);
2434
66.9k
        return mf->usable_protocols_exact;
2435
84.5k
    } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
2436
        /* Tunnel metadata matches on the existence of the field itself, so
2437
         * it still needs to be encoded even if the value is wildcarded. */
2438
33.2k
        mf_set_wild(mf, match, err_str);
2439
33.2k
        return OFPUTIL_P_ANY;
2440
33.2k
    }
2441
2442
51.3k
    if (err_str) {
2443
34.3k
        *err_str = NULL;
2444
34.3k
    }
2445
2446
    /* The cases where 'mask' is all-1-bits or all-0-bits were already handled
2447
     * above[*], so the code below only needs to work for the remaining cases
2448
     * of a nontrivial mask.
2449
     *
2450
     * [*] Except where the field is a tunnel metadata field and 'mask' is
2451
     *     all-0-bits; see above. */
2452
51.3k
    switch (mf->id) {
2453
460
    case MFF_CT_ZONE:
2454
534
    case MFF_CT_NW_PROTO:
2455
771
    case MFF_RECIRC_ID:
2456
1.03k
    case MFF_PACKET_TYPE:
2457
1.38k
    case MFF_CONJ_ID:
2458
1.61k
    case MFF_IN_PORT:
2459
1.68k
    case MFF_IN_PORT_OXM:
2460
1.88k
    case MFF_ACTSET_OUTPUT:
2461
1.88k
    case MFF_SKB_PRIORITY:
2462
2.13k
    case MFF_ETH_TYPE:
2463
2.51k
    case MFF_DL_VLAN:
2464
2.78k
    case MFF_DL_VLAN_PCP:
2465
3.04k
    case MFF_VLAN_PCP:
2466
3.30k
    case MFF_MPLS_LABEL:
2467
3.56k
    case MFF_MPLS_TC:
2468
3.69k
    case MFF_MPLS_BOS:
2469
3.94k
    case MFF_MPLS_TTL:
2470
4.34k
    case MFF_IP_PROTO:
2471
4.59k
    case MFF_IP_TTL:
2472
5.13k
    case MFF_IP_DSCP:
2473
5.39k
    case MFF_IP_DSCP_SHIFTED:
2474
5.65k
    case MFF_IP_ECN:
2475
5.91k
    case MFF_ARP_OP:
2476
6.11k
    case MFF_ICMPV4_TYPE:
2477
6.31k
    case MFF_ICMPV4_CODE:
2478
6.44k
    case MFF_ICMPV6_TYPE:
2479
6.65k
    case MFF_ICMPV6_CODE:
2480
6.65k
    case MFF_ND_RESERVED:
2481
6.65k
    case MFF_ND_OPTIONS_TYPE:
2482
6.65k
        return OFPUTIL_P_NONE;
2483
2484
242
    case MFF_DP_HASH:
2485
242
        match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
2486
242
        break;
2487
365
    case MFF_TUN_ID:
2488
365
        match_set_tun_id_masked(match, value->be64, mask->be64);
2489
365
        break;
2490
213
    case MFF_TUN_SRC:
2491
213
        match_set_tun_src_masked(match, value->be32, mask->be32);
2492
213
        break;
2493
88
    case MFF_TUN_DST:
2494
88
        match_set_tun_dst_masked(match, value->be32, mask->be32);
2495
88
        break;
2496
410
    case MFF_TUN_IPV6_SRC:
2497
410
        match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2498
410
        break;
2499
73
    case MFF_TUN_IPV6_DST:
2500
73
        match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2501
73
        break;
2502
595
    case MFF_TUN_FLAGS:
2503
595
        match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2504
595
        break;
2505
364
    case MFF_TUN_GBP_ID:
2506
364
        match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
2507
364
        break;
2508
101
    case MFF_TUN_GBP_FLAGS:
2509
101
        match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
2510
101
        break;
2511
488
    case MFF_TUN_TTL:
2512
488
        match_set_tun_ttl_masked(match, value->u8, mask->u8);
2513
488
        break;
2514
132
    case MFF_TUN_TOS:
2515
132
        match_set_tun_tos_masked(match, value->u8, mask->u8);
2516
132
        break;
2517
377
    case MFF_TUN_ERSPAN_VER:
2518
377
        match_set_tun_erspan_ver_masked(match, value->u8, mask->u8);
2519
377
        break;
2520
258
    case MFF_TUN_ERSPAN_IDX:
2521
258
        match_set_tun_erspan_idx_masked(match, ntohl(value->be32),
2522
258
                                        ntohl(mask->be32));
2523
258
        break;
2524
19
    case MFF_TUN_ERSPAN_DIR:
2525
19
        match_set_tun_erspan_dir_masked(match, value->u8, mask->u8);
2526
19
        break;
2527
0
    case MFF_TUN_ERSPAN_HWID:
2528
0
        match_set_tun_erspan_hwid_masked(match, value->u8, mask->u8);
2529
0
        break;
2530
257
    case MFF_TUN_GTPU_FLAGS:
2531
257
        match_set_tun_gtpu_flags_masked(match, value->u8, mask->u8);
2532
257
        break;
2533
0
    case MFF_TUN_GTPU_MSGTYPE:
2534
0
        match_set_tun_gtpu_msgtype_masked(match, value->u8, mask->u8);
2535
0
        break;
2536
16.0k
    CASE_MFF_TUN_METADATA:
2537
16.0k
        tun_metadata_set_match(mf, value, mask, match, err_str);
2538
16.0k
        break;
2539
2540
277
    case MFF_METADATA:
2541
277
        match_set_metadata_masked(match, value->be64, mask->be64);
2542
277
        break;
2543
2544
4.59k
    CASE_MFF_REGS:
2545
4.59k
        match_set_reg_masked(match, mf->id - MFF_REG0,
2546
4.59k
                             ntohl(value->be32), ntohl(mask->be32));
2547
4.59k
        break;
2548
2549
2.18k
    CASE_MFF_XREGS:
2550
2.18k
        match_set_xreg_masked(match, mf->id - MFF_XREG0,
2551
2.18k
                              ntohll(value->be64), ntohll(mask->be64));
2552
2.18k
        break;
2553
2554
904
    CASE_MFF_XXREGS: {
2555
904
        match_set_xxreg_masked(match, mf->id - MFF_XXREG0,
2556
904
                ntoh128(value->be128), ntoh128(mask->be128));
2557
904
        break;
2558
1.38k
    }
2559
2560
196
    case MFF_PKT_MARK:
2561
196
        match_set_pkt_mark_masked(match, ntohl(value->be32),
2562
196
                                  ntohl(mask->be32));
2563
196
        break;
2564
2565
723
    case MFF_CT_STATE:
2566
723
        match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
2567
723
        break;
2568
2569
251
    case MFF_CT_MARK:
2570
251
        match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
2571
251
        break;
2572
2573
275
    case MFF_CT_LABEL:
2574
275
        match_set_ct_label_masked(match, ntoh128(value->be128),
2575
275
                                  ntoh128(mask->be128));
2576
275
        break;
2577
2578
392
    case MFF_CT_NW_SRC:
2579
392
        match_set_ct_nw_src_masked(match, value->be32, mask->be32);
2580
392
        break;
2581
2582
303
    case MFF_CT_NW_DST:
2583
303
        match_set_ct_nw_dst_masked(match, value->be32, mask->be32);
2584
303
        break;
2585
2586
313
    case MFF_CT_IPV6_SRC:
2587
313
        match_set_ct_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2588
313
        break;
2589
2590
278
    case MFF_CT_IPV6_DST:
2591
278
        match_set_ct_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2592
278
        break;
2593
2594
581
    case MFF_CT_TP_SRC:
2595
581
        match_set_ct_tp_src_masked(match, value->be16, mask->be16);
2596
581
        break;
2597
2598
280
    case MFF_CT_TP_DST:
2599
280
        match_set_ct_tp_dst_masked(match, value->be16, mask->be16);
2600
280
        break;
2601
2602
342
    case MFF_ETH_DST:
2603
342
        match_set_dl_dst_masked(match, value->mac, mask->mac);
2604
342
        break;
2605
2606
453
    case MFF_ETH_SRC:
2607
453
        match_set_dl_src_masked(match, value->mac, mask->mac);
2608
453
        break;
2609
2610
195
    case MFF_ARP_SHA:
2611
453
    case MFF_ND_SLL:
2612
453
        match_set_arp_sha_masked(match, value->mac, mask->mac);
2613
453
        break;
2614
2615
195
    case MFF_ARP_THA:
2616
454
    case MFF_ND_TLL:
2617
454
        match_set_arp_tha_masked(match, value->mac, mask->mac);
2618
454
        break;
2619
2620
553
    case MFF_VLAN_TCI:
2621
553
        match_set_dl_tci_masked(match, value->be16, mask->be16);
2622
553
        break;
2623
2624
375
    case MFF_VLAN_VID:
2625
375
        match_set_vlan_vid_masked(match, value->be16, mask->be16);
2626
375
        break;
2627
2628
414
    case MFF_IPV4_SRC:
2629
414
        match_set_nw_src_masked(match, value->be32, mask->be32);
2630
414
        break;
2631
2632
361
    case MFF_IPV4_DST:
2633
361
        match_set_nw_dst_masked(match, value->be32, mask->be32);
2634
361
        break;
2635
2636
481
    case MFF_IPV6_SRC:
2637
481
        match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2638
481
        break;
2639
2640
668
    case MFF_IPV6_DST:
2641
668
        match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2642
668
        break;
2643
2644
553
    case MFF_IPV6_LABEL:
2645
553
        if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2646
227
            mf_set_value(mf, value, match, err_str);
2647
326
        } else {
2648
326
            match_set_ipv6_label_masked(match, value->be32, mask->be32);
2649
326
        }
2650
553
        break;
2651
2652
75
    case MFF_ND_TARGET:
2653
75
        match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2654
75
        break;
2655
2656
442
    case MFF_IP_FRAG:
2657
442
        match_set_nw_frag_masked(match, value->u8,
2658
442
                                 mask->u8 & FLOW_NW_FRAG_MASK);
2659
442
        break;
2660
2661
67
    case MFF_ARP_SPA:
2662
67
        match_set_nw_src_masked(match, value->be32, mask->be32);
2663
67
        break;
2664
2665
277
    case MFF_ARP_TPA:
2666
277
        match_set_nw_dst_masked(match, value->be32, mask->be32);
2667
277
        break;
2668
2669
572
    case MFF_TCP_SRC:
2670
1.01k
    case MFF_UDP_SRC:
2671
1.11k
    case MFF_SCTP_SRC:
2672
1.11k
        match_set_tp_src_masked(match, value->be16, mask->be16);
2673
1.11k
        break;
2674
2675
319
    case MFF_TCP_DST:
2676
625
    case MFF_UDP_DST:
2677
869
    case MFF_SCTP_DST:
2678
869
        match_set_tp_dst_masked(match, value->be16, mask->be16);
2679
869
        break;
2680
2681
586
    case MFF_TCP_FLAGS:
2682
586
        match_set_tcp_flags_masked(match, value->be16, mask->be16);
2683
586
        break;
2684
2685
432
    case MFF_NSH_FLAGS:
2686
432
        MATCH_SET_FIELD_MASKED(match, nsh.flags, value->u8, mask->u8);
2687
432
        break;
2688
228
    case MFF_NSH_TTL:
2689
228
        MATCH_SET_FIELD_MASKED(match, nsh.ttl, value->u8, mask->u8);
2690
228
        break;
2691
358
    case MFF_NSH_MDTYPE:
2692
358
        MATCH_SET_FIELD_MASKED(match, nsh.mdtype, value->u8, mask->u8);
2693
358
        break;
2694
209
    case MFF_NSH_NP:
2695
209
        MATCH_SET_FIELD_MASKED(match, nsh.np, value->u8, mask->u8);
2696
209
        break;
2697
905
    case MFF_NSH_SPI:
2698
905
        match->wc.masks.nsh.path_hdr |= mask->be32;
2699
905
        nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr,
2700
905
                             value->be32 & mask->be32);
2701
905
        break;
2702
277
    case MFF_NSH_SI:
2703
277
        match->wc.masks.nsh.path_hdr |= htonl(mask->u8);
2704
277
        nsh_path_hdr_set_si(&match->flow.nsh.path_hdr,
2705
277
                             value->u8 & mask->u8);
2706
277
        break;
2707
356
    case MFF_NSH_C1:
2708
814
    case MFF_NSH_C2:
2709
1.13k
    case MFF_NSH_C3:
2710
2.19k
    case MFF_NSH_C4:
2711
2.19k
        MATCH_SET_FIELD_MASKED(match, nsh.context[mf->id - MFF_NSH_C1],
2712
2.19k
                               value->be32, mask->be32);
2713
2.19k
        break;
2714
2715
0
    case MFF_N_IDS:
2716
0
    default:
2717
0
        OVS_NOT_REACHED();
2718
51.3k
    }
2719
2720
44.6k
    return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
2721
1.11k
             || ip_is_cidr(mask->be32))
2722
44.6k
            ? mf->usable_protocols_cidr
2723
44.6k
            : mf->usable_protocols_bitwise);
2724
51.3k
}
2725
2726
static enum ofperr
2727
mf_check__(const struct mf_subfield *sf, const struct match *match,
2728
           const char *type)
2729
56.8k
{
2730
56.8k
    if (!sf->field) {
2731
0
        VLOG_WARN_RL(&rl, "unknown %s field", type);
2732
0
        return OFPERR_OFPBAC_BAD_SET_TYPE;
2733
56.8k
    } else if (!sf->n_bits) {
2734
0
        VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2735
0
        return OFPERR_OFPBAC_BAD_SET_LEN;
2736
56.8k
    } else if (sf->ofs >= sf->field->n_bits) {
2737
0
        VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2738
0
                     sf->ofs, sf->field->n_bits, type, sf->field->name);
2739
0
        return OFPERR_OFPBAC_BAD_SET_LEN;
2740
56.8k
    } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2741
0
        VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2742
0
                     "of %s field %s", sf->ofs, sf->n_bits,
2743
0
                     sf->field->n_bits, type, sf->field->name);
2744
0
        return OFPERR_OFPBAC_BAD_SET_LEN;
2745
56.8k
    } else if (match && !mf_are_match_prereqs_ok(sf->field, match)) {
2746
291
        VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2747
291
                     type, sf->field->name);
2748
291
        return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2749
56.5k
    } else {
2750
56.5k
        return 0;
2751
56.5k
    }
2752
56.8k
}
2753
2754
/* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2755
static void
2756
unwildcard_subfield(const struct mf_subfield *sf, struct flow_wildcards *wc)
2757
0
{
2758
0
    if (wc) {
2759
0
        union mf_value mask;
2760
2761
0
        memset(&mask, 0, sizeof mask);
2762
0
        bitwise_one(&mask, sf->field->n_bytes, sf->ofs, sf->n_bits);
2763
0
        mf_mask_field_masked(sf->field, &mask, wc);
2764
0
    }
2765
0
}
2766
2767
/* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2768
 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2769
 *
2770
 * 'src' and 'dst' may overlap. */
2771
void
2772
mf_subfield_copy(const struct mf_subfield *src,
2773
                 const struct mf_subfield *dst,
2774
                 struct flow *flow, struct flow_wildcards *wc)
2775
0
{
2776
0
    ovs_assert(src->n_bits == dst->n_bits);
2777
0
    if (mf_are_prereqs_ok(dst->field, flow, wc)
2778
0
        && mf_are_prereqs_ok(src->field, flow, wc)) {
2779
0
        unwildcard_subfield(src, wc);
2780
0
        unwildcard_subfield(dst, wc);
2781
2782
0
        union mf_value src_value;
2783
0
        union mf_value dst_value;
2784
0
        mf_get_value(dst->field, flow, &dst_value);
2785
0
        mf_get_value(src->field, flow, &src_value);
2786
0
        bitwise_copy(&src_value, src->field->n_bytes, src->ofs,
2787
0
                     &dst_value, dst->field->n_bytes, dst->ofs,
2788
0
                     src->n_bits);
2789
0
        mf_set_flow_value(dst->field, &dst_value, flow);
2790
0
    }
2791
0
}
2792
2793
/* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2794
 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2795
 *
2796
 * 'src' and 'dst' may overlap. */
2797
void
2798
mf_subfield_swap(const struct mf_subfield *a,
2799
                 const struct mf_subfield *b,
2800
                 struct flow *flow, struct flow_wildcards *wc)
2801
0
{
2802
0
    ovs_assert(a->n_bits == b->n_bits);
2803
0
    if (mf_are_prereqs_ok(a->field, flow, wc)
2804
0
        && mf_are_prereqs_ok(b->field, flow, wc)) {
2805
0
        unwildcard_subfield(a, wc);
2806
0
        unwildcard_subfield(b, wc);
2807
2808
0
        union mf_value a_value;
2809
0
        union mf_value b_value;
2810
0
        mf_get_value(a->field, flow, &a_value);
2811
0
        mf_get_value(b->field, flow, &b_value);
2812
0
        union mf_value b2_value = b_value;
2813
2814
        /* Copy 'a' into 'b'. */
2815
0
        bitwise_copy(&a_value, a->field->n_bytes, a->ofs,
2816
0
                     &b_value, b->field->n_bytes, b->ofs,
2817
0
                     a->n_bits);
2818
0
        mf_set_flow_value(b->field, &b_value, flow);
2819
2820
        /* Copy original 'b' into 'a'. */
2821
0
        bitwise_copy(&b2_value, b->field->n_bytes, b->ofs,
2822
0
                     &a_value, a->field->n_bytes, a->ofs,
2823
0
                     b->n_bits);
2824
0
        mf_set_flow_value(a->field, &a_value, flow);
2825
0
    }
2826
0
}
2827
2828
/* Checks whether 'sf' is valid for reading a subfield out of 'flow'.  Returns
2829
 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2830
 * ofp_mkerr()).  */
2831
enum ofperr
2832
mf_check_src(const struct mf_subfield *sf, const struct match *match)
2833
53.9k
{
2834
53.9k
    return mf_check__(sf, match, "source");
2835
53.9k
}
2836
2837
/* Checks whether 'sf' is valid for writing a subfield into 'flow'.  Returns 0
2838
 * if so, otherwise an OpenFlow error code (e.g. as returned by
2839
 * ofp_mkerr()). */
2840
enum ofperr
2841
mf_check_dst(const struct mf_subfield *sf, const struct match *match)
2842
2.89k
{
2843
2.89k
    int error = mf_check__(sf, match, "destination");
2844
2.89k
    if (!error && !sf->field->writable) {
2845
1
        VLOG_WARN_RL(&rl, "destination field %s is not writable",
2846
1
                     sf->field->name);
2847
1
        return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2848
1
    }
2849
2.88k
    return error;
2850
2.89k
}
2851
2852
/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2853
 * 'value' and 'mask', respectively. */
2854
void
2855
mf_get(const struct mf_field *mf, const struct match *match,
2856
       union mf_value *value, union mf_value *mask)
2857
91.0k
{
2858
91.0k
    mf_get_value(mf, &match->flow, value);
2859
91.0k
    mf_get_mask(mf, &match->wc, mask);
2860
91.0k
}
2861
2862
static char *
2863
mf_from_integer_string(const struct mf_field *mf, const char *s,
2864
                       uint8_t *valuep, uint8_t *maskp)
2865
87.6k
{
2866
87.6k
    const char *err_str;
2867
87.6k
    char *tail;
2868
87.6k
    int err;
2869
2870
87.6k
    err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2871
87.6k
    if (err || (*tail != '\0' && *tail != '/')) {
2872
92
        err_str = "value";
2873
92
        goto syntax_error;
2874
92
    }
2875
2876
87.5k
    if (*tail == '/') {
2877
34.4k
        err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2878
34.4k
        if (err || *tail != '\0') {
2879
6
            err_str = "mask";
2880
6
            goto syntax_error;
2881
6
        }
2882
53.0k
    } else {
2883
53.0k
        memset(maskp, 0xff, mf->n_bytes);
2884
53.0k
    }
2885
2886
87.5k
    return NULL;
2887
2888
98
syntax_error:
2889
98
    if (err == ERANGE) {
2890
48
        return xasprintf("%s: %s too large for %u-byte field %s",
2891
48
                         s, err_str, mf->n_bytes, mf->name);
2892
50
    } else {
2893
50
        return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2894
50
    }
2895
98
}
2896
2897
static char *
2898
mf_from_packet_type_string(const char *s, ovs_be32 *packet_type)
2899
572
{
2900
572
    const char *err_str;
2901
572
    char *tail;
2902
572
    int err;
2903
2904
572
    if (*s != '(') {
2905
10
        err_str = "missing '('";
2906
10
        goto syntax_error;
2907
10
    }
2908
562
    s++;
2909
562
    err = parse_int_string(s, (uint8_t *)packet_type, 2, &tail);
2910
562
    if (err) {
2911
3
        err_str = "ns";
2912
3
        goto syntax_error;
2913
3
    }
2914
559
    if (*tail != ',') {
2915
4
        err_str = "missing ','";
2916
4
        goto syntax_error;
2917
4
    }
2918
555
    s = tail + 1;
2919
555
    err = parse_int_string(s, ((uint8_t *)packet_type) + 2, 2, &tail);
2920
555
    if (err) {
2921
2
        err_str = "ns_type";
2922
2
        goto syntax_error;
2923
2
    }
2924
553
    if (*tail != ')') {
2925
8
        err_str = "missing ')'";
2926
8
        goto syntax_error;
2927
8
    }
2928
2929
545
    return NULL;
2930
2931
27
syntax_error:
2932
27
    return xasprintf("%s: bad syntax for packet type %s", s, err_str);
2933
553
}
2934
2935
static char *
2936
mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2937
                        struct eth_addr *mac, struct eth_addr *mask)
2938
522
{
2939
522
    int n;
2940
2941
522
    ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2942
2943
522
    n = -1;
2944
522
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
2945
501
        && n == strlen(s)) {
2946
325
        *mask = eth_addr_exact;
2947
325
        return NULL;
2948
325
    }
2949
2950
197
    n = -1;
2951
197
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2952
197
                 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
2953
162
        && n == strlen(s)) {
2954
144
        return NULL;
2955
144
    }
2956
2957
53
    return xasprintf("%s: invalid Ethernet address", s);
2958
197
}
2959
2960
static char *
2961
mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2962
                    ovs_be32 *ip, ovs_be32 *mask)
2963
1.25k
{
2964
1.25k
    ovs_assert(mf->n_bytes == sizeof *ip);
2965
1.25k
    return ip_parse_masked(s, ip, mask);
2966
1.25k
}
2967
2968
static char *
2969
mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2970
                    struct in6_addr *ipv6, struct in6_addr *mask)
2971
3.43k
{
2972
3.43k
    ovs_assert(mf->n_bytes == sizeof *ipv6);
2973
3.43k
    return ipv6_parse_masked(s, ipv6, mask);
2974
3.43k
}
2975
2976
static char *
2977
mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2978
                        const struct ofputil_port_map *port_map,
2979
                        ovs_be16 *valuep, ovs_be16 *maskp)
2980
452
{
2981
452
    ofp_port_t port;
2982
2983
452
    ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2984
2985
452
    if (ofputil_port_from_string(s, port_map, &port)) {
2986
451
        *valuep = htons(ofp_to_u16(port));
2987
451
        *maskp = OVS_BE16_MAX;
2988
451
        return NULL;
2989
451
    }
2990
1
    return xasprintf("%s: invalid or unknown port for %s", s, mf->name);
2991
452
}
2992
2993
static char *
2994
mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2995
                          const struct ofputil_port_map *port_map,
2996
                          ovs_be32 *valuep, ovs_be32 *maskp)
2997
317
{
2998
317
    ofp_port_t port;
2999
3000
317
    ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3001
317
    if (ofputil_port_from_string(s, port_map, &port)) {
3002
316
        *valuep = ofputil_port_to_ofp11(port);
3003
316
        *maskp = OVS_BE32_MAX;
3004
316
        return NULL;
3005
316
    }
3006
1
    return xasprintf("%s: port value out of range for %s", s, mf->name);
3007
317
}
3008
3009
struct frag_handling {
3010
    const char *name;
3011
    uint8_t mask;
3012
    uint8_t value;
3013
};
3014
3015
static const struct frag_handling all_frags[] = {
3016
#define A FLOW_NW_FRAG_ANY
3017
#define L FLOW_NW_FRAG_LATER
3018
    /* name               mask  value */
3019
3020
    { "no",               A|L,  0     },
3021
    { "first",            A|L,  A     },
3022
    { "later",            A|L,  A|L   },
3023
3024
    { "no",               A,    0     },
3025
    { "yes",              A,    A     },
3026
3027
    { "not_later",        L,    0     },
3028
    { "later",            L,    L     },
3029
#undef A
3030
#undef L
3031
};
3032
3033
static char *
3034
mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
3035
480
{
3036
480
    const struct frag_handling *h;
3037
3038
1.84k
    for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
3039
1.82k
        if (!strcasecmp(s, h->name)) {
3040
            /* We force the upper bits of the mask on to make mf_parse_value()
3041
             * happy (otherwise it will never think it's an exact match.) */
3042
462
            *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
3043
462
            *valuep = h->value;
3044
462
            return NULL;
3045
462
        }
3046
1.82k
    }
3047
3048
18
    return xasprintf("%s: unknown fragment type (valid types are \"no\", "
3049
18
                     "\"yes\", \"first\", \"later\", \"not_later\"", s);
3050
480
}
3051
3052
static char *
3053
parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
3054
               const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
3055
               ovs_be16 *maskp)
3056
3.55k
{
3057
3.55k
    int err;
3058
3.55k
    char *err_str;
3059
3.55k
    uint32_t flags, mask;
3060
3061
3.55k
    err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
3062
3.55k
                      &flags, ntohs(allowed), maskp ? &mask : NULL);
3063
3.55k
    if (err < 0) {
3064
114
        return err_str;
3065
114
    }
3066
3067
3.44k
    *flagsp = htons(flags);
3068
3.44k
    if (maskp) {
3069
3.44k
        *maskp = htons(mask);
3070
3.44k
    }
3071
3072
3.44k
    return NULL;
3073
3.55k
}
3074
3075
static char *
3076
mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
3077
1.28k
{
3078
1.28k
    return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
3079
1.28k
                          TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
3080
1.28k
}
3081
3082
static char *
3083
mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
3084
2.27k
{
3085
2.27k
    return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
3086
2.27k
                          htons(FLOW_TNL_PUB_F_MASK), maskp);
3087
2.27k
}
3088
3089
static char *
3090
mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
3091
2.14k
{
3092
2.14k
    int err;
3093
2.14k
    char *err_str;
3094
2.14k
    uint32_t flags, mask;
3095
3096
2.14k
    err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
3097
2.14k
                      &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
3098
2.14k
    if (err < 0) {
3099
186
        return err_str;
3100
186
    }
3101
3102
1.95k
    *flagsp = htonl(flags);
3103
1.95k
    if (maskp) {
3104
1.95k
        *maskp = htonl(mask);
3105
1.95k
    }
3106
3107
1.95k
    return NULL;
3108
2.14k
}
3109
3110
/* Parses 's', a string value for field 'mf', into 'value' and 'mask'.  Returns
3111
 * NULL if successful, otherwise a malloc()'d string describing the error. */
3112
char *
3113
mf_parse(const struct mf_field *mf, const char *s,
3114
         const struct ofputil_port_map *port_map,
3115
         union mf_value *value, union mf_value *mask)
3116
134k
{
3117
134k
    char *error;
3118
3119
134k
    if (!strcmp(s, "*")) {
3120
33.6k
        memset(value, 0, mf->n_bytes);
3121
33.6k
        memset(mask, 0, mf->n_bytes);
3122
33.6k
        return NULL;
3123
33.6k
    }
3124
3125
100k
    switch (mf->string) {
3126
19.5k
    case MFS_DECIMAL:
3127
87.6k
    case MFS_HEXADECIMAL:
3128
87.6k
        error = mf_from_integer_string(mf, s,
3129
87.6k
                                       (uint8_t *) value, (uint8_t *) mask);
3130
87.6k
        break;
3131
3132
2.14k
    case MFS_CT_STATE:
3133
2.14k
        ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3134
2.14k
        error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
3135
2.14k
        break;
3136
3137
522
    case MFS_ETHERNET:
3138
522
        error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
3139
522
        break;
3140
3141
1.25k
    case MFS_IPV4:
3142
1.25k
        error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
3143
1.25k
        break;
3144
3145
3.43k
    case MFS_IPV6:
3146
3.43k
        error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
3147
3.43k
        break;
3148
3149
452
    case MFS_OFP_PORT:
3150
452
        error = mf_from_ofp_port_string(mf, s, port_map,
3151
452
                                        &value->be16, &mask->be16);
3152
452
        break;
3153
3154
317
    case MFS_OFP_PORT_OXM:
3155
317
        error = mf_from_ofp_port_string32(mf, s, port_map,
3156
317
                                          &value->be32, &mask->be32);
3157
317
        break;
3158
3159
480
    case MFS_FRAG:
3160
480
        error = mf_from_frag_string(s, &value->u8, &mask->u8);
3161
480
        break;
3162
3163
2.27k
    case MFS_TNL_FLAGS:
3164
2.27k
        ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3165
2.27k
        error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
3166
2.27k
        break;
3167
3168
1.28k
    case MFS_TCP_FLAGS:
3169
1.28k
        ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3170
1.28k
        error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
3171
1.28k
        break;
3172
3173
572
    case MFS_PACKET_TYPE:
3174
572
        ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3175
572
        error = mf_from_packet_type_string(s, &value->be32);
3176
572
        mask->be32 = OVS_BE32_MAX;
3177
572
        break;
3178
3179
0
    default:
3180
0
        OVS_NOT_REACHED();
3181
100k
    }
3182
3183
100k
    if (!error && !mf_is_mask_valid(mf, mask)) {
3184
5
        error = xasprintf("%s: invalid mask for field %s", s, mf->name);
3185
5
    }
3186
100k
    return error;
3187
100k
}
3188
3189
/* Parses 's', a string value for field 'mf', into 'value'.  Returns NULL if
3190
 * successful, otherwise a malloc()'d string describing the error. */
3191
char *
3192
mf_parse_value(const struct mf_field *mf, const char *s,
3193
               const struct ofputil_port_map *port_map, union mf_value *value)
3194
32.2k
{
3195
32.2k
    union mf_value mask;
3196
32.2k
    char *error;
3197
3198
32.2k
    error = mf_parse(mf, s, port_map, value, &mask);
3199
32.2k
    if (error) {
3200
8
        return error;
3201
8
    }
3202
3203
32.2k
    if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
3204
2
        return xasprintf("%s: wildcards not allowed here", s);
3205
2
    }
3206
32.2k
    return NULL;
3207
32.2k
}
3208
3209
static void
3210
mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
3211
                         const uint8_t *maskp, struct ds *s)
3212
32
{
3213
32
    if (mf->string == MFS_HEXADECIMAL) {
3214
28
        ds_put_hex(s, valuep, mf->n_bytes);
3215
28
    } else {
3216
4
        unsigned long long int integer = 0;
3217
4
        int i;
3218
3219
4
        ovs_assert(mf->n_bytes <= 8);
3220
12
        for (i = 0; i < mf->n_bytes; i++) {
3221
8
            integer = (integer << 8) | valuep[i];
3222
8
        }
3223
4
        ds_put_format(s, "%lld", integer);
3224
4
    }
3225
3226
32
    if (maskp) {
3227
        /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
3228
         * not sure that that a bit-mask written in decimal is ever easier to
3229
         * understand than the same bit-mask written in hexadecimal. */
3230
0
        ds_put_char(s, '/');
3231
0
        ds_put_hex(s, maskp, mf->n_bytes);
3232
0
    }
3233
32
}
3234
3235
static void
3236
mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
3237
4
{
3238
4
    const struct frag_handling *h;
3239
3240
4
    mask &= FLOW_NW_FRAG_MASK;
3241
4
    value &= mask;
3242
3243
14
    for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
3244
13
        if (value == h->value && mask == h->mask) {
3245
3
            ds_put_cstr(s, h->name);
3246
3
            return;
3247
3
        }
3248
13
    }
3249
1
    ds_put_cstr(s, "<error>");
3250
1
}
3251
3252
static void
3253
mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
3254
15
{
3255
15
    format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
3256
15
                        ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
3257
15
}
3258
3259
static void
3260
mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
3261
7
{
3262
7
    format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
3263
7
                        TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
3264
7
}
3265
3266
static void
3267
mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
3268
1
{
3269
1
    format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
3270
1
                        ntohl(mask), UINT16_MAX);
3271
1
}
3272
3273
static void
3274
mf_format_packet_type_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
3275
36
{
3276
36
    format_packet_type_masked(s, value, mask);
3277
36
}
3278
3279
/* Appends to 's' a string representation of field 'mf' whose value is in
3280
 * 'value' and 'mask'.  'mask' may be NULL to indicate an exact match. */
3281
void
3282
mf_format(const struct mf_field *mf,
3283
          const union mf_value *value, const union mf_value *mask,
3284
          const struct ofputil_port_map *port_map,
3285
          struct ds *s)
3286
126
{
3287
126
    if (mask) {
3288
0
        if (is_all_zeros(mask, mf->n_bytes)) {
3289
0
            ds_put_cstr(s, "ANY");
3290
0
            return;
3291
0
        } else if (is_all_ones(mask, mf->n_bytes)) {
3292
0
            mask = NULL;
3293
0
        }
3294
0
    }
3295
3296
126
    switch (mf->string) {
3297
1
    case MFS_OFP_PORT_OXM:
3298
1
        if (!mask) {
3299
1
            ofp_port_t port;
3300
1
            ofputil_port_from_ofp11(value->be32, &port);
3301
1
            ofputil_format_port(port, port_map, s);
3302
1
            break;
3303
1
        }
3304
        /* fall through */
3305
26
    case MFS_OFP_PORT:
3306
26
        if (!mask) {
3307
26
            ofputil_format_port(u16_to_ofp(ntohs(value->be16)), port_map, s);
3308
26
            break;
3309
26
        }
3310
        /* fall through */
3311
4
    case MFS_DECIMAL:
3312
32
    case MFS_HEXADECIMAL:
3313
32
        mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
3314
32
        break;
3315
3316
1
    case MFS_CT_STATE:
3317
1
        mf_format_ct_state_string(value->be32,
3318
1
                                  mask ? mask->be32 : OVS_BE32_MAX, s);
3319
1
        break;
3320
3321
1
    case MFS_ETHERNET:
3322
1
        eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
3323
1
        break;
3324
3325
2
    case MFS_IPV4:
3326
2
        ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
3327
2
        break;
3328
3329
1
    case MFS_IPV6:
3330
1
        ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
3331
1
        break;
3332
3333
4
    case MFS_FRAG:
3334
4
        mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
3335
4
        break;
3336
3337
15
    case MFS_TNL_FLAGS:
3338
15
        mf_format_tnl_flags_string(value->be16,
3339
15
                                   mask ? mask->be16 : OVS_BE16_MAX, s);
3340
15
        break;
3341
3342
7
    case MFS_TCP_FLAGS:
3343
7
        mf_format_tcp_flags_string(value->be16,
3344
7
                                   mask ? mask->be16 : OVS_BE16_MAX, s);
3345
7
        break;
3346
3347
36
    case MFS_PACKET_TYPE:
3348
36
        mf_format_packet_type_string(value->be32,
3349
36
                                     mask ? mask->be32 : OVS_BE32_MAX, s);
3350
36
        break;
3351
3352
0
    default:
3353
0
        OVS_NOT_REACHED();
3354
126
    }
3355
126
}
3356

3357
/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3358
 * least-significant bits in 'x'.
3359
 */
3360
void
3361
mf_write_subfield_flow(const struct mf_subfield *sf,
3362
                       const union mf_subvalue *x, struct flow *flow)
3363
0
{
3364
0
    const struct mf_field *field = sf->field;
3365
0
    union mf_value value;
3366
3367
0
    mf_get_value(field, flow, &value);
3368
0
    bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
3369
0
                 sf->ofs, sf->n_bits);
3370
0
    mf_set_flow_value(field, &value, flow);
3371
0
}
3372
3373
/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
3374
 * least-significant bits in 'x'.
3375
 */
3376
void
3377
mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
3378
                  struct match *match)
3379
0
{
3380
0
    const struct mf_field *field = sf->field;
3381
0
    union mf_value value, mask;
3382
3383
0
    mf_get(field, match, &value, &mask);
3384
0
    bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3385
0
    bitwise_one (                 &mask,  field->n_bytes, sf->ofs, sf->n_bits);
3386
0
    mf_set(field, &value, &mask, match, NULL);
3387
0
}
3388
3389
void
3390
mf_write_subfield_value(const struct mf_subfield *sf, const void *src,
3391
                        struct match *match)
3392
46.9k
{
3393
46.9k
    const struct mf_field *field = sf->field;
3394
46.9k
    union mf_value value, mask;
3395
46.9k
    unsigned int size = DIV_ROUND_UP(sf->n_bits, 8);
3396
3397
46.9k
    mf_get(field, match, &value, &mask);
3398
46.9k
    bitwise_copy(src, size, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3399
46.9k
    bitwise_one (              &mask,  field->n_bytes, sf->ofs, sf->n_bits);
3400
46.9k
    mf_set(field, &value, &mask, match, NULL);
3401
46.9k
}
3402
3403
/* 'v' and 'm' correspond to values of 'field'.  This function copies them into
3404
 * 'match' in the correspond positions. */
3405
void
3406
mf_mask_subfield(const struct mf_field *field,
3407
                 const union mf_subvalue *v,
3408
                 const union mf_subvalue *m,
3409
                 struct match *match)
3410
0
{
3411
0
    union mf_value value, mask;
3412
3413
0
    mf_get(field, match, &value, &mask);
3414
0
    bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
3415
0
    bitwise_copy(m, sizeof *m, 0, &mask,  field->n_bytes, 0, field->n_bits);
3416
0
    mf_set(field, &value, &mask, match, NULL);
3417
0
}
3418
3419
/* Initializes 'x' to the value of 'sf' within 'flow'.  'sf' must be valid for
3420
 * reading 'flow', e.g. as checked by mf_check_src(). */
3421
void
3422
mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
3423
                 union mf_subvalue *x)
3424
0
{
3425
0
    union mf_value value;
3426
3427
0
    mf_get_value(sf->field, flow, &value);
3428
3429
0
    memset(x, 0, sizeof *x);
3430
0
    bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
3431
0
                 x, sizeof *x, 0,
3432
0
                 sf->n_bits);
3433
0
}
3434
3435
/* Returns the value of 'sf' within 'flow'.  'sf' must be valid for reading
3436
 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
3437
 * less. */
3438
uint64_t
3439
mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
3440
0
{
3441
0
    union mf_value value;
3442
3443
0
    mf_get_value(sf->field, flow, &value);
3444
0
    return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
3445
0
}
3446
3447
void
3448
mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3449
0
{
3450
0
    ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
3451
0
}
3452
3453
void
3454
field_array_set(enum mf_field_id id, const union mf_value *value,
3455
                struct field_array *fa)
3456
0
{
3457
0
    size_t i, offset = 0;
3458
3459
0
    ovs_assert(id < MFF_N_IDS);
3460
3461
    /* Find the spot for 'id'. */
3462
0
    BITMAP_FOR_EACH_1 (i, id, fa->used.bm) {
3463
0
        offset += mf_from_id(i)->n_bytes;
3464
0
    }
3465
3466
0
    size_t value_size = mf_from_id(id)->n_bytes;
3467
3468
    /* make room if necessary. */
3469
0
    if (!bitmap_is_set(fa->used.bm, id)) {
3470
0
        fa->values = xrealloc(fa->values, fa->values_size + value_size);
3471
        /* Move remainder forward, if any. */
3472
0
        if (offset < fa->values_size) {
3473
0
            memmove(fa->values + offset + value_size, fa->values + offset,
3474
0
                    fa->values_size - offset);
3475
0
        }
3476
0
        fa->values_size += value_size;
3477
0
    }
3478
0
    bitmap_set1(fa->used.bm, id);
3479
3480
0
    memcpy(fa->values + offset, value, value_size);
3481
0
}
3482
3483
/* A wrapper for variable length mf_fields that is maintained by
3484
 * struct vl_mff_map.*/
3485
struct vl_mf_field {
3486
    struct mf_field mf;
3487
    struct ovs_refcount ref_cnt;
3488
    struct cmap_node cmap_node; /* In ofproto->vl_mff_map->cmap. */
3489
};
3490
3491
static inline uint32_t
3492
mf_field_hash(uint32_t key)
3493
0
{
3494
0
    return hash_int(key, 0);
3495
0
}
3496
3497
static void
3498
vmf_delete(struct vl_mf_field *vmf)
3499
0
{
3500
0
    if (ovs_refcount_unref(&vmf->ref_cnt) == 1) {
3501
        /* Postpone as this function is typically called immediately
3502
         * after removing from cmap. */
3503
0
        ovsrcu_postpone(free, vmf);
3504
0
    } else {
3505
0
        VLOG_WARN_RL(&rl,
3506
0
                     "Attempted to delete VMF %s but refcount is nonzero!",
3507
0
                     vmf->mf.name);
3508
0
    }
3509
0
}
3510
3511
enum ofperr
3512
mf_vl_mff_map_clear(struct vl_mff_map *vl_mff_map, bool force)
3513
    OVS_REQUIRES(vl_mff_map->mutex)
3514
0
{
3515
0
    struct vl_mf_field *vmf;
3516
3517
0
    if (!force) {
3518
0
        CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3519
0
            if (ovs_refcount_read(&vmf->ref_cnt) != 1) {
3520
0
                return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3521
0
            }
3522
0
        }
3523
0
    }
3524
3525
0
    CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3526
0
        cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
3527
0
                    mf_field_hash(vmf->mf.id));
3528
0
        vmf_delete(vmf);
3529
0
    }
3530
3531
0
    return 0;
3532
0
}
3533
3534
static struct vl_mf_field *
3535
mf_get_vl_mff__(uint32_t id, const struct vl_mff_map *vl_mff_map)
3536
0
{
3537
0
    struct vl_mf_field *vmf;
3538
3539
0
    CMAP_FOR_EACH_WITH_HASH (vmf, cmap_node, mf_field_hash(id),
3540
0
                             &vl_mff_map->cmap) {
3541
0
        if (vmf->mf.id == id) {
3542
0
            return vmf;
3543
0
        }
3544
0
    }
3545
3546
0
    return NULL;
3547
0
}
3548
3549
/* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
3550
 * pointer to the variable length meta-flow field corresponding to 'mff'.
3551
 * Returns NULL if no mapping is existed for 'mff'. */
3552
const struct mf_field *
3553
mf_get_vl_mff(const struct mf_field *mff,
3554
              const struct vl_mff_map *vl_mff_map)
3555
0
{
3556
0
    if (mff && mff->variable_len && vl_mff_map) {
3557
0
        struct vl_mf_field *vl_mff = mf_get_vl_mff__(mff->id, vl_mff_map);
3558
3559
0
        return vl_mff ? &vl_mff->mf : NULL;
3560
0
    }
3561
3562
0
    return NULL;
3563
0
}
3564
3565
static enum ofperr
3566
mf_vl_mff_map_del(struct vl_mff_map *vl_mff_map,
3567
                  const struct ofputil_tlv_table_mod *ttm, bool force)
3568
    OVS_REQUIRES(vl_mff_map->mutex)
3569
0
{
3570
0
    struct ofputil_tlv_map *tlv_map;
3571
0
    struct vl_mf_field *vmf;
3572
0
    unsigned int idx;
3573
3574
0
    if (!force) {
3575
0
        LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3576
0
            idx = MFF_TUN_METADATA0 + tlv_map->index;
3577
0
            if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3578
0
                return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3579
0
            }
3580
3581
0
            vmf = mf_get_vl_mff__(idx, vl_mff_map);
3582
0
            if (vmf && ovs_refcount_read(&vmf->ref_cnt) != 1) {
3583
0
                return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3584
0
            }
3585
0
        }
3586
0
    }
3587
3588
0
    LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3589
0
        idx = MFF_TUN_METADATA0 + tlv_map->index;
3590
0
        if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3591
0
            return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3592
0
        }
3593
3594
0
        vmf = mf_get_vl_mff__(idx, vl_mff_map);
3595
0
        if (vmf) {
3596
0
            cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
3597
0
                        mf_field_hash(idx));
3598
0
            vmf_delete(vmf);
3599
0
        }
3600
0
    }
3601
3602
0
    return 0;
3603
0
}
3604
3605
static enum ofperr
3606
mf_vl_mff_map_add(struct vl_mff_map *vl_mff_map,
3607
                  const struct ofputil_tlv_table_mod *ttm)
3608
    OVS_REQUIRES(vl_mff_map->mutex)
3609
0
{
3610
0
    struct ofputil_tlv_map *tlv_map;
3611
0
    struct vl_mf_field *vmf;
3612
0
    unsigned int idx;
3613
3614
0
    LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3615
0
        idx = MFF_TUN_METADATA0 + tlv_map->index;
3616
0
        if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3617
0
            return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3618
0
        }
3619
3620
0
        vmf = xmalloc(sizeof *vmf);
3621
0
        vmf->mf = mf_fields[idx];
3622
0
        vmf->mf.n_bytes = tlv_map->option_len;
3623
0
        vmf->mf.n_bits = tlv_map->option_len * 8;
3624
0
        vmf->mf.mapped = true;
3625
0
        ovs_refcount_init(&vmf->ref_cnt);
3626
3627
0
        cmap_insert(&vl_mff_map->cmap, &vmf->cmap_node,
3628
0
                    mf_field_hash(idx));
3629
0
    }
3630
3631
0
    return 0;
3632
0
}
3633
3634
/* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3635
 * This function must be invoked after tun_metadata_table_mod().
3636
 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3637
 * invalid.
3638
 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3639
 * vl_mf_field that is still used by any active flow.*/
3640
enum ofperr
3641
mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map *vl_mff_map,
3642
                                    const struct ofputil_tlv_table_mod *ttm)
3643
    OVS_REQUIRES(vl_mff_map->mutex)
3644
0
{
3645
0
    switch (ttm->command) {
3646
0
    case NXTTMC_ADD:
3647
0
        return mf_vl_mff_map_add(vl_mff_map, ttm);
3648
3649
0
    case NXTTMC_DELETE:
3650
0
        return mf_vl_mff_map_del(vl_mff_map, ttm, false);
3651
3652
0
    case NXTTMC_CLEAR:
3653
0
        return mf_vl_mff_map_clear(vl_mff_map, false);
3654
3655
0
    default:
3656
0
        OVS_NOT_REACHED();
3657
0
    }
3658
3659
0
    return 0;
3660
0
}
3661
3662
/* Returns true if a variable length meta-flow field 'mff' is not mapped in
3663
 * the 'vl_mff_map'. */
3664
bool
3665
mf_vl_mff_invalid(const struct mf_field *mff, const struct vl_mff_map *map)
3666
0
{
3667
0
    return map && mff && mff->variable_len && !mff->mapped;
3668
0
}
3669
3670
void
3671
mf_vl_mff_set_tlv_bitmap(const struct mf_field *mff, uint64_t *tlv_bitmap)
3672
0
{
3673
0
    if (mff && mff->mapped) {
3674
0
        ovs_assert(mf_is_tun_metadata(mff));
3675
0
        ULLONG_SET1(*tlv_bitmap, mff->id - MFF_TUN_METADATA0);
3676
0
    }
3677
0
}
3678
3679
static void
3680
mf_vl_mff_ref_cnt_mod(const struct vl_mff_map *map, uint64_t tlv_bitmap,
3681
                      bool ref)
3682
0
{
3683
0
    struct vl_mf_field *vmf;
3684
0
    int i;
3685
3686
0
    if (map) {
3687
0
        ULLONG_FOR_EACH_1 (i, tlv_bitmap) {
3688
0
            vmf = mf_get_vl_mff__(i + MFF_TUN_METADATA0, map);
3689
0
            if (vmf) {
3690
0
                if (ref) {
3691
0
                    ovs_refcount_ref(&vmf->ref_cnt);
3692
0
                } else {
3693
0
                    ovs_refcount_unref(&vmf->ref_cnt);
3694
0
                }
3695
0
            } else {
3696
0
                VLOG_WARN("Invalid TLV index %d.", i);
3697
0
            }
3698
0
        }
3699
0
    }
3700
0
}
3701
3702
void
3703
mf_vl_mff_ref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3704
0
{
3705
0
    mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, true);
3706
0
}
3707
3708
void
3709
mf_vl_mff_unref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3710
0
{
3711
0
    mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, false);
3712
0
}
3713
3714
enum ofperr
3715
mf_vl_mff_nx_pull_header(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3716
                         const struct mf_field **field, bool *masked,
3717
                         uint64_t *tlv_bitmap)
3718
0
{
3719
0
    enum ofperr error = nx_pull_header(b, vl_mff_map, field, masked);
3720
0
    if (error) {
3721
0
        return error;
3722
0
    }
3723
3724
0
    mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3725
0
    return 0;
3726
0
}
3727
3728
enum ofperr
3729
mf_vl_mff_nx_pull_entry(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3730
                        const struct mf_field **field, union mf_value *value,
3731
                        union mf_value *mask, uint64_t *tlv_bitmap)
3732
0
{
3733
0
    enum ofperr error = nx_pull_entry(b, vl_mff_map, field, value, mask, true);
3734
0
    if (error) {
3735
0
        return error;
3736
0
    }
3737
3738
0
    mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3739
0
    return 0;
3740
0
}
3741
3742
enum ofperr
3743
mf_vl_mff_mf_from_nxm_header(uint32_t header,
3744
                             const struct vl_mff_map *vl_mff_map,
3745
                             const struct mf_field **field,
3746
                             uint64_t *tlv_bitmap)
3747
0
{
3748
0
    *field = mf_from_nxm_header(header, vl_mff_map);
3749
0
    if (!*field) {
3750
0
        return OFPERR_OFPBAC_BAD_SET_TYPE;
3751
0
    } else if (mf_vl_mff_invalid(*field, vl_mff_map)) {
3752
0
        return OFPERR_NXFMFC_INVALID_TLV_FIELD;
3753
0
    }
3754
3755
0
    mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3756
0
    return 0;
3757
0
}
3758

3759
/* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
3760
 * false otherwise. */
3761
bool
3762
mf_bitmap_is_superset(const struct mf_bitmap *super,
3763
                      const struct mf_bitmap *sub)
3764
0
{
3765
0
    return bitmap_is_superset(super->bm, sub->bm, MFF_N_IDS);
3766
0
}
3767
3768
/* Returns the bitwise-and of 'a' and 'b'. */
3769
struct mf_bitmap
3770
mf_bitmap_and(struct mf_bitmap a, struct mf_bitmap b)
3771
0
{
3772
0
    bitmap_and(a.bm, b.bm, MFF_N_IDS);
3773
0
    return a;
3774
0
}
3775
3776
/* Returns the bitwise-or of 'a' and 'b'. */
3777
struct mf_bitmap
3778
mf_bitmap_or(struct mf_bitmap a, struct mf_bitmap b)
3779
0
{
3780
0
    bitmap_or(a.bm, b.bm, MFF_N_IDS);
3781
0
    return a;
3782
0
}
3783
3784
/* Returns the bitwise-not of 'x'. */
3785
struct mf_bitmap
3786
mf_bitmap_not(struct mf_bitmap x)
3787
0
{
3788
0
    bitmap_not(x.bm, MFF_N_IDS);
3789
0
    return x;
3790
0
}
3791
3792
void
3793
mf_set_mask_l3_prereqs(const struct mf_field *mf, const struct flow *fl,
3794
                       struct flow_wildcards *wc)
3795
0
{
3796
0
    if (is_ip_any(fl) &&
3797
0
        ((mf->id == MFF_IPV4_SRC) ||
3798
0
         (mf->id == MFF_IPV4_DST) ||
3799
0
         (mf->id == MFF_IPV6_SRC) ||
3800
0
         (mf->id == MFF_IPV6_DST) ||
3801
0
         (mf->id == MFF_IPV6_LABEL) ||
3802
0
         (mf->id == MFF_IP_DSCP) ||
3803
0
         (mf->id == MFF_IP_ECN) ||
3804
0
         (mf->id == MFF_IP_TTL))) {
3805
0
        WC_MASK_FIELD(wc, nw_proto);
3806
0
    } else if ((fl->dl_type == htons(ETH_TYPE_ARP)) &&
3807
0
               ((mf->id == MFF_ARP_OP) ||
3808
0
                (mf->id == MFF_ARP_SHA) ||
3809
0
                (mf->id == MFF_ARP_THA) ||
3810
0
                (mf->id == MFF_ARP_SPA) ||
3811
0
                (mf->id == MFF_ARP_TPA))) {
3812
        /* mask only the lower 8 bits. */
3813
0
        wc->masks.nw_proto = 0xff;
3814
0
    }
3815
0
}