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