Coverage Report

Created: 2026-10-17 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/match.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 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
#include "openvswitch/match.h"
19
#include <stdlib.h>
20
#include "flow.h"
21
#include "byte-order.h"
22
#include "colors.h"
23
#include "openvswitch/dynamic-string.h"
24
#include "openvswitch/meta-flow.h"
25
#include "openvswitch/ofp-port.h"
26
#include "packets.h"
27
#include "tun-metadata.h"
28
#include "openvswitch/nsh.h"
29
30
/* Converts the flow in 'flow' into a match in 'match', with the given
31
 * 'wildcards'. */
32
void
33
match_init(struct match *match,
34
           const struct flow *flow, const struct flow_wildcards *wc)
35
0
{
36
0
    match->flow = *flow;
37
0
    match->wc = *wc;
38
0
    match_zero_wildcarded_fields(match);
39
0
    memset(&match->tun_md, 0, sizeof match->tun_md);
40
0
}
41
42
/* Converts a flow into a match.  It sets the wildcard masks based on
43
 * the packet contents.  It will not set the mask for fields that do not
44
 * make sense for the packet type. */
45
void
46
match_wc_init(struct match *match, const struct flow *flow)
47
104k
{
48
104k
    match->flow = *flow;
49
50
104k
    flow_wildcards_init_for_packet(&match->wc, flow);
51
104k
    WC_MASK_FIELD(&match->wc, regs);
52
104k
    WC_MASK_FIELD(&match->wc, metadata);
53
54
104k
    memset(&match->tun_md, 0, sizeof match->tun_md);
55
104k
}
56
57
/* Initializes 'match' as a "catch-all" match that matches every packet. */
58
void
59
match_init_catchall(struct match *match)
60
640k
{
61
640k
    memset(&match->flow, 0, sizeof match->flow);
62
640k
    flow_wildcards_init_catchall(&match->wc);
63
640k
    memset(&match->tun_md, 0, sizeof match->tun_md);
64
640k
}
65
66
/* For each bit or field wildcarded in 'match', sets the corresponding bit or
67
 * field in 'flow' to all-0-bits.  It is important to maintain this invariant
68
 * in a match that might be inserted into a classifier.
69
 *
70
 * It is never necessary to call this function directly for a match that is
71
 * initialized or modified only by match_*() functions.  It is useful to
72
 * restore the invariant in a match whose 'wc' member is modified by hand.
73
 */
74
void
75
match_zero_wildcarded_fields(struct match *match)
76
164k
{
77
164k
    flow_zero_wildcards(&match->flow, &match->wc);
78
164k
}
79
80
void
81
match_set_dp_hash(struct match *match, uint32_t value)
82
394
{
83
394
    match_set_dp_hash_masked(match, value, UINT32_MAX);
84
394
}
85
86
void
87
match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask)
88
1.38k
{
89
1.38k
    match->wc.masks.dp_hash = mask;
90
1.38k
    match->flow.dp_hash = value & mask;
91
1.38k
}
92
93
void
94
match_set_recirc_id(struct match *match, uint32_t value)
95
789
{
96
789
    match->flow.recirc_id = value;
97
789
    match->wc.masks.recirc_id = UINT32_MAX;
98
789
}
99
100
void
101
match_set_conj_id(struct match *match, uint32_t value)
102
286
{
103
286
    match->flow.conj_id = value;
104
286
    match->wc.masks.conj_id = UINT32_MAX;
105
286
}
106
107
void
108
match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
109
19.5k
{
110
19.5k
    match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
111
19.5k
}
112
113
void
114
match_set_reg_masked(struct match *match, unsigned int reg_idx,
115
                     uint32_t value, uint32_t mask)
116
60.8k
{
117
60.8k
    ovs_assert(reg_idx < FLOW_N_REGS);
118
60.8k
    flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
119
60.8k
    match->flow.regs[reg_idx] = value & mask;
120
60.8k
}
121
122
void
123
match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value)
124
10.5k
{
125
10.5k
    match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX);
126
10.5k
}
127
128
void
129
match_set_xreg_masked(struct match *match, unsigned int xreg_idx,
130
                      uint64_t value, uint64_t mask)
131
28.6k
{
132
28.6k
    ovs_assert(xreg_idx < FLOW_N_XREGS);
133
28.6k
    flow_wildcards_set_xreg_mask(&match->wc, xreg_idx, mask);
134
28.6k
    flow_set_xreg(&match->flow, xreg_idx, value & mask);
135
28.6k
}
136
137
void
138
match_set_xxreg(struct match *match, unsigned int xxreg_idx, ovs_u128 value)
139
3.09k
{
140
3.09k
    match_set_xxreg_masked(match, xxreg_idx, value, OVS_U128_MAX);
141
3.09k
}
142
143
void
144
match_set_xxreg_masked(struct match *match, unsigned int xxreg_idx,
145
                      ovs_u128 value, ovs_u128 mask)
146
15.0k
{
147
15.0k
    ovs_assert(xxreg_idx < FLOW_N_XXREGS);
148
15.0k
    flow_wildcards_set_xxreg_mask(&match->wc, xxreg_idx, mask);
149
15.0k
    flow_set_xxreg(&match->flow, xxreg_idx, ovs_u128_and(value, mask));
150
15.0k
}
151
152
void
153
match_set_actset_output(struct match *match, ofp_port_t actset_output)
154
2.07k
{
155
2.07k
    match->wc.masks.actset_output = u16_to_ofp(UINT16_MAX);
156
2.07k
    match->flow.actset_output = actset_output;
157
2.07k
}
158
159
void
160
match_set_metadata(struct match *match, ovs_be64 metadata)
161
491
{
162
491
    match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
163
491
}
164
165
void
166
match_set_metadata_masked(struct match *match,
167
                          ovs_be64 metadata, ovs_be64 mask)
168
9.71k
{
169
9.71k
    match->wc.masks.metadata = mask;
170
9.71k
    match->flow.metadata = metadata & mask;
171
9.71k
}
172
173
void
174
match_set_tun_id(struct match *match, ovs_be64 tun_id)
175
420
{
176
420
    match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
177
420
}
178
179
void
180
match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
181
2.52k
{
182
2.52k
    match->wc.masks.tunnel.tun_id = mask;
183
2.52k
    match->flow.tunnel.tun_id = tun_id & mask;
184
2.52k
}
185
186
void
187
match_set_tun_src(struct match *match, ovs_be32 src)
188
669
{
189
669
    match_set_tun_src_masked(match, src, OVS_BE32_MAX);
190
669
}
191
192
void
193
match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
194
1.52k
{
195
1.52k
    match->wc.masks.tunnel.ip_src = mask;
196
1.52k
    match->flow.tunnel.ip_src = src & mask;
197
1.52k
}
198
199
void
200
match_set_tun_dst(struct match *match, ovs_be32 dst)
201
273
{
202
273
    match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
203
273
}
204
205
void
206
match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
207
1.12k
{
208
1.12k
    match->wc.masks.tunnel.ip_dst = mask;
209
1.12k
    match->flow.tunnel.ip_dst = dst & mask;
210
1.12k
}
211
212
void
213
match_set_tun_ipv6_src(struct match *match, const struct in6_addr *src)
214
593
{
215
593
    match->flow.tunnel.ipv6_src = *src;
216
593
    match->wc.masks.tunnel.ipv6_src = in6addr_exact;
217
593
}
218
219
void
220
match_set_tun_ipv6_src_masked(struct match *match, const struct in6_addr *src,
221
                              const struct in6_addr *mask)
222
1.14k
{
223
1.14k
    match->flow.tunnel.ipv6_src = ipv6_addr_bitand(src, mask);
224
1.14k
    match->wc.masks.tunnel.ipv6_src = *mask;
225
1.14k
}
226
227
void
228
match_set_tun_ipv6_dst(struct match *match, const struct in6_addr *dst)
229
326
{
230
326
    match->flow.tunnel.ipv6_dst = *dst;
231
326
    match->wc.masks.tunnel.ipv6_dst = in6addr_exact;
232
326
}
233
234
void
235
match_set_tun_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
236
                              const struct in6_addr *mask)
237
1.16k
{
238
1.16k
    match->flow.tunnel.ipv6_dst = ipv6_addr_bitand(dst, mask);
239
1.16k
    match->wc.masks.tunnel.ipv6_dst = *mask;
240
1.16k
}
241
242
void
243
match_set_tun_ttl(struct match *match, uint8_t ttl)
244
293
{
245
293
    match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
246
293
}
247
248
void
249
match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
250
1.12k
{
251
1.12k
    match->wc.masks.tunnel.ip_ttl = mask;
252
1.12k
    match->flow.tunnel.ip_ttl = ttl & mask;
253
1.12k
}
254
255
void
256
match_set_tun_tos(struct match *match, uint8_t tos)
257
313
{
258
313
    match_set_tun_tos_masked(match, tos, UINT8_MAX);
259
313
}
260
261
void
262
match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
263
1.13k
{
264
1.13k
    match->wc.masks.tunnel.ip_tos = mask;
265
1.13k
    match->flow.tunnel.ip_tos = tos & mask;
266
1.13k
}
267
268
void
269
match_set_tun_flags(struct match *match, uint16_t flags)
270
472
{
271
472
    match_set_tun_flags_masked(match, flags, UINT16_MAX);
272
472
}
273
274
void
275
match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
276
2.18k
{
277
2.18k
    mask &= FLOW_TNL_PUB_F_MASK;
278
279
2.18k
    match->wc.masks.tunnel.flags = mask;
280
2.18k
    match->flow.tunnel.flags = flags & mask;
281
2.18k
}
282
283
void
284
match_set_tun_tp_dst(struct match *match, ovs_be16 tp_dst)
285
0
{
286
0
    match_set_tun_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
287
0
}
288
289
void
290
match_set_tun_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
291
0
{
292
0
    match->wc.masks.tunnel.tp_dst = mask;
293
0
    match->flow.tunnel.tp_dst = port & mask;
294
0
}
295
296
void
297
match_set_tun_gbp_id_masked(struct match *match, ovs_be16 gbp_id, ovs_be16 mask)
298
1.82k
{
299
1.82k
    match->wc.masks.tunnel.gbp_id = mask;
300
1.82k
    match->flow.tunnel.gbp_id = gbp_id & mask;
301
1.82k
}
302
303
void
304
match_set_tun_gbp_id(struct match *match, ovs_be16 gbp_id)
305
257
{
306
257
    match_set_tun_gbp_id_masked(match, gbp_id, OVS_BE16_MAX);
307
257
}
308
309
void
310
match_set_tun_gbp_flags_masked(struct match *match, uint8_t flags, uint8_t mask)
311
2.96k
{
312
2.96k
    match->wc.masks.tunnel.gbp_flags = mask;
313
2.96k
    match->flow.tunnel.gbp_flags = flags & mask;
314
2.96k
}
315
316
void
317
match_set_tun_gbp_flags(struct match *match, uint8_t flags)
318
347
{
319
347
    match_set_tun_gbp_flags_masked(match, flags, UINT8_MAX);
320
347
}
321
322
void
323
match_set_tun_erspan_ver_masked(struct match *match, uint8_t ver, uint8_t mask)
324
1.06k
{
325
1.06k
    match->wc.masks.tunnel.erspan_ver = ver;
326
1.06k
    match->flow.tunnel.erspan_ver = ver & mask;
327
1.06k
}
328
329
void
330
match_set_tun_erspan_ver(struct match *match, uint8_t ver)
331
211
{
332
211
    match_set_tun_erspan_ver_masked(match, ver, UINT8_MAX);
333
211
}
334
335
void
336
match_set_tun_erspan_idx_masked(struct match *match, uint32_t erspan_idx,
337
                                uint32_t mask)
