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