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