338
467
{
339
467
    match->wc.masks.tunnel.erspan_idx = mask;
340
467
    match->flow.tunnel.erspan_idx = erspan_idx & mask;
341
467
}
342
343
void
344
match_set_tun_erspan_idx(struct match *match, uint32_t erspan_idx)
345
98
{
346
98
    match_set_tun_erspan_idx_masked(match, erspan_idx, UINT32_MAX);
347
98
}
348
349
void
350
match_set_tun_erspan_dir_masked(struct match *match, uint8_t dir,
351
                                uint8_t mask)
352
560
{
353
560
    match->wc.masks.tunnel.erspan_dir = dir;
354
560
    match->flow.tunnel.erspan_dir = dir & mask;
355
560
}
356
357
void
358
match_set_tun_erspan_dir(struct match *match, uint8_t dir)
359
133
{
360
133
    match_set_tun_erspan_dir_masked(match, dir, UINT8_MAX);
361
133
}
362
363
void
364
match_set_tun_erspan_hwid_masked(struct match *match, uint8_t hwid,
365
                                 uint8_t mask)
366
739
{
367
739
    match->wc.masks.tunnel.erspan_hwid = hwid;
368
739
    match->flow.tunnel.erspan_hwid = hwid & mask;
369
739
}
370
371
void
372
match_set_tun_erspan_hwid(struct match *match, uint8_t hwid)
373
232
{
374
232
    match_set_tun_erspan_hwid_masked(match, hwid, UINT8_MAX);
375
232
}
376
377
void
378
match_set_tun_gtpu_flags_masked(struct match *match, uint8_t flags,
379
                                uint8_t mask)
380
1.43k
{
381
1.43k
    match->wc.masks.tunnel.gtpu_flags = flags;
382
1.43k
    match->flow.tunnel.gtpu_flags = flags & mask;
383
1.43k
}
384
385
void
386
match_set_tun_gtpu_flags(struct match *match, uint8_t flags)
387
812
{
388
812
    match_set_tun_gtpu_flags_masked(match, flags, UINT8_MAX);
389
812
}
390
391
void
392
match_set_tun_gtpu_msgtype_masked(struct match *match, uint8_t msgtype,
393
                                  uint8_t mask)
394
649
{
395
649
    match->wc.masks.tunnel.gtpu_msgtype = msgtype;
396
649
    match->flow.tunnel.gtpu_msgtype = msgtype & mask;
397
649
}
398
399
void
400
match_set_tun_gtpu_msgtype(struct match *match, uint8_t msgtype)
401
45
{
402
45
    match_set_tun_gtpu_msgtype_masked(match, msgtype, UINT8_MAX);
403
45
}
404
405
void
406
match_set_in_port(struct match *match, ofp_port_t ofp_port)
407
193k
{
408
193k
    match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
409
193k
    match->flow.in_port.ofp_port = ofp_port;
410
193k
}
411
412
void
413
match_set_skb_priority(struct match *match, uint32_t skb_priority)
414
0
{
415
0
    match->wc.masks.skb_priority = UINT32_MAX;
416
0
    match->flow.skb_priority = skb_priority;
417
0
}
418
419
void
420
match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
421
524
{
422
524
    match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
423
524
}
424
425
void
426
match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
427
1.79k
{
428
1.79k
    match->flow.pkt_mark = pkt_mark & mask;
429
1.79k
    match->wc.masks.pkt_mark = mask;
430
1.79k
}
431
432
void
433
match_set_ct_state(struct match *match, uint32_t ct_state)
434
2.38k
{
435
2.38k
    match_set_ct_state_masked(match, ct_state, UINT32_MAX);
436
2.38k
}
437
438
void
439
match_set_ct_state_masked(struct match *match, uint32_t ct_state, uint32_t mask)
440
3.19k
{
441
3.19k
    match->flow.ct_state = ct_state & mask & UINT8_MAX;
442
3.19k
    match->wc.masks.ct_state = mask & UINT8_MAX;
443
3.19k
}
444
445
void
446
match_set_ct_zone(struct match *match, uint16_t ct_zone)
447
490
{
448
490
    match_set_ct_zone_masked(match, ct_zone, UINT16_MAX);
449
490
}
450
451
void
452
match_set_ct_zone_masked(struct match *match, uint16_t ct_zone, uint16_t mask)
453
490
{
454
490
    match->flow.ct_zone = ct_zone & mask;
455
490
    match->wc.masks.ct_zone = mask;
456
490
}
457
458
void
459
match_set_ct_mark(struct match *match, uint32_t ct_mark)
460
321
{
461
321
    match_set_ct_mark_masked(match, ct_mark, UINT32_MAX);
462
321
}
463
464
void
465
match_set_ct_mark_masked(struct match *match, uint32_t ct_mark,
466
                           uint32_t mask)
467
3.62k
{
468
3.62k
    match->flow.ct_mark = ct_mark & mask;
469
3.62k
    match->wc.masks.ct_mark = mask;
470
3.62k
}
471
472
void
473
match_set_ct_label(struct match *match, ovs_u128 ct_label)
474
451
{
475
451
    ovs_u128 mask;
476
477
451
    mask.u64.lo = UINT64_MAX;
478
451
    mask.u64.hi = UINT64_MAX;
479
451
    match_set_ct_label_masked(match, ct_label, mask);
480
451
}
481
482
void
483
match_set_ct_label_masked(struct match *match, ovs_u128 value, ovs_u128 mask)
484
1.37k
{
485
1.37k
    match->flow.ct_label.u64.lo = value.u64.lo & mask.u64.lo;
486
1.37k
    match->flow.ct_label.u64.hi = value.u64.hi & mask.u64.hi;
487
1.37k
    match->wc.masks.ct_label = mask;
488
1.37k
}
489
490
void
491
match_set_ct_nw_src(struct match *match, ovs_be32 ct_nw_src)
492
337
{
493
337
    match->flow.ct_nw_src = ct_nw_src;
494
337
    match->wc.masks.ct_nw_src = OVS_BE32_MAX;
495
337
}
496
497
void
498
match_set_ct_nw_src_masked(struct match *match, ovs_be32 ct_nw_src,
499
                           ovs_be32 mask)
500
319
{
501
319
    match->flow.ct_nw_src = ct_nw_src & mask;
502
319
    match->wc.masks.ct_nw_src = mask;
503
319
}
504
505
void
506
match_set_ct_nw_dst(struct match *match, ovs_be32 ct_nw_dst)
507
620
{
508
620
    match->flow.ct_nw_dst = ct_nw_dst;
509
620
    match->wc.masks.ct_nw_dst = OVS_BE32_MAX;
510
620
}
511
512
void
513
match_set_ct_nw_dst_masked(struct match *match, ovs_be32 ct_nw_dst,
514
                           ovs_be32 mask)
515
420
{
516
420
    match->flow.ct_nw_dst = ct_nw_dst & mask;
517
420
    match->wc.masks.ct_nw_dst = mask;
518
420
}
519
520
void
521
match_set_ct_nw_proto(struct match *match, uint8_t ct_nw_proto)
522
405
{
523
405
    match->flow.ct_nw_proto = ct_nw_proto;
524
405
    match->wc.masks.ct_nw_proto = UINT8_MAX;
525
405
}
526
527
void
528
match_set_ct_tp_src(struct match *match, ovs_be16 ct_tp_src)
529
659
{
530
659
    match_set_ct_tp_src_masked(match, ct_tp_src, OVS_BE16_MAX);
531
659
}
532
533
void
534
match_set_ct_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
535
1.08k
{
536
1.08k
    match->flow.ct_tp_src = port & mask;
537
1.08k
    match->wc.masks.ct_tp_src = mask;
538
1.08k
}
539
540
void
541
match_set_ct_tp_dst(struct match *match, ovs_be16 ct_tp_dst)
542
450
{
543
450
    match_set_ct_tp_dst_masked(match, ct_tp_dst, OVS_BE16_MAX);
544
450
}
545
546
void
547
match_set_ct_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
548
1.28k
{
549
1.28k
    match->flow.ct_tp_dst = port & mask;
550
1.28k
    match->wc.masks.ct_tp_dst = mask;
551
1.28k
}
552
553
void
554
match_set_ct_ipv6_src(struct match *match, const struct in6_addr *src)
555
312
{
556
312
    match->flow.ct_ipv6_src = *src;
557
312
    match->wc.masks.ct_ipv6_src = in6addr_exact;
558
312
}
559
560
void
561
match_set_ct_ipv6_src_masked(struct match *match, const struct in6_addr *src,
562
                             const struct in6_addr *mask)
