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