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