563
663
{
564
663
    match->flow.ct_ipv6_src = ipv6_addr_bitand(src, mask);
565
663
    match->wc.masks.ct_ipv6_src = *mask;
566
663
}
567
568
void
569
match_set_ct_ipv6_dst(struct match *match, const struct in6_addr *dst)
570
469
{
571
469
    match->flow.ct_ipv6_dst = *dst;
572
469
    match->wc.masks.ct_ipv6_dst = in6addr_exact;
573
469
}
574
575
void
576
match_set_ct_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
577
                             const struct in6_addr *mask)
578
1.47k
{
579
1.47k
    match->flow.ct_ipv6_dst = ipv6_addr_bitand(dst, mask);
580
1.47k
    match->wc.masks.ct_ipv6_dst = *mask;
581
1.47k
}
582
583
void
584
match_set_packet_type(struct match *match, ovs_be32 packet_type)
585
299k
{
586
299k
    match->flow.packet_type = packet_type;
587
299k
    match->wc.masks.packet_type = OVS_BE32_MAX;
588
299k
}
589
590
/* If 'match' does not match on any packet type, make it match on Ethernet
591
 * packets (the default packet type, as specified by OpenFlow). */
592
void
593
match_set_default_packet_type(struct match *match)
594
219k
{
595
219k
    if (!match->wc.masks.packet_type) {
596
128k
        match_set_packet_type(match, htonl(PT_ETH));
597
128k
    }
598
219k
}
599
600
/* Returns true if 'match' matches only Ethernet packets (the default packet
601
 * type, as specified by OpenFlow). */
602
bool
603
match_has_default_packet_type(const struct match *match)
604
298k
{
605
298k
    return (match->flow.packet_type == htonl(PT_ETH)
606
264k
            && match->wc.masks.packet_type == OVS_BE32_MAX);
607
298k
}
608
609
/* A match on 'field' is being added to or has been added to 'match'.  If
610
 * 'field' is a data field, and 'match' does not already match on packet_type,
611
 * this function make it match on the Ethernet packet_type.
612
 *
613
 * This function is useful because OpenFlow implicitly applies to Ethernet
614
 * packets when there's no explicit packet_type, but matching on a metadata
615
 * field doesn't imply anything about the packet_type and falsely inferring
616
 * that it does can cause harm.  A flow that matches only on metadata fields,
617
 * for example, should be able to match more than just Ethernet flows.  There
618
 * are also important reasons that a catch-all match (one with no field matches
619
 * at all) should not imply a packet_type(0,0) match.  For example, a "flow
620
 * dump" request that matches on no fields should return every flow in the
621
 * switch, not just the flows that match on Ethernet.  As a second example,
622
 * OpenFlow 1.2+ special-cases "table miss" flows, that is catch-all flows with
623
 * priority 0, and inferring a match on packet_type(0,0) causes such a flow not
624
 * to be a table miss flow.  */
625
void
626
match_add_ethernet_prereq(struct match *match, const struct mf_field *field)
627
367k
{
628
367k
    if (field->prereqs != MFP_NONE) {
629
95.7k
        match_set_default_packet_type(match);
630
95.7k
    }
631
367k
}
632
633
void
634
match_set_dl_type(struct match *match, ovs_be16 dl_type)
635
21.3k
{
636
21.3k
    match->wc.masks.dl_type = OVS_BE16_MAX;
637
21.3k
    match->flow.dl_type = dl_type;
638
21.3k
}
639
640
/* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
641
 * exactly.  'mask_dst' is set to all 1s. */
642
static void
643
set_eth(const struct eth_addr value_src,
644
        struct eth_addr *value_dst,
645
        struct eth_addr *mask_dst)
646
611
{
647
611
    *value_dst = value_src;
648
611
    *mask_dst = eth_addr_exact;
649
611
}
650
651
/* Modifies 'value_src' so that the Ethernet address must match 'value_src'
652
 * after each byte is ANDed with the appropriate byte in 'mask_src'.
653
 * 'mask_dst' is set to 'mask_src' */
654
static void
655
set_eth_masked(const struct eth_addr value_src,
656
               const struct eth_addr mask_src,
657
               struct eth_addr *value_dst, struct eth_addr *mask_dst)
658
21.3k
{
659
21.3k
    size_t i;
660
661
85.4k
    for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
662
64.0k
        value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
663
64.0k
    }
664
21.3k
    *mask_dst = mask_src;
665
21.3k
}
666
667
/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
668
 * exactly. */
669
void
670
match_set_dl_src(struct match *match, const struct eth_addr dl_src)
671
335
{
672
335
    set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
673
335
}
674
675
/* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
676
 * after each byte is ANDed with the appropriate byte in 'mask'. */
677
void
678
match_set_dl_src_masked(struct match *match,
679
                        const struct eth_addr dl_src,
680
                        const struct eth_addr mask)
681
10.1k
{
682
10.1k
    set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
683
10.1k
}
684
685
/* Modifies 'match' so that the Ethernet address must match 'dl_dst'
686
 * exactly. */
687
void
688
match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
689
276
{
690
276
    set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
691
276
}
692
693
/* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
694
 * byte is ANDed with the appropriate byte in 'mask'.
695
 *
696
 * This function will assert-fail if 'mask' is invalid.  Only 'mask' values
697
 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
698
void
699
match_set_dl_dst_masked(struct match *match,
700
                        const struct eth_addr dl_dst,
701
                        const struct eth_addr mask)
702
9.51k
{
703
9.51k
    set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
704
9.51k
}
705
706
void
707
match_set_dl_tci(struct match *match, ovs_be16 tci)
708
304
{
709
304
    match_set_dl_tci_masked(match, tci, htons(0xffff));
710
304
}
711
712
void
713
match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
714
2.86k
{
715
2.86k
    match->flow.vlans[0].tci = tci & mask;
716
2.86k
    match->wc.masks.vlans[0].tci = mask;
717
2.86k
}
718
719
/* Modifies 'match' so that the VLAN VID is wildcarded.  If the PCP is already
720
 * wildcarded, then 'match' will match a packet regardless of whether it has an
721
 * 802.1Q header or not. */
722
void
723
match_set_any_vid(struct match *match)
724
1.32k
{
725
1.32k
    if (match->wc.masks.vlans[0].tci & htons(VLAN_PCP_MASK)) {
726
744
        match->wc.masks.vlans[0].tci &= ~htons(VLAN_VID_MASK);
727
744
        match->flow.vlans[0].tci &= ~htons(VLAN_VID_MASK);
728
744
    } else {
729
579
        match_set_dl_tci_masked(match, htons(0), htons(0));
730
579
    }
731
1.32k
}
732
733
/* Modifies 'match' depending on 'dl_vlan':
734
 *
735
 *   - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
736
 *     without an 802.1Q header.
737
 *
738
 *   - Otherwise, makes 'match' match only packets with an 802.1Q header whose
739
 *     VID equals the low 12 bits of 'dl_vlan'.
740
 */
741
void
742
match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan, int id)
743
665
{
744
665
    flow_set_dl_vlan(&match->flow, dl_vlan, id);
745
665
    if (dl_vlan == htons(OFP10_VLAN_NONE)) {
746
254
        match->wc.masks.vlans[id].tci = OVS_BE16_MAX;
747
411
    } else {
748
411
        match->wc.masks.vlans[id].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
749
411
    }
750
665
}
751
752
/* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
753
 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
754
 * plus CFI). */
755
void
756
match_set_vlan_vid(struct match *match, ovs_be16 vid)
757
644
{
758
644
    match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
759
644
}
760
761
762
/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
763
 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
764
 * plus CFI), with the corresponding 'mask'. */
765
void
766
match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
767
1.00k
{
768
1.00k
    ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
769
1.00k
    ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
770
771
1.00k
    mask &= vid_mask;
772
1.00k
    flow_set_vlan_vid(&match->flow, vid & mask);
773
1.00k
    match->wc.masks.vlans[0].tci =
774
1.00k
        mask | (match->wc.masks.vlans[0].tci & pcp_mask);
775
1.00k
}
776
777
/* Modifies 'match' so that the VLAN PCP is wildcarded.  If the VID is already
778
 * wildcarded, then 'match' will match a packet regardless of whether it has an
779
 * 802.1Q header or not. */
780
void
781
match_set_any_pcp(struct match *match)
782
2.06k
{
783
2.06k
    if (match->wc.masks.vlans[0].tci & htons(VLAN_VID_MASK)) {
784
1.02k
        match->wc.masks.vlans[0].tci &= ~htons(VLAN_PCP_MASK);
785
1.02k
        match->flow.vlans[0].tci &= ~htons(VLAN_PCP_MASK);
786
1.03k
    } else {
787
1.03k
        match_set_dl_tci_masked(match, htons(0), htons(0));
788
1.03k
    }
789
2.06k
}
790
791
/* Modifies 'match' so that it matches only packets with an 802.1Q header whose
792
 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
793
void
794
match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp, int id)
795
1.21k
{
796
1.21k
    flow_set_vlan_pcp(&match->flow, dl_vlan_pcp, id);
797
1.21k
    match->wc.masks.vlans[id].tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
798
1.21k
}
799
800
/* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
801
void
802
match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
803
0
{
804
0
    match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
805
0
    match->flow.mpls_lse[idx] = lse;
806
0
}
807
808
/* Modifies 'match' so that the MPLS label is wildcarded. */
809
void
810
match_set_any_mpls_label(struct match *match, int idx)
811
1.84k
{
812
1.84k
    match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
813
1.84k
    flow_set_mpls_label(&match->flow, idx, htonl(0));
814
1.84k
}
815
816
/* Modifies 'match' so that it matches only packets with an MPLS header whose
817
 * label equals the low 20 bits of 'mpls_label'. */
