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