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