818
void
819
match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
820
1.69k
{
821
1.69k
    match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
822
1.69k
    flow_set_mpls_label(&match->flow, idx, mpls_label);
823
1.69k
}
824
825
/* Modifies 'match' so that the MPLS TC is wildcarded. */
826
void
827
match_set_any_mpls_tc(struct match *match, int idx)
828
414
{
829
414
    match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
830
414
    flow_set_mpls_tc(&match->flow, idx, 0);
831
414
}
832
833
/* Modifies 'match' so that it matches only packets with an MPLS header whose
834
 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
835
void
836
match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
837
1.92k
{
838
1.92k
    match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
839
1.92k
    flow_set_mpls_tc(&match->flow, idx, mpls_tc);
840
1.92k
}
841
842
/* Modifies 'match' so that the MPLS stack flag is wildcarded. */
843
void
844
match_set_any_mpls_bos(struct match *match, int idx)
845
232
{
846
232
    match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
847
232
    flow_set_mpls_bos(&match->flow, idx, 0);
848
232
}
849
850
/* Modifies 'match' so that it matches only packets with an MPLS header whose
851
 * Stack Flag equals the lower bit of 'mpls_bos' */
852
void
853
match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
854
474
{
855
474
    match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
856
474
    flow_set_mpls_bos(&match->flow, idx, mpls_bos);
857
474
}
858
859
/* Modifies 'match' so that the TTL of MPLS label 'idx' is wildcarded. */
860
void
861
match_set_any_mpls_ttl(struct match *match, int idx)
862
843
{
863
843
    match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TTL_MASK);
864
843
    flow_set_mpls_ttl(&match->flow, idx, 0);
865
843
}
866
867
/* Modifies 'match' so that it matches only packets in which the TTL of MPLS
868
 * label 'idx' equals 'mpls_ttl'. */
869
void
870
match_set_mpls_ttl(struct match *match, int idx, uint8_t mpls_ttl)
871
750
{
872
750
    match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TTL_MASK);
873
750
    flow_set_mpls_ttl(&match->flow, idx, mpls_ttl);
874
750
}
875
876
/* Modifies 'match' so that the MPLS LSE is wildcarded. */
877
void
878
match_set_any_mpls_lse(struct match *match, int idx)
879
0
{
880
0
    match->wc.masks.mpls_lse[idx] = htonl(0);
881
0
    flow_set_mpls_lse(&match->flow, idx, htonl(0));
882
0
}
883
884
void
885
match_set_tp_src(struct match *match, ovs_be16 tp_src)
886
2.91k
{
887
2.91k
    match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
888
2.91k
}
889
890
void
891
match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
892
4.10k
{
893
4.10k
    match->flow.tp_src = port & mask;
894
4.10k
    match->wc.masks.tp_src = mask;
895
4.10k
}
896
897
void
898
match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
899
3.52k
{
900
3.52k
    match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
901
3.52k
}
902
903
void
904
match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
905
4.63k
{
906
4.63k
    match->flow.tp_dst = port & mask;
907
4.63k
    match->wc.masks.tp_dst = mask;
908
4.63k
}
909
910
void
911
match_set_tcp_flags(struct match *match, ovs_be16 flags)
912
847
{
913
847
    match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
914
847
}
915
916
void
917
match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
918
1.82k
{
919
1.82k
    match->flow.tcp_flags = flags & mask;
920
1.82k
    match->wc.masks.tcp_flags = mask;
921
1.82k
}
922
923
void
924
match_set_nw_proto(struct match *match, uint8_t nw_proto)
925
8.98k
{
926
8.98k
    match->flow.nw_proto = nw_proto;
927
8.98k
    match->wc.masks.nw_proto = UINT8_MAX;
928
8.98k
}
929
930
void
931
match_set_nw_proto_masked(struct match *match,
932
                          const uint8_t nw_proto, const uint8_t mask)
933
0
{
934
0
    match->flow.nw_proto = nw_proto;
935
0
    match->wc.masks.nw_proto = mask;
936
0
}
937
938
void
939
match_set_nw_src(struct match *match, ovs_be32 nw_src)
940
856
{
941
856
    match->flow.nw_src = nw_src;
942
856
    match->wc.masks.nw_src = OVS_BE32_MAX;
943
856
}
944
945
void
946
match_set_nw_src_masked(struct match *match,
947
                        ovs_be32 nw_src, ovs_be32 mask)
948
4.29k
{
949
4.29k
    match->flow.nw_src = nw_src & mask;
950
4.29k
    match->wc.masks.nw_src = mask;
951
4.29k
}
952
953
void
954
match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
955
2.04k
{
956
2.04k
    match->flow.nw_dst = nw_dst;
957
2.04k
    match->wc.masks.nw_dst = OVS_BE32_MAX;
958
2.04k
}
959
960
void
961
match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
962
5.56k
{
963
5.56k
    match->flow.nw_dst = ip & mask;
964
5.56k
    match->wc.masks.nw_dst = mask;
965
5.56k
}
966
967
void
968
match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
969
2.97k
{
970
2.97k
    match->wc.masks.nw_tos |= IP_DSCP_MASK;
971
2.97k
    match->flow.nw_tos &= ~IP_DSCP_MASK;
972
2.97k
    match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
973
2.97k
}
974
975
void
976
match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
977
463
{
978
463
    match->wc.masks.nw_tos |= IP_ECN_MASK;
979
463
    match->flow.nw_tos &= ~IP_ECN_MASK;
980
463
    match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
981
463
}
982
983
void
984
match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
985
689
{
986
689
    match->wc.masks.nw_ttl = UINT8_MAX;
987
689
    match->flow.nw_ttl = nw_ttl;
988
689
}
989
990
void
991
match_set_nw_tos_masked(struct match *match, uint8_t nw_tos, uint8_t mask)
992
0
{
993
0
    match->flow.nw_tos = nw_tos & mask;
994
0
    match->wc.masks.nw_tos = mask;
995
0
}
996
997
void
998
match_set_nw_ttl_masked(struct match *match, uint8_t nw_ttl, uint8_t mask)
999
0
{
1000
0
    match->flow.nw_ttl = nw_ttl & mask;
1001
0
    match->wc.masks.nw_ttl = mask;
1002
0
}
1003
1004
void
1005
match_set_nw_frag(struct match *match, uint8_t nw_frag)
1006
378
{
1007
378
    match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1008
378
    match->flow.nw_frag = nw_frag;
1009
378
}
1010
1011
void
1012
match_set_nw_frag_masked(struct match *match,
1013
                         uint8_t nw_frag, uint8_t mask)
1014
1.07k
{
1015
1.07k
    match->flow.nw_frag = nw_frag & mask;
1016
1.07k
    match->wc.masks.nw_frag = mask;
1017
1.07k
}
1018
1019
void
1020
match_set_icmp_type(struct match *match, uint8_t icmp_type)
1021
1.26k
{
1022
1.26k
    match_set_tp_src(match, htons(icmp_type));
1023
1.26k
}
1024
1025
void
1026
match_set_icmp_code(struct match *match, uint8_t icmp_code)
1027
1.04k
{
1028
1.04k
    match_set_tp_dst(match, htons(icmp_code));
1029
1.04k
}
1030
1031
void
1032
match_set_arp_opcode_masked(struct match *match,
1033
                            const uint8_t opcode,
1034
                            const uint8_t mask)
1035
0
{
1036
0
    match_set_nw_proto_masked(match, opcode, mask);
1037
0
}
1038
1039
void
1040
match_set_arp_spa_masked(struct match *match,
1041
                         const ovs_be32 arp_spa,
1042
                         const ovs_be32 mask)
1043
0
{
1044
0
    match_set_nw_src_masked(match, arp_spa, mask);
1045
0
}
1046
1047
void
1048
match_set_arp_tpa_masked(struct match *match,
1049
                         const ovs_be32 arp_tpa,
1050
                         const ovs_be32 mask)
1051
0
{
1052
0
    match_set_nw_dst_masked(match, arp_tpa, mask);
1053
0
}
1054
1055
void
1056
match_set_arp_sha(struct match *match, const struct eth_addr sha)
1057
556
{
1058
556
    match->flow.arp_sha = sha;
1059
556
    match->wc.masks.arp_sha = eth_addr_exact;
1060
556
}
1061
1062
void
1063
match_set_arp_sha_masked(struct match *match,
1064
                         const struct eth_addr arp_sha,
1065
                         const struct eth_addr mask)
1066
849
{
1067
849
    set_eth_masked(arp_sha, mask,
1068
849
                   &match->flow.arp_sha, &match->wc.masks.arp_sha);
1069
849
}
1070
1071
void
1072
match_set_arp_tha(struct match *match, const struct eth_addr tha)
1073
929
{
1074
929
    match->flow.arp_tha = tha;
1075
929
    match->wc.masks.arp_tha = eth_addr_exact;
1076
929
}
1077
1078
void
1079
match_set_arp_tha_masked(struct match *match,
1080
                         const struct eth_addr arp_tha,
1081
                         const struct eth_addr mask)
1082
871
{
1083
871
    set_eth_masked(arp_tha, mask,
1084
871
                   &match->flow.arp_tha, &match->wc.masks.arp_tha);
1085
871
}
1086
1087
void
1088
match_set_ipv6_src(struct match *match, const struct in6_addr *src)
1089
396
{
1090
396
    match->flow.ipv6_src = *src;
1091
396
    match->wc.masks.ipv6_src = in6addr_exact;
1092
396
}
1093
1094
void
1095
match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
1096
                          const struct in6_addr *mask)
1097
969
{
1098
969
    match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
1099
969
    match->wc.masks.ipv6_src = *mask;
1100
969
}
1101
1102
void
1103
match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
1104
303
{
1105
303
    match->flow.ipv6_dst = *dst;
1106
303
    match->wc.masks.ipv6_dst = in6addr_exact;
1107
303
}
1108
1109
void
1110
match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
1111
                          const struct in6_addr *mask)
1112
978
{
1113
978
    match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
1114
978
    match->wc.masks.ipv6_dst = *mask;
1115
978
}
1116
1117
void
1118
match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
1119
689
{
1120
689
    match->wc.masks.ipv6_label = OVS_BE32_MAX;
1121
689
    match->flow.ipv6_label = ipv6_label;
1122
689
}
1123
1124
1125
void
1126
match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
1127
                            ovs_be32 mask)
