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