d527d04314c76a150bbb350ef09f18e47ca97f8e
[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_ct_state(struct match *match, uint32_t ct_state)
289 {
290     match_set_ct_state_masked(match, ct_state, UINT32_MAX);
291 }
292
293 void
294 match_set_ct_state_masked(struct match *match, uint32_t ct_state, uint32_t mask)
295 {
296     match->flow.ct_state = ct_state & mask & UINT16_MAX;
297     match->wc.masks.ct_state = mask & UINT16_MAX;
298 }
299
300 void
301 match_set_ct_zone(struct match *match, uint16_t ct_zone)
302 {
303     match->flow.ct_zone = ct_zone;
304     match->wc.masks.ct_zone = UINT16_MAX;
305 }
306
307 void
308 match_set_dl_type(struct match *match, ovs_be16 dl_type)
309 {
310     match->wc.masks.dl_type = OVS_BE16_MAX;
311     match->flow.dl_type = dl_type;
312 }
313
314 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
315  * exactly.  'mask_dst' is set to all 1s. */
316 static void
317 set_eth(const struct eth_addr value_src,
318         struct eth_addr *value_dst,
319         struct eth_addr *mask_dst)
320 {
321     *value_dst = value_src;
322     *mask_dst = eth_addr_exact;
323 }
324
325 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
326  * after each byte is ANDed with the appropriate byte in 'mask_src'.
327  * 'mask_dst' is set to 'mask_src' */
328 static void
329 set_eth_masked(const struct eth_addr value_src,
330                const struct eth_addr mask_src,
331                struct eth_addr *value_dst, struct eth_addr *mask_dst)
332 {
333     size_t i;
334
335     for (i = 0; i < ARRAY_SIZE(value_dst->be16); i++) {
336         value_dst->be16[i] = value_src.be16[i] & mask_src.be16[i];
337     }
338     *mask_dst = mask_src;
339 }
340
341 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
342  * exactly. */
343 void
344 match_set_dl_src(struct match *match, const struct eth_addr dl_src)
345 {
346     set_eth(dl_src, &match->flow.dl_src, &match->wc.masks.dl_src);
347 }
348
349 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
350  * after each byte is ANDed with the appropriate byte in 'mask'. */
351 void
352 match_set_dl_src_masked(struct match *match,
353                         const struct eth_addr dl_src,
354                         const struct eth_addr mask)
355 {
356     set_eth_masked(dl_src, mask, &match->flow.dl_src, &match->wc.masks.dl_src);
357 }
358
359 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
360  * exactly. */
361 void
362 match_set_dl_dst(struct match *match, const struct eth_addr dl_dst)
363 {
364     set_eth(dl_dst, &match->flow.dl_dst, &match->wc.masks.dl_dst);
365 }
366
367 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
368  * byte is ANDed with the appropriate byte in 'mask'.
369  *
370  * This function will assert-fail if 'mask' is invalid.  Only 'mask' values
371  * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
372 void
373 match_set_dl_dst_masked(struct match *match,
374                         const struct eth_addr dl_dst,
375                         const struct eth_addr mask)
376 {
377     set_eth_masked(dl_dst, mask, &match->flow.dl_dst, &match->wc.masks.dl_dst);
378 }
379
380 void
381 match_set_dl_tci(struct match *match, ovs_be16 tci)
382 {
383     match_set_dl_tci_masked(match, tci, htons(0xffff));
384 }
385
386 void
387 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
388 {
389     match->flow.vlan_tci = tci & mask;
390     match->wc.masks.vlan_tci = mask;
391 }
392
393 /* Modifies 'match' so that the VLAN VID is wildcarded.  If the PCP is already
394  * wildcarded, then 'match' will match a packet regardless of whether it has an
395  * 802.1Q header or not. */
396 void
397 match_set_any_vid(struct match *match)
398 {
399     if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
400         match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
401         match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
402     } else {
403         match_set_dl_tci_masked(match, htons(0), htons(0));
404     }
405 }
406
407 /* Modifies 'match' depending on 'dl_vlan':
408  *
409  *   - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
410  *     without an 802.1Q header.
411  *
412  *   - Otherwise, makes 'match' match only packets with an 802.1Q header whose
413  *     VID equals the low 12 bits of 'dl_vlan'.
414  */
415 void
416 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
417 {
418     flow_set_dl_vlan(&match->flow, dl_vlan);
419     if (dl_vlan == htons(OFP10_VLAN_NONE)) {
420         match->wc.masks.vlan_tci = OVS_BE16_MAX;
421     } else {
422         match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
423     }
424 }
425
426 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
427  * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
428  * plus CFI). */
429 void
430 match_set_vlan_vid(struct match *match, ovs_be16 vid)
431 {
432     match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
433 }
434
435
436 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
437  * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
438  * plus CFI), with the corresponding 'mask'. */
439 void
440 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
441 {
442     ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
443     ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
444
445     mask &= vid_mask;
446     flow_set_vlan_vid(&match->flow, vid & mask);
447     match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
448 }
449
450 /* Modifies 'match' so that the VLAN PCP is wildcarded.  If the VID is already
451  * wildcarded, then 'match' will match a packet regardless of whether it has an
452  * 802.1Q header or not. */
453 void
454 match_set_any_pcp(struct match *match)
455 {
456     if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
457         match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
458         match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
459     } else {
460         match_set_dl_tci_masked(match, htons(0), htons(0));
461     }
462 }
463
464 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
465  * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
466 void
467 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
468 {
469     flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
470     match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
471 }
472
473 /* Modifies 'match' so that the MPLS label 'idx' matches 'lse' exactly. */
474 void
475 match_set_mpls_lse(struct match *match, int idx, ovs_be32 lse)
476 {
477     match->wc.masks.mpls_lse[idx] = OVS_BE32_MAX;
478     match->flow.mpls_lse[idx] = lse;
479 }
480
481 /* Modifies 'match' so that the MPLS label is wildcarded. */
482 void
483 match_set_any_mpls_label(struct match *match, int idx)
484 {
485     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_LABEL_MASK);
486     flow_set_mpls_label(&match->flow, idx, htonl(0));
487 }
488
489 /* Modifies 'match' so that it matches only packets with an MPLS header whose
490  * label equals the low 20 bits of 'mpls_label'. */
491 void
492 match_set_mpls_label(struct match *match, int idx, ovs_be32 mpls_label)
493 {
494     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_LABEL_MASK);
495     flow_set_mpls_label(&match->flow, idx, mpls_label);
496 }
497
498 /* Modifies 'match' so that the MPLS TC is wildcarded. */
499 void
500 match_set_any_mpls_tc(struct match *match, int idx)
501 {
502     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_TC_MASK);
503     flow_set_mpls_tc(&match->flow, idx, 0);
504 }
505
506 /* Modifies 'match' so that it matches only packets with an MPLS header whose
507  * Traffic Class equals the low 3 bits of 'mpls_tc'. */
508 void
509 match_set_mpls_tc(struct match *match, int idx, uint8_t mpls_tc)
510 {
511     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_TC_MASK);
512     flow_set_mpls_tc(&match->flow, idx, mpls_tc);
513 }
514
515 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
516 void
517 match_set_any_mpls_bos(struct match *match, int idx)
518 {
519     match->wc.masks.mpls_lse[idx] &= ~htonl(MPLS_BOS_MASK);
520     flow_set_mpls_bos(&match->flow, idx, 0);
521 }
522
523 /* Modifies 'match' so that it matches only packets with an MPLS header whose
524  * Stack Flag equals the lower bit of 'mpls_bos' */
525 void
526 match_set_mpls_bos(struct match *match, int idx, uint8_t mpls_bos)
527 {
528     match->wc.masks.mpls_lse[idx] |= htonl(MPLS_BOS_MASK);
529     flow_set_mpls_bos(&match->flow, idx, mpls_bos);
530 }
531
532 /* Modifies 'match' so that the MPLS LSE is wildcarded. */
533 void
534 match_set_any_mpls_lse(struct match *match, int idx)
535 {
536     match->wc.masks.mpls_lse[idx] = htonl(0);
537     flow_set_mpls_lse(&match->flow, idx, htonl(0));
538 }
539
540 void
541 match_set_tp_src(struct match *match, ovs_be16 tp_src)
542 {
543     match_set_tp_src_masked(match, tp_src, OVS_BE16_MAX);
544 }
545
546 void
547 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
548 {
549     match->flow.tp_src = port & mask;
550     match->wc.masks.tp_src = mask;
551 }
552
553 void
554 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
555 {
556     match_set_tp_dst_masked(match, tp_dst, OVS_BE16_MAX);
557 }
558
559 void
560 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
561 {
562     match->flow.tp_dst = port & mask;
563     match->wc.masks.tp_dst = mask;
564 }
565
566 void
567 match_set_tcp_flags(struct match *match, ovs_be16 flags)
568 {
569     match_set_tcp_flags_masked(match, flags, OVS_BE16_MAX);
570 }
571
572 void
573 match_set_tcp_flags_masked(struct match *match, ovs_be16 flags, ovs_be16 mask)
574 {
575     match->flow.tcp_flags = flags & mask;
576     match->wc.masks.tcp_flags = mask;
577 }
578
579 void
580 match_set_nw_proto(struct match *match, uint8_t nw_proto)
581 {
582     match->flow.nw_proto = nw_proto;
583     match->wc.masks.nw_proto = UINT8_MAX;
584 }
585
586 void
587 match_set_nw_src(struct match *match, ovs_be32 nw_src)
588 {
589     match->flow.nw_src = nw_src;
590     match->wc.masks.nw_src = OVS_BE32_MAX;
591 }
592
593 void
594 match_set_nw_src_masked(struct match *match,
595                         ovs_be32 nw_src, ovs_be32 mask)
596 {
597     match->flow.nw_src = nw_src & mask;
598     match->wc.masks.nw_src = mask;
599 }
600
601 void
602 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
603 {
604     match->flow.nw_dst = nw_dst;
605     match->wc.masks.nw_dst = OVS_BE32_MAX;
606 }
607
608 void
609 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
610 {
611     match->flow.nw_dst = ip & mask;
612     match->wc.masks.nw_dst = mask;
613 }
614
615 void
616 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
617 {
618     match->wc.masks.nw_tos |= IP_DSCP_MASK;
619     match->flow.nw_tos &= ~IP_DSCP_MASK;
620     match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
621 }
622
623 void
624 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
625 {
626     match->wc.masks.nw_tos |= IP_ECN_MASK;
627     match->flow.nw_tos &= ~IP_ECN_MASK;
628     match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
629 }
630
631 void
632 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
633 {
634     match->wc.masks.nw_ttl = UINT8_MAX;
635     match->flow.nw_ttl = nw_ttl;
636 }
637
638 void
639 match_set_nw_frag(struct match *match, uint8_t nw_frag)
640 {
641     match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
642     match->flow.nw_frag = nw_frag;
643 }
644
645 void
646 match_set_nw_frag_masked(struct match *match,
647                          uint8_t nw_frag, uint8_t mask)
648 {
649     match->flow.nw_frag = nw_frag & mask;
650     match->wc.masks.nw_frag = mask;
651 }
652
653 void
654 match_set_icmp_type(struct match *match, uint8_t icmp_type)
655 {
656     match_set_tp_src(match, htons(icmp_type));
657 }
658
659 void
660 match_set_icmp_code(struct match *match, uint8_t icmp_code)
661 {
662     match_set_tp_dst(match, htons(icmp_code));
663 }
664
665 void
666 match_set_arp_sha(struct match *match, const struct eth_addr sha)
667 {
668     match->flow.arp_sha = sha;
669     match->wc.masks.arp_sha = eth_addr_exact;
670 }
671
672 void
673 match_set_arp_sha_masked(struct match *match,
674                          const struct eth_addr arp_sha,
675                          const struct eth_addr mask)
676 {
677     set_eth_masked(arp_sha, mask,
678                    &match->flow.arp_sha, &match->wc.masks.arp_sha);
679 }
680
681 void
682 match_set_arp_tha(struct match *match, const struct eth_addr tha)
683 {
684     match->flow.arp_tha = tha;
685     match->wc.masks.arp_tha = eth_addr_exact;
686 }
687
688 void
689 match_set_arp_tha_masked(struct match *match,
690                          const struct eth_addr arp_tha,
691                          const struct eth_addr mask)
692 {
693     set_eth_masked(arp_tha, mask,
694                    &match->flow.arp_tha, &match->wc.masks.arp_tha);
695 }
696
697 void
698 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
699 {
700     match->flow.ipv6_src = *src;
701     match->wc.masks.ipv6_src = in6addr_exact;
702 }
703
704 void
705 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
706                           const struct in6_addr *mask)
707 {
708     match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
709     match->wc.masks.ipv6_src = *mask;
710 }
711
712 void
713 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
714 {
715     match->flow.ipv6_dst = *dst;
716     match->wc.masks.ipv6_dst = in6addr_exact;
717 }
718
719 void
720 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
721                           const struct in6_addr *mask)
722 {
723     match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
724     match->wc.masks.ipv6_dst = *mask;
725 }
726
727 void
728 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
729 {
730     match->wc.masks.ipv6_label = OVS_BE32_MAX;
731     match->flow.ipv6_label = ipv6_label;
732 }
733
734
735 void
736 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
737                             ovs_be32 mask)
738 {
739     match->flow.ipv6_label = ipv6_label & mask;
740     match->wc.masks.ipv6_label = mask;
741 }
742
743 void
744 match_set_nd_target(struct match *match, const struct in6_addr *target)
745 {
746     match->flow.nd_target = *target;
747     match->wc.masks.nd_target = in6addr_exact;
748 }
749
750 void
751 match_set_nd_target_masked(struct match *match,
752                            const struct in6_addr *target,
753                            const struct in6_addr *mask)
754 {
755     match->flow.nd_target = ipv6_addr_bitand(target, mask);
756     match->wc.masks.nd_target = *mask;
757 }
758
759 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
760  * values for fixed fields, otherwise false. */
761 bool
762 match_equal(const struct match *a, const struct match *b)
763 {
764     return (flow_wildcards_equal(&a->wc, &b->wc)
765             && flow_equal(&a->flow, &b->flow));
766 }
767
768 /* Returns a hash value for the flow and wildcards in 'match', starting from
769  * 'basis'. */
770 uint32_t
771 match_hash(const struct match *match, uint32_t basis)
772 {
773     return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
774 }
775
776 static bool
777 match_has_default_recirc_id(const struct match *m)
778 {
779     return m->flow.recirc_id == 0 && (m->wc.masks.recirc_id == UINT32_MAX ||
780                                       m->wc.masks.recirc_id == 0);
781 }
782
783 static bool
784 match_has_default_dp_hash(const struct match *m)
785 {
786     return ((m->flow.dp_hash | m->wc.masks.dp_hash) == 0);
787 }
788
789 /* Return true if the hidden fields of the match are set to the default values.
790  * The default values equals to those set up by match_init_hidden_fields(). */
791 bool
792 match_has_default_hidden_fields(const struct match *m)
793 {
794     return match_has_default_recirc_id(m) && match_has_default_dp_hash(m);
795 }
796
797 void
798 match_init_hidden_fields(struct match *m)
799 {
800     match_set_recirc_id(m, 0);
801     match_set_dp_hash_masked(m, 0, 0);
802 }
803
804 static void
805 format_eth_masked(struct ds *s, const char *name,
806                   const struct eth_addr eth, const struct eth_addr mask)
807 {
808     if (!eth_addr_is_zero(mask)) {
809         ds_put_format(s, "%s=", name);
810         eth_format_masked(eth, &mask, s);
811         ds_put_char(s, ',');
812     }
813 }
814
815 static void
816 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
817                   ovs_be32 netmask)
818 {
819     if (netmask) {
820         ds_put_format(s, "%s=", name);
821         ip_format_masked(ip, netmask, s);
822         ds_put_char(s, ',');
823     }
824 }
825
826 static void
827 format_ipv6_netmask(struct ds *s, const char *name,
828                     const struct in6_addr *addr,
829                     const struct in6_addr *netmask)
830 {
831     if (!ipv6_mask_is_any(netmask)) {
832         ds_put_format(s, "%s=", name);
833         print_ipv6_masked(s, addr, netmask);
834         ds_put_char(s, ',');
835     }
836 }
837
838 static void
839 format_uint16_masked(struct ds *s, const char *name,
840                    uint16_t value, uint16_t mask)
841 {
842     if (mask != 0) {
843         ds_put_format(s, "%s=", name);
844         if (mask == UINT16_MAX) {
845             ds_put_format(s, "%"PRIu16, value);
846         } else {
847             ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16, value, mask);
848         }
849         ds_put_char(s, ',');
850     }
851 }
852
853 static void
854 format_be16_masked(struct ds *s, const char *name,
855                    ovs_be16 value, ovs_be16 mask)
856 {
857     if (mask != htons(0)) {
858         ds_put_format(s, "%s=", name);
859         if (mask == OVS_BE16_MAX) {
860             ds_put_format(s, "%"PRIu16, ntohs(value));
861         } else {
862             ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
863                           ntohs(value), ntohs(mask));
864         }
865         ds_put_char(s, ',');
866     }
867 }
868
869 static void
870 format_be32_masked(struct ds *s, const char *name,
871                    ovs_be32 value, ovs_be32 mask)
872 {
873     if (mask != htonl(0)) {
874         ds_put_format(s, "%s=", name);
875         if (mask == OVS_BE32_MAX) {
876             ds_put_format(s, "%"PRIu32, ntohl(value));
877         } else {
878             ds_put_format(s, "0x%"PRIx32"/0x%"PRIx32,
879                           ntohl(value), ntohl(mask));
880         }
881         ds_put_char(s, ',');
882     }
883 }
884
885 static void
886 format_uint32_masked(struct ds *s, const char *name,
887                    uint32_t value, uint32_t mask)
888 {
889     if (mask) {
890         ds_put_format(s, "%s=%#"PRIx32, name, value);
891         if (mask != UINT32_MAX) {
892             ds_put_format(s, "/%#"PRIx32, mask);
893         }
894         ds_put_char(s, ',');
895     }
896 }
897
898 static void
899 format_be64_masked(struct ds *s, const char *name,
900                    ovs_be64 value, ovs_be64 mask)
901 {
902     if (mask != htonll(0)) {
903         ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
904         if (mask != OVS_BE64_MAX) {
905             ds_put_format(s, "/%#"PRIx64, ntohll(mask));
906         }
907         ds_put_char(s, ',');
908     }
909 }
910
911 static void
912 format_flow_tunnel(struct ds *s, const struct match *match)
913 {
914     const struct flow_wildcards *wc = &match->wc;
915     const struct flow_tnl *tnl = &match->flow.tunnel;
916
917     format_be64_masked(s, "tun_id", tnl->tun_id, wc->masks.tunnel.tun_id);
918     format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
919     format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
920
921     if (wc->masks.tunnel.gbp_id) {
922         format_be16_masked(s, "tun_gbp_id", tnl->gbp_id,
923                            wc->masks.tunnel.gbp_id);
924     }
925
926     if (wc->masks.tunnel.gbp_flags) {
927         ds_put_format(s, "tun_gbp_flags=%#"PRIx8",", tnl->gbp_flags);
928     }
929
930     if (wc->masks.tunnel.ip_tos) {
931         ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
932     }
933     if (wc->masks.tunnel.ip_ttl) {
934         ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
935     }
936     if (wc->masks.tunnel.flags) {
937         format_flags_masked(s, "tun_flags", flow_tun_flag_to_string,
938                             tnl->flags,
939                             wc->masks.tunnel.flags & FLOW_TNL_F_MASK,
940                             FLOW_TNL_F_MASK);
941         ds_put_char(s, ',');
942     }
943     tun_metadata_match_format(s, match);
944 }
945
946 /* Appends a string representation of 'match' to 's'.  If 'priority' is
947  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
948 void
949 match_format(const struct match *match, struct ds *s, int priority)
950 {
951     const struct flow_wildcards *wc = &match->wc;
952     size_t start_len = s->length;
953     const struct flow *f = &match->flow;
954     bool skip_type = false;
955     bool skip_proto = false;
956
957     int i;
958
959     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 34);
960
961     if (priority != OFP_DEFAULT_PRIORITY) {
962         ds_put_format(s, "priority=%d,", priority);
963     }
964
965     format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
966
967     if (wc->masks.recirc_id) {
968         format_uint32_masked(s, "recirc_id", f->recirc_id,
969                              wc->masks.recirc_id);
970     }
971
972     if (wc->masks.dp_hash) {
973         format_uint32_masked(s, "dp_hash", f->dp_hash,
974                              wc->masks.dp_hash);
975     }
976
977     if (wc->masks.conj_id) {
978         ds_put_format(s, "conj_id=%"PRIu32",", f->conj_id);
979     }
980
981     if (wc->masks.skb_priority) {
982         ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
983     }
984
985     if (wc->masks.actset_output) {
986         ds_put_cstr(s, "actset_output=");
987         ofputil_format_port(f->actset_output, s);
988         ds_put_char(s, ',');
989     }
990
991     if (wc->masks.ct_state) {
992         if (wc->masks.ct_state == UINT16_MAX) {
993             ds_put_cstr(s, "ct_state=");
994             if (f->ct_state) {
995                 format_flags(s, ct_state_to_string, f->ct_state, '|');
996             } else {
997                 ds_put_cstr(s, "0"); /* No state. */
998             }
999         } else {
1000             format_flags_masked(s, "ct_state", ct_state_to_string,
1001                                 f->ct_state, wc->masks.ct_state, UINT16_MAX);
1002         }
1003         ds_put_char(s, ',');
1004     }
1005
1006     if (wc->masks.ct_zone) {
1007         format_uint16_masked(s, "ct_zone", f->ct_zone, wc->masks.ct_zone);
1008     }
1009
1010     if (wc->masks.dl_type) {
1011         skip_type = true;
1012         if (f->dl_type == htons(ETH_TYPE_IP)) {
1013             if (wc->masks.nw_proto) {
1014                 skip_proto = true;
1015                 if (f->nw_proto == IPPROTO_ICMP) {
1016                     ds_put_cstr(s, "icmp,");
1017                 } else if (f->nw_proto == IPPROTO_IGMP) {
1018                     ds_put_cstr(s, "igmp,");
1019                 } else if (f->nw_proto == IPPROTO_TCP) {
1020                     ds_put_cstr(s, "tcp,");
1021                 } else if (f->nw_proto == IPPROTO_UDP) {
1022                     ds_put_cstr(s, "udp,");
1023                 } else if (f->nw_proto == IPPROTO_SCTP) {
1024                     ds_put_cstr(s, "sctp,");
1025                 } else {
1026                     ds_put_cstr(s, "ip,");
1027                     skip_proto = false;
1028                 }
1029             } else {
1030                 ds_put_cstr(s, "ip,");
1031             }
1032         } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1033             if (wc->masks.nw_proto) {
1034                 skip_proto = true;
1035                 if (f->nw_proto == IPPROTO_ICMPV6) {
1036                     ds_put_cstr(s, "icmp6,");
1037                 } else if (f->nw_proto == IPPROTO_TCP) {
1038                     ds_put_cstr(s, "tcp6,");
1039                 } else if (f->nw_proto == IPPROTO_UDP) {
1040                     ds_put_cstr(s, "udp6,");
1041                 } else if (f->nw_proto == IPPROTO_SCTP) {
1042                     ds_put_cstr(s, "sctp6,");
1043                 } else {
1044                     ds_put_cstr(s, "ipv6,");
1045                     skip_proto = false;
1046                 }
1047             } else {
1048                 ds_put_cstr(s, "ipv6,");
1049             }
1050         } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
1051             ds_put_cstr(s, "arp,");
1052         } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
1053             ds_put_cstr(s, "rarp,");
1054         } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
1055             ds_put_cstr(s, "mpls,");
1056         } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
1057             ds_put_cstr(s, "mplsm,");
1058         } else {
1059             skip_type = false;
1060         }
1061     }
1062     for (i = 0; i < FLOW_N_REGS; i++) {
1063         #define REGNAME_LEN 20
1064         char regname[REGNAME_LEN];
1065         if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) {
1066             strcpy(regname, "reg?");
1067         }
1068         format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]);
1069     }
1070
1071     format_flow_tunnel(s, match);
1072
1073     format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);
1074
1075     if (wc->masks.in_port.ofp_port) {
1076         ds_put_cstr(s, "in_port=");
1077         ofputil_format_port(f->in_port.ofp_port, s);
1078         ds_put_char(s, ',');
1079     }
1080     if (wc->masks.vlan_tci) {
1081         ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
1082         ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
1083         ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
1084
1085         if (cfi && f->vlan_tci & htons(VLAN_CFI)
1086             && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
1087             && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
1088             && (vid_mask || pcp_mask)) {
1089             if (vid_mask) {
1090                 ds_put_format(s, "dl_vlan=%"PRIu16",",
1091                               vlan_tci_to_vid(f->vlan_tci));
1092             }
1093             if (pcp_mask) {
1094                 ds_put_format(s, "dl_vlan_pcp=%d,",
1095                               vlan_tci_to_pcp(f->vlan_tci));
1096             }
1097         } else if (wc->masks.vlan_tci == htons(0xffff)) {
1098             ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
1099         } else {
1100             ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
1101                           ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
1102         }
1103     }
1104     format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
1105     format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
1106     if (!skip_type && wc->masks.dl_type) {
1107         ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
1108     }
1109     if (f->dl_type == htons(ETH_TYPE_IPV6)) {
1110         format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
1111         format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
1112         if (wc->masks.ipv6_label) {
1113             if (wc->masks.ipv6_label == OVS_BE32_MAX) {
1114                 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
1115                               ntohl(f->ipv6_label));
1116             } else {
1117                 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
1118                               ntohl(f->ipv6_label),
1119                               ntohl(wc->masks.ipv6_label));
1120             }
1121         }
1122     } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
1123                f->dl_type == htons(ETH_TYPE_RARP)) {
1124         format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
1125         format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
1126     } else {
1127         format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
1128         format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
1129     }
1130     if (!skip_proto && wc->masks.nw_proto) {
1131         if (f->dl_type == htons(ETH_TYPE_ARP) ||
1132             f->dl_type == htons(ETH_TYPE_RARP)) {
1133             ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
1134         } else {
1135             ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
1136         }
1137     }
1138     if (f->dl_type == htons(ETH_TYPE_ARP) ||
1139         f->dl_type == htons(ETH_TYPE_RARP)) {
1140         format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1141         format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1142     }
1143     if (wc->masks.nw_tos & IP_DSCP_MASK) {
1144         ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1145     }
1146     if (wc->masks.nw_tos & IP_ECN_MASK) {
1147         ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1148     }
1149     if (wc->masks.nw_ttl) {
1150         ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1151     }
1152     if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
1153         ds_put_format(s, "mpls_label=%"PRIu32",",
1154                       mpls_lse_to_label(f->mpls_lse[0]));
1155     }
1156     if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
1157         ds_put_format(s, "mpls_tc=%"PRIu8",",
1158                       mpls_lse_to_tc(f->mpls_lse[0]));
1159     }
1160     if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
1161         ds_put_format(s, "mpls_ttl=%"PRIu8",",
1162                       mpls_lse_to_ttl(f->mpls_lse[0]));
1163     }
1164     if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
1165         ds_put_format(s, "mpls_bos=%"PRIu8",",
1166                       mpls_lse_to_bos(f->mpls_lse[0]));
1167     }
1168     format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
1169     format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);
1170
1171     switch (wc->masks.nw_frag) {
1172     case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1173         ds_put_format(s, "nw_frag=%s,",
1174                       f->nw_frag & FLOW_NW_FRAG_ANY
1175                       ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1176                       : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1177         break;
1178
1179     case FLOW_NW_FRAG_ANY:
1180         ds_put_format(s, "nw_frag=%s,",
1181                       f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1182         break;
1183
1184     case FLOW_NW_FRAG_LATER:
1185         ds_put_format(s, "nw_frag=%s,",
1186                       f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1187         break;
1188     }
1189     if (f->dl_type == htons(ETH_TYPE_IP) &&
1190         f->nw_proto == IPPROTO_ICMP) {
1191         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1192         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1193     } else if (f->dl_type == htons(ETH_TYPE_IP) &&
1194                f->nw_proto == IPPROTO_IGMP) {
1195         format_be16_masked(s, "igmp_type", f->tp_src, wc->masks.tp_src);
1196         format_be16_masked(s, "igmp_code", f->tp_dst, wc->masks.tp_dst);
1197     } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1198                f->nw_proto == IPPROTO_ICMPV6) {
1199         format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1200         format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1201         format_ipv6_netmask(s, "nd_target", &f->nd_target,
1202                             &wc->masks.nd_target);
1203         format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1204         format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1205     } else {
1206         format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1207         format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1208     }
1209     if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) {
1210         format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string,
1211                             ntohs(f->tcp_flags), TCP_FLAGS(wc->masks.tcp_flags),
1212                             TCP_FLAGS(OVS_BE16_MAX));
1213     }
1214
1215     if (s->length > start_len) {
1216         ds_chomp(s, ',');
1217     }
1218 }
1219
1220 /* Converts 'match' to a string and returns the string.  If 'priority' is
1221  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1222  * must free the string (with free()). */
1223 char *
1224 match_to_string(const struct match *match, int priority)
1225 {
1226     struct ds s = DS_EMPTY_INITIALIZER;
1227     match_format(match, &s, priority);
1228     return ds_steal_cstr(&s);
1229 }
1230
1231 void
1232 match_print(const struct match *match)
1233 {
1234     char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1235     puts(s);
1236     free(s);
1237 }
1238 \f
1239 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1240  * with minimatch_destroy(). */
1241 void
1242 minimatch_init(struct minimatch *dst, const struct match *src)
1243 {
1244     struct miniflow tmp;
1245
1246     miniflow_map_init(&tmp, &src->wc.masks);
1247     /* Allocate two consecutive miniflows. */
1248     miniflow_alloc(dst->flows, 2, &tmp);
1249     miniflow_init(dst->flow, &src->flow);
1250     minimask_init(dst->mask, &src->wc);
1251 }
1252
1253 /* Initializes 'dst' as a copy of 'src'.  The caller must eventually free 'dst'
1254  * with minimatch_destroy(). */
1255 void
1256 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1257 {
1258     /* Allocate two consecutive miniflows. */
1259     size_t data_size = miniflow_alloc(dst->flows, 2, &src->mask->masks);
1260
1261     memcpy(miniflow_values(dst->flow),
1262            miniflow_get_values(src->flow), data_size);
1263     memcpy(miniflow_values(&dst->mask->masks),
1264            miniflow_get_values(&src->mask->masks), data_size);
1265 }
1266
1267 /* Initializes 'dst' with the data in 'src', destroying 'src'.  The caller must
1268  * eventually free 'dst' with minimatch_destroy(). */
1269 void
1270 minimatch_move(struct minimatch *dst, struct minimatch *src)
1271 {
1272     dst->flow = src->flow;
1273     dst->mask = src->mask;
1274 }
1275
1276 /* Frees any memory owned by 'match'.  Does not free the storage in which
1277  * 'match' itself resides; the caller is responsible for that. */
1278 void
1279 minimatch_destroy(struct minimatch *match)
1280 {
1281     free(match->flow);
1282 }
1283
1284 /* Initializes 'dst' as a copy of 'src'. */
1285 void
1286 minimatch_expand(const struct minimatch *src, struct match *dst)
1287 {
1288     miniflow_expand(src->flow, &dst->flow);
1289     minimask_expand(src->mask, &dst->wc);
1290     memset(&dst->tun_md, 0, sizeof dst->tun_md);
1291 }
1292
1293 /* Returns true if 'a' and 'b' match the same packets, false otherwise.  */
1294 bool
1295 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1296 {
1297     return minimask_equal(a->mask, b->mask)
1298         && miniflow_equal(a->flow, b->flow);
1299 }
1300
1301 /* Returns true if 'target' satisifies 'match', that is, if each bit for which
1302  * 'match' specifies a particular value has the correct value in 'target'.
1303  *
1304  * This function is equivalent to miniflow_equal_flow_in_minimask(&match->flow,
1305  * target, &match->mask) but it is faster because of the invariant that
1306  * match->flow.map and match->mask.map are the same. */
1307 bool
1308 minimatch_matches_flow(const struct minimatch *match,
1309                        const struct flow *target)
1310 {
1311     const uint64_t *flowp = miniflow_get_values(match->flow);
1312     const uint64_t *maskp = miniflow_get_values(&match->mask->masks);
1313     size_t idx;
1314
1315     FLOWMAP_FOR_EACH_INDEX(idx, match->flow->map) {
1316         if ((*flowp++ ^ flow_u64_value(target, idx)) & *maskp++) {
1317             return false;
1318         }
1319     }
1320
1321     return true;
1322 }
1323
1324 /* Appends a string representation of 'match' to 's'.  If 'priority' is
1325  * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1326 void
1327 minimatch_format(const struct minimatch *match, struct ds *s, int priority)
1328 {
1329     struct match megamatch;
1330
1331     minimatch_expand(match, &megamatch);
1332     match_format(&megamatch, s, priority);
1333 }
1334
1335 /* Converts 'match' to a string and returns the string.  If 'priority' is
1336  * different from OFP_DEFAULT_PRIORITY, includes it in the string.  The caller
1337  * must free the string (with free()). */
1338 char *
1339 minimatch_to_string(const struct minimatch *match, int priority)
1340 {
1341     struct match megamatch;
1342
1343     minimatch_expand(match, &megamatch);
1344     return match_to_string(&megamatch, priority);
1345 }