1128
288
{
1129
288
    match->flow.ipv6_label = ipv6_label & mask;
1130
288
    match->wc.masks.ipv6_label = mask;
1131
288
}
1132
1133
void
1134
match_set_nd_target(struct match *match, const struct in6_addr *target)
1135
332
{
1136
332
    match->flow.nd_target = *target;
1137
332
    match->wc.masks.nd_target = in6addr_exact;
1138
332
}
1139
1140
void
1141
match_set_nd_target_masked(struct match *match,
1142
                           const struct in6_addr *target,
1143
                           const struct in6_addr *mask)
1144
1.26k
{
1145
1.26k
    match->flow.nd_target = ipv6_addr_bitand(target, mask);
1146
1.26k
    match->wc.masks.nd_target = *mask;
1147
1.26k
}
1148
1149
void
1150
match_set_nd_reserved (struct match *match, ovs_be32 value)
1151
361
{
1152
361
   match->flow.igmp_group_ip4 = value;
1153
361
   match->wc.masks.igmp_group_ip4 = OVS_BE32_MAX;
1154
361
}
1155
1156
void
1157
match_set_nd_options_type(struct match *match, uint8_t option)
1158
300
{
1159
300
    match_set_tcp_flags(match, htons(option));
1160
300
}
1161
1162
/* Returns true if 'a' and 'b' wildcard the same fields and have the same
1163
 * values for fixed fields, otherwise false. */
1164
bool
1165
match_equal(const struct match *a, const struct match *b)
1166
0
{
1167
0
    return (flow_wildcards_equal(&a->wc, &b->wc)
1168
0
            && flow_equal(&a->flow, &b->flow));
1169
0
}
1170
1171
/* Returns a hash value for the flow and wildcards in 'match', starting from
1172
 * 'basis'. */
1173
uint32_t
1174
match_hash(const struct match *match, uint32_t basis)
1175
0
{
1176
0
    return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
1177
0
}
1178
1179
static bool
1180
match_has_default_recirc_id(const struct match *m)
1181
0
{
1182
0
    return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
1183
0
                                      m->wc.masks.recirc_id == 0);
1184
0
}
1185
1186
static bool
1187
match_has_default_dp_hash(const struct match *m)
1188
0
{
1189
0
    return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
1190
0
}
1191
1192
/* Return true if the hidden fields of the match are set to the default values.
1193
 * The default values equals to those set up by match_init_hidden_fields(). */
1194
bool
1195
match_has_default_hidden_fields(const struct match *m)
1196
0
{
1197
0
    return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
1198
0
}
1199
1200
void
1201
match_init_hidden_fields(struct match *m)
1202
0
{
1203
0
    match_set_recirc_id(m, 0);
1204
0
    match_set_dp_hash_masked(m, 0, 0);
1205
0
}
1206
1207
static void
1208
format_eth_masked(struct ds *s, const char *name,
1209
                  const struct eth_addr eth, const struct eth_addr mask)
1210
655k
{
1211
655k
    if (!eth_addr_is_zero(mask)) {
1212
248k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1213
248k
        eth_format_masked(eth, &mask, s);
1214
248k
        ds_put_char(s, ',');
1215
248k
    }
1216
655k
}
1217
1218
static void
1219
format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
1220
                  ovs_be32 netmask)
1221
1.83M
{
1222
1.83M
    if (netmask) {
1223
37.7k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1224
37.7k
        ip_format_masked(ip, netmask, s);
1225
37.7k
        ds_put_char(s, ',');
1226
37.7k
    }
1227
1.83M
}
1228
1229
static void
1230
format_ipv6_netmask(struct ds *s, const char *name,
1231
                    const struct in6_addr *addr,
1232
                    const struct in6_addr *netmask)
1233
1.35M
{
1234
1.35M
    if (!ipv6_mask_is_any(netmask)) {
1235
58.8k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1236
58.8k
        ipv6_format_masked(addr, netmask, s);
1237
58.8k
        ds_put_char(s, ',');
1238
58.8k
    }
1239
1.35M
}
1240
1241
static void
1242
format_uint8_masked(struct ds *s, const char *name,
1243
                   uint8_t value, uint8_t mask)
1244
113k
{
1245
113k
    if (mask != 0) {
1246
68.0k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1247
68.0k
        if (mask == UINT8_MAX) {
1248
68.0k
            ds_put_format(s, "%"PRIu8, value);
1249
68.0k
        } else {
1250
0
            ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask);
1251
0
        }
1252
68.0k
        ds_put_char(s, ',');
1253
68.0k
    }
1254
113k
}
1255
1256
static void
1257
format_uint16_masked(struct ds *s, const char *name,
1258
                   uint16_t value, uint16_t mask)
1259
2.37k
{
1260
2.37k
    if (mask != 0) {
1261
2.37k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1262
2.37k
        if (mask == UINT16_MAX) {
1263
2.37k
            ds_put_format(s, "%"PRIu16, value);
1264
2.37k
        } else {
1265
0
            ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
1266
0
        }
1267
2.37k
        ds_put_char(s, ',');
1268
2.37k
    }
1269
2.37k
}
1270
1271
static void
1272
format_be16_masked(struct ds *s, const char *name,
1273
                   ovs_be16 value, ovs_be16 mask)
1274
650k
{
1275
650k
    if (mask != htons(0)) {
1276
69.8k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1277
69.8k
        if (mask == OVS_BE16_MAX) {
1278
69.5k
            ds_put_format(s, "%"PRIu16, ntohs(value));
1279
69.5k
        } else {
1280
326
            ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
1281
326
                          ntohs(value), ntohs(mask));
1282
326
        }
1283
69.8k
        ds_put_char(s, ',');
1284
69.8k
    }
1285
650k
}
1286
1287
static void
1288
format_be32_masked(struct ds *s, const char *name,
1289
                   ovs_be32 value, ovs_be32 mask)
1290
646k
{
1291
646k
    if (mask != htonl(0)) {
1292
1.28k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1293
1.28k
        if (mask == OVS_BE32_MAX) {
1294
1.28k
            ds_put_format(s, "%"PRIu32, ntohl(value));
1295
1.28k
        } else {
1296
0
            ds_put_format(s, "0x%08"PRIx32"/0x%08"PRIx32,
1297
0
                          ntohl(value), ntohl(mask));
1298
0
        }
1299
1.28k
        ds_put_char(s, ',');
1300
1.28k
    }
1301
646k
}
1302
1303
static void
1304
format_be32_masked_hex(struct ds *s, const char *name,
1305
                       ovs_be32 value, ovs_be32 mask)
1306
23.3k
{
1307
23.3k
    if (mask != htonl(0)) {
1308
14.2k
        ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
1309
14.2k
        if (mask == OVS_BE32_MAX) {
1310
14.2k
            ds_put_format(s, "0x%"PRIx32, ntohl(value));
1311
14.2k
        } else {
1312
0
            ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
1313
0
                          ntohl(value), ntohl(mask));
1314
0
        }
1315
14.2k
        ds_put_char(s, ',');
1316
14.2k
    }
1317
23.3k
}
1318
1319
static void
1320
format_uint32_masked(struct ds *s, const char *name,
1321
                   uint32_t value, uint32_t mask)
1322
10.6M
{
1323
10.6M
    if (mask) {
1324
82.9k
        ds_put_format(s, "%s%s=%s%#"PRIx32,
1325
82.9k
                      colors.param, name, colors.end, value);
1326
82.9k
        if (mask != UINT32_MAX) {
1327
3.37k
            ds_put_format(s, "/%#"PRIx32, mask);
1328
3.37k
        }
1329
82.9k
        ds_put_char(s, ',');
1330
82.9k
    }
1331
10.6M
}
1332
1333
static void
1334
format_be64_masked(struct ds *s, const char *name,
1335
                   ovs_be64 value, ovs_be64 mask)
1336
646k
{
1337
646k
    if (mask != htonll(0)) {
1338
8.00k
        ds_put_format(s, "%s%s=%s%#"PRIx64,
1339
8.00k
                      colors.param, name, colors.end, ntohll(value));
1340
8.00k
        if (mask != OVS_BE64_MAX) {
1341
5.58k
            ds_put_format(s, "/%#"PRIx64, ntohll(mask));
1342
5.58k
        }
1343
8.00k
        ds_put_char(s, ',');
1344
8.00k
    }
1345
646k
}
1346
1347
static void
1348
format_flow_tunnel(struct ds *s, const struct match *match)
1349
323k
{
1350
323k
    const struct flow_wildcards *wc = &match->wc;
1351
323k
    const struct flow_tnl *tnl = &match->flow.tunnel;
1352
1353
323k
    format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
1354
323k
    format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
1355
323k
    format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
1356
323k
    format_ipv6_netmask(s, "tun_ipv6_src", &tnl->ipv6_src,
1357
323k
                        &wc->masks.tunnel.ipv6_src);
1358
323k
    format_ipv6_netmask(s, "tun_ipv6_dst", &tnl->ipv6_dst,
1359
323k
                        &wc->masks.tunnel.ipv6_dst);
1360
1361
323k
    if (wc->masks.tunnel.gbp_id) {
1362
27
        format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
1363
27
                           wc->masks.tunnel.gbp_id);
1364
27
    }
1365
1366
323k
    if (wc->masks.tunnel.gbp_flags) {
1367
195
        ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
1368
195
    }
1369
1370
323k
    if (wc->masks.tunnel.ip_tos) {
1371
0
        ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
1372
0
    }
1373
323k
    if (wc->masks.tunnel.ip_ttl) {
1374
0
        ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
1375
0
    }
1376
323k
    if (wc->masks.tunnel.erspan_ver) {
1377
165
        ds_put_format(s, "tun_erspan_ver=%"PRIu8",", tnl->erspan_ver);
1378
165
    }
1379
323k
    if (wc->masks.tunnel.erspan_idx && tnl->erspan_ver == 1) {
1380
0
       ds_put_format(s, "tun_erspan_idx=%#"PRIx32",", tnl->erspan_idx);
1381
0
    }
1382
323k
    if (wc->masks.tunnel.erspan_dir && tnl->erspan_ver == 2) {
1383
92
        ds_put_format(s, "tun_erspan_dir=%"PRIu8",", tnl->erspan_dir);
1384
92
    }
