76ccb43881da4fb39b7cc104477421d1fbdcc461
[cascardo/ovs.git] / lib / match.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 "match.h"
19 #include <stdlib.h>
20 #include "byte-order.h"
21 #include "dynamic-string.h"
22 #include "ofp-util.h"
23 #include "packets.h"
24
25 /* Converts the flow in 'flow' into a match in 'match', with the given
26  * 'wildcards'. */
27 void
28 match_init(struct match *match,
29            const struct flow *flow, const struct flow_wildcards *wc)
30 {
31     match->flow = *flow;
32     match->wc = *wc;
33     match_zero_wildcarded_fields(match);
34 }
35
36 /* Converts a flow into a match.  It sets the wildcard masks based on
37  * the packet contents.  It will not set the mask for fields that do not
38  * make sense for the packet type. */
39 void
40 match_wc_init(struct match *match, const struct flow *flow)
41 {
42     match->flow = *flow;
43
44     flow_wildcards_init_for_packet(&match->wc, flow);
45     WC_MASK_FIELD(&match->wc, regs);
46     WC_MASK_FIELD(&match->wc, metadata);
47 }
48
49 /* Initializes 'match' as a "catch-all" match that matches every packet. */
50 void
51 match_init_catchall(struct match *match)
52 {
53     memset(&match->flow, 0, sizeof match->flow);
54     flow_wildcards_init_catchall(&match->wc);
55 }
56
57 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
58  * field in 'flow' to all-0-bits.  It is important to maintain this invariant
59  * in a match that might be inserted into a classifier.
60  *
61  * It is never necessary to call this function directly for a match that is
62  * initialized or modified only by match_*() functions.  It is useful to
63  * restore the invariant in a match whose 'wc' member is modified by hand.
64  */
65 void
66 match_zero_wildcarded_fields(struct match *match)
67 {
68     flow_zero_wildcards(&match->flow, &match->wc);
69 }
70
71 void
72 match_set_dp_hash(struct match *match, uint32_t value)
73 {
74     match_set_dp_hash_masked(match, value, UINT32_MAX);
75 }
76
77 void
78 match_set_dp_hash_masked(struct match *match, uint32_t value, uint32_t mask)
79 {
80     match->wc.masks.dp_hash = mask;
81     match->flow.dp_hash = value & mask;
82 }
83
84 void
85 match_set_recirc_id(struct match *match, uint32_t value)
86 {
87     match->flow.recirc_id = value;
88     match->wc.masks.recirc_id = UINT32_MAX;
89 }
90
91 void
92 match_set_conj_id(struct match *match, uint32_t value)
93 {
94     match->flow.conj_id = value;
95     match->wc.masks.conj_id = UINT32_MAX;
96 }
97
98 void
99 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
100 {
101     match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
102 }
103
104 void
105 match_set_reg_masked(struct match *match, unsigned int reg_idx,
106                      uint32_t value, uint32_t mask)
107 {
108     ovs_assert(reg_idx < FLOW_N_REGS);
109     flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
110     match->flow.regs[reg_idx] = value & mask;
111 }
112
113 void
114 match_set_xreg(struct match *match, unsigned int xreg_idx, uint64_t value)
115 {
116     match_set_xreg_masked(match, xreg_idx, value, UINT64_MAX);
117 }
118
119 void
120 match_set_xreg_masked(struct match *match, unsigned int xreg_idx,
121                       uint64_t value, uint64_t mask)
122 {
123     ovs_assert(xreg_idx < FLOW_N_XREGS);
124     flow_wildcards_set_xreg_mask(&match->wc, xreg_idx, mask);
125     flow_set_xreg(&match->flow, xreg_idx, value & mask);
126 }
127
128 void
129 match_set_actset_output(struct match *match, ofp_port_t actset_output)
130 {
131     match->wc.masks.actset_output = u16_to_ofp(UINT16_MAX);
132     match->flow.actset_output = actset_output;
133 }
134
135 void
136 match_set_metadata(struct match *match, ovs_be64 metadata)
137 {
138     match_set_metadata_masked(match, metadata, OVS_BE64_MAX);
139 }
140
141 void
142 match_set_metadata_masked(struct match *match,
143                           ovs_be64 metadata, ovs_be64 mask)
144 {
145     match->wc.masks.metadata = mask;
146     match->flow.metadata = metadata & mask;
147 }
148
149 void
150 match_set_tun_id(struct match *match, ovs_be64 tun_id)
151 {
152     match_set_tun_id_masked(match, tun_id, OVS_BE64_MAX);
153 }
154
155 void
156 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
157 {
158     match->wc.masks.tunnel.tun_id = mask;
159     match->flow.tunnel.tun_id = tun_id & mask;
160 }
161
162 void
163 match_set_tun_src(struct match *match, ovs_be32 src)
164 {
165     match_set_tun_src_masked(match, src, OVS_BE32_MAX);
166 }
167
168 void
169 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
170 {
171     match->wc.masks.tunnel.ip_src = mask;
172     match->flow.tunnel.ip_src = src & mask;
173 }
174
175 void
176 match_set_tun_dst(struct match *match, ovs_be32 dst)
177 {
178     match_set_tun_dst_masked(match, dst, OVS_BE32_MAX);
179 }
180
181 void
182 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
183 {
184     match->wc.masks.tunnel.ip_dst = mask;
185     match->flow.tunnel.ip_dst = dst & mask;
186 }
187
188 void
189 match_set_tun_ttl(struct match *match, uint8_t ttl)
190 {
191     match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
192 }
193
194 void
195 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
196 {
197     match->wc.masks.tunnel.ip_ttl = mask;
198     match->flow.tunnel.ip_ttl = ttl & mask;
199 }
200
201 void
202 match_set_tun_tos(struct match *match, uint8_t tos)
203 {
204     match_set_tun_tos_masked(match, tos, UINT8_MAX);
205 }
206
207 void
208 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
209 {
210     match->wc.masks.tunnel.ip_tos = mask;
211     match->flow.tunnel.ip_tos = tos & mask;
212 }
213
214 void
215 match_set_tun_flags(struct match *match, uint16_t flags)
216 {
217     match_set_tun_flags_masked(match, flags, UINT16_MAX);
218 }
219
220 void
221 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
222 {
223     match->wc.masks.tunnel.flags = mask;
224     match->flow.tunnel.flags = flags & mask;
225 }
226
227 void
228 match_set_in_port(struct match *match, ofp_port_t ofp_port)
229 {
230     match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
231     match->flow.in_port.ofp_port = ofp_port;
232 }
233
234 void
235 match_set_skb_priority(struct match *match, uint32_t skb_priority)
236 {
237     match->wc.masks.skb_priority = UINT32_MAX;
238     match->flow.skb_priority = skb_priority;
239 }
240
241 void
242 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
243 {
244     match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
245 }
246
247 void
248 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
249 {
250     match->flow.pkt_mark = pkt_mark & mask;
251     match->wc.masks.pkt_mark = mask;
252 }
253
254 void
255 match_set_dl_type(struct match *match, ovs_be16 dl_type)
256 {
257     match->wc.masks.dl_type = OVS_BE16_MAX;
258     match->flow.dl_type = dl_type;
259 }
260
261 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
262  * exactly.  'mask_dst' is set to all 1s. */
263 static void
264 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
265         uint8_t value_dst[ETH_ADDR_LEN],
266         uint8_t mask_dst[ETH_ADDR_LEN])
267 {
268     memcpy(value_dst, value_src, ETH_ADDR_LEN);
269     memset(mask_dst, 0xff, ETH_ADDR_LEN);
270 }
271
272 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
273  * after each byte is ANDed with the appropriate byte in 'mask_src'.
274  * 'mask_dst' is set to 'mask_src' */
275 static void
276 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
277                const uint8_t mask_src[ETH_ADDR_LEN],
278                uint8_t value_dst[ETH_ADDR_LEN],
279                uint8_t mask_dst[ETH_ADDR_LEN])
280 {
281     size_t i;
282
283     for (i = 0; i < ETH_ADDR_LEN; i++) {
284         value_dst[i] = value_src[i] & mask_src[i];
285         mask_dst[i] = mask_src[i];
286     }
287 }
288
289 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
290  * exactly. */
291 void
292 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
293 {
294     set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
295 }
296
297 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
298  * after each byte is ANDed with the appropriate byte in 'mask'. */
299 void
300 match_set_dl_src_masked(struct match *match,
301                         const uint8_t dl_src[ETH_ADDR_LEN],
302                         const uint8_t mask[ETH_ADDR_LEN])
303 {
304     set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
305 }
306
307 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
308  * exactly. */
309 void
310 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
311 {
312     set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
313 }
314
315 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
316  * byte is ANDed with the appropriate byte in 'mask'.
317  *
318  * This function will assert-fail if 'mask' is invalid.  Only 'mask' values
319  * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
320 void
321 match_set_dl_dst_masked(struct match *match,
322                         const uint8_t dl_dst[ETH_ADDR_LEN],
323                         const uint8_t mask[ETH_ADDR_LEN])
324 {
325     set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
326 }
327
328 void
329 match_set_dl_tci(struct match *match, ovs_be16 tci)
330 {
331     match_set_dl_tci_masked(match, tci, htons(0xffff));
332 }
333
334 void
335 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
336 {
337     match->flow.vlan_tci = tci & mask;
338     match->wc.masks.vlan_tci = mask;
339 }
340
341 /* Modifies 'match' so that the VLAN VID is wildcarded.  If the PCP is already
342  * wildcarded, then 'match' will match a packet regardless of whether it has an
343  * 802.1Q header or not. */
344 void
345 match_set_any_vid(struct match *match)
346 {
347     if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
348         match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
349         match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
350     } else {
351         match_set_dl_tci_masked(match, htons(0), htons(0));
352     }
353 }
354
355 /* Modifies 'match' depending on 'dl_vlan':
356  *
357  *   - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
358  *     without an 802.1Q header.
359  *
360  *   - Otherwise, makes 'match' match only packets with an 802.1Q header whose
361  *     VID equals the low 12 bits of 'dl_vlan'.
362  */
363 void
364 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
365 {
366     flow_set_dl_vlan(&match->flow, dl_vlan);
367     if (dl_vlan == htons(OFP10_VLAN_NONE)) {
368         match->wc.masks.vlan_tci = OVS_BE16_MAX;
369     } else {
370         match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
371     }
372 }
373
374 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
375  * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
376  * plus CFI). */
377 void
378 match_set_vlan_vid(struct match *match, ovs_be16 vid)
379 {
380     match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
381 }
382
383
384 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
385  * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
386  * plus CFI), with the corresponding 'mask'. */
387 void
388 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
389 {
390     ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
391     ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
392
393     mask &= vid_mask;
394     flow_set_vlan_vid(&match->flow, vid & mask);
395     match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
396 }
397
398 /* Modifies 'match' so that the VLAN PCP is wildcarded.  If the VID is already
399  * wildcarded, then 'match' will match a packet regardless of whether it has an
400  * 802.1Q header or not. */
401 void
402 match_set_any_pcp(struct match *match)
403 {
404     if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
405         match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
406         match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
407     } else {
408         match_set_dl_tci_masked(match, htons(0), htons(0));
409     }
410 }
411
412 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
413  * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
414 void
415 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
416 {
417     flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
418     match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
419 }
420
421 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
422 void
423 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
424 {
425     match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
426     match->flow.mpls_lse[idx] = lse;
427 }
428
429 /* Modifies 'match' so that the MPLS label is wildcarded. */
430 void
431 match_set_any_mpls_label(struct match *match, int idx)
432 {
433     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
434     flow_set_mpls_label(&match->flow, idx, htonl(0));
435 }
436
437 /* Modifies 'match' so that it matches only packets with an MPLS header whose
438  * label equals the low 20 bits of 'mpls_label'. */
439 void
440 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
441 {
442     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
443     flow_set_mpls_label(&match->flow, idx, mpls_label);
444 }
445
446 /* Modifies 'match' so that the MPLS TC is wildcarded. */
447 void
448 match_set_any_mpls_tc(struct match *match, int idx)
449 {
450     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
451     flow_set_mpls_tc(&match->flow, idx, 0);
452 }
453
454 /* Modifies 'match' so that it matches only packets with an MPLS header whose
455  * Traffic Class equals the low 3 bits of 'mpls_tc'. */
456 void
457 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
458 {
459     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
460     flow_set_mpls_tc(&match->flow, idx, mpls_tc);
461 }
462
463 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
464 void
465 match_set_any_mpls_bos(struct match *match, int idx)
466 {
467     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
468     flow_set_mpls_bos(&match->flow, idx, 0);
469 }
470
471 /* Modifies 'match' so that it matches only packets with an MPLS header whose
472  * Stack Flag equals the lower bit of 'mpls_bos' */
473 void
474 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
475 {
476     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
477     flow_set_mpls_bos(&match->flow, idx, mpls_bos);
478 }
479
480 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
481 void
482 match_set_any_mpls_lse(struct match *match, int idx)
483 {
484     match->wc.masks.mpls_lse[idx] = htonl(0);
485     flow_set_mpls_lse(&match->flow, idx, htonl(0));
486 }
487
488 void
489 match_set_tp_src(struct match *match, ovs_be16 tp_src)
490 {
491     match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
492 }
493
494 void
495 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
496 {
497     match->flow.tp_src = port & mask;
498     match->wc.masks.tp_src = mask;
499 }
500
501 void
502 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
503 {
504     match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
505 }
506
507 void
508 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
509 {
510     match->flow.tp_dst = port & mask;
511     match->wc.masks.tp_dst = mask;
512 }
513
514 void
515 match_set_tcp_flags(struct match *match, ovs_be16 flags)
516 {
517     match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
518 }
519
520 void
521 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
522 {
523     match->flow.tcp_flags = flags & mask;
524     match->wc.masks.tcp_flags = mask;
525 }
526
527 void
528 match_set_nw_proto(struct match *match, uint8_t nw_proto)
529 {
530     match->flow.nw_proto = nw_proto;
531     match->wc.masks.nw_proto = UINT8_MAX;
532 }
533
534 void
535 match_set_nw_src(struct match *match, ovs_be32 nw_src)
536 {
537     match->flow.nw_src = nw_src;
538     match->wc.masks.nw_src = OVS_BE32_MAX;
539 }
540
541 void
542 match_set_nw_src_masked(struct match *match,
543                         ovs_be32 nw_src, ovs_be32 mask)
544 {
545     match->flow.nw_src = nw_src & mask;
546     match->wc.masks.nw_src = mask;
547 }
548
549 void
550 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
551 {
552     match->flow.nw_dst = nw_dst;
553     match->wc.masks.nw_dst = OVS_BE32_MAX;
554 }
555
556 void
557 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
558 {
559     match->flow.nw_dst = ip & mask;
560     match->wc.masks.nw_dst = mask;
561 }
562
563 void
564 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
565 {
566     match->wc.masks.nw_tos |= IP_DSCP_MASK;
567     match->flow.nw_tos &= ~IP_DSCP_MASK;
568     match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
569 }
570
571 void
572 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
573 {
574     match->wc.masks.nw_tos |= IP_ECN_MASK;
575     match->flow.nw_tos &= ~IP_ECN_MASK;
576     match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
577 }
578
579 void
580 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
581 {
582     match->wc.masks.nw_ttl = UINT8_MAX;
583     match->flow.nw_ttl = nw_ttl;
584 }
585
586 void
587 match_set_nw_frag(struct match *match, uint8_t nw_frag)
588 {
589     match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
590     match->flow.nw_frag = nw_frag;
591 }
592
593 void
594 match_set_nw_frag_masked(struct match *match,
595                          uint8_t nw_frag, uint8_t mask)
596 {
597     match->flow.nw_frag = nw_frag & mask;
598     match->wc.masks.nw_frag = mask;
599 }
600
601 void
602 match_set_icmp_type(struct match *match, uint8_t icmp_type)
603 {
604     match_set_tp_src(match, htons(icmp_type));
605 }
606
607 void
608 match_set_icmp_code(struct match *match, uint8_t icmp_code)
609 {
610     match_set_tp_dst(match, htons(icmp_code));
611 }
612
613 void
614 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
615 {
616     memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
617     memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
618 }
619
620 void
621 match_set_arp_sha_masked(struct match *match,
622                          const uint8_t arp_sha[ETH_ADDR_LEN],
623                          const uint8_t mask[ETH_ADDR_LEN])
624 {
625     set_eth_masked(arp_sha, mask,
626                    match->flow.arp_sha, match->wc.masks.arp_sha);
627 }
628
629 void
630 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
631 {
632     memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
633     memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
634 }
635
636 void
637 match_set_arp_tha_masked(struct match *match,
638                          const uint8_t arp_tha[ETH_ADDR_LEN],
639                          const uint8_t mask[ETH_ADDR_LEN])
640 {
641     set_eth_masked(arp_tha, mask,
642                    match->flow.arp_tha, match->wc.masks.arp_tha);
643 }
644
645 void
646 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
647 {
648     match->flow.ipv6_src = *src;
649     match->wc.masks.ipv6_src = in6addr_exact;
650 }
651
652 void
653 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
654                           const struct in6_addr *mask)
655 {
656     match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
657     match->wc.masks.ipv6_src = *mask;
658 }
659
660 void
661 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
662 {
663     match->flow.ipv6_dst = *dst;
664     match->wc.masks.ipv6_dst = in6addr_exact;
665 }
666
667 void
668 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
669                           const struct in6_addr *mask)
670 {
671     match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
672     match->wc.masks.ipv6_dst = *mask;
673 }
674
675 void
676 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
677 {
678     match->wc.masks.ipv6_label = OVS_BE32_MAX;
679     match->flow.ipv6_label = ipv6_label;
680 }
681
682
683 void
684 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
685                             ovs_be32 mask)
686 {
687     match->flow.ipv6_label = ipv6_label & mask;
688     match->wc.masks.ipv6_label = mask;
689 }
690
691 void
692 match_set_nd_target(struct match *match, const struct in6_addr *target)
693 {
694     match->flow.nd_target = *target;
695     match->wc.masks.nd_target = in6addr_exact;
696 }
697
698 void
699 match_set_nd_target_masked(struct match *match,
700                            const struct in6_addr *target,
701                            const struct in6_addr *mask)
702 {
703     match->flow.nd_target = ipv6_addr_bitand(target, mask);
704     match->wc.masks.nd_target = *mask;
705 }
706
707 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
708  * values for fixed fields, otherwise false. */
709 bool
710 match_equal(const struct match *a, const struct match *b)
711 {
712     return (flow_wildcards_equal(&a->wc, &b->wc)
713             && flow_equal(&a->flow, &b->flow));
714 }
715
716 /* Returns a hash value for the flow and wildcards in 'match', starting from
717  * 'basis'. */
718 uint32_t
719 match_hash(const struct match *match, uint32_t basis)
720 {
721     return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
722 }
723
724 static bool
725 match_has_default_recirc_id(const struct match *m)
726 {
727     return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
728                                       m->wc.masks.recirc_id == 0);
729 }
730
731 static bool
732 match_has_default_dp_hash(const struct match *m)
733 {
734     return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
735 }
736
737 /* Return true if the hidden fields of the match are set to the default values.
738  * The default values equals to those set up by match_init_hidden_fields(). */
739 bool
740 match_has_default_hidden_fields(const struct match *m)
741 {
742     return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
743 }
744
745 void
746 match_init_hidden_fields(struct match *m)
747 {
748     match_set_recirc_id(m, 0);
749     match_set_dp_hash_masked(m, 0, 0);
750 }
751
752 static void
753 format_eth_masked(struct ds *s, const char *name,
754                   const uint8_t eth[ETH_ADDR_LEN],
755                   const uint8_t mask[ETH_ADDR_LEN])
756 {
757     if (!eth_addr_is_zero(mask)) {
758         ds_put_format(s, "%s=", name);
759         eth_format_masked(eth, mask, s);
760         ds_put_char(s, ',');
761     }
762 }
763
764 static void
765 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
766                   ovs_be32 netmask)
767 {
768     if (netmask) {
769         ds_put_format(s, "%s=", name);
770         ip_format_masked(ip, netmask, s);
771         ds_put_char(s, ',');
772     }
773 }
774
775 static void
776 format_ipv6_netmask(struct ds *s, const char *name,
777                     const struct in6_addr *addr,
778                     const struct in6_addr *netmask)
779 {
780     if (!ipv6_mask_is_any(netmask)) {
781         ds_put_format(s, "%s=", name);
782         print_ipv6_masked(s, addr, netmask);
783         ds_put_char(s, ',');
784     }
785 }
786
787 static void
788 format_be16_masked(struct ds *s, const char *name,
789                    ovs_be16 value, ovs_be16 mask)
790 {
791     if (mask != htons(0)) {
792         ds_put_format(s, "%s=", name);
793         if (mask == OVS_BE16_MAX) {
794             ds_put_format(s, "%"PRIu16, ntohs(value));
795         } else {
796             ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
797                           ntohs(value), ntohs(mask));
798         }
799         ds_put_char(s, ',');
800     }
801 }
802
803 static void
804 format_be32_masked(struct ds *s, const char *name,
805                    ovs_be32 value, ovs_be32 mask)
806 {
807     if (mask != htonl(0)) {
808         ds_put_format(s, "%s=", name);
809         if (mask == OVS_BE32_MAX) {
810             ds_put_format(s, "%"PRIu32, ntohl(value));
811         } else {
812             ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
813                           ntohl(value), ntohl(mask));
814         }
815         ds_put_char(s, ',');
816     }
817 }
818
819 static void
820 format_uint32_masked(struct ds *s, const char *name,
821                    uint32_t value, uint32_t mask)
822 {
823     if (mask) {
824         ds_put_format(s, "%s=%#"PRIx32, name, value);
825         if (mask != UINT32_MAX) {
826             ds_put_format(s, "/%#"PRIx32, mask);
827         }
828         ds_put_char(s, ',');
829     }
830 }
831
832 static void
833 format_be64_masked(struct ds *s, const char *name,
834                    ovs_be64 value, ovs_be64 mask)
835 {
836     if (mask != htonll(0)) {
837         ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
838         if (mask != OVS_BE64_MAX) {
839             ds_put_format(s, "/%#"PRIx64, ntohll(mask));
840         }
841         ds_put_char(s, ',');
842     }
843 }
844
845 static void
846 format_flow_tunnel(struct ds *s, const struct match *match)
847 {
848     const struct flow_wildcards *wc = &match->wc;
849     const struct flow_tnl *tnl = &match->flow.tunnel;
850
851     format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
852     format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
853     format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
854
855     if (wc->masks.tunnel.ip_tos) {
856         ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
857     }
858     if (wc->masks.tunnel.ip_ttl) {
859         ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
860     }
861     if (wc->masks.tunnel.flags) {
862         format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
863         ds_put_char(s, ',');
864     }
865 }
866
867 /* Appends a string representation of 'match' to 's'.  If 'priority' is
868  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
869 void
870 match_format(const struct match *match, struct ds *s, int priority)
871 {
872     const struct flow_wildcards *wc = &match->wc;
873     size_t start_len = s->length;
874     const struct flow *f = &match->flow;
875     bool skip_type = false;
876     bool skip_proto = false;
877
878     int i;
879
880     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 30);
881
882     if (priority != OFP_DEFAULT_PRIORITY) {
883         ds_put_format(s, "priority=%d,", priority);
884     }
885
886     format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
887
888     if (wc->masks.recirc_id) {
889         format_uint32_masked(s, "recirc_id", f->recirc_id,
890                              wc->masks.recirc_id);
891     }
892
893     if (f->dp_hash && wc->masks.dp_hash) {
894         format_uint32_masked(s, "dp_hash", f->dp_hash,
895                              wc->masks.dp_hash);
896     }
897
898     if (wc->masks.conj_id) {
899         ds_put_format(s, "conj_id=%"PRIu32",", f->conj_id);
900     }
901
902     if (wc->masks.skb_priority) {
903         ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
904     }
905
906     if (wc->masks.actset_output) {
907         ds_put_cstr(s, "actset_output=");
908         ofputil_format_port(f->actset_output, s);
909         ds_put_char(s, ',');
910     }
911
912     if (wc->masks.dl_type) {
913         skip_type = true;
914         if (f->dl_type == htons(ETH_TYPE_IP)) {
915             if (wc->masks.nw_proto) {
916                 skip_proto = true;
917                 if (f->nw_proto == IPPROTO_ICMP) {
918                     ds_put_cstr(s, "icmp,");
919                 } else if (f->nw_proto == IPPROTO_IGMP) {
920                     ds_put_cstr(s, "igmp,");
921                 } else if (f->nw_proto == IPPROTO_TCP) {
922                     ds_put_cstr(s, "tcp,");
923                 } else if (f->nw_proto == IPPROTO_UDP) {
924                     ds_put_cstr(s, "udp,");
925                 } else if (f->nw_proto == IPPROTO_SCTP) {
926                     ds_put_cstr(s, "sctp,");
927                 } else {
928                     ds_put_cstr(s, "ip,");
929                     skip_proto = false;
930                 }
931             } else {
932                 ds_put_cstr(s, "ip,");
933             }
934         } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
935             if (wc->masks.nw_proto) {
936                 skip_proto = true;
937                 if (f->nw_proto == IPPROTO_ICMPV6) {
938                     ds_put_cstr(s, "icmp6,");
939                 } else if (f->nw_proto == IPPROTO_TCP) {
940                     ds_put_cstr(s, "tcp6,");
941                 } else if (f->nw_proto == IPPROTO_UDP) {
942                     ds_put_cstr(s, "udp6,");
943                 } else if (f->nw_proto == IPPROTO_SCTP) {
944                     ds_put_cstr(s, "sctp6,");
945                 } else {
946                     ds_put_cstr(s, "ipv6,");
947                     skip_proto = false;
948                 }
949             } else {
950                 ds_put_cstr(s, "ipv6,");
951             }
952         } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
953             ds_put_cstr(s, "arp,");
954         } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
955             ds_put_cstr(s, "rarp,");
956         } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
957             ds_put_cstr(s, "mpls,");
958         } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
959             ds_put_cstr(s, "mplsm,");
960         } else {
961             skip_type = false;
962         }
963     }
964     for (i = 0; i < FLOW_N_REGS; i++) {
965         #define REGNAME_LEN 20
966         char regname[REGNAME_LEN];
967         if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
968             strcpy(regname, "reg?");
969         }
970         format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
971     }
972
973     format_flow_tunnel(s, match);
974
975     format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
976
977     if (wc->masks.in_port.ofp_port) {
978         ds_put_cstr(s, "in_port=");
979         ofputil_format_port(f->in_port.ofp_port, s);
980         ds_put_char(s, ',');
981     }
982     if (wc->masks.vlan_tci) {
983         ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
984         ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
985         ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
986
987         if (cfi && f->vlan_tci & htons(VLAN_CFI)
988             && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
989             && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
990             && (vid_mask || pcp_mask)) {
991             if (vid_mask) {
992                 ds_put_format(s, "dl_vlan=%"PRIu16",",
993                               vlan_tci_to_vid(f->vlan_tci));
994             }
995             if (pcp_mask) {
996                 ds_put_format(s, "dl_vlan_pcp=%d,",
997                               vlan_tci_to_pcp(f->vlan_tci));
998             }
999         } else if (wc->masks.vlan_tci == htons(0xffff)) {
1000             ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
1001         } else {
1002             ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
1003                           ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
1004         }
1005     }
1006     format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1007     format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1008     if (!skip_type && wc->masks.dl_type) {
1009         ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
1010     }
1011     if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1012         format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1013         format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1014         if (wc->masks.ipv6_label) {
1015             if (wc->masks.ipv6_label == OVS_BE32_MAX) {
1016                 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
1017                               ntohl(f->ipv6_label));
1018             } else {
1019                 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
1020                               ntohl(f->ipv6_label),
1021                               ntohl(wc->masks.ipv6_label));
1022             }
1023         }
1024     } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1025                f->dl_type == htons(ETH_TYPE_RARP)) {
1026         format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1027         format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1028     } else {
1029         format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1030         format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1031     }
1032     if (!skip_proto && wc->masks.nw_proto) {
1033         if (f->dl_type == htons(ETH_TYPE_ARP) ||
1034             f->dl_type == htons(ETH_TYPE_RARP)) {
1035             ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1036         } else {
1037             ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1038         }
1039     }
1040     if (f->dl_type == htons(ETH_TYPE_ARP) ||
1041         f->dl_type == htons(ETH_TYPE_RARP)) {
1042         format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1043         format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1044     }
1045     if (wc->masks.nw_tos & IP_DSCP_MASK) {
1046         ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1047     }
1048     if (wc->masks.nw_tos & IP_ECN_MASK) {
1049         ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1050     }
1051     if (wc->masks.nw_ttl) {
1052         ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1053     }
1054     if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1055         ds_put_format(s, "mpls_label=%"PRIu32",",
1056                       mpls_lse_to_label(f->mpls_lse[0]));
1057     }
1058     if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1059         ds_put_format(s, "mpls_tc=%"PRIu8",",
1060                       mpls_lse_to_tc(f->mpls_lse[0]));
1061     }
1062     if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1063         ds_put_format(s, "mpls_ttl=%"PRIu8",",
1064                       mpls_lse_to_ttl(f->mpls_lse[0]));
1065     }
1066     if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1067         ds_put_format(s, "mpls_bos=%"PRIu8",",
1068                       mpls_lse_to_bos(f->mpls_lse[0]));
1069     }
1070     format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1071     format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1072
1073     switch (wc->masks.nw_frag) {
1074     case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1075         ds_put_format(s, "nw_frag=%s,",
1076                       f->nw_frag & FLOW_NW_FRAG_ANY
1077                       ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1078                       : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1079         break;
1080
1081     case FLOW_NW_FRAG_ANY:
1082         ds_put_format(s, "nw_frag=%s,",
1083                       f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1084         break;
1085
1086     case FLOW_NW_FRAG_LATER:
1087         ds_put_format(s, "nw_frag=%s,",
1088                       f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1089         break;
1090     }
1091     if (f->dl_type == htons(ETH_TYPE_IP) &&
1092         f->nw_proto == IPPROTO_ICMP) {
1093         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1094         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1095     } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1096                f->nw_proto == IPPROTO_IGMP) {
1097         format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1098         format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1099     } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1100                f->nw_proto == IPPROTO_ICMPV6) {
1101         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1102         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1103         format_ipv6_netmask(s, "nd_target", &f->nd_target,
1104                             &wc->masks.nd_target);
1105         format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1106         format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1107     } else {
1108         format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1109         format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1110     }
1111     if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1112         uint16_t mask = TCP_FLAGS(wc->masks.tcp_flags);
1113
1114         if (mask == TCP_FLAGS(OVS_BE16_MAX)) {
1115             ds_put_cstr(s, "tcp_flags=");
1116             if (f->tcp_flags) {
1117                 format_flags(s, packet_tcp_flag_to_string, ntohs(f->tcp_flags),
1118                              '|');
1119             } else {
1120                 ds_put_cstr(s, "0"); /* Zero flags. */
1121             }
1122         } else if (mask) {
1123             format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1124                                 ntohs(f->tcp_flags), mask);
1125         }
1126     }
1127
1128     if (s->length > start_len) {
1129         ds_chomp(s, ',');
1130     }
1131 }
1132
1133 /* Converts 'match' to a string and returns the string.  If 'priority' is
1134  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1135  * must free the string (with free()). */
1136 char *
1137 match_to_string(const struct match *match, int priority)
1138 {
1139     struct ds s = DS_EMPTY_INITIALIZER;
1140     match_format(match, &s, priority);
1141     return ds_steal_cstr(&s);
1142 }
1143
1144 void
1145 match_print(const struct match *match)
1146 {
1147     char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1148     puts(s);
1149     free(s);
1150 }
1151 \f
1152 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1153  * with minimatch_destroy(). */
1154 void
1155 minimatch_init(struct minimatch *dst, const struct match *src)
1156 {
1157     minimask_init(&dst->mask, &src->wc);
1158     miniflow_init_with_minimask(&dst->flow, &src->flow, &dst->mask);
1159 }
1160
1161 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1162  * with minimatch_destroy(). */
1163 void
1164 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1165 {
1166     miniflow_clone(&dst->flow, &src->flow);
1167     minimask_clone(&dst->mask, &src->mask);
1168 }
1169
1170 /* Initializes 'dst' with the data in 'src', destroying 'src'.  The caller must
1171  * eventually free 'dst' with minimatch_destroy(). */
1172 void
1173 minimatch_move(struct minimatch *dst, struct minimatch *src)
1174 {
1175     miniflow_move(&dst->flow, &src->flow);
1176     minimask_move(&dst->mask, &src->mask);
1177 }
1178
1179 /* Frees any memory owned by 'match'.  Does not free the storage in which
1180  * 'match' itself resides; the caller is responsible for that. */
1181 void
1182 minimatch_destroy(struct minimatch *match)
1183 {
1184     miniflow_destroy(&match->flow);
1185     minimask_destroy(&match->mask);
1186 }
1187
1188 /* Initializes 'dst' as a copy of 'src'. */
1189 void
1190 minimatch_expand(const struct minimatch *src, struct match *dst)
1191 {
1192     miniflow_expand(&src->flow, &dst->flow);
1193     minimask_expand(&src->mask, &dst->wc);
1194 }
1195
1196 /* Returns true if 'a' and 'b' match the same packets, false otherwise.  */
1197 bool
1198 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1199 {
1200     return (miniflow_equal(&a->flow, &b->flow)
1201             && minimask_equal(&a->mask, &b->mask));
1202 }
1203
1204 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1205  * 'match' specifies a particular value has the correct value in 'target'.
1206  *
1207  * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1208  * target, &match->mask) but it is faster because of the invariant that
1209  * match->flow.map and match->mask.map are the same. */
1210 bool
1211 minimatch_matches_flow(const struct minimatch *match,
1212                        const struct flow *target)
1213 {
1214     const uint64_t *target_u64 = (const uint64_t *) target;
1215     const uint64_t *flowp = miniflow_get_values(&match->flow);
1216     const uint64_t *maskp = miniflow_get_values(&match->mask.masks);
1217     int idx;
1218
1219     MAP_FOR_EACH_INDEX(idx, match->flow.map) {
1220         if ((*flowp++ ^ target_u64[idx]) & *maskp++) {
1221             return false;
1222         }
1223     }
1224
1225     return true;
1226 }
1227
1228 /* Appends a string representation of 'match' to 's'.  If 'priority' is
1229  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1230 void
1231 minimatch_format(const struct minimatch *match, struct ds *s, int priority)
1232 {
1233     struct match megamatch;
1234
1235     minimatch_expand(match, &megamatch);
1236     match_format(&megamatch, s, priority);
1237 }
1238
1239 /* Converts 'match' to a string and returns the string.  If 'priority' is
1240  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1241  * must free the string (with free()). */
1242 char *
1243 minimatch_to_string(const struct minimatch *match, int priority)
1244 {
1245     struct match megamatch;
1246
1247     minimatch_expand(match, &megamatch);
1248     return match_to_string(&megamatch, priority);
1249 }