1385
323k
    if (wc->masks.tunnel.erspan_hwid && tnl->erspan_ver == 2) {
1386
66
        ds_put_format(s, "tun_erspan_hwid=%#"PRIx8",", tnl->erspan_hwid);
1387
66
    }
1388
323k
    if (wc->masks.tunnel.gtpu_flags) {
1389
195
        ds_put_format(s, "gtpu_flags=%#"PRIx8",", tnl->gtpu_flags);
1390
195
    }
1391
323k
    if (wc->masks.tunnel.gtpu_msgtype) {
1392
34
        ds_put_format(s, "gtpu_msgtype=%"PRIu8",", tnl->gtpu_msgtype);
1393
34
    }
1394
323k
    if (wc->masks.tunnel.flags & FLOW_TNL_F_MASK) {
1395
372
        format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
1396
372
                            tnl->flags & FLOW_TNL_F_MASK,
1397
372
                            wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
1398
372
                            FLOW_TNL_F_MASK);
1399
372
        ds_put_char(s, ',');
1400
372
    }
1401
323k
    tun_metadata_match_format(s, match);
1402
323k
}
1403
1404
static void
1405
format_ct_label_masked(struct ds *s, const ovs_u128 *key, const ovs_u128 *mask)
1406
2.96k
{
1407
2.96k
    if (!ovs_u128_is_zero(*mask)) {
1408
2.96k
        ovs_be128 value = hton128(*key);
1409
2.96k
        ds_put_format(s, "%sct_label=%s", colors.param, colors.end);
1410
2.96k
        ds_put_hex(s, &value, sizeof value);
1411
2.96k
        if (!is_all_ones(mask, sizeof(*mask))) {
1412
630
            value = hton128(*mask);
1413
630
            ds_put_char(s, '/');
1414
630
            ds_put_hex(s, &value, sizeof value);
1415
630
        }
1416
2.96k
        ds_put_char(s, ',');
1417
2.96k
    }
1418
2.96k
}
1419
1420
static void
1421
format_nsh_masked(struct ds *s, const struct flow *f, const struct flow *m)
1422
22.6k
{
1423
22.6k
    ovs_be32 spi_mask = nsh_path_hdr_to_spi(m->nsh.path_hdr);
1424
22.6k
    if (spi_mask == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
1425
13.6k
        spi_mask = OVS_BE32_MAX;
1426
13.6k
    }
1427
22.6k
    format_uint8_masked(s, "nsh_flags", f->nsh.flags, m->nsh.flags);
1428
22.6k
    format_uint8_masked(s, "nsh_ttl", f->nsh.ttl, m->nsh.ttl);
1429
22.6k
    format_uint8_masked(s, "nsh_mdtype", f->nsh.mdtype, m->nsh.mdtype);
1430
22.6k
    format_uint8_masked(s, "nsh_np", f->nsh.np, m->nsh.np);
1431
1432
22.6k
    format_be32_masked_hex(s, "nsh_spi", nsh_path_hdr_to_spi(f->nsh.path_hdr),
1433
22.6k
                           spi_mask);
1434
22.6k
    format_uint8_masked(s, "nsh_si", nsh_path_hdr_to_si(f->nsh.path_hdr),
1435
22.6k
                        nsh_path_hdr_to_si(m->nsh.path_hdr));
1436
22.6k
    if (m->nsh.mdtype == UINT8_MAX && f->nsh.mdtype == NSH_M_TYPE1) {
1437
167
        format_be32_masked_hex(s, "nsh_c1", f->nsh.context[0],
1438
167
                               m->nsh.context[0]);
1439
167
        format_be32_masked_hex(s, "nsh_c2", f->nsh.context[1],
1440
167
                               m->nsh.context[1]);
1441
167
        format_be32_masked_hex(s, "nsh_c3", f->nsh.context[2],
1442
167
                               m->nsh.context[2]);
1443
167
        format_be32_masked_hex(s, "nsh_c4", f->nsh.context[3],
1444
167
                               m->nsh.context[3]);
1445
167
    }
1446
22.6k
}
1447
1448
/* Appends a string representation of 'match' to 's'.  If 'priority' is
1449
 * different from OFP_DEFAULT_PRIORITY, includes it in 's'.  If 'port_map' is
1450
 * nonnull, uses it to translate port numbers to names in output. */
1451
void
1452
match_format(const struct match *match,
1453
             const struct ofputil_port_map *port_map,
1454
             struct ds *s, int priority)
1455
323k
{
1456
323k
    const struct flow_wildcards *wc = &match->wc;
1457
323k
    size_t start_len = s->length;
1458
323k
    const struct flow *f = &match->flow;
1459
323k
    bool skip_type = false;
1460
323k
    bool skip_proto = false;
1461
323k
    ovs_be16 dl_type = f->dl_type;
1462
323k
    bool is_megaflow = false;
1463
323k
    int i;
1464
1465
323k
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
1466
1467
323k
    if (priority != OFP_DEFAULT_PRIORITY) {
1468
71.4k
        ds_put_format(s, "%spriority=%s%d,",
1469
71.4k
                      colors.special, colors.end, priority);
1470
71.4k
    }
1471
1472
323k
    format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
1473
1474
323k
    if (wc->masks.recirc_id) {
1475
2.59k
        format_uint32_masked(s, "recirc_id", f->recirc_id,
1476
2.59k
                             wc->masks.recirc_id);
1477
2.59k
        is_megaflow = true;
1478
2.59k
    }
1479
1480
323k
    if (wc->masks.dp_hash) {
1481
2.55k
        format_uint32_masked(s, "dp_hash", f->dp_hash,
1482
2.55k
                             wc->masks.dp_hash);
1483
2.55k
    }
1484
1485
323k
    if (wc->masks.conj_id) {
1486
34
        ds_put_format(s, "%sconj_id%s=%"PRIu32",",
1487
34
                      colors.param, colors.end, f->conj_id);
1488
34
    }
1489
1490
323k
    if (wc->masks.skb_priority) {
1491
2.10k
        ds_put_format(s, "%sskb_priority=%s%#"PRIx32",",
1492
2.10k
                      colors.param, colors.end, f->skb_priority);
1493
2.10k
    }
1494
1495
323k
    if (wc->masks.actset_output) {
1496
876
        ds_put_format(s, "%sactset_output=%s", colors.param, colors.end);
1497
876
        ofputil_format_port(f->actset_output, port_map, s);
1498
876
        ds_put_char(s, ',');
1499
876
    }
1500
1501
323k
    if (wc->masks.ct_state) {
1502
2.55k
        if (wc->masks.ct_state == UINT8_MAX) {
1503
2.35k
            ds_put_format(s, "%sct_state=%s", colors.param, colors.end);
1504
2.35k
            if (f->ct_state) {
1505
226
                format_flags(s, ct_state_to_string, f->ct_state, '|');
1506
2.12k
            } else {
1507
2.12k
                ds_put_cstr(s, "0"); /* No state. */
1508
2.12k
            }
1509
2.35k
        } else {
1510
206
            format_flags_masked(s, "ct_state", ct_state_to_string,
1511
206
                                f->ct_state, wc->masks.ct_state, UINT8_MAX);
1512
206
        }
1513
2.55k
        ds_put_char(s, ',');
1514
2.55k
    }
1515
1516
323k
    if (wc->masks.ct_zone) {
1517
2.37k
        format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1518
2.37k
    }
1519
1520
323k
    if (wc->masks.ct_mark) {
1521
2.56k
        format_uint32_masked(s, "ct_mark", f->ct_mark, wc->masks.ct_mark);
1522
2.56k
    }
1523
1524
323k
    if (!ovs_u128_is_zero(wc->masks.ct_label)) {
1525
2.96k
        format_ct_label_masked(s, &f->ct_label, &wc->masks.ct_label);
1526
2.96k
    }
1527
1528
323k
    format_ip_netmask(s, "ct_nw_src", f->ct_nw_src,
1529
323k
                      wc->masks.ct_nw_src);
1530
323k
    format_ipv6_netmask(s, "ct_ipv6_src", &f->ct_ipv6_src,
1531
323k
                        &wc->masks.ct_ipv6_src);
1532
323k
    format_ip_netmask(s, "ct_nw_dst", f->ct_nw_dst,
1533
323k
                      wc->masks.ct_nw_dst);
1534
323k
    format_ipv6_netmask(s, "ct_ipv6_dst", &f->ct_ipv6_dst,
1535
323k
                        &wc->masks.ct_ipv6_dst);
1536
323k
    if (wc->masks.ct_nw_proto) {
1537
1.71k
        ds_put_format(s, "%sct_nw_proto=%s%"PRIu8",",
1538
1.71k
                      colors.param, colors.end, f->ct_nw_proto);
1539
1.71k
        format_be16_masked(s, "ct_tp_src", f->ct_tp_src, wc->masks.ct_tp_src);
1540
1.71k
        format_be16_masked(s, "ct_tp_dst", f->ct_tp_dst, wc->masks.ct_tp_dst);
1541
1.71k
    }
1542
1543
323k
    if (wc->masks.packet_type &&
1544
203k
        (!match_has_default_packet_type(match) || is_megaflow)) {
1545
34.3k
        format_packet_type_masked(s, f->packet_type, wc->masks.packet_type);
1546
34.3k
        ds_put_char(s, ',');
1547
34.3k
        if (pt_ns(f->packet_type) == OFPHTN_ETHERTYPE) {
1548
21.7k
            dl_type = pt_ns_type_be(f->packet_type);
1549
21.7k
        }
1550
34.3k
    }
1551
1552
323k
    if (wc->masks.dl_type) {
1553
127k
        skip_type = true;
1554
127k
        if (dl_type == htons(ETH_TYPE_IP)) {
1555
16.3k
            if (wc->masks.nw_proto) {
1556
12.5k
                skip_proto = true;
1557
12.5k
                if (f->nw_proto == IPPROTO_ICMP) {
1558
1.02k
                    ds_put_format(s, "%sicmp%s,", colors.value, colors.end);
1559
11.4k
                } else if (f->nw_proto == IPPROTO_TCP) {
1560
3.53k
                    ds_put_format(s, "%stcp%s,", colors.value, colors.end);
1561
7.96k
                } else if (f->nw_proto == IPPROTO_UDP) {
1562
1.27k
                    ds_put_format(s, "%sudp%s,", colors.value, colors.end);
1563
6.69k
                } else if (f->nw_proto == IPPROTO_SCTP) {
1564
1.93k
                    ds_put_format(s, "%ssctp%s,", colors.value, colors.end);
1565
4.75k
                } else {
1566
4.75k
                    ds_put_format(s, "%sip%s,", colors.value, colors.end);
1567
4.75k
                    skip_proto = false;
1568
4.75k
                }
1569
12.5k
            } else {
1570
3.87k
                ds_put_format(s, "%sip%s,", colors.value, colors.end);
1571
3.87k
            }
1572
110k
        } else if (dl_type == htons(ETH_TYPE_IPV6)) {
1573
27.7k
            if (wc->masks.nw_proto) {
1574
27.6k
                skip_proto = true;
1575
27.6k
                if (f->nw_proto == IPPROTO_ICMPV6) {
1576
1.30k
                    ds_put_format(s, "%sicmp6%s,", colors.value, colors.end);
1577
26.3k
                } else if (f->nw_proto == IPPROTO_TCP) {
1578
12.1k
                    ds_put_format(s, "%stcp6%s,", colors.value, colors.end);
1579
14.2k
                } else if (f->nw_proto == IPPROTO_UDP) {
1580
900
                    ds_put_format(s, "%sudp6%s,", colors.value, colors.end);
1581
13.3k
                } else if (f->nw_proto == IPPROTO_SCTP) {
1582
1.25k
                    ds_put_format(s, "%ssctp6%s,", colors.value, colors.end);
1583
12.0k
                } else {
1584
12.0k
                    ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
1585
12.0k
                    skip_proto = false;
1586
12.0k
                }
1587
27.6k
            } else {
1588
131
                ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
1589
131
            }
1590
83.0k
        } else if (dl_type == htons(ETH_TYPE_ARP)) {
1591
1.99k
            ds_put_format(s, "%sarp%s,", colors.value, colors.end);
1592
81.0k
        } else if (dl_type == htons(ETH_TYPE_RARP)) {
1593
1.31k
            ds_put_format(s, "%srarp%s,", colors.value, colors.end);
1594
79.7k
        } else if (dl_type == htons(ETH_TYPE_MPLS)) {
1595
992
            ds_put_format(s, "%smpls%s,", colors.value, colors.end);
1596
78.7k
        } else if (dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
1597
1.20k
            ds_put_format(s, "%smplsm%s,", colors.value, colors.end);
1598
77.5k
        } else {
1599
77.5k
            skip_type = false;
1600
77.5k
        }
1601
127k
    }
1602
10.6M
    for (i = 0; i < FLOW_N_REGS; i++) {
1603
20.6M
        #define REGNAME_LEN 20
1604
10.3M
        char regname[REGNAME_LEN];
1605
10.3M
        if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1606
0
            strcpy(regname, "reg?");
1607
0
        }
1608
10.3M
        format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
1609
10.3M
    }
1610
1611
323k
    format_flow_tunnel(s, match);
1612
1613
323k
    format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1614
1615
323k
    if (wc->masks.in_port.ofp_port) {
1616
106k
        ds_put_format(s, "%sin_port=%s", colors.param, colors.end);
1617
106k
        ofputil_format_port(f->in_port.ofp_port, port_map, s);
1618
106k
        ds_put_char(s, ',');
1619
106k
    }
1620
453k
    for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
1621
452k
        char str_i[12];
1622
1623
452k
        if (!wc->masks.vlans[i].tci) {
1624
322k
            break;
1625
322k
        }
1626
1627
        /* Print VLAN tags as dl_vlan, dl_vlan1, dl_vlan2 ... */
1628
129k
        if (i == 0) {
1629
128k
            str_i[0] = '\0';
1630
128k
        } else {
1631
1.21k
            snprintf(str_i, sizeof(str_i), "%d", i);
1632
1.21k
        }
1633
129k
        ovs_be16 vid_mask = wc->masks.vlans[i].tci & htons(VLAN_VID_MASK);
1634
129k
        ovs_be16 pcp_mask = wc->masks.vlans[i].tci & htons(VLAN_PCP_MASK);
1635
129k
        ovs_be16 cfi = wc->masks.vlans[i].tci & htons(VLAN_CFI);
1636
1637
129k
        if (cfi && f->vlans[i].tci & htons(VLAN_CFI)
1638
37.5k
            && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1639
37.3k
            && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1640
37.3k
            && (vid_mask || pcp_mask)) {
1641
36.3k
            if (vid_mask) {
1642
26.4k
                ds_put_format(s, "%sdl_vlan%s=%s%"PRIu16",",
1643
26.4k
                              colors.param, str_i, colors.end,
1644
26.4k
                              vlan_tci_to_vid(f->vlans[i].tci));
1645
26.4k
            }
1646
36.3k
            if (pcp_mask) {
1647
28.9k
                ds_put_format(s, "%sdl_vlan_pcp%s=%s%d,",
1648
28.9k
                              colors.param, str_i, colors.end,
1649
28.9k
                              vlan_tci_to_pcp(f->vlans[i].tci));
1650
28.9k
            }
1651
93.5k
        } else if (wc->masks.vlans[i].tci == htons(0xffff)) {
1652
91.9k
            ds_put_format(s, "%svlan_tci%s=%s0x%04"PRIx16",",
1653
91.9k
                          colors.param, str_i, colors.end,
1654
91.9k
                          ntohs(f->vlans[i].tci));
1655
91.9k
        } else {
1656
1.62k
            ds_put_format(s, "%svlan_tci%s=%s0x%04"PRIx16"/0x%04"PRIx16",",
1657
1.62k
                          colors.param, str_i, colors.end,
1658
1.62k
                          ntohs(f->vlans[i].tci),
1659
1.62k
                          ntohs(wc->masks.vlans[i].tci));
1660
1.62k
        }
1661
129k
    }
1662
1663
323k
    format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1664
323k
    format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1665
1666
323k
    if (!skip_type && wc->masks.dl_type) {
1667
77.5k
        ds_put_format(s, "%sdl_type=%s0x%04"PRIx16",",
1668
77.5k
                      colors.param, colors.end, ntohs(dl_type));
1669
77.5k
    }
1670
323k
    if (dl_type == htons(ETH_TYPE_IPV6)) {
1671
28.7k
        format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1672
28.7k
        format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1673
28.7k
        if (wc->masks.ipv6_label) {
1674
27.3k
            if (wc->masks.ipv6_label == OVS_BE32_MAX) {
1675
27.3k
                ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32",",
1676
27.3k
                              colors.param, colors.end,
1677
27.3k
                              ntohl(f->ipv6_label));
1678
27.3k
            } else {
1679
0
                ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32"/0x%05"PRIx32",",
1680
0
                              colors.param, colors.end, ntohl(f->ipv6_label),
1681
0
                              ntohl(wc->masks.ipv6_label));
1682
0
            }
1683
27.3k
        }
1684
294k
    } else if (dl_type == htons(ETH_TYPE_ARP) ||
1685
292k
               dl_type == htons(ETH_TYPE_RARP)) {
1686
3.31k
        format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1687
3.31k
        format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1688
291k
    } else if (dl_type == htons(ETH_TYPE_NSH)) {
1689
22.6k
        format_nsh_masked(s, f, &wc->masks);
1690
268k
    } else {
1691
268k
        format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1692
268k
        format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1693
268k
    }
1694
323k
    if (!skip_proto && wc->masks.nw_proto) {
1695
36.2k
        if (dl_type == htons(ETH_TYPE_ARP) ||
1696
34.3k
            dl_type == htons(ETH_TYPE_RARP)) {
1697
3.29k
            ds_put_format(s, "%sarp_op=%s%"PRIu8",",
1698
3.29k
                          colors.param, colors.end, f->nw_proto);
1699
33.0k
        } else {
1700
33.0k
            ds_put_format(s, "%snw_proto=%s%"PRIu8",",
1701
33.0k
                          colors.param, colors.end, f->nw_proto);
1702
33.0k
        }
1703
36.2k
    }
1704
323k
    if (dl_type == htons(ETH_TYPE_ARP) ||
1705
321k
        dl_type == htons(ETH_TYPE_RARP)) {
1706
3.31k
        format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1707
3.31k
        format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1708
3.31k
    }
1709
323k
    if (wc->masks.nw_tos & IP_DSCP_MASK) {
1710
57.8k
        ds_put_format(s, "%snw_tos=%s%d,",
1711
57.8k
                      colors.param, colors.end, f->nw_tos & IP_DSCP_MASK);
1712
57.8k
    }
1713
323k
    if (wc->masks.nw_tos & IP_ECN_MASK) {
1714
46.1k
        ds_put_format(s, "%snw_ecn=%s%d,",
1715
46.1k
                      colors.param, colors.end, f->nw_tos & IP_ECN_MASK);
1716
46.1k
    }
1717
323k
    if (wc->masks.nw_ttl) {
1718
46.1k
        ds_put_format(s, "%snw_ttl=%s%d,",
1719
46.1k
                      colors.param, colors.end, f->nw_ttl);
1720
46.1k
    }
1721
323k
    if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1722
2.31k
        ds_put_format(s, "%smpls_label=%s%"PRIu32",", colors.param,
1723
2.31k
                      colors.end, mpls_lse_to_label(f->mpls_lse[0]));
1724
2.31k
    }
1725
323k
    if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1726
1.94k
        ds_put_format(s, "%smpls_tc=%s%"PRIu8",", colors.param, colors.end,
1727
1.94k
                      mpls_lse_to_tc(f->mpls_lse[0]));
1728
1.94k
    }
1729
323k
    if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1730
984
        ds_put_format(s, "%smpls_ttl=%s%"PRIu8",", colors.param, colors.end,
1731
984
                      mpls_lse_to_ttl(f->mpls_lse[0]));
1732
984
    }
1733
323k
    if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1734
1.20k
        ds_put_format(s, "%smpls_bos=%s%"PRIu8",", colors.param, colors.end,
1735
1.20k
                      mpls_lse_to_bos(f->mpls_lse[0]));
1736
1.20k
    }
1737
323k
    format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1738
323k
    format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1739
1740
323k
    switch (wc->masks.nw_frag & FLOW_NW_FRAG_MASK) {
1741
46.2k
    case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1742
46.2k
        ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
1743
46.2k
                      f->nw_frag & FLOW_NW_FRAG_ANY
1744
46.2k
                      ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1745
46.2k
                      : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1746
46.2k
        break;
1747
1748
130
    case FLOW_NW_FRAG_ANY:
1749
130
        ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
1750
130
                      f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1751
130
        break;
1752
1753
251
    case FLOW_NW_FRAG_LATER:
1754
251
        ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
1755
251
                      f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1756
251
        break;
1757
323k
    }
1758
323k
    if (dl_type == htons(ETH_TYPE_IP) &&
1759
16.4k
        f->nw_proto == IPPROTO_ICMP) {
1760
1.02k
        format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1761
1.02k
        format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1762
322k
    } else if (dl_type == htons(ETH_TYPE_IPV6) &&
1763
28.7k
               f->nw_proto == IPPROTO_ICMPV6) {
1764
1.30k
        format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1765
1.30k
        format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1766
1.30k
        format_ipv6_netmask(s, "nd_target", &f->nd_target,
1767
1.30k
                            &wc->masks.nd_target);
1768
1.30k
        format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1769
1.30k
        format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1770
1.30k
        if (wc->masks.igmp_group_ip4) {
1771
0
            format_be32_masked(s,"nd_reserved", f->igmp_group_ip4,
1772
0
                               wc->masks.igmp_group_ip4);
1773
0
        }
1774
1.30k
        if (wc->masks.tcp_flags) {
1775
0
            format_be16_masked(s,"nd_options_type", f->tcp_flags,
1776
0
                               wc->masks.tcp_flags);
1777
0
        }
1778
321k
    } else {
1779
321k
        format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1780
321k
        format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1781
321k
    }
1782
323k
    if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1783
14.1k
        format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1784
14.1k
                            ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1785
14.1k
                            TCP_FLAGS(OVS_BE16_MAX));
1786
14.1k
    }
1787
1788
323k
    if (s->length > start_len) {
1789
285k
        ds_chomp(s, ',');
1790
285k
    }
1791
323k
}
1792
1793
/* Converts 'match' to a string and returns the string.  If 'priority' is
1794
 * different from OFP_DEFAULT_PRIORITY, includes it in the string.  If
1795
 * 'port_map' is nonnull, uses it to translate port numbers to names in
1796
 * output.  The caller must free the string (with free()). */
1797
char *
1798
match_to_string(const struct match *match,
1799
                const struct ofputil_port_map *port_map, int priority)
1800
2.10k
{
1801
2.10k
    struct ds s = DS_EMPTY_INITIALIZER;
1802
2.10k
    match_format(match, port_map, &s, priority);
1803
2.10k
    return ds_steal_cstr(&s);
1804
2.10k
}
1805
1806
void
1807
match_print(const struct match *match,
1808
            const struct ofputil_port_map *port_map)
1809
2.10k
{
1810
2.10k
    char *s = match_to_string(match, port_map, OFP_DEFAULT_PRIORITY);
1811
2.10k
    puts(s);
1812
2.10k
    free(s);
1813
2.10k
}
1814

1815
/* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1816
 * with minimatch_destroy(). */
1817
void
1818
minimatch_init(struct minimatch *dst, const struct match *src)
1819
68.5k
{
1820
68.5k
    struct miniflow tmp;
1821
1822
68.5k
    miniflow_map_init(&tmp, &src->wc.masks);
1823
    /* Allocate two consecutive miniflows. */
1824
68.5k
    miniflow_alloc(dst->flows, 2, &tmp);
1825
68.5k
    miniflow_init(dst->flow, &src->flow);
1826
68.5k
    minimask_init(dst->mask, &src->wc);
1827
1828
68.5k
    dst->tun_md = tun_metadata_allocation_clone(&src->tun_md);
1829
68.5k
}
1830
1831
/* Initializes 'match' as a "catch-all" match that matches every packet. */
1832
void
1833
minimatch_init_catchall(struct minimatch *match)
1834
0
{
1835
0
    match->flows[0] = xcalloc(2, sizeof *match->flow);
1836
0
    match->flows[1] = match->flows[0] + 1;
1837
0
    match->tun_md = NULL;
1838
0
}
1839
1840
/* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1841
 * with minimatch_destroy(). */
1842
void
1843
minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1844
0
{
1845
    /* Allocate two consecutive miniflows. */
1846
0
    size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1847
1848
0
    memcpy(miniflow_values(dst->flow),
1849
0
           miniflow_get_values(src->flow), data_size);
1850
0
    memcpy(miniflow_values(&dst->mask->masks),
1851
0
           miniflow_get_values(&src->mask->masks), data_size);
1852
0
    dst->tun_md = tun_metadata_allocation_clone(src->tun_md);
1853
0
}
1854
1855
/* Initializes 'dst' with the data in 'src', destroying 'src'.  The caller must
1856
 * eventually free 'dst' with minimatch_destroy(). */
1857
void
1858
minimatch_move(struct minimatch *dst, struct minimatch *src)
1859
0
{
1860
0
    dst->flow = src->flow;
1861
0
    dst->mask = src->mask;
1862
0
    dst->tun_md = src->tun_md;
1863
0
}
1864
1865
/* Frees any memory owned by 'match'.  Does not free the storage in which
1866
 * 'match' itself resides; the caller is responsible for that. */
1867
void
1868
minimatch_destroy(struct minimatch *match)
1869
68.5k
{
1870
68.5k
    free(match->flow);
1871
68.5k
    free(match->tun_md);
1872
68.5k
}
1873
1874
/* Initializes 'dst' as a copy of 'src'. */
1875
void
1876
minimatch_expand(const struct minimatch *src, struct match *dst)
1877
78.7k
{
1878
78.7k
    miniflow_expand(src->flow, &dst->flow);
1879
78.7k
    minimask_expand(src->mask, &dst->wc);
1880
78.7k
    tun_metadata_allocation_copy(&dst->tun_md, src->tun_md);
1881
78.7k
}
1882
1883
/* Returns true if 'a' and 'b' match the same packets, false otherwise.  */
1884
bool
1885
minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1886
0
{
1887
0
    return minimask_equal(a->mask, b->mask)
1888
0
        && miniflow_equal(a->flow, b->flow);
1889
0
}
1890
1891
/* Returns a hash value for the flow and wildcards in 'match', starting from
1892
 * 'basis'. */
1893
uint32_t
1894
minimatch_hash(const struct minimatch *match, uint32_t basis)
1895
0
{
1896
0
    size_t n_values = miniflow_n_values(match->flow);
1897
0
    size_t flow_size = sizeof *match->flow + MINIFLOW_VALUES_SIZE(n_values);
1898
0
    return hash_bytes(match->flow, 2 * flow_size, basis);
1899
0
}
1900
1901
/* Returns true if 'target' satisifies 'match', that is, if each bit for which
1902
 * 'match' specifies a particular value has the correct value in 'target'.
1903
 *
1904
 * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1905
 * target, &match->mask) but it is faster because of the invariant that
1906
 * match->flow.map and match->mask.map are the same. */
1907
bool
1908
minimatch_matches_flow(const struct minimatch *match,
1909
                       const struct flow *target)
1910
0
{
1911
0
    const uint64_t *flowp = miniflow_get_values(match->flow);
1912
0
    const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
1913
0
    size_t idx;
1914
1915
0
    FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1916
0
        if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
1917
0
            return false;
1918
0
        }
1919
0
    }
1920
1921
0
    return true;
1922
0
}
1923
1924
/* Appends a string representation of 'match' to 's'.  If 'priority' is
1925
 * different from OFP_DEFAULT_PRIORITY, includes it in 's'.  If 'port_map' is
1926
 * nonnull, uses it to translate port numbers to names in output. */
1927
void
1928
minimatch_format(const struct minimatch *match,
1929
                 const struct tun_table *tun_table,
1930
                 const struct ofputil_port_map *port_map,
1931
                 struct ds *s, int priority)
1932
0
{
1933
0
    struct match megamatch;
1934
1935
0
    minimatch_expand(match, &megamatch);
1936
0
    megamatch.flow.tunnel.metadata.tab = tun_table;
1937
1938
0
    match_format(&megamatch, port_map, s, priority);
1939
0
}
1940
1941
/* Converts 'match' to a string and returns the string.  If 'priority' is
1942
 * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1943
 * must free the string (with free()).  If 'port_map' is nonnull, uses it to
1944
 * translate port numbers to names in output. */
1945
char *
1946
minimatch_to_string(const struct minimatch *match,
1947
                    const struct ofputil_port_map *port_map, int priority)
1948
0
{
1949
0
    struct match megamatch;
1950
1951
0
    minimatch_expand(match, &megamatch);
1952
0
    return match_to_string(&megamatch, port_map, priority);
1953
0
}
1954
1955
static bool
1956
minimatch_has_default_recirc_id(const struct minimatch *m)
1957
0
{
1958
0
    uint32_t flow_recirc_id = miniflow_get_recirc_id(m->flow);
1959
0
    uint32_t mask_recirc_id = miniflow_get_recirc_id(&m->mask->masks);
1960
0
    return flow_recirc_id == 0 && (mask_recirc_id == UINT32_MAX ||
1961
0
                                   mask_recirc_id == 0);
1962
0
}
1963
1964
static bool
1965
minimatch_has_default_dp_hash(const struct minimatch *m)
1966
0
{
1967
0
    return (!miniflow_get_dp_hash(m->flow)
1968
0
            && !miniflow_get_dp_hash(&m->mask->masks));
1969
0
}
1970
1971
/* Return true if the hidden fields of the match are set to the default values.
1972
 * The default values equals to those set up by match_init_hidden_fields(). */
1973
bool
1974
minimatch_has_default_hidden_fields(const struct minimatch *m)
1975
0
{
1976
0
    return (minimatch_has_default_recirc_id(m)
1977
0
            && minimatch_has_default_dp_hash(m));
1